*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread t
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-nil, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [faq 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer)
731     (faq . gnus-faq-buffer))
732   "Mapping from short symbols to buffer names or buffer variables.")
733
734 (defvar gnus-carpal nil
735   "*If non-nil, display clickable icons.")
736
737 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
738   "*Function called with a group name when new group is detected.
739 A few pre-made functions are supplied: `gnus-subscribe-randomly'
740 inserts new groups at the beginning of the list of groups;
741 `gnus-subscribe-alphabetically' inserts new groups in strict
742 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
743 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
744 for your decision.")
745
746 ;; Suggested by a bug report by Hallvard B Furuseth.
747 ;; <h.b.furuseth@usit.uio.no>. 
748 (defvar gnus-subscribe-options-newsgroup-method
749   (function gnus-subscribe-alphabetically)
750   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
751 If, for instance, you want to subscribe to all newsgroups in the
752 \"no\" and \"alt\" hierarchies, you'd put the following in your
753 .newsrc file:
754
755 options -n no.all alt.all
756
757 Gnus will the subscribe all new newsgroups in these hierarchies with
758 the subscription method in this variable.")
759
760 (defvar gnus-subscribe-hierarchical-interactive nil
761   "*If non-nil, Gnus will offer to subscribe hierarchically.
762 When a new hierarchy appears, Gnus will ask the user:
763
764 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
765
766 If the user pressed `d', Gnus will descend the hierarchy, `y' will
767 subscribe to all newsgroups in the hierarchy and `s' will skip this
768 hierarchy in its entirety.")
769
770 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
771   "*Function used for sorting the group buffer.
772 This function will be called with group info entries as the arguments
773 for the groups to be sorted.  Pre-made functions include
774 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
775 `gnus-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095
1096 (defvar gnus-article-x-face-command
1097   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1098   "String or function to be executed to display an X-Face header.
1099 If it is a string, the command will be executed in a sub-shell
1100 asynchronously. The compressed face will be piped to this command.") 
1101
1102 (defvar gnus-article-x-face-too-ugly nil
1103   "Regexp matching posters whose face shouldn't be shown automatically.")
1104
1105 (defvar gnus-select-group-hook nil
1106   "*A hook called when a newsgroup is selected.
1107
1108 If you'd like to simplify subjects like the
1109 `gnus-summary-next-same-subject' command does, you can use the
1110 following hook:
1111
1112  (setq gnus-select-group-hook
1113       (list
1114         (lambda ()
1115           (mapcar (lambda (header)
1116                      (header-set-subject
1117                       header
1118                       (gnus-simplify-subject
1119                        (header-subject header) 're-only)))
1120                   gnus-newsgroup-headers))))")
1121
1122 (defvar gnus-select-article-hook
1123   '(gnus-summary-show-thread)
1124   "*A hook called when an article is selected.
1125 The default hook shows conversation thread subtrees of the selected
1126 article automatically using `gnus-summary-show-thread'.")
1127
1128 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1129   "*A hook called to apply kill files to a group.
1130 This hook is intended to apply a kill file to the selected newsgroup.
1131 The function `gnus-apply-kill-file' is called by default.
1132
1133 Since a general kill file is too heavy to use only for a few
1134 newsgroups, I recommend you to use a lighter hook function. For
1135 example, if you'd like to apply a kill file to articles which contains
1136 a string `rmgroup' in subject in newsgroup `control', you can use the
1137 following hook:
1138
1139 \(setq gnus-apply-kill-hook
1140       (list
1141         (lambda ()
1142           (cond ((string-match \"control\" gnus-newsgroup-name)
1143                  (gnus-kill \"Subject\" \"rmgroup\")
1144                  (gnus-expunge \"X\"))))))")
1145
1146 (defvar gnus-visual-mark-article-hook 
1147   (list 'gnus-highlight-selected-summary)
1148   "*Hook run after selecting an article in the summary buffer.
1149 It is meant to be used for highlighting the article in some way.  It
1150 is not run if `gnus-visual' is nil.")
1151
1152 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1153   "*A hook called after preparing body, but before preparing header headers.
1154 The default hook (`gnus-inews-insert-signature') inserts a signature
1155 file specified by the variable `gnus-signature-file'.")
1156
1157 (defvar gnus-exit-group-hook nil
1158   "*A hook called when exiting (not quitting) summary mode.")
1159
1160 (defvar gnus-suspend-gnus-hook nil
1161   "*A hook called when suspending (not exiting) Gnus.")
1162
1163 (defvar gnus-exit-gnus-hook nil
1164   "*A hook called when exiting Gnus.")
1165
1166 (defvar gnus-save-newsrc-hook nil
1167   "*A hook called when saving the newsrc file.")
1168
1169 (defvar gnus-summary-update-hook 
1170   (list 'gnus-summary-highlight-line)
1171   "*A hook called when a summary line is changed.
1172 The hook will not be called if `gnus-visual' is nil.
1173
1174 The default function `gnus-summary-highlight-line' will
1175 highlight the line according to the `gnus-summary-highlight'
1176 variable.")
1177
1178 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1179   "*A hook called when an article is selected for the first time.
1180 The hook is intended to mark an article as read (or unread)
1181 automatically when it is selected.")
1182
1183 ;; Remove any hilit infestation.
1184 (add-hook 'gnus-startup-hook
1185           (lambda ()
1186             (remove-hook 'gnus-summary-prepare-hook
1187                          'hilit-rehighlight-buffer-quietly)
1188             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1189             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1190             (remove-hook 'gnus-article-prepare-hook
1191                          'hilit-rehighlight-buffer-quietly)))
1192
1193
1194 \f
1195 ;; Internal variables
1196
1197 ;; Avoid highlighting in kill files.
1198 (defvar gnus-summary-inhibit-highlight nil)
1199 (defvar gnus-newsgroup-selected-overlay nil)
1200
1201 (defvar gnus-article-mode-map nil)
1202 (defvar gnus-dribble-buffer nil)
1203 (defvar gnus-headers-retrieved-by nil)
1204 (defvar gnus-article-reply nil)
1205 (defvar gnus-override-method nil)
1206 (defvar gnus-article-check-size nil)
1207
1208 (defvar gnus-current-score-file nil)
1209 (defvar gnus-internal-global-score-files nil)
1210 (defvar gnus-score-file-list nil)
1211
1212
1213 (defvar gnus-current-move-group nil)
1214
1215 (defvar gnus-newsgroup-dependencies nil)
1216 (defvar gnus-newsgroup-threads nil)
1217 (defvar gnus-newsgroup-async nil)
1218 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1219
1220 (defvar gnus-newsgroup-adaptive nil)
1221
1222 (defvar gnus-summary-display-table nil)
1223
1224 (defconst gnus-group-line-format-alist
1225   (list (list ?M 'marked ?c)
1226         (list ?S 'subscribed ?c)
1227         (list ?L 'level ?d)
1228         (list ?N 'number ?s)
1229         (list ?I 'number-of-dormant ?d)
1230         (list ?T 'number-of-ticked ?d)
1231         (list ?R 'number-of-read ?s)
1232         (list ?t 'number-total ?d)
1233         (list ?y 'number-of-unread-unticked ?s)
1234         (list ?i 'number-of-ticked-and-dormant ?d)
1235         (list ?g 'group ?s)
1236         (list ?G 'qualified-group ?s)
1237         (list ?D 'newsgroup-description ?s)
1238         (list ?o 'moderated ?c)
1239         (list ?O 'moderated-string ?s)
1240         (list ?p 'process-marked ?c)
1241         (list ?s 'news-server ?s)
1242         (list ?n 'news-method ?s)
1243         (list ?z 'news-method-string ?s)
1244         (list ?u 'user-defined ?s)))
1245
1246 (defconst gnus-summary-line-format-alist 
1247   (list (list ?N 'number ?d)
1248         (list ?S 'subject ?s)
1249         (list ?s 'subject-or-nil ?s)
1250         (list ?n 'name ?s)
1251         (list ?A '(car (cdr (funcall gnus-extract-address-components from))) ?s)
1252         (list ?a '(or (car (funcall gnus-extract-address-components from)) from) ?s)
1253         (list ?F 'from ?s)
1254         (list ?x (macroexpand '(header-xref header)) ?s)
1255         (list ?D (macroexpand '(header-date header)) ?s)
1256         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1257         (list ?M (macroexpand '(header-id header)) ?s)
1258         (list ?r (macroexpand '(header-references header)) ?s)
1259         (list ?c '(or (header-chars header) 0) ?d)
1260         (list ?L 'lines ?d)
1261         (list ?I 'indentation ?s)
1262         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1263         (list ?R 'replied ?c)
1264         (list ?\[ 'opening-bracket ?c)
1265         (list ?\] 'closing-bracket ?c)
1266         (list ?\> '(make-string level ? ) ?s)
1267         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1268         (list ?i 'score ?d)
1269         (list ?z 'score-char ?c)
1270         (list ?U 'unread ?c)
1271         (list ?t '(gnus-summary-number-of-articles-in-thread 
1272                    (and (boundp 'thread) (car thread)))
1273               ?d)
1274         (list ?e '(gnus-summary-number-of-articles-in-thread 
1275                    (and (boundp 'thread) (car thread)) t)
1276               ?c)
1277         (list ?u 'user-defined ?s))
1278   "An alist of format specifications that can appear in summary lines,
1279 and what variables they correspond with, along with the type of the
1280 variable (string, integer, character, etc).")
1281
1282 (defconst gnus-summary-dummy-line-format-alist
1283   (list (list ?S 'subject ?s)
1284         (list ?N 'number ?d)
1285         (list ?u 'user-defined ?s)))
1286
1287 (defconst gnus-summary-mode-line-format-alist 
1288   (list (list ?G 'group-name ?s)
1289         (list ?g '(gnus-short-group-name group-name) ?s)
1290         (list ?A 'article-number ?d)
1291         (list ?Z 'unread-and-unselected ?s)
1292         (list ?V 'gnus-version ?s)
1293         (list ?U 'unread ?d)
1294         (list ?S 'subject ?s)
1295         (list ?e 'unselected ?d)
1296         (list ?u 'user-defined ?s)
1297         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1298
1299 (defconst gnus-group-mode-line-format-alist 
1300   (list (list ?S 'news-server ?s)
1301         (list ?M 'news-method ?s)
1302         (list ?u 'user-defined ?s)))
1303
1304 (defvar gnus-have-read-active-file nil)
1305
1306 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1307   "The mail address of the Gnus maintainers.")
1308
1309 (defconst gnus-version "(ding) Gnus v0.99"
1310   "Version number for this version of Gnus.")
1311
1312 (defvar gnus-info-nodes
1313   '((gnus-group-mode            "(gnus)The Group Buffer")
1314     (gnus-summary-mode          "(gnus)The Summary Buffer")
1315     (gnus-article-mode          "(gnus)The Article Buffer"))
1316   "Assoc list of major modes and related Info nodes.")
1317
1318 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1319   "The location of the (ding) Gnus documentation group.")
1320
1321 (defvar gnus-group-buffer "*Group*")
1322 (defvar gnus-summary-buffer "*Summary*")
1323 (defvar gnus-article-buffer "*Article*")
1324 (defvar gnus-server-buffer "*Server*")
1325
1326 (defvar gnus-work-buffer " *gnus work*")
1327
1328 (defvar gnus-buffer-list nil
1329   "Gnus buffers that should be killed on exit.")
1330
1331 (defvar gnus-server-alist nil
1332   "List of available servers.")
1333
1334 (defvar gnus-variable-list
1335   '(gnus-newsrc-options gnus-newsrc-options-n
1336     gnus-newsrc-last-checked-date 
1337     gnus-newsrc-alist gnus-server-alist
1338     gnus-killed-list gnus-zombie-list)
1339   "Gnus variables saved in the quick startup file.")
1340
1341 (defvar gnus-overload-functions
1342   '((news-inews gnus-inews-news "rnewspost"))
1343   "Functions overloaded by gnus.
1344 It is a list of `(original overload &optional file)'.")
1345
1346 (defvar gnus-newsrc-options nil
1347   "Options line in the .newsrc file.")
1348
1349 (defvar gnus-newsrc-options-n nil
1350   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1351
1352 (defvar gnus-newsrc-last-checked-date nil
1353   "Date Gnus last asked server for new newsgroups.")
1354
1355 (defvar gnus-newsrc-alist nil
1356   "Assoc list of read articles.
1357 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1358
1359 (defvar gnus-newsrc-hashtb nil
1360   "Hashtable of gnus-newsrc-alist.")
1361
1362 (defvar gnus-killed-list nil
1363   "List of killed newsgroups.")
1364
1365 (defvar gnus-killed-hashtb nil
1366   "Hash table equivalent of gnus-killed-list.")
1367
1368 (defvar gnus-zombie-list nil
1369   "List of almost dead newsgroups.")
1370
1371 (defvar gnus-description-hashtb nil
1372   "Descriptions of newsgroups.")
1373
1374 (defvar gnus-list-of-killed-groups nil
1375   "List of newsgroups that have recently been killed by the user.")
1376
1377 (defvar gnus-active-hashtb nil
1378   "Hashtable of active articles.")
1379
1380 (defvar gnus-moderated-list nil
1381   "List of moderated newsgroups.")
1382
1383 (defvar gnus-group-marked nil)
1384
1385 (defvar gnus-current-startup-file nil
1386   "Startup file for the current host.")
1387
1388 (defvar gnus-last-search-regexp nil
1389   "Default regexp for article search command.")
1390
1391 (defvar gnus-last-shell-command nil
1392   "Default shell command on article.")
1393
1394 (defvar gnus-current-select-method nil
1395   "The current method for selecting a newsgroup.")
1396
1397 (defvar gnus-have-all-newsgroups nil)
1398
1399 (defvar gnus-article-internal-prepare-hook nil)
1400
1401 (defvar gnus-newsgroup-name nil)
1402 (defvar gnus-newsgroup-begin nil)
1403 (defvar gnus-newsgroup-end nil)
1404 (defvar gnus-newsgroup-last-rmail nil)
1405 (defvar gnus-newsgroup-last-mail nil)
1406 (defvar gnus-newsgroup-last-folder nil)
1407 (defvar gnus-newsgroup-last-file nil)
1408 (defvar gnus-newsgroup-auto-expire nil)
1409 (defvar gnus-newsgroup-active nil)
1410
1411 (defvar gnus-newsgroup-unreads nil
1412   "List of unread articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-unselected nil
1415   "List of unselected unread articles in the current newsgroup.")
1416
1417 (defvar gnus-newsgroup-marked nil
1418   "List of ticked articles in the current newsgroup (a subset of unread art).")
1419
1420 (defvar gnus-newsgroup-killed nil
1421   "List of ranges of articles that have been through the scoring process.")
1422
1423 (defvar gnus-newsgroup-kill-headers nil)
1424
1425 (defvar gnus-newsgroup-replied nil
1426   "List of articles that have been replied to in the current newsgroup.")
1427
1428 (defvar gnus-newsgroup-expirable nil
1429   "List of articles in the current newsgroup that can be expired.")
1430
1431 (defvar gnus-newsgroup-processable nil
1432   "List of articles in the current newsgroup that can be processed.")
1433
1434 (defvar gnus-newsgroup-bookmarks nil
1435   "List of articles in the current newsgroup that have bookmarks.")
1436
1437 (defvar gnus-newsgroup-dormant nil
1438   "List of dormant articles in the current newsgroup.")
1439
1440 (defvar gnus-newsgroup-scored nil
1441   "List of scored articles in the current newsgroup.")
1442
1443 (defvar gnus-newsgroup-headers nil
1444   "List of article headers in the current newsgroup.")
1445 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1446
1447 (defvar gnus-newsgroup-ancient nil
1448   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1449
1450 (defvar gnus-current-article nil)
1451 (defvar gnus-article-current nil)
1452 (defvar gnus-current-headers nil)
1453 (defvar gnus-have-all-headers nil)
1454 (defvar gnus-last-article nil)
1455 (defvar gnus-newsgroup-history nil)
1456 (defvar gnus-current-kill-article nil)
1457
1458 ;; Save window configuration.
1459 (defvar gnus-prev-winconf nil)
1460
1461 ;; Format specs
1462 (defvar gnus-summary-line-format-spec nil)
1463 (defvar gnus-summary-dummy-line-format-spec nil)
1464 (defvar gnus-group-line-format-spec nil)
1465 (defvar gnus-summary-mode-line-format-spec nil)
1466 (defvar gnus-article-mode-line-format-spec nil)
1467 (defvar gnus-group-mode-line-format-spec nil)
1468 (defvar gnus-summary-mark-positions nil)
1469
1470 (defvar gnus-summary-expunge-below nil)
1471 (defvar gnus-reffed-article-number nil)
1472
1473 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1474 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1475
1476 (defvar gnus-cache-removeable-articles nil)
1477
1478 (defconst gnus-summary-local-variables 
1479   '(gnus-newsgroup-name 
1480     gnus-newsgroup-begin gnus-newsgroup-end 
1481     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1482     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1483     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1484     gnus-newsgroup-unselected gnus-newsgroup-marked
1485     gnus-newsgroup-replied gnus-newsgroup-expirable
1486     gnus-newsgroup-processable gnus-newsgroup-killed
1487     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1488     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1489     gnus-current-article gnus-current-headers gnus-have-all-headers
1490     gnus-last-article gnus-article-internal-prepare-hook
1491     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1492     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1493     gnus-newsgroup-threads gnus-newsgroup-async
1494     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1495     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1496     gnus-newsgroup-history gnus-newsgroup-ancient
1497     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1498     gnus-cache-removeable-articles)
1499   "Variables that are buffer-local to the summary buffers.")
1500
1501 (defconst gnus-bug-message
1502   "Sending a bug report to the Gnus Towers.
1503 ========================================
1504
1505 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1506 be sent to the Gnus Bug Exterminators. 
1507
1508 At the bottom of the buffer you'll see lots of variable settings.
1509 Please do not delete those.  They will tell the Bug People what your
1510 environment is, so that it will be easier to locate the bugs.
1511
1512 If you have found a bug that makes Emacs go \"beep\", set
1513 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1514 and include the backtrace in your bug report.
1515
1516 Please describe the bug in annoying, painstaking detail.
1517
1518 Thank you for your help in stamping out bugs.
1519 ")
1520
1521 ;;; End of variables.
1522
1523 ;; Define some autoload functions Gnus might use.
1524 (eval-and-compile
1525
1526   ;; Various 
1527   (autoload 'metamail-buffer "metamail")
1528   (autoload 'Info-goto-node "info")
1529   (autoload 'hexl-hex-string-to-integer "hexl")
1530   (autoload 'pp "pp")
1531   (autoload 'pp-to-string "pp")
1532   (autoload 'pp-eval-expression "pp")
1533   (autoload 'mail-extract-address-components "mail-extr")
1534
1535   (autoload 'nnmail-split-fancy "nnmail")
1536   (autoload 'nnvirtual-catchup-group "nnvirtual")
1537
1538   ;; timezone
1539   (autoload 'timezone-make-date-arpa-standard "timezone")
1540   (autoload 'timezone-fix-time "timezone")
1541   (autoload 'timezone-make-sortable-date "timezone")
1542   (autoload 'timezone-make-time-string "timezone")
1543
1544   ;; rmail & friends
1545   (autoload 'mail-position-on-field "sendmail")
1546   (autoload 'mail-setup "sendmail")
1547   (autoload 'rmail-output "rmailout")
1548   (autoload 'news-mail-other-window "rnewspost")
1549   (autoload 'news-reply-yank-original "rnewspost")
1550   (autoload 'news-caesar-buffer-body "rnewspost")
1551   (autoload 'rmail-insert-rmail-file-header "rmail")
1552   (autoload 'rmail-count-new-messages "rmail")
1553   (autoload 'rmail-show-message "rmail")
1554
1555   ;; gnus-soup
1556   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1557   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1558   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1559   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1560   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1561   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1562   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1563
1564   ;; gnus-mh
1565   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1566   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1567   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1568   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1569   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1570   (autoload 'gnus-Folder-save-name "gnus-mh")
1571   (autoload 'gnus-folder-save-name "gnus-mh")
1572
1573   ;; gnus-vis misc
1574   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1575   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1576   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1577   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1578   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1579   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1580   (autoload 'gnus-summary-highlight-line "gnus-vis")
1581   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1582
1583   ;; gnus-vis article
1584   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1585   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1586   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1587   (autoload 'gnus-article-hide "gnus-vis" nil t)
1588   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1589   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1590   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1591   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1592   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1593   (autoload 'gnus-article-add-button "gnus-vis")
1594
1595   ;; gnus-cite
1596   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1597   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1598   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1599
1600   ;; gnus-kill
1601   (autoload 'gnus-kill "gnus-kill")
1602   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1603   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1604   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1605   (autoload 'gnus-execute "gnus-kill")
1606   (autoload 'gnus-expunge "gnus-kill")
1607
1608   ;; gnus-cache
1609   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1610   (autoload 'gnus-cache-save-buffers "gnus-cache")
1611   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1612   (autoload 'gnus-cache-request-article "gnus-cache")
1613   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1614   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1615   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1616   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1617
1618   ;; gnus-score
1619   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1620   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1621   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1622   (autoload 'gnus-score-save "gnus-score")
1623   (autoload 'gnus-score-headers "gnus-score")
1624   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1625   (autoload 'gnus-score-adaptive "gnus-score")
1626   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1627   (autoload 'gnus-score-find-trace "gnus-score")
1628
1629   ;; gnus-edit
1630   (autoload 'gnus-score-customize "gnus-edit" nil t)
1631
1632   ;; gnus-uu
1633   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1634   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1635   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1636   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1637   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1638   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1639   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1640   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1641   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1642   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1643   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1644   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1645   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1646   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1647   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1648   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1649   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1652   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1655
1656   ;; gnus-msg
1657   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1658   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1659   (autoload 'gnus-group-mail "gnus-msg" nil t)
1660   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1661   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1662   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1663   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1664   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1665   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1666   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1667   (autoload 'gnus-post-news "gnus-msg" nil t)
1668   (autoload 'gnus-inews-news "gnus-msg" nil t)
1669   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1670   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1671   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1672   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1673   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1674   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1675   (autoload 'gnus-mail-yank-original "gnus-msg")
1676   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1677   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1678   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1679   (autoload 'gnus-article-mail-with-original "gnus-msg")
1680   (autoload 'gnus-article-mail "gnus-msg")
1681   (autoload 'gnus-bug "gnus-msg" nil t)
1682
1683   ;; gnus-vm
1684   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1685   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1686   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1687   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1688   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1689   (autoload 'gnus-yank-article "gnus-vm" nil t)
1690
1691   )
1692
1693 \f
1694
1695 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1696 ;; If you want the cursor to go somewhere else, set these two
1697 ;; functions in some startup hook to whatever you want.
1698 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1699 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1700
1701 ;;; Various macros and substs.
1702
1703 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1704   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1705   (` (let ((GnusStartBufferWindow (selected-window)))
1706        (unwind-protect
1707            (progn
1708              (pop-to-buffer (, buffer))
1709              (,@ forms))
1710          (select-window GnusStartBufferWindow)))))
1711
1712 (defmacro gnus-gethash (string hashtable)
1713   "Get hash value of STRING in HASHTABLE."
1714   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1715   ;;(` (abbrev-expansion (, string) (, hashtable)))
1716   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1717
1718 (defmacro gnus-sethash (string value hashtable)
1719   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1720   ;; We cannot use define-abbrev since it only accepts string as value.
1721   ;; (set (intern string hashtable) value))
1722   (` (set (intern (, string) (, hashtable)) (, value))))
1723
1724 (defsubst gnus-buffer-substring (beg end)
1725   (buffer-substring (match-beginning beg) (match-end end)))
1726
1727 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1728 ;;   function `substring' might cut on a middle of multi-octet
1729 ;;   character.
1730 (defun gnus-truncate-string (str width)
1731   (substring str 0 width))
1732
1733 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1734 ;; to limit the length of a string. This function is necessary since
1735 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1736 (defsubst gnus-limit-string (str width)
1737   (if (> (length str) width)
1738       (substring str 0 width)
1739     str))
1740
1741 (defsubst gnus-simplify-subject-re (subject)
1742   "Remove \"Re:\" from subject lines."
1743   (let ((case-fold-search t))
1744     (if (string-match "^re: *" subject)
1745         (substring subject (match-end 0))
1746       subject)))
1747
1748 (defsubst gnus-goto-char (point)
1749   (and point (goto-char point)))
1750
1751 (defmacro gnus-buffer-exists-p (buffer)
1752   (` (and (, buffer)
1753           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1754                    (, buffer)))))
1755
1756 (defmacro gnus-kill-buffer (buffer)
1757   (` (if (gnus-buffer-exists-p (, buffer))
1758          (kill-buffer (, buffer)))))
1759
1760 (defsubst gnus-point-at-bol ()
1761   "Return point at the beginning of line."
1762   (let ((p (point)))
1763     (beginning-of-line)
1764     (prog1
1765         (point)
1766       (goto-char p))))
1767
1768 (defsubst gnus-point-at-eol ()
1769   "Return point at the beginning of line."
1770   (let ((p (point)))
1771     (end-of-line)
1772     (prog1
1773         (point)
1774       (goto-char p))))
1775
1776 ;; Delete the current line (and the next N lines.);
1777 (defmacro gnus-delete-line (&optional n)
1778   (` (delete-region (progn (beginning-of-line) (point))
1779                     (progn (forward-line (, (or n 1))) (point)))))
1780
1781 ;;; Load the compatability functions. 
1782
1783 (require 'gnus-ems)
1784
1785 \f
1786 ;;;
1787 ;;; Gnus Utility Functions
1788 ;;;
1789
1790 (defun gnus-extract-address-components (from)
1791   (let (name address)
1792     ;; First find the address - the thing with the @ in it.  This may
1793     ;; not be accurate in mail addresses, but does the trick most of
1794     ;; the time in news messages.
1795     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1796         (setq address (substring from (match-beginning 0) (match-end 0))))
1797     ;; Then we check whether the "name <address>" format is used.
1798     (and address
1799          (string-match (concat "<" (regexp-quote address) ">") from)
1800          (and (setq name (substring from 0 (1- (match-beginning 0))))
1801               ;; Strip any quotes from the name.
1802               (string-match "\".*\"" name)
1803               (setq name (substring name 1 (1- (match-end 0))))))
1804     ;; If not, then "address (name)" is used.
1805     (or name
1806         (and (string-match "(.+)" from)
1807              (setq name (substring from (1+ (match-beginning 0)) 
1808                                    (1- (match-end 0)))))
1809         (and (string-match "()" from)
1810              (setq name address))
1811         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1812         ;; XOVER might not support folded From headers.
1813         (and (string-match "(.*" from)
1814              (setq name (substring from (1+ (match-beginning 0)) 
1815                                    (match-end 0)))))
1816     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1817     (list (or name from) (or address from))))
1818
1819 (defun gnus-fetch-field (field)
1820   "Return the value of the header FIELD of current article."
1821   (save-excursion
1822     (save-restriction
1823       (let ((case-fold-search t))
1824         (gnus-narrow-to-headers)
1825         (mail-fetch-field field)))))
1826
1827 (defun gnus-goto-colon ()
1828   (beginning-of-line)
1829   (search-forward ":" (gnus-point-at-eol) t))
1830
1831 (defun gnus-narrow-to-headers ()
1832   (widen)
1833   (save-excursion
1834     (narrow-to-region
1835      (goto-char (point-min))
1836      (if (search-forward "\n\n" nil t)
1837          (1- (point))
1838        (point-max)))))
1839
1840 (defvar gnus-old-specs nil)
1841
1842 (defun gnus-update-format-specifications ()
1843   (gnus-make-thread-indent-array)
1844
1845   (let ((formats '(summary summary-dummy group 
1846                            summary-mode group-mode article-mode))
1847         old-format new-format)
1848     (while formats
1849       (setq new-format (symbol-value
1850                         (intern (format "gnus-%s-line-format" (car formats)))))
1851       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1852                (equal old-format new-format))
1853           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1854                (gnus-parse-format
1855                 new-format
1856                 (symbol-value 
1857                  (intern (format "gnus-%s-line-format-alist"
1858                                  (if (eq (car formats) 'article-mode)
1859                                      'summary-mode (car formats))))))))
1860       (setq gnus-old-specs (cons (cons (car formats) new-format)
1861                                  (delq (car formats) gnus-old-specs)))
1862       (setq formats (cdr formats))))
1863       
1864   (gnus-update-summary-mark-positions)
1865
1866   (if (and (string-match "%D" gnus-group-line-format)
1867            (not gnus-description-hashtb)
1868            gnus-read-active-file)
1869       (gnus-read-all-descriptions-files)))
1870
1871 (defun gnus-update-summary-mark-positions ()
1872   (save-excursion
1873     (let ((gnus-replied-mark 129)
1874           (gnus-score-below-mark 130)
1875           (gnus-score-over-mark 130)
1876           (thread nil)
1877           pos)
1878       (gnus-set-work-buffer)
1879       (gnus-summary-insert-line 
1880        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1881       (goto-char (point-min))
1882       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1883                                          (- (point) 2)))))
1884       (goto-char (point-min))
1885       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1886                                           (- (point) 2))) pos))
1887       (goto-char (point-min))
1888       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1889                                         (- (point) 2))) pos))
1890       (setq gnus-summary-mark-positions pos))))
1891
1892 (defun gnus-mouse-face-function (form)
1893   (` (let ((string (, form)))
1894        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1895        string)))
1896
1897 (defun gnus-max-width-function (el max-width)
1898   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1899   (` (let* ((val (eval (, el)))
1900             (valstr (if (numberp val)
1901                         (int-to-string val) val)))
1902        (if (> (length valstr) (, max-width))
1903            (substring valstr 0 (, max-width))
1904          valstr))))
1905
1906 (defun gnus-parse-format (format spec-alist)
1907   ;; This function parses the FORMAT string with the help of the
1908   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1909   ;; string.  If the FORMAT string contains the specifiers %( and %)
1910   ;; the text between them will have the mouse-face text property.
1911   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1912       (if (and gnus-visual gnus-mouse-face)
1913           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1914                 (button (substring format (match-beginning 2) (match-end 2)))
1915                 (post (substring format (match-beginning 3) (match-end 3))))
1916             (list 'concat
1917                   (gnus-parse-simple-format pre spec-alist)
1918                   (gnus-mouse-face-function 
1919                    (gnus-parse-simple-format button spec-alist))
1920                   (gnus-parse-simple-format post spec-alist)))
1921         (gnus-parse-simple-format
1922          (concat (substring format (match-beginning 1) (match-end 1))
1923                  (substring format (match-beginning 2) (match-end 2))
1924                  (substring format (match-beginning 3) (match-end 3)))
1925          spec-alist))
1926     (gnus-parse-simple-format format spec-alist)))
1927
1928 (defun gnus-parse-simple-format (format spec-alist)
1929   ;; This function parses the FORMAT string with the help of the
1930   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1931   ;; string. The list will consist of the symbol `format', a format
1932   ;; specification string, and a list of forms depending on the
1933   ;; SPEC-ALIST.
1934   (let ((max-width 0)
1935         spec flist fstring newspec elem beg)
1936     (save-excursion
1937       (gnus-set-work-buffer)
1938       (insert format)
1939       (goto-char (point-min))
1940       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1941         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1942                                                      (match-end 2))))
1943         ;; First check if there are any specs that look anything like
1944         ;; "%12,12A", ie. with a "max width specification". These have
1945         ;; to be treated specially.
1946         (if (setq beg (match-beginning 1))
1947             (setq max-width 
1948                   (string-to-int 
1949                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1950           (setq max-width 0)
1951           (setq beg (match-beginning 2)))
1952         ;; Find the specification from `spec-alist'.
1953         (if (not (setq elem (cdr (assq spec spec-alist))))
1954             (setq elem '("*" ?s)))
1955         ;; Treat user defined format specifiers specially
1956         (and (eq (car elem) 'user-defined)
1957              (setq elem
1958                    (list 
1959                     (list (intern (concat "gnus-user-format-function-"
1960                                           (buffer-substring
1961                                            (match-beginning 3)
1962                                            (match-end 3))))
1963                           'header)
1964                     ?s))
1965              (delete-region (match-beginning 3) (match-end 3)))
1966         (if (not (zerop max-width))
1967             (let ((el (car elem)))
1968               (cond ((= (car (cdr elem)) ?c) 
1969                      (setq el (list 'char-to-string el)))
1970                     ((= (car (cdr elem)) ?d)
1971                      (numberp el) (setq el (list 'int-to-string el))))
1972               (setq flist (cons (gnus-max-width-function el max-width)
1973                                 flist))
1974               (setq newspec ?s))
1975           (setq flist (cons (car elem) flist))
1976           (setq newspec (car (cdr elem))))
1977         ;; Remove the old specification (and possibly a ",12" string).
1978         (delete-region beg (match-end 2))
1979         ;; Insert the new specification.
1980         (goto-char beg)
1981         (insert newspec))
1982       (setq fstring (buffer-substring 1 (point-max))))
1983     (cons 'format (cons fstring (nreverse flist)))))
1984
1985 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1986 (defun gnus-read-init-file ()
1987   (and gnus-init-file
1988        (or (and (file-exists-p gnus-init-file) 
1989                 ;; Don't try to load a directory.
1990                 (not (file-directory-p gnus-init-file)))
1991            (file-exists-p (concat gnus-init-file ".el"))
1992            (file-exists-p (concat gnus-init-file ".elc")))
1993        (load gnus-init-file nil t)))
1994
1995 (defun gnus-set-work-buffer ()
1996   (if (get-buffer gnus-work-buffer)
1997       (progn
1998         (set-buffer gnus-work-buffer)
1999         (erase-buffer))
2000     (set-buffer (get-buffer-create gnus-work-buffer))
2001     (kill-all-local-variables)
2002     (buffer-disable-undo (current-buffer))
2003     (gnus-add-current-to-buffer-list)))
2004
2005 ;; Article file names when saving.
2006
2007 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2008   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2009 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2010 Otherwise, it is like ~/News/news/group/num."
2011   (let ((default
2012           (expand-file-name
2013            (concat (if (gnus-use-long-file-name 'not-save)
2014                        (gnus-capitalize-newsgroup newsgroup)
2015                      (gnus-newsgroup-directory-form newsgroup))
2016                    "/" (int-to-string (header-number headers)))
2017            (or gnus-article-save-directory "~/News"))))
2018     (if (and last-file
2019              (string-equal (file-name-directory default)
2020                            (file-name-directory last-file))
2021              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2022         default
2023       (or last-file default))))
2024
2025 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2026   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2027 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2028 Otherwise, it is like ~/News/news/group/num."
2029   (let ((default
2030           (expand-file-name
2031            (concat (if (gnus-use-long-file-name 'not-save)
2032                        newsgroup
2033                      (gnus-newsgroup-directory-form newsgroup))
2034                    "/" (int-to-string (header-number headers)))
2035            (or gnus-article-save-directory "~/News"))))
2036     (if (and last-file
2037              (string-equal (file-name-directory default)
2038                            (file-name-directory last-file))
2039              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2040         default
2041       (or last-file default))))
2042
2043 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2044   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2045 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2046 Otherwise, it is like ~/News/news/group/news."
2047   (or last-file
2048       (expand-file-name
2049        (if (gnus-use-long-file-name 'not-save)
2050            (gnus-capitalize-newsgroup newsgroup)
2051          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2052        (or gnus-article-save-directory "~/News"))))
2053
2054 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2055   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2056 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2057 Otherwise, it is like ~/News/news/group/news."
2058   (or last-file
2059       (expand-file-name
2060        (if (gnus-use-long-file-name 'not-save)
2061            newsgroup
2062          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2063        (or gnus-article-save-directory "~/News"))))
2064
2065 ;; For subscribing new newsgroup
2066
2067 (defun gnus-subscribe-hierarchical-interactive (groups)
2068   (let ((groups (sort groups 'string<))
2069         prefixes prefix start ans group starts)
2070     (while groups
2071       (setq prefixes (list "^"))
2072       (while (and groups prefixes)
2073         (while (not (string-match (car prefixes) (car groups)))
2074           (setq prefixes (cdr prefixes)))
2075         (setq prefix (car prefixes))
2076         (setq start (1- (length prefix)))
2077         (if (and (string-match "[^\\.]\\." (car groups) start)
2078                  (cdr groups)
2079                  (setq prefix 
2080                        (concat "^" (substring (car groups) 0 (match-end 0))))
2081                  (string-match prefix (car (cdr groups))))
2082             (progn
2083               (setq prefixes (cons prefix prefixes))
2084               (message "Descend hierarchy %s? ([y]nsq): " 
2085                        (substring prefix 1 (1- (length prefix))))
2086               (setq ans (read-char))
2087               (cond ((= ans ?n)
2088                      (while (and groups 
2089                                  (string-match prefix 
2090                                                (setq group (car groups))))
2091                        (setq gnus-killed-list 
2092                              (cons group gnus-killed-list))
2093                        (gnus-sethash group group gnus-killed-hashtb)
2094                        (setq groups (cdr groups)))
2095                      (setq starts (cdr starts)))
2096                     ((= ans ?s)
2097                      (while (and groups 
2098                                  (string-match prefix 
2099                                                (setq group (car groups))))
2100                        (gnus-sethash group group gnus-killed-hashtb)
2101                        (gnus-subscribe-alphabetically (car groups))
2102                        (setq groups (cdr groups)))
2103                      (setq starts (cdr starts)))
2104                     ((= ans ?q)
2105                      (while groups
2106                        (setq group (car groups))
2107                        (setq gnus-killed-list (cons group gnus-killed-list))
2108                        (gnus-sethash group group gnus-killed-hashtb)
2109                        (setq groups (cdr groups))))
2110                     (t nil)))
2111           (message "Subscribe %s? ([n]yq)" (car groups))
2112           (setq ans (read-char))
2113           (setq group (car groups))
2114           (cond ((= ans ?y)
2115                  (gnus-subscribe-alphabetically (car groups))
2116                  (gnus-sethash group group gnus-killed-hashtb))
2117                 ((= ans ?q)
2118                  (while groups
2119                    (setq group (car groups))
2120                    (setq gnus-killed-list (cons group gnus-killed-list))
2121                    (gnus-sethash group group gnus-killed-hashtb)
2122                    (setq groups (cdr groups))))
2123                 (t 
2124                  (setq gnus-killed-list (cons group gnus-killed-list))
2125                  (gnus-sethash group group gnus-killed-hashtb)))
2126           (setq groups (cdr groups)))))))
2127
2128 (defun gnus-subscribe-randomly (newsgroup)
2129   "Subscribe new NEWSGROUP by making it the first newsgroup."
2130   (gnus-subscribe-newsgroup newsgroup))
2131
2132 (defun gnus-subscribe-alphabetically (newgroup)
2133   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2134   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2135   (let ((groups (cdr gnus-newsrc-alist))
2136         before)
2137     (while (and (not before) groups)
2138       (if (string< newgroup (car (car groups)))
2139           (setq before (car (car groups)))
2140         (setq groups (cdr groups))))
2141     (gnus-subscribe-newsgroup newgroup before)))
2142
2143 (defun gnus-subscribe-hierarchically (newgroup)
2144   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2145   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2146   (save-excursion
2147     (set-buffer (find-file-noselect gnus-current-startup-file))
2148     (let ((groupkey newgroup)
2149           before)
2150       (while (and (not before) groupkey)
2151         (goto-char (point-min))
2152         (let ((groupkey-re
2153                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2154           (while (and (re-search-forward groupkey-re nil t)
2155                       (progn
2156                         (setq before (buffer-substring
2157                                       (match-beginning 1) (match-end 1)))
2158                         (string< before newgroup)))))
2159         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2160         (setq groupkey
2161               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2162                   (substring groupkey (match-beginning 1) (match-end 1)))))
2163       (gnus-subscribe-newsgroup newgroup before))))
2164
2165 (defun gnus-subscribe-interactively (newsgroup)
2166   "Subscribe new NEWSGROUP interactively.
2167 It is inserted in hierarchical newsgroup order if subscribed. If not,
2168 it is killed."
2169   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2170       (gnus-subscribe-hierarchically newsgroup)
2171     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2172
2173 (defun gnus-subscribe-zombies (newsgroup)
2174   "Make new NEWSGROUP a zombie group."
2175   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2176
2177 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2178   "Subscribe new NEWSGROUP.
2179 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2180 the first newsgroup."
2181   ;; We subscribe the group by changing its level to `subscribed'.
2182   (gnus-group-change-level 
2183    newsgroup gnus-level-default-subscribed
2184    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2185   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2186
2187 ;; For directories
2188
2189 (defun gnus-newsgroup-directory-form (newsgroup)
2190   "Make hierarchical directory name from NEWSGROUP name."
2191   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2192         (len (length newsgroup))
2193         idx)
2194     ;; If this is a foreign group, we don't want to translate the
2195     ;; entire name.  
2196     (if (setq idx (string-match ":" newsgroup))
2197         (aset newsgroup idx ?/)
2198       (setq idx 0))
2199     ;; Replace all occurrences of `.' with `/'.
2200     (while (< idx len)
2201       (if (= (aref newsgroup idx) ?.)
2202           (aset newsgroup idx ?/))
2203       (setq idx (1+ idx)))
2204     newsgroup))
2205
2206 (defun gnus-make-directory (dir)
2207   "Make DIRECTORY recursively."
2208   (let* ((dir (expand-file-name dir default-directory))
2209          dirs)
2210     (if (string-match "/$" dir)
2211         (setq dir (substring dir 0 (match-beginning 0))))
2212     (while (not (file-exists-p dir))
2213       (setq dirs (cons dir dirs))
2214       (string-match "/[^/]+$" dir)
2215       (setq dir (substring dir 0 (match-beginning 0))))
2216     (while dirs
2217       (make-directory (car dirs))
2218       (setq dirs (cdr dirs)))))
2219
2220 (defun gnus-capitalize-newsgroup (newsgroup)
2221   "Capitalize NEWSGROUP name."
2222   (and (not (zerop (length newsgroup)))
2223        (concat (char-to-string (upcase (aref newsgroup 0)))
2224                (substring newsgroup 1))))
2225
2226 ;; Var
2227
2228 (defun gnus-simplify-subject (subject &optional re-only)
2229   "Remove `Re:' and words in parentheses.
2230 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2231   (let ((case-fold-search t))           ;Ignore case.
2232     ;; Remove `Re:' and `Re^N:'.
2233     (if (string-match "^re:[ \t]*" subject)
2234         (setq subject (substring subject (match-end 0))))
2235     ;; Remove words in parentheses from end.
2236     (or re-only
2237         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2238           (setq subject (substring subject 0 (match-beginning 0)))))
2239     ;; Return subject string.
2240     subject))
2241
2242 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2243 ;; all whitespace.
2244 (defun gnus-simplify-subject-fuzzy (subject)
2245   (let ((case-fold-search t))
2246     (save-excursion
2247       (gnus-set-work-buffer)
2248       (insert subject)
2249       (inline (gnus-simplify-buffer-fuzzy))
2250       (buffer-string))))
2251
2252 (defun gnus-simplify-buffer-fuzzy ()
2253   (goto-char (point-min))
2254   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2255   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2256                             nil t)
2257     (replace-match "" t t))
2258   (goto-char (point-min))
2259   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2260     (replace-match "" t t))
2261   (goto-char (point-min))
2262   (while (re-search-forward "[ \t]+" nil t)
2263     (replace-match " " t t))
2264   (goto-char (point-min))
2265   (while (re-search-forward "[ \t]+$" nil t)
2266     (replace-match "" t t))
2267   (goto-char (point-min))
2268   (while (re-search-forward "^[ \t]+" nil t)
2269     (replace-match "" t t))
2270   (if gnus-simplify-subject-fuzzy-regexp
2271       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2272         (replace-match "" t t))))
2273
2274 ;; Add the current buffer to the list of buffers to be killed on exit. 
2275 (defun gnus-add-current-to-buffer-list ()
2276   (or (memq (current-buffer) gnus-buffer-list)
2277       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2278
2279 (defun gnus-string> (s1 s2)
2280   (not (or (string< s1 s2)
2281            (string= s1 s2))))
2282
2283 ;; Functions accessing headers.
2284 ;; Functions are more convenient than macros in some cases.
2285
2286 (defun gnus-header-number (header)
2287   (header-number header))
2288
2289 (defun gnus-header-subject (header)
2290   (header-subject header))
2291
2292 (defun gnus-header-from (header)
2293   (header-from header))
2294
2295 (defun gnus-header-xref (header)
2296   (header-xref header))
2297
2298 (defun gnus-header-lines (header)
2299   (header-lines header))
2300
2301 (defun gnus-header-date (header)
2302   (header-date header))
2303
2304 (defun gnus-header-id (header)
2305   (header-id header))
2306
2307 (defun gnus-header-references (header)
2308   (header-references header))
2309
2310 ;;; General various misc type functions.
2311
2312 (defun gnus-clear-system ()
2313   "Clear all variables and buffers."
2314   ;; Clear Gnus variables.
2315   (let ((variables gnus-variable-list))
2316     (while variables
2317       (set (car variables) nil)
2318       (setq variables (cdr variables))))
2319   ;; Clear other internal variables.
2320   (setq gnus-list-of-killed-groups nil
2321         gnus-have-read-active-file nil
2322         gnus-newsrc-alist nil
2323         gnus-newsrc-hashtb nil
2324         gnus-killed-list nil
2325         gnus-zombie-list nil
2326         gnus-killed-hashtb nil
2327         gnus-active-hashtb nil
2328         gnus-moderated-list nil
2329         gnus-description-hashtb nil
2330         gnus-newsgroup-headers nil
2331         gnus-newsgroup-headers-hashtb-by-number nil
2332         gnus-newsgroup-name nil
2333         gnus-server-alist nil
2334         gnus-current-select-method nil)
2335   ;; Reset any score variables.
2336   (and (boundp 'gnus-score-cache)
2337        (set 'gnus-score-cache nil))
2338   (and (boundp 'gnus-internal-global-score-files)
2339        (set 'gnus-internal-global-score-files nil))
2340   ;; Kill the startup file.
2341   (and gnus-current-startup-file
2342        (get-file-buffer gnus-current-startup-file)
2343        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2344   ;; Save any cache buffers.
2345   (and gnus-use-cache (gnus-cache-save-buffers))
2346   ;; Clear the dribble buffer.
2347   (gnus-dribble-clear)
2348   ;; Kill global KILL file buffer.
2349   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2350       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2351   (gnus-kill-buffer nntp-server-buffer)
2352   ;; Kill Gnus buffers.
2353   (while gnus-buffer-list
2354     (gnus-kill-buffer (car gnus-buffer-list))
2355     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2356
2357 (defun gnus-windows-old-to-new (setting)
2358   (if (symbolp setting)
2359       (setq setting 
2360             (cond ((eq setting 'SelectArticle)
2361                    'article)
2362                   ((eq setting 'SelectSubject)
2363                    'summary)
2364                   ((eq setting 'SelectNewsgroup)
2365                    'group)
2366                   (t setting))))
2367   (if (or (listp setting)
2368           (not (and gnus-window-configuration
2369                     (memq setting '(group summary article)))))
2370       setting
2371     (let* ((setting (if (eq setting 'group) 
2372                         (if (assq 'newsgroup gnus-window-configuration)
2373                             'newsgroup
2374                           'newsgroups) setting))
2375            (elem (car (cdr (assq setting gnus-window-configuration))))
2376            (total (apply '+ elem))
2377            (types '(group summary article))
2378            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2379            (i 0)
2380            perc
2381            out)
2382       (while (< i 3)
2383         (or (zerop (nth i elem))
2384             (progn
2385               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2386               (setq out (cons (if (eq pbuf (nth i types))
2387                                   (vector (nth i types) perc 'point)
2388                                 (vector (nth i types) perc))
2389                               out))))
2390         (setq i (1+ i)))
2391       (list (nreverse out)))))
2392            
2393 (defun gnus-add-configuration (conf)
2394   (setq gnus-buffer-configuration 
2395         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2396                          gnus-buffer-configuration))))
2397
2398 (defun gnus-configure-windows (setting &optional force)
2399   (setq setting (gnus-windows-old-to-new setting))
2400   (let ((r (if (symbolp setting)
2401                (cdr (assq setting gnus-buffer-configuration))
2402              setting))
2403         (in-buf (current-buffer))
2404         rule val w height hor ohor heights sub jump-buffer
2405         rel total to-buf all-visible)
2406     (or r (error "No such setting: %s" setting))
2407
2408     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2409         ;; All the windows mentioned are already visibe, so we just
2410         ;; put point in the assigned buffer, and do not touch the
2411         ;; winconf. 
2412         (select-window (get-buffer-window all-visible))
2413
2414       ;; Either remove all windows or just remove all Gnus windows.
2415       (if gnus-use-full-window
2416           (delete-other-windows)
2417         (gnus-remove-some-windows)
2418         (switch-to-buffer nntp-server-buffer))
2419
2420       (while r
2421         (setq hor (car r)
2422               ohor nil)
2423
2424         ;; We have to do the (possible) horizontal splitting before the
2425         ;; vertical. 
2426         (if (and (listp (car hor)) 
2427                  (eq (car (car hor)) 'horizontal))
2428             (progn
2429               (split-window 
2430                nil
2431                (if (integerp (nth 1 (car hor)))
2432                    (nth 1 (car hor))
2433                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2434                t)
2435               (setq hor (cdr hor))))
2436
2437         ;; Go through the rules and eval the elements that are to be
2438         ;; evaled.  
2439         (while hor
2440           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2441               (progn
2442                 ;; Expand short buffer name.
2443                 (setq w (aref val 0))
2444                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2445                      (progn
2446                        (setq val (apply 'vector (mapcar 'identity val)))
2447                        (aset val 0 w)))
2448                 (setq ohor (cons val ohor))))
2449           (setq hor (cdr hor)))
2450         (setq rule (cons (nreverse ohor) rule))
2451         (setq r (cdr r)))
2452       (setq rule (nreverse rule))
2453
2454       ;; We tally the window sizes.
2455       (setq total (window-height))
2456       (while rule
2457         (setq hor (car rule))
2458         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2459             (setq hor (cdr hor)))
2460         (setq sub 0)
2461         (while hor
2462           (setq rel (aref (car hor) 1)
2463                 heights (cons
2464                          (cond ((and (floatp rel) (= 1.0 rel))
2465                                 'x)
2466                                ((integerp rel)
2467                                 rel)
2468                                (t
2469                                 (max (floor (* total rel)) 4)))
2470                          heights)
2471                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2472                 hor (cdr hor)))
2473         (setq heights (nreverse heights)
2474               hor (car rule))
2475
2476         ;; We then go through these heighs and create windows for them.
2477         (while heights
2478           (setq height (car heights)
2479                 heights (cdr heights))
2480           (and (eq height 'x)
2481                (setq height (- total sub)))
2482           (and heights
2483                (split-window nil height))
2484           (setq to-buf (aref (car hor) 0))
2485           (switch-to-buffer 
2486            (cond ((not to-buf)
2487                   in-buf)
2488                  ((symbolp to-buf)
2489                   (symbol-value (aref (car hor) 0)))
2490                  (t
2491                   (aref (car hor) 0))))
2492           (and (> (length (car hor)) 2)
2493                (eq (aref (car hor) 2) 'point)
2494                (setq jump-buffer (current-buffer)))
2495           (other-window 1)
2496           (setq hor (cdr hor)))
2497       
2498         (setq rule (cdr rule)))
2499
2500       ;; Finally, we pop to the buffer that's supposed to have point. 
2501       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2502
2503       (select-window (get-buffer-window jump-buffer))
2504       (set-buffer jump-buffer))))
2505
2506 (defun gnus-all-windows-visible-p (rule)
2507   (let (invisible hor jump-buffer val buffer)
2508     ;; Go through the rules and eval the elements that are to be
2509     ;; evaled.  
2510     (while (and rule (not invisible))
2511       (setq hor (car rule)
2512             rule (cdr rule))
2513       (while (and hor (not invisible))
2514         (if (setq val (if (vectorp (car hor)) 
2515                           (car hor)
2516                         (if (not (eq (car (car hor)) 'horizontal))
2517                             (eval (car hor)))))
2518             (progn
2519               ;; Expand short buffer name.
2520               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2521                                (aref val 0)))
2522               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2523                              buffer))
2524               (and (> (length val) 2) (eq 'point (aref val 2))
2525                    (setq jump-buffer buffer))
2526               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2527         (setq hor (cdr hor))))
2528     (and (not invisible) jump-buffer)))
2529
2530       
2531 (defun gnus-remove-some-windows ()
2532   (let ((buffers gnus-window-to-buffer)
2533         (first t)
2534         buf)
2535     (save-excursion
2536       ;; Remove windows on all known Gnus buffers.
2537       (while buffers
2538         (setq buf (cdr (car buffers)))
2539         (if (symbolp buf)
2540             (setq buf (and (boundp buf) (symbol-value buf))))
2541         (and buf 
2542              (get-buffer-window buf)
2543              (progn
2544                (if first
2545                    (progn
2546                      (pop-to-buffer buf)
2547                      (switch-to-buffer nntp-server-buffer)
2548                      (setq first nil))
2549                  (delete-window (get-buffer-window buf)))))
2550         (setq buffers (cdr buffers)))
2551       ;; Remove windows on *all* summary buffers.
2552       (let ((buffers (buffer-list)))
2553         (while buffers
2554           (if (and (string-match 
2555                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2556                    (get-buffer-window (car buffers)))
2557               (delete-window (get-buffer-window (car buffers))))
2558           (setq buffers (cdr buffers)))))))
2559                           
2560 (defun gnus-version ()
2561   "Version numbers of this version of Gnus."
2562   (interactive)
2563   (let ((methods gnus-valid-select-methods)
2564         (mess gnus-version)
2565         meth)
2566     ;; Go through all the legal select methods and add their version
2567     ;; numbers to the total version string. Only the backends that are
2568     ;; currently in use will have their message numbers taken into
2569     ;; consideration. 
2570     (while methods
2571       (setq meth (intern (concat (car (car methods)) "-version")))
2572       (and (boundp meth)
2573            (stringp (symbol-value meth))
2574            (setq mess (concat mess "; " (symbol-value meth))))
2575       (setq methods (cdr methods)))
2576     (gnus-message 2 mess)))
2577
2578 (defun gnus-info-find-node ()
2579   "Find Info documentation of Gnus."
2580   (interactive)
2581   ;; Enlarge info window if needed.
2582   (let ((mode major-mode))
2583     (gnus-configure-windows 'info)
2584     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2585
2586 (defun gnus-overload-functions (&optional overloads)
2587   "Overload functions specified by optional argument OVERLOADS.
2588 If nothing is specified, use the variable gnus-overload-functions."
2589   (let ((defs nil)
2590         (overloads (or overloads gnus-overload-functions)))
2591     (while overloads
2592       (setq defs (car overloads))
2593       (setq overloads (cdr overloads))
2594       ;; Load file before overloading function if necessary.  Make
2595       ;; sure we cannot use `require' always.
2596       (and (not (fboundp (car defs)))
2597            (car (cdr (cdr defs)))
2598            (load (car (cdr (cdr defs))) nil 'nomessage))
2599       (fset (car defs) (car (cdr defs))))))
2600
2601 (defun gnus-replace-chars-in-string (string from to)
2602   "Replace characters in STRING from FROM to TO."
2603   (let ((string (substring string 0))   ;Copy string.
2604         (len (length string))
2605         (idx 0))
2606     ;; Replace all occurrences of FROM with TO.
2607     (while (< idx len)
2608       (if (= (aref string idx) from)
2609           (aset string idx to))
2610       (setq idx (1+ idx)))
2611     string))
2612
2613 (defun gnus-days-between (date1 date2)
2614   ;; Return the number of days between date1 and date2.
2615   (- (gnus-day-number date1) (gnus-day-number date2)))
2616
2617 (defun gnus-day-number (date)
2618   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2619                      (timezone-parse-date date))))
2620     (timezone-absolute-from-gregorian 
2621      (nth 1 dat) (nth 2 dat) (car dat))))
2622
2623 ;; Returns a floating point number that says how many seconds have
2624 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2625 (defun gnus-seconds-since-epoch (date)
2626   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2627                         (timezone-parse-date date)))
2628          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2629                         (timezone-parse-time
2630                          (aref (timezone-parse-date date) 3))))
2631          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2632                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2633          (tday (- (timezone-absolute-from-gregorian 
2634                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2635                   (timezone-absolute-from-gregorian 
2636                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2637     (+ (nth 2 ttime)
2638        (* (nth 1 ttime) 60)
2639        (* 1.0 (nth 0 ttime) 60 60)
2640        (* 1.0 tday 60 60 24))))
2641
2642 (defun gnus-file-newer-than (file date)
2643   (let ((fdate (nth 5 (file-attributes file))))
2644     (or (> (car fdate) (car date))
2645         (and (= (car fdate) (car date))
2646              (> (nth 1 fdate) (nth 1 date))))))
2647
2648 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2649 ;; the echo area.
2650 (defun gnus-y-or-n-p (prompt)
2651   (prog1
2652       (y-or-n-p prompt)
2653     (message "")))
2654
2655 (defun gnus-yes-or-no-p (prompt)
2656   (prog1
2657       (yes-or-no-p prompt)
2658     (message "")))
2659
2660 ;; Check whether to use long file names.
2661 (defun gnus-use-long-file-name (symbol)
2662   ;; The variable has to be set...
2663   (and gnus-use-long-file-name
2664        ;; If it isn't a list, then we return t.
2665        (or (not (listp gnus-use-long-file-name))
2666            ;; If it is a list, and the list contains `symbol', we
2667            ;; return nil.  
2668            (not (memq symbol gnus-use-long-file-name)))))
2669
2670 ;; I suspect there's a better way, but I haven't taken the time to do
2671 ;; it yet. -erik selberg@cs.washington.edu
2672 (defun gnus-dd-mmm (messy-date)
2673   "Return a string like DD-MMM from a big messy string"
2674   (let ((datevec (timezone-parse-date messy-date)))
2675     (format "%2s-%s"
2676             (or (aref datevec 2) "??")
2677             (capitalize
2678              (or (car 
2679                   (nth (1- (string-to-number (aref datevec 1)))
2680                        timezone-months-assoc))
2681                  "???")))))
2682
2683 ;; Make a hash table (default and minimum size is 255).
2684 ;; Optional argument HASHSIZE specifies the table size.
2685 (defun gnus-make-hashtable (&optional hashsize)
2686   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2687
2688 ;; Make a number that is suitable for hashing; bigger than MIN and one
2689 ;; less than 2^x.
2690 (defun gnus-create-hash-size (min)
2691   (let ((i 1))
2692     (while (< i min)
2693       (setq i (* 2 i)))
2694     (1- i)))
2695
2696 ;; Show message if message has a lower level than `gnus-verbose'. 
2697 ;; Guide-line for numbers:
2698 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2699 ;; for things that take a long time, 7 - not very important messages
2700 ;; on stuff, 9 - messages inside loops.
2701 (defun gnus-message (level &rest args)
2702   (if (<= level gnus-verbose)
2703       (apply 'message args)
2704     ;; We have to do this format thingie here even if the result isn't
2705     ;; shown - the return value has to be the same as the return value
2706     ;; from `message'.
2707     (apply 'format args)))
2708
2709 ;; Generate a unique new group name.
2710 (defun gnus-generate-new-group-name (leaf)
2711   (let ((name leaf)
2712         (num 0))
2713     (while (gnus-gethash name gnus-newsrc-hashtb)
2714       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2715     name))
2716
2717 (defun gnus-find-file-noselect (file &optional force)
2718   "Does vaguely the same as find-file-noselect. No hooks are run."
2719   (let (buf insert)
2720     (if (setq buf (get-file-buffer file))
2721         (setq insert force)
2722       (setq buf (create-file-buffer file))
2723       (setq insert t))
2724     (if (not insert)
2725         buf
2726       (save-excursion
2727         (set-buffer buf)
2728         (erase-buffer)
2729         (and (file-readable-p file)
2730              (insert-file-contents file))
2731         (set-visited-file-name file)
2732         (set-buffer-modified-p nil)
2733         (current-buffer)))))
2734
2735 ;;; List and range functions
2736
2737 (defun gnus-last-element (list)
2738   "Return last element of LIST."
2739   (while (cdr list)
2740     (setq list (cdr list)))
2741   (car list))
2742
2743 (defun gnus-copy-sequence (list)
2744   "Do a complete, total copy of a list."
2745   (if (and (consp list) (not (consp (cdr list))))
2746       (cons (car list) (cdr list))
2747     (mapcar (lambda (elem) (if (consp elem) 
2748                                (if (consp (cdr elem))
2749                                    (gnus-copy-sequence elem)
2750                                  (cons (car elem) (cdr elem)))
2751                              elem))
2752             list)))
2753
2754 (defun gnus-set-difference (list1 list2)
2755   "Return a list of elements of LIST1 that do not appear in LIST2."
2756   (let ((list1 (copy-sequence list1)))
2757     (while list2
2758       (setq list1 (delq (car list2) list1))
2759       (setq list2 (cdr list2)))
2760     list1))
2761
2762 (defun gnus-sorted-complement (list1 list2)
2763   "Return a list of elements of LIST1 that do not appear in LIST2.
2764 Both lists have to be sorted over <."
2765   (let (out)
2766     (if (or (null list1) (null list2))
2767         (or list1 list2)
2768       (while (and list1 list2)
2769         (cond ((= (car list1) (car list2))
2770                (setq list1 (cdr list1)
2771                      list2 (cdr list2)))
2772               ((< (car list1) (car list2))
2773                (setq out (cons (car list1) out))
2774                (setq list1 (cdr list1)))
2775               (t
2776                (setq out (cons (car list2) out))
2777                (setq list2 (cdr list2)))))
2778       (nconc (nreverse out) (or list1 list2)))))
2779
2780 (defun gnus-intersection (list1 list2)      
2781   (let ((result nil))
2782     (while list2
2783       (if (memq (car list2) list1)
2784           (setq result (cons (car list2) result)))
2785       (setq list2 (cdr list2)))
2786     result))
2787
2788 (defun gnus-sorted-intersection (list1 list2)
2789   ;; LIST1 and LIST2 have to be sorted over <.
2790   (let (out)
2791     (while (and list1 list2)
2792       (cond ((= (car list1) (car list2))
2793              (setq out (cons (car list1) out)
2794                    list1 (cdr list1)
2795                    list2 (cdr list2)))
2796             ((< (car list1) (car list2))
2797              (setq list1 (cdr list1)))
2798             (t
2799              (setq list2 (cdr list2)))))
2800     (nreverse out)))
2801
2802 (defun gnus-set-sorted-intersection (list1 list2)
2803   ;; LIST1 and LIST2 have to be sorted over <.
2804   ;; This function modifies LIST1.
2805   (let* ((top (cons nil list1))
2806          (prev top))
2807   (while (and list1 list2)
2808     (cond ((= (car list1) (car list2))
2809            (setq prev list1
2810                  list1 (cdr list1)
2811                  list2 (cdr list2)))
2812           ((< (car list1) (car list2))
2813            (setcdr prev (cdr list1))
2814            (setq list1 (cdr list1)))
2815           (t
2816            (setq list2 (cdr list2)))))
2817   (setcdr prev nil)
2818   (cdr top)))
2819
2820 (defun gnus-compress-sequence (numbers &optional always-list)
2821   "Convert list of numbers to a list of ranges or a single range.
2822 If ALWAYS-LIST is non-nil, this function will always release a list of
2823 ranges."
2824   (let* ((first (car numbers))
2825          (last (car numbers))
2826          result)
2827     (if (null numbers)
2828         nil
2829       (if (not (listp (cdr numbers)))
2830           numbers
2831         (while numbers
2832           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2833                 ((= (1+ last) (car numbers)) ;Still in sequence
2834                  (setq last (car numbers)))
2835                 (t                      ;End of one sequence
2836                  (setq result 
2837                        (cons (if (= first last) first
2838                                (cons first last)) result))
2839                  (setq first (car numbers))
2840                  (setq last  (car numbers))))
2841           (setq numbers (cdr numbers)))
2842         (if (and (not always-list) (null result))
2843             (if (= first last) (list first) (cons first last))
2844           (nreverse (cons (if (= first last) first (cons first last))
2845                           result)))))))
2846
2847 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2848 (defun gnus-uncompress-range (ranges)
2849   "Expand a list of ranges into a list of numbers.
2850 RANGES is either a single range on the form `(num . num)' or a list of
2851 these ranges."
2852   (let (first last result)
2853     (cond 
2854      ((null ranges)
2855       nil)
2856      ((not (listp (cdr ranges)))
2857       (setq first (car ranges))
2858       (setq last (cdr ranges))
2859       (while (<= first last)
2860         (setq result (cons first result))
2861         (setq first (1+ first)))
2862       (nreverse result))
2863      (t
2864       (while ranges
2865         (if (atom (car ranges))
2866             (if (numberp (car ranges))
2867                 (setq result (cons (car ranges) result)))
2868           (setq first (car (car ranges)))
2869           (setq last  (cdr (car ranges)))
2870           (while (<= first last)
2871             (setq result (cons first result))
2872             (setq first (1+ first))))
2873         (setq ranges (cdr ranges)))
2874       (nreverse result)))))
2875
2876 (defun gnus-add-to-range (ranges list)
2877   "Return a list of ranges that has all articles from both RANGES and LIST.
2878 Note: LIST has to be sorted over `<'."
2879   (if (not ranges)
2880       (gnus-compress-sequence list t)
2881     (setq list (copy-sequence list))
2882     (or (listp (cdr ranges))
2883         (setq ranges (list ranges)))
2884     (let ((out ranges)
2885           ilist lowest highest temp)
2886       (while (and ranges list)
2887         (setq ilist list)
2888         (setq lowest (or (and (atom (car ranges)) (car ranges))
2889                          (car (car ranges))))
2890         (while (and list (cdr list) (< (car (cdr list)) lowest))
2891           (setq list (cdr list)))
2892         (if (< (car ilist) lowest)
2893             (progn
2894               (setq temp list)
2895               (setq list (cdr list))
2896               (setcdr temp nil)
2897               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2898         (setq highest (or (and (atom (car ranges)) (car ranges))
2899                           (cdr (car ranges))))
2900         (while (and list (<= (car list) highest))
2901           (setq list (cdr list)))
2902         (setq ranges (cdr ranges)))
2903       (if list
2904           (setq out (nconc (gnus-compress-sequence list t) out)))
2905       (setq out (sort out (lambda (r1 r2) 
2906                             (< (or (and (atom r1) r1) (car r1))
2907                                (or (and (atom r2) r2) (car r2))))))
2908       (setq ranges out)
2909       (while ranges
2910         (if (atom (car ranges))
2911             (if (cdr ranges)
2912                 (if (atom (car (cdr ranges)))
2913                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2914                         (progn
2915                           (setcar ranges (cons (car ranges) 
2916                                                (car (cdr ranges))))
2917                           (setcdr ranges (cdr (cdr ranges)))))
2918                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2919                       (progn
2920                         (setcar (car (cdr ranges)) (car ranges))
2921                         (setcar ranges (car (cdr ranges)))
2922                         (setcdr ranges (cdr (cdr ranges)))))))
2923           (if (cdr ranges)
2924               (if (atom (car (cdr ranges)))
2925                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2926                       (progn
2927                         (setcdr (car ranges) (car (cdr ranges)))
2928                         (setcdr ranges (cdr (cdr ranges)))))
2929                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2930                     (progn
2931                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2932                       (setcdr ranges (cdr (cdr ranges))))))))
2933         (setq ranges (cdr ranges)))
2934       out)))
2935
2936 (defun gnus-remove-from-range (ranges list)
2937   "Return a list of ranges that has all articles from LIST removed from RANGES.
2938 Note: LIST has to be sorted over `<'."
2939   ;; !!! This function shouldn't look like this, but I've got a headache.
2940   (gnus-compress-sequence 
2941    (gnus-sorted-complement
2942     (gnus-uncompress-range ranges) list)))
2943
2944 (defun gnus-member-of-range (number ranges)
2945   (if (not (listp (cdr ranges)))
2946       (and (>= number (car ranges)) 
2947            (<= number (cdr ranges)))
2948     (let ((not-stop t))
2949       (while (and ranges 
2950                   (if (numberp (car ranges))
2951                       (>= number (car ranges))
2952                     (>= number (car (car ranges))))
2953                   not-stop)
2954         (if (if (numberp (car ranges))
2955                 (= number (car ranges))
2956               (and (>= number (car (car ranges)))
2957                    (<= number (cdr (car ranges)))))
2958             (setq not-stop nil))
2959         (setq ranges (cdr ranges)))
2960       (not not-stop))))
2961
2962 \f
2963 ;;;
2964 ;;; Gnus group mode
2965 ;;;
2966
2967 (defvar gnus-group-mode-map nil)
2968 (defvar gnus-group-group-map nil)
2969 (defvar gnus-group-mark-map nil)
2970 (defvar gnus-group-list-map nil)
2971 (defvar gnus-group-sub-map nil)
2972 (put 'gnus-group-mode 'mode-class 'special)
2973
2974 (if gnus-group-mode-map
2975     nil
2976   (setq gnus-group-mode-map (make-keymap))
2977   (suppress-keymap gnus-group-mode-map)
2978   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2979   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2980   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2981   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2982   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2983   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2984   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2985   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2986   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2987   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2988   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2989   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2990   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2991   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2992   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2993   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2994   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2995   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2996   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2997   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2998   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2999   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3000   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3001   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3002   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3003   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3004   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3005   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3006   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3007   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3008   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3009   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3010   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3011   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3012   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3013   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3014   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3015   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3016   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3017   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3018   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3019   (define-key gnus-group-mode-map "V" 'gnus-version)
3020   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3021   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3022   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3023   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3024   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3025   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3026   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3027   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3028   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3029   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3030   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3031   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3032   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3033   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3034   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3035
3036   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3037   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3038   (define-prefix-command 'gnus-group-mark-map)
3039   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3040   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3041   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3042   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3043
3044   (define-prefix-command 'gnus-group-group-map)
3045   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3046   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3047   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3048   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3049   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3050   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3051   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3052   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3053   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3054   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3055   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3056   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3057   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3058   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3059   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3060   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3061   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3062   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3063
3064   (define-prefix-command 'gnus-group-list-map)
3065   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3066   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3067   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3068   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3069   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3070   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3071   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3072   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3073   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3074
3075   (define-prefix-command 'gnus-group-sub-map)
3076   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3077   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3078   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3079   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3080   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3081   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3082   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3083   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3084
3085 (defun gnus-group-mode ()
3086   "Major mode for reading news.
3087
3088 All normal editing commands are switched off.
3089 \\<gnus-group-mode-map>
3090 The group buffer lists (some of) the groups available.  For instance,
3091 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3092 lists all zombie groups. 
3093
3094 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3095 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3096
3097 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3098
3099 The following commands are available:
3100
3101 \\{gnus-group-mode-map}"
3102   (interactive)
3103   (if gnus-visual (gnus-group-make-menu-bar))
3104   (kill-all-local-variables)
3105   (setq mode-line-modified "-- ")
3106   (make-local-variable 'mode-line-format)
3107   (setq mode-line-format (copy-sequence mode-line-format))
3108   (and (equal (nth 3 mode-line-format) "   ")
3109        (setcar (nthcdr 3 mode-line-format) ""))
3110   (setq major-mode 'gnus-group-mode)
3111   (setq mode-name "Group")
3112   (gnus-group-set-mode-line)
3113   (setq mode-line-process nil)
3114   (use-local-map gnus-group-mode-map)
3115   (buffer-disable-undo (current-buffer))
3116   (setq truncate-lines t)
3117   (setq buffer-read-only t)
3118   (run-hooks 'gnus-group-mode-hook))
3119
3120 (defun gnus-mouse-pick-group (e)
3121   (interactive "e")
3122   (mouse-set-point e)
3123   (gnus-group-read-group nil))
3124
3125 ;;;###autoload
3126 (defun gnus-no-server (&optional arg)
3127   "Read network news.
3128 If ARG is a positive number, Gnus will use that as the
3129 startup level. If ARG is nil, Gnus will be started at level 2. 
3130 If ARG is non-nil and not a positive number, Gnus will
3131 prompt the user for the name of an NNTP server to use.
3132 As opposed to `gnus', this command will not connect to the local server."
3133   (interactive "P")
3134   (setq gnus-group-use-permanent-levels t)
3135   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3136
3137 (defalias '\(ding\) 'gnus)
3138
3139 ;;;###autoload
3140 (defun gnus (&optional arg dont-connect)
3141   "Read network news.
3142 If ARG is non-nil and a positive number, Gnus will use that as the
3143 startup level. If ARG is non-nil and not a positive number, Gnus will
3144 prompt the user for the name of an NNTP server to use."
3145   (interactive "P")
3146   (if (get-buffer gnus-group-buffer)
3147       (progn
3148         (switch-to-buffer gnus-group-buffer)
3149         (gnus-group-get-new-news))
3150     (gnus-clear-system)
3151     (nnheader-init-server-buffer)
3152     (gnus-read-init-file)
3153
3154     ;; Read the dribble file.
3155     (and gnus-use-dribble-file (gnus-dribble-read-file))
3156
3157     (let ((level (and arg (numberp arg) (> arg 0) arg))
3158           did-connect)
3159       (unwind-protect
3160           (progn
3161             (gnus-group-setup-buffer)
3162             (or dont-connect 
3163                 (setq did-connect
3164                       (gnus-start-news-server (and arg (not level))))))
3165         (if (and (not dont-connect) 
3166                  (not did-connect))
3167             (gnus-group-quit)
3168           (run-hooks 'gnus-startup-hook)
3169           ;; NNTP server is successfully open. 
3170           (gnus-update-format-specifications)
3171           (gnus-summary-make-display-table)
3172           (let ((buffer-read-only nil))
3173             (erase-buffer)
3174             (if (not gnus-inhibit-startup-message)
3175                 (progn
3176                   (gnus-group-startup-message)
3177                   (sit-for 0))))
3178           (gnus-setup-news nil level)
3179           (and gnus-use-dribble-file (gnus-dribble-open))
3180           (gnus-group-list-groups level)
3181           (gnus-configure-windows 'group))))))
3182
3183 (defun gnus-unload ()
3184   "Unload all Gnus features."
3185   (interactive)
3186   (let ((history load-history)
3187         feature)
3188     (while history
3189       (and (string-match "^gnus" (car (car history)))
3190            (setq feature (cdr (assq 'provide (car history))))
3191            (unload-feature feature 'force))
3192       (setq history (cdr history)))))
3193
3194 (defun gnus-group-startup-message (&optional x y)
3195   "Insert startup message in current buffer."
3196   ;; Insert the message.
3197   (erase-buffer)
3198   (insert
3199    (format "
3200      %s
3201            A newsreader 
3202       for GNU Emacs
3203
3204         Based on GNUS 
3205              written by 
3206      Masanobu UMEDA
3207
3208        A Praxis Release
3209       larsi@ifi.uio.no
3210
3211            gnus-version))
3212   ;; And then hack it.
3213   ;; 18 is the longest line.
3214   (indent-rigidly (point-min) (point-max) 
3215                   (/ (max (- (window-width) (or x 28)) 0) 2))
3216   (goto-char (point-min))
3217   ;; +4 is fuzzy factor.
3218   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3219
3220   ;; Fontify some.
3221   (goto-char (point-min))
3222   (search-forward "Praxis")
3223   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3224   (goto-char (point-min)))
3225
3226 (defun gnus-group-setup-buffer ()
3227   (or (get-buffer gnus-group-buffer)
3228       (progn
3229         (switch-to-buffer gnus-group-buffer)
3230         (gnus-add-current-to-buffer-list)
3231         (gnus-group-mode)
3232         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3233
3234 (defun gnus-group-list-groups (level &optional unread)
3235   "List newsgroups with level LEVEL or lower that have unread articles.
3236 Default is all subscribed groups.
3237 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3238   (interactive (list (and current-prefix-arg
3239                           (prefix-numeric-value current-prefix-arg))))
3240   (if gnus-group-use-permanent-levels
3241       (progn
3242         (setq gnus-group-default-list-level 
3243               (or level gnus-group-default-list-level))
3244         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3245     (setq level (or level gnus-group-default-list-level 
3246                     gnus-level-subscribed)))
3247   (gnus-group-setup-buffer)     ;May call from out of group buffer
3248   (let ((case-fold-search nil)
3249         (group (gnus-group-group-name)))
3250     (funcall gnus-group-prepare-function level unread nil)
3251     (if (zerop (buffer-size))
3252         (gnus-message 5 gnus-no-groups-message)
3253       (goto-char (point-min))
3254       (if (not group)
3255           ;; Go to the first group with unread articles.
3256           (gnus-group-search-forward nil nil nil t)
3257         ;; Find the right group to put point on. If the current group
3258         ;; has disapeared in the new listing, try to find the next
3259         ;; one. If no next one can be found, just leave point at the
3260         ;; first newsgroup in the buffer.
3261         (if (not (gnus-goto-char
3262                   (text-property-any (point-min) (point-max) 
3263                                      'gnus-group (intern group))))
3264             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3265               (while (and newsrc
3266                           (not (gnus-goto-char 
3267                                 (text-property-any 
3268                                  (point-min) (point-max) 'gnus-group 
3269                                  (intern (car (car newsrc)))))))
3270                 (setq newsrc (cdr newsrc)))
3271               (or newsrc (progn (goto-char (point-max))
3272                                 (forward-line -1))))))
3273       ;; Adjust cursor point.
3274       (gnus-group-position-cursor))))
3275
3276 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3277   "List all newsgroups with unread articles of level LEVEL or lower.
3278 If ALL is non-nil, list groups that have no unread articles.
3279 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3280 If REGEXP, only list groups matching REGEXP."
3281   (set-buffer gnus-group-buffer)
3282   (let ((buffer-read-only nil)
3283         (newsrc (cdr gnus-newsrc-alist))
3284         (lowest (or lowest 1))
3285         info clevel unread group)
3286     (erase-buffer)
3287     (if (< lowest gnus-level-zombie)
3288         ;; List living groups.
3289         (while newsrc
3290           (setq info (car newsrc)
3291                 group (car info)
3292                 newsrc (cdr newsrc)
3293                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3294           (and unread ; This group might be bogus
3295                (or (not regexp)
3296                    (string-match regexp group))
3297                (<= (setq clevel (car (cdr info))) level) 
3298                (>= clevel lowest)
3299                (or all            ; We list all groups?
3300                    (eq unread t)  ; We list unactivated groups
3301                    (> unread 0)   ; We list groups with unread articles
3302                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3303                (gnus-group-insert-group-line 
3304                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3305
3306     ;; List dead groups.
3307     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3308          (gnus-group-prepare-flat-list-dead 
3309           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3310           gnus-level-zombie ?Z
3311           regexp))
3312     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3313          (gnus-group-prepare-flat-list-dead 
3314           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3315           gnus-level-killed ?K regexp))
3316
3317     (gnus-group-set-mode-line)
3318     (setq gnus-have-all-newsgroups all)
3319     (run-hooks 'gnus-group-prepare-hook)))
3320
3321 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3322   ;; List zombies and killed lists somehwat faster, which was
3323   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3324   ;; this by ignoring the group format specification altogether.
3325   (let (group beg)
3326     (while groups
3327       (setq group (car groups)
3328             groups (cdr groups))
3329       (if (or (not regexp)
3330               (string-match regexp group))
3331           (progn
3332             (setq beg (point))
3333             (insert (format " %c     *: %s\n" mark group))
3334             (add-text-properties 
3335              beg (1+ beg) 
3336              (list 'gnus-group (intern group)
3337                    'gnus-unread t
3338                    'gnus-level level)))))))
3339
3340 (defun gnus-group-real-name (group)
3341   "Find the real name of a foreign newsgroup."
3342   (if (string-match ":[^:]+$" group)
3343       (substring group (1+ (match-beginning 0)))
3344     group))
3345
3346 (defun gnus-group-prefixed-name (group method)
3347   "Return the whole name from GROUP and METHOD."
3348   (and (stringp method) (setq method (gnus-server-to-method method)))
3349   (concat (format "%s" (car method))
3350           (if (and 
3351                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3352                (not (string= (nth 1 method) "")))
3353               (concat "+" (nth 1 method)))
3354           ":" group))
3355
3356 (defun gnus-group-real-prefix (group)
3357   "Return the prefix of the current group name."
3358   (if (string-match "^[^:]+:" group)
3359       (substring group 0 (match-end 0))
3360     ""))
3361
3362 (defun gnus-group-method-name (group)
3363   "Return the method used for selecting GROUP."
3364   (let ((prefix (gnus-group-real-prefix group)))
3365     (if (equal prefix "")
3366         gnus-select-method
3367       (if (string-match "^[^\\+]+\\+" prefix)
3368           (list (intern (substring prefix 0 (1- (match-end 0))))
3369                 (substring prefix (match-end 0) (1- (length prefix))))
3370         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3371
3372 (defun gnus-group-foreign-p (group)
3373   "Return nil if GROUP is native, non-nil if it is foreign."
3374   (string-match ":" group))
3375
3376 (defun gnus-group-set-info (info &optional method-only-group part)
3377   (let* ((entry (gnus-gethash
3378                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3379          (part-info info)
3380          (info (if method-only-group (nth 2 entry) info)))
3381     (if (not method-only-group)
3382         ()
3383       (or entry
3384           (error "Trying to change non-existent group %s" method-only-group))
3385       ;; We have recevied parts of the actual group info - either the
3386       ;; select method or the group parameters.  We first check
3387       ;; whether we have to extend the info, and if so, do that.
3388       (let ((len (length info))
3389             (total (if (eq part 'method) 5 6)))
3390         (and (< len total)
3391              (setcdr (nthcdr (1- len) info)
3392                      (make-list (- total len) nil)))
3393         ;; Then we enter the new info.
3394         (setcar (nthcdr (1- total) info) part-info)))
3395     ;; We uncompress some lists of marked articles.
3396     (let (marked)
3397       (if (not (setq marked (nth 3 info)))
3398           ()
3399         (while marked
3400           (or (eq 'score (car (car marked)))
3401               (eq 'bookmark (car (car marked)))
3402               (eq 'killed (car (car marked)))
3403               (setcdr (car marked) 
3404                       (gnus-uncompress-range (cdr (car marked)))))
3405           (setq marked (cdr marked)))))
3406     (if entry
3407         ()
3408       ;; This is a new group, so we just create it.
3409       (save-excursion
3410         (set-buffer gnus-group-buffer)
3411         (if (nth 4 info)
3412             ;; It's a foreign group...
3413             (gnus-group-make-group 
3414              (gnus-group-real-name (car info))
3415              (prin1-to-string (car (nth 4 info)))
3416              (nth 1 (nth 4 info)))
3417           ;; It's a native group.
3418           (gnus-group-make-group
3419            (car info)
3420            (prin1-to-string (car gnus-select-method))
3421            (nth 1 gnus-select-method)))
3422         (gnus-message 6 "Note: New group created")
3423         (setq entry 
3424               (gnus-gethash (gnus-group-prefixed-name 
3425                              (gnus-group-real-name (car info))
3426                              (or (nth 4 info) gnus-select-method))
3427                             gnus-newsrc-hashtb))))
3428     ;; Whether it was a new group or not, we now have the entry, so we
3429     ;; can do the update.
3430     (if entry
3431         (progn
3432           (setcar (nthcdr 2 entry) info)
3433           (if (and (not (eq (car entry) t)) 
3434                    (gnus-gethash (car info) gnus-active-hashtb))
3435               (let ((marked (nth 3 info)))
3436                 (setcar entry 
3437                         (max 0 (- (length (gnus-list-of-unread-articles 
3438                                            (car info)))
3439                                   (length (cdr (assq 'tick marked)))
3440                                   (length (cdr (assq 'dormant marked)))))))))
3441       (error "No such group: %s" (car info)))))
3442
3443 (defun gnus-group-set-method-info (group select-method)
3444   (gnus-group-set-info select-method group 'method))
3445
3446 (defun gnus-group-set-params-info (group params)
3447   (gnus-group-set-info params group 'params))
3448
3449 (defun gnus-group-update-group-line ()
3450   "This function updates the current line in the newsgroup buffer and
3451 moves the point to the colon."
3452   (let* ((buffer-read-only nil)
3453          (group (gnus-group-group-name))
3454          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3455     (if entry
3456         (gnus-dribble-enter 
3457          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3458                  ")")))
3459     (beginning-of-line)
3460     (delete-region (point) (progn (forward-line 1) (point)))
3461     (gnus-group-insert-group-line-info group)
3462     (forward-line -1)
3463     (gnus-group-position-cursor)))
3464
3465 (defun gnus-group-insert-group-line-info (group)
3466   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3467         active info)
3468     (if entry
3469         (progn
3470           (setq info (nth 2 entry))
3471           (gnus-group-insert-group-line 
3472            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3473       (setq active (gnus-gethash group gnus-active-hashtb))
3474       (gnus-group-insert-group-line 
3475        nil group 
3476        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3477        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3478
3479 (defun gnus-group-insert-group-line (gformat group level marked number method)
3480   (let* ((gformat (or gformat gnus-group-line-format-spec))
3481          (active (gnus-gethash group gnus-active-hashtb))
3482          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3483          (number-of-dormant (length (cdr (assq 'dormant marked))))
3484          (number-of-ticked (length (cdr (assq 'tick marked))))
3485          (number-of-ticked-and-dormant
3486           (+ number-of-ticked number-of-dormant))
3487          (number-of-unread-unticked 
3488           (if (numberp number) (int-to-string (max 0 number))
3489             "*"))
3490          (number-of-read
3491           (if (numberp number)
3492               (max 0 (- number-total number))
3493             "*"))
3494          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3495                            ((<= level gnus-level-unsubscribed) ?U)
3496                            ((= level gnus-level-zombie) ?Z)
3497                            (t ?K)))
3498          (qualified-group (gnus-group-real-name group))
3499          (newsgroup-description 
3500           (if gnus-description-hashtb
3501               (or (gnus-gethash group gnus-description-hashtb) "")
3502             ""))
3503          (moderated (if (member group gnus-moderated-list) ?m ? ))
3504          (moderated-string (if (eq moderated ?m) "(m)" ""))
3505          (method (gnus-server-get-method group method))
3506          (news-server (or (car (cdr method)) ""))
3507          (news-method (or (car method) ""))
3508          (news-method-string 
3509           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3510          (marked (if (and 
3511                       (numberp number) 
3512                       (zerop number)
3513                       (> number-of-ticked 0))
3514                      ?* ? ))
3515          (number (if (eq number t) "*" (+ number number-of-dormant 
3516                                           number-of-ticked)))
3517          (process-marked (if (member qualified-group gnus-group-marked)
3518                              gnus-process-mark ? ))
3519          (buffer-read-only nil)
3520          header ; passed as parameter to user-funcs.
3521          b)
3522     (beginning-of-line)
3523     (setq b (point))
3524     ;; Insert the text.
3525     (insert (eval gformat))
3526
3527     (add-text-properties 
3528      b (1+ b) (list 'gnus-group (intern group)
3529                     'gnus-unread (if (numberp number)
3530                                      (string-to-int number-of-unread-unticked)
3531                                    t)
3532                     'gnus-marked marked
3533                     'gnus-level level))))
3534
3535 (defun gnus-group-update-group (group &optional visible-only)
3536   "Update newsgroup info of GROUP.
3537 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3538   (save-excursion
3539     (set-buffer gnus-group-buffer)
3540     (let ((buffer-read-only nil)
3541           visible)
3542       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3543         (if entry
3544             (gnus-dribble-enter 
3545              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3546                      ")"))))
3547       ;; Buffer may be narrowed.
3548       (save-restriction
3549         (widen)
3550         ;; Search a line to modify.  If the buffer is large, the search
3551         ;; takes long time.  In most cases, current point is on the line
3552         ;; we are looking for.  So, first of all, check current line. 
3553         (if (or (progn
3554                   (beginning-of-line)
3555                   (eq (get-text-property (point) 'gnus-group)
3556                       (intern group)))
3557                 (progn
3558                   (gnus-goto-char 
3559                    (text-property-any 
3560                     (point-min) (point-max) 'gnus-group (intern group)))))
3561             ;; GROUP is listed in current buffer. So, delete old line.
3562             (progn
3563               (setq visible t)
3564               (beginning-of-line)
3565               (delete-region (point) (progn (forward-line 1) (point))))
3566           ;; No such line in the buffer, find out where it's supposed to
3567           ;; go, and insert it there (or at the end of the buffer).
3568           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3569           (or visible-only
3570               (let ((entry 
3571                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3572                 (while (and entry
3573                             (car entry)
3574                             (not
3575                              (gnus-goto-char
3576                               (text-property-any
3577                                (point-min) (point-max) 
3578                                'gnus-group (intern (car (car entry)))))))
3579                   (setq entry (cdr entry)))
3580                 (or entry (goto-char (point-max)))))))
3581       (if (or visible (not visible-only))
3582           (gnus-group-insert-group-line-info group))
3583       (gnus-group-set-mode-line))))
3584
3585 (defun gnus-group-set-mode-line ()
3586   (if (memq 'group gnus-updated-mode-lines)
3587       (let* ((gformat (or gnus-group-mode-line-format-spec
3588                           (setq gnus-group-mode-line-format-spec
3589                                 (gnus-parse-format 
3590                                  gnus-group-mode-line-format 
3591                                  gnus-group-mode-line-format-alist))))
3592              (news-server (car (cdr gnus-select-method)))
3593              (news-method (car gnus-select-method))
3594              (max-len 60)
3595              (mode-string (eval gformat)))
3596         (setq mode-string (eval gformat))
3597         (if (> (length mode-string) max-len) 
3598             (setq mode-string (substring mode-string 0 (- max-len 4))))
3599         (setq mode-line-buffer-identification mode-string)
3600         (set-buffer-modified-p t))))
3601
3602 (defun gnus-group-group-name ()
3603   "Get the name of the newsgroup on the current line."
3604   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3605     (and group (symbol-name group))))
3606
3607 (defun gnus-group-group-level ()
3608   "Get the level of the newsgroup on the current line."
3609   (get-text-property (gnus-point-at-bol) 'gnus-level))
3610
3611 (defun gnus-group-group-unread ()
3612   "Get the number of unread articles of the newsgroup on the current line."
3613   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3614
3615 (defun gnus-group-search-forward (&optional backward all level first-too)
3616   "Find the next newsgroup with unread articles.
3617 If BACKWARD is non-nil, find the previous newsgroup instead.
3618 If ALL is non-nil, just find any newsgroup.
3619 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3620 group exists.
3621 If FIRST-TOO, the current line is also eligible as a target."
3622   (let ((way (if backward -1 1))
3623         (low gnus-level-killed)
3624         (beg (point))
3625         pos found lev)
3626     (if (and backward (progn (beginning-of-line)) (bobp))
3627         nil
3628       (or first-too (forward-line way))
3629       (while (and 
3630               (not (eobp))
3631               (not (setq 
3632                     found 
3633                     (and (or all
3634                              (and
3635                               (let ((unread 
3636                                      (get-text-property (point) 'gnus-unread)))
3637                                 (or (eq unread t) (and unread (> unread 0))))
3638                               (setq lev (get-text-property (point)
3639                                                            'gnus-level))
3640                               (<= lev gnus-level-subscribed)))
3641                          (or (not level)
3642                              (and (setq lev (get-text-property (point)
3643                                                                'gnus-level))
3644                                   (or (= lev level)
3645                                       (and (< lev low)
3646                                            (< level lev)
3647                                            (progn
3648                                              (setq low lev)
3649                                              (setq pos (point))
3650                                              nil))))))))
3651               (zerop (forward-line way)))))
3652     (if found 
3653         (progn (gnus-group-position-cursor) t)
3654       (goto-char (or pos beg))
3655       (and pos t))))
3656
3657 ;;; Gnus group mode commands
3658
3659 ;; Group marking.
3660
3661 (defun gnus-group-mark-group (n &optional unmark no-advance)
3662   "Mark the current group."
3663   (interactive "p")
3664   (let ((buffer-read-only nil)
3665         group)
3666     (while 
3667         (and (> n 0) 
3668              (setq group (gnus-group-group-name))
3669              (progn
3670                (beginning-of-line)
3671                (forward-char 2)
3672                (delete-char 1)
3673                (if unmark
3674                    (progn
3675                      (insert " ")
3676                      (setq gnus-group-marked (delete group gnus-group-marked)))
3677                  (insert "#")
3678                  (setq gnus-group-marked
3679                        (cons group (delete group gnus-group-marked))))
3680                t)
3681              (or no-advance (zerop (gnus-group-next-group 1))))
3682       (setq n (1- n)))
3683     (gnus-summary-position-cursor)
3684     n))
3685
3686 (defun gnus-group-unmark-group (n)
3687   "Remove the mark from the current group."
3688   (interactive "p")
3689   (gnus-group-mark-group n 'unmark))
3690
3691 (defun gnus-group-mark-region (unmark beg end)
3692   "Mark all groups between point and mark.
3693 If UNMARK, remove the mark instead."
3694   (interactive "P\nr")
3695   (let ((num (count-lines beg end)))
3696     (save-excursion
3697       (goto-char beg)
3698       (- num (gnus-group-mark-group num unmark)))))
3699
3700 (defun gnus-group-remove-mark (group)
3701   (and (gnus-group-goto-group group)
3702        (save-excursion
3703          (gnus-group-mark-group 1 'unmark t))))
3704
3705 ;; Return a list of groups to work on.  Take into consideration N (the
3706 ;; prefix) and the list of marked groups.
3707 (defun gnus-group-process-prefix (n)
3708   (cond (n
3709          (setq n (prefix-numeric-value n))
3710          ;; There is a prefix, so we return a list of the N next
3711          ;; groups. 
3712          (let ((way (if (< n 0) -1 1))
3713                (n (abs n))
3714                group groups)
3715            (save-excursion
3716              (while (and (> n 0)
3717                          (setq group (gnus-group-group-name)))
3718                (setq groups (cons group groups))
3719                (setq n (1- n))
3720                (forward-line way)))
3721            (nreverse groups)))
3722         (gnus-group-marked
3723          ;; No prefix, but a list of marked articles.
3724          (reverse gnus-group-marked))
3725         (t
3726          ;; Neither marked articles or a prefix, so we return the
3727          ;; current group.
3728          (let ((group (gnus-group-group-name)))
3729            (and group (list group))))))
3730
3731 ;; Selecting groups.
3732
3733 (defun gnus-group-read-group (&optional all no-article group)
3734   "Read news in this newsgroup.
3735 If the prefix argument ALL is non-nil, already read articles become
3736 readable. If the optional argument NO-ARTICLE is non-nil, no article
3737 will be auto-selected upon group entry."
3738   (interactive "P")
3739   (let ((group (or group (gnus-group-group-name)))
3740         number active marked entry)
3741     (or group (error "No group on current line"))
3742     (setq marked 
3743           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3744     ;; This group might be a dead group. In that case we have to get
3745     ;; the number of unread articles from `gnus-active-hashtb'.
3746     (if entry
3747         (setq number (car entry))
3748       (if (setq active (gnus-gethash group gnus-active-hashtb))
3749           (setq number (- (1+ (cdr active)) (car active)))))
3750     (gnus-summary-read-group 
3751      group (or all (and (numberp number) 
3752                         (zerop (+ number (length (cdr (assq 'tick marked)))
3753                                   (length (cdr (assq 'dormant marked)))))))
3754      no-article)))
3755
3756 (defun gnus-group-select-group (&optional all)
3757   "Select this newsgroup.
3758 No article is selected automatically.
3759 If argument ALL is non-nil, already read articles become readable."
3760   (interactive "P")
3761   (gnus-group-read-group all t))
3762
3763 ;; Enter a group that is not in the group buffer. Non-nil is returned
3764 ;; if selection was successful.
3765 (defun gnus-group-read-ephemeral-group 
3766   (group method &optional activate quit-config)
3767   (let ((group (if (gnus-group-foreign-p group) group
3768                  (gnus-group-prefixed-name group method))))
3769     (gnus-sethash 
3770      group
3771      (list t nil (list group gnus-level-default-subscribed nil nil 
3772                        (append method
3773                                (list
3774                                 (list 'quit-config 
3775                                       (if quit-config quit-config
3776                                         (cons (current-buffer) 'summary)))))))
3777      gnus-newsrc-hashtb)
3778     (set-buffer gnus-group-buffer)
3779     (or (gnus-server-opened method)
3780         (gnus-open-server method)
3781         (error "Unable to contact server: %s" (gnus-status-message method)))
3782     (if activate (or (gnus-request-group group)
3783                      (error "Couldn't request group")))
3784     (condition-case ()
3785         (gnus-group-read-group t t group)
3786       (error nil)
3787       (quit nil))
3788     (not (equal major-mode 'gnus-group-mode))))
3789   
3790 (defun gnus-group-jump-to-group (group)
3791   "Jump to newsgroup GROUP."
3792   (interactive 
3793    (list (completing-read 
3794           "Group: " gnus-active-hashtb nil 
3795           (memq gnus-select-method gnus-have-read-active-file))))
3796
3797   (if (equal group "")
3798       (error "Empty group name"))
3799
3800   (let ((b (text-property-any 
3801             (point-min) (point-max) 'gnus-group (intern group))))
3802     (if b
3803         ;; Either go to the line in the group buffer...
3804         (goto-char b)
3805       ;; ... or insert the line.
3806       (or
3807        (gnus-gethash group gnus-active-hashtb)
3808        (gnus-activate-newsgroup group)
3809        (error "%s error: %s" group (gnus-status-message group)))
3810
3811       (gnus-group-update-group group)
3812       (goto-char (text-property-any 
3813                   (point-min) (point-max) 'gnus-group (intern group)))))
3814   ;; Adjust cursor point.
3815   (gnus-group-position-cursor))
3816
3817 (defun gnus-group-goto-group (group)
3818   "Goto to newsgroup GROUP."
3819   (let ((b (text-property-any (point-min) (point-max) 
3820                               'gnus-group (intern group))))
3821     (and b (goto-char b))))
3822
3823 (defun gnus-group-next-group (n)
3824   "Go to next N'th newsgroup.
3825 If N is negative, search backward instead.
3826 Returns the difference between N and the number of skips actually
3827 done."
3828   (interactive "p")
3829   (gnus-group-next-unread-group n t))
3830
3831 (defun gnus-group-next-unread-group (n &optional all level)
3832   "Go to next N'th unread newsgroup.
3833 If N is negative, search backward instead.
3834 If ALL is non-nil, choose any newsgroup, unread or not.
3835 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3836 such group can be found, the next group with a level higher than
3837 LEVEL.
3838 Returns the difference between N and the number of skips actually
3839 made."
3840   (interactive "p")
3841   (let ((backward (< n 0))
3842         (n (abs n)))
3843     (while (and (> n 0)
3844                 (gnus-group-search-forward 
3845                  backward (or (not gnus-group-goto-unread) all) level))
3846       (setq n (1- n)))
3847     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3848                                (if level " on this level or higher" "")))
3849     n))
3850
3851 (defun gnus-group-prev-group (n)
3852   "Go to previous N'th newsgroup.
3853 Returns the difference between N and the number of skips actually
3854 done."
3855   (interactive "p")
3856   (gnus-group-next-unread-group (- n) t))
3857
3858 (defun gnus-group-prev-unread-group (n)
3859   "Go to previous N'th unread newsgroup.
3860 Returns the difference between N and the number of skips actually
3861 done."  
3862   (interactive "p")
3863   (gnus-group-next-unread-group (- n)))
3864
3865 (defun gnus-group-next-unread-group-same-level (n)
3866   "Go to next N'th unread newsgroup on the same level.
3867 If N is negative, search backward instead.
3868 Returns the difference between N and the number of skips actually
3869 done."
3870   (interactive "p")
3871   (gnus-group-next-unread-group n t (gnus-group-group-level))
3872   (gnus-group-position-cursor))
3873
3874 (defun gnus-group-prev-unread-group-same-level (n)
3875   "Go to next N'th unread newsgroup on the same level.
3876 Returns the difference between N and the number of skips actually
3877 done."
3878   (interactive "p")
3879   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3880   (gnus-group-position-cursor))
3881
3882 (defun gnus-group-best-unread-group (&optional exclude-group)
3883   "Go to the group with the highest level.
3884 If EXCLUDE-GROUP, do not go to that group."
3885   (interactive)
3886   (goto-char (point-min))
3887   (let ((best 100000)
3888         unread best-point)
3889     (while (setq unread (get-text-property (point) 'gnus-unread))
3890       (if (and (numberp unread) (> unread 0))
3891           (progn
3892             (if (and (< (get-text-property (point) 'gnus-level) best)
3893                      (or (not exclude-group)
3894                          (not (equal exclude-group (gnus-group-group-name)))))
3895                 (progn 
3896                   (setq best (get-text-property (point) 'gnus-level))
3897                   (setq best-point (point))))))
3898       (forward-line 1))
3899     (if best-point (goto-char best-point))
3900     (gnus-summary-position-cursor)
3901     (and best-point (gnus-group-group-name))))
3902
3903 (defun gnus-group-first-unread-group ()
3904   "Go to the first group with unread articles."
3905   (interactive)
3906   (goto-char (point-min))
3907   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3908       (gnus-group-next-unread-group 1))
3909   (gnus-group-position-cursor))
3910
3911 (defun gnus-group-enter-server-mode ()
3912   "Jump to the server buffer."
3913   (interactive)
3914   (gnus-server-setup-buffer)
3915   (gnus-configure-windows 'server)
3916   (gnus-server-prepare))
3917
3918 (defun gnus-group-make-group (name method &optional address)
3919   "Add a new newsgroup.
3920 The user will be prompted for a NAME, for a select METHOD, and an
3921 ADDRESS."
3922   (interactive
3923    (cons 
3924     (read-string "Group name: ")
3925     (let ((method
3926            (completing-read 
3927             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3928             nil t)))
3929       (if (assoc method gnus-valid-select-methods)
3930           (list method
3931                 (if (memq 'prompt-address
3932                           (assoc method gnus-valid-select-methods))
3933                     (read-string "Address: ")
3934                   ""))
3935         (list method nil)))))
3936   
3937   (let* ((meth (if address (list (intern method) address) method))
3938          (nname (gnus-group-prefixed-name name meth))
3939          info)
3940     (and (gnus-gethash nname gnus-newsrc-hashtb)
3941          (error "Group %s already exists" nname))
3942     (gnus-group-change-level 
3943      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3944      gnus-level-default-subscribed gnus-level-killed 
3945      (and (gnus-group-group-name)
3946           (gnus-gethash (gnus-group-group-name)
3947                         gnus-newsrc-hashtb))
3948      t)
3949     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3950     (gnus-dribble-enter 
3951      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3952     (gnus-group-insert-group-line-info nname)
3953
3954     (if (assoc method gnus-valid-select-methods)
3955         (require (intern method)))
3956     (and (gnus-check-backend-function 'request-create-group nname)
3957          (gnus-request-create-group nname))))
3958
3959 (defun gnus-group-edit-group (group &optional part)
3960   "Edit the group on the current line."
3961   (interactive (list (gnus-group-group-name)))
3962   (let ((done-func '(lambda () 
3963                       "Exit editing mode and update the information."
3964                       (interactive)
3965                       (gnus-group-edit-group-done 'part 'group)))
3966         (part (or part 'info))
3967         (winconf (current-window-configuration))
3968         info)
3969     (or group (error "No group on current line"))
3970     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3971         (error "Killed group; can't be edited"))
3972     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3973     (gnus-configure-windows 'edit-group)
3974     (gnus-add-current-to-buffer-list)
3975     (emacs-lisp-mode)
3976     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3977     (use-local-map (copy-keymap emacs-lisp-mode-map))
3978     (local-set-key "\C-c\C-c" done-func)
3979     (make-local-variable 'gnus-prev-winconf)
3980     (setq gnus-prev-winconf winconf)
3981     ;; We modify the func to let it know what part it is editing.
3982     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3983     (setcar (cdr (cdr (nth 4 done-func))) group)
3984     (erase-buffer)
3985     (insert
3986      (cond 
3987       ((eq part 'method)
3988        ";; Type `C-c C-c' after editing the select method.\n\n")
3989       ((eq part 'params)
3990        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3991       ((eq part 'info)
3992        ";; Type `C-c C-c' after editing the group info.\n\n")))
3993     (let ((cinfo (gnus-copy-sequence info))
3994           marked)
3995       (if (not (setq marked (nth 3 cinfo)))
3996           ()
3997         (while marked
3998           (or (eq 'score (car (car marked)))
3999               (eq 'bookmark (car (car marked)))
4000               (eq 'killed (car (car marked)))
4001               (not (numberp (car (cdr (car marked)))))
4002               (setcdr (car marked) 
4003                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4004           (setq marked (cdr marked))))
4005       (insert 
4006        (pp-to-string
4007         (cond ((eq part 'method)
4008                (or (nth 4 info) "native"))
4009               ((eq part 'params)
4010                (nth 5 info))
4011               (t
4012                cinfo)))
4013        "\n"))))
4014
4015 (defun gnus-group-edit-group-method (group)
4016   "Edit the select method of GROUP."
4017   (interactive (list (gnus-group-group-name)))
4018   (gnus-group-edit-group group 'method))
4019
4020 (defun gnus-group-edit-group-parameters (group)
4021   "Edit the group parameters of GROUP."
4022   (interactive (list (gnus-group-group-name)))
4023   (gnus-group-edit-group group 'params))
4024
4025 (defun gnus-group-edit-group-done (part group)
4026   "Get info from buffer, update variables and jump to the group buffer."
4027   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4028   (goto-char (point-min))
4029   (let ((form (read (current-buffer)))
4030         (winconf gnus-prev-winconf))
4031     (if (eq part 'info) 
4032         (gnus-group-set-info form)
4033       (gnus-group-set-info form group part))
4034     (kill-buffer (current-buffer))
4035     (and winconf (set-window-configuration winconf))
4036     (set-buffer gnus-group-buffer)
4037     (gnus-group-update-group (gnus-group-group-name))
4038     (gnus-group-position-cursor)))
4039
4040 (defun gnus-group-make-help-group ()
4041   "Create the (ding) Gnus documentation group."
4042   (interactive)
4043   (let ((path load-path)
4044         name)
4045     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4046                                    "gnus-help" '(nndoc "gnus-help")))
4047                        gnus-newsrc-hashtb)
4048          (error "Documentation group already exists"))
4049     (while (and path
4050                 (not (file-exists-p (concat (file-name-as-directory (car path))
4051                                             "doc.txt"))))
4052       (setq path (cdr path)))
4053     (or path (error "Couldn't find doc group"))
4054     (gnus-group-make-group 
4055      (gnus-group-real-name name)
4056      (list 'nndoc name
4057            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4058            (list 'nndoc-article-type 'mbox))))
4059   (gnus-group-position-cursor))
4060
4061 (defun gnus-group-make-doc-group (file type)
4062   "Create a group that uses a single file as the source."
4063   (interactive 
4064    (list (read-file-name "File name: ") 
4065          (let ((err "")
4066                found char)
4067            (while (not found)
4068              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4069              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4070                                ((= char ?b) 'babyl)
4071                                ((= char ?d) 'digest)
4072                                (t (setq err "%c unknown. " char)
4073                                   nil))))
4074            found)))
4075   (let* ((file (expand-file-name file))
4076          (name (gnus-generate-new-group-name
4077                 (gnus-group-prefixed-name
4078                  (file-name-nondirectory file) '(nndoc "")))))
4079     (gnus-group-make-group 
4080      (gnus-group-real-name name)
4081      (list 'nndoc name
4082            (list 'nndoc-address file)
4083            (list 'nndoc-article-type type)))))
4084
4085 (defun gnus-group-make-archive-group ()
4086   "Create the (ding) Gnus archive group."
4087   (interactive)
4088   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4089                      gnus-newsrc-hashtb)
4090        (error "Archive group already exists"))
4091   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4092   (gnus-group-position-cursor))
4093
4094 (defun gnus-group-make-directory-group (dir)
4095   "Create an nndir group.
4096 The user will be prompted for a directory. The contents of this
4097 directory will be used as a newsgroup. The directory should contain
4098 mail messages or news articles in files that have numeric names."
4099   (interactive
4100    (list (read-file-name "Create group from directory: ")))
4101   (or (file-exists-p dir) (error "No such directory"))
4102   (or (file-directory-p dir) (error "Not a directory"))
4103   (gnus-group-make-group dir "nndir" dir)
4104   (gnus-group-position-cursor))
4105
4106 (defun gnus-group-make-kiboze-group (group address scores)
4107   "Create an nnkiboze group.
4108 The user will be prompted for a name, a regexp to match groups, and
4109 score file entries for articles to include in the group."
4110   (interactive
4111    (list
4112     (read-string "nnkiboze group name: ")
4113     (read-string "Source groups (regexp): ")
4114     (let ((headers (mapcar (lambda (group) (list group))
4115                            '("subject" "from" "number" "date" "message-id"
4116                              "references" "chars" "lines" "xref")))
4117           scores header regexp regexps)
4118       (while (not (equal "" (setq header (completing-read 
4119                                           "Match on header: " headers nil t))))
4120         (setq regexps nil)
4121         (while (not (equal "" (setq regexp (read-string 
4122                                             (format "Match on %s (string): "
4123                                                     header)))))
4124           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4125         (setq scores (cons (cons header regexps) scores)))
4126       scores)))
4127   (gnus-group-make-group group "nnkiboze" address)
4128   (save-excursion
4129     (gnus-set-work-buffer)
4130     (let (emacs-lisp-mode-hook)
4131       (pp scores (current-buffer)))
4132     (write-region (point-min) (point-max) 
4133                   (concat (or gnus-kill-files-directory "~/News")
4134                           "nnkiboze:" group "." gnus-score-file-suffix)))
4135   (gnus-group-position-cursor))
4136
4137 (defun gnus-group-add-to-virtual (n vgroup)
4138   "Add the current group to a virtual group."
4139   (interactive
4140    (list current-prefix-arg
4141          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4142                           "nnvirtual:")))
4143   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4144       (error "%s is not an nnvirtual group" vgroup))
4145   (let* ((groups (gnus-group-process-prefix n))
4146          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4147     (setcar (cdr method)
4148             (concat 
4149              (nth 1 method) "\\|"
4150              (mapconcat 
4151               (lambda (s) 
4152                 (gnus-group-remove-mark s)
4153                 (concat "\\(^" (regexp-quote s) "$\\)"))
4154               groups "\\|"))))
4155   (gnus-group-position-cursor))
4156
4157 (defun gnus-group-make-empty-virtual (group)
4158   "Create a new, fresh, empty virtual group."
4159   (interactive "sCreate new, empty virtual group: ")
4160   (let* ((method (list 'nnvirtual "^$"))
4161          (pgroup (gnus-group-prefixed-name group method)))
4162     ;; Check whether it exists already.
4163     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4164          (error "Group %s already exists." pgroup))
4165     ;; Subscribe the new group after the group on the current line.
4166     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4167     (gnus-group-update-group pgroup)
4168     (forward-line -1)
4169     (gnus-group-position-cursor)))
4170
4171 (defun gnus-group-enter-directory (dir)
4172   "Enter an ephemeral nneething group."
4173   (interactive "DDirectory to read: ")
4174   (let* ((method (list 'nneething dir))
4175          (leaf (gnus-group-prefixed-name
4176                 (file-name-nondirectory (directory-file-name dir))
4177                 method))
4178          (name (gnus-generate-new-group-name leaf)))
4179     (let ((nneething-read-only t))
4180       (or (gnus-group-read-ephemeral-group 
4181            name method t
4182            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4183                                       'summary 'group)))
4184           (error "Couldn't enter %s" dir)))))
4185
4186 ;; Group sorting commands
4187 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4188
4189 (defun gnus-group-sort-groups ()
4190   "Sort the group buffer using `gnus-group-sort-function'."
4191   (interactive)
4192   (setq gnus-newsrc-alist 
4193         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4194   (gnus-make-hashtable-from-newsrc-alist)
4195   (gnus-group-list-groups (if gnus-have-all-newsgroups gnus-level-unsubscribed)
4196                           gnus-have-all-newsgroups))
4197
4198 (defun gnus-group-sort-by-alphabet (info1 info2)
4199   (string< (car info1) (car info2)))
4200
4201 (defun gnus-group-sort-by-unread (info1 info2)
4202   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4203         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4204     (< (or (and (numberp n1) n1) 0)
4205        (or (and (numberp n2) n2) 0))))
4206
4207 (defun gnus-group-sort-by-level (info1 info2)
4208   (< (nth 1 info1) (nth 1 info2)))
4209
4210 ;; Group catching up.
4211
4212 (defun gnus-group-catchup-current (&optional n all)
4213   "Mark all articles not marked as unread in current newsgroup as read.
4214 If prefix argument N is numeric, the ARG next newsgroups will be
4215 caught up. If ALL is non-nil, marked articles will also be marked as
4216 read. Cross references (Xref: header) of articles are ignored.
4217 The difference between N and actual number of newsgroups that were
4218 caught up is returned."
4219   (interactive "P")
4220   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4221                gnus-expert-user
4222                (gnus-y-or-n-p
4223                 (if all
4224                     "Do you really want to mark all articles as read? "
4225                   "Mark all unread articles as read? "))))
4226       n
4227     (let ((groups (gnus-group-process-prefix n))
4228           (ret 0))
4229       (while groups
4230         ;; Virtual groups have to be given special treatment. 
4231         (let ((method (gnus-find-method-for-group (car groups))))
4232           (if (eq 'nnvirtual (car method))
4233               (nnvirtual-catchup-group
4234                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4235         (gnus-group-remove-mark (car groups))
4236         (if (prog1
4237                 (gnus-group-goto-group (car groups))
4238               (gnus-group-catchup (car groups) all))
4239             (gnus-group-update-group-line)
4240           (setq ret (1+ ret)))
4241         (setq groups (cdr groups)))
4242       (gnus-group-next-unread-group 1)
4243       ret)))
4244
4245 (defun gnus-group-catchup-current-all (&optional n)
4246   "Mark all articles in current newsgroup as read.
4247 Cross references (Xref: header) of articles are ignored."
4248   (interactive "P")
4249   (gnus-group-catchup-current n 'all))
4250
4251 (defun gnus-group-catchup (group &optional all)
4252   "Mark all articles in GROUP as read.
4253 If ALL is non-nil, all articles are marked as read.
4254 The return value is the number of articles that were marked as read,
4255 or nil if no action could be taken."
4256   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4257          (num (car entry))
4258          (marked (nth 3 (nth 2 entry))))
4259     (if (not (numberp (car entry)))
4260         (gnus-message 1 "Can't catch up; non-active group")
4261       ;; Do the updating only if the newsgroup isn't killed.
4262       (if (not entry)
4263           ()
4264         (gnus-update-read-articles 
4265          group (and (not all) (append (cdr (assq 'tick marked))
4266                                       (cdr (assq 'dormant marked))))
4267          nil (and (not all) (cdr (assq 'tick marked))))
4268         (and all marked
4269              (setcar (nthcdr 3 (nth 2 entry)) 
4270                      (delq (assq 'dormant marked) 
4271                            (nth 3 (nth 2 entry)))))))
4272     num))
4273
4274 (defun gnus-group-expire-articles (&optional n)
4275   "Expire all expirable articles in the current newsgroup."
4276   (interactive "P")
4277   (let ((groups (gnus-group-process-prefix n))
4278         group)
4279     (or groups (error "No groups to expire"))
4280     (while groups
4281       (setq group (car groups)
4282             groups (cdr groups))
4283       (gnus-group-remove-mark group)
4284       (if (not (gnus-check-backend-function 'request-expire-articles group))
4285           ()
4286         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4287                (expirable (if (memq 'total-expire (nth 5 info))
4288                               (cons nil (gnus-list-of-read-articles group))
4289                             (assq 'expire (nth 3 info)))))
4290           (and expirable 
4291                (setcdr expirable
4292                        (gnus-request-expire-articles 
4293                         (cdr expirable) group))))))))
4294
4295 (defun gnus-group-expire-all-groups ()
4296   "Expire all expirable articles in all newsgroups."
4297   (interactive)
4298   (save-excursion
4299     (gnus-message 5 "Expiring...")
4300     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4301                                      (cdr gnus-newsrc-alist))))
4302       (gnus-group-expire-articles nil)))
4303   (gnus-group-position-cursor)
4304   (gnus-message 5 "Expiring...done"))
4305
4306 (defun gnus-group-set-current-level (n level)
4307   "Set the level of the next N groups to LEVEL."
4308   (interactive "P\nnLevel: ")
4309   (or (and (>= level 1) (<= level gnus-level-killed))
4310       (error "Illegal level: %d" level))
4311   (let ((groups (gnus-group-process-prefix n))
4312         group)
4313     (while groups
4314       (setq group (car groups)
4315             groups (cdr groups))
4316       (gnus-group-remove-mark group)
4317       (gnus-message 6 "Changed level of %s from %d to %d" 
4318                     group (gnus-group-group-level) level)
4319       (gnus-group-change-level group level
4320                                (gnus-group-group-level))
4321       (gnus-group-update-group-line)))
4322   (gnus-group-position-cursor))
4323
4324 (defun gnus-group-unsubscribe-current-group (&optional n)
4325   "Toggle subscription of the current group.
4326 If given numerical prefix, toggle the N next groups."
4327   (interactive "P")
4328   (let ((groups (gnus-group-process-prefix n))
4329         group)
4330     (while groups
4331       (setq group (car groups)
4332             groups (cdr groups))
4333       (gnus-group-remove-mark group)
4334       (gnus-group-unsubscribe-group
4335        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4336                  gnus-level-default-unsubscribed
4337                gnus-level-default-subscribed))
4338       (gnus-group-update-group-line))
4339     (gnus-group-next-group 1)))
4340
4341 (defun gnus-group-unsubscribe-group (group &optional level)
4342   "Toggle subscribe from/to unsubscribe GROUP.
4343 New newsgroup is added to .newsrc automatically."
4344   (interactive
4345    (list (completing-read
4346           "Group: " gnus-active-hashtb nil 
4347           (memq gnus-select-method gnus-have-read-active-file))))
4348   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4349     (cond (newsrc
4350            ;; Toggle subscription flag.
4351            (gnus-group-change-level 
4352             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4353                                            gnus-level-subscribed) 
4354                                        (1+ gnus-level-subscribed)
4355                                      gnus-level-default-subscribed)))
4356            (gnus-group-update-group group))
4357           ((and (stringp group)
4358                 (or (not gnus-have-read-active-file)
4359                     (gnus-gethash group gnus-active-hashtb)))
4360            ;; Add new newsgroup.
4361            (gnus-group-change-level 
4362             group 
4363             (if level level gnus-level-default-subscribed) 
4364             (or (and (member group gnus-zombie-list) 
4365                      gnus-level-zombie) 
4366                 gnus-level-killed)
4367             (and (gnus-group-group-name)
4368                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4369            (gnus-group-update-group group))
4370           (t (error "No such newsgroup: %s" group)))
4371     (gnus-group-position-cursor)))
4372
4373 (defun gnus-group-transpose-groups (n)
4374   "Move the current newsgroup up N places.
4375 If given a negative prefix, move down instead. The difference between
4376 N and the number of steps taken is returned." 
4377   (interactive "p")
4378   (or (gnus-group-group-name)
4379       (error "No group on current line"))
4380   (gnus-group-kill-group 1)
4381   (prog1
4382       (forward-line (- n))
4383     (gnus-group-yank-group)
4384     (gnus-group-position-cursor)))
4385
4386 (defun gnus-group-kill-all-zombies ()
4387   "Kill all zombie newsgroups."
4388   (interactive)
4389   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4390   (setq gnus-zombie-list nil)
4391   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4392   (goto-char (point-min))
4393   (gnus-group-position-cursor))
4394
4395 (defun gnus-group-kill-region (begin end)
4396   "Kill newsgroups in current region (excluding current point).
4397 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4398   (interactive "r")
4399   (let ((lines
4400          ;; Count lines.
4401          (save-excursion
4402            (count-lines
4403             (progn
4404               (goto-char begin)
4405               (beginning-of-line)
4406               (point))
4407             (progn
4408               (goto-char end)
4409               (beginning-of-line)
4410               (point))))))
4411     (goto-char begin)
4412     (beginning-of-line)                 ;Important when LINES < 1
4413     (gnus-group-kill-group lines)))
4414
4415 (defun gnus-group-kill-group (&optional n)
4416   "The the next N groups.
4417 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4418 However, only groups that were alive can be yanked; already killed 
4419 groups or zombie groups can't be yanked.
4420 The return value is the name of the (last) group that was killed."
4421   (interactive "P")
4422   (let ((buffer-read-only nil)
4423         (groups (gnus-group-process-prefix n))
4424         group entry level)
4425     (while groups
4426       (setq group (car groups)
4427             groups (cdr groups))
4428       (gnus-group-remove-mark group)
4429       (setq level (gnus-group-group-level))
4430       (gnus-delete-line)
4431       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4432           (setq gnus-list-of-killed-groups 
4433                 (cons (cons (car entry) (nth 2 entry)) 
4434                       gnus-list-of-killed-groups)))
4435       (gnus-group-change-level 
4436        (if entry entry group) gnus-level-killed (if entry nil level)))
4437     (gnus-group-position-cursor)
4438     group))
4439
4440 (defun gnus-group-yank-group (&optional arg)
4441   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4442 inserting it before the current newsgroup.  The numeric ARG specifies
4443 how many newsgroups are to be yanked.  The name of the (last)
4444 newsgroup yanked is returned."
4445   (interactive "p")
4446   (if (not arg) (setq arg 1))
4447   (let (info group prev)
4448     (while (>= (setq arg (1- arg)) 0)
4449       (if (not (setq info (car gnus-list-of-killed-groups)))
4450           (error "No more newsgroups to yank"))
4451       (setq group (nth 2 info))
4452       ;; Find which newsgroup to insert this one before - search
4453       ;; backward until something suitable is found. If there are no
4454       ;; other newsgroups in this buffer, just make this newsgroup the
4455       ;; first newsgroup.
4456       (setq prev (gnus-group-group-name))
4457       (gnus-group-change-level 
4458        info (nth 2 info) gnus-level-killed 
4459        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4460        t)
4461       (gnus-group-insert-group-line-info (nth 1 info))
4462       (setq gnus-list-of-killed-groups 
4463             (cdr gnus-list-of-killed-groups)))
4464     (forward-line -1)
4465     (gnus-group-position-cursor)
4466     group))
4467       
4468 (defun gnus-group-list-all-groups (&optional arg)
4469   "List all newsgroups with level ARG or lower.
4470 Default is gnus-level-unsubscribed, which lists all subscribed and most
4471 unsubscribed groups."
4472   (interactive "P")
4473   (setq arg (or arg gnus-level-unsubscribed))
4474   (gnus-group-list-groups arg t))
4475
4476 (defun gnus-group-list-killed ()
4477   "List all killed newsgroups in the group buffer."
4478   (interactive)
4479   (if (not gnus-killed-list)
4480       (gnus-message 6 "No killed groups")
4481     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4482     (goto-char (point-min)))
4483   (gnus-group-position-cursor))
4484
4485 (defun gnus-group-list-zombies ()
4486   "List all zombie newsgroups in the group buffer."
4487   (interactive)
4488   (if (not gnus-zombie-list)
4489       (gnus-message 6 "No zombie groups")
4490     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4491     (goto-char (point-min)))
4492   (gnus-group-position-cursor))
4493
4494 (defun gnus-group-get-new-news (&optional arg)
4495   "Get newly arrived articles.
4496 If ARG is non-nil, it should be a number between one and nine to
4497 specify which levels you are interested in re-scanning."
4498   (interactive "P")
4499   (run-hooks 'gnus-get-new-news-hook)
4500   (if gnus-group-use-permanent-levels
4501       (setq arg
4502             (setq gnus-group-default-list-level 
4503                   (or arg gnus-group-default-list-level
4504                       gnus-level-subscribed))))
4505   (if (and gnus-read-active-file (not arg))
4506       (progn
4507         (gnus-read-active-file)
4508         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4509     (let ((gnus-read-active-file (not arg))
4510           (gnus-have-read-active-file 
4511            (and (not arg) gnus-have-read-active-file)))
4512       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4513   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4514                               gnus-group-default-list-level
4515                               gnus-level-subscribed)
4516                           gnus-have-all-newsgroups))
4517
4518 (defun gnus-group-get-new-news-this-group (&optional n)
4519   "Check for newly arrived news in the current group (and the N-1 next groups).
4520 The difference between N and the number of newsgroup checked is returned.
4521 If N is negative, this group and the N-1 previous groups will be checked."
4522   (interactive "P")
4523   (let* ((groups (gnus-group-process-prefix n))
4524          (ret (if (numberp n) (- n (length groups)) 0))
4525          group)
4526     (while groups
4527       (setq group (car groups)
4528             groups (cdr groups))
4529       (gnus-group-remove-mark group)
4530       (or (gnus-get-new-news-in-group group)
4531           (progn 
4532             (ding) 
4533             (message "%s error: %s" group (gnus-status-message group))
4534             (sit-for 2))))
4535     ;; !!! I don't know why the buffer scrolls forward when updating
4536     ;; the first line in the group buffer, but it does. So we set the
4537     ;; window start forcibly.
4538 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4539     (gnus-group-next-unread-group 1 t)
4540     (gnus-summary-position-cursor)
4541     ret))
4542
4543 (defun gnus-get-new-news-in-group (group)
4544   (and group 
4545        (gnus-activate-newsgroup group)
4546        (progn
4547          (gnus-get-unread-articles-in-group 
4548           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4549           (gnus-gethash group gnus-active-hashtb))
4550          (gnus-group-update-group-line)
4551          t)))
4552
4553 (defun gnus-group-fetch-faq (group)
4554   "Fetch the FAQ for the current group."
4555   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4556   (or group (error "No group name given"))
4557   (let ((file (concat gnus-group-faq-directory group))) 
4558     (if (not (file-exists-p file))
4559         (error "No such file: %s" file)
4560       (find-file file))))
4561   
4562 (defun gnus-group-describe-group (force &optional group)
4563   "Display a description of the current newsgroup."
4564   (interactive (list current-prefix-arg (gnus-group-group-name)))
4565   (and force (setq gnus-description-hashtb nil))
4566   (let ((method (gnus-find-method-for-group group))
4567         desc)
4568     (or group (error "No group name given"))
4569     (and (or (and gnus-description-hashtb
4570                   ;; We check whether this group's method has been
4571                   ;; queried for a description file.  
4572                   (gnus-gethash 
4573                    (gnus-group-prefixed-name "" method) 
4574                    gnus-description-hashtb))
4575              (setq desc (gnus-group-get-description group))
4576              (gnus-read-descriptions-file method))
4577          (message
4578           (or desc (gnus-gethash group gnus-description-hashtb)
4579               "No description available")))))
4580
4581 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4582 (defun gnus-group-describe-all-groups (&optional force)
4583   "Pop up a buffer with descriptions of all newsgroups."
4584   (interactive "P")
4585   (and force (setq gnus-description-hashtb nil))
4586   (if (not (or gnus-description-hashtb
4587                (gnus-read-all-descriptions-files)))
4588       (error "Couldn't request descriptions file"))
4589   (let ((buffer-read-only nil)
4590         b)
4591     (erase-buffer)
4592     (mapatoms
4593      (lambda (group)
4594        (setq b (point))
4595        (insert (format "      *: %-20s %s\n" (symbol-name group)
4596                        (symbol-value group)))
4597        (add-text-properties 
4598         b (1+ b) (list 'gnus-group group
4599                        'gnus-unread t 'gnus-marked nil
4600                        'gnus-level (1+ gnus-level-subscribed))))
4601      gnus-description-hashtb)
4602     (goto-char (point-min))
4603     (gnus-group-position-cursor)))
4604
4605 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4606 (defun gnus-group-apropos (regexp &optional search-description)
4607   "List all newsgroups that have names that match a regexp."
4608   (interactive "sGnus apropos (regexp): ")
4609   (let ((prev "")
4610         (obuf (current-buffer))
4611         groups des)
4612     ;; Go through all newsgroups that are known to Gnus.
4613     (mapatoms 
4614      (lambda (group)
4615        (and (string-match regexp (symbol-name group))
4616             (setq groups (cons (symbol-name group) groups))))
4617      gnus-active-hashtb)
4618     ;; Go through all descriptions that are known to Gnus. 
4619     (if search-description
4620         (mapatoms 
4621          (lambda (group)
4622            (and (string-match regexp (symbol-value group))
4623                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4624                 (setq groups (cons (symbol-name group) groups))))
4625          gnus-description-hashtb))
4626     (if (not groups)
4627         (gnus-message 3 "No groups matched \"%s\"." regexp)
4628       ;; Print out all the groups.
4629       (save-excursion
4630         (pop-to-buffer "*Gnus Help*")
4631         (buffer-disable-undo (current-buffer))
4632         (erase-buffer)
4633         (setq groups (sort groups 'string<))
4634         (while groups
4635           ;; Groups may be entered twice into the list of groups.
4636           (if (not (string= (car groups) prev))
4637               (progn
4638                 (insert (setq prev (car groups)) "\n")
4639                 (if (and gnus-description-hashtb
4640                          (setq des (gnus-gethash (car groups) 
4641                                                  gnus-description-hashtb)))
4642                     (insert "  " des "\n"))))
4643           (setq groups (cdr groups)))
4644         (goto-char (point-min))))
4645     (pop-to-buffer obuf)))
4646
4647 (defun gnus-group-description-apropos (regexp)
4648   "List all newsgroups that have names or descriptions that match a regexp."
4649   (interactive "sGnus description apropos (regexp): ")
4650   (if (not (or gnus-description-hashtb
4651                (gnus-read-all-descriptions-files)))
4652       (error "Couldn't request descriptions file"))
4653   (gnus-group-apropos regexp t))
4654
4655 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4656 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4657   "List all groups with unread articles that match REGEXP.
4658 If the prefix LEVEL is non-nil, it should be a number that says which
4659 level to cut off listing groups. 
4660 If ALL, also list groups with no unread articles.
4661 If LOWEST, don't list groups with level lower than LOWEST."
4662   (interactive "P\nsList newsgroups matching: ")
4663   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4664                            all (or lowest 1) regexp)
4665   (goto-char (point-min))
4666   (gnus-group-position-cursor))
4667
4668 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4669   "List all groups that match REGEXP.
4670 If the prefix LEVEL is non-nil, it should be a number that says which
4671 level to cut off listing groups. 
4672 If LOWEST, don't list groups with level lower than LOWEST."
4673   (interactive "P\nsList newsgroups matching: ")
4674   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4675
4676 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4677 (defun gnus-group-save-newsrc ()
4678   "Save the Gnus startup files."
4679   (interactive)
4680   (gnus-save-newsrc-file))
4681
4682 (defun gnus-group-restart (&optional arg)
4683   "Force Gnus to read the .newsrc file."
4684   (interactive "P")
4685   (gnus-save-newsrc-file)
4686   (gnus-setup-news 'force)
4687   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4688
4689 (defun gnus-group-read-init-file ()
4690   "Read the Gnus elisp init file."
4691   (interactive)
4692   (gnus-read-init-file))
4693
4694 (defun gnus-group-check-bogus-groups (&optional silent)
4695   "Check bogus newsgroups.
4696 If given a prefix, don't ask for confirmation before removing a bogus
4697 group."
4698   (interactive "P")
4699   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4700   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4701
4702 (defun gnus-group-edit-global-kill (&optional article group)
4703   "Edit the global kill file.
4704 If GROUP, edit that local kill file instead."
4705   (interactive "P")
4706   (setq gnus-current-kill-article article)
4707   (gnus-kill-file-edit-file group)
4708   (gnus-message 6
4709    (substitute-command-keys
4710     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4711
4712 (defun gnus-group-edit-local-kill (article group)
4713   "Edit a local kill file."
4714   (interactive (list nil (gnus-group-group-name)))
4715   (gnus-group-edit-global-kill article group))
4716
4717 (defun gnus-group-force-update ()
4718   "Update `.newsrc' file."
4719   (interactive)
4720   (gnus-save-newsrc-file))
4721
4722 (defun gnus-group-suspend ()
4723   "Suspend the current Gnus session.
4724 In fact, cleanup buffers except for group mode buffer.
4725 The hook gnus-suspend-gnus-hook is called before actually suspending."
4726   (interactive)
4727   (run-hooks 'gnus-suspend-gnus-hook)
4728   ;; Kill Gnus buffers except for group mode buffer.
4729   (let ((group-buf (get-buffer gnus-group-buffer)))
4730     ;; Do this on a separate list in case the user does a ^G before we finish
4731     (let ((gnus-buffer-list
4732            (delq group-buf (delq gnus-dribble-buffer
4733                                  (append gnus-buffer-list nil)))))
4734       (while gnus-buffer-list
4735         (gnus-kill-buffer (car gnus-buffer-list))
4736         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4737     (if group-buf
4738         (progn
4739           (setq gnus-buffer-list (list group-buf))
4740           (bury-buffer group-buf)
4741           (delete-windows-on group-buf t)))))
4742
4743 (defun gnus-group-clear-dribble ()
4744   "Clear all information from the dribble buffer."
4745   (interactive)
4746   (gnus-dribble-clear))
4747
4748 (defun gnus-group-exit ()
4749   "Quit reading news after updating .newsrc.eld and .newsrc.
4750 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4751   (interactive)
4752   (if (or noninteractive                ;For gnus-batch-kill
4753           (zerop (buffer-size))         ;No news is good news.
4754           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4755           (not gnus-interactive-exit)   ;Without confirmation
4756           gnus-expert-user
4757           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4758       (progn
4759         (if gnus-use-full-window
4760             (delete-other-windows)
4761           (gnus-remove-some-windows))
4762         (run-hooks 'gnus-exit-gnus-hook)
4763         (gnus-offer-save-summaries)
4764         (gnus-save-newsrc-file)
4765         (gnus-close-backends)
4766         (gnus-clear-system))))
4767
4768 (defun gnus-close-backends ()
4769   ;; Send a close request to all backends that support such a request. 
4770   (let ((methods gnus-valid-select-methods)
4771         func)
4772     (while methods
4773       (if (fboundp (setq func (intern (concat (car (car methods))
4774                                               "-request-close"))))
4775           (funcall func))
4776       (setq methods (cdr methods)))))
4777
4778 (defun gnus-group-quit ()
4779   "Quit reading news without updating .newsrc.eld or .newsrc.
4780 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4781   (interactive)
4782   (if (or noninteractive                ;For gnus-batch-kill
4783           (zerop (buffer-size))
4784           (not (gnus-server-opened gnus-select-method))
4785           gnus-expert-user
4786           (not gnus-current-startup-file)
4787           (gnus-yes-or-no-p
4788            (format "Quit reading news without saving %s? "
4789                    (file-name-nondirectory gnus-current-startup-file))))
4790       (progn
4791         (run-hooks 'gnus-exit-gnus-hook)
4792         (if gnus-use-full-window
4793             (delete-other-windows)
4794           (gnus-remove-some-windows))
4795         (gnus-dribble-save)
4796         (gnus-close-backends)
4797         (gnus-clear-system))))
4798
4799 (defun gnus-offer-save-summaries ()
4800   (let ((buffers (buffer-list)))
4801     (save-excursion
4802       (while buffers
4803         (and 
4804          ;; We look for buffers with "Summary" in the name.
4805          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4806          (progn
4807            (set-buffer (car buffers))
4808            ;; We check that this is, indeed, a summary buffer.
4809            (eq major-mode 'gnus-summary-mode)) 
4810          ;; We ask the user whether she wants to save the info.
4811          (gnus-y-or-n-p
4812                (format "Update summary buffer %s? " (buffer-name)))
4813          ;; We do it by simply exiting.
4814          (gnus-summary-exit))
4815         (setq buffers (cdr buffers))))))
4816
4817 (defun gnus-group-describe-briefly ()
4818   "Give a one line description of the group mode commands."
4819   (interactive)
4820   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
4821
4822 (defun gnus-group-browse-foreign-server (method)
4823   "Browse a foreign news server.
4824 If called interactively, this function will ask for a select method
4825  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4826 If not, METHOD should be a list where the first element is the method
4827 and the second element is the address."
4828   (interactive
4829    (list (let ((how (completing-read 
4830                      "Which backend: "
4831                      (append gnus-valid-select-methods gnus-server-alist)
4832                      nil t "nntp")))
4833            ;; We either got a backend name or a virtual server name.
4834            ;; If the first, we also need an address.
4835            (if (assoc how gnus-valid-select-methods)
4836                (list (intern how)
4837                      ;; Suggested by mapjph@bath.ac.uk.
4838                      (completing-read 
4839                       "Address: " 
4840                       (mapcar (lambda (server) (list server))
4841                               gnus-secondary-servers)))
4842              ;; We got a server name, so we find the method.
4843              (gnus-server-to-method how)))))
4844   (gnus-browse-foreign-server method))
4845
4846 \f
4847 ;;;
4848 ;;; Browse Server Mode
4849 ;;;
4850
4851 (defvar gnus-browse-mode-hook nil)
4852 (defvar gnus-browse-mode-map nil)
4853 (put 'gnus-browse-mode 'mode-class 'special)
4854
4855 (if gnus-browse-mode-map
4856     nil
4857   (setq gnus-browse-mode-map (make-keymap))
4858   (suppress-keymap gnus-browse-mode-map)
4859   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4860   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4861   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4862   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4863   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4864   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4865   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4866   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4867   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4868   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4869   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4870   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4871   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4872   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4873   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4874   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4875   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4876   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4877   )
4878
4879 (defvar gnus-browse-current-method nil)
4880 (defvar gnus-browse-return-buffer nil)
4881
4882 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4883
4884 (defun gnus-browse-foreign-server (method &optional return-buffer)
4885   (setq gnus-browse-current-method method)
4886   (setq gnus-browse-return-buffer return-buffer)
4887   (let ((gnus-select-method method)
4888         groups group)
4889     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4890     (or (gnus-server-opened method)
4891         (gnus-open-server method)
4892         (error "Unable to contact server: %s" (gnus-status-message method)))
4893     (or (gnus-request-list method)
4894         (error "Couldn't request list: %s" (gnus-status-message method)))
4895     (get-buffer-create gnus-browse-buffer)
4896     (gnus-add-current-to-buffer-list)
4897     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4898     (gnus-configure-windows 'browse)
4899     (buffer-disable-undo (current-buffer))
4900     (let ((buffer-read-only nil))
4901       (erase-buffer))
4902     (gnus-browse-mode)
4903     (setq mode-line-buffer-identification
4904           (format
4905            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4906     (save-excursion
4907       (set-buffer nntp-server-buffer)
4908       (let ((cur (current-buffer)))
4909         (goto-char (point-min))
4910         (or (string= gnus-ignored-newsgroups "")
4911             (delete-matching-lines gnus-ignored-newsgroups))
4912         (while (re-search-forward 
4913                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4914           (goto-char (match-end 1))
4915           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4916                                                      (match-end 1))
4917                                    (max 0 (- (1+ (read cur)) (read cur))))
4918                              groups)))))
4919     (setq groups (sort groups 
4920                        (lambda (l1 l2)
4921                          (string< (car l1) (car l2)))))
4922     (let ((buffer-read-only nil))
4923       (while groups
4924         (setq group (car groups))
4925         (insert 
4926          (format "K%7d: %s\n" (cdr group) (car group)))
4927         (setq groups (cdr groups))))
4928     (switch-to-buffer (current-buffer))
4929     (goto-char (point-min))
4930     (gnus-group-position-cursor)))
4931
4932 (defun gnus-browse-mode ()
4933   "Major mode for browsing a foreign server.
4934
4935 All normal editing commands are switched off.
4936
4937 \\<gnus-browse-mode-map>
4938 The only things you can do in this buffer is
4939
4940 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4941 The group will be inserted into the group buffer upon exit from this
4942 buffer.  
4943
4944 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4945
4946 3) `\\[gnus-browse-exit]' to return to the group buffer."
4947   (interactive)
4948   (kill-all-local-variables)
4949   (if gnus-visual (gnus-browse-make-menu-bar))
4950   (setq mode-line-modified "-- ")
4951   (make-local-variable 'mode-line-format)
4952   (setq mode-line-format (copy-sequence mode-line-format))
4953   (and (equal (nth 3 mode-line-format) "   ")
4954        (setcar (nthcdr 3 mode-line-format) ""))
4955   (setq major-mode 'gnus-browse-mode)
4956   (setq mode-name "Browse Server")
4957   (setq mode-line-process nil)
4958   (use-local-map gnus-browse-mode-map)
4959   (buffer-disable-undo (current-buffer))
4960   (setq truncate-lines t)
4961   (setq buffer-read-only t)
4962   (run-hooks 'gnus-browse-mode-hook))
4963
4964 (defun gnus-browse-read-group (&optional no-article)
4965   "Enter the group at the current line."
4966   (interactive)
4967   (let ((group (gnus-browse-group-name)))
4968     (or (gnus-group-read-ephemeral-group 
4969          group gnus-browse-current-method nil
4970          (cons (current-buffer) 'browse))
4971         (error "Couldn't enter %s" group))))
4972
4973 (defun gnus-browse-select-group ()
4974   "Select the current group."
4975   (interactive)
4976   (gnus-browse-read-group 'no))
4977
4978 (defun gnus-browse-next-group (n)
4979   "Go to the next group."
4980   (interactive "p")
4981   (prog1
4982       (forward-line n)
4983     (gnus-group-position-cursor)))
4984
4985 (defun gnus-browse-prev-group (n)
4986   "Go to the next group."
4987   (interactive "p")
4988   (gnus-browse-next-group (- n)))
4989
4990 (defun gnus-browse-unsubscribe-current-group (arg)
4991   "(Un)subscribe to the next ARG groups."
4992   (interactive "p")
4993   (and (eobp)
4994        (error "No group at current line."))
4995   (let ((ward (if (< arg 0) -1 1))
4996         (arg (abs arg)))
4997     (while (and (> arg 0)
4998                 (not (eobp))
4999                 (gnus-browse-unsubscribe-group)
5000                 (zerop (gnus-browse-next-group ward)))
5001       (setq arg (1- arg)))
5002     (gnus-group-position-cursor)
5003     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5004     arg))
5005
5006 (defun gnus-browse-group-name ()
5007   (save-excursion
5008     (beginning-of-line)
5009     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5010         ()
5011       (gnus-group-prefixed-name 
5012        (buffer-substring (match-beginning 1) (match-end 1))
5013        gnus-browse-current-method))))
5014   
5015 (defun gnus-browse-unsubscribe-group ()
5016   (let ((sub nil)
5017         (buffer-read-only nil)
5018         group)
5019     (save-excursion
5020       (beginning-of-line)
5021       (if (= (following-char) ?K) (setq sub t))
5022       (setq group (gnus-browse-group-name))
5023       (beginning-of-line)
5024       (delete-char 1)
5025       (if sub
5026           (progn
5027             (gnus-group-change-level 
5028              (list t group gnus-level-default-subscribed
5029                    nil nil gnus-browse-current-method) 
5030              gnus-level-default-subscribed gnus-level-killed
5031              (and (car (nth 1 gnus-newsrc-alist))
5032                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5033                                 gnus-newsrc-hashtb))
5034              t)
5035             (insert ? ))
5036         (gnus-group-change-level 
5037          group gnus-level-killed gnus-level-default-subscribed)
5038         (insert ?K)))
5039     t))
5040
5041 (defun gnus-browse-exit ()
5042   "Quit browsing and return to the group buffer."
5043   (interactive)
5044   (if (eq major-mode 'gnus-browse-mode)
5045       (kill-buffer (current-buffer)))
5046   (if gnus-browse-return-buffer
5047       (gnus-configure-windows 'server 'force)
5048     (gnus-configure-windows 'group 'force)
5049     (gnus-group-list-groups nil)))
5050
5051 (defun gnus-browse-describe-briefly ()
5052   "Give a one line description of the group mode commands."
5053   (interactive)
5054   (gnus-message 6
5055    (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")))
5056       
5057 \f
5058 ;;;
5059 ;;; Gnus summary mode
5060 ;;;
5061
5062 (defvar gnus-summary-mode-map nil)
5063 (defvar gnus-summary-mark-map nil)
5064 (defvar gnus-summary-mscore-map nil)
5065 (defvar gnus-summary-article-map nil)
5066 (defvar gnus-summary-thread-map nil)
5067 (defvar gnus-summary-goto-map nil)
5068 (defvar gnus-summary-exit-map nil)
5069 (defvar gnus-summary-various-map nil)
5070 (defvar gnus-summary-interest-map nil)
5071 (defvar gnus-summary-sort-map nil)
5072 (defvar gnus-summary-backend-map nil)
5073 (defvar gnus-summary-save-map nil)
5074 (defvar gnus-summary-wash-map nil)
5075 (defvar gnus-summary-help-map nil)
5076
5077 (put 'gnus-summary-mode 'mode-class 'special)
5078
5079 (if gnus-summary-mode-map
5080     nil
5081   (setq gnus-summary-mode-map (make-keymap))
5082   (suppress-keymap gnus-summary-mode-map)
5083
5084   ;; Non-orthogonal keys
5085
5086   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5087   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5088   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5089   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5090   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5091   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5092   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5093   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5094   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5095   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5096   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5097   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5098   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5099   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5100   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5101   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5102   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5103   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5104   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5105   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5106   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5107   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5108   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5109   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5110   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5111   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5112   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5113   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5114   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5115   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5116   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5117   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5118   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5119   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5120   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5121   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5122   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5123   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5124   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5125   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5126   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5127   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5128   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5129   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5130   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5131   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5132   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5133   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5134   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5135   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5136   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5137   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5138   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5139   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5140   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5141   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5142   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5143   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5144   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5145   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5146   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5147   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5148   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5149   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5150   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5151   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5152   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5153   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5154   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5155   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5156   (define-key gnus-summary-mode-map "V" 'gnus-version)
5157   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5158   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5159   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5160   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5161   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5162   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5163   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5164   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5165 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5166   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5167   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5168   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5169 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5170   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5171   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5172   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5173   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5174   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5175
5176
5177   ;; Sort of orthogonal keymap
5178   (define-prefix-command 'gnus-summary-mark-map)
5179   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5180   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5181   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5182   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5183   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5184   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5185   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5186   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5187   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5188   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5189   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5190   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5191   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5192   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5193   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5194   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5195   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5196   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5197   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5198   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5199   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5200   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5201   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5202   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5203
5204   (define-prefix-command 'gnus-summary-mscore-map)
5205   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5206   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5207   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5208   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5209   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5210
5211   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5212   
5213   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5214   
5215   (define-prefix-command 'gnus-summary-goto-map)
5216   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5217   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5218   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5219   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5220   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5221   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5222   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5223   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5224   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5225   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5226   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5227   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5228   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5229   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5230
5231
5232   (define-prefix-command 'gnus-summary-thread-map)
5233   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5234   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5235   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5236   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5237   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5238   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5239   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5240   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5241   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5242   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5243   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5244   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5245   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5246   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5247
5248   
5249   (define-prefix-command 'gnus-summary-exit-map)
5250   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5251   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5252   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5253   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5254   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5255   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5256   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5257   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5258   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5259   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5260   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5261
5262
5263   (define-prefix-command 'gnus-summary-article-map)
5264   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5265   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5266   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5267   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5268   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5269   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5270   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5271   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5272   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5273   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5274   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5275   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5276   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5277   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5278   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5279   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5280   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5281   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5282
5283
5284   (define-prefix-command 'gnus-summary-wash-map)
5285   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5286   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5287   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5288   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5289   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5290   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5291   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5292   (define-key gnus-summary-wash-map "m" 'gnus-article-remove-cr)
5293   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5294   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5295   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5296   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5297   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5298
5299   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5300   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5301   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5302   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5303   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5304   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5305
5306
5307   (define-prefix-command 'gnus-summary-help-map)
5308   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5309   (define-key gnus-summary-help-map "v" 'gnus-version)
5310   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5311   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5312   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5313   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5314
5315
5316   (define-prefix-command 'gnus-summary-backend-map)
5317   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5318   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5319   (define-key gnus-summary-backend-map "\M-\C-e" 
5320     'gnus-summary-expire-articles-now)
5321   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5322   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5323   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5324   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5325   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5326   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5327   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5328
5329
5330   (define-prefix-command 'gnus-summary-save-map)
5331   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5332   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5333   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5334   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5335   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5336   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5337   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5338   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5339 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5340
5341   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5342   
5343   (define-prefix-command 'gnus-summary-various-map)
5344   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5345   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5346   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5347   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5348   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5349   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5350   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5351   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5352   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5353   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5354   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5355
5356   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5357
5358   (define-prefix-command 'gnus-summary-sort-map)
5359   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5360   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5361   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5362   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5363   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5364   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5365
5366   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5367   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5368   )
5369
5370
5371 \f
5372
5373 (defun gnus-summary-mode (&optional group)
5374   "Major mode for reading articles.
5375
5376 All normal editing commands are switched off.
5377 \\<gnus-summary-mode-map>
5378 Each line in this buffer represents one article.  To read an
5379 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5380 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5381 respectively.
5382
5383 You can also post articles and send mail from this buffer.  To 
5384 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5385 of an article, type `\\[gnus-summary-reply]'.
5386
5387 There are approx. one gazillion commands you can execute in this 
5388 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5389
5390 The following commands are available:
5391
5392 \\{gnus-summary-mode-map}"
5393   (interactive)
5394   (if gnus-visual (gnus-summary-make-menu-bar))
5395   (kill-all-local-variables)
5396   (let ((locals gnus-summary-local-variables))
5397     (while locals
5398       (if (consp (car locals))
5399           (progn
5400             (make-local-variable (car (car locals)))
5401             (set (car (car locals)) (eval (cdr (car locals)))))
5402         (make-local-variable (car locals))
5403         (set (car locals) nil))
5404       (setq locals (cdr locals))))
5405   (gnus-make-thread-indent-array)
5406   (setq mode-line-modified "-- ")
5407   (make-local-variable 'mode-line-format)
5408   (setq mode-line-format (copy-sequence mode-line-format))
5409   (and (equal (nth 3 mode-line-format) "   ")
5410        (setcar (nthcdr 3 mode-line-format) ""))
5411   (setq major-mode 'gnus-summary-mode)
5412   (setq mode-name "Summary")
5413   (make-local-variable 'minor-mode-alist)
5414   (use-local-map gnus-summary-mode-map)
5415   (buffer-disable-undo (current-buffer))
5416   (setq buffer-read-only t)             ;Disable modification
5417   (setq truncate-lines t)
5418   (setq selective-display t)
5419   (setq selective-display-ellipses t)   ;Display `...'
5420   (setq buffer-display-table gnus-summary-display-table)
5421   (setq gnus-newsgroup-name group)
5422   (run-hooks 'gnus-summary-mode-hook))
5423
5424 (defun gnus-summary-make-display-table ()
5425   ;; Change the display table.  Odd characters have a tendency to mess
5426   ;; up nicely formatted displays - we make all possible glyphs
5427   ;; display only a single character.
5428
5429   ;; We start from the standard display table, if any.
5430   (setq gnus-summary-display-table 
5431         (or (copy-sequence standard-display-table)
5432             (make-display-table)))
5433   ;; Nix out all the control chars...
5434   (let ((i 32))
5435     (while (>= (setq i (1- i)) 0)
5436       (aset gnus-summary-display-table i [??])))
5437   ;; ... but not newline and cr, of course. (cr is necessary for the
5438   ;; selective display).  
5439   (aset gnus-summary-display-table ?\n nil)
5440   (aset gnus-summary-display-table ?\r nil)
5441   ;; We nix out any glyphs over 126 that are not set already.  
5442   (let ((i 256))
5443     (while (>= (setq i (1- i)) 127)
5444       ;; Only modify if the entry is nil.
5445       (or (aref gnus-summary-display-table i) 
5446           (aset gnus-summary-display-table i [??])))))
5447
5448 (defun gnus-summary-clear-local-variables ()
5449   (let ((locals gnus-summary-local-variables))
5450     (while locals
5451       (if (consp (car locals))
5452           (and (vectorp (car (car locals)))
5453                (set (car (car locals)) nil))
5454         (and (vectorp (car locals))
5455              (set (car locals) nil)))
5456       (setq locals (cdr locals)))))
5457
5458 ;; Some summary mode macros.
5459
5460 ;; Return a header specified by a NUMBER.
5461 (defun gnus-get-header-by-number (number)
5462   (save-excursion
5463     (set-buffer gnus-summary-buffer)
5464     (or gnus-newsgroup-headers-hashtb-by-number
5465         (gnus-make-headers-hashtable-by-number))
5466     (gnus-gethash (int-to-string number)
5467                   gnus-newsgroup-headers-hashtb-by-number)))
5468
5469 ;; Fast version of the function above.
5470 (defmacro gnus-get-header-by-num (number)
5471   (` (gnus-gethash (int-to-string (, number)) 
5472                    gnus-newsgroup-headers-hashtb-by-number)))
5473
5474 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5475   "Search for article forward.
5476 If UNREAD is non-nil, only unread articles are selected.
5477 If SUBJECT is non-nil, the article which has the same subject will be
5478 searched for. 
5479 If BACKWARD is non-nil, the search will be performed backwards instead."
5480   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5481
5482 (defmacro gnus-summary-search-backward (&optional unread subject)
5483   "Search for article backward.
5484 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5485 If 2nd optional argument SUBJECT is non-nil, the article which has
5486 the same subject will be searched for."
5487   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5488
5489 (defmacro gnus-summary-article-number (&optional number-or-nil)
5490   "The article number of the article on the current line.
5491 If there isn's an article number here, then we return the current
5492 article number."
5493   (if number-or-nil
5494       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5495     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5496          gnus-current-article)))
5497
5498 (defmacro gnus-summary-thread-level ()
5499   "The thread level of the article on the current line."
5500   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5501        0))
5502
5503 (defmacro gnus-summary-article-mark ()
5504   "The mark on the current line."
5505   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5506
5507 (defun gnus-summary-subject-string ()
5508   "Return current subject string or nil if nothing."
5509   (let ((article (gnus-summary-article-number))
5510         header)
5511     (and article 
5512          (setq header (gnus-get-header-by-num article))
5513          (vectorp header)
5514          (header-subject header))))
5515
5516
5517 (defun gnus-mouse-pick-article (e)
5518   (interactive "e")
5519   (mouse-set-point e)
5520   (gnus-summary-next-page nil t))
5521
5522 (defun gnus-summary-setup-buffer (group)
5523   "Initialize summary buffer."
5524   (let ((buffer (concat "*Summary " group "*")))
5525     (if (get-buffer buffer)
5526         (progn
5527           (set-buffer buffer)
5528           (not gnus-newsgroup-begin))
5529       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5530       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5531       (gnus-add-current-to-buffer-list)
5532       (gnus-summary-mode group)
5533       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5534       (setq gnus-newsgroup-name group)
5535       t)))
5536
5537 (defun gnus-set-global-variables ()
5538   ;; Set the global equivalents of the summary buffer-local variables
5539   ;; to the latest values they had. These reflect the summary buffer
5540   ;; that was in action when the last article was fetched.
5541   (if (eq major-mode 'gnus-summary-mode) 
5542       (progn
5543         (setq gnus-summary-buffer (current-buffer))
5544         (let ((name gnus-newsgroup-name)
5545               (marked gnus-newsgroup-marked)
5546               (unread gnus-newsgroup-unreads)
5547               (headers gnus-current-headers)
5548               (score-file gnus-current-score-file))
5549           (save-excursion
5550             (set-buffer gnus-group-buffer)
5551             (setq gnus-newsgroup-name name)
5552             (setq gnus-newsgroup-marked marked)
5553             (setq gnus-newsgroup-unreads unread)
5554             (setq gnus-current-headers headers)
5555             (setq gnus-current-score-file score-file))))))
5556
5557 (defun gnus-summary-insert-dummy-line (sformat subject number)
5558   (if (not sformat) 
5559       (setq sformat gnus-summary-dummy-line-format-spec))
5560   (let (b)
5561     (beginning-of-line)
5562     (setq b (point))
5563     (insert (eval sformat))
5564     (add-text-properties
5565      b (1+ b)
5566      (list 'gnus-number number 
5567            'gnus-mark gnus-dummy-mark
5568            'gnus-level 0))))
5569
5570 (defvar gnus-thread-indent-array nil)
5571 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5572 (defun gnus-make-thread-indent-array ()
5573   (let ((n 200))
5574     (if (and gnus-thread-indent-array
5575              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5576         nil
5577       (setq gnus-thread-indent-array (make-vector 201 "")
5578             gnus-thread-indent-array-level gnus-thread-indent-level)
5579       (while (>= n 0)
5580         (aset gnus-thread-indent-array n
5581               (make-string (* n gnus-thread-indent-level) ? ))
5582         (setq n (1- n))))))
5583
5584 (defun gnus-summary-insert-line 
5585   (sformat header level current unread replied expirable subject-or-nil
5586            &optional dummy score)
5587   (or sformat (setq sformat gnus-summary-line-format-spec))
5588   (let* ((indentation (aref gnus-thread-indent-array level))
5589          (lines (header-lines header))
5590          (score (or score gnus-summary-default-score 0))
5591          (score-char
5592           (if (or (null gnus-summary-default-score)
5593                   (<= (abs (- score gnus-summary-default-score))
5594                       gnus-summary-zcore-fuzz)) ? 
5595             (if (< score gnus-summary-default-score)
5596                 gnus-score-below-mark gnus-score-over-mark)))
5597          (replied (if replied gnus-replied-mark ? ))
5598          (from (header-from header))
5599          (name (cond 
5600                 ((string-match "(.+)" from)
5601                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5602                 ((string-match "<[^>]+> *$" from)
5603                  (let ((beg (match-beginning 0)))
5604                    (or (and (string-match "^\"[^\"]*\"" from)
5605                             (substring from (1+ (match-beginning 0))
5606                                        (1- (match-end 0))))
5607                        (substring from 0 beg))))
5608                 (t from)))
5609          (subject (header-subject header))
5610          (number (header-number header))
5611          (opening-bracket (if dummy ?\< ?\[))
5612          (closing-bracket (if dummy ?\> ?\]))
5613          (buffer-read-only nil)
5614          (b (progn (beginning-of-line) (point))))
5615     (or (numberp lines) (setq lines 0))
5616     (insert (eval sformat))
5617     (add-text-properties
5618      b (1+ b) (list 'gnus-number number 
5619                     'gnus-mark (or unread gnus-unread-mark)
5620                     'gnus-level level))))
5621
5622 (defun gnus-summary-update-line (&optional dont-update)
5623   ;; Update summary line after change.
5624   (or (not gnus-summary-default-score)
5625       gnus-summary-inhibit-highlight
5626       (let ((gnus-summary-inhibit-highlight t)
5627             (article (gnus-summary-article-number)))
5628         (progn
5629           (or dont-update
5630               (if (and gnus-summary-mark-below
5631                        (< (gnus-summary-article-score)
5632                           gnus-summary-mark-below))
5633                   (and (not (memq article gnus-newsgroup-marked))
5634                        (not (memq article gnus-newsgroup-dormant))
5635                        (memq article gnus-newsgroup-unreads)
5636                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5637                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5638                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5639           (and gnus-visual
5640                (run-hooks 'gnus-summary-update-hook))))))
5641
5642 (defun gnus-summary-update-lines (&optional beg end)
5643   ;; Mark article as read (or not) by taking into account scores.
5644   (let ((beg (or beg (point-min)))
5645         (end (or end (point-max))))
5646     (if (or (not gnus-summary-default-score)
5647             gnus-summary-inhibit-highlight)
5648         ()
5649       (let ((gnus-summary-inhibit-highlight t)
5650             article)
5651         (save-excursion
5652           (set-buffer gnus-summary-buffer)
5653           (goto-char beg)
5654           (beginning-of-line)
5655           (while (and (not (eobp)) (< (point) end))
5656             (if (and gnus-summary-mark-below
5657                      (< (or (cdr (assq 
5658                                   (setq article (get-text-property 
5659                                                  (point) 'gnus-number))
5660                                   gnus-newsgroup-scored))
5661                             gnus-summary-default-score 0)
5662                         gnus-summary-mark-below))
5663                 ;; We want to possibly mark it as read...
5664                 (and (not (memq article gnus-newsgroup-marked))
5665                      (not (memq article gnus-newsgroup-dormant))
5666                      (memq article gnus-newsgroup-unreads)
5667                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5668               ;; We want to possibly mark it as unread.
5669               (and (eq (get-text-property (point) 'gnus-mark)
5670                        gnus-low-score-mark)
5671                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5672             ;; Do the visual highlights at the same time.
5673             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5674             (forward-line 1)))))))
5675
5676 (defvar gnus-tmp-gathered nil)
5677
5678 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5679   ;; Sum up all elements (and sub-elements) in a list.
5680   (let* ((number 
5681           (if (and (consp thread) (cdr thread)
5682                    (not (memq (header-number (car (car (cdr thread))))
5683                               gnus-tmp-gathered)))
5684               (apply 
5685                '+ 1 (mapcar 'gnus-summary-number-of-articles-in-thread 
5686                             (cdr thread)))
5687             1)))
5688     (if char 
5689         (if (> number 1) gnus-not-empty-thread-mark
5690           gnus-empty-thread-mark)
5691       number)))
5692
5693 (defun gnus-summary-read-group 
5694   (group &optional show-all no-article kill-buffer)
5695   "Start reading news in newsgroup GROUP.
5696 If SHOW-ALL is non-nil, already read articles are also listed.
5697 If NO-ARTICLE is non-nil, no article is selected initially."
5698   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5699   (let* ((new-group (gnus-summary-setup-buffer group))
5700          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5701                                                   group))))
5702          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5703     (cond 
5704      ((not new-group)
5705       (gnus-set-global-variables)
5706       (gnus-kill-buffer kill-buffer)
5707       (gnus-configure-windows 'summary)
5708       (gnus-set-mode-line 'summary)
5709       (gnus-summary-position-cursor)
5710       (message "")
5711       t)
5712      ((null did-select) 
5713       (and (eq major-mode 'gnus-summary-mode)
5714            (not (equal (current-buffer) kill-buffer))
5715            (progn
5716              (kill-buffer (current-buffer))
5717              (if (not quit-config)
5718                  (progn
5719                    (set-buffer gnus-group-buffer)
5720                    (gnus-group-jump-to-group group)
5721                    (gnus-group-next-unread-group 1))
5722                (if (not (buffer-name (car quit-config)))
5723                    (gnus-configure-windows 'group 'force)
5724                  (set-buffer (car quit-config))
5725                  (and (eq major-mode 'gnus-summary-mode)
5726                       (gnus-set-global-variables))
5727                  (gnus-configure-windows (cdr quit-config))))))
5728       (message "Can't select group")
5729       nil)
5730      ((eq did-select 'quit)
5731       (and (eq major-mode 'gnus-summary-mode)
5732            (not (equal (current-buffer) kill-buffer))
5733            (kill-buffer (current-buffer)))
5734       (gnus-kill-buffer kill-buffer)
5735       (if (not quit-config)
5736           (progn
5737             (set-buffer gnus-group-buffer)
5738             (gnus-group-jump-to-group group)
5739             (gnus-group-next-unread-group 1)
5740             (gnus-configure-windows 'group 'force))
5741         (if (not (buffer-name (car quit-config)))
5742             (gnus-configure-windows 'group 'force)
5743           (set-buffer (car quit-config))
5744           (and (eq major-mode 'gnus-summary-mode)
5745                (gnus-set-global-variables))
5746           (gnus-configure-windows (cdr quit-config))))
5747       (signal 'quit nil))
5748      (t
5749       (gnus-set-global-variables)
5750       ;; Save the active value in effect when the group was entered.
5751       (setq gnus-newsgroup-active 
5752             (gnus-copy-sequence
5753              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5754       ;; You can change the subjects in this hook.
5755       (run-hooks 'gnus-select-group-hook)
5756       ;; Do score processing.
5757       (and gnus-use-scoring (gnus-possibly-score-headers))
5758       (gnus-update-format-specifications)
5759       ;; Generate the summary buffer.
5760       (gnus-summary-prepare)
5761       (if (zerop (buffer-size))
5762           (cond (gnus-newsgroup-dormant
5763                  (gnus-summary-show-all-dormant))
5764                 ((and gnus-newsgroup-scored show-all)
5765                  (gnus-summary-show-all-expunged))))
5766       ;; Function `gnus-apply-kill-file' must be called in this hook.
5767       (run-hooks 'gnus-apply-kill-hook)
5768       (if (zerop (buffer-size))
5769           (progn
5770             ;; This newsgroup is empty.
5771             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5772             (gnus-message 6 "No unread news")
5773             (gnus-kill-buffer kill-buffer)
5774             nil)
5775         ;;(save-excursion
5776         ;;  (if kill-buffer
5777         ;;      (let ((gnus-summary-buffer kill-buffer))
5778         ;;      (gnus-configure-windows 'group))))
5779         ;; Hide conversation thread subtrees.  We cannot do this in
5780         ;; gnus-summary-prepare-hook since kill processing may not
5781         ;; work with hidden articles.
5782         (and gnus-show-threads
5783              gnus-thread-hide-subtree
5784              (gnus-summary-hide-all-threads))
5785         ;; Show first unread article if requested.
5786         (goto-char (point-min))
5787         (if (and (not no-article)
5788                  gnus-auto-select-first
5789                  (gnus-summary-first-unread-article))
5790             ()
5791           (gnus-configure-windows 'summary))
5792         (gnus-set-mode-line 'summary)
5793         (gnus-summary-position-cursor)
5794         ;; If in async mode, we send some info to the backend.
5795         (and gnus-newsgroup-async
5796              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5797              (gnus-request-asynchronous 
5798               gnus-newsgroup-name
5799               (if (and gnus-asynchronous-article-function
5800                        (fboundp gnus-asynchronous-article-function))
5801                   (funcall gnus-asynchronous-article-function
5802                            gnus-newsgroup-threads)
5803                 gnus-newsgroup-threads)))
5804         (gnus-kill-buffer kill-buffer)
5805         (if (not (get-buffer-window gnus-group-buffer))
5806             ()
5807           ;; gotta use windows, because recenter does wierd stuff if
5808           ;; the current buffer ain't the displayed window.
5809           (let ((owin (selected-window))) 
5810             (select-window (get-buffer-window gnus-group-buffer))
5811             (and (gnus-group-goto-group group)
5812                  (recenter))
5813             (select-window owin))))
5814       t))))
5815
5816 (defun gnus-summary-prepare ()
5817   ;; Generate the summary buffer.
5818   (let ((buffer-read-only nil))
5819     (erase-buffer)
5820     (gnus-message 5 "Threading...")
5821     (gnus-summary-prepare-threads 
5822      (if gnus-show-threads
5823          (gnus-gather-threads 
5824           (gnus-sort-threads 
5825            (if (and gnus-summary-expunge-below
5826                     (not gnus-fetch-old-headers))
5827                (gnus-make-threads-and-expunge)
5828              (gnus-make-threads))))
5829        gnus-newsgroup-headers)
5830      'cull)
5831     (gnus-message 5 "Threading...done")
5832     (gnus-summary-update-lines)
5833     ;; Remove the final newline.
5834     ;;(goto-char (point-max))
5835     ;;(delete-char -1)
5836     ;; Call hooks for modifying summary buffer.
5837     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5838     (goto-char (point-min))
5839     (run-hooks 'gnus-summary-prepare-hook)))
5840
5841 (defun gnus-gather-threads (threads)
5842   "Gather threads that have lost their roots."
5843   (if (not gnus-summary-make-false-root)
5844       threads 
5845     (let ((hashtb (gnus-make-hashtable 1023))
5846           (prev threads)
5847           (result threads)
5848           subject hthread whole-subject)
5849       (while threads
5850         (setq whole-subject 
5851               (setq subject (header-subject (car (car threads)))))
5852         (if gnus-summary-gather-subject-limit
5853             (or (and (numberp gnus-summary-gather-subject-limit)
5854                      (> (length subject) gnus-summary-gather-subject-limit)
5855                      (setq subject
5856                            (substring subject 0 
5857                                       gnus-summary-gather-subject-limit)))
5858                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5859                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5860           (setq subject (gnus-simplify-subject-re subject)))
5861         (if (setq hthread 
5862                   (gnus-gethash subject hashtb))
5863             (progn
5864               (or (stringp (car (car hthread)))
5865                   (setcar hthread (list whole-subject (car hthread))))
5866               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5867                                            (list (car threads))))
5868               (setcdr prev (cdr threads))
5869               (setq threads prev))
5870           (gnus-sethash subject threads hashtb))
5871         (setq prev threads)
5872         (setq threads (cdr threads)))
5873       result)))
5874
5875 (defun gnus-make-threads ()
5876   ;; This function takes the dependencies already made by 
5877   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5878   ;; through the dependecies in the hash table and finds all the
5879   ;; roots. Roots do not refer back to any valid articles.
5880   (let (roots new-roots)
5881     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5882          (gnus-build-old-threads))
5883     (mapatoms
5884      (lambda (refs)
5885        (if (not (car (symbol-value refs)))
5886            (setq roots (append (cdr (symbol-value refs)) roots))
5887          ;; Ok, these refer back to valid articles, but if
5888          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5889          ;; the root has the same subject as its children. The children
5890          ;; that do not are made into roots and removed from the list
5891          ;; of children. 
5892          (or gnus-thread-ignore-subject
5893              (let* ((prev (symbol-value refs))
5894                     (subject (gnus-simplify-subject-re 
5895                               (header-subject (car prev))))
5896                     (headers (cdr prev)))
5897                (while headers
5898                  (if (not (string= subject
5899                                    (gnus-simplify-subject-re 
5900                                     (header-subject (car headers)))))
5901                      (progn
5902                        (setq new-roots (cons (car headers) new-roots))
5903                        (setcdr prev (cdr headers)))
5904                    (setq prev headers))
5905                  (setq headers (cdr headers)))))))
5906      gnus-newsgroup-dependencies)
5907
5908     ;; We enter the new roots into the dependencies structure to
5909     ;; ensure that any possible later thread-regeneration will be
5910     ;; possible. 
5911     (let ((r new-roots))
5912       (while r
5913         (gnus-sethash (concat (header-id (car r)) ".boo")
5914                       (list nil (car r)) gnus-newsgroup-dependencies)
5915         (setq r (cdr r))))
5916
5917     (setq roots (nconc new-roots roots))
5918     
5919     (mapcar 'gnus-trim-thread
5920             (apply 'append
5921                    (mapcar 'gnus-cut-thread
5922                            (mapcar 'gnus-make-sub-thread roots))))))
5923   
5924 (defun gnus-make-threads-and-expunge ()
5925   ;; This function takes the dependencies already made by 
5926   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5927   ;; through the dependecies in the hash table and finds all the
5928   ;; roots. Roots do not refer back to any valid articles.
5929   (let ((default (or gnus-summary-default-score 0))
5930         (below gnus-summary-expunge-below)
5931         roots article new-roots)
5932     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5933          (gnus-build-old-threads))
5934     (mapatoms
5935      (lambda (refs)
5936        (if (not (car (symbol-value refs)))
5937            ;; These articles do not refer back to any other articles -
5938            ;; they are roots.
5939            (let ((headers (cdr (symbol-value refs))))
5940              ;; We weed out the low-scored articles.
5941              (while headers
5942                (if (not (< (or (cdr (assq (header-number (car headers))
5943                                           gnus-newsgroup-scored)) default)
5944                            below))
5945                    ;; It is over.
5946                    (setq roots (cons (car headers) roots))
5947                  ;; It is below, so we mark it as read.
5948                  (setq gnus-newsgroup-unreads
5949                        (delq (header-number (car headers))
5950                              gnus-newsgroup-unreads)))
5951                (setq headers (cdr headers))))
5952          ;; Ok, these refer back to valid articles, but if
5953          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5954          ;; the root has the same subject as its children. The children
5955          ;; that do not are made into roots and removed from the list
5956          ;; of children. 
5957          (or gnus-thread-ignore-subject
5958              (let* ((prev (symbol-value refs))
5959                     (subject (gnus-simplify-subject-re 
5960                               (header-subject (car prev))))
5961                     (headers (cdr prev)))
5962                (while headers
5963                  (if (not (string= subject
5964                                    (gnus-simplify-subject-re 
5965                                     (header-subject (car headers)))))
5966                      (progn
5967                        (if (not (< (or (cdr (assq (header-number (car headers))
5968                                                   gnus-newsgroup-scored))
5969                                        default) below))
5970                            (setq new-roots (cons (car headers) new-roots))
5971                          (setq gnus-newsgroup-unreads
5972                                (delq (header-number (car headers))
5973                                      gnus-newsgroup-unreads)))
5974                        (setcdr prev (cdr headers)))
5975                    (setq prev headers))
5976                  (setq headers (cdr headers)))))
5977          ;; If this article is expunged, some of the children might be
5978          ;; roots.  
5979          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5980                                gnus-newsgroup-scored)) default)
5981                 below)
5982              (let* ((prev (symbol-value refs))
5983                     (headers (cdr prev)))
5984                (while headers
5985                  (setq article (header-number (car headers)))
5986                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5987                                  default) below))
5988                      (progn (setq new-roots (cons (car headers) new-roots))
5989                             (setq prev headers))
5990                    (setq gnus-newsgroup-unreads 
5991                          (delq article gnus-newsgroup-unreads))
5992                    (setcdr prev (cdr headers)))
5993                  (setq headers (cdr headers))))
5994            ;; It was not expunged, but we look at expunged children.
5995            (let* ((prev (symbol-value refs))
5996                   (headers (cdr prev))
5997                   article)
5998              (while headers
5999                (setq article (header-number (car headers)))
6000                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6001                                default) below))
6002                    (setq prev headers)
6003                  (setq gnus-newsgroup-unreads 
6004                        (delq article gnus-newsgroup-unreads))
6005                  (setcdr prev (cdr headers)))
6006                (setq headers (cdr headers)))))))
6007      gnus-newsgroup-dependencies)
6008
6009     ;; We enter the new roots into the dependencies structure to
6010     ;; ensure that any possible later thread-regeneration will be
6011     ;; possible. 
6012     (let ((r new-roots))
6013       (while r
6014         (gnus-sethash (concat (header-id (car r)) ".boo")
6015                       (list nil (car r)) gnus-newsgroup-dependencies)
6016         (setq r (cdr r))))
6017
6018     (setq roots (nconc new-roots roots))
6019     
6020     (mapcar 'gnus-trim-thread
6021             (apply 'append
6022                    (mapcar 'gnus-cut-thread
6023                            (mapcar 'gnus-make-sub-thread roots))))))
6024   
6025 (defun gnus-cut-thread (thread)
6026   ;; Remove leaf dormant or ancient articles from THREAD.
6027   (let ((head (car thread))
6028         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6029     (if (and (null tail)
6030              (let ((number (header-number head)))
6031                (or (memq number gnus-newsgroup-ancient)
6032                    (memq number gnus-newsgroup-dormant)
6033                    (and gnus-summary-expunge-below
6034                         (eq gnus-fetch-old-headers 'some)
6035                         (< (or (cdr (assq number gnus-newsgroup-scored))
6036                                gnus-summary-default-score 0)
6037                            gnus-summary-expunge-below)
6038                         (progn
6039                           (setq gnus-newsgroup-unreads
6040                                 (delq number gnus-newsgroup-unreads))
6041                           t)))))
6042         nil
6043       (list (cons head tail)))))
6044
6045 (defun gnus-trim-thread (thread)
6046   ;; Remove root ancient articles with only one child from THREAD.
6047   (if (and (eq gnus-fetch-old-headers 'some)
6048            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6049            (= (length thread) 2))
6050       (gnus-trim-thread (nth 1 thread))
6051     thread))
6052
6053 (defun gnus-make-sub-thread (root)
6054   ;; This function makes a sub-tree for a node in the tree.
6055   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6056                                               gnus-newsgroup-dependencies)))))
6057     (cons root (mapcar 'gnus-make-sub-thread children))))
6058
6059 (defun gnus-build-old-threads ()
6060   ;; Look at all the articles that refer back to old articles, and
6061   ;; fetch the headers for the articles that aren't there. This will
6062   ;; build complete threads - if the roots haven't been expired by the
6063   ;; server, that is.
6064   (let (id heads)
6065     (mapatoms
6066      (lambda (refs)
6067        (if (not (car (symbol-value refs)))
6068            (progn
6069              (setq heads (cdr (symbol-value refs)))
6070              (while heads
6071                (if (not (memq (header-number (car heads))
6072                               gnus-newsgroup-dormant))
6073                    (progn
6074                      (setq id (symbol-name refs))
6075                      (while (and (setq id (gnus-build-get-header id))
6076                                  (not (car (gnus-gethash 
6077                                             id gnus-newsgroup-dependencies)))))
6078                      (setq heads nil))
6079                  (setq heads (cdr heads)))))))
6080      gnus-newsgroup-dependencies)))
6081
6082 (defun gnus-build-get-header (id)
6083   ;; Look through the buffer of NOV lines and find the header to
6084   ;; ID. Enter this line into the dependencies hash table, and return
6085   ;; the id of the parent article (if any).
6086   (let ((deps gnus-newsgroup-dependencies)
6087         found header)
6088     (prog1
6089         (save-excursion
6090           (set-buffer nntp-server-buffer)
6091           (goto-char (point-min))
6092           (while (and (not found) (search-forward id nil t))
6093             (beginning-of-line)
6094             (setq found (looking-at 
6095                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6096                                  (regexp-quote id))))
6097             (or found (beginning-of-line 2)))
6098           (if found
6099               (let (ref)
6100                 (beginning-of-line)
6101                 (and
6102                  (setq header (gnus-nov-parse-line 
6103                                (read (current-buffer)) deps))
6104                  (setq ref (header-references header))
6105                  (string-match "\\(<[^>]+>\\) *$" ref)
6106                  (substring ref (match-beginning 1) (match-end 1))))))
6107       (and header
6108            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6109                  gnus-newsgroup-ancient (cons (header-number header)
6110                                               gnus-newsgroup-ancient))))))
6111
6112 ;; Re-build the thread containing ID.
6113 (defun gnus-rebuild-thread (id)
6114   (let ((dep gnus-newsgroup-dependencies)
6115         (buffer-read-only nil)
6116         parent headers refs thread art)
6117     (while (and id (setq headers
6118                          (car (setq art (gnus-gethash (downcase id) dep)))))
6119       (setq parent art)
6120       (setq id (and (setq refs (header-references headers))
6121                     (string-match "\\(<[^>]+>\\) *$" refs)
6122                     (substring refs (match-beginning 1) (match-end 1)))))
6123     (setq thread (gnus-make-sub-thread (car parent)))
6124     (gnus-rebuild-remove-articles thread)
6125     (let ((beg (point)))
6126       (gnus-summary-prepare-threads (list thread))
6127       (gnus-summary-update-lines beg (point)))))
6128
6129 ;; Delete all lines in the summary buffer that correspond to articles
6130 ;; in this thread.
6131 (defun gnus-rebuild-remove-articles (thread)
6132   (and (gnus-summary-goto-subject (header-number (car thread)))
6133        (gnus-delete-line))
6134   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6135
6136 (defun gnus-sort-threads (threads)
6137   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6138   (let ((fun gnus-thread-sort-functions))
6139     (while fun
6140       (setq threads (sort threads (car fun))
6141             fun (cdr fun))))
6142   threads)
6143
6144 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6145 (defmacro gnus-thread-header (thread)
6146   ;; Return header of first article in THREAD.
6147   ;; Note that THREAD must never, evr be anything else than a variable -
6148   ;; using some other form will lead to serious barfage.
6149   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6150   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6151   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6152         (vector thread) 2))
6153
6154 (defun gnus-thread-sort-by-number (h1 h2)
6155   "Sort threads by root article number."
6156   (< (header-number (gnus-thread-header h1))
6157      (header-number (gnus-thread-header h2))))
6158
6159 (defun gnus-thread-sort-by-author (h1 h2)
6160   "Sort threads by root author."
6161   (string-lessp
6162    (let ((extract (funcall 
6163                    gnus-extract-address-components
6164                    (header-from (gnus-thread-header h1)))))
6165      (or (car extract) (cdr extract)))
6166    (let ((extract (funcall
6167                    gnus-extract-address-components 
6168                    (header-from (gnus-thread-header h2)))))
6169      (or (car extract) (cdr extract)))))
6170
6171 (defun gnus-thread-sort-by-subject (h1 h2)
6172   "Sort threads by root subject."
6173   (string-lessp
6174    (downcase (gnus-simplify-subject 
6175               (header-subject (gnus-thread-header h1))))
6176    (downcase (gnus-simplify-subject 
6177               (header-subject (gnus-thread-header h2))))))
6178
6179 (defun gnus-thread-sort-by-date (h1 h2)
6180   "Sort threads by root article date."
6181   (string-lessp
6182    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6183    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6184
6185 (defun gnus-thread-sort-by-score (h1 h2)
6186   "Sort threads by root article score.
6187 Unscored articles will be counted as having a score of zero."
6188   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6189                     gnus-newsgroup-scored))
6190          gnus-summary-default-score 0)
6191      (or (cdr (assq (header-number (gnus-thread-header h2))
6192                     gnus-newsgroup-scored))
6193          gnus-summary-default-score 0)))
6194
6195 (defun gnus-thread-sort-by-total-score (h1 h2)
6196   "Sort threads by the sum of all scores in the thread.
6197 Unscored articles will be counted as having a score of zero."
6198   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6199
6200 (defun gnus-thread-total-score (thread)
6201   ;;  This function find the total score of THREAD.
6202   (if (consp thread)
6203       (if (stringp (car thread))
6204           (apply gnus-thread-score-function 0
6205                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6206         (gnus-thread-total-score-1 thread))
6207     (gnus-thread-total-score-1 (list thread))))
6208
6209 (defun gnus-thread-total-score-1 (root)
6210   ;; This function find the total score of the thread below ROOT.
6211   (setq root (car root))
6212   (apply gnus-thread-score-function
6213          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6214              gnus-summary-default-score 0)
6215          (mapcar 'gnus-thread-total-score
6216                  (cdr (gnus-gethash (downcase (header-id root))
6217                                     gnus-newsgroup-dependencies)))))
6218
6219 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6220 (defvar gnus-tmp-prev-subject "")
6221
6222 (defun gnus-summary-prepare-threads (threads &optional cull)
6223   "Prepare summary buffer from THREADS and indentation LEVEL.  
6224 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6225 or a straight list of headers."
6226   (let ((level 0)
6227         thread header number subject stack state gnus-tmp-gathered)
6228     (if (vectorp (car threads))
6229         ;; If this is a straight (sic) list of headers, then a
6230         ;; threaded summary display isn't required, so we just create
6231         ;; an unthreaded one.
6232         (gnus-summary-prepare-unthreaded threads cull)
6233
6234       ;; Do the threaded display.
6235
6236       (while (or threads stack)
6237         
6238         (if threads
6239             ;; If there are some threads, we do them before the
6240             ;; threads on the stack.
6241             (setq thread threads
6242                   header (car (car thread)))
6243           ;; There were no current threads, so we pop something off
6244           ;; the stack. 
6245           (setq state (car stack)
6246                 level (car state)
6247                 thread (cdr state)
6248                 stack (cdr stack)
6249                 header (car (car thread))))
6250
6251         (if (stringp header)
6252             (progn
6253               ;; The header is a dummy root.
6254               (cond 
6255                ((eq gnus-summary-make-false-root 'adopt)
6256                 ;; We let the first article adopt the rest.
6257                 (let ((th (car (cdr (car thread)))))
6258                   (while (cdr th)
6259                     (setq th (cdr th)))
6260                   (setcdr th (cdr (cdr (car thread))))
6261                   (setq gnus-tmp-gathered 
6262                         (nconc (mapcar
6263                                 (lambda (h) (header-number (car h)))
6264                                 (cdr (cdr (car thread))))
6265                                gnus-tmp-gathered))
6266                   (setcdr (cdr (car thread)) nil))
6267                 (setq level -1))
6268                ((eq gnus-summary-make-false-root 'empty)
6269                 ;; We print adopted articles with empty subject fields.
6270                 (setq gnus-tmp-gathered 
6271                       (nconc (mapcar
6272                               (lambda (h) (header-number (car h)))
6273                               (cdr (cdr (car thread))))
6274                              gnus-tmp-gathered))
6275                 (setq level -1))
6276                ((eq gnus-summary-make-false-root 'dummy)
6277                 ;; We output a dummy root.
6278                 (gnus-summary-insert-dummy-line 
6279                  nil header (header-number (car (car (cdr (car thread)))))))
6280                (t
6281                 ;; We do not make a root for the gathered
6282                 ;; sub-threads at all.  
6283                 (setq level -1))))
6284       
6285           (setq number (header-number header)
6286                 subject (header-subject header))
6287
6288           ;; Do the async thing.
6289           (and gnus-newsgroup-async
6290                (setq gnus-newsgroup-threads
6291                      (cons (cons number (header-lines header)) 
6292                            gnus-newsgroup-threads)))
6293
6294           ;; We may have to root out some bad articles...
6295           (and cull
6296                (= level 0)
6297                (cond ((and (memq (setq number (header-number header))
6298                                  gnus-newsgroup-dormant)
6299                            (null threads))
6300                       (setq header nil))
6301                      ((and gnus-summary-expunge-below
6302                            (< (or (cdr (assq number gnus-newsgroup-scored))
6303                                   gnus-summary-default-score 0)
6304                               gnus-summary-expunge-below))
6305                       (setq header nil)
6306                       (setq gnus-newsgroup-unreads 
6307                             (delq number gnus-newsgroup-unreads)))))
6308
6309           (and
6310            header
6311            (progn
6312              (inline
6313                (gnus-summary-insert-line
6314                 nil header level nil 
6315                 (cond 
6316                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6317                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6318                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6319                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6320                  (t gnus-ancient-mark))
6321                 (memq number gnus-newsgroup-replied)
6322                 (memq number gnus-newsgroup-expirable)
6323                 (if (and (eq gnus-summary-make-false-root 'empty)
6324                          (memq number gnus-tmp-gathered))
6325                     gnus-summary-same-subject
6326                   (if (or (zerop level)
6327                           (and gnus-thread-ignore-subject
6328                                (not (string= 
6329                                      (gnus-simplify-subject-re
6330                                       gnus-tmp-prev-subject)
6331                                      (gnus-simplify-subject-re
6332                                       subject)))))
6333                       subject
6334                     gnus-summary-same-subject))
6335                 (and (eq gnus-summary-make-false-root 'adopt)
6336                      (memq number gnus-tmp-gathered))
6337                 (cdr (assq number gnus-newsgroup-scored)))
6338
6339                (setq gnus-tmp-prev-subject subject)))))
6340
6341         (if (nth 1 thread) 
6342             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6343         (setq level (1+ level))
6344         (setq threads (cdr (car thread)))))))
6345
6346
6347 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6348   (let (header number)
6349
6350     ;; Do the async thing, if that is required.
6351     (if gnus-newsgroup-async
6352         (setq gnus-newsgroup-threads
6353               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6354                       headers)))
6355
6356     (while headers
6357       (setq header (car headers)
6358             headers (cdr headers)
6359             number (header-number header))
6360
6361       ;; We may have to root out some bad articles...
6362       (cond 
6363        ((and cull
6364              (memq (setq number (header-number header))
6365                    gnus-newsgroup-dormant)))
6366        ((and cull gnus-summary-expunge-below
6367              (< (or (cdr (assq number gnus-newsgroup-scored))
6368                     gnus-summary-default-score 0)
6369                 gnus-summary-expunge-below))
6370         (setq gnus-newsgroup-unreads 
6371               (delq number gnus-newsgroup-unreads)))
6372        (t
6373         (gnus-summary-insert-line
6374          nil header 0 nil 
6375          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6376                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6377                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6378                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6379                (t gnus-ancient-mark))
6380          (memq number gnus-newsgroup-replied)
6381          (memq number gnus-newsgroup-expirable)
6382          (header-subject header) nil
6383          (cdr (assq number gnus-newsgroup-scored))))))))
6384
6385 (defun gnus-select-newsgroup (group &optional read-all)
6386   "Select newsgroup GROUP.
6387 If READ-ALL is non-nil, all articles in the group are selected."
6388   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6389          (info (nth 2 entry))
6390          articles)
6391     (gnus-check-news-server
6392      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6393
6394     (or (gnus-server-opened gnus-current-select-method)
6395         (gnus-open-server gnus-current-select-method)
6396         (error "Couldn't open server"))
6397     
6398     (or (and (eq (car entry) t)
6399              (gnus-activate-newsgroup (car info)))
6400         (gnus-request-group group t)
6401         (progn
6402           (kill-buffer (current-buffer))
6403           (error "Couldn't request group %s: %s" 
6404                  group (gnus-status-message group))))
6405
6406     (setq gnus-newsgroup-name group)
6407     (setq gnus-newsgroup-unselected nil)
6408     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6409
6410     (and gnus-asynchronous
6411          (gnus-check-backend-function 
6412           'request-asynchronous gnus-newsgroup-name)
6413          (setq gnus-newsgroup-async
6414                (gnus-request-asynchronous gnus-newsgroup-name)))
6415
6416     (setq articles (gnus-articles-to-read group read-all))
6417
6418     (cond 
6419      ((null articles) 
6420       (gnus-message 3 "Couldn't select newsgroup")
6421       'quit)
6422      ((eq articles 0) nil)
6423      (t
6424       ;; Init the dependencies hash table.
6425       (setq gnus-newsgroup-dependencies 
6426             (gnus-make-hashtable (length articles)))
6427       ;; Retrieve the headers and read them in.
6428       (gnus-message 5 "Fetching headers...")
6429       (setq gnus-newsgroup-headers 
6430             (if (eq 'nov (setq gnus-headers-retrieved-by
6431                                (gnus-retrieve-headers 
6432                                 (if (and gnus-fetch-old-headers 
6433                                          (not (eq 1 (car articles))))
6434                                     (cons 1 articles)
6435                                   articles)
6436                                 gnus-newsgroup-name)))
6437                 (progn
6438                   (gnus-get-newsgroup-headers-xover articles))
6439               ;; If we were to fetch old headers, but the backend didn't
6440               ;; support XOVER, then it is possible we fetched one article
6441               ;; that we shouldn't have. If that's the case, we pop it off the
6442               ;; list of headers.
6443               (if (not gnus-fetch-old-headers)
6444                   ()
6445                 (save-excursion
6446                   (set-buffer nntp-server-buffer)
6447                   (goto-char (point-min))
6448                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6449                        (delete-region 
6450                         (point) 
6451                         (search-forward "\n.\n" nil t)))))
6452               (gnus-get-newsgroup-headers)))
6453       (gnus-message 5 "Fetching headers...done")      
6454       ;; Remove canceled articles from the list of unread articles.
6455       (setq gnus-newsgroup-unreads
6456             (gnus-set-sorted-intersection 
6457              gnus-newsgroup-unreads
6458              (mapcar (lambda (headers) (header-number headers))
6459                      gnus-newsgroup-headers)))
6460       ;; Adjust and set lists of article marks.
6461       (and info
6462            (let (marked)
6463              (gnus-adjust-marked-articles info)
6464              (setq gnus-newsgroup-marked 
6465                    (copy-sequence
6466                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6467              (setq gnus-newsgroup-replied 
6468                    (copy-sequence (cdr (assq 'reply marked))))
6469              (setq gnus-newsgroup-expirable
6470                    (copy-sequence (cdr (assq 'expire marked))))
6471              (setq gnus-newsgroup-killed
6472                    (copy-sequence (cdr (assq 'killed marked))))
6473              (setq gnus-newsgroup-bookmarks 
6474                    (copy-sequence (cdr (assq 'bookmark marked))))
6475              (setq gnus-newsgroup-dormant 
6476                    (copy-sequence (cdr (assq 'dormant marked))))
6477              (setq gnus-newsgroup-scored 
6478                    (copy-sequence (cdr (assq 'score marked))))
6479              (setq gnus-newsgroup-processable nil)))
6480       ;; Create the header hashtb.
6481       (or gnus-newsgroup-headers-hashtb-by-number
6482           (gnus-make-headers-hashtable-by-number))
6483       ;; Check whether auto-expire is to be done in this group.
6484       (setq gnus-newsgroup-auto-expire
6485             (or (and (stringp gnus-auto-expirable-newsgroups)
6486                      (string-match gnus-auto-expirable-newsgroups group))
6487                 (memq 'auto-expire (nth 5 info))))
6488       ;; First and last article in this newsgroup.
6489       (and gnus-newsgroup-headers
6490            (setq gnus-newsgroup-begin 
6491                  (header-number (car gnus-newsgroup-headers)))
6492            (setq gnus-newsgroup-end
6493                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6494       (setq gnus-reffed-article-number -1)
6495       ;; GROUP is successfully selected.
6496       (or gnus-newsgroup-headers t)))))
6497
6498 (defun gnus-articles-to-read (group read-all)
6499   ;; Find out what articles the user wants to read.
6500   (let* ((articles
6501           ;; Select all articles if `read-all' is non-nil, or if all the
6502           ;; unread articles are dormant articles.
6503           (if (or (and read-all (not (numberp read-all)))
6504                   (= (length gnus-newsgroup-unreads) 
6505                      (length gnus-newsgroup-dormant)))
6506               (gnus-uncompress-range 
6507                (gnus-gethash group gnus-active-hashtb))
6508             gnus-newsgroup-unreads))
6509          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6510          (scored (length scored-list))
6511          (number (length articles))
6512          (marked (+ (length gnus-newsgroup-marked)
6513                     (length gnus-newsgroup-dormant)))
6514          (select
6515           (cond 
6516            ((numberp read-all)
6517             read-all)
6518            (t
6519             (condition-case ()
6520                 (cond ((and (or (<= scored marked)
6521                                 (= scored number))
6522                             (numberp gnus-large-newsgroup)
6523                             (> number gnus-large-newsgroup))
6524                        (let ((input
6525                               (read-string
6526                                (format
6527                                 "How many articles from %s (default %d): "
6528                                 gnus-newsgroup-name number))))
6529                          (if (string-match "^[ \t]*$" input)
6530                              number input)))
6531                       ((and (> scored marked) (< scored number))
6532                        (let ((input
6533                               (read-string
6534                                (format 
6535                                 "%s %s (%d scored, %d total): "
6536                                 "How many articles from"
6537                                 group scored number))))
6538                          (if (string-match "^[ \t]*$" input)
6539                              number input)))
6540                       (t number))
6541               (quit nil))))))
6542     (setq select (if (stringp select) (string-to-number select) select))
6543     (if (or (null select) (zerop select))
6544         select
6545       (if (and (not (zerop scored)) (<= (abs select) scored))
6546           (progn
6547             (setq articles (sort scored-list '<))
6548             (setq number (length articles)))
6549         (setq articles (copy-sequence articles)))
6550
6551       (if (< (abs select) number)
6552           (if (< select 0) 
6553               ;; Select the N oldest articles.
6554               (setcdr (nthcdr (1- (abs select)) articles) nil)
6555             ;; Select the N most recent articles.
6556             (setq articles (nthcdr (- number select) articles))))
6557       (setq gnus-newsgroup-unselected
6558             (gnus-sorted-intersection
6559              gnus-newsgroup-unreads
6560              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6561       articles)))
6562
6563 (defun gnus-killed-articles (killed articles)
6564   (let (out)
6565     (while articles
6566       (if (inline (gnus-member-of-range (car articles) killed))
6567           (setq out (cons (car articles) out)))
6568       (setq articles (cdr articles)))
6569     out))
6570
6571 (defun gnus-adjust-marked-articles (info &optional active)
6572   "Remove all marked articles that are no longer legal."
6573   (let ((marked-lists (nth 3 info))
6574         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6575         m prev)
6576     ;; There are many types of marked articles.
6577     (while marked-lists
6578       (setq m (cdr (setq prev (car marked-lists))))
6579       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6580              ;; Make sure that all ticked articles are a subset of the
6581              ;; unread/unselected articles.
6582              (while m
6583                (if (or (memq (car m) gnus-newsgroup-unreads)
6584                        (memq (car m) gnus-newsgroup-unselected))
6585                    (setq prev m)
6586                  (setcdr prev (cdr m)))
6587                (setq m (cdr m))))
6588             ((eq 'score (car prev))
6589              ;; Scored articles should be a subset of
6590              ;; unread/unselected articles. 
6591              (while m
6592                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6593                        (memq (car (car m)) gnus-newsgroup-unreads))
6594                    (setq prev m)
6595                  (setcdr prev (cdr m)))
6596                (setq m (cdr m))))
6597             ((eq 'bookmark (car prev))
6598              ;; Bookmarks should be a subset of active articles.
6599              (while m
6600                (if (< (car (car m)) (car active))
6601                    (setcdr prev (cdr m))
6602                  (setq prev m))
6603                (setq m (cdr m))))
6604             ((eq 'killed (car prev))
6605              ;; Articles that have been through the kill process are
6606              ;; to be a subset of active articles.
6607              (while (and m (< (or (and (numberp (car m)) (car m))
6608                                   (cdr (car m)))
6609                               (car active)))
6610                (setcdr prev (cdr m))
6611                (setq m (cdr m)))
6612              (if (and m (< (or (and (numberp (car m)) (car m))
6613                                (car (car m)))
6614                            (car active))) 
6615                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6616             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6617              ;; The replied and expirable articles have to be articles
6618              ;; that are active. 
6619              (while m
6620                (if (< (car m) (car active))
6621                    (setcdr prev (cdr m))
6622                  (setq prev m))
6623                (setq m (cdr m)))))
6624       (setq marked-lists (cdr marked-lists)))
6625     ;; Remove all lists that are empty.
6626     (setq marked-lists (nth 3 info))
6627     (if marked-lists
6628         (progn
6629           (while (= 1 (length (car marked-lists)))
6630             (setq marked-lists (cdr marked-lists)))
6631           (setq m (cdr (setq prev marked-lists)))
6632           (while m
6633             (if (= 1 (length (car m)))
6634                 (setcdr prev (cdr m))
6635               (setq prev m))
6636             (setq m (cdr m)))
6637           (setcar (nthcdr 3 info) marked-lists)))
6638     ;; Finally, if there are no marked lists at all left, and if there
6639     ;; are no elements after the lists in the info list, we just chop
6640     ;; the info list off before the marked lists.
6641     (and (null marked-lists) 
6642          (not (nthcdr 4 info))
6643          (setcdr (nthcdr 2 info) nil)))
6644   info)
6645
6646 (defun gnus-set-marked-articles 
6647   (info ticked replied expirable killed dormant bookmark score) 
6648   "Enter the various lists of marked articles into the newsgroup info list."
6649   (let (newmarked)
6650     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6651     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6652     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6653                                          newmarked)))
6654     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6655     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6656     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6657                                         newmarked)))
6658     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6659     (if (nthcdr 3 info)
6660         (progn
6661           (setcar (nthcdr 3 info) newmarked)
6662           (and (not newmarked)
6663                (not (nthcdr 4 info))
6664                (setcdr (nthcdr 2 info) nil)))
6665       (if newmarked
6666           (setcdr (nthcdr 2 info) (list newmarked))))))
6667
6668 (defun gnus-add-marked-articles (group type articles &optional info force)
6669   ;; Add ARTICLES of TYPE to the info of GROUP.
6670   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6671   ;; add, but replace marked articles of TYPE with ARTICLES.
6672   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6673         marked m)
6674     (or (not info)
6675         (and (not (setq marked (nthcdr 3 info)))
6676              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6677         (and (not (setq m (assq type (car marked))))
6678              (setcar marked (cons (cons type articles) (car marked))))
6679         (if force
6680             (setcdr m articles)
6681           (nconc m articles)))))
6682          
6683 (defun gnus-set-mode-line (where)
6684   "This function sets the mode line of the article or summary buffers.
6685 If WHERE is `summary', the summary mode line format will be used."
6686   (if (memq where gnus-updated-mode-lines)
6687       (let (mode-string)
6688         (save-excursion
6689           (set-buffer gnus-summary-buffer)
6690           (let* ((mformat (if (eq where 'article) 
6691                               gnus-article-mode-line-format-spec
6692                             gnus-summary-mode-line-format-spec))
6693                  (group-name gnus-newsgroup-name)
6694                  (article-number (or gnus-current-article 0))
6695                  (unread (- (length gnus-newsgroup-unreads)
6696                             (length gnus-newsgroup-dormant)))
6697                  (unread-and-unticked 
6698                   (- unread (length gnus-newsgroup-marked)))
6699                  (unselected (length gnus-newsgroup-unselected))
6700                  (unread-and-unselected
6701                   (cond ((and (zerop unread-and-unticked)
6702                               (zerop unselected)) "")
6703                         ((zerop unselected) 
6704                          (format "{%d more}" unread-and-unticked))
6705                         (t (format "{%d(+%d) more}"
6706                                    unread-and-unticked unselected))))
6707                  (subject
6708                   (if gnus-current-headers
6709                       (header-subject gnus-current-headers) ""))
6710                  (max-len (and gnus-mode-non-string-length
6711                                (- (frame-width) gnus-mode-non-string-length)))
6712                  header) ;; passed as argument to any user-format-funcs
6713             (setq mode-string (eval mformat))
6714             (or (numberp max-len)
6715                 (setq max-len (length mode-string)))
6716             (if (< max-len 4) (setq max-len 4))
6717             (if (> (length mode-string) max-len)
6718                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6719                 ;;  function `substring' might cut on a middle
6720                 ;;  of multi-octet character.
6721                 (setq mode-string 
6722                       (concat (gnus-truncate-string mode-string (- max-len 3))
6723                               "...")))
6724             (setq mode-string (format (format "%%-%ds" max-len)
6725                                       mode-string))))
6726         (setq mode-line-buffer-identification mode-string)
6727         (set-buffer-modified-p t))))
6728
6729 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6730   "Go through the HEADERS list and add all Xrefs to a hash table.
6731 The resulting hash table is returned, or nil if no Xrefs were found."
6732   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6733          (prefix (if (and 
6734                       (gnus-group-foreign-p from-newsgroup)
6735                       (not (memq 'virtual 
6736                                  (assoc (symbol-name (car from-method))
6737                                         gnus-valid-select-methods))))
6738                      (gnus-group-real-prefix from-newsgroup)))
6739          (xref-hashtb (make-vector 63 0))
6740          start group entry number xrefs header)
6741     (while headers
6742       (setq header (car headers))
6743       (if (and (setq xrefs (header-xref header))
6744                (not (memq (header-number header) unreads)))
6745           (progn
6746             (setq start 0)
6747             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6748               (setq start (match-end 0))
6749               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6750                                                     (match-end 1))))
6751               (setq number 
6752                     (string-to-int (substring xrefs (match-beginning 2) 
6753                                               (match-end 2))))
6754               (if (setq entry (gnus-gethash group xref-hashtb))
6755                   (setcdr entry (cons number (cdr entry)))
6756                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6757       (setq headers (cdr headers)))
6758     (if start xref-hashtb nil)))
6759
6760 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6761   "Look through all the headers and mark the Xrefs as read."
6762   (let ((virtual (memq 'virtual 
6763                        (assoc (symbol-name (car (gnus-find-method-for-group 
6764                                                  from-newsgroup)))
6765                               gnus-valid-select-methods)))
6766         name entry info xref-hashtb idlist method
6767         nth4)
6768     (save-excursion
6769       (set-buffer gnus-group-buffer)
6770       (if (setq xref-hashtb 
6771                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6772           (mapatoms 
6773            (lambda (group)
6774              (if (string= from-newsgroup (setq name (symbol-name group)))
6775                  ()
6776                (setq idlist (symbol-value group))
6777                ;; Dead groups are not updated.
6778                (if (and (prog1 
6779                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6780                                   info (nth 2 entry))
6781                           (if (stringp (setq nth4 (nth 4 info)))
6782                               (setq nth4 (gnus-server-to-method nth4))))
6783                         ;; Only do the xrefs if the group has the same
6784                         ;; select method as the group we have just read.
6785                         (or (gnus-methods-equal-p 
6786                              nth4 (gnus-find-method-for-group from-newsgroup))
6787                             virtual
6788                             (equal nth4 
6789                                    (setq method (gnus-find-method-for-group 
6790                                                  from-newsgroup)))
6791                             (and (equal (car nth4) (car method))
6792                                  (equal (nth 1 nth4) (nth 1 method))))
6793                         gnus-use-cross-reference
6794                         (or (not (eq gnus-use-cross-reference t))
6795                             virtual
6796                             ;; Only do cross-references on subscribed
6797                             ;; groups, if that is what is wanted.  
6798                             (<= (nth 1 info) gnus-level-subscribed)))
6799                    (gnus-group-make-articles-read name idlist expirable))))
6800            xref-hashtb)))))
6801
6802 (defun gnus-group-make-articles-read (group articles expirable)
6803   (let* ((num 0)
6804          (entry (gnus-gethash group gnus-newsrc-hashtb))
6805          (info (nth 2 entry))
6806          (active (gnus-gethash group gnus-active-hashtb))
6807          exps expirable range)
6808     ;; First peel off all illegal article numbers.
6809     (if active
6810         (let ((ids articles)
6811               (ticked (cdr (assq 'tick (nth 3 info))))
6812               (dormant (cdr (assq 'dormant (nth 3 info))))
6813               id)
6814           (setq exps nil)
6815           (while ids
6816             (setq id (car ids))
6817             (if (or (> id (cdr active))
6818                     (< id (car active))
6819                     (memq id ticked)
6820                     (memq id dormant))
6821                 (setq articles (delq id articles)))
6822             (and (memq id expirable)
6823                  (setq exps (cons id exps)))
6824             (setq ids (cdr ids)))))
6825     ;; Update expirable articles.
6826     (gnus-add-marked-articles nil 'expirable exps info)
6827     (and active
6828          (null (nth 2 info))
6829          (> (car active) 1)
6830          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6831     (setcar (nthcdr 2 info)
6832             (setq range
6833                   (gnus-add-to-range 
6834                    (nth 2 info) 
6835                    (setq articles (sort articles '<)))))
6836     ;; Then we have to re-compute how many unread
6837     ;; articles there are in this group.
6838     (if active
6839         (progn
6840           (cond 
6841            ((not range)
6842             (setq num (- (1+ (cdr active)) (car active))))
6843            ((not (listp (cdr range)))
6844             (setq num (- (cdr active) (- (1+ (cdr range)) 
6845                                          (car range)))))
6846            (t
6847             (while range
6848               (if (numberp (car range))
6849                   (setq num (1+ num))
6850                 (setq num (+ num (- (1+ (cdr (car range)))
6851                                     (car (car range))))))
6852               (setq range (cdr range)))
6853             (setq num (- (cdr active) num))))
6854           ;; Update the number of unread articles.
6855           (setcar 
6856            entry 
6857            (max 0 (- num 
6858                      (length (cdr (assq 'tick (nth 3 info))))
6859                      (length 
6860                       (cdr (assq 'dormant (nth 3 info)))))))
6861           ;; Update the group buffer.
6862           (gnus-group-update-group group t)))))
6863
6864 (defun gnus-methods-equal-p (m1 m2)
6865   (let ((m1 (or m1 gnus-select-method))
6866         (m2 (or m2 gnus-select-method)))
6867     (or (equal m1 m2)
6868         (and (eq (car m1) (car m2))
6869              (or (not (memq 'address (assoc (symbol-name (car m1))
6870                                             gnus-valid-select-methods)))
6871                  (equal (nth 1 m1) (nth 1 m2)))))))
6872
6873 (defsubst gnus-header-value ()
6874   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6875
6876 (defvar gnus-newsgroup-none-id 0)
6877
6878 (defun gnus-get-newsgroup-headers ()
6879   (setq gnus-article-internal-prepare-hook nil)
6880   (let ((cur nntp-server-buffer)
6881         (dependencies gnus-newsgroup-dependencies)
6882         headers id dep end ref)
6883     (save-excursion
6884       (set-buffer nntp-server-buffer)
6885       (goto-char (point-min))
6886       ;; Search to the beginning of the next header. Error messages
6887       ;; do not begin with 2 or 3.
6888       (while (re-search-forward "^[23][0-9]+ " nil t)
6889         (let ((header (make-vector 9 nil))
6890               (case-fold-search t)
6891               (p (point))
6892               in-reply-to)
6893           (setq id nil
6894                 ref nil)
6895           (header-set-number header (read cur))
6896           ;; This implementation of this function, with nine
6897           ;; search-forwards instead of the one re-search-forward and
6898           ;; a case (which basically was the old function) is actually
6899           ;; about twice as fast, even though it looks messier. You
6900           ;; can't have everything, I guess. Speed and elegance
6901           ;; doesn't always come hand in hand.
6902           (save-restriction
6903             (narrow-to-region (point) (or (save-excursion 
6904                                             (search-forward "\n.\n" nil t))
6905                                           (point)))
6906             (if (search-forward "\nfrom: " nil t)
6907                 (header-set-from header (gnus-header-value))
6908               (header-set-from header "(nobody)"))
6909             (goto-char p)
6910             (if (search-forward "\nsubject: " nil t)
6911                 (header-set-subject header (gnus-header-value))
6912               (header-set-subject header "(none)"))
6913             (goto-char p)
6914             (and (search-forward "\nxref: " nil t)
6915                  (header-set-xref header (gnus-header-value)))
6916             (goto-char p)
6917             (or (numberp (and (search-forward "\nlines: " nil t)
6918                               (header-set-lines header (read cur))))
6919                 (header-set-lines header 0))
6920             (goto-char p)
6921             (and (search-forward "\ndate: " nil t)
6922                  (header-set-date header (gnus-header-value)))
6923             (goto-char p)
6924             (if (search-forward "\nmessage-id: " nil t)
6925                 (header-set-id header (setq id (gnus-header-value)))
6926               ;; If there was no message-id, we just fake one to make
6927               ;; subsequent routines simpler.
6928               (header-set-id 
6929                header 
6930                (setq id (concat "none+" 
6931                                 (int-to-string 
6932                                  (setq gnus-newsgroup-none-id 
6933                                        (1+ gnus-newsgroup-none-id)))))))
6934             (goto-char p)
6935             (if (search-forward "\nreferences: " nil t)
6936                 (progn
6937                   (header-set-references header (gnus-header-value))
6938                   (setq end (match-end 0))
6939                   (save-excursion
6940                     (setq ref 
6941                           (downcase
6942                            (buffer-substring
6943                             (progn 
6944                               (end-of-line)
6945                               (search-backward ">" end t)
6946                               (1+ (point)))
6947                             (progn
6948                               (search-backward "<" end t)
6949                               (point)))))))
6950               ;; Get the references from the in-reply-to header if there
6951               ;; ware no references and the in-reply-to header looks
6952               ;; promising. 
6953               (if (and (search-forward "\nin-reply-to: " nil t)
6954                        (setq in-reply-to (gnus-header-value))
6955                        (string-match "<[^>]+>" in-reply-to))
6956                   (progn
6957                     (header-set-references 
6958                      header 
6959                      (setq ref (substring in-reply-to (match-beginning 0)
6960                                           (match-end 0))))
6961                     (setq ref (downcase ref)))
6962                 (setq ref "none")))
6963             ;; We do some threading while we read the headers. The
6964             ;; message-id and the last reference are both entered into
6965             ;; the same hash table. Some tippy-toeing around has to be
6966             ;; done in case an article has arrived before the article
6967             ;; which it refers to.
6968             (if (boundp (setq dep (intern (downcase id) dependencies)))
6969                 (if (car (symbol-value dep))
6970                     ;; An article with this Message-ID has already
6971                     ;; been seen, so we ignore this one, except we add
6972                     ;; any additional Xrefs (in case the two articles
6973                     ;; came from different servers.
6974                     (progn
6975                       (header-set-xref 
6976                        (car (symbol-value dep))
6977                        (concat (or (header-xref (car (symbol-value dep))) "")
6978                                (or (header-xref header) "")))
6979                       (setq header nil))
6980                   (setcar (symbol-value dep) header))
6981               (set dep (list header)))
6982             (if header
6983                 (progn
6984                   (if (boundp (setq dep (intern ref dependencies)))
6985                       (setcdr (symbol-value dep) 
6986                               (cons header (cdr (symbol-value dep))))
6987                     (set dep (list nil header)))
6988                   (setq headers (cons header headers))))
6989             (goto-char (point-max))))))
6990     (nreverse headers)))
6991
6992 ;; The following macros and functions were written by Felix Lee
6993 ;; <flee@cse.psu.edu>. 
6994
6995 (defmacro gnus-nov-read-integer ()
6996   '(prog1
6997        (if (= (following-char) ?\t)
6998            0
6999          (let ((num (condition-case nil (read buffer) (error nil))))
7000            (if (numberp num) num 0)))
7001      (or (eobp) (forward-char 1))))
7002
7003 (defmacro gnus-nov-skip-field ()
7004   '(search-forward "\t" eol 'move))
7005
7006 (defmacro gnus-nov-field ()
7007   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7008
7009 ;; Goes through the xover lines and returns a list of vectors
7010 (defun gnus-get-newsgroup-headers-xover (sequence)
7011   "Parse the news overview data in the server buffer, and return a
7012 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7013   ;; Get the Xref when the users reads the articles since most/some
7014   ;; NNTP servers do not include Xrefs when using XOVER.
7015   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7016   (let ((cur nntp-server-buffer)
7017         (dependencies gnus-newsgroup-dependencies)
7018         number headers header)
7019     (save-excursion
7020       (set-buffer nntp-server-buffer)
7021       (goto-char (point-min))
7022       (while (and sequence (not (eobp)))
7023         (setq number (read cur))
7024         (while (and sequence (< (car sequence) number))
7025           (setq sequence (cdr sequence)))
7026         (and sequence 
7027              (eq number (car sequence))
7028              (progn
7029                (setq sequence (cdr sequence))
7030                (if (setq header 
7031                          (inline (gnus-nov-parse-line number dependencies)))
7032                    (setq headers (cons header headers)))))
7033         (forward-line 1))
7034       (setq headers (nreverse headers)))
7035     headers))
7036
7037 ;; This function has to be called with point after the article number
7038 ;; on the beginning of the line.
7039 (defun gnus-nov-parse-line (number dependencies)
7040   (let ((none 0)
7041         (eol (gnus-point-at-eol)) 
7042         (buffer (current-buffer))
7043         header ref id dep)
7044
7045     ;; overview: [num subject from date id refs chars lines misc]
7046     (narrow-to-region (point) eol)
7047     (forward-char)
7048
7049     (condition-case nil
7050         (setq header
7051               (vector 
7052                number                   ; number
7053                (gnus-nov-field)         ; subject
7054                (gnus-nov-field)         ; from
7055                (gnus-nov-field)         ; date
7056                (setq id (or (gnus-nov-field)
7057                             (concat "none+"
7058                                     (int-to-string 
7059                                      (setq none (1+ none)))))) ; id
7060                (progn
7061                  (save-excursion
7062                    (let ((beg (point)))
7063                      (search-forward "\t" eol)
7064                      (if (search-backward ">" beg t)
7065                          (setq ref 
7066                                (downcase 
7067                                 (buffer-substring 
7068                                  (1+ (point))
7069                                  (progn
7070                                    (search-backward "<" beg t)
7071                                    (point)))))
7072                        (setq ref nil))))
7073                  (gnus-nov-field))      ; refs
7074                (gnus-nov-read-integer)  ; chars
7075                (gnus-nov-read-integer)  ; lines
7076                (if (= (following-char) ?\n)
7077                    nil
7078                  (gnus-nov-field))      ; misc
7079                ))
7080       (error (progn 
7081                (ding)
7082                (message "Strange nov line.")
7083                (setq header nil)
7084                (goto-char eol))))
7085
7086     (widen)
7087
7088     ;; We build the thread tree.
7089     (and header
7090          (if (boundp (setq dep (intern (downcase id) dependencies)))
7091              (if (car (symbol-value dep))
7092                  ;; An article with this Message-ID has already been seen,
7093                  ;; so we ignore this one, except we add any additional
7094                  ;; Xrefs (in case the two articles came from different
7095                  ;; servers.
7096                  (progn
7097                    (header-set-xref 
7098                     (car (symbol-value dep))
7099                     (concat (or (header-xref (car (symbol-value dep))) "")
7100                             (or (header-xref header) "")))
7101                    (setq header nil))
7102                (setcar (symbol-value dep) header))
7103            (set dep (list header))))
7104     (if header
7105         (progn
7106           (if (boundp (setq dep (intern (or ref "none") 
7107                                         dependencies)))
7108               (setcdr (symbol-value dep) 
7109                       (cons header (cdr (symbol-value dep))))
7110             (set dep (list nil header)))))
7111     header))
7112
7113 (defun gnus-article-get-xrefs ()
7114   "Fill in the Xref value in `gnus-current-headers', if necessary.
7115 This is meant to be called in `gnus-article-internal-prepare-hook'."
7116   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7117                                  gnus-current-headers)))
7118     (or (not gnus-use-cross-reference)
7119         (not headers)
7120         (and (header-xref headers)
7121              (not (string= (header-xref headers) "")))
7122         (let ((case-fold-search t)
7123               xref)
7124           (save-restriction
7125             (gnus-narrow-to-headers)
7126             (goto-char (point-min))
7127             (if (or (and (eq (downcase (following-char)) ?x)
7128                          (looking-at "Xref:"))
7129                     (search-forward "\nXref:" nil t))
7130                 (progn
7131                   (goto-char (1+ (match-end 0)))
7132                   (setq xref (buffer-substring (point) 
7133                                                (progn (end-of-line) (point))))
7134                   (header-set-xref headers xref))))))))
7135
7136 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7137 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7138
7139 (defun gnus-make-headers-hashtable-by-number ()
7140   "Make hashtable for the variable gnus-newsgroup-headers by number."
7141   (save-excursion
7142     (set-buffer gnus-summary-buffer)
7143     (let ((headers gnus-newsgroup-headers)
7144           header)
7145       (setq gnus-newsgroup-headers-hashtb-by-number
7146             (gnus-make-hashtable (length headers)))
7147       (while headers
7148         (setq header (car headers))
7149         (gnus-sethash (int-to-string (header-number header))
7150                       header gnus-newsgroup-headers-hashtb-by-number)
7151         (setq headers (cdr headers))))))
7152
7153 (defun gnus-more-header-backward ()
7154   "Find new header backward."
7155   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7156         (artnum gnus-newsgroup-begin)
7157         (header nil))
7158     (while (and (not header)
7159                 (> artnum first))
7160       (setq artnum (1- artnum))
7161       (setq header (gnus-read-header artnum)))
7162     header))
7163
7164 (defun gnus-more-header-forward (&optional backward)
7165   "Find new header forward.
7166 If BACKWARD, find new header backward instead."
7167   (if backward
7168       (gnus-more-header-backward)
7169     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7170           (artnum gnus-newsgroup-end)
7171           (header nil))
7172       (while (and (not header)
7173                   (< artnum last))
7174         (setq artnum (1+ artnum))
7175         (setq header (gnus-read-header artnum)))
7176       header)))
7177
7178 (defun gnus-extend-newsgroup (header &optional backward)
7179   "Extend newsgroup selection with HEADER.
7180 Optional argument BACKWARD means extend toward backward."
7181   (if header
7182       (let ((artnum (header-number header)))
7183         (setq gnus-newsgroup-headers
7184               (if backward
7185                   (cons header gnus-newsgroup-headers)
7186                 (nconc gnus-newsgroup-headers (list header))))
7187         (setq gnus-newsgroup-unselected
7188               (delq artnum gnus-newsgroup-unselected))
7189         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7190         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7191
7192 (defun gnus-summary-work-articles (n)
7193   "Return a list of articles to be worked upon. The prefix argument,
7194 the list of process marked articles, and the current article will be
7195 taken into consideration."
7196   (let (articles)
7197     (if (and n (numberp n))
7198         (let ((backward (< n 0))
7199               (n (abs n)))
7200           (save-excursion
7201             (while (and (> n 0)
7202                         (setq articles (cons (gnus-summary-article-number) 
7203                                              articles))
7204                         (gnus-summary-search-forward nil nil backward))
7205               (setq n (1- n))))
7206           (sort articles (function <)))
7207       (or (reverse gnus-newsgroup-processable)
7208           (list (gnus-summary-article-number))))))
7209
7210 (defun gnus-summary-search-group (&optional backward use-level)
7211   "Search for next unread newsgroup.
7212 If optional argument BACKWARD is non-nil, search backward instead."
7213   (save-excursion
7214     (set-buffer gnus-group-buffer)
7215     (if (gnus-group-search-forward 
7216          backward nil (if use-level (gnus-group-group-level) nil))
7217         (gnus-group-group-name))))
7218
7219 (defun gnus-summary-best-group (&optional exclude-group)
7220   "Find the name of the best unread group.
7221 If EXCLUDE-GROUP, do not go to this group."
7222   (save-excursion
7223     (set-buffer gnus-group-buffer)
7224     (save-excursion
7225       (gnus-group-best-unread-group exclude-group))))
7226
7227 (defun gnus-subject-equal (s1 s2)
7228   (cond
7229    ((null gnus-summary-gather-subject-limit)
7230     (equal (gnus-simplify-subject-re s1)
7231            (gnus-simplify-subject-re s2)))
7232    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7233     (equal (gnus-simplify-subject-fuzzy s1)
7234            (gnus-simplify-subject-fuzzy s2)))
7235    ((numberp gnus-summary-gather-subject-limit)
7236     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7237            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7238    (t
7239     (equal s1 s2))))
7240     
7241 (defun gnus-summary-search-subject (&optional backward unread subject)
7242   "Search for article forward.
7243 If BACKWARD is non-nil, search backward.
7244 If UNREAD is non-nil, only unread articles are selected.
7245 If SUBJECT is non-nil, the article which has the same subject will be
7246 searched for." 
7247   (let ((func (if backward 'previous-single-property-change
7248                 'next-single-property-change))
7249         (beg (point))
7250         (did t)
7251         pos psubject)
7252     (beginning-of-line)
7253     (and gnus-summary-check-current unread
7254          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7255          (setq did nil))
7256     (if (not did)
7257         ()
7258       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7259       (while
7260           (and 
7261            (setq pos (funcall func (point) 'gnus-number))
7262            (goto-char (if backward (1- pos) pos))
7263            (setq did
7264                  (not (and
7265                        (or (not unread)
7266                            (eq (get-text-property (point) 'gnus-mark)
7267                                gnus-unread-mark))
7268                        (or (not subject)
7269                            (and (setq psubject (inline (gnus-summary-subject-string)))
7270                                 (inline (gnus-subject-equal subject psubject)))))))
7271            (if backward (if (bobp) nil (forward-char -1) t)
7272              (if (eobp) nil (forward-char 1) t)))))
7273     (if did
7274         (progn (goto-char beg) nil)
7275       (prog1
7276           (get-text-property (point) 'gnus-number)
7277         (gnus-summary-position-cursor)))))
7278
7279 (defun gnus-summary-pseudo-article ()
7280   "The thread level of the article on the current line."
7281   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7282
7283 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7284 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7285 (defun gnus-summary-article-score ()
7286   "Return current article score."
7287   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7288       gnus-summary-default-score 0))
7289
7290 (defun gnus-summary-recenter ()
7291   "Center point in the summary window.
7292 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7293 displayed, no centering will be performed." 
7294   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7295   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7296   (let* ((top (cond ((< (window-height) 4) 0)
7297                     ((< (window-height) 7) 1)
7298                     (t 2)))
7299          (height (1- (window-height)))
7300          (bottom (save-excursion (goto-char (point-max))
7301                                  (forward-line (- height))
7302                                  (point)))
7303          (window (get-buffer-window (current-buffer))))
7304     (and 
7305      ;; The user has to want it,
7306      gnus-auto-center-summary 
7307      ;; the article buffer must be displayed,
7308      (get-buffer-window gnus-article-buffer)
7309      ;; Set the window start to either `bottom', which is the biggest
7310      ;; possible valid number, or the second line from the top,
7311      ;; whichever is the least.
7312      (set-window-start
7313       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7314
7315 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7316 (defun gnus-short-group-name (group &optional levels)
7317   "Collapse GROUP name LEVELS."
7318   (let* ((name "") (foreign "") (depth -1) (skip 1)
7319          (levels (or levels
7320                      (progn
7321                        (while (string-match "\\." group skip)
7322                          (setq skip (match-end 0)
7323                                depth (+ depth 1)))
7324                        depth))))
7325     (if (string-match ":" group)
7326         (setq foreign (substring group 0 (match-end 0))
7327               group (substring group (match-end 0))))
7328     (while group
7329       (if (and (string-match "\\." group) (> levels 0))
7330           (setq name (concat name (substring group 0 1))
7331                 group (substring group (match-end 0))
7332                 levels (- levels 1)
7333                 name (concat name "."))
7334         (setq name (concat foreign name group)
7335               group nil)))
7336     name))
7337
7338 (defun gnus-summary-jump-to-group (newsgroup)
7339   "Move point to NEWSGROUP in group mode buffer."
7340   ;; Keep update point of group mode buffer if visible.
7341   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7342       (save-window-excursion
7343         ;; Take care of tree window mode.
7344         (if (get-buffer-window gnus-group-buffer)
7345             (pop-to-buffer gnus-group-buffer))
7346         (gnus-group-jump-to-group newsgroup))
7347     (save-excursion
7348       ;; Take care of tree window mode.
7349       (if (get-buffer-window gnus-group-buffer)
7350           (pop-to-buffer gnus-group-buffer)
7351         (set-buffer gnus-group-buffer))
7352       (gnus-group-jump-to-group newsgroup))))
7353
7354 ;; This function returns a list of article numbers based on the
7355 ;; difference between the ranges of read articles in this group and
7356 ;; the range of active articles.
7357 (defun gnus-list-of-unread-articles (group)
7358   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7359          (active (gnus-gethash group gnus-active-hashtb))
7360          (last (cdr active))
7361          first nlast unread)
7362     ;; If none are read, then all are unread. 
7363     (if (not read)
7364         (setq first (car active))
7365       ;; If the range of read articles is a single range, then the
7366       ;; first unread article is the article after the last read
7367       ;; article. Sounds logical, doesn't it?
7368       (if (not (listp (cdr read)))
7369           (setq first (1+ (cdr read)))
7370         ;; `read' is a list of ranges.
7371         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7372                                 (car (car read)))) 1)
7373             (setq first 1))
7374         (while read
7375           (if first 
7376               (while (< first nlast)
7377                 (setq unread (cons first unread))
7378                 (setq first (1+ first))))
7379           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7380           (setq nlast (if (atom (car (cdr read))) 
7381                           (car (cdr read))
7382                         (car (car (cdr read)))))
7383           (setq read (cdr read)))))
7384     ;; And add the last unread articles.
7385     (while (<= first last)
7386       (setq unread (cons first unread))
7387       (setq first (1+ first)))
7388     ;; Return the list of unread articles.
7389     (nreverse unread)))
7390
7391 (defun gnus-list-of-read-articles (group)
7392   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7393         (active (gnus-gethash group gnus-active-hashtb)))
7394     (and info active
7395          (gnus-sorted-complement 
7396           (gnus-uncompress-range active) 
7397           (gnus-list-of-unread-articles group)))))
7398
7399 ;; Various summary commands
7400
7401 (defun gnus-summary-universal-argument ()
7402   "Perform any operation on all articles marked with the process mark."
7403   (interactive)
7404   (gnus-set-global-variables)
7405   (let ((articles (reverse gnus-newsgroup-processable))
7406         func)
7407     (or articles (error "No articles marked"))
7408     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7409         (error "Undefined key"))
7410     (while articles
7411       (gnus-summary-goto-subject (car articles))
7412       (command-execute func)
7413       (gnus-summary-remove-process-mark (car articles))
7414       (setq articles (cdr articles)))))
7415
7416 (defun gnus-summary-toggle-truncation (&optional arg)
7417   "Toggle truncation of summary lines.
7418 With arg, turn line truncation on iff arg is positive."
7419   (interactive "P")
7420   (setq truncate-lines
7421         (if (null arg) (not truncate-lines)
7422           (> (prefix-numeric-value arg) 0)))
7423   (redraw-display))
7424
7425 (defun gnus-summary-reselect-current-group (&optional all)
7426   "Once exit and then reselect the current newsgroup.
7427 The prefix argument ALL means to select all articles."
7428   (interactive "P")
7429   (gnus-set-global-variables)
7430   (let ((current-subject (gnus-summary-article-number))
7431         (group gnus-newsgroup-name))
7432     (setq gnus-newsgroup-begin nil)
7433     (gnus-summary-exit t)
7434     ;; We have to adjust the point of group mode buffer because the
7435     ;; current point was moved to the next unread newsgroup by
7436     ;; exiting.
7437     (gnus-summary-jump-to-group group)
7438     (gnus-group-read-group all t)
7439     (gnus-summary-goto-subject current-subject)))
7440
7441 (defun gnus-summary-rescan-group (&optional all)
7442   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7443   (interactive "P")
7444   (gnus-set-global-variables)
7445   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7446   (let ((group gnus-newsgroup-name))
7447     (gnus-summary-exit)
7448     (gnus-summary-jump-to-group group)
7449     (save-excursion
7450       (set-buffer gnus-group-buffer)
7451       (gnus-group-get-new-news-this-group 1))
7452     (gnus-summary-jump-to-group group)
7453     (gnus-group-read-group all)))
7454
7455 (defun gnus-summary-update-info ()
7456   (let* ((group gnus-newsgroup-name))
7457     (if gnus-newsgroup-kill-headers
7458         (setq gnus-newsgroup-killed
7459               (gnus-compress-sequence
7460                (nconc
7461                 (gnus-set-sorted-intersection
7462                  (gnus-uncompress-range gnus-newsgroup-killed)
7463                  (setq gnus-newsgroup-unselected
7464                        (sort gnus-newsgroup-unselected '<)))
7465                 (setq gnus-newsgroup-unreads
7466                       (sort gnus-newsgroup-unreads '<))) t)))
7467     (or (listp (cdr gnus-newsgroup-killed))
7468         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7469     (let ((headers gnus-newsgroup-headers))
7470       (gnus-close-group group)
7471       (run-hooks 'gnus-exit-group-hook)
7472       (gnus-update-read-articles 
7473        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7474        gnus-newsgroup-marked
7475        t gnus-newsgroup-replied gnus-newsgroup-expirable
7476        gnus-newsgroup-killed gnus-newsgroup-dormant
7477        gnus-newsgroup-bookmarks 
7478        (and gnus-save-score gnus-newsgroup-scored))
7479       (and gnus-use-cross-reference
7480            (gnus-mark-xrefs-as-read 
7481             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7482       ;; Do adaptive scoring, and possibly save score files.
7483       (and gnus-newsgroup-adaptive
7484            (gnus-score-adaptive))
7485       (and gnus-use-scoring 
7486            (fboundp 'gnus-score-save)
7487            (funcall 'gnus-score-save))
7488       ;; Do not switch windows but change the buffer to work.
7489       (set-buffer gnus-group-buffer)
7490       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7491           (gnus-group-update-group group)))))
7492   
7493 (defun gnus-summary-exit (&optional temporary)
7494   "Exit reading current newsgroup, and then return to group selection mode.
7495 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7496   (interactive)
7497   (gnus-set-global-variables)
7498   (gnus-kill-save-kill-buffer)
7499   (let* ((group gnus-newsgroup-name)
7500          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7501                                                   gnus-newsgroup-name))))
7502          (mode major-mode)
7503          (buf (current-buffer)))
7504     (run-hooks 'gnus-summary-prepare-exit-hook)
7505     (gnus-summary-update-info) ; Make all changes in this group permanent.
7506     (set-buffer buf)
7507     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7508     ;; Make sure where I was, and go to next newsgroup.
7509     (set-buffer gnus-group-buffer)
7510     (or quit-config
7511         (progn
7512           (gnus-group-jump-to-group group)
7513           (gnus-group-next-unread-group 1)))
7514     (if temporary
7515         nil                             ;Nothing to do.
7516       ;; We set all buffer-local variables to nil. It is unclear why
7517       ;; this is needed, but if we don't, buffer-local variables are
7518       ;; not garbage-collected, it seems. This would the lead to en
7519       ;; ever-growing Emacs.
7520       (set-buffer buf)
7521       (gnus-summary-clear-local-variables)
7522       ;; We clear the global counterparts of the buffer-local
7523       ;; variables as well, just to be on the safe side.
7524       (gnus-configure-windows 'group 'force)
7525       (gnus-summary-clear-local-variables)
7526       ;; Return to group mode buffer. 
7527       (if (eq mode 'gnus-summary-mode)
7528           (gnus-kill-buffer buf))
7529       (if (get-buffer gnus-article-buffer)
7530           (bury-buffer gnus-article-buffer))
7531       (setq gnus-current-select-method gnus-select-method)
7532       (pop-to-buffer gnus-group-buffer)
7533       (if (not quit-config)
7534           (progn
7535             (gnus-group-jump-to-group group)
7536             (gnus-group-next-unread-group 1))
7537         (if (not (buffer-name (car quit-config)))
7538             (gnus-configure-windows 'group 'force)
7539           (set-buffer (car quit-config))
7540           (and (eq major-mode 'gnus-summary-mode)
7541                (gnus-set-global-variables))
7542           (gnus-configure-windows (cdr quit-config))))
7543       (run-hooks 'gnus-summary-exit-hook))))
7544
7545 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7546 (defun gnus-summary-exit-no-update (&optional no-questions)
7547   "Quit reading current newsgroup without updating read article info."
7548   (interactive)
7549   (gnus-set-global-variables)
7550   (let* ((group gnus-newsgroup-name)
7551          (quit-config (nth 1 (assoc 'quit-config 
7552                                     (gnus-find-method-for-group group)))))
7553     (if (or no-questions
7554             gnus-expert-user
7555             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7556         (progn
7557           (gnus-close-group group)
7558           (gnus-summary-clear-local-variables)
7559           (set-buffer gnus-group-buffer)
7560           (gnus-summary-clear-local-variables)
7561           ;; Return to group selection mode.
7562           (gnus-configure-windows 'group 'force)
7563           (if (get-buffer gnus-summary-buffer)
7564               (kill-buffer gnus-summary-buffer))
7565           (if (get-buffer gnus-article-buffer)
7566               (bury-buffer gnus-article-buffer))
7567           (if (equal (gnus-group-group-name) group)
7568               (gnus-group-next-unread-group 1))
7569           (if quit-config
7570               (progn
7571                 (if (not (buffer-name (car quit-config)))
7572                     (gnus-configure-windows 'group 'force)
7573                   (set-buffer (car quit-config))
7574                   (and (eq major-mode 'gnus-summary-mode)
7575                        (gnus-set-global-variables))
7576                   (gnus-configure-windows (cdr quit-config)))))))))
7577
7578 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7579 (defun gnus-summary-fetch-faq (group)
7580   "Fetch the FAQ for the current group."
7581   (interactive (list gnus-newsgroup-name))
7582   (let ((gnus-faq-buffer 
7583          (find-file (concat gnus-group-faq-directory 
7584                             (gnus-group-real-name group)))))
7585   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7586
7587 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7588 (defun gnus-summary-describe-group (&optional force)
7589   "Describe the current newsgroup."
7590   (interactive "P")
7591   (gnus-group-describe-group force gnus-newsgroup-name))
7592
7593 (defun gnus-summary-describe-briefly ()
7594   "Describe summary mode commands briefly."
7595   (interactive)
7596   (gnus-message 6
7597     (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")))
7598
7599 ;; Walking around group mode buffer from summary mode.
7600
7601 (defun gnus-summary-next-group (&optional no-article target-group backward)
7602   "Exit current newsgroup and then select next unread newsgroup.
7603 If prefix argument NO-ARTICLE is non-nil, no article is selected
7604 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7605 previous group instead."
7606   (interactive "P")
7607   (gnus-set-global-variables)
7608   (let ((current-group gnus-newsgroup-name)
7609         (current-buffer (current-buffer))
7610         entered)
7611     ;; First we semi-exit this group to update Xrefs and all variables.
7612     ;; We can't do a real exit, because the window conf must remain
7613     ;; the same in case the user is prompted for info, and we don't
7614     ;; want the window conf to change before that...
7615     (gnus-summary-exit t)
7616     (while (not entered)
7617       ;; Then we find what group we are supposed to enter.
7618       (set-buffer gnus-group-buffer)
7619       (gnus-group-jump-to-group current-group)
7620       (setq target-group 
7621             (or target-group        
7622                 (if (eq gnus-keep-same-level 'best) 
7623                     (gnus-summary-best-group gnus-newsgroup-name)
7624                   (gnus-summary-search-group backward gnus-keep-same-level))))
7625       (if (not target-group)
7626           ;; There are no further groups, so we return to the group
7627           ;; buffer.
7628           (progn
7629             (gnus-message 5 "Returning to the group buffer")
7630             (setq entered t)
7631             (set-buffer current-buffer)
7632             (gnus-summary-exit))
7633         ;; We try to enter the target group.
7634         (gnus-group-jump-to-group target-group)
7635         (let ((unreads (gnus-group-group-unread)))
7636           (if (and (or (eq t unreads)
7637                        (and unreads (not (zerop unreads))))
7638                    (gnus-summary-read-group
7639                     target-group nil no-article current-buffer))
7640               (setq entered t)
7641             (setq current-group target-group
7642                   target-group nil)))))))
7643
7644 (defun gnus-summary-next-group-old (&optional no-article group backward)
7645   "Exit current newsgroup and then select next unread newsgroup.
7646 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7647 If BACKWARD, go to previous group instead."
7648   (interactive "P")
7649   (gnus-set-global-variables)
7650   (let ((ingroup gnus-newsgroup-name)
7651         (sumbuf (current-buffer))
7652         num)
7653     (set-buffer gnus-group-buffer)
7654     (if (and group
7655              (or (and (numberp (setq num (car (gnus-gethash
7656                                                group gnus-newsrc-hashtb))))
7657                       (< num 1))
7658                  (null num)))
7659         (progn
7660           (gnus-group-jump-to-group group)
7661           (setq group nil))
7662       (gnus-group-jump-to-group ingroup))
7663     (gnus-summary-search-group backward)
7664     (let ((group (or group (gnus-summary-search-group backward))))
7665       (set-buffer sumbuf)
7666       (gnus-summary-exit t)             ;Update all information.
7667       (if (null group)
7668           (gnus-summary-exit-no-update t)
7669         (gnus-group-jump-to-group ingroup)
7670         (setq group (gnus-summary-search-group backward))
7671         (gnus-message 5 "Selecting %s..." group)
7672         (set-buffer gnus-group-buffer)
7673         ;; We are now in group mode buffer.
7674         ;; Make sure group mode buffer point is on GROUP.
7675         (gnus-group-jump-to-group group)
7676         (if (not (eq gnus-auto-select-next 'quietly))
7677             (progn
7678               (gnus-summary-read-group group nil no-article sumbuf)
7679               (and (string= gnus-newsgroup-name ingroup)
7680                    (bufferp sumbuf) (buffer-name sumbuf)
7681                    (progn
7682                      (set-buffer (setq gnus-summary-buffer sumbuf))
7683                      (gnus-summary-exit-no-update t))))
7684           (let ((prevgroup group))
7685             (gnus-group-jump-to-group ingroup)
7686             (setq group (gnus-summary-search-group backward))
7687             (gnus-summary-read-group group nil no-article sumbuf)
7688             (while (and (string= gnus-newsgroup-name ingroup)
7689                         (bufferp sumbuf) 
7690                         (buffer-name sumbuf)
7691                         (not (string= prevgroup (gnus-group-group-name))))
7692               (set-buffer gnus-group-buffer)
7693               (gnus-summary-read-group 
7694                (setq prevgroup (gnus-group-group-name)) 
7695                nil no-article sumbuf))
7696             (and (string= prevgroup (gnus-group-group-name))
7697                  ;; We have reached the final group in the group
7698                  ;; buffer.
7699                  (progn
7700                    (if (buffer-name sumbuf)
7701                        (progn
7702                          (set-buffer sumbuf)
7703                          (gnus-summary-exit)))))))))))
7704
7705 (defun gnus-summary-prev-group (&optional no-article)
7706   "Exit current newsgroup and then select previous unread newsgroup.
7707 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7708   (interactive "P")
7709   (gnus-summary-next-group no-article nil t))
7710
7711 ;; Walking around summary lines.
7712
7713 (defun gnus-summary-first-subject (&optional unread)
7714   "Go to the first unread subject.
7715 If UNREAD is non-nil, go to the first unread article.
7716 Returns nil if there are no unread articles."
7717   (interactive "P")
7718   (prog1
7719       (cond ((not unread)
7720              (goto-char (point-min)))
7721             ((gnus-goto-char 
7722               (text-property-any 
7723                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7724              t)
7725             (t 
7726              ;; There are no unread articles.
7727              (gnus-message 3 "No more unread articles")
7728              nil))
7729     (gnus-summary-position-cursor)))
7730
7731 (defun gnus-summary-next-subject (n &optional unread dont-display)
7732   "Go to next N'th summary line.
7733 If N is negative, go to the previous N'th subject line.
7734 If UNREAD is non-nil, only unread articles are selected.
7735 The difference between N and the actual number of steps taken is
7736 returned."
7737   (interactive "p")
7738   (let ((backward (< n 0))
7739         (n (abs n)))
7740     (while (and (> n 0)
7741                 (gnus-summary-search-forward unread nil backward))
7742       (setq n (1- n)))
7743     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7744                                (if unread " unread" "")))
7745     (or dont-display
7746         (progn
7747           (gnus-summary-recenter)
7748           (gnus-summary-position-cursor)))
7749   n))
7750
7751 (defun gnus-summary-next-unread-subject (n)
7752   "Go to next N'th unread summary line."
7753   (interactive "p")
7754   (gnus-summary-next-subject n t))
7755
7756 (defun gnus-summary-prev-subject (n &optional unread)
7757   "Go to previous N'th summary line.
7758 If optional argument UNREAD is non-nil, only unread article is selected."
7759   (interactive "p")
7760   (gnus-summary-next-subject (- n) unread))
7761
7762 (defun gnus-summary-prev-unread-subject (n)
7763   "Go to previous N'th unread summary line."
7764   (interactive "p")
7765   (gnus-summary-next-subject (- n) t))
7766
7767 (defun gnus-summary-goto-subject (article)
7768   "Go the subject line of ARTICLE."
7769   (interactive
7770    (list
7771     (string-to-int
7772      (completing-read "Article number: "
7773                       (mapcar
7774                        (lambda (headers)
7775                          (list
7776                           (int-to-string (header-number headers))))
7777                        gnus-newsgroup-headers)
7778                       nil 'require-match))))
7779   (or article (error "No article number"))
7780   (let ((b (point)))
7781     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7782                                                 'gnus-number article)))
7783         ()
7784       (gnus-summary-show-thread)
7785       ;; Skip dummy articles. 
7786       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7787           (forward-line 1))
7788       (prog1
7789           (if (not (eobp))
7790               article
7791             (goto-char b)
7792             nil)
7793         (gnus-summary-position-cursor)))))
7794
7795 ;; Walking around summary lines with displaying articles.
7796
7797 (defun gnus-summary-expand-window (&optional arg)
7798   "Make the summary buffer take up the entire Emacs frame.
7799 Given a prefix, will force an `article' buffer configuration."
7800   (interactive "P")
7801   (gnus-set-global-variables)
7802   (if arg
7803       (gnus-configure-windows 'article 'force)
7804     (gnus-configure-windows 'summary 'force)))
7805
7806 (defun gnus-summary-display-article (article &optional all-header)
7807   "Display ARTICLE in article buffer."
7808   (gnus-set-global-variables)
7809   (if (null article)
7810       nil
7811     (prog1
7812         (gnus-article-prepare article all-header)
7813       (gnus-summary-show-thread)
7814       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7815           (progn
7816             (forward-line 1)
7817             (gnus-summary-position-cursor)))
7818       (run-hooks 'gnus-select-article-hook)
7819       (gnus-summary-recenter)
7820       (gnus-summary-goto-subject article)
7821       ;; Successfully display article.
7822       (gnus-summary-update-line)
7823       (gnus-article-set-window-start 
7824        (cdr (assq article gnus-newsgroup-bookmarks)))
7825       t)))
7826
7827 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7828   "Select the current article.
7829 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7830 non-nil, the article will be re-fetched even if it already present in
7831 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7832 be displayed."
7833   (and (not pseudo) (gnus-summary-pseudo-article)
7834        (error "This is a pseudo-article."))
7835   (let ((article (or article (gnus-summary-article-number)))
7836         (all-headers (not (not all-headers))) ;Must be T or NIL.
7837         did) 
7838     (prog1
7839         (save-excursion
7840           (set-buffer gnus-summary-buffer)
7841           (if (or (null gnus-current-article)
7842                   (null gnus-article-current)
7843                   (null (get-buffer gnus-article-buffer))
7844                   (not (eq article (cdr gnus-article-current)))
7845                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7846                   force)
7847               ;; The requested article is different from the current article.
7848               (progn
7849                 (gnus-summary-display-article article all-headers)
7850                 (setq did article))
7851             (if (or all-headers gnus-show-all-headers) 
7852                 (gnus-article-show-all-headers))
7853             nil))
7854       (if did 
7855           (gnus-article-set-window-start 
7856            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7857
7858 (defun gnus-summary-set-current-mark (&optional current-mark)
7859   "Obsolete function."
7860   nil)
7861
7862 (defun gnus-summary-next-article (&optional unread subject backward)
7863   "Select the next article.
7864 If UNREAD, only unread articles are selected.
7865 If SUBJECT, only articles with SUBJECT are selected.
7866 If BACKWARD, the previous article is selected instead of the next."
7867   (interactive "P")
7868   (gnus-set-global-variables)
7869   (let (header)
7870     (cond
7871      ;; Is there such an article?
7872      ((or (gnus-summary-display-article 
7873            (gnus-summary-search-forward unread subject backward))
7874           (eq (gnus-summary-article-mark) gnus-canceled-mark))
7875       (gnus-summary-position-cursor))
7876      ;; If not, we try the first unread, if that is wanted.
7877      ((and subject
7878            gnus-auto-select-same
7879            (gnus-summary-first-unread-article))
7880       (gnus-message 6 "Wrapped"))
7881      ;; Try to get next/previous article not displayed in this group.
7882      ((and gnus-auto-extend-newsgroup
7883            (not unread) (not subject)
7884            (setq header (gnus-more-header-forward backward)))
7885       (gnus-extend-newsgroup header backward)
7886       (let ((buffer-read-only nil))
7887         (goto-char (if backward (point-min) (point-max)))
7888         (gnus-summary-prepare-threads (list header)))
7889       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7890                                    gnus-newsgroup-end)))
7891      ;; Go to next/previous group.
7892      (t
7893       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7894           (gnus-summary-jump-to-group gnus-newsgroup-name))
7895       (let ((cmd (aref (this-command-keys) 0))
7896             (group 
7897              (if (eq gnus-keep-same-level 'best) 
7898                  (gnus-summary-best-group gnus-newsgroup-name)
7899                (gnus-summary-search-group backward gnus-keep-same-level))))
7900         ;; For some reason, the group window gets selected. We change
7901         ;; it back.  
7902         (select-window (get-buffer-window (current-buffer)))
7903         ;; Keep just the event type of CMD.
7904         (and (listp cmd) (setq cmd (car cmd)))
7905         ;; Select next unread newsgroup automagically.
7906         (cond 
7907          ((not gnus-auto-select-next)
7908           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7909          ((eq gnus-auto-select-next 'quietly)
7910           ;; Select quietly.
7911           (if (assoc 'quit-config (gnus-find-method-for-group 
7912                                    gnus-newsgroup-name))
7913               (gnus-summary-exit)
7914             (gnus-message 7 "No more%s articles (%s)..."
7915                           (if unread " unread" "") 
7916                           (if group (concat "selecting " group)
7917                             "exiting"))
7918             (gnus-summary-next-group nil group backward)))
7919          (t
7920           (let ((keystrokes '(?\C-n ?\C-p))
7921                 key)
7922             (while (or (null key) (memq key keystrokes))
7923               (gnus-message 
7924                7 "No more%s articles%s" (if unread " unread" "")
7925                (if (and group (not (assoc 'quit-config
7926                                           (gnus-find-method-for-group 
7927                                            gnus-newsgroup-name))))
7928                    (format " (Type %s for %s [%s])"
7929                            (single-key-description cmd) group
7930                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7931                  (format " (Type %s to exit %s)"
7932                          (single-key-description cmd)
7933                          gnus-newsgroup-name)))
7934               ;; Confirm auto selection.
7935               (let* ((event (read-event)))
7936                 (setq key (if (listp event) (car event) event))
7937                 (if (memq key keystrokes)
7938                     (let ((obuf (current-buffer)))
7939                       (switch-to-buffer gnus-group-buffer)
7940                       (gnus-group-jump-to-group group)
7941                       (execute-kbd-macro (char-to-string key))
7942                       (setq group (gnus-group-group-name))
7943                       (switch-to-buffer obuf)))))
7944             (if (equal key cmd)
7945                 (if (or (not group) (assoc 'quit-config
7946                                            (gnus-find-method-for-group
7947                                             gnus-newsgroup-name)))
7948                     (gnus-summary-exit)
7949                   (gnus-summary-next-group nil group backward))
7950               (setq unread-command-events (list key)))))))))))
7951
7952 (defun gnus-summary-next-unread-article ()
7953   "Select unread article after current one."
7954   (interactive)
7955   (gnus-summary-next-article t (and gnus-auto-select-same
7956                                     (gnus-summary-subject-string))))
7957
7958 (defun gnus-summary-prev-article (&optional unread subject)
7959   "Select the article after the current one.
7960 If UNREAD is non-nil, only unread articles are selected."
7961   (interactive "P")
7962   (gnus-summary-next-article unread subject t))
7963
7964 (defun gnus-summary-prev-unread-article ()
7965   "Select unred article before current one."
7966   (interactive)
7967   (gnus-summary-prev-article t (and gnus-auto-select-same
7968                                     (gnus-summary-subject-string))))
7969
7970 (defun gnus-summary-next-page (&optional lines circular)
7971   "Show next page of selected article.
7972 If end of article, select next article.
7973 Argument LINES specifies lines to be scrolled up.
7974 If CIRCULAR is non-nil, go to the start of the article instead of 
7975 instead of selecting the next article when reaching the end of the
7976 current article." 
7977   (interactive "P")
7978   (setq gnus-summary-buffer (current-buffer))
7979   (gnus-set-global-variables)
7980   (let ((article (gnus-summary-article-number))
7981         (endp nil))
7982     (gnus-configure-windows 'article)
7983     (if (or (null gnus-current-article)
7984             (null gnus-article-current)
7985             (/= article (cdr gnus-article-current))
7986             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7987         ;; Selected subject is different from current article's.
7988         (gnus-summary-display-article article)
7989       (gnus-eval-in-buffer-window
7990        gnus-article-buffer
7991        (setq endp (gnus-article-next-page lines)))
7992       (if endp
7993           (cond (circular
7994                  (gnus-summary-beginning-of-article))
7995                 (lines
7996                  (gnus-message 3 "End of message"))
7997                 ((null lines)
7998                  (gnus-summary-next-unread-article)))))
7999     (gnus-summary-recenter)
8000     (gnus-summary-position-cursor)))
8001
8002 (defun gnus-summary-prev-page (&optional lines)
8003   "Show previous page of selected article.
8004 Argument LINES specifies lines to be scrolled down."
8005   (interactive "P")
8006   (gnus-set-global-variables)
8007   (let ((article (gnus-summary-article-number)))
8008     (gnus-configure-windows 'article)
8009     (if (or (null gnus-current-article)
8010             (null gnus-article-current)
8011             (/= article (cdr gnus-article-current))
8012             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8013         ;; Selected subject is different from current article's.
8014         (gnus-summary-display-article article)
8015       (gnus-summary-recenter)
8016       (gnus-eval-in-buffer-window gnus-article-buffer
8017         (gnus-article-prev-page lines))))
8018   (gnus-summary-position-cursor))
8019
8020 (defun gnus-summary-scroll-up (lines)
8021   "Scroll up (or down) one line current article.
8022 Argument LINES specifies lines to be scrolled up (or down if negative)."
8023   (interactive "p")
8024   (gnus-set-global-variables)
8025   (gnus-configure-windows 'article)
8026   (or (gnus-summary-select-article nil nil 'pseudo)
8027       (gnus-eval-in-buffer-window 
8028        gnus-article-buffer
8029        (cond ((> lines 0)
8030               (if (gnus-article-next-page lines)
8031                   (gnus-message 3 "End of message")))
8032              ((< lines 0)
8033               (gnus-article-prev-page (- lines))))))
8034   (gnus-summary-recenter)
8035   (gnus-summary-position-cursor))
8036
8037 (defun gnus-summary-next-same-subject ()
8038   "Select next article which has the same subject as current one."
8039   (interactive)
8040   (gnus-set-global-variables)
8041   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8042
8043 (defun gnus-summary-prev-same-subject ()
8044   "Select previous article which has the same subject as current one."
8045   (interactive)
8046   (gnus-set-global-variables)
8047   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8048
8049 (defun gnus-summary-next-unread-same-subject ()
8050   "Select next unread article which has the same subject as current one."
8051   (interactive)
8052   (gnus-set-global-variables)
8053   (gnus-summary-next-article t (gnus-summary-subject-string)))
8054
8055 (defun gnus-summary-prev-unread-same-subject ()
8056   "Select previous unread article which has the same subject as current one."
8057   (interactive)
8058   (gnus-set-global-variables)
8059   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8060
8061 (defun gnus-summary-first-unread-article ()
8062   "Select the first unread article. 
8063 Return nil if there are no unread articles."
8064   (interactive)
8065   (gnus-set-global-variables)
8066   (prog1
8067       (if (gnus-summary-first-subject t)
8068           (gnus-summary-display-article (gnus-summary-article-number)))
8069     (gnus-summary-position-cursor)))
8070
8071 (defun gnus-summary-best-unread-article ()
8072   "Select the unread article with the highest score."
8073   (interactive)
8074   (gnus-set-global-variables)
8075   (let ((scored gnus-newsgroup-scored)
8076         (best -1000000)
8077         article art)
8078     (while scored
8079       (or (> best (cdr (car scored)))
8080           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8081                (not (memq art gnus-newsgroup-marked))
8082                (not (memq art gnus-newsgroup-dormant))
8083                (if (= best (cdr (car scored)))
8084                    (setq article (min art article))
8085                  (setq article art)
8086                  (setq best (cdr (car scored))))))
8087       (setq scored (cdr scored)))
8088     (if article 
8089         (gnus-summary-goto-article article)
8090       (gnus-summary-first-unread-article))
8091     (gnus-summary-position-cursor)))
8092
8093 (defun gnus-summary-goto-article (article &optional all-headers)
8094   "Fetch ARTICLE and display it if it exists.
8095 If ALL-HEADERS is non-nil, no header lines are hidden."
8096   (interactive
8097    (list
8098     (string-to-int
8099      (completing-read 
8100       "Article number: "
8101       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8102               gnus-newsgroup-headers) 
8103       nil 'require-match))))
8104   (prog1
8105       (and (gnus-summary-goto-subject article)
8106            (gnus-summary-display-article article all-headers))
8107     (gnus-summary-position-cursor)))
8108
8109 (defun gnus-summary-goto-last-article ()
8110   "Go to the previously read article."
8111   (interactive)
8112   (prog1
8113       (and gnus-last-article
8114            (gnus-summary-goto-article gnus-last-article))
8115     (gnus-summary-position-cursor)))
8116
8117 (defun gnus-summary-pop-article (number)
8118   "Pop one article off the history and go to the previous.
8119 NUMBER articles will be popped off."
8120   (interactive "p")
8121   (let (to)
8122     (setq gnus-newsgroup-history
8123           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8124     (if to
8125         (gnus-summary-goto-article (car to))
8126       (error "Article history empty")))
8127   (gnus-summary-position-cursor))
8128
8129 ;; Summary article oriented commands
8130
8131 (defun gnus-summary-refer-parent-article (n)
8132   "Refer parent article N times.
8133 The difference between N and the number of articles fetched is returned."
8134   (interactive "p")
8135   (gnus-set-global-variables)
8136   (while 
8137       (and 
8138        (> n 0)
8139        (let ((ref (header-references (gnus-get-header-by-num
8140                                       (gnus-summary-article-number)))))
8141          (if (and ref (not (equal ref ""))
8142                   (string-match "<[^<>]*>[ \t]*$" ref))
8143              (gnus-summary-refer-article 
8144               (substring ref (match-beginning 0) (match-end 0)))
8145            (gnus-message 1 "No references in article %d"
8146                          (gnus-summary-article-number))
8147            nil)))
8148     (setq n (1- n)))
8149   (gnus-summary-position-cursor)
8150   n)
8151     
8152 (defun gnus-summary-refer-article (message-id)
8153   "Refer article specified by MESSAGE-ID.
8154 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8155   (interactive "sMessage-ID: ")
8156   (if (or (not (stringp message-id))
8157           (zerop (length message-id)))
8158       ()
8159     ;; Construct the correct Message-ID if necessary.
8160     ;; Suggested by tale@pawl.rpi.edu.
8161     (or (string-match "^<" message-id)
8162         (setq message-id (concat "<" message-id)))
8163     (or (string-match ">$" message-id)
8164         (setq message-id (concat message-id ">")))
8165     (let ((header (car (gnus-gethash (downcase message-id)
8166                                      gnus-newsgroup-dependencies))))
8167       (if header
8168           (or (gnus-summary-goto-article (header-number header))
8169               ;; The header has been read, but the article had been
8170               ;; expunged, so we insert it again.
8171               (progn
8172                 (gnus-summary-insert-line
8173                  nil header 0 nil gnus-read-mark nil nil
8174                  (header-subject header))
8175                 (forward-line -1)
8176                 (header-number header)))
8177         (let ((gnus-override-method gnus-refer-article-method)
8178               (gnus-ancient-mark gnus-read-mark)
8179               (tmp-point (window-start
8180                           (get-buffer-window gnus-article-buffer)))
8181               number tmp-buf)
8182           (and gnus-refer-article-method
8183                (or (gnus-server-opened gnus-refer-article-method)
8184                    (gnus-open-server gnus-refer-article-method)))
8185           ;; Save the old article buffer.
8186           (save-excursion
8187             (set-buffer gnus-article-buffer)
8188             (gnus-kill-buffer " *temp Article*")
8189             (setq tmp-buf (rename-buffer " *temp Article*")))
8190           (prog1
8191               (if (gnus-article-prepare 
8192                    message-id nil (gnus-read-header message-id))
8193                   (progn
8194                     (setq number (header-number gnus-current-headers))
8195                     (gnus-rebuild-thread message-id)
8196                     (gnus-summary-goto-subject number)
8197                     (gnus-summary-recenter)
8198                     (gnus-article-set-window-start 
8199                      (cdr (assq number gnus-newsgroup-bookmarks)))
8200                     message-id)
8201                 ;; We restore the old article buffer.
8202                 (save-excursion
8203                   (kill-buffer gnus-article-buffer)
8204                   (set-buffer tmp-buf)
8205                   (rename-buffer gnus-article-buffer)
8206                   (let ((buffer-read-only nil))
8207                     (and tmp-point
8208                          (set-window-start (get-buffer-window (current-buffer))
8209                                            tmp-point)))))))))))
8210
8211 (defun gnus-summary-enter-digest-group ()
8212   "Enter a digest group based on the current article."
8213   (interactive)
8214   (gnus-set-global-variables)
8215   (gnus-summary-select-article)
8216   ;; We do not want a narrowed article.
8217   (gnus-summary-stop-page-breaking)
8218   (let ((name (format "%s-%d" 
8219                       (gnus-group-prefixed-name 
8220                        gnus-newsgroup-name (list 'nndoc "")) 
8221                       gnus-current-article))
8222         (ogroup gnus-newsgroup-name)
8223         (buf (current-buffer)))
8224     (if (gnus-group-read-ephemeral-group 
8225          name (list 'nndoc name
8226                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8227                     '(nndoc-article-type digest))
8228          t)
8229         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8230                 (list (list (cons 'to-group ogroup))))
8231       (switch-to-buffer buf)
8232       (gnus-set-global-variables)
8233       (gnus-configure-windows 'summary)
8234       (gnus-message 3 "Article not a digest?"))))
8235
8236 (defun gnus-summary-isearch-article ()
8237   "Do incremental search forward on current article."
8238   (interactive)
8239   (gnus-set-global-variables)
8240   (gnus-summary-select-article)
8241   (gnus-eval-in-buffer-window 
8242    gnus-article-buffer (isearch-forward)))
8243
8244 (defun gnus-summary-search-article-forward (regexp &optional backward)
8245   "Search for an article containing REGEXP forward.
8246 If BACKWARD, search backward instead."
8247   (interactive
8248    (list (read-string
8249           (format "Search article %s (regexp%s): "
8250                   (if current-prefix-arg "backward" "forward")
8251                   (if gnus-last-search-regexp
8252                       (concat ", default " gnus-last-search-regexp)
8253                     "")))
8254          current-prefix-arg))
8255   (gnus-set-global-variables)
8256   (if (string-equal regexp "")
8257       (setq regexp (or gnus-last-search-regexp ""))
8258     (setq gnus-last-search-regexp regexp))
8259   (if (gnus-summary-search-article regexp backward)
8260       (gnus-article-set-window-start 
8261        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8262     (error "Search failed: \"%s\"" regexp)))
8263
8264 (defun gnus-summary-search-article-backward (regexp)
8265   "Search for an article containing REGEXP backward."
8266   (interactive
8267    (list (read-string
8268           (format "Search article backward (regexp%s): "
8269                   (if gnus-last-search-regexp
8270                       (concat ", default " gnus-last-search-regexp)
8271                     "")))))
8272   (gnus-summary-search-article-forward regexp 'backward))
8273
8274 (defun gnus-summary-search-article (regexp &optional backward)
8275   "Search for an article containing REGEXP.
8276 Optional argument BACKWARD means do search for backward.
8277 gnus-select-article-hook is not called during the search."
8278   (let ((gnus-select-article-hook nil)  ;Disable hook.
8279         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8280         (re-search
8281          (if backward
8282              (function re-search-backward) (function re-search-forward)))
8283         (found nil)
8284         (last nil))
8285     ;; Hidden thread subtrees must be searched for ,too.
8286     (gnus-summary-show-all-threads)
8287     (if (eobp) (forward-line -1))
8288     ;; First of all, search current article.
8289     ;; We don't want to read article again from NNTP server nor reset
8290     ;; current point.
8291     (gnus-summary-select-article)
8292     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8293     (setq last gnus-current-article)
8294     (gnus-eval-in-buffer-window gnus-article-buffer
8295       (save-restriction
8296         (widen)
8297         ;; Begin search from current point.
8298         (setq found (funcall re-search regexp nil t))))
8299     ;; Then search next articles.
8300     (while (and (not found)
8301                 (gnus-summary-display-article 
8302                  (gnus-summary-search-subject backward nil nil)))
8303       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8304       (gnus-eval-in-buffer-window gnus-article-buffer
8305         (save-restriction
8306           (widen)
8307           (goto-char (if backward (point-max) (point-min)))
8308           (setq found (funcall re-search regexp nil t)))))
8309     (message "")
8310     ;; Adjust article pointer.
8311     (or (eq last gnus-current-article)
8312         (setq gnus-last-article last))
8313     ;; Return T if found such article.
8314     found))
8315
8316 (defun gnus-summary-execute-command (header regexp command &optional backward)
8317   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8318 If HEADER is an empty string (or nil), the match is done on the entire
8319 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8320   (interactive
8321    (list (let ((completion-ignore-case t))
8322            (completing-read 
8323             "Header name: "
8324             (mapcar (lambda (string) (list string))
8325                     '("Number" "Subject" "From" "Lines" "Date"
8326                       "Message-ID" "Xref" "References"))
8327             nil 'require-match))
8328          (read-string "Regexp: ")
8329          (read-key-sequence "Command: ")
8330          current-prefix-arg))
8331   (gnus-set-global-variables)
8332   ;; Hidden thread subtrees must be searched as well.
8333   (gnus-summary-show-all-threads)
8334   ;; We don't want to change current point nor window configuration.
8335   (save-excursion
8336     (save-window-excursion
8337       (gnus-message 6 "Executing %s..." (key-description command))
8338       ;; We'd like to execute COMMAND interactively so as to give arguments.
8339       (gnus-execute header regexp
8340                     (` (lambda ()
8341                          (call-interactively '(, (key-binding command)))))
8342                     backward)
8343       (gnus-message 6 "Executing %s...done" (key-description command)))))
8344
8345 (defun gnus-summary-beginning-of-article ()
8346   "Scroll the article back to the beginning."
8347   (interactive)
8348   (gnus-set-global-variables)
8349   (gnus-summary-select-article)
8350   (gnus-configure-windows 'article)
8351   (gnus-eval-in-buffer-window
8352    gnus-article-buffer
8353    (widen)
8354    (goto-char (point-min))
8355    (and gnus-break-pages (gnus-narrow-to-page))))
8356
8357 (defun gnus-summary-end-of-article ()
8358   "Scroll to the end of the article."
8359   (interactive)
8360   (gnus-set-global-variables)
8361   (gnus-summary-select-article)
8362   (gnus-configure-windows 'article)
8363   (gnus-eval-in-buffer-window 
8364    gnus-article-buffer
8365    (widen)
8366    (goto-char (point-max))
8367    (and gnus-break-pages (gnus-narrow-to-page))))
8368
8369 (defun gnus-summary-show-article (&optional no-refetch)
8370   "Force re-fetching of the current article.
8371 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8372 be performed.  The current article will simply be redisplayed."
8373   (interactive "P")
8374   (gnus-set-global-variables)
8375   (if (not no-refetch)
8376       (gnus-summary-select-article gnus-have-all-headers t)
8377     (or gnus-current-article
8378         (error "There is no current article"))
8379     (gnus-summary-goto-subject gnus-current-article)
8380     (gnus-configure-windows 'article)
8381     (gnus-summary-position-cursor)))
8382
8383 (defun gnus-summary-verbose-headers (&optional arg)
8384   "Toggle permanent full header display.
8385 If ARG is a positive number, turn header display on.
8386 If ARG is a negative number, turn header display off."
8387   (interactive "P")
8388   (gnus-set-global-variables)
8389   (gnus-summary-toggle-header arg)
8390   (setq gnus-show-all-headers
8391         (cond ((or (not (numberp arg))
8392                    (zerop arg))
8393                (not gnus-show-all-headers))
8394               ((natnump arg)
8395                t))))
8396
8397 (defun gnus-summary-toggle-header (&optional arg)
8398   "Show the headers if they are hidden, or hide them if they are shown.
8399 If ARG is a positive number, show the entire header.
8400 If ARG is a negative number, hide the unwanted header lines."
8401   (interactive "P")
8402   (gnus-set-global-variables)
8403   (save-excursion
8404     (set-buffer gnus-article-buffer)
8405     (let ((buffer-read-only nil))
8406       (if (numberp arg) 
8407           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8408                                                 gnus-hidden-properties)
8409             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8410         (if (text-property-any (point-min) (point-max) 'invisible t)
8411             (remove-text-properties 
8412              (point-min) (point-max) gnus-hidden-properties)
8413           ;; We hide the headers. This song and dance act below is
8414           ;; done because `gnus-have-all-headers' is buffer-local to
8415           ;; the summary buffer, and we only want to temporarily
8416           ;; change it in that buffer. Ugh.
8417           (let ((have gnus-have-all-headers))
8418             (save-excursion
8419               (set-buffer gnus-summary-buffer)
8420               (setq gnus-have-all-headers nil)
8421               (save-excursion
8422                 (set-buffer gnus-article-buffer)
8423                 (run-hooks 'gnus-article-display-hook))
8424               (setq gnus-have-all-headers have)))))
8425       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8426
8427 (defun gnus-summary-show-all-headers ()
8428   "Make all header lines visible."
8429   (interactive)
8430   (gnus-set-global-variables)
8431   (gnus-article-show-all-headers))
8432
8433 (defun gnus-summary-toggle-mime (&optional arg)
8434   "Toggle MIME processing.
8435 If ARG is a positive number, turn MIME processing on."
8436   (interactive "P")
8437   (gnus-set-global-variables)
8438   (setq gnus-show-mime
8439         (if (null arg) (not gnus-show-mime)
8440           (> (prefix-numeric-value arg) 0)))
8441   (gnus-summary-select-article t 'force))
8442
8443 (defun gnus-summary-caesar-message (&optional arg)
8444   "Caesar rotate the current article by 13.
8445 The numerical prefix specifies how manu places to rotate each letter
8446 forward."
8447   (interactive "P")
8448   (gnus-set-global-variables)
8449   (gnus-summary-select-article)
8450   (let ((mail-header-separator ""))
8451     (gnus-eval-in-buffer-window 
8452      gnus-article-buffer
8453      (save-restriction
8454        (widen)
8455        (let ((last (point)))
8456          (news-caesar-buffer-body arg)
8457          (goto-char last)
8458          (recenter 0))))))
8459
8460 (defun gnus-summary-stop-page-breaking ()
8461   "Stop page breaking in the current article."
8462   (interactive)
8463   (gnus-set-global-variables)
8464   (gnus-summary-select-article)
8465   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8466
8467 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8468
8469 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8470   "Move the current article to a different newsgroup.
8471 If N is a positive number, move the N next articles.
8472 If N is a negative number, move the N previous articles.
8473 If N is nil and any articles have been marked with the process mark,
8474 move those articles instead.
8475 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8476 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8477 re-spool using this method.
8478 For this function to work, both the current newsgroup and the
8479 newsgroup that you want to move to have to support the `request-move'
8480 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8481   (interactive "P")
8482   (gnus-set-global-variables)
8483   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8484       (error "The current newsgroup does not support article moving"))
8485   (let ((articles (gnus-summary-work-articles n))
8486         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8487         art-group to-method sel-met)
8488     (if (and (not to-newsgroup) (not select-method))
8489         (setq to-newsgroup
8490               (completing-read 
8491                (format "Where do you want to move %s? %s"
8492                        (if (> (length articles) 1)
8493                            (format "these %d articles" (length articles))
8494                          "this article")
8495                        (if gnus-current-move-group
8496                            (format "(%s default) " gnus-current-move-group)
8497                          ""))
8498                gnus-active-hashtb nil nil prefix)))
8499     (if to-newsgroup
8500         (progn
8501           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8502               (setq to-newsgroup (or gnus-current-move-group "")))
8503           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8504               (gnus-activate-newsgroup to-newsgroup)
8505               (error "No such group: %s" to-newsgroup))
8506           (setq gnus-current-move-group to-newsgroup)))
8507     (setq to-method (if select-method (list select-method "")
8508                       (gnus-find-method-for-group to-newsgroup)))
8509     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8510         (error "%s does not support article copying" (car to-method)))
8511     (or (gnus-server-opened to-method)
8512         (gnus-open-server to-method)
8513         (error "Can't open server %s" (car to-method)))
8514     (gnus-message 6 "Moving to %s: %s..." 
8515                   (or select-method to-newsgroup) articles)
8516     (while articles
8517       (if (setq art-group
8518                 (gnus-request-move-article 
8519                  (car articles)                   ; Article to move
8520                  gnus-newsgroup-name              ; From newsgrouo
8521                  (nth 1 (gnus-find-method-for-group 
8522                          gnus-newsgroup-name))    ; Server
8523                  (list 'gnus-request-accept-article 
8524                        (if select-method
8525                            (list 'quote select-method)
8526                          to-newsgroup)
8527                        (not (cdr articles)))     ; Accept form
8528                  (not (cdr articles))))          ; Only save nov last time
8529           (let* ((buffer-read-only nil)
8530                  (entry 
8531                   (or
8532                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8533                    (gnus-gethash 
8534                     (gnus-group-prefixed-name 
8535                      (car art-group) 
8536                      (if select-method (list select-method "")
8537                        (gnus-find-method-for-group to-newsgroup)))
8538                     gnus-newsrc-hashtb)))
8539                  (info (nth 2 entry))
8540                  (article (car articles)))
8541             (gnus-summary-goto-subject article)
8542             (beginning-of-line)
8543             (delete-region (point)
8544                            (progn (forward-line 1) (point)))
8545             (if (not (memq article gnus-newsgroup-unreads))
8546                 (setcar (cdr (cdr info))
8547                         (gnus-add-to-range (nth 2 info) 
8548                                            (list (cdr art-group)))))
8549             ;; Copy any marks over to the new group.
8550             (let ((marks '((tick . gnus-newsgroup-marked)
8551                            (dormant . gnus-newsgroup-dormant)
8552                            (expire . gnus-newsgroup-expirable)
8553                            (bookmark . gnus-newsgroup-bookmarks)
8554                         ;   (score . gnus-newsgroup-scored)
8555                            (reply . gnus-newsgroup-replied)))
8556                   (to-article (cdr art-group)))
8557               (while marks
8558                 (if (memq article (symbol-value (cdr (car marks))))
8559                     (gnus-add-marked-articles 
8560                      (car info) (car (car marks)) (list to-article) info))
8561                 (setq marks (cdr marks))))
8562             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8563             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8564             (setq gnus-newsgroup-dormant
8565                   (delq article gnus-newsgroup-dormant)))
8566         (gnus-message 1 "Couldn't move article %s" (car articles)))
8567       (gnus-summary-remove-process-mark (car articles))
8568       (setq articles (cdr articles)))))
8569
8570 (defun gnus-summary-respool-article (&optional n respool-method)
8571   "Respool the current article.
8572 The article will be squeezed through the mail spooling process again,
8573 which means that it will be put in some mail newsgroup or other
8574 depending on `nnmail-split-methods'.
8575 If N is a positive number, respool the N next articles.
8576 If N is a negative number, respool the N previous articles.
8577 If N is nil and any articles have been marked with the process mark,
8578 respool those articles instead.
8579
8580 Respooling can be done both from mail groups and \"real\" newsgroups.
8581 In the former case, the articles in question will be moved from the
8582 current group into whatever groups they are destined to.  In the
8583 latter case, they will be copied into the relevant groups."
8584   (interactive "P")
8585   (gnus-set-global-variables)
8586   (let ((respool-methods (gnus-methods-using 'respool))
8587         (methname 
8588          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8589     (or respool-method
8590         (setq respool-method
8591               (completing-read
8592                "What method do you want to use when respooling? "
8593                respool-methods nil t methname)))
8594     (or (string= respool-method "")
8595         (if (assoc (symbol-name
8596                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8597                    respool-methods)
8598             (gnus-summary-move-article n nil (intern respool-method))
8599           (gnus-summary-copy-article n nil (intern respool-method))))))
8600
8601 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8602 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8603   "Move the current article to a different newsgroup.
8604 If N is a positive number, move the N next articles.
8605 If N is a negative number, move the N previous articles.
8606 If N is nil and any articles have been marked with the process mark,
8607 move those articles instead.
8608 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8609 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8610 re-spool using this method.
8611 For this function to work, the newsgroup that you want to move to have
8612 to support the `request-move' and `request-accept'
8613 functions. (Ie. mail newsgroups at present.)"
8614   (interactive "P")
8615   (gnus-set-global-variables)
8616   (let ((articles (gnus-summary-work-articles n))
8617         (copy-buf (get-buffer-create "*copy work*"))
8618         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8619         art-group to-method)
8620     (buffer-disable-undo copy-buf)
8621     (if (and (not to-newsgroup) (not select-method))
8622         (setq to-newsgroup
8623               (completing-read 
8624                (format "Where do you want to copy %s? %s"
8625                        (if (> (length articles) 1)
8626                            (format "these %d articles" (length articles))
8627                          "this article")
8628                        (if gnus-current-move-group
8629                            (format "(%s default) " gnus-current-move-group)
8630                          ""))
8631                gnus-active-hashtb nil nil prefix)))
8632     (if to-newsgroup
8633         (progn
8634           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8635               (setq to-newsgroup (or gnus-current-move-group "")))
8636           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8637               (gnus-activate-newsgroup to-newsgroup)
8638               (error "No such group: %s" to-newsgroup))
8639           (setq gnus-current-move-group to-newsgroup)))
8640     (setq to-method (if select-method (list select-method "")
8641                       (gnus-find-method-for-group to-newsgroup)))
8642     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8643         (error "%s does not support article copying" (car to-method)))
8644     (or (gnus-server-opened to-method)
8645         (gnus-open-server to-method)
8646         (error "Can't open server %s" (car to-method)))
8647     (while articles
8648       (gnus-message 6 "Copying to %s: %s..." 
8649                     (or select-method to-newsgroup) articles)
8650       (if (setq art-group
8651                 (save-excursion
8652                   (set-buffer copy-buf)
8653                   (gnus-request-article-this-buffer
8654                    (car articles) gnus-newsgroup-name)
8655                   (gnus-request-accept-article
8656                    (if select-method (quote select-method) to-newsgroup)
8657                    (not (cdr articles)))))
8658           (let* ((entry 
8659                   (or
8660                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8661                    (gnus-gethash 
8662                     (gnus-group-prefixed-name 
8663                      (car art-group) 
8664                      (if select-method (list select-method "")
8665                        (gnus-find-method-for-group to-newsgroup)))
8666                     gnus-newsrc-hashtb)))
8667                  (info (nth 2 entry))
8668                  (article (car articles)))
8669             (if (not (memq article gnus-newsgroup-unreads))
8670                 (setcar (cdr (cdr info))
8671                         (gnus-add-to-range (nth 2 info) 
8672                                            (list (cdr art-group)))))
8673             ;; Copy any marks over to the new group.
8674             (let ((marks '((tick . gnus-newsgroup-marked)
8675                            (dormant . gnus-newsgroup-dormant)
8676                            (expire . gnus-newsgroup-expirable)
8677                            (bookmark . gnus-newsgroup-bookmarks)
8678                         ;   (score . gnus-newsgroup-scored)
8679                            (reply . gnus-newsgroup-replied)))
8680                   (to-article (cdr art-group)))
8681               (while marks
8682                 (if (memq article (symbol-value (cdr (car marks))))
8683                     (gnus-add-marked-articles 
8684                      (car info) (car (car marks)) (list to-article) info))
8685                 (setq marks (cdr marks)))))
8686         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8687       (gnus-summary-remove-process-mark (car articles))
8688       (setq articles (cdr articles)))
8689     (kill-buffer copy-buf)))
8690
8691 (defun gnus-summary-import-article (file)
8692   "Import a random file into a mail newsgroup."
8693   (interactive "fImport file: ")
8694   (let ((group gnus-newsgroup-name)
8695         atts)
8696     (or (gnus-check-backend-function 'request-accept-article group)
8697         (error "%s does not support article importing" group))
8698     (or (file-readable-p file)
8699         (not (file-regular-p file))
8700         (error "Can't read %s" file))
8701     (save-excursion
8702       (set-buffer (get-buffer-create " *import file*"))
8703       (buffer-disable-undo (current-buffer))
8704       (erase-buffer)
8705       (insert-file-contents file)
8706       (goto-char (point-min))
8707       (if (nnheader-article-p)
8708           ()
8709         (setq atts (file-attributes file))
8710         (insert "From: " (read-string "From: ") "\n"
8711                 "Subject: " (read-string "Subject: ") "\n"
8712                 "Date: " (current-time-string (nth 5 atts)) "\n"
8713                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8714       (gnus-request-accept-article group t)
8715       (kill-buffer (current-buffer)))))
8716
8717 (defun gnus-summary-expire-articles ()
8718   "Expire all articles that are marked as expirable in the current group."
8719   (interactive)
8720   (if (not (gnus-check-backend-function 
8721             'request-expire-articles gnus-newsgroup-name))
8722       ()
8723     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8724                                       gnus-newsrc-hashtb)))
8725            (total (memq 'total-expire (nth 5 info)))
8726            (expirable (if total
8727                           (gnus-list-of-read-articles gnus-newsgroup-name)
8728                         (setq gnus-newsgroup-expirable
8729                               (sort gnus-newsgroup-expirable '<))))
8730            es)
8731       (if (not expirable)
8732           ()
8733         (gnus-message 6 "Expiring articles...")
8734         ;; The list of articles that weren't expired is returned.
8735         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8736         (or total (setq gnus-newsgroup-expirable es))
8737         ;; We go through the old list of expirable, and mark all
8738         ;; really expired articles as non-existant.
8739         (or (eq es expirable) ; If nothing was expired, we don't mark.
8740             (let ((gnus-use-cache nil))
8741               (while expirable
8742                 (or (memq (car expirable) es)
8743                     (gnus-summary-mark-article
8744                      (car expirable) gnus-canceled-mark))
8745                 (setq expirable (cdr expirable)))))
8746         (gnus-message 6 "Expiring articles...done")))))
8747
8748 (defun gnus-summary-expire-articles-now ()
8749   "Expunge all expirable articles in the current group.
8750 This means that *all* articles that are marked as expirable will be
8751 deleted forever, right now."
8752   (interactive)
8753   (or gnus-expert-user
8754       (gnus-y-or-n-p
8755        "Are you really, really, really sure you want to expunge? ")
8756       (error "Phew!"))
8757   (let ((nnmail-expiry-wait -1)
8758         (nnmail-expiry-wait-function nil))
8759     (gnus-summary-expire-articles)))
8760
8761 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8762 (defun gnus-summary-delete-article (&optional n)
8763   "Delete the N next (mail) articles.
8764 This command actually deletes articles. This is not a marking
8765 command. The article will disappear forever from you life, never to
8766 return. 
8767 If N is negative, delete backwards.
8768 If N is nil and articles have been marked with the process mark,
8769 delete these instead."
8770   (interactive "P")
8771   (or (gnus-check-backend-function 'request-expire-articles 
8772                                    gnus-newsgroup-name)
8773       (error "The current newsgroup does not support article deletion."))
8774   ;; Compute the list of articles to delete.
8775   (let ((articles (gnus-summary-work-articles n))
8776         not-deleted)
8777     (if (and gnus-novice-user
8778              (not (gnus-y-or-n-p 
8779                    (format "Do you really want to delete %s forever? "
8780                            (if (> (length articles) 1) "these articles"
8781                              "this article")))))
8782         ()
8783       ;; Delete the articles.
8784       (setq not-deleted (gnus-request-expire-articles 
8785                          articles gnus-newsgroup-name 'force))
8786       (while articles
8787         (gnus-summary-remove-process-mark (car articles))       
8788         ;; The backend might not have been able to delete the article
8789         ;; after all.  
8790         (or (memq (car articles) not-deleted)
8791             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8792         (setq articles (cdr articles))))
8793     (gnus-summary-position-cursor)
8794     not-deleted))
8795
8796 (defun gnus-summary-edit-article ()
8797   "Enter into a buffer and edit the current article.
8798 This will have permanent effect only in mail groups."
8799   (interactive)
8800   (or (gnus-check-backend-function 
8801        'request-replace-article gnus-newsgroup-name)
8802       (error "The current newsgroup does not support article editing."))
8803   (gnus-summary-select-article t)
8804   (gnus-configure-windows 'article)
8805   (select-window (get-buffer-window gnus-article-buffer))
8806   (gnus-message 6 "C-c C-c to end edits")
8807   (setq buffer-read-only nil)
8808   (text-mode)
8809   (use-local-map (copy-keymap (current-local-map)))
8810   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8811   (buffer-enable-undo)
8812   (widen)
8813   (goto-char (point-min))
8814   (search-forward "\n\n" nil t))
8815
8816 (defun gnus-summary-edit-article-done ()
8817   "Make edits to the current article permanent."
8818   (interactive)
8819   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8820     (erase-buffer)
8821     (insert buf)
8822     (if (not (gnus-request-replace-article 
8823               (cdr gnus-article-current) (car gnus-article-current) 
8824               (current-buffer)))
8825         (error "Couldn't replace article.")
8826       (gnus-article-mode)
8827       (use-local-map gnus-article-mode-map)
8828       (setq buffer-read-only t)
8829       (buffer-disable-undo (current-buffer))
8830       (gnus-configure-windows 'summary))))
8831
8832 (defun gnus-summary-fancy-query ()
8833   "Query where the fancy respool algorithm would put this article."
8834   (interactive)
8835   (gnus-summary-select-article)
8836   (save-excursion
8837     (set-buffer gnus-article-buffer)
8838     (save-restriction
8839       (goto-char (point-min))
8840       (search-forward "\n\n")
8841       (narrow-to-region (point-min) (point))
8842       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8843
8844 ;; Summary score commands.
8845
8846 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8847
8848 (defun gnus-summary-raise-score (n)
8849   "Raise the score of the current article by N."
8850   (interactive "p")
8851   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8852
8853 (defun gnus-summary-set-score (n)
8854   "Set the score of the current article to N."
8855   (interactive "p")
8856   ;; Skip dummy header line.
8857   (save-excursion
8858     (gnus-summary-show-thread)
8859     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8860         (forward-line 1))
8861     (let ((buffer-read-only nil))
8862       ;; Set score.
8863       (gnus-summary-update-mark
8864        (if (= n (or gnus-summary-default-score 0)) ? 
8865          (if (< n (or gnus-summary-default-score 0)) 
8866              gnus-score-below-mark gnus-score-over-mark)) 'score))
8867     (let* ((article (gnus-summary-article-number))
8868            (score (assq article gnus-newsgroup-scored)))
8869       (if score (setcdr score n)
8870         (setq gnus-newsgroup-scored 
8871               (cons (cons article n) gnus-newsgroup-scored))))
8872     (gnus-summary-update-line)))
8873
8874 (defun gnus-summary-current-score ()
8875   "Return the score of the current article."
8876   (interactive)
8877   (message "%s" (gnus-summary-article-score)))
8878
8879 ;; Summary marking commands.
8880
8881 (defun gnus-summary-raise-same-subject-and-select (score)
8882   "Raise articles which has the same subject with SCORE and select the next."
8883   (interactive "p")
8884   (let ((subject (gnus-summary-subject-string)))
8885     (gnus-summary-raise-score score)
8886     (while (gnus-summary-search-subject nil nil subject)
8887       (gnus-summary-raise-score score))
8888     (gnus-summary-next-article t)))
8889
8890 (defun gnus-summary-raise-same-subject (score)
8891   "Raise articles which has the same subject with SCORE."
8892   (interactive "p")
8893   (let ((subject (gnus-summary-subject-string)))
8894     (gnus-summary-raise-score score)
8895     (while (gnus-summary-search-subject nil nil subject)
8896       (gnus-summary-raise-score score))
8897     (gnus-summary-next-subject 1 t)))
8898
8899 (defun gnus-score-default (level)
8900   (if level (prefix-numeric-value level) 
8901     gnus-score-interactive-default-score))
8902
8903 (defun gnus-summary-raise-thread (&optional score)
8904   "Raise the score of the articles in the current thread with SCORE."
8905   (interactive "P")
8906   (setq score (1- (gnus-score-default score)))
8907   (let (e)
8908     (save-excursion
8909       (let ((level (gnus-summary-thread-level)))
8910         (gnus-summary-raise-score score)
8911         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8912                     (> (gnus-summary-thread-level) level))
8913           (gnus-summary-raise-score score))
8914         (setq e (point))))
8915     (let ((gnus-summary-check-current t))
8916       (or (zerop (gnus-summary-next-subject 1 t))
8917           (goto-char e))))
8918   (gnus-summary-recenter)
8919   (gnus-summary-position-cursor)
8920   (gnus-set-mode-line 'summary))
8921
8922 (defun gnus-summary-lower-same-subject-and-select (score)
8923   "Raise articles which has the same subject with SCORE and select the next."
8924   (interactive "p")
8925   (gnus-summary-raise-same-subject-and-select (- score)))
8926
8927 (defun gnus-summary-lower-same-subject (score)
8928   "Raise articles which has the same subject with SCORE."
8929   (interactive "p")
8930   (gnus-summary-raise-same-subject (- score)))
8931
8932 (defun gnus-summary-lower-thread (&optional score)
8933   "Lower score of articles in the current thread with SCORE."
8934   (interactive "P")
8935   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8936
8937 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8938   "Mark articles which has the same subject as read, and then select the next.
8939 If UNMARK is positive, remove any kind of mark.
8940 If UNMARK is negative, tick articles."
8941   (interactive "P")
8942   (if unmark
8943       (setq unmark (prefix-numeric-value unmark)))
8944   (let ((count
8945          (gnus-summary-mark-same-subject
8946           (gnus-summary-subject-string) unmark)))
8947     ;; Select next unread article. If auto-select-same mode, should
8948     ;; select the first unread article.
8949     (gnus-summary-next-article t (and gnus-auto-select-same
8950                                       (gnus-summary-subject-string)))
8951     (gnus-message 7 "%d articles are marked as %s"
8952                   count (if unmark "unread" "read"))))
8953
8954 (defun gnus-summary-kill-same-subject (&optional unmark)
8955   "Mark articles which has the same subject as read. 
8956 If UNMARK is positive, remove any kind of mark.
8957 If UNMARK is negative, tick articles."
8958   (interactive "P")
8959   (if unmark
8960       (setq unmark (prefix-numeric-value unmark)))
8961   (let ((count
8962          (gnus-summary-mark-same-subject
8963           (gnus-summary-subject-string) unmark)))
8964     ;; If marked as read, go to next unread subject.
8965     (if (null unmark)
8966         ;; Go to next unread subject.
8967         (gnus-summary-next-subject 1 t))
8968     (gnus-message 7 "%d articles are marked as %s"
8969                   count (if unmark "unread" "read"))))
8970
8971 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8972   "Mark articles with same SUBJECT as read, and return marked number.
8973 If optional argument UNMARK is positive, remove any kinds of marks.
8974 If optional argument UNMARK is negative, mark articles as unread instead."
8975   (let ((count 1))
8976     (save-excursion
8977       (cond 
8978        ((null unmark)                   ; Mark as read.
8979         (while (and 
8980                 (progn
8981                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8982                   (gnus-summary-show-thread) t)
8983                 (gnus-summary-search-forward nil subject))
8984           (setq count (1+ count))))
8985        ((> unmark 0)                    ; Tick.
8986         (while (and
8987                 (progn
8988                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8989                   (gnus-summary-show-thread) t)
8990                 (gnus-summary-search-forward nil subject))
8991           (setq count (1+ count))))
8992        (t                               ; Mark as unread.
8993         (while (and
8994                 (progn
8995                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8996                   (gnus-summary-show-thread) t)
8997                 (gnus-summary-search-forward nil subject))
8998           (setq count (1+ count)))))
8999       ;; Return the number of marked articles.
9000       count)))
9001
9002 (defun gnus-summary-mark-as-processable (n &optional unmark)
9003   "Set the process mark on the next N articles.
9004 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9005 the process mark instead.  The difference between N and the actual
9006 number of articles marked is returned."
9007   (interactive "p")
9008   (let ((backward (< n 0))
9009         (n (abs n)))
9010   (while (and 
9011           (> n 0)
9012           (if unmark
9013               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9014             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9015           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9016     (setq n (1- n)))
9017   (if (/= 0 n) (gnus-message 7 "No more articles"))
9018   (gnus-summary-recenter)
9019   (gnus-summary-position-cursor)
9020   n))
9021
9022 (defun gnus-summary-unmark-as-processable (n)
9023   "Remove the process mark from the next N articles.
9024 If N is negative, mark backward instead.  The difference between N and
9025 the actual number of articles marked is returned."
9026   (interactive "p")
9027   (gnus-summary-mark-as-processable n t))
9028
9029 (defun gnus-summary-unmark-all-processable ()
9030   "Remove the process mark from all articles."
9031   (interactive)
9032   (save-excursion
9033     (while gnus-newsgroup-processable
9034       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9035   (gnus-summary-position-cursor))
9036
9037 (defun gnus-summary-mark-as-expirable (n)
9038   "Mark N articles forward as expirable.
9039 If N is negative, mark backward instead. The difference between N and
9040 the actual number of articles marked is returned."
9041   (interactive "p")
9042   (gnus-summary-mark-forward n gnus-expirable-mark))
9043
9044 (defun gnus-summary-mark-article-as-replied (article)
9045   "Mark ARTICLE replied and update the summary line."
9046   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9047   (let ((buffer-read-only nil))
9048     (if (gnus-summary-goto-subject article)
9049         (progn
9050           (gnus-summary-update-mark gnus-replied-mark 'replied)
9051           t))))
9052
9053 (defun gnus-summary-set-bookmark (article)
9054   "Set a bookmark in current article."
9055   (interactive (list (gnus-summary-article-number)))
9056   (if (or (not (get-buffer gnus-article-buffer))
9057           (not gnus-current-article)
9058           (not gnus-article-current)
9059           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9060       (error "No current article selected"))
9061   ;; Remove old bookmark, if one exists.
9062   (let ((old (assq article gnus-newsgroup-bookmarks)))
9063     (if old (setq gnus-newsgroup-bookmarks 
9064                   (delq old gnus-newsgroup-bookmarks))))
9065   ;; Set the new bookmark, which is on the form 
9066   ;; (article-number . line-number-in-body).
9067   (setq gnus-newsgroup-bookmarks 
9068         (cons 
9069          (cons article 
9070                (save-excursion
9071                  (set-buffer gnus-article-buffer)
9072                  (count-lines
9073                   (min (point)
9074                        (save-excursion
9075                          (goto-char (point-min))
9076                          (search-forward "\n\n" nil t)
9077                          (point)))
9078                   (point))))
9079          gnus-newsgroup-bookmarks))
9080   (gnus-message 6 "A bookmark has been added to the current article."))
9081
9082 (defun gnus-summary-remove-bookmark (article)
9083   "Remove the bookmark from the current article."
9084   (interactive (list (gnus-summary-article-number)))
9085   ;; Remove old bookmark, if one exists.
9086   (let ((old (assq article gnus-newsgroup-bookmarks)))
9087     (if old 
9088         (progn
9089           (setq gnus-newsgroup-bookmarks 
9090                 (delq old gnus-newsgroup-bookmarks))
9091           (gnus-message 6 "Removed bookmark."))
9092       (gnus-message 6 "No bookmark in current article."))))
9093
9094 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9095 (defun gnus-summary-mark-as-dormant (n)
9096   "Mark N articles forward as dormant.
9097 If N is negative, mark backward instead.  The difference between N and
9098 the actual number of articles marked is returned."
9099   (interactive "p")
9100   (gnus-summary-mark-forward n gnus-dormant-mark))
9101
9102 (defun gnus-summary-set-process-mark (article)
9103   "Set the process mark on ARTICLE and update the summary line."
9104   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9105   (let ((buffer-read-only nil))
9106     (if (gnus-summary-goto-subject article)
9107         (progn
9108           (gnus-summary-show-thread)
9109           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9110                (forward-line 1))
9111           (gnus-summary-update-mark gnus-process-mark 'replied)
9112           t))))
9113
9114 (defun gnus-summary-remove-process-mark (article)
9115   "Remove the process mark from ARTICLE and update the summary line."
9116   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9117   (let ((buffer-read-only nil))
9118     (if (gnus-summary-goto-subject article)
9119         (progn
9120           (gnus-summary-show-thread)
9121           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9122                (forward-line 1))
9123           (gnus-summary-update-mark ?  'replied)
9124           (if (memq article gnus-newsgroup-replied) 
9125               (gnus-summary-update-mark gnus-replied-mark 'replied))
9126           t))))
9127
9128 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9129   "Mark N articles as read forwards.
9130 If N is negative, mark backwards instead.
9131 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9132 marked as unread. 
9133 The difference between N and the actual number of articles marked is
9134 returned."
9135   (interactive "p")
9136   (gnus-set-global-variables)
9137   (let ((backward (< n 0))
9138         (gnus-summary-goto-unread
9139          (and gnus-summary-goto-unread
9140               (not (memq mark (list gnus-unread-mark
9141                                     gnus-ticked-mark gnus-dormant-mark)))))
9142         (n (abs n))
9143         (mark (or mark gnus-del-mark)))
9144   (while (and (> n 0)
9145               (gnus-summary-mark-article nil mark no-expire)
9146               (zerop (gnus-summary-next-subject 
9147                       (if backward -1 1) gnus-summary-goto-unread t)))
9148     (setq n (1- n)))
9149   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9150   (gnus-summary-recenter)
9151   (gnus-summary-position-cursor)
9152   (gnus-set-mode-line 'summary)
9153   n))
9154
9155 (defun gnus-summary-mark-article-as-read (mark)
9156   "Mark the current article quickly as read with MARK."
9157   (let ((article (gnus-summary-article-number)))
9158     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9159     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9160     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9161     ;; Possibly remove from cache, if that is used. 
9162     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9163     (and gnus-newsgroup-auto-expire 
9164          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9165              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9166              (= mark gnus-read-mark))
9167          (progn
9168            (setq mark gnus-expirable-mark)
9169            (setq gnus-newsgroup-expirable 
9170                  (cons article gnus-newsgroup-expirable))))
9171     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9172       (forward-line 1))
9173     ;; Fix the mark.
9174     (gnus-summary-update-mark mark 'unread)
9175     t))
9176
9177 (defun gnus-summary-mark-article-as-unread (mark)
9178   "Mark the current article quickly as unread with MARK."
9179   (let ((article (gnus-summary-article-number)))
9180     (or (memq article gnus-newsgroup-unreads)
9181         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9182     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9183     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9184     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9185     (if (= mark gnus-ticked-mark)
9186         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9187     (if (= mark gnus-dormant-mark)
9188         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9189
9190     ;; See whether the article is to be put in the cache.
9191     (and gnus-use-cache
9192          (save-excursion
9193            (gnus-cache-possibly-enter-article 
9194             gnus-newsgroup-name article 
9195             (gnus-get-header-by-num article)
9196             (= mark gnus-ticked-mark)
9197             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9198
9199     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9200       (forward-line 1))
9201     ;; Fix the mark.
9202     (gnus-summary-update-mark mark 'unread)
9203     t))
9204
9205 (defun gnus-summary-mark-article (&optional article mark no-expire)
9206   "Mark ARTICLE with MARK.  MARK can be any character.
9207 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9208 (dormant) and `?E' (expirable).
9209 If MARK is nil, then the default character `?D' is used.
9210 If ARTICLE is nil, then the article on the current line will be
9211 marked." 
9212   (and (stringp mark)
9213        (setq mark (aref mark 0)))
9214   ;; If no mark is given, then we check auto-expiring.
9215   (and (not no-expire)
9216        gnus-newsgroup-auto-expire 
9217        (or (not mark)
9218            (and (numberp mark) 
9219                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9220                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9221                     (= mark gnus-read-mark))))
9222        (setq mark gnus-expirable-mark))
9223   (let* ((mark (or mark gnus-del-mark))
9224          (article (or article (gnus-summary-article-number))))
9225     (or article (error "No article on current line"))
9226     (if (or (= mark gnus-unread-mark) 
9227             (= mark gnus-ticked-mark) 
9228             (= mark gnus-dormant-mark))
9229         (gnus-mark-article-as-unread article mark)
9230       (gnus-mark-article-as-read article mark))
9231
9232     ;; See whether the article is to be put in the cache.
9233     (and gnus-use-cache
9234          (not (= mark gnus-canceled-mark))
9235          (save-excursion
9236            (gnus-cache-possibly-enter-article 
9237             gnus-newsgroup-name article 
9238             (gnus-get-header-by-num article)
9239             (= mark gnus-ticked-mark)
9240             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9241
9242     (if (gnus-summary-goto-subject article)
9243         (let ((buffer-read-only nil))
9244           (gnus-summary-show-thread)
9245           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9246                (forward-line 1))
9247           ;; Fix the mark.
9248           (gnus-summary-update-mark mark 'unread)
9249           t))))
9250
9251 (defun gnus-summary-update-mark (mark type)
9252   (beginning-of-line)
9253   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9254         (buffer-read-only nil)
9255         plist)
9256     (if (not forward)
9257         ()
9258       (forward-char forward)
9259       (setq plist (text-properties-at (point)))
9260       (delete-char 1)
9261       (insert mark)
9262       (and plist (add-text-properties (1- (point)) (point) plist))
9263       (and (eq type 'unread)
9264            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9265       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9266   
9267 (defun gnus-mark-article-as-read (article &optional mark)
9268   "Enter ARTICLE in the pertinent lists and remove it from others."
9269   ;; Make the article expirable.
9270   (let ((mark (or mark gnus-del-mark)))
9271     (if (= mark gnus-expirable-mark)
9272         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9273       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9274     ;; Remove from unread and marked lists.
9275     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9276     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9277     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9278     ;; Possibly remove from cache, if that is used. 
9279     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9280
9281 (defun gnus-mark-article-as-unread (article &optional mark)
9282   "Enter ARTICLE in the pertinent lists and remove it from others."
9283   (let ((mark (or mark gnus-ticked-mark)))
9284     ;; Add to unread list.
9285     (or (memq article gnus-newsgroup-unreads)
9286         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9287     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9288     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9289     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9290     (if (= mark gnus-ticked-mark)
9291         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9292     (if (= mark gnus-dormant-mark)
9293         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9294
9295 (defalias 'gnus-summary-mark-as-unread-forward 
9296   'gnus-summary-tick-article-forward)
9297 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9298                'gnus-summary-tick-article-forward)
9299 (defun gnus-summary-tick-article-forward (n)
9300   "Tick N articles forwards.
9301 If N is negative, tick backwards instead.
9302 The difference between N and the number of articles ticked is returned."
9303   (interactive "p")
9304   (gnus-summary-mark-forward n gnus-ticked-mark))
9305
9306 (defalias 'gnus-summary-mark-as-unread-backward 
9307   'gnus-summary-tick-article-backward)
9308 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9309                'gnus-summary-tick-article-backward)
9310 (defun gnus-summary-tick-article-backward (n)
9311   "Tick N articles backwards.
9312 The difference between N and the number of articles ticked is returned."
9313   (interactive "p")
9314   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9315
9316 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9317 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9318 (defun gnus-summary-tick-article (&optional article clear-mark)
9319   "Mark current article as unread.
9320 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9321 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9322   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9323                                        gnus-ticked-mark)))
9324
9325 (defun gnus-summary-mark-as-read-forward (n)
9326   "Mark N articles as read forwards.
9327 If N is negative, mark backwards instead.
9328 The difference between N and the actual number of articles marked is
9329 returned."
9330   (interactive "p")
9331   (gnus-summary-mark-forward n gnus-del-mark t))
9332
9333 (defun gnus-summary-mark-as-read-backward (n)
9334   "Mark the N articles as read backwards.
9335 The difference between N and the actual number of articles marked is
9336 returned."
9337   (interactive "p")
9338   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9339
9340 (defun gnus-summary-mark-as-read (&optional article mark)
9341   "Mark current article as read.
9342 ARTICLE specifies the article to be marked as read.
9343 MARK specifies a string to be inserted at the beginning of the line."
9344   (gnus-summary-mark-article article mark))
9345
9346 (defun gnus-summary-clear-mark-forward (n)
9347   "Clear marks from N articles forward.
9348 If N is negative, clear backward instead.
9349 The difference between N and the number of marks cleared is returned."
9350   (interactive "p")
9351   (gnus-summary-mark-forward n gnus-unread-mark))
9352
9353 (defun gnus-summary-clear-mark-backward (n)
9354   "Clear marks from N articles backward.
9355 The difference between N and the number of marks cleared is returned."
9356   (interactive "p")
9357   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9358
9359 (defun gnus-summary-mark-unread-as-read ()
9360   "Intended to be used by `gnus-summary-mark-article-hook'."
9361   (and (memq gnus-current-article gnus-newsgroup-unreads)
9362        (or (memq gnus-current-article gnus-newsgroup-marked)
9363            (memq gnus-current-article gnus-newsgroup-dormant)
9364            (memq gnus-current-article gnus-newsgroup-expirable)
9365            (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9366
9367 (defun gnus-summary-mark-region-as-read (point mark all)
9368   "Mark all unread articles between point and mark as read.
9369 If given a prefix, mark all articles between point and mark as read,
9370 even ticked and dormant ones."
9371   (interactive "r\nP")
9372   (save-excursion
9373     (goto-char point)
9374     (beginning-of-line)
9375     (while (and 
9376             (< (point) mark)
9377             (progn
9378               (and
9379                (or all
9380                    (and
9381                     (not (memq (gnus-summary-article-number)
9382                                gnus-newsgroup-marked))
9383                     (not (memq (gnus-summary-article-number)
9384                                gnus-newsgroup-dormant))))
9385                (gnus-summary-mark-article
9386                 (gnus-summary-article-number) gnus-del-mark))
9387               t)
9388             (zerop (forward-line 1))))))
9389
9390 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9391 (defalias 'gnus-summary-delete-marked-as-read 
9392   'gnus-summary-remove-lines-marked-as-read)
9393 (make-obsolete 'gnus-summary-delete-marked-as-read 
9394                'gnus-summary-remove-lines-marked-as-read)
9395 (defun gnus-summary-remove-lines-marked-as-read ()
9396   "Remove lines that are marked as read."
9397   (interactive)
9398   (gnus-summary-remove-lines-marked-with 
9399    (concat (mapconcat
9400             (lambda (char) (char-to-string (symbol-value char)))
9401             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9402               gnus-killed-mark gnus-kill-file-mark
9403               gnus-low-score-mark gnus-expirable-mark
9404               gnus-canceled-mark)
9405             ""))))
9406
9407 (defalias 'gnus-summary-delete-marked-with 
9408   'gnus-summary-remove-lines-marked-with)
9409 (make-obsolete 'gnus-summary-delete-marked-with 
9410                'gnus-summary-remove-lines-marked-with)
9411 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9412 (defun gnus-summary-remove-lines-marked-with (marks)
9413   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9414   (interactive "sMarks: ")
9415   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9416   (gnus-set-global-variables)
9417   (let ((buffer-read-only nil)
9418         (marks (concat "^[" marks "]")))
9419     (goto-char (point-min))
9420     (if gnus-newsgroup-adaptive
9421         (gnus-score-remove-lines-adaptive marks)
9422       (while (re-search-forward marks nil t)
9423         (gnus-delete-line)))
9424     ;; If we use dummy roots, we have to do an additional sweep over
9425     ;; the buffer.
9426     (if (not (eq gnus-summary-make-false-root 'dummy))
9427         ()
9428       (goto-char (point-min))
9429       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9430       (while (re-search-forward marks nil t)
9431         (if (gnus-subject-equal
9432              (gnus-summary-subject-string)
9433              (progn
9434                (forward-line 1)
9435                (gnus-summary-subject-string)))
9436             ()
9437           (forward-line -1)
9438           (gnus-delete-line)))))
9439   (or (zerop (buffer-size))
9440       (if (eobp)
9441           (gnus-summary-prev-subject 1)
9442         (gnus-summary-position-cursor))))
9443
9444 (defun gnus-summary-expunge-below (&optional score)
9445   "Remove articles with score less than SCORE."
9446   (interactive "P")
9447   (gnus-set-global-variables)
9448   (setq score (if score
9449                   (prefix-numeric-value score)
9450                 (or gnus-summary-default-score 0)))
9451   (save-excursion
9452     (set-buffer gnus-summary-buffer)
9453     (goto-char (point-min))
9454     (let ((buffer-read-only nil)
9455           beg)
9456       (while (not (eobp))
9457         (if (< (gnus-summary-article-score) score)
9458             (progn
9459               (setq beg (point))
9460               (forward-line 1)
9461               (delete-region beg (point)))
9462           (forward-line 1)))
9463       ;; Adjust point.
9464       (or (zerop (buffer-size))
9465           (if (eobp)
9466               (gnus-summary-prev-subject 1)
9467             (gnus-summary-position-cursor))))))
9468
9469 (defun gnus-summary-mark-below (score mark)
9470   "Mark articles with score less than SCORE with MARK."
9471   (interactive "P\ncMark: ")
9472   (gnus-set-global-variables)
9473   (setq score (if score
9474                   (prefix-numeric-value score)
9475                 (or gnus-summary-default-score 0)))
9476   (save-excursion
9477     (set-buffer gnus-summary-buffer)
9478     (goto-char (point-min))
9479     (while (not (eobp))
9480       (and (< (gnus-summary-article-score) score)
9481            (gnus-summary-mark-article nil mark))
9482       (forward-line 1))))
9483
9484 (defun gnus-summary-kill-below (&optional score)
9485   "Mark articles with score below SCORE as read."
9486   (interactive "P")
9487   (gnus-set-global-variables)
9488   (gnus-summary-mark-below score gnus-killed-mark))
9489
9490 (defun gnus-summary-clear-above (&optional score)
9491   "Clear all marks from articles with score above SCORE."
9492   (interactive "P")
9493   (gnus-set-global-variables)
9494   (gnus-summary-mark-above score gnus-unread-mark))
9495
9496 (defun gnus-summary-tick-above (&optional score)
9497   "Tick all articles with score above SCORE."
9498   (interactive "P")
9499   (gnus-set-global-variables)
9500   (gnus-summary-mark-above score gnus-ticked-mark))
9501
9502 (defun gnus-summary-mark-above (score mark)
9503   "Mark articles with score over SCORE with MARK."
9504   (interactive "P\ncMark: ")
9505   (gnus-set-global-variables)
9506   (setq score (if score
9507                   (prefix-numeric-value score)
9508                 (or gnus-summary-default-score 0)))
9509   (save-excursion
9510     (set-buffer gnus-summary-buffer)
9511     (goto-char (point-min))
9512     (while (not (eobp))
9513       (if (> (gnus-summary-article-score) score)
9514           (progn
9515             (gnus-summary-mark-article nil mark)
9516             (forward-line 1))
9517         (forward-line 1)))))
9518
9519 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9520 (defun gnus-summary-show-all-expunged ()
9521   "Display all the hidden articles that were expunged for low scores."
9522   (interactive)
9523   (gnus-set-global-variables)
9524   (let ((buffer-read-only nil))
9525     (let ((scored gnus-newsgroup-scored)
9526           headers h)
9527       (while scored
9528         (or (gnus-summary-goto-subject (car (car scored)))
9529             (and (setq h (gnus-get-header-by-num (car (car scored))))
9530                  (< (cdr (car scored)) gnus-summary-expunge-below)
9531                  (setq headers (cons h headers))))
9532         (setq scored (cdr scored)))
9533       (or headers (error "No expunged articles hidden."))
9534       (goto-char (point-min))
9535       (save-excursion 
9536         (gnus-summary-update-lines 
9537          (point)
9538          (progn
9539            (gnus-summary-prepare-unthreaded (nreverse headers))
9540            (point)))))
9541     (goto-char (point-min))
9542     (gnus-summary-position-cursor)))
9543
9544 (defun gnus-summary-show-all-dormant ()
9545   "Display all the hidden articles that are marked as dormant."
9546   (interactive)
9547   (gnus-set-global-variables)
9548   (let ((buffer-read-only nil))
9549     (let ((dormant gnus-newsgroup-dormant)
9550           headers h)
9551       (while dormant
9552         (or (gnus-summary-goto-subject (car dormant))
9553             (and (setq h (gnus-get-header-by-num (car dormant)))
9554                  (setq headers (cons h headers))))
9555         (setq dormant (cdr dormant)))
9556       (or headers (error "No dormant articles hidden."))
9557       (goto-char (point-min))
9558       (save-excursion 
9559         (gnus-summary-update-lines 
9560          (point)
9561          (progn
9562            (gnus-summary-prepare-unthreaded (nreverse headers))
9563            (point)))))
9564     (goto-char (point-min))
9565     (gnus-summary-position-cursor)))
9566
9567 (defun gnus-summary-hide-all-dormant ()
9568   "Hide all dormant articles."
9569   (interactive)
9570   (gnus-set-global-variables)
9571   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9572   (gnus-summary-position-cursor))
9573
9574 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9575   "Mark all articles not marked as unread in this newsgroup as read.
9576 If prefix argument ALL is non-nil, all articles are marked as read.
9577 If QUIETLY is non-nil, no questions will be asked.
9578 If TO-HERE is non-nil, it should be a point in the buffer. All
9579 articles before this point will be marked as read.
9580 The number of articles marked as read is returned."
9581   (interactive "P")
9582   (gnus-set-global-variables)
9583   (prog1
9584       (if (or quietly
9585               (not gnus-interactive-catchup) ;Without confirmation?
9586               gnus-expert-user
9587               (gnus-y-or-n-p
9588                (if all
9589                    "Mark absolutely all articles as read? "
9590                  "Mark all unread articles as read? ")))
9591           (if (and not-mark 
9592                    (not gnus-newsgroup-adaptive)
9593                    (not gnus-newsgroup-auto-expire))
9594               (progn
9595                 (and all (setq gnus-newsgroup-marked nil
9596                                gnus-newsgroup-dormant nil))
9597                 (setq gnus-newsgroup-unreads 
9598                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9599             ;; We actually mark all articles as canceled, which we
9600             ;; have to do when using auto-expiry or adaptive scoring. 
9601             (let ((unreads (length gnus-newsgroup-unreads)))
9602               (gnus-summary-show-all-threads)
9603               (if (gnus-summary-first-subject (not all))
9604                   (while (and (if to-here (< (point) to-here) t)
9605                               (gnus-summary-mark-article-as-read
9606                                gnus-catchup-mark)
9607                               (gnus-summary-search-subject nil (not all)))))
9608               (- unreads (length gnus-newsgroup-unreads))
9609               (or to-here
9610                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9611     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9612       (if (and (not to-here) (eq 'nnvirtual (car method)))
9613           (nnvirtual-catchup-group
9614            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9615     (gnus-summary-position-cursor)))
9616
9617 (defun gnus-summary-catchup-to-here (&optional all)
9618   "Mark all unticked articles before the current one as read.
9619 If ALL is non-nil, also mark ticked and dormant articles as read."
9620   (interactive)
9621   (gnus-set-global-variables)
9622   (save-excursion
9623     (and (zerop (forward-line -1))
9624          (progn
9625            (end-of-line)
9626            (gnus-summary-catchup all t (point))
9627            (gnus-set-mode-line 'summary))))
9628   (gnus-summary-position-cursor))
9629
9630 (defun gnus-summary-catchup-all (&optional quietly)
9631   "Mark all articles in this newsgroup as read."
9632   (interactive)
9633   (gnus-set-global-variables)
9634   (gnus-summary-catchup t quietly))
9635
9636 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9637   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9638 If prefix argument ALL is non-nil, all articles are marked as read."
9639   (interactive "P")
9640   (gnus-set-global-variables)
9641   (gnus-summary-catchup all quietly nil 'fast)
9642   ;; Select next newsgroup or exit.
9643   (if (eq gnus-auto-select-next 'quietly)
9644       (gnus-summary-next-group nil)
9645     (gnus-summary-exit)))
9646
9647 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9648   "Mark all articles in this newsgroup as read, and then exit."
9649   (interactive)
9650   (gnus-set-global-variables)
9651   (gnus-summary-catchup-and-exit t quietly))
9652
9653 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9654 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9655   "Mark all articles in this group as read and select the next group.
9656 If given a prefix, mark all articles, unread as well as ticked, as
9657 read." 
9658   (interactive "P")
9659   (gnus-set-global-variables)
9660   (gnus-summary-catchup all)
9661   (gnus-summary-next-group))
9662
9663 ;; Thread-based commands.
9664
9665 (defun gnus-summary-toggle-threads (&optional arg)
9666   "Toggle showing conversation threads.
9667 If ARG is positive number, turn showing conversation threads on."
9668   (interactive "P")
9669   (gnus-set-global-variables)
9670   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9671     (setq gnus-show-threads
9672           (if (null arg) (not gnus-show-threads)
9673             (> (prefix-numeric-value arg) 0)))
9674     (gnus-summary-prepare)
9675     (gnus-summary-goto-subject current)
9676     (gnus-summary-position-cursor)))
9677
9678 (defun gnus-summary-show-all-threads ()
9679   "Show all threads."
9680   (interactive)
9681   (gnus-set-global-variables)
9682   (save-excursion
9683     (let ((buffer-read-only nil))
9684       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9685   (gnus-summary-position-cursor))
9686
9687 (defun gnus-summary-show-thread ()
9688   "Show thread subtrees.
9689 Returns nil if no thread was there to be shown."
9690   (interactive)
9691   (gnus-set-global-variables)
9692   (let ((buffer-read-only nil)
9693         (orig (point))
9694         ;; first goto end then to beg, to have point at beg after let
9695         (end (progn (end-of-line) (point)))
9696         (beg (progn (beginning-of-line) (point))))
9697     (prog1
9698         ;; Any hidden lines here?
9699         (search-forward "\r" end t)
9700       (subst-char-in-region beg end ?\^M ?\n t)
9701       (goto-char orig)
9702       (gnus-summary-position-cursor))))
9703
9704 (defun gnus-summary-hide-all-threads ()
9705   "Hide all thread subtrees."
9706   (interactive)
9707   (gnus-set-global-variables)
9708   (save-excursion
9709     (goto-char (point-min))
9710     (gnus-summary-hide-thread)
9711     (while (and (not (eobp)) (zerop (forward-line 1)))
9712       (gnus-summary-hide-thread)))
9713   (gnus-summary-position-cursor))
9714
9715 (defun gnus-summary-hide-thread ()
9716   "Hide thread subtrees.
9717 Returns nil if no threads were there to be hidden."
9718   (interactive)
9719   (gnus-set-global-variables)
9720   (let ((buffer-read-only nil)
9721         (start (point))
9722         (level (gnus-summary-thread-level))
9723         (end (point)))
9724     ;; Go forward until either the buffer ends or the subthread
9725     ;; ends. 
9726     (if (eobp)
9727         ()
9728       (while (and (zerop (forward-line 1))
9729                   (> (gnus-summary-thread-level) level))
9730         (setq end (point)))
9731       (prog1
9732           (save-excursion
9733             (goto-char end)
9734             (search-backward "\n" start t))
9735         (subst-char-in-region start end ?\n ?\^M t)
9736         (forward-line -1)))))
9737
9738 (defun gnus-summary-go-to-next-thread (&optional previous)
9739   "Go to the same level (or less) next thread.
9740 If PREVIOUS is non-nil, go to previous thread instead.
9741 Return the article number moved to, or nil if moving was impossible."
9742   (let ((level (gnus-summary-thread-level))
9743         (article (gnus-summary-article-number)))
9744     (if previous 
9745         (while (and (zerop (forward-line -1))
9746                     (> (gnus-summary-thread-level) level)))
9747       (while (and (save-excursion
9748                     (forward-line 1)
9749                     (not (eobp)))
9750                   (zerop (forward-line 1))
9751                   (> (gnus-summary-thread-level) level))))
9752     (gnus-summary-recenter)
9753     (gnus-summary-position-cursor)
9754     (let ((oart (gnus-summary-article-number)))
9755       (and (/= oart article) oart))))
9756
9757 (defun gnus-summary-next-thread (n)
9758   "Go to the same level next N'th thread.
9759 If N is negative, search backward instead.
9760 Returns the difference between N and the number of skips actually
9761 done."
9762   (interactive "p")
9763   (gnus-set-global-variables)
9764   (let ((backward (< n 0))
9765         (n (abs n)))
9766   (while (and (> n 0)
9767               (gnus-summary-go-to-next-thread backward))
9768     (setq n (1- n)))
9769   (gnus-summary-position-cursor)
9770   (if (/= 0 n) (gnus-message 7 "No more threads"))
9771   n))
9772
9773 (defun gnus-summary-prev-thread (n)
9774   "Go to the same level previous N'th thread.
9775 Returns the difference between N and the number of skips actually
9776 done."
9777   (interactive "p")
9778   (gnus-set-global-variables)
9779   (gnus-summary-next-thread (- n)))
9780
9781 (defun gnus-summary-go-down-thread (&optional same)
9782   "Go down one level in the current thread.
9783 If SAME is non-nil, also move to articles of the same level."
9784   (let ((level (gnus-summary-thread-level))
9785         (start (point)))
9786     (if (and (zerop (forward-line 1))
9787              (> (gnus-summary-thread-level) level))
9788         t
9789       (goto-char start)
9790       nil)))
9791
9792 (defun gnus-summary-go-up-thread ()
9793   "Go up one level in the current thread."
9794   (let ((level (gnus-summary-thread-level))
9795         (start (point)))
9796     (while (and (zerop (forward-line -1))
9797                 (>= (gnus-summary-thread-level) level)))
9798     (if (>= (gnus-summary-thread-level) level)
9799         (progn
9800           (goto-char start)
9801           nil)
9802       t)))
9803
9804 (defun gnus-summary-down-thread (n)
9805   "Go down thread N steps.
9806 If N is negative, go up instead.
9807 Returns the difference between N and how many steps down that were
9808 taken."
9809   (interactive "p")
9810   (gnus-set-global-variables)
9811   (let ((up (< n 0))
9812         (n (abs n)))
9813   (while (and (> n 0)
9814               (if up (gnus-summary-go-up-thread)
9815                 (gnus-summary-go-down-thread)))
9816     (setq n (1- n)))
9817   (gnus-summary-position-cursor)
9818   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9819   n))
9820
9821 (defun gnus-summary-up-thread (n)
9822   "Go up thread N steps.
9823 If N is negative, go up instead.
9824 Returns the difference between N and how many steps down that were
9825 taken."
9826   (interactive "p")
9827   (gnus-set-global-variables)
9828   (gnus-summary-down-thread (- n)))
9829
9830 (defun gnus-summary-kill-thread (&optional unmark)
9831   "Mark articles under current thread as read.
9832 If the prefix argument is positive, remove any kinds of marks.
9833 If the prefix argument is negative, tick articles instead."
9834   (interactive "P")
9835   (gnus-set-global-variables)
9836   (if unmark
9837       (setq unmark (prefix-numeric-value unmark)))
9838   (let ((killing t)
9839         (level (gnus-summary-thread-level)))
9840     (save-excursion
9841       (while killing
9842         ;; Mark the article...
9843         (cond ((null unmark) (gnus-summary-mark-article-as-read
9844                                gnus-killed-mark))
9845               ((> unmark 0) (gnus-summary-mark-article-as-unread 
9846                              gnus-unread-mark))
9847               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9848         ;; ...and go forward until either the buffer ends or the subtree
9849         ;; ends. 
9850         (if (not (and (zerop (forward-line 1))
9851                       (> (gnus-summary-thread-level) level)))
9852             (setq killing nil))))
9853     ;; Hide killed subtrees.
9854     (and (null unmark)
9855          gnus-thread-hide-killed
9856          (gnus-summary-hide-thread))
9857     ;; If marked as read, go to next unread subject.
9858     (if (null unmark)
9859         ;; Go to next unread subject.
9860         (gnus-summary-next-subject 1 t)))
9861   (gnus-set-mode-line 'summary))
9862
9863 ;; Summary sorting commands
9864
9865 (defun gnus-summary-sort-by-number (&optional reverse)
9866   "Sort summary buffer by article number.
9867 Argument REVERSE means reverse order."
9868   (interactive "P")
9869   (gnus-set-global-variables)
9870   (gnus-summary-sort 
9871    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
9872    ;; a function, so we wrap it.
9873    (cons (lambda () (gnus-summary-article-number))
9874          'gnus-thread-sort-by-number) reverse))
9875
9876 (defun gnus-summary-sort-by-author (&optional reverse)
9877   "Sort summary buffer by author name alphabetically.
9878 If case-fold-search is non-nil, case of letters is ignored.
9879 Argument REVERSE means reverse order."
9880   (interactive "P")
9881   (gnus-set-global-variables)
9882   (gnus-summary-sort
9883    (cons
9884     (lambda ()
9885       (let ((extract (funcall
9886                       gnus-extract-address-components
9887                       (header-from (gnus-get-header-by-num
9888                                     (gnus-summary-article-number))))))
9889         (or (car extract) (cdr extract))))
9890     'gnus-thread-sort-by-author)
9891    reverse))
9892
9893 (defun gnus-summary-sort-by-subject (&optional reverse)
9894   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9895 If case-fold-search is non-nil, case of letters is ignored.
9896 Argument REVERSE means reverse order."
9897   (interactive "P")
9898   (gnus-set-global-variables)
9899   (gnus-summary-sort
9900    (cons
9901     (lambda ()
9902       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9903     'gnus-thread-sort-by-subject)
9904    reverse))
9905
9906 (defun gnus-summary-sort-by-date (&optional reverse)
9907   "Sort summary buffer by date.
9908 Argument REVERSE means reverse order."
9909   (interactive "P")
9910   (gnus-set-global-variables)
9911   (gnus-summary-sort
9912    (cons
9913     (lambda ()
9914       (gnus-sortable-date
9915        (header-date 
9916         (gnus-get-header-by-num (gnus-summary-article-number)))))
9917     'gnus-thread-sort-by-date)
9918    reverse))
9919
9920 (defun gnus-summary-sort-by-score (&optional reverse)
9921   "Sort summary buffer by score.
9922 Argument REVERSE means reverse order."
9923   (interactive "P")
9924   (gnus-set-global-variables)
9925   (gnus-summary-sort 
9926    (cons (lambda () (gnus-summary-article-score))
9927          'gnus-thread-sort-by-score)
9928    (not reverse)))
9929
9930 (defvar gnus-summary-already-sorted nil)
9931 (defun gnus-summary-sort (predicate reverse)
9932   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9933   (if gnus-summary-already-sorted
9934       ()
9935     (let (buffer-read-only)
9936       (if (not gnus-show-threads)
9937           ;; We do untreaded sorting...
9938           (progn
9939             (goto-char (point-min))
9940             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9941         ;; ... or we do threaded sorting.
9942         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9943               (gnus-summary-prepare-hook nil)
9944               (gnus-summary-already-sorted nil))
9945           ;; We do that by simply regenerating the threads.
9946           (gnus-summary-prepare)
9947           (and gnus-show-threads
9948                gnus-thread-hide-subtree
9949                (gnus-summary-hide-all-threads))
9950           ;; If in async mode, we send some info to the backend.
9951           (and gnus-newsgroup-async
9952                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9953                (gnus-request-asynchronous 
9954                 gnus-newsgroup-name
9955                 (if (and gnus-asynchronous-article-function
9956                          (fboundp gnus-asynchronous-article-function))
9957                     (funcall gnus-asynchronous-article-function
9958                              gnus-newsgroup-threads)))))))))
9959
9960   
9961 (defun gnus-sortable-date (date)
9962   "Make sortable string by string-lessp from DATE.
9963 Timezone package is used."
9964   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
9965          (year (aref date 0))
9966          (month (aref date 1))
9967          (day (aref date 2)))
9968     (timezone-make-sortable-date 
9969      year month day 
9970      (timezone-make-time-string
9971       (aref date 3) (aref date 4) (aref date 5)))))
9972
9973
9974 ;; Summary saving commands.
9975
9976 (defun gnus-summary-save-article (&optional n)
9977   "Save the current article using the default saver function.
9978 If N is a positive number, save the N next articles.
9979 If N is a negative number, save the N previous articles.
9980 If N is nil and any articles have been marked with the process mark,
9981 save those articles instead.
9982 The variable `gnus-default-article-saver' specifies the saver function."
9983   (interactive "P")
9984   (gnus-set-global-variables)
9985   (let ((articles (gnus-summary-work-articles n)))
9986     (while articles
9987       (let ((header (gnus-get-header-by-num (car articles))))
9988         (if (vectorp header)
9989             (progn
9990               (save-window-excursion
9991                 (gnus-summary-select-article t nil nil (car articles)))
9992               (or gnus-save-all-headers
9993                   (gnus-article-hide-headers t))
9994               ;; Remove any X-Gnus lines.
9995               (save-excursion
9996                 (save-restriction
9997                   (set-buffer gnus-article-buffer)
9998                   (let ((buffer-read-only nil))
9999                     (goto-char (point-min))
10000                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10001                                                   (point-max)))
10002                     (while (re-search-forward "^X-Gnus" nil t)
10003                       (beginning-of-line)
10004                       (delete-region (point)
10005                                      (progn (forward-line 1) (point))))
10006                     (widen))))
10007               (save-window-excursion
10008                 (if gnus-default-article-saver
10009                     (funcall gnus-default-article-saver)
10010                   (error "No default saver is defined."))))
10011           (if (assq 'name header)
10012               (gnus-copy-file (cdr (assq 'name header)))
10013             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10014       (gnus-summary-remove-process-mark (car articles))
10015       (setq articles (cdr articles)))
10016     (gnus-summary-position-cursor)
10017     n))
10018
10019 (defun gnus-summary-pipe-output (&optional arg)
10020   "Pipe the current article to a subprocess.
10021 If N is a positive number, pipe the N next articles.
10022 If N is a negative number, pipe the N previous articles.
10023 If N is nil and any articles have been marked with the process mark,
10024 pipe those articles instead."
10025   (interactive "P")
10026   (gnus-set-global-variables)
10027   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10028     (gnus-summary-save-article arg)))
10029
10030 (defun gnus-summary-save-article-mail (&optional arg)
10031   "Append the current article to an mail file.
10032 If N is a positive number, save the N next articles.
10033 If N is a negative number, save the N previous articles.
10034 If N is nil and any articles have been marked with the process mark,
10035 save those articles instead."
10036   (interactive "P")
10037   (gnus-set-global-variables)
10038   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10039     (gnus-summary-save-article arg)))
10040
10041 (defun gnus-summary-save-article-rmail (&optional arg)
10042   "Append the current article to an rmail file.
10043 If N is a positive number, save the N next articles.
10044 If N is a negative number, save the N previous articles.
10045 If N is nil and any articles have been marked with the process mark,
10046 save those articles instead."
10047   (interactive "P")
10048   (gnus-set-global-variables)
10049   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10050     (gnus-summary-save-article arg)))
10051
10052 (defun gnus-summary-save-article-file (&optional arg)
10053   "Append the current article to a file.
10054 If N is a positive number, save the N next articles.
10055 If N is a negative number, save the N previous articles.
10056 If N is nil and any articles have been marked with the process mark,
10057 save those articles instead."
10058   (interactive "P")
10059   (gnus-set-global-variables)
10060   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10061     (gnus-summary-save-article arg)))
10062
10063 (defun gnus-read-save-file-name (prompt default-name)
10064   (let ((methods gnus-split-methods)
10065         split-name)
10066     (if (not gnus-split-methods)
10067         ()
10068       (save-excursion
10069         (set-buffer gnus-article-buffer)
10070         (gnus-narrow-to-headers)
10071         (while methods
10072           (goto-char (point-min))
10073           (and (condition-case () 
10074                    (re-search-forward (car (car methods)) nil t)
10075                  (error nil))
10076                (setq split-name (cons (nth 1 (car methods)) split-name)))
10077           (setq methods (cdr methods)))
10078         (widen)))
10079     (cond ((null split-name)
10080            (read-file-name
10081             (concat prompt " (default "
10082                     (file-name-nondirectory default-name) ") ")
10083             (file-name-directory default-name)
10084             default-name))
10085           ((= 1 (length split-name))
10086            (read-file-name
10087             (concat prompt " (default " (car split-name) ") ")
10088             gnus-article-save-directory
10089             (concat gnus-article-save-directory (car split-name))))
10090           (t
10091            (setq split-name (mapcar (lambda (el) (list el))
10092                                     (nreverse split-name)))
10093            (let ((result (completing-read 
10094                           (concat prompt " ")
10095                           split-name nil nil)))
10096              (concat gnus-article-save-directory
10097                      (if (string= result "")
10098                          (car (car split-name))
10099                        result)))))))
10100
10101 (defun gnus-summary-save-in-rmail (&optional filename)
10102   "Append this article to Rmail file.
10103 Optional argument FILENAME specifies file name.
10104 Directory to save to is default to `gnus-article-save-directory' which
10105 is initialized from the SAVEDIR environment variable."
10106   (interactive)
10107   (gnus-set-global-variables)
10108   (let ((default-name
10109           (funcall gnus-rmail-save-name gnus-newsgroup-name
10110                    gnus-current-headers gnus-newsgroup-last-rmail)))
10111     (or filename
10112         (setq filename (gnus-read-save-file-name 
10113                         "Save in rmail file:" default-name)))
10114     (gnus-make-directory (file-name-directory filename))
10115     (gnus-eval-in-buffer-window 
10116      gnus-article-buffer
10117      (save-excursion
10118        (save-restriction
10119          (widen)
10120          (gnus-output-to-rmail filename))))
10121     ;; Remember the directory name to save articles
10122     (setq gnus-newsgroup-last-rmail filename)))
10123
10124 (defun gnus-summary-save-in-mail (&optional filename)
10125   "Append this article to Unix mail file.
10126 Optional argument FILENAME specifies file name.
10127 Directory to save to is default to `gnus-article-save-directory' which
10128 is initialized from the SAVEDIR environment variable."
10129   (interactive)
10130   (gnus-set-global-variables)
10131   (let ((default-name
10132           (funcall gnus-mail-save-name gnus-newsgroup-name
10133                    gnus-current-headers gnus-newsgroup-last-mail)))
10134     (or filename
10135         (setq filename (gnus-read-save-file-name 
10136                         "Save in Unix mail file:" default-name)))
10137     (setq filename
10138           (expand-file-name filename
10139                             (and default-name
10140                                  (file-name-directory default-name))))
10141     (gnus-make-directory (file-name-directory filename))
10142     (gnus-eval-in-buffer-window 
10143      gnus-article-buffer
10144      (save-excursion
10145        (save-restriction
10146          (widen)
10147          (if (and (file-readable-p filename) (rmail-file-p filename))
10148              (gnus-output-to-rmail filename)
10149            (rmail-output filename 1 t t)))))
10150     ;; Remember the directory name to save articles.
10151     (setq gnus-newsgroup-last-mail filename)))
10152
10153 (defun gnus-summary-save-in-file (&optional filename)
10154   "Append this article to file.
10155 Optional argument FILENAME specifies file name.
10156 Directory to save to is default to `gnus-article-save-directory' which
10157 is initialized from the SAVEDIR environment variable."
10158   (interactive)
10159   (gnus-set-global-variables)
10160   (let ((default-name
10161           (funcall gnus-file-save-name gnus-newsgroup-name
10162                    gnus-current-headers gnus-newsgroup-last-file)))
10163     (or filename
10164         (setq filename (gnus-read-save-file-name 
10165                         "Save in file:" default-name)))
10166     (gnus-make-directory (file-name-directory filename))
10167     (gnus-eval-in-buffer-window 
10168      gnus-article-buffer
10169      (save-excursion
10170        (save-restriction
10171          (widen)
10172          (gnus-output-to-file filename))))
10173     ;; Remember the directory name to save articles.
10174     (setq gnus-newsgroup-last-file filename)))
10175
10176 (defun gnus-summary-save-in-pipe (&optional command)
10177   "Pipe this article to subprocess."
10178   (interactive)
10179   (gnus-set-global-variables)
10180   (let ((command (read-string "Shell command on article: "
10181                               gnus-last-shell-command)))
10182     (if (string-equal command "")
10183         (setq command gnus-last-shell-command))
10184     (gnus-eval-in-buffer-window 
10185      gnus-article-buffer
10186      (save-restriction
10187        (widen)
10188        (shell-command-on-region (point-min) (point-max) command nil)))
10189     (setq gnus-last-shell-command command)))
10190
10191 ;; Summary extract commands
10192
10193 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10194   (let ((buffer-read-only nil)
10195         (article (gnus-summary-article-number))
10196         b)
10197     (or (gnus-summary-goto-subject article)
10198         (error (format "No such article: %d" article)))
10199     (or gnus-newsgroup-headers-hashtb-by-number
10200         (gnus-make-headers-hashtable-by-number))
10201     (gnus-summary-position-cursor)
10202     ;; If all commands are to be bunched up on one line, we collect
10203     ;; them here.  
10204     (if gnus-view-pseudos-separately
10205         ()
10206       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10207             files action)
10208         (while ps
10209           (setq action (cdr (assq 'action (car ps))))
10210           (setq files (list (cdr (assq 'name (car ps)))))
10211           (while (and ps (cdr ps)
10212                       (string= (or action "1")
10213                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10214             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10215             (setcdr ps (cdr (cdr ps))))
10216           (if (not files)
10217               ()
10218             (if (not (string-match "%s" action))
10219                 (setq files (cons " " files)))
10220             (setq files (cons " " files))
10221             (and (assq 'execute (car ps))
10222                  (setcdr (assq 'execute (car ps))
10223                          (funcall (if (string-match "%s" action)
10224                                       'format 'concat)
10225                                   action 
10226                                   (mapconcat (lambda (f) f) files " ")))))
10227           (setq ps (cdr ps)))))
10228     (if (and gnus-view-pseudos (not not-view))
10229         (while pslist
10230           (and (assq 'execute (car pslist))
10231                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10232                                      (eq gnus-view-pseudos 'not-confirm)))
10233           (setq pslist (cdr pslist)))
10234       (save-excursion
10235         (while pslist
10236           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10237                                          (gnus-summary-article-number)))
10238           (forward-line 1)
10239           (setq b (point))
10240           (insert "          " (file-name-nondirectory 
10241                                 (cdr (assq 'name (car pslist))))
10242                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10243           (add-text-properties 
10244            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10245                           'gnus-mark gnus-unread-mark 
10246                           'gnus-level 0
10247                           'gnus-pseudo (car pslist)))
10248           (forward-line -1)
10249           (gnus-sethash (int-to-string gnus-reffed-article-number)
10250                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10251           (setq gnus-newsgroup-unreads
10252                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10253           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10254           (setq pslist (cdr pslist)))))))
10255
10256 (defun gnus-pseudos< (p1 p2)
10257   (let ((c1 (cdr (assq 'action p1)))
10258         (c2 (cdr (assq 'action p2))))
10259     (and c1 c2 (string< c1 c2))))
10260
10261 (defun gnus-request-pseudo-article (props)
10262   (cond ((assq 'execute props)
10263          (gnus-execute-command (cdr (assq 'execute props)))))
10264   (let ((gnus-current-article (gnus-summary-article-number)))
10265     (run-hooks 'gnus-mark-article-hook)))
10266
10267 (defun gnus-execute-command (command &optional automatic)
10268   (save-excursion
10269     (gnus-article-setup-buffer)
10270     (set-buffer gnus-article-buffer)
10271     (let ((command (if automatic command (read-string "Command: " command)))
10272           (buffer-read-only nil))
10273       (erase-buffer)
10274       (insert "$ " command "\n\n")
10275       (if gnus-view-pseudo-asynchronously
10276           (start-process "gnus-execute" nil "sh" "-c" command)
10277         (call-process "sh" nil t nil "-c" command)))))
10278
10279 (defun gnus-copy-file (file &optional to)
10280   "Copy FILE to TO."
10281   (interactive
10282    (list (read-file-name "Copy file: " default-directory)
10283          (read-file-name "Copy file to: " default-directory)))
10284   (gnus-set-global-variables)
10285   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10286   (and (file-directory-p to) 
10287        (setq to (concat (file-name-as-directory to)
10288                         (file-name-nondirectory file))))
10289   (copy-file file to))
10290
10291 ;; Summary kill commands.
10292
10293 (defun gnus-summary-edit-global-kill (article)
10294   "Edit the \"global\" kill file."
10295   (interactive (list (gnus-summary-article-number)))
10296   (gnus-set-global-variables)
10297   (gnus-group-edit-global-kill article))
10298
10299 (defun gnus-summary-edit-local-kill ()
10300   "Edit a local kill file applied to the current newsgroup."
10301   (interactive)
10302   (gnus-set-global-variables)
10303   (setq gnus-current-headers 
10304         (gnus-gethash 
10305          (int-to-string (gnus-summary-article-number))
10306          gnus-newsgroup-headers-hashtb-by-number))
10307   (gnus-set-global-variables)
10308   (gnus-group-edit-local-kill 
10309    (gnus-summary-article-number) gnus-newsgroup-name))
10310
10311 \f
10312 ;;;
10313 ;;; Gnus article mode
10314 ;;;
10315
10316 (put 'gnus-article-mode 'mode-class 'special)
10317
10318 (if gnus-article-mode-map
10319     nil
10320   (setq gnus-article-mode-map (make-keymap))
10321   (suppress-keymap gnus-article-mode-map)
10322   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10323   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10324   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10325   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10326   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10327   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10328   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10329   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10330   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10331   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10332   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10333   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10334   
10335   ;; Duplicate almost all summary keystrokes in the article mode map.
10336   (let ((commands 
10337          (list 
10338           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10339           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10340           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10341           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10342           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10343           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10344           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10345           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10346           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10347           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10348           "\C-c\C-v\C-v" "\C-d" "v" 
10349 ;;        "Mt" "M!" "Md" "Mr"
10350 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10351 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10352 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10353 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10354 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10355 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10356 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10357 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10358 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10359 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10360 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10361           )))
10362     (while commands
10363       (define-key gnus-article-mode-map (car commands) 
10364         'gnus-article-summary-command)
10365       (setq commands (cdr commands))))
10366
10367   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10368 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10369                          "=" "n")))
10370     (while commands
10371       (define-key gnus-article-mode-map (car commands) 
10372         'gnus-article-summary-command-nosave)
10373       (setq commands (cdr commands)))))
10374
10375
10376 (defun gnus-article-mode ()
10377   "Major mode for displaying an article.
10378
10379 All normal editing commands are switched off.
10380
10381 The following commands are available:
10382
10383 \\<gnus-article-mode-map>
10384 \\[gnus-article-next-page]\t Scroll the article one page forwards
10385 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10386 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10387 \\[gnus-article-show-summary]\t Display the summary buffer
10388 \\[gnus-article-mail]\t Send a reply to the address near point
10389 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10390 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10391 \\[gnus-info-find-node]\t Go to the Gnus info node"
10392   (interactive)
10393   (if gnus-visual (gnus-article-make-menu-bar))
10394   (kill-all-local-variables)
10395   (setq mode-line-modified "-- ")
10396   (make-local-variable 'mode-line-format)
10397   (setq mode-line-format (copy-sequence mode-line-format))
10398   (and (equal (nth 3 mode-line-format) "   ")
10399        (setcar (nthcdr 3 mode-line-format) ""))
10400   (setq mode-name "Article")
10401   (setq major-mode 'gnus-article-mode)
10402   (make-local-variable 'minor-mode-alist)
10403   (or (assq 'gnus-show-mime minor-mode-alist)
10404       (setq minor-mode-alist
10405             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10406   (use-local-map gnus-article-mode-map)
10407   (make-local-variable 'page-delimiter)
10408   (setq page-delimiter gnus-page-delimiter)
10409   (buffer-disable-undo (current-buffer))
10410   (setq buffer-read-only t)             ;Disable modification
10411   (run-hooks 'gnus-article-mode-hook))
10412
10413 (defun gnus-article-setup-buffer ()
10414   "Initialize article mode buffer."
10415   (if (get-buffer gnus-article-buffer)
10416       (save-excursion
10417         (set-buffer gnus-article-buffer)
10418         (buffer-disable-undo (current-buffer))
10419         (setq buffer-read-only t)
10420         (gnus-add-current-to-buffer-list)
10421         (or (eq major-mode 'gnus-article-mode)
10422             (gnus-article-mode)))
10423     (save-excursion
10424       (set-buffer (get-buffer-create gnus-article-buffer))
10425       (gnus-add-current-to-buffer-list)
10426       (gnus-article-mode))))
10427
10428 ;; Set article window start at LINE, where LINE is the number of lines
10429 ;; from the head of the article.
10430 (defun gnus-article-set-window-start (&optional line)
10431   (set-window-start 
10432    (get-buffer-window gnus-article-buffer)
10433    (save-excursion
10434      (set-buffer gnus-article-buffer)
10435      (goto-char (point-min))
10436      (if (not line)
10437          (point-min)
10438        (gnus-message 6 "Moved to bookmark")
10439        (search-forward "\n\n" nil t)
10440        (forward-line line)
10441        (point)))))
10442
10443 (defun gnus-request-article-this-buffer (article group)
10444   "Get an article and insert it into this buffer."
10445   (setq group (or group gnus-newsgroup-name))
10446
10447   ;; Open server if it has closed.
10448   (gnus-check-news-server (gnus-find-method-for-group group))
10449
10450   ;; Using `gnus-request-article' directly will insert the article into
10451   ;; `nntp-server-buffer' - so we'll save some time by not having to
10452   ;; copy it from the server buffer into the article buffer.
10453
10454   ;; We only request an article by message-id when we do not have the
10455   ;; headers for it, so we'll have to get those.
10456   (and (stringp article) 
10457        (let ((gnus-override-method gnus-refer-article-method))
10458          (gnus-read-header article)))
10459
10460   ;; If the article number is negative, that means that this article
10461   ;; doesn't belong in this newsgroup (possibly), so we find its
10462   ;; message-id and request it by id instead of number.
10463   (if (not (numberp article))
10464       ()
10465     (save-excursion
10466       (set-buffer gnus-summary-buffer)
10467       (let ((header (gnus-get-header-by-num article)))
10468         (if (< article 0)
10469             (if (vectorp header)
10470                 ;; It's a real article.
10471                 (setq article (header-id header))
10472               ;; It is an extracted pseudo-article.
10473               (setq article 'pseudo)
10474               (gnus-request-pseudo-article header)))
10475
10476         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10477           (if (not (eq (car method) 'nneething))
10478               ()
10479             (let ((dir (concat (file-name-as-directory (nth 1 method))
10480                                (header-subject header))))
10481               (if (file-directory-p dir)
10482                   (progn
10483                     (setq article 'nneething)
10484                     (gnus-group-enter-directory dir)))))))))
10485
10486   ;; Check the cache.
10487   (if (and gnus-use-cache
10488            (numberp article)
10489            (gnus-cache-request-article article group))
10490       'article
10491     ;; Get the article and into the article buffer.
10492     (if (or (stringp article) (numberp article))
10493         (progn
10494           (erase-buffer)
10495           (let ((gnus-override-method 
10496                  (and (stringp article) gnus-refer-article-method)))
10497             (and (gnus-request-article article group (current-buffer))
10498                  'article)))
10499       article)))
10500
10501 (defun gnus-read-header (id)
10502   "Read the headers of article ID and enter them into the Gnus system."
10503   (or gnus-newsgroup-headers-hashtb-by-number
10504       (gnus-make-headers-hashtable-by-number))
10505   (let (header)
10506     (if (not (setq header 
10507                    (car (if (let ((gnus-nov-is-evil t))
10508                               (gnus-retrieve-headers 
10509                                (list id) gnus-newsgroup-name))
10510                             (gnus-get-newsgroup-headers)))))
10511         nil
10512       (if (stringp id)
10513           (header-set-number header gnus-reffed-article-number))
10514       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10515       (gnus-sethash (int-to-string (header-number header)) header
10516                     gnus-newsgroup-headers-hashtb-by-number)
10517       (if (stringp id)
10518           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10519       (setq gnus-current-headers header)
10520       header)))
10521
10522 (defun gnus-article-prepare (article &optional all-headers header)
10523   "Prepare ARTICLE in article mode buffer.
10524 ARTICLE should either be an article number or a Message-ID.
10525 If ARTICLE is an id, HEADER should be the article headers.
10526 If ALL-HEADERS is non-nil, no headers are hidden."
10527   (save-excursion
10528     ;; Make sure we start in a summary buffer.
10529     (or (eq major-mode 'gnus-summary-mode)
10530         (set-buffer gnus-summary-buffer))
10531     (setq gnus-summary-buffer (current-buffer))
10532     ;; Make sure the connection to the server is alive.
10533     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10534         (progn
10535           (gnus-check-news-server 
10536            (gnus-find-method-for-group gnus-newsgroup-name))
10537           (gnus-request-group gnus-newsgroup-name t)))
10538     (or gnus-newsgroup-headers-hashtb-by-number
10539         (gnus-make-headers-hashtable-by-number))
10540     (let* ((article (if header (header-number header) article))
10541            (summary-buffer (current-buffer))
10542            (internal-hook gnus-article-internal-prepare-hook)
10543            (group gnus-newsgroup-name)
10544            result)
10545       (save-excursion
10546         (gnus-article-setup-buffer)
10547         (set-buffer gnus-article-buffer)
10548         (if (not (setq result (let ((buffer-read-only nil))
10549                                 (gnus-request-article-this-buffer 
10550                                  article group))))
10551             ;; There is no such article.
10552             (save-excursion
10553               (if (not (numberp article))
10554                   ()
10555                 (setq gnus-article-current 
10556                       (cons gnus-newsgroup-name article))
10557                 (set-buffer gnus-summary-buffer)
10558                 (setq gnus-current-article article)
10559                 (gnus-summary-mark-article article gnus-canceled-mark))
10560               (gnus-message 1 "No such article (may be canceled)")
10561               (ding)
10562               nil)
10563           (if (or (eq result 'pseudo) (eq result 'nneething))
10564               (progn
10565                 (save-excursion
10566                   (set-buffer summary-buffer)
10567                   (setq gnus-last-article gnus-current-article
10568                         gnus-newsgroup-history (cons gnus-current-article
10569                                                      gnus-newsgroup-history)
10570                         gnus-current-article 0
10571                         gnus-current-headers nil
10572                         gnus-article-current nil)
10573                   (if (eq result 'nneething)
10574                       (gnus-configure-windows 'summary)
10575                     (gnus-configure-windows 'article))
10576                   (gnus-set-global-variables))
10577                 (gnus-set-mode-line 'article))
10578             ;; The result from the `request' was an actual article -
10579             ;; or at least some text that is now displayed in the
10580             ;; article buffer.
10581             (if (and (numberp article)
10582                      (not (eq article gnus-current-article)))
10583                 ;; Seems like a new article has been selected.
10584                 ;; `gnus-current-article' must be an article number.
10585                 (save-excursion
10586                   (set-buffer summary-buffer)
10587                   (setq gnus-last-article gnus-current-article
10588                         gnus-newsgroup-history (cons gnus-current-article
10589                                                      gnus-newsgroup-history)
10590                         gnus-current-article article
10591                         gnus-current-headers 
10592                         (gnus-get-header-by-num gnus-current-article)
10593                         gnus-article-current 
10594                         (cons gnus-newsgroup-name gnus-current-article))
10595                   (gnus-summary-show-thread)
10596                   (run-hooks 'gnus-mark-article-hook)
10597                   (gnus-set-mode-line 'summary)
10598                   (and gnus-visual 
10599                        (run-hooks 'gnus-visual-mark-article-hook))
10600                   ;; Set the global newsgroup variables here.
10601                   ;; Suggested by Jim Sisolak
10602                   ;; <sisolak@trans4.neep.wisc.edu>.
10603                   (gnus-set-global-variables)
10604                   (setq gnus-have-all-headers 
10605                         (or all-headers gnus-show-all-headers))
10606                   (and gnus-use-cache 
10607                        (gnus-cache-possibly-enter-article
10608                         group article
10609                         (gnus-get-header-by-number article)
10610                         (memq article gnus-newsgroup-marked)
10611                         (memq article gnus-newsgroup-dormant)
10612                         (memq article gnus-newsgroup-unreads)))))
10613             ;; Hooks for getting information from the article.
10614             ;; This hook must be called before being narrowed.
10615             (let (buffer-read-only)
10616               (run-hooks 'internal-hook)
10617               (run-hooks 'gnus-article-prepare-hook)
10618               ;; Decode MIME message.
10619               (if (and gnus-show-mime
10620                        (or (not gnus-strict-mime)
10621                            (gnus-fetch-field "Mime-Version")))
10622                   (funcall gnus-show-mime-method))
10623               ;; Perform the article display hooks.
10624               (run-hooks 'gnus-article-display-hook))
10625             ;; Do page break.
10626             (goto-char (point-min))
10627             (and gnus-break-pages (gnus-narrow-to-page))
10628             (gnus-set-mode-line 'article)
10629             (gnus-configure-windows 'article)
10630             (goto-char (point-min))
10631             t))))))
10632
10633 (defun gnus-article-show-all-headers ()
10634   "Show all article headers in article mode buffer."
10635   (save-excursion 
10636     (gnus-article-setup-buffer)
10637     (set-buffer gnus-article-buffer)
10638     (let ((buffer-read-only nil))
10639       (remove-text-properties (point-min) (point-max) 
10640                               gnus-hidden-properties))))
10641
10642 (defun gnus-article-hide-headers-if-wanted ()
10643   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10644 Provided for backwards compatability."
10645   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10646       (gnus-article-hide-headers)))
10647
10648 (defun gnus-article-hide-headers (&optional delete)
10649   "Hide unwanted headers and possibly sort them as well."
10650   (interactive "P")
10651   (save-excursion
10652     (set-buffer gnus-article-buffer)
10653     (save-restriction
10654       (let ((sorted gnus-sorted-header-list)
10655             (buffer-read-only nil)
10656             want-list beg want-l)
10657         ;; First we narrow to just the headers.
10658         (widen)
10659         (goto-char (point-min))
10660         ;; Hide any "From " lines at the beginning of (mail) articles. 
10661         (while (looking-at "From ")
10662           (forward-line 1))
10663         (if (bobp) 
10664             (add-text-properties (point-min) (point) gnus-hidden-properties))
10665         ;; Then treat the rest of the header lines.
10666         (narrow-to-region 
10667          (point) 
10668          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10669         ;; Then we use the two regular expressions
10670         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10671         ;; select which header lines is to remain visible in the
10672         ;; article buffer.
10673         (goto-char (point-min))
10674         (while (re-search-forward "^[^ \t]*:" nil t)
10675           (beginning-of-line)
10676           ;; We add the headers we want to keep to a list and delete
10677           ;; them from the buffer.
10678           (if (or (and (stringp gnus-visible-headers)
10679                        (looking-at gnus-visible-headers))
10680                   (and (not (stringp gnus-visible-headers))
10681                        (stringp gnus-ignored-headers)
10682                        (not (looking-at gnus-ignored-headers))))
10683               (progn
10684                 (setq beg (point))
10685                 (forward-line 1)
10686                 ;; Be sure to get multi-line headers...
10687                 (re-search-forward "^[^ \t]*:" nil t)
10688                 (beginning-of-line)
10689                 (setq want-list 
10690                       (cons (buffer-substring beg (point)) want-list))
10691                 (delete-region beg (point))
10692                 (goto-char beg))
10693             (forward-line 1)))
10694         ;; Next we perform the sorting by looking at
10695         ;; `gnus-sorted-header-list'. 
10696         (goto-char (point-min))
10697         (while (and sorted want-list)
10698           (setq want-l want-list)
10699           (while (and want-l
10700                       (not (string-match (car sorted) (car want-l))))
10701             (setq want-l (cdr want-l)))
10702           (if want-l 
10703               (progn
10704                 (insert (car want-l))
10705                 (setq want-list (delq (car want-l) want-list))))
10706           (setq sorted (cdr sorted)))
10707         ;; Any headers that were not matched by the sorted list we
10708         ;; just tack on the end of the visible header list.
10709         (while want-list
10710           (insert (car want-list))
10711           (setq want-list (cdr want-list)))
10712         ;; And finally we make the unwanted headers invisible.
10713         (if delete
10714             (delete-region (point) (point-max))
10715           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10716           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10717
10718 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10719 (defun gnus-article-treat-overstrike ()
10720   "Translate overstrikes into bold text."
10721   (interactive)
10722   (save-excursion
10723     (set-buffer gnus-article-buffer)
10724     (let ((buffer-read-only nil))
10725       (while (search-forward "\b" nil t)
10726         (let ((next (following-char))
10727               (previous (char-after (- (point) 2))))
10728           (cond ((eq next previous)
10729                  (delete-region (- (point) 2) (point))
10730                  (put-text-property (point) (1+ (point))
10731                                     'face 'bold))
10732                 ((eq next ?_)
10733                  (delete-region (1- (point)) (1+ (point)))
10734                  (put-text-property (1- (point)) (point)
10735                                     'face 'underline))
10736                 ((eq previous ?_)
10737                  (delete-region (- (point) 2) (point))
10738                  (put-text-property (point) (1+ (point))
10739                                     'face 'underline))))))))
10740
10741 (defun gnus-article-word-wrap ()
10742   "Format too long lines."
10743   (interactive)
10744   (save-excursion
10745     (set-buffer gnus-article-buffer)
10746     (let ((buffer-read-only nil))
10747       (goto-char (point-min))
10748       (search-forward "\n\n" nil t)
10749       (end-of-line 1)
10750       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10751             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10752             (adaptive-fill-mode t))
10753         (while (not (eobp))
10754           (and (>= (current-column) (min fill-column (window-width)))
10755                (/= (preceding-char) ?:)
10756                (fill-paragraph nil))
10757           (end-of-line 2))))))
10758
10759 (defun gnus-article-remove-cr ()
10760   "Remove carriage returns from an article."
10761   (interactive)
10762   (save-excursion
10763     (set-buffer gnus-article-buffer)
10764     (let ((buffer-read-only nil))
10765       (goto-char (point-min))
10766       (while (search-forward "\r" nil t)
10767         (replace-match "" t t)))))
10768
10769 (defun gnus-article-display-x-face (&optional force)
10770   "Look for an X-Face header and display it if present."
10771   (interactive (list 'force))
10772   (save-excursion
10773     (set-buffer gnus-article-buffer)
10774     (let ((inhibit-point-motion-hooks t)
10775           (case-fold-search nil))
10776       (save-restriction
10777         (goto-char (point-min))
10778         (search-forward "\n\n")
10779         (narrow-to-region (point-min) (point))
10780         (goto-char (point-min))
10781         (if (or (not gnus-article-x-face-command)
10782                 (and (not force)
10783                      (or (not gnus-article-x-face-too-ugly)
10784                          (string-match gnus-article-x-face-too-ugly
10785                                        (mail-fetch-field "from"))))
10786                 (progn
10787                   (goto-char (point-min))
10788                   (not (re-search-forward "^X-Face: " nil t))))
10789             nil
10790           (let ((beg (point))
10791                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10792             (if (symbolp gnus-article-x-face-command)
10793                 (and (or (fboundp gnus-article-x-face-command)
10794                          (error "%s is not a function"
10795                                 gnus-article-x-face-command))
10796                      (funcall gnus-article-x-face-command beg end))
10797               (call-process-region beg end "sh" nil 0 nil
10798                                    "-c" gnus-article-x-face-command))))))))
10799
10800 (defun gnus-article-de-quoted-unreadable (&optional force)
10801   "Do a naïve translation of a quoted-printable-encoded article.
10802 This is in no way, shape or form meant as a replacement for real MIME
10803 processing, but is simply a stop-gap measure until MIME support is
10804 written.
10805 If FORCE, decode the article whether it is marked as quoted-printable
10806 or not." 
10807   (interactive (list 'force))
10808   (save-excursion
10809     (set-buffer gnus-article-buffer)
10810     (let ((case-fold-search t)
10811           (buffer-read-only nil)
10812           (type (gnus-fetch-field "content-transfer-encoding")))
10813       (if (or force (and type (string-match "quoted-printable" type)))
10814           (progn
10815             (goto-char (point-min))
10816             (search-forward "\n\n" nil 'move)
10817             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10818
10819 (defun gnus-mime-decode-quoted-printable (from to)
10820   ;; Decode quoted-printable from region between FROM and TO.
10821   (save-excursion
10822     (goto-char from)
10823     (while (search-forward "=" to t)
10824       (cond ((eq (following-char) ?\n)
10825              (delete-char -1)
10826              (delete-char 1))
10827             ((looking-at "[0-9A-F][0-9A-F]")
10828              (delete-char -1)
10829              (insert (hexl-hex-string-to-integer
10830                       (buffer-substring (point) (+ 2 (point)))))
10831              (delete-char 2))
10832             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10833
10834 (defvar gnus-article-time-units
10835   (list (cons 'year (* 365.25 24 60 60))
10836         (cons 'week (* 7 24 60 60))
10837         (cons 'day (* 24 60 60))
10838         (cons 'hour (* 60 60))
10839         (cons 'minute 60)
10840         (cons 'second 1)))
10841
10842 (defun gnus-article-date-ut (&optional type)
10843   "Convert DATE date to universal time in the current article.
10844 If TYPE is `local', convert to local time; if it is `lapsed', output
10845 how much time has lapsed since DATE."
10846   (interactive (list 'ut))
10847   (let ((date (header-date (or gnus-current-headers 
10848                                (gnus-get-header-by-number
10849                                 (gnus-summary-article-number))"")))
10850         (date-regexp "^Date: \\|^X-Sent: "))
10851     (if (or (not date)
10852             (string= date ""))
10853         ()
10854       (save-excursion
10855         (set-buffer gnus-article-buffer)
10856         (let ((buffer-read-only nil))
10857           (goto-char (point-min))
10858           (if (and (re-search-forward date-regexp nil t)
10859                    (progn 
10860                      (beginning-of-line)
10861                      (looking-at date-regexp)))
10862               (delete-region (gnus-point-at-bol)
10863                              (progn (end-of-line) (1+ (point))))
10864             (goto-char (point-min))
10865             (goto-char (- (search-forward "\n\n") 2)))
10866           (insert
10867            (cond 
10868             ((eq type 'local)
10869              (concat "Date: " (condition-case ()
10870                                   (timezone-make-date-arpa-standard date)
10871                                 (error date))
10872                      "\n"))
10873             ((eq type 'ut)
10874              (concat "Date: "
10875                      (condition-case ()
10876                          (timezone-make-date-arpa-standard date nil "UT")
10877                        (error date))
10878                      "\n"))
10879             ((eq type 'lapsed)
10880              ;; If the date is seriously mangled, the timezone
10881              ;; functions are liable to bug out, so we condition-case
10882              ;; the entire thing.  
10883              (let* ((real-sec (condition-case ()
10884                                   (- (gnus-seconds-since-epoch 
10885                                       (timezone-make-date-arpa-standard
10886                                        (current-time-string) 
10887                                        (current-time-zone) "UT"))
10888                                      (gnus-seconds-since-epoch 
10889                                       (timezone-make-date-arpa-standard 
10890                                        date nil "UT")))
10891                                 (error 0)))
10892                     (sec (abs real-sec))
10893                     num prev)
10894                (if (zerop sec)
10895                    "X-Sent: Now\n"
10896                  (concat
10897                   "X-Sent: "
10898                   (mapconcat 
10899                    (lambda (unit)
10900                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10901                          ""
10902                        (setq sec (- sec (* num (cdr unit))))
10903                        (prog1
10904                            (concat (if prev ", " "") (int-to-string 
10905                                                       (floor num))
10906                                    " " (symbol-name (car unit))
10907                                    (if (> num 1) "s" ""))
10908                          (setq prev t))))
10909                    gnus-article-time-units "")
10910                   (if (> real-sec 0)
10911                       " ago\n"
10912                     " in the future\n")))))
10913             (t
10914              (error "Unknown conversion type: %s" type)))))))))
10915
10916 (defun gnus-article-date-local ()
10917   "Convert the current article date to the local timezone."
10918   (interactive)
10919   (gnus-article-date-ut 'local))
10920
10921 (defun gnus-article-date-lapsed ()
10922   "Convert the current article date to time lapsed since it was sent."
10923   (interactive)
10924   (gnus-article-date-ut 'lapsed))
10925
10926 (defun gnus-article-maybe-highlight ()
10927   (if gnus-visual (gnus-article-highlight)))
10928
10929 ;; Article savers.
10930
10931 (defun gnus-output-to-rmail (file-name)
10932   "Append the current article to an Rmail file named FILE-NAME."
10933   (require 'rmail)
10934   ;; Most of these codes are borrowed from rmailout.el.
10935   (setq file-name (expand-file-name file-name))
10936   (setq rmail-default-rmail-file file-name)
10937   (let ((artbuf (current-buffer))
10938         (tmpbuf (get-buffer-create " *Gnus-output*")))
10939     (save-excursion
10940       (or (get-file-buffer file-name)
10941           (file-exists-p file-name)
10942           (if (gnus-yes-or-no-p
10943                (concat "\"" file-name "\" does not exist, create it? "))
10944               (let ((file-buffer (create-file-buffer file-name)))
10945                 (save-excursion
10946                   (set-buffer file-buffer)
10947                   (rmail-insert-rmail-file-header)
10948                   (let ((require-final-newline nil))
10949                     (write-region (point-min) (point-max) file-name t 1)))
10950                 (kill-buffer file-buffer))
10951             (error "Output file does not exist")))
10952       (set-buffer tmpbuf)
10953       (buffer-disable-undo (current-buffer))
10954       (erase-buffer)
10955       (insert-buffer-substring artbuf)
10956       (gnus-convert-article-to-rmail)
10957       ;; Decide whether to append to a file or to an Emacs buffer.
10958       (let ((outbuf (get-file-buffer file-name)))
10959         (if (not outbuf)
10960             (append-to-file (point-min) (point-max) file-name)
10961           ;; File has been visited, in buffer OUTBUF.
10962           (set-buffer outbuf)
10963           (let ((buffer-read-only nil)
10964                 (msg (and (boundp 'rmail-current-message)
10965                           (symbol-value 'rmail-current-message))))
10966             ;; If MSG is non-nil, buffer is in RMAIL mode.
10967             (if msg
10968                 (progn (widen)
10969                        (narrow-to-region (point-max) (point-max))))
10970             (insert-buffer-substring tmpbuf)
10971             (if msg
10972                 (progn
10973                   (goto-char (point-min))
10974                   (widen)
10975                   (search-backward "\^_")
10976                   (narrow-to-region (point) (point-max))
10977                   (goto-char (1+ (point-min)))
10978                   (rmail-count-new-messages t)
10979                   (rmail-show-message msg)))))))
10980     (kill-buffer tmpbuf)))
10981
10982 (defun gnus-output-to-file (file-name)
10983   "Append the current article to a file named FILE-NAME."
10984   (setq file-name (expand-file-name file-name))
10985   (let ((artbuf (current-buffer))
10986         (tmpbuf (get-buffer-create " *Gnus-output*")))
10987     (save-excursion
10988       (set-buffer tmpbuf)
10989       (buffer-disable-undo (current-buffer))
10990       (erase-buffer)
10991       (insert-buffer-substring artbuf)
10992       ;; Append newline at end of the buffer as separator, and then
10993       ;; save it to file.
10994       (goto-char (point-max))
10995       (insert "\n")
10996       (append-to-file (point-min) (point-max) file-name))
10997     (kill-buffer tmpbuf)))
10998
10999 (defun gnus-convert-article-to-rmail ()
11000   "Convert article in current buffer to Rmail message format."
11001   (let ((buffer-read-only nil))
11002     ;; Convert article directly into Babyl format.
11003     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11004     (goto-char (point-min))
11005     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11006     (while (search-forward "\n\^_" nil t) ;single char
11007       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11008     (goto-char (point-max))
11009     (insert "\^_")))
11010
11011 (defun gnus-narrow-to-page (&optional arg)
11012   "Make text outside current page invisible except for page delimiter.
11013 A numeric arg specifies to move forward or backward by that many pages,
11014 thus showing a page other than the one point was originally in."
11015   (interactive "P")
11016   (setq arg (if arg (prefix-numeric-value arg) 0))
11017   (save-excursion
11018     (forward-page -1)                   ;Beginning of current page.
11019     (widen)
11020     (if (> arg 0)
11021         (forward-page arg)
11022       (if (< arg 0)
11023           (forward-page (1- arg))))
11024     ;; Find the end of the page.
11025     (forward-page)
11026     ;; If we stopped due to end of buffer, stay there.
11027     ;; If we stopped after a page delimiter, put end of restriction
11028     ;; at the beginning of that line.
11029     ;; These are commented out.
11030     ;;    (if (save-excursion (beginning-of-line)
11031     ;;                  (looking-at page-delimiter))
11032     ;;  (beginning-of-line))
11033     (narrow-to-region (point)
11034                       (progn
11035                         ;; Find the top of the page.
11036                         (forward-page -1)
11037                         ;; If we found beginning of buffer, stay there.
11038                         ;; If extra text follows page delimiter on same line,
11039                         ;; include it.
11040                         ;; Otherwise, show text starting with following line.
11041                         (if (and (eolp) (not (bobp)))
11042                             (forward-line 1))
11043                         (point)))))
11044
11045 (defun gnus-gmt-to-local ()
11046   "Rewrite Date header described in GMT to local in current buffer.
11047 Intended to be used with gnus-article-prepare-hook."
11048   (save-excursion
11049     (save-restriction
11050       (widen)
11051       (goto-char (point-min))
11052       (narrow-to-region (point-min)
11053                         (progn (search-forward "\n\n" nil 'move) (point)))
11054       (goto-char (point-min))
11055       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11056           (let ((buffer-read-only nil)
11057                 (date (buffer-substring-no-properties
11058                        (match-beginning 1) (match-end 1))))
11059             (delete-region (match-beginning 1) (match-end 1))
11060             (insert
11061              (timezone-make-date-arpa-standard 
11062               date nil (current-time-zone))))))))
11063
11064
11065 ;; Article mode commands
11066
11067 (defun gnus-article-next-page (&optional lines)
11068   "Show next page of current article.
11069 If end of article, return non-nil. Otherwise return nil.
11070 Argument LINES specifies lines to be scrolled up."
11071   (interactive "P")
11072   (move-to-window-line -1)
11073   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11074   (if (save-excursion
11075         (end-of-line)
11076         (and (pos-visible-in-window-p)  ;Not continuation line.
11077              (eobp)))
11078       ;; Nothing in this page.
11079       (if (or (not gnus-break-pages)
11080               (save-excursion
11081                 (save-restriction
11082                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11083           t                             ;Nothing more.
11084         (gnus-narrow-to-page 1)         ;Go to next page.
11085         nil)
11086     ;; More in this page.
11087     (condition-case ()
11088         (scroll-up lines)
11089       (end-of-buffer
11090        ;; Long lines may cause an end-of-buffer error.
11091        (goto-char (point-max))))
11092     nil))
11093
11094 (defun gnus-article-prev-page (&optional lines)
11095   "Show previous page of current article.
11096 Argument LINES specifies lines to be scrolled down."
11097   (interactive "P")
11098   (move-to-window-line 0)
11099   (if (and gnus-break-pages
11100            (bobp)
11101            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11102       (progn
11103         (gnus-narrow-to-page -1) ;Go to previous page.
11104         (goto-char (point-max))
11105         (recenter -1))
11106     (scroll-down lines)))
11107
11108 (defun gnus-article-refer-article ()
11109   "Read article specified by message-id around point."
11110   (interactive)
11111   (search-forward ">" nil t)    ;Move point to end of "<....>".
11112   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11113       (let ((message-id
11114              (buffer-substring (match-beginning 1) (match-end 1))))
11115         (set-buffer gnus-summary-buffer)
11116         (gnus-summary-refer-article message-id))
11117     (error "No references around point")))
11118
11119 (defun gnus-article-show-summary ()
11120   "Reconfigure windows to show summary buffer."
11121   (interactive)
11122   (gnus-configure-windows 'article)
11123   (gnus-summary-goto-subject gnus-current-article))
11124
11125 (defun gnus-article-describe-briefly ()
11126   "Describe article mode commands briefly."
11127   (interactive)
11128   (gnus-message 6
11129    (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")))
11130
11131 (defun gnus-article-summary-command ()
11132   "Execute the last keystroke in the summary buffer."
11133   (interactive)
11134   (let ((obuf (current-buffer))
11135         (owin (current-window-configuration))
11136         func)
11137     (switch-to-buffer gnus-summary-buffer 'norecord)
11138     (setq func (lookup-key (current-local-map) (this-command-keys)))
11139     (call-interactively func)
11140     (set-buffer obuf)
11141     (set-window-configuration owin)
11142     (set-window-start (get-buffer-window (current-buffer)) (point))))
11143
11144 (defun gnus-article-summary-command-nosave ()
11145   "Execute the last keystroke in the summary buffer."
11146   (interactive)
11147   (let (func)
11148     (pop-to-buffer gnus-summary-buffer 'norecord)
11149     (setq func (lookup-key (current-local-map) (this-command-keys)))
11150     (call-interactively func)))
11151
11152 \f
11153 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11154
11155 ;;;###autoload
11156 (defalias 'gnus-batch-kill 'gnus-batch-score)
11157 ;;;###autoload
11158 (defun gnus-batch-score ()
11159   "Run batched scoring.
11160 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11161 Newsgroups is a list of strings in Bnews format.  If you want to score
11162 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11163 score the alt hierarchy, you'd say \"!alt.all\"."
11164   (interactive)
11165   (let* ((yes-and-no
11166           (gnus-newsrc-parse-options
11167            (apply (function concat)
11168                   (mapcar (lambda (g) (concat g " "))
11169                           command-line-args-left))))
11170          (gnus-expert-user t)
11171          (nnmail-spool-file nil)
11172          (gnus-use-dribble-file nil)
11173          (yes (car yes-and-no))
11174          (no (cdr yes-and-no))
11175          group newsrc entry
11176          ;; Disable verbose message.
11177          gnus-novice-user gnus-large-newsgroup)
11178     ;; Eat all arguments.
11179     (setq command-line-args-left nil)
11180     ;; Start Gnus.
11181     (gnus)
11182     ;; Apply kills to specified newsgroups in command line arguments.
11183     (setq newsrc (cdr gnus-newsrc-alist))
11184     (while newsrc
11185       (setq group (car (car newsrc)))
11186       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11187       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11188                (and (car entry)
11189                     (or (eq (car entry) t)
11190                         (not (zerop (car entry)))))
11191                (if yes (string-match yes group) t)
11192                (or (null no) (not (string-match no group))))
11193           (progn
11194             (gnus-summary-read-group group nil t)
11195             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11196                  (gnus-summary-exit))))
11197       (setq newsrc (cdr newsrc)))
11198     ;; Exit Emacs.
11199     (switch-to-buffer gnus-group-buffer)
11200     (gnus-group-save-newsrc)))
11201
11202 (defun gnus-apply-kill-file ()
11203   "Apply a kill file to the current newsgroup.
11204 Returns the number of articles marked as read."
11205   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11206           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11207       (gnus-apply-kill-file-internal)
11208     0))
11209
11210 (defun gnus-kill-save-kill-buffer ()
11211   (save-excursion
11212     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11213       (if (get-file-buffer file)
11214           (progn
11215             (set-buffer (get-file-buffer file))
11216             (and (buffer-modified-p) (save-buffer))
11217             (kill-buffer (current-buffer)))))))
11218
11219 (defvar gnus-kill-file-name "KILL"
11220   "Suffix of the kill files.")
11221
11222 (defun gnus-newsgroup-kill-file (newsgroup)
11223   "Return the name of a kill file name for NEWSGROUP.
11224 If NEWSGROUP is nil, return the global kill file name instead."
11225   (cond ((or (null newsgroup)
11226              (string-equal newsgroup ""))
11227          ;; The global KILL file is placed at top of the directory.
11228          (expand-file-name gnus-kill-file-name
11229                            (or gnus-kill-files-directory "~/News")))
11230         ((gnus-use-long-file-name 'not-kill)
11231          ;; Append ".KILL" to newsgroup name.
11232          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11233                            (or gnus-kill-files-directory "~/News")))
11234         (t
11235          ;; Place "KILL" under the hierarchical directory.
11236          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11237                                    "/" gnus-kill-file-name)
11238                            (or gnus-kill-files-directory "~/News")))))
11239
11240 \f
11241 ;;;
11242 ;;; Dribble file
11243 ;;;
11244
11245 (defvar gnus-dribble-ignore nil)
11246 (defvar gnus-dribble-eval-file nil)
11247
11248 (defun gnus-dribble-file-name ()
11249   (concat gnus-current-startup-file "-dribble"))
11250
11251 (defun gnus-dribble-open ()
11252   (save-excursion 
11253     (set-buffer 
11254      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11255     (buffer-disable-undo (current-buffer))
11256     (bury-buffer gnus-dribble-buffer)
11257     (auto-save-mode t)
11258     (goto-char (point-max))))
11259
11260 (defun gnus-dribble-enter (string)
11261   (if (and (not gnus-dribble-ignore)
11262            gnus-dribble-buffer
11263            (buffer-name gnus-dribble-buffer))
11264       (let ((obuf (current-buffer)))
11265         (set-buffer gnus-dribble-buffer)
11266         (insert string "\n")
11267         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11268         (set-buffer obuf))))
11269
11270 (defun gnus-dribble-read-file ()
11271   (let ((dribble-file (gnus-dribble-file-name)))
11272     (save-excursion 
11273       (set-buffer (setq gnus-dribble-buffer 
11274                         (get-buffer-create 
11275                          (file-name-nondirectory dribble-file))))
11276       (gnus-add-current-to-buffer-list)
11277       (erase-buffer)
11278       (set-visited-file-name dribble-file)
11279       (buffer-disable-undo (current-buffer))
11280       (bury-buffer (current-buffer))
11281       (set-buffer-modified-p nil)
11282       (let ((auto (make-auto-save-file-name))
11283             (gnus-dribble-ignore t))
11284         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11285             (progn
11286               (if (file-newer-than-file-p auto dribble-file)
11287                   (setq dribble-file auto))
11288               (insert-file-contents dribble-file)
11289               (if (not (zerop (buffer-size)))
11290                   (set-buffer-modified-p t))
11291               (if (gnus-y-or-n-p 
11292                    "Auto-save file exists. Do you want to read it? ")
11293                   (setq gnus-dribble-eval-file t))))))))
11294
11295 (defun gnus-dribble-eval-file ()
11296   (if (not gnus-dribble-eval-file)
11297       ()
11298     (setq gnus-dribble-eval-file nil)
11299     (save-excursion
11300       (set-buffer gnus-dribble-buffer)
11301       (eval-buffer (current-buffer)))))
11302
11303 (defun gnus-dribble-delete-file ()
11304   (if (file-exists-p (gnus-dribble-file-name))
11305       (delete-file (gnus-dribble-file-name)))
11306   (if gnus-dribble-buffer
11307       (save-excursion
11308         (set-buffer gnus-dribble-buffer)
11309         (let ((auto (make-auto-save-file-name)))
11310           (if (file-exists-p auto)
11311               (delete-file auto))
11312           (erase-buffer)
11313           (set-buffer-modified-p nil)))))
11314
11315 (defun gnus-dribble-save ()
11316   (if (and gnus-dribble-buffer
11317            (buffer-name gnus-dribble-buffer))
11318       (save-excursion
11319         (set-buffer gnus-dribble-buffer)
11320         (save-buffer))))
11321
11322 (defun gnus-dribble-clear ()
11323   (save-excursion
11324     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11325         (progn
11326           (set-buffer gnus-dribble-buffer)
11327           (erase-buffer)
11328           (set-buffer-modified-p nil)
11329           (setq buffer-saved-size (buffer-size))))))
11330
11331 ;;;
11332 ;;; Server Communication
11333 ;;;
11334
11335 ;; All the Gnus backends have the same interface, and should return
11336 ;; data in a similar format. Below is an overview of what functions
11337 ;; these packages must supply and what results they should return.
11338 ;;
11339 ;; Variables:
11340 ;;
11341 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11342 ;; buffer. 
11343 ;;
11344 ;; Functions for the imaginary backend `choke':
11345 ;;
11346 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11347 ;; Should return all headers for all ARTICLES, or return NOV lines for
11348 ;; the same.
11349 ;;
11350 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11351 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11352 ;; must be returned.
11353 ;;
11354 ;; `choke-close-group GROUP &optional SERVER'
11355 ;; Close group. Most backends won't have to do anything with this
11356 ;; call, but it is an opportunity to clean up, if that is needed. It
11357 ;; is called when Gnus exits a group.
11358 ;;
11359 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11360 ;; Return ARTICLE, which is either an article number or
11361 ;; message-id. Note that not all backends can return articles based on
11362 ;; message-id. 
11363 ;;
11364 ;; `choke-request-list SERVER'
11365 ;; Return a list of all newsgroups on SERVER.
11366 ;;
11367 ;; `choke-request-list-newsgroups SERVER'
11368 ;; Return a list of descriptions of all newsgroups on SERVER.
11369 ;;
11370 ;; `choke-request-newgroups DATE &optional SERVER'
11371 ;; Return a list of all groups that have arrived after DATE on
11372 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11373 ;; always check whether the groups are old or not. Backends that do
11374 ;; not store date information may just return the entire list of
11375 ;; groups, although this might not be a good idea in general.
11376 ;;
11377 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11378 ;; Should return a buffer that is suitable for "posting". nnspool and
11379 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11380 ;; buffer. This function should fill out the appropriate headers. 
11381 ;;
11382 ;; `choke-request-post &optional SERVER'
11383 ;; Function that will be called from a buffer to be posted. 
11384 ;;
11385 ;; `choke-open-server SERVER &optional ARGUMENT'
11386 ;; Open a connection to SERVER.
11387 ;;
11388 ;; `choke-close-server &optional SERVER'
11389 ;; Close the connection to SERVER.
11390 ;;
11391 ;; `choke-server-opened &optional SERVER'
11392 ;; Whether the conenction to SERVER is opened or not.
11393 ;;
11394 ;; `choke-server-status &optional SERVER'
11395 ;; Should return a status string (not in the nntp buffer, but as the
11396 ;; result of the function).
11397 ;;
11398 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11399 ;; Optional function for retrieving active file info on all groups in
11400 ;; GROUPS.  Two return formats are supported: The normal active file
11401 ;; format, and a list of GROUP lines.  This function should return (as
11402 ;; a function value) either `active' or `group', depending on what
11403 ;; format it returns.
11404 ;;
11405 ;; The following functions are optional and apply only to backends
11406 ;; that are able to control the contents of their groups totally
11407 ;; (ie. mail backends.)  Backends that aren't able to do that
11408 ;; shouldn't define these functions at all. Gnus will check for their
11409 ;; presence before attempting to call them.
11410 ;;
11411 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11412 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11413 ;; backends will not be able to expire articles. Should return a list
11414 ;; of all articles that were not expired.
11415 ;;
11416 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11417 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11418 ;; Removes any information it has added to the article (extra headers,
11419 ;; whatever - make it as clean as possible), and then passes the
11420 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11421 ;; function described below. If the ACCEPT-FORM returns a non-nil
11422 ;; value, the article should then be deleted. If LAST is nil, that
11423 ;; means that there will be further calls to this function. This might
11424 ;; be taken as an advice not to save buffers/internal variables just
11425 ;; yet, but wait until the last call to speed things up.
11426 ;;
11427 ;; `choke-request-accept-article GROUP &optional LAST' 
11428 ;; The contents of the current buffer will be put into GROUP.  There
11429 ;; should, of course, be an article in the current buffer.  This
11430 ;; function is normally only called by the function described above,
11431 ;; and LAST works the same way as in that function.
11432 ;;
11433 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11434 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11435 ;; This provides an easy interface for allowing editing of
11436 ;; articles. Note that even headers may be edited, so the backend has
11437 ;; to update any tables (nov buffers, etc) that it maintains after
11438 ;; replacing the article.
11439 ;;
11440 ;; `choke-request-create-group GROUP &optional SERVER'
11441 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11442 ;; might be a group that already exists, but hasn't been registered
11443 ;; yet. 
11444 ;;
11445 ;; All these functions must return nil if they couldn't service the
11446 ;; request. If the optional arguments are not supplied, some "current"
11447 ;; or "default" values should be used. In short, one should emulate an
11448 ;; NNTP server, in a way.
11449 ;;
11450 ;; If you want to write a new backend, you just have to supply the
11451 ;; functions listed above. In addition, you must enter the new backend
11452 ;; into the list of valid select methods:
11453 ;; (setq gnus-valid-select-methods 
11454 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11455 ;; The first element in this list is the name of the backend. Other
11456 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11457 ;; groups), `none' (neither), `respool' (for groups that can control
11458 ;; their contents). 
11459
11460 (defun gnus-start-news-server (&optional confirm)
11461   "Open a method for getting news.
11462 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11463   (let (how)
11464     (if gnus-current-select-method
11465         ;; Stream is already opened.
11466         nil
11467       ;; Open NNTP server.
11468       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11469       (if confirm
11470           (progn
11471             ;; Read server name with completion.
11472             (setq gnus-nntp-server
11473                   (completing-read "NNTP server: "
11474                                    (mapcar (lambda (server) (list server))
11475                                            (cons (list gnus-nntp-server)
11476                                                  gnus-secondary-servers))
11477                                    nil nil gnus-nntp-server))))
11478
11479       (if (and gnus-nntp-server 
11480                (stringp gnus-nntp-server)
11481                (not (string= gnus-nntp-server "")))
11482           (setq gnus-select-method
11483                 (cond ((or (string= gnus-nntp-server "")
11484                            (string= gnus-nntp-server "::"))
11485                        (list 'nnspool (system-name)))
11486                       ((string-match "^:" gnus-nntp-server)
11487                        (list 'nnmh gnus-nntp-server 
11488                              (list 'nnmh-directory 
11489                                    (file-name-as-directory
11490                                     (expand-file-name
11491                                      (concat "~/" (substring
11492                                                    gnus-nntp-server 1)))))
11493                              (list 'nnmh-get-new-mail nil)))
11494                       (t
11495                        (list 'nntp gnus-nntp-server)))))
11496
11497       (setq how (car gnus-select-method))
11498       (cond ((eq how 'nnspool)
11499              (require 'nnspool)
11500              (gnus-message 5 "Looking up local news spool..."))
11501             ((eq how 'nnmh)
11502              (require 'nnmh)
11503              (gnus-message 5 "Looking up mh spool..."))
11504             (t
11505              (require 'nntp)))
11506       (setq gnus-current-select-method gnus-select-method)
11507       (run-hooks 'gnus-open-server-hook)
11508       (or 
11509        ;; gnus-open-server-hook might have opened it
11510        (gnus-server-opened gnus-select-method)  
11511        (gnus-open-server gnus-select-method)
11512        (gnus-y-or-n-p
11513         (format
11514          "%s server on %s can't be opened. Continue? "
11515          (car gnus-select-method) (nth 1 gnus-select-method)))
11516        (progn
11517          (gnus-message 1 "Couldn't open server on %s" 
11518                        (nth 1 gnus-select-method))
11519          (ding)
11520          nil)))))
11521
11522 (defun gnus-check-news-server (&optional method)
11523   "If the news server is down, start it up again."
11524   (let ((method (if method method gnus-select-method)))
11525     (and (stringp method)
11526          (setq method (gnus-server-to-method method)))
11527     (if (gnus-server-opened method)
11528         ;; Stream is already opened.
11529         t
11530       ;; Open server.
11531       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11532       (run-hooks 'gnus-open-server-hook)
11533       (or (gnus-server-opened method)
11534           (gnus-open-server method))
11535       (message ""))))
11536
11537 (defun gnus-nntp-message (&optional message)
11538   "Check the status of the NNTP server.
11539 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11540 is returned insted of the status string."
11541   (let ((status (gnus-status-message (gnus-find-method-for-group 
11542                                       gnus-newsgroup-name)))
11543         (message (or message "")))
11544     (if (and (stringp status) (> (length status) 0))
11545         status message)))
11546
11547 (defun gnus-get-function (method function)
11548   (and (stringp method)
11549        (setq method (gnus-server-to-method method)))
11550   (let ((func (intern (format "%s-%s" (car method) function))))
11551     (if (not (fboundp func)) 
11552         (progn
11553           (require (car method))
11554           (if (not (fboundp func)) 
11555               (error "No such function: %s" func))))
11556     func))
11557
11558 ;;; Interface functions to the backends.
11559
11560 (defun gnus-open-server (method)
11561   (funcall (gnus-get-function method 'open-server)
11562            (nth 1 method) (nthcdr 2 method)))
11563
11564 (defun gnus-close-server (method)
11565   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11566
11567 (defun gnus-request-list (method)
11568   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11569
11570 (defun gnus-request-list-newsgroups (method)
11571   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11572
11573 (defun gnus-request-newgroups (date method)
11574   (funcall (gnus-get-function method 'request-newgroups) 
11575            date (nth 1 method)))
11576
11577 (defun gnus-server-opened (method)
11578   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11579
11580 (defun gnus-status-message (method)
11581   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11582                   method)))
11583     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11584
11585 (defun gnus-request-group (group &optional dont-check)
11586   (let ((method (gnus-find-method-for-group group)))
11587 ;    (and t (message "%s GROUP %s" (car method) group))
11588     (funcall (gnus-get-function method 'request-group) 
11589              (gnus-group-real-name group) (nth 1 method) dont-check)))
11590
11591 (defun gnus-request-asynchronous (group &optional articles)
11592   (let ((method (gnus-find-method-for-group group)))
11593     (funcall (gnus-get-function method 'request-asynchronous) 
11594              (gnus-group-real-name group) (nth 1 method) articles)))
11595
11596 (defun gnus-list-active-group (group)
11597   (let ((method (gnus-find-method-for-group group))
11598         (func 'list-active-group))
11599     (and (gnus-check-backend-function func group)
11600          (funcall (gnus-get-function method func) 
11601                   (gnus-group-real-name group) (nth 1 method)))))
11602
11603 (defun gnus-request-group-description (group)
11604   (let ((method (gnus-find-method-for-group group))
11605         (func 'request-group-description))
11606     (and (gnus-check-backend-function func group)
11607          (funcall (gnus-get-function method func) 
11608                   (gnus-group-real-name group) (nth 1 method)))))
11609
11610 (defun gnus-close-group (group)
11611   (let ((method (gnus-find-method-for-group group)))
11612     (funcall (gnus-get-function method 'close-group) 
11613              (gnus-group-real-name group) (nth 1 method))))
11614
11615 (defun gnus-retrieve-headers (articles group)
11616   (let ((method (gnus-find-method-for-group group)))
11617     (if (and gnus-use-cache (numberp (car articles)))
11618         (gnus-cache-retrieve-headers articles group)
11619       (funcall (gnus-get-function method 'retrieve-headers) 
11620                articles (gnus-group-real-name group) (nth 1 method)))))
11621
11622 (defun gnus-retrieve-groups (groups method)
11623   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11624
11625 (defun gnus-request-article (article group &optional buffer)
11626   (let ((method (gnus-find-method-for-group group)))
11627     (funcall (gnus-get-function method 'request-article) 
11628              article (gnus-group-real-name group) (nth 1 method) buffer)))
11629
11630 (defun gnus-request-head (article group)
11631   (let ((method (gnus-find-method-for-group group)))
11632     (funcall (gnus-get-function method 'request-head) 
11633              article (gnus-group-real-name group) (nth 1 method))))
11634
11635 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11636 (defun gnus-request-post-buffer (post group subject header artbuf
11637                                       info follow-to respect-poster)
11638    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11639                                             group gnus-newsrc-hashtb)))))
11640           (method
11641            (if (and gnus-post-method
11642                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11643                     (memq 'post (assoc
11644                                  (format "%s" (car (gnus-find-method-for-group
11645                                                     gnus-newsgroup-name)))
11646                                         gnus-valid-select-methods)))
11647                gnus-post-method
11648              (gnus-find-method-for-group gnus-newsgroup-name))))
11649      (or (gnus-server-opened method)
11650          (gnus-open-server method)
11651          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11652      (let ((mail-self-blind nil)
11653            (mail-archive-file-name nil))
11654        (funcall (gnus-get-function method 'request-post-buffer) 
11655                 post group subject header artbuf info follow-to
11656                 respect-poster))))
11657
11658 (defun gnus-request-post (method &optional force)
11659   (and (stringp method)
11660        (setq method (gnus-server-to-method method)))
11661   (and (not force) gnus-post-method
11662        (memq 'post (assoc (format "%s" (car method))
11663                           gnus-valid-select-methods))
11664        (setq method gnus-post-method))
11665   (funcall (gnus-get-function method 'request-post) 
11666            (nth 1 method)))
11667
11668 (defun gnus-request-expire-articles (articles group &optional force)
11669   (let ((method (gnus-find-method-for-group group)))
11670     (funcall (gnus-get-function method 'request-expire-articles) 
11671              articles (gnus-group-real-name group) (nth 1 method)
11672              force)))
11673
11674 (defun gnus-request-move-article 
11675   (article group server accept-function &optional last)
11676   (let ((method (gnus-find-method-for-group group)))
11677     (funcall (gnus-get-function method 'request-move-article) 
11678              article (gnus-group-real-name group) 
11679              (nth 1 method) accept-function last)))
11680
11681 (defun gnus-request-accept-article (group &optional last)
11682   (let ((func (if (symbolp group) group
11683                 (car (gnus-find-method-for-group group)))))
11684     (funcall (intern (format "%s-request-accept-article" func))
11685              (if (stringp group) (gnus-group-real-name group) group)
11686              last)))
11687
11688 (defun gnus-request-replace-article (article group buffer)
11689   (let ((func (car (gnus-find-method-for-group group))))
11690     (funcall (intern (format "%s-request-replace-article" func))
11691              article (gnus-group-real-name group) buffer)))
11692
11693 (defun gnus-request-create-group (group)
11694   (let ((method (gnus-find-method-for-group group)))
11695     (funcall (gnus-get-function method 'request-create-group) 
11696              (gnus-group-real-name group) (nth 1 method))))
11697
11698 (defun gnus-member-of-valid (symbol group)
11699   (memq symbol (assoc
11700                 (format "%s" (car (gnus-find-method-for-group group)))
11701                 gnus-valid-select-methods)))
11702
11703 (defsubst gnus-secondary-method-p (method)
11704   (member method gnus-secondary-select-methods))
11705
11706 (defun gnus-find-method-for-group (group &optional info)
11707   (or gnus-override-method
11708       (and (not group)
11709            gnus-select-method)
11710       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11711             method)
11712         (if (or (not info)
11713                 (not (setq method (nth 4 info))))
11714             (setq method gnus-select-method)
11715           (setq method
11716                 (cond ((stringp method)
11717                        (gnus-server-to-method method))
11718                       ((stringp (car method))
11719                        (gnus-server-extend-method group method))
11720                       (t
11721                        method))))
11722         (gnus-server-add-address method))))
11723
11724 (defun gnus-check-backend-function (func group)
11725   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11726                  group)))
11727     (fboundp (intern (format "%s-%s" method func)))))
11728
11729 (defun gnus-methods-using (method)
11730   (let ((valids gnus-valid-select-methods)
11731         outs)
11732     (while valids
11733       (if (memq method (car valids)) 
11734           (setq outs (cons (car valids) outs)))
11735       (setq valids (cdr valids)))
11736     outs))
11737
11738 ;;; 
11739 ;;; Active & Newsrc File Handling
11740 ;;;
11741
11742 ;; Newsrc related functions.
11743 ;; Gnus internal format of gnus-newsrc-alist:
11744 ;; (("alt.general" 3 (1 . 1))
11745 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11746 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11747 ;; The first item is the group name; the second is the subscription
11748 ;; level; the third is either a range of a list of ranges of read
11749 ;; articles, the optional fourth element is a list of marked articles,
11750 ;; the optional fifth element is the select method.
11751 ;;
11752 ;; Gnus internal format of gnus-newsrc-hashtb:
11753 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11754 ;; This is the entry for "alt.misc". The first element is the number
11755 ;; of unread articles in "alt.misc". The cdr of this entry is the
11756 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11757 ;; trivial to remove or add new elements into gnus-newsrc-alist
11758 ;; without scanning the entire list. So, to get the actual information
11759 ;; of "alt.misc", you'd say something like 
11760 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11761 ;;
11762 ;; Gnus internal format of gnus-active-hashtb:
11763 ;; ((1 . 1))
11764 ;;  (5 . 10))
11765 ;;  (67 . 99)) ...)
11766 ;; The only element in each entry in this hash table is a range of
11767 ;; (possibly) available articles. (Articles in this range may have
11768 ;; been expired or canceled.)
11769 ;;
11770 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11771 ;; ("alt.misc" "alt.test" "alt.general" ...)
11772
11773 (defun gnus-setup-news (&optional rawfile level)
11774   "Setup news information.
11775 If RAWFILE is non-nil, the .newsrc file will also be read.
11776 If LEVEL is non-nil, the news will be set up at level LEVEL."
11777   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11778     ;; Clear some variables to re-initialize news information.
11779     (if init (setq gnus-newsrc-alist nil 
11780                    gnus-active-hashtb nil))
11781
11782     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11783     (if init (gnus-read-newsrc-file rawfile))
11784
11785     ;; Read the active file and create `gnus-active-hashtb'.
11786     ;; If `gnus-read-active-file' is nil, then we just create an empty
11787     ;; hash table. The partial filling out of the hash table will be
11788     ;; done in `gnus-get-unread-articles'.
11789     (if (and gnus-read-active-file 
11790              (not level))
11791         (gnus-read-active-file)
11792       (setq gnus-active-hashtb (make-vector 4095 0)))
11793
11794     ;; Possibly eval the dribble file.
11795     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11796
11797     ;; Find the number of unread articles in each non-dead group.
11798     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11799     ;; Find new newsgroups and treat them.
11800     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11801              (gnus-server-opened gnus-select-method))
11802         (gnus-find-new-newsgroups))
11803     (if (and init gnus-check-bogus-newsgroups 
11804              gnus-read-active-file (not level)
11805              (gnus-server-opened gnus-select-method))
11806         (gnus-check-bogus-newsgroups))))
11807
11808 (defun gnus-find-new-newsgroups ()
11809   "Search for new newsgroups and add them.
11810 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11811 The `-n' option line from .newsrc is respected."
11812   (interactive)
11813   (or (gnus-check-first-time-used)
11814       (if (or (consp gnus-check-new-newsgroups)
11815               (eq gnus-check-new-newsgroups 'ask-server))
11816           (gnus-ask-server-for-new-groups)
11817         (let ((groups 0)
11818               group new-newsgroups)
11819           (gnus-message 5 "Checking for new newsgroups...")
11820           (or gnus-have-read-active-file (gnus-read-active-file))
11821           (setq gnus-newsrc-last-checked-date (current-time-string))
11822           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11823           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11824           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11825           (mapatoms
11826            (lambda (sym)
11827              (setq group (symbol-name sym))
11828              (if (or (gnus-gethash group gnus-killed-hashtb)
11829                      (gnus-gethash group gnus-newsrc-hashtb))
11830                  ()
11831                (let ((do-sub (gnus-matches-options-n group)))
11832                  (cond ((eq do-sub 'subscribe)
11833                         (setq groups (1+ groups))
11834                         (gnus-sethash group group gnus-killed-hashtb)
11835                         (funcall 
11836                          gnus-subscribe-options-newsgroup-method group))
11837                        ((eq do-sub 'ignore)
11838                         nil)
11839                        (t
11840                         (setq groups (1+ groups))
11841                         (gnus-sethash group group gnus-killed-hashtb)
11842                         (if gnus-subscribe-hierarchical-interactive
11843                             (setq new-newsgroups (cons group new-newsgroups))
11844                           (funcall gnus-subscribe-newsgroup-method group)))))))
11845            gnus-active-hashtb)
11846           (if new-newsgroups 
11847               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11848           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11849           (if (> groups 0)
11850               (gnus-message 6 "%d new newsgroup%s arrived." 
11851                             groups (if (> groups 1) "s have" " has"))
11852             (gnus-message 6 "No new newsgroups."))))))
11853
11854 (defun gnus-matches-options-n (group)
11855   ;; Returns `subscribe' if the group is to be uncoditionally
11856   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11857   ;; no match for the group.
11858
11859   ;; First we check the two user variables.
11860   (cond
11861    ((and gnus-options-subscribe
11862          (string-match gnus-options-subscribe group))
11863     'subscribe)
11864    ((and gnus-options-not-subscribe
11865          (string-match gnus-options-not-subscribe group))
11866     'ignore)
11867    ;; Then we go through the list that was retrieved from the .newsrc
11868    ;; file.  This list has elements on the form 
11869    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11870    ;; is in the reverse order of the options line) is returned.
11871    (t
11872     (let ((regs gnus-newsrc-options-n))
11873       (while (and regs
11874                   (not (string-match (car (car regs)) group)))
11875         (setq regs (cdr regs)))
11876       (and regs (cdr (car regs)))))))
11877
11878 (defun gnus-ask-server-for-new-groups ()
11879   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11880          (methods (cons gnus-select-method 
11881                         (append
11882                          (and (consp gnus-check-new-newsgroups)
11883                               gnus-check-new-newsgroups)
11884                          gnus-secondary-select-methods)))
11885          (groups 0)
11886          (new-date (current-time-string))
11887          hashtb group new-newsgroups got-new)
11888     ;; Go thorugh both primary and secondary select methods and
11889     ;; request new newsgroups.  
11890     (while methods
11891       (and (or (gnus-server-opened (car methods))
11892                (gnus-open-server (car methods)))
11893            (gnus-request-newgroups date (car methods))
11894            (save-excursion
11895              (setq got-new t)
11896              (set-buffer nntp-server-buffer)
11897              (or hashtb (setq hashtb (gnus-make-hashtable 
11898                                       (count-lines (point-min) (point-max)))))
11899              ;; Enter all the new groups in a hashtable.
11900              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11901       (setq methods (cdr methods)))
11902     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11903     ;; Now all new groups from all select methods are in `hashtb'.
11904     (mapatoms
11905      (lambda (group-sym)
11906        (setq group (symbol-name group-sym))
11907        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11908                (member group gnus-zombie-list)
11909                (member group gnus-killed-list))
11910            ;; The group is already known.
11911            ()
11912          (and (symbol-value group-sym)
11913               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11914          (let ((do-sub (gnus-matches-options-n group)))
11915            (cond ((eq do-sub 'subscribe)
11916                   (setq groups (1+ groups))
11917                   (gnus-sethash group group gnus-killed-hashtb)
11918                   (funcall 
11919                    gnus-subscribe-options-newsgroup-method group))
11920                  ((eq do-sub 'ignore)
11921                   nil)
11922                  (t
11923                   (setq groups (1+ groups))
11924                   (gnus-sethash group group gnus-killed-hashtb)
11925                   (if gnus-subscribe-hierarchical-interactive
11926                       (setq new-newsgroups (cons group new-newsgroups))
11927                     (funcall gnus-subscribe-newsgroup-method group)))))))
11928      hashtb)
11929     (if new-newsgroups 
11930         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11931     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11932     (if (> groups 0)
11933         (gnus-message 6 "%d new newsgroup%s arrived." 
11934                       groups (if (> groups 1) "s have" " has")))
11935     got-new))
11936
11937 (defun gnus-check-first-time-used ()
11938   (if (or (> (length gnus-newsrc-alist) 1)
11939           (file-exists-p gnus-startup-file)
11940           (file-exists-p (concat gnus-startup-file ".el"))
11941           (file-exists-p (concat gnus-startup-file ".eld")))
11942       nil
11943     (gnus-message 6 "First time user; subscribing you to default groups")
11944     (or gnus-have-read-active-file (gnus-read-active-file))
11945     (setq gnus-newsrc-last-checked-date (current-time-string))
11946     (let ((groups gnus-default-subscribed-newsgroups)
11947           group)
11948       (if (eq groups t)
11949           nil
11950         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11951         (mapatoms
11952          (lambda (sym)
11953            (setq group (symbol-name sym))
11954            (let ((do-sub (gnus-matches-options-n group)))
11955              (cond ((eq do-sub 'subscribe)
11956                     (gnus-sethash group group gnus-killed-hashtb)
11957                     (funcall 
11958                      gnus-subscribe-options-newsgroup-method group))
11959                    ((eq do-sub 'ignore)
11960                     nil)
11961                    (t
11962                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11963          gnus-active-hashtb)
11964         (while groups
11965           (if (gnus-gethash (car groups) gnus-active-hashtb)
11966               (gnus-group-change-level 
11967                (car groups) gnus-level-default-subscribed gnus-level-killed))
11968           (setq groups (cdr groups)))
11969         (gnus-group-make-help-group)
11970         (and gnus-novice-user
11971              (gnus-message 7 "`A k' to list killed groups"))))))
11972
11973 (defun gnus-subscribe-group (group previous &optional method)
11974   (gnus-group-change-level 
11975    (if method
11976        (list t group gnus-level-default-subscribed nil nil method)
11977      group) 
11978    gnus-level-default-subscribed gnus-level-killed previous t))
11979
11980 ;; `gnus-group-change-level' is the fundamental function for changing
11981 ;; subscription levels of newsgroups. This might mean just changing
11982 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11983 ;; again, which subscribes/unsubscribes a group, which is equally
11984 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11985 ;; from 8-9 to 1-7 means that you remove the group from the list of
11986 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11987 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11988 ;; which is trivial.
11989 ;; ENTRY can either be a string (newsgroup name) or a list (if
11990 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11991 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11992 ;; entries. 
11993 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11994 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11995 ;; after. 
11996 (defun gnus-group-change-level (entry level &optional oldlevel
11997                                       previous fromkilled)
11998   (let (group info active num)
11999     ;; Glean what info we can from the arguments
12000     (if (consp entry)
12001         (if fromkilled (setq group (nth 1 entry))
12002           (setq group (car (nth 2 entry))))
12003       (setq group entry))
12004     (if (and (stringp entry)
12005              oldlevel 
12006              (< oldlevel gnus-level-zombie))
12007         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12008     (if (and (not oldlevel)
12009              (consp entry))
12010         (setq oldlevel (car (cdr (nth 2 entry)))))
12011     (if (stringp previous)
12012         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12013
12014     (gnus-dribble-enter
12015      (format "(gnus-group-change-level %S %S %S %S %S)" 
12016              group level oldlevel (car (nth 2 previous)) fromkilled))
12017     
12018     ;; Then we remove the newgroup from any old structures, if needed.
12019     ;; If the group was killed, we remove it from the killed or zombie
12020     ;; list. If not, and it is in fact going to be killed, we remove
12021     ;; it from the newsrc hash table and assoc.
12022     (cond ((>= oldlevel gnus-level-zombie)
12023            (if (= oldlevel gnus-level-zombie)
12024                (setq gnus-zombie-list (delete group gnus-zombie-list))
12025              (setq gnus-killed-list (delete group gnus-killed-list))))
12026           (t
12027            (if (and (>= level gnus-level-zombie)
12028                     entry)
12029                (progn
12030                  (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12031                  (if (nth 3 entry)
12032                      (setcdr (gnus-gethash (car (nth 3 entry))
12033                                            gnus-newsrc-hashtb)
12034                              (cdr entry)))
12035                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12036
12037     ;; Finally we enter (if needed) the list where it is supposed to
12038     ;; go, and change the subscription level. If it is to be killed,
12039     ;; we enter it into the killed or zombie list.
12040     (cond ((>= level gnus-level-zombie)
12041            ;; Remove from the hash table.
12042            (gnus-sethash group nil gnus-newsrc-hashtb)
12043            (or (gnus-group-foreign-p group)
12044                ;; We do not enter foreign groups into the list of dead
12045                ;; groups.  
12046                (if (= level gnus-level-zombie)
12047                    (setq gnus-zombie-list (cons group gnus-zombie-list))
12048                  (setq gnus-killed-list (cons group gnus-killed-list)))))
12049           (t
12050            ;; If the list is to be entered into the newsrc assoc, and
12051            ;; it was killed, we have to create an entry in the newsrc
12052            ;; hashtb format and fix the pointers in the newsrc assoc.
12053            (if (>= oldlevel gnus-level-zombie)
12054                (progn
12055                  (if (listp entry)
12056                      (progn
12057                        (setq info (cdr entry))
12058                        (setq num (car entry)))
12059                    (setq active (gnus-gethash group gnus-active-hashtb))
12060                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
12061                    ;; Check whether the group is foreign. If so, the
12062                    ;; foreign select method has to be entered into the
12063                    ;; info. 
12064                    (let ((method (gnus-group-method-name group)))
12065                      (if (eq method gnus-select-method)
12066                          (setq info (list group level nil))
12067                        (setq info (list group level nil nil method)))))
12068                  (or previous 
12069                      (setq previous 
12070                            (let ((p gnus-newsrc-alist))
12071                              (while (cdr (cdr p))
12072                                (setq p (cdr p)))
12073                              p)))
12074                  (setq entry (cons info (cdr (cdr previous))))
12075                  (if (cdr previous)
12076                      (progn
12077                        (setcdr (cdr previous) entry)
12078                        (gnus-sethash group (cons num (cdr previous)) 
12079                                      gnus-newsrc-hashtb))
12080                    (setcdr previous entry)
12081                    (gnus-sethash group (cons num previous)
12082                                  gnus-newsrc-hashtb))
12083                  (if (cdr entry)
12084                      (setcdr (gnus-gethash (car (car (cdr entry)))
12085                                            gnus-newsrc-hashtb)
12086                              entry)))
12087              ;; It was alive, and it is going to stay alive, so we
12088              ;; just change the level and don't change any pointers or
12089              ;; hash table entries.
12090              (setcar (cdr (car (cdr (cdr entry)))) level))))))
12091
12092 (defun gnus-kill-newsgroup (newsgroup)
12093   "Obsolete function. Kills a newsgroup."
12094   (gnus-group-change-level
12095    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12096
12097 (defun gnus-check-bogus-newsgroups (&optional confirm)
12098   "Remove bogus newsgroups.
12099 If CONFIRM is non-nil, the user has to confirm the deletion of every
12100 newsgroup." 
12101   (let ((newsrc (cdr gnus-newsrc-alist))
12102         bogus group entry)
12103     (gnus-message 5 "Checking bogus newsgroups...")
12104     (or gnus-have-read-active-file (gnus-read-active-file))
12105     ;; Find all bogus newsgroup that are subscribed.
12106     (while newsrc
12107       (setq group (car (car newsrc)))
12108       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12109               (nth 4 (car newsrc))      ; Foreign
12110               (and confirm
12111                    (not (gnus-y-or-n-p
12112                          (format "Remove bogus newsgroup: %s " group)))))
12113           ;; Don't remove.
12114           ()
12115         ;; Found a bogus newsgroup.
12116         (setq bogus (cons group bogus)))
12117       (setq newsrc (cdr newsrc)))
12118     ;; Remove all bogus subscribed groups by first killing them, and
12119     ;; then removing them from the list of killed groups.
12120     (while bogus
12121       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12122            (progn
12123              (gnus-group-change-level entry gnus-level-killed)
12124              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12125       (setq bogus (cdr bogus)))
12126     ;; Then we remove all bogus groups from the list of killed and
12127     ;; zombie groups. They are are removed without confirmation.
12128     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12129           killed)
12130       (while dead-lists
12131         (setq killed (symbol-value (car dead-lists)))
12132         (while killed
12133           (setq group (car killed))
12134           (or (gnus-gethash group gnus-active-hashtb)
12135               ;; The group is bogus.
12136               (set (car dead-lists)
12137                    (delete group (symbol-value (car dead-lists)))))
12138           (setq killed (cdr killed)))
12139         (setq dead-lists (cdr dead-lists))))
12140     (gnus-message 5 "Checking bogus newsgroups...done")))
12141
12142 (defun gnus-check-duplicate-killed-groups ()
12143   "Remove duplicates from the list of killed groups."
12144   (interactive)
12145   (let ((killed gnus-killed-list))
12146     (while killed
12147       (gnus-message 9 "%d" (length killed))
12148       (setcdr killed (delete (car killed) (cdr killed)))
12149       (setq killed (cdr killed)))))
12150
12151 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12152 ;; and compute how many unread articles there are in each group.
12153 (defun gnus-get-unread-articles (&optional level) 
12154   (let* ((newsrc (cdr gnus-newsrc-alist))
12155          (level (or level (1+ gnus-level-subscribed)))
12156          (foreign-level
12157           (min 
12158            (cond ((and gnus-activate-foreign-newsgroups 
12159                        (not (numberp gnus-activate-foreign-newsgroups)))
12160                   (1+ gnus-level-subscribed))
12161                  ((numberp gnus-activate-foreign-newsgroups)
12162                   gnus-activate-foreign-newsgroups)
12163                  (t 0))
12164            level))
12165          info group active virtuals method)
12166     (gnus-message 5 "Checking new news...")
12167
12168     (while newsrc
12169       (setq info (car newsrc)
12170             group (car info)
12171             active (gnus-gethash group gnus-active-hashtb))
12172
12173       ;; Check newsgroups. If the user doesn't want to check them, or
12174       ;; they can't be checked (for instance, if the news server can't
12175       ;; be reached) we just set the number of unread articles in this
12176       ;; newsgroup to t. This means that Gnus thinks that there are
12177       ;; unread articles, but it has no idea how many.
12178       (if (and (setq method (nth 4 info))
12179                (not (gnus-server-equal gnus-select-method
12180                                        (gnus-server-get-method nil method)))
12181                (not (gnus-secondary-method-p method)))
12182           ;; These groups are foreign. Check the level.
12183           (if (<= (nth 1 info) foreign-level)
12184               (if (eq (car (if (stringp method) 
12185                                (gnus-server-to-method method)
12186                              (nth 4 info))) 'nnvirtual)
12187                   ;; We have to activate the virtual groups after all
12188                   ;; the others, so we just pop them on a list for
12189                   ;; now. 
12190                   (setq virtuals (cons info virtuals))
12191                 (and (setq active (gnus-activate-newsgroup (car info)))
12192                      ;; Close the groups as we look at them!
12193                      (gnus-close-group group))))
12194                 
12195         ;; These groups are native or secondary. 
12196         (if (and (not gnus-read-active-file)
12197                  (<= (nth 1 info) level))
12198             (setq active (gnus-activate-newsgroup (car info)))))
12199       
12200       (if active
12201           (gnus-get-unread-articles-in-group info active)
12202         ;; The group couldn't be reached, so we nix out the number of
12203         ;; unread articles and stuff.
12204         (gnus-sethash group nil gnus-active-hashtb)
12205         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12206
12207       (setq newsrc (cdr newsrc)))
12208
12209     ;; Activate the virtual groups. This has to be done after all the
12210     ;; other groups. 
12211     ;; !!! If one virtual group contains another virtual group, even
12212     ;; doing it this way might cause problems.
12213    (while virtuals
12214       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12215            (gnus-get-unread-articles-in-group (car virtuals) active))
12216       (setq virtuals (cdr virtuals)))
12217
12218     (gnus-message 5 "Checking new news...done")))
12219
12220 ;; Create a hash table out of the newsrc alist. The `car's of the
12221 ;; alist elements are used as keys.
12222 (defun gnus-make-hashtable-from-newsrc-alist ()
12223   (let ((alist gnus-newsrc-alist)
12224         (ohashtb gnus-newsrc-hashtb)
12225         prev)
12226     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12227     (setq alist 
12228           (setq prev (setq gnus-newsrc-alist 
12229                            (if (equal (car (car gnus-newsrc-alist))
12230                                       "dummy.group")
12231                                gnus-newsrc-alist
12232                              (cons (list "dummy.group" 0 nil) alist)))))
12233     (while alist
12234       (gnus-sethash (car (car alist)) 
12235                     (cons (and ohashtb (car (gnus-gethash 
12236                                              (car (car alist)) ohashtb))) 
12237                           prev) gnus-newsrc-hashtb)
12238       (setq prev alist
12239             alist (cdr alist)))))
12240
12241 (defun gnus-make-hashtable-from-killed ()
12242   "Create a hash table from the killed and zombie lists."
12243   (let ((lists '(gnus-killed-list gnus-zombie-list))
12244         list)
12245     (setq gnus-killed-hashtb 
12246           (gnus-make-hashtable 
12247            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12248     (while lists
12249       (setq list (symbol-value (car lists)))
12250       (setq lists (cdr lists))
12251       (while list
12252         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12253         (setq list (cdr list))))))
12254
12255 (defun gnus-get-unread-articles-in-group (info active)
12256   (let* ((range (nth 2 info))
12257          (num 0)
12258          (marked (nth 3 info)))
12259     ;; If a cache is present, we may have to alter the active info.
12260     (and gnus-use-cache
12261          (gnus-cache-possibly-alter-active (car info) active))
12262     ;; Modify the list of read articles according to what articles 
12263     ;; are available; then tally the unread articles and add the
12264     ;; number to the group hash table entry.
12265     (cond 
12266      ((zerop (cdr active))
12267       (setq num 0))
12268      ((not range)
12269       (setq num (- (1+ (cdr active)) (car active))))
12270      ((not (listp (cdr range)))
12271       ;; Fix a single (num . num) range according to the
12272       ;; active hash table.
12273       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12274       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12275       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12276       ;; Compute number of unread articles.
12277       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12278      (t
12279       ;; The read list is a list of ranges. Fix them according to
12280       ;; the active hash table.
12281       ;; First peel off any elements that are below the lower
12282       ;; active limit. 
12283       (while (and (cdr range) 
12284                   (>= (car active) 
12285                       (or (and (atom (car (cdr range))) (car (cdr range)))
12286                           (car (car (cdr range))))))
12287         (if (numberp (car range))
12288             (setcar range 
12289                     (cons (car range) 
12290                           (or (and (numberp (car (cdr range)))
12291                                    (car (cdr range))) 
12292                               (cdr (car (cdr range))))))
12293           (setcdr (car range) 
12294                   (or (and (numberp (nth 1 range)) (nth 1 range))
12295                       (cdr (car (cdr range))))))
12296         (setcdr range (cdr (cdr range))))
12297       ;; Adjust the first element to be the same as the lower limit. 
12298       (if (and (not (atom (car range))) 
12299                (< (cdr (car range)) (car active)))
12300           (setcdr (car range) (1- (car active))))
12301       ;; Then we want to peel off any elements that are higher
12302       ;; than the upper active limit.  
12303       (let ((srange range))
12304         ;; Go past all legal elements.
12305         (while (and (cdr srange) 
12306                     (<= (or (and (atom (car (cdr srange)))
12307                                  (car (cdr srange)))
12308                             (car (car (cdr srange)))) (cdr active)))
12309           (setq srange (cdr srange)))
12310         (if (cdr srange)
12311             ;; Nuke all remaining illegal elements.
12312             (setcdr srange nil))
12313
12314         ;; Adjust the final element.
12315         (if (and (not (atom (car srange)))
12316                  (> (cdr (car srange)) (cdr active)))
12317             (setcdr (car srange) (cdr active))))
12318       ;; Compute the number of unread articles.
12319       (while range
12320         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12321                                     (cdr (car range))))
12322                             (or (and (atom (car range)) (car range))
12323                                 (car (car range))))))
12324         (setq range (cdr range)))
12325       (setq num (max 0 (- (cdr active) num)))))
12326     (and info
12327          (progn
12328            (and (assq 'tick marked)
12329                 (inline (gnus-remove-illegal-marked-articles
12330                          (assq 'tick marked) (nth 2 info))))
12331            (and (assq 'dormant marked)
12332                 (inline (gnus-remove-illegal-marked-articles
12333                          (assq 'dormant marked) (nth 2 info))))
12334            (setcar
12335             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12336             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12337                                 (length (cdr (assq 'dormant marked)))))))))
12338     num))
12339
12340 (defun gnus-remove-illegal-marked-articles (marked ranges)
12341   (let ((m (cdr marked)))
12342     ;; Make sure that all ticked articles are a subset of the unread
12343     ;; articles. 
12344     (while m
12345       (if (gnus-member-of-range (car m) ranges)
12346           (setcdr marked (cdr m))
12347         (setq marked m))
12348       (setq m (cdr m)))))
12349
12350 (defun gnus-activate-newsgroup (group)
12351   (let ((method (gnus-find-method-for-group group))
12352         active)
12353     (and (or (gnus-server-opened method) (gnus-open-server method))
12354          (gnus-request-group group)
12355          (save-excursion
12356            (set-buffer nntp-server-buffer)
12357            (goto-char (point-min))
12358            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12359                 (progn
12360                   (goto-char (match-beginning 1))
12361                   (gnus-sethash 
12362                    group (setq active (cons (read (current-buffer))
12363                                             (read (current-buffer))))
12364                    gnus-active-hashtb))
12365                 active)))))
12366
12367 (defun gnus-update-read-articles 
12368   (group unread unselected ticked &optional domarks replied expirable killed
12369          dormant bookmark score)
12370   "Update the list of read and ticked articles in GROUP using the
12371 UNREAD and TICKED lists.
12372 Note: UNSELECTED has to be sorted over `<'.
12373 Returns whether the updating was successful."
12374   (let* ((active (or gnus-newsgroup-active 
12375                      (gnus-gethash group gnus-active-hashtb)))
12376          (entry (gnus-gethash group gnus-newsrc-hashtb))
12377          (info (nth 2 entry))
12378          (marked (nth 3 info))
12379          (prev 1)
12380          (unread (sort (copy-sequence unread) (function <)))
12381          read)
12382     (if (or (not info) (not active))
12383         ;; There is no info on this group if it was, in fact,
12384         ;; killed. Gnus stores no information on killed groups, so
12385         ;; there's nothing to be done. 
12386         ;; One could store the information somewhere temporarily,
12387         ;; perhaps... Hmmm... 
12388         ()
12389       ;; Remove any negative articles numbers.
12390       (while (and unread (< (car unread) 0))
12391         (setq unread (cdr unread)))
12392       ;; Remove any expired article numbers
12393       (while (and unread (< (car unread) (car active)))
12394         (setq unread (cdr unread)))
12395       (while (and ticked (< (car ticked) (car active)))
12396         (setq ticked (cdr ticked)))
12397       (while (and dormant (< (car dormant) (car active)))
12398         (setq dormant (cdr dormant)))
12399       (setq unread (sort (append unselected unread) '<))
12400       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12401       (setcar entry (max 0 (- (length unread) (length ticked) 
12402                               (length dormant))))
12403       ;; Compute the ranges of read articles by looking at the list of
12404       ;; unread articles.  
12405       (while unread
12406         (if (/= (car unread) prev)
12407             (setq read (cons (if (= prev (1- (car unread))) prev
12408                                (cons prev (1- (car unread)))) read)))
12409         (setq prev (1+ (car unread)))
12410         (setq unread (cdr unread)))
12411       (if (<= prev (cdr active))
12412           (setq read (cons (cons prev (cdr active)) read)))
12413       ;; Enter this list into the group info.
12414       (setcar (cdr (cdr info)) 
12415               (if (> (length read) 1) (nreverse read) read))
12416       ;; Enter the list of ticked articles.
12417       (gnus-set-marked-articles 
12418        info ticked
12419        (if domarks replied (cdr (assq 'reply marked)))
12420        (if domarks expirable (cdr (assq 'expire marked)))
12421        (if domarks killed (cdr (assq 'killed marked)))
12422        (if domarks dormant (cdr (assq 'dormant marked)))
12423        (if domarks bookmark (cdr (assq 'bookmark marked)))
12424        (if domarks score (cdr (assq 'score marked))))
12425       t)))
12426
12427 (defun gnus-make-articles-unread (group articles)
12428   "Mark ARTICLES in GROUP as unread."
12429   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12430                           (gnus-gethash (gnus-group-real-name group)
12431                                         gnus-newsrc-hashtb))))
12432          (ranges (nth 2 info))
12433          news)
12434     (while articles
12435       (and (gnus-member-of-range (car articles) ranges)
12436            (setq news (cons (car articles) news)))
12437       (setq articles (cdr articles)))
12438     (if (not news)
12439         ()
12440       (setcar (nthcdr 2 info)
12441               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12442       (gnus-group-update-group group t))))
12443
12444 ;; Get the active file(s) from the backend(s).
12445 (defun gnus-read-active-file ()
12446   (gnus-group-set-mode-line)
12447   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12448                          (gnus-open-server gnus-select-method))
12449                      ;; The native server is available.
12450                      (cons gnus-select-method gnus-secondary-select-methods)
12451                    ;; The native server is down, so we just do the
12452                    ;; secondary ones.   
12453                    gnus-secondary-select-methods))
12454         list-type)
12455     (setq gnus-have-read-active-file nil)
12456     (save-excursion
12457       (set-buffer nntp-server-buffer)
12458       (while methods
12459         (let* ((where (nth 1 (car methods)))
12460                (mesg (format "Reading active file%s via %s..."
12461                              (if (and where (not (zerop (length where))))
12462                                  (concat " from " where) "")
12463                              (car (car methods)))))
12464           (gnus-message 5 mesg)
12465           (gnus-check-news-server (car methods))
12466           (cond 
12467            ((and (eq gnus-read-active-file 'some)
12468                  (gnus-check-backend-function
12469                   'retrieve-groups (car (car methods))))
12470             (let ((newsrc (cdr gnus-newsrc-alist))
12471                   groups)
12472               (while newsrc
12473                 (and (gnus-server-equal 
12474                       (gnus-find-method-for-group
12475                        (car (car newsrc)) (car newsrc))
12476                       (gnus-server-get-method nil (car methods)))
12477                      (setq groups (cons (car (car newsrc)) groups)))
12478                 (setq newsrc (cdr newsrc)))
12479               (setq list-type (gnus-retrieve-groups groups (car methods)))
12480               (cond ((not list-type)
12481                      (gnus-message 
12482                       1 "Cannot read partial active file from %s server." 
12483                       (car (car methods)))
12484                      (ding)
12485                      (sit-for 2))
12486                     ((eq list-type 'active)
12487                      (gnus-active-to-gnus-format (car methods)))
12488                     (t
12489                      (gnus-groups-to-gnus-format (car methods))))))
12490            (t
12491             (if (not (gnus-request-list (car methods)))
12492                 (progn
12493                   (gnus-message 1 "Cannot read active file from %s server." 
12494                                 (car (car methods)))
12495                   (ding))
12496               (gnus-active-to-gnus-format (car methods))
12497               ;; We mark this active file as read.
12498               (setq gnus-have-read-active-file
12499                     (cons (car methods) gnus-have-read-active-file))
12500               (gnus-message 5 "%sdone" mesg)))))
12501         (setq methods (cdr methods))))))
12502
12503 ;; Read an active file and place the results in `gnus-active-hashtb'.
12504 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12505   (let ((cur (current-buffer))
12506         (hashtb (or hashtb 
12507                     (if (and gnus-active-hashtb 
12508                              (not (equal method gnus-select-method)))
12509                         gnus-active-hashtb
12510                       (setq gnus-active-hashtb
12511                             (if (equal method gnus-select-method)
12512                                 (gnus-make-hashtable 
12513                                  (count-lines (point-min) (point-max)))
12514                               (gnus-make-hashtable 4096))))))
12515         (flag-hashtb (gnus-make-hashtable 60)))
12516     ;; Delete unnecessary lines.
12517     (goto-char (point-min))
12518     (while (search-forward "\nto." nil t)
12519       (delete-region (1+ (match-beginning 0)) 
12520                      (progn (forward-line 1) (point))))
12521     (or (string= gnus-ignored-newsgroups "")
12522         (progn
12523           (goto-char (point-min))
12524           (delete-matching-lines gnus-ignored-newsgroups)))
12525     ;; If these are groups from a foreign select method, we insert the
12526     ;; group prefix in front of the group names. 
12527     (and method (not (eq method gnus-select-method))
12528          (let ((prefix (gnus-group-prefixed-name "" method)))
12529            (goto-char (point-min))
12530            (while (and (not (eobp))
12531                        (progn (insert prefix)
12532                               (zerop (forward-line 1)))))))
12533     (goto-char (point-min))
12534     ;; Store active file in hashtable.
12535     (goto-char (point-min))
12536     (if (string-match "%[oO]" gnus-group-line-format)
12537         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12538         ;; If we want information on moderated groups, we use this
12539         ;; loop...   
12540         (let* ((mod-hashtb (make-vector 7 0))
12541                (m (intern "m" mod-hashtb))
12542                group max min)
12543           (while (not (eobp))
12544             (condition-case nil
12545                 (progn
12546                   (narrow-to-region (point) (gnus-point-at-eol))
12547                   (setq group (let ((obarray hashtb)) (read cur)))
12548                   (if (and (numberp (setq max (read cur)))
12549                            (numberp (setq min (read cur)))
12550                            (progn 
12551                              (skip-chars-forward " ")
12552                              (not
12553                               (or (= (following-char) ?=)
12554                                   (= (following-char) ?x)
12555                                   (= (following-char) ?j)))))
12556                       (set group (cons min max))
12557                     (set group nil))
12558                   ;; Enter moderated groups into a list.
12559                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12560                       (setq gnus-moderated-list 
12561                             (cons (symbol-name group) gnus-moderated-list))))
12562               (error nil))
12563             (widen)
12564             (forward-line 1)))
12565       ;; And if we do not care about moderation, we use this loop,
12566       ;; which is faster.
12567       (let (group max min)
12568         (while (not (eobp))
12569           (condition-case ()
12570               (progn
12571                 (narrow-to-region (point) (gnus-point-at-eol))
12572                 ;; group gets set to a symbol interned in the hash table
12573                 ;; (what a hack!!) - jwz
12574                 (setq group (let ((obarray hashtb)) (read cur)))
12575                 (if (and (numberp (setq max (read cur)))
12576                          (numberp (setq min (read cur)))
12577                          (progn 
12578                            (skip-chars-forward " ")
12579                            (not
12580                             (or (= (following-char) ?=)
12581                                 (= (following-char) ?x)
12582                                 (= (following-char) ?j)))))
12583                     (set group (cons min max))
12584                   (set group nil)))
12585             (error 
12586              (progn 
12587                (if ignore-errors
12588                    (set group nil)
12589                  (ding) 
12590                  (gnus-message 3 "Warning - illegal active: %s"
12591                                (buffer-substring 
12592                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12593                  nil))))
12594           (widen)
12595           (forward-line 1))))))
12596
12597 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12598   ;; Parse a "groups" active file.
12599   (let ((cur (current-buffer))
12600         (hashtb (or hashtb 
12601                     (if method
12602                         gnus-active-hashtb
12603                       (setq gnus-active-hashtb
12604                             (gnus-make-hashtable 
12605                              (count-lines (point-min) (point-max)))))))
12606         (prefix (and method (not (eq method gnus-select-method))
12607                      (gnus-group-prefixed-name "" method))))
12608
12609     (goto-char (point-min))
12610     (condition-case ()
12611         ;; We split this into to separate loops, one with the prefix
12612         ;; and one without to speed the reading up somewhat.
12613         (if prefix
12614             (let (min max opoint)
12615               (while (not (eobp))
12616                 (read cur) (read cur)
12617                 (setq min (read cur)
12618                       max (read cur)
12619                       opoint (point))
12620                 (skip-chars-forward " \t")
12621                 (insert prefix)
12622                 (goto-char opoint)
12623                 (set (let ((obarray hashtb)) (read cur)) 
12624                      (cons min max))
12625                 (forward-line 1)))
12626           (let (min max)
12627             (while (not (eobp))
12628               (if (= (following-char) ?2)
12629                   (progn
12630                     (read cur) (read cur)
12631                     (setq min (read cur)
12632                           max (read cur))
12633                     (set (let ((obarray hashtb)) (read cur)) 
12634                          (cons min max))))
12635               (forward-line 1))))
12636       (error 
12637        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12638
12639 (defun gnus-read-newsrc-file (&optional force)
12640   "Read startup file.
12641 If FORCE is non-nil, the .newsrc file is read."
12642   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12643   ;; Reset variables that might be defined in the .newsrc.eld file.
12644   (let ((variables gnus-variable-list))
12645     (while variables
12646       (set (car variables) nil)
12647       (setq variables (cdr variables))))
12648   (let* ((newsrc-file gnus-current-startup-file)
12649          (quick-file (concat newsrc-file ".el")))
12650     (save-excursion
12651       ;; We always load the .newsrc.eld file. If always contains
12652       ;; much information that can not be gotten from the .newsrc
12653       ;; file (ticked articles, killed groups, foreign methods, etc.)
12654       (gnus-read-newsrc-el-file quick-file)
12655  
12656       (if (or force
12657               (and (file-newer-than-file-p newsrc-file quick-file)
12658                    (file-newer-than-file-p newsrc-file 
12659                                            (concat quick-file "d")))
12660               (not gnus-newsrc-alist))
12661           ;; We read the .newsrc file. Note that if there if a
12662           ;; .newsrc.eld file exists, it has already been read, and
12663           ;; the `gnus-newsrc-hashtb' has been created. While reading
12664           ;; the .newsrc file, Gnus will only use the information it
12665           ;; can find there for changing the data already read -
12666           ;; ie. reading the .newsrc file will not trash the data
12667           ;; already read (except for read articles).
12668           (save-excursion
12669             (gnus-message 5 "Reading %s..." newsrc-file)
12670             (set-buffer (find-file-noselect newsrc-file))
12671             (buffer-disable-undo (current-buffer))
12672             (gnus-newsrc-to-gnus-format)
12673             (kill-buffer (current-buffer))
12674             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12675
12676 (defun gnus-read-newsrc-el-file (file)
12677   (let ((ding-file (concat file "d")))
12678     ;; We always, always read the .eld file.
12679     (gnus-message 5 "Reading %s..." ding-file)
12680     (let (gnus-newsrc-assoc)
12681       (condition-case nil
12682           (load ding-file t t t)
12683         (error nil))
12684       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12685     (let ((inhibit-quit t))
12686       (gnus-uncompress-newsrc-alist))
12687     (gnus-make-hashtable-from-newsrc-alist)
12688     (if (not (file-newer-than-file-p file ding-file))
12689         ()
12690       ;; Old format quick file
12691       (gnus-message 5 "Reading %s..." file)
12692       ;; The .el file is newer than the .eld file, so we read that one
12693       ;; as well. 
12694       (gnus-read-old-newsrc-el-file file))))
12695
12696 ;; Parse the old-style quick startup file
12697 (defun gnus-read-old-newsrc-el-file (file)
12698   (let (newsrc killed marked group m)
12699     (prog1
12700         (let ((gnus-killed-assoc nil)
12701               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12702           (prog1
12703               (condition-case nil
12704                   (load file t t t)
12705                 (error nil))
12706             (setq newsrc gnus-newsrc-assoc
12707                   killed gnus-killed-assoc
12708                   marked gnus-marked-assoc)))
12709       (setq gnus-newsrc-alist nil)
12710       (while newsrc
12711         (setq group (car newsrc))
12712         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12713           (if info
12714               (progn
12715                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12716                 (setcar (cdr info)
12717                         (if (nth 1 group) gnus-level-default-subscribed 
12718                           gnus-level-default-unsubscribed))
12719                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12720             (setq gnus-newsrc-alist
12721                   (cons 
12722                    (setq info
12723                          (list (car group)
12724                                (if (nth 1 group) gnus-level-default-subscribed
12725                                  gnus-level-default-unsubscribed) 
12726                                (cdr (cdr group))))
12727                    gnus-newsrc-alist)))
12728           (if (setq m (assoc (car group) marked))
12729             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12730         (setq newsrc (cdr newsrc)))
12731       (setq newsrc killed)
12732       (while newsrc
12733         (setcar newsrc (car (car newsrc)))
12734         (setq newsrc (cdr newsrc)))
12735       (setq gnus-killed-list killed))
12736     ;; The .el file version of this variable does not begin with
12737     ;; "options", while the .eld version does, so we just add it if it
12738     ;; isn't there.
12739     (and
12740      gnus-newsrc-options 
12741      (progn
12742        (and (not (string-match "^ *options" gnus-newsrc-options))
12743             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12744        (and (not (string-match "\n$" gnus-newsrc-options))
12745             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12746     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12747     (gnus-make-hashtable-from-newsrc-alist)))
12748       
12749 (defun gnus-make-newsrc-file (file)
12750   "Make server dependent file name by catenating FILE and server host name."
12751   (let* ((file (expand-file-name file nil))
12752          (real-file (concat file "-" (nth 1 gnus-select-method))))
12753     (if (or (file-exists-p real-file)
12754             (file-exists-p (concat real-file ".el"))
12755             (file-exists-p (concat real-file ".eld")))
12756         real-file file)))
12757
12758 (defun gnus-uncompress-newsrc-alist ()
12759   ;; Uncompress all lists of marked articles in the newsrc assoc.
12760   (let ((newsrc gnus-newsrc-alist)
12761         marked)
12762     (while newsrc
12763       (if (not (setq marked (nth 3 (car newsrc))))
12764           ()
12765         (while marked
12766           (or (eq 'score (car (car marked)))
12767               (eq 'bookmark (car (car marked)))
12768               (eq 'killed (car (car marked)))
12769               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12770           (setq marked (cdr marked))))
12771       (setq newsrc (cdr newsrc)))))
12772
12773 (defun gnus-compress-newsrc-alist ()
12774   ;; Compress all lists of marked articles in the newsrc assoc.
12775   (let ((newsrc gnus-newsrc-alist)
12776         marked)
12777     (while newsrc
12778       (if (not (setq marked (nth 3 (car newsrc))))
12779           ()
12780         (while marked
12781           (or (eq 'score (car (car marked)))
12782               (eq 'bookmark (car (car marked)))
12783               (eq 'killed (car (car marked)))
12784               (setcdr (car marked) 
12785                       (condition-case ()
12786                           (gnus-compress-sequence 
12787                            (sort (cdr (car marked)) '<) t)
12788                         (error (cdr (car marked))))))
12789           (setq marked (cdr marked))))
12790       (setq newsrc (cdr newsrc)))))
12791
12792 (defun gnus-newsrc-to-gnus-format ()
12793   (setq gnus-newsrc-options "")
12794   (setq gnus-newsrc-options-n nil)
12795
12796   (or gnus-active-hashtb
12797       (setq gnus-active-hashtb (make-vector 4095 0)))
12798   (let ((buf (current-buffer))
12799         (already-read (> (length gnus-newsrc-alist) 1))
12800         group subscribed options-symbol newsrc Options-symbol
12801         symbol reads num1)
12802     (goto-char (point-min))
12803     ;; We intern the symbol `options' in the active hashtb so that we
12804     ;; can `eq' against it later.
12805     (setq options-symbol (intern "options" gnus-active-hashtb))
12806     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12807   
12808     (while (not (eobp))
12809       ;; We first read the first word on the line by narrowing and
12810       ;; then reading into `gnus-active-hashtb'.  Most groups will
12811       ;; already exist in that hashtb, so this will save some string
12812       ;; space.
12813       (narrow-to-region
12814        (point)
12815        (progn (skip-chars-forward "^ \t!:\n") (point)))
12816       (goto-char (point-min))
12817       (setq symbol 
12818             (and (/= (point-min) (point-max))
12819                  (let ((obarray gnus-active-hashtb)) (read buf))))
12820       (widen)
12821       ;; Now, the symbol we have read is either `options' or a group
12822       ;; name.  If it is an options line, we just add it to a string. 
12823       (cond 
12824        ((or (eq symbol options-symbol)
12825             (eq symbol Options-symbol))
12826         (setq gnus-newsrc-options
12827               ;; This concatting is quite inefficient, but since our
12828               ;; thorough studies show that approx 99.37% of all
12829               ;; .newsrc files only contain a single options line, we
12830               ;; don't give a damn, frankly, my dear.
12831               (concat gnus-newsrc-options
12832                       (buffer-substring 
12833                        (gnus-point-at-bol)
12834                        ;; Options may continue on the next line.
12835                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12836                                 (progn (beginning-of-line) (point)))
12837                            (point))))))
12838        (symbol
12839         ;; It was a group name.
12840         (setq subscribed (= (following-char) ?:)
12841               group (symbol-name symbol)
12842               reads nil)
12843         (if (eolp)
12844             ;; If the line ends here, this is clearly a buggy line, so
12845             ;; we put point a the beginning of line and let the cond
12846             ;; below do the error handling.
12847             (beginning-of-line)
12848           ;; We skip to the beginning of the ranges.
12849           (skip-chars-forward "!: \t"))
12850         ;; We are now at the beginning of the list of read articles.
12851         ;; We read them range by range.
12852         (while
12853             (cond 
12854              ((looking-at "[0-9]+")
12855               ;; We narrow and read a number instead of buffer-substring/
12856               ;; string-to-int because it's faster. narrow/widen is
12857               ;; faster than save-restriction/narrow, and save-restriction
12858               ;; produces a garbage object.
12859               (setq num1 (progn
12860                            (narrow-to-region (match-beginning 0) (match-end 0))
12861                            (read buf)))
12862               (widen)
12863               ;; If the next character is a dash, then this is a range.
12864               (if (= (following-char) ?-)
12865                   (progn
12866                     ;; We read the upper bound of the range.
12867                     (forward-char 1)
12868                     (if (not (looking-at "[0-9]+"))
12869                         ;; This is a buggy line, by we pretend that
12870                         ;; it's kinda OK. Perhaps the user should be
12871                         ;; dinged? 
12872                         (setq reads (cons num1 reads))
12873                       (setq reads 
12874                             (cons 
12875                              (cons num1 (progn
12876                                           (narrow-to-region (match-beginning 0) 
12877                                                             (match-end 0))
12878                                           (read buf)))
12879                              reads))
12880                       (widen)))
12881                 ;; It was just a simple number, so we add it to the
12882                 ;; list of ranges.
12883                 (setq reads (cons num1 reads)))
12884               ;; If the next char in ?\n, then we have reached the end
12885               ;; of the line and return nil.
12886               (/= (following-char) ?\n))
12887              ((= (following-char) ?\n)
12888               ;; End of line, so we end.
12889               nil)
12890              (t
12891               ;; Not numbers and not eol, so this might be a buggy
12892               ;; line... 
12893               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12894                   (progn
12895                     ;; The line was buggy.
12896                     (setq group nil)
12897                     (gnus-message 3 "Mangled line: %s" 
12898                                   (buffer-substring (gnus-point-at-bol) 
12899                                                     (gnus-point-at-eol)))
12900                     (ding)
12901                     (sit-for 1)))
12902               nil))
12903           ;; Skip past ", ". Spaces are illegal in these ranges, but
12904           ;; we allow them, because it's a common mistake to put a
12905           ;; space after the comma.
12906           (skip-chars-forward ", "))
12907
12908         ;; We have already read .newsrc.eld, so we gently update the
12909         ;; data in the hash table with the information we have just
12910         ;; read. 
12911         (if (not group)
12912             ()
12913           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12914                 level)
12915             (if info
12916                 ;; There is an entry for this file in the alist.
12917                 (progn
12918                   (setcar (nthcdr 2 info) (nreverse reads))
12919                   ;; We update the level very gently.  In fact, we
12920                   ;; only change it if there's been a status change
12921                   ;; from subscribed to unsubscribed, or vice versa.
12922                   (setq level (nth 1 info))
12923                   (cond ((and (<= level gnus-level-subscribed)
12924                               (not subscribed))
12925                          (setq level (if reads
12926                                          gnus-level-default-unsubscribed 
12927                                        (1+ gnus-level-default-unsubscribed))))
12928                         ((and (> level gnus-level-subscribed) subscribed)
12929                          (setq level gnus-level-default-subscribed)))
12930                   (setcar (cdr info) level))
12931               ;; This is a new group.
12932               (setq info (list group 
12933                                (if subscribed
12934                                    gnus-level-default-subscribed 
12935                                  (if reads
12936                                      (1+ gnus-level-subscribed)
12937                                    gnus-level-default-unsubscribed))
12938                                (nreverse reads))))
12939             (setq newsrc (cons info newsrc))))))
12940       (forward-line 1))
12941     
12942     (setq newsrc (nreverse newsrc))
12943
12944     (if (not already-read)
12945         ()
12946       ;; We now have two newsrc lists - `newsrc', which is what we
12947       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12948       ;; what we've read from .newsrc.eld. We have to merge these
12949       ;; lists. We do this by "attaching" any (foreign) groups in the
12950       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12951       (let ((rc (cdr gnus-newsrc-alist))
12952             (prev gnus-newsrc-alist)
12953             entry mentry)
12954         (while rc
12955           (or (null (nth 4 (car rc))) ; It's a native group.
12956               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12957               (if (setq entry (assoc (car (car prev)) newsrc))
12958                   (setcdr (setq mentry (memq entry newsrc))
12959                           (cons (car rc) (cdr mentry)))
12960                 (setq newsrc (cons (car rc) newsrc))))
12961           (setq prev rc
12962                 rc (cdr rc)))))
12963
12964     (setq gnus-newsrc-alist newsrc)
12965     ;; We make the newsrc hashtb.
12966     (gnus-make-hashtable-from-newsrc-alist)
12967
12968     ;; Finally, if we read some options lines, we parse them.
12969     (or (string= gnus-newsrc-options "")
12970         (gnus-newsrc-parse-options gnus-newsrc-options))))
12971
12972 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12973 ;; The return value will be a list on the form
12974 ;; ((regexp1 . ignore)
12975 ;;  (regexp2 . subscribe)...)
12976 ;; When handling new newsgroups, groups that match a `ignore' regexp
12977 ;; will be ignored, and groups that match a `subscribe' regexp will be
12978 ;; subscribed. A line like
12979 ;; options -n !all rec.all
12980 ;; will lead to a list that looks like
12981 ;; (("^rec\\..+" . subscribe) 
12982 ;;  ("^.+" . ignore))
12983 ;; So all "rec.*" groups will be subscribed, while all the other
12984 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12985 ;; different from "options -n rec.all !all". 
12986 (defun gnus-newsrc-parse-options (options)
12987   (let (out eol)
12988     (save-excursion
12989       (gnus-set-work-buffer)
12990       (insert (regexp-quote options))
12991       ;; First we treat all continuation lines.
12992       (goto-char (point-min))
12993       (while (re-search-forward "\n[ \t]+" nil t)
12994         (replace-match " " t t))
12995       ;; Then we transform all "all"s into ".+"s.
12996       (goto-char (point-min))
12997       (while (re-search-forward "\\ball\\b" nil t)
12998         (replace-match ".+" t t))
12999       (goto-char (point-min))
13000       ;; We remove all other options than the "-n" ones.
13001       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13002         (replace-match " ")
13003         (forward-char -1))
13004       (goto-char (point-min))
13005
13006       ;; We are only interested in "options -n" lines - we
13007       ;; ignore the other option lines.
13008       (while (re-search-forward "[ \t]-n" nil t)
13009         (setq eol 
13010               (or (save-excursion
13011                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13012                          (- (point) 2)))
13013                   (gnus-point-at-eol)))
13014         ;; Search for all "words"...
13015         (while (re-search-forward "[^ \t,\n]+" eol t)
13016           (if (= (char-after (match-beginning 0)) ?!)
13017               ;; If the word begins with a bang (!), this is a "not"
13018               ;; spec. We put this spec (minus the bang) and the
13019               ;; symbol `ignore' into the list.
13020               (setq out (cons (cons (concat 
13021                                      "^" (buffer-substring 
13022                                           (1+ (match-beginning 0))
13023                                           (match-end 0)))
13024                                     'ignore) out))
13025             ;; There was no bang, so this is a "yes" spec.
13026             (setq out (cons (cons (concat 
13027                                    "^" (buffer-substring (match-beginning 0)
13028                                                          (match-end 0)))
13029                                   'subscribe) out)))))
13030     
13031       (setq gnus-newsrc-options-n out))))
13032                
13033
13034 (defun gnus-save-newsrc-file ()
13035   "Save .newsrc file."
13036   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13037   ;; from the variable gnus-newsrc-alist.
13038   (and (or gnus-newsrc-alist gnus-killed-list)
13039        gnus-current-startup-file
13040        (progn
13041          (run-hooks 'gnus-save-newsrc-hook)
13042          (save-excursion
13043            (if (or (not gnus-dribble-buffer)
13044                    (not (buffer-name gnus-dribble-buffer))
13045                    (zerop (save-excursion
13046                             (set-buffer gnus-dribble-buffer)
13047                             (buffer-size))))
13048                (gnus-message 4 "(No changes need to be saved)")
13049              (if gnus-save-newsrc-file
13050                  (progn
13051                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13052                    ;; Make backup file of master newsrc.
13053                    (gnus-gnus-to-newsrc-format)
13054                    (gnus-message 5 "Saving %s...done"
13055                                  gnus-current-startup-file)))
13056              ;; Quickly loadable .newsrc.
13057              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13058              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13059              (gnus-add-current-to-buffer-list)
13060              (buffer-disable-undo (current-buffer))
13061              (erase-buffer)
13062              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13063              (gnus-gnus-to-quick-newsrc-format)
13064              (save-buffer)
13065              (kill-buffer (current-buffer))
13066              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13067              (gnus-dribble-delete-file))))))
13068
13069 (defun gnus-gnus-to-quick-newsrc-format ()
13070   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13071   (insert ";; (ding) Gnus startup file.\n")
13072   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13073   (insert ";; to read .newsrc.\n")
13074   (let ((variables gnus-variable-list)
13075         (inhibit-quit t)
13076         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13077         variable)
13078     ;; insert lisp expressions.
13079     (gnus-compress-newsrc-alist)
13080     (while variables
13081       (setq variable (car variables))
13082       (and (boundp variable)
13083            (symbol-value variable)
13084            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13085            (insert "(setq " (symbol-name variable) " '"
13086                    (prin1-to-string (symbol-value variable))
13087                    ")\n"))
13088       (setq variables (cdr variables)))
13089     (gnus-uncompress-newsrc-alist)))
13090
13091
13092 (defun gnus-gnus-to-newsrc-format ()
13093   ;; Generate and save the .newsrc file.
13094   (let ((newsrc (cdr gnus-newsrc-alist))
13095         info ranges range)
13096     (save-excursion
13097       (set-buffer (create-file-buffer gnus-current-startup-file))
13098       (set-visited-file-name gnus-current-startup-file)
13099       (buffer-disable-undo (current-buffer))
13100       (erase-buffer)
13101       ;; Write options.
13102       (if gnus-newsrc-options (insert gnus-newsrc-options))
13103       ;; Write subscribed and unsubscribed.
13104       (while newsrc
13105         (setq info (car newsrc))
13106         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13107             (progn
13108               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13109                                      "!" ":"))
13110               (if (setq ranges (nth 2 info))
13111                   (progn
13112                     (insert " ")
13113                     (if (not (listp (cdr ranges)))
13114                         (if (= (car ranges) (cdr ranges))
13115                             (insert (int-to-string (car ranges)))
13116                           (insert (int-to-string (car ranges)) "-" 
13117                                   (int-to-string (cdr ranges))))
13118                       (while ranges
13119                         (setq range (car ranges)
13120                               ranges (cdr ranges))
13121                         (if (or (atom range) (= (car range) (cdr range)))
13122                             (insert (int-to-string 
13123                                      (or (and (atom range) range) 
13124                                          (car range))))
13125                           (insert (int-to-string (car range)) "-"
13126                                   (int-to-string (cdr range))))
13127                         (if ranges (insert ","))))))
13128               (insert "\n")))
13129         (setq newsrc (cdr newsrc)))
13130       (save-buffer)
13131       (kill-buffer (current-buffer)))))
13132
13133 (defun gnus-read-all-descriptions-files ()
13134   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13135     (while methods
13136       (gnus-read-descriptions-file (car methods))
13137       (setq methods (cdr methods)))
13138     t))
13139
13140 (defun gnus-read-descriptions-file (&optional method)
13141   (let ((method (or method gnus-select-method)))
13142     ;; We create the hashtable whether we manage to read the desc file
13143     ;; to avoid trying to re-read after a failed read.
13144     (or gnus-description-hashtb
13145         (setq gnus-description-hashtb 
13146               (gnus-make-hashtable (length gnus-active-hashtb))))
13147     ;; Mark this method's desc file as read.
13148     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13149                   gnus-description-hashtb)
13150
13151     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13152     (cond 
13153      ((not (or (gnus-server-opened method)
13154                (gnus-open-server method)))
13155       (gnus-message 1 "Couldn't open server")
13156       nil)
13157      ((not (gnus-request-list-newsgroups method))
13158       (gnus-message 1 "Couldn't read newsgroups descriptions")
13159       nil)
13160      (t
13161       (let (group)
13162         (save-excursion
13163           (save-restriction
13164             (set-buffer nntp-server-buffer)
13165             (goto-char (point-min))
13166             (if (or (search-forward "\n.\n" nil t)
13167                     (goto-char (point-max)))
13168                 (progn
13169                   (beginning-of-line)
13170                   (narrow-to-region (point-min) (point))))
13171             (goto-char (point-min))
13172             (while (not (eobp))
13173               ;; If we get an error, we set group to 0, which is not a
13174               ;; symbol... 
13175               (setq group 
13176                     (condition-case ()
13177                         (let ((obarray gnus-description-hashtb))
13178                           ;; Group is set to a symbol interned in this
13179                           ;; hash table.
13180                           (read nntp-server-buffer))
13181                       (error 0)))
13182               (skip-chars-forward " \t")
13183               ;; ... which leads to this line being effectively ignored.
13184               (and (symbolp group)
13185                    (set group (buffer-substring 
13186                                (point) (progn (end-of-line) (point)))))
13187               (forward-line 1))))
13188         (gnus-message 5 "Reading descriptions file...done")
13189         t)))))
13190
13191 (defun gnus-group-get-description (group)
13192   ;; Get the description of a group by sending XGTITLE to the server.
13193   (and (gnus-request-group-description group)
13194        (save-excursion
13195          (set-buffer nntp-server-buffer)
13196          (goto-char (point-min))
13197          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13198               (buffer-substring (match-beginning 1) (match-end 1))))))
13199
13200 ;;;
13201 ;;; Server
13202 ;;;
13203
13204 (defvar gnus-server-mode-hook nil
13205   "Hook run in `gnus-server-mode' buffers.")
13206
13207 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13208   "Format of server lines.
13209 It works along the same lines as a normal formatting string,
13210 with some simple extensions.")
13211
13212 (defvar gnus-server-mode-line-format "(ding) List of servers"
13213   "The format specification for the server mode line.")
13214
13215 (defconst gnus-server-line-format-alist
13216   (list (list ?h 'how ?s)
13217         (list ?n 'name ?s)
13218         (list ?w 'where ?s)
13219         ))
13220
13221 (defconst gnus-server-mode-line-format-alist 
13222   (list (list ?S 'news-server ?s)
13223         (list ?M 'news-method ?s)
13224         (list ?u 'user-defined ?s)))
13225
13226 (defvar gnus-server-line-format-spec nil)
13227 (defvar gnus-server-mode-line-format-spec nil)
13228 (defvar gnus-server-killed-servers nil)
13229
13230 (defvar gnus-server-mode-map nil)
13231 (put 'gnus-server-mode 'mode-class 'special)
13232
13233 (if gnus-server-mode-map
13234     nil
13235   (setq gnus-server-mode-map (make-sparse-keymap))
13236   (suppress-keymap gnus-server-mode-map)
13237   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13238   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13239   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13240   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13241   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13242   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13243   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13244   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13245   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13246   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13247
13248 (defun gnus-server-mode ()
13249   "Major mode for listing and editing servers.
13250
13251 All normal editing commands are switched off.
13252 \\<gnus-server-mode-map>
13253
13254 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13255
13256 The following commands are available:
13257
13258 \\{gnus-server-mode-map}"
13259   (interactive)
13260   (if gnus-visual (gnus-server-make-menu-bar))
13261   (kill-all-local-variables)
13262   (setq mode-line-modified "-- ")
13263   (make-local-variable 'mode-line-format)
13264   (setq mode-line-format (copy-sequence mode-line-format))
13265   (and (equal (nth 3 mode-line-format) "   ")
13266        (setcar (nthcdr 3 mode-line-format) ""))
13267   (setq major-mode 'gnus-server-mode)
13268   (setq mode-name "Server")
13269 ;  (gnus-group-set-mode-line)
13270   (setq mode-line-process nil)
13271   (use-local-map gnus-server-mode-map)
13272   (buffer-disable-undo (current-buffer))
13273   (setq truncate-lines t)
13274   (setq buffer-read-only t)
13275   (run-hooks 'gnus-server-mode-hook))
13276
13277 (defun gnus-server-insert-server-line (sformat name method)
13278   (let* ((sformat (or sformat gnus-server-line-format-spec))
13279          (how (car method))
13280          (where (nth 1 method))
13281          b)
13282     (beginning-of-line)
13283     (setq b (point))
13284     ;; Insert the text.
13285     (insert (eval sformat))
13286     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13287
13288 (defun gnus-server-setup-buffer ()
13289   (if (get-buffer gnus-server-buffer)
13290       ()
13291     (save-excursion
13292       (set-buffer (get-buffer-create gnus-server-buffer))
13293       (gnus-server-mode)
13294       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13295
13296 (defun gnus-server-prepare ()
13297   (setq gnus-server-mode-line-format-spec 
13298         (gnus-parse-format gnus-server-mode-line-format 
13299                            gnus-server-mode-line-format-alist))
13300   (setq gnus-server-line-format-spec 
13301         (gnus-parse-format gnus-server-line-format 
13302                            gnus-server-line-format-alist))
13303   (let ((alist gnus-server-alist)
13304         (buffer-read-only nil))
13305     (erase-buffer)
13306     (while alist
13307       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13308       (setq alist (cdr alist))))
13309   (goto-char (point-min))
13310   (gnus-server-position-cursor))
13311
13312 (defun gnus-server-server-name ()
13313   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13314     (and server (symbol-name server))))
13315
13316 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13317
13318 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13319
13320 (defun gnus-server-update-server (server)
13321   (save-excursion
13322     (set-buffer gnus-server-buffer)
13323     (let ((buffer-read-only nil)
13324           (info (cdr (assoc server gnus-server-alist))))
13325       (gnus-dribble-enter 
13326        (concat "(gnus-server-set-info \"" server "\" '"
13327                (prin1-to-string info) ")"))
13328       ;; Buffer may be narrowed.
13329       (save-restriction
13330         (widen)
13331         (if (gnus-server-goto-server server)
13332             (delete-region (progn (beginning-of-line) (point))
13333                            (progn (forward-line 1) (point))))
13334         (let ((entry (assoc server gnus-server-alist)))
13335           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13336           (gnus-server-position-cursor))))))
13337
13338 (defun gnus-server-set-info (server info)
13339   ;; Enter a select method into the virtual server alist.
13340   (gnus-dribble-enter 
13341    (concat "(gnus-server-set-info \"" server "\" '"
13342            (prin1-to-string info) ")"))
13343   (let* ((server (nth 1 info))
13344          (entry (assoc server gnus-server-alist)))
13345     (if entry (setcdr entry info)
13346       (setq gnus-server-alist
13347             (nconc gnus-server-alist (list (cons server info)))))))
13348
13349 (defun gnus-server-to-method (server)
13350   ;; Map virtual server names to select methods.
13351   (or (and (equal server "native") gnus-select-method)
13352       (cdr (assoc server gnus-server-alist))))
13353
13354 (defun gnus-server-extend-method (group method)
13355   ;; This function "extends" a virtual server.  If the server is
13356   ;; "hello", and the select method is ("hello" (my-var "something")) 
13357   ;; in the group "alt.alt", this will result in a new virtual server
13358   ;; called "helly+alt.alt".
13359   (let ((entry
13360          (gnus-copy-sequence 
13361           (if (equal (car method) "native") gnus-select-method
13362               (cdr (assoc (car method) gnus-server-alist))))))
13363     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13364     (nconc entry (cdr method))))
13365
13366 (defun gnus-server-get-method (group method)
13367   ;; Input either a server name, and extended server name, or a
13368   ;; select method, and return a select method. 
13369   (cond ((stringp method)
13370          (gnus-server-to-method method))
13371         ((stringp (car method))
13372          (gnus-server-extend-method group method))
13373         (t
13374          (gnus-server-add-address method))))
13375
13376 (defun gnus-server-add-address (method)
13377   (let ((method-name (symbol-name (car method))))
13378     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13379              (not (assq (intern (concat method-name "-address")) method)))
13380         (append method (list (list (intern (concat method-name "-address"))
13381                                    (nth 1 method))))
13382       method)))
13383
13384 (defun gnus-server-equal (s1 s2)
13385   (or (equal s1 s2)
13386       (and (= (length s1) (length s2))
13387            (progn
13388              (while (and s1 (member (car s1) s2))
13389                (setq s1 (cdr s1)))
13390              (null s1)))))
13391
13392 ;;; Interactive server functions.
13393
13394 (defun gnus-server-kill-server (server)
13395   "Kill the server on the current line."
13396   (interactive (list (gnus-server-server-name)))
13397   (or (gnus-server-goto-server server)
13398       (if server (error "No such server: %s" server)
13399         (error "No server on the current line")))
13400   (let ((buffer-read-only nil))
13401     (delete-region (progn (beginning-of-line) (point))
13402                    (progn (forward-line 1) (point))))
13403   (setq gnus-server-killed-servers 
13404         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13405   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13406                                 gnus-server-alist))
13407   (gnus-server-position-cursor))
13408
13409 (defun gnus-server-yank-server ()
13410   "Yank the previously killed server."
13411   (interactive)
13412   (or gnus-server-killed-servers
13413       (error "No killed servers to be yanked"))
13414   (let ((alist gnus-server-alist)
13415         (server (gnus-server-server-name))
13416         (killed (car gnus-server-killed-servers)))
13417     (if (not server) 
13418         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13419       (if (string= server (car (car gnus-server-alist)))
13420           (setq gnus-server-alist (cons killed gnus-server-alist))
13421         (while (and (cdr alist)
13422                     (not (string= server (car (car (cdr alist))))))
13423           (setq alist (cdr alist)))
13424         (setcdr alist (cons killed (cdr alist)))))
13425     (gnus-server-update-server (car killed))
13426     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13427     (gnus-server-position-cursor)))
13428
13429 (defun gnus-server-exit ()
13430   "Return to the group buffer."
13431   (interactive)
13432   (kill-buffer (current-buffer))
13433   (switch-to-buffer gnus-group-buffer))
13434
13435 (defun gnus-server-list-servers ()
13436   "List all available servers."
13437   (interactive)
13438   (let ((cur (gnus-server-server-name)))
13439     (gnus-server-prepare)
13440     (if cur (gnus-server-goto-server cur)
13441       (goto-char (point-max))
13442       (forward-line -1))
13443     (gnus-server-position-cursor)))
13444
13445 (defun gnus-server-copy-server (from to)
13446   (interactive
13447    (list
13448     (or (gnus-server-server-name)
13449         (error "No server on the current line"))
13450     (read-string "Copy to: ")))
13451   (or from (error "No server on current line"))
13452   (or (and to (not (string= to ""))) (error "No name to copy to"))
13453   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13454   (or (assoc from gnus-server-alist) 
13455       (error "%s: no such server" from))
13456   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13457     (setcar to-entry to)
13458     (setcar (nthcdr 2 to-entry) to)
13459     (setq gnus-server-killed-servers 
13460           (cons to-entry gnus-server-killed-servers))
13461     (gnus-server-yank-server)))
13462
13463 (defun gnus-server-add-server (how where)
13464   (interactive 
13465    (list (intern (completing-read "Server method: "
13466                                   gnus-valid-select-methods nil t))
13467          (read-string "Server name: ")))
13468   (setq gnus-server-killed-servers 
13469         (cons (list where how where) gnus-server-killed-servers))
13470   (gnus-server-yank-server))
13471
13472 (defun gnus-server-goto-server (server)
13473   "Jump to a server line."
13474   (interactive
13475    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13476   (let ((to (text-property-any (point-min) (point-max) 
13477                                'gnus-server (intern server))))
13478     (and to
13479          (progn
13480            (goto-char to) 
13481            (gnus-server-position-cursor)))))
13482
13483 (defun gnus-server-edit-server (server)
13484   "Edit the server on the current line."
13485   (interactive (list (gnus-server-server-name)))
13486   (or server
13487       (error "No server on current line"))
13488   (let ((winconf (current-window-configuration)))
13489     (get-buffer-create gnus-server-edit-buffer)
13490     (gnus-configure-windows 'edit-server)
13491     (gnus-add-current-to-buffer-list)
13492     (emacs-lisp-mode)
13493     (make-local-variable 'gnus-prev-winconf)
13494     (setq gnus-prev-winconf winconf)
13495     (use-local-map (copy-keymap (current-local-map)))
13496     (let ((done-func '(lambda () 
13497                         "Exit editing mode and update the information."
13498                         (interactive)
13499                         (gnus-server-edit-server-done 'group))))
13500       (setcar (cdr (nth 4 done-func)) server)
13501       (local-set-key "\C-c\C-c" done-func))
13502     (erase-buffer)
13503     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13504     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13505
13506 (defun gnus-server-edit-server-done (server)
13507   (interactive)
13508   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13509   (goto-char (point-min))
13510   (let ((form (read (current-buffer)))
13511         (winconf gnus-prev-winconf))
13512     (gnus-server-set-info server form)
13513     (kill-buffer (current-buffer))
13514     (and winconf (set-window-configuration winconf))
13515     (set-buffer gnus-server-buffer)
13516     (gnus-server-update-server (gnus-server-server-name))
13517     (gnus-server-position-cursor)))
13518
13519 (defun gnus-server-read-server (server)
13520   "Browse a server."
13521   (interactive (list (gnus-server-server-name)))
13522   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13523
13524 (defun gnus-mouse-pick-server (e)
13525   (interactive "e")
13526   (mouse-set-point e)
13527   (gnus-server-read-server (gnus-server-server-name)))
13528
13529 ;;;
13530 ;;; entry points into gnus-score.el
13531 ;;;
13532
13533 ;;; Finding score files. 
13534
13535 (defvar gnus-global-score-files nil
13536   "*List of global score files and directories.
13537 Set this variable if you want to use people's score files.  One entry
13538 for each score file or each score file directory.  Gnus will decide
13539 by itself what score files are applicable to which group.
13540
13541 Say you want to use the single score file
13542 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13543 score files in the \"/ftp.some-where:/pub/score\" directory.
13544
13545  (setq gnus-global-score-files
13546        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13547          \"/ftp.some-where:/pub/score\"))")
13548
13549 (defun gnus-score-score-files (group)
13550   "Return a list of all possible score files."
13551   ;; Search and set any global score files.
13552   (and gnus-global-score-files 
13553        (or gnus-internal-global-score-files
13554            (gnus-score-search-global-directories gnus-global-score-files)))
13555   ;; Fix the kill-file dir variable.
13556   (setq gnus-kill-files-directory 
13557         (file-name-as-directory
13558          (or gnus-kill-files-directory "~/News/")))
13559   ;; If we can't read it, there are no score files.
13560   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13561       (setq gnus-score-file-list nil)
13562     (if (gnus-use-long-file-name 'not-score)
13563         ;; We want long file names.
13564         (if (or (not gnus-score-file-list)
13565                 (not (car gnus-score-file-list))
13566                 (gnus-file-newer-than gnus-kill-files-directory
13567                                       (car gnus-score-file-list)))
13568               (setq gnus-score-file-list 
13569                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13570                           (nreverse 
13571                            (directory-files 
13572                             gnus-kill-files-directory t 
13573                             (gnus-score-file-regexp))))))
13574       ;; We do not use long file names, so we have to do some
13575       ;; directory traversing.  
13576       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13577             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13578             dir files suffix)
13579         (while suffixes
13580           (setq dir (expand-file-name
13581                      (concat gnus-kill-files-directory
13582                              (gnus-replace-chars-in-string group ?. ?/))))
13583           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13584           (setq suffix (car suffixes)
13585                 suffixes (cdr suffixes))
13586           (if (file-exists-p (concat dir "/" suffix))
13587               (setq files (cons (concat dir "/" suffix) files)))
13588           (while (>= (1+ (length dir)) mdir)
13589             (and (file-exists-p (concat dir "/all/" suffix))
13590                  (setq files (cons (concat dir "/all/" suffix) files)))
13591             (string-match "/[^/]*$" dir)
13592             (setq dir (substring dir 0 (match-beginning 0)))))
13593         (setq gnus-score-file-list 
13594               (cons nil (nreverse files)))))
13595     (cdr gnus-score-file-list)))
13596
13597 (defun gnus-score-file-regexp ()
13598   (concat "\\(" gnus-score-file-suffix 
13599           "\\|" gnus-adaptive-file-suffix "\\)$"))
13600         
13601 (defun gnus-score-find-bnews (group)
13602   "Return a list of score files for GROUP.
13603 The score files are those files in the ~/News directory which matches
13604 GROUP using BNews sys file syntax."
13605   (let* ((sfiles (append (gnus-score-score-files group)
13606                          gnus-internal-global-score-files))
13607          (kill-dir (file-name-as-directory 
13608                     (expand-file-name gnus-kill-files-directory)))
13609          (klen (length kill-dir))
13610          ofiles not-match regexp)
13611     (save-excursion
13612       (set-buffer (get-buffer-create "*gnus score files*"))
13613       (buffer-disable-undo (current-buffer))
13614       ;; Go through all score file names and create regexp with them
13615       ;; as the source.  
13616       (while sfiles
13617         (erase-buffer)
13618         (insert (car sfiles))
13619         (goto-char (point-min))
13620         ;; First remove the suffix itself.
13621         (re-search-forward (concat "." (gnus-score-file-regexp)))
13622         (replace-match "" t t) 
13623         (goto-char (point-min))
13624         (if (looking-at (regexp-quote kill-dir))
13625             ;; If the file name was just "SCORE", `klen' is one character
13626             ;; too much.
13627             (delete-char (min (1- (point-max)) klen))
13628           (goto-char (point-max))
13629           (search-backward "/")
13630           (delete-region (1+ (point)) (point-min)))
13631         ;; If short file names were used, we have to translate slashes.
13632         (goto-char (point-min))
13633         (while (re-search-forward "[/:]" nil t)
13634           (replace-match "." t t))
13635         ;; Translate "all" to ".*".
13636         (while (search-forward "all" nil t)
13637           (replace-match ".*" t t))
13638         (goto-char (point-min))
13639         ;; Deal with "not."s.
13640         (if (looking-at "not.")
13641             (progn
13642               (setq not-match t)
13643               (setq regexp (buffer-substring 5 (point-max))))
13644           (setq regexp (buffer-substring 1 (point-max)))
13645           (setq not-match nil))
13646         ;; Finally - if this resulting regexp matches the group name,
13647         ;; we add this score file to the list of score files
13648         ;; applicable to this group.
13649         (if (or (and not-match
13650                      (not (string-match regexp group)))
13651                 (and (not not-match)
13652                      (string-match regexp group)))
13653             (setq ofiles (cons (car sfiles) ofiles)))
13654         (setq sfiles (cdr sfiles)))
13655       (kill-buffer (current-buffer))
13656       ;; Slight kludge here - the last score file returned should be
13657       ;; the local score file, whether it exists or not. This is so
13658       ;; that any score commands the user enters will go to the right
13659       ;; file, and not end up in some global score file.
13660       (let ((localscore
13661              (expand-file-name
13662               (if (gnus-use-long-file-name 'not-score)
13663                   (concat gnus-kill-files-directory group "." 
13664                           gnus-score-file-suffix)
13665                 (concat gnus-kill-files-directory
13666                         (gnus-replace-chars-in-string group ?. ?/)
13667                         "/" gnus-score-file-suffix)))))
13668         (and (member localscore ofiles)
13669              (delete localscore ofiles))
13670         (setq ofiles (cons localscore ofiles)))
13671       (nreverse ofiles))))
13672
13673 (defun gnus-score-find-single (group)
13674   "Return list containing the score file for GROUP."
13675   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13676         (gnus-score-file-name group)))
13677
13678 (defun gnus-score-find-hierarchical (group)
13679   "Return list of score files for GROUP.
13680 This includes the score file for the group and all its parents."
13681   (let ((all (copy-sequence '(nil)))
13682         (start 0))
13683     (while (string-match "\\." group (1+ start))
13684       (setq start (match-beginning 0))
13685       (setq all (cons (substring group 0 start) all)))
13686     (setq all (cons group all))
13687     (nconc
13688      (mapcar (lambda (newsgroup)
13689                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13690              (setq all (nreverse all)))
13691      (mapcar 'gnus-score-file-name all))))
13692
13693 (defvar gnus-score-file-alist-cache nil)
13694
13695 (defun gnus-score-find-alist (group)
13696   "Return list of score files for GROUP.
13697 The list is determined from the variable gnus-score-file-alist."
13698   (let ((alist gnus-score-file-multiple-match-alist)
13699         score-files)
13700     ;; if this group has been seen before, return the cached entry
13701     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13702         (cdr score-files)       ; ensures caching of groups with no matches
13703       ;; handle the multiple match alist
13704       (while alist
13705         (and (string-match (car (car alist)) group)
13706              (setq score-files
13707                    (nconc score-files (cdr (car alist)))))
13708         (setq alist (cdr alist)))
13709       (setq alist gnus-score-file-single-match-alist)
13710       ;; handle the single match alist
13711       (catch 'done
13712         (while alist
13713           (and (string-match (car (car alist)) group)
13714                ;; progn used just in case ("regexp") has no files
13715                ;; and score-files is still nil. -sj
13716                ;; this can be construed as a "stop searching here" feature :>
13717                ;; and used to simplify regexps in the single-alist 
13718                (progn
13719                  (setq score-files
13720                        (append score-files (cdr (car alist))))
13721                  (throw 'done nil)))
13722           (setq alist (cdr alist))))
13723       ;; cache the score files
13724       (setq gnus-score-file-alist-cache
13725             (cons (cons group score-files) gnus-score-file-alist-cache))
13726       score-files)))
13727
13728
13729 (defun gnus-possibly-score-headers (&optional trace)
13730   (let ((func gnus-score-find-score-files-function)
13731         score-files)
13732     (and func (not (listp func))
13733          (setq func (list func)))
13734     ;; Go through all the functions for finding score files (or actual
13735     ;; scores) and add them to a list.
13736     (setq score-files (copy-sequence
13737                        (gnus-score-find-alist gnus-newsgroup-name)))
13738     (while func
13739       (and (symbolp (car func))
13740            (fboundp (car func))
13741            (setq score-files 
13742                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13743       (setq func (cdr func)))
13744     (if score-files (gnus-score-headers score-files trace))))
13745
13746 (defun gnus-score-file-name (newsgroup &optional suffix)
13747   "Return the name of a score file for NEWSGROUP."
13748   (let ((suffix (or suffix gnus-score-file-suffix)))
13749     (cond  ((or (null newsgroup)
13750                 (string-equal newsgroup ""))
13751             ;; The global score file is placed at top of the directory.
13752             (expand-file-name 
13753              suffix (or gnus-kill-files-directory "~/News")))
13754            ((gnus-use-long-file-name 'not-score)
13755             ;; Append ".SCORE" to newsgroup name.
13756             (expand-file-name (concat newsgroup "." suffix)
13757                               (or gnus-kill-files-directory "~/News")))
13758            (t
13759             ;; Place "SCORE" under the hierarchical directory.
13760             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13761                                       "/" suffix)
13762                               (or gnus-kill-files-directory "~/News"))))))
13763
13764 (defun gnus-score-search-global-directories (files)
13765   "Scan all global score directories for score files."
13766   ;; Set the variable `gnus-internal-global-score-files' to all
13767   ;; available global score files.
13768   (interactive (list gnus-global-score-files))
13769   (let (out)
13770     (while files
13771       (if (string-match "/$" (car files))
13772           (setq out (nconc (directory-files 
13773                             (car files) t
13774                             (concat (gnus-score-file-regexp) "$"))))
13775         (setq out (cons (car files) out)))
13776       (setq files (cdr files)))
13777     (setq gnus-internal-global-score-files out)))
13778
13779 ;; Allow redefinition of Gnus functions.
13780
13781 (gnus-ems-redefine)
13782
13783 (provide 'gnus)
13784
13785 ;;; gnus.el ends here