*** 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 4
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-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
775 `gnus-group-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095
1096 (defvar gnus-article-x-face-command
1097   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1098   "String or function to be executed to display an X-Face header.
1099 If it is a string, the command will be executed in a sub-shell
1100 asynchronously. The compressed face will be piped to this command.") 
1101
1102 (defvar gnus-article-x-face-too-ugly nil
1103   "Regexp matching posters whose face shouldn't be shown automatically.")
1104
1105 (defvar gnus-select-group-hook nil
1106   "*A hook called when a newsgroup is selected.
1107
1108 If you'd like to simplify subjects like the
1109 `gnus-summary-next-same-subject' command does, you can use the
1110 following hook:
1111
1112  (setq gnus-select-group-hook
1113       (list
1114         (lambda ()
1115           (mapcar (lambda (header)
1116                      (header-set-subject
1117                       header
1118                       (gnus-simplify-subject
1119                        (header-subject header) 're-only)))
1120                   gnus-newsgroup-headers))))")
1121
1122 (defvar gnus-select-article-hook
1123   '(gnus-summary-show-thread)
1124   "*A hook called when an article is selected.
1125 The default hook shows conversation thread subtrees of the selected
1126 article automatically using `gnus-summary-show-thread'.")
1127
1128 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1129   "*A hook called to apply kill files to a group.
1130 This hook is intended to apply a kill file to the selected newsgroup.
1131 The function `gnus-apply-kill-file' is called by default.
1132
1133 Since a general kill file is too heavy to use only for a few
1134 newsgroups, I recommend you to use a lighter hook function. For
1135 example, if you'd like to apply a kill file to articles which contains
1136 a string `rmgroup' in subject in newsgroup `control', you can use the
1137 following hook:
1138
1139 \(setq gnus-apply-kill-hook
1140       (list
1141         (lambda ()
1142           (cond ((string-match \"control\" gnus-newsgroup-name)
1143                  (gnus-kill \"Subject\" \"rmgroup\")
1144                  (gnus-expunge \"X\"))))))")
1145
1146 (defvar gnus-visual-mark-article-hook 
1147   (list 'gnus-highlight-selected-summary)
1148   "*Hook run after selecting an article in the summary buffer.
1149 It is meant to be used for highlighting the article in some way.  It
1150 is not run if `gnus-visual' is nil.")
1151
1152 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1153   "*A hook called after preparing body, but before preparing header headers.
1154 The default hook (`gnus-inews-insert-signature') inserts a signature
1155 file specified by the variable `gnus-signature-file'.")
1156
1157 (defvar gnus-exit-group-hook nil
1158   "*A hook called when exiting (not quitting) summary mode.")
1159
1160 (defvar gnus-suspend-gnus-hook nil
1161   "*A hook called when suspending (not exiting) Gnus.")
1162
1163 (defvar gnus-exit-gnus-hook nil
1164   "*A hook called when exiting Gnus.")
1165
1166 (defvar gnus-save-newsrc-hook nil
1167   "*A hook called when saving the newsrc file.")
1168
1169 (defvar gnus-summary-update-hook 
1170   (list 'gnus-summary-highlight-line)
1171   "*A hook called when a summary line is changed.
1172 The hook will not be called if `gnus-visual' is nil.
1173
1174 The default function `gnus-summary-highlight-line' will
1175 highlight the line according to the `gnus-summary-highlight'
1176 variable.")
1177
1178 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1179   "*A hook called when an article is selected for the first time.
1180 The hook is intended to mark an article as read (or unread)
1181 automatically when it is selected.")
1182
1183 ;; Remove any hilit infestation.
1184 (add-hook 'gnus-startup-hook
1185           (lambda ()
1186             (remove-hook 'gnus-summary-prepare-hook
1187                          'hilit-rehighlight-buffer-quietly)
1188             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1189             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1190             (remove-hook 'gnus-article-prepare-hook
1191                          'hilit-rehighlight-buffer-quietly)))
1192
1193
1194 \f
1195 ;; Internal variables
1196
1197 ;; Avoid highlighting in kill files.
1198 (defvar gnus-summary-inhibit-highlight nil)
1199 (defvar gnus-newsgroup-selected-overlay nil)
1200
1201 (defvar gnus-article-mode-map nil)
1202 (defvar gnus-dribble-buffer nil)
1203 (defvar gnus-headers-retrieved-by nil)
1204 (defvar gnus-article-reply nil)
1205 (defvar gnus-override-method nil)
1206 (defvar gnus-article-check-size nil)
1207
1208 (defvar gnus-current-score-file nil)
1209 (defvar gnus-internal-global-score-files nil)
1210 (defvar gnus-score-file-list nil)
1211
1212
1213 (defvar gnus-current-move-group nil)
1214
1215 (defvar gnus-newsgroup-dependencies nil)
1216 (defvar gnus-newsgroup-threads nil)
1217 (defvar gnus-newsgroup-async nil)
1218 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1219
1220 (defvar gnus-newsgroup-adaptive nil)
1221
1222 (defvar gnus-summary-display-table nil)
1223
1224 (defconst gnus-group-line-format-alist
1225   (list (list ?M 'marked ?c)
1226         (list ?S 'subscribed ?c)
1227         (list ?L 'level ?d)
1228         (list ?N 'number ?s)
1229         (list ?I 'number-of-dormant ?d)
1230         (list ?T 'number-of-ticked ?d)
1231         (list ?R 'number-of-read ?s)
1232         (list ?t 'number-total ?d)
1233         (list ?y 'number-of-unread-unticked ?s)
1234         (list ?i 'number-of-ticked-and-dormant ?d)
1235         (list ?g 'group ?s)
1236         (list ?G 'qualified-group ?s)
1237         (list ?D 'newsgroup-description ?s)
1238         (list ?o 'moderated ?c)
1239         (list ?O 'moderated-string ?s)
1240         (list ?p 'process-marked ?c)
1241         (list ?s 'news-server ?s)
1242         (list ?n 'news-method ?s)
1243         (list ?z 'news-method-string ?s)
1244         (list ?u 'user-defined ?s)))
1245
1246 (defconst gnus-summary-line-format-alist 
1247   (list (list ?N 'number ?d)
1248         (list ?S 'subject ?s)
1249         (list ?s 'subject-or-nil ?s)
1250         (list ?n 'name ?s)
1251         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1252               ?s)
1253         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1254                       from) ?s)
1255         (list ?F 'from ?s)
1256         (list ?x (macroexpand '(header-xref header)) ?s)
1257         (list ?D (macroexpand '(header-date header)) ?s)
1258         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1259         (list ?M (macroexpand '(header-id header)) ?s)
1260         (list ?r (macroexpand '(header-references header)) ?s)
1261         (list ?c '(or (header-chars header) 0) ?d)
1262         (list ?L 'lines ?d)
1263         (list ?I 'indentation ?s)
1264         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1265         (list ?R 'replied ?c)
1266         (list ?\[ 'opening-bracket ?c)
1267         (list ?\] 'closing-bracket ?c)
1268         (list ?\> '(make-string level ? ) ?s)
1269         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1270         (list ?i 'score ?d)
1271         (list ?z 'score-char ?c)
1272         (list ?U 'unread ?c)
1273         (list ?t '(gnus-summary-number-of-articles-in-thread 
1274                    (and (boundp 'thread) (car thread)))
1275               ?d)
1276         (list ?e '(gnus-summary-number-of-articles-in-thread 
1277                    (and (boundp 'thread) (car thread)) t)
1278               ?c)
1279         (list ?u 'user-defined ?s))
1280   "An alist of format specifications that can appear in summary lines,
1281 and what variables they correspond with, along with the type of the
1282 variable (string, integer, character, etc).")
1283
1284 (defconst gnus-summary-dummy-line-format-alist
1285   (list (list ?S 'subject ?s)
1286         (list ?N 'number ?d)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defconst gnus-summary-mode-line-format-alist 
1290   (list (list ?G 'group-name ?s)
1291         (list ?g '(gnus-short-group-name group-name) ?s)
1292         (list ?A 'article-number ?d)
1293         (list ?Z 'unread-and-unselected ?s)
1294         (list ?V 'gnus-version ?s)
1295         (list ?U 'unread ?d)
1296         (list ?S 'subject ?s)
1297         (list ?e 'unselected ?d)
1298         (list ?u 'user-defined ?s)
1299         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1300
1301 (defconst gnus-group-mode-line-format-alist 
1302   (list (list ?S 'news-server ?s)
1303         (list ?M 'news-method ?s)
1304         (list ?u 'user-defined ?s)))
1305
1306 (defvar gnus-have-read-active-file nil)
1307
1308 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1309   "The mail address of the Gnus maintainers.")
1310
1311 (defconst gnus-version "(ding) Gnus v0.99.10"
1312   "Version number for this version of Gnus.")
1313
1314 (defvar gnus-info-nodes
1315   '((gnus-group-mode            "(gnus)The Group Buffer")
1316     (gnus-summary-mode          "(gnus)The Summary Buffer")
1317     (gnus-article-mode          "(gnus)The Article Buffer"))
1318   "Assoc list of major modes and related Info nodes.")
1319
1320 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1321   "The location of the (ding) Gnus documentation group.")
1322
1323 (defvar gnus-group-buffer "*Group*")
1324 (defvar gnus-summary-buffer "*Summary*")
1325 (defvar gnus-article-buffer "*Article*")
1326 (defvar gnus-server-buffer "*Server*")
1327
1328 (defvar gnus-work-buffer " *gnus work*")
1329
1330 (defvar gnus-buffer-list nil
1331   "Gnus buffers that should be killed on exit.")
1332
1333 (defvar gnus-server-alist nil
1334   "List of available servers.")
1335
1336 (defvar gnus-variable-list
1337   '(gnus-newsrc-options gnus-newsrc-options-n
1338     gnus-newsrc-last-checked-date 
1339     gnus-newsrc-alist gnus-server-alist
1340     gnus-killed-list gnus-zombie-list)
1341   "Gnus variables saved in the quick startup file.")
1342
1343 (defvar gnus-overload-functions
1344   '((news-inews gnus-inews-news "rnewspost"))
1345   "Functions overloaded by gnus.
1346 It is a list of `(original overload &optional file)'.")
1347
1348 (defvar gnus-newsrc-options nil
1349   "Options line in the .newsrc file.")
1350
1351 (defvar gnus-newsrc-options-n nil
1352   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1353
1354 (defvar gnus-newsrc-last-checked-date nil
1355   "Date Gnus last asked server for new newsgroups.")
1356
1357 (defvar gnus-newsrc-alist nil
1358   "Assoc list of read articles.
1359 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1360
1361 (defvar gnus-newsrc-hashtb nil
1362   "Hashtable of gnus-newsrc-alist.")
1363
1364 (defvar gnus-killed-list nil
1365   "List of killed newsgroups.")
1366
1367 (defvar gnus-killed-hashtb nil
1368   "Hash table equivalent of gnus-killed-list.")
1369
1370 (defvar gnus-zombie-list nil
1371   "List of almost dead newsgroups.")
1372
1373 (defvar gnus-description-hashtb nil
1374   "Descriptions of newsgroups.")
1375
1376 (defvar gnus-list-of-killed-groups nil
1377   "List of newsgroups that have recently been killed by the user.")
1378
1379 (defvar gnus-active-hashtb nil
1380   "Hashtable of active articles.")
1381
1382 (defvar gnus-moderated-list nil
1383   "List of moderated newsgroups.")
1384
1385 (defvar gnus-group-marked nil)
1386
1387 (defvar gnus-current-startup-file nil
1388   "Startup file for the current host.")
1389
1390 (defvar gnus-last-search-regexp nil
1391   "Default regexp for article search command.")
1392
1393 (defvar gnus-last-shell-command nil
1394   "Default shell command on article.")
1395
1396 (defvar gnus-current-select-method nil
1397   "The current method for selecting a newsgroup.")
1398
1399 (defvar gnus-group-list-mode nil)
1400
1401 (defvar gnus-article-internal-prepare-hook nil)
1402
1403 (defvar gnus-newsgroup-name nil)
1404 (defvar gnus-newsgroup-begin nil)
1405 (defvar gnus-newsgroup-end nil)
1406 (defvar gnus-newsgroup-last-rmail nil)
1407 (defvar gnus-newsgroup-last-mail nil)
1408 (defvar gnus-newsgroup-last-folder nil)
1409 (defvar gnus-newsgroup-last-file nil)
1410 (defvar gnus-newsgroup-auto-expire nil)
1411 (defvar gnus-newsgroup-active nil)
1412
1413 (defvar gnus-newsgroup-unreads nil
1414   "List of unread articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-unselected nil
1417   "List of unselected unread articles in the current newsgroup.")
1418
1419 (defvar gnus-newsgroup-reads nil
1420   "Alist of read articles and article marks in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-marked nil
1423   "List of ticked articles in the current newsgroup (a subset of unread art).")
1424
1425 (defvar gnus-newsgroup-killed nil
1426   "List of ranges of articles that have been through the scoring process.")
1427
1428 (defvar gnus-newsgroup-kill-headers nil)
1429
1430 (defvar gnus-newsgroup-replied nil
1431   "List of articles that have been replied to in the current newsgroup.")
1432
1433 (defvar gnus-newsgroup-expirable nil
1434   "List of articles in the current newsgroup that can be expired.")
1435
1436 (defvar gnus-newsgroup-processable nil
1437   "List of articles in the current newsgroup that can be processed.")
1438
1439 (defvar gnus-newsgroup-bookmarks nil
1440   "List of articles in the current newsgroup that have bookmarks.")
1441
1442 (defvar gnus-newsgroup-dormant nil
1443   "List of dormant articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-scored nil
1446   "List of scored articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-headers nil
1449   "List of article headers in the current newsgroup.")
1450 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1451
1452 (defvar gnus-newsgroup-ancient nil
1453   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1454
1455 (defvar gnus-current-article nil)
1456 (defvar gnus-article-current nil)
1457 (defvar gnus-current-headers nil)
1458 (defvar gnus-have-all-headers nil)
1459 (defvar gnus-last-article nil)
1460 (defvar gnus-newsgroup-history nil)
1461 (defvar gnus-current-kill-article nil)
1462
1463 ;; Save window configuration.
1464 (defvar gnus-prev-winconf nil)
1465
1466 ;; Format specs
1467 (defvar gnus-summary-line-format-spec nil)
1468 (defvar gnus-summary-dummy-line-format-spec nil)
1469 (defvar gnus-group-line-format-spec nil)
1470 (defvar gnus-summary-mode-line-format-spec nil)
1471 (defvar gnus-article-mode-line-format-spec nil)
1472 (defvar gnus-group-mode-line-format-spec nil)
1473 (defvar gnus-summary-mark-positions nil)
1474 (defvar gnus-group-mark-positions nil)
1475
1476 (defvar gnus-summary-expunge-below nil)
1477 (defvar gnus-reffed-article-number nil)
1478
1479 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1480 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1481
1482 (defvar gnus-cache-removeable-articles nil)
1483
1484 (defconst gnus-summary-local-variables 
1485   '(gnus-newsgroup-name 
1486     gnus-newsgroup-begin gnus-newsgroup-end 
1487     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1488     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1489     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1490     gnus-newsgroup-unselected gnus-newsgroup-marked
1491     gnus-newsgroup-reads
1492     gnus-newsgroup-replied gnus-newsgroup-expirable
1493     gnus-newsgroup-processable gnus-newsgroup-killed
1494     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1495     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1496     gnus-current-article gnus-current-headers gnus-have-all-headers
1497     gnus-last-article gnus-article-internal-prepare-hook
1498     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1499     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1500     gnus-newsgroup-threads gnus-newsgroup-async
1501     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1502     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1503     gnus-newsgroup-history gnus-newsgroup-ancient
1504     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1505     gnus-cache-removeable-articles)
1506   "Variables that are buffer-local to the summary buffers.")
1507
1508 (defconst gnus-bug-message
1509   "Sending a bug report to the Gnus Towers.
1510 ========================================
1511
1512 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1513 be sent to the Gnus Bug Exterminators. 
1514
1515 At the bottom of the buffer you'll see lots of variable settings.
1516 Please do not delete those.  They will tell the Bug People what your
1517 environment is, so that it will be easier to locate the bugs.
1518
1519 If you have found a bug that makes Emacs go \"beep\", set
1520 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1521 and include the backtrace in your bug report.
1522
1523 Please describe the bug in annoying, painstaking detail.
1524
1525 Thank you for your help in stamping out bugs.
1526 ")
1527
1528 ;;; End of variables.
1529
1530 ;; Define some autoload functions Gnus might use.
1531 (eval-and-compile
1532
1533   ;; Various 
1534   (autoload 'metamail-buffer "metamail")
1535   (autoload 'Info-goto-node "info")
1536   (autoload 'hexl-hex-string-to-integer "hexl")
1537   (autoload 'pp "pp")
1538   (autoload 'pp-to-string "pp")
1539   (autoload 'pp-eval-expression "pp")
1540   (autoload 'mail-extract-address-components "mail-extr")
1541
1542   (autoload 'nnmail-split-fancy "nnmail")
1543   (autoload 'nnvirtual-catchup-group "nnvirtual")
1544
1545   ;; timezone
1546   (autoload 'timezone-make-date-arpa-standard "timezone")
1547   (autoload 'timezone-fix-time "timezone")
1548   (autoload 'timezone-make-sortable-date "timezone")
1549   (autoload 'timezone-make-time-string "timezone")
1550
1551   ;; rmail & friends
1552   (autoload 'mail-position-on-field "sendmail")
1553   (autoload 'mail-setup "sendmail")
1554   (autoload 'rmail-output "rmailout")
1555   (autoload 'news-mail-other-window "rnewspost")
1556   (autoload 'news-reply-yank-original "rnewspost")
1557   (autoload 'news-caesar-buffer-body "rnewspost")
1558   (autoload 'rmail-insert-rmail-file-header "rmail")
1559   (autoload 'rmail-count-new-messages "rmail")
1560   (autoload 'rmail-show-message "rmail")
1561
1562   ;; gnus-soup
1563   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1564   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1565   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1566   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1567   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1568   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1569   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1570
1571   ;; gnus-mh
1572   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1573   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1574   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1575   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1576   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1577   (autoload 'gnus-Folder-save-name "gnus-mh")
1578   (autoload 'gnus-folder-save-name "gnus-mh")
1579
1580   ;; gnus-vis misc
1581   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1582   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1583   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1584   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1585   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1586   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1587   (autoload 'gnus-summary-highlight-line "gnus-vis")
1588   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1589
1590   ;; gnus-vis article
1591   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1592   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1593   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1594   (autoload 'gnus-article-hide "gnus-vis" nil t)
1595   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1596   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1597   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1598   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1599   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1600   (autoload 'gnus-article-add-button "gnus-vis")
1601
1602   ;; gnus-cite
1603   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1604   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1605   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1606
1607   ;; gnus-kill
1608   (autoload 'gnus-kill "gnus-kill")
1609   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1610   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1611   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1612   (autoload 'gnus-execute "gnus-kill")
1613   (autoload 'gnus-expunge "gnus-kill")
1614
1615   ;; gnus-cache
1616   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1617   (autoload 'gnus-cache-save-buffers "gnus-cache")
1618   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1619   (autoload 'gnus-cache-request-article "gnus-cache")
1620   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1621   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1622   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1623   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1624
1625   ;; gnus-score
1626   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1627   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1628   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1629   (autoload 'gnus-score-save "gnus-score")
1630   (autoload 'gnus-score-headers "gnus-score")
1631   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1632   (autoload 'gnus-score-adaptive "gnus-score")
1633   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1634   (autoload 'gnus-score-find-trace "gnus-score")
1635
1636   ;; gnus-edit
1637   (autoload 'gnus-score-customize "gnus-edit" nil t)
1638
1639   ;; gnus-uu
1640   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1641   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1642   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1643   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1644   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1645   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1646   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1647   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1648   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1649   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1652   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1657   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1658   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1662
1663   ;; gnus-msg
1664   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1665   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1666   (autoload 'gnus-group-mail "gnus-msg" nil t)
1667   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1668   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1669   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1670   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1671   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1672   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1673   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1674   (autoload 'gnus-post-news "gnus-msg" nil t)
1675   (autoload 'gnus-inews-news "gnus-msg" nil t)
1676   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1677   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1678   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1679   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1680   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1681   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1682   (autoload 'gnus-mail-yank-original "gnus-msg")
1683   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1684   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1685   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1686   (autoload 'gnus-article-mail-with-original "gnus-msg")
1687   (autoload 'gnus-article-mail "gnus-msg")
1688   (autoload 'gnus-bug "gnus-msg" nil t)
1689
1690   ;; gnus-vm
1691   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1692   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1693   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1694   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1695   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1696   (autoload 'gnus-yank-article "gnus-vm" nil t)
1697
1698   )
1699
1700 \f
1701
1702 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1703 ;; If you want the cursor to go somewhere else, set these two
1704 ;; functions in some startup hook to whatever you want.
1705 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1706 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1707
1708 ;;; Various macros and substs.
1709
1710 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1711   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1712   (` (let ((GnusStartBufferWindow (selected-window)))
1713        (unwind-protect
1714            (progn
1715              (pop-to-buffer (, buffer))
1716              (,@ forms))
1717          (select-window GnusStartBufferWindow)))))
1718
1719 (defmacro gnus-gethash (string hashtable)
1720   "Get hash value of STRING in HASHTABLE."
1721   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1722   ;;(` (abbrev-expansion (, string) (, hashtable)))
1723   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1724
1725 (defmacro gnus-sethash (string value hashtable)
1726   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1727   ;; We cannot use define-abbrev since it only accepts string as value.
1728   ;; (set (intern string hashtable) value))
1729   (` (set (intern (, string) (, hashtable)) (, value))))
1730
1731 (defsubst gnus-buffer-substring (beg end)
1732   (buffer-substring (match-beginning beg) (match-end end)))
1733
1734 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1735 ;;   function `substring' might cut on a middle of multi-octet
1736 ;;   character.
1737 (defun gnus-truncate-string (str width)
1738   (substring str 0 width))
1739
1740 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1741 ;; to limit the length of a string. This function is necessary since
1742 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1743 (defsubst gnus-limit-string (str width)
1744   (if (> (length str) width)
1745       (substring str 0 width)
1746     str))
1747
1748 (defsubst gnus-simplify-subject-re (subject)
1749   "Remove \"Re:\" from subject lines."
1750   (let ((case-fold-search t))
1751     (if (string-match "^re: *" subject)
1752         (substring subject (match-end 0))
1753       subject)))
1754
1755 (defsubst gnus-goto-char (point)
1756   (and point (goto-char point)))
1757
1758 (defmacro gnus-buffer-exists-p (buffer)
1759   (` (and (, buffer)
1760           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1761                    (, buffer)))))
1762
1763 (defmacro gnus-kill-buffer (buffer)
1764   (` (if (gnus-buffer-exists-p (, buffer))
1765          (kill-buffer (, buffer)))))
1766
1767 (defsubst gnus-point-at-bol ()
1768   "Return point at the beginning of line."
1769   (let ((p (point)))
1770     (beginning-of-line)
1771     (prog1
1772         (point)
1773       (goto-char p))))
1774
1775 (defsubst gnus-point-at-eol ()
1776   "Return point at the beginning of line."
1777   (let ((p (point)))
1778     (end-of-line)
1779     (prog1
1780         (point)
1781       (goto-char p))))
1782
1783 ;; Delete the current line (and the next N lines.);
1784 (defmacro gnus-delete-line (&optional n)
1785   (` (delete-region (progn (beginning-of-line) (point))
1786                     (progn (forward-line (, (or n 1))) (point)))))
1787
1788 ;;; Load the compatability functions. 
1789
1790 (require 'gnus-ems)
1791
1792 \f
1793 ;;;
1794 ;;; Gnus Utility Functions
1795 ;;;
1796
1797 (defun gnus-extract-address-components (from)
1798   (let (name address)
1799     ;; First find the address - the thing with the @ in it.  This may
1800     ;; not be accurate in mail addresses, but does the trick most of
1801     ;; the time in news messages.
1802     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1803         (setq address (substring from (match-beginning 0) (match-end 0))))
1804     ;; Then we check whether the "name <address>" format is used.
1805     (and address
1806          (string-match (concat "<" (regexp-quote address) ">") from)
1807          (and (setq name (substring from 0 (1- (match-beginning 0))))
1808               ;; Strip any quotes from the name.
1809               (string-match "\".*\"" name)
1810               (setq name (substring name 1 (1- (match-end 0))))))
1811     ;; If not, then "address (name)" is used.
1812     (or name
1813         (and (string-match "(.+)" from)
1814              (setq name (substring from (1+ (match-beginning 0)) 
1815                                    (1- (match-end 0)))))
1816         (and (string-match "()" from)
1817              (setq name address))
1818         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1819         ;; XOVER might not support folded From headers.
1820         (and (string-match "(.*" from)
1821              (setq name (substring from (1+ (match-beginning 0)) 
1822                                    (match-end 0)))))
1823     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1824     (list (or name from) (or address from))))
1825
1826 (defun gnus-fetch-field (field)
1827   "Return the value of the header FIELD of current article."
1828   (save-excursion
1829     (save-restriction
1830       (let ((case-fold-search t))
1831         (gnus-narrow-to-headers)
1832         (mail-fetch-field field)))))
1833
1834 (defun gnus-goto-colon ()
1835   (beginning-of-line)
1836   (search-forward ":" (gnus-point-at-eol) t))
1837
1838 (defun gnus-narrow-to-headers ()
1839   (widen)
1840   (save-excursion
1841     (narrow-to-region
1842      (goto-char (point-min))
1843      (if (search-forward "\n\n" nil t)
1844          (1- (point))
1845        (point-max)))))
1846
1847 (defvar gnus-old-specs nil)
1848
1849 (defun gnus-update-format-specifications ()
1850   (gnus-make-thread-indent-array)
1851
1852   (let ((formats '(summary summary-dummy group 
1853                            summary-mode group-mode article-mode))
1854         old-format new-format)
1855     (while formats
1856       (setq new-format (symbol-value
1857                         (intern (format "gnus-%s-line-format" (car formats)))))
1858       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1859                (equal old-format new-format))
1860           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1861                (gnus-parse-format
1862                 new-format
1863                 (symbol-value 
1864                  (intern (format "gnus-%s-line-format-alist"
1865                                  (if (eq (car formats) 'article-mode)
1866                                      'summary-mode (car formats))))))))
1867       (setq gnus-old-specs (cons (cons (car formats) new-format)
1868                                  (delq (car formats) gnus-old-specs)))
1869       (setq formats (cdr formats))))
1870       
1871   (gnus-update-group-mark-positions)
1872   (gnus-update-summary-mark-positions)
1873
1874   (if (and (string-match "%D" gnus-group-line-format)
1875            (not gnus-description-hashtb)
1876            gnus-read-active-file)
1877       (gnus-read-all-descriptions-files)))
1878
1879 (defun gnus-update-summary-mark-positions ()
1880   (save-excursion
1881     (let ((gnus-replied-mark 129)
1882           (gnus-score-below-mark 130)
1883           (gnus-score-over-mark 130)
1884           (thread nil)
1885           pos)
1886       (gnus-set-work-buffer)
1887       (gnus-summary-insert-line 
1888        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1889       (goto-char (point-min))
1890       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1891                                          (- (point) 2)))))
1892       (goto-char (point-min))
1893       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1894                                           (- (point) 2))) pos))
1895       (goto-char (point-min))
1896       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1897                                         (- (point) 2))) pos))
1898       (setq gnus-summary-mark-positions pos))))
1899
1900 (defun gnus-update-group-mark-positions ()
1901   (save-excursion
1902     (let ((gnus-process-mark 128)
1903           (gnus-group-marked '("dummy.group")))
1904       (gnus-set-work-buffer)
1905       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1906       (goto-char (point-min))
1907       (setq gnus-group-mark-positions
1908             (list (cons 'process (and (search-forward "\200" nil t)
1909                                       (- (point) 2))))))))
1910
1911 (defun gnus-mouse-face-function (form)
1912   (` (let ((string (, form)))
1913        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1914        string)))
1915
1916 (defun gnus-max-width-function (el max-width)
1917   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1918   (` (let* ((val (eval (, el)))
1919             (valstr (if (numberp val)
1920                         (int-to-string val) val)))
1921        (if (> (length valstr) (, max-width))
1922            (substring valstr 0 (, max-width))
1923          valstr))))
1924
1925 (defun gnus-parse-format (format spec-alist)
1926   ;; This function parses the FORMAT string with the help of the
1927   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1928   ;; string.  If the FORMAT string contains the specifiers %( and %)
1929   ;; the text between them will have the mouse-face text property.
1930   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1931       (if (and gnus-visual gnus-mouse-face)
1932           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1933                 (button (substring format (match-beginning 2) (match-end 2)))
1934                 (post (substring format (match-beginning 3) (match-end 3))))
1935             (list 'concat
1936                   (gnus-parse-simple-format pre spec-alist)
1937                   (gnus-mouse-face-function 
1938                    (gnus-parse-simple-format button spec-alist))
1939                   (gnus-parse-simple-format post spec-alist)))
1940         (gnus-parse-simple-format
1941          (concat (substring format (match-beginning 1) (match-end 1))
1942                  (substring format (match-beginning 2) (match-end 2))
1943                  (substring format (match-beginning 3) (match-end 3)))
1944          spec-alist))
1945     (gnus-parse-simple-format format spec-alist)))
1946
1947 (defun gnus-parse-simple-format (format spec-alist)
1948   ;; This function parses the FORMAT string with the help of the
1949   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1950   ;; string. The list will consist of the symbol `format', a format
1951   ;; specification string, and a list of forms depending on the
1952   ;; SPEC-ALIST.
1953   (let ((max-width 0)
1954         spec flist fstring newspec elem beg)
1955     (save-excursion
1956       (gnus-set-work-buffer)
1957       (insert format)
1958       (goto-char (point-min))
1959       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1960         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1961                                                      (match-end 2))))
1962         ;; First check if there are any specs that look anything like
1963         ;; "%12,12A", ie. with a "max width specification". These have
1964         ;; to be treated specially.
1965         (if (setq beg (match-beginning 1))
1966             (setq max-width 
1967                   (string-to-int 
1968                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1969           (setq max-width 0)
1970           (setq beg (match-beginning 2)))
1971         ;; Find the specification from `spec-alist'.
1972         (if (not (setq elem (cdr (assq spec spec-alist))))
1973             (setq elem '("*" ?s)))
1974         ;; Treat user defined format specifiers specially
1975         (and (eq (car elem) 'user-defined)
1976              (setq elem
1977                    (list 
1978                     (list (intern (concat "gnus-user-format-function-"
1979                                           (buffer-substring
1980                                            (match-beginning 3)
1981                                            (match-end 3))))
1982                           'header)
1983                     ?s))
1984              (delete-region (match-beginning 3) (match-end 3)))
1985         (if (not (zerop max-width))
1986             (let ((el (car elem)))
1987               (cond ((= (car (cdr elem)) ?c) 
1988                      (setq el (list 'char-to-string el)))
1989                     ((= (car (cdr elem)) ?d)
1990                      (numberp el) (setq el (list 'int-to-string el))))
1991               (setq flist (cons (gnus-max-width-function el max-width)
1992                                 flist))
1993               (setq newspec ?s))
1994           (setq flist (cons (car elem) flist))
1995           (setq newspec (car (cdr elem))))
1996         ;; Remove the old specification (and possibly a ",12" string).
1997         (delete-region beg (match-end 2))
1998         ;; Insert the new specification.
1999         (goto-char beg)
2000         (insert newspec))
2001       (setq fstring (buffer-substring 1 (point-max))))
2002     (cons 'format (cons fstring (nreverse flist)))))
2003
2004 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2005 (defun gnus-read-init-file ()
2006   (and gnus-init-file
2007        (or (and (file-exists-p gnus-init-file) 
2008                 ;; Don't try to load a directory.
2009                 (not (file-directory-p gnus-init-file)))
2010            (file-exists-p (concat gnus-init-file ".el"))
2011            (file-exists-p (concat gnus-init-file ".elc")))
2012        (load gnus-init-file nil t)))
2013
2014 (defun gnus-set-work-buffer ()
2015   (if (get-buffer gnus-work-buffer)
2016       (progn
2017         (set-buffer gnus-work-buffer)
2018         (erase-buffer))
2019     (set-buffer (get-buffer-create gnus-work-buffer))
2020     (kill-all-local-variables)
2021     (buffer-disable-undo (current-buffer))
2022     (gnus-add-current-to-buffer-list)))
2023
2024 ;; Article file names when saving.
2025
2026 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2027   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2028 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2029 Otherwise, it is like ~/News/news/group/num."
2030   (let ((default
2031           (expand-file-name
2032            (concat (if (gnus-use-long-file-name 'not-save)
2033                        (gnus-capitalize-newsgroup newsgroup)
2034                      (gnus-newsgroup-directory-form newsgroup))
2035                    "/" (int-to-string (header-number headers)))
2036            (or gnus-article-save-directory "~/News"))))
2037     (if (and last-file
2038              (string-equal (file-name-directory default)
2039                            (file-name-directory last-file))
2040              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2041         default
2042       (or last-file default))))
2043
2044 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2045   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2046 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2047 Otherwise, it is like ~/News/news/group/num."
2048   (let ((default
2049           (expand-file-name
2050            (concat (if (gnus-use-long-file-name 'not-save)
2051                        newsgroup
2052                      (gnus-newsgroup-directory-form newsgroup))
2053                    "/" (int-to-string (header-number headers)))
2054            (or gnus-article-save-directory "~/News"))))
2055     (if (and last-file
2056              (string-equal (file-name-directory default)
2057                            (file-name-directory last-file))
2058              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2059         default
2060       (or last-file default))))
2061
2062 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2063   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2064 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2065 Otherwise, it is like ~/News/news/group/news."
2066   (or last-file
2067       (expand-file-name
2068        (if (gnus-use-long-file-name 'not-save)
2069            (gnus-capitalize-newsgroup newsgroup)
2070          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2071        (or gnus-article-save-directory "~/News"))))
2072
2073 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2074   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2075 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2076 Otherwise, it is like ~/News/news/group/news."
2077   (or last-file
2078       (expand-file-name
2079        (if (gnus-use-long-file-name 'not-save)
2080            newsgroup
2081          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2082        (or gnus-article-save-directory "~/News"))))
2083
2084 ;; For subscribing new newsgroup
2085
2086 (defun gnus-subscribe-hierarchical-interactive (groups)
2087   (let ((groups (sort groups 'string<))
2088         prefixes prefix start ans group starts)
2089     (while groups
2090       (setq prefixes (list "^"))
2091       (while (and groups prefixes)
2092         (while (not (string-match (car prefixes) (car groups)))
2093           (setq prefixes (cdr prefixes)))
2094         (setq prefix (car prefixes))
2095         (setq start (1- (length prefix)))
2096         (if (and (string-match "[^\\.]\\." (car groups) start)
2097                  (cdr groups)
2098                  (setq prefix 
2099                        (concat "^" (substring (car groups) 0 (match-end 0))))
2100                  (string-match prefix (car (cdr groups))))
2101             (progn
2102               (setq prefixes (cons prefix prefixes))
2103               (message "Descend hierarchy %s? ([y]nsq): " 
2104                        (substring prefix 1 (1- (length prefix))))
2105               (setq ans (read-char))
2106               (cond ((= ans ?n)
2107                      (while (and groups 
2108                                  (string-match prefix 
2109                                                (setq group (car groups))))
2110                        (setq gnus-killed-list 
2111                              (cons group gnus-killed-list))
2112                        (gnus-sethash group group gnus-killed-hashtb)
2113                        (setq groups (cdr groups)))
2114                      (setq starts (cdr starts)))
2115                     ((= ans ?s)
2116                      (while (and groups 
2117                                  (string-match prefix 
2118                                                (setq group (car groups))))
2119                        (gnus-sethash group group gnus-killed-hashtb)
2120                        (gnus-subscribe-alphabetically (car groups))
2121                        (setq groups (cdr groups)))
2122                      (setq starts (cdr starts)))
2123                     ((= ans ?q)
2124                      (while groups
2125                        (setq group (car groups))
2126                        (setq gnus-killed-list (cons group gnus-killed-list))
2127                        (gnus-sethash group group gnus-killed-hashtb)
2128                        (setq groups (cdr groups))))
2129                     (t nil)))
2130           (message "Subscribe %s? ([n]yq)" (car groups))
2131           (setq ans (read-char))
2132           (setq group (car groups))
2133           (cond ((= ans ?y)
2134                  (gnus-subscribe-alphabetically (car groups))
2135                  (gnus-sethash group group gnus-killed-hashtb))
2136                 ((= ans ?q)
2137                  (while groups
2138                    (setq group (car groups))
2139                    (setq gnus-killed-list (cons group gnus-killed-list))
2140                    (gnus-sethash group group gnus-killed-hashtb)
2141                    (setq groups (cdr groups))))
2142                 (t 
2143                  (setq gnus-killed-list (cons group gnus-killed-list))
2144                  (gnus-sethash group group gnus-killed-hashtb)))
2145           (setq groups (cdr groups)))))))
2146
2147 (defun gnus-subscribe-randomly (newsgroup)
2148   "Subscribe new NEWSGROUP by making it the first newsgroup."
2149   (gnus-subscribe-newsgroup newsgroup))
2150
2151 (defun gnus-subscribe-alphabetically (newgroup)
2152   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2153   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2154   (let ((groups (cdr gnus-newsrc-alist))
2155         before)
2156     (while (and (not before) groups)
2157       (if (string< newgroup (car (car groups)))
2158           (setq before (car (car groups)))
2159         (setq groups (cdr groups))))
2160     (gnus-subscribe-newsgroup newgroup before)))
2161
2162 (defun gnus-subscribe-hierarchically (newgroup)
2163   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2164   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2165   (save-excursion
2166     (set-buffer (find-file-noselect gnus-current-startup-file))
2167     (let ((groupkey newgroup)
2168           before)
2169       (while (and (not before) groupkey)
2170         (goto-char (point-min))
2171         (let ((groupkey-re
2172                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2173           (while (and (re-search-forward groupkey-re nil t)
2174                       (progn
2175                         (setq before (buffer-substring
2176                                       (match-beginning 1) (match-end 1)))
2177                         (string< before newgroup)))))
2178         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2179         (setq groupkey
2180               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2181                   (substring groupkey (match-beginning 1) (match-end 1)))))
2182       (gnus-subscribe-newsgroup newgroup before))))
2183
2184 (defun gnus-subscribe-interactively (newsgroup)
2185   "Subscribe new NEWSGROUP interactively.
2186 It is inserted in hierarchical newsgroup order if subscribed. If not,
2187 it is killed."
2188   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2189       (gnus-subscribe-hierarchically newsgroup)
2190     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2191
2192 (defun gnus-subscribe-zombies (newsgroup)
2193   "Make new NEWSGROUP a zombie group."
2194   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2195
2196 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2197   "Subscribe new NEWSGROUP.
2198 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2199 the first newsgroup."
2200   ;; We subscribe the group by changing its level to `subscribed'.
2201   (gnus-group-change-level 
2202    newsgroup gnus-level-default-subscribed
2203    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2204   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2205
2206 ;; For directories
2207
2208 (defun gnus-newsgroup-directory-form (newsgroup)
2209   "Make hierarchical directory name from NEWSGROUP name."
2210   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2211         (len (length newsgroup))
2212         idx)
2213     ;; If this is a foreign group, we don't want to translate the
2214     ;; entire name.  
2215     (if (setq idx (string-match ":" newsgroup))
2216         (aset newsgroup idx ?/)
2217       (setq idx 0))
2218     ;; Replace all occurrences of `.' with `/'.
2219     (while (< idx len)
2220       (if (= (aref newsgroup idx) ?.)
2221           (aset newsgroup idx ?/))
2222       (setq idx (1+ idx)))
2223     newsgroup))
2224
2225 (defun gnus-newsgroup-saveable-name (group)
2226   (gnus-replace-chars-in-string group ?/ ?.))
2227
2228 (defun gnus-make-directory (dir)
2229   "Make DIRECTORY recursively."
2230   (let* ((dir (expand-file-name dir default-directory))
2231          dirs)
2232     (if (string-match "/$" dir)
2233         (setq dir (substring dir 0 (match-beginning 0))))
2234     (while (not (file-exists-p dir))
2235       (setq dirs (cons dir dirs))
2236       (string-match "/[^/]+$" dir)
2237       (setq dir (substring dir 0 (match-beginning 0))))
2238     (while dirs
2239       (make-directory (car dirs))
2240       (setq dirs (cdr dirs)))))
2241
2242 (defun gnus-capitalize-newsgroup (newsgroup)
2243   "Capitalize NEWSGROUP name."
2244   (and (not (zerop (length newsgroup)))
2245        (concat (char-to-string (upcase (aref newsgroup 0)))
2246                (substring newsgroup 1))))
2247
2248 ;; Var
2249
2250 (defun gnus-simplify-subject (subject &optional re-only)
2251   "Remove `Re:' and words in parentheses.
2252 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2253   (let ((case-fold-search t))           ;Ignore case.
2254     ;; Remove `Re:' and `Re^N:'.
2255     (if (string-match "^re:[ \t]*" subject)
2256         (setq subject (substring subject (match-end 0))))
2257     ;; Remove words in parentheses from end.
2258     (or re-only
2259         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2260           (setq subject (substring subject 0 (match-beginning 0)))))
2261     ;; Return subject string.
2262     subject))
2263
2264 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2265 ;; all whitespace.
2266 (defun gnus-simplify-subject-fuzzy (subject)
2267   (let ((case-fold-search t))
2268     (save-excursion
2269       (gnus-set-work-buffer)
2270       (insert subject)
2271       (inline (gnus-simplify-buffer-fuzzy))
2272       (buffer-string))))
2273
2274 (defun gnus-simplify-buffer-fuzzy ()
2275   (goto-char (point-min))
2276   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2277   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2278                             nil t)
2279     (replace-match "" t t))
2280   (goto-char (point-min))
2281   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2282     (replace-match "" t t))
2283   (goto-char (point-min))
2284   (while (re-search-forward "[ \t]+" nil t)
2285     (replace-match " " t t))
2286   (goto-char (point-min))
2287   (while (re-search-forward "[ \t]+$" nil t)
2288     (replace-match "" t t))
2289   (goto-char (point-min))
2290   (while (re-search-forward "^[ \t]+" nil t)
2291     (replace-match "" t t))
2292   (if gnus-simplify-subject-fuzzy-regexp
2293       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2294         (replace-match "" t t))))
2295
2296 ;; Add the current buffer to the list of buffers to be killed on exit. 
2297 (defun gnus-add-current-to-buffer-list ()
2298   (or (memq (current-buffer) gnus-buffer-list)
2299       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2300
2301 (defun gnus-string> (s1 s2)
2302   (not (or (string< s1 s2)
2303            (string= s1 s2))))
2304
2305 ;; Functions accessing headers.
2306 ;; Functions are more convenient than macros in some cases.
2307
2308 (defun gnus-header-number (header)
2309   (header-number header))
2310
2311 (defun gnus-header-subject (header)
2312   (header-subject header))
2313
2314 (defun gnus-header-from (header)
2315   (header-from header))
2316
2317 (defun gnus-header-xref (header)
2318   (header-xref header))
2319
2320 (defun gnus-header-lines (header)
2321   (header-lines header))
2322
2323 (defun gnus-header-date (header)
2324   (header-date header))
2325
2326 (defun gnus-header-id (header)
2327   (header-id header))
2328
2329 (defun gnus-header-references (header)
2330   (header-references header))
2331
2332 ;;; General various misc type functions.
2333
2334 (defun gnus-clear-system ()
2335   "Clear all variables and buffers."
2336   ;; Clear Gnus variables.
2337   (let ((variables gnus-variable-list))
2338     (while variables
2339       (set (car variables) nil)
2340       (setq variables (cdr variables))))
2341   ;; Clear other internal variables.
2342   (setq gnus-list-of-killed-groups nil
2343         gnus-have-read-active-file nil
2344         gnus-newsrc-alist nil
2345         gnus-newsrc-hashtb nil
2346         gnus-killed-list nil
2347         gnus-zombie-list nil
2348         gnus-killed-hashtb nil
2349         gnus-active-hashtb nil
2350         gnus-moderated-list nil
2351         gnus-description-hashtb nil
2352         gnus-newsgroup-headers nil
2353         gnus-newsgroup-headers-hashtb-by-number nil
2354         gnus-newsgroup-name nil
2355         gnus-server-alist nil
2356         gnus-current-select-method nil)
2357   ;; Reset any score variables.
2358   (and (boundp 'gnus-score-cache)
2359        (set 'gnus-score-cache nil))
2360   (and (boundp 'gnus-internal-global-score-files)
2361        (set 'gnus-internal-global-score-files nil))
2362   ;; Kill the startup file.
2363   (and gnus-current-startup-file
2364        (get-file-buffer gnus-current-startup-file)
2365        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2366   ;; Save any cache buffers.
2367   (and gnus-use-cache (gnus-cache-save-buffers))
2368   ;; Clear the dribble buffer.
2369   (gnus-dribble-clear)
2370   ;; Kill global KILL file buffer.
2371   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2372       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2373   (gnus-kill-buffer nntp-server-buffer)
2374   ;; Kill Gnus buffers.
2375   (while gnus-buffer-list
2376     (gnus-kill-buffer (car gnus-buffer-list))
2377     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2378
2379 (defun gnus-windows-old-to-new (setting)
2380   (if (symbolp setting)
2381       (setq setting 
2382             (cond ((eq setting 'SelectArticle)
2383                    'article)
2384                   ((eq setting 'SelectSubject)
2385                    'summary)
2386                   ((eq setting 'SelectNewsgroup)
2387                    'group)
2388                   (t setting))))
2389   (if (or (listp setting)
2390           (not (and gnus-window-configuration
2391                     (memq setting '(group summary article)))))
2392       setting
2393     (let* ((setting (if (eq setting 'group) 
2394                         (if (assq 'newsgroup gnus-window-configuration)
2395                             'newsgroup
2396                           'newsgroups) setting))
2397            (elem (car (cdr (assq setting gnus-window-configuration))))
2398            (total (apply '+ elem))
2399            (types '(group summary article))
2400            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2401            (i 0)
2402            perc
2403            out)
2404       (while (< i 3)
2405         (or (zerop (nth i elem))
2406             (progn
2407               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2408               (setq out (cons (if (eq pbuf (nth i types))
2409                                   (vector (nth i types) perc 'point)
2410                                 (vector (nth i types) perc))
2411                               out))))
2412         (setq i (1+ i)))
2413       (list (nreverse out)))))
2414            
2415 (defun gnus-add-configuration (conf)
2416   (setq gnus-buffer-configuration 
2417         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2418                          gnus-buffer-configuration))))
2419
2420 (defun gnus-configure-windows (setting &optional force)
2421   (setq setting (gnus-windows-old-to-new setting))
2422   (let ((r (if (symbolp setting)
2423                (cdr (assq setting gnus-buffer-configuration))
2424              setting))
2425         (in-buf (current-buffer))
2426         rule val w height hor ohor heights sub jump-buffer
2427         rel total to-buf all-visible)
2428     (or r (error "No such setting: %s" setting))
2429
2430     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2431         ;; All the windows mentioned are already visibe, so we just
2432         ;; put point in the assigned buffer, and do not touch the
2433         ;; winconf. 
2434         (select-window (get-buffer-window all-visible))
2435
2436       ;; Either remove all windows or just remove all Gnus windows.
2437       (if gnus-use-full-window
2438           (delete-other-windows)
2439         (gnus-remove-some-windows)
2440         (switch-to-buffer nntp-server-buffer))
2441
2442       (while r
2443         (setq hor (car r)
2444               ohor nil)
2445
2446         ;; We have to do the (possible) horizontal splitting before the
2447         ;; vertical. 
2448         (if (and (listp (car hor)) 
2449                  (eq (car (car hor)) 'horizontal))
2450             (progn
2451               (split-window 
2452                nil
2453                (if (integerp (nth 1 (car hor)))
2454                    (nth 1 (car hor))
2455                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2456                t)
2457               (setq hor (cdr hor))))
2458
2459         ;; Go through the rules and eval the elements that are to be
2460         ;; evaled.  
2461         (while hor
2462           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2463               (progn
2464                 ;; Expand short buffer name.
2465                 (setq w (aref val 0))
2466                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2467                      (progn
2468                        (setq val (apply 'vector (mapcar 'identity val)))
2469                        (aset val 0 w)))
2470                 (setq ohor (cons val ohor))))
2471           (setq hor (cdr hor)))
2472         (setq rule (cons (nreverse ohor) rule))
2473         (setq r (cdr r)))
2474       (setq rule (nreverse rule))
2475
2476       ;; We tally the window sizes.
2477       (setq total (window-height))
2478       (while rule
2479         (setq hor (car rule))
2480         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2481             (setq hor (cdr hor)))
2482         (setq sub 0)
2483         (while hor
2484           (setq rel (aref (car hor) 1)
2485                 heights (cons
2486                          (cond ((and (floatp rel) (= 1.0 rel))
2487                                 'x)
2488                                ((integerp rel)
2489                                 rel)
2490                                (t
2491                                 (max (floor (* total rel)) 4)))
2492                          heights)
2493                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2494                 hor (cdr hor)))
2495         (setq heights (nreverse heights)
2496               hor (car rule))
2497
2498         ;; We then go through these heighs and create windows for them.
2499         (while heights
2500           (setq height (car heights)
2501                 heights (cdr heights))
2502           (and (eq height 'x)
2503                (setq height (- total sub)))
2504           (and heights
2505                (split-window nil height))
2506           (setq to-buf (aref (car hor) 0))
2507           (switch-to-buffer 
2508            (cond ((not to-buf)
2509                   in-buf)
2510                  ((symbolp to-buf)
2511                   (symbol-value (aref (car hor) 0)))
2512                  (t
2513                   (aref (car hor) 0))))
2514           (and (> (length (car hor)) 2)
2515                (eq (aref (car hor) 2) 'point)
2516                (setq jump-buffer (current-buffer)))
2517           (other-window 1)
2518           (setq hor (cdr hor)))
2519       
2520         (setq rule (cdr rule)))
2521
2522       ;; Finally, we pop to the buffer that's supposed to have point. 
2523       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2524
2525       (select-window (get-buffer-window jump-buffer))
2526       (set-buffer jump-buffer))))
2527
2528 (defun gnus-all-windows-visible-p (rule)
2529   (let (invisible hor jump-buffer val buffer)
2530     ;; Go through the rules and eval the elements that are to be
2531     ;; evaled.  
2532     (while (and rule (not invisible))
2533       (setq hor (car rule)
2534             rule (cdr rule))
2535       (while (and hor (not invisible))
2536         (if (setq val (if (vectorp (car hor)) 
2537                           (car hor)
2538                         (if (not (eq (car (car hor)) 'horizontal))
2539                             (eval (car hor)))))
2540             (progn
2541               ;; Expand short buffer name.
2542               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2543                                (aref val 0)))
2544               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2545                              buffer))
2546               (and (> (length val) 2) (eq 'point (aref val 2))
2547                    (setq jump-buffer buffer))
2548               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2549         (setq hor (cdr hor))))
2550     (and (not invisible) jump-buffer)))
2551
2552       
2553 (defun gnus-remove-some-windows ()
2554   (let ((buffers gnus-window-to-buffer)
2555         (lowest (frame-height))
2556         buf bufs lowest-buf)
2557     (save-excursion
2558       ;; Remove windows on all known Gnus buffers.
2559       (while buffers
2560         (setq buf (cdr (car buffers)))
2561         (if (symbolp buf)
2562             (setq buf (and (boundp buf) (symbol-value buf))))
2563         (and buf 
2564              (get-buffer-window buf)
2565              (progn
2566                (setq bufs (cons buf bufs))
2567                (pop-to-buffer buf)
2568                (if (< (nth 1 (window-edges)) lowest)
2569                    (progn
2570                      (setq lowest (nth 1 (window-edges)))
2571                      (setq lowest-buf buf)))))
2572         (setq buffers (cdr buffers)))
2573       ;; Remove windows on *all* summary buffers.
2574       (let (wins)
2575         (walk-windows
2576          (lambda (win)
2577            (let ((buf (window-buffer win)))
2578              (if (string-match  "^\\*Summary" (buffer-name buf))
2579              (progn
2580                (setq bufs (cons buf bufs))
2581                (pop-to-buffer buf)
2582                (if (< (nth 1 (window-edges)) lowest)
2583                    (progn
2584                      (setq lowest-buf buf)
2585                      (setq lowest (nth 1 (window-edges)))))))))))
2586       (and lowest-buf 
2587            (progn
2588              (pop-to-buffer lowest-buf)
2589              (switch-to-buffer nntp-server-buffer)))
2590       (while bufs
2591         (and (not (eq (car bufs) lowest-buf))
2592              (delete-windows-on (car bufs)))
2593         (setq bufs (cdr bufs))))))
2594                           
2595 (defun gnus-version ()
2596   "Version numbers of this version of Gnus."
2597   (interactive)
2598   (let ((methods gnus-valid-select-methods)
2599         (mess gnus-version)
2600         meth)
2601     ;; Go through all the legal select methods and add their version
2602     ;; numbers to the total version string. Only the backends that are
2603     ;; currently in use will have their message numbers taken into
2604     ;; consideration. 
2605     (while methods
2606       (setq meth (intern (concat (car (car methods)) "-version")))
2607       (and (boundp meth)
2608            (stringp (symbol-value meth))
2609            (setq mess (concat mess "; " (symbol-value meth))))
2610       (setq methods (cdr methods)))
2611     (gnus-message 2 mess)))
2612
2613 (defun gnus-info-find-node ()
2614   "Find Info documentation of Gnus."
2615   (interactive)
2616   ;; Enlarge info window if needed.
2617   (let ((mode major-mode))
2618     (gnus-configure-windows 'info)
2619     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2620
2621 (defun gnus-overload-functions (&optional overloads)
2622   "Overload functions specified by optional argument OVERLOADS.
2623 If nothing is specified, use the variable gnus-overload-functions."
2624   (let ((defs nil)
2625         (overloads (or overloads gnus-overload-functions)))
2626     (while overloads
2627       (setq defs (car overloads))
2628       (setq overloads (cdr overloads))
2629       ;; Load file before overloading function if necessary.  Make
2630       ;; sure we cannot use `require' always.
2631       (and (not (fboundp (car defs)))
2632            (car (cdr (cdr defs)))
2633            (load (car (cdr (cdr defs))) nil 'nomessage))
2634       (fset (car defs) (car (cdr defs))))))
2635
2636 (defun gnus-replace-chars-in-string (string from to)
2637   "Replace characters in STRING from FROM to TO."
2638   (let ((string (substring string 0))   ;Copy string.
2639         (len (length string))
2640         (idx 0))
2641     ;; Replace all occurrences of FROM with TO.
2642     (while (< idx len)
2643       (if (= (aref string idx) from)
2644           (aset string idx to))
2645       (setq idx (1+ idx)))
2646     string))
2647
2648 (defun gnus-days-between (date1 date2)
2649   ;; Return the number of days between date1 and date2.
2650   (- (gnus-day-number date1) (gnus-day-number date2)))
2651
2652 (defun gnus-day-number (date)
2653   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2654                      (timezone-parse-date date))))
2655     (timezone-absolute-from-gregorian 
2656      (nth 1 dat) (nth 2 dat) (car dat))))
2657
2658 ;; Returns a floating point number that says how many seconds have
2659 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2660 (defun gnus-seconds-since-epoch (date)
2661   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2662                         (timezone-parse-date date)))
2663          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2664                         (timezone-parse-time
2665                          (aref (timezone-parse-date date) 3))))
2666          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2667                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2668          (tday (- (timezone-absolute-from-gregorian 
2669                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2670                   (timezone-absolute-from-gregorian 
2671                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2672     (+ (nth 2 ttime)
2673        (* (nth 1 ttime) 60)
2674        (* 1.0 (nth 0 ttime) 60 60)
2675        (* 1.0 tday 60 60 24))))
2676
2677 (defun gnus-file-newer-than (file date)
2678   (let ((fdate (nth 5 (file-attributes file))))
2679     (or (> (car fdate) (car date))
2680         (and (= (car fdate) (car date))
2681              (> (nth 1 fdate) (nth 1 date))))))
2682
2683 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2684 ;; the echo area.
2685 (defun gnus-y-or-n-p (prompt)
2686   (prog1
2687       (y-or-n-p prompt)
2688     (message "")))
2689
2690 (defun gnus-yes-or-no-p (prompt)
2691   (prog1
2692       (yes-or-no-p prompt)
2693     (message "")))
2694
2695 ;; Check whether to use long file names.
2696 (defun gnus-use-long-file-name (symbol)
2697   ;; The variable has to be set...
2698   (and gnus-use-long-file-name
2699        ;; If it isn't a list, then we return t.
2700        (or (not (listp gnus-use-long-file-name))
2701            ;; If it is a list, and the list contains `symbol', we
2702            ;; return nil.  
2703            (not (memq symbol gnus-use-long-file-name)))))
2704
2705 ;; I suspect there's a better way, but I haven't taken the time to do
2706 ;; it yet. -erik selberg@cs.washington.edu
2707 (defun gnus-dd-mmm (messy-date)
2708   "Return a string like DD-MMM from a big messy string"
2709   (let ((datevec (timezone-parse-date messy-date)))
2710     (format "%2s-%s"
2711             (or (aref datevec 2) "??")
2712             (capitalize
2713              (or (car 
2714                   (nth (1- (string-to-number (aref datevec 1)))
2715                        timezone-months-assoc))
2716                  "???")))))
2717
2718 ;; Make a hash table (default and minimum size is 255).
2719 ;; Optional argument HASHSIZE specifies the table size.
2720 (defun gnus-make-hashtable (&optional hashsize)
2721   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2722
2723 ;; Make a number that is suitable for hashing; bigger than MIN and one
2724 ;; less than 2^x.
2725 (defun gnus-create-hash-size (min)
2726   (let ((i 1))
2727     (while (< i min)
2728       (setq i (* 2 i)))
2729     (1- i)))
2730
2731 ;; Show message if message has a lower level than `gnus-verbose'. 
2732 ;; Guide-line for numbers:
2733 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2734 ;; for things that take a long time, 7 - not very important messages
2735 ;; on stuff, 9 - messages inside loops.
2736 (defun gnus-message (level &rest args)
2737   (if (<= level gnus-verbose)
2738       (apply 'message args)
2739     ;; We have to do this format thingie here even if the result isn't
2740     ;; shown - the return value has to be the same as the return value
2741     ;; from `message'.
2742     (apply 'format args)))
2743
2744 ;; Generate a unique new group name.
2745 (defun gnus-generate-new-group-name (leaf)
2746   (let ((name leaf)
2747         (num 0))
2748     (while (gnus-gethash name gnus-newsrc-hashtb)
2749       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2750     name))
2751
2752 (defun gnus-find-file-noselect (file &optional force)
2753   "Does vaguely the same as find-file-noselect. No hooks are run."
2754   (let (buf insert)
2755     (if (setq buf (get-file-buffer file))
2756         (setq insert force)
2757       (setq buf (create-file-buffer file))
2758       (setq insert t))
2759     (if (not insert)
2760         buf
2761       (save-excursion
2762         (set-buffer buf)
2763         (erase-buffer)
2764         (and (file-readable-p file)
2765              (insert-file-contents file))
2766         (set-visited-file-name file)
2767         (set-buffer-modified-p nil)
2768         (current-buffer)))))
2769
2770 ;;; List and range functions
2771
2772 (defun gnus-last-element (list)
2773   "Return last element of LIST."
2774   (while (cdr list)
2775     (setq list (cdr list)))
2776   (car list))
2777
2778 (defun gnus-copy-sequence (list)
2779   "Do a complete, total copy of a list."
2780   (if (and (consp list) (not (consp (cdr list))))
2781       (cons (car list) (cdr list))
2782     (mapcar (lambda (elem) (if (consp elem) 
2783                                (if (consp (cdr elem))
2784                                    (gnus-copy-sequence elem)
2785                                  (cons (car elem) (cdr elem)))
2786                              elem))
2787             list)))
2788
2789 (defun gnus-set-difference (list1 list2)
2790   "Return a list of elements of LIST1 that do not appear in LIST2."
2791   (let ((list1 (copy-sequence list1)))
2792     (while list2
2793       (setq list1 (delq (car list2) list1))
2794       (setq list2 (cdr list2)))
2795     list1))
2796
2797 (defun gnus-sorted-complement (list1 list2)
2798   "Return a list of elements of LIST1 that do not appear in LIST2.
2799 Both lists have to be sorted over <."
2800   (let (out)
2801     (if (or (null list1) (null list2))
2802         (or list1 list2)
2803       (while (and list1 list2)
2804         (cond ((= (car list1) (car list2))
2805                (setq list1 (cdr list1)
2806                      list2 (cdr list2)))
2807               ((< (car list1) (car list2))
2808                (setq out (cons (car list1) out))
2809                (setq list1 (cdr list1)))
2810               (t
2811                (setq out (cons (car list2) out))
2812                (setq list2 (cdr list2)))))
2813       (nconc (nreverse out) (or list1 list2)))))
2814
2815 (defun gnus-intersection (list1 list2)      
2816   (let ((result nil))
2817     (while list2
2818       (if (memq (car list2) list1)
2819           (setq result (cons (car list2) result)))
2820       (setq list2 (cdr list2)))
2821     result))
2822
2823 (defun gnus-sorted-intersection (list1 list2)
2824   ;; LIST1 and LIST2 have to be sorted over <.
2825   (let (out)
2826     (while (and list1 list2)
2827       (cond ((= (car list1) (car list2))
2828              (setq out (cons (car list1) out)
2829                    list1 (cdr list1)
2830                    list2 (cdr list2)))
2831             ((< (car list1) (car list2))
2832              (setq list1 (cdr list1)))
2833             (t
2834              (setq list2 (cdr list2)))))
2835     (nreverse out)))
2836
2837 (defun gnus-set-sorted-intersection (list1 list2)
2838   ;; LIST1 and LIST2 have to be sorted over <.
2839   ;; This function modifies LIST1.
2840   (let* ((top (cons nil list1))
2841          (prev top))
2842   (while (and list1 list2)
2843     (cond ((= (car list1) (car list2))
2844            (setq prev list1
2845                  list1 (cdr list1)
2846                  list2 (cdr list2)))
2847           ((< (car list1) (car list2))
2848            (setcdr prev (cdr list1))
2849            (setq list1 (cdr list1)))
2850           (t
2851            (setq list2 (cdr list2)))))
2852   (setcdr prev nil)
2853   (cdr top)))
2854
2855 (defun gnus-compress-sequence (numbers &optional always-list)
2856   "Convert list of numbers to a list of ranges or a single range.
2857 If ALWAYS-LIST is non-nil, this function will always release a list of
2858 ranges."
2859   (let* ((first (car numbers))
2860          (last (car numbers))
2861          result)
2862     (if (null numbers)
2863         nil
2864       (if (not (listp (cdr numbers)))
2865           numbers
2866         (while numbers
2867           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2868                 ((= (1+ last) (car numbers)) ;Still in sequence
2869                  (setq last (car numbers)))
2870                 (t                      ;End of one sequence
2871                  (setq result 
2872                        (cons (if (= first last) first
2873                                (cons first last)) result))
2874                  (setq first (car numbers))
2875                  (setq last  (car numbers))))
2876           (setq numbers (cdr numbers)))
2877         (if (and (not always-list) (null result))
2878             (if (= first last) (list first) (cons first last))
2879           (nreverse (cons (if (= first last) first (cons first last))
2880                           result)))))))
2881
2882 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2883 (defun gnus-uncompress-range (ranges)
2884   "Expand a list of ranges into a list of numbers.
2885 RANGES is either a single range on the form `(num . num)' or a list of
2886 these ranges."
2887   (let (first last result)
2888     (cond 
2889      ((null ranges)
2890       nil)
2891      ((not (listp (cdr ranges)))
2892       (setq first (car ranges))
2893       (setq last (cdr ranges))
2894       (while (<= first last)
2895         (setq result (cons first result))
2896         (setq first (1+ first)))
2897       (nreverse result))
2898      (t
2899       (while ranges
2900         (if (atom (car ranges))
2901             (if (numberp (car ranges))
2902                 (setq result (cons (car ranges) result)))
2903           (setq first (car (car ranges)))
2904           (setq last  (cdr (car ranges)))
2905           (while (<= first last)
2906             (setq result (cons first result))
2907             (setq first (1+ first))))
2908         (setq ranges (cdr ranges)))
2909       (nreverse result)))))
2910
2911 (defun gnus-add-to-range (ranges list)
2912   "Return a list of ranges that has all articles from both RANGES and LIST.
2913 Note: LIST has to be sorted over `<'."
2914   (if (not ranges)
2915       (gnus-compress-sequence list t)
2916     (setq list (copy-sequence list))
2917     (or (listp (cdr ranges))
2918         (setq ranges (list ranges)))
2919     (let ((out ranges)
2920           ilist lowest highest temp)
2921       (while (and ranges list)
2922         (setq ilist list)
2923         (setq lowest (or (and (atom (car ranges)) (car ranges))
2924                          (car (car ranges))))
2925         (while (and list (cdr list) (< (car (cdr list)) lowest))
2926           (setq list (cdr list)))
2927         (if (< (car ilist) lowest)
2928             (progn
2929               (setq temp list)
2930               (setq list (cdr list))
2931               (setcdr temp nil)
2932               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2933         (setq highest (or (and (atom (car ranges)) (car ranges))
2934                           (cdr (car ranges))))
2935         (while (and list (<= (car list) highest))
2936           (setq list (cdr list)))
2937         (setq ranges (cdr ranges)))
2938       (if list
2939           (setq out (nconc (gnus-compress-sequence list t) out)))
2940       (setq out (sort out (lambda (r1 r2) 
2941                             (< (or (and (atom r1) r1) (car r1))
2942                                (or (and (atom r2) r2) (car r2))))))
2943       (setq ranges out)
2944       (while ranges
2945         (if (atom (car ranges))
2946             (if (cdr ranges)
2947                 (if (atom (car (cdr ranges)))
2948                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2949                         (progn
2950                           (setcar ranges (cons (car ranges) 
2951                                                (car (cdr ranges))))
2952                           (setcdr ranges (cdr (cdr ranges)))))
2953                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2954                       (progn
2955                         (setcar (car (cdr ranges)) (car ranges))
2956                         (setcar ranges (car (cdr ranges)))
2957                         (setcdr ranges (cdr (cdr ranges)))))))
2958           (if (cdr ranges)
2959               (if (atom (car (cdr ranges)))
2960                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2961                       (progn
2962                         (setcdr (car ranges) (car (cdr ranges)))
2963                         (setcdr ranges (cdr (cdr ranges)))))
2964                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2965                     (progn
2966                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2967                       (setcdr ranges (cdr (cdr ranges))))))))
2968         (setq ranges (cdr ranges)))
2969       out)))
2970
2971 (defun gnus-remove-from-range (ranges list)
2972   "Return a list of ranges that has all articles from LIST removed from RANGES.
2973 Note: LIST has to be sorted over `<'."
2974   ;; !!! This function shouldn't look like this, but I've got a headache.
2975   (gnus-compress-sequence 
2976    (gnus-sorted-complement
2977     (gnus-uncompress-range ranges) list)))
2978
2979 (defun gnus-member-of-range (number ranges)
2980   (if (not (listp (cdr ranges)))
2981       (and (>= number (car ranges)) 
2982            (<= number (cdr ranges)))
2983     (let ((not-stop t))
2984       (while (and ranges 
2985                   (if (numberp (car ranges))
2986                       (>= number (car ranges))
2987                     (>= number (car (car ranges))))
2988                   not-stop)
2989         (if (if (numberp (car ranges))
2990                 (= number (car ranges))
2991               (and (>= number (car (car ranges)))
2992                    (<= number (cdr (car ranges)))))
2993             (setq not-stop nil))
2994         (setq ranges (cdr ranges)))
2995       (not not-stop))))
2996
2997 \f
2998 ;;;
2999 ;;; Gnus group mode
3000 ;;;
3001
3002 (defvar gnus-group-mode-map nil)
3003 (defvar gnus-group-group-map nil)
3004 (defvar gnus-group-mark-map nil)
3005 (defvar gnus-group-list-map nil)
3006 (defvar gnus-group-sub-map nil)
3007 (put 'gnus-group-mode 'mode-class 'special)
3008
3009 (if gnus-group-mode-map
3010     nil
3011   (setq gnus-group-mode-map (make-keymap))
3012   (suppress-keymap gnus-group-mode-map)
3013   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3014   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3015   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3016   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3017   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3018   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3019   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3020   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3021   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3022   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3023   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3024   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3025   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3026   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3027   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3028   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3029   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3030   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3031   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3032   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3033   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3034   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3035   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3036   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3037   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3038   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3039   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3040   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3041   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3042   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3043   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3044   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3045   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3046   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3047   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3048   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3049   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3050   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3051   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3052   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3053   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3054   (define-key gnus-group-mode-map "V" 'gnus-version)
3055   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3056   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3057   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3058   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3059   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3060   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3061   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3062   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3063   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3064   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3065   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3066   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3067   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3068   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3069   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3070
3071   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3072   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3073   (define-prefix-command 'gnus-group-mark-map)
3074   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3075   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3076   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3077   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3078
3079   (define-prefix-command 'gnus-group-group-map)
3080   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3081   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3082   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3083   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3084   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3085   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3086   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3087   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3088   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3089   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3090   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3091   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3092   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3093   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3094   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3095   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3096   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3097   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3098
3099   (define-prefix-command 'gnus-group-list-map)
3100   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3101   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3102   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3103   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3104   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3105   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3106   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3107   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3108   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3109
3110   (define-prefix-command 'gnus-group-sub-map)
3111   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3112   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3113   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3114   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3115   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3116   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3117   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3118   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3119
3120 (defun gnus-group-mode ()
3121   "Major mode for reading news.
3122
3123 All normal editing commands are switched off.
3124 \\<gnus-group-mode-map>
3125 The group buffer lists (some of) the groups available.  For instance,
3126 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3127 lists all zombie groups. 
3128
3129 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3130 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3131
3132 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3133
3134 The following commands are available:
3135
3136 \\{gnus-group-mode-map}"
3137   (interactive)
3138   (if gnus-visual (gnus-group-make-menu-bar))
3139   (kill-all-local-variables)
3140   (setq mode-line-modified "-- ")
3141   (make-local-variable 'mode-line-format)
3142   (setq mode-line-format (copy-sequence mode-line-format))
3143   (and (equal (nth 3 mode-line-format) "   ")
3144        (setcar (nthcdr 3 mode-line-format) ""))
3145   (setq major-mode 'gnus-group-mode)
3146   (setq mode-name "Group")
3147   (gnus-group-set-mode-line)
3148   (setq mode-line-process nil)
3149   (use-local-map gnus-group-mode-map)
3150   (buffer-disable-undo (current-buffer))
3151   (setq truncate-lines t)
3152   (setq buffer-read-only t)
3153   (run-hooks 'gnus-group-mode-hook))
3154
3155 (defun gnus-mouse-pick-group (e)
3156   (interactive "e")
3157   (mouse-set-point e)
3158   (gnus-group-read-group nil))
3159
3160 ;; Look at LEVEL and find out what the level is really supposed to be.
3161 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3162 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3163 (defun gnus-group-default-level (&optional level number-or-nil)
3164   (cond  
3165    (gnus-group-use-permanent-levels
3166     (setq gnus-group-default-list-level 
3167           (or level gnus-group-default-list-level))
3168     (or gnus-group-default-list-level gnus-level-subscribed))
3169    (number-or-nil
3170     level)
3171    (t
3172     (or level gnus-group-default-list-level gnus-level-subscribed))))
3173   
3174
3175 ;;;###autoload
3176 (defun gnus-no-server (&optional arg)
3177   "Read network news.
3178 If ARG is a positive number, Gnus will use that as the
3179 startup level. If ARG is nil, Gnus will be started at level 2. 
3180 If ARG is non-nil and not a positive number, Gnus will
3181 prompt the user for the name of an NNTP server to use.
3182 As opposed to `gnus', this command will not connect to the local server."
3183   (interactive "P")
3184   (setq gnus-group-use-permanent-levels t)
3185   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3186
3187 (defalias '\(ding\) 'gnus)
3188
3189 ;;;###autoload
3190 (defun gnus (&optional arg dont-connect)
3191   "Read network news.
3192 If ARG is non-nil and a positive number, Gnus will use that as the
3193 startup level. If ARG is non-nil and not a positive number, Gnus will
3194 prompt the user for the name of an NNTP server to use."
3195   (interactive "P")
3196   (if (get-buffer gnus-group-buffer)
3197       (progn
3198         (switch-to-buffer gnus-group-buffer)
3199         (gnus-group-get-new-news))
3200     (gnus-clear-system)
3201     (nnheader-init-server-buffer)
3202     (gnus-read-init-file)
3203
3204     (let ((level (and arg (numberp arg) (> arg 0) arg))
3205           did-connect)
3206       (unwind-protect
3207           (progn
3208             (gnus-group-setup-buffer)
3209             (or dont-connect 
3210                 (setq did-connect
3211                       (gnus-start-news-server (and arg (not level))))))
3212         (if (and (not dont-connect) 
3213                  (not did-connect))
3214             (gnus-group-quit)
3215           (run-hooks 'gnus-startup-hook)
3216           ;; NNTP server is successfully open. 
3217
3218           ;; Find the current startup file name.
3219           (setq gnus-current-startup-file 
3220                 (gnus-make-newsrc-file gnus-startup-file))
3221
3222           ;; Read the dribble file.
3223           (and gnus-use-dribble-file (gnus-dribble-read-file))
3224
3225           (gnus-summary-make-display-table)
3226           (let ((buffer-read-only nil))
3227             (erase-buffer)
3228             (if (not gnus-inhibit-startup-message)
3229                 (progn
3230                   (gnus-group-startup-message)
3231                   (sit-for 0))))
3232           (gnus-setup-news nil level)
3233           (gnus-group-list-groups level)
3234           (gnus-configure-windows 'group))))))
3235
3236 (defun gnus-unload ()
3237   "Unload all Gnus features."
3238   (interactive)
3239   (let ((history load-history)
3240         feature)
3241     (while history
3242       (and (string-match "^gnus" (car (car history)))
3243            (setq feature (cdr (assq 'provide (car history))))
3244            (unload-feature feature 'force))
3245       (setq history (cdr history)))))
3246
3247 (defun gnus-group-startup-message (&optional x y)
3248   "Insert startup message in current buffer."
3249   ;; Insert the message.
3250   (erase-buffer)
3251   (insert
3252    (format "
3253      %s
3254            A newsreader 
3255       for GNU Emacs
3256
3257         Based on GNUS 
3258              written by 
3259      Masanobu UMEDA
3260
3261        A Praxis Release
3262       larsi@ifi.uio.no
3263
3264            gnus-version))
3265   ;; And then hack it.
3266   ;; 18 is the longest line.
3267   (indent-rigidly (point-min) (point-max) 
3268                   (/ (max (- (window-width) (or x 28)) 0) 2))
3269   (goto-char (point-min))
3270   ;; +4 is fuzzy factor.
3271   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3272
3273   ;; Fontify some.
3274   (goto-char (point-min))
3275   (search-forward "Praxis")
3276   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3277   (goto-char (point-min)))
3278
3279 (defun gnus-group-setup-buffer ()
3280   (or (get-buffer gnus-group-buffer)
3281       (progn
3282         (switch-to-buffer gnus-group-buffer)
3283         (gnus-add-current-to-buffer-list)
3284         (gnus-group-mode)
3285         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3286
3287 (defun gnus-group-list-groups (&optional level unread)
3288   "List newsgroups with level LEVEL or lower that have unread articles.
3289 Default is all subscribed groups.
3290 If argument UNREAD is non-nil, groups with no unread articles are also
3291 listed." 
3292   (interactive (list (if current-prefix-arg
3293                          (prefix-numeric-value current-prefix-arg)
3294                        (or
3295                         (gnus-group-default-level nil t)
3296                         gnus-level-subscribed))))
3297   (or level
3298       (setq level (car gnus-group-list-mode)
3299             unread (cdr gnus-group-list-mode)))
3300   (setq level (gnus-group-default-level level))
3301   (gnus-group-setup-buffer)     ;May call from out of group buffer
3302   (let ((case-fold-search nil)
3303         (group (gnus-group-group-name)))
3304     (funcall gnus-group-prepare-function level unread nil)
3305     (if (zerop (buffer-size))
3306         (gnus-message 5 gnus-no-groups-message)
3307       (goto-char (point-min))
3308       (if (not group)
3309           ;; Go to the first group with unread articles.
3310           (gnus-group-search-forward nil nil nil t)
3311         ;; Find the right group to put point on. If the current group
3312         ;; has disapeared in the new listing, try to find the next
3313         ;; one. If no next one can be found, just leave point at the
3314         ;; first newsgroup in the buffer.
3315         (if (not (gnus-goto-char
3316                   (text-property-any (point-min) (point-max) 
3317                                      'gnus-group (intern group))))
3318             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3319               (while (and newsrc
3320                           (not (gnus-goto-char 
3321                                 (text-property-any 
3322                                  (point-min) (point-max) 'gnus-group 
3323                                  (intern (car (car newsrc)))))))
3324                 (setq newsrc (cdr newsrc)))
3325               (or newsrc (progn (goto-char (point-max))
3326                                 (forward-line -1))))))
3327       ;; Adjust cursor point.
3328       (gnus-group-position-cursor))))
3329
3330 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3331   "List all newsgroups with unread articles of level LEVEL or lower.
3332 If ALL is non-nil, list groups that have no unread articles.
3333 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3334 If REGEXP, only list groups matching REGEXP."
3335   (set-buffer gnus-group-buffer)
3336   (let ((buffer-read-only nil)
3337         (newsrc (cdr gnus-newsrc-alist))
3338         (lowest (or lowest 1))
3339         info clevel unread group)
3340     (erase-buffer)
3341     (if (< lowest gnus-level-zombie)
3342         ;; List living groups.
3343         (while newsrc
3344           (setq info (car newsrc)
3345                 group (car info)
3346                 newsrc (cdr newsrc)
3347                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3348           (and unread ; This group might be bogus
3349                (or (not regexp)
3350                    (string-match regexp group))
3351                (<= (setq clevel (car (cdr info))) level) 
3352                (>= clevel lowest)
3353                (or all            ; We list all groups?
3354                    (eq unread t)  ; We list unactivated groups
3355                    (> unread 0)   ; We list groups with unread articles
3356                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3357                (gnus-group-insert-group-line 
3358                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3359
3360     ;; List dead groups.
3361     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3362          (gnus-group-prepare-flat-list-dead 
3363           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3364           gnus-level-zombie ?Z
3365           regexp))
3366     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3367          (gnus-group-prepare-flat-list-dead 
3368           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3369           gnus-level-killed ?K regexp))
3370
3371     (gnus-group-set-mode-line)
3372     (setq gnus-group-list-mode (cons level all))
3373     (run-hooks 'gnus-group-prepare-hook)))
3374
3375 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3376   ;; List zombies and killed lists somehwat faster, which was
3377   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3378   ;; this by ignoring the group format specification altogether.
3379   (let (group beg)
3380     (while groups
3381       (setq group (car groups)
3382             groups (cdr groups))
3383       (if (or (not regexp)
3384               (string-match regexp group))
3385           (progn
3386             (setq beg (point))
3387             (insert (format " %c     *: %s\n" mark group))
3388             (add-text-properties 
3389              beg (1+ beg) 
3390              (list 'gnus-group (intern group)
3391                    'gnus-unread t
3392                    'gnus-level level)))))))
3393
3394 (defun gnus-group-real-name (group)
3395   "Find the real name of a foreign newsgroup."
3396   (if (string-match ":[^:]+$" group)
3397       (substring group (1+ (match-beginning 0)))
3398     group))
3399
3400 (defun gnus-group-prefixed-name (group method)
3401   "Return the whole name from GROUP and METHOD."
3402   (and (stringp method) (setq method (gnus-server-to-method method)))
3403   (concat (format "%s" (car method))
3404           (if (and 
3405                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3406                (not (string= (nth 1 method) "")))
3407               (concat "+" (nth 1 method)))
3408           ":" group))
3409
3410 (defun gnus-group-real-prefix (group)
3411   "Return the prefix of the current group name."
3412   (if (string-match "^[^:]+:" group)
3413       (substring group 0 (match-end 0))
3414     ""))
3415
3416 (defun gnus-group-method-name (group)
3417   "Return the method used for selecting GROUP."
3418   (let ((prefix (gnus-group-real-prefix group)))
3419     (if (equal prefix "")
3420         gnus-select-method
3421       (if (string-match "^[^\\+]+\\+" prefix)
3422           (list (intern (substring prefix 0 (1- (match-end 0))))
3423                 (substring prefix (match-end 0) (1- (length prefix))))
3424         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3425
3426 (defun gnus-group-foreign-p (group)
3427   "Return nil if GROUP is native, non-nil if it is foreign."
3428   (string-match ":" group))
3429
3430 (defun gnus-group-set-info (info &optional method-only-group part)
3431   (let* ((entry (gnus-gethash
3432                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3433          (part-info info)
3434          (info (if method-only-group (nth 2 entry) info)))
3435     (if (not method-only-group)
3436         ()
3437       (or entry
3438           (error "Trying to change non-existent group %s" method-only-group))
3439       ;; We have recevied parts of the actual group info - either the
3440       ;; select method or the group parameters.  We first check
3441       ;; whether we have to extend the info, and if so, do that.
3442       (let ((len (length info))
3443             (total (if (eq part 'method) 5 6)))
3444         (and (< len total)
3445              (setcdr (nthcdr (1- len) info)
3446                      (make-list (- total len) nil)))
3447         ;; Then we enter the new info.
3448         (setcar (nthcdr (1- total) info) part-info)))
3449     ;; We uncompress some lists of marked articles.
3450     (let (marked)
3451       (if (not (setq marked (nth 3 info)))
3452           ()
3453         (while marked
3454           (or (eq 'score (car (car marked)))
3455               (eq 'bookmark (car (car marked)))
3456               (eq 'killed (car (car marked)))
3457               (setcdr (car marked) 
3458                       (gnus-uncompress-range (cdr (car marked)))))
3459           (setq marked (cdr marked)))))
3460     (if entry
3461         ()
3462       ;; This is a new group, so we just create it.
3463       (save-excursion
3464         (set-buffer gnus-group-buffer)
3465         (if (nth 4 info)
3466             ;; It's a foreign group...
3467             (gnus-group-make-group 
3468              (gnus-group-real-name (car info))
3469              (prin1-to-string (car (nth 4 info)))
3470              (nth 1 (nth 4 info)))
3471           ;; It's a native group.
3472           (gnus-group-make-group
3473            (car info)
3474            (prin1-to-string (car gnus-select-method))
3475            (nth 1 gnus-select-method)))
3476         (gnus-message 6 "Note: New group created")
3477         (setq entry 
3478               (gnus-gethash (gnus-group-prefixed-name 
3479                              (gnus-group-real-name (car info))
3480                              (or (nth 4 info) gnus-select-method))
3481                             gnus-newsrc-hashtb))))
3482     ;; Whether it was a new group or not, we now have the entry, so we
3483     ;; can do the update.
3484     (if entry
3485         (progn
3486           (setcar (nthcdr 2 entry) info)
3487           (if (and (not (eq (car entry) t)) 
3488                    (gnus-gethash (car info) gnus-active-hashtb))
3489               (let ((marked (nth 3 info)))
3490                 (setcar entry 
3491                         (max 0 (- (length (gnus-list-of-unread-articles 
3492                                            (car info)))
3493                                   (length (cdr (assq 'tick marked)))
3494                                   (length (cdr (assq 'dormant marked)))))))))
3495       (error "No such group: %s" (car info)))))
3496
3497 (defun gnus-group-set-method-info (group select-method)
3498   (gnus-group-set-info select-method group 'method))
3499
3500 (defun gnus-group-set-params-info (group params)
3501   (gnus-group-set-info params group 'params))
3502
3503 (defun gnus-group-update-group-line ()
3504   "This function updates the current line in the newsgroup buffer and
3505 moves the point to the colon."
3506   (let* ((buffer-read-only nil)
3507          (group (gnus-group-group-name))
3508          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3509     (if entry
3510         (gnus-dribble-enter 
3511          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3512                  ")")))
3513     (beginning-of-line)
3514     (delete-region (point) (progn (forward-line 1) (point)))
3515     (gnus-group-insert-group-line-info group)
3516     (forward-line -1)
3517     (gnus-group-position-cursor)))
3518
3519 (defun gnus-group-insert-group-line-info (group)
3520   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3521         active info)
3522     (if entry
3523         (progn
3524           (setq info (nth 2 entry))
3525           (gnus-group-insert-group-line 
3526            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3527       (setq active (gnus-gethash group gnus-active-hashtb))
3528       (gnus-group-insert-group-line 
3529        nil group 
3530        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3531        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3532
3533 (defun gnus-group-insert-group-line (gformat group level marked number method)
3534   (let* ((gformat (or gformat gnus-group-line-format-spec))
3535          (active (gnus-gethash group gnus-active-hashtb))
3536          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3537          (number-of-dormant (length (cdr (assq 'dormant marked))))
3538          (number-of-ticked (length (cdr (assq 'tick marked))))
3539          (number-of-ticked-and-dormant
3540           (+ number-of-ticked number-of-dormant))
3541          (number-of-unread-unticked 
3542           (if (numberp number) (int-to-string (max 0 number))
3543             "*"))
3544          (number-of-read
3545           (if (numberp number)
3546               (max 0 (- number-total number))
3547             "*"))
3548          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3549                            ((<= level gnus-level-unsubscribed) ?U)
3550                            ((= level gnus-level-zombie) ?Z)
3551                            (t ?K)))
3552          (qualified-group (gnus-group-real-name group))
3553          (newsgroup-description 
3554           (if gnus-description-hashtb
3555               (or (gnus-gethash group gnus-description-hashtb) "")
3556             ""))
3557          (moderated (if (member group gnus-moderated-list) ?m ? ))
3558          (moderated-string (if (eq moderated ?m) "(m)" ""))
3559          (method (gnus-server-get-method group method))
3560          (news-server (or (car (cdr method)) ""))
3561          (news-method (or (car method) ""))
3562          (news-method-string 
3563           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3564          (marked (if (and 
3565                       (numberp number) 
3566                       (zerop number)
3567                       (> number-of-ticked 0))
3568                      ?* ? ))
3569          (number (if (eq number t) "*" (+ number number-of-dormant 
3570                                           number-of-ticked)))
3571          (process-marked (if (member qualified-group gnus-group-marked)
3572                              gnus-process-mark ? ))
3573          (buffer-read-only nil)
3574          header ; passed as parameter to user-funcs.
3575          b)
3576     (beginning-of-line)
3577     (setq b (point))
3578     ;; Insert the text.
3579     (insert (eval gformat))
3580
3581     (add-text-properties 
3582      b (1+ b) (list 'gnus-group (intern group)
3583                     'gnus-unread (if (numberp number)
3584                                      (string-to-int number-of-unread-unticked)
3585                                    t)
3586                     'gnus-marked marked
3587                     'gnus-level level))))
3588
3589 (defun gnus-group-update-group (group &optional visible-only)
3590   "Update newsgroup info of GROUP.
3591 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3592   (save-excursion
3593     (set-buffer gnus-group-buffer)
3594     (let ((buffer-read-only nil)
3595           visible)
3596       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3597         (if entry
3598             (gnus-dribble-enter 
3599              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3600                      ")"))))
3601       ;; Buffer may be narrowed.
3602       (save-restriction
3603         (widen)
3604         ;; Search a line to modify.  If the buffer is large, the search
3605         ;; takes long time.  In most cases, current point is on the line
3606         ;; we are looking for.  So, first of all, check current line. 
3607         (if (or (progn
3608                   (beginning-of-line)
3609                   (eq (get-text-property (point) 'gnus-group)
3610                       (intern group)))
3611                 (progn
3612                   (gnus-goto-char 
3613                    (text-property-any 
3614                     (point-min) (point-max) 'gnus-group (intern group)))))
3615             ;; GROUP is listed in current buffer. So, delete old line.
3616             (progn
3617               (setq visible t)
3618               (beginning-of-line)
3619               (delete-region (point) (progn (forward-line 1) (point))))
3620           ;; No such line in the buffer, find out where it's supposed to
3621           ;; go, and insert it there (or at the end of the buffer).
3622           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3623           (or visible-only
3624               (let ((entry 
3625                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3626                 (while (and entry
3627                             (car entry)
3628                             (not
3629                              (gnus-goto-char
3630                               (text-property-any
3631                                (point-min) (point-max) 
3632                                'gnus-group (intern (car (car entry)))))))
3633                   (setq entry (cdr entry)))
3634                 (or entry (goto-char (point-max)))))))
3635       (if (or visible (not visible-only))
3636           (gnus-group-insert-group-line-info group))
3637       (gnus-group-set-mode-line))))
3638
3639 (defun gnus-group-set-mode-line ()
3640   (if (memq 'group gnus-updated-mode-lines)
3641       (let* ((gformat (or gnus-group-mode-line-format-spec
3642                           (setq gnus-group-mode-line-format-spec
3643                                 (gnus-parse-format 
3644                                  gnus-group-mode-line-format 
3645                                  gnus-group-mode-line-format-alist))))
3646              (news-server (car (cdr gnus-select-method)))
3647              (news-method (car gnus-select-method))
3648              (max-len 60)
3649              (mode-string (eval gformat)))
3650         (setq mode-string (eval gformat))
3651         (if (> (length mode-string) max-len) 
3652             (setq mode-string (substring mode-string 0 (- max-len 4))))
3653         (setq mode-line-buffer-identification mode-string)
3654         (set-buffer-modified-p t))))
3655
3656 (defun gnus-group-group-name ()
3657   "Get the name of the newsgroup on the current line."
3658   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3659     (and group (symbol-name group))))
3660
3661 (defun gnus-group-group-level ()
3662   "Get the level of the newsgroup on the current line."
3663   (get-text-property (gnus-point-at-bol) 'gnus-level))
3664
3665 (defun gnus-group-group-unread ()
3666   "Get the number of unread articles of the newsgroup on the current line."
3667   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3668
3669 (defun gnus-group-search-forward (&optional backward all level first-too)
3670   "Find the next newsgroup with unread articles.
3671 If BACKWARD is non-nil, find the previous newsgroup instead.
3672 If ALL is non-nil, just find any newsgroup.
3673 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3674 group exists.
3675 If FIRST-TOO, the current line is also eligible as a target."
3676   (let ((way (if backward -1 1))
3677         (low gnus-level-killed)
3678         (beg (point))
3679         pos found lev)
3680     (if (and backward (progn (beginning-of-line)) (bobp))
3681         nil
3682       (or first-too (forward-line way))
3683       (while (and 
3684               (not (eobp))
3685               (not (setq 
3686                     found 
3687                     (and (or all
3688                              (and
3689                               (let ((unread 
3690                                      (get-text-property (point) 'gnus-unread)))
3691                                 (or (eq unread t) (and unread (> unread 0))))
3692                               (setq lev (get-text-property (point)
3693                                                            'gnus-level))
3694                               (<= lev gnus-level-subscribed)))
3695                          (or (not level)
3696                              (and (setq lev (get-text-property (point)
3697                                                                'gnus-level))
3698                                   (or (= lev level)
3699                                       (and (< lev low)
3700                                            (< level lev)
3701                                            (progn
3702                                              (setq low lev)
3703                                              (setq pos (point))
3704                                              nil))))))))
3705               (zerop (forward-line way)))))
3706     (if found 
3707         (progn (gnus-group-position-cursor) t)
3708       (goto-char (or pos beg))
3709       (and pos t))))
3710
3711 ;;; Gnus group mode commands
3712
3713 ;; Group marking.
3714
3715 (defun gnus-group-mark-group (n &optional unmark no-advance)
3716   "Mark the current group."
3717   (interactive "p")
3718   (let ((buffer-read-only nil)
3719         group)
3720     (while 
3721         (and (> n 0) 
3722              (setq group (gnus-group-group-name))
3723              (progn
3724                (beginning-of-line)
3725                (forward-char 
3726                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3727                (delete-char 1)
3728                (if unmark
3729                    (progn
3730                      (insert " ")
3731                      (setq gnus-group-marked (delete group gnus-group-marked)))
3732                  (insert "#")
3733                  (setq gnus-group-marked
3734                        (cons group (delete group gnus-group-marked))))
3735                t)
3736              (or no-advance (zerop (gnus-group-next-group 1))))
3737       (setq n (1- n)))
3738     (gnus-summary-position-cursor)
3739     n))
3740
3741 (defun gnus-group-unmark-group (n)
3742   "Remove the mark from the current group."
3743   (interactive "p")
3744   (gnus-group-mark-group n 'unmark))
3745
3746 (defun gnus-group-mark-region (unmark beg end)
3747   "Mark all groups between point and mark.
3748 If UNMARK, remove the mark instead."
3749   (interactive "P\nr")
3750   (let ((num (count-lines beg end)))
3751     (save-excursion
3752       (goto-char beg)
3753       (- num (gnus-group-mark-group num unmark)))))
3754
3755 (defun gnus-group-remove-mark (group)
3756   (and (gnus-group-goto-group group)
3757        (save-excursion
3758          (gnus-group-mark-group 1 'unmark t))))
3759
3760 ;; Return a list of groups to work on.  Take into consideration N (the
3761 ;; prefix) and the list of marked groups.
3762 (defun gnus-group-process-prefix (n)
3763   (cond (n
3764          (setq n (prefix-numeric-value n))
3765          ;; There is a prefix, so we return a list of the N next
3766          ;; groups. 
3767          (let ((way (if (< n 0) -1 1))
3768                (n (abs n))
3769                group groups)
3770            (save-excursion
3771              (while (and (> n 0)
3772                          (setq group (gnus-group-group-name)))
3773                (setq groups (cons group groups))
3774                (setq n (1- n))
3775                (forward-line way)))
3776            (nreverse groups)))
3777         (gnus-group-marked
3778          ;; No prefix, but a list of marked articles.
3779          (reverse gnus-group-marked))
3780         (t
3781          ;; Neither marked articles or a prefix, so we return the
3782          ;; current group.
3783          (let ((group (gnus-group-group-name)))
3784            (and group (list group))))))
3785
3786 ;; Selecting groups.
3787
3788 (defun gnus-group-read-group (&optional all no-article group)
3789   "Read news in this newsgroup.
3790 If the prefix argument ALL is non-nil, already read articles become
3791 readable. If the optional argument NO-ARTICLE is non-nil, no article
3792 will be auto-selected upon group entry."
3793   (interactive "P")
3794   (let ((group (or group (gnus-group-group-name)))
3795         number active marked entry)
3796     (or group (error "No group on current line"))
3797     (setq marked 
3798           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3799     ;; This group might be a dead group. In that case we have to get
3800     ;; the number of unread articles from `gnus-active-hashtb'.
3801     (if entry
3802         (setq number (car entry))
3803       (if (setq active (gnus-gethash group gnus-active-hashtb))
3804           (setq number (- (1+ (cdr active)) (car active)))))
3805     (gnus-summary-read-group 
3806      group (or all (and (numberp number) 
3807                         (zerop (+ number (length (cdr (assq 'tick marked)))
3808                                   (length (cdr (assq 'dormant marked)))))))
3809      no-article)))
3810
3811 (defun gnus-group-select-group (&optional all)
3812   "Select this newsgroup.
3813 No article is selected automatically.
3814 If argument ALL is non-nil, already read articles become readable."
3815   (interactive "P")
3816   (gnus-group-read-group all t))
3817
3818 ;; Enter a group that is not in the group buffer. Non-nil is returned
3819 ;; if selection was successful.
3820 (defun gnus-group-read-ephemeral-group 
3821   (group method &optional activate quit-config)
3822   (let ((group (if (gnus-group-foreign-p group) group
3823                  (gnus-group-prefixed-name group method))))
3824     (gnus-sethash 
3825      group
3826      (list t nil (list group gnus-level-default-subscribed nil nil 
3827                        (append method
3828                                (list
3829                                 (list 'quit-config 
3830                                       (if quit-config quit-config
3831                                         (cons (current-buffer) 'summary)))))))
3832      gnus-newsrc-hashtb)
3833     (set-buffer gnus-group-buffer)
3834     (or (gnus-server-opened method)
3835         (gnus-open-server method)
3836         (error "Unable to contact server: %s" (gnus-status-message method)))
3837     (if activate (or (gnus-request-group group)
3838                      (error "Couldn't request group")))
3839     (condition-case ()
3840         (gnus-group-read-group t t group)
3841       (error nil)
3842       (quit nil))
3843     (not (equal major-mode 'gnus-group-mode))))
3844   
3845 (defun gnus-group-jump-to-group (group)
3846   "Jump to newsgroup GROUP."
3847   (interactive 
3848    (list (completing-read 
3849           "Group: " gnus-active-hashtb nil 
3850           (memq gnus-select-method gnus-have-read-active-file))))
3851
3852   (if (equal group "")
3853       (error "Empty group name"))
3854
3855   (let ((b (text-property-any 
3856             (point-min) (point-max) 'gnus-group (intern group))))
3857     (if b
3858         ;; Either go to the line in the group buffer...
3859         (goto-char b)
3860       ;; ... or insert the line.
3861       (or
3862        (gnus-gethash group gnus-active-hashtb)
3863        (gnus-activate-newsgroup group)
3864        (error "%s error: %s" group (gnus-status-message group)))
3865
3866       (gnus-group-update-group group)
3867       (goto-char (text-property-any 
3868                   (point-min) (point-max) 'gnus-group (intern group)))))
3869   ;; Adjust cursor point.
3870   (gnus-group-position-cursor))
3871
3872 (defun gnus-group-goto-group (group)
3873   "Goto to newsgroup GROUP."
3874   (let ((b (text-property-any (point-min) (point-max) 
3875                               'gnus-group (intern group))))
3876     (and b (goto-char b))))
3877
3878 (defun gnus-group-next-group (n)
3879   "Go to next N'th newsgroup.
3880 If N is negative, search backward instead.
3881 Returns the difference between N and the number of skips actually
3882 done."
3883   (interactive "p")
3884   (gnus-group-next-unread-group n t))
3885
3886 (defun gnus-group-next-unread-group (n &optional all level)
3887   "Go to next N'th unread newsgroup.
3888 If N is negative, search backward instead.
3889 If ALL is non-nil, choose any newsgroup, unread or not.
3890 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3891 such group can be found, the next group with a level higher than
3892 LEVEL.
3893 Returns the difference between N and the number of skips actually
3894 made."
3895   (interactive "p")
3896   (let ((backward (< n 0))
3897         (n (abs n)))
3898     (while (and (> n 0)
3899                 (gnus-group-search-forward 
3900                  backward (or (not gnus-group-goto-unread) all) level))
3901       (setq n (1- n)))
3902     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3903                                (if level " on this level or higher" "")))
3904     n))
3905
3906 (defun gnus-group-prev-group (n)
3907   "Go to previous N'th newsgroup.
3908 Returns the difference between N and the number of skips actually
3909 done."
3910   (interactive "p")
3911   (gnus-group-next-unread-group (- n) t))
3912
3913 (defun gnus-group-prev-unread-group (n)
3914   "Go to previous N'th unread newsgroup.
3915 Returns the difference between N and the number of skips actually
3916 done."  
3917   (interactive "p")
3918   (gnus-group-next-unread-group (- n)))
3919
3920 (defun gnus-group-next-unread-group-same-level (n)
3921   "Go to next N'th unread newsgroup on the same level.
3922 If N is negative, search backward instead.
3923 Returns the difference between N and the number of skips actually
3924 done."
3925   (interactive "p")
3926   (gnus-group-next-unread-group n t (gnus-group-group-level))
3927   (gnus-group-position-cursor))
3928
3929 (defun gnus-group-prev-unread-group-same-level (n)
3930   "Go to next N'th unread newsgroup on the same level.
3931 Returns the difference between N and the number of skips actually
3932 done."
3933   (interactive "p")
3934   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3935   (gnus-group-position-cursor))
3936
3937 (defun gnus-group-best-unread-group (&optional exclude-group)
3938   "Go to the group with the highest level.
3939 If EXCLUDE-GROUP, do not go to that group."
3940   (interactive)
3941   (goto-char (point-min))
3942   (let ((best 100000)
3943         unread best-point)
3944     (while (setq unread (get-text-property (point) 'gnus-unread))
3945       (if (and (numberp unread) (> unread 0))
3946           (progn
3947             (if (and (< (get-text-property (point) 'gnus-level) best)
3948                      (or (not exclude-group)
3949                          (not (equal exclude-group (gnus-group-group-name)))))
3950                 (progn 
3951                   (setq best (get-text-property (point) 'gnus-level))
3952                   (setq best-point (point))))))
3953       (forward-line 1))
3954     (if best-point (goto-char best-point))
3955     (gnus-summary-position-cursor)
3956     (and best-point (gnus-group-group-name))))
3957
3958 (defun gnus-group-first-unread-group ()
3959   "Go to the first group with unread articles."
3960   (interactive)
3961   (prog1
3962       (let ((opoint (point))
3963             unread)
3964         (goto-char (point-min))
3965         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
3966                 (not (zerop unread)) ; Has unread articles.
3967                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
3968             (point) ; Success.
3969           (goto-char opoint)
3970           nil)) ; Not success.
3971     (gnus-group-position-cursor)))
3972
3973 (defun gnus-group-enter-server-mode ()
3974   "Jump to the server buffer."
3975   (interactive)
3976   (gnus-server-setup-buffer)
3977   (gnus-configure-windows 'server)
3978   (gnus-server-prepare))
3979
3980 (defun gnus-group-make-group (name method &optional address)
3981   "Add a new newsgroup.
3982 The user will be prompted for a NAME, for a select METHOD, and an
3983 ADDRESS."
3984   (interactive
3985    (cons 
3986     (read-string "Group name: ")
3987     (let ((method
3988            (completing-read 
3989             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3990             nil t)))
3991       (if (assoc method gnus-valid-select-methods)
3992           (list method
3993                 (if (memq 'prompt-address
3994                           (assoc method gnus-valid-select-methods))
3995                     (read-string "Address: ")
3996                   ""))
3997         (list method nil)))))
3998   
3999   (let* ((meth (if address (list (intern method) address) method))
4000          (nname (gnus-group-prefixed-name name meth))
4001          info)
4002     (and (gnus-gethash nname gnus-newsrc-hashtb)
4003          (error "Group %s already exists" nname))
4004     (gnus-group-change-level 
4005      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4006      gnus-level-default-subscribed gnus-level-killed 
4007      (and (gnus-group-group-name)
4008           (gnus-gethash (gnus-group-group-name)
4009                         gnus-newsrc-hashtb))
4010      t)
4011     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4012     (gnus-dribble-enter 
4013      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4014     (gnus-group-insert-group-line-info nname)
4015
4016     (if (assoc method gnus-valid-select-methods)
4017         (require (intern method)))
4018     (and (gnus-check-backend-function 'request-create-group nname)
4019          (gnus-request-create-group nname))))
4020
4021 (defun gnus-group-edit-group (group &optional part)
4022   "Edit the group on the current line."
4023   (interactive (list (gnus-group-group-name)))
4024   (let ((done-func '(lambda () 
4025                       "Exit editing mode and update the information."
4026                       (interactive)
4027                       (gnus-group-edit-group-done 'part 'group)))
4028         (part (or part 'info))
4029         (winconf (current-window-configuration))
4030         info)
4031     (or group (error "No group on current line"))
4032     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4033         (error "Killed group; can't be edited"))
4034     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4035     (gnus-configure-windows 'edit-group)
4036     (gnus-add-current-to-buffer-list)
4037     (emacs-lisp-mode)
4038     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4039     (use-local-map (copy-keymap emacs-lisp-mode-map))
4040     (local-set-key "\C-c\C-c" done-func)
4041     (make-local-variable 'gnus-prev-winconf)
4042     (setq gnus-prev-winconf winconf)
4043     ;; We modify the func to let it know what part it is editing.
4044     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4045     (setcar (cdr (cdr (nth 4 done-func))) group)
4046     (erase-buffer)
4047     (insert
4048      (cond 
4049       ((eq part 'method)
4050        ";; Type `C-c C-c' after editing the select method.\n\n")
4051       ((eq part 'params)
4052        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4053       ((eq part 'info)
4054        ";; Type `C-c C-c' after editing the group info.\n\n")))
4055     (let ((cinfo (gnus-copy-sequence info))
4056           marked)
4057       (if (not (setq marked (nth 3 cinfo)))
4058           ()
4059         (while marked
4060           (or (eq 'score (car (car marked)))
4061               (eq 'bookmark (car (car marked)))
4062               (eq 'killed (car (car marked)))
4063               (not (numberp (car (cdr (car marked)))))
4064               (setcdr (car marked) 
4065                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4066           (setq marked (cdr marked))))
4067       (insert 
4068        (pp-to-string
4069         (cond ((eq part 'method)
4070                (or (nth 4 info) "native"))
4071               ((eq part 'params)
4072                (nth 5 info))
4073               (t
4074                cinfo)))
4075        "\n"))))
4076
4077 (defun gnus-group-edit-group-method (group)
4078   "Edit the select method of GROUP."
4079   (interactive (list (gnus-group-group-name)))
4080   (gnus-group-edit-group group 'method))
4081
4082 (defun gnus-group-edit-group-parameters (group)
4083   "Edit the group parameters of GROUP."
4084   (interactive (list (gnus-group-group-name)))
4085   (gnus-group-edit-group group 'params))
4086
4087 (defun gnus-group-edit-group-done (part group)
4088   "Get info from buffer, update variables and jump to the group buffer."
4089   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4090   (goto-char (point-min))
4091   (let ((form (read (current-buffer)))
4092         (winconf gnus-prev-winconf))
4093     (if (eq part 'info) 
4094         (gnus-group-set-info form)
4095       (gnus-group-set-info form group part))
4096     (kill-buffer (current-buffer))
4097     (and winconf (set-window-configuration winconf))
4098     (set-buffer gnus-group-buffer)
4099     (gnus-group-update-group (gnus-group-group-name))
4100     (gnus-group-position-cursor)))
4101
4102 (defun gnus-group-make-help-group ()
4103   "Create the (ding) Gnus documentation group."
4104   (interactive)
4105   (let ((path load-path)
4106         name)
4107     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4108                                    "gnus-help" '(nndoc "gnus-help")))
4109                        gnus-newsrc-hashtb)
4110          (error "Documentation group already exists"))
4111     (while (and path
4112                 (not (file-exists-p (concat (file-name-as-directory (car path))
4113                                             "doc.txt"))))
4114       (setq path (cdr path)))
4115     (or path (error "Couldn't find doc group"))
4116     (gnus-group-make-group 
4117      (gnus-group-real-name name)
4118      (list 'nndoc name
4119            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4120            (list 'nndoc-article-type 'mbox))))
4121   (gnus-group-position-cursor))
4122
4123 (defun gnus-group-make-doc-group (file type)
4124   "Create a group that uses a single file as the source."
4125   (interactive 
4126    (list (read-file-name "File name: ") 
4127          (let ((err "")
4128                found char)
4129            (while (not found)
4130              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4131              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4132                                ((= char ?b) 'babyl)
4133                                ((= char ?d) 'digest)
4134                                (t (setq err (format "%c unknown. " char))
4135                                   nil))))
4136            found)))
4137   (let* ((file (expand-file-name file))
4138          (name (gnus-generate-new-group-name
4139                 (gnus-group-prefixed-name
4140                  (file-name-nondirectory file) '(nndoc "")))))
4141     (gnus-group-make-group 
4142      (gnus-group-real-name name)
4143      (list 'nndoc name
4144            (list 'nndoc-address file)
4145            (list 'nndoc-article-type type)))))
4146
4147 (defun gnus-group-make-archive-group ()
4148   "Create the (ding) Gnus archive group."
4149   (interactive)
4150   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4151                      gnus-newsrc-hashtb)
4152        (error "Archive group already exists"))
4153   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4154   (gnus-group-position-cursor))
4155
4156 (defun gnus-group-make-directory-group (dir)
4157   "Create an nndir group.
4158 The user will be prompted for a directory. The contents of this
4159 directory will be used as a newsgroup. The directory should contain
4160 mail messages or news articles in files that have numeric names."
4161   (interactive
4162    (list (read-file-name "Create group from directory: ")))
4163   (or (file-exists-p dir) (error "No such directory"))
4164   (or (file-directory-p dir) (error "Not a directory"))
4165   (gnus-group-make-group dir "nndir" dir)
4166   (gnus-group-position-cursor))
4167
4168 (defun gnus-group-make-kiboze-group (group address scores)
4169   "Create an nnkiboze group.
4170 The user will be prompted for a name, a regexp to match groups, and
4171 score file entries for articles to include in the group."
4172   (interactive
4173    (list
4174     (read-string "nnkiboze group name: ")
4175     (read-string "Source groups (regexp): ")
4176     (let ((headers (mapcar (lambda (group) (list group))
4177                            '("subject" "from" "number" "date" "message-id"
4178                              "references" "chars" "lines" "xref")))
4179           scores header regexp regexps)
4180       (while (not (equal "" (setq header (completing-read 
4181                                           "Match on header: " headers nil t))))
4182         (setq regexps nil)
4183         (while (not (equal "" (setq regexp (read-string 
4184                                             (format "Match on %s (string): "
4185                                                     header)))))
4186           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4187         (setq scores (cons (cons header regexps) scores)))
4188       scores)))
4189   (gnus-group-make-group group "nnkiboze" address)
4190   (save-excursion
4191     (gnus-set-work-buffer)
4192     (let (emacs-lisp-mode-hook)
4193       (pp scores (current-buffer)))
4194     (write-region (point-min) (point-max) 
4195                   (concat (or gnus-kill-files-directory "~/News")
4196                           "nnkiboze:" group "." gnus-score-file-suffix)))
4197   (gnus-group-position-cursor))
4198
4199 (defun gnus-group-add-to-virtual (n vgroup)
4200   "Add the current group to a virtual group."
4201   (interactive
4202    (list current-prefix-arg
4203          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4204                           "nnvirtual:")))
4205   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4206       (error "%s is not an nnvirtual group" vgroup))
4207   (let* ((groups (gnus-group-process-prefix n))
4208          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4209     (setcar (cdr method)
4210             (concat 
4211              (nth 1 method) "\\|"
4212              (mapconcat 
4213               (lambda (s) 
4214                 (gnus-group-remove-mark s)
4215                 (concat "\\(^" (regexp-quote s) "$\\)"))
4216               groups "\\|"))))
4217   (gnus-group-position-cursor))
4218
4219 (defun gnus-group-make-empty-virtual (group)
4220   "Create a new, fresh, empty virtual group."
4221   (interactive "sCreate new, empty virtual group: ")
4222   (let* ((method (list 'nnvirtual "^$"))
4223          (pgroup (gnus-group-prefixed-name group method)))
4224     ;; Check whether it exists already.
4225     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4226          (error "Group %s already exists." pgroup))
4227     ;; Subscribe the new group after the group on the current line.
4228     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4229     (gnus-group-update-group pgroup)
4230     (forward-line -1)
4231     (gnus-group-position-cursor)))
4232
4233 (defun gnus-group-enter-directory (dir)
4234   "Enter an ephemeral nneething group."
4235   (interactive "DDirectory to read: ")
4236   (let* ((method (list 'nneething dir))
4237          (leaf (gnus-group-prefixed-name
4238                 (file-name-nondirectory (directory-file-name dir))
4239                 method))
4240          (name (gnus-generate-new-group-name leaf)))
4241     (let ((nneething-read-only t))
4242       (or (gnus-group-read-ephemeral-group 
4243            name method t
4244            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4245                                       'summary 'group)))
4246           (error "Couldn't enter %s" dir)))))
4247
4248 ;; Group sorting commands
4249 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4250
4251 (defun gnus-group-sort-groups ()
4252   "Sort the group buffer using `gnus-group-sort-function'."
4253   (interactive)
4254   (setq gnus-newsrc-alist 
4255         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4256   (gnus-make-hashtable-from-newsrc-alist)
4257   (gnus-group-list-groups))
4258
4259 (defun gnus-group-sort-by-alphabet (info1 info2)
4260   (string< (car info1) (car info2)))
4261
4262 (defun gnus-group-sort-by-unread (info1 info2)
4263   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4264         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4265     (< (or (and (numberp n1) n1) 0)
4266        (or (and (numberp n2) n2) 0))))
4267
4268 (defun gnus-group-sort-by-level (info1 info2)
4269   (< (nth 1 info1) (nth 1 info2)))
4270
4271 ;; Group catching up.
4272
4273 (defun gnus-group-catchup-current (&optional n all)
4274   "Mark all articles not marked as unread in current newsgroup as read.
4275 If prefix argument N is numeric, the ARG next newsgroups will be
4276 caught up. If ALL is non-nil, marked articles will also be marked as
4277 read. Cross references (Xref: header) of articles are ignored.
4278 The difference between N and actual number of newsgroups that were
4279 caught up is returned."
4280   (interactive "P")
4281   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4282                gnus-expert-user
4283                (gnus-y-or-n-p
4284                 (if all
4285                     "Do you really want to mark all articles as read? "
4286                   "Mark all unread articles as read? "))))
4287       n
4288     (let ((groups (gnus-group-process-prefix n))
4289           (ret 0))
4290       (while groups
4291         ;; Virtual groups have to be given special treatment. 
4292         (let ((method (gnus-find-method-for-group (car groups))))
4293           (if (eq 'nnvirtual (car method))
4294               (nnvirtual-catchup-group
4295                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4296         (gnus-group-remove-mark (car groups))
4297         (if (prog1
4298                 (gnus-group-goto-group (car groups))
4299               (gnus-group-catchup (car groups) all))
4300             (gnus-group-update-group-line)
4301           (setq ret (1+ ret)))
4302         (setq groups (cdr groups)))
4303       (gnus-group-next-unread-group 1)
4304       ret)))
4305
4306 (defun gnus-group-catchup-current-all (&optional n)
4307   "Mark all articles in current newsgroup as read.
4308 Cross references (Xref: header) of articles are ignored."
4309   (interactive "P")
4310   (gnus-group-catchup-current n 'all))
4311
4312 (defun gnus-group-catchup (group &optional all)
4313   "Mark all articles in GROUP as read.
4314 If ALL is non-nil, all articles are marked as read.
4315 The return value is the number of articles that were marked as read,
4316 or nil if no action could be taken."
4317   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4318          (num (car entry))
4319          (marked (nth 3 (nth 2 entry))))
4320     (if (not (numberp (car entry)))
4321         (gnus-message 1 "Can't catch up; non-active group")
4322       ;; Do the updating only if the newsgroup isn't killed.
4323       (if (not entry)
4324           ()
4325         (gnus-update-read-articles 
4326          group (and (not all) (append (cdr (assq 'tick marked))
4327                                       (cdr (assq 'dormant marked))))
4328          nil (and (not all) (cdr (assq 'tick marked))))
4329         (and all marked
4330              (setcar (nthcdr 3 (nth 2 entry)) 
4331                      (delq (assq 'dormant marked) 
4332                            (nth 3 (nth 2 entry)))))))
4333     num))
4334
4335 (defun gnus-group-expire-articles (&optional n)
4336   "Expire all expirable articles in the current newsgroup."
4337   (interactive "P")
4338   (let ((groups (gnus-group-process-prefix n))
4339         group)
4340     (or groups (error "No groups to expire"))
4341     (while groups
4342       (setq group (car groups)
4343             groups (cdr groups))
4344       (gnus-group-remove-mark group)
4345       (if (not (gnus-check-backend-function 'request-expire-articles group))
4346           ()
4347         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4348                (expirable (if (memq 'total-expire (nth 5 info))
4349                               (cons nil (gnus-list-of-read-articles group))
4350                             (assq 'expire (nth 3 info)))))
4351           (and expirable 
4352                (setcdr expirable
4353                        (gnus-request-expire-articles 
4354                         (cdr expirable) group))))))))
4355
4356 (defun gnus-group-expire-all-groups ()
4357   "Expire all expirable articles in all newsgroups."
4358   (interactive)
4359   (save-excursion
4360     (gnus-message 5 "Expiring...")
4361     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4362                                      (cdr gnus-newsrc-alist))))
4363       (gnus-group-expire-articles nil)))
4364   (gnus-group-position-cursor)
4365   (gnus-message 5 "Expiring...done"))
4366
4367 (defun gnus-group-set-current-level (n level)
4368   "Set the level of the next N groups to LEVEL."
4369   (interactive "P\nnLevel: ")
4370   (or (and (>= level 1) (<= level gnus-level-killed))
4371       (error "Illegal level: %d" level))
4372   (let ((groups (gnus-group-process-prefix n))
4373         group)
4374     (while groups
4375       (setq group (car groups)
4376             groups (cdr groups))
4377       (gnus-group-remove-mark group)
4378       (gnus-message 6 "Changed level of %s from %d to %d" 
4379                     group (gnus-group-group-level) level)
4380       (gnus-group-change-level group level
4381                                (gnus-group-group-level))
4382       (gnus-group-update-group-line)))
4383   (gnus-group-position-cursor))
4384
4385 (defun gnus-group-unsubscribe-current-group (&optional n)
4386   "Toggle subscription of the current group.
4387 If given numerical prefix, toggle the N next groups."
4388   (interactive "P")
4389   (let ((groups (gnus-group-process-prefix n))
4390         group)
4391     (while groups
4392       (setq group (car groups)
4393             groups (cdr groups))
4394       (gnus-group-remove-mark group)
4395       (gnus-group-unsubscribe-group
4396        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4397                  gnus-level-default-unsubscribed
4398                gnus-level-default-subscribed))
4399       (gnus-group-update-group-line))
4400     (gnus-group-next-group 1)))
4401
4402 (defun gnus-group-unsubscribe-group (group &optional level)
4403   "Toggle subscribe from/to unsubscribe GROUP.
4404 New newsgroup is added to .newsrc automatically."
4405   (interactive
4406    (list (completing-read
4407           "Group: " gnus-active-hashtb nil 
4408           (memq gnus-select-method gnus-have-read-active-file))))
4409   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4410     (cond (newsrc
4411            ;; Toggle subscription flag.
4412            (gnus-group-change-level 
4413             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4414                                            gnus-level-subscribed) 
4415                                        (1+ gnus-level-subscribed)
4416                                      gnus-level-default-subscribed)))
4417            (gnus-group-update-group group))
4418           ((and (stringp group)
4419                 (or (not (memq gnus-select-method gnus-have-read-active-file))
4420                     (gnus-gethash group gnus-active-hashtb)))
4421            ;; Add new newsgroup.
4422            (gnus-group-change-level 
4423             group 
4424             (if level level gnus-level-default-subscribed) 
4425             (or (and (member group gnus-zombie-list) 
4426                      gnus-level-zombie) 
4427                 gnus-level-killed)
4428             (and (gnus-group-group-name)
4429                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4430            (gnus-group-update-group group))
4431           (t (error "No such newsgroup: %s" group)))
4432     (gnus-group-position-cursor)))
4433
4434 (defun gnus-group-transpose-groups (n)
4435   "Move the current newsgroup up N places.
4436 If given a negative prefix, move down instead. The difference between
4437 N and the number of steps taken is returned." 
4438   (interactive "p")
4439   (or (gnus-group-group-name)
4440       (error "No group on current line"))
4441   (gnus-group-kill-group 1)
4442   (prog1
4443       (forward-line (- n))
4444     (gnus-group-yank-group)
4445     (gnus-group-position-cursor)))
4446
4447 (defun gnus-group-kill-all-zombies ()
4448   "Kill all zombie newsgroups."
4449   (interactive)
4450   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4451   (setq gnus-zombie-list nil)
4452   (gnus-group-list-groups))
4453
4454 (defun gnus-group-kill-region (begin end)
4455   "Kill newsgroups in current region (excluding current point).
4456 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4457   (interactive "r")
4458   (let ((lines
4459          ;; Count lines.
4460          (save-excursion
4461            (count-lines
4462             (progn
4463               (goto-char begin)
4464               (beginning-of-line)
4465               (point))
4466             (progn
4467               (goto-char end)
4468               (beginning-of-line)
4469               (point))))))
4470     (goto-char begin)
4471     (beginning-of-line)                 ;Important when LINES < 1
4472     (gnus-group-kill-group lines)))
4473
4474 (defun gnus-group-kill-group (&optional n)
4475   "The the next N groups.
4476 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4477 However, only groups that were alive can be yanked; already killed 
4478 groups or zombie groups can't be yanked.
4479 The return value is the name of the (last) group that was killed."
4480   (interactive "P")
4481   (let ((buffer-read-only nil)
4482         (groups (gnus-group-process-prefix n))
4483         group entry level)
4484     (while groups
4485       (setq group (car groups)
4486             groups (cdr groups))
4487       (gnus-group-remove-mark group)
4488       (setq level (gnus-group-group-level))
4489       (gnus-delete-line)
4490       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4491           (setq gnus-list-of-killed-groups 
4492                 (cons (cons (car entry) (nth 2 entry)) 
4493                       gnus-list-of-killed-groups)))
4494       (gnus-group-change-level 
4495        (if entry entry group) gnus-level-killed (if entry nil level)))
4496     (gnus-group-position-cursor)
4497     group))
4498
4499 (defun gnus-group-yank-group (&optional arg)
4500   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4501 inserting it before the current newsgroup.  The numeric ARG specifies
4502 how many newsgroups are to be yanked.  The name of the (last)
4503 newsgroup yanked is returned."
4504   (interactive "p")
4505   (if (not arg) (setq arg 1))
4506   (let (info group prev)
4507     (while (>= (setq arg (1- arg)) 0)
4508       (if (not (setq info (car gnus-list-of-killed-groups)))
4509           (error "No more newsgroups to yank"))
4510       (setq group (nth 2 info))
4511       ;; Find which newsgroup to insert this one before - search
4512       ;; backward until something suitable is found. If there are no
4513       ;; other newsgroups in this buffer, just make this newsgroup the
4514       ;; first newsgroup.
4515       (setq prev (gnus-group-group-name))
4516       (gnus-group-change-level 
4517        info (nth 2 info) gnus-level-killed 
4518        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4519        t)
4520       (gnus-group-insert-group-line-info (nth 1 info))
4521       (setq gnus-list-of-killed-groups 
4522             (cdr gnus-list-of-killed-groups)))
4523     (forward-line -1)
4524     (gnus-group-position-cursor)
4525     group))
4526       
4527 (defun gnus-group-list-all-groups (&optional arg)
4528   "List all newsgroups with level ARG or lower.
4529 Default is gnus-level-unsubscribed, which lists all subscribed and most
4530 unsubscribed groups."
4531   (interactive "P")
4532   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4533
4534 (defun gnus-group-list-killed ()
4535   "List all killed newsgroups in the group buffer."
4536   (interactive)
4537   (if (not gnus-killed-list)
4538       (gnus-message 6 "No killed groups")
4539     (let (gnus-group-list-mode)
4540       (funcall gnus-group-prepare-function 
4541                gnus-level-killed t gnus-level-killed))
4542     (goto-char (point-min)))
4543   (gnus-group-position-cursor))
4544
4545 (defun gnus-group-list-zombies ()
4546   "List all zombie newsgroups in the group buffer."
4547   (interactive)
4548   (if (not gnus-zombie-list)
4549       (gnus-message 6 "No zombie groups")
4550     (let (gnus-group-list-mode)
4551       (funcall gnus-group-prepare-function
4552                gnus-level-zombie t gnus-level-zombie))
4553     (goto-char (point-min)))
4554   (gnus-group-position-cursor))
4555
4556 (defun gnus-group-get-new-news (&optional arg)
4557   "Get newly arrived articles.
4558 If ARG is non-nil, it should be a number between one and nine to
4559 specify which levels you are interested in re-scanning."
4560   (interactive "P")
4561   (run-hooks 'gnus-get-new-news-hook)
4562   (setq arg (gnus-group-default-level arg t))
4563   (if (and gnus-read-active-file (not arg))
4564       (progn
4565         (gnus-read-active-file)
4566         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4567     (let ((gnus-read-active-file (not arg))
4568           (gnus-have-read-active-file 
4569            (and (not arg) gnus-have-read-active-file)))
4570       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4571   (gnus-group-list-groups))
4572
4573 (defun gnus-group-get-new-news-this-group (&optional n)
4574   "Check for newly arrived news in the current group (and the N-1 next groups).
4575 The difference between N and the number of newsgroup checked is returned.
4576 If N is negative, this group and the N-1 previous groups will be checked."
4577   (interactive "P")
4578   (let* ((groups (gnus-group-process-prefix n))
4579          (ret (if (numberp n) (- n (length groups)) 0))
4580          group)
4581     (while groups
4582       (setq group (car groups)
4583             groups (cdr groups))
4584       (gnus-group-remove-mark group)
4585       (or (gnus-get-new-news-in-group group)
4586           (progn 
4587             (ding) 
4588             (message "%s error: %s" group (gnus-status-message group))
4589             (sit-for 2))))
4590     ;; !!! I don't know why the buffer scrolls forward when updating
4591     ;; the first line in the group buffer, but it does. So we set the
4592     ;; window start forcibly.
4593 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4594     (gnus-group-next-unread-group 1 t)
4595     (gnus-summary-position-cursor)
4596     ret))
4597
4598 (defun gnus-get-new-news-in-group (group)
4599   (and group 
4600        (gnus-activate-newsgroup group)
4601        (progn
4602          (gnus-get-unread-articles-in-group 
4603           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4604           (gnus-gethash group gnus-active-hashtb))
4605          (gnus-group-update-group-line)
4606          t)))
4607
4608 (defun gnus-group-fetch-faq (group)
4609   "Fetch the FAQ for the current group."
4610   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4611   (or group (error "No group name given"))
4612   (let ((file (concat gnus-group-faq-directory group))) 
4613     (if (not (file-exists-p file))
4614         (error "No such file: %s" file)
4615       (find-file file))))
4616   
4617 (defun gnus-group-describe-group (force &optional group)
4618   "Display a description of the current newsgroup."
4619   (interactive (list current-prefix-arg (gnus-group-group-name)))
4620   (and force (setq gnus-description-hashtb nil))
4621   (let ((method (gnus-find-method-for-group group))
4622         desc)
4623     (or group (error "No group name given"))
4624     (and (or (and gnus-description-hashtb
4625                   ;; We check whether this group's method has been
4626                   ;; queried for a description file.  
4627                   (gnus-gethash 
4628                    (gnus-group-prefixed-name "" method) 
4629                    gnus-description-hashtb))
4630              (setq desc (gnus-group-get-description group))
4631              (gnus-read-descriptions-file method))
4632          (message
4633           (or desc (gnus-gethash group gnus-description-hashtb)
4634               "No description available")))))
4635
4636 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4637 (defun gnus-group-describe-all-groups (&optional force)
4638   "Pop up a buffer with descriptions of all newsgroups."
4639   (interactive "P")
4640   (and force (setq gnus-description-hashtb nil))
4641   (if (not (or gnus-description-hashtb
4642                (gnus-read-all-descriptions-files)))
4643       (error "Couldn't request descriptions file"))
4644   (let ((buffer-read-only nil)
4645         b)
4646     (erase-buffer)
4647     (mapatoms
4648      (lambda (group)
4649        (setq b (point))
4650        (insert (format "      *: %-20s %s\n" (symbol-name group)
4651                        (symbol-value group)))
4652        (add-text-properties 
4653         b (1+ b) (list 'gnus-group group
4654                        'gnus-unread t 'gnus-marked nil
4655                        'gnus-level (1+ gnus-level-subscribed))))
4656      gnus-description-hashtb)
4657     (goto-char (point-min))
4658     (gnus-group-position-cursor)))
4659
4660 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4661 (defun gnus-group-apropos (regexp &optional search-description)
4662   "List all newsgroups that have names that match a regexp."
4663   (interactive "sGnus apropos (regexp): ")
4664   (let ((prev "")
4665         (obuf (current-buffer))
4666         groups des)
4667     ;; Go through all newsgroups that are known to Gnus.
4668     (mapatoms 
4669      (lambda (group)
4670        (and (symbol-name group)
4671             (string-match regexp (symbol-name group))
4672             (setq groups (cons (symbol-name group) groups))))
4673      gnus-active-hashtb)
4674     ;; Go through all descriptions that are known to Gnus. 
4675     (if search-description
4676         (mapatoms 
4677          (lambda (group)
4678            (and (string-match regexp (symbol-value group))
4679                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4680                 (setq groups (cons (symbol-name group) groups))))
4681          gnus-description-hashtb))
4682     (if (not groups)
4683         (gnus-message 3 "No groups matched \"%s\"." regexp)
4684       ;; Print out all the groups.
4685       (save-excursion
4686         (pop-to-buffer "*Gnus Help*")
4687         (buffer-disable-undo (current-buffer))
4688         (erase-buffer)
4689         (setq groups (sort groups 'string<))
4690         (while groups
4691           ;; Groups may be entered twice into the list of groups.
4692           (if (not (string= (car groups) prev))
4693               (progn
4694                 (insert (setq prev (car groups)) "\n")
4695                 (if (and gnus-description-hashtb
4696                          (setq des (gnus-gethash (car groups) 
4697                                                  gnus-description-hashtb)))
4698                     (insert "  " des "\n"))))
4699           (setq groups (cdr groups)))
4700         (goto-char (point-min))))
4701     (pop-to-buffer obuf)))
4702
4703 (defun gnus-group-description-apropos (regexp)
4704   "List all newsgroups that have names or descriptions that match a regexp."
4705   (interactive "sGnus description apropos (regexp): ")
4706   (if (not (or gnus-description-hashtb
4707                (gnus-read-all-descriptions-files)))
4708       (error "Couldn't request descriptions file"))
4709   (gnus-group-apropos regexp t))
4710
4711 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4712 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4713   "List all groups with unread articles that match REGEXP.
4714 If the prefix LEVEL is non-nil, it should be a number that says which
4715 level to cut off listing groups. 
4716 If ALL, also list groups with no unread articles.
4717 If LOWEST, don't list groups with level lower than LOWEST."
4718   (interactive "P\nsList newsgroups matching: ")
4719   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4720                            all (or lowest 1) regexp)
4721   (goto-char (point-min))
4722   (gnus-group-position-cursor))
4723
4724 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4725   "List all groups that match REGEXP.
4726 If the prefix LEVEL is non-nil, it should be a number that says which
4727 level to cut off listing groups. 
4728 If LOWEST, don't list groups with level lower than LOWEST."
4729   (interactive "P\nsList newsgroups matching: ")
4730   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4731
4732 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4733 (defun gnus-group-save-newsrc ()
4734   "Save the Gnus startup files."
4735   (interactive)
4736   (gnus-save-newsrc-file))
4737
4738 (defun gnus-group-restart (&optional arg)
4739   "Force Gnus to read the .newsrc file."
4740   (interactive "P")
4741   (gnus-save-newsrc-file)
4742   (gnus-setup-news 'force)
4743   (gnus-group-list-groups arg))
4744
4745 (defun gnus-group-read-init-file ()
4746   "Read the Gnus elisp init file."
4747   (interactive)
4748   (gnus-read-init-file))
4749
4750 (defun gnus-group-check-bogus-groups (&optional silent)
4751   "Check bogus newsgroups.
4752 If given a prefix, don't ask for confirmation before removing a bogus
4753 group."
4754   (interactive "P")
4755   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4756   (gnus-group-list-groups))
4757
4758 (defun gnus-group-edit-global-kill (&optional article group)
4759   "Edit the global kill file.
4760 If GROUP, edit that local kill file instead."
4761   (interactive "P")
4762   (setq gnus-current-kill-article article)
4763   (gnus-kill-file-edit-file group)
4764   (gnus-message 6
4765    (substitute-command-keys
4766     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4767
4768 (defun gnus-group-edit-local-kill (article group)
4769   "Edit a local kill file."
4770   (interactive (list nil (gnus-group-group-name)))
4771   (gnus-group-edit-global-kill article group))
4772
4773 (defun gnus-group-force-update ()
4774   "Update `.newsrc' file."
4775   (interactive)
4776   (gnus-save-newsrc-file))
4777
4778 (defun gnus-group-suspend ()
4779   "Suspend the current Gnus session.
4780 In fact, cleanup buffers except for group mode buffer.
4781 The hook gnus-suspend-gnus-hook is called before actually suspending."
4782   (interactive)
4783   (run-hooks 'gnus-suspend-gnus-hook)
4784   ;; Kill Gnus buffers except for group mode buffer.
4785   (let ((group-buf (get-buffer gnus-group-buffer)))
4786     ;; Do this on a separate list in case the user does a ^G before we finish
4787     (let ((gnus-buffer-list
4788            (delq group-buf (delq gnus-dribble-buffer
4789                                  (append gnus-buffer-list nil)))))
4790       (while gnus-buffer-list
4791         (gnus-kill-buffer (car gnus-buffer-list))
4792         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4793     (if group-buf
4794         (progn
4795           (setq gnus-buffer-list (list group-buf))
4796           (bury-buffer group-buf)
4797           (delete-windows-on group-buf t)))))
4798
4799 (defun gnus-group-clear-dribble ()
4800   "Clear all information from the dribble buffer."
4801   (interactive)
4802   (gnus-dribble-clear))
4803
4804 (defun gnus-group-exit ()
4805   "Quit reading news after updating .newsrc.eld and .newsrc.
4806 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4807   (interactive)
4808   (if (or noninteractive                ;For gnus-batch-kill
4809           (zerop (buffer-size))         ;No news is good news.
4810           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4811           (not gnus-interactive-exit)   ;Without confirmation
4812           gnus-expert-user
4813           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4814       (progn
4815         (run-hooks 'gnus-exit-gnus-hook)
4816         ;; Offer to save data from non-quitted summary buffers.
4817         (gnus-offer-save-summaries)
4818         ;; Save the newsrc file(s).
4819         (gnus-save-newsrc-file)
4820         ;; Kill-em-all.
4821         (gnus-close-backends)
4822         ;; Reset everything.
4823         (gnus-clear-system))))
4824
4825 (defun gnus-close-backends ()
4826   ;; Send a close request to all backends that support such a request. 
4827   (let ((methods gnus-valid-select-methods)
4828         func)
4829     (while methods
4830       (if (fboundp (setq func (intern (concat (car (car methods))
4831                                               "-request-close"))))
4832           (funcall func))
4833       (setq methods (cdr methods)))))
4834
4835 (defun gnus-group-quit ()
4836   "Quit reading news without updating .newsrc.eld or .newsrc.
4837 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4838   (interactive)
4839   (if (or noninteractive                ;For gnus-batch-kill
4840           (zerop (buffer-size))
4841           (not (gnus-server-opened gnus-select-method))
4842           gnus-expert-user
4843           (not gnus-current-startup-file)
4844           (gnus-yes-or-no-p
4845            (format "Quit reading news without saving %s? "
4846                    (file-name-nondirectory gnus-current-startup-file))))
4847       (progn
4848         (run-hooks 'gnus-exit-gnus-hook)
4849         (if gnus-use-full-window
4850             (delete-other-windows)
4851           (gnus-remove-some-windows))
4852         (gnus-dribble-save)
4853         (gnus-close-backends)
4854         (gnus-clear-system))))
4855
4856 (defun gnus-offer-save-summaries ()
4857   (let ((buffers (buffer-list)))
4858     (save-excursion
4859       (while buffers
4860         (and 
4861          ;; We look for buffers with "Summary" in the name.
4862          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4863          (progn
4864            (set-buffer (car buffers))
4865            ;; We check that this is, indeed, a summary buffer.
4866            (eq major-mode 'gnus-summary-mode)) 
4867          ;; We ask the user whether she wants to save the info.
4868          (gnus-y-or-n-p
4869                (format "Update summary buffer %s? " (buffer-name)))
4870          ;; We do it by simply exiting.
4871          (gnus-summary-exit))
4872         (setq buffers (cdr buffers))))))
4873
4874 (defun gnus-group-describe-briefly ()
4875   "Give a one line description of the group mode commands."
4876   (interactive)
4877   (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")))
4878
4879 (defun gnus-group-browse-foreign-server (method)
4880   "Browse a foreign news server.
4881 If called interactively, this function will ask for a select method
4882  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4883 If not, METHOD should be a list where the first element is the method
4884 and the second element is the address."
4885   (interactive
4886    (list (let ((how (completing-read 
4887                      "Which backend: "
4888                      (append gnus-valid-select-methods gnus-server-alist)
4889                      nil t "nntp")))
4890            ;; We either got a backend name or a virtual server name.
4891            ;; If the first, we also need an address.
4892            (if (assoc how gnus-valid-select-methods)
4893                (list (intern how)
4894                      ;; Suggested by mapjph@bath.ac.uk.
4895                      (completing-read 
4896                       "Address: " 
4897                       (mapcar (lambda (server) (list server))
4898                               gnus-secondary-servers)))
4899              ;; We got a server name, so we find the method.
4900              (gnus-server-to-method how)))))
4901   (gnus-browse-foreign-server method))
4902
4903 \f
4904 ;;;
4905 ;;; Browse Server Mode
4906 ;;;
4907
4908 (defvar gnus-browse-mode-hook nil)
4909 (defvar gnus-browse-mode-map nil)
4910 (put 'gnus-browse-mode 'mode-class 'special)
4911
4912 (if gnus-browse-mode-map
4913     nil
4914   (setq gnus-browse-mode-map (make-keymap))
4915   (suppress-keymap gnus-browse-mode-map)
4916   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4917   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4918   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4919   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4920   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4921   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4922   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4923   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4924   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4925   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4926   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4927   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4928   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4929   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4930   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4931   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4932   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4933   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4934   )
4935
4936 (defvar gnus-browse-current-method nil)
4937 (defvar gnus-browse-return-buffer nil)
4938
4939 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4940
4941 (defun gnus-browse-foreign-server (method &optional return-buffer)
4942   (setq gnus-browse-current-method method)
4943   (setq gnus-browse-return-buffer return-buffer)
4944   (let ((gnus-select-method method)
4945         groups group)
4946     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4947     (or (gnus-server-opened method)
4948         (gnus-open-server method)
4949         (error "Unable to contact server: %s" (gnus-status-message method)))
4950     (or (gnus-request-list method)
4951         (error "Couldn't request list: %s" (gnus-status-message method)))
4952     (get-buffer-create gnus-browse-buffer)
4953     (gnus-add-current-to-buffer-list)
4954     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4955     (gnus-configure-windows 'browse)
4956     (buffer-disable-undo (current-buffer))
4957     (let ((buffer-read-only nil))
4958       (erase-buffer))
4959     (gnus-browse-mode)
4960     (setq mode-line-buffer-identification
4961           (format
4962            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4963     (save-excursion
4964       (set-buffer nntp-server-buffer)
4965       (let ((cur (current-buffer)))
4966         (goto-char (point-min))
4967         (or (string= gnus-ignored-newsgroups "")
4968             (delete-matching-lines gnus-ignored-newsgroups))
4969         (while (re-search-forward 
4970                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4971           (goto-char (match-end 1))
4972           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4973                                                      (match-end 1))
4974                                    (max 0 (- (1+ (read cur)) (read cur))))
4975                              groups)))))
4976     (setq groups (sort groups 
4977                        (lambda (l1 l2)
4978                          (string< (car l1) (car l2)))))
4979     (let ((buffer-read-only nil))
4980       (while groups
4981         (setq group (car groups))
4982         (insert 
4983          (format "K%7d: %s\n" (cdr group) (car group)))
4984         (setq groups (cdr groups))))
4985     (switch-to-buffer (current-buffer))
4986     (goto-char (point-min))
4987     (gnus-group-position-cursor)))
4988
4989 (defun gnus-browse-mode ()
4990   "Major mode for browsing a foreign server.
4991
4992 All normal editing commands are switched off.
4993
4994 \\<gnus-browse-mode-map>
4995 The only things you can do in this buffer is
4996
4997 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4998 The group will be inserted into the group buffer upon exit from this
4999 buffer.  
5000
5001 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5002
5003 3) `\\[gnus-browse-exit]' to return to the group buffer."
5004   (interactive)
5005   (kill-all-local-variables)
5006   (if gnus-visual (gnus-browse-make-menu-bar))
5007   (setq mode-line-modified "-- ")
5008   (make-local-variable 'mode-line-format)
5009   (setq mode-line-format (copy-sequence mode-line-format))
5010   (and (equal (nth 3 mode-line-format) "   ")
5011        (setcar (nthcdr 3 mode-line-format) ""))
5012   (setq major-mode 'gnus-browse-mode)
5013   (setq mode-name "Browse Server")
5014   (setq mode-line-process nil)
5015   (use-local-map gnus-browse-mode-map)
5016   (buffer-disable-undo (current-buffer))
5017   (setq truncate-lines t)
5018   (setq buffer-read-only t)
5019   (run-hooks 'gnus-browse-mode-hook))
5020
5021 (defun gnus-browse-read-group (&optional no-article)
5022   "Enter the group at the current line."
5023   (interactive)
5024   (let ((group (gnus-browse-group-name)))
5025     (or (gnus-group-read-ephemeral-group 
5026          group gnus-browse-current-method nil
5027          (cons (current-buffer) 'browse))
5028         (error "Couldn't enter %s" group))))
5029
5030 (defun gnus-browse-select-group ()
5031   "Select the current group."
5032   (interactive)
5033   (gnus-browse-read-group 'no))
5034
5035 (defun gnus-browse-next-group (n)
5036   "Go to the next group."
5037   (interactive "p")
5038   (prog1
5039       (forward-line n)
5040     (gnus-group-position-cursor)))
5041
5042 (defun gnus-browse-prev-group (n)
5043   "Go to the next group."
5044   (interactive "p")
5045   (gnus-browse-next-group (- n)))
5046
5047 (defun gnus-browse-unsubscribe-current-group (arg)
5048   "(Un)subscribe to the next ARG groups."
5049   (interactive "p")
5050   (and (eobp)
5051        (error "No group at current line."))
5052   (let ((ward (if (< arg 0) -1 1))
5053         (arg (abs arg)))
5054     (while (and (> arg 0)
5055                 (not (eobp))
5056                 (gnus-browse-unsubscribe-group)
5057                 (zerop (gnus-browse-next-group ward)))
5058       (setq arg (1- arg)))
5059     (gnus-group-position-cursor)
5060     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5061     arg))
5062
5063 (defun gnus-browse-group-name ()
5064   (save-excursion
5065     (beginning-of-line)
5066     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5067         ()
5068       (gnus-group-prefixed-name 
5069        (buffer-substring (match-beginning 1) (match-end 1))
5070        gnus-browse-current-method))))
5071   
5072 (defun gnus-browse-unsubscribe-group ()
5073   (let ((sub nil)
5074         (buffer-read-only nil)
5075         group)
5076     (save-excursion
5077       (beginning-of-line)
5078       (if (= (following-char) ?K) (setq sub t))
5079       (setq group (gnus-browse-group-name))
5080       (beginning-of-line)
5081       (delete-char 1)
5082       (if sub
5083           (progn
5084             (gnus-group-change-level 
5085              (list t group gnus-level-default-subscribed
5086                    nil nil gnus-browse-current-method) 
5087              gnus-level-default-subscribed gnus-level-killed
5088              (and (car (nth 1 gnus-newsrc-alist))
5089                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5090                                 gnus-newsrc-hashtb))
5091              t)
5092             (insert ? ))
5093         (gnus-group-change-level 
5094          group gnus-level-killed gnus-level-default-subscribed)
5095         (insert ?K)))
5096     t))
5097
5098 (defun gnus-browse-exit ()
5099   "Quit browsing and return to the group buffer."
5100   (interactive)
5101   (if (eq major-mode 'gnus-browse-mode)
5102       (kill-buffer (current-buffer)))
5103   (if gnus-browse-return-buffer
5104       (gnus-configure-windows 'server 'force)
5105     (gnus-configure-windows 'group 'force)
5106     (gnus-group-list-groups nil)))
5107
5108 (defun gnus-browse-describe-briefly ()
5109   "Give a one line description of the group mode commands."
5110   (interactive)
5111   (gnus-message 6
5112    (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")))
5113       
5114 \f
5115 ;;;
5116 ;;; Gnus summary mode
5117 ;;;
5118
5119 (defvar gnus-summary-mode-map nil)
5120 (defvar gnus-summary-mark-map nil)
5121 (defvar gnus-summary-mscore-map nil)
5122 (defvar gnus-summary-article-map nil)
5123 (defvar gnus-summary-thread-map nil)
5124 (defvar gnus-summary-goto-map nil)
5125 (defvar gnus-summary-exit-map nil)
5126 (defvar gnus-summary-various-map nil)
5127 (defvar gnus-summary-interest-map nil)
5128 (defvar gnus-summary-sort-map nil)
5129 (defvar gnus-summary-backend-map nil)
5130 (defvar gnus-summary-save-map nil)
5131 (defvar gnus-summary-wash-map nil)
5132 (defvar gnus-summary-help-map nil)
5133
5134 (put 'gnus-summary-mode 'mode-class 'special)
5135
5136 (if gnus-summary-mode-map
5137     nil
5138   (setq gnus-summary-mode-map (make-keymap))
5139   (suppress-keymap gnus-summary-mode-map)
5140
5141   ;; Non-orthogonal keys
5142
5143   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5144   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5145   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5146   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5147   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5148   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5149   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5150   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5151   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5152   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5153   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5154   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5155   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5156   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5157   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5158   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5159   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5160   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5161   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5162   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5163   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5164   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5165   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5166   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5167   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5168   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5169   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5170   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5171   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5172   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5173   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5174   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5175   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5176   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5177   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5178   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5179   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5180   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5181   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5182   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5183   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5184   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5185   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5186   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5187   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5188   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5189   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5190   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5191   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5192   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5193   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5194   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5195   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5196   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5197   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5198   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5199   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5200   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5201   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5202   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5203   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5204   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5205   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5206   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5207   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5208   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5209   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5210   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5211   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5212   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5213   (define-key gnus-summary-mode-map "V" 'gnus-version)
5214   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5215   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5216   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5217   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5218   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5219   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5220   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5221   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5222 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5223   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5224   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5225   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5226 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5227   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5228   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5229   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5230   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5231   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5232
5233
5234   ;; Sort of orthogonal keymap
5235   (define-prefix-command 'gnus-summary-mark-map)
5236   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5237   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5238   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5239   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5240   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5241   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5242   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5243   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5244   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5245   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5246   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5247   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5248   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5249   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5250   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5251   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5252   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5253   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5254   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5255   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5256   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5257   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5258   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5259   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5260
5261   (define-prefix-command 'gnus-summary-mscore-map)
5262   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5263   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5264   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5265   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5266   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5267
5268   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5269   
5270   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5271   
5272   (define-prefix-command 'gnus-summary-goto-map)
5273   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5274   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5275   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5276   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5277   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5278   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5279   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5280   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5281   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5282   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5283   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5284   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5285   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5286   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5287
5288
5289   (define-prefix-command 'gnus-summary-thread-map)
5290   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5291   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5292   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5293   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5294   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5295   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5296   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5297   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5298   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5299   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5300   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5301   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5302   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5303   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5304
5305   
5306   (define-prefix-command 'gnus-summary-exit-map)
5307   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5308   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5309   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5310   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5311   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5312   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5313   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5314   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5315   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5316   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5317   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5318
5319
5320   (define-prefix-command 'gnus-summary-article-map)
5321   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5322   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5323   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5324   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5325   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5326   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5327   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5328   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5329   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5330   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5331   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5332   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5333   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5334   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5335
5336
5337
5338   (define-prefix-command 'gnus-summary-wash-map)
5339   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5340   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5341   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5342   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5343   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5344   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5345   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5346   (define-key gnus-summary-wash-map "M" 'gnus-article-remove-cr)
5347   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5348   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5349   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5350   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5351   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5352
5353   (define-key gnus-summary-wash-map "L" 'gnus-summary-stop-page-breaking)
5354   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5355   (define-key gnus-summary-wash-map "G" 'gnus-summary-toggle-header)
5356   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5357
5358   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5359   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5360   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5361   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5362   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5363   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5364
5365
5366   (define-prefix-command 'gnus-summary-help-map)
5367   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5368   (define-key gnus-summary-help-map "v" 'gnus-version)
5369   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5370   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5371   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5372   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5373
5374
5375   (define-prefix-command 'gnus-summary-backend-map)
5376   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5377   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5378   (define-key gnus-summary-backend-map "\M-\C-e" 
5379     'gnus-summary-expire-articles-now)
5380   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5381   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5382   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5383   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5384   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5385   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5386   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5387
5388
5389   (define-prefix-command 'gnus-summary-save-map)
5390   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5391   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5392   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5393   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5394   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5395   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5396   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5397   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5398 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5399
5400   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5401
5402   (define-prefix-command 'gnus-summary-various-map)
5403   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5404     (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5405 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5406 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5407 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5408 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5409 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5410 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5411   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5412 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5413 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5414
5415   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5416
5417 ;  (define-prefix-command 'gnus-summary-sort-map)
5418 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5419 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5420 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5421 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5422 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5423 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5424
5425   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5426   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5427   )
5428
5429
5430 \f
5431
5432 (defun gnus-summary-mode (&optional group)
5433   "Major mode for reading articles.
5434
5435 All normal editing commands are switched off.
5436 \\<gnus-summary-mode-map>
5437 Each line in this buffer represents one article.  To read an
5438 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5439 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5440 respectively.
5441
5442 You can also post articles and send mail from this buffer.  To 
5443 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5444 of an article, type `\\[gnus-summary-reply]'.
5445
5446 There are approx. one gazillion commands you can execute in this 
5447 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5448
5449 The following commands are available:
5450
5451 \\{gnus-summary-mode-map}"
5452   (interactive)
5453   (if gnus-visual (gnus-summary-make-menu-bar))
5454   (kill-all-local-variables)
5455   (let ((locals gnus-summary-local-variables))
5456     (while locals
5457       (if (consp (car locals))
5458           (progn
5459             (make-local-variable (car (car locals)))
5460             (set (car (car locals)) (eval (cdr (car locals)))))
5461         (make-local-variable (car locals))
5462         (set (car locals) nil))
5463       (setq locals (cdr locals))))
5464   (gnus-make-thread-indent-array)
5465   (setq mode-line-modified "-- ")
5466   (make-local-variable 'mode-line-format)
5467   (setq mode-line-format (copy-sequence mode-line-format))
5468   (and (equal (nth 3 mode-line-format) "   ")
5469        (setcar (nthcdr 3 mode-line-format) ""))
5470   (setq major-mode 'gnus-summary-mode)
5471   (setq mode-name "Summary")
5472   (make-local-variable 'minor-mode-alist)
5473   (use-local-map gnus-summary-mode-map)
5474   (buffer-disable-undo (current-buffer))
5475   (setq buffer-read-only t)             ;Disable modification
5476   (setq truncate-lines t)
5477   (setq selective-display t)
5478   (setq selective-display-ellipses t)   ;Display `...'
5479   (setq buffer-display-table gnus-summary-display-table)
5480   (setq gnus-newsgroup-name group)
5481   (run-hooks 'gnus-summary-mode-hook))
5482
5483 (defun gnus-summary-make-display-table ()
5484   ;; Change the display table.  Odd characters have a tendency to mess
5485   ;; up nicely formatted displays - we make all possible glyphs
5486   ;; display only a single character.
5487
5488   ;; We start from the standard display table, if any.
5489   (setq gnus-summary-display-table 
5490         (or (copy-sequence standard-display-table)
5491             (make-display-table)))
5492   ;; Nix out all the control chars...
5493   (let ((i 32))
5494     (while (>= (setq i (1- i)) 0)
5495       (aset gnus-summary-display-table i [??])))
5496   ;; ... but not newline and cr, of course. (cr is necessary for the
5497   ;; selective display).  
5498   (aset gnus-summary-display-table ?\n nil)
5499   (aset gnus-summary-display-table ?\r nil)
5500   ;; We nix out any glyphs over 126 that are not set already.  
5501   (let ((i 256))
5502     (while (>= (setq i (1- i)) 127)
5503       ;; Only modify if the entry is nil.
5504       (or (aref gnus-summary-display-table i) 
5505           (aset gnus-summary-display-table i [??])))))
5506
5507 (defun gnus-summary-clear-local-variables ()
5508   (let ((locals gnus-summary-local-variables))
5509     (while locals
5510       (if (consp (car locals))
5511           (and (vectorp (car (car locals)))
5512                (set (car (car locals)) nil))
5513         (and (vectorp (car locals))
5514              (set (car locals) nil)))
5515       (setq locals (cdr locals)))))
5516
5517 ;; Some summary mode macros.
5518
5519 ;; Return a header specified by a NUMBER.
5520 (defun gnus-get-header-by-number (number)
5521   (save-excursion
5522     (set-buffer gnus-summary-buffer)
5523     (or gnus-newsgroup-headers-hashtb-by-number
5524         (gnus-make-headers-hashtable-by-number))
5525     (gnus-gethash (int-to-string number)
5526                   gnus-newsgroup-headers-hashtb-by-number)))
5527
5528 ;; Fast version of the function above.
5529 (defmacro gnus-get-header-by-num (number)
5530   (` (gnus-gethash (int-to-string (, number)) 
5531                    gnus-newsgroup-headers-hashtb-by-number)))
5532
5533 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5534   "Search for article forward.
5535 If UNREAD is non-nil, only unread articles are selected.
5536 If SUBJECT is non-nil, the article which has the same subject will be
5537 searched for. 
5538 If BACKWARD is non-nil, the search will be performed backwards instead."
5539   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5540
5541 (defmacro gnus-summary-search-backward (&optional unread subject)
5542   "Search for article backward.
5543 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5544 If 2nd optional argument SUBJECT is non-nil, the article which has
5545 the same subject will be searched for."
5546   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5547
5548 (defmacro gnus-summary-article-number (&optional number-or-nil)
5549   "The article number of the article on the current line.
5550 If there isn's an article number here, then we return the current
5551 article number."
5552   (if number-or-nil
5553       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5554     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5555          gnus-current-article)))
5556
5557 (defmacro gnus-summary-thread-level ()
5558   "The thread level of the article on the current line."
5559   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5560        0))
5561
5562 (defmacro gnus-summary-article-mark ()
5563   "The mark on the current line."
5564   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5565
5566 (defun gnus-summary-subject-string ()
5567   "Return current subject string or nil if nothing."
5568   (let ((article (gnus-summary-article-number))
5569         header)
5570     (and article 
5571          (setq header (gnus-get-header-by-num article))
5572          (vectorp header)
5573          (header-subject header))))
5574
5575 ;; Various summary mode internalish functions.
5576
5577 (defun gnus-mouse-pick-article (e)
5578   (interactive "e")
5579   (mouse-set-point e)
5580   (gnus-summary-next-page nil t))
5581
5582 (defun gnus-summary-setup-buffer (group)
5583   "Initialize summary buffer."
5584   (let ((buffer (concat "*Summary " group "*")))
5585     (if (get-buffer buffer)
5586         (progn
5587           (set-buffer buffer)
5588           (not gnus-newsgroup-begin))
5589       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5590       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5591       (gnus-add-current-to-buffer-list)
5592       (gnus-summary-mode group)
5593       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5594       (setq gnus-newsgroup-name group)
5595       t)))
5596
5597 (defun gnus-set-global-variables ()
5598   ;; Set the global equivalents of the summary buffer-local variables
5599   ;; to the latest values they had. These reflect the summary buffer
5600   ;; that was in action when the last article was fetched.
5601   (if (eq major-mode 'gnus-summary-mode) 
5602       (progn
5603         (setq gnus-summary-buffer (current-buffer))
5604         (let ((name gnus-newsgroup-name)
5605               (marked gnus-newsgroup-marked)
5606               (unread gnus-newsgroup-unreads)
5607               (headers gnus-current-headers)
5608               (score-file gnus-current-score-file))
5609           (save-excursion
5610             (set-buffer gnus-group-buffer)
5611             (setq gnus-newsgroup-name name)
5612             (setq gnus-newsgroup-marked marked)
5613             (setq gnus-newsgroup-unreads unread)
5614             (setq gnus-current-headers headers)
5615             (setq gnus-current-score-file score-file))))))
5616
5617 (defun gnus-summary-insert-dummy-line (sformat subject number)
5618   (if (not sformat) 
5619       (setq sformat gnus-summary-dummy-line-format-spec))
5620   (let (b)
5621     (beginning-of-line)
5622     (setq b (point))
5623     (insert (eval sformat))
5624     (add-text-properties
5625      b (1+ b)
5626      (list 'gnus-number number 
5627            'gnus-mark gnus-dummy-mark
5628            'gnus-level 0))))
5629
5630 (defvar gnus-thread-indent-array nil)
5631 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5632 (defun gnus-make-thread-indent-array ()
5633   (let ((n 200))
5634     (if (and gnus-thread-indent-array
5635              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5636         nil
5637       (setq gnus-thread-indent-array (make-vector 201 "")
5638             gnus-thread-indent-array-level gnus-thread-indent-level)
5639       (while (>= n 0)
5640         (aset gnus-thread-indent-array n
5641               (make-string (* n gnus-thread-indent-level) ? ))
5642         (setq n (1- n))))))
5643
5644 (defun gnus-summary-insert-line 
5645   (sformat header level current unread replied expirable subject-or-nil
5646            &optional dummy score)
5647   (or sformat (setq sformat gnus-summary-line-format-spec))
5648   (let* ((indentation (aref gnus-thread-indent-array level))
5649          (lines (header-lines header))
5650          (score (or score gnus-summary-default-score 0))
5651          (score-char
5652           (if (or (null gnus-summary-default-score)
5653                   (<= (abs (- score gnus-summary-default-score))
5654                       gnus-summary-zcore-fuzz)) ? 
5655             (if (< score gnus-summary-default-score)
5656                 gnus-score-below-mark gnus-score-over-mark)))
5657          (replied (if replied gnus-replied-mark ? ))
5658          (from (header-from header))
5659          (name (cond 
5660                 ((string-match "(.+)" from)
5661                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5662                 ((string-match "<[^>]+> *$" from)
5663                  (let ((beg (match-beginning 0)))
5664                    (or (and (string-match "^\"[^\"]*\"" from)
5665                             (substring from (1+ (match-beginning 0))
5666                                        (1- (match-end 0))))
5667                        (substring from 0 beg))))
5668                 (t from)))
5669          (subject (header-subject header))
5670          (number (header-number header))
5671          (opening-bracket (if dummy ?\< ?\[))
5672          (closing-bracket (if dummy ?\> ?\]))
5673          (buffer-read-only nil)
5674          (b (progn (beginning-of-line) (point))))
5675     (or (numberp lines) (setq lines 0))
5676     (insert (eval sformat))
5677     (add-text-properties
5678      b (1+ b) (list 'gnus-number number 
5679                     'gnus-mark (or unread gnus-unread-mark)
5680                     'gnus-level level))))
5681
5682 (defun gnus-summary-update-line (&optional dont-update)
5683   ;; Update summary line after change.
5684   (or (not gnus-summary-default-score)
5685       gnus-summary-inhibit-highlight
5686       (let ((gnus-summary-inhibit-highlight t)
5687             (article (gnus-summary-article-number)))
5688         (progn
5689           (or dont-update
5690               (if (and gnus-summary-mark-below
5691                        (< (gnus-summary-article-score)
5692                           gnus-summary-mark-below))
5693                   (and (not (memq article gnus-newsgroup-marked))
5694                        (not (memq article gnus-newsgroup-dormant))
5695                        (memq article gnus-newsgroup-unreads)
5696                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5697                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5698                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5699           (and gnus-visual
5700                (run-hooks 'gnus-summary-update-hook))))))
5701
5702 (defun gnus-summary-update-lines (&optional beg end)
5703   ;; Mark article as read (or not) by taking into account scores.
5704   (let ((beg (or beg (point-min)))
5705         (end (or end (point-max))))
5706     (if (or (not gnus-summary-default-score)
5707             gnus-summary-inhibit-highlight)
5708         ()
5709       (let ((gnus-summary-inhibit-highlight t)
5710             article)
5711         (save-excursion
5712           (set-buffer gnus-summary-buffer)
5713           (goto-char beg)
5714           (beginning-of-line)
5715           (while (and (not (eobp)) (< (point) end))
5716             (if (and gnus-summary-mark-below
5717                      (< (or (cdr (assq 
5718                                   (setq article (get-text-property 
5719                                                  (point) 'gnus-number))
5720                                   gnus-newsgroup-scored))
5721                             gnus-summary-default-score 0)
5722                         gnus-summary-mark-below))
5723                 ;; We want to possibly mark it as read...
5724                 (and (not (memq article gnus-newsgroup-marked))
5725                      (not (memq article gnus-newsgroup-dormant))
5726                      (memq article gnus-newsgroup-unreads)
5727                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5728               ;; We want to possibly mark it as unread.
5729               (and (eq (get-text-property (point) 'gnus-mark)
5730                        gnus-low-score-mark)
5731                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5732             ;; Do the visual highlights at the same time.
5733             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5734             (forward-line 1)))))))
5735
5736 (defvar gnus-tmp-gathered nil)
5737
5738 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5739   ;; Sum up all elements (and sub-elements) in a list.
5740   (let* ((number
5741           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5742           (if (and (consp thread) (cdr thread))
5743               (apply
5744                '+ 1 (mapcar
5745                      (function
5746                       (lambda (arg)
5747                         (if (memq (header-number (car arg)) gnus-tmp-gathered)
5748                             0
5749                           (gnus-summary-number-of-articles-in-thread arg))))
5750                      (cdr thread)))
5751             1)))
5752     (if char 
5753         (if (> number 1) gnus-not-empty-thread-mark
5754           gnus-empty-thread-mark)
5755       number)))
5756
5757 (defun gnus-summary-read-group 
5758   (group &optional show-all no-article kill-buffer)
5759   "Start reading news in newsgroup GROUP.
5760 If SHOW-ALL is non-nil, already read articles are also listed.
5761 If NO-ARTICLE is non-nil, no article is selected initially."
5762   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5763   (let* ((new-group (gnus-summary-setup-buffer group))
5764          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5765                                                   group))))
5766          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5767     (cond 
5768      ((not new-group)
5769       (gnus-set-global-variables)
5770       (gnus-kill-buffer kill-buffer)
5771       (gnus-configure-windows 'summary)
5772       (gnus-set-mode-line 'summary)
5773       (gnus-summary-position-cursor)
5774       (message "")
5775       t)
5776      ((null did-select) 
5777       (and (eq major-mode 'gnus-summary-mode)
5778            (not (equal (current-buffer) kill-buffer))
5779            (progn
5780              (kill-buffer (current-buffer))
5781              (if (not quit-config)
5782                  (progn
5783                    (set-buffer gnus-group-buffer)
5784                    (gnus-group-jump-to-group group)
5785                    (gnus-group-next-unread-group 1))
5786                (if (not (buffer-name (car quit-config)))
5787                    (gnus-configure-windows 'group 'force)
5788                  (set-buffer (car quit-config))
5789                  (and (eq major-mode 'gnus-summary-mode)
5790                       (gnus-set-global-variables))
5791                  (gnus-configure-windows (cdr quit-config))))))
5792       (message "Can't select group")
5793       nil)
5794      ((eq did-select 'quit)
5795       (and (eq major-mode 'gnus-summary-mode)
5796            (not (equal (current-buffer) kill-buffer))
5797            (kill-buffer (current-buffer)))
5798       (gnus-kill-buffer kill-buffer)
5799       (if (not quit-config)
5800           (progn
5801             (set-buffer gnus-group-buffer)
5802             (gnus-group-jump-to-group group)
5803             (gnus-group-next-unread-group 1)
5804             (gnus-configure-windows 'group 'force))
5805         (if (not (buffer-name (car quit-config)))
5806             (gnus-configure-windows 'group 'force)
5807           (set-buffer (car quit-config))
5808           (and (eq major-mode 'gnus-summary-mode)
5809                (gnus-set-global-variables))
5810           (gnus-configure-windows (cdr quit-config))))
5811       (signal 'quit nil))
5812      (t
5813       (gnus-set-global-variables)
5814       ;; Save the active value in effect when the group was entered.
5815       (setq gnus-newsgroup-active 
5816             (gnus-copy-sequence
5817              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5818       ;; You can change the subjects in this hook.
5819       (run-hooks 'gnus-select-group-hook)
5820       ;; Do score processing.
5821       (and gnus-use-scoring (gnus-possibly-score-headers))
5822       (gnus-update-format-specifications)
5823       ;; Generate the summary buffer.
5824       (gnus-summary-prepare)
5825       (if (zerop (buffer-size))
5826           (cond (gnus-newsgroup-dormant
5827                  (gnus-summary-show-all-dormant))
5828                 ((and gnus-newsgroup-scored show-all)
5829                  (gnus-summary-show-all-expunged))))
5830       ;; Function `gnus-apply-kill-file' must be called in this hook.
5831       (run-hooks 'gnus-apply-kill-hook)
5832       (if (zerop (buffer-size))
5833           (progn
5834             ;; This newsgroup is empty.
5835             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5836             (gnus-message 6 "No unread news")
5837             (gnus-kill-buffer kill-buffer)
5838             nil)
5839         ;;(save-excursion
5840         ;;  (if kill-buffer
5841         ;;      (let ((gnus-summary-buffer kill-buffer))
5842         ;;      (gnus-configure-windows 'group))))
5843         ;; Hide conversation thread subtrees.  We cannot do this in
5844         ;; gnus-summary-prepare-hook since kill processing may not
5845         ;; work with hidden articles.
5846         (and gnus-show-threads
5847              gnus-thread-hide-subtree
5848              (gnus-summary-hide-all-threads))
5849         ;; Show first unread article if requested.
5850         (goto-char (point-min))
5851         (if (and (not no-article)
5852                  gnus-auto-select-first
5853                  (gnus-summary-first-unread-article))
5854             ()
5855           (gnus-configure-windows 'summary))
5856         (gnus-set-mode-line 'summary)
5857         (gnus-summary-position-cursor)
5858         ;; If in async mode, we send some info to the backend.
5859         (and gnus-newsgroup-async
5860              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5861              (gnus-request-asynchronous 
5862               gnus-newsgroup-name
5863               (if (and gnus-asynchronous-article-function
5864                        (fboundp gnus-asynchronous-article-function))
5865                   (funcall gnus-asynchronous-article-function
5866                            gnus-newsgroup-threads)
5867                 gnus-newsgroup-threads)))
5868         (gnus-kill-buffer kill-buffer)
5869         (if (not (get-buffer-window gnus-group-buffer))
5870             ()
5871           ;; gotta use windows, because recenter does wierd stuff if
5872           ;; the current buffer ain't the displayed window.
5873           (let ((owin (selected-window))) 
5874             (select-window (get-buffer-window gnus-group-buffer))
5875             (and (gnus-group-goto-group group)
5876                  (recenter))
5877             (select-window owin))))
5878       t))))
5879
5880 (defun gnus-summary-prepare ()
5881   ;; Generate the summary buffer.
5882   (let ((buffer-read-only nil))
5883     (erase-buffer)
5884     (gnus-message 5 "Threading...")
5885     (gnus-summary-prepare-threads 
5886      (if gnus-show-threads
5887          (gnus-gather-threads 
5888           (gnus-sort-threads 
5889            (if (and gnus-summary-expunge-below
5890                     (not gnus-fetch-old-headers))
5891                (gnus-make-threads-and-expunge)
5892              (gnus-make-threads))))
5893        gnus-newsgroup-headers)
5894      'cull)
5895     (gnus-message 5 "Threading...done")
5896     (gnus-summary-update-lines)
5897     ;; Remove the final newline.
5898     ;;(goto-char (point-max))
5899     ;;(delete-char -1)
5900     ;; Call hooks for modifying summary buffer.
5901     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5902     (goto-char (point-min))
5903     (run-hooks 'gnus-summary-prepare-hook)))
5904
5905 (defun gnus-gather-threads (threads)
5906   "Gather threads that have lost their roots."
5907   (if (not gnus-summary-make-false-root)
5908       threads 
5909     (let ((hashtb (gnus-make-hashtable 1023))
5910           (prev threads)
5911           (result threads)
5912           subject hthread whole-subject)
5913       (while threads
5914         (setq whole-subject 
5915               (setq subject (header-subject (car (car threads)))))
5916         (if gnus-summary-gather-subject-limit
5917             (or (and (numberp gnus-summary-gather-subject-limit)
5918                      (> (length subject) gnus-summary-gather-subject-limit)
5919                      (setq subject
5920                            (substring subject 0 
5921                                       gnus-summary-gather-subject-limit)))
5922                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5923                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5924           (setq subject (gnus-simplify-subject-re subject)))
5925         (if (setq hthread 
5926                   (gnus-gethash subject hashtb))
5927             (progn
5928               (or (stringp (car (car hthread)))
5929                   (setcar hthread (list whole-subject (car hthread))))
5930               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5931                                            (list (car threads))))
5932               (setcdr prev (cdr threads))
5933               (setq threads prev))
5934           (gnus-sethash subject threads hashtb))
5935         (setq prev threads)
5936         (setq threads (cdr threads)))
5937       result)))
5938
5939 (defun gnus-make-threads ()
5940   ;; This function takes the dependencies already made by 
5941   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5942   ;; through the dependecies in the hash table and finds all the
5943   ;; roots. Roots do not refer back to any valid articles.
5944   (let (roots new-roots)
5945     (and gnus-fetch-old-headers
5946          (eq gnus-headers-retrieved-by 'nov)
5947          (gnus-build-old-threads))
5948     (mapatoms
5949      (lambda (refs)
5950        (if (not (car (symbol-value refs)))
5951            (setq roots (append (cdr (symbol-value refs)) roots))
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                        (setq new-roots (cons (car headers) new-roots))
5968                        (setcdr prev (cdr headers)))
5969                    (setq prev headers))
5970                  (setq headers (cdr headers)))))))
5971      gnus-newsgroup-dependencies)
5972
5973     ;; We enter the new roots into the dependencies structure to
5974     ;; ensure that any possible later thread-regeneration will be
5975     ;; possible. 
5976     (let ((r new-roots))
5977       (while r
5978         (gnus-sethash (concat (header-id (car r)) ".boo")
5979                       (list nil (car r)) gnus-newsgroup-dependencies)
5980         (setq r (cdr r))))
5981
5982     (setq roots (nconc new-roots roots))
5983     
5984     (mapcar 'gnus-trim-thread
5985             (apply 'append
5986                    (mapcar 'gnus-cut-thread
5987                            (mapcar 'gnus-make-sub-thread roots))))))
5988   
5989 (defun gnus-make-threads-and-expunge ()
5990   ;; This function takes the dependencies already made by 
5991   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5992   ;; through the dependecies in the hash table and finds all the
5993   ;; roots. Roots do not refer back to any valid articles.
5994   (let ((default (or gnus-summary-default-score 0))
5995         (below gnus-summary-expunge-below)
5996         roots article new-roots)
5997     (and gnus-fetch-old-headers
5998          (eq gnus-headers-retrieved-by 'nov)
5999          (gnus-build-old-threads))
6000     (mapatoms
6001      (lambda (refs)
6002        (if (not (car (symbol-value refs)))
6003            ;; These articles do not refer back to any other articles -
6004            ;; they are roots.
6005            (let ((headers (cdr (symbol-value refs))))
6006              ;; We weed out the low-scored articles.
6007              (while headers
6008                (if (not (< (or (cdr (assq (header-number (car headers))
6009                                           gnus-newsgroup-scored)) default)
6010                            below))
6011                    ;; It is over.
6012                    (setq roots (cons (car headers) roots))
6013                  ;; It is below, so we mark it as read.
6014                  (setq gnus-newsgroup-unreads
6015                        (delq (header-number (car headers))
6016                              gnus-newsgroup-unreads))
6017                  (setq gnus-newsgroup-reads 
6018                        (cons (cons (header-number (car headers))
6019                                    gnus-low-score-mark) 
6020                              gnus-newsgroup-reads)))
6021                (setq headers (cdr headers))))
6022          ;; Ok, these refer back to valid articles, but if
6023          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6024          ;; the root has the same subject as its children. The children
6025          ;; that do not are made into roots and removed from the list
6026          ;; of children. 
6027          (or gnus-thread-ignore-subject
6028              (let* ((prev (symbol-value refs))
6029                     (subject (gnus-simplify-subject-re 
6030                               (header-subject (car prev))))
6031                     (headers (cdr prev)))
6032                (while headers
6033                  (if (not (string= subject
6034                                    (gnus-simplify-subject-re 
6035                                     (header-subject (car headers)))))
6036                      (progn
6037                        (if (not (< (or (cdr (assq (header-number (car headers))
6038                                                   gnus-newsgroup-scored))
6039                                        default) below))
6040                            (setq new-roots (cons (car headers) new-roots))
6041                          (setq gnus-newsgroup-unreads
6042                                (delq (header-number (car headers))
6043                                      gnus-newsgroup-unreads))
6044                          (setq gnus-newsgroup-reads
6045                                (cons (cons (header-number (car headers)) 
6046                                            gnus-low-score-mark) 
6047                                      gnus-newsgroup-reads)))
6048                        (setcdr prev (cdr headers)))
6049                    (setq prev headers))
6050                  (setq headers (cdr headers)))))
6051          ;; If this article is expunged, some of the children might be
6052          ;; roots.  
6053          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6054                                gnus-newsgroup-scored)) default)
6055                 below)
6056              (let* ((prev (symbol-value refs))
6057                     (headers (cdr prev)))
6058                (while headers
6059                  (setq article (header-number (car headers)))
6060                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6061                                  default) below))
6062                      (progn (setq new-roots (cons (car headers) new-roots))
6063                             (setq prev headers))
6064                    (setq gnus-newsgroup-unreads 
6065                          (delq article gnus-newsgroup-unreads))
6066                    (setq gnus-newsgroup-reads 
6067                          (cons (cons article gnus-low-score-mark) 
6068                                gnus-newsgroup-reads))
6069                    (setcdr prev (cdr headers)))
6070                  (setq headers (cdr headers))))
6071            ;; It was not expunged, but we look at expunged children.
6072            (let* ((prev (symbol-value refs))
6073                   (headers (cdr prev))
6074                   article)
6075              (while headers
6076                (setq article (header-number (car headers)))
6077                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6078                                default) below))
6079                    (setq prev headers)
6080                  (setq gnus-newsgroup-unreads 
6081                        (delq article gnus-newsgroup-unreads))
6082                  (setq gnus-newsgroup-reads 
6083                        (cons (cons article gnus-low-score-mark)
6084                              gnus-newsgroup-reads))
6085                  (setcdr prev (cdr headers)))
6086                (setq headers (cdr headers)))))))
6087      gnus-newsgroup-dependencies)
6088
6089     ;; We enter the new roots into the dependencies structure to
6090     ;; ensure that any possible later thread-regeneration will be
6091     ;; possible. 
6092     (let ((r new-roots))
6093       (while r
6094         (gnus-sethash (concat (header-id (car r)) ".boo")
6095                       (list nil (car r)) gnus-newsgroup-dependencies)
6096         (setq r (cdr r))))
6097
6098     (setq roots (nconc new-roots roots))
6099     
6100     (mapcar 'gnus-trim-thread
6101             (apply 'append
6102                    (mapcar 'gnus-cut-thread
6103                            (mapcar 'gnus-make-sub-thread roots))))))
6104   
6105 (defun gnus-cut-thread (thread)
6106   ;; Remove leaf dormant or ancient articles from THREAD.
6107   (let ((head (car thread))
6108         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6109     (if (and (null tail)
6110              (let ((number (header-number head)))
6111                (or (memq number gnus-newsgroup-ancient)
6112                    (memq number gnus-newsgroup-dormant)
6113                    (and gnus-summary-expunge-below
6114                         (eq gnus-fetch-old-headers 'some)
6115                         (< (or (cdr (assq number gnus-newsgroup-scored))
6116                                gnus-summary-default-score 0)
6117                            gnus-summary-expunge-below)
6118                         (progn
6119                           (setq gnus-newsgroup-unreads
6120                                 (delq number gnus-newsgroup-unreads))
6121                           (setq gnus-newsgroup-reads
6122                                 (cons (cons number gnus-low-score-mark)
6123                                       gnus-newsgroup-reads))
6124                           t)))))
6125         nil
6126       (list (cons head tail)))))
6127
6128 (defun gnus-trim-thread (thread)
6129   ;; Remove root ancient articles with only one child from THREAD.
6130   (if (and (eq gnus-fetch-old-headers 'some)
6131            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6132            (= (length thread) 2))
6133       (gnus-trim-thread (nth 1 thread))
6134     thread))
6135
6136 (defun gnus-make-sub-thread (root)
6137   ;; This function makes a sub-tree for a node in the tree.
6138   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6139                                               gnus-newsgroup-dependencies)))))
6140     (cons root (mapcar 'gnus-make-sub-thread children))))
6141
6142 (defun gnus-build-old-threads ()
6143   ;; Look at all the articles that refer back to old articles, and
6144   ;; fetch the headers for the articles that aren't there. This will
6145   ;; build complete threads - if the roots haven't been expired by the
6146   ;; server, that is.
6147   (let (id heads)
6148     (mapatoms
6149      (lambda (refs)
6150        (if (not (car (symbol-value refs)))
6151            (progn
6152              (setq heads (cdr (symbol-value refs)))
6153              (while heads
6154                (if (not (memq (header-number (car heads))
6155                               gnus-newsgroup-dormant))
6156                    (progn
6157                      (setq id (symbol-name refs))
6158                      (while (and (setq id (gnus-build-get-header id))
6159                                  (not (car (gnus-gethash 
6160                                             id gnus-newsgroup-dependencies)))))
6161                      (setq heads nil))
6162                  (setq heads (cdr heads)))))))
6163      gnus-newsgroup-dependencies)))
6164
6165 (defun gnus-build-get-header (id)
6166   ;; Look through the buffer of NOV lines and find the header to
6167   ;; ID. Enter this line into the dependencies hash table, and return
6168   ;; the id of the parent article (if any).
6169   (let ((deps gnus-newsgroup-dependencies)
6170         found header)
6171     (prog1
6172         (save-excursion
6173           (set-buffer nntp-server-buffer)
6174           (goto-char (point-min))
6175           (while (and (not found) (search-forward id nil t))
6176             (beginning-of-line)
6177             (setq found (looking-at 
6178                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6179                                  (regexp-quote id))))
6180             (or found (beginning-of-line 2)))
6181           (if found
6182               (let (ref)
6183                 (beginning-of-line)
6184                 (and
6185                  (setq header (gnus-nov-parse-line 
6186                                (read (current-buffer)) deps))
6187                  (setq ref (header-references header))
6188                  (string-match "\\(<[^>]+>\\) *$" ref)
6189                  (substring ref (match-beginning 1) (match-end 1))))))
6190       (and header
6191            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6192                  gnus-newsgroup-ancient (cons (header-number header)
6193                                               gnus-newsgroup-ancient))))))
6194
6195 ;; Re-build the thread containing ID.
6196 (defun gnus-rebuild-thread (id)
6197   (let ((dep gnus-newsgroup-dependencies)
6198         (buffer-read-only nil)
6199         parent headers refs thread art)
6200     (while (and id (setq headers
6201                          (car (setq art (gnus-gethash (downcase id) dep)))))
6202       (setq parent art)
6203       (setq id (and (setq refs (header-references headers))
6204                     (string-match "\\(<[^>]+>\\) *$" refs)
6205                     (substring refs (match-beginning 1) (match-end 1)))))
6206     (setq thread (gnus-make-sub-thread (car parent)))
6207     (gnus-rebuild-remove-articles thread)
6208     (let ((beg (point)))
6209       (gnus-summary-prepare-threads (list thread))
6210       (gnus-summary-update-lines beg (point)))))
6211
6212 ;; Delete all lines in the summary buffer that correspond to articles
6213 ;; in this thread.
6214 (defun gnus-rebuild-remove-articles (thread)
6215   (and (gnus-summary-goto-subject (header-number (car thread)))
6216        (gnus-delete-line))
6217   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6218
6219 (defun gnus-sort-threads (threads)
6220   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6221   (let ((fun gnus-thread-sort-functions))
6222     (while fun
6223       (setq threads (sort threads (car fun))
6224             fun (cdr fun))))
6225   threads)
6226
6227 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6228 (defmacro gnus-thread-header (thread)
6229   ;; Return header of first article in THREAD.
6230   ;; Note that THREAD must never, evr be anything else than a variable -
6231   ;; using some other form will lead to serious barfage.
6232   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6233   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6234   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6235         (vector thread) 2))
6236
6237 (defun gnus-thread-sort-by-number (h1 h2)
6238   "Sort threads by root article number."
6239   (< (header-number (gnus-thread-header h1))
6240      (header-number (gnus-thread-header h2))))
6241
6242 (defun gnus-thread-sort-by-author (h1 h2)
6243   "Sort threads by root author."
6244   (string-lessp
6245    (let ((extract (funcall 
6246                    gnus-extract-address-components
6247                    (header-from (gnus-thread-header h1)))))
6248      (or (car extract) (cdr extract)))
6249    (let ((extract (funcall
6250                    gnus-extract-address-components 
6251                    (header-from (gnus-thread-header h2)))))
6252      (or (car extract) (cdr extract)))))
6253
6254 (defun gnus-thread-sort-by-subject (h1 h2)
6255   "Sort threads by root subject."
6256   (string-lessp
6257    (downcase (gnus-simplify-subject 
6258               (header-subject (gnus-thread-header h1))))
6259    (downcase (gnus-simplify-subject 
6260               (header-subject (gnus-thread-header h2))))))
6261
6262 (defun gnus-thread-sort-by-date (h1 h2)
6263   "Sort threads by root article date."
6264   (string-lessp
6265    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6266    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6267
6268 (defun gnus-thread-sort-by-score (h1 h2)
6269   "Sort threads by root article score.
6270 Unscored articles will be counted as having a score of zero."
6271   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6272                     gnus-newsgroup-scored))
6273          gnus-summary-default-score 0)
6274      (or (cdr (assq (header-number (gnus-thread-header h2))
6275                     gnus-newsgroup-scored))
6276          gnus-summary-default-score 0)))
6277
6278 (defun gnus-thread-sort-by-total-score (h1 h2)
6279   "Sort threads by the sum of all scores in the thread.
6280 Unscored articles will be counted as having a score of zero."
6281   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6282
6283 (defun gnus-thread-total-score (thread)
6284   ;;  This function find the total score of THREAD.
6285   (if (consp thread)
6286       (if (stringp (car thread))
6287           (apply gnus-thread-score-function 0
6288                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6289         (gnus-thread-total-score-1 thread))
6290     (gnus-thread-total-score-1 (list thread))))
6291
6292 (defun gnus-thread-total-score-1 (root)
6293   ;; This function find the total score of the thread below ROOT.
6294   (setq root (car root))
6295   (apply gnus-thread-score-function
6296          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6297              gnus-summary-default-score 0)
6298          (mapcar 'gnus-thread-total-score
6299                  (cdr (gnus-gethash (downcase (header-id root))
6300                                     gnus-newsgroup-dependencies)))))
6301
6302 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6303 (defvar gnus-tmp-prev-subject "")
6304
6305 (defun gnus-summary-prepare-threads (threads &optional cull)
6306   "Prepare summary buffer from THREADS and indentation LEVEL.  
6307 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6308 or a straight list of headers."
6309   (let ((level 0)
6310         thread header number subject stack state gnus-tmp-gathered)
6311     (if (vectorp (car threads))
6312         ;; If this is a straight (sic) list of headers, then a
6313         ;; threaded summary display isn't required, so we just create
6314         ;; an unthreaded one.
6315         (gnus-summary-prepare-unthreaded threads cull)
6316
6317       ;; Do the threaded display.
6318
6319       (while (or threads stack)
6320         
6321         (if threads
6322             ;; If there are some threads, we do them before the
6323             ;; threads on the stack.
6324             (setq thread threads
6325                   header (car (car thread)))
6326           ;; There were no current threads, so we pop something off
6327           ;; the stack. 
6328           (setq state (car stack)
6329                 level (car state)
6330                 thread (cdr state)
6331                 stack (cdr stack)
6332                 header (car (car thread))))
6333
6334         (if (stringp header)
6335             (progn
6336               ;; The header is a dummy root.
6337               (cond 
6338                ((eq gnus-summary-make-false-root 'adopt)
6339                 ;; We let the first article adopt the rest.
6340                 (let ((th (car (cdr (car thread)))))
6341                   (while (cdr th)
6342                     (setq th (cdr th)))
6343                   (setcdr th (cdr (cdr (car thread))))
6344                   (setq gnus-tmp-gathered 
6345                         (nconc (mapcar
6346                                 (lambda (h) (header-number (car h)))
6347                                 (cdr (cdr (car thread))))
6348                                gnus-tmp-gathered))
6349                   (setcdr (cdr (car thread)) nil))
6350                 (setq level -1))
6351                ((eq gnus-summary-make-false-root 'empty)
6352                 ;; We print adopted articles with empty subject fields.
6353                 (setq gnus-tmp-gathered 
6354                       (nconc (mapcar
6355                               (lambda (h) (header-number (car h)))
6356                               (cdr (cdr (car thread))))
6357                              gnus-tmp-gathered))
6358                 (setq level -1))
6359                ((eq gnus-summary-make-false-root 'dummy)
6360                 ;; We output a dummy root.
6361                 (gnus-summary-insert-dummy-line 
6362                  nil header (header-number (car (car (cdr (car thread)))))))
6363                (t
6364                 ;; We do not make a root for the gathered
6365                 ;; sub-threads at all.  
6366                 (setq level -1))))
6367       
6368           (setq number (header-number header)
6369                 subject (header-subject header))
6370
6371           ;; Do the async thing.
6372           (and gnus-newsgroup-async
6373                (setq gnus-newsgroup-threads
6374                      (cons (cons number (header-lines header)) 
6375                            gnus-newsgroup-threads)))
6376
6377           ;; We may have to root out some bad articles...
6378           (and cull
6379                (= level 0)
6380                (cond ((and (memq (setq number (header-number header))
6381                                  gnus-newsgroup-dormant)
6382                            (null thread))
6383                       (setq header nil))
6384                      ((and gnus-summary-expunge-below
6385                            (< (or (cdr (assq number gnus-newsgroup-scored))
6386                                   gnus-summary-default-score 0)
6387                               gnus-summary-expunge-below))
6388                       (setq header nil)
6389                       (setq gnus-newsgroup-unreads 
6390                             (delq number gnus-newsgroup-unreads))
6391                       (setq gnus-newsgroup-reads
6392                             (cons (cons number gnus-low-score-mark)
6393                                   gnus-newsgroup-reads)))))
6394           
6395           (and
6396            header
6397            (progn
6398              (inline
6399                (gnus-summary-insert-line
6400                 nil header level nil 
6401                 (cond 
6402                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6403                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6404                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6405                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6406                  (t (or (cdr (assq number gnus-newsgroup-reads))
6407                         gnus-ancient-mark)))
6408                 (memq number gnus-newsgroup-replied)
6409                 (memq number gnus-newsgroup-expirable)
6410                 (if (and (eq gnus-summary-make-false-root 'empty)
6411                          (memq number gnus-tmp-gathered))
6412                     gnus-summary-same-subject
6413                   (if (or (zerop level)
6414                           (and gnus-thread-ignore-subject
6415                                (not (string= 
6416                                      (gnus-simplify-subject-re
6417                                       gnus-tmp-prev-subject)
6418                                      (gnus-simplify-subject-re
6419                                       subject)))))
6420                       subject
6421                     gnus-summary-same-subject))
6422                 (and (eq gnus-summary-make-false-root 'adopt)
6423                      (memq number gnus-tmp-gathered))
6424                 (cdr (assq number gnus-newsgroup-scored)))
6425
6426                (setq gnus-tmp-prev-subject subject)))))
6427
6428         (if (nth 1 thread) 
6429             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6430         (setq level (1+ level))
6431         (setq threads (cdr (car thread)))))))
6432
6433
6434 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6435   (let (header number)
6436
6437     ;; Do the async thing, if that is required.
6438     (if gnus-newsgroup-async
6439         (setq gnus-newsgroup-threads
6440               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6441                       headers)))
6442
6443     (while headers
6444       (setq header (car headers)
6445             headers (cdr headers)
6446             number (header-number header))
6447
6448       ;; We may have to root out some bad articles...
6449       (cond 
6450        ((and cull
6451              (memq (setq number (header-number header))
6452                    gnus-newsgroup-dormant)))
6453        ((and cull gnus-summary-expunge-below
6454              (< (or (cdr (assq number gnus-newsgroup-scored))
6455                     gnus-summary-default-score 0)
6456                 gnus-summary-expunge-below))
6457         (setq gnus-newsgroup-unreads 
6458               (delq number gnus-newsgroup-unreads))
6459         (setq gnus-newsgroup-reads
6460               (cons (cons number gnus-low-score-mark)
6461                     gnus-newsgroup-reads)))
6462        (t
6463         (gnus-summary-insert-line
6464          nil header 0 nil 
6465          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6466                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6467                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6468                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6469                (t (or (cdr (assq number gnus-newsgroup-reads))
6470                       gnus-ancient-mark)))
6471          (memq number gnus-newsgroup-replied)
6472          (memq number gnus-newsgroup-expirable)
6473          (header-subject header) nil
6474          (cdr (assq number gnus-newsgroup-scored))))))))
6475
6476 (defun gnus-select-newsgroup (group &optional read-all)
6477   "Select newsgroup GROUP.
6478 If READ-ALL is non-nil, all articles in the group are selected."
6479   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6480          (info (nth 2 entry))
6481          articles)
6482     (gnus-check-news-server
6483      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6484
6485     (or (gnus-server-opened gnus-current-select-method)
6486         (gnus-open-server gnus-current-select-method)
6487         (error "Couldn't open server"))
6488     
6489     (or (and (null entry)
6490              (gnus-activate-newsgroup group))
6491         (and (eq (car entry) t)
6492              (gnus-activate-newsgroup (car info)))
6493         (gnus-request-group group t)
6494         (progn
6495           (kill-buffer (current-buffer))
6496           (error "Couldn't request group %s: %s" 
6497                  group (gnus-status-message group))))
6498
6499     (setq gnus-newsgroup-name group)
6500     (setq gnus-newsgroup-unselected nil)
6501     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6502
6503     (and gnus-asynchronous
6504          (gnus-check-backend-function 
6505           'request-asynchronous gnus-newsgroup-name)
6506          (setq gnus-newsgroup-async
6507                (gnus-request-asynchronous gnus-newsgroup-name)))
6508
6509     (setq articles (gnus-articles-to-read group read-all))
6510
6511     (cond 
6512      ((null articles) 
6513       (gnus-message 3 "Couldn't select newsgroup")
6514       'quit)
6515      ((eq articles 0) nil)
6516      (t
6517       ;; Init the dependencies hash table.
6518       (setq gnus-newsgroup-dependencies 
6519             (gnus-make-hashtable (length articles)))
6520       ;; Retrieve the headers and read them in.
6521       (gnus-message 5 "Fetching headers...")
6522       (setq gnus-newsgroup-headers 
6523             (if (eq 'nov (setq gnus-headers-retrieved-by
6524                                ;; This is a naughty hack. To get the
6525                                ;; retrieval of old headers to work, we
6526                                ;; set `nntp-nov-gap' to nil (locally),
6527                                ;; and then just retrieve the headers.
6528                                ;; Mucho magic.
6529                                (if gnus-fetch-old-headers
6530                                    (let (nntp-nov-gap)
6531                                      (gnus-retrieve-headers 
6532                                       (if (not (eq 1 (car articles)))
6533                                           (cons 1 articles)
6534                                         articles)
6535                                       gnus-newsgroup-name))
6536                                  (gnus-retrieve-headers 
6537                                   articles gnus-newsgroup-name))))
6538                 (progn
6539                   (gnus-get-newsgroup-headers-xover articles))
6540               ;; If we were to fetch old headers, but the backend didn't
6541               ;; support XOVER, then it is possible we fetched one article
6542               ;; that we shouldn't have. If that's the case, we remove it.
6543               (if (not gnus-fetch-old-headers)
6544                   ()
6545                 (save-excursion
6546                   (set-buffer nntp-server-buffer)
6547                   (goto-char (point-min))
6548                   (and 
6549                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6550                    (delete-region       ; So we delete this head.
6551                     (point) 
6552                     (search-forward "\n.\n" nil t)))))
6553               (gnus-get-newsgroup-headers)))
6554       (gnus-message 5 "Fetching headers...done")      
6555       ;; Remove canceled articles from the list of unread articles.
6556       (setq gnus-newsgroup-unreads
6557             (gnus-set-sorted-intersection 
6558              gnus-newsgroup-unreads
6559              (mapcar (lambda (headers) (header-number headers))
6560                      gnus-newsgroup-headers)))
6561       ;; Adjust and set lists of article marks.
6562       (and info
6563            (let (marked)
6564              (gnus-adjust-marked-articles info)
6565              (setq gnus-newsgroup-marked 
6566                    (copy-sequence
6567                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6568              (setq gnus-newsgroup-replied 
6569                    (copy-sequence (cdr (assq 'reply marked))))
6570              (setq gnus-newsgroup-expirable
6571                    (copy-sequence (cdr (assq 'expire marked))))
6572              (setq gnus-newsgroup-killed
6573                    (copy-sequence (cdr (assq 'killed marked))))
6574              (setq gnus-newsgroup-bookmarks 
6575                    (copy-sequence (cdr (assq 'bookmark marked))))
6576              (setq gnus-newsgroup-dormant 
6577                    (copy-sequence (cdr (assq 'dormant marked))))
6578              (setq gnus-newsgroup-scored 
6579                    (copy-sequence (cdr (assq 'score marked))))
6580              (setq gnus-newsgroup-processable nil)))
6581       ;; Create the header hashtb.
6582       (or gnus-newsgroup-headers-hashtb-by-number
6583           (gnus-make-headers-hashtable-by-number))
6584       ;; Check whether auto-expire is to be done in this group.
6585       (setq gnus-newsgroup-auto-expire
6586             (or (and (stringp gnus-auto-expirable-newsgroups)
6587                      (string-match gnus-auto-expirable-newsgroups group))
6588                 (memq 'auto-expire (nth 5 info))))
6589       ;; First and last article in this newsgroup.
6590       (and gnus-newsgroup-headers
6591            (setq gnus-newsgroup-begin 
6592                  (header-number (car gnus-newsgroup-headers)))
6593            (setq gnus-newsgroup-end
6594                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6595       (setq gnus-reffed-article-number -1)
6596       ;; GROUP is successfully selected.
6597       (or gnus-newsgroup-headers t)))))
6598
6599 (defun gnus-articles-to-read (group read-all)
6600   ;; Find out what articles the user wants to read.
6601   (let* ((articles
6602           ;; Select all articles if `read-all' is non-nil, or if all the
6603           ;; unread articles are dormant articles.
6604           (if (or (and read-all (not (numberp read-all)))
6605                   (= (length gnus-newsgroup-unreads) 
6606                      (length gnus-newsgroup-dormant)))
6607               (gnus-uncompress-range 
6608                (gnus-gethash group gnus-active-hashtb))
6609             gnus-newsgroup-unreads))
6610          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6611          (scored (length scored-list))
6612          (number (length articles))
6613          (marked (+ (length gnus-newsgroup-marked)
6614                     (length gnus-newsgroup-dormant)))
6615          (select
6616           (cond 
6617            ((numberp read-all)
6618             read-all)
6619            (t
6620             (condition-case ()
6621                 (cond ((and (or (<= scored marked)
6622                                 (= scored number))
6623                             (numberp gnus-large-newsgroup)
6624                             (> number gnus-large-newsgroup))
6625                        (let ((input
6626                               (read-string
6627                                (format
6628                                 "How many articles from %s (default %d): "
6629                                 gnus-newsgroup-name number))))
6630                          (if (string-match "^[ \t]*$" input)
6631                              number input)))
6632                       ((and (> scored marked) (< scored number))
6633                        (let ((input
6634                               (read-string
6635                                (format 
6636                                 "%s %s (%d scored, %d total): "
6637                                 "How many articles from"
6638                                 group scored number))))
6639                          (if (string-match "^[ \t]*$" input)
6640                              number input)))
6641                       (t number))
6642               (quit nil))))))
6643     (setq select (if (stringp select) (string-to-number select) select))
6644     (if (or (null select) (zerop select))
6645         select
6646       (if (and (not (zerop scored)) (<= (abs select) scored))
6647           (progn
6648             (setq articles (sort scored-list '<))
6649             (setq number (length articles)))
6650         (setq articles (copy-sequence articles)))
6651
6652       (if (< (abs select) number)
6653           (if (< select 0) 
6654               ;; Select the N oldest articles.
6655               (setcdr (nthcdr (1- (abs select)) articles) nil)
6656             ;; Select the N most recent articles.
6657             (setq articles (nthcdr (- number select) articles))))
6658       (setq gnus-newsgroup-unselected
6659             (gnus-sorted-intersection
6660              gnus-newsgroup-unreads
6661              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6662       articles)))
6663
6664 (defun gnus-killed-articles (killed articles)
6665   (let (out)
6666     (while articles
6667       (if (inline (gnus-member-of-range (car articles) killed))
6668           (setq out (cons (car articles) out)))
6669       (setq articles (cdr articles)))
6670     out))
6671
6672 (defun gnus-adjust-marked-articles (info &optional active)
6673   "Remove all marked articles that are no longer legal."
6674   (let ((marked-lists (nth 3 info))
6675         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6676         m prev)
6677     ;; There are many types of marked articles.
6678     (while marked-lists
6679       (setq m (cdr (setq prev (car marked-lists))))
6680       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6681              ;; Make sure that all ticked articles are a subset of the
6682              ;; unread/unselected articles.
6683              (while m
6684                (if (or (memq (car m) gnus-newsgroup-unreads)
6685                        (memq (car m) gnus-newsgroup-unselected))
6686                    (setq prev m)
6687                  (setcdr prev (cdr m)))
6688                (setq m (cdr m))))
6689             ((eq 'score (car prev))
6690              ;; Scored articles should be a subset of
6691              ;; unread/unselected articles. 
6692              (while m
6693                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6694                        (memq (car (car m)) gnus-newsgroup-unreads))
6695                    (setq prev m)
6696                  (setcdr prev (cdr m)))
6697                (setq m (cdr m))))
6698             ((eq 'bookmark (car prev))
6699              ;; Bookmarks should be a subset of active articles.
6700              (while m
6701                (if (< (car (car m)) (car active))
6702                    (setcdr prev (cdr m))
6703                  (setq prev m))
6704                (setq m (cdr m))))
6705             ((eq 'killed (car prev))
6706              ;; Articles that have been through the kill process are
6707              ;; to be a subset of active articles.
6708              (while (and m (< (or (and (numberp (car m)) (car m))
6709                                   (cdr (car m)))
6710                               (car active)))
6711                (setcdr prev (cdr m))
6712                (setq m (cdr m)))
6713              (if (and m (< (or (and (numberp (car m)) (car m))
6714                                (car (car m)))
6715                            (car active))) 
6716                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6717             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6718              ;; The replied and expirable articles have to be articles
6719              ;; that are active. 
6720              (while m
6721                (if (< (car m) (car active))
6722                    (setcdr prev (cdr m))
6723                  (setq prev m))
6724                (setq m (cdr m)))))
6725       (setq marked-lists (cdr marked-lists)))
6726     ;; Remove all lists that are empty.
6727     (setq marked-lists (nth 3 info))
6728     (if marked-lists
6729         (progn
6730           (while (= 1 (length (car marked-lists)))
6731             (setq marked-lists (cdr marked-lists)))
6732           (setq m (cdr (setq prev marked-lists)))
6733           (while m
6734             (if (= 1 (length (car m)))
6735                 (setcdr prev (cdr m))
6736               (setq prev m))
6737             (setq m (cdr m)))
6738           (setcar (nthcdr 3 info) marked-lists)))
6739     ;; Finally, if there are no marked lists at all left, and if there
6740     ;; are no elements after the lists in the info list, we just chop
6741     ;; the info list off before the marked lists.
6742     (and (null marked-lists) 
6743          (not (nthcdr 4 info))
6744          (setcdr (nthcdr 2 info) nil)))
6745   info)
6746
6747 (defun gnus-set-marked-articles 
6748   (info ticked replied expirable killed dormant bookmark score) 
6749   "Enter the various lists of marked articles into the newsgroup info list."
6750   (let (newmarked)
6751     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6752     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6753     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6754                                          newmarked)))
6755     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6756     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6757     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6758                                         newmarked)))
6759     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6760     (if (nthcdr 3 info)
6761         (progn
6762           (setcar (nthcdr 3 info) newmarked)
6763           (and (not newmarked)
6764                (not (nthcdr 4 info))
6765                (setcdr (nthcdr 2 info) nil)))
6766       (if newmarked
6767           (setcdr (nthcdr 2 info) (list newmarked))))))
6768
6769 (defun gnus-add-marked-articles (group type articles &optional info force)
6770   ;; Add ARTICLES of TYPE to the info of GROUP.
6771   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6772   ;; add, but replace marked articles of TYPE with ARTICLES.
6773   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6774         marked m)
6775     (or (not info)
6776         (and (not (setq marked (nthcdr 3 info)))
6777              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6778         (and (not (setq m (assq type (car marked))))
6779              (setcar marked (cons (cons type articles) (car marked))))
6780         (if force
6781             (setcdr m articles)
6782           (nconc m articles)))))
6783          
6784 (defun gnus-set-mode-line (where)
6785   "This function sets the mode line of the article or summary buffers.
6786 If WHERE is `summary', the summary mode line format will be used."
6787   (if (memq where gnus-updated-mode-lines)
6788       (let (mode-string)
6789         (save-excursion
6790           (set-buffer gnus-summary-buffer)
6791           (let* ((mformat (if (eq where 'article) 
6792                               gnus-article-mode-line-format-spec
6793                             gnus-summary-mode-line-format-spec))
6794                  (group-name gnus-newsgroup-name)
6795                  (article-number (or gnus-current-article 0))
6796                  (unread (- (length gnus-newsgroup-unreads)
6797                             (length gnus-newsgroup-dormant)))
6798                  (unread-and-unticked 
6799                   (- unread (length gnus-newsgroup-marked)))
6800                  (unselected (length gnus-newsgroup-unselected))
6801                  (unread-and-unselected
6802                   (cond ((and (zerop unread-and-unticked)
6803                               (zerop unselected)) "")
6804                         ((zerop unselected) 
6805                          (format "{%d more}" unread-and-unticked))
6806                         (t (format "{%d(+%d) more}"
6807                                    unread-and-unticked unselected))))
6808                  (subject
6809                   (if gnus-current-headers
6810                       (header-subject gnus-current-headers) ""))
6811                  (max-len (and gnus-mode-non-string-length
6812                                (- (frame-width) gnus-mode-non-string-length)))
6813                  header) ;; passed as argument to any user-format-funcs
6814             (setq mode-string (eval mformat))
6815             (or (numberp max-len)
6816                 (setq max-len (length mode-string)))
6817             (if (< max-len 4) (setq max-len 4))
6818             (if (> (length mode-string) max-len)
6819                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6820                 ;;  function `substring' might cut on a middle
6821                 ;;  of multi-octet character.
6822                 (setq mode-string 
6823                       (concat (gnus-truncate-string mode-string (- max-len 3))
6824                               "...")))
6825             (setq mode-string (format (format "%%-%ds" max-len)
6826                                       mode-string))))
6827         (setq mode-line-buffer-identification mode-string)
6828         (set-buffer-modified-p t))))
6829
6830 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6831   "Go through the HEADERS list and add all Xrefs to a hash table.
6832 The resulting hash table is returned, or nil if no Xrefs were found."
6833   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6834          (prefix (if (and 
6835                       (gnus-group-foreign-p from-newsgroup)
6836                       (not (memq 'virtual 
6837                                  (assoc (symbol-name (car from-method))
6838                                         gnus-valid-select-methods))))
6839                      (gnus-group-real-prefix from-newsgroup)))
6840          (xref-hashtb (make-vector 63 0))
6841          start group entry number xrefs header)
6842     (while headers
6843       (setq header (car headers))
6844       (if (and (setq xrefs (header-xref header))
6845                (not (memq (header-number header) unreads)))
6846           (progn
6847             (setq start 0)
6848             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6849               (setq start (match-end 0))
6850               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6851                                                     (match-end 1))))
6852               (setq number 
6853                     (string-to-int (substring xrefs (match-beginning 2) 
6854                                               (match-end 2))))
6855               (if (setq entry (gnus-gethash group xref-hashtb))
6856                   (setcdr entry (cons number (cdr entry)))
6857                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6858       (setq headers (cdr headers)))
6859     (if start xref-hashtb nil)))
6860
6861 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6862   "Look through all the headers and mark the Xrefs as read."
6863   (let ((virtual (memq 'virtual 
6864                        (assoc (symbol-name (car (gnus-find-method-for-group 
6865                                                  from-newsgroup)))
6866                               gnus-valid-select-methods)))
6867         name entry info xref-hashtb idlist method
6868         nth4)
6869     (save-excursion
6870       (set-buffer gnus-group-buffer)
6871       (if (setq xref-hashtb 
6872                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6873           (mapatoms 
6874            (lambda (group)
6875              (if (string= from-newsgroup (setq name (symbol-name group)))
6876                  ()
6877                (setq idlist (symbol-value group))
6878                ;; Dead groups are not updated.
6879                (if (and (prog1 
6880                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6881                                   info (nth 2 entry))
6882                           (if (stringp (setq nth4 (nth 4 info)))
6883                               (setq nth4 (gnus-server-to-method nth4))))
6884                         ;; Only do the xrefs if the group has the same
6885                         ;; select method as the group we have just read.
6886                         (or (gnus-methods-equal-p 
6887                              nth4 (gnus-find-method-for-group from-newsgroup))
6888                             virtual
6889                             (equal nth4 
6890                                    (setq method (gnus-find-method-for-group 
6891                                                  from-newsgroup)))
6892                             (and (equal (car nth4) (car method))
6893                                  (equal (nth 1 nth4) (nth 1 method))))
6894                         gnus-use-cross-reference
6895                         (or (not (eq gnus-use-cross-reference t))
6896                             virtual
6897                             ;; Only do cross-references on subscribed
6898                             ;; groups, if that is what is wanted.  
6899                             (<= (nth 1 info) gnus-level-subscribed)))
6900                    (gnus-group-make-articles-read name idlist expirable))))
6901            xref-hashtb)))))
6902
6903 (defun gnus-group-make-articles-read (group articles expirable)
6904   (let* ((num 0)
6905          (entry (gnus-gethash group gnus-newsrc-hashtb))
6906          (info (nth 2 entry))
6907          (active (gnus-gethash group gnus-active-hashtb))
6908          exps expirable range)
6909     ;; First peel off all illegal article numbers.
6910     (if active
6911         (let ((ids articles)
6912               (ticked (cdr (assq 'tick (nth 3 info))))
6913               (dormant (cdr (assq 'dormant (nth 3 info))))
6914               id)
6915           (setq exps nil)
6916           (while ids
6917             (setq id (car ids))
6918             (if (or (> id (cdr active))
6919                     (< id (car active))
6920                     (memq id ticked)
6921                     (memq id dormant))
6922                 (setq articles (delq id articles)))
6923             (and (memq id expirable)
6924                  (setq exps (cons id exps)))
6925             (setq ids (cdr ids)))))
6926     ;; Update expirable articles.
6927     (gnus-add-marked-articles nil 'expirable exps info)
6928     (and active
6929          (null (nth 2 info))
6930          (> (car active) 1)
6931          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6932     (setcar (nthcdr 2 info)
6933             (setq range
6934                   (gnus-add-to-range 
6935                    (nth 2 info) 
6936                    (setq articles (sort articles '<)))))
6937     ;; Then we have to re-compute how many unread
6938     ;; articles there are in this group.
6939     (if active
6940         (progn
6941           (cond 
6942            ((not range)
6943             (setq num (- (1+ (cdr active)) (car active))))
6944            ((not (listp (cdr range)))
6945             (setq num (- (cdr active) (- (1+ (cdr range)) 
6946                                          (car range)))))
6947            (t
6948             (while range
6949               (if (numberp (car range))
6950                   (setq num (1+ num))
6951                 (setq num (+ num (- (1+ (cdr (car range)))
6952                                     (car (car range))))))
6953               (setq range (cdr range)))
6954             (setq num (- (cdr active) num))))
6955           ;; Update the number of unread articles.
6956           (setcar 
6957            entry 
6958            (max 0 (- num 
6959                      (length (cdr (assq 'tick (nth 3 info))))
6960                      (length 
6961                       (cdr (assq 'dormant (nth 3 info)))))))
6962           ;; Update the group buffer.
6963           (gnus-group-update-group group t)))))
6964
6965 (defun gnus-methods-equal-p (m1 m2)
6966   (let ((m1 (or m1 gnus-select-method))
6967         (m2 (or m2 gnus-select-method)))
6968     (or (equal m1 m2)
6969         (and (eq (car m1) (car m2))
6970              (or (not (memq 'address (assoc (symbol-name (car m1))
6971                                             gnus-valid-select-methods)))
6972                  (equal (nth 1 m1) (nth 1 m2)))))))
6973
6974 (defsubst gnus-header-value ()
6975   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6976
6977 (defvar gnus-newsgroup-none-id 0)
6978
6979 (defun gnus-get-newsgroup-headers ()
6980   (setq gnus-article-internal-prepare-hook nil)
6981   (let ((cur nntp-server-buffer)
6982         (dependencies gnus-newsgroup-dependencies)
6983         headers id dep end ref)
6984     (save-excursion
6985       (set-buffer nntp-server-buffer)
6986       (goto-char (point-min))
6987       ;; Search to the beginning of the next header. Error messages
6988       ;; do not begin with 2 or 3.
6989       (while (re-search-forward "^[23][0-9]+ " nil t)
6990         (let ((header (make-vector 9 nil))
6991               (case-fold-search t)
6992               (p (point))
6993               in-reply-to)
6994           (setq id nil
6995                 ref nil)
6996           (header-set-number header (read cur))
6997           ;; This implementation of this function, with nine
6998           ;; search-forwards instead of the one re-search-forward and
6999           ;; a case (which basically was the old function) is actually
7000           ;; about twice as fast, even though it looks messier. You
7001           ;; can't have everything, I guess. Speed and elegance
7002           ;; doesn't always come hand in hand.
7003           (save-restriction
7004             (narrow-to-region (point) (or (save-excursion 
7005                                             (search-forward "\n.\n" nil t))
7006                                           (point)))
7007             (if (search-forward "\nfrom: " nil t)
7008                 (header-set-from header (gnus-header-value))
7009               (header-set-from header "(nobody)"))
7010             (goto-char p)
7011             (if (search-forward "\nsubject: " nil t)
7012                 (header-set-subject header (gnus-header-value))
7013               (header-set-subject header "(none)"))
7014             (goto-char p)
7015             (and (search-forward "\nxref: " nil t)
7016                  (header-set-xref header (gnus-header-value)))
7017             (goto-char p)
7018             (or (numberp (and (search-forward "\nlines: " nil t)
7019                               (header-set-lines header (read cur))))
7020                 (header-set-lines header 0))
7021             (goto-char p)
7022             (and (search-forward "\ndate: " nil t)
7023                  (header-set-date header (gnus-header-value)))
7024             (goto-char p)
7025             (if (search-forward "\nmessage-id: " nil t)
7026                 (header-set-id header (setq id (gnus-header-value)))
7027               ;; If there was no message-id, we just fake one to make
7028               ;; subsequent routines simpler.
7029               (header-set-id 
7030                header 
7031                (setq id (concat "none+" 
7032                                 (int-to-string 
7033                                  (setq gnus-newsgroup-none-id 
7034                                        (1+ gnus-newsgroup-none-id)))))))
7035             (goto-char p)
7036             (if (search-forward "\nreferences: " nil t)
7037                 (progn
7038                   (header-set-references header (gnus-header-value))
7039                   (setq end (match-end 0))
7040                   (save-excursion
7041                     (setq ref 
7042                           (downcase
7043                            (buffer-substring
7044                             (progn 
7045                               (end-of-line)
7046                               (search-backward ">" end t)
7047                               (1+ (point)))
7048                             (progn
7049                               (search-backward "<" end t)
7050                               (point)))))))
7051               ;; Get the references from the in-reply-to header if there
7052               ;; ware no references and the in-reply-to header looks
7053               ;; promising. 
7054               (if (and (search-forward "\nin-reply-to: " nil t)
7055                        (setq in-reply-to (gnus-header-value))
7056                        (string-match "<[^>]+>" in-reply-to))
7057                   (progn
7058                     (header-set-references 
7059                      header 
7060                      (setq ref (substring in-reply-to (match-beginning 0)
7061                                           (match-end 0))))
7062                     (setq ref (downcase ref)))
7063                 (setq ref "none")))
7064             ;; We do some threading while we read the headers. The
7065             ;; message-id and the last reference are both entered into
7066             ;; the same hash table. Some tippy-toeing around has to be
7067             ;; done in case an article has arrived before the article
7068             ;; which it refers to.
7069             (if (boundp (setq dep (intern (downcase id) dependencies)))
7070                 (if (car (symbol-value dep))
7071                     ;; An article with this Message-ID has already
7072                     ;; been seen, so we ignore this one, except we add
7073                     ;; any additional Xrefs (in case the two articles
7074                     ;; came from different servers.
7075                     (progn
7076                       (header-set-xref 
7077                        (car (symbol-value dep))
7078                        (concat (or (header-xref (car (symbol-value dep))) "")
7079                                (or (header-xref header) "")))
7080                       (setq header nil))
7081                   (setcar (symbol-value dep) header))
7082               (set dep (list header)))
7083             (if header
7084                 (progn
7085                   (if (boundp (setq dep (intern ref dependencies)))
7086                       (setcdr (symbol-value dep) 
7087                               (cons header (cdr (symbol-value dep))))
7088                     (set dep (list nil header)))
7089                   (setq headers (cons header headers))))
7090             (goto-char (point-max))))))
7091     (nreverse headers)))
7092
7093 ;; The following macros and functions were written by Felix Lee
7094 ;; <flee@cse.psu.edu>. 
7095
7096 (defmacro gnus-nov-read-integer ()
7097   '(prog1
7098        (if (= (following-char) ?\t)
7099            0
7100          (let ((num (condition-case nil (read buffer) (error nil))))
7101            (if (numberp num) num 0)))
7102      (or (eobp) (forward-char 1))))
7103
7104 (defmacro gnus-nov-skip-field ()
7105   '(search-forward "\t" eol 'move))
7106
7107 (defmacro gnus-nov-field ()
7108   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7109
7110 ;; Goes through the xover lines and returns a list of vectors
7111 (defun gnus-get-newsgroup-headers-xover (sequence)
7112   "Parse the news overview data in the server buffer, and return a
7113 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7114   ;; Get the Xref when the users reads the articles since most/some
7115   ;; NNTP servers do not include Xrefs when using XOVER.
7116   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7117   (let ((cur nntp-server-buffer)
7118         (dependencies gnus-newsgroup-dependencies)
7119         number headers header)
7120     (save-excursion
7121       (set-buffer nntp-server-buffer)
7122       (goto-char (point-min))
7123       (while (and sequence (not (eobp)))
7124         (setq number (read cur))
7125         (while (and sequence (< (car sequence) number))
7126           (setq sequence (cdr sequence)))
7127         (and sequence 
7128              (eq number (car sequence))
7129              (progn
7130                (setq sequence (cdr sequence))
7131                (if (setq header 
7132                          (inline (gnus-nov-parse-line number dependencies)))
7133                    (setq headers (cons header headers)))))
7134         (forward-line 1))
7135       (setq headers (nreverse headers)))
7136     headers))
7137
7138 ;; This function has to be called with point after the article number
7139 ;; on the beginning of the line.
7140 (defun gnus-nov-parse-line (number dependencies)
7141   (let ((none 0)
7142         (eol (gnus-point-at-eol)) 
7143         (buffer (current-buffer))
7144         header ref id dep)
7145
7146     ;; overview: [num subject from date id refs chars lines misc]
7147     (narrow-to-region (point) eol)
7148     (forward-char)
7149
7150     (condition-case nil
7151         (setq header
7152               (vector 
7153                number                   ; number
7154                (gnus-nov-field)         ; subject
7155                (gnus-nov-field)         ; from
7156                (gnus-nov-field)         ; date
7157                (setq id (or (gnus-nov-field)
7158                             (concat "none+"
7159                                     (int-to-string 
7160                                      (setq none (1+ none)))))) ; id
7161                (progn
7162                  (save-excursion
7163                    (let ((beg (point)))
7164                      (search-forward "\t" eol)
7165                      (if (search-backward ">" beg t)
7166                          (setq ref 
7167                                (downcase 
7168                                 (buffer-substring 
7169                                  (1+ (point))
7170                                  (progn
7171                                    (search-backward "<" beg t)
7172                                    (point)))))
7173                        (setq ref nil))))
7174                  (gnus-nov-field))      ; refs
7175                (gnus-nov-read-integer)  ; chars
7176                (gnus-nov-read-integer)  ; lines
7177                (if (= (following-char) ?\n)
7178                    nil
7179                  (gnus-nov-field))      ; misc
7180                ))
7181       (error (progn 
7182                (ding)
7183                (message "Strange nov line.")
7184                (setq header nil)
7185                (goto-char eol))))
7186
7187     (widen)
7188
7189     ;; We build the thread tree.
7190     (and header
7191          (if (boundp (setq dep (intern (downcase id) dependencies)))
7192              (if (car (symbol-value dep))
7193                  ;; An article with this Message-ID has already been seen,
7194                  ;; so we ignore this one, except we add any additional
7195                  ;; Xrefs (in case the two articles came from different
7196                  ;; servers.
7197                  (progn
7198                    (header-set-xref 
7199                     (car (symbol-value dep))
7200                     (concat (or (header-xref (car (symbol-value dep))) "")
7201                             (or (header-xref header) "")))
7202                    (setq header nil))
7203                (setcar (symbol-value dep) header))
7204            (set dep (list header))))
7205     (if header
7206         (progn
7207           (if (boundp (setq dep (intern (or ref "none") 
7208                                         dependencies)))
7209               (setcdr (symbol-value dep) 
7210                       (cons header (cdr (symbol-value dep))))
7211             (set dep (list nil header)))))
7212     header))
7213
7214 (defun gnus-article-get-xrefs ()
7215   "Fill in the Xref value in `gnus-current-headers', if necessary.
7216 This is meant to be called in `gnus-article-internal-prepare-hook'."
7217   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7218                                  gnus-current-headers)))
7219     (or (not gnus-use-cross-reference)
7220         (not headers)
7221         (and (header-xref headers)
7222              (not (string= (header-xref headers) "")))
7223         (let ((case-fold-search t)
7224               xref)
7225           (save-restriction
7226             (gnus-narrow-to-headers)
7227             (goto-char (point-min))
7228             (if (or (and (eq (downcase (following-char)) ?x)
7229                          (looking-at "Xref:"))
7230                     (search-forward "\nXref:" nil t))
7231                 (progn
7232                   (goto-char (1+ (match-end 0)))
7233                   (setq xref (buffer-substring (point) 
7234                                                (progn (end-of-line) (point))))
7235                   (header-set-xref headers xref))))))))
7236
7237 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7238 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7239
7240 (defun gnus-make-headers-hashtable-by-number ()
7241   "Make hashtable for the variable gnus-newsgroup-headers by number."
7242   (save-excursion
7243     (set-buffer gnus-summary-buffer)
7244     (let ((headers gnus-newsgroup-headers)
7245           header)
7246       (setq gnus-newsgroup-headers-hashtb-by-number
7247             (gnus-make-hashtable (length headers)))
7248       (while headers
7249         (setq header (car headers))
7250         (gnus-sethash (int-to-string (header-number header))
7251                       header gnus-newsgroup-headers-hashtb-by-number)
7252         (setq headers (cdr headers))))))
7253
7254 (defun gnus-more-header-backward ()
7255   "Find new header backward."
7256   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7257         (artnum gnus-newsgroup-begin)
7258         (header nil))
7259     (while (and (not header)
7260                 (> artnum first))
7261       (setq artnum (1- artnum))
7262       (setq header (gnus-read-header artnum)))
7263     header))
7264
7265 (defun gnus-more-header-forward (&optional backward)
7266   "Find new header forward.
7267 If BACKWARD, find new header backward instead."
7268   (if backward
7269       (gnus-more-header-backward)
7270     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7271           (artnum gnus-newsgroup-end)
7272           (header nil))
7273       (while (and (not header)
7274                   (< artnum last))
7275         (setq artnum (1+ artnum))
7276         (setq header (gnus-read-header artnum)))
7277       header)))
7278
7279 (defun gnus-extend-newsgroup (header &optional backward)
7280   "Extend newsgroup selection with HEADER.
7281 Optional argument BACKWARD means extend toward backward."
7282   (if header
7283       (let ((artnum (header-number header)))
7284         (setq gnus-newsgroup-headers
7285               (if backward
7286                   (cons header gnus-newsgroup-headers)
7287                 (nconc gnus-newsgroup-headers (list header))))
7288         (setq gnus-newsgroup-unselected
7289               (delq artnum gnus-newsgroup-unselected))
7290         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7291         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7292
7293 (defun gnus-summary-work-articles (n)
7294   "Return a list of articles to be worked upon. The prefix argument,
7295 the list of process marked articles, and the current article will be
7296 taken into consideration."
7297   (let (articles)
7298     (if (and n (numberp n))
7299         (let ((backward (< n 0))
7300               (n (abs n)))
7301           (save-excursion
7302             (while (and (> n 0)
7303                         (setq articles (cons (gnus-summary-article-number) 
7304                                              articles))
7305                         (gnus-summary-search-forward nil nil backward))
7306               (setq n (1- n))))
7307           (sort articles (function <)))
7308       (or (reverse gnus-newsgroup-processable)
7309           (list (gnus-summary-article-number))))))
7310
7311 (defun gnus-summary-search-group (&optional backward use-level)
7312   "Search for next unread newsgroup.
7313 If optional argument BACKWARD is non-nil, search backward instead."
7314   (save-excursion
7315     (set-buffer gnus-group-buffer)
7316     (if (gnus-group-search-forward 
7317          backward nil (if use-level (gnus-group-group-level) nil))
7318         (gnus-group-group-name))))
7319
7320 (defun gnus-summary-best-group (&optional exclude-group)
7321   "Find the name of the best unread group.
7322 If EXCLUDE-GROUP, do not go to this group."
7323   (save-excursion
7324     (set-buffer gnus-group-buffer)
7325     (save-excursion
7326       (gnus-group-best-unread-group exclude-group))))
7327
7328 (defun gnus-subject-equal (s1 s2)
7329   (cond
7330    ((null gnus-summary-gather-subject-limit)
7331     (equal (gnus-simplify-subject-re s1)
7332            (gnus-simplify-subject-re s2)))
7333    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7334     (equal (gnus-simplify-subject-fuzzy s1)
7335            (gnus-simplify-subject-fuzzy s2)))
7336    ((numberp gnus-summary-gather-subject-limit)
7337     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7338            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7339    (t
7340     (equal s1 s2))))
7341     
7342 (defun gnus-summary-search-subject (&optional backward unread subject)
7343   "Search for article forward.
7344 If BACKWARD is non-nil, search backward.
7345 If UNREAD is non-nil, only unread articles are selected.
7346 If SUBJECT is non-nil, the article which has the same subject will be
7347 searched for." 
7348   (let ((func (if backward 'previous-single-property-change
7349                 'next-single-property-change))
7350         (beg (point))
7351         (did t)
7352         pos psubject)
7353     (beginning-of-line)
7354     (and gnus-summary-check-current unread
7355          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7356          (setq did nil))
7357     (if (not did)
7358         ()
7359       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7360       (while
7361           (and 
7362            (setq pos (funcall func (point) 'gnus-number))
7363            (goto-char (if backward (1- pos) pos))
7364            (setq did
7365                  (not (and
7366                        (or (not unread)
7367                            (eq (get-text-property (point) 'gnus-mark)
7368                                gnus-unread-mark))
7369                        (or (not subject)
7370                            (and (setq psubject (inline (gnus-summary-subject-string)))
7371                                 (inline (gnus-subject-equal subject psubject)))))))
7372            (if backward (if (bobp) nil (forward-char -1) t)
7373              (if (eobp) nil (forward-char 1) t)))))
7374     (if did
7375         (progn (goto-char beg) nil)
7376       (prog1
7377           (get-text-property (point) 'gnus-number)
7378         (gnus-summary-position-cursor)))))
7379
7380 (defun gnus-summary-pseudo-article ()
7381   "The thread level of the article on the current line."
7382   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7383
7384 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7385 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7386 (defun gnus-summary-article-score ()
7387   "Return current article score."
7388   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7389       gnus-summary-default-score 0))
7390
7391 (defun gnus-summary-recenter ()
7392   "Center point in the summary window.
7393 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7394 displayed, no centering will be performed." 
7395   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7396   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7397   (let* ((top (cond ((< (window-height) 4) 0)
7398                     ((< (window-height) 7) 1)
7399                     (t 2)))
7400          (height (1- (window-height)))
7401          (bottom (save-excursion (goto-char (point-max))
7402                                  (forward-line (- height))
7403                                  (point)))
7404          (window (get-buffer-window (current-buffer))))
7405     (and 
7406      ;; The user has to want it,
7407      gnus-auto-center-summary 
7408      ;; the article buffer must be displayed,
7409      (get-buffer-window gnus-article-buffer)
7410      ;; Set the window start to either `bottom', which is the biggest
7411      ;; possible valid number, or the second line from the top,
7412      ;; whichever is the least.
7413      (set-window-start
7414       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7415
7416 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7417 (defun gnus-short-group-name (group &optional levels)
7418   "Collapse GROUP name LEVELS."
7419   (let* ((name "") (foreign "") (depth -1) (skip 1)
7420          (levels (or levels
7421                      (progn
7422                        (while (string-match "\\." group skip)
7423                          (setq skip (match-end 0)
7424                                depth (+ depth 1)))
7425                        depth))))
7426     (if (string-match ":" group)
7427         (setq foreign (substring group 0 (match-end 0))
7428               group (substring group (match-end 0))))
7429     (while group
7430       (if (and (string-match "\\." group) (> levels 0))
7431           (setq name (concat name (substring group 0 1))
7432                 group (substring group (match-end 0))
7433                 levels (- levels 1)
7434                 name (concat name "."))
7435         (setq name (concat foreign name group)
7436               group nil)))
7437     name))
7438
7439 (defun gnus-summary-jump-to-group (newsgroup)
7440   "Move point to NEWSGROUP in group mode buffer."
7441   ;; Keep update point of group mode buffer if visible.
7442   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7443       (save-window-excursion
7444         ;; Take care of tree window mode.
7445         (if (get-buffer-window gnus-group-buffer)
7446             (pop-to-buffer gnus-group-buffer))
7447         (gnus-group-jump-to-group newsgroup))
7448     (save-excursion
7449       ;; Take care of tree window mode.
7450       (if (get-buffer-window gnus-group-buffer)
7451           (pop-to-buffer gnus-group-buffer)
7452         (set-buffer gnus-group-buffer))
7453       (gnus-group-jump-to-group newsgroup))))
7454
7455 ;; This function returns a list of article numbers based on the
7456 ;; difference between the ranges of read articles in this group and
7457 ;; the range of active articles.
7458 (defun gnus-list-of-unread-articles (group)
7459   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7460          (active (gnus-gethash group gnus-active-hashtb))
7461          (last (cdr active))
7462          first nlast unread)
7463     ;; If none are read, then all are unread. 
7464     (if (not read)
7465         (setq first (car active))
7466       ;; If the range of read articles is a single range, then the
7467       ;; first unread article is the article after the last read
7468       ;; article. Sounds logical, doesn't it?
7469       (if (not (listp (cdr read)))
7470           (setq first (1+ (cdr read)))
7471         ;; `read' is a list of ranges.
7472         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7473                                 (car (car read)))) 1)
7474             (setq first 1))
7475         (while read
7476           (if first 
7477               (while (< first nlast)
7478                 (setq unread (cons first unread))
7479                 (setq first (1+ first))))
7480           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7481           (setq nlast (if (atom (car (cdr read))) 
7482                           (car (cdr read))
7483                         (car (car (cdr read)))))
7484           (setq read (cdr read)))))
7485     ;; And add the last unread articles.
7486     (while (<= first last)
7487       (setq unread (cons first unread))
7488       (setq first (1+ first)))
7489     ;; Return the list of unread articles.
7490     (nreverse unread)))
7491
7492 (defun gnus-list-of-read-articles (group)
7493   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7494         (active (gnus-gethash group gnus-active-hashtb)))
7495     (and info active
7496          (gnus-sorted-complement 
7497           (gnus-uncompress-range active) 
7498           (gnus-list-of-unread-articles group)))))
7499
7500 ;; Various summary commands
7501
7502 (defun gnus-summary-universal-argument ()
7503   "Perform any operation on all articles marked with the process mark."
7504   (interactive)
7505   (gnus-set-global-variables)
7506   (let ((articles (reverse gnus-newsgroup-processable))
7507         func)
7508     (or articles (error "No articles marked"))
7509     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7510         (error "Undefined key"))
7511     (while articles
7512       (gnus-summary-goto-subject (car articles))
7513       (command-execute func)
7514       (gnus-summary-remove-process-mark (car articles))
7515       (setq articles (cdr articles)))))
7516
7517 (defun gnus-summary-toggle-truncation (&optional arg)
7518   "Toggle truncation of summary lines.
7519 With arg, turn line truncation on iff arg is positive."
7520   (interactive "P")
7521   (setq truncate-lines
7522         (if (null arg) (not truncate-lines)
7523           (> (prefix-numeric-value arg) 0)))
7524   (redraw-display))
7525
7526 (defun gnus-summary-reselect-current-group (&optional all)
7527   "Once exit and then reselect the current newsgroup.
7528 The prefix argument ALL means to select all articles."
7529   (interactive "P")
7530   (gnus-set-global-variables)
7531   (let ((current-subject (gnus-summary-article-number))
7532         (group gnus-newsgroup-name))
7533     (setq gnus-newsgroup-begin nil)
7534     (gnus-summary-exit t)
7535     ;; We have to adjust the point of group mode buffer because the
7536     ;; current point was moved to the next unread newsgroup by
7537     ;; exiting.
7538     (gnus-summary-jump-to-group group)
7539     (gnus-group-read-group all t)
7540     (gnus-summary-goto-subject current-subject)))
7541
7542 (defun gnus-summary-rescan-group (&optional all)
7543   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7544   (interactive "P")
7545   (gnus-set-global-variables)
7546   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7547   (let ((group gnus-newsgroup-name))
7548     (gnus-summary-exit)
7549     (gnus-summary-jump-to-group group)
7550     (save-excursion
7551       (set-buffer gnus-group-buffer)
7552       (gnus-group-get-new-news-this-group 1))
7553     (gnus-summary-jump-to-group group)
7554     (gnus-group-read-group all)))
7555
7556 (defun gnus-summary-update-info ()
7557   (let* ((group gnus-newsgroup-name))
7558     (if gnus-newsgroup-kill-headers
7559         (setq gnus-newsgroup-killed
7560               (gnus-compress-sequence
7561                (nconc
7562                 (gnus-set-sorted-intersection
7563                  (gnus-uncompress-range gnus-newsgroup-killed)
7564                  (setq gnus-newsgroup-unselected
7565                        (sort gnus-newsgroup-unselected '<)))
7566                 (setq gnus-newsgroup-unreads
7567                       (sort gnus-newsgroup-unreads '<))) t)))
7568     (or (listp (cdr gnus-newsgroup-killed))
7569         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7570     (let ((headers gnus-newsgroup-headers))
7571       (gnus-close-group group)
7572       (run-hooks 'gnus-exit-group-hook)
7573       (gnus-update-read-articles 
7574        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7575        gnus-newsgroup-marked
7576        t gnus-newsgroup-replied gnus-newsgroup-expirable
7577        gnus-newsgroup-killed gnus-newsgroup-dormant
7578        gnus-newsgroup-bookmarks 
7579        (and gnus-save-score gnus-newsgroup-scored))
7580       (and gnus-use-cross-reference
7581            (gnus-mark-xrefs-as-read 
7582             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7583       ;; Do adaptive scoring, and possibly save score files.
7584       (and gnus-newsgroup-adaptive
7585            (gnus-score-adaptive))
7586       (and gnus-use-scoring 
7587            (fboundp 'gnus-score-save)
7588            (funcall 'gnus-score-save))
7589       ;; Do not switch windows but change the buffer to work.
7590       (set-buffer gnus-group-buffer)
7591       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7592           (gnus-group-update-group group)))))
7593   
7594 (defun gnus-summary-exit (&optional temporary)
7595   "Exit reading current newsgroup, and then return to group selection mode.
7596 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7597   (interactive)
7598   (gnus-set-global-variables)
7599   (gnus-kill-save-kill-buffer)
7600   (let* ((group gnus-newsgroup-name)
7601          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7602                                                   gnus-newsgroup-name))))
7603          (mode major-mode)
7604          (buf (current-buffer)))
7605     (run-hooks 'gnus-summary-prepare-exit-hook)
7606     (gnus-summary-update-info) ; Make all changes in this group permanent.
7607     (set-buffer buf)
7608     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7609     ;; Make sure where I was, and go to next newsgroup.
7610     (set-buffer gnus-group-buffer)
7611     (or quit-config
7612         (progn
7613           (gnus-group-jump-to-group group)
7614           (gnus-group-next-unread-group 1)))
7615     (if temporary
7616         nil                             ;Nothing to do.
7617       ;; We set all buffer-local variables to nil. It is unclear why
7618       ;; this is needed, but if we don't, buffer-local variables are
7619       ;; not garbage-collected, it seems. This would the lead to en
7620       ;; ever-growing Emacs.
7621       (set-buffer buf)
7622       (gnus-summary-clear-local-variables)
7623       ;; We clear the global counterparts of the buffer-local
7624       ;; variables as well, just to be on the safe side.
7625       (gnus-configure-windows 'group 'force)
7626       (gnus-summary-clear-local-variables)
7627       ;; Return to group mode buffer. 
7628       (if (eq mode 'gnus-summary-mode)
7629           (gnus-kill-buffer buf))
7630       (if (get-buffer gnus-article-buffer)
7631           (bury-buffer gnus-article-buffer))
7632       (setq gnus-current-select-method gnus-select-method)
7633       (pop-to-buffer gnus-group-buffer)
7634       (if (not quit-config)
7635           (progn
7636             (gnus-group-jump-to-group group)
7637             (gnus-group-next-unread-group 1))
7638         (if (not (buffer-name (car quit-config)))
7639             (gnus-configure-windows 'group 'force)
7640           (set-buffer (car quit-config))
7641           (and (eq major-mode 'gnus-summary-mode)
7642                (gnus-set-global-variables))
7643           (gnus-configure-windows (cdr quit-config))))
7644       (run-hooks 'gnus-summary-exit-hook))))
7645
7646 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7647 (defun gnus-summary-exit-no-update (&optional no-questions)
7648   "Quit reading current newsgroup without updating read article info."
7649   (interactive)
7650   (gnus-set-global-variables)
7651   (let* ((group gnus-newsgroup-name)
7652          (quit-config (nth 1 (assoc 'quit-config 
7653                                     (gnus-find-method-for-group group)))))
7654     (if (or no-questions
7655             gnus-expert-user
7656             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7657         (progn
7658           (gnus-close-group group)
7659           (gnus-summary-clear-local-variables)
7660           (set-buffer gnus-group-buffer)
7661           (gnus-summary-clear-local-variables)
7662           ;; Return to group selection mode.
7663           (gnus-configure-windows 'group 'force)
7664           (if (get-buffer gnus-summary-buffer)
7665               (kill-buffer gnus-summary-buffer))
7666           (if (get-buffer gnus-article-buffer)
7667               (bury-buffer gnus-article-buffer))
7668           (if (equal (gnus-group-group-name) group)
7669               (gnus-group-next-unread-group 1))
7670           (if quit-config
7671               (progn
7672                 (if (not (buffer-name (car quit-config)))
7673                     (gnus-configure-windows 'group 'force)
7674                   (set-buffer (car quit-config))
7675                   (and (eq major-mode 'gnus-summary-mode)
7676                        (gnus-set-global-variables))
7677                   (gnus-configure-windows (cdr quit-config)))))))))
7678
7679 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7680 (defun gnus-summary-fetch-faq (group)
7681   "Fetch the FAQ for the current group."
7682   (interactive (list gnus-newsgroup-name))
7683   (let ((gnus-faq-buffer 
7684          (find-file (concat gnus-group-faq-directory 
7685                             (gnus-group-real-name group)))))
7686   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7687
7688 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7689 (defun gnus-summary-describe-group (&optional force)
7690   "Describe the current newsgroup."
7691   (interactive "P")
7692   (gnus-group-describe-group force gnus-newsgroup-name))
7693
7694 (defun gnus-summary-describe-briefly ()
7695   "Describe summary mode commands briefly."
7696   (interactive)
7697   (gnus-message 6
7698     (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")))
7699
7700 ;; Walking around group mode buffer from summary mode.
7701
7702 (defun gnus-summary-next-group (&optional no-article target-group backward)
7703   "Exit current newsgroup and then select next unread newsgroup.
7704 If prefix argument NO-ARTICLE is non-nil, no article is selected
7705 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7706 previous group instead."
7707   (interactive "P")
7708   (gnus-set-global-variables)
7709   (let ((current-group gnus-newsgroup-name)
7710         (current-buffer (current-buffer))
7711         entered)
7712     ;; First we semi-exit this group to update Xrefs and all variables.
7713     ;; We can't do a real exit, because the window conf must remain
7714     ;; the same in case the user is prompted for info, and we don't
7715     ;; want the window conf to change before that...
7716     (gnus-summary-exit t)
7717     (while (not entered)
7718       ;; Then we find what group we are supposed to enter.
7719       (set-buffer gnus-group-buffer)
7720       (gnus-group-jump-to-group current-group)
7721       (setq target-group 
7722             (or target-group        
7723                 (if (eq gnus-keep-same-level 'best) 
7724                     (gnus-summary-best-group gnus-newsgroup-name)
7725                   (gnus-summary-search-group backward gnus-keep-same-level))))
7726       (if (not target-group)
7727           ;; There are no further groups, so we return to the group
7728           ;; buffer.
7729           (progn
7730             (gnus-message 5 "Returning to the group buffer")
7731             (setq entered t)
7732             (set-buffer current-buffer)
7733             (gnus-summary-exit))
7734         ;; We try to enter the target group.
7735         (gnus-group-jump-to-group target-group)
7736         (let ((unreads (gnus-group-group-unread)))
7737           (if (and (or (eq t unreads)
7738                        (and unreads (not (zerop unreads))))
7739                    (gnus-summary-read-group
7740                     target-group nil no-article current-buffer))
7741               (setq entered t)
7742             (setq current-group target-group
7743                   target-group nil)))))))
7744
7745 (defun gnus-summary-next-group-old (&optional no-article group backward)
7746   "Exit current newsgroup and then select next unread newsgroup.
7747 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7748 If BACKWARD, go to previous group instead."
7749   (interactive "P")
7750   (gnus-set-global-variables)
7751   (let ((ingroup gnus-newsgroup-name)
7752         (sumbuf (current-buffer))
7753         num)
7754     (set-buffer gnus-group-buffer)
7755     (if (and group
7756              (or (and (numberp (setq num (car (gnus-gethash
7757                                                group gnus-newsrc-hashtb))))
7758                       (< num 1))
7759                  (null num)))
7760         (progn
7761           (gnus-group-jump-to-group group)
7762           (setq group nil))
7763       (gnus-group-jump-to-group ingroup))
7764     (gnus-summary-search-group backward)
7765     (let ((group (or group (gnus-summary-search-group backward))))
7766       (set-buffer sumbuf)
7767       (gnus-summary-exit t)             ;Update all information.
7768       (if (null group)
7769           (gnus-summary-exit-no-update t)
7770         (gnus-group-jump-to-group ingroup)
7771         (setq group (gnus-summary-search-group backward))
7772         (gnus-message 5 "Selecting %s..." group)
7773         (set-buffer gnus-group-buffer)
7774         ;; We are now in group mode buffer.
7775         ;; Make sure group mode buffer point is on GROUP.
7776         (gnus-group-jump-to-group group)
7777         (if (not (eq gnus-auto-select-next 'quietly))
7778             (progn
7779               (gnus-summary-read-group group nil no-article sumbuf)
7780               (and (string= gnus-newsgroup-name ingroup)
7781                    (bufferp sumbuf) (buffer-name sumbuf)
7782                    (progn
7783                      (set-buffer (setq gnus-summary-buffer sumbuf))
7784                      (gnus-summary-exit-no-update t))))
7785           (let ((prevgroup group))
7786             (gnus-group-jump-to-group ingroup)
7787             (setq group (gnus-summary-search-group backward))
7788             (gnus-summary-read-group group nil no-article sumbuf)
7789             (while (and (string= gnus-newsgroup-name ingroup)
7790                         (bufferp sumbuf) 
7791                         (buffer-name sumbuf)
7792                         (not (string= prevgroup (gnus-group-group-name))))
7793               (set-buffer gnus-group-buffer)
7794               (gnus-summary-read-group 
7795                (setq prevgroup (gnus-group-group-name)) 
7796                nil no-article sumbuf))
7797             (and (string= prevgroup (gnus-group-group-name))
7798                  ;; We have reached the final group in the group
7799                  ;; buffer.
7800                  (progn
7801                    (if (buffer-name sumbuf)
7802                        (progn
7803                          (set-buffer sumbuf)
7804                          (gnus-summary-exit)))))))))))
7805
7806 (defun gnus-summary-prev-group (&optional no-article)
7807   "Exit current newsgroup and then select previous unread newsgroup.
7808 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7809   (interactive "P")
7810   (gnus-summary-next-group no-article nil t))
7811
7812 ;; Walking around summary lines.
7813
7814 (defun gnus-summary-first-subject (&optional unread)
7815   "Go to the first unread subject.
7816 If UNREAD is non-nil, go to the first unread article.
7817 Returns nil if there are no unread articles."
7818   (interactive "P")
7819   (prog1
7820       (cond ((not unread)
7821              (goto-char (point-min)))
7822             ((gnus-goto-char 
7823               (text-property-any 
7824                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7825              t)
7826             (t 
7827              ;; There are no unread articles.
7828              (gnus-message 3 "No more unread articles")
7829              nil))
7830     (gnus-summary-position-cursor)))
7831
7832 (defun gnus-summary-next-subject (n &optional unread dont-display)
7833   "Go to next N'th summary line.
7834 If N is negative, go to the previous N'th subject line.
7835 If UNREAD is non-nil, only unread articles are selected.
7836 The difference between N and the actual number of steps taken is
7837 returned."
7838   (interactive "p")
7839   (let ((backward (< n 0))
7840         (n (abs n)))
7841     (while (and (> n 0)
7842                 (gnus-summary-search-forward unread nil backward))
7843       (setq n (1- n)))
7844     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7845                                (if unread " unread" "")))
7846     (or dont-display
7847         (progn
7848           (gnus-summary-recenter)
7849           (gnus-summary-position-cursor)))
7850   n))
7851
7852 (defun gnus-summary-next-unread-subject (n)
7853   "Go to next N'th unread summary line."
7854   (interactive "p")
7855   (gnus-summary-next-subject n t))
7856
7857 (defun gnus-summary-prev-subject (n &optional unread)
7858   "Go to previous N'th summary line.
7859 If optional argument UNREAD is non-nil, only unread article is selected."
7860   (interactive "p")
7861   (gnus-summary-next-subject (- n) unread))
7862
7863 (defun gnus-summary-prev-unread-subject (n)
7864   "Go to previous N'th unread summary line."
7865   (interactive "p")
7866   (gnus-summary-next-subject (- n) t))
7867
7868 (defun gnus-summary-goto-subject (article)
7869   "Go the subject line of ARTICLE."
7870   (interactive
7871    (list
7872     (string-to-int
7873      (completing-read "Article number: "
7874                       (mapcar
7875                        (lambda (headers)
7876                          (list
7877                           (int-to-string (header-number headers))))
7878                        gnus-newsgroup-headers)
7879                       nil 'require-match))))
7880   (or article (error "No article number"))
7881   (let ((b (point)))
7882     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7883                                                 'gnus-number article)))
7884         ()
7885       (gnus-summary-show-thread)
7886       ;; Skip dummy articles. 
7887       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7888           (forward-line 1))
7889       (prog1
7890           (if (not (eobp))
7891               article
7892             (goto-char b)
7893             nil)
7894         (gnus-summary-position-cursor)))))
7895
7896 ;; Walking around summary lines with displaying articles.
7897
7898 (defun gnus-summary-expand-window (&optional arg)
7899   "Make the summary buffer take up the entire Emacs frame.
7900 Given a prefix, will force an `article' buffer configuration."
7901   (interactive "P")
7902   (gnus-set-global-variables)
7903   (if arg
7904       (gnus-configure-windows 'article 'force)
7905     (gnus-configure-windows 'summary 'force)))
7906
7907 (defun gnus-summary-display-article (article &optional all-header)
7908   "Display ARTICLE in article buffer."
7909   (gnus-set-global-variables)
7910   (if (null article)
7911       nil
7912     (prog1
7913         (gnus-article-prepare article all-header)
7914       (gnus-summary-show-thread)
7915       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7916           (progn
7917             (forward-line 1)
7918             (gnus-summary-position-cursor)))
7919       (run-hooks 'gnus-select-article-hook)
7920       (gnus-summary-recenter)
7921       (gnus-summary-goto-subject article)
7922       ;; Successfully display article.
7923       (gnus-summary-update-line)
7924       (gnus-article-set-window-start 
7925        (cdr (assq article gnus-newsgroup-bookmarks)))
7926       t)))
7927
7928 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7929   "Select the current article.
7930 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7931 non-nil, the article will be re-fetched even if it already present in
7932 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7933 be displayed."
7934   (and (not pseudo) (gnus-summary-pseudo-article)
7935        (error "This is a pseudo-article."))
7936   (let ((article (or article (gnus-summary-article-number)))
7937         (all-headers (not (not all-headers))) ;Must be T or NIL.
7938         did) 
7939     (prog1
7940         (save-excursion
7941           (set-buffer gnus-summary-buffer)
7942           (if (or (null gnus-current-article)
7943                   (null gnus-article-current)
7944                   (null (get-buffer gnus-article-buffer))
7945                   (not (eq article (cdr gnus-article-current)))
7946                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7947                   force)
7948               ;; The requested article is different from the current article.
7949               (progn
7950                 (gnus-summary-display-article article all-headers)
7951                 (setq did article))
7952             (if (or all-headers gnus-show-all-headers) 
7953                 (gnus-article-show-all-headers))
7954             nil))
7955       (if did 
7956           (gnus-article-set-window-start 
7957            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7958
7959 (defun gnus-summary-set-current-mark (&optional current-mark)
7960   "Obsolete function."
7961   nil)
7962
7963 (defun gnus-summary-next-article (&optional unread subject backward)
7964   "Select the next article.
7965 If UNREAD, only unread articles are selected.
7966 If SUBJECT, only articles with SUBJECT are selected.
7967 If BACKWARD, the previous article is selected instead of the next."
7968   (interactive "P")
7969   (gnus-set-global-variables)
7970   (let (header)
7971     (cond
7972      ;; Is there such an article?
7973      ((or (gnus-summary-display-article 
7974            (gnus-summary-search-forward unread subject backward))
7975           (eq (gnus-summary-article-mark) gnus-canceled-mark))
7976       (gnus-summary-position-cursor))
7977      ;; If not, we try the first unread, if that is wanted.
7978      ((and subject
7979            gnus-auto-select-same
7980            (gnus-summary-first-unread-article))
7981       (gnus-message 6 "Wrapped"))
7982      ;; Try to get next/previous article not displayed in this group.
7983      ((and gnus-auto-extend-newsgroup
7984            (not unread) (not subject)
7985            (setq header (gnus-more-header-forward backward)))
7986       (gnus-extend-newsgroup header backward)
7987       (let ((buffer-read-only nil))
7988         (goto-char (if backward (point-min) (point-max)))
7989         (gnus-summary-prepare-threads (list header)))
7990       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7991                                    gnus-newsgroup-end)))
7992      ;; Go to next/previous group.
7993      (t
7994       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7995           (gnus-summary-jump-to-group gnus-newsgroup-name))
7996       (let ((cmd (aref (this-command-keys) 0))
7997             (group 
7998              (if (eq gnus-keep-same-level 'best) 
7999                  (gnus-summary-best-group gnus-newsgroup-name)
8000                (gnus-summary-search-group backward gnus-keep-same-level))))
8001         ;; For some reason, the group window gets selected. We change
8002         ;; it back.  
8003         (select-window (get-buffer-window (current-buffer)))
8004         ;; Keep just the event type of CMD.
8005         (and (listp cmd) (setq cmd (car cmd)))
8006         ;; Select next unread newsgroup automagically.
8007         (cond 
8008          ((not gnus-auto-select-next)
8009           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8010          ((eq gnus-auto-select-next 'quietly)
8011           ;; Select quietly.
8012           (if (assoc 'quit-config (gnus-find-method-for-group 
8013                                    gnus-newsgroup-name))
8014               (gnus-summary-exit)
8015             (gnus-message 7 "No more%s articles (%s)..."
8016                           (if unread " unread" "") 
8017                           (if group (concat "selecting " group)
8018                             "exiting"))
8019             (gnus-summary-next-group nil group backward)))
8020          (t
8021           (let ((keystrokes '(?\C-n ?\C-p))
8022                 key)
8023             (while (or (null key) (memq key keystrokes))
8024               (gnus-message 
8025                7 "No more%s articles%s" (if unread " unread" "")
8026                (if (and group (not (assoc 'quit-config
8027                                           (gnus-find-method-for-group 
8028                                            gnus-newsgroup-name))))
8029                    (format " (Type %s for %s [%s])"
8030                            (single-key-description cmd) group
8031                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8032                  (format " (Type %s to exit %s)"
8033                          (single-key-description cmd)
8034                          gnus-newsgroup-name)))
8035               ;; Confirm auto selection.
8036               (let* ((event (read-event)))
8037                 (setq key (if (listp event) (car event) event))
8038                 (if (memq key keystrokes)
8039                     (let ((obuf (current-buffer)))
8040                       (switch-to-buffer gnus-group-buffer)
8041                       (and group
8042                            (gnus-group-jump-to-group group))
8043                       (condition-case ()
8044                           (execute-kbd-macro (char-to-string key))
8045                         (error (ding) nil))
8046                       (setq group (gnus-group-group-name))
8047                       (switch-to-buffer obuf)))))
8048             (if (equal key cmd)
8049                 (if (or (not group) (assoc 'quit-config
8050                                            (gnus-find-method-for-group
8051                                             gnus-newsgroup-name)))
8052                     (gnus-summary-exit)
8053                   (gnus-summary-next-group nil group backward))
8054               (setq unread-command-events (list key)))))))))))
8055
8056 (defun gnus-summary-next-unread-article ()
8057   "Select unread article after current one."
8058   (interactive)
8059   (gnus-summary-next-article t (and gnus-auto-select-same
8060                                     (gnus-summary-subject-string))))
8061
8062 (defun gnus-summary-prev-article (&optional unread subject)
8063   "Select the article after the current one.
8064 If UNREAD is non-nil, only unread articles are selected."
8065   (interactive "P")
8066   (gnus-summary-next-article unread subject t))
8067
8068 (defun gnus-summary-prev-unread-article ()
8069   "Select unred article before current one."
8070   (interactive)
8071   (gnus-summary-prev-article t (and gnus-auto-select-same
8072                                     (gnus-summary-subject-string))))
8073
8074 (defun gnus-summary-next-page (&optional lines circular)
8075   "Show next page of selected article.
8076 If end of article, select next article.
8077 Argument LINES specifies lines to be scrolled up.
8078 If CIRCULAR is non-nil, go to the start of the article instead of 
8079 instead of selecting the next article when reaching the end of the
8080 current article." 
8081   (interactive "P")
8082   (setq gnus-summary-buffer (current-buffer))
8083   (gnus-set-global-variables)
8084   (let ((article (gnus-summary-article-number))
8085         (endp nil))
8086     (gnus-configure-windows 'article)
8087     (if (or (null gnus-current-article)
8088             (null gnus-article-current)
8089             (/= article (cdr gnus-article-current))
8090             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8091         ;; Selected subject is different from current article's.
8092         (gnus-summary-display-article article)
8093       (gnus-eval-in-buffer-window
8094        gnus-article-buffer
8095        (setq endp (gnus-article-next-page lines)))
8096       (if endp
8097           (cond (circular
8098                  (gnus-summary-beginning-of-article))
8099                 (lines
8100                  (gnus-message 3 "End of message"))
8101                 ((null lines)
8102                  (gnus-summary-next-unread-article)))))
8103     (gnus-summary-recenter)
8104     (gnus-summary-position-cursor)))
8105
8106 (defun gnus-summary-prev-page (&optional lines)
8107   "Show previous page of selected article.
8108 Argument LINES specifies lines to be scrolled down."
8109   (interactive "P")
8110   (gnus-set-global-variables)
8111   (let ((article (gnus-summary-article-number)))
8112     (gnus-configure-windows 'article)
8113     (if (or (null gnus-current-article)
8114             (null gnus-article-current)
8115             (/= article (cdr gnus-article-current))
8116             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8117         ;; Selected subject is different from current article's.
8118         (gnus-summary-display-article article)
8119       (gnus-summary-recenter)
8120       (gnus-eval-in-buffer-window gnus-article-buffer
8121         (gnus-article-prev-page lines))))
8122   (gnus-summary-position-cursor))
8123
8124 (defun gnus-summary-scroll-up (lines)
8125   "Scroll up (or down) one line current article.
8126 Argument LINES specifies lines to be scrolled up (or down if negative)."
8127   (interactive "p")
8128   (gnus-set-global-variables)
8129   (gnus-configure-windows 'article)
8130   (or (gnus-summary-select-article nil nil 'pseudo)
8131       (gnus-eval-in-buffer-window 
8132        gnus-article-buffer
8133        (cond ((> lines 0)
8134               (if (gnus-article-next-page lines)
8135                   (gnus-message 3 "End of message")))
8136              ((< lines 0)
8137               (gnus-article-prev-page (- lines))))))
8138   (gnus-summary-recenter)
8139   (gnus-summary-position-cursor))
8140
8141 (defun gnus-summary-next-same-subject ()
8142   "Select next article which has the same subject as current one."
8143   (interactive)
8144   (gnus-set-global-variables)
8145   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8146
8147 (defun gnus-summary-prev-same-subject ()
8148   "Select previous article which has the same subject as current one."
8149   (interactive)
8150   (gnus-set-global-variables)
8151   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8152
8153 (defun gnus-summary-next-unread-same-subject ()
8154   "Select next unread article which has the same subject as current one."
8155   (interactive)
8156   (gnus-set-global-variables)
8157   (gnus-summary-next-article t (gnus-summary-subject-string)))
8158
8159 (defun gnus-summary-prev-unread-same-subject ()
8160   "Select previous unread article which has the same subject as current one."
8161   (interactive)
8162   (gnus-set-global-variables)
8163   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8164
8165 (defun gnus-summary-first-unread-article ()
8166   "Select the first unread article. 
8167 Return nil if there are no unread articles."
8168   (interactive)
8169   (gnus-set-global-variables)
8170   (prog1
8171       (if (gnus-summary-first-subject t)
8172           (progn
8173             (gnus-summary-show-thread)
8174             (gnus-summary-first-subject t)
8175             (gnus-summary-display-article (gnus-summary-article-number))))
8176     (gnus-summary-position-cursor)))
8177
8178 (defun gnus-summary-best-unread-article ()
8179   "Select the unread article with the highest score."
8180   (interactive)
8181   (gnus-set-global-variables)
8182   (let ((scored gnus-newsgroup-scored)
8183         (best -1000000)
8184         article art)
8185     (while scored
8186       (or (> best (cdr (car scored)))
8187           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8188                (not (memq art gnus-newsgroup-marked))
8189                (not (memq art gnus-newsgroup-dormant))
8190                (if (= best (cdr (car scored)))
8191                    (setq article (min art article))
8192                  (setq article art)
8193                  (setq best (cdr (car scored))))))
8194       (setq scored (cdr scored)))
8195     (cond
8196      ((or (not article) (null gnus-newsgroup-unreads))
8197       ;; We didn't find any scored articles, so we just jump to the
8198       ;; first article. 
8199       (gnus-summary-first-unread-article))
8200      ((> best gnus-summary-default-score)
8201       ;; We found one, and it's bigger than the default score, so we
8202       ;; select it.
8203       (gnus-summary-goto-article article))
8204      (t
8205       ;; We found an article, but it has a score lower than the
8206       ;; defaults, so we try to find an article with the default
8207       ;; score. 
8208       (goto-char (point-min))
8209       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8210                       (not (eq (cdr (memq (gnus-summary-article-number)
8211                                           gnus-newsgroup-scored))
8212                                gnus-summary-default-score)))
8213                   (zerop (forward-line 1))
8214                   (not (eobp))))
8215       ;; We jump to the article we have finally found.
8216       (gnus-summary-goto-article (gnus-summary-article-number))))
8217     (gnus-summary-position-cursor)))
8218
8219 (defun gnus-summary-goto-article (article &optional all-headers)
8220   "Fetch ARTICLE and display it if it exists.
8221 If ALL-HEADERS is non-nil, no header lines are hidden."
8222   (interactive
8223    (list
8224     (string-to-int
8225      (completing-read 
8226       "Article number: "
8227       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8228               gnus-newsgroup-headers) 
8229       nil 'require-match))))
8230   (prog1
8231       (and (gnus-summary-goto-subject article)
8232            (gnus-summary-display-article article all-headers))
8233     (gnus-summary-position-cursor)))
8234
8235 (defun gnus-summary-goto-last-article ()
8236   "Go to the previously read article."
8237   (interactive)
8238   (prog1
8239       (and gnus-last-article
8240            (gnus-summary-goto-article gnus-last-article))
8241     (gnus-summary-position-cursor)))
8242
8243 (defun gnus-summary-pop-article (number)
8244   "Pop one article off the history and go to the previous.
8245 NUMBER articles will be popped off."
8246   (interactive "p")
8247   (let (to)
8248     (setq gnus-newsgroup-history
8249           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8250     (if to
8251         (gnus-summary-goto-article (car to))
8252       (error "Article history empty")))
8253   (gnus-summary-position-cursor))
8254
8255 ;; Summary article oriented commands
8256
8257 (defun gnus-summary-refer-parent-article (n)
8258   "Refer parent article N times.
8259 The difference between N and the number of articles fetched is returned."
8260   (interactive "p")
8261   (gnus-set-global-variables)
8262   (while 
8263       (and 
8264        (> n 0)
8265        (let ((ref (header-references (gnus-get-header-by-num
8266                                       (gnus-summary-article-number)))))
8267          (if (and ref (not (equal ref ""))
8268                   (string-match "<[^<>]*>[ \t]*$" ref))
8269              (gnus-summary-refer-article 
8270               (substring ref (match-beginning 0) (match-end 0)))
8271            (gnus-message 1 "No references in article %d"
8272                          (gnus-summary-article-number))
8273            nil)))
8274     (setq n (1- n)))
8275   (gnus-summary-position-cursor)
8276   n)
8277     
8278 (defun gnus-summary-refer-article (message-id)
8279   "Refer article specified by MESSAGE-ID.
8280 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8281   (interactive "sMessage-ID: ")
8282   (if (or (not (stringp message-id))
8283           (zerop (length message-id)))
8284       ()
8285     ;; Construct the correct Message-ID if necessary.
8286     ;; Suggested by tale@pawl.rpi.edu.
8287     (or (string-match "^<" message-id)
8288         (setq message-id (concat "<" message-id)))
8289     (or (string-match ">$" message-id)
8290         (setq message-id (concat message-id ">")))
8291     (let ((header (car (gnus-gethash (downcase message-id)
8292                                      gnus-newsgroup-dependencies))))
8293       (if header
8294           (or (gnus-summary-goto-article (header-number header))
8295               ;; The header has been read, but the article had been
8296               ;; expunged, so we insert it again.
8297               (progn
8298                 (gnus-summary-insert-line
8299                  nil header 0 nil gnus-read-mark nil nil
8300                  (header-subject header))
8301                 (forward-line -1)
8302                 (header-number header)))
8303         (let ((gnus-override-method gnus-refer-article-method)
8304               (gnus-ancient-mark gnus-read-mark)
8305               (tmp-point (window-start
8306                           (get-buffer-window gnus-article-buffer)))
8307               number tmp-buf)
8308           (and gnus-refer-article-method
8309                (or (gnus-server-opened gnus-refer-article-method)
8310                    (gnus-open-server gnus-refer-article-method)))
8311           ;; Save the old article buffer.
8312           (save-excursion
8313             (set-buffer gnus-article-buffer)
8314             (gnus-kill-buffer " *temp Article*")
8315             (setq tmp-buf (rename-buffer " *temp Article*")))
8316           (prog1
8317               (if (gnus-article-prepare 
8318                    message-id nil (gnus-read-header message-id))
8319                   (progn
8320                     (setq number (header-number gnus-current-headers))
8321                     (gnus-rebuild-thread message-id)
8322                     (gnus-summary-goto-subject number)
8323                     (gnus-summary-recenter)
8324                     (gnus-article-set-window-start 
8325                      (cdr (assq number gnus-newsgroup-bookmarks)))
8326                     message-id)
8327                 ;; We restore the old article buffer.
8328                 (save-excursion
8329                   (kill-buffer gnus-article-buffer)
8330                   (set-buffer tmp-buf)
8331                   (rename-buffer gnus-article-buffer)
8332                   (let ((buffer-read-only nil))
8333                     (and tmp-point
8334                          (set-window-start (get-buffer-window (current-buffer))
8335                                            tmp-point)))))))))))
8336
8337 (defun gnus-summary-enter-digest-group ()
8338   "Enter a digest group based on the current article."
8339   (interactive)
8340   (gnus-set-global-variables)
8341   (gnus-summary-select-article)
8342   ;; We do not want a narrowed article.
8343   (gnus-summary-stop-page-breaking)
8344   (let ((name (format "%s-%d" 
8345                       (gnus-group-prefixed-name 
8346                        gnus-newsgroup-name (list 'nndoc "")) 
8347                       gnus-current-article))
8348         (ogroup gnus-newsgroup-name)
8349         (buf (current-buffer)))
8350     (if (gnus-group-read-ephemeral-group 
8351          name (list 'nndoc name
8352                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8353                     '(nndoc-article-type digest))
8354          t)
8355         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8356                 (list (list (cons 'to-group ogroup))))
8357       (switch-to-buffer buf)
8358       (gnus-set-global-variables)
8359       (gnus-configure-windows 'summary)
8360       (gnus-message 3 "Article not a digest?"))))
8361
8362 (defun gnus-summary-isearch-article ()
8363   "Do incremental search forward on current article."
8364   (interactive)
8365   (gnus-set-global-variables)
8366   (gnus-summary-select-article)
8367   (gnus-eval-in-buffer-window 
8368    gnus-article-buffer (isearch-forward)))
8369
8370 (defun gnus-summary-search-article-forward (regexp &optional backward)
8371   "Search for an article containing REGEXP forward.
8372 If BACKWARD, search backward instead."
8373   (interactive
8374    (list (read-string
8375           (format "Search article %s (regexp%s): "
8376                   (if current-prefix-arg "backward" "forward")
8377                   (if gnus-last-search-regexp
8378                       (concat ", default " gnus-last-search-regexp)
8379                     "")))
8380          current-prefix-arg))
8381   (gnus-set-global-variables)
8382   (if (string-equal regexp "")
8383       (setq regexp (or gnus-last-search-regexp ""))
8384     (setq gnus-last-search-regexp regexp))
8385   (if (gnus-summary-search-article regexp backward)
8386       (gnus-article-set-window-start 
8387        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8388     (error "Search failed: \"%s\"" regexp)))
8389
8390 (defun gnus-summary-search-article-backward (regexp)
8391   "Search for an article containing REGEXP backward."
8392   (interactive
8393    (list (read-string
8394           (format "Search article backward (regexp%s): "
8395                   (if gnus-last-search-regexp
8396                       (concat ", default " gnus-last-search-regexp)
8397                     "")))))
8398   (gnus-summary-search-article-forward regexp 'backward))
8399
8400 (defun gnus-summary-search-article (regexp &optional backward)
8401   "Search for an article containing REGEXP.
8402 Optional argument BACKWARD means do search for backward.
8403 gnus-select-article-hook is not called during the search."
8404   (let ((gnus-select-article-hook nil)  ;Disable hook.
8405         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8406         (re-search
8407          (if backward
8408              (function re-search-backward) (function re-search-forward)))
8409         (found nil)
8410         (last nil))
8411     ;; Hidden thread subtrees must be searched for ,too.
8412     (gnus-summary-show-all-threads)
8413     (if (eobp) (forward-line -1))
8414     ;; First of all, search current article.
8415     ;; We don't want to read article again from NNTP server nor reset
8416     ;; current point.
8417     (gnus-summary-select-article)
8418     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8419     (setq last gnus-current-article)
8420     (gnus-eval-in-buffer-window gnus-article-buffer
8421       (save-restriction
8422         (widen)
8423         ;; Begin search from current point.
8424         (setq found (funcall re-search regexp nil t))))
8425     ;; Then search next articles.
8426     (while (and (not found)
8427                 (gnus-summary-display-article 
8428                  (gnus-summary-search-subject backward nil nil)))
8429       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8430       (gnus-eval-in-buffer-window gnus-article-buffer
8431         (save-restriction
8432           (widen)
8433           (goto-char (if backward (point-max) (point-min)))
8434           (setq found (funcall re-search regexp nil t)))))
8435     (message "")
8436     ;; Adjust article pointer.
8437     (or (eq last gnus-current-article)
8438         (setq gnus-last-article last))
8439     ;; Return T if found such article.
8440     found))
8441
8442 (defun gnus-summary-execute-command (header regexp command &optional backward)
8443   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8444 If HEADER is an empty string (or nil), the match is done on the entire
8445 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8446   (interactive
8447    (list (let ((completion-ignore-case t))
8448            (completing-read 
8449             "Header name: "
8450             (mapcar (lambda (string) (list string))
8451                     '("Number" "Subject" "From" "Lines" "Date"
8452                       "Message-ID" "Xref" "References"))
8453             nil 'require-match))
8454          (read-string "Regexp: ")
8455          (read-key-sequence "Command: ")
8456          current-prefix-arg))
8457   (gnus-set-global-variables)
8458   ;; Hidden thread subtrees must be searched as well.
8459   (gnus-summary-show-all-threads)
8460   ;; We don't want to change current point nor window configuration.
8461   (save-excursion
8462     (save-window-excursion
8463       (gnus-message 6 "Executing %s..." (key-description command))
8464       ;; We'd like to execute COMMAND interactively so as to give arguments.
8465       (gnus-execute header regexp
8466                     (` (lambda ()
8467                          (call-interactively '(, (key-binding command)))))
8468                     backward)
8469       (gnus-message 6 "Executing %s...done" (key-description command)))))
8470
8471 (defun gnus-summary-beginning-of-article ()
8472   "Scroll the article back to the beginning."
8473   (interactive)
8474   (gnus-set-global-variables)
8475   (gnus-summary-select-article)
8476   (gnus-configure-windows 'article)
8477   (gnus-eval-in-buffer-window
8478    gnus-article-buffer
8479    (widen)
8480    (goto-char (point-min))
8481    (and gnus-break-pages (gnus-narrow-to-page))))
8482
8483 (defun gnus-summary-end-of-article ()
8484   "Scroll to the end of the article."
8485   (interactive)
8486   (gnus-set-global-variables)
8487   (gnus-summary-select-article)
8488   (gnus-configure-windows 'article)
8489   (gnus-eval-in-buffer-window 
8490    gnus-article-buffer
8491    (widen)
8492    (goto-char (point-max))
8493    (and gnus-break-pages (gnus-narrow-to-page))))
8494
8495 (defun gnus-summary-show-article (&optional no-refetch)
8496   "Force re-fetching of the current article.
8497 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8498 be performed.  The current article will simply be redisplayed."
8499   (interactive "P")
8500   (gnus-set-global-variables)
8501   (if (not no-refetch)
8502       (gnus-summary-select-article gnus-have-all-headers t)
8503     (or gnus-current-article
8504         (error "There is no current article"))
8505     (gnus-summary-goto-subject gnus-current-article)
8506     (gnus-configure-windows 'article)
8507     (gnus-summary-position-cursor)))
8508
8509 (defun gnus-summary-verbose-headers (&optional arg)
8510   "Toggle permanent full header display.
8511 If ARG is a positive number, turn header display on.
8512 If ARG is a negative number, turn header display off."
8513   (interactive "P")
8514   (gnus-set-global-variables)
8515   (gnus-summary-toggle-header arg)
8516   (setq gnus-show-all-headers
8517         (cond ((or (not (numberp arg))
8518                    (zerop arg))
8519                (not gnus-show-all-headers))
8520               ((natnump arg)
8521                t))))
8522
8523 (defun gnus-summary-toggle-header (&optional arg)
8524   "Show the headers if they are hidden, or hide them if they are shown.
8525 If ARG is a positive number, show the entire header.
8526 If ARG is a negative number, hide the unwanted header lines."
8527   (interactive "P")
8528   (gnus-set-global-variables)
8529   (save-excursion
8530     (set-buffer gnus-article-buffer)
8531     (let ((buffer-read-only nil))
8532       (if (numberp arg) 
8533           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8534                                                 gnus-hidden-properties)
8535             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8536         (if (text-property-any (point-min) (point-max) 'invisible t)
8537             (remove-text-properties 
8538              (point-min) (point-max) gnus-hidden-properties)
8539           ;; We hide the headers. This song and dance act below is
8540           ;; done because `gnus-have-all-headers' is buffer-local to
8541           ;; the summary buffer, and we only want to temporarily
8542           ;; change it in that buffer. Ugh.
8543           (let ((have gnus-have-all-headers))
8544             (save-excursion
8545               (set-buffer gnus-summary-buffer)
8546               (setq gnus-have-all-headers nil)
8547               (save-excursion
8548                 (set-buffer gnus-article-buffer)
8549                 (run-hooks 'gnus-article-display-hook))
8550               (setq gnus-have-all-headers have)))))
8551       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8552
8553 (defun gnus-summary-show-all-headers ()
8554   "Make all header lines visible."
8555   (interactive)
8556   (gnus-set-global-variables)
8557   (gnus-article-show-all-headers))
8558
8559 (defun gnus-summary-toggle-mime (&optional arg)
8560   "Toggle MIME processing.
8561 If ARG is a positive number, turn MIME processing on."
8562   (interactive "P")
8563   (gnus-set-global-variables)
8564   (setq gnus-show-mime
8565         (if (null arg) (not gnus-show-mime)
8566           (> (prefix-numeric-value arg) 0)))
8567   (gnus-summary-select-article t 'force))
8568
8569 (defun gnus-summary-caesar-message (&optional arg)
8570   "Caesar rotate the current article by 13.
8571 The numerical prefix specifies how manu places to rotate each letter
8572 forward."
8573   (interactive "P")
8574   (gnus-set-global-variables)
8575   (gnus-summary-select-article)
8576   (let ((mail-header-separator ""))
8577     (gnus-eval-in-buffer-window 
8578      gnus-article-buffer
8579      (save-restriction
8580        (widen)
8581        (let ((last (point)))
8582          (news-caesar-buffer-body arg)
8583          (goto-char last)
8584          (recenter 0))))))
8585
8586 (defun gnus-summary-stop-page-breaking ()
8587   "Stop page breaking in the current article."
8588   (interactive)
8589   (gnus-set-global-variables)
8590   (gnus-summary-select-article)
8591   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8592
8593 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8594
8595 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8596   "Move the current article to a different newsgroup.
8597 If N is a positive number, move the N next articles.
8598 If N is a negative number, move the N previous articles.
8599 If N is nil and any articles have been marked with the process mark,
8600 move those articles instead.
8601 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8602 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8603 re-spool using this method.
8604 For this function to work, both the current newsgroup and the
8605 newsgroup that you want to move to have to support the `request-move'
8606 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8607   (interactive "P")
8608   (gnus-set-global-variables)
8609   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8610       (error "The current newsgroup does not support article moving"))
8611   (let ((articles (gnus-summary-work-articles n))
8612         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8613         art-group to-method sel-met)
8614     (if (and (not to-newsgroup) (not select-method))
8615         (setq to-newsgroup
8616               (completing-read 
8617                (format "Where do you want to move %s? %s"
8618                        (if (> (length articles) 1)
8619                            (format "these %d articles" (length articles))
8620                          "this article")
8621                        (if gnus-current-move-group
8622                            (format "(%s default) " gnus-current-move-group)
8623                          ""))
8624                gnus-active-hashtb nil nil prefix)))
8625     (if to-newsgroup
8626         (progn
8627           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8628               (setq to-newsgroup (or gnus-current-move-group "")))
8629           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8630               (gnus-activate-newsgroup to-newsgroup)
8631               (error "No such group: %s" to-newsgroup))
8632           (setq gnus-current-move-group to-newsgroup)))
8633     (setq to-method (if select-method (list select-method "")
8634                       (gnus-find-method-for-group to-newsgroup)))
8635     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8636         (error "%s does not support article copying" (car to-method)))
8637     (or (gnus-server-opened to-method)
8638         (gnus-open-server to-method)
8639         (error "Can't open server %s" (car to-method)))
8640     (gnus-message 6 "Moving to %s: %s..." 
8641                   (or select-method to-newsgroup) articles)
8642     (while articles
8643       (if (setq art-group
8644                 (gnus-request-move-article 
8645                  (car articles)                   ; Article to move
8646                  gnus-newsgroup-name              ; From newsgrouo
8647                  (nth 1 (gnus-find-method-for-group 
8648                          gnus-newsgroup-name))    ; Server
8649                  (list 'gnus-request-accept-article 
8650                        (if select-method
8651                            (list 'quote select-method)
8652                          to-newsgroup)
8653                        (not (cdr articles)))     ; Accept form
8654                  (not (cdr articles))))          ; Only save nov last time
8655           (let* ((buffer-read-only nil)
8656                  (entry 
8657                   (or
8658                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8659                    (gnus-gethash 
8660                     (gnus-group-prefixed-name 
8661                      (car art-group) 
8662                      (if select-method (list select-method "")
8663                        (gnus-find-method-for-group to-newsgroup)))
8664                     gnus-newsrc-hashtb)))
8665                  (info (nth 2 entry))
8666                  (article (car articles)))
8667             (gnus-summary-goto-subject article)
8668             (beginning-of-line)
8669             (delete-region (point)
8670                            (progn (forward-line 1) (point)))
8671             (if (not (memq article gnus-newsgroup-unreads))
8672                 (setcar (cdr (cdr info))
8673                         (gnus-add-to-range (nth 2 info) 
8674                                            (list (cdr art-group)))))
8675             ;; Copy any marks over to the new group.
8676             (let ((marks '((tick . gnus-newsgroup-marked)
8677                            (dormant . gnus-newsgroup-dormant)
8678                            (expire . gnus-newsgroup-expirable)
8679                            (bookmark . gnus-newsgroup-bookmarks)
8680                         ;   (score . gnus-newsgroup-scored)
8681                            (reply . gnus-newsgroup-replied)))
8682                   (to-article (cdr art-group)))
8683               (while marks
8684                 (if (memq article (symbol-value (cdr (car marks))))
8685                     (gnus-add-marked-articles 
8686                      (car info) (car (car marks)) (list to-article) info))
8687                 (setq marks (cdr marks))))
8688             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8689             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8690             (setq gnus-newsgroup-dormant
8691                   (delq article gnus-newsgroup-dormant))
8692             (setq gnus-newsgroup-reads
8693                   (cons (cons article gnus-canceled-mark)
8694                         gnus-newsgroup-reads)))
8695         (gnus-message 1 "Couldn't move article %s" (car articles)))
8696       (gnus-summary-remove-process-mark (car articles))
8697       (setq articles (cdr articles)))))
8698
8699 (defun gnus-summary-respool-article (&optional n respool-method)
8700   "Respool the current article.
8701 The article will be squeezed through the mail spooling process again,
8702 which means that it will be put in some mail newsgroup or other
8703 depending on `nnmail-split-methods'.
8704 If N is a positive number, respool the N next articles.
8705 If N is a negative number, respool the N previous articles.
8706 If N is nil and any articles have been marked with the process mark,
8707 respool those articles instead.
8708
8709 Respooling can be done both from mail groups and \"real\" newsgroups.
8710 In the former case, the articles in question will be moved from the
8711 current group into whatever groups they are destined to.  In the
8712 latter case, they will be copied into the relevant groups."
8713   (interactive "P")
8714   (gnus-set-global-variables)
8715   (let ((respool-methods (gnus-methods-using 'respool))
8716         (methname 
8717          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8718     (or respool-method
8719         (setq respool-method
8720               (completing-read
8721                "What method do you want to use when respooling? "
8722                respool-methods nil t methname)))
8723     (or (string= respool-method "")
8724         (if (assoc (symbol-name
8725                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8726                    respool-methods)
8727             (gnus-summary-move-article n nil (intern respool-method))
8728           (gnus-summary-copy-article n nil (intern respool-method))))))
8729
8730 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8731 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8732   "Move the current article to a different newsgroup.
8733 If N is a positive number, move the N next articles.
8734 If N is a negative number, move the N previous articles.
8735 If N is nil and any articles have been marked with the process mark,
8736 move those articles instead.
8737 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8738 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8739 re-spool using this method.
8740 For this function to work, the newsgroup that you want to move to have
8741 to support the `request-move' and `request-accept'
8742 functions. (Ie. mail newsgroups at present.)"
8743   (interactive "P")
8744   (gnus-set-global-variables)
8745   (let ((articles (gnus-summary-work-articles n))
8746         (copy-buf (get-buffer-create "*copy work*"))
8747         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8748         art-group to-method)
8749     (buffer-disable-undo copy-buf)
8750     (if (and (not to-newsgroup) (not select-method))
8751         (setq to-newsgroup
8752               (completing-read 
8753                (format "Where do you want to copy %s? %s"
8754                        (if (> (length articles) 1)
8755                            (format "these %d articles" (length articles))
8756                          "this article")
8757                        (if gnus-current-move-group
8758                            (format "(%s default) " gnus-current-move-group)
8759                          ""))
8760                gnus-active-hashtb nil nil prefix)))
8761     (if to-newsgroup
8762         (progn
8763           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8764               (setq to-newsgroup (or gnus-current-move-group "")))
8765           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8766               (gnus-activate-newsgroup to-newsgroup)
8767               (error "No such group: %s" to-newsgroup))
8768           (setq gnus-current-move-group to-newsgroup)))
8769     (setq to-method (if select-method (list select-method "")
8770                       (gnus-find-method-for-group to-newsgroup)))
8771     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8772         (error "%s does not support article copying" (car to-method)))
8773     (or (gnus-server-opened to-method)
8774         (gnus-open-server to-method)
8775         (error "Can't open server %s" (car to-method)))
8776     (while articles
8777       (gnus-message 6 "Copying to %s: %s..." 
8778                     (or select-method to-newsgroup) articles)
8779       (if (setq art-group
8780                 (save-excursion
8781                   (set-buffer copy-buf)
8782                   (gnus-request-article-this-buffer
8783                    (car articles) gnus-newsgroup-name)
8784                   (gnus-request-accept-article
8785                    (if select-method (quote select-method) to-newsgroup)
8786                    (not (cdr articles)))))
8787           (let* ((entry 
8788                   (or
8789                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8790                    (gnus-gethash 
8791                     (gnus-group-prefixed-name 
8792                      (car art-group) 
8793                      (if select-method (list select-method "")
8794                        (gnus-find-method-for-group to-newsgroup)))
8795                     gnus-newsrc-hashtb)))
8796                  (info (nth 2 entry))
8797                  (article (car articles)))
8798             (if (not (memq article gnus-newsgroup-unreads))
8799                 (setcar (cdr (cdr info))
8800                         (gnus-add-to-range (nth 2 info) 
8801                                            (list (cdr art-group)))))
8802             ;; Copy any marks over to the new group.
8803             (let ((marks '((tick . gnus-newsgroup-marked)
8804                            (dormant . gnus-newsgroup-dormant)
8805                            (expire . gnus-newsgroup-expirable)
8806                            (bookmark . gnus-newsgroup-bookmarks)
8807                         ;   (score . gnus-newsgroup-scored)
8808                            (reply . gnus-newsgroup-replied)))
8809                   (to-article (cdr art-group)))
8810               (while marks
8811                 (if (memq article (symbol-value (cdr (car marks))))
8812                     (gnus-add-marked-articles 
8813                      (car info) (car (car marks)) (list to-article) info))
8814                 (setq marks (cdr marks)))))
8815         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8816       (gnus-summary-remove-process-mark (car articles))
8817       (setq articles (cdr articles)))
8818     (kill-buffer copy-buf)))
8819
8820 (defun gnus-summary-import-article (file)
8821   "Import a random file into a mail newsgroup."
8822   (interactive "fImport file: ")
8823   (let ((group gnus-newsgroup-name)
8824         atts)
8825     (or (gnus-check-backend-function 'request-accept-article group)
8826         (error "%s does not support article importing" group))
8827     (or (file-readable-p file)
8828         (not (file-regular-p file))
8829         (error "Can't read %s" file))
8830     (save-excursion
8831       (set-buffer (get-buffer-create " *import file*"))
8832       (buffer-disable-undo (current-buffer))
8833       (erase-buffer)
8834       (insert-file-contents file)
8835       (goto-char (point-min))
8836       (if (nnheader-article-p)
8837           ()
8838         (setq atts (file-attributes file))
8839         (insert "From: " (read-string "From: ") "\n"
8840                 "Subject: " (read-string "Subject: ") "\n"
8841                 "Date: " (current-time-string (nth 5 atts)) "\n"
8842                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8843       (gnus-request-accept-article group t)
8844       (kill-buffer (current-buffer)))))
8845
8846 (defun gnus-summary-expire-articles ()
8847   "Expire all articles that are marked as expirable in the current group."
8848   (interactive)
8849   (if (not (gnus-check-backend-function 
8850             'request-expire-articles gnus-newsgroup-name))
8851       ()
8852     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8853                                       gnus-newsrc-hashtb)))
8854            (total (memq 'total-expire (nth 5 info)))
8855            (expirable (if total
8856                           (gnus-list-of-read-articles gnus-newsgroup-name)
8857                         (setq gnus-newsgroup-expirable
8858                               (sort gnus-newsgroup-expirable '<))))
8859            es)
8860       (if (not expirable)
8861           ()
8862         (gnus-message 6 "Expiring articles...")
8863         ;; The list of articles that weren't expired is returned.
8864         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8865         (or total (setq gnus-newsgroup-expirable es))
8866         ;; We go through the old list of expirable, and mark all
8867         ;; really expired articles as non-existant.
8868         (or (eq es expirable) ; If nothing was expired, we don't mark.
8869             (let ((gnus-use-cache nil))
8870               (while expirable
8871                 (or (memq (car expirable) es)
8872                     (gnus-summary-mark-article
8873                      (car expirable) gnus-canceled-mark))
8874                 (setq expirable (cdr expirable)))))
8875         (gnus-message 6 "Expiring articles...done")))))
8876
8877 (defun gnus-summary-expire-articles-now ()
8878   "Expunge all expirable articles in the current group.
8879 This means that *all* articles that are marked as expirable will be
8880 deleted forever, right now."
8881   (interactive)
8882   (or gnus-expert-user
8883       (gnus-y-or-n-p
8884        "Are you really, really, really sure you want to expunge? ")
8885       (error "Phew!"))
8886   (let ((nnmail-expiry-wait -1)
8887         (nnmail-expiry-wait-function nil))
8888     (gnus-summary-expire-articles)))
8889
8890 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8891 (defun gnus-summary-delete-article (&optional n)
8892   "Delete the N next (mail) articles.
8893 This command actually deletes articles. This is not a marking
8894 command. The article will disappear forever from you life, never to
8895 return. 
8896 If N is negative, delete backwards.
8897 If N is nil and articles have been marked with the process mark,
8898 delete these instead."
8899   (interactive "P")
8900   (or (gnus-check-backend-function 'request-expire-articles 
8901                                    gnus-newsgroup-name)
8902       (error "The current newsgroup does not support article deletion."))
8903   ;; Compute the list of articles to delete.
8904   (let ((articles (gnus-summary-work-articles n))
8905         not-deleted)
8906     (if (and gnus-novice-user
8907              (not (gnus-y-or-n-p 
8908                    (format "Do you really want to delete %s forever? "
8909                            (if (> (length articles) 1) "these articles"
8910                              "this article")))))
8911         ()
8912       ;; Delete the articles.
8913       (setq not-deleted (gnus-request-expire-articles 
8914                          articles gnus-newsgroup-name 'force))
8915       (while articles
8916         (gnus-summary-remove-process-mark (car articles))       
8917         ;; The backend might not have been able to delete the article
8918         ;; after all.  
8919         (or (memq (car articles) not-deleted)
8920             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8921         (setq articles (cdr articles))))
8922     (gnus-summary-position-cursor)
8923     not-deleted))
8924
8925 (defun gnus-summary-edit-article ()
8926   "Enter into a buffer and edit the current article.
8927 This will have permanent effect only in mail groups."
8928   (interactive)
8929   (or (gnus-check-backend-function 
8930        'request-replace-article gnus-newsgroup-name)
8931       (error "The current newsgroup does not support article editing."))
8932   (gnus-summary-select-article t)
8933   (gnus-configure-windows 'article)
8934   (select-window (get-buffer-window gnus-article-buffer))
8935   (gnus-message 6 "C-c C-c to end edits")
8936   (setq buffer-read-only nil)
8937   (text-mode)
8938   (use-local-map (copy-keymap (current-local-map)))
8939   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8940   (buffer-enable-undo)
8941   (widen)
8942   (goto-char (point-min))
8943   (search-forward "\n\n" nil t))
8944
8945 (defun gnus-summary-edit-article-done ()
8946   "Make edits to the current article permanent."
8947   (interactive)
8948   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8949     (erase-buffer)
8950     (insert buf)
8951     (if (not (gnus-request-replace-article 
8952               (cdr gnus-article-current) (car gnus-article-current) 
8953               (current-buffer)))
8954         (error "Couldn't replace article.")
8955       (gnus-article-mode)
8956       (use-local-map gnus-article-mode-map)
8957       (setq buffer-read-only t)
8958       (buffer-disable-undo (current-buffer))
8959       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))
8960       (gnus-configure-windows 'summary))))
8961
8962 (defun gnus-summary-edit-article-postpone ()
8963   "Postpone changes to the current article."
8964   (interactive)
8965   (gnus-article-mode)
8966   (use-local-map gnus-article-mode-map)
8967   (setq buffer-read-only t)
8968   (buffer-disable-undo (current-buffer))
8969   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))
8970   (gnus-configure-windows 'summary))
8971
8972 (defun gnus-summary-fancy-query ()
8973   "Query where the fancy respool algorithm would put this article."
8974   (interactive)
8975   (gnus-summary-select-article)
8976   (save-excursion
8977     (set-buffer gnus-article-buffer)
8978     (save-restriction
8979       (goto-char (point-min))
8980       (search-forward "\n\n")
8981       (narrow-to-region (point-min) (point))
8982       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8983
8984 ;; Summary score commands.
8985
8986 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8987
8988 (defun gnus-summary-raise-score (n)
8989   "Raise the score of the current article by N."
8990   (interactive "p")
8991   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8992
8993 (defun gnus-summary-set-score (n)
8994   "Set the score of the current article to N."
8995   (interactive "p")
8996   ;; Skip dummy header line.
8997   (save-excursion
8998     (gnus-summary-show-thread)
8999     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9000         (forward-line 1))
9001     (let ((buffer-read-only nil))
9002       ;; Set score.
9003       (gnus-summary-update-mark
9004        (if (= n (or gnus-summary-default-score 0)) ? 
9005          (if (< n (or gnus-summary-default-score 0)) 
9006              gnus-score-below-mark gnus-score-over-mark)) 'score))
9007     (let* ((article (gnus-summary-article-number))
9008            (score (assq article gnus-newsgroup-scored)))
9009       (if score (setcdr score n)
9010         (setq gnus-newsgroup-scored 
9011               (cons (cons article n) gnus-newsgroup-scored))))
9012     (gnus-summary-update-line)))
9013
9014 (defun gnus-summary-current-score ()
9015   "Return the score of the current article."
9016   (interactive)
9017   (message "%s" (gnus-summary-article-score)))
9018
9019 ;; Summary marking commands.
9020
9021 (defun gnus-summary-raise-same-subject-and-select (score)
9022   "Raise articles which has the same subject with SCORE and select the next."
9023   (interactive "p")
9024   (let ((subject (gnus-summary-subject-string)))
9025     (gnus-summary-raise-score score)
9026     (while (gnus-summary-search-subject nil nil subject)
9027       (gnus-summary-raise-score score))
9028     (gnus-summary-next-article t)))
9029
9030 (defun gnus-summary-raise-same-subject (score)
9031   "Raise articles which has the same subject with SCORE."
9032   (interactive "p")
9033   (let ((subject (gnus-summary-subject-string)))
9034     (gnus-summary-raise-score score)
9035     (while (gnus-summary-search-subject nil nil subject)
9036       (gnus-summary-raise-score score))
9037     (gnus-summary-next-subject 1 t)))
9038
9039 (defun gnus-score-default (level)
9040   (if level (prefix-numeric-value level) 
9041     gnus-score-interactive-default-score))
9042
9043 (defun gnus-summary-raise-thread (&optional score)
9044   "Raise the score of the articles in the current thread with SCORE."
9045   (interactive "P")
9046   (setq score (1- (gnus-score-default score)))
9047   (let (e)
9048     (save-excursion
9049       (let ((level (gnus-summary-thread-level)))
9050         (gnus-summary-raise-score score)
9051         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9052                     (> (gnus-summary-thread-level) level))
9053           (gnus-summary-raise-score score))
9054         (setq e (point))))
9055     (let ((gnus-summary-check-current t))
9056       (or (zerop (gnus-summary-next-subject 1 t))
9057           (goto-char e))))
9058   (gnus-summary-recenter)
9059   (gnus-summary-position-cursor)
9060   (gnus-set-mode-line 'summary))
9061
9062 (defun gnus-summary-lower-same-subject-and-select (score)
9063   "Raise articles which has the same subject with SCORE and select the next."
9064   (interactive "p")
9065   (gnus-summary-raise-same-subject-and-select (- score)))
9066
9067 (defun gnus-summary-lower-same-subject (score)
9068   "Raise articles which has the same subject with SCORE."
9069   (interactive "p")
9070   (gnus-summary-raise-same-subject (- score)))
9071
9072 (defun gnus-summary-lower-thread (&optional score)
9073   "Lower score of articles in the current thread with SCORE."
9074   (interactive "P")
9075   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9076
9077 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9078   "Mark articles which has the same subject as read, and then select the next.
9079 If UNMARK is positive, remove any kind of mark.
9080 If UNMARK is negative, tick articles."
9081   (interactive "P")
9082   (if unmark
9083       (setq unmark (prefix-numeric-value unmark)))
9084   (let ((count
9085          (gnus-summary-mark-same-subject
9086           (gnus-summary-subject-string) unmark)))
9087     ;; Select next unread article. If auto-select-same mode, should
9088     ;; select the first unread article.
9089     (gnus-summary-next-article t (and gnus-auto-select-same
9090                                       (gnus-summary-subject-string)))
9091     (gnus-message 7 "%d articles are marked as %s"
9092                   count (if unmark "unread" "read"))))
9093
9094 (defun gnus-summary-kill-same-subject (&optional unmark)
9095   "Mark articles which has the same subject as read. 
9096 If UNMARK is positive, remove any kind of mark.
9097 If UNMARK is negative, tick articles."
9098   (interactive "P")
9099   (if unmark
9100       (setq unmark (prefix-numeric-value unmark)))
9101   (let ((count
9102          (gnus-summary-mark-same-subject
9103           (gnus-summary-subject-string) unmark)))
9104     ;; If marked as read, go to next unread subject.
9105     (if (null unmark)
9106         ;; Go to next unread subject.
9107         (gnus-summary-next-subject 1 t))
9108     (gnus-message 7 "%d articles are marked as %s"
9109                   count (if unmark "unread" "read"))))
9110
9111 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9112   "Mark articles with same SUBJECT as read, and return marked number.
9113 If optional argument UNMARK is positive, remove any kinds of marks.
9114 If optional argument UNMARK is negative, mark articles as unread instead."
9115   (let ((count 1))
9116     (save-excursion
9117       (cond 
9118        ((null unmark)                   ; Mark as read.
9119         (while (and 
9120                 (progn
9121                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9122                   (gnus-summary-show-thread) t)
9123                 (gnus-summary-search-forward nil subject))
9124           (setq count (1+ count))))
9125        ((> unmark 0)                    ; Tick.
9126         (while (and
9127                 (progn
9128                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9129                   (gnus-summary-show-thread) t)
9130                 (gnus-summary-search-forward nil subject))
9131           (setq count (1+ count))))
9132        (t                               ; Mark as unread.
9133         (while (and
9134                 (progn
9135                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9136                   (gnus-summary-show-thread) t)
9137                 (gnus-summary-search-forward nil subject))
9138           (setq count (1+ count)))))
9139       ;; Return the number of marked articles.
9140       count)))
9141
9142 (defun gnus-summary-mark-as-processable (n &optional unmark)
9143   "Set the process mark on the next N articles.
9144 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9145 the process mark instead.  The difference between N and the actual
9146 number of articles marked is returned."
9147   (interactive "p")
9148   (let ((backward (< n 0))
9149         (n (abs n)))
9150   (while (and 
9151           (> n 0)
9152           (if unmark
9153               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9154             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9155           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9156     (setq n (1- n)))
9157   (if (/= 0 n) (gnus-message 7 "No more articles"))
9158   (gnus-summary-recenter)
9159   (gnus-summary-position-cursor)
9160   n))
9161
9162 (defun gnus-summary-unmark-as-processable (n)
9163   "Remove the process mark from the next N articles.
9164 If N is negative, mark backward instead.  The difference between N and
9165 the actual number of articles marked is returned."
9166   (interactive "p")
9167   (gnus-summary-mark-as-processable n t))
9168
9169 (defun gnus-summary-unmark-all-processable ()
9170   "Remove the process mark from all articles."
9171   (interactive)
9172   (save-excursion
9173     (while gnus-newsgroup-processable
9174       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9175   (gnus-summary-position-cursor))
9176
9177 (defun gnus-summary-mark-as-expirable (n)
9178   "Mark N articles forward as expirable.
9179 If N is negative, mark backward instead. The difference between N and
9180 the actual number of articles marked is returned."
9181   (interactive "p")
9182   (gnus-summary-mark-forward n gnus-expirable-mark))
9183
9184 (defun gnus-summary-mark-article-as-replied (article)
9185   "Mark ARTICLE replied and update the summary line."
9186   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9187   (let ((buffer-read-only nil))
9188     (if (gnus-summary-goto-subject article)
9189         (progn
9190           (gnus-summary-update-mark gnus-replied-mark 'replied)
9191           t))))
9192
9193 (defun gnus-summary-set-bookmark (article)
9194   "Set a bookmark in current article."
9195   (interactive (list (gnus-summary-article-number)))
9196   (if (or (not (get-buffer gnus-article-buffer))
9197           (not gnus-current-article)
9198           (not gnus-article-current)
9199           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9200       (error "No current article selected"))
9201   ;; Remove old bookmark, if one exists.
9202   (let ((old (assq article gnus-newsgroup-bookmarks)))
9203     (if old (setq gnus-newsgroup-bookmarks 
9204                   (delq old gnus-newsgroup-bookmarks))))
9205   ;; Set the new bookmark, which is on the form 
9206   ;; (article-number . line-number-in-body).
9207   (setq gnus-newsgroup-bookmarks 
9208         (cons 
9209          (cons article 
9210                (save-excursion
9211                  (set-buffer gnus-article-buffer)
9212                  (count-lines
9213                   (min (point)
9214                        (save-excursion
9215                          (goto-char (point-min))
9216                          (search-forward "\n\n" nil t)
9217                          (point)))
9218                   (point))))
9219          gnus-newsgroup-bookmarks))
9220   (gnus-message 6 "A bookmark has been added to the current article."))
9221
9222 (defun gnus-summary-remove-bookmark (article)
9223   "Remove the bookmark from the current article."
9224   (interactive (list (gnus-summary-article-number)))
9225   ;; Remove old bookmark, if one exists.
9226   (let ((old (assq article gnus-newsgroup-bookmarks)))
9227     (if old 
9228         (progn
9229           (setq gnus-newsgroup-bookmarks 
9230                 (delq old gnus-newsgroup-bookmarks))
9231           (gnus-message 6 "Removed bookmark."))
9232       (gnus-message 6 "No bookmark in current article."))))
9233
9234 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9235 (defun gnus-summary-mark-as-dormant (n)
9236   "Mark N articles forward as dormant.
9237 If N is negative, mark backward instead.  The difference between N and
9238 the actual number of articles marked is returned."
9239   (interactive "p")
9240   (gnus-summary-mark-forward n gnus-dormant-mark))
9241
9242 (defun gnus-summary-set-process-mark (article)
9243   "Set the process mark on ARTICLE and update the summary line."
9244   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9245   (let ((buffer-read-only nil))
9246     (if (gnus-summary-goto-subject article)
9247         (progn
9248           (gnus-summary-show-thread)
9249           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9250                (forward-line 1))
9251           (gnus-summary-update-mark gnus-process-mark 'replied)
9252           t))))
9253
9254 (defun gnus-summary-remove-process-mark (article)
9255   "Remove the process mark from ARTICLE and update the summary line."
9256   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9257   (let ((buffer-read-only nil))
9258     (if (gnus-summary-goto-subject article)
9259         (progn
9260           (gnus-summary-show-thread)
9261           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9262                (forward-line 1))
9263           (gnus-summary-update-mark ?  'replied)
9264           (if (memq article gnus-newsgroup-replied) 
9265               (gnus-summary-update-mark gnus-replied-mark 'replied))
9266           t))))
9267
9268 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9269   "Mark N articles as read forwards.
9270 If N is negative, mark backwards instead.
9271 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9272 marked as unread. 
9273 The difference between N and the actual number of articles marked is
9274 returned."
9275   (interactive "p")
9276   (gnus-set-global-variables)
9277   (let ((backward (< n 0))
9278         (gnus-summary-goto-unread
9279          (and gnus-summary-goto-unread
9280               (not (memq mark (list gnus-unread-mark
9281                                     gnus-ticked-mark gnus-dormant-mark)))))
9282         (n (abs n))
9283         (mark (or mark gnus-del-mark)))
9284   (while (and (> n 0)
9285               (gnus-summary-mark-article nil mark no-expire)
9286               (zerop (gnus-summary-next-subject 
9287                       (if backward -1 1) gnus-summary-goto-unread t)))
9288     (setq n (1- n)))
9289   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9290   (gnus-summary-recenter)
9291   (gnus-summary-position-cursor)
9292   (gnus-set-mode-line 'summary)
9293   n))
9294
9295 (defun gnus-summary-mark-article-as-read (mark)
9296   "Mark the current article quickly as read with MARK."
9297   (let ((article (gnus-summary-article-number)))
9298     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9299     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9300     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9301     (setq gnus-newsgroup-reads
9302           (cons (cons article mark) gnus-newsgroup-reads))
9303     ;; Possibly remove from cache, if that is used. 
9304     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9305     (and gnus-newsgroup-auto-expire 
9306          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9307              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9308              (= mark gnus-read-mark))
9309          (progn
9310            (setq mark gnus-expirable-mark)
9311            (setq gnus-newsgroup-expirable 
9312                  (cons article gnus-newsgroup-expirable))))
9313     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9314       (forward-line 1))
9315     ;; Fix the mark.
9316     (gnus-summary-update-mark mark 'unread)
9317     t))
9318
9319 (defun gnus-summary-mark-article-as-unread (mark)
9320   "Mark the current article quickly as unread with MARK."
9321   (let ((article (gnus-summary-article-number)))
9322     (or (memq article gnus-newsgroup-unreads)
9323         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9324     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9325     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9326     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9327     (setq gnus-newsgroup-reads
9328           (delq (assq article gnus-newsgroup-reads)
9329                 gnus-newsgroup-reads))
9330     (if (= mark gnus-ticked-mark)
9331         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9332     (if (= mark gnus-dormant-mark)
9333         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9334
9335     ;; See whether the article is to be put in the cache.
9336     (and gnus-use-cache
9337          (vectorp (gnus-get-header-by-num article))
9338          (save-excursion
9339            (gnus-cache-possibly-enter-article 
9340             gnus-newsgroup-name article 
9341             (gnus-get-header-by-num article)
9342             (= mark gnus-ticked-mark)
9343             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9344
9345     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9346       (forward-line 1))
9347     ;; Fix the mark.
9348     (gnus-summary-update-mark mark 'unread)
9349     t))
9350
9351 (defun gnus-summary-mark-article (&optional article mark no-expire)
9352   "Mark ARTICLE with MARK.  MARK can be any character.
9353 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9354 (dormant) and `?E' (expirable).
9355 If MARK is nil, then the default character `?D' is used.
9356 If ARTICLE is nil, then the article on the current line will be
9357 marked." 
9358   (and (stringp mark)
9359        (setq mark (aref mark 0)))
9360   ;; If no mark is given, then we check auto-expiring.
9361   (and (not no-expire)
9362        gnus-newsgroup-auto-expire 
9363        (or (not mark)
9364            (and (numberp mark) 
9365                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9366                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9367                     (= mark gnus-read-mark))))
9368        (setq mark gnus-expirable-mark))
9369   (let* ((mark (or mark gnus-del-mark))
9370          (article (or article (gnus-summary-article-number))))
9371     (or article (error "No article on current line"))
9372     (if (or (= mark gnus-unread-mark) 
9373             (= mark gnus-ticked-mark) 
9374             (= mark gnus-dormant-mark))
9375         (gnus-mark-article-as-unread article mark)
9376       (gnus-mark-article-as-read article mark))
9377
9378     ;; See whether the article is to be put in the cache.
9379     (and gnus-use-cache
9380          (not (= mark gnus-canceled-mark))
9381          (vectorp (gnus-get-header-by-num article))
9382          (save-excursion
9383            (gnus-cache-possibly-enter-article 
9384             gnus-newsgroup-name article 
9385             (gnus-get-header-by-num article)
9386             (= mark gnus-ticked-mark)
9387             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9388
9389     (if (gnus-summary-goto-subject article)
9390         (let ((buffer-read-only nil))
9391           (gnus-summary-show-thread)
9392           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9393                (forward-line 1))
9394           ;; Fix the mark.
9395           (gnus-summary-update-mark mark 'unread)
9396           t))))
9397
9398 (defun gnus-summary-update-mark (mark type)
9399   (beginning-of-line)
9400   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9401         (buffer-read-only nil)
9402         plist)
9403     (if (not forward)
9404         ()
9405       (forward-char forward)
9406       (setq plist (text-properties-at (point)))
9407       (delete-char 1)
9408       (insert mark)
9409       (and plist (add-text-properties (1- (point)) (point) plist))
9410       (and (eq type 'unread)
9411            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9412       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9413   
9414 (defun gnus-mark-article-as-read (article &optional mark)
9415   "Enter ARTICLE in the pertinent lists and remove it from others."
9416   ;; Make the article expirable.
9417   (let ((mark (or mark gnus-del-mark)))
9418     (if (= mark gnus-expirable-mark)
9419         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9420       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9421     ;; Remove from unread and marked lists.
9422     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9423     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9424     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9425     (setq gnus-newsgroup-reads 
9426           (cons (cons article mark) gnus-newsgroup-reads))
9427     ;; Possibly remove from cache, if that is used. 
9428     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9429
9430 (defun gnus-mark-article-as-unread (article &optional mark)
9431   "Enter ARTICLE in the pertinent lists and remove it from others."
9432   (let ((mark (or mark gnus-ticked-mark)))
9433     ;; Add to unread list.
9434     (or (memq article gnus-newsgroup-unreads)
9435         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9436     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9437     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9438     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9439     (setq gnus-newsgroup-reads
9440           (delq (assq article gnus-newsgroup-reads)
9441                 gnus-newsgroup-reads))
9442     (if (= mark gnus-ticked-mark)
9443         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9444     (if (= mark gnus-dormant-mark)
9445         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9446
9447 (defalias 'gnus-summary-mark-as-unread-forward 
9448   'gnus-summary-tick-article-forward)
9449 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9450                'gnus-summary-tick-article-forward)
9451 (defun gnus-summary-tick-article-forward (n)
9452   "Tick N articles forwards.
9453 If N is negative, tick backwards instead.
9454 The difference between N and the number of articles ticked is returned."
9455   (interactive "p")
9456   (gnus-summary-mark-forward n gnus-ticked-mark))
9457
9458 (defalias 'gnus-summary-mark-as-unread-backward 
9459   'gnus-summary-tick-article-backward)
9460 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9461                'gnus-summary-tick-article-backward)
9462 (defun gnus-summary-tick-article-backward (n)
9463   "Tick N articles backwards.
9464 The difference between N and the number of articles ticked is returned."
9465   (interactive "p")
9466   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9467
9468 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9469 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9470 (defun gnus-summary-tick-article (&optional article clear-mark)
9471   "Mark current article as unread.
9472 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9473 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9474   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9475                                        gnus-ticked-mark)))
9476
9477 (defun gnus-summary-mark-as-read-forward (n)
9478   "Mark N articles as read forwards.
9479 If N is negative, mark backwards instead.
9480 The difference between N and the actual number of articles marked is
9481 returned."
9482   (interactive "p")
9483   (gnus-summary-mark-forward n gnus-del-mark t))
9484
9485 (defun gnus-summary-mark-as-read-backward (n)
9486   "Mark the N articles as read backwards.
9487 The difference between N and the actual number of articles marked is
9488 returned."
9489   (interactive "p")
9490   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9491
9492 (defun gnus-summary-mark-as-read (&optional article mark)
9493   "Mark current article as read.
9494 ARTICLE specifies the article to be marked as read.
9495 MARK specifies a string to be inserted at the beginning of the line."
9496   (gnus-summary-mark-article article mark))
9497
9498 (defun gnus-summary-clear-mark-forward (n)
9499   "Clear marks from N articles forward.
9500 If N is negative, clear backward instead.
9501 The difference between N and the number of marks cleared is returned."
9502   (interactive "p")
9503   (gnus-summary-mark-forward n gnus-unread-mark))
9504
9505 (defun gnus-summary-clear-mark-backward (n)
9506   "Clear marks from N articles backward.
9507 The difference between N and the number of marks cleared is returned."
9508   (interactive "p")
9509   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9510
9511 (defun gnus-summary-mark-unread-as-read ()
9512   "Intended to be used by `gnus-summary-mark-article-hook'."
9513   (or (memq gnus-current-article gnus-newsgroup-marked)
9514       (memq gnus-current-article gnus-newsgroup-dormant)
9515       (memq gnus-current-article gnus-newsgroup-expirable)
9516       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9517
9518 (defun gnus-summary-mark-region-as-read (point mark all)
9519   "Mark all unread articles between point and mark as read.
9520 If given a prefix, mark all articles between point and mark as read,
9521 even ticked and dormant ones."
9522   (interactive "r\nP")
9523   (save-excursion
9524     (goto-char point)
9525     (beginning-of-line)
9526     (while (and 
9527             (< (point) mark)
9528             (progn
9529               (and
9530                (or all
9531                    (and
9532                     (not (memq (gnus-summary-article-number)
9533                                gnus-newsgroup-marked))
9534                     (not (memq (gnus-summary-article-number)
9535                                gnus-newsgroup-dormant))))
9536                (gnus-summary-mark-article
9537                 (gnus-summary-article-number) gnus-del-mark))
9538               t)
9539             (zerop (forward-line 1))))))
9540
9541 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9542 (defalias 'gnus-summary-delete-marked-as-read 
9543   'gnus-summary-remove-lines-marked-as-read)
9544 (make-obsolete 'gnus-summary-delete-marked-as-read 
9545                'gnus-summary-remove-lines-marked-as-read)
9546 (defun gnus-summary-remove-lines-marked-as-read ()
9547   "Remove lines that are marked as read."
9548   (interactive)
9549   (gnus-summary-remove-lines-marked-with 
9550    (concat (mapconcat
9551             (lambda (char) (char-to-string (symbol-value char)))
9552             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9553               gnus-killed-mark gnus-kill-file-mark
9554               gnus-low-score-mark gnus-expirable-mark
9555               gnus-canceled-mark gnus-catchup-mark)
9556             ""))))
9557
9558 (defalias 'gnus-summary-delete-marked-with 
9559   'gnus-summary-remove-lines-marked-with)
9560 (make-obsolete 'gnus-summary-delete-marked-with 
9561                'gnus-summary-remove-lines-marked-with)
9562 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9563 (defun gnus-summary-remove-lines-marked-with (marks)
9564   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9565   (interactive "sMarks: ")
9566   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9567   (gnus-set-global-variables)
9568   (let ((buffer-read-only nil)
9569         (marks (concat "^[" marks "]")))
9570     (goto-char (point-min))
9571     (if gnus-newsgroup-adaptive
9572         (gnus-score-remove-lines-adaptive marks)
9573       (while (re-search-forward marks nil t)
9574         (gnus-delete-line)))
9575     ;; If we use dummy roots, we have to do an additional sweep over
9576     ;; the buffer.
9577     (if (not (eq gnus-summary-make-false-root 'dummy))
9578         ()
9579       (goto-char (point-min))
9580       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9581       (while (re-search-forward marks nil t)
9582         (if (gnus-subject-equal
9583              (gnus-summary-subject-string)
9584              (progn
9585                (forward-line 1)
9586                (gnus-summary-subject-string)))
9587             ()
9588           (forward-line -1)
9589           (gnus-delete-line)))))
9590   (or (zerop (buffer-size))
9591       (if (eobp)
9592           (gnus-summary-prev-subject 1)
9593         (gnus-summary-position-cursor))))
9594
9595 (defun gnus-summary-expunge-below (&optional score)
9596   "Remove articles with score less than SCORE."
9597   (interactive "P")
9598   (gnus-set-global-variables)
9599   (setq score (if score
9600                   (prefix-numeric-value score)
9601                 (or gnus-summary-default-score 0)))
9602   (save-excursion
9603     (set-buffer gnus-summary-buffer)
9604     (goto-char (point-min))
9605     (let ((buffer-read-only nil)
9606           beg)
9607       (while (not (eobp))
9608         (if (< (gnus-summary-article-score) score)
9609             (progn
9610               (setq beg (point))
9611               (forward-line 1)
9612               (delete-region beg (point)))
9613           (forward-line 1)))
9614       ;; Adjust point.
9615       (or (zerop (buffer-size))
9616           (if (eobp)
9617               (gnus-summary-prev-subject 1)
9618             (gnus-summary-position-cursor))))))
9619
9620 (defun gnus-summary-mark-below (score mark)
9621   "Mark articles with score less than SCORE with MARK."
9622   (interactive "P\ncMark: ")
9623   (gnus-set-global-variables)
9624   (setq score (if score
9625                   (prefix-numeric-value score)
9626                 (or gnus-summary-default-score 0)))
9627   (save-excursion
9628     (set-buffer gnus-summary-buffer)
9629     (goto-char (point-min))
9630     (while (not (eobp))
9631       (and (< (gnus-summary-article-score) score)
9632            (gnus-summary-mark-article nil mark))
9633       (forward-line 1))))
9634
9635 (defun gnus-summary-kill-below (&optional score)
9636   "Mark articles with score below SCORE as read."
9637   (interactive "P")
9638   (gnus-set-global-variables)
9639   (gnus-summary-mark-below score gnus-killed-mark))
9640
9641 (defun gnus-summary-clear-above (&optional score)
9642   "Clear all marks from articles with score above SCORE."
9643   (interactive "P")
9644   (gnus-set-global-variables)
9645   (gnus-summary-mark-above score gnus-unread-mark))
9646
9647 (defun gnus-summary-tick-above (&optional score)
9648   "Tick all articles with score above SCORE."
9649   (interactive "P")
9650   (gnus-set-global-variables)
9651   (gnus-summary-mark-above score gnus-ticked-mark))
9652
9653 (defun gnus-summary-mark-above (score mark)
9654   "Mark articles with score over SCORE with MARK."
9655   (interactive "P\ncMark: ")
9656   (gnus-set-global-variables)
9657   (setq score (if score
9658                   (prefix-numeric-value score)
9659                 (or gnus-summary-default-score 0)))
9660   (save-excursion
9661     (set-buffer gnus-summary-buffer)
9662     (goto-char (point-min))
9663     (while (not (eobp))
9664       (if (> (gnus-summary-article-score) score)
9665           (progn
9666             (gnus-summary-mark-article nil mark)
9667             (forward-line 1))
9668         (forward-line 1)))))
9669
9670 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9671 (defun gnus-summary-show-all-expunged ()
9672   "Display all the hidden articles that were expunged for low scores."
9673   (interactive)
9674   (gnus-set-global-variables)
9675   (let ((buffer-read-only nil))
9676     (let ((scored gnus-newsgroup-scored)
9677           headers h)
9678       (while scored
9679         (or (gnus-summary-goto-subject (car (car scored)))
9680             (and (setq h (gnus-get-header-by-num (car (car scored))))
9681                  (< (cdr (car scored)) gnus-summary-expunge-below)
9682                  (setq headers (cons h headers))))
9683         (setq scored (cdr scored)))
9684       (or headers (error "No expunged articles hidden."))
9685       (goto-char (point-min))
9686       (save-excursion 
9687         (gnus-summary-update-lines 
9688          (point)
9689          (progn
9690            (gnus-summary-prepare-unthreaded (nreverse headers))
9691            (point)))))
9692     (goto-char (point-min))
9693     (gnus-summary-position-cursor)))
9694
9695 (defun gnus-summary-show-all-dormant ()
9696   "Display all the hidden articles that are marked as dormant."
9697   (interactive)
9698   (gnus-set-global-variables)
9699   (let ((buffer-read-only nil))
9700     (let ((dormant gnus-newsgroup-dormant)
9701           headers h)
9702       (while dormant
9703         (or (gnus-summary-goto-subject (car dormant))
9704             (and (setq h (gnus-get-header-by-num (car dormant)))
9705                  (setq headers (cons h headers))))
9706         (setq dormant (cdr dormant)))
9707       (or headers (error "No dormant articles hidden."))
9708       (goto-char (point-min))
9709       (save-excursion 
9710         (gnus-summary-update-lines 
9711          (point)
9712          (progn
9713            (gnus-summary-prepare-unthreaded (nreverse headers))
9714            (point)))))
9715     (goto-char (point-min))
9716     (gnus-summary-position-cursor)))
9717
9718 (defun gnus-summary-hide-all-dormant ()
9719   "Hide all dormant articles."
9720   (interactive)
9721   (gnus-set-global-variables)
9722   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9723   (gnus-summary-position-cursor))
9724
9725 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9726   "Mark all articles not marked as unread in this newsgroup as read.
9727 If prefix argument ALL is non-nil, all articles are marked as read.
9728 If QUIETLY is non-nil, no questions will be asked.
9729 If TO-HERE is non-nil, it should be a point in the buffer. All
9730 articles before this point will be marked as read.
9731 The number of articles marked as read is returned."
9732   (interactive "P")
9733   (gnus-set-global-variables)
9734   (prog1
9735       (if (or quietly
9736               (not gnus-interactive-catchup) ;Without confirmation?
9737               gnus-expert-user
9738               (gnus-y-or-n-p
9739                (if all
9740                    "Mark absolutely all articles as read? "
9741                  "Mark all unread articles as read? ")))
9742           (if (and not-mark 
9743                    (not gnus-newsgroup-adaptive)
9744                    (not gnus-newsgroup-auto-expire))
9745               (progn
9746                 (and all (setq gnus-newsgroup-marked nil
9747                                gnus-newsgroup-dormant nil))
9748                 (setq gnus-newsgroup-unreads 
9749                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9750             ;; We actually mark all articles as canceled, which we
9751             ;; have to do when using auto-expiry or adaptive scoring. 
9752             (let ((unreads (length gnus-newsgroup-unreads)))
9753               (gnus-summary-show-all-threads)
9754               (if (gnus-summary-first-subject (not all))
9755                   (while (and 
9756                           (if to-here (< (point) to-here) t)
9757                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9758                           (gnus-summary-search-subject nil (not all)))))
9759               (- unreads (length gnus-newsgroup-unreads))
9760               (or to-here
9761                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9762     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9763       (if (and (not to-here) (eq 'nnvirtual (car method)))
9764           (nnvirtual-catchup-group
9765            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9766     (gnus-summary-position-cursor)))
9767
9768 (defun gnus-summary-catchup-to-here (&optional all)
9769   "Mark all unticked articles before the current one as read.
9770 If ALL is non-nil, also mark ticked and dormant articles as read."
9771   (interactive)
9772   (gnus-set-global-variables)
9773   (save-excursion
9774     (and (zerop (forward-line -1))
9775          (progn
9776            (end-of-line)
9777            (gnus-summary-catchup all t (point))
9778            (gnus-set-mode-line 'summary))))
9779   (gnus-summary-position-cursor))
9780
9781 (defun gnus-summary-catchup-all (&optional quietly)
9782   "Mark all articles in this newsgroup as read."
9783   (interactive)
9784   (gnus-set-global-variables)
9785   (gnus-summary-catchup t quietly))
9786
9787 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9788   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9789 If prefix argument ALL is non-nil, all articles are marked as read."
9790   (interactive "P")
9791   (gnus-set-global-variables)
9792   (gnus-summary-catchup all quietly nil 'fast)
9793   ;; Select next newsgroup or exit.
9794   (if (eq gnus-auto-select-next 'quietly)
9795       (gnus-summary-next-group nil)
9796     (gnus-summary-exit)))
9797
9798 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9799   "Mark all articles in this newsgroup as read, and then exit."
9800   (interactive)
9801   (gnus-set-global-variables)
9802   (gnus-summary-catchup-and-exit t quietly))
9803
9804 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9805 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9806   "Mark all articles in this group as read and select the next group.
9807 If given a prefix, mark all articles, unread as well as ticked, as
9808 read." 
9809   (interactive "P")
9810   (gnus-set-global-variables)
9811   (gnus-summary-catchup all)
9812   (gnus-summary-next-group))
9813
9814 ;; Thread-based commands.
9815
9816 (defun gnus-summary-toggle-threads (&optional arg)
9817   "Toggle showing conversation threads.
9818 If ARG is positive number, turn showing conversation threads on."
9819   (interactive "P")
9820   (gnus-set-global-variables)
9821   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9822     (setq gnus-show-threads
9823           (if (null arg) (not gnus-show-threads)
9824             (> (prefix-numeric-value arg) 0)))
9825     (gnus-summary-prepare)
9826     (gnus-summary-goto-subject current)
9827     (gnus-summary-position-cursor)))
9828
9829 (defun gnus-summary-show-all-threads ()
9830   "Show all threads."
9831   (interactive)
9832   (gnus-set-global-variables)
9833   (save-excursion
9834     (let ((buffer-read-only nil))
9835       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9836   (gnus-summary-position-cursor))
9837
9838 (defun gnus-summary-show-thread ()
9839   "Show thread subtrees.
9840 Returns nil if no thread was there to be shown."
9841   (interactive)
9842   (gnus-set-global-variables)
9843   (let ((buffer-read-only nil)
9844         (orig (point))
9845         ;; first goto end then to beg, to have point at beg after let
9846         (end (progn (end-of-line) (point)))
9847         (beg (progn (beginning-of-line) (point))))
9848     (prog1
9849         ;; Any hidden lines here?
9850         (search-forward "\r" end t)
9851       (subst-char-in-region beg end ?\^M ?\n t)
9852       (goto-char orig)
9853       (gnus-summary-position-cursor))))
9854
9855 (defun gnus-summary-hide-all-threads ()
9856   "Hide all thread subtrees."
9857   (interactive)
9858   (gnus-set-global-variables)
9859   (save-excursion
9860     (goto-char (point-min))
9861     (gnus-summary-hide-thread)
9862     (while (and (not (eobp)) (zerop (forward-line 1)))
9863       (gnus-summary-hide-thread)))
9864   (gnus-summary-position-cursor))
9865
9866 (defun gnus-summary-hide-thread ()
9867   "Hide thread subtrees.
9868 Returns nil if no threads were there to be hidden."
9869   (interactive)
9870   (gnus-set-global-variables)
9871   (let ((buffer-read-only nil)
9872         (start (point))
9873         (level (gnus-summary-thread-level))
9874         (end (point)))
9875     ;; Go forward until either the buffer ends or the subthread
9876     ;; ends. 
9877     (if (eobp)
9878         ()
9879       (while (and (zerop (forward-line 1))
9880                   (> (gnus-summary-thread-level) level))
9881         (setq end (point)))
9882       (prog1
9883           (save-excursion
9884             (goto-char end)
9885             (search-backward "\n" start t))
9886         (subst-char-in-region start end ?\n ?\^M t)
9887         (forward-line -1)))))
9888
9889 (defun gnus-summary-go-to-next-thread (&optional previous)
9890   "Go to the same level (or less) next thread.
9891 If PREVIOUS is non-nil, go to previous thread instead.
9892 Return the article number moved to, or nil if moving was impossible."
9893   (let ((level (gnus-summary-thread-level))
9894         (article (gnus-summary-article-number)))
9895     (if previous 
9896         (while (and (zerop (forward-line -1))
9897                     (> (gnus-summary-thread-level) level)))
9898       (while (and (save-excursion
9899                     (forward-line 1)
9900                     (not (eobp)))
9901                   (zerop (forward-line 1))
9902                   (> (gnus-summary-thread-level) level))))
9903     (gnus-summary-recenter)
9904     (gnus-summary-position-cursor)
9905     (let ((oart (gnus-summary-article-number)))
9906       (and (/= oart article) oart))))
9907
9908 (defun gnus-summary-next-thread (n)
9909   "Go to the same level next N'th thread.
9910 If N is negative, search backward instead.
9911 Returns the difference between N and the number of skips actually
9912 done."
9913   (interactive "p")
9914   (gnus-set-global-variables)
9915   (let ((backward (< n 0))
9916         (n (abs n)))
9917   (while (and (> n 0)
9918               (gnus-summary-go-to-next-thread backward))
9919     (setq n (1- n)))
9920   (gnus-summary-position-cursor)
9921   (if (/= 0 n) (gnus-message 7 "No more threads"))
9922   n))
9923
9924 (defun gnus-summary-prev-thread (n)
9925   "Go to the same level previous N'th thread.
9926 Returns the difference between N and the number of skips actually
9927 done."
9928   (interactive "p")
9929   (gnus-set-global-variables)
9930   (gnus-summary-next-thread (- n)))
9931
9932 (defun gnus-summary-go-down-thread (&optional same)
9933   "Go down one level in the current thread.
9934 If SAME is non-nil, also move to articles of the same level."
9935   (let ((level (gnus-summary-thread-level))
9936         (start (point)))
9937     (if (and (zerop (forward-line 1))
9938              (> (gnus-summary-thread-level) level))
9939         t
9940       (goto-char start)
9941       nil)))
9942
9943 (defun gnus-summary-go-up-thread ()
9944   "Go up one level in the current thread."
9945   (let ((level (gnus-summary-thread-level))
9946         (start (point)))
9947     (while (and (zerop (forward-line -1))
9948                 (>= (gnus-summary-thread-level) level)))
9949     (if (>= (gnus-summary-thread-level) level)
9950         (progn
9951           (goto-char start)
9952           nil)
9953       t)))
9954
9955 (defun gnus-summary-down-thread (n)
9956   "Go down thread N steps.
9957 If N is negative, go up instead.
9958 Returns the difference between N and how many steps down that were
9959 taken."
9960   (interactive "p")
9961   (gnus-set-global-variables)
9962   (let ((up (< n 0))
9963         (n (abs n)))
9964   (while (and (> n 0)
9965               (if up (gnus-summary-go-up-thread)
9966                 (gnus-summary-go-down-thread)))
9967     (setq n (1- n)))
9968   (gnus-summary-position-cursor)
9969   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9970   n))
9971
9972 (defun gnus-summary-up-thread (n)
9973   "Go up thread N steps.
9974 If N is negative, go up instead.
9975 Returns the difference between N and how many steps down that were
9976 taken."
9977   (interactive "p")
9978   (gnus-set-global-variables)
9979   (gnus-summary-down-thread (- n)))
9980
9981 (defun gnus-summary-kill-thread (&optional unmark)
9982   "Mark articles under current thread as read.
9983 If the prefix argument is positive, remove any kinds of marks.
9984 If the prefix argument is negative, tick articles instead."
9985   (interactive "P")
9986   (gnus-set-global-variables)
9987   (if unmark
9988       (setq unmark (prefix-numeric-value unmark)))
9989   (let ((killing t)
9990         (level (gnus-summary-thread-level)))
9991     (save-excursion
9992       (while killing
9993         ;; Mark the article...
9994         (cond ((null unmark) (gnus-summary-mark-article-as-read
9995                                gnus-killed-mark))
9996               ((> unmark 0) (gnus-summary-mark-article-as-unread 
9997                              gnus-unread-mark))
9998               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9999         ;; ...and go forward until either the buffer ends or the subtree
10000         ;; ends. 
10001         (if (not (and (zerop (forward-line 1))
10002                       (> (gnus-summary-thread-level) level)))
10003             (setq killing nil))))
10004     ;; Hide killed subtrees.
10005     (and (null unmark)
10006          gnus-thread-hide-killed
10007          (gnus-summary-hide-thread))
10008     ;; If marked as read, go to next unread subject.
10009     (if (null unmark)
10010         ;; Go to next unread subject.
10011         (gnus-summary-next-subject 1 t)))
10012   (gnus-set-mode-line 'summary))
10013
10014 ;; Summary sorting commands
10015
10016 (defun gnus-summary-sort-by-number (&optional reverse)
10017   "Sort summary buffer by article number.
10018 Argument REVERSE means reverse order."
10019   (interactive "P")
10020   (gnus-set-global-variables)
10021   (gnus-summary-sort 
10022    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10023    ;; a function, so we wrap it.
10024    (cons (lambda () (gnus-summary-article-number))
10025          'gnus-thread-sort-by-number) reverse))
10026
10027 (defun gnus-summary-sort-by-author (&optional reverse)
10028   "Sort summary buffer by author name alphabetically.
10029 If case-fold-search is non-nil, case of letters is ignored.
10030 Argument REVERSE means reverse order."
10031   (interactive "P")
10032   (gnus-set-global-variables)
10033   (gnus-summary-sort
10034    (cons
10035     (lambda ()
10036       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10037              (extract (funcall
10038                        gnus-extract-address-components
10039                        (header-from header))))
10040         (concat (or (car extract) (cdr extract))
10041                 "\r" (header-subject header))))
10042     'gnus-thread-sort-by-author)
10043    reverse))
10044
10045 (defun gnus-summary-sort-by-subject (&optional reverse)
10046   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10047 If case-fold-search is non-nil, case of letters is ignored.
10048 Argument REVERSE means reverse order."
10049   (interactive "P")
10050   (gnus-set-global-variables)
10051   (gnus-summary-sort
10052    (cons
10053     (lambda ()
10054       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10055              (extract (funcall
10056                        gnus-extract-address-components
10057                        (header-from header))))
10058         (concat 
10059          (downcase (gnus-simplify-subject (gnus-summary-subject-string)))
10060          "\r" (or (car extract) (cdr extract)))))
10061     'gnus-thread-sort-by-subject)
10062    reverse))
10063
10064 (defun gnus-summary-sort-by-date (&optional reverse)
10065   "Sort summary buffer by date.
10066 Argument REVERSE means reverse order."
10067   (interactive "P")
10068   (gnus-set-global-variables)
10069   (gnus-summary-sort
10070    (cons
10071     (lambda ()
10072       (gnus-sortable-date
10073        (header-date 
10074         (gnus-get-header-by-num (gnus-summary-article-number)))))
10075     'gnus-thread-sort-by-date)
10076    reverse))
10077
10078 (defun gnus-summary-sort-by-score (&optional reverse)
10079   "Sort summary buffer by score.
10080 Argument REVERSE means reverse order."
10081   (interactive "P")
10082   (gnus-set-global-variables)
10083   (gnus-summary-sort 
10084    (cons (lambda () (gnus-summary-article-score))
10085          'gnus-thread-sort-by-score)
10086    (not reverse)))
10087
10088 (defvar gnus-summary-already-sorted nil)
10089 (defun gnus-summary-sort (predicate reverse)
10090   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10091   (if gnus-summary-already-sorted
10092       ()
10093     (let (buffer-read-only)
10094       (if (not gnus-show-threads)
10095           ;; We do untreaded sorting...
10096           (progn
10097             (goto-char (point-min))
10098             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10099         ;; ... or we do threaded sorting.
10100         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10101               (gnus-summary-prepare-hook nil)
10102               (gnus-summary-already-sorted nil))
10103           ;; We do that by simply regenerating the threads.
10104           (gnus-summary-prepare)
10105           (and gnus-show-threads
10106                gnus-thread-hide-subtree
10107                (gnus-summary-hide-all-threads))
10108           ;; If in async mode, we send some info to the backend.
10109           (and gnus-newsgroup-async
10110                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10111                (gnus-request-asynchronous 
10112                 gnus-newsgroup-name
10113                 (if (and gnus-asynchronous-article-function
10114                          (fboundp gnus-asynchronous-article-function))
10115                     (funcall gnus-asynchronous-article-function
10116                              gnus-newsgroup-threads)))))))))
10117
10118   
10119 (defun gnus-sortable-date (date)
10120   "Make sortable string by string-lessp from DATE.
10121 Timezone package is used."
10122   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10123          (year (aref date 0))
10124          (month (aref date 1))
10125          (day (aref date 2)))
10126     (timezone-make-sortable-date 
10127      year month day 
10128      (timezone-make-time-string
10129       (aref date 3) (aref date 4) (aref date 5)))))
10130
10131
10132 ;; Summary saving commands.
10133
10134 (defun gnus-summary-save-article (&optional n)
10135   "Save the current article using the default saver function.
10136 If N is a positive number, save the N next articles.
10137 If N is a negative number, save the N previous articles.
10138 If N is nil and any articles have been marked with the process mark,
10139 save those articles instead.
10140 The variable `gnus-default-article-saver' specifies the saver function."
10141   (interactive "P")
10142   (gnus-set-global-variables)
10143   (let ((articles (gnus-summary-work-articles n)))
10144     (while articles
10145       (let ((header (gnus-get-header-by-num (car articles))))
10146         (if (vectorp header)
10147             (progn
10148               (save-window-excursion
10149                 (gnus-summary-select-article t nil nil (car articles)))
10150               (or gnus-save-all-headers
10151                   (gnus-article-hide-headers t))
10152               ;; Remove any X-Gnus lines.
10153               (save-excursion
10154                 (save-restriction
10155                   (set-buffer gnus-article-buffer)
10156                   (let ((buffer-read-only nil))
10157                     (goto-char (point-min))
10158                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10159                                                   (point-max)))
10160                     (while (re-search-forward "^X-Gnus" nil t)
10161                       (beginning-of-line)
10162                       (delete-region (point)
10163                                      (progn (forward-line 1) (point))))
10164                     (widen))))
10165               (save-window-excursion
10166                 (if gnus-default-article-saver
10167                     (funcall gnus-default-article-saver)
10168                   (error "No default saver is defined."))))
10169           (if (assq 'name header)
10170               (gnus-copy-file (cdr (assq 'name header)))
10171             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10172       (gnus-summary-remove-process-mark (car articles))
10173       (setq articles (cdr articles)))
10174     (gnus-summary-position-cursor)
10175     n))
10176
10177 (defun gnus-summary-pipe-output (&optional arg)
10178   "Pipe the current article to a subprocess.
10179 If N is a positive number, pipe the N next articles.
10180 If N is a negative number, pipe the N previous articles.
10181 If N is nil and any articles have been marked with the process mark,
10182 pipe those articles instead."
10183   (interactive "P")
10184   (gnus-set-global-variables)
10185   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10186     (gnus-summary-save-article arg)))
10187
10188 (defun gnus-summary-save-article-mail (&optional arg)
10189   "Append the current article to an mail file.
10190 If N is a positive number, save the N next articles.
10191 If N is a negative number, save the N previous articles.
10192 If N is nil and any articles have been marked with the process mark,
10193 save those articles instead."
10194   (interactive "P")
10195   (gnus-set-global-variables)
10196   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10197     (gnus-summary-save-article arg)))
10198
10199 (defun gnus-summary-save-article-rmail (&optional arg)
10200   "Append the current article to an rmail file.
10201 If N is a positive number, save the N next articles.
10202 If N is a negative number, save the N previous articles.
10203 If N is nil and any articles have been marked with the process mark,
10204 save those articles instead."
10205   (interactive "P")
10206   (gnus-set-global-variables)
10207   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10208     (gnus-summary-save-article arg)))
10209
10210 (defun gnus-summary-save-article-file (&optional arg)
10211   "Append the current article to a file.
10212 If N is a positive number, save the N next articles.
10213 If N is a negative number, save the N previous articles.
10214 If N is nil and any articles have been marked with the process mark,
10215 save those articles instead."
10216   (interactive "P")
10217   (gnus-set-global-variables)
10218   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10219     (gnus-summary-save-article arg)))
10220
10221 (defun gnus-read-save-file-name (prompt default-name)
10222   (let ((methods gnus-split-methods)
10223         split-name)
10224     (if (not gnus-split-methods)
10225         ()
10226       (save-excursion
10227         (set-buffer gnus-article-buffer)
10228         (gnus-narrow-to-headers)
10229         (while methods
10230           (goto-char (point-min))
10231           (and (condition-case () 
10232                    (re-search-forward (car (car methods)) nil t)
10233                  (error nil))
10234                (setq split-name (cons (nth 1 (car methods)) split-name)))
10235           (setq methods (cdr methods)))
10236         (widen)))
10237     (cond ((null split-name)
10238            (read-file-name
10239             (concat prompt " (default "
10240                     (file-name-nondirectory default-name) ") ")
10241             (file-name-directory default-name)
10242             default-name))
10243           ((= 1 (length split-name))
10244            (read-file-name
10245             (concat prompt " (default " (car split-name) ") ")
10246             gnus-article-save-directory
10247             (concat gnus-article-save-directory (car split-name))))
10248           (t
10249            (setq split-name (mapcar (lambda (el) (list el))
10250                                     (nreverse split-name)))
10251            (let ((result (completing-read 
10252                           (concat prompt " ")
10253                           split-name nil nil)))
10254              (concat gnus-article-save-directory
10255                      (if (string= result "")
10256                          (car (car split-name))
10257                        result)))))))
10258
10259 (defun gnus-summary-save-in-rmail (&optional filename)
10260   "Append this article to Rmail file.
10261 Optional argument FILENAME specifies file name.
10262 Directory to save to is default to `gnus-article-save-directory' which
10263 is initialized from the SAVEDIR environment variable."
10264   (interactive)
10265   (gnus-set-global-variables)
10266   (let ((default-name
10267           (funcall gnus-rmail-save-name gnus-newsgroup-name
10268                    gnus-current-headers gnus-newsgroup-last-rmail)))
10269     (or filename
10270         (setq filename (gnus-read-save-file-name 
10271                         "Save in rmail file:" default-name)))
10272     (gnus-make-directory (file-name-directory filename))
10273     (gnus-eval-in-buffer-window 
10274      gnus-article-buffer
10275      (save-excursion
10276        (save-restriction
10277          (widen)
10278          (gnus-output-to-rmail filename))))
10279     ;; Remember the directory name to save articles
10280     (setq gnus-newsgroup-last-rmail filename)))
10281
10282 (defun gnus-summary-save-in-mail (&optional filename)
10283   "Append this article to Unix mail file.
10284 Optional argument FILENAME specifies file name.
10285 Directory to save to is default to `gnus-article-save-directory' which
10286 is initialized from the SAVEDIR environment variable."
10287   (interactive)
10288   (gnus-set-global-variables)
10289   (let ((default-name
10290           (funcall gnus-mail-save-name gnus-newsgroup-name
10291                    gnus-current-headers gnus-newsgroup-last-mail)))
10292     (or filename
10293         (setq filename (gnus-read-save-file-name 
10294                         "Save in Unix mail file:" default-name)))
10295     (setq filename
10296           (expand-file-name filename
10297                             (and default-name
10298                                  (file-name-directory default-name))))
10299     (gnus-make-directory (file-name-directory filename))
10300     (gnus-eval-in-buffer-window 
10301      gnus-article-buffer
10302      (save-excursion
10303        (save-restriction
10304          (widen)
10305          (if (and (file-readable-p filename) (rmail-file-p filename))
10306              (gnus-output-to-rmail filename)
10307            (rmail-output filename 1 t t)))))
10308     ;; Remember the directory name to save articles.
10309     (setq gnus-newsgroup-last-mail filename)))
10310
10311 (defun gnus-summary-save-in-file (&optional filename)
10312   "Append this article to file.
10313 Optional argument FILENAME specifies file name.
10314 Directory to save to is default to `gnus-article-save-directory' which
10315 is initialized from the SAVEDIR environment variable."
10316   (interactive)
10317   (gnus-set-global-variables)
10318   (let ((default-name
10319           (funcall gnus-file-save-name gnus-newsgroup-name
10320                    gnus-current-headers gnus-newsgroup-last-file)))
10321     (or filename
10322         (setq filename (gnus-read-save-file-name 
10323                         "Save in file:" default-name)))
10324     (gnus-make-directory (file-name-directory filename))
10325     (gnus-eval-in-buffer-window 
10326      gnus-article-buffer
10327      (save-excursion
10328        (save-restriction
10329          (widen)
10330          (gnus-output-to-file filename))))
10331     ;; Remember the directory name to save articles.
10332     (setq gnus-newsgroup-last-file filename)))
10333
10334 (defun gnus-summary-save-in-pipe (&optional command)
10335   "Pipe this article to subprocess."
10336   (interactive)
10337   (gnus-set-global-variables)
10338   (let ((command (read-string "Shell command on article: "
10339                               gnus-last-shell-command)))
10340     (if (string-equal command "")
10341         (setq command gnus-last-shell-command))
10342     (gnus-eval-in-buffer-window 
10343      gnus-article-buffer
10344      (save-restriction
10345        (widen)
10346        (shell-command-on-region (point-min) (point-max) command nil)))
10347     (setq gnus-last-shell-command command)))
10348
10349 ;; Summary extract commands
10350
10351 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10352   (let ((buffer-read-only nil)
10353         (article (gnus-summary-article-number))
10354         b)
10355     (or (gnus-summary-goto-subject article)
10356         (error (format "No such article: %d" article)))
10357     (or gnus-newsgroup-headers-hashtb-by-number
10358         (gnus-make-headers-hashtable-by-number))
10359     (gnus-summary-position-cursor)
10360     ;; If all commands are to be bunched up on one line, we collect
10361     ;; them here.  
10362     (if gnus-view-pseudos-separately
10363         ()
10364       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10365             files action)
10366         (while ps
10367           (setq action (cdr (assq 'action (car ps))))
10368           (setq files (list (cdr (assq 'name (car ps)))))
10369           (while (and ps (cdr ps)
10370                       (string= (or action "1")
10371                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10372             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10373             (setcdr ps (cdr (cdr ps))))
10374           (if (not files)
10375               ()
10376             (if (not (string-match "%s" action))
10377                 (setq files (cons " " files)))
10378             (setq files (cons " " files))
10379             (and (assq 'execute (car ps))
10380                  (setcdr (assq 'execute (car ps))
10381                          (funcall (if (string-match "%s" action)
10382                                       'format 'concat)
10383                                   action 
10384                                   (mapconcat (lambda (f) f) files " ")))))
10385           (setq ps (cdr ps)))))
10386     (if (and gnus-view-pseudos (not not-view))
10387         (while pslist
10388           (and (assq 'execute (car pslist))
10389                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10390                                      (eq gnus-view-pseudos 'not-confirm)))
10391           (setq pslist (cdr pslist)))
10392       (save-excursion
10393         (while pslist
10394           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10395                                          (gnus-summary-article-number)))
10396           (forward-line 1)
10397           (setq b (point))
10398           (insert "          " (file-name-nondirectory 
10399                                 (cdr (assq 'name (car pslist))))
10400                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10401           (add-text-properties 
10402            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10403                           'gnus-mark gnus-unread-mark 
10404                           'gnus-level 0
10405                           'gnus-pseudo (car pslist)))
10406           (forward-line -1)
10407           (gnus-sethash (int-to-string gnus-reffed-article-number)
10408                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10409           (setq gnus-newsgroup-unreads
10410                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10411           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10412           (setq pslist (cdr pslist)))))))
10413
10414 (defun gnus-pseudos< (p1 p2)
10415   (let ((c1 (cdr (assq 'action p1)))
10416         (c2 (cdr (assq 'action p2))))
10417     (and c1 c2 (string< c1 c2))))
10418
10419 (defun gnus-request-pseudo-article (props)
10420   (cond ((assq 'execute props)
10421          (gnus-execute-command (cdr (assq 'execute props)))))
10422   (let ((gnus-current-article (gnus-summary-article-number)))
10423     (run-hooks 'gnus-mark-article-hook)))
10424
10425 (defun gnus-execute-command (command &optional automatic)
10426   (save-excursion
10427     (gnus-article-setup-buffer)
10428     (set-buffer gnus-article-buffer)
10429     (let ((command (if automatic command (read-string "Command: " command)))
10430           (buffer-read-only nil))
10431       (erase-buffer)
10432       (insert "$ " command "\n\n")
10433       (if gnus-view-pseudo-asynchronously
10434           (start-process "gnus-execute" nil "sh" "-c" command)
10435         (call-process "sh" nil t nil "-c" command)))))
10436
10437 (defun gnus-copy-file (file &optional to)
10438   "Copy FILE to TO."
10439   (interactive
10440    (list (read-file-name "Copy file: " default-directory)
10441          (read-file-name "Copy file to: " default-directory)))
10442   (gnus-set-global-variables)
10443   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10444   (and (file-directory-p to) 
10445        (setq to (concat (file-name-as-directory to)
10446                         (file-name-nondirectory file))))
10447   (copy-file file to))
10448
10449 ;; Summary kill commands.
10450
10451 (defun gnus-summary-edit-global-kill (article)
10452   "Edit the \"global\" kill file."
10453   (interactive (list (gnus-summary-article-number)))
10454   (gnus-set-global-variables)
10455   (gnus-group-edit-global-kill article))
10456
10457 (defun gnus-summary-edit-local-kill ()
10458   "Edit a local kill file applied to the current newsgroup."
10459   (interactive)
10460   (gnus-set-global-variables)
10461   (setq gnus-current-headers 
10462         (gnus-gethash 
10463          (int-to-string (gnus-summary-article-number))
10464          gnus-newsgroup-headers-hashtb-by-number))
10465   (gnus-set-global-variables)
10466   (gnus-group-edit-local-kill 
10467    (gnus-summary-article-number) gnus-newsgroup-name))
10468
10469 \f
10470 ;;;
10471 ;;; Gnus article mode
10472 ;;;
10473
10474 (put 'gnus-article-mode 'mode-class 'special)
10475
10476 (if gnus-article-mode-map
10477     nil
10478   (setq gnus-article-mode-map (make-keymap))
10479   (suppress-keymap gnus-article-mode-map)
10480   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10481   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10482   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10483   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10484   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10485   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10486   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10487   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10488   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10489   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10490   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10491   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10492   
10493   ;; Duplicate almost all summary keystrokes in the article mode map.
10494   (let ((commands 
10495          (list 
10496           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10497           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10498           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10499           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10500           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10501           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10502           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10503           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10504           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10505           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10506           "\C-c\C-v\C-v" "\C-d" "v" 
10507 ;;        "Mt" "M!" "Md" "Mr"
10508 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10509 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10510 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10511 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10512 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10513 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10514 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10515 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10516 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10517 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10518 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10519           )))
10520     (while commands
10521       (define-key gnus-article-mode-map (car commands) 
10522         'gnus-article-summary-command)
10523       (setq commands (cdr commands))))
10524
10525   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10526 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10527                          "=" "n"  "^" "\M-^")))
10528     (while commands
10529       (define-key gnus-article-mode-map (car commands) 
10530         'gnus-article-summary-command-nosave)
10531       (setq commands (cdr commands)))))
10532
10533
10534 (defun gnus-article-mode ()
10535   "Major mode for displaying an article.
10536
10537 All normal editing commands are switched off.
10538
10539 The following commands are available:
10540
10541 \\<gnus-article-mode-map>
10542 \\[gnus-article-next-page]\t Scroll the article one page forwards
10543 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10544 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10545 \\[gnus-article-show-summary]\t Display the summary buffer
10546 \\[gnus-article-mail]\t Send a reply to the address near point
10547 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10548 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10549 \\[gnus-info-find-node]\t Go to the Gnus info node"
10550   (interactive)
10551   (if gnus-visual (gnus-article-make-menu-bar))
10552   (kill-all-local-variables)
10553   (setq mode-line-modified "-- ")
10554   (make-local-variable 'mode-line-format)
10555   (setq mode-line-format (copy-sequence mode-line-format))
10556   (and (equal (nth 3 mode-line-format) "   ")
10557        (setcar (nthcdr 3 mode-line-format) ""))
10558   (setq mode-name "Article")
10559   (setq major-mode 'gnus-article-mode)
10560   (make-local-variable 'minor-mode-alist)
10561   (or (assq 'gnus-show-mime minor-mode-alist)
10562       (setq minor-mode-alist
10563             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10564   (use-local-map gnus-article-mode-map)
10565   (make-local-variable 'page-delimiter)
10566   (setq page-delimiter gnus-page-delimiter)
10567   (buffer-disable-undo (current-buffer))
10568   (setq buffer-read-only t)             ;Disable modification
10569   (run-hooks 'gnus-article-mode-hook))
10570
10571 (defun gnus-article-setup-buffer ()
10572   "Initialize article mode buffer."
10573   (if (get-buffer gnus-article-buffer)
10574       (save-excursion
10575         (set-buffer gnus-article-buffer)
10576         (buffer-disable-undo (current-buffer))
10577         (setq buffer-read-only t)
10578         (gnus-add-current-to-buffer-list)
10579         (or (eq major-mode 'gnus-article-mode)
10580             (gnus-article-mode)))
10581     (save-excursion
10582       (set-buffer (get-buffer-create gnus-article-buffer))
10583       (gnus-add-current-to-buffer-list)
10584       (gnus-article-mode))))
10585
10586 ;; Set article window start at LINE, where LINE is the number of lines
10587 ;; from the head of the article.
10588 (defun gnus-article-set-window-start (&optional line)
10589   (set-window-start 
10590    (get-buffer-window gnus-article-buffer)
10591    (save-excursion
10592      (set-buffer gnus-article-buffer)
10593      (goto-char (point-min))
10594      (if (not line)
10595          (point-min)
10596        (gnus-message 6 "Moved to bookmark")
10597        (search-forward "\n\n" nil t)
10598        (forward-line line)
10599        (point)))))
10600
10601 (defun gnus-request-article-this-buffer (article group)
10602   "Get an article and insert it into this buffer."
10603   (setq group (or group gnus-newsgroup-name))
10604
10605   ;; Open server if it has closed.
10606   (gnus-check-news-server (gnus-find-method-for-group group))
10607
10608   ;; Using `gnus-request-article' directly will insert the article into
10609   ;; `nntp-server-buffer' - so we'll save some time by not having to
10610   ;; copy it from the server buffer into the article buffer.
10611
10612   ;; We only request an article by message-id when we do not have the
10613   ;; headers for it, so we'll have to get those.
10614   (and (stringp article) 
10615        (let ((gnus-override-method gnus-refer-article-method))
10616          (gnus-read-header article)))
10617
10618   ;; If the article number is negative, that means that this article
10619   ;; doesn't belong in this newsgroup (possibly), so we find its
10620   ;; message-id and request it by id instead of number.
10621   (if (not (numberp article))
10622       ()
10623     (save-excursion
10624       (set-buffer gnus-summary-buffer)
10625       (let ((header (gnus-get-header-by-num article)))
10626         (if (< article 0)
10627             (if (vectorp header)
10628                 ;; It's a real article.
10629                 (setq article (header-id header))
10630               ;; It is an extracted pseudo-article.
10631               (setq article 'pseudo)
10632               (gnus-request-pseudo-article header)))
10633
10634         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10635           (if (not (eq (car method) 'nneething))
10636               ()
10637             (let ((dir (concat (file-name-as-directory (nth 1 method))
10638                                (header-subject header))))
10639               (if (file-directory-p dir)
10640                   (progn
10641                     (setq article 'nneething)
10642                     (gnus-group-enter-directory dir)))))))))
10643
10644   ;; Check the cache.
10645   (if (and gnus-use-cache
10646            (numberp article)
10647            (gnus-cache-request-article article group))
10648       'article
10649     ;; Get the article and into the article buffer.
10650     (if (or (stringp article) (numberp article))
10651         (progn
10652           (erase-buffer)
10653           (let ((gnus-override-method 
10654                  (and (stringp article) gnus-refer-article-method)))
10655             (and (gnus-request-article article group (current-buffer))
10656                  'article)))
10657       article)))
10658
10659 (defun gnus-read-header (id)
10660   "Read the headers of article ID and enter them into the Gnus system."
10661   (or gnus-newsgroup-headers-hashtb-by-number
10662       (gnus-make-headers-hashtable-by-number))
10663   (let (header)
10664     (if (not (setq header 
10665                    (car (if (let ((gnus-nov-is-evil t))
10666                               (gnus-retrieve-headers 
10667                                (list id) gnus-newsgroup-name))
10668                             (gnus-get-newsgroup-headers)))))
10669         nil
10670       (if (stringp id)
10671           (header-set-number header gnus-reffed-article-number))
10672       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10673       (gnus-sethash (int-to-string (header-number header)) header
10674                     gnus-newsgroup-headers-hashtb-by-number)
10675       (if (stringp id)
10676           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10677       (setq gnus-current-headers header)
10678       header)))
10679
10680 (defun gnus-article-prepare (article &optional all-headers header)
10681   "Prepare ARTICLE in article mode buffer.
10682 ARTICLE should either be an article number or a Message-ID.
10683 If ARTICLE is an id, HEADER should be the article headers.
10684 If ALL-HEADERS is non-nil, no headers are hidden."
10685   (save-excursion
10686     ;; Make sure we start in a summary buffer.
10687     (or (eq major-mode 'gnus-summary-mode)
10688         (set-buffer gnus-summary-buffer))
10689     (setq gnus-summary-buffer (current-buffer))
10690     ;; Make sure the connection to the server is alive.
10691     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10692         (progn
10693           (gnus-check-news-server 
10694            (gnus-find-method-for-group gnus-newsgroup-name))
10695           (gnus-request-group gnus-newsgroup-name t)))
10696     (or gnus-newsgroup-headers-hashtb-by-number
10697         (gnus-make-headers-hashtable-by-number))
10698     (let* ((article (if header (header-number header) article))
10699            (summary-buffer (current-buffer))
10700            (internal-hook gnus-article-internal-prepare-hook)
10701            (group gnus-newsgroup-name)
10702            result)
10703       (save-excursion
10704         (gnus-article-setup-buffer)
10705         (set-buffer gnus-article-buffer)
10706         (if (not (setq result (let ((buffer-read-only nil))
10707                                 (gnus-request-article-this-buffer 
10708                                  article group))))
10709             ;; There is no such article.
10710             (save-excursion
10711               (if (not (numberp article))
10712                   ()
10713                 (setq gnus-article-current 
10714                       (cons gnus-newsgroup-name article))
10715                 (set-buffer gnus-summary-buffer)
10716                 (setq gnus-current-article article)
10717                 (gnus-summary-mark-article article gnus-canceled-mark))
10718               (gnus-message 1 "No such article (may be canceled)")
10719               (ding)
10720               nil)
10721           (if (or (eq result 'pseudo) (eq result 'nneething))
10722               (progn
10723                 (save-excursion
10724                   (set-buffer summary-buffer)
10725                   (setq gnus-last-article gnus-current-article
10726                         gnus-newsgroup-history (cons gnus-current-article
10727                                                      gnus-newsgroup-history)
10728                         gnus-current-article 0
10729                         gnus-current-headers nil
10730                         gnus-article-current nil)
10731                   (if (eq result 'nneething)
10732                       (gnus-configure-windows 'summary)
10733                     (gnus-configure-windows 'article))
10734                   (gnus-set-global-variables))
10735                 (gnus-set-mode-line 'article))
10736             ;; The result from the `request' was an actual article -
10737             ;; or at least some text that is now displayed in the
10738             ;; article buffer.
10739             (if (and (numberp article)
10740                      (not (eq article gnus-current-article)))
10741                 ;; Seems like a new article has been selected.
10742                 ;; `gnus-current-article' must be an article number.
10743                 (save-excursion
10744                   (set-buffer summary-buffer)
10745                   (setq gnus-last-article gnus-current-article
10746                         gnus-newsgroup-history (cons gnus-current-article
10747                                                      gnus-newsgroup-history)
10748                         gnus-current-article article
10749                         gnus-current-headers 
10750                         (gnus-get-header-by-num gnus-current-article)
10751                         gnus-article-current 
10752                         (cons gnus-newsgroup-name gnus-current-article))
10753                   (gnus-summary-show-thread)
10754                   (run-hooks 'gnus-mark-article-hook)
10755                   (gnus-set-mode-line 'summary)
10756                   (and gnus-visual 
10757                        (run-hooks 'gnus-visual-mark-article-hook))
10758                   ;; Set the global newsgroup variables here.
10759                   ;; Suggested by Jim Sisolak
10760                   ;; <sisolak@trans4.neep.wisc.edu>.
10761                   (gnus-set-global-variables)
10762                   (setq gnus-have-all-headers 
10763                         (or all-headers gnus-show-all-headers))
10764                   (and gnus-use-cache 
10765                        (vectorp (gnus-get-header-by-number article))
10766                        (gnus-cache-possibly-enter-article
10767                         group article
10768                         (gnus-get-header-by-number article)
10769                         (memq article gnus-newsgroup-marked)
10770                         (memq article gnus-newsgroup-dormant)
10771                         (memq article gnus-newsgroup-unreads)))))
10772             ;; Hooks for getting information from the article.
10773             ;; This hook must be called before being narrowed.
10774             (let (buffer-read-only)
10775               (run-hooks 'internal-hook)
10776               (run-hooks 'gnus-article-prepare-hook)
10777               ;; Decode MIME message.
10778               (if (and gnus-show-mime
10779                        (or (not gnus-strict-mime)
10780                            (gnus-fetch-field "Mime-Version")))
10781                   (funcall gnus-show-mime-method))
10782               ;; Perform the article display hooks.
10783               (run-hooks 'gnus-article-display-hook))
10784             ;; Do page break.
10785             (goto-char (point-min))
10786             (and gnus-break-pages (gnus-narrow-to-page))
10787             (gnus-set-mode-line 'article)
10788             (gnus-configure-windows 'article)
10789             (goto-char (point-min))
10790             t))))))
10791
10792 (defun gnus-article-show-all-headers ()
10793   "Show all article headers in article mode buffer."
10794   (save-excursion 
10795     (gnus-article-setup-buffer)
10796     (set-buffer gnus-article-buffer)
10797     (let ((buffer-read-only nil))
10798       (remove-text-properties (point-min) (point-max) 
10799                               gnus-hidden-properties))))
10800
10801 (defun gnus-article-hide-headers-if-wanted ()
10802   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10803 Provided for backwards compatability."
10804   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10805       (gnus-article-hide-headers)))
10806
10807 (defun gnus-article-hide-headers (&optional delete)
10808   "Hide unwanted headers and possibly sort them as well."
10809   (interactive "P")
10810   (save-excursion
10811     (set-buffer gnus-article-buffer)
10812     (save-restriction
10813       (let ((sorted gnus-sorted-header-list)
10814             (buffer-read-only nil)
10815             want-list beg want-l)
10816         ;; First we narrow to just the headers.
10817         (widen)
10818         (goto-char (point-min))
10819         ;; Hide any "From " lines at the beginning of (mail) articles. 
10820         (while (looking-at "From ")
10821           (forward-line 1))
10822         (if (bobp) 
10823             (add-text-properties (point-min) (point) gnus-hidden-properties))
10824         ;; Then treat the rest of the header lines.
10825         (narrow-to-region 
10826          (point) 
10827          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10828         ;; Then we use the two regular expressions
10829         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10830         ;; select which header lines is to remain visible in the
10831         ;; article buffer.
10832         (goto-char (point-min))
10833         (while (re-search-forward "^[^ \t]*:" nil t)
10834           (beginning-of-line)
10835           ;; We add the headers we want to keep to a list and delete
10836           ;; them from the buffer.
10837           (if (or (and (stringp gnus-visible-headers)
10838                        (looking-at gnus-visible-headers))
10839                   (and (not (stringp gnus-visible-headers))
10840                        (stringp gnus-ignored-headers)
10841                        (not (looking-at gnus-ignored-headers))))
10842               (progn
10843                 (setq beg (point))
10844                 (forward-line 1)
10845                 ;; Be sure to get multi-line headers...
10846                 (re-search-forward "^[^ \t]*:" nil t)
10847                 (beginning-of-line)
10848                 (setq want-list 
10849                       (cons (buffer-substring beg (point)) want-list))
10850                 (delete-region beg (point))
10851                 (goto-char beg))
10852             (forward-line 1)))
10853         ;; Next we perform the sorting by looking at
10854         ;; `gnus-sorted-header-list'. 
10855         (goto-char (point-min))
10856         (while (and sorted want-list)
10857           (setq want-l want-list)
10858           (while (and want-l
10859                       (not (string-match (car sorted) (car want-l))))
10860             (setq want-l (cdr want-l)))
10861           (if want-l 
10862               (progn
10863                 (insert (car want-l))
10864                 (setq want-list (delq (car want-l) want-list))))
10865           (setq sorted (cdr sorted)))
10866         ;; Any headers that were not matched by the sorted list we
10867         ;; just tack on the end of the visible header list.
10868         (while want-list
10869           (insert (car want-list))
10870           (setq want-list (cdr want-list)))
10871         ;; And finally we make the unwanted headers invisible.
10872         (if delete
10873             (delete-region (point) (point-max))
10874           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10875           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10876
10877 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10878 (defun gnus-article-treat-overstrike ()
10879   "Translate overstrikes into bold text."
10880   (interactive)
10881   (save-excursion
10882     (set-buffer gnus-article-buffer)
10883     (let ((buffer-read-only nil))
10884       (while (search-forward "\b" nil t)
10885         (let ((next (following-char))
10886               (previous (char-after (- (point) 2))))
10887           (cond ((eq next previous)
10888                  (delete-region (- (point) 2) (point))
10889                  (put-text-property (point) (1+ (point))
10890                                     'face 'bold))
10891                 ((eq next ?_)
10892                  (delete-region (1- (point)) (1+ (point)))
10893                  (put-text-property (1- (point)) (point)
10894                                     'face 'underline))
10895                 ((eq previous ?_)
10896                  (delete-region (- (point) 2) (point))
10897                  (put-text-property (point) (1+ (point))
10898                                     'face 'underline))))))))
10899
10900 (defun gnus-article-word-wrap ()
10901   "Format too long lines."
10902   (interactive)
10903   (save-excursion
10904     (set-buffer gnus-article-buffer)
10905     (let ((buffer-read-only nil))
10906       (goto-char (point-min))
10907       (search-forward "\n\n" nil t)
10908       (end-of-line 1)
10909       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10910             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10911             (adaptive-fill-mode t))
10912         (while (not (eobp))
10913           (and (>= (current-column) (min fill-column (window-width)))
10914                (/= (preceding-char) ?:)
10915                (fill-paragraph nil))
10916           (end-of-line 2))))))
10917
10918 (defun gnus-article-remove-cr ()
10919   "Remove carriage returns from an article."
10920   (interactive)
10921   (save-excursion
10922     (set-buffer gnus-article-buffer)
10923     (let ((buffer-read-only nil))
10924       (goto-char (point-min))
10925       (while (search-forward "\r" nil t)
10926         (replace-match "" t t)))))
10927
10928 (defun gnus-article-display-x-face (&optional force)
10929   "Look for an X-Face header and display it if present."
10930   (interactive (list 'force))
10931   (save-excursion
10932     (set-buffer gnus-article-buffer)
10933     (let ((inhibit-point-motion-hooks t)
10934           (case-fold-search nil))
10935       (save-restriction
10936         (goto-char (point-min))
10937         (search-forward "\n\n")
10938         (narrow-to-region (point-min) (point))
10939         (goto-char (point-min))
10940         (if (or (not gnus-article-x-face-command)
10941                 (and (not force)
10942                      (or (not gnus-article-x-face-too-ugly)
10943                          (string-match gnus-article-x-face-too-ugly
10944                                        (mail-fetch-field "from"))))
10945                 (progn
10946                   (goto-char (point-min))
10947                   (not (re-search-forward "^X-Face: " nil t))))
10948             nil
10949           (let ((beg (point))
10950                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10951             (if (symbolp gnus-article-x-face-command)
10952                 (and (or (fboundp gnus-article-x-face-command)
10953                          (error "%s is not a function"
10954                                 gnus-article-x-face-command))
10955                      (funcall gnus-article-x-face-command beg end))
10956               (call-process-region beg end "sh" nil 0 nil
10957                                    "-c" gnus-article-x-face-command))))))))
10958
10959 (defun gnus-article-de-quoted-unreadable (&optional force)
10960   "Do a naïve translation of a quoted-printable-encoded article.
10961 This is in no way, shape or form meant as a replacement for real MIME
10962 processing, but is simply a stop-gap measure until MIME support is
10963 written.
10964 If FORCE, decode the article whether it is marked as quoted-printable
10965 or not." 
10966   (interactive (list 'force))
10967   (save-excursion
10968     (set-buffer gnus-article-buffer)
10969     (let ((case-fold-search t)
10970           (buffer-read-only nil)
10971           (type (gnus-fetch-field "content-transfer-encoding")))
10972       (if (or force (and type (string-match "quoted-printable" type)))
10973           (progn
10974             (goto-char (point-min))
10975             (search-forward "\n\n" nil 'move)
10976             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10977
10978 (defun gnus-mime-decode-quoted-printable (from to)
10979   ;; Decode quoted-printable from region between FROM and TO.
10980   (save-excursion
10981     (goto-char from)
10982     (while (search-forward "=" to t)
10983       (cond ((eq (following-char) ?\n)
10984              (delete-char -1)
10985              (delete-char 1))
10986             ((looking-at "[0-9A-F][0-9A-F]")
10987              (delete-char -1)
10988              (insert (hexl-hex-string-to-integer
10989                       (buffer-substring (point) (+ 2 (point)))))
10990              (delete-char 2))
10991             ((looking-at "=")
10992              (delete-char 1))
10993             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10994
10995 (defvar gnus-article-time-units
10996   (list (cons 'year (* 365.25 24 60 60))
10997         (cons 'week (* 7 24 60 60))
10998         (cons 'day (* 24 60 60))
10999         (cons 'hour (* 60 60))
11000         (cons 'minute 60)
11001         (cons 'second 1)))
11002
11003 (defun gnus-article-date-ut (&optional type)
11004   "Convert DATE date to universal time in the current article.
11005 If TYPE is `local', convert to local time; if it is `lapsed', output
11006 how much time has lapsed since DATE."
11007   (interactive (list 'ut))
11008   (let ((date (header-date (or gnus-current-headers 
11009                                (gnus-get-header-by-number
11010                                 (gnus-summary-article-number))"")))
11011         (date-regexp "^Date: \\|^X-Sent: "))
11012     (if (or (not date)
11013             (string= date ""))
11014         ()
11015       (save-excursion
11016         (set-buffer gnus-article-buffer)
11017         (let ((buffer-read-only nil))
11018           (goto-char (point-min))
11019           (if (and (re-search-forward date-regexp nil t)
11020                    (progn 
11021                      (beginning-of-line)
11022                      (looking-at date-regexp)))
11023               (delete-region (gnus-point-at-bol)
11024                              (progn (end-of-line) (1+ (point))))
11025             (goto-char (point-min))
11026             (goto-char (- (search-forward "\n\n") 2)))
11027           (insert
11028            (cond 
11029             ((eq type 'local)
11030              (concat "Date: " (condition-case ()
11031                                   (timezone-make-date-arpa-standard date)
11032                                 (error date))
11033                      "\n"))
11034             ((eq type 'ut)
11035              (concat "Date: "
11036                      (condition-case ()
11037                          (timezone-make-date-arpa-standard date nil "UT")
11038                        (error date))
11039                      "\n"))
11040             ((eq type 'lapsed)
11041              ;; If the date is seriously mangled, the timezone
11042              ;; functions are liable to bug out, so we condition-case
11043              ;; the entire thing.  
11044              (let* ((real-sec (condition-case ()
11045                                   (- (gnus-seconds-since-epoch 
11046                                       (timezone-make-date-arpa-standard
11047                                        (current-time-string) 
11048                                        (current-time-zone) "UT"))
11049                                      (gnus-seconds-since-epoch 
11050                                       (timezone-make-date-arpa-standard 
11051                                        date nil "UT")))
11052                                 (error 0)))
11053                     (sec (abs real-sec))
11054                     num prev)
11055                (if (zerop sec)
11056                    "X-Sent: Now\n"
11057                  (concat
11058                   "X-Sent: "
11059                   (mapconcat 
11060                    (lambda (unit)
11061                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11062                          ""
11063                        (setq sec (- sec (* num (cdr unit))))
11064                        (prog1
11065                            (concat (if prev ", " "") (int-to-string 
11066                                                       (floor num))
11067                                    " " (symbol-name (car unit))
11068                                    (if (> num 1) "s" ""))
11069                          (setq prev t))))
11070                    gnus-article-time-units "")
11071                   (if (> real-sec 0)
11072                       " ago\n"
11073                     " in the future\n")))))
11074             (t
11075              (error "Unknown conversion type: %s" type)))))))))
11076
11077 (defun gnus-article-date-local ()
11078   "Convert the current article date to the local timezone."
11079   (interactive)
11080   (gnus-article-date-ut 'local))
11081
11082 (defun gnus-article-date-lapsed ()
11083   "Convert the current article date to time lapsed since it was sent."
11084   (interactive)
11085   (gnus-article-date-ut 'lapsed))
11086
11087 (defun gnus-article-maybe-highlight ()
11088   (if gnus-visual (gnus-article-highlight)))
11089
11090 ;; Article savers.
11091
11092 (defun gnus-output-to-rmail (file-name)
11093   "Append the current article to an Rmail file named FILE-NAME."
11094   (require 'rmail)
11095   ;; Most of these codes are borrowed from rmailout.el.
11096   (setq file-name (expand-file-name file-name))
11097   (setq rmail-default-rmail-file file-name)
11098   (let ((artbuf (current-buffer))
11099         (tmpbuf (get-buffer-create " *Gnus-output*")))
11100     (save-excursion
11101       (or (get-file-buffer file-name)
11102           (file-exists-p file-name)
11103           (if (gnus-yes-or-no-p
11104                (concat "\"" file-name "\" does not exist, create it? "))
11105               (let ((file-buffer (create-file-buffer file-name)))
11106                 (save-excursion
11107                   (set-buffer file-buffer)
11108                   (rmail-insert-rmail-file-header)
11109                   (let ((require-final-newline nil))
11110                     (write-region (point-min) (point-max) file-name t 1)))
11111                 (kill-buffer file-buffer))
11112             (error "Output file does not exist")))
11113       (set-buffer tmpbuf)
11114       (buffer-disable-undo (current-buffer))
11115       (erase-buffer)
11116       (insert-buffer-substring artbuf)
11117       (gnus-convert-article-to-rmail)
11118       ;; Decide whether to append to a file or to an Emacs buffer.
11119       (let ((outbuf (get-file-buffer file-name)))
11120         (if (not outbuf)
11121             (append-to-file (point-min) (point-max) file-name)
11122           ;; File has been visited, in buffer OUTBUF.
11123           (set-buffer outbuf)
11124           (let ((buffer-read-only nil)
11125                 (msg (and (boundp 'rmail-current-message)
11126                           (symbol-value 'rmail-current-message))))
11127             ;; If MSG is non-nil, buffer is in RMAIL mode.
11128             (if msg
11129                 (progn (widen)
11130                        (narrow-to-region (point-max) (point-max))))
11131             (insert-buffer-substring tmpbuf)
11132             (if msg
11133                 (progn
11134                   (goto-char (point-min))
11135                   (widen)
11136                   (search-backward "\^_")
11137                   (narrow-to-region (point) (point-max))
11138                   (goto-char (1+ (point-min)))
11139                   (rmail-count-new-messages t)
11140                   (rmail-show-message msg)))))))
11141     (kill-buffer tmpbuf)))
11142
11143 (defun gnus-output-to-file (file-name)
11144   "Append the current article to a file named FILE-NAME."
11145   (setq file-name (expand-file-name file-name))
11146   (let ((artbuf (current-buffer))
11147         (tmpbuf (get-buffer-create " *Gnus-output*")))
11148     (save-excursion
11149       (set-buffer tmpbuf)
11150       (buffer-disable-undo (current-buffer))
11151       (erase-buffer)
11152       (insert-buffer-substring artbuf)
11153       ;; Append newline at end of the buffer as separator, and then
11154       ;; save it to file.
11155       (goto-char (point-max))
11156       (insert "\n")
11157       (append-to-file (point-min) (point-max) file-name))
11158     (kill-buffer tmpbuf)))
11159
11160 (defun gnus-convert-article-to-rmail ()
11161   "Convert article in current buffer to Rmail message format."
11162   (let ((buffer-read-only nil))
11163     ;; Convert article directly into Babyl format.
11164     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11165     (goto-char (point-min))
11166     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11167     (while (search-forward "\n\^_" nil t) ;single char
11168       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11169     (goto-char (point-max))
11170     (insert "\^_")))
11171
11172 (defun gnus-narrow-to-page (&optional arg)
11173   "Make text outside current page invisible except for page delimiter.
11174 A numeric arg specifies to move forward or backward by that many pages,
11175 thus showing a page other than the one point was originally in."
11176   (interactive "P")
11177   (setq arg (if arg (prefix-numeric-value arg) 0))
11178   (save-excursion
11179     (forward-page -1)                   ;Beginning of current page.
11180     (widen)
11181     (if (> arg 0)
11182         (forward-page arg)
11183       (if (< arg 0)
11184           (forward-page (1- arg))))
11185     ;; Find the end of the page.
11186     (forward-page)
11187     ;; If we stopped due to end of buffer, stay there.
11188     ;; If we stopped after a page delimiter, put end of restriction
11189     ;; at the beginning of that line.
11190     ;; These are commented out.
11191     ;;    (if (save-excursion (beginning-of-line)
11192     ;;                  (looking-at page-delimiter))
11193     ;;  (beginning-of-line))
11194     (narrow-to-region (point)
11195                       (progn
11196                         ;; Find the top of the page.
11197                         (forward-page -1)
11198                         ;; If we found beginning of buffer, stay there.
11199                         ;; If extra text follows page delimiter on same line,
11200                         ;; include it.
11201                         ;; Otherwise, show text starting with following line.
11202                         (if (and (eolp) (not (bobp)))
11203                             (forward-line 1))
11204                         (point)))))
11205
11206 (defun gnus-gmt-to-local ()
11207   "Rewrite Date header described in GMT to local in current buffer.
11208 Intended to be used with gnus-article-prepare-hook."
11209   (save-excursion
11210     (save-restriction
11211       (widen)
11212       (goto-char (point-min))
11213       (narrow-to-region (point-min)
11214                         (progn (search-forward "\n\n" nil 'move) (point)))
11215       (goto-char (point-min))
11216       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11217           (let ((buffer-read-only nil)
11218                 (date (buffer-substring-no-properties
11219                        (match-beginning 1) (match-end 1))))
11220             (delete-region (match-beginning 1) (match-end 1))
11221             (insert
11222              (timezone-make-date-arpa-standard 
11223               date nil (current-time-zone))))))))
11224
11225
11226 ;; Article mode commands
11227
11228 (defun gnus-article-next-page (&optional lines)
11229   "Show next page of current article.
11230 If end of article, return non-nil. Otherwise return nil.
11231 Argument LINES specifies lines to be scrolled up."
11232   (interactive "P")
11233   (move-to-window-line -1)
11234   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11235   (if (save-excursion
11236         (end-of-line)
11237         (and (pos-visible-in-window-p)  ;Not continuation line.
11238              (eobp)))
11239       ;; Nothing in this page.
11240       (if (or (not gnus-break-pages)
11241               (save-excursion
11242                 (save-restriction
11243                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11244           t                             ;Nothing more.
11245         (gnus-narrow-to-page 1)         ;Go to next page.
11246         nil)
11247     ;; More in this page.
11248     (condition-case ()
11249         (scroll-up lines)
11250       (end-of-buffer
11251        ;; Long lines may cause an end-of-buffer error.
11252        (goto-char (point-max))))
11253     nil))
11254
11255 (defun gnus-article-prev-page (&optional lines)
11256   "Show previous page of current article.
11257 Argument LINES specifies lines to be scrolled down."
11258   (interactive "P")
11259   (move-to-window-line 0)
11260   (if (and gnus-break-pages
11261            (bobp)
11262            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11263       (progn
11264         (gnus-narrow-to-page -1) ;Go to previous page.
11265         (goto-char (point-max))
11266         (recenter -1))
11267     (scroll-down lines)))
11268
11269 (defun gnus-article-refer-article ()
11270   "Read article specified by message-id around point."
11271   (interactive)
11272   (search-forward ">" nil t)    ;Move point to end of "<....>".
11273   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11274       (let ((message-id
11275              (buffer-substring (match-beginning 1) (match-end 1))))
11276         (set-buffer gnus-summary-buffer)
11277         (gnus-summary-refer-article message-id))
11278     (error "No references around point")))
11279
11280 (defun gnus-article-show-summary ()
11281   "Reconfigure windows to show summary buffer."
11282   (interactive)
11283   (gnus-configure-windows 'article)
11284   (gnus-summary-goto-subject gnus-current-article))
11285
11286 (defun gnus-article-describe-briefly ()
11287   "Describe article mode commands briefly."
11288   (interactive)
11289   (gnus-message 6
11290    (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")))
11291
11292 (defun gnus-article-summary-command ()
11293   "Execute the last keystroke in the summary buffer."
11294   (interactive)
11295   (let ((obuf (current-buffer))
11296         (owin (current-window-configuration))
11297         func)
11298     (switch-to-buffer gnus-summary-buffer 'norecord)
11299     (setq func (lookup-key (current-local-map) (this-command-keys)))
11300     (call-interactively func)
11301     (set-buffer obuf)
11302     (set-window-configuration owin)
11303     (set-window-point (get-buffer-window (current-buffer)) (point))))
11304
11305 (defun gnus-article-summary-command-nosave ()
11306   "Execute the last keystroke in the summary buffer."
11307   (interactive)
11308   (let (func)
11309     (pop-to-buffer gnus-summary-buffer 'norecord)
11310     (setq func (lookup-key (current-local-map) (this-command-keys)))
11311     (call-interactively func)))
11312
11313 \f
11314 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11315
11316 ;;;###autoload
11317 (defalias 'gnus-batch-kill 'gnus-batch-score)
11318 ;;;###autoload
11319 (defun gnus-batch-score ()
11320   "Run batched scoring.
11321 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11322 Newsgroups is a list of strings in Bnews format.  If you want to score
11323 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11324 score the alt hierarchy, you'd say \"!alt.all\"."
11325   (interactive)
11326   (let* ((yes-and-no
11327           (gnus-newsrc-parse-options
11328            (apply (function concat)
11329                   (mapcar (lambda (g) (concat g " "))
11330                           command-line-args-left))))
11331          (gnus-expert-user t)
11332          (nnmail-spool-file nil)
11333          (gnus-use-dribble-file nil)
11334          (yes (car yes-and-no))
11335          (no (cdr yes-and-no))
11336          group newsrc entry
11337          ;; Disable verbose message.
11338          gnus-novice-user gnus-large-newsgroup)
11339     ;; Eat all arguments.
11340     (setq command-line-args-left nil)
11341     ;; Start Gnus.
11342     (gnus)
11343     ;; Apply kills to specified newsgroups in command line arguments.
11344     (setq newsrc (cdr gnus-newsrc-alist))
11345     (while newsrc
11346       (setq group (car (car newsrc)))
11347       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11348       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11349                (and (car entry)
11350                     (or (eq (car entry) t)
11351                         (not (zerop (car entry)))))
11352                (if yes (string-match yes group) t)
11353                (or (null no) (not (string-match no group))))
11354           (progn
11355             (gnus-summary-read-group group nil t)
11356             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11357                  (gnus-summary-exit))))
11358       (setq newsrc (cdr newsrc)))
11359     ;; Exit Emacs.
11360     (switch-to-buffer gnus-group-buffer)
11361     (gnus-group-save-newsrc)))
11362
11363 (defun gnus-apply-kill-file ()
11364   "Apply a kill file to the current newsgroup.
11365 Returns the number of articles marked as read."
11366   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11367           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11368       (gnus-apply-kill-file-internal)
11369     0))
11370
11371 (defun gnus-kill-save-kill-buffer ()
11372   (save-excursion
11373     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11374       (if (get-file-buffer file)
11375           (progn
11376             (set-buffer (get-file-buffer file))
11377             (and (buffer-modified-p) (save-buffer))
11378             (kill-buffer (current-buffer)))))))
11379
11380 (defvar gnus-kill-file-name "KILL"
11381   "Suffix of the kill files.")
11382
11383 (defun gnus-newsgroup-kill-file (newsgroup)
11384   "Return the name of a kill file name for NEWSGROUP.
11385 If NEWSGROUP is nil, return the global kill file name instead."
11386   (cond ((or (null newsgroup)
11387              (string-equal newsgroup ""))
11388          ;; The global KILL file is placed at top of the directory.
11389          (expand-file-name gnus-kill-file-name
11390                            (or gnus-kill-files-directory "~/News")))
11391         ((gnus-use-long-file-name 'not-kill)
11392          ;; Append ".KILL" to newsgroup name.
11393          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11394                                    "." gnus-kill-file-name)
11395                            (or gnus-kill-files-directory "~/News")))
11396         (t
11397          ;; Place "KILL" under the hierarchical directory.
11398          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11399                                    "/" gnus-kill-file-name)
11400                            (or gnus-kill-files-directory "~/News")))))
11401
11402 \f
11403 ;;;
11404 ;;; Dribble file
11405 ;;;
11406
11407 (defvar gnus-dribble-ignore nil)
11408 (defvar gnus-dribble-eval-file nil)
11409
11410 (defun gnus-dribble-file-name ()
11411   (concat gnus-current-startup-file "-dribble"))
11412
11413 (defun gnus-dribble-enter (string)
11414   (if (and (not gnus-dribble-ignore)
11415            gnus-dribble-buffer
11416            (buffer-name gnus-dribble-buffer))
11417       (let ((obuf (current-buffer)))
11418         (set-buffer gnus-dribble-buffer)
11419         (insert string "\n")
11420         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11421         (set-buffer obuf))))
11422
11423 (defun gnus-dribble-read-file ()
11424   (let ((dribble-file (gnus-dribble-file-name)))
11425     (save-excursion 
11426       (set-buffer (setq gnus-dribble-buffer 
11427                         (get-buffer-create 
11428                          (file-name-nondirectory dribble-file))))
11429       (gnus-add-current-to-buffer-list)
11430       (erase-buffer)
11431       (set-visited-file-name dribble-file)
11432       (buffer-disable-undo (current-buffer))
11433       (bury-buffer (current-buffer))
11434       (set-buffer-modified-p nil)
11435       (let ((auto (make-auto-save-file-name))
11436             (gnus-dribble-ignore t))
11437         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11438             (progn
11439               (if (file-newer-than-file-p auto dribble-file)
11440                   (setq dribble-file auto))
11441               (insert-file-contents dribble-file)
11442               (if (not (zerop (buffer-size)))
11443                   (set-buffer-modified-p t))
11444               (if (gnus-y-or-n-p 
11445                    "Auto-save file exists. Do you want to read it? ")
11446                   (setq gnus-dribble-eval-file t))))))))
11447
11448 (defun gnus-dribble-eval-file ()
11449   (if (not gnus-dribble-eval-file)
11450       ()
11451     (setq gnus-dribble-eval-file nil)
11452     (save-excursion
11453       (let ((gnus-dribble-ignore t))
11454         (set-buffer gnus-dribble-buffer)
11455         (eval-buffer (current-buffer))))))
11456
11457 (defun gnus-dribble-delete-file ()
11458   (if (file-exists-p (gnus-dribble-file-name))
11459       (delete-file (gnus-dribble-file-name)))
11460   (if gnus-dribble-buffer
11461       (save-excursion
11462         (set-buffer gnus-dribble-buffer)
11463         (let ((auto (make-auto-save-file-name)))
11464           (if (file-exists-p auto)
11465               (delete-file auto))
11466           (erase-buffer)
11467           (set-buffer-modified-p nil)))))
11468
11469 (defun gnus-dribble-save ()
11470   (if (and gnus-dribble-buffer
11471            (buffer-name gnus-dribble-buffer))
11472       (save-excursion
11473         (set-buffer gnus-dribble-buffer)
11474         (save-buffer))))
11475
11476 (defun gnus-dribble-clear ()
11477   (save-excursion
11478     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11479         (progn
11480           (set-buffer gnus-dribble-buffer)
11481           (erase-buffer)
11482           (set-buffer-modified-p nil)
11483           (setq buffer-saved-size (buffer-size))))))
11484
11485 ;;;
11486 ;;; Server Communication
11487 ;;;
11488
11489 ;; All the Gnus backends have the same interface, and should return
11490 ;; data in a similar format. Below is an overview of what functions
11491 ;; these packages must supply and what results they should return.
11492 ;;
11493 ;; Variables:
11494 ;;
11495 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11496 ;; buffer. 
11497 ;;
11498 ;; Functions for the imaginary backend `choke':
11499 ;;
11500 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11501 ;; Should return all headers for all ARTICLES, or return NOV lines for
11502 ;; the same.
11503 ;;
11504 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11505 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11506 ;; must be returned.
11507 ;;
11508 ;; `choke-close-group GROUP &optional SERVER'
11509 ;; Close group. Most backends won't have to do anything with this
11510 ;; call, but it is an opportunity to clean up, if that is needed. It
11511 ;; is called when Gnus exits a group.
11512 ;;
11513 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11514 ;; Return ARTICLE, which is either an article number or
11515 ;; message-id. Note that not all backends can return articles based on
11516 ;; message-id. 
11517 ;;
11518 ;; `choke-request-list SERVER'
11519 ;; Return a list of all newsgroups on SERVER.
11520 ;;
11521 ;; `choke-request-list-newsgroups SERVER'
11522 ;; Return a list of descriptions of all newsgroups on SERVER.
11523 ;;
11524 ;; `choke-request-newgroups DATE &optional SERVER'
11525 ;; Return a list of all groups that have arrived after DATE on
11526 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11527 ;; always check whether the groups are old or not. Backends that do
11528 ;; not store date information may just return the entire list of
11529 ;; groups, although this might not be a good idea in general.
11530 ;;
11531 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11532 ;; Should return a buffer that is suitable for "posting". nnspool and
11533 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11534 ;; buffer. This function should fill out the appropriate headers. 
11535 ;;
11536 ;; `choke-request-post &optional SERVER'
11537 ;; Function that will be called from a buffer to be posted. 
11538 ;;
11539 ;; `choke-open-server SERVER &optional ARGUMENT'
11540 ;; Open a connection to SERVER.
11541 ;;
11542 ;; `choke-close-server &optional SERVER'
11543 ;; Close the connection to SERVER.
11544 ;;
11545 ;; `choke-server-opened &optional SERVER'
11546 ;; Whether the conenction to SERVER is opened or not.
11547 ;;
11548 ;; `choke-server-status &optional SERVER'
11549 ;; Should return a status string (not in the nntp buffer, but as the
11550 ;; result of the function).
11551 ;;
11552 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11553 ;; Optional function for retrieving active file info on all groups in
11554 ;; GROUPS.  Two return formats are supported: The normal active file
11555 ;; format, and a list of GROUP lines.  This function should return (as
11556 ;; a function value) either `active' or `group', depending on what
11557 ;; format it returns.
11558 ;;
11559 ;; The following functions are optional and apply only to backends
11560 ;; that are able to control the contents of their groups totally
11561 ;; (ie. mail backends.)  Backends that aren't able to do that
11562 ;; shouldn't define these functions at all. Gnus will check for their
11563 ;; presence before attempting to call them.
11564 ;;
11565 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11566 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11567 ;; backends will not be able to expire articles. Should return a list
11568 ;; of all articles that were not expired.
11569 ;;
11570 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11571 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11572 ;; Removes any information it has added to the article (extra headers,
11573 ;; whatever - make it as clean as possible), and then passes the
11574 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11575 ;; function described below. If the ACCEPT-FORM returns a non-nil
11576 ;; value, the article should then be deleted. If LAST is nil, that
11577 ;; means that there will be further calls to this function. This might
11578 ;; be taken as an advice not to save buffers/internal variables just
11579 ;; yet, but wait until the last call to speed things up.
11580 ;;
11581 ;; `choke-request-accept-article GROUP &optional LAST' 
11582 ;; The contents of the current buffer will be put into GROUP.  There
11583 ;; should, of course, be an article in the current buffer.  This
11584 ;; function is normally only called by the function described above,
11585 ;; and LAST works the same way as in that function.
11586 ;;
11587 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11588 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11589 ;; This provides an easy interface for allowing editing of
11590 ;; articles. Note that even headers may be edited, so the backend has
11591 ;; to update any tables (nov buffers, etc) that it maintains after
11592 ;; replacing the article.
11593 ;;
11594 ;; `choke-request-create-group GROUP &optional SERVER'
11595 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11596 ;; might be a group that already exists, but hasn't been registered
11597 ;; yet. 
11598 ;;
11599 ;; All these functions must return nil if they couldn't service the
11600 ;; request. If the optional arguments are not supplied, some "current"
11601 ;; or "default" values should be used. In short, one should emulate an
11602 ;; NNTP server, in a way.
11603 ;;
11604 ;; If you want to write a new backend, you just have to supply the
11605 ;; functions listed above. In addition, you must enter the new backend
11606 ;; into the list of valid select methods:
11607 ;; (setq gnus-valid-select-methods 
11608 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11609 ;; The first element in this list is the name of the backend. Other
11610 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11611 ;; groups), `none' (neither), `respool' (for groups that can control
11612 ;; their contents). 
11613
11614 (defun gnus-start-news-server (&optional confirm)
11615   "Open a method for getting news.
11616 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11617   (let (how)
11618     (if gnus-current-select-method
11619         ;; Stream is already opened.
11620         nil
11621       ;; Open NNTP server.
11622       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11623       (if confirm
11624           (progn
11625             ;; Read server name with completion.
11626             (setq gnus-nntp-server
11627                   (completing-read "NNTP server: "
11628                                    (mapcar (lambda (server) (list server))
11629                                            (cons (list gnus-nntp-server)
11630                                                  gnus-secondary-servers))
11631                                    nil nil gnus-nntp-server))))
11632
11633       (if (and gnus-nntp-server 
11634                (stringp gnus-nntp-server)
11635                (not (string= gnus-nntp-server "")))
11636           (setq gnus-select-method
11637                 (cond ((or (string= gnus-nntp-server "")
11638                            (string= gnus-nntp-server "::"))
11639                        (list 'nnspool (system-name)))
11640                       ((string-match "^:" gnus-nntp-server)
11641                        (list 'nnmh gnus-nntp-server 
11642                              (list 'nnmh-directory 
11643                                    (file-name-as-directory
11644                                     (expand-file-name
11645                                      (concat "~/" (substring
11646                                                    gnus-nntp-server 1)))))
11647                              (list 'nnmh-get-new-mail nil)))
11648                       (t
11649                        (list 'nntp gnus-nntp-server)))))
11650
11651       (setq how (car gnus-select-method))
11652       (cond ((eq how 'nnspool)
11653              (require 'nnspool)
11654              (gnus-message 5 "Looking up local news spool..."))
11655             ((eq how 'nnmh)
11656              (require 'nnmh)
11657              (gnus-message 5 "Looking up mh spool..."))
11658             (t
11659              (require 'nntp)))
11660       (setq gnus-current-select-method gnus-select-method)
11661       (run-hooks 'gnus-open-server-hook)
11662       (or 
11663        ;; gnus-open-server-hook might have opened it
11664        (gnus-server-opened gnus-select-method)  
11665        (gnus-open-server gnus-select-method)
11666        (gnus-y-or-n-p
11667         (format
11668          "%s server on %s can't be opened. Continue? "
11669          (car gnus-select-method) (nth 1 gnus-select-method)))
11670        (progn
11671          (gnus-message 1 "Couldn't open server on %s" 
11672                        (nth 1 gnus-select-method))
11673          (ding)
11674          nil)))))
11675
11676 (defun gnus-check-news-server (&optional method)
11677   "If the news server is down, start it up again."
11678   (let ((method (if method method gnus-select-method)))
11679     (and (stringp method)
11680          (setq method (gnus-server-to-method method)))
11681     (if (gnus-server-opened method)
11682         ;; Stream is already opened.
11683         t
11684       ;; Open server.
11685       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11686       (run-hooks 'gnus-open-server-hook)
11687       (or (gnus-server-opened method)
11688           (gnus-open-server method))
11689       (message ""))))
11690
11691 (defun gnus-nntp-message (&optional message)
11692   "Check the status of the NNTP server.
11693 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11694 is returned insted of the status string."
11695   (let ((status (gnus-status-message (gnus-find-method-for-group 
11696                                       gnus-newsgroup-name)))
11697         (message (or message "")))
11698     (if (and (stringp status) (> (length status) 0))
11699         status message)))
11700
11701 (defun gnus-get-function (method function)
11702   (and (stringp method)
11703        (setq method (gnus-server-to-method method)))
11704   (let ((func (intern (format "%s-%s" (car method) function))))
11705     (if (not (fboundp func)) 
11706         (progn
11707           (require (car method))
11708           (if (not (fboundp func)) 
11709               (error "No such function: %s" func))))
11710     func))
11711
11712 ;;; Interface functions to the backends.
11713
11714 (defun gnus-open-server (method)
11715   (funcall (gnus-get-function method 'open-server)
11716            (nth 1 method) (nthcdr 2 method)))
11717
11718 (defun gnus-close-server (method)
11719   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11720
11721 (defun gnus-request-list (method)
11722   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11723
11724 (defun gnus-request-list-newsgroups (method)
11725   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11726
11727 (defun gnus-request-newgroups (date method)
11728   (funcall (gnus-get-function method 'request-newgroups) 
11729            date (nth 1 method)))
11730
11731 (defun gnus-server-opened (method)
11732   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11733
11734 (defun gnus-status-message (method)
11735   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11736                   method)))
11737     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11738
11739 (defun gnus-request-group (group &optional dont-check)
11740   (let ((method (gnus-find-method-for-group group)))
11741 ;    (and t (message "%s GROUP %s" (car method) group))
11742     (funcall (gnus-get-function method 'request-group) 
11743              (gnus-group-real-name group) (nth 1 method) dont-check)))
11744
11745 (defun gnus-request-asynchronous (group &optional articles)
11746   (let ((method (gnus-find-method-for-group group)))
11747     (funcall (gnus-get-function method 'request-asynchronous) 
11748              (gnus-group-real-name group) (nth 1 method) articles)))
11749
11750 (defun gnus-list-active-group (group)
11751   (let ((method (gnus-find-method-for-group group))
11752         (func 'list-active-group))
11753     (and (gnus-check-backend-function func group)
11754          (funcall (gnus-get-function method func) 
11755                   (gnus-group-real-name group) (nth 1 method)))))
11756
11757 (defun gnus-request-group-description (group)
11758   (let ((method (gnus-find-method-for-group group))
11759         (func 'request-group-description))
11760     (and (gnus-check-backend-function func group)
11761          (funcall (gnus-get-function method func) 
11762                   (gnus-group-real-name group) (nth 1 method)))))
11763
11764 (defun gnus-close-group (group)
11765   (let ((method (gnus-find-method-for-group group)))
11766     (funcall (gnus-get-function method 'close-group) 
11767              (gnus-group-real-name group) (nth 1 method))))
11768
11769 (defun gnus-retrieve-headers (articles group)
11770   (let ((method (gnus-find-method-for-group group)))
11771     (if (and gnus-use-cache (numberp (car articles)))
11772         (gnus-cache-retrieve-headers articles group)
11773       (funcall (gnus-get-function method 'retrieve-headers) 
11774                articles (gnus-group-real-name group) (nth 1 method)))))
11775
11776 (defun gnus-retrieve-groups (groups method)
11777   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11778
11779 (defun gnus-request-article (article group &optional buffer)
11780   (let ((method (gnus-find-method-for-group group)))
11781     (funcall (gnus-get-function method 'request-article) 
11782              article (gnus-group-real-name group) (nth 1 method) buffer)))
11783
11784 (defun gnus-request-head (article group)
11785   (let ((method (gnus-find-method-for-group group)))
11786     (funcall (gnus-get-function method 'request-head) 
11787              article (gnus-group-real-name group) (nth 1 method))))
11788
11789 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11790 (defun gnus-request-post-buffer (post group subject header artbuf
11791                                       info follow-to respect-poster)
11792    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11793                                             group gnus-newsrc-hashtb)))))
11794           (method
11795            (if (and gnus-post-method
11796                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11797                     (memq 'post (assoc
11798                                  (format "%s" (car (gnus-find-method-for-group
11799                                                     gnus-newsgroup-name)))
11800                                         gnus-valid-select-methods)))
11801                gnus-post-method
11802              (gnus-find-method-for-group gnus-newsgroup-name))))
11803      (or (gnus-server-opened method)
11804          (gnus-open-server method)
11805          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11806      (let ((mail-self-blind nil)
11807            (mail-archive-file-name nil))
11808        (funcall (gnus-get-function method 'request-post-buffer) 
11809                 post group subject header artbuf info follow-to
11810                 respect-poster))))
11811
11812 (defun gnus-request-post (method &optional force)
11813   (and (stringp method)
11814        (setq method (gnus-server-to-method method)))
11815   (and (not force) gnus-post-method
11816        (memq 'post (assoc (format "%s" (car method))
11817                           gnus-valid-select-methods))
11818        (setq method gnus-post-method))
11819   (funcall (gnus-get-function method 'request-post) 
11820            (nth 1 method)))
11821
11822 (defun gnus-request-expire-articles (articles group &optional force)
11823   (let ((method (gnus-find-method-for-group group)))
11824     (funcall (gnus-get-function method 'request-expire-articles) 
11825              articles (gnus-group-real-name group) (nth 1 method)
11826              force)))
11827
11828 (defun gnus-request-move-article 
11829   (article group server accept-function &optional last)
11830   (let ((method (gnus-find-method-for-group group)))
11831     (funcall (gnus-get-function method 'request-move-article) 
11832              article (gnus-group-real-name group) 
11833              (nth 1 method) accept-function last)))
11834
11835 (defun gnus-request-accept-article (group &optional last)
11836   (let ((func (if (symbolp group) group
11837                 (car (gnus-find-method-for-group group)))))
11838     (funcall (intern (format "%s-request-accept-article" func))
11839              (if (stringp group) (gnus-group-real-name group) group)
11840              last)))
11841
11842 (defun gnus-request-replace-article (article group buffer)
11843   (let ((func (car (gnus-find-method-for-group group))))
11844     (funcall (intern (format "%s-request-replace-article" func))
11845              article (gnus-group-real-name group) buffer)))
11846
11847 (defun gnus-request-create-group (group)
11848   (let ((method (gnus-find-method-for-group group)))
11849     (funcall (gnus-get-function method 'request-create-group) 
11850              (gnus-group-real-name group) (nth 1 method))))
11851
11852 (defun gnus-member-of-valid (symbol group)
11853   (memq symbol (assoc
11854                 (format "%s" (car (gnus-find-method-for-group group)))
11855                 gnus-valid-select-methods)))
11856
11857 (defun gnus-secondary-method-p (method)
11858   (let ((methods gnus-secondary-select-methods)
11859         (gmethod (gnus-server-get-method nil method)))
11860     (while (and methods
11861                 (not (equal (gnus-server-get-method nil (car methods)) 
11862                             gmethod)))
11863       (setq methods (cdr methods)))
11864     methods))
11865
11866 (defun gnus-find-method-for-group (group &optional info)
11867   (or gnus-override-method
11868       (and (not group)
11869            gnus-select-method)
11870       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11871             method)
11872         (if (or (not info)
11873                 (not (setq method (nth 4 info))))
11874             (setq method gnus-select-method)
11875           (setq method
11876                 (cond ((stringp method)
11877                        (gnus-server-to-method method))
11878                       ((stringp (car method))
11879                        (gnus-server-extend-method group method))
11880                       (t
11881                        method))))
11882         (gnus-server-add-address method))))
11883
11884 (defun gnus-check-backend-function (func group)
11885   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11886                  group)))
11887     (fboundp (intern (format "%s-%s" method func)))))
11888
11889 (defun gnus-methods-using (method)
11890   (let ((valids gnus-valid-select-methods)
11891         outs)
11892     (while valids
11893       (if (memq method (car valids)) 
11894           (setq outs (cons (car valids) outs)))
11895       (setq valids (cdr valids)))
11896     outs))
11897
11898 ;;; 
11899 ;;; Active & Newsrc File Handling
11900 ;;;
11901
11902 ;; Newsrc related functions.
11903 ;; Gnus internal format of gnus-newsrc-alist:
11904 ;; (("alt.general" 3 (1 . 1))
11905 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11906 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11907 ;; The first item is the group name; the second is the subscription
11908 ;; level; the third is either a range of a list of ranges of read
11909 ;; articles, the optional fourth element is a list of marked articles,
11910 ;; the optional fifth element is the select method.
11911 ;;
11912 ;; Gnus internal format of gnus-newsrc-hashtb:
11913 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11914 ;; This is the entry for "alt.misc". The first element is the number
11915 ;; of unread articles in "alt.misc". The cdr of this entry is the
11916 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11917 ;; trivial to remove or add new elements into gnus-newsrc-alist
11918 ;; without scanning the entire list. So, to get the actual information
11919 ;; of "alt.misc", you'd say something like 
11920 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11921 ;;
11922 ;; Gnus internal format of gnus-active-hashtb:
11923 ;; ((1 . 1))
11924 ;;  (5 . 10))
11925 ;;  (67 . 99)) ...)
11926 ;; The only element in each entry in this hash table is a range of
11927 ;; (possibly) available articles. (Articles in this range may have
11928 ;; been expired or canceled.)
11929 ;;
11930 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11931 ;; ("alt.misc" "alt.test" "alt.general" ...)
11932
11933 (defun gnus-setup-news (&optional rawfile level)
11934   "Setup news information.
11935 If RAWFILE is non-nil, the .newsrc file will also be read.
11936 If LEVEL is non-nil, the news will be set up at level LEVEL."
11937   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11938     ;; Clear some variables to re-initialize news information.
11939     (if init (setq gnus-newsrc-alist nil 
11940                    gnus-active-hashtb nil))
11941
11942     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11943     (if init (gnus-read-newsrc-file rawfile))
11944
11945     ;; Read the active file and create `gnus-active-hashtb'.
11946     ;; If `gnus-read-active-file' is nil, then we just create an empty
11947     ;; hash table. The partial filling out of the hash table will be
11948     ;; done in `gnus-get-unread-articles'.
11949     (if (and gnus-read-active-file 
11950              (not level))
11951         (gnus-read-active-file)
11952       (setq gnus-active-hashtb (make-vector 4095 0)))
11953
11954     ;; Possibly eval the dribble file.
11955     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11956
11957     (gnus-update-format-specifications)
11958
11959     ;; Find the number of unread articles in each non-dead group.
11960     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
11961       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
11962     ;; Find new newsgroups and treat them.
11963     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11964              (gnus-server-opened gnus-select-method))
11965         (gnus-find-new-newsgroups))
11966     (if (and init gnus-check-bogus-newsgroups 
11967              gnus-read-active-file (not level)
11968              (gnus-server-opened gnus-select-method))
11969         (gnus-check-bogus-newsgroups))))
11970
11971 (defun gnus-find-new-newsgroups ()
11972   "Search for new newsgroups and add them.
11973 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11974 The `-n' option line from .newsrc is respected."
11975   (interactive)
11976   (or (gnus-check-first-time-used)
11977       (if (or (consp gnus-check-new-newsgroups)
11978               (eq gnus-check-new-newsgroups 'ask-server))
11979           (gnus-ask-server-for-new-groups)
11980         (let ((groups 0)
11981               group new-newsgroups)
11982           (gnus-message 5 "Checking for new newsgroups...")
11983           (or gnus-have-read-active-file (gnus-read-active-file))
11984           (setq gnus-newsrc-last-checked-date (current-time-string))
11985           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11986           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11987           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11988           (mapatoms
11989            (lambda (sym)
11990              (if (or (null (setq group (symbol-name sym)))
11991                      (null (symbol-value sym))
11992                      (gnus-gethash group gnus-killed-hashtb)
11993                      (gnus-gethash group gnus-newsrc-hashtb))
11994                  ()
11995                (let ((do-sub (gnus-matches-options-n group)))
11996                  (cond 
11997                   ((eq do-sub 'subscribe)
11998                    (setq groups (1+ groups))
11999                    (gnus-sethash group group gnus-killed-hashtb)
12000                    (funcall gnus-subscribe-options-newsgroup-method group))
12001                   ((eq do-sub 'ignore)
12002                    nil)
12003                   (t
12004                    (setq groups (1+ groups))
12005                    (gnus-sethash group group gnus-killed-hashtb)
12006                    (if gnus-subscribe-hierarchical-interactive
12007                        (setq new-newsgroups (cons group new-newsgroups))
12008                      (funcall gnus-subscribe-newsgroup-method group)))))))
12009            gnus-active-hashtb)
12010           (if new-newsgroups 
12011               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12012           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12013           (if (> groups 0)
12014               (gnus-message 6 "%d new newsgroup%s arrived." 
12015                             groups (if (> groups 1) "s have" " has"))
12016             (gnus-message 6 "No new newsgroups."))))))
12017
12018 (defun gnus-matches-options-n (group)
12019   ;; Returns `subscribe' if the group is to be uncoditionally
12020   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12021   ;; no match for the group.
12022
12023   ;; First we check the two user variables.
12024   (cond
12025    ((and gnus-options-subscribe
12026          (string-match gnus-options-subscribe group))
12027     'subscribe)
12028    ((and gnus-options-not-subscribe
12029          (string-match gnus-options-not-subscribe group))
12030     'ignore)
12031    ;; Then we go through the list that was retrieved from the .newsrc
12032    ;; file.  This list has elements on the form 
12033    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12034    ;; is in the reverse order of the options line) is returned.
12035    (t
12036     (let ((regs gnus-newsrc-options-n))
12037       (while (and regs
12038                   (not (string-match (car (car regs)) group)))
12039         (setq regs (cdr regs)))
12040       (and regs (cdr (car regs)))))))
12041
12042 (defun gnus-ask-server-for-new-groups ()
12043   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12044          (methods (cons gnus-select-method 
12045                         (append
12046                          (and (consp gnus-check-new-newsgroups)
12047                               gnus-check-new-newsgroups)
12048                          gnus-secondary-select-methods)))
12049          (groups 0)
12050          (new-date (current-time-string))
12051          hashtb group new-newsgroups got-new method)
12052     ;; Go thorugh both primary and secondary select methods and
12053     ;; request new newsgroups.  
12054     (while methods
12055       (setq method (gnus-server-get-method nil (car methods)))
12056       (and (or (gnus-server-opened method)
12057                (gnus-open-server method))
12058            (gnus-request-newgroups date method)
12059            (save-excursion
12060              (setq got-new t)
12061              (set-buffer nntp-server-buffer)
12062              (or hashtb (setq hashtb (gnus-make-hashtable 
12063                                       (count-lines (point-min) (point-max)))))
12064              ;; Enter all the new groups in a hashtable.
12065              (gnus-active-to-gnus-format method hashtb 'ignore)))
12066       (setq methods (cdr methods)))
12067     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12068     ;; Now all new groups from all select methods are in `hashtb'.
12069     (mapatoms
12070      (lambda (group-sym)
12071        (setq group (symbol-name group-sym))
12072        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12073                (member group gnus-zombie-list)
12074                (member group gnus-killed-list))
12075            ;; The group is already known.
12076            ()
12077          (and (symbol-value group-sym)
12078               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12079          (let ((do-sub (gnus-matches-options-n group)))
12080            (cond ((eq do-sub 'subscribe)
12081                   (setq groups (1+ groups))
12082                   (gnus-sethash group group gnus-killed-hashtb)
12083                   (funcall 
12084                    gnus-subscribe-options-newsgroup-method group))
12085                  ((eq do-sub 'ignore)
12086                   nil)
12087                  (t
12088                   (setq groups (1+ groups))
12089                   (gnus-sethash group group gnus-killed-hashtb)
12090                   (if gnus-subscribe-hierarchical-interactive
12091                       (setq new-newsgroups (cons group new-newsgroups))
12092                     (funcall gnus-subscribe-newsgroup-method group)))))))
12093      hashtb)
12094     (if new-newsgroups 
12095         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12096     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12097     (if (> groups 0)
12098         (gnus-message 6 "%d new newsgroup%s arrived." 
12099                       groups (if (> groups 1) "s have" " has")))
12100     got-new))
12101
12102 (defun gnus-check-first-time-used ()
12103   (if (or (> (length gnus-newsrc-alist) 1)
12104           (file-exists-p gnus-startup-file)
12105           (file-exists-p (concat gnus-startup-file ".el"))
12106           (file-exists-p (concat gnus-startup-file ".eld")))
12107       nil
12108     (gnus-message 6 "First time user; subscribing you to default groups")
12109     (or gnus-have-read-active-file (gnus-read-active-file))
12110     (setq gnus-newsrc-last-checked-date (current-time-string))
12111     (let ((groups gnus-default-subscribed-newsgroups)
12112           group)
12113       (if (eq groups t)
12114           nil
12115         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12116         (mapatoms
12117          (lambda (sym)
12118            (if (null (setq group (symbol-name sym)))
12119                ()
12120              (let ((do-sub (gnus-matches-options-n group)))
12121                (cond 
12122                 ((eq do-sub 'subscribe)
12123                  (gnus-sethash group group gnus-killed-hashtb)
12124                  (funcall gnus-subscribe-options-newsgroup-method group))
12125                 ((eq do-sub 'ignore)
12126                  nil)
12127                 (t
12128                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12129          gnus-active-hashtb)
12130         (while groups
12131           (if (gnus-gethash (car groups) gnus-active-hashtb)
12132               (gnus-group-change-level 
12133                (car groups) gnus-level-default-subscribed gnus-level-killed))
12134           (setq groups (cdr groups)))
12135         (gnus-group-make-help-group)
12136         (and gnus-novice-user
12137              (gnus-message 7 "`A k' to list killed groups"))))))
12138
12139 (defun gnus-subscribe-group (group previous &optional method)
12140   (gnus-group-change-level 
12141    (if method
12142        (list t group gnus-level-default-subscribed nil nil method)
12143      group) 
12144    gnus-level-default-subscribed gnus-level-killed previous t))
12145
12146 ;; `gnus-group-change-level' is the fundamental function for changing
12147 ;; subscription levels of newsgroups. This might mean just changing
12148 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12149 ;; again, which subscribes/unsubscribes a group, which is equally
12150 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12151 ;; from 8-9 to 1-7 means that you remove the group from the list of
12152 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12153 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12154 ;; which is trivial.
12155 ;; ENTRY can either be a string (newsgroup name) or a list (if
12156 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12157 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12158 ;; entries. 
12159 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12160 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12161 ;; after. 
12162 (defun gnus-group-change-level (entry level &optional oldlevel
12163                                       previous fromkilled)
12164   (let (group info active num)
12165     ;; Glean what info we can from the arguments
12166     (if (consp entry)
12167         (if fromkilled (setq group (nth 1 entry))
12168           (setq group (car (nth 2 entry))))
12169       (setq group entry))
12170     (if (and (stringp entry)
12171              oldlevel 
12172              (< oldlevel gnus-level-zombie))
12173         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12174     (if (and (not oldlevel)
12175              (consp entry))
12176         (setq oldlevel (car (cdr (nth 2 entry)))))
12177     (if (stringp previous)
12178         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12179
12180     (if (and (>= oldlevel gnus-level-zombie)
12181              (gnus-gethash group gnus-newsrc-hashtb))
12182         ;; We are trying to subscribe a group that is already
12183         ;; subscribed. 
12184         () ; Do nothing. 
12185
12186       (gnus-dribble-enter
12187        (format "(gnus-group-change-level %S %S %S %S %S)" 
12188                group level oldlevel (car (nth 2 previous)) fromkilled))
12189     
12190       ;; Then we remove the newgroup from any old structures, if needed.
12191       ;; If the group was killed, we remove it from the killed or zombie
12192       ;; list. If not, and it is in fact going to be killed, we remove
12193       ;; it from the newsrc hash table and assoc.
12194       (cond ((>= oldlevel gnus-level-zombie)
12195              (if (= oldlevel gnus-level-zombie)
12196                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12197                (setq gnus-killed-list (delete group gnus-killed-list))))
12198             (t
12199              (if (and (>= level gnus-level-zombie)
12200                       entry)
12201                  (progn
12202                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12203                    (if (nth 3 entry)
12204                        (setcdr (gnus-gethash (car (nth 3 entry))
12205                                              gnus-newsrc-hashtb)
12206                                (cdr entry)))
12207                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12208
12209       ;; Finally we enter (if needed) the list where it is supposed to
12210       ;; go, and change the subscription level. If it is to be killed,
12211       ;; we enter it into the killed or zombie list.
12212       (cond ((>= level gnus-level-zombie)
12213              ;; Remove from the hash table.
12214              (gnus-sethash group nil gnus-newsrc-hashtb)
12215              (or (gnus-group-foreign-p group)
12216                  ;; We do not enter foreign groups into the list of dead
12217                  ;; groups.  
12218                  (if (= level gnus-level-zombie)
12219                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12220                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12221             (t
12222              ;; If the list is to be entered into the newsrc assoc, and
12223              ;; it was killed, we have to create an entry in the newsrc
12224              ;; hashtb format and fix the pointers in the newsrc assoc.
12225              (if (>= oldlevel gnus-level-zombie)
12226                  (progn
12227                    (if (listp entry)
12228                        (progn
12229                          (setq info (cdr entry))
12230                          (setq num (car entry)))
12231                      (setq active (gnus-gethash group gnus-active-hashtb))
12232                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12233                      ;; Check whether the group is foreign. If so, the
12234                      ;; foreign select method has to be entered into the
12235                      ;; info. 
12236                      (let ((method (gnus-group-method-name group)))
12237                        (if (eq method gnus-select-method)
12238                            (setq info (list group level nil))
12239                          (setq info (list group level nil nil method)))))
12240                    (or previous 
12241                        (setq previous 
12242                              (let ((p gnus-newsrc-alist))
12243                                (while (cdr (cdr p))
12244                                  (setq p (cdr p)))
12245                                p)))
12246                    (setq entry (cons info (cdr (cdr previous))))
12247                    (if (cdr previous)
12248                        (progn
12249                          (setcdr (cdr previous) entry)
12250                          (gnus-sethash group (cons num (cdr previous)) 
12251                                        gnus-newsrc-hashtb))
12252                      (setcdr previous entry)
12253                      (gnus-sethash group (cons num previous)
12254                                    gnus-newsrc-hashtb))
12255                    (if (cdr entry)
12256                        (setcdr (gnus-gethash (car (car (cdr entry)))
12257                                              gnus-newsrc-hashtb)
12258                                entry)))
12259                ;; It was alive, and it is going to stay alive, so we
12260                ;; just change the level and don't change any pointers or
12261                ;; hash table entries.
12262                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12263
12264 (defun gnus-kill-newsgroup (newsgroup)
12265   "Obsolete function. Kills a newsgroup."
12266   (gnus-group-change-level
12267    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12268
12269 (defun gnus-check-bogus-newsgroups (&optional confirm)
12270   "Remove bogus newsgroups.
12271 If CONFIRM is non-nil, the user has to confirm the deletion of every
12272 newsgroup." 
12273   (let ((newsrc (cdr gnus-newsrc-alist))
12274         bogus group entry)
12275     (gnus-message 5 "Checking bogus newsgroups...")
12276     (or gnus-have-read-active-file (gnus-read-active-file))
12277     ;; Find all bogus newsgroup that are subscribed.
12278     (while newsrc
12279       (setq group (car (car newsrc)))
12280       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12281               (nth 4 (car newsrc))      ; Foreign
12282               (and confirm
12283                    (not (gnus-y-or-n-p
12284                          (format "Remove bogus newsgroup: %s " group)))))
12285           ;; Don't remove.
12286           ()
12287         ;; Found a bogus newsgroup.
12288         (setq bogus (cons group bogus)))
12289       (setq newsrc (cdr newsrc)))
12290     ;; Remove all bogus subscribed groups by first killing them, and
12291     ;; then removing them from the list of killed groups.
12292     (while bogus
12293       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12294            (progn
12295              (gnus-group-change-level entry gnus-level-killed)
12296              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12297       (setq bogus (cdr bogus)))
12298     ;; Then we remove all bogus groups from the list of killed and
12299     ;; zombie groups. They are are removed without confirmation.
12300     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12301           killed)
12302       (while dead-lists
12303         (setq killed (symbol-value (car dead-lists)))
12304         (while killed
12305           (setq group (car killed))
12306           (or (gnus-gethash group gnus-active-hashtb)
12307               ;; The group is bogus.
12308               (set (car dead-lists)
12309                    (delete group (symbol-value (car dead-lists)))))
12310           (setq killed (cdr killed)))
12311         (setq dead-lists (cdr dead-lists))))
12312     (gnus-message 5 "Checking bogus newsgroups...done")))
12313
12314 (defun gnus-check-duplicate-killed-groups ()
12315   "Remove duplicates from the list of killed groups."
12316   (interactive)
12317   (let ((killed gnus-killed-list))
12318     (while killed
12319       (gnus-message 9 "%d" (length killed))
12320       (setcdr killed (delete (car killed) (cdr killed)))
12321       (setq killed (cdr killed)))))
12322
12323 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12324 ;; and compute how many unread articles there are in each group.
12325 (defun gnus-get-unread-articles (&optional level) 
12326   (let* ((newsrc (cdr gnus-newsrc-alist))
12327          (level (or level (1+ gnus-level-subscribed)))
12328          (foreign-level
12329           (min 
12330            (cond ((and gnus-activate-foreign-newsgroups 
12331                        (not (numberp gnus-activate-foreign-newsgroups)))
12332                   (1+ gnus-level-subscribed))
12333                  ((numberp gnus-activate-foreign-newsgroups)
12334                   gnus-activate-foreign-newsgroups)
12335                  (t 0))
12336            level))
12337          info group active virtuals method)
12338     (gnus-message 5 "Checking new news...")
12339
12340     (while newsrc
12341       (setq info (car newsrc)
12342             group (car info)
12343             active (gnus-gethash group gnus-active-hashtb))
12344
12345       ;; Check newsgroups. If the user doesn't want to check them, or
12346       ;; they can't be checked (for instance, if the news server can't
12347       ;; be reached) we just set the number of unread articles in this
12348       ;; newsgroup to t. This means that Gnus thinks that there are
12349       ;; unread articles, but it has no idea how many.
12350       (if (and (setq method (nth 4 info))
12351                (not (gnus-server-equal gnus-select-method
12352                                        (gnus-server-get-method nil method)))
12353                (not (gnus-secondary-method-p method)))
12354           ;; These groups are foreign. Check the level.
12355           (if (<= (nth 1 info) foreign-level)
12356               (if (eq (car (if (stringp method) 
12357                                (gnus-server-to-method method)
12358                              (nth 4 info))) 'nnvirtual)
12359                   ;; We have to activate the virtual groups after all
12360                   ;; the others, so we just pop them on a list for
12361                   ;; now. 
12362                   (setq virtuals (cons info virtuals))
12363                 (and (setq active (gnus-activate-newsgroup (car info)))
12364                      ;; Close the groups as we look at them!
12365                      (gnus-close-group group))))
12366                 
12367         ;; These groups are native or secondary. 
12368         (if (and (not gnus-read-active-file)
12369                  (<= (nth 1 info) level))
12370             (setq active (gnus-activate-newsgroup (car info)))))
12371       
12372       (if active
12373           (gnus-get-unread-articles-in-group info active)
12374         ;; The group couldn't be reached, so we nix out the number of
12375         ;; unread articles and stuff.
12376         (gnus-sethash group nil gnus-active-hashtb)
12377         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12378
12379       (setq newsrc (cdr newsrc)))
12380
12381     ;; Activate the virtual groups. This has to be done after all the
12382     ;; other groups. 
12383     ;; !!! If one virtual group contains another virtual group, even
12384     ;; doing it this way might cause problems.
12385    (while virtuals
12386       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12387            (gnus-get-unread-articles-in-group (car virtuals) active))
12388       (setq virtuals (cdr virtuals)))
12389
12390     (gnus-message 5 "Checking new news...done")))
12391
12392 ;; Create a hash table out of the newsrc alist. The `car's of the
12393 ;; alist elements are used as keys.
12394 (defun gnus-make-hashtable-from-newsrc-alist ()
12395   (let ((alist gnus-newsrc-alist)
12396         (ohashtb gnus-newsrc-hashtb)
12397         prev)
12398     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12399     (setq alist 
12400           (setq prev (setq gnus-newsrc-alist 
12401                            (if (equal (car (car gnus-newsrc-alist))
12402                                       "dummy.group")
12403                                gnus-newsrc-alist
12404                              (cons (list "dummy.group" 0 nil) alist)))))
12405     (while alist
12406       (gnus-sethash (car (car alist)) 
12407                     (cons (and ohashtb (car (gnus-gethash 
12408                                              (car (car alist)) ohashtb))) 
12409                           prev) gnus-newsrc-hashtb)
12410       (setq prev alist
12411             alist (cdr alist)))))
12412
12413 (defun gnus-make-hashtable-from-killed ()
12414   "Create a hash table from the killed and zombie lists."
12415   (let ((lists '(gnus-killed-list gnus-zombie-list))
12416         list)
12417     (setq gnus-killed-hashtb 
12418           (gnus-make-hashtable 
12419            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12420     (while lists
12421       (setq list (symbol-value (car lists)))
12422       (setq lists (cdr lists))
12423       (while list
12424         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12425         (setq list (cdr list))))))
12426
12427 (defun gnus-get-unread-articles-in-group (info active)
12428   (let* ((range (nth 2 info))
12429          (num 0)
12430          (marked (nth 3 info)))
12431     ;; If a cache is present, we may have to alter the active info.
12432     (and gnus-use-cache
12433          (gnus-cache-possibly-alter-active (car info) active))
12434     ;; Modify the list of read articles according to what articles 
12435     ;; are available; then tally the unread articles and add the
12436     ;; number to the group hash table entry.
12437     (cond 
12438      ((zerop (cdr active))
12439       (setq num 0))
12440      ((not range)
12441       (setq num (- (1+ (cdr active)) (car active))))
12442      ((not (listp (cdr range)))
12443       ;; Fix a single (num . num) range according to the
12444       ;; active hash table.
12445       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12446       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12447       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12448       ;; Compute number of unread articles.
12449       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12450      (t
12451       ;; The read list is a list of ranges. Fix them according to
12452       ;; the active hash table.
12453       ;; First peel off any elements that are below the lower
12454       ;; active limit. 
12455       (while (and (cdr range) 
12456                   (>= (car active) 
12457                       (or (and (atom (car (cdr range))) (car (cdr range)))
12458                           (car (car (cdr range))))))
12459         (if (numberp (car range))
12460             (setcar range 
12461                     (cons (car range) 
12462                           (or (and (numberp (car (cdr range)))
12463                                    (car (cdr range))) 
12464                               (cdr (car (cdr range))))))
12465           (setcdr (car range) 
12466                   (or (and (numberp (nth 1 range)) (nth 1 range))
12467                       (cdr (car (cdr range))))))
12468         (setcdr range (cdr (cdr range))))
12469       ;; Adjust the first element to be the same as the lower limit. 
12470       (if (and (not (atom (car range))) 
12471                (< (cdr (car range)) (car active)))
12472           (setcdr (car range) (1- (car active))))
12473       ;; Then we want to peel off any elements that are higher
12474       ;; than the upper active limit.  
12475       (let ((srange range))
12476         ;; Go past all legal elements.
12477         (while (and (cdr srange) 
12478                     (<= (or (and (atom (car (cdr srange)))
12479                                  (car (cdr srange)))
12480                             (car (car (cdr srange)))) (cdr active)))
12481           (setq srange (cdr srange)))
12482         (if (cdr srange)
12483             ;; Nuke all remaining illegal elements.
12484             (setcdr srange nil))
12485
12486         ;; Adjust the final element.
12487         (if (and (not (atom (car srange)))
12488                  (> (cdr (car srange)) (cdr active)))
12489             (setcdr (car srange) (cdr active))))
12490       ;; Compute the number of unread articles.
12491       (while range
12492         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12493                                     (cdr (car range))))
12494                             (or (and (atom (car range)) (car range))
12495                                 (car (car range))))))
12496         (setq range (cdr range)))
12497       (setq num (max 0 (- (cdr active) num)))))
12498     (and info
12499          (progn
12500            (and (assq 'tick marked)
12501                 (inline (gnus-remove-illegal-marked-articles
12502                          (assq 'tick marked) (nth 2 info))))
12503            (and (assq 'dormant marked)
12504                 (inline (gnus-remove-illegal-marked-articles
12505                          (assq 'dormant marked) (nth 2 info))))
12506            (setcar
12507             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12508             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12509                                 (length (cdr (assq 'dormant marked)))))))))
12510     num))
12511
12512 (defun gnus-remove-illegal-marked-articles (marked ranges)
12513   (let ((m (cdr marked)))
12514     ;; Make sure that all ticked articles are a subset of the unread
12515     ;; articles. 
12516     (while m
12517       (if (gnus-member-of-range (car m) ranges)
12518           (setcdr marked (cdr m))
12519         (setq marked m))
12520       (setq m (cdr m)))))
12521
12522 (defun gnus-activate-newsgroup (group)
12523   (let ((method (gnus-find-method-for-group group))
12524         active)
12525     (and (or (gnus-server-opened method) (gnus-open-server method))
12526          (gnus-request-group group)
12527          (save-excursion
12528            (set-buffer nntp-server-buffer)
12529            (goto-char (point-min))
12530            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12531                 (progn
12532                   (goto-char (match-beginning 1))
12533                   (gnus-sethash 
12534                    group (setq active (cons (read (current-buffer))
12535                                             (read (current-buffer))))
12536                    gnus-active-hashtb))
12537                 active)))))
12538
12539 (defun gnus-update-read-articles 
12540   (group unread unselected ticked &optional domarks replied expirable killed
12541          dormant bookmark score)
12542   "Update the list of read and ticked articles in GROUP using the
12543 UNREAD and TICKED lists.
12544 Note: UNSELECTED has to be sorted over `<'.
12545 Returns whether the updating was successful."
12546   (let* ((active (or gnus-newsgroup-active 
12547                      (gnus-gethash group gnus-active-hashtb)))
12548          (entry (gnus-gethash group gnus-newsrc-hashtb))
12549          (info (nth 2 entry))
12550          (marked (nth 3 info))
12551          (prev 1)
12552          (unread (sort (copy-sequence unread) (function <)))
12553          read)
12554     (if (or (not info) (not active))
12555         ;; There is no info on this group if it was, in fact,
12556         ;; killed. Gnus stores no information on killed groups, so
12557         ;; there's nothing to be done. 
12558         ;; One could store the information somewhere temporarily,
12559         ;; perhaps... Hmmm... 
12560         ()
12561       ;; Remove any negative articles numbers.
12562       (while (and unread (< (car unread) 0))
12563         (setq unread (cdr unread)))
12564       ;; Remove any expired article numbers
12565       (while (and unread (< (car unread) (car active)))
12566         (setq unread (cdr unread)))
12567       (while (and ticked (< (car ticked) (car active)))
12568         (setq ticked (cdr ticked)))
12569       (while (and dormant (< (car dormant) (car active)))
12570         (setq dormant (cdr dormant)))
12571       (setq unread (sort (append unselected unread) '<))
12572       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12573       (setcar entry (max 0 (- (length unread) (length ticked) 
12574                               (length dormant))))
12575       ;; Compute the ranges of read articles by looking at the list of
12576       ;; unread articles.  
12577       (while unread
12578         (if (/= (car unread) prev)
12579             (setq read (cons (if (= prev (1- (car unread))) prev
12580                                (cons prev (1- (car unread)))) read)))
12581         (setq prev (1+ (car unread)))
12582         (setq unread (cdr unread)))
12583       (if (<= prev (cdr active))
12584           (setq read (cons (cons prev (cdr active)) read)))
12585       ;; Enter this list into the group info.
12586       (setcar (cdr (cdr info)) 
12587               (if (> (length read) 1) (nreverse read) read))
12588       ;; Enter the list of ticked articles.
12589       (gnus-set-marked-articles 
12590        info ticked
12591        (if domarks replied (cdr (assq 'reply marked)))
12592        (if domarks expirable (cdr (assq 'expire marked)))
12593        (if domarks killed (cdr (assq 'killed marked)))
12594        (if domarks dormant (cdr (assq 'dormant marked)))
12595        (if domarks bookmark (cdr (assq 'bookmark marked)))
12596        (if domarks score (cdr (assq 'score marked))))
12597       t)))
12598
12599 (defun gnus-make-articles-unread (group articles)
12600   "Mark ARTICLES in GROUP as unread."
12601   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12602                           (gnus-gethash (gnus-group-real-name group)
12603                                         gnus-newsrc-hashtb))))
12604          (ranges (nth 2 info))
12605          news)
12606     (while articles
12607       (and (gnus-member-of-range (car articles) ranges)
12608            (setq news (cons (car articles) news)))
12609       (setq articles (cdr articles)))
12610     (if (not news)
12611         ()
12612       (setcar (nthcdr 2 info)
12613               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12614       (gnus-group-update-group group t))))
12615
12616 ;; Get the active file(s) from the backend(s).
12617 (defun gnus-read-active-file ()
12618   (gnus-group-set-mode-line)
12619   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12620                          (gnus-open-server gnus-select-method))
12621                      ;; The native server is available.
12622                      (cons gnus-select-method gnus-secondary-select-methods)
12623                    ;; The native server is down, so we just do the
12624                    ;; secondary ones.   
12625                    gnus-secondary-select-methods))
12626         list-type)
12627     (setq gnus-have-read-active-file nil)
12628     (save-excursion
12629       (set-buffer nntp-server-buffer)
12630       (while methods
12631         (let* ((method (gnus-server-get-method nil (car methods)))
12632                (where (nth 1 method))
12633                (mesg (format "Reading active file%s via %s..."
12634                              (if (and where (not (zerop (length where))))
12635                                  (concat " from " where) "")
12636                              (car method))))
12637           (gnus-message 5 mesg)
12638           (gnus-check-news-server method)
12639           (cond 
12640            ((and (eq gnus-read-active-file 'some)
12641                  (gnus-check-backend-function
12642                   'retrieve-groups (car method)))
12643             (let ((newsrc (cdr gnus-newsrc-alist))
12644                   (gmethod (gnus-server-get-method nil method))
12645                   groups)
12646               (while newsrc
12647                 (and (gnus-server-equal 
12648                       (gnus-find-method-for-group 
12649                        (car (car newsrc)) (car newsrc))
12650                       gmethod)
12651                      (setq groups (cons (gnus-group-real-name 
12652                                          (car (car newsrc))) groups)))
12653                 (setq newsrc (cdr newsrc)))
12654               (or (gnus-server-opened method)
12655                   (gnus-open-server method))
12656               (setq list-type (gnus-retrieve-groups groups method))
12657               (cond ((not list-type)
12658                      (gnus-message 
12659                       1 "Cannot read partial active file from %s server." 
12660                       (car method))
12661                      (ding)
12662                      (sit-for 2))
12663                     ((eq list-type 'active)
12664                      (gnus-active-to-gnus-format method))
12665                     (t
12666                      (gnus-groups-to-gnus-format method)))))
12667            (t
12668             (if (not (gnus-request-list method))
12669                 (progn
12670                   (gnus-message 1 "Cannot read active file from %s server." 
12671                                 (car method))
12672                   (ding))
12673               (gnus-active-to-gnus-format method)
12674               ;; We mark this active file as read.
12675               (setq gnus-have-read-active-file
12676                     (cons method gnus-have-read-active-file))
12677               (gnus-message 5 "%sdone" mesg))))
12678           )
12679         (setq methods (cdr methods))))))
12680
12681 ;; Read an active file and place the results in `gnus-active-hashtb'.
12682 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12683   (let ((cur (current-buffer))
12684         (hashtb (or hashtb 
12685                     (if (and gnus-active-hashtb 
12686                              (not (equal method gnus-select-method)))
12687                         gnus-active-hashtb
12688                       (setq gnus-active-hashtb
12689                             (if (equal method gnus-select-method)
12690                                 (gnus-make-hashtable 
12691                                  (count-lines (point-min) (point-max)))
12692                               (gnus-make-hashtable 4096))))))
12693         (flag-hashtb (gnus-make-hashtable 60)))
12694     ;; Delete unnecessary lines.
12695     (goto-char (point-min))
12696     (while (search-forward "\nto." nil t)
12697       (delete-region (1+ (match-beginning 0)) 
12698                      (progn (forward-line 1) (point))))
12699     (or (string= gnus-ignored-newsgroups "")
12700         (progn
12701           (goto-char (point-min))
12702           (delete-matching-lines gnus-ignored-newsgroups)))
12703     ;; If these are groups from a foreign select method, we insert the
12704     ;; group prefix in front of the group names. 
12705     (and method (not (gnus-server-equal
12706                       (gnus-server-get-method nil method)
12707                       (gnus-server-get-method nil gnus-select-method)))
12708          (let ((prefix (gnus-group-prefixed-name "" method)))
12709            (goto-char (point-min))
12710            (while (and (not (eobp))
12711                        (progn (insert prefix)
12712                               (zerop (forward-line 1)))))))
12713     (goto-char (point-min))
12714     ;; Store active file in hashtable.
12715     (goto-char (point-min))
12716     (if (string-match "%[oO]" gnus-group-line-format)
12717         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12718         ;; If we want information on moderated groups, we use this
12719         ;; loop...   
12720         (let* ((mod-hashtb (make-vector 7 0))
12721                (m (intern "m" mod-hashtb))
12722                group max min)
12723           (while (not (eobp))
12724             (condition-case nil
12725                 (progn
12726                   (narrow-to-region (point) (gnus-point-at-eol))
12727                   (setq group (let ((obarray hashtb)) (read cur)))
12728                   (if (and (numberp (setq max (read cur)))
12729                            (numberp (setq min (read cur)))
12730                            (progn 
12731                              (skip-chars-forward " \t")
12732                              (not
12733                               (or (= (following-char) ?=)
12734                                   (= (following-char) ?x)
12735                                   (= (following-char) ?j)))))
12736                       (set group (cons min max))
12737                     (set group nil))
12738                   ;; Enter moderated groups into a list.
12739                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12740                       (setq gnus-moderated-list 
12741                             (cons (symbol-name group) gnus-moderated-list))))
12742               (error 
12743                (set group nil)))
12744             (widen)
12745             (forward-line 1)))
12746       ;; And if we do not care about moderation, we use this loop,
12747       ;; which is faster.
12748       (let (group max min)
12749         (while (not (eobp))
12750           (condition-case ()
12751               (progn
12752                 (narrow-to-region (point) (gnus-point-at-eol))
12753                 ;; group gets set to a symbol interned in the hash table
12754                 ;; (what a hack!!) - jwz
12755                 (setq group (let ((obarray hashtb)) (read cur)))
12756                 (if (and (numberp (setq max (read cur)))
12757                          (numberp (setq min (read cur)))
12758                          (progn 
12759                            (skip-chars-forward " \t")
12760                            (not
12761                             (or (= (following-char) ?=)
12762                                 (= (following-char) ?x)
12763                                 (= (following-char) ?j)))))
12764                     (set group (cons min max))
12765                   (set group nil)))
12766             (error 
12767              (progn 
12768                (set group nil)
12769                (if ignore-errors
12770                    ()
12771                  (ding) 
12772                  (gnus-message 3 "Warning - illegal active: %s"
12773                                (buffer-substring 
12774                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12775                  nil))))
12776           (widen)
12777           (forward-line 1))))))
12778
12779 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12780   ;; Parse a "groups" active file.
12781   (let ((cur (current-buffer))
12782         (hashtb (or hashtb 
12783                     (if (and method gnus-active-hashtb)
12784                         gnus-active-hashtb
12785                       (setq gnus-active-hashtb
12786                             (gnus-make-hashtable 
12787                              (count-lines (point-min) (point-max)))))))
12788         (prefix (and method (not (eq method gnus-select-method))
12789                      (gnus-group-prefixed-name "" method))))
12790
12791     (goto-char (point-min))
12792     (condition-case ()
12793         ;; We split this into to separate loops, one with the prefix
12794         ;; and one without to speed the reading up somewhat.
12795         (if prefix
12796             (let (min max opoint)
12797               (while (not (eobp))
12798                 (read cur) (read cur)
12799                 (setq min (read cur)
12800                       max (read cur)
12801                       opoint (point))
12802                 (skip-chars-forward " \t")
12803                 (insert prefix)
12804                 (goto-char opoint)
12805                 (set (let ((obarray hashtb)) (read cur)) 
12806                      (cons min max))
12807                 (forward-line 1)))
12808           (let (min max)
12809             (while (not (eobp))
12810               (if (= (following-char) ?2)
12811                   (progn
12812                     (read cur) (read cur)
12813                     (setq min (read cur)
12814                           max (read cur))
12815                     (set (let ((obarray hashtb)) (read cur)) 
12816                          (cons min max))))
12817               (forward-line 1))))
12818       (error 
12819        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12820
12821 (defun gnus-read-newsrc-file (&optional force)
12822   "Read startup file.
12823 If FORCE is non-nil, the .newsrc file is read."
12824   ;; Reset variables that might be defined in the .newsrc.eld file.
12825   (let ((variables gnus-variable-list))
12826     (while variables
12827       (set (car variables) nil)
12828       (setq variables (cdr variables))))
12829   (let* ((newsrc-file gnus-current-startup-file)
12830          (quick-file (concat newsrc-file ".el")))
12831     (save-excursion
12832       ;; We always load the .newsrc.eld file. If always contains
12833       ;; much information that can not be gotten from the .newsrc
12834       ;; file (ticked articles, killed groups, foreign methods, etc.)
12835       (gnus-read-newsrc-el-file quick-file)
12836  
12837       (if (or force
12838               (and (file-newer-than-file-p newsrc-file quick-file)
12839                    (file-newer-than-file-p newsrc-file 
12840                                            (concat quick-file "d")))
12841               (not gnus-newsrc-alist))
12842           ;; We read the .newsrc file. Note that if there if a
12843           ;; .newsrc.eld file exists, it has already been read, and
12844           ;; the `gnus-newsrc-hashtb' has been created. While reading
12845           ;; the .newsrc file, Gnus will only use the information it
12846           ;; can find there for changing the data already read -
12847           ;; ie. reading the .newsrc file will not trash the data
12848           ;; already read (except for read articles).
12849           (save-excursion
12850             (gnus-message 5 "Reading %s..." newsrc-file)
12851             (set-buffer (find-file-noselect newsrc-file))
12852             (buffer-disable-undo (current-buffer))
12853             (gnus-newsrc-to-gnus-format)
12854             (kill-buffer (current-buffer))
12855             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12856
12857 (defun gnus-read-newsrc-el-file (file)
12858   (let ((ding-file (concat file "d")))
12859     ;; We always, always read the .eld file.
12860     (gnus-message 5 "Reading %s..." ding-file)
12861     (let (gnus-newsrc-assoc)
12862       (condition-case nil
12863           (load ding-file t t t)
12864         (error nil))
12865       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12866     (let ((inhibit-quit t))
12867       (gnus-uncompress-newsrc-alist))
12868     (gnus-make-hashtable-from-newsrc-alist)
12869     (if (not (file-newer-than-file-p file ding-file))
12870         ()
12871       ;; Old format quick file
12872       (gnus-message 5 "Reading %s..." file)
12873       ;; The .el file is newer than the .eld file, so we read that one
12874       ;; as well. 
12875       (gnus-read-old-newsrc-el-file file))))
12876
12877 ;; Parse the old-style quick startup file
12878 (defun gnus-read-old-newsrc-el-file (file)
12879   (let (newsrc killed marked group m)
12880     (prog1
12881         (let ((gnus-killed-assoc nil)
12882               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12883           (prog1
12884               (condition-case nil
12885                   (load file t t t)
12886                 (error nil))
12887             (setq newsrc gnus-newsrc-assoc
12888                   killed gnus-killed-assoc
12889                   marked gnus-marked-assoc)))
12890       (setq gnus-newsrc-alist nil)
12891       (while newsrc
12892         (setq group (car newsrc))
12893         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12894           (if info
12895               (progn
12896                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12897                 (setcar (cdr info)
12898                         (if (nth 1 group) gnus-level-default-subscribed 
12899                           gnus-level-default-unsubscribed))
12900                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12901             (setq gnus-newsrc-alist
12902                   (cons 
12903                    (setq info
12904                          (list (car group)
12905                                (if (nth 1 group) gnus-level-default-subscribed
12906                                  gnus-level-default-unsubscribed) 
12907                                (cdr (cdr group))))
12908                    gnus-newsrc-alist)))
12909           (if (setq m (assoc (car group) marked))
12910             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12911         (setq newsrc (cdr newsrc)))
12912       (setq newsrc killed)
12913       (while newsrc
12914         (setcar newsrc (car (car newsrc)))
12915         (setq newsrc (cdr newsrc)))
12916       (setq gnus-killed-list killed))
12917     ;; The .el file version of this variable does not begin with
12918     ;; "options", while the .eld version does, so we just add it if it
12919     ;; isn't there.
12920     (and
12921      gnus-newsrc-options 
12922      (progn
12923        (and (not (string-match "^ *options" gnus-newsrc-options))
12924             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12925        (and (not (string-match "\n$" gnus-newsrc-options))
12926             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12927     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12928     (gnus-make-hashtable-from-newsrc-alist)))
12929       
12930 (defun gnus-make-newsrc-file (file)
12931   "Make server dependent file name by catenating FILE and server host name."
12932   (let* ((file (expand-file-name file nil))
12933          (real-file (concat file "-" (nth 1 gnus-select-method))))
12934     (if (or (file-exists-p real-file)
12935             (file-exists-p (concat real-file ".el"))
12936             (file-exists-p (concat real-file ".eld")))
12937         real-file file)))
12938
12939 (defun gnus-uncompress-newsrc-alist ()
12940   ;; Uncompress all lists of marked articles in the newsrc assoc.
12941   (let ((newsrc gnus-newsrc-alist)
12942         marked)
12943     (while newsrc
12944       (if (not (setq marked (nth 3 (car newsrc))))
12945           ()
12946         (while marked
12947           (or (eq 'score (car (car marked)))
12948               (eq 'bookmark (car (car marked)))
12949               (eq 'killed (car (car marked)))
12950               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12951           (setq marked (cdr marked))))
12952       (setq newsrc (cdr newsrc)))))
12953
12954 (defun gnus-compress-newsrc-alist ()
12955   ;; Compress all lists of marked articles in the newsrc assoc.
12956   (let ((newsrc gnus-newsrc-alist)
12957         marked)
12958     (while newsrc
12959       (if (not (setq marked (nth 3 (car newsrc))))
12960           ()
12961         (while marked
12962           (or (eq 'score (car (car marked)))
12963               (eq 'bookmark (car (car marked)))
12964               (eq 'killed (car (car marked)))
12965               (setcdr (car marked) 
12966                       (condition-case ()
12967                           (gnus-compress-sequence 
12968                            (sort (cdr (car marked)) '<) t)
12969                         (error (cdr (car marked))))))
12970           (setq marked (cdr marked))))
12971       (setq newsrc (cdr newsrc)))))
12972
12973 (defun gnus-newsrc-to-gnus-format ()
12974   (setq gnus-newsrc-options "")
12975   (setq gnus-newsrc-options-n nil)
12976
12977   (or gnus-active-hashtb
12978       (setq gnus-active-hashtb (make-vector 4095 0)))
12979   (let ((buf (current-buffer))
12980         (already-read (> (length gnus-newsrc-alist) 1))
12981         group subscribed options-symbol newsrc Options-symbol
12982         symbol reads num1)
12983     (goto-char (point-min))
12984     ;; We intern the symbol `options' in the active hashtb so that we
12985     ;; can `eq' against it later.
12986     (setq options-symbol (intern "options" gnus-active-hashtb))
12987     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12988   
12989     (while (not (eobp))
12990       ;; We first read the first word on the line by narrowing and
12991       ;; then reading into `gnus-active-hashtb'.  Most groups will
12992       ;; already exist in that hashtb, so this will save some string
12993       ;; space.
12994       (narrow-to-region
12995        (point)
12996        (progn (skip-chars-forward "^ \t!:\n") (point)))
12997       (goto-char (point-min))
12998       (setq symbol 
12999             (and (/= (point-min) (point-max))
13000                  (let ((obarray gnus-active-hashtb)) (read buf))))
13001       (widen)
13002       ;; Now, the symbol we have read is either `options' or a group
13003       ;; name.  If it is an options line, we just add it to a string. 
13004       (cond 
13005        ((or (eq symbol options-symbol)
13006             (eq symbol Options-symbol))
13007         (setq gnus-newsrc-options
13008               ;; This concatting is quite inefficient, but since our
13009               ;; thorough studies show that approx 99.37% of all
13010               ;; .newsrc files only contain a single options line, we
13011               ;; don't give a damn, frankly, my dear.
13012               (concat gnus-newsrc-options
13013                       (buffer-substring 
13014                        (gnus-point-at-bol)
13015                        ;; Options may continue on the next line.
13016                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13017                                 (progn (beginning-of-line) (point)))
13018                            (point))))))
13019        (symbol
13020         ;; It was a group name.
13021         (setq subscribed (= (following-char) ?:)
13022               group (symbol-name symbol)
13023               reads nil)
13024         (if (eolp)
13025             ;; If the line ends here, this is clearly a buggy line, so
13026             ;; we put point a the beginning of line and let the cond
13027             ;; below do the error handling.
13028             (beginning-of-line)
13029           ;; We skip to the beginning of the ranges.
13030           (skip-chars-forward "!: \t"))
13031         ;; We are now at the beginning of the list of read articles.
13032         ;; We read them range by range.
13033         (while
13034             (cond 
13035              ((looking-at "[0-9]+")
13036               ;; We narrow and read a number instead of buffer-substring/
13037               ;; string-to-int because it's faster. narrow/widen is
13038               ;; faster than save-restriction/narrow, and save-restriction
13039               ;; produces a garbage object.
13040               (setq num1 (progn
13041                            (narrow-to-region (match-beginning 0) (match-end 0))
13042                            (read buf)))
13043               (widen)
13044               ;; If the next character is a dash, then this is a range.
13045               (if (= (following-char) ?-)
13046                   (progn
13047                     ;; We read the upper bound of the range.
13048                     (forward-char 1)
13049                     (if (not (looking-at "[0-9]+"))
13050                         ;; This is a buggy line, by we pretend that
13051                         ;; it's kinda OK. Perhaps the user should be
13052                         ;; dinged? 
13053                         (setq reads (cons num1 reads))
13054                       (setq reads 
13055                             (cons 
13056                              (cons num1 (progn
13057                                           (narrow-to-region (match-beginning 0) 
13058                                                             (match-end 0))
13059                                           (read buf)))
13060                              reads))
13061                       (widen)))
13062                 ;; It was just a simple number, so we add it to the
13063                 ;; list of ranges.
13064                 (setq reads (cons num1 reads)))
13065               ;; If the next char in ?\n, then we have reached the end
13066               ;; of the line and return nil.
13067               (/= (following-char) ?\n))
13068              ((= (following-char) ?\n)
13069               ;; End of line, so we end.
13070               nil)
13071              (t
13072               ;; Not numbers and not eol, so this might be a buggy
13073               ;; line... 
13074               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13075                   (progn
13076                     ;; The line was buggy.
13077                     (setq group nil)
13078                     (gnus-message 3 "Mangled line: %s" 
13079                                   (buffer-substring (gnus-point-at-bol) 
13080                                                     (gnus-point-at-eol)))
13081                     (ding)
13082                     (sit-for 1)))
13083               nil))
13084           ;; Skip past ", ". Spaces are illegal in these ranges, but
13085           ;; we allow them, because it's a common mistake to put a
13086           ;; space after the comma.
13087           (skip-chars-forward ", "))
13088
13089         ;; We have already read .newsrc.eld, so we gently update the
13090         ;; data in the hash table with the information we have just
13091         ;; read. 
13092         (if (not group)
13093             ()
13094           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13095                 level)
13096             (if info
13097                 ;; There is an entry for this file in the alist.
13098                 (progn
13099                   (setcar (nthcdr 2 info) (nreverse reads))
13100                   ;; We update the level very gently.  In fact, we
13101                   ;; only change it if there's been a status change
13102                   ;; from subscribed to unsubscribed, or vice versa.
13103                   (setq level (nth 1 info))
13104                   (cond ((and (<= level gnus-level-subscribed)
13105                               (not subscribed))
13106                          (setq level (if reads
13107                                          gnus-level-default-unsubscribed 
13108                                        (1+ gnus-level-default-unsubscribed))))
13109                         ((and (> level gnus-level-subscribed) subscribed)
13110                          (setq level gnus-level-default-subscribed)))
13111                   (setcar (cdr info) level))
13112               ;; This is a new group.
13113               (setq info (list group 
13114                                (if subscribed
13115                                    gnus-level-default-subscribed 
13116                                  (if reads
13117                                      (1+ gnus-level-subscribed)
13118                                    gnus-level-default-unsubscribed))
13119                                (nreverse reads))))
13120             (setq newsrc (cons info newsrc))))))
13121       (forward-line 1))
13122     
13123     (setq newsrc (nreverse newsrc))
13124
13125     (if (not already-read)
13126         ()
13127       ;; We now have two newsrc lists - `newsrc', which is what we
13128       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13129       ;; what we've read from .newsrc.eld. We have to merge these
13130       ;; lists. We do this by "attaching" any (foreign) groups in the
13131       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13132       (let ((rc (cdr gnus-newsrc-alist))
13133             (prev gnus-newsrc-alist)
13134             entry mentry)
13135         (while rc
13136           (or (null (nth 4 (car rc))) ; It's a native group.
13137               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13138               (if (setq entry (assoc (car (car prev)) newsrc))
13139                   (setcdr (setq mentry (memq entry newsrc))
13140                           (cons (car rc) (cdr mentry)))
13141                 (setq newsrc (cons (car rc) newsrc))))
13142           (setq prev rc
13143                 rc (cdr rc)))))
13144
13145     (setq gnus-newsrc-alist newsrc)
13146     ;; We make the newsrc hashtb.
13147     (gnus-make-hashtable-from-newsrc-alist)
13148
13149     ;; Finally, if we read some options lines, we parse them.
13150     (or (string= gnus-newsrc-options "")
13151         (gnus-newsrc-parse-options gnus-newsrc-options))))
13152
13153 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13154 ;; The return value will be a list on the form
13155 ;; ((regexp1 . ignore)
13156 ;;  (regexp2 . subscribe)...)
13157 ;; When handling new newsgroups, groups that match a `ignore' regexp
13158 ;; will be ignored, and groups that match a `subscribe' regexp will be
13159 ;; subscribed. A line like
13160 ;; options -n !all rec.all
13161 ;; will lead to a list that looks like
13162 ;; (("^rec\\..+" . subscribe) 
13163 ;;  ("^.+" . ignore))
13164 ;; So all "rec.*" groups will be subscribed, while all the other
13165 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13166 ;; different from "options -n rec.all !all". 
13167 (defun gnus-newsrc-parse-options (options)
13168   (let (out eol)
13169     (save-excursion
13170       (gnus-set-work-buffer)
13171       (insert (regexp-quote options))
13172       ;; First we treat all continuation lines.
13173       (goto-char (point-min))
13174       (while (re-search-forward "\n[ \t]+" nil t)
13175         (replace-match " " t t))
13176       ;; Then we transform all "all"s into ".+"s.
13177       (goto-char (point-min))
13178       (while (re-search-forward "\\ball\\b" nil t)
13179         (replace-match ".+" t t))
13180       (goto-char (point-min))
13181       ;; We remove all other options than the "-n" ones.
13182       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13183         (replace-match " ")
13184         (forward-char -1))
13185       (goto-char (point-min))
13186
13187       ;; We are only interested in "options -n" lines - we
13188       ;; ignore the other option lines.
13189       (while (re-search-forward "[ \t]-n" nil t)
13190         (setq eol 
13191               (or (save-excursion
13192                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13193                          (- (point) 2)))
13194                   (gnus-point-at-eol)))
13195         ;; Search for all "words"...
13196         (while (re-search-forward "[^ \t,\n]+" eol t)
13197           (if (= (char-after (match-beginning 0)) ?!)
13198               ;; If the word begins with a bang (!), this is a "not"
13199               ;; spec. We put this spec (minus the bang) and the
13200               ;; symbol `ignore' into the list.
13201               (setq out (cons (cons (concat 
13202                                      "^" (buffer-substring 
13203                                           (1+ (match-beginning 0))
13204                                           (match-end 0)))
13205                                     'ignore) out))
13206             ;; There was no bang, so this is a "yes" spec.
13207             (setq out (cons (cons (concat 
13208                                    "^" (buffer-substring (match-beginning 0)
13209                                                          (match-end 0)))
13210                                   'subscribe) out)))))
13211     
13212       (setq gnus-newsrc-options-n out))))
13213                
13214
13215 (defun gnus-save-newsrc-file ()
13216   "Save .newsrc file."
13217   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13218   ;; from the variable gnus-newsrc-alist.
13219   (and (or gnus-newsrc-alist gnus-killed-list)
13220        gnus-current-startup-file
13221        (progn
13222          (run-hooks 'gnus-save-newsrc-hook)
13223          (save-excursion
13224            (if (and gnus-use-dribble-file
13225                     (or (not gnus-dribble-buffer)
13226                         (not (buffer-name gnus-dribble-buffer))
13227                         (zerop (save-excursion
13228                                  (set-buffer gnus-dribble-buffer)
13229                                  (buffer-size)))))
13230                (gnus-message 4 "(No changes need to be saved)")
13231              (if gnus-save-newsrc-file
13232                  (progn
13233                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13234                    ;; Make backup file of master newsrc.
13235                    (gnus-gnus-to-newsrc-format)
13236                    (gnus-message 5 "Saving %s...done"
13237                                  gnus-current-startup-file)))
13238              ;; Quickly loadable .newsrc.
13239              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13240              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13241              (gnus-add-current-to-buffer-list)
13242              (buffer-disable-undo (current-buffer))
13243              (erase-buffer)
13244              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13245              (gnus-gnus-to-quick-newsrc-format)
13246              (save-buffer)
13247              (kill-buffer (current-buffer))
13248              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13249              (gnus-dribble-delete-file))))))
13250
13251 (defun gnus-gnus-to-quick-newsrc-format ()
13252   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13253   (insert ";; (ding) Gnus startup file.\n")
13254   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13255   (insert ";; to read .newsrc.\n")
13256   (insert "(setq gnus-newsrc-file-version "
13257           (prin1-to-string gnus-version) ")\n")
13258   (let ((variables gnus-variable-list)
13259         (inhibit-quit t)
13260         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13261         variable)
13262     ;; insert lisp expressions.
13263     (gnus-compress-newsrc-alist)
13264     (while variables
13265       (setq variable (car variables))
13266       (and (boundp variable)
13267            (symbol-value variable)
13268            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13269            (insert "(setq " (symbol-name variable) " '"
13270                    (prin1-to-string (symbol-value variable))
13271                    ")\n"))
13272       (setq variables (cdr variables)))
13273     (gnus-uncompress-newsrc-alist)))
13274
13275
13276 (defun gnus-gnus-to-newsrc-format ()
13277   ;; Generate and save the .newsrc file.
13278   (let ((newsrc (cdr gnus-newsrc-alist))
13279         info ranges range)
13280     (save-excursion
13281       (set-buffer (create-file-buffer gnus-current-startup-file))
13282       (set-visited-file-name gnus-current-startup-file)
13283       (buffer-disable-undo (current-buffer))
13284       (erase-buffer)
13285       ;; Write options.
13286       (if gnus-newsrc-options (insert gnus-newsrc-options))
13287       ;; Write subscribed and unsubscribed.
13288       (while newsrc
13289         (setq info (car newsrc))
13290         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13291             (progn
13292               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13293                                      "!" ":"))
13294               (if (setq ranges (nth 2 info))
13295                   (progn
13296                     (insert " ")
13297                     (if (not (listp (cdr ranges)))
13298                         (if (= (car ranges) (cdr ranges))
13299                             (insert (int-to-string (car ranges)))
13300                           (insert (int-to-string (car ranges)) "-" 
13301                                   (int-to-string (cdr ranges))))
13302                       (while ranges
13303                         (setq range (car ranges)
13304                               ranges (cdr ranges))
13305                         (if (or (atom range) (= (car range) (cdr range)))
13306                             (insert (int-to-string 
13307                                      (or (and (atom range) range) 
13308                                          (car range))))
13309                           (insert (int-to-string (car range)) "-"
13310                                   (int-to-string (cdr range))))
13311                         (if ranges (insert ","))))))
13312               (insert "\n")))
13313         (setq newsrc (cdr newsrc)))
13314       (save-buffer)
13315       (kill-buffer (current-buffer)))))
13316
13317 (defun gnus-read-all-descriptions-files ()
13318   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13319     (while methods
13320       (gnus-read-descriptions-file (car methods))
13321       (setq methods (cdr methods)))
13322     t))
13323
13324 (defun gnus-read-descriptions-file (&optional method)
13325   (let ((method (or method gnus-select-method)))
13326     ;; We create the hashtable whether we manage to read the desc file
13327     ;; to avoid trying to re-read after a failed read.
13328     (or gnus-description-hashtb
13329         (setq gnus-description-hashtb 
13330               (gnus-make-hashtable (length gnus-active-hashtb))))
13331     ;; Mark this method's desc file as read.
13332     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13333                   gnus-description-hashtb)
13334
13335     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13336     (cond 
13337      ((not (or (gnus-server-opened method)
13338                (gnus-open-server method)))
13339       (gnus-message 1 "Couldn't open server")
13340       nil)
13341      ((not (gnus-request-list-newsgroups method))
13342       (gnus-message 1 "Couldn't read newsgroups descriptions")
13343       nil)
13344      (t
13345       (let (group)
13346         (save-excursion
13347           (save-restriction
13348             (set-buffer nntp-server-buffer)
13349             (goto-char (point-min))
13350             (if (or (search-forward "\n.\n" nil t)
13351                     (goto-char (point-max)))
13352                 (progn
13353                   (beginning-of-line)
13354                   (narrow-to-region (point-min) (point))))
13355             (goto-char (point-min))
13356             (while (not (eobp))
13357               ;; If we get an error, we set group to 0, which is not a
13358               ;; symbol... 
13359               (setq group 
13360                     (condition-case ()
13361                         (let ((obarray gnus-description-hashtb))
13362                           ;; Group is set to a symbol interned in this
13363                           ;; hash table.
13364                           (read nntp-server-buffer))
13365                       (error 0)))
13366               (skip-chars-forward " \t")
13367               ;; ... which leads to this line being effectively ignored.
13368               (and (symbolp group)
13369                    (set group (buffer-substring 
13370                                (point) (progn (end-of-line) (point)))))
13371               (forward-line 1))))
13372         (gnus-message 5 "Reading descriptions file...done")
13373         t)))))
13374
13375 (defun gnus-group-get-description (group)
13376   ;; Get the description of a group by sending XGTITLE to the server.
13377   (and (gnus-request-group-description group)
13378        (save-excursion
13379          (set-buffer nntp-server-buffer)
13380          (goto-char (point-min))
13381          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13382               (buffer-substring (match-beginning 1) (match-end 1))))))
13383
13384 ;;;
13385 ;;; Server
13386 ;;;
13387
13388 (defvar gnus-server-mode-hook nil
13389   "Hook run in `gnus-server-mode' buffers.")
13390
13391 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13392   "Format of server lines.
13393 It works along the same lines as a normal formatting string,
13394 with some simple extensions.")
13395
13396 (defvar gnus-server-mode-line-format "(ding) List of servers"
13397   "The format specification for the server mode line.")
13398
13399 (defconst gnus-server-line-format-alist
13400   (list (list ?h 'how ?s)
13401         (list ?n 'name ?s)
13402         (list ?w 'where ?s)
13403         ))
13404
13405 (defconst gnus-server-mode-line-format-alist 
13406   (list (list ?S 'news-server ?s)
13407         (list ?M 'news-method ?s)
13408         (list ?u 'user-defined ?s)))
13409
13410 (defvar gnus-server-line-format-spec nil)
13411 (defvar gnus-server-mode-line-format-spec nil)
13412 (defvar gnus-server-killed-servers nil)
13413
13414 (defvar gnus-server-mode-map nil)
13415 (put 'gnus-server-mode 'mode-class 'special)
13416
13417 (if gnus-server-mode-map
13418     nil
13419   (setq gnus-server-mode-map (make-sparse-keymap))
13420   (suppress-keymap gnus-server-mode-map)
13421   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13422   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13423   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13424   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13425   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13426   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13427   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13428   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13429   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13430   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13431
13432 (defun gnus-server-mode ()
13433   "Major mode for listing and editing servers.
13434
13435 All normal editing commands are switched off.
13436 \\<gnus-server-mode-map>
13437
13438 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13439
13440 The following commands are available:
13441
13442 \\{gnus-server-mode-map}"
13443   (interactive)
13444   (if gnus-visual (gnus-server-make-menu-bar))
13445   (kill-all-local-variables)
13446   (setq mode-line-modified "-- ")
13447   (make-local-variable 'mode-line-format)
13448   (setq mode-line-format (copy-sequence mode-line-format))
13449   (and (equal (nth 3 mode-line-format) "   ")
13450        (setcar (nthcdr 3 mode-line-format) ""))
13451   (setq major-mode 'gnus-server-mode)
13452   (setq mode-name "Server")
13453 ;  (gnus-group-set-mode-line)
13454   (setq mode-line-process nil)
13455   (use-local-map gnus-server-mode-map)
13456   (buffer-disable-undo (current-buffer))
13457   (setq truncate-lines t)
13458   (setq buffer-read-only t)
13459   (run-hooks 'gnus-server-mode-hook))
13460
13461 (defun gnus-server-insert-server-line (sformat name method)
13462   (let* ((sformat (or sformat gnus-server-line-format-spec))
13463          (how (car method))
13464          (where (nth 1 method))
13465          b)
13466     (beginning-of-line)
13467     (setq b (point))
13468     ;; Insert the text.
13469     (insert (eval sformat))
13470     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13471
13472 (defun gnus-server-setup-buffer ()
13473   (if (get-buffer gnus-server-buffer)
13474       ()
13475     (save-excursion
13476       (set-buffer (get-buffer-create gnus-server-buffer))
13477       (gnus-server-mode)
13478       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13479
13480 (defun gnus-server-prepare ()
13481   (setq gnus-server-mode-line-format-spec 
13482         (gnus-parse-format gnus-server-mode-line-format 
13483                            gnus-server-mode-line-format-alist))
13484   (setq gnus-server-line-format-spec 
13485         (gnus-parse-format gnus-server-line-format 
13486                            gnus-server-line-format-alist))
13487   (let ((alist gnus-server-alist)
13488         (buffer-read-only nil))
13489     (erase-buffer)
13490     (while alist
13491       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13492       (setq alist (cdr alist))))
13493   (goto-char (point-min))
13494   (gnus-server-position-cursor))
13495
13496 (defun gnus-server-server-name ()
13497   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13498     (and server (symbol-name server))))
13499
13500 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13501
13502 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13503
13504 (defun gnus-server-update-server (server)
13505   (save-excursion
13506     (set-buffer gnus-server-buffer)
13507     (let ((buffer-read-only nil)
13508           (info (cdr (assoc server gnus-server-alist))))
13509       (gnus-dribble-enter 
13510        (concat "(gnus-server-set-info \"" server "\" '"
13511                (prin1-to-string info) ")"))
13512       ;; Buffer may be narrowed.
13513       (save-restriction
13514         (widen)
13515         (if (gnus-server-goto-server server)
13516             (delete-region (progn (beginning-of-line) (point))
13517                            (progn (forward-line 1) (point))))
13518         (let ((entry (assoc server gnus-server-alist)))
13519           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13520           (gnus-server-position-cursor))))))
13521
13522 (defun gnus-server-set-info (server info)
13523   ;; Enter a select method into the virtual server alist.
13524   (gnus-dribble-enter 
13525    (concat "(gnus-server-set-info \"" server "\" '"
13526            (prin1-to-string info) ")"))
13527   (let* ((server (nth 1 info))
13528          (entry (assoc server gnus-server-alist)))
13529     (if entry (setcdr entry info)
13530       (setq gnus-server-alist
13531             (nconc gnus-server-alist (list (cons server info)))))))
13532
13533 (defun gnus-server-to-method (server)
13534   ;; Map virtual server names to select methods.
13535   (or (and (equal server "native") gnus-select-method)
13536       (cdr (assoc server gnus-server-alist))))
13537
13538 (defun gnus-server-extend-method (group method)
13539   ;; This function "extends" a virtual server.  If the server is
13540   ;; "hello", and the select method is ("hello" (my-var "something")) 
13541   ;; in the group "alt.alt", this will result in a new virtual server
13542   ;; called "helly+alt.alt".
13543   (let ((entry
13544          (gnus-copy-sequence 
13545           (if (equal (car method) "native") gnus-select-method
13546               (cdr (assoc (car method) gnus-server-alist))))))
13547     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13548     (nconc entry (cdr method))))
13549
13550 (defun gnus-server-get-method (group method)
13551   ;; Input either a server name, and extended server name, or a
13552   ;; select method, and return a select method. 
13553   (cond ((stringp method)
13554          (gnus-server-to-method method))
13555         ((and (stringp (car method)) group)
13556          (gnus-server-extend-method group method))
13557         (t
13558          (gnus-server-add-address method))))
13559
13560 (defun gnus-server-add-address (method)
13561   (let ((method-name (symbol-name (car method))))
13562     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13563              (not (assq (intern (concat method-name "-address")) method)))
13564         (append method (list (list (intern (concat method-name "-address"))
13565                                    (nth 1 method))))
13566       method)))
13567
13568 (defun gnus-server-equal (s1 s2)
13569   (or (equal s1 s2)
13570       (and (= (length s1) (length s2))
13571            (progn
13572              (while (and s1 (member (car s1) s2))
13573                (setq s1 (cdr s1)))
13574              (null s1)))))
13575
13576 ;;; Interactive server functions.
13577
13578 (defun gnus-server-kill-server (server)
13579   "Kill the server on the current line."
13580   (interactive (list (gnus-server-server-name)))
13581   (or (gnus-server-goto-server server)
13582       (if server (error "No such server: %s" server)
13583         (error "No server on the current line")))
13584   (let ((buffer-read-only nil))
13585     (delete-region (progn (beginning-of-line) (point))
13586                    (progn (forward-line 1) (point))))
13587   (setq gnus-server-killed-servers 
13588         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13589   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13590                                 gnus-server-alist))
13591   (gnus-server-position-cursor))
13592
13593 (defun gnus-server-yank-server ()
13594   "Yank the previously killed server."
13595   (interactive)
13596   (or gnus-server-killed-servers
13597       (error "No killed servers to be yanked"))
13598   (let ((alist gnus-server-alist)
13599         (server (gnus-server-server-name))
13600         (killed (car gnus-server-killed-servers)))
13601     (if (not server) 
13602         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13603       (if (string= server (car (car gnus-server-alist)))
13604           (setq gnus-server-alist (cons killed gnus-server-alist))
13605         (while (and (cdr alist)
13606                     (not (string= server (car (car (cdr alist))))))
13607           (setq alist (cdr alist)))
13608         (setcdr alist (cons killed (cdr alist)))))
13609     (gnus-server-update-server (car killed))
13610     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13611     (gnus-server-position-cursor)))
13612
13613 (defun gnus-server-exit ()
13614   "Return to the group buffer."
13615   (interactive)
13616   (kill-buffer (current-buffer))
13617   (switch-to-buffer gnus-group-buffer))
13618
13619 (defun gnus-server-list-servers ()
13620   "List all available servers."
13621   (interactive)
13622   (let ((cur (gnus-server-server-name)))
13623     (gnus-server-prepare)
13624     (if cur (gnus-server-goto-server cur)
13625       (goto-char (point-max))
13626       (forward-line -1))
13627     (gnus-server-position-cursor)))
13628
13629 (defun gnus-server-copy-server (from to)
13630   (interactive
13631    (list
13632     (or (gnus-server-server-name)
13633         (error "No server on the current line"))
13634     (read-string "Copy to: ")))
13635   (or from (error "No server on current line"))
13636   (or (and to (not (string= to ""))) (error "No name to copy to"))
13637   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13638   (or (assoc from gnus-server-alist) 
13639       (error "%s: no such server" from))
13640   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13641     (setcar to-entry to)
13642     (setcar (nthcdr 2 to-entry) to)
13643     (setq gnus-server-killed-servers 
13644           (cons to-entry gnus-server-killed-servers))
13645     (gnus-server-yank-server)))
13646
13647 (defun gnus-server-add-server (how where)
13648   (interactive 
13649    (list (intern (completing-read "Server method: "
13650                                   gnus-valid-select-methods nil t))
13651          (read-string "Server name: ")))
13652   (setq gnus-server-killed-servers 
13653         (cons (list where how where) gnus-server-killed-servers))
13654   (gnus-server-yank-server))
13655
13656 (defun gnus-server-goto-server (server)
13657   "Jump to a server line."
13658   (interactive
13659    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13660   (let ((to (text-property-any (point-min) (point-max) 
13661                                'gnus-server (intern server))))
13662     (and to
13663          (progn
13664            (goto-char to) 
13665            (gnus-server-position-cursor)))))
13666
13667 (defun gnus-server-edit-server (server)
13668   "Edit the server on the current line."
13669   (interactive (list (gnus-server-server-name)))
13670   (or server
13671       (error "No server on current line"))
13672   (let ((winconf (current-window-configuration)))
13673     (get-buffer-create gnus-server-edit-buffer)
13674     (gnus-configure-windows 'edit-server)
13675     (gnus-add-current-to-buffer-list)
13676     (emacs-lisp-mode)
13677     (make-local-variable 'gnus-prev-winconf)
13678     (setq gnus-prev-winconf winconf)
13679     (use-local-map (copy-keymap (current-local-map)))
13680     (let ((done-func '(lambda () 
13681                         "Exit editing mode and update the information."
13682                         (interactive)
13683                         (gnus-server-edit-server-done 'group))))
13684       (setcar (cdr (nth 4 done-func)) server)
13685       (local-set-key "\C-c\C-c" done-func))
13686     (erase-buffer)
13687     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13688     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13689
13690 (defun gnus-server-edit-server-done (server)
13691   (interactive)
13692   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13693   (goto-char (point-min))
13694   (let ((form (read (current-buffer)))
13695         (winconf gnus-prev-winconf))
13696     (gnus-server-set-info server form)
13697     (kill-buffer (current-buffer))
13698     (and winconf (set-window-configuration winconf))
13699     (set-buffer gnus-server-buffer)
13700     (gnus-server-update-server (gnus-server-server-name))
13701     (gnus-server-position-cursor)))
13702
13703 (defun gnus-server-read-server (server)
13704   "Browse a server."
13705   (interactive (list (gnus-server-server-name)))
13706   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13707
13708 (defun gnus-mouse-pick-server (e)
13709   (interactive "e")
13710   (mouse-set-point e)
13711   (gnus-server-read-server (gnus-server-server-name)))
13712
13713 ;;;
13714 ;;; entry points into gnus-score.el
13715 ;;;
13716
13717 ;;; Finding score files. 
13718
13719 (defvar gnus-global-score-files nil
13720   "*List of global score files and directories.
13721 Set this variable if you want to use people's score files.  One entry
13722 for each score file or each score file directory.  Gnus will decide
13723 by itself what score files are applicable to which group.
13724
13725 Say you want to use the single score file
13726 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13727 score files in the \"/ftp.some-where:/pub/score\" directory.
13728
13729  (setq gnus-global-score-files
13730        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13731          \"/ftp.some-where:/pub/score\"))")
13732
13733 (defun gnus-score-score-files (group)
13734   "Return a list of all possible score files."
13735   ;; Search and set any global score files.
13736   (and gnus-global-score-files 
13737        (or gnus-internal-global-score-files
13738            (gnus-score-search-global-directories gnus-global-score-files)))
13739   ;; Fix the kill-file dir variable.
13740   (setq gnus-kill-files-directory 
13741         (file-name-as-directory
13742          (or gnus-kill-files-directory "~/News/")))
13743   ;; If we can't read it, there are no score files.
13744   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13745       (setq gnus-score-file-list nil)
13746     (if (gnus-use-long-file-name 'not-score)
13747         ;; We want long file names.
13748         (if (or (not gnus-score-file-list)
13749                 (not (car gnus-score-file-list))
13750                 (gnus-file-newer-than gnus-kill-files-directory
13751                                       (car gnus-score-file-list)))
13752               (setq gnus-score-file-list 
13753                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13754                           (nreverse 
13755                            (directory-files 
13756                             gnus-kill-files-directory t 
13757                             (gnus-score-file-regexp))))))
13758       ;; We do not use long file names, so we have to do some
13759       ;; directory traversing.  
13760       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13761             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13762             dir files suffix)
13763         (while suffixes
13764           (setq dir (expand-file-name
13765                      (concat gnus-kill-files-directory
13766                              (gnus-replace-chars-in-string group ?. ?/))))
13767           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13768           (setq suffix (car suffixes)
13769                 suffixes (cdr suffixes))
13770           (if (file-exists-p (concat dir "/" suffix))
13771               (setq files (cons (concat dir "/" suffix) files)))
13772           (while (>= (1+ (length dir)) mdir)
13773             (and (file-exists-p (concat dir "/all/" suffix))
13774                  (setq files (cons (concat dir "/all/" suffix) files)))
13775             (string-match "/[^/]*$" dir)
13776             (setq dir (substring dir 0 (match-beginning 0)))))
13777         (setq gnus-score-file-list 
13778               (cons nil (nreverse files)))))
13779     (cdr gnus-score-file-list)))
13780
13781 (defun gnus-score-file-regexp ()
13782   (concat "\\(" gnus-score-file-suffix 
13783           "\\|" gnus-adaptive-file-suffix "\\)$"))
13784         
13785 (defun gnus-score-find-bnews (group)
13786   "Return a list of score files for GROUP.
13787 The score files are those files in the ~/News directory which matches
13788 GROUP using BNews sys file syntax."
13789   (let* ((sfiles (append (gnus-score-score-files group)
13790                          gnus-internal-global-score-files))
13791          (kill-dir (file-name-as-directory 
13792                     (expand-file-name gnus-kill-files-directory)))
13793          (klen (length kill-dir))
13794          ofiles not-match regexp)
13795     (save-excursion
13796       (set-buffer (get-buffer-create "*gnus score files*"))
13797       (buffer-disable-undo (current-buffer))
13798       ;; Go through all score file names and create regexp with them
13799       ;; as the source.  
13800       (while sfiles
13801         (erase-buffer)
13802         (insert (car sfiles))
13803         (goto-char (point-min))
13804         ;; First remove the suffix itself.
13805         (re-search-forward (concat "." (gnus-score-file-regexp)))
13806         (replace-match "" t t) 
13807         (goto-char (point-min))
13808         (if (looking-at (regexp-quote kill-dir))
13809             ;; If the file name was just "SCORE", `klen' is one character
13810             ;; too much.
13811             (delete-char (min (1- (point-max)) klen))
13812           (goto-char (point-max))
13813           (search-backward "/")
13814           (delete-region (1+ (point)) (point-min)))
13815         ;; If short file names were used, we have to translate slashes.
13816         (goto-char (point-min))
13817         (while (re-search-forward "[/:]" nil t)
13818           (replace-match "." t t))
13819         ;; Translate "all" to ".*".
13820         (while (search-forward "all" nil t)
13821           (replace-match ".*" t t))
13822         (goto-char (point-min))
13823         ;; Deal with "not."s.
13824         (if (looking-at "not.")
13825             (progn
13826               (setq not-match t)
13827               (setq regexp (buffer-substring 5 (point-max))))
13828           (setq regexp (buffer-substring 1 (point-max)))
13829           (setq not-match nil))
13830         ;; Finally - if this resulting regexp matches the group name,
13831         ;; we add this score file to the list of score files
13832         ;; applicable to this group.
13833         (if (or (and not-match
13834                      (not (string-match regexp group)))
13835                 (and (not not-match)
13836                      (string-match regexp group)))
13837             (setq ofiles (cons (car sfiles) ofiles)))
13838         (setq sfiles (cdr sfiles)))
13839       (kill-buffer (current-buffer))
13840       ;; Slight kludge here - the last score file returned should be
13841       ;; the local score file, whether it exists or not. This is so
13842       ;; that any score commands the user enters will go to the right
13843       ;; file, and not end up in some global score file.
13844       (let ((localscore
13845              (expand-file-name
13846               (if (gnus-use-long-file-name 'not-score)
13847                   (concat gnus-kill-files-directory group "." 
13848                           gnus-score-file-suffix)
13849                 (concat gnus-kill-files-directory
13850                         (gnus-replace-chars-in-string group ?. ?/)
13851                         "/" gnus-score-file-suffix)))))
13852         (and (member localscore ofiles)
13853              (delete localscore ofiles))
13854         (setq ofiles (cons localscore ofiles)))
13855       (nreverse ofiles))))
13856
13857 (defun gnus-score-find-single (group)
13858   "Return list containing the score file for GROUP."
13859   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13860         (gnus-score-file-name group)))
13861
13862 (defun gnus-score-find-hierarchical (group)
13863   "Return list of score files for GROUP.
13864 This includes the score file for the group and all its parents."
13865   (let ((all (copy-sequence '(nil)))
13866         (start 0))
13867     (while (string-match "\\." group (1+ start))
13868       (setq start (match-beginning 0))
13869       (setq all (cons (substring group 0 start) all)))
13870     (setq all (cons group all))
13871     (nconc
13872      (mapcar (lambda (newsgroup)
13873                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13874              (setq all (nreverse all)))
13875      (mapcar 'gnus-score-file-name all))))
13876
13877 (defvar gnus-score-file-alist-cache nil)
13878
13879 (defun gnus-score-find-alist (group)
13880   "Return list of score files for GROUP.
13881 The list is determined from the variable gnus-score-file-alist."
13882   (let ((alist gnus-score-file-multiple-match-alist)
13883         score-files)
13884     ;; if this group has been seen before, return the cached entry
13885     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13886         (cdr score-files)      ; ensures caching of groups with no matches
13887       ;; handle the multiple match alist
13888       (while alist
13889         (and (string-match (car (car alist)) group)
13890              (setq score-files
13891                    (nconc score-files (copy-sequence (cdr (car alist))))))
13892         (setq alist (cdr alist)))
13893       (setq alist gnus-score-file-single-match-alist)
13894       ;; handle the single match alist
13895       (while alist
13896         (and (string-match (car (car alist)) group)
13897              ;; progn used just in case ("regexp") has no files
13898              ;; and score-files is still nil. -sj
13899              ;; this can be construed as a "stop searching here" feature :>
13900              ;; and used to simplify regexps in the single-alist 
13901              (progn
13902                (setq score-files
13903                      (nconc score-files (copy-sequence (cdr (car alist)))))
13904                (setq alist nil)))
13905         (setq alist (cdr alist)))
13906       ;; cache the score files
13907       (setq gnus-score-file-alist-cache
13908             (cons (cons group score-files) gnus-score-file-alist-cache))
13909       score-files)))
13910
13911
13912 (defun gnus-possibly-score-headers (&optional trace)
13913   (let ((func gnus-score-find-score-files-function)
13914         score-files)
13915     (and func (not (listp func))
13916          (setq func (list func)))
13917     ;; Go through all the functions for finding score files (or actual
13918     ;; scores) and add them to a list.
13919     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13920     (while func
13921       (and (symbolp (car func))
13922            (fboundp (car func))
13923            (setq score-files 
13924                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13925       (setq func (cdr func)))
13926     (if score-files (gnus-score-headers score-files trace))))
13927
13928 (defun gnus-score-file-name (newsgroup &optional suffix)
13929   "Return the name of a score file for NEWSGROUP."
13930   (let ((suffix (or suffix gnus-score-file-suffix)))
13931     (cond 
13932      ((or (null newsgroup)
13933           (string-equal newsgroup ""))
13934       ;; The global score file is placed at top of the directory.
13935       (expand-file-name 
13936        suffix (or gnus-kill-files-directory "~/News")))
13937      ((gnus-use-long-file-name 'not-score)
13938       ;; Append ".SCORE" to newsgroup name.
13939       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
13940                                 "." suffix)
13941                         (or gnus-kill-files-directory "~/News")))
13942      (t
13943       ;; Place "SCORE" under the hierarchical directory.
13944       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13945                                 "/" suffix)
13946                         (or gnus-kill-files-directory "~/News"))))))
13947
13948 (defun gnus-score-search-global-directories (files)
13949   "Scan all global score directories for score files."
13950   ;; Set the variable `gnus-internal-global-score-files' to all
13951   ;; available global score files.
13952   (interactive (list gnus-global-score-files))
13953   (let (out)
13954     (while files
13955       (if (string-match "/$" (car files))
13956           (setq out (nconc (directory-files 
13957                             (car files) t
13958                             (concat (gnus-score-file-regexp) "$"))))
13959         (setq out (cons (car files) out)))
13960       (setq files (cdr files)))
13961     (setq gnus-internal-global-score-files out)))
13962
13963 ;; Allow redefinition of Gnus functions.
13964
13965 (gnus-ems-redefine)
13966
13967 (provide 'gnus)
13968
13969 ;;; gnus.el ends here