*** 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 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
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 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
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 (add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1096
1097 (defvar gnus-article-x-face-command
1098   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1099   "String or function to be executed to display an X-Face header.
1100 If it is a string, the command will be executed in a sub-shell
1101 asynchronously. The compressed face will be piped to this command.") 
1102
1103 (defvar gnus-article-x-face-too-ugly nil
1104   "Regexp matching posters whose face shouldn't be shown automatically.")
1105
1106 (defvar gnus-select-group-hook nil
1107   "*A hook called when a newsgroup is selected.
1108
1109 If you'd like to simplify subjects like the
1110 `gnus-summary-next-same-subject' command does, you can use the
1111 following hook:
1112
1113  (setq gnus-select-group-hook
1114       (list
1115         (lambda ()
1116           (mapcar (lambda (header)
1117                      (header-set-subject
1118                       header
1119                       (gnus-simplify-subject
1120                        (header-subject header) 're-only)))
1121                   gnus-newsgroup-headers))))")
1122
1123 (defvar gnus-select-article-hook
1124   '(gnus-summary-show-thread)
1125   "*A hook called when an article is selected.
1126 The default hook shows conversation thread subtrees of the selected
1127 article automatically using `gnus-summary-show-thread'.")
1128
1129 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1130   "*A hook called to apply kill files to a group.
1131 This hook is intended to apply a kill file to the selected newsgroup.
1132 The function `gnus-apply-kill-file' is called by default.
1133
1134 Since a general kill file is too heavy to use only for a few
1135 newsgroups, I recommend you to use a lighter hook function. For
1136 example, if you'd like to apply a kill file to articles which contains
1137 a string `rmgroup' in subject in newsgroup `control', you can use the
1138 following hook:
1139
1140 \(setq gnus-apply-kill-hook
1141       (list
1142         (lambda ()
1143           (cond ((string-match \"control\" gnus-newsgroup-name)
1144                  (gnus-kill \"Subject\" \"rmgroup\")
1145                  (gnus-expunge \"X\"))))))")
1146
1147 (defvar gnus-visual-mark-article-hook 
1148   (list 'gnus-highlight-selected-summary)
1149   "*Hook run after selecting an article in the summary buffer.
1150 It is meant to be used for highlighting the article in some way.  It
1151 is not run if `gnus-visual' is nil.")
1152
1153 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1154   "*A hook called after preparing body, but before preparing header headers.
1155 The default hook (`gnus-inews-insert-signature') inserts a signature
1156 file specified by the variable `gnus-signature-file'.")
1157
1158 (defvar gnus-exit-group-hook nil
1159   "*A hook called when exiting (not quitting) summary mode.")
1160
1161 (defvar gnus-suspend-gnus-hook nil
1162   "*A hook called when suspending (not exiting) Gnus.")
1163
1164 (defvar gnus-exit-gnus-hook nil
1165   "*A hook called when exiting Gnus.")
1166
1167 (defvar gnus-save-newsrc-hook nil
1168   "*A hook called when saving the newsrc file.")
1169
1170 (defvar gnus-summary-update-hook 
1171   (list 'gnus-summary-highlight-line)
1172   "*A hook called when a summary line is changed.
1173 The hook will not be called if `gnus-visual' is nil.
1174
1175 The default function `gnus-summary-highlight-line' will
1176 highlight the line according to the `gnus-summary-highlight'
1177 variable.")
1178
1179 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1180   "*A hook called when an article is selected for the first time.
1181 The hook is intended to mark an article as read (or unread)
1182 automatically when it is selected.")
1183
1184 ;; Remove any hilit infestation.
1185 (add-hook 'gnus-startup-hook
1186           (lambda ()
1187             (remove-hook 'gnus-summary-prepare-hook
1188                          'hilit-rehighlight-buffer-quietly)
1189             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1190             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1191             (remove-hook 'gnus-article-prepare-hook
1192                          'hilit-rehighlight-buffer-quietly)))
1193
1194
1195 \f
1196 ;; Internal variables
1197
1198 ;; Avoid highlighting in kill files.
1199 (defvar gnus-summary-inhibit-highlight nil)
1200 (defvar gnus-newsgroup-selected-overlay nil)
1201
1202 (defvar gnus-article-mode-map nil)
1203 (defvar gnus-dribble-buffer nil)
1204 (defvar gnus-headers-retrieved-by nil)
1205 (defvar gnus-article-reply nil)
1206 (defvar gnus-override-method nil)
1207 (defvar gnus-article-check-size nil)
1208
1209 (defvar gnus-current-score-file nil)
1210 (defvar gnus-internal-global-score-files nil)
1211 (defvar gnus-score-file-list nil)
1212
1213
1214 (defvar gnus-current-move-group nil)
1215
1216 (defvar gnus-newsgroup-dependencies nil)
1217 (defvar gnus-newsgroup-threads nil)
1218 (defvar gnus-newsgroup-async nil)
1219 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1220
1221 (defvar gnus-newsgroup-adaptive nil)
1222
1223 (defvar gnus-summary-display-table nil)
1224
1225 (defconst gnus-group-line-format-alist
1226   (list (list ?M 'marked ?c)
1227         (list ?S 'subscribed ?c)
1228         (list ?L 'level ?d)
1229         (list ?N 'number ?s)
1230         (list ?I 'number-of-dormant ?d)
1231         (list ?T 'number-of-ticked ?d)
1232         (list ?R 'number-of-read ?s)
1233         (list ?t 'number-total ?d)
1234         (list ?y 'number-of-unread-unticked ?s)
1235         (list ?i 'number-of-ticked-and-dormant ?d)
1236         (list ?g 'group ?s)
1237         (list ?G 'qualified-group ?s)
1238         (list ?D 'newsgroup-description ?s)
1239         (list ?o 'moderated ?c)
1240         (list ?O 'moderated-string ?s)
1241         (list ?p 'process-marked ?c)
1242         (list ?s 'news-server ?s)
1243         (list ?n 'news-method ?s)
1244         (list ?z 'news-method-string ?s)
1245         (list ?u 'user-defined ?s)))
1246
1247 (defconst gnus-summary-line-format-alist 
1248   (list (list ?N 'number ?d)
1249         (list ?S 'subject ?s)
1250         (list ?s 'subject-or-nil ?s)
1251         (list ?n 'name ?s)
1252         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1253               ?s)
1254         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1255                       from) ?s)
1256         (list ?F 'from ?s)
1257         (list ?x (macroexpand '(header-xref header)) ?s)
1258         (list ?D (macroexpand '(header-date header)) ?s)
1259         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1260         (list ?M (macroexpand '(header-id header)) ?s)
1261         (list ?r (macroexpand '(header-references header)) ?s)
1262         (list ?c '(or (header-chars header) 0) ?d)
1263         (list ?L 'lines ?d)
1264         (list ?I 'indentation ?s)
1265         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1266         (list ?R 'replied ?c)
1267         (list ?\[ 'opening-bracket ?c)
1268         (list ?\] 'closing-bracket ?c)
1269         (list ?\> '(make-string level ? ) ?s)
1270         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1271         (list ?i 'score ?d)
1272         (list ?z 'score-char ?c)
1273         (list ?U 'unread ?c)
1274         (list ?t '(gnus-summary-number-of-articles-in-thread 
1275                    (and (boundp 'thread) (car thread)))
1276               ?d)
1277         (list ?e '(gnus-summary-number-of-articles-in-thread 
1278                    (and (boundp 'thread) (car thread)) t)
1279               ?c)
1280         (list ?u 'user-defined ?s))
1281   "An alist of format specifications that can appear in summary lines,
1282 and what variables they correspond with, along with the type of the
1283 variable (string, integer, character, etc).")
1284
1285 (defconst gnus-summary-dummy-line-format-alist
1286   (list (list ?S 'subject ?s)
1287         (list ?N 'number ?d)
1288         (list ?u 'user-defined ?s)))
1289
1290 (defconst gnus-summary-mode-line-format-alist 
1291   (list (list ?G 'group-name ?s)
1292         (list ?g '(gnus-short-group-name group-name) ?s)
1293         (list ?A 'article-number ?d)
1294         (list ?Z 'unread-and-unselected ?s)
1295         (list ?V 'gnus-version ?s)
1296         (list ?U 'unread ?d)
1297         (list ?S 'subject ?s)
1298         (list ?e 'unselected ?d)
1299         (list ?u 'user-defined ?s)
1300         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1301
1302 (defconst gnus-group-mode-line-format-alist 
1303   (list (list ?S 'news-server ?s)
1304         (list ?M 'news-method ?s)
1305         (list ?u 'user-defined ?s)))
1306
1307 (defvar gnus-have-read-active-file nil)
1308
1309 (defconst gnus-maintainer
1310   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1311   "The mail address of the Gnus maintainers.")
1312
1313 (defconst gnus-version "(ding) Gnus v0.99.12"
1314   "Version number for this version of Gnus.")
1315
1316 (defvar gnus-info-nodes
1317   '((gnus-group-mode            "(gnus)The Group Buffer")
1318     (gnus-summary-mode          "(gnus)The Summary Buffer")
1319     (gnus-article-mode          "(gnus)The Article Buffer"))
1320   "Assoc list of major modes and related Info nodes.")
1321
1322 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1323   "The location of the (ding) Gnus documentation group.")
1324
1325 (defvar gnus-group-buffer "*Group*")
1326 (defvar gnus-summary-buffer "*Summary*")
1327 (defvar gnus-article-buffer "*Article*")
1328 (defvar gnus-server-buffer "*Server*")
1329
1330 (defvar gnus-work-buffer " *gnus work*")
1331
1332 (defvar gnus-buffer-list nil
1333   "Gnus buffers that should be killed on exit.")
1334
1335 (defvar gnus-server-alist nil
1336   "List of available servers.")
1337
1338 (defvar gnus-variable-list
1339   '(gnus-newsrc-options gnus-newsrc-options-n
1340     gnus-newsrc-last-checked-date 
1341     gnus-newsrc-alist gnus-server-alist
1342     gnus-killed-list gnus-zombie-list)
1343   "Gnus variables saved in the quick startup file.")
1344
1345 (defvar gnus-overload-functions
1346   '((news-inews gnus-inews-news "rnewspost"))
1347   "Functions overloaded by gnus.
1348 It is a list of `(original overload &optional file)'.")
1349
1350 (defvar gnus-newsrc-options nil
1351   "Options line in the .newsrc file.")
1352
1353 (defvar gnus-newsrc-options-n nil
1354   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1355
1356 (defvar gnus-newsrc-last-checked-date nil
1357   "Date Gnus last asked server for new newsgroups.")
1358
1359 (defvar gnus-newsrc-alist nil
1360   "Assoc list of read articles.
1361 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1362
1363 (defvar gnus-newsrc-hashtb nil
1364   "Hashtable of gnus-newsrc-alist.")
1365
1366 (defvar gnus-killed-list nil
1367   "List of killed newsgroups.")
1368
1369 (defvar gnus-killed-hashtb nil
1370   "Hash table equivalent of gnus-killed-list.")
1371
1372 (defvar gnus-zombie-list nil
1373   "List of almost dead newsgroups.")
1374
1375 (defvar gnus-description-hashtb nil
1376   "Descriptions of newsgroups.")
1377
1378 (defvar gnus-list-of-killed-groups nil
1379   "List of newsgroups that have recently been killed by the user.")
1380
1381 (defvar gnus-active-hashtb nil
1382   "Hashtable of active articles.")
1383
1384 (defvar gnus-moderated-list nil
1385   "List of moderated newsgroups.")
1386
1387 (defvar gnus-group-marked nil)
1388
1389 (defvar gnus-current-startup-file nil
1390   "Startup file for the current host.")
1391
1392 (defvar gnus-last-search-regexp nil
1393   "Default regexp for article search command.")
1394
1395 (defvar gnus-last-shell-command nil
1396   "Default shell command on article.")
1397
1398 (defvar gnus-current-select-method nil
1399   "The current method for selecting a newsgroup.")
1400
1401 (defvar gnus-group-list-mode nil)
1402
1403 (defvar gnus-article-internal-prepare-hook nil)
1404
1405 (defvar gnus-newsgroup-name nil)
1406 (defvar gnus-newsgroup-begin nil)
1407 (defvar gnus-newsgroup-end nil)
1408 (defvar gnus-newsgroup-last-rmail nil)
1409 (defvar gnus-newsgroup-last-mail nil)
1410 (defvar gnus-newsgroup-last-folder nil)
1411 (defvar gnus-newsgroup-last-file nil)
1412 (defvar gnus-newsgroup-auto-expire nil)
1413 (defvar gnus-newsgroup-active nil)
1414
1415 (defvar gnus-newsgroup-unreads nil
1416   "List of unread articles in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-unselected nil
1419   "List of unselected unread articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-reads nil
1422   "Alist of read articles and article marks in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-marked nil
1425   "List of ticked articles in the current newsgroup (a subset of unread art).")
1426
1427 (defvar gnus-newsgroup-killed nil
1428   "List of ranges of articles that have been through the scoring process.")
1429
1430 (defvar gnus-newsgroup-kill-headers nil)
1431
1432 (defvar gnus-newsgroup-replied nil
1433   "List of articles that have been replied to in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-expirable nil
1436   "List of articles in the current newsgroup that can be expired.")
1437
1438 (defvar gnus-newsgroup-processable nil
1439   "List of articles in the current newsgroup that can be processed.")
1440
1441 (defvar gnus-newsgroup-bookmarks nil
1442   "List of articles in the current newsgroup that have bookmarks.")
1443
1444 (defvar gnus-newsgroup-dormant nil
1445   "List of dormant articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-scored nil
1448   "List of scored articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-headers nil
1451   "List of article headers in the current newsgroup.")
1452 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1453
1454 (defvar gnus-newsgroup-ancient nil
1455   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1456
1457 (defvar gnus-current-article nil)
1458 (defvar gnus-article-current nil)
1459 (defvar gnus-current-headers nil)
1460 (defvar gnus-have-all-headers nil)
1461 (defvar gnus-last-article nil)
1462 (defvar gnus-newsgroup-history nil)
1463 (defvar gnus-current-kill-article nil)
1464
1465 ;; Save window configuration.
1466 (defvar gnus-prev-winconf nil)
1467
1468 ;; Format specs
1469 (defvar gnus-summary-line-format-spec nil)
1470 (defvar gnus-summary-dummy-line-format-spec nil)
1471 (defvar gnus-group-line-format-spec nil)
1472 (defvar gnus-summary-mode-line-format-spec nil)
1473 (defvar gnus-article-mode-line-format-spec nil)
1474 (defvar gnus-group-mode-line-format-spec nil)
1475 (defvar gnus-summary-mark-positions nil)
1476 (defvar gnus-group-mark-positions nil)
1477
1478 (defvar gnus-summary-expunge-below nil)
1479 (defvar gnus-reffed-article-number nil)
1480
1481 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1482 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1483
1484 (defvar gnus-cache-removeable-articles nil)
1485
1486 (defconst gnus-summary-local-variables 
1487   '(gnus-newsgroup-name 
1488     gnus-newsgroup-begin gnus-newsgroup-end 
1489     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1490     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1491     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1492     gnus-newsgroup-unselected gnus-newsgroup-marked
1493     gnus-newsgroup-reads
1494     gnus-newsgroup-replied gnus-newsgroup-expirable
1495     gnus-newsgroup-processable gnus-newsgroup-killed
1496     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1497     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1498     gnus-current-article gnus-current-headers gnus-have-all-headers
1499     gnus-last-article gnus-article-internal-prepare-hook
1500     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1501     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1502     gnus-newsgroup-threads gnus-newsgroup-async
1503     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1504     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1505     gnus-newsgroup-history gnus-newsgroup-ancient
1506     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1507     gnus-cache-removeable-articles)
1508   "Variables that are buffer-local to the summary buffers.")
1509
1510 (defconst gnus-bug-message
1511   "Sending a bug report to the Gnus Towers.
1512 ========================================
1513
1514 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1515 be sent to the Gnus Bug Exterminators. 
1516
1517 At the bottom of the buffer you'll see lots of variable settings.
1518 Please do not delete those.  They will tell the Bug People what your
1519 environment is, so that it will be easier to locate the bugs.
1520
1521 If you have found a bug that makes Emacs go \"beep\", set
1522 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1523 and include the backtrace in your bug report.
1524
1525 Please describe the bug in annoying, painstaking detail.
1526
1527 Thank you for your help in stamping out bugs.
1528 ")
1529
1530 ;;; End of variables.
1531
1532 ;; Define some autoload functions Gnus might use.
1533 (eval-and-compile
1534
1535   ;; Various 
1536   (autoload 'metamail-buffer "metamail")
1537   (autoload 'Info-goto-node "info")
1538   (autoload 'hexl-hex-string-to-integer "hexl")
1539   (autoload 'pp "pp")
1540   (autoload 'pp-to-string "pp")
1541   (autoload 'pp-eval-expression "pp")
1542   (autoload 'mail-extract-address-components "mail-extr")
1543
1544   (autoload 'nnmail-split-fancy "nnmail")
1545   (autoload 'nnvirtual-catchup-group "nnvirtual")
1546
1547   ;; timezone
1548   (autoload 'timezone-make-date-arpa-standard "timezone")
1549   (autoload 'timezone-fix-time "timezone")
1550   (autoload 'timezone-make-sortable-date "timezone")
1551   (autoload 'timezone-make-time-string "timezone")
1552
1553   ;; rmail & friends
1554   (autoload 'mail-position-on-field "sendmail")
1555   (autoload 'mail-setup "sendmail")
1556   (autoload 'rmail-output "rmailout")
1557   (autoload 'news-mail-other-window "rnewspost")
1558   (autoload 'news-reply-yank-original "rnewspost")
1559   (autoload 'news-caesar-buffer-body "rnewspost")
1560   (autoload 'rmail-insert-rmail-file-header "rmail")
1561   (autoload 'rmail-count-new-messages "rmail")
1562   (autoload 'rmail-show-message "rmail")
1563
1564   ;; gnus-soup
1565   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1566   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1567   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1568   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1569   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1570   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1571   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1572
1573   ;; gnus-mh
1574   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1575   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1576   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1577   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1578   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1579   (autoload 'gnus-Folder-save-name "gnus-mh")
1580   (autoload 'gnus-folder-save-name "gnus-mh")
1581
1582   ;; gnus-vis misc
1583   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1584   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1585   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1586   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1587   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1588   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1589   (autoload 'gnus-summary-highlight-line "gnus-vis")
1590   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1591
1592   ;; gnus-vis article
1593   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1594   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1595   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1596   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1597   (autoload 'gnus-article-hide "gnus-vis" nil t)
1598   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1599   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1600   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1601   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1602   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1603   (autoload 'gnus-article-add-button "gnus-vis")
1604
1605   ;; gnus-cite
1606   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1607   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1608   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1609
1610   ;; gnus-kill
1611   (autoload 'gnus-kill "gnus-kill")
1612   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1613   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1614   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1615   (autoload 'gnus-execute "gnus-kill")
1616   (autoload 'gnus-expunge "gnus-kill")
1617
1618   ;; gnus-cache
1619   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1620   (autoload 'gnus-cache-save-buffers "gnus-cache")
1621   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1622   (autoload 'gnus-cache-request-article "gnus-cache")
1623   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1624   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1625   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1626   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1627
1628   ;; gnus-score
1629   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1630   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1631   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1632   (autoload 'gnus-score-save "gnus-score")
1633   (autoload 'gnus-score-headers "gnus-score")
1634   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1635   (autoload 'gnus-score-adaptive "gnus-score")
1636   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1637   (autoload 'gnus-score-find-trace "gnus-score")
1638
1639   ;; gnus-edit
1640   (autoload 'gnus-score-customize "gnus-edit" nil t)
1641
1642   ;; gnus-uu
1643   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1644   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1645   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1646   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1647   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1648   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1649   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1650   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1651   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1652   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1657   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1658   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1662   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1663   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1664   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1665
1666   ;; gnus-msg
1667   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1668   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1669   (autoload 'gnus-group-mail "gnus-msg" nil t)
1670   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1671   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1672   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1673   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1674   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1675   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1676   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1677   (autoload 'gnus-post-news "gnus-msg" nil t)
1678   (autoload 'gnus-inews-news "gnus-msg" nil t)
1679   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1680   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1681   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1682   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1683   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1684   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1685   (autoload 'gnus-mail-yank-original "gnus-msg")
1686   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1687   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1688   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1689   (autoload 'gnus-article-mail-with-original "gnus-msg")
1690   (autoload 'gnus-article-mail "gnus-msg")
1691   (autoload 'gnus-bug "gnus-msg" nil t)
1692
1693   ;; gnus-vm
1694   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1695   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1696   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1697   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1698   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1699   (autoload 'gnus-yank-article "gnus-vm" nil t)
1700
1701   )
1702
1703 \f
1704
1705 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1706 ;; If you want the cursor to go somewhere else, set these two
1707 ;; functions in some startup hook to whatever you want.
1708 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1709 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1710
1711 ;;; Various macros and substs.
1712
1713 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1714   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1715   (` (let ((GnusStartBufferWindow (selected-window)))
1716        (unwind-protect
1717            (progn
1718              (pop-to-buffer (, buffer))
1719              (,@ forms))
1720          (select-window GnusStartBufferWindow)))))
1721
1722 (defmacro gnus-gethash (string hashtable)
1723   "Get hash value of STRING in HASHTABLE."
1724   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1725   ;;(` (abbrev-expansion (, string) (, hashtable)))
1726   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1727
1728 (defmacro gnus-sethash (string value hashtable)
1729   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1730   ;; We cannot use define-abbrev since it only accepts string as value.
1731   ;; (set (intern string hashtable) value))
1732   (` (set (intern (, string) (, hashtable)) (, value))))
1733
1734 (defsubst gnus-buffer-substring (beg end)
1735   (buffer-substring (match-beginning beg) (match-end end)))
1736
1737 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1738 ;;   function `substring' might cut on a middle of multi-octet
1739 ;;   character.
1740 (defun gnus-truncate-string (str width)
1741   (substring str 0 width))
1742
1743 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1744 ;; to limit the length of a string. This function is necessary since
1745 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1746 (defsubst gnus-limit-string (str width)
1747   (if (> (length str) width)
1748       (substring str 0 width)
1749     str))
1750
1751 (defsubst gnus-simplify-subject-re (subject)
1752   "Remove \"Re:\" from subject lines."
1753   (let ((case-fold-search t))
1754     (if (string-match "^re: *" subject)
1755         (substring subject (match-end 0))
1756       subject)))
1757
1758 (defsubst gnus-goto-char (point)
1759   (and point (goto-char point)))
1760
1761 (defmacro gnus-buffer-exists-p (buffer)
1762   (` (and (, buffer)
1763           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1764                    (, buffer)))))
1765
1766 (defmacro gnus-kill-buffer (buffer)
1767   (` (if (gnus-buffer-exists-p (, buffer))
1768          (kill-buffer (, buffer)))))
1769
1770 (defsubst gnus-point-at-bol ()
1771   "Return point at the beginning of line."
1772   (let ((p (point)))
1773     (beginning-of-line)
1774     (prog1
1775         (point)
1776       (goto-char p))))
1777
1778 (defsubst gnus-point-at-eol ()
1779   "Return point at the beginning of line."
1780   (let ((p (point)))
1781     (end-of-line)
1782     (prog1
1783         (point)
1784       (goto-char p))))
1785
1786 ;; Delete the current line (and the next N lines.);
1787 (defmacro gnus-delete-line (&optional n)
1788   (` (delete-region (progn (beginning-of-line) (point))
1789                     (progn (forward-line (, (or n 1))) (point)))))
1790
1791 ;;; Load the compatability functions. 
1792
1793 (require 'gnus-ems)
1794
1795 \f
1796 ;;;
1797 ;;; Gnus Utility Functions
1798 ;;;
1799
1800 (defun gnus-extract-address-components (from)
1801   (let (name address)
1802     ;; First find the address - the thing with the @ in it.  This may
1803     ;; not be accurate in mail addresses, but does the trick most of
1804     ;; the time in news messages.
1805     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1806         (setq address (substring from (match-beginning 0) (match-end 0))))
1807     ;; Then we check whether the "name <address>" format is used.
1808     (and address
1809          (string-match (concat "<" (regexp-quote address) ">") from)
1810          (and (setq name (substring from 0 (1- (match-beginning 0))))
1811               ;; Strip any quotes from the name.
1812               (string-match "\".*\"" name)
1813               (setq name (substring name 1 (1- (match-end 0))))))
1814     ;; If not, then "address (name)" is used.
1815     (or name
1816         (and (string-match "(.+)" from)
1817              (setq name (substring from (1+ (match-beginning 0)) 
1818                                    (1- (match-end 0)))))
1819         (and (string-match "()" from)
1820              (setq name address))
1821         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1822         ;; XOVER might not support folded From headers.
1823         (and (string-match "(.*" from)
1824              (setq name (substring from (1+ (match-beginning 0)) 
1825                                    (match-end 0)))))
1826     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1827     (list (or name from) (or address from))))
1828
1829 (defun gnus-fetch-field (field)
1830   "Return the value of the header FIELD of current article."
1831   (save-excursion
1832     (save-restriction
1833       (let ((case-fold-search t))
1834         (gnus-narrow-to-headers)
1835         (mail-fetch-field field)))))
1836
1837 (defun gnus-goto-colon ()
1838   (beginning-of-line)
1839   (search-forward ":" (gnus-point-at-eol) t))
1840
1841 (defun gnus-narrow-to-headers ()
1842   (widen)
1843   (save-excursion
1844     (narrow-to-region
1845      (goto-char (point-min))
1846      (if (search-forward "\n\n" nil t)
1847          (1- (point))
1848        (point-max)))))
1849
1850 (defvar gnus-old-specs nil)
1851
1852 (defun gnus-update-format-specifications ()
1853   (gnus-make-thread-indent-array)
1854
1855   (let ((formats '(summary summary-dummy group 
1856                            summary-mode group-mode article-mode))
1857         old-format new-format)
1858     (while formats
1859       (setq new-format (symbol-value
1860                         (intern (format "gnus-%s-line-format" (car formats)))))
1861       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1862                (equal old-format new-format))
1863           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1864                (gnus-parse-format
1865                 new-format
1866                 (symbol-value 
1867                  (intern (format "gnus-%s-line-format-alist"
1868                                  (if (eq (car formats) 'article-mode)
1869                                      'summary-mode (car formats))))))))
1870       (setq gnus-old-specs (cons (cons (car formats) new-format)
1871                                  (delq (car formats) gnus-old-specs)))
1872       (setq formats (cdr formats))))
1873       
1874   (gnus-update-group-mark-positions)
1875   (gnus-update-summary-mark-positions)
1876
1877   (if (and (string-match "%D" gnus-group-line-format)
1878            (not gnus-description-hashtb)
1879            gnus-read-active-file)
1880       (gnus-read-all-descriptions-files)))
1881
1882 (defun gnus-update-summary-mark-positions ()
1883   (save-excursion
1884     (let ((gnus-replied-mark 129)
1885           (gnus-score-below-mark 130)
1886           (gnus-score-over-mark 130)
1887           (thread nil)
1888           pos)
1889       (gnus-set-work-buffer)
1890       (gnus-summary-insert-line 
1891        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1892       (goto-char (point-min))
1893       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1894                                          (- (point) 2)))))
1895       (goto-char (point-min))
1896       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1897                                           (- (point) 2))) pos))
1898       (goto-char (point-min))
1899       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1900                                         (- (point) 2))) pos))
1901       (setq gnus-summary-mark-positions pos))))
1902
1903 (defun gnus-update-group-mark-positions ()
1904   (save-excursion
1905     (let ((gnus-process-mark 128)
1906           (gnus-group-marked '("dummy.group")))
1907       (gnus-set-work-buffer)
1908       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1909       (goto-char (point-min))
1910       (setq gnus-group-mark-positions
1911             (list (cons 'process (and (search-forward "\200" nil t)
1912                                       (- (point) 2))))))))
1913
1914 (defun gnus-mouse-face-function (form)
1915   (` (let ((string (, form)))
1916        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1917        string)))
1918
1919 (defun gnus-max-width-function (el max-width)
1920   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1921   (` (let* ((val (eval (, el)))
1922             (valstr (if (numberp val)
1923                         (int-to-string val) val)))
1924        (if (> (length valstr) (, max-width))
1925            (substring valstr 0 (, max-width))
1926          valstr))))
1927
1928 (defun gnus-parse-format (format spec-alist)
1929   ;; This function parses the FORMAT string with the help of the
1930   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1931   ;; string.  If the FORMAT string contains the specifiers %( and %)
1932   ;; the text between them will have the mouse-face text property.
1933   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1934       (if (and gnus-visual gnus-mouse-face)
1935           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1936                 (button (substring format (match-beginning 2) (match-end 2)))
1937                 (post (substring format (match-beginning 3) (match-end 3))))
1938             (list 'concat
1939                   (gnus-parse-simple-format pre spec-alist)
1940                   (gnus-mouse-face-function 
1941                    (gnus-parse-simple-format button spec-alist))
1942                   (gnus-parse-simple-format post spec-alist)))
1943         (gnus-parse-simple-format
1944          (concat (substring format (match-beginning 1) (match-end 1))
1945                  (substring format (match-beginning 2) (match-end 2))
1946                  (substring format (match-beginning 3) (match-end 3)))
1947          spec-alist))
1948     (gnus-parse-simple-format format spec-alist)))
1949
1950 (defun gnus-parse-simple-format (format spec-alist)
1951   ;; This function parses the FORMAT string with the help of the
1952   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1953   ;; string. The list will consist of the symbol `format', a format
1954   ;; specification string, and a list of forms depending on the
1955   ;; SPEC-ALIST.
1956   (let ((max-width 0)
1957         spec flist fstring newspec elem beg)
1958     (save-excursion
1959       (gnus-set-work-buffer)
1960       (insert format)
1961       (goto-char (point-min))
1962       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1963         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1964                                                      (match-end 2))))
1965         ;; First check if there are any specs that look anything like
1966         ;; "%12,12A", ie. with a "max width specification". These have
1967         ;; to be treated specially.
1968         (if (setq beg (match-beginning 1))
1969             (setq max-width 
1970                   (string-to-int 
1971                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1972           (setq max-width 0)
1973           (setq beg (match-beginning 2)))
1974         ;; Find the specification from `spec-alist'.
1975         (if (not (setq elem (cdr (assq spec spec-alist))))
1976             (setq elem '("*" ?s)))
1977         ;; Treat user defined format specifiers specially
1978         (and (eq (car elem) 'user-defined)
1979              (setq elem
1980                    (list 
1981                     (list (intern (concat "gnus-user-format-function-"
1982                                           (buffer-substring
1983                                            (match-beginning 3)
1984                                            (match-end 3))))
1985                           'header)
1986                     ?s))
1987              (delete-region (match-beginning 3) (match-end 3)))
1988         (if (not (zerop max-width))
1989             (let ((el (car elem)))
1990               (cond ((= (car (cdr elem)) ?c) 
1991                      (setq el (list 'char-to-string el)))
1992                     ((= (car (cdr elem)) ?d)
1993                      (numberp el) (setq el (list 'int-to-string el))))
1994               (setq flist (cons (gnus-max-width-function el max-width)
1995                                 flist))
1996               (setq newspec ?s))
1997           (setq flist (cons (car elem) flist))
1998           (setq newspec (car (cdr elem))))
1999         ;; Remove the old specification (and possibly a ",12" string).
2000         (delete-region beg (match-end 2))
2001         ;; Insert the new specification.
2002         (goto-char beg)
2003         (insert newspec))
2004       (setq fstring (buffer-substring 1 (point-max))))
2005     (cons 'format (cons fstring (nreverse flist)))))
2006
2007 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2008 (defun gnus-read-init-file ()
2009   (and gnus-init-file
2010        (or (and (file-exists-p gnus-init-file) 
2011                 ;; Don't try to load a directory.
2012                 (not (file-directory-p gnus-init-file)))
2013            (file-exists-p (concat gnus-init-file ".el"))
2014            (file-exists-p (concat gnus-init-file ".elc")))
2015        (load gnus-init-file nil t)))
2016
2017 (defun gnus-set-work-buffer ()
2018   (if (get-buffer gnus-work-buffer)
2019       (progn
2020         (set-buffer gnus-work-buffer)
2021         (erase-buffer))
2022     (set-buffer (get-buffer-create gnus-work-buffer))
2023     (kill-all-local-variables)
2024     (buffer-disable-undo (current-buffer))
2025     (gnus-add-current-to-buffer-list)))
2026
2027 ;; Article file names when saving.
2028
2029 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2030   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2031 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2032 Otherwise, it is like ~/News/news/group/num."
2033   (let ((default
2034           (expand-file-name
2035            (concat (if (gnus-use-long-file-name 'not-save)
2036                        (gnus-capitalize-newsgroup newsgroup)
2037                      (gnus-newsgroup-directory-form newsgroup))
2038                    "/" (int-to-string (header-number headers)))
2039            (or gnus-article-save-directory "~/News"))))
2040     (if (and last-file
2041              (string-equal (file-name-directory default)
2042                            (file-name-directory last-file))
2043              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2044         default
2045       (or last-file default))))
2046
2047 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2048   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2049 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2050 Otherwise, it is like ~/News/news/group/num."
2051   (let ((default
2052           (expand-file-name
2053            (concat (if (gnus-use-long-file-name 'not-save)
2054                        newsgroup
2055                      (gnus-newsgroup-directory-form newsgroup))
2056                    "/" (int-to-string (header-number headers)))
2057            (or gnus-article-save-directory "~/News"))))
2058     (if (and last-file
2059              (string-equal (file-name-directory default)
2060                            (file-name-directory last-file))
2061              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2062         default
2063       (or last-file default))))
2064
2065 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2066   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2067 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2068 Otherwise, it is like ~/News/news/group/news."
2069   (or last-file
2070       (expand-file-name
2071        (if (gnus-use-long-file-name 'not-save)
2072            (gnus-capitalize-newsgroup newsgroup)
2073          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2074        (or gnus-article-save-directory "~/News"))))
2075
2076 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2077   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2078 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2079 Otherwise, it is like ~/News/news/group/news."
2080   (or last-file
2081       (expand-file-name
2082        (if (gnus-use-long-file-name 'not-save)
2083            newsgroup
2084          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2085        (or gnus-article-save-directory "~/News"))))
2086
2087 ;; For subscribing new newsgroup
2088
2089 (defun gnus-subscribe-hierarchical-interactive (groups)
2090   (let ((groups (sort groups 'string<))
2091         prefixes prefix start ans group starts)
2092     (while groups
2093       (setq prefixes (list "^"))
2094       (while (and groups prefixes)
2095         (while (not (string-match (car prefixes) (car groups)))
2096           (setq prefixes (cdr prefixes)))
2097         (setq prefix (car prefixes))
2098         (setq start (1- (length prefix)))
2099         (if (and (string-match "[^\\.]\\." (car groups) start)
2100                  (cdr groups)
2101                  (setq prefix 
2102                        (concat "^" (substring (car groups) 0 (match-end 0))))
2103                  (string-match prefix (car (cdr groups))))
2104             (progn
2105               (setq prefixes (cons prefix prefixes))
2106               (message "Descend hierarchy %s? ([y]nsq): " 
2107                        (substring prefix 1 (1- (length prefix))))
2108               (setq ans (read-char))
2109               (cond ((= ans ?n)
2110                      (while (and groups 
2111                                  (string-match prefix 
2112                                                (setq group (car groups))))
2113                        (setq gnus-killed-list 
2114                              (cons group gnus-killed-list))
2115                        (gnus-sethash group group gnus-killed-hashtb)
2116                        (setq groups (cdr groups)))
2117                      (setq starts (cdr starts)))
2118                     ((= ans ?s)
2119                      (while (and groups 
2120                                  (string-match prefix 
2121                                                (setq group (car groups))))
2122                        (gnus-sethash group group gnus-killed-hashtb)
2123                        (gnus-subscribe-alphabetically (car groups))
2124                        (setq groups (cdr groups)))
2125                      (setq starts (cdr starts)))
2126                     ((= ans ?q)
2127                      (while groups
2128                        (setq group (car groups))
2129                        (setq gnus-killed-list (cons group gnus-killed-list))
2130                        (gnus-sethash group group gnus-killed-hashtb)
2131                        (setq groups (cdr groups))))
2132                     (t nil)))
2133           (message "Subscribe %s? ([n]yq)" (car groups))
2134           (setq ans (read-char))
2135           (setq group (car groups))
2136           (cond ((= ans ?y)
2137                  (gnus-subscribe-alphabetically (car groups))
2138                  (gnus-sethash group group gnus-killed-hashtb))
2139                 ((= ans ?q)
2140                  (while groups
2141                    (setq group (car groups))
2142                    (setq gnus-killed-list (cons group gnus-killed-list))
2143                    (gnus-sethash group group gnus-killed-hashtb)
2144                    (setq groups (cdr groups))))
2145                 (t 
2146                  (setq gnus-killed-list (cons group gnus-killed-list))
2147                  (gnus-sethash group group gnus-killed-hashtb)))
2148           (setq groups (cdr groups)))))))
2149
2150 (defun gnus-subscribe-randomly (newsgroup)
2151   "Subscribe new NEWSGROUP by making it the first newsgroup."
2152   (gnus-subscribe-newsgroup newsgroup))
2153
2154 (defun gnus-subscribe-alphabetically (newgroup)
2155   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2156   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2157   (let ((groups (cdr gnus-newsrc-alist))
2158         before)
2159     (while (and (not before) groups)
2160       (if (string< newgroup (car (car groups)))
2161           (setq before (car (car groups)))
2162         (setq groups (cdr groups))))
2163     (gnus-subscribe-newsgroup newgroup before)))
2164
2165 (defun gnus-subscribe-hierarchically (newgroup)
2166   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2167   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2168   (save-excursion
2169     (set-buffer (find-file-noselect gnus-current-startup-file))
2170     (let ((groupkey newgroup)
2171           before)
2172       (while (and (not before) groupkey)
2173         (goto-char (point-min))
2174         (let ((groupkey-re
2175                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2176           (while (and (re-search-forward groupkey-re nil t)
2177                       (progn
2178                         (setq before (buffer-substring
2179                                       (match-beginning 1) (match-end 1)))
2180                         (string< before newgroup)))))
2181         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2182         (setq groupkey
2183               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2184                   (substring groupkey (match-beginning 1) (match-end 1)))))
2185       (gnus-subscribe-newsgroup newgroup before))))
2186
2187 (defun gnus-subscribe-interactively (newsgroup)
2188   "Subscribe new NEWSGROUP interactively.
2189 It is inserted in hierarchical newsgroup order if subscribed. If not,
2190 it is killed."
2191   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2192       (gnus-subscribe-hierarchically newsgroup)
2193     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2194
2195 (defun gnus-subscribe-zombies (newsgroup)
2196   "Make new NEWSGROUP a zombie group."
2197   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2198
2199 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2200   "Subscribe new NEWSGROUP.
2201 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2202 the first newsgroup."
2203   ;; We subscribe the group by changing its level to `subscribed'.
2204   (gnus-group-change-level 
2205    newsgroup gnus-level-default-subscribed
2206    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2207   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2208
2209 ;; For directories
2210
2211 (defun gnus-newsgroup-directory-form (newsgroup)
2212   "Make hierarchical directory name from NEWSGROUP name."
2213   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2214         (len (length newsgroup))
2215         idx)
2216     ;; If this is a foreign group, we don't want to translate the
2217     ;; entire name.  
2218     (if (setq idx (string-match ":" newsgroup))
2219         (aset newsgroup idx ?/)
2220       (setq idx 0))
2221     ;; Replace all occurrences of `.' with `/'.
2222     (while (< idx len)
2223       (if (= (aref newsgroup idx) ?.)
2224           (aset newsgroup idx ?/))
2225       (setq idx (1+ idx)))
2226     newsgroup))
2227
2228 (defun gnus-newsgroup-saveable-name (group)
2229   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2230   ;; with dots.
2231   (gnus-replace-chars-in-string group ?/ ?.))
2232
2233 (defun gnus-make-directory (dir)
2234   "Make DIRECTORY recursively."
2235   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2236   ;; of the many mysteries of the universe.
2237  (let* ((dir (expand-file-name dir default-directory))
2238          dirs err)
2239     (if (string-match "/$" dir)
2240         (setq dir (substring dir 0 (match-beginning 0))))
2241     ;; First go down the path until we find a directory that exists.
2242     (while (not (file-exists-p dir))
2243       (setq dirs (cons dir dirs))
2244       (string-match "/[^/]+$" dir)
2245       (setq dir (substring dir 0 (match-beginning 0))))
2246     ;; Then create all the subdirs.
2247     (while (and dirs (not err))
2248       (condition-case ()
2249           (make-directory (car dirs))
2250         (error (setq err t)))
2251       (setq dirs (cdr dirs)))
2252     ;; We return whether we were successful or not. 
2253     (not dirs)))
2254
2255 (defun gnus-capitalize-newsgroup (newsgroup)
2256   "Capitalize NEWSGROUP name."
2257   (and (not (zerop (length newsgroup)))
2258        (concat (char-to-string (upcase (aref newsgroup 0)))
2259                (substring newsgroup 1))))
2260
2261 ;; Var
2262
2263 (defun gnus-simplify-subject (subject &optional re-only)
2264   "Remove `Re:' and words in parentheses.
2265 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2266   (let ((case-fold-search t))           ;Ignore case.
2267     ;; Remove `Re:' and `Re^N:'.
2268     (if (string-match "^re:[ \t]*" subject)
2269         (setq subject (substring subject (match-end 0))))
2270     ;; Remove words in parentheses from end.
2271     (or re-only
2272         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2273           (setq subject (substring subject 0 (match-beginning 0)))))
2274     ;; Return subject string.
2275     subject))
2276
2277 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2278 ;; all whitespace.
2279 (defun gnus-simplify-subject-fuzzy (subject)
2280   (let ((case-fold-search t))
2281     (save-excursion
2282       (gnus-set-work-buffer)
2283       (insert subject)
2284       (inline (gnus-simplify-buffer-fuzzy))
2285       (buffer-string))))
2286
2287 (defun gnus-simplify-buffer-fuzzy ()
2288   (goto-char (point-min))
2289   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2290   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2291                             nil t)
2292     (replace-match "" t t))
2293   (goto-char (point-min))
2294   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2295     (replace-match "" t t))
2296   (goto-char (point-min))
2297   (while (re-search-forward "[ \t]+" nil t)
2298     (replace-match " " t t))
2299   (goto-char (point-min))
2300   (while (re-search-forward "[ \t]+$" nil t)
2301     (replace-match "" t t))
2302   (goto-char (point-min))
2303   (while (re-search-forward "^[ \t]+" nil t)
2304     (replace-match "" t t))
2305   (if gnus-simplify-subject-fuzzy-regexp
2306       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2307         (replace-match "" t t))))
2308
2309 ;; Add the current buffer to the list of buffers to be killed on exit. 
2310 (defun gnus-add-current-to-buffer-list ()
2311   (or (memq (current-buffer) gnus-buffer-list)
2312       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2313
2314 (defun gnus-string> (s1 s2)
2315   (not (or (string< s1 s2)
2316            (string= s1 s2))))
2317
2318 ;; Functions accessing headers.
2319 ;; Functions are more convenient than macros in some cases.
2320
2321 (defun gnus-header-number (header)
2322   (header-number header))
2323
2324 (defun gnus-header-subject (header)
2325   (header-subject header))
2326
2327 (defun gnus-header-from (header)
2328   (header-from header))
2329
2330 (defun gnus-header-xref (header)
2331   (header-xref header))
2332
2333 (defun gnus-header-lines (header)
2334   (header-lines header))
2335
2336 (defun gnus-header-date (header)
2337   (header-date header))
2338
2339 (defun gnus-header-id (header)
2340   (header-id header))
2341
2342 (defun gnus-header-references (header)
2343   (header-references header))
2344
2345 ;;; General various misc type functions.
2346
2347 (defun gnus-clear-system ()
2348   "Clear all variables and buffers."
2349   ;; Clear Gnus variables.
2350   (let ((variables gnus-variable-list))
2351     (while variables
2352       (set (car variables) nil)
2353       (setq variables (cdr variables))))
2354   ;; Clear other internal variables.
2355   (setq gnus-list-of-killed-groups nil
2356         gnus-have-read-active-file nil
2357         gnus-newsrc-alist nil
2358         gnus-newsrc-hashtb nil
2359         gnus-killed-list nil
2360         gnus-zombie-list nil
2361         gnus-killed-hashtb nil
2362         gnus-active-hashtb nil
2363         gnus-moderated-list nil
2364         gnus-description-hashtb nil
2365         gnus-newsgroup-headers nil
2366         gnus-newsgroup-headers-hashtb-by-number nil
2367         gnus-newsgroup-name nil
2368         gnus-server-alist nil
2369         gnus-current-select-method nil)
2370   ;; Reset any score variables.
2371   (and (boundp 'gnus-score-cache)
2372        (set 'gnus-score-cache nil))
2373   (and (boundp 'gnus-internal-global-score-files)
2374        (set 'gnus-internal-global-score-files nil))
2375   ;; Kill the startup file.
2376   (and gnus-current-startup-file
2377        (get-file-buffer gnus-current-startup-file)
2378        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2379   ;; Save any cache buffers.
2380   (and gnus-use-cache (gnus-cache-save-buffers))
2381   ;; Clear the dribble buffer.
2382   (gnus-dribble-clear)
2383   ;; Kill global KILL file buffer.
2384   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2385       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2386   (gnus-kill-buffer nntp-server-buffer)
2387   ;; Kill Gnus buffers.
2388   (while gnus-buffer-list
2389     (gnus-kill-buffer (car gnus-buffer-list))
2390     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2391
2392 (defun gnus-windows-old-to-new (setting)
2393   (if (symbolp setting)
2394       (setq setting 
2395             (cond ((eq setting 'SelectArticle)
2396                    'article)
2397                   ((eq setting 'SelectSubject)
2398                    'summary)
2399                   ((eq setting 'SelectNewsgroup)
2400                    'group)
2401                   (t setting))))
2402   (if (or (listp setting)
2403           (not (and gnus-window-configuration
2404                     (memq setting '(group summary article)))))
2405       setting
2406     (let* ((setting (if (eq setting 'group) 
2407                         (if (assq 'newsgroup gnus-window-configuration)
2408                             'newsgroup
2409                           'newsgroups) setting))
2410            (elem (car (cdr (assq setting gnus-window-configuration))))
2411            (total (apply '+ elem))
2412            (types '(group summary article))
2413            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2414            (i 0)
2415            perc
2416            out)
2417       (while (< i 3)
2418         (or (zerop (nth i elem))
2419             (progn
2420               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2421               (setq out (cons (if (eq pbuf (nth i types))
2422                                   (vector (nth i types) perc 'point)
2423                                 (vector (nth i types) perc))
2424                               out))))
2425         (setq i (1+ i)))
2426       (list (nreverse out)))))
2427            
2428 (defun gnus-add-configuration (conf)
2429   (setq gnus-buffer-configuration 
2430         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2431                          gnus-buffer-configuration))))
2432
2433 (defun gnus-configure-windows (setting &optional force)
2434   (setq setting (gnus-windows-old-to-new setting))
2435   (let ((r (if (symbolp setting)
2436                (cdr (assq setting gnus-buffer-configuration))
2437              setting))
2438         (in-buf (current-buffer))
2439         rule val w height hor ohor heights sub jump-buffer
2440         rel total to-buf all-visible)
2441     (or r (error "No such setting: %s" setting))
2442
2443     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2444         ;; All the windows mentioned are already visibe, so we just
2445         ;; put point in the assigned buffer, and do not touch the
2446         ;; winconf. 
2447         (select-window (get-buffer-window all-visible))
2448
2449       ;; Either remove all windows or just remove all Gnus windows.
2450       (if gnus-use-full-window
2451           (delete-other-windows)
2452         (gnus-remove-some-windows)
2453         (switch-to-buffer nntp-server-buffer))
2454
2455       (while r
2456         (setq hor (car r)
2457               ohor nil)
2458
2459         ;; We have to do the (possible) horizontal splitting before the
2460         ;; vertical. 
2461         (if (and (listp (car hor)) 
2462                  (eq (car (car hor)) 'horizontal))
2463             (progn
2464               (split-window 
2465                nil
2466                (if (integerp (nth 1 (car hor)))
2467                    (nth 1 (car hor))
2468                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2469                t)
2470               (setq hor (cdr hor))))
2471
2472         ;; Go through the rules and eval the elements that are to be
2473         ;; evaled.  
2474         (while hor
2475           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2476               (progn
2477                 ;; Expand short buffer name.
2478                 (setq w (aref val 0))
2479                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2480                      (progn
2481                        (setq val (apply 'vector (mapcar 'identity val)))
2482                        (aset val 0 w)))
2483                 (setq ohor (cons val ohor))))
2484           (setq hor (cdr hor)))
2485         (setq rule (cons (nreverse ohor) rule))
2486         (setq r (cdr r)))
2487       (setq rule (nreverse rule))
2488
2489       ;; We tally the window sizes.
2490       (setq total (window-height))
2491       (while rule
2492         (setq hor (car rule))
2493         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2494             (setq hor (cdr hor)))
2495         (setq sub 0)
2496         (while hor
2497           (setq rel (aref (car hor) 1)
2498                 heights (cons
2499                          (cond ((and (floatp rel) (= 1.0 rel))
2500                                 'x)
2501                                ((integerp rel)
2502                                 rel)
2503                                (t
2504                                 (max (floor (* total rel)) 4)))
2505                          heights)
2506                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2507                 hor (cdr hor)))
2508         (setq heights (nreverse heights)
2509               hor (car rule))
2510
2511         ;; We then go through these heighs and create windows for them.
2512         (while heights
2513           (setq height (car heights)
2514                 heights (cdr heights))
2515           (and (eq height 'x)
2516                (setq height (- total sub)))
2517           (and heights
2518                (split-window nil height))
2519           (setq to-buf (aref (car hor) 0))
2520           (switch-to-buffer 
2521            (cond ((not to-buf)
2522                   in-buf)
2523                  ((symbolp to-buf)
2524                   (symbol-value (aref (car hor) 0)))
2525                  (t
2526                   (aref (car hor) 0))))
2527           (and (> (length (car hor)) 2)
2528                (eq (aref (car hor) 2) 'point)
2529                (setq jump-buffer (current-buffer)))
2530           (other-window 1)
2531           (setq hor (cdr hor)))
2532       
2533         (setq rule (cdr rule)))
2534
2535       ;; Finally, we pop to the buffer that's supposed to have point. 
2536       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2537
2538       (select-window (get-buffer-window jump-buffer))
2539       (set-buffer jump-buffer))))
2540
2541 (defun gnus-all-windows-visible-p (rule)
2542   (let (invisible hor jump-buffer val buffer)
2543     ;; Go through the rules and eval the elements that are to be
2544     ;; evaled.  
2545     (while (and rule (not invisible))
2546       (setq hor (car rule)
2547             rule (cdr rule))
2548       (while (and hor (not invisible))
2549         (if (setq val (if (vectorp (car hor)) 
2550                           (car hor)
2551                         (if (not (eq (car (car hor)) 'horizontal))
2552                             (eval (car hor)))))
2553             (progn
2554               ;; Expand short buffer name.
2555               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2556                                (aref val 0)))
2557               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2558                              buffer))
2559               (and (> (length val) 2) (eq 'point (aref val 2))
2560                    (setq jump-buffer buffer))
2561               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2562         (setq hor (cdr hor))))
2563     (and (not invisible) jump-buffer)))
2564
2565       
2566 (defun gnus-remove-some-windows ()
2567   (let ((buffers gnus-window-to-buffer)
2568         (lowest (frame-height))
2569         buf bufs lowest-buf)
2570     (save-excursion
2571       ;; Remove windows on all known Gnus buffers.
2572       (while buffers
2573         (setq buf (cdr (car buffers)))
2574         (if (symbolp buf)
2575             (setq buf (and (boundp buf) (symbol-value buf))))
2576         (and buf 
2577              (get-buffer-window buf)
2578              (progn
2579                (setq bufs (cons buf bufs))
2580                (pop-to-buffer buf)
2581                (if (< (nth 1 (window-edges)) lowest)
2582                    (progn
2583                      (setq lowest (nth 1 (window-edges)))
2584                      (setq lowest-buf buf)))))
2585         (setq buffers (cdr buffers)))
2586       ;; Remove windows on *all* summary buffers.
2587       (let (wins)
2588         (walk-windows
2589          (lambda (win)
2590            (let ((buf (window-buffer win)))
2591              (if (string-match  "^\\*Summary" (buffer-name buf))
2592              (progn
2593                (setq bufs (cons buf bufs))
2594                (pop-to-buffer buf)
2595                (if (< (nth 1 (window-edges)) lowest)
2596                    (progn
2597                      (setq lowest-buf buf)
2598                      (setq lowest (nth 1 (window-edges)))))))))))
2599       (and lowest-buf 
2600            (progn
2601              (pop-to-buffer lowest-buf)
2602              (switch-to-buffer nntp-server-buffer)))
2603       (while bufs
2604         (and (not (eq (car bufs) lowest-buf))
2605              (delete-windows-on (car bufs)))
2606         (setq bufs (cdr bufs))))))
2607                           
2608 (defun gnus-version ()
2609   "Version numbers of this version of Gnus."
2610   (interactive)
2611   (let ((methods gnus-valid-select-methods)
2612         (mess gnus-version)
2613         meth)
2614     ;; Go through all the legal select methods and add their version
2615     ;; numbers to the total version string. Only the backends that are
2616     ;; currently in use will have their message numbers taken into
2617     ;; consideration. 
2618     (while methods
2619       (setq meth (intern (concat (car (car methods)) "-version")))
2620       (and (boundp meth)
2621            (stringp (symbol-value meth))
2622            (setq mess (concat mess "; " (symbol-value meth))))
2623       (setq methods (cdr methods)))
2624     (gnus-message 2 mess)))
2625
2626 (defun gnus-info-find-node ()
2627   "Find Info documentation of Gnus."
2628   (interactive)
2629   ;; Enlarge info window if needed.
2630   (let ((mode major-mode))
2631     (gnus-configure-windows 'info)
2632     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2633
2634 (defun gnus-overload-functions (&optional overloads)
2635   "Overload functions specified by optional argument OVERLOADS.
2636 If nothing is specified, use the variable gnus-overload-functions."
2637   (let ((defs nil)
2638         (overloads (or overloads gnus-overload-functions)))
2639     (while overloads
2640       (setq defs (car overloads))
2641       (setq overloads (cdr overloads))
2642       ;; Load file before overloading function if necessary.  Make
2643       ;; sure we cannot use `require' always.
2644       (and (not (fboundp (car defs)))
2645            (car (cdr (cdr defs)))
2646            (load (car (cdr (cdr defs))) nil 'nomessage))
2647       (fset (car defs) (car (cdr defs))))))
2648
2649 (defun gnus-replace-chars-in-string (string from to)
2650   "Replace characters in STRING from FROM to TO."
2651   (let ((string (substring string 0))   ;Copy string.
2652         (len (length string))
2653         (idx 0))
2654     ;; Replace all occurrences of FROM with TO.
2655     (while (< idx len)
2656       (if (= (aref string idx) from)
2657           (aset string idx to))
2658       (setq idx (1+ idx)))
2659     string))
2660
2661 (defun gnus-days-between (date1 date2)
2662   ;; Return the number of days between date1 and date2.
2663   (- (gnus-day-number date1) (gnus-day-number date2)))
2664
2665 (defun gnus-day-number (date)
2666   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2667                      (timezone-parse-date date))))
2668     (timezone-absolute-from-gregorian 
2669      (nth 1 dat) (nth 2 dat) (car dat))))
2670
2671 ;; Returns a floating point number that says how many seconds have
2672 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2673 (defun gnus-seconds-since-epoch (date)
2674   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2675                         (timezone-parse-date date)))
2676          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2677                         (timezone-parse-time
2678                          (aref (timezone-parse-date date) 3))))
2679          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2680                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2681          (tday (- (timezone-absolute-from-gregorian 
2682                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2683                   (timezone-absolute-from-gregorian 
2684                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2685     (+ (nth 2 ttime)
2686        (* (nth 1 ttime) 60)
2687        (* 1.0 (nth 0 ttime) 60 60)
2688        (* 1.0 tday 60 60 24))))
2689
2690 (defun gnus-file-newer-than (file date)
2691   (let ((fdate (nth 5 (file-attributes file))))
2692     (or (> (car fdate) (car date))
2693         (and (= (car fdate) (car date))
2694              (> (nth 1 fdate) (nth 1 date))))))
2695
2696 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2697 ;; the echo area.
2698 (defun gnus-y-or-n-p (prompt)
2699   (prog1
2700       (y-or-n-p prompt)
2701     (message "")))
2702
2703 (defun gnus-yes-or-no-p (prompt)
2704   (prog1
2705       (yes-or-no-p prompt)
2706     (message "")))
2707
2708 ;; Check whether to use long file names.
2709 (defun gnus-use-long-file-name (symbol)
2710   ;; The variable has to be set...
2711   (and gnus-use-long-file-name
2712        ;; If it isn't a list, then we return t.
2713        (or (not (listp gnus-use-long-file-name))
2714            ;; If it is a list, and the list contains `symbol', we
2715            ;; return nil.  
2716            (not (memq symbol gnus-use-long-file-name)))))
2717
2718 ;; I suspect there's a better way, but I haven't taken the time to do
2719 ;; it yet. -erik selberg@cs.washington.edu
2720 (defun gnus-dd-mmm (messy-date)
2721   "Return a string like DD-MMM from a big messy string"
2722   (let ((datevec (timezone-parse-date messy-date)))
2723     (format "%2s-%s"
2724             (or (aref datevec 2) "??")
2725             (capitalize
2726              (or (car 
2727                   (nth (1- (string-to-number (aref datevec 1)))
2728                        timezone-months-assoc))
2729                  "???")))))
2730
2731 ;; Make a hash table (default and minimum size is 255).
2732 ;; Optional argument HASHSIZE specifies the table size.
2733 (defun gnus-make-hashtable (&optional hashsize)
2734   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2735
2736 ;; Make a number that is suitable for hashing; bigger than MIN and one
2737 ;; less than 2^x.
2738 (defun gnus-create-hash-size (min)
2739   (let ((i 1))
2740     (while (< i min)
2741       (setq i (* 2 i)))
2742     (1- i)))
2743
2744 ;; Show message if message has a lower level than `gnus-verbose'. 
2745 ;; Guide-line for numbers:
2746 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2747 ;; for things that take a long time, 7 - not very important messages
2748 ;; on stuff, 9 - messages inside loops.
2749 (defun gnus-message (level &rest args)
2750   (if (<= level gnus-verbose)
2751       (apply 'message args)
2752     ;; We have to do this format thingie here even if the result isn't
2753     ;; shown - the return value has to be the same as the return value
2754     ;; from `message'.
2755     (apply 'format args)))
2756
2757 ;; Generate a unique new group name.
2758 (defun gnus-generate-new-group-name (leaf)
2759   (let ((name leaf)
2760         (num 0))
2761     (while (gnus-gethash name gnus-newsrc-hashtb)
2762       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2763     name))
2764
2765 (defun gnus-find-file-noselect (file &optional force)
2766   "Does vaguely the same as find-file-noselect. No hooks are run."
2767   (let (buf insert)
2768     (if (setq buf (get-file-buffer file))
2769         (setq insert force)
2770       (setq buf (create-file-buffer file))
2771       (setq insert t))
2772     (if (not insert)
2773         buf
2774       (save-excursion
2775         (set-buffer buf)
2776         (erase-buffer)
2777         (and (file-readable-p file)
2778              (insert-file-contents file))
2779         (set-visited-file-name file)
2780         (set-buffer-modified-p nil)
2781         (current-buffer)))))
2782
2783 ;;; List and range functions
2784
2785 (defun gnus-last-element (list)
2786   "Return last element of LIST."
2787   (while (cdr list)
2788     (setq list (cdr list)))
2789   (car list))
2790
2791 (defun gnus-copy-sequence (list)
2792   "Do a complete, total copy of a list."
2793   (if (and (consp list) (not (consp (cdr list))))
2794       (cons (car list) (cdr list))
2795     (mapcar (lambda (elem) (if (consp elem) 
2796                                (if (consp (cdr elem))
2797                                    (gnus-copy-sequence elem)
2798                                  (cons (car elem) (cdr elem)))
2799                              elem))
2800             list)))
2801
2802 (defun gnus-set-difference (list1 list2)
2803   "Return a list of elements of LIST1 that do not appear in LIST2."
2804   (let ((list1 (copy-sequence list1)))
2805     (while list2
2806       (setq list1 (delq (car list2) list1))
2807       (setq list2 (cdr list2)))
2808     list1))
2809
2810 (defun gnus-sorted-complement (list1 list2)
2811   "Return a list of elements of LIST1 that do not appear in LIST2.
2812 Both lists have to be sorted over <."
2813   (let (out)
2814     (if (or (null list1) (null list2))
2815         (or list1 list2)
2816       (while (and list1 list2)
2817         (cond ((= (car list1) (car list2))
2818                (setq list1 (cdr list1)
2819                      list2 (cdr list2)))
2820               ((< (car list1) (car list2))
2821                (setq out (cons (car list1) out))
2822                (setq list1 (cdr list1)))
2823               (t
2824                (setq out (cons (car list2) out))
2825                (setq list2 (cdr list2)))))
2826       (nconc (nreverse out) (or list1 list2)))))
2827
2828 (defun gnus-intersection (list1 list2)      
2829   (let ((result nil))
2830     (while list2
2831       (if (memq (car list2) list1)
2832           (setq result (cons (car list2) result)))
2833       (setq list2 (cdr list2)))
2834     result))
2835
2836 (defun gnus-sorted-intersection (list1 list2)
2837   ;; LIST1 and LIST2 have to be sorted over <.
2838   (let (out)
2839     (while (and list1 list2)
2840       (cond ((= (car list1) (car list2))
2841              (setq out (cons (car list1) out)
2842                    list1 (cdr list1)
2843                    list2 (cdr list2)))
2844             ((< (car list1) (car list2))
2845              (setq list1 (cdr list1)))
2846             (t
2847              (setq list2 (cdr list2)))))
2848     (nreverse out)))
2849
2850 (defun gnus-set-sorted-intersection (list1 list2)
2851   ;; LIST1 and LIST2 have to be sorted over <.
2852   ;; This function modifies LIST1.
2853   (let* ((top (cons nil list1))
2854          (prev top))
2855   (while (and list1 list2)
2856     (cond ((= (car list1) (car list2))
2857            (setq prev list1
2858                  list1 (cdr list1)
2859                  list2 (cdr list2)))
2860           ((< (car list1) (car list2))
2861            (setcdr prev (cdr list1))
2862            (setq list1 (cdr list1)))
2863           (t
2864            (setq list2 (cdr list2)))))
2865   (setcdr prev nil)
2866   (cdr top)))
2867
2868 (defun gnus-compress-sequence (numbers &optional always-list)
2869   "Convert list of numbers to a list of ranges or a single range.
2870 If ALWAYS-LIST is non-nil, this function will always release a list of
2871 ranges."
2872   (let* ((first (car numbers))
2873          (last (car numbers))
2874          result)
2875     (if (null numbers)
2876         nil
2877       (if (not (listp (cdr numbers)))
2878           numbers
2879         (while numbers
2880           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2881                 ((= (1+ last) (car numbers)) ;Still in sequence
2882                  (setq last (car numbers)))
2883                 (t                      ;End of one sequence
2884                  (setq result 
2885                        (cons (if (= first last) first
2886                                (cons first last)) result))
2887                  (setq first (car numbers))
2888                  (setq last  (car numbers))))
2889           (setq numbers (cdr numbers)))
2890         (if (and (not always-list) (null result))
2891             (if (= first last) (list first) (cons first last))
2892           (nreverse (cons (if (= first last) first (cons first last))
2893                           result)))))))
2894
2895 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2896 (defun gnus-uncompress-range (ranges)
2897   "Expand a list of ranges into a list of numbers.
2898 RANGES is either a single range on the form `(num . num)' or a list of
2899 these ranges."
2900   (let (first last result)
2901     (cond 
2902      ((null ranges)
2903       nil)
2904      ((not (listp (cdr ranges)))
2905       (setq first (car ranges))
2906       (setq last (cdr ranges))
2907       (while (<= first last)
2908         (setq result (cons first result))
2909         (setq first (1+ first)))
2910       (nreverse result))
2911      (t
2912       (while ranges
2913         (if (atom (car ranges))
2914             (if (numberp (car ranges))
2915                 (setq result (cons (car ranges) result)))
2916           (setq first (car (car ranges)))
2917           (setq last  (cdr (car ranges)))
2918           (while (<= first last)
2919             (setq result (cons first result))
2920             (setq first (1+ first))))
2921         (setq ranges (cdr ranges)))
2922       (nreverse result)))))
2923
2924 (defun gnus-add-to-range (ranges list)
2925   "Return a list of ranges that has all articles from both RANGES and LIST.
2926 Note: LIST has to be sorted over `<'."
2927   (if (not ranges)
2928       (gnus-compress-sequence list t)
2929     (setq list (copy-sequence list))
2930     (or (listp (cdr ranges))
2931         (setq ranges (list ranges)))
2932     (let ((out ranges)
2933           ilist lowest highest temp)
2934       (while (and ranges list)
2935         (setq ilist list)
2936         (setq lowest (or (and (atom (car ranges)) (car ranges))
2937                          (car (car ranges))))
2938         (while (and list (cdr list) (< (car (cdr list)) lowest))
2939           (setq list (cdr list)))
2940         (if (< (car ilist) lowest)
2941             (progn
2942               (setq temp list)
2943               (setq list (cdr list))
2944               (setcdr temp nil)
2945               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2946         (setq highest (or (and (atom (car ranges)) (car ranges))
2947                           (cdr (car ranges))))
2948         (while (and list (<= (car list) highest))
2949           (setq list (cdr list)))
2950         (setq ranges (cdr ranges)))
2951       (if list
2952           (setq out (nconc (gnus-compress-sequence list t) out)))
2953       (setq out (sort out (lambda (r1 r2) 
2954                             (< (or (and (atom r1) r1) (car r1))
2955                                (or (and (atom r2) r2) (car r2))))))
2956       (setq ranges out)
2957       (while ranges
2958         (if (atom (car ranges))
2959             (if (cdr ranges)
2960                 (if (atom (car (cdr ranges)))
2961                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2962                         (progn
2963                           (setcar ranges (cons (car ranges) 
2964                                                (car (cdr ranges))))
2965                           (setcdr ranges (cdr (cdr ranges)))))
2966                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2967                       (progn
2968                         (setcar (car (cdr ranges)) (car ranges))
2969                         (setcar ranges (car (cdr ranges)))
2970                         (setcdr ranges (cdr (cdr ranges)))))))
2971           (if (cdr ranges)
2972               (if (atom (car (cdr ranges)))
2973                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2974                       (progn
2975                         (setcdr (car ranges) (car (cdr ranges)))
2976                         (setcdr ranges (cdr (cdr ranges)))))
2977                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2978                     (progn
2979                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2980                       (setcdr ranges (cdr (cdr ranges))))))))
2981         (setq ranges (cdr ranges)))
2982       out)))
2983
2984 (defun gnus-remove-from-range (ranges list)
2985   "Return a list of ranges that has all articles from LIST removed from RANGES.
2986 Note: LIST has to be sorted over `<'."
2987   ;; !!! This function shouldn't look like this, but I've got a headache.
2988   (gnus-compress-sequence 
2989    (gnus-sorted-complement
2990     (gnus-uncompress-range ranges) list)))
2991
2992 (defun gnus-member-of-range (number ranges)
2993   (if (not (listp (cdr ranges)))
2994       (and (>= number (car ranges)) 
2995            (<= number (cdr ranges)))
2996     (let ((not-stop t))
2997       (while (and ranges 
2998                   (if (numberp (car ranges))
2999                       (>= number (car ranges))
3000                     (>= number (car (car ranges))))
3001                   not-stop)
3002         (if (if (numberp (car ranges))
3003                 (= number (car ranges))
3004               (and (>= number (car (car ranges)))
3005                    (<= number (cdr (car ranges)))))
3006             (setq not-stop nil))
3007         (setq ranges (cdr ranges)))
3008       (not not-stop))))
3009
3010 \f
3011 ;;;
3012 ;;; Gnus group mode
3013 ;;;
3014
3015 (defvar gnus-group-mode-map nil)
3016 (defvar gnus-group-group-map nil)
3017 (defvar gnus-group-mark-map nil)
3018 (defvar gnus-group-list-map nil)
3019 (defvar gnus-group-sub-map nil)
3020 (put 'gnus-group-mode 'mode-class 'special)
3021
3022 (if gnus-group-mode-map
3023     nil
3024   (setq gnus-group-mode-map (make-keymap))
3025   (suppress-keymap gnus-group-mode-map)
3026   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3027   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3028   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3029   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3030   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3031   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3032   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3033   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3034   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3035   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3036   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3037   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3038   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3039   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3040   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3041   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3042   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3043   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3044   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3045   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3046   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3047   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3048   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3049   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3050   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3051   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3052   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3053   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3054   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3055   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3056   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3057   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3058   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3059   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3060   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3061   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3062   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3063   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3064   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3065   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3066   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3067   (define-key gnus-group-mode-map "V" 'gnus-version)
3068   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3069   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3070   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3071   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3072   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3073   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3074   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3075   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3076   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3077   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3078   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3079   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3080   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3081   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3082   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3083
3084   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3085   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3086   (define-prefix-command 'gnus-group-mark-map)
3087   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3088   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3089   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3090   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3091
3092   (define-prefix-command 'gnus-group-group-map)
3093   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3094   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3095   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3096   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3097   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3098   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3099   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3100   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3101   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3102   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3103   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3104   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3105   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3106   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3107   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3108   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3109   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3110   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3111
3112   (define-prefix-command 'gnus-group-list-map)
3113   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3114   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3115   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3116   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3117   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3118   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3119   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3120   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3121   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3122
3123   (define-prefix-command 'gnus-group-sub-map)
3124   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3125   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3126   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3127   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3128   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3129   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3130   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3131   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3132
3133 (defun gnus-group-mode ()
3134   "Major mode for reading news.
3135
3136 All normal editing commands are switched off.
3137 \\<gnus-group-mode-map>
3138 The group buffer lists (some of) the groups available.  For instance,
3139 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3140 lists all zombie groups. 
3141
3142 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3143 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3144
3145 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3146
3147 The following commands are available:
3148
3149 \\{gnus-group-mode-map}"
3150   (interactive)
3151   (if gnus-visual (gnus-group-make-menu-bar))
3152   (kill-all-local-variables)
3153   (setq mode-line-modified "-- ")
3154   (make-local-variable 'mode-line-format)
3155   (setq mode-line-format (copy-sequence mode-line-format))
3156   (and (equal (nth 3 mode-line-format) "   ")
3157        (setcar (nthcdr 3 mode-line-format) ""))
3158   (setq major-mode 'gnus-group-mode)
3159   (setq mode-name "Group")
3160   (gnus-group-set-mode-line)
3161   (setq mode-line-process nil)
3162   (use-local-map gnus-group-mode-map)
3163   (buffer-disable-undo (current-buffer))
3164   (setq truncate-lines t)
3165   (setq buffer-read-only t)
3166   (run-hooks 'gnus-group-mode-hook))
3167
3168 (defun gnus-mouse-pick-group (e)
3169   (interactive "e")
3170   (mouse-set-point e)
3171   (gnus-group-read-group nil))
3172
3173 ;; Look at LEVEL and find out what the level is really supposed to be.
3174 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3175 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3176 (defun gnus-group-default-level (&optional level number-or-nil)
3177   (cond  
3178    (gnus-group-use-permanent-levels
3179     (setq gnus-group-default-list-level 
3180           (or level gnus-group-default-list-level))
3181     (or gnus-group-default-list-level gnus-level-subscribed))
3182    (number-or-nil
3183     level)
3184    (t
3185     (or level gnus-group-default-list-level gnus-level-subscribed))))
3186   
3187
3188 ;;;###autoload
3189 (defun gnus-no-server (&optional arg)
3190   "Read network news.
3191 If ARG is a positive number, Gnus will use that as the
3192 startup level. If ARG is nil, Gnus will be started at level 2. 
3193 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 As opposed to `gnus', this command will not connect to the local server."
3196   (interactive "P")
3197   (setq gnus-group-use-permanent-levels t)
3198   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3199
3200 (defalias '\(ding\) 'gnus)
3201
3202 ;;;###autoload
3203 (defun gnus (&optional arg dont-connect)
3204   "Read network news.
3205 If ARG is non-nil and a positive number, Gnus will use that as the
3206 startup level. If ARG is non-nil and not a positive number, Gnus will
3207 prompt the user for the name of an NNTP server to use."
3208   (interactive "P")
3209   (if (get-buffer gnus-group-buffer)
3210       (progn
3211         (switch-to-buffer gnus-group-buffer)
3212         (gnus-group-get-new-news))
3213     (gnus-clear-system)
3214     (nnheader-init-server-buffer)
3215     (gnus-read-init-file)
3216
3217     (let ((level (and arg (numberp arg) (> arg 0) arg))
3218           did-connect)
3219       (unwind-protect
3220           (progn
3221             (gnus-group-setup-buffer)
3222             (or dont-connect 
3223                 (setq did-connect
3224                       (gnus-start-news-server (and arg (not level))))))
3225         (if (and (not dont-connect) 
3226                  (not did-connect))
3227             (gnus-group-quit)
3228           (run-hooks 'gnus-startup-hook)
3229           ;; NNTP server is successfully open. 
3230
3231           ;; Find the current startup file name.
3232           (setq gnus-current-startup-file 
3233                 (gnus-make-newsrc-file gnus-startup-file))
3234
3235           ;; Read the dribble file.
3236           (and gnus-use-dribble-file (gnus-dribble-read-file))
3237
3238           (gnus-summary-make-display-table)
3239           (let ((buffer-read-only nil))
3240             (erase-buffer)
3241             (if (not gnus-inhibit-startup-message)
3242                 (progn
3243                   (gnus-group-startup-message)
3244                   (sit-for 0))))
3245           (gnus-setup-news nil level)
3246           (gnus-group-list-groups level)
3247           (gnus-configure-windows 'group))))))
3248
3249 (defun gnus-unload ()
3250   "Unload all Gnus features."
3251   (interactive)
3252   (let ((history load-history)
3253         feature)
3254     (while history
3255       (and (string-match "^gnus" (car (car history)))
3256            (setq feature (cdr (assq 'provide (car history))))
3257            (unload-feature feature 'force))
3258       (setq history (cdr history)))))
3259
3260 (defun gnus-group-startup-message (&optional x y)
3261   "Insert startup message in current buffer."
3262   ;; Insert the message.
3263   (erase-buffer)
3264   (insert
3265    (format "
3266      %s
3267            A newsreader 
3268       for GNU Emacs
3269
3270         Based on GNUS 
3271              written by 
3272      Masanobu UMEDA
3273
3274        A Praxis Release
3275       larsi@ifi.uio.no
3276
3277            gnus-version))
3278   ;; And then hack it.
3279   ;; 18 is the longest line.
3280   (indent-rigidly (point-min) (point-max) 
3281                   (/ (max (- (window-width) (or x 28)) 0) 2))
3282   (goto-char (point-min))
3283   ;; +4 is fuzzy factor.
3284   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3285
3286   ;; Fontify some.
3287   (goto-char (point-min))
3288   (search-forward "Praxis")
3289   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3290   (goto-char (point-min)))
3291
3292 (defun gnus-group-setup-buffer ()
3293   (or (get-buffer gnus-group-buffer)
3294       (progn
3295         (switch-to-buffer gnus-group-buffer)
3296         (gnus-add-current-to-buffer-list)
3297         (gnus-group-mode)
3298         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3299
3300 (defun gnus-group-list-groups (&optional level unread)
3301   "List newsgroups with level LEVEL or lower that have unread articles.
3302 Default is all subscribed groups.
3303 If argument UNREAD is non-nil, groups with no unread articles are also
3304 listed." 
3305   (interactive (list (if current-prefix-arg
3306                          (prefix-numeric-value current-prefix-arg)
3307                        (or
3308                         (gnus-group-default-level nil t)
3309                         gnus-level-subscribed))))
3310   (or level
3311       (setq level (car gnus-group-list-mode)
3312             unread (cdr gnus-group-list-mode)))
3313   (setq level (gnus-group-default-level level))
3314   (gnus-group-setup-buffer)     ;May call from out of group buffer
3315   (let ((case-fold-search nil)
3316         (group (gnus-group-group-name)))
3317     (funcall gnus-group-prepare-function level unread nil)
3318     (if (zerop (buffer-size))
3319         (gnus-message 5 gnus-no-groups-message)
3320       (goto-char (point-min))
3321       (if (not group)
3322           ;; Go to the first group with unread articles.
3323           (gnus-group-search-forward nil nil nil t)
3324         ;; Find the right group to put point on. If the current group
3325         ;; has disapeared in the new listing, try to find the next
3326         ;; one. If no next one can be found, just leave point at the
3327         ;; first newsgroup in the buffer.
3328         (if (not (gnus-goto-char
3329                   (text-property-any (point-min) (point-max) 
3330                                      'gnus-group (intern group))))
3331             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3332               (while (and newsrc
3333                           (not (gnus-goto-char 
3334                                 (text-property-any 
3335                                  (point-min) (point-max) 'gnus-group 
3336                                  (intern (car (car newsrc)))))))
3337                 (setq newsrc (cdr newsrc)))
3338               (or newsrc (progn (goto-char (point-max))
3339                                 (forward-line -1))))))
3340       ;; Adjust cursor point.
3341       (gnus-group-position-cursor))))
3342
3343 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3344   "List all newsgroups with unread articles of level LEVEL or lower.
3345 If ALL is non-nil, list groups that have no unread articles.
3346 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3347 If REGEXP, only list groups matching REGEXP."
3348   (set-buffer gnus-group-buffer)
3349   (let ((buffer-read-only nil)
3350         (newsrc (cdr gnus-newsrc-alist))
3351         (lowest (or lowest 1))
3352         info clevel unread group)
3353     (erase-buffer)
3354     (if (< lowest gnus-level-zombie)
3355         ;; List living groups.
3356         (while newsrc
3357           (setq info (car newsrc)
3358                 group (car info)
3359                 newsrc (cdr newsrc)
3360                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3361           (and unread ; This group might be bogus
3362                (or (not regexp)
3363                    (string-match regexp group))
3364                (<= (setq clevel (car (cdr info))) level) 
3365                (>= clevel lowest)
3366                (or all            ; We list all groups?
3367                    (eq unread t)  ; We list unactivated groups
3368                    (> unread 0)   ; We list groups with unread articles
3369                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3370                (gnus-group-insert-group-line 
3371                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3372
3373     ;; List dead groups.
3374     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3375          (gnus-group-prepare-flat-list-dead 
3376           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3377           gnus-level-zombie ?Z
3378           regexp))
3379     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3380          (gnus-group-prepare-flat-list-dead 
3381           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3382           gnus-level-killed ?K regexp))
3383
3384     (gnus-group-set-mode-line)
3385     (setq gnus-group-list-mode (cons level all))
3386     (run-hooks 'gnus-group-prepare-hook)))
3387
3388 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3389   ;; List zombies and killed lists somehwat faster, which was
3390   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3391   ;; this by ignoring the group format specification altogether.
3392   (let (group beg)
3393     (while groups
3394       (setq group (car groups)
3395             groups (cdr groups))
3396       (if (or (not regexp)
3397               (string-match regexp group))
3398           (progn
3399             (setq beg (point))
3400             (insert (format " %c     *: %s\n" mark group))
3401             (add-text-properties 
3402              beg (1+ beg) 
3403              (list 'gnus-group (intern group)
3404                    'gnus-unread t
3405                    'gnus-level level)))))))
3406
3407 (defun gnus-group-real-name (group)
3408   "Find the real name of a foreign newsgroup."
3409   (if (string-match ":[^:]+$" group)
3410       (substring group (1+ (match-beginning 0)))
3411     group))
3412
3413 (defun gnus-group-prefixed-name (group method)
3414   "Return the whole name from GROUP and METHOD."
3415   (and (stringp method) (setq method (gnus-server-to-method method)))
3416   (concat (format "%s" (car method))
3417           (if (and 
3418                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3419                (not (string= (nth 1 method) "")))
3420               (concat "+" (nth 1 method)))
3421           ":" group))
3422
3423 (defun gnus-group-real-prefix (group)
3424   "Return the prefix of the current group name."
3425   (if (string-match "^[^:]+:" group)
3426       (substring group 0 (match-end 0))
3427     ""))
3428
3429 (defun gnus-group-method-name (group)
3430   "Return the method used for selecting GROUP."
3431   (let ((prefix (gnus-group-real-prefix group)))
3432     (if (equal prefix "")
3433         gnus-select-method
3434       (if (string-match "^[^\\+]+\\+" prefix)
3435           (list (intern (substring prefix 0 (1- (match-end 0))))
3436                 (substring prefix (match-end 0) (1- (length prefix))))
3437         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3438
3439 (defun gnus-group-foreign-p (group)
3440   "Return nil if GROUP is native, non-nil if it is foreign."
3441   (string-match ":" group))
3442
3443 (defun gnus-group-set-info (info &optional method-only-group part)
3444   (let* ((entry (gnus-gethash
3445                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3446          (part-info info)
3447          (info (if method-only-group (nth 2 entry) info)))
3448     (if (not method-only-group)
3449         ()
3450       (or entry
3451           (error "Trying to change non-existent group %s" method-only-group))
3452       ;; We have recevied parts of the actual group info - either the
3453       ;; select method or the group parameters.  We first check
3454       ;; whether we have to extend the info, and if so, do that.
3455       (let ((len (length info))
3456             (total (if (eq part 'method) 5 6)))
3457         (and (< len total)
3458              (setcdr (nthcdr (1- len) info)
3459                      (make-list (- total len) nil)))
3460         ;; Then we enter the new info.
3461         (setcar (nthcdr (1- total) info) part-info)))
3462     ;; We uncompress some lists of marked articles.
3463     (let (marked)
3464       (if (not (setq marked (nth 3 info)))
3465           ()
3466         (while marked
3467           (or (eq 'score (car (car marked)))
3468               (eq 'bookmark (car (car marked)))
3469               (eq 'killed (car (car marked)))
3470               (setcdr (car marked) 
3471                       (gnus-uncompress-range (cdr (car marked)))))
3472           (setq marked (cdr marked)))))
3473     (if entry
3474         ()
3475       ;; This is a new group, so we just create it.
3476       (save-excursion
3477         (set-buffer gnus-group-buffer)
3478         (if (nth 4 info)
3479             ;; It's a foreign group...
3480             (gnus-group-make-group 
3481              (gnus-group-real-name (car info))
3482              (prin1-to-string (car (nth 4 info)))
3483              (nth 1 (nth 4 info)))
3484           ;; It's a native group.
3485           (gnus-group-make-group
3486            (car info)
3487            (prin1-to-string (car gnus-select-method))
3488            (nth 1 gnus-select-method)))
3489         (gnus-message 6 "Note: New group created")
3490         (setq entry 
3491               (gnus-gethash (gnus-group-prefixed-name 
3492                              (gnus-group-real-name (car info))
3493                              (or (nth 4 info) gnus-select-method))
3494                             gnus-newsrc-hashtb))))
3495     ;; Whether it was a new group or not, we now have the entry, so we
3496     ;; can do the update.
3497     (if entry
3498         (progn
3499           (setcar (nthcdr 2 entry) info)
3500           (if (and (not (eq (car entry) t)) 
3501                    (gnus-gethash (car info) gnus-active-hashtb))
3502               (let ((marked (nth 3 info)))
3503                 (setcar entry 
3504                         (max 0 (- (length (gnus-list-of-unread-articles 
3505                                            (car info)))
3506                                   (length (cdr (assq 'tick marked)))
3507                                   (length (cdr (assq 'dormant marked)))))))))
3508       (error "No such group: %s" (car info)))))
3509
3510 (defun gnus-group-set-method-info (group select-method)
3511   (gnus-group-set-info select-method group 'method))
3512
3513 (defun gnus-group-set-params-info (group params)
3514   (gnus-group-set-info params group 'params))
3515
3516 (defun gnus-group-update-group-line ()
3517   "This function updates the current line in the newsgroup buffer and
3518 moves the point to the colon."
3519   (let* ((buffer-read-only nil)
3520          (group (gnus-group-group-name))
3521          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3522     (if entry
3523         (gnus-dribble-enter 
3524          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3525                  ")")))
3526     (beginning-of-line)
3527     (delete-region (point) (progn (forward-line 1) (point)))
3528     (gnus-group-insert-group-line-info group)
3529     (forward-line -1)
3530     (gnus-group-position-cursor)))
3531
3532 (defun gnus-group-insert-group-line-info (group)
3533   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3534         active info)
3535     (if entry
3536         (progn
3537           (setq info (nth 2 entry))
3538           (gnus-group-insert-group-line 
3539            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3540       (setq active (gnus-gethash group gnus-active-hashtb))
3541       (gnus-group-insert-group-line 
3542        nil group 
3543        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3544        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3545
3546 (defun gnus-group-insert-group-line (gformat group level marked number method)
3547   (let* ((gformat (or gformat gnus-group-line-format-spec))
3548          (active (gnus-gethash group gnus-active-hashtb))
3549          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3550          (number-of-dormant (length (cdr (assq 'dormant marked))))
3551          (number-of-ticked (length (cdr (assq 'tick marked))))
3552          (number-of-ticked-and-dormant
3553           (+ number-of-ticked number-of-dormant))
3554          (number-of-unread-unticked 
3555           (if (numberp number) (int-to-string (max 0 number))
3556             "*"))
3557          (number-of-read
3558           (if (numberp number)
3559               (max 0 (- number-total number))
3560             "*"))
3561          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3562                            ((<= level gnus-level-unsubscribed) ?U)
3563                            ((= level gnus-level-zombie) ?Z)
3564                            (t ?K)))
3565          (qualified-group (gnus-group-real-name group))
3566          (newsgroup-description 
3567           (if gnus-description-hashtb
3568               (or (gnus-gethash group gnus-description-hashtb) "")
3569             ""))
3570          (moderated (if (member group gnus-moderated-list) ?m ? ))
3571          (moderated-string (if (eq moderated ?m) "(m)" ""))
3572          (method (gnus-server-get-method group method))
3573          (news-server (or (car (cdr method)) ""))
3574          (news-method (or (car method) ""))
3575          (news-method-string 
3576           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3577          (marked (if (and 
3578                       (numberp number) 
3579                       (zerop number)
3580                       (> number-of-ticked 0))
3581                      ?* ? ))
3582          (number (if (eq number t) "*" (+ number number-of-dormant 
3583                                           number-of-ticked)))
3584          (process-marked (if (member qualified-group gnus-group-marked)
3585                              gnus-process-mark ? ))
3586          (buffer-read-only nil)
3587          header ; passed as parameter to user-funcs.
3588          b)
3589     (beginning-of-line)
3590     (setq b (point))
3591     ;; Insert the text.
3592     (insert (eval gformat))
3593
3594     (add-text-properties 
3595      b (1+ b) (list 'gnus-group (intern group)
3596                     'gnus-unread (if (numberp number)
3597                                      (string-to-int number-of-unread-unticked)
3598                                    t)
3599                     'gnus-marked marked
3600                     'gnus-level level))))
3601
3602 (defun gnus-group-update-group (group &optional visible-only)
3603   "Update newsgroup info of GROUP.
3604 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3605   (save-excursion
3606     (set-buffer gnus-group-buffer)
3607     (let ((buffer-read-only nil)
3608           visible)
3609       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3610         (if entry
3611             (gnus-dribble-enter 
3612              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3613                      ")"))))
3614       ;; Buffer may be narrowed.
3615       (save-restriction
3616         (widen)
3617         ;; Search a line to modify.  If the buffer is large, the search
3618         ;; takes long time.  In most cases, current point is on the line
3619         ;; we are looking for.  So, first of all, check current line. 
3620         (if (or (progn
3621                   (beginning-of-line)
3622                   (eq (get-text-property (point) 'gnus-group)
3623                       (intern group)))
3624                 (progn
3625                   (gnus-goto-char 
3626                    (text-property-any 
3627                     (point-min) (point-max) 'gnus-group (intern group)))))
3628             ;; GROUP is listed in current buffer. So, delete old line.
3629             (progn
3630               (setq visible t)
3631               (beginning-of-line)
3632               (delete-region (point) (progn (forward-line 1) (point))))
3633           ;; No such line in the buffer, find out where it's supposed to
3634           ;; go, and insert it there (or at the end of the buffer).
3635           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3636           (or visible-only
3637               (let ((entry 
3638                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3639                 (while (and entry
3640                             (car entry)
3641                             (not
3642                              (gnus-goto-char
3643                               (text-property-any
3644                                (point-min) (point-max) 
3645                                'gnus-group (intern (car (car entry)))))))
3646                   (setq entry (cdr entry)))
3647                 (or entry (goto-char (point-max)))))))
3648       (if (or visible (not visible-only))
3649           (gnus-group-insert-group-line-info group))
3650       (gnus-group-set-mode-line))))
3651
3652 (defun gnus-group-set-mode-line ()
3653   (if (memq 'group gnus-updated-mode-lines)
3654       (let* ((gformat (or gnus-group-mode-line-format-spec
3655                           (setq gnus-group-mode-line-format-spec
3656                                 (gnus-parse-format 
3657                                  gnus-group-mode-line-format 
3658                                  gnus-group-mode-line-format-alist))))
3659              (news-server (car (cdr gnus-select-method)))
3660              (news-method (car gnus-select-method))
3661              (max-len 60)
3662              (mode-string (eval gformat)))
3663         (setq mode-string (eval gformat))
3664         (if (> (length mode-string) max-len) 
3665             (setq mode-string (substring mode-string 0 (- max-len 4))))
3666         (setq mode-line-buffer-identification mode-string)
3667         (set-buffer-modified-p t))))
3668
3669 (defun gnus-group-group-name ()
3670   "Get the name of the newsgroup on the current line."
3671   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3672     (and group (symbol-name group))))
3673
3674 (defun gnus-group-group-level ()
3675   "Get the level of the newsgroup on the current line."
3676   (get-text-property (gnus-point-at-bol) 'gnus-level))
3677
3678 (defun gnus-group-group-unread ()
3679   "Get the number of unread articles of the newsgroup on the current line."
3680   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3681
3682 (defun gnus-group-search-forward (&optional backward all level first-too)
3683   "Find the next newsgroup with unread articles.
3684 If BACKWARD is non-nil, find the previous newsgroup instead.
3685 If ALL is non-nil, just find any newsgroup.
3686 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3687 group exists.
3688 If FIRST-TOO, the current line is also eligible as a target."
3689   (let ((way (if backward -1 1))
3690         (low gnus-level-killed)
3691         (beg (point))
3692         pos found lev)
3693     (if (and backward (progn (beginning-of-line)) (bobp))
3694         nil
3695       (or first-too (forward-line way))
3696       (while (and 
3697               (not (eobp))
3698               (not (setq 
3699                     found 
3700                     (and (or all
3701                              (and
3702                               (let ((unread 
3703                                      (get-text-property (point) 'gnus-unread)))
3704                                 (or (eq unread t) (and unread (> unread 0))))
3705                               (setq lev (get-text-property (point)
3706                                                            'gnus-level))
3707                               (<= lev gnus-level-subscribed)))
3708                          (or (not level)
3709                              (and (setq lev (get-text-property (point)
3710                                                                'gnus-level))
3711                                   (or (= lev level)
3712                                       (and (< lev low)
3713                                            (< level lev)
3714                                            (progn
3715                                              (setq low lev)
3716                                              (setq pos (point))
3717                                              nil))))))))
3718               (zerop (forward-line way)))))
3719     (if found 
3720         (progn (gnus-group-position-cursor) t)
3721       (goto-char (or pos beg))
3722       (and pos t))))
3723
3724 ;;; Gnus group mode commands
3725
3726 ;; Group marking.
3727
3728 (defun gnus-group-mark-group (n &optional unmark no-advance)
3729   "Mark the current group."
3730   (interactive "p")
3731   (let ((buffer-read-only nil)
3732         group)
3733     (while 
3734         (and (> n 0) 
3735              (setq group (gnus-group-group-name))
3736              (progn
3737                (beginning-of-line)
3738                (forward-char 
3739                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3740                (delete-char 1)
3741                (if unmark
3742                    (progn
3743                      (insert " ")
3744                      (setq gnus-group-marked (delete group gnus-group-marked)))
3745                  (insert "#")
3746                  (setq gnus-group-marked
3747                        (cons group (delete group gnus-group-marked))))
3748                t)
3749              (or no-advance (zerop (gnus-group-next-group 1))))
3750       (setq n (1- n)))
3751     (gnus-summary-position-cursor)
3752     n))
3753
3754 (defun gnus-group-unmark-group (n)
3755   "Remove the mark from the current group."
3756   (interactive "p")
3757   (gnus-group-mark-group n 'unmark))
3758
3759 (defun gnus-group-mark-region (unmark beg end)
3760   "Mark all groups between point and mark.
3761 If UNMARK, remove the mark instead."
3762   (interactive "P\nr")
3763   (let ((num (count-lines beg end)))
3764     (save-excursion
3765       (goto-char beg)
3766       (- num (gnus-group-mark-group num unmark)))))
3767
3768 (defun gnus-group-remove-mark (group)
3769   (and (gnus-group-goto-group group)
3770        (save-excursion
3771          (gnus-group-mark-group 1 'unmark t))))
3772
3773 ;; Return a list of groups to work on.  Take into consideration N (the
3774 ;; prefix) and the list of marked groups.
3775 (defun gnus-group-process-prefix (n)
3776   (cond (n
3777          (setq n (prefix-numeric-value n))
3778          ;; There is a prefix, so we return a list of the N next
3779          ;; groups. 
3780          (let ((way (if (< n 0) -1 1))
3781                (n (abs n))
3782                group groups)
3783            (save-excursion
3784              (while (and (> n 0)
3785                          (setq group (gnus-group-group-name)))
3786                (setq groups (cons group groups))
3787                (setq n (1- n))
3788                (forward-line way)))
3789            (nreverse groups)))
3790         (gnus-group-marked
3791          ;; No prefix, but a list of marked articles.
3792          (reverse gnus-group-marked))
3793         (t
3794          ;; Neither marked articles or a prefix, so we return the
3795          ;; current group.
3796          (let ((group (gnus-group-group-name)))
3797            (and group (list group))))))
3798
3799 ;; Selecting groups.
3800
3801 (defun gnus-group-read-group (&optional all no-article group)
3802   "Read news in this newsgroup.
3803 If the prefix argument ALL is non-nil, already read articles become
3804 readable. If the optional argument NO-ARTICLE is non-nil, no article
3805 will be auto-selected upon group entry."
3806   (interactive "P")
3807   (let ((group (or group (gnus-group-group-name)))
3808         number active marked entry)
3809     (or group (error "No group on current line"))
3810     (setq marked 
3811           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3812     ;; This group might be a dead group. In that case we have to get
3813     ;; the number of unread articles from `gnus-active-hashtb'.
3814     (if entry
3815         (setq number (car entry))
3816       (if (setq active (gnus-gethash group gnus-active-hashtb))
3817           (setq number (- (1+ (cdr active)) (car active)))))
3818     (gnus-summary-read-group 
3819      group (or all (and (numberp number) 
3820                         (zerop (+ number (length (cdr (assq 'tick marked)))
3821                                   (length (cdr (assq 'dormant marked)))))))
3822      no-article)))
3823
3824 (defun gnus-group-select-group (&optional all)
3825   "Select this newsgroup.
3826 No article is selected automatically.
3827 If argument ALL is non-nil, already read articles become readable."
3828   (interactive "P")
3829   (gnus-group-read-group all t))
3830
3831 (defun gnus-group-select-group-all ()
3832   "Select the current group and display all articles in it."
3833   (interactive)
3834   (gnus-group-select-group 'all))
3835
3836 ;; Enter a group that is not in the group buffer. Non-nil is returned
3837 ;; if selection was successful.
3838 (defun gnus-group-read-ephemeral-group 
3839   (group method &optional activate quit-config)
3840   (let ((group (if (gnus-group-foreign-p group) group
3841                  (gnus-group-prefixed-name group method))))
3842     (gnus-sethash 
3843      group
3844      (list t nil (list group gnus-level-default-subscribed nil nil 
3845                        (append method
3846                                (list
3847                                 (list 'quit-config 
3848                                       (if quit-config quit-config
3849                                         (cons (current-buffer) 'summary)))))))
3850      gnus-newsrc-hashtb)
3851     (set-buffer gnus-group-buffer)
3852     (or (gnus-check-server method)
3853         (error "Unable to contact server: %s" (gnus-status-message method)))
3854     (if activate (or (gnus-request-group group)
3855                      (error "Couldn't request group")))
3856     (condition-case ()
3857         (gnus-group-read-group t t group)
3858       (error nil)
3859       (quit nil))
3860     (not (equal major-mode 'gnus-group-mode))))
3861   
3862 (defun gnus-group-jump-to-group (group)
3863   "Jump to newsgroup GROUP."
3864   (interactive 
3865    (list (completing-read 
3866           "Group: " gnus-active-hashtb nil 
3867           (memq gnus-select-method gnus-have-read-active-file))))
3868
3869   (if (equal group "")
3870       (error "Empty group name"))
3871
3872   (let ((b (text-property-any 
3873             (point-min) (point-max) 'gnus-group (intern group))))
3874     (if b
3875         ;; Either go to the line in the group buffer...
3876         (goto-char b)
3877       ;; ... or insert the line.
3878       (or
3879        (gnus-gethash group gnus-active-hashtb)
3880        (gnus-activate-group group)
3881        (error "%s error: %s" group (gnus-status-message group)))
3882
3883       (gnus-group-update-group group)
3884       (goto-char (text-property-any 
3885                   (point-min) (point-max) 'gnus-group (intern group)))))
3886   ;; Adjust cursor point.
3887   (gnus-group-position-cursor))
3888
3889 (defun gnus-group-goto-group (group)
3890   "Goto to newsgroup GROUP."
3891   (let ((b (text-property-any (point-min) (point-max) 
3892                               'gnus-group (intern group))))
3893     (and b (goto-char b))))
3894
3895 (defun gnus-group-next-group (n)
3896   "Go to next N'th newsgroup.
3897 If N is negative, search backward instead.
3898 Returns the difference between N and the number of skips actually
3899 done."
3900   (interactive "p")
3901   (gnus-group-next-unread-group n t))
3902
3903 (defun gnus-group-next-unread-group (n &optional all level)
3904   "Go to next N'th unread newsgroup.
3905 If N is negative, search backward instead.
3906 If ALL is non-nil, choose any newsgroup, unread or not.
3907 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3908 such group can be found, the next group with a level higher than
3909 LEVEL.
3910 Returns the difference between N and the number of skips actually
3911 made."
3912   (interactive "p")
3913   (let ((backward (< n 0))
3914         (n (abs n)))
3915     (while (and (> n 0)
3916                 (gnus-group-search-forward 
3917                  backward (or (not gnus-group-goto-unread) all) level))
3918       (setq n (1- n)))
3919     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3920                                (if level " on this level or higher" "")))
3921     n))
3922
3923 (defun gnus-group-prev-group (n)
3924   "Go to previous N'th newsgroup.
3925 Returns the difference between N and the number of skips actually
3926 done."
3927   (interactive "p")
3928   (gnus-group-next-unread-group (- n) t))
3929
3930 (defun gnus-group-prev-unread-group (n)
3931   "Go to previous N'th unread newsgroup.
3932 Returns the difference between N and the number of skips actually
3933 done."  
3934   (interactive "p")
3935   (gnus-group-next-unread-group (- n)))
3936
3937 (defun gnus-group-next-unread-group-same-level (n)
3938   "Go to next N'th unread newsgroup on the same level.
3939 If N is negative, search backward instead.
3940 Returns the difference between N and the number of skips actually
3941 done."
3942   (interactive "p")
3943   (gnus-group-next-unread-group n t (gnus-group-group-level))
3944   (gnus-group-position-cursor))
3945
3946 (defun gnus-group-prev-unread-group-same-level (n)
3947   "Go to next N'th unread newsgroup on the same level.
3948 Returns the difference between N and the number of skips actually
3949 done."
3950   (interactive "p")
3951   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3952   (gnus-group-position-cursor))
3953
3954 (defun gnus-group-best-unread-group (&optional exclude-group)
3955   "Go to the group with the highest level.
3956 If EXCLUDE-GROUP, do not go to that group."
3957   (interactive)
3958   (goto-char (point-min))
3959   (let ((best 100000)
3960         unread best-point)
3961     (while (setq unread (get-text-property (point) 'gnus-unread))
3962       (if (and (numberp unread) (> unread 0))
3963           (progn
3964             (if (and (< (get-text-property (point) 'gnus-level) best)
3965                      (or (not exclude-group)
3966                          (not (equal exclude-group (gnus-group-group-name)))))
3967                 (progn 
3968                   (setq best (get-text-property (point) 'gnus-level))
3969                   (setq best-point (point))))))
3970       (forward-line 1))
3971     (if best-point (goto-char best-point))
3972     (gnus-summary-position-cursor)
3973     (and best-point (gnus-group-group-name))))
3974
3975 (defun gnus-group-first-unread-group ()
3976   "Go to the first group with unread articles."
3977   (interactive)
3978   (prog1
3979       (let ((opoint (point))
3980             unread)
3981         (goto-char (point-min))
3982         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
3983                 (not (zerop unread)) ; Has unread articles.
3984                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
3985             (point) ; Success.
3986           (goto-char opoint)
3987           nil)) ; Not success.
3988     (gnus-group-position-cursor)))
3989
3990 (defun gnus-group-enter-server-mode ()
3991   "Jump to the server buffer."
3992   (interactive)
3993   (gnus-server-setup-buffer)
3994   (gnus-configure-windows 'server)
3995   (gnus-server-prepare))
3996
3997 (defun gnus-group-make-group (name method &optional address)
3998   "Add a new newsgroup.
3999 The user will be prompted for a NAME, for a select METHOD, and an
4000 ADDRESS."
4001   (interactive
4002    (cons 
4003     (read-string "Group name: ")
4004     (let ((method
4005            (completing-read 
4006             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4007             nil t)))
4008       (if (assoc method gnus-valid-select-methods)
4009           (list method
4010                 (if (memq 'prompt-address
4011                           (assoc method gnus-valid-select-methods))
4012                     (read-string "Address: ")
4013                   ""))
4014         (list method nil)))))
4015   
4016   (let* ((meth (if address (list (intern method) address) method))
4017          (nname (gnus-group-prefixed-name name meth))
4018          info)
4019     (and (gnus-gethash nname gnus-newsrc-hashtb)
4020          (error "Group %s already exists" nname))
4021     (gnus-group-change-level 
4022      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4023      gnus-level-default-subscribed gnus-level-killed 
4024      (and (gnus-group-group-name)
4025           (gnus-gethash (gnus-group-group-name)
4026                         gnus-newsrc-hashtb))
4027      t)
4028     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4029     (gnus-dribble-enter 
4030      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4031     (gnus-group-insert-group-line-info nname)
4032
4033     (if (assoc method gnus-valid-select-methods)
4034         (require (intern method)))
4035     (and (gnus-check-backend-function 'request-create-group nname)
4036          (gnus-request-create-group nname))))
4037
4038 (defun gnus-group-edit-group (group &optional part)
4039   "Edit the group on the current line."
4040   (interactive (list (gnus-group-group-name)))
4041   (let ((done-func '(lambda () 
4042                       "Exit editing mode and update the information."
4043                       (interactive)
4044                       (gnus-group-edit-group-done 'part 'group)))
4045         (part (or part 'info))
4046         (winconf (current-window-configuration))
4047         info)
4048     (or group (error "No group on current line"))
4049     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4050         (error "Killed group; can't be edited"))
4051     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4052     (gnus-configure-windows 'edit-group)
4053     (gnus-add-current-to-buffer-list)
4054     (emacs-lisp-mode)
4055     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4056     (use-local-map (copy-keymap emacs-lisp-mode-map))
4057     (local-set-key "\C-c\C-c" done-func)
4058     (make-local-variable 'gnus-prev-winconf)
4059     (setq gnus-prev-winconf winconf)
4060     ;; We modify the func to let it know what part it is editing.
4061     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4062     (setcar (cdr (cdr (nth 4 done-func))) group)
4063     (erase-buffer)
4064     (insert
4065      (cond 
4066       ((eq part 'method)
4067        ";; Type `C-c C-c' after editing the select method.\n\n")
4068       ((eq part 'params)
4069        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4070       ((eq part 'info)
4071        ";; Type `C-c C-c' after editing the group info.\n\n")))
4072     (let ((cinfo (gnus-copy-sequence info))
4073           marked)
4074       (if (not (setq marked (nth 3 cinfo)))
4075           ()
4076         (while marked
4077           (or (eq 'score (car (car marked)))
4078               (eq 'bookmark (car (car marked)))
4079               (eq 'killed (car (car marked)))
4080               (not (numberp (car (cdr (car marked)))))
4081               (setcdr (car marked) 
4082                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4083           (setq marked (cdr marked))))
4084       (insert 
4085        (pp-to-string
4086         (cond ((eq part 'method)
4087                (or (nth 4 info) "native"))
4088               ((eq part 'params)
4089                (nth 5 info))
4090               (t
4091                cinfo)))
4092        "\n"))))
4093
4094 (defun gnus-group-edit-group-method (group)
4095   "Edit the select method of GROUP."
4096   (interactive (list (gnus-group-group-name)))
4097   (gnus-group-edit-group group 'method))
4098
4099 (defun gnus-group-edit-group-parameters (group)
4100   "Edit the group parameters of GROUP."
4101   (interactive (list (gnus-group-group-name)))
4102   (gnus-group-edit-group group 'params))
4103
4104 (defun gnus-group-edit-group-done (part group)
4105   "Get info from buffer, update variables and jump to the group buffer."
4106   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4107   (goto-char (point-min))
4108   (let ((form (read (current-buffer)))
4109         (winconf gnus-prev-winconf))
4110     (if (eq part 'info) 
4111         (gnus-group-set-info form)
4112       (gnus-group-set-info form group part))
4113     (kill-buffer (current-buffer))
4114     (and winconf (set-window-configuration winconf))
4115     (set-buffer gnus-group-buffer)
4116     (gnus-group-update-group (gnus-group-group-name))
4117     (gnus-group-position-cursor)))
4118
4119 (defun gnus-group-make-help-group ()
4120   "Create the (ding) Gnus documentation group."
4121   (interactive)
4122   (let ((path load-path)
4123         name)
4124     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4125                                    "gnus-help" '(nndoc "gnus-help")))
4126                        gnus-newsrc-hashtb)
4127          (error "Documentation group already exists"))
4128     (while (and path
4129                 (not (file-exists-p (concat (file-name-as-directory (car path))
4130                                             "doc.txt"))))
4131       (setq path (cdr path)))
4132     (or path (error "Couldn't find doc group"))
4133     (gnus-group-make-group 
4134      (gnus-group-real-name name)
4135      (list 'nndoc name
4136            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4137            (list 'nndoc-article-type 'mbox))))
4138   (gnus-group-position-cursor))
4139
4140 (defun gnus-group-make-doc-group (file type)
4141   "Create a group that uses a single file as the source."
4142   (interactive 
4143    (list (read-file-name "File name: ") 
4144          (let ((err "")
4145                found char)
4146            (while (not found)
4147              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4148              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4149                                ((= char ?b) 'babyl)
4150                                ((= char ?d) 'digest)
4151                                (t (setq err (format "%c unknown. " char))
4152                                   nil))))
4153            found)))
4154   (let* ((file (expand-file-name file))
4155          (name (gnus-generate-new-group-name
4156                 (gnus-group-prefixed-name
4157                  (file-name-nondirectory file) '(nndoc "")))))
4158     (gnus-group-make-group 
4159      (gnus-group-real-name name)
4160      (list 'nndoc name
4161            (list 'nndoc-address file)
4162            (list 'nndoc-article-type type)))))
4163
4164 (defun gnus-group-make-archive-group ()
4165   "Create the (ding) Gnus archive group."
4166   (interactive)
4167   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4168                      gnus-newsrc-hashtb)
4169        (error "Archive group already exists"))
4170   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4171   (gnus-group-position-cursor))
4172
4173 (defun gnus-group-make-directory-group (dir)
4174   "Create an nndir group.
4175 The user will be prompted for a directory. The contents of this
4176 directory will be used as a newsgroup. The directory should contain
4177 mail messages or news articles in files that have numeric names."
4178   (interactive
4179    (list (read-file-name "Create group from directory: ")))
4180   (or (file-exists-p dir) (error "No such directory"))
4181   (or (file-directory-p dir) (error "Not a directory"))
4182   (gnus-group-make-group dir "nndir" dir)
4183   (gnus-group-position-cursor))
4184
4185 (defun gnus-group-make-kiboze-group (group address scores)
4186   "Create an nnkiboze group.
4187 The user will be prompted for a name, a regexp to match groups, and
4188 score file entries for articles to include in the group."
4189   (interactive
4190    (list
4191     (read-string "nnkiboze group name: ")
4192     (read-string "Source groups (regexp): ")
4193     (let ((headers (mapcar (lambda (group) (list group))
4194                            '("subject" "from" "number" "date" "message-id"
4195                              "references" "chars" "lines" "xref")))
4196           scores header regexp regexps)
4197       (while (not (equal "" (setq header (completing-read 
4198                                           "Match on header: " headers nil t))))
4199         (setq regexps nil)
4200         (while (not (equal "" (setq regexp (read-string 
4201                                             (format "Match on %s (string): "
4202                                                     header)))))
4203           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4204         (setq scores (cons (cons header regexps) scores)))
4205       scores)))
4206   (gnus-group-make-group group "nnkiboze" address)
4207   (save-excursion
4208     (gnus-set-work-buffer)
4209     (let (emacs-lisp-mode-hook)
4210       (pp scores (current-buffer)))
4211     (write-region (point-min) (point-max) 
4212                   (concat (or gnus-kill-files-directory "~/News")
4213                           "nnkiboze:" group "." gnus-score-file-suffix)))
4214   (gnus-group-position-cursor))
4215
4216 (defun gnus-group-add-to-virtual (n vgroup)
4217   "Add the current group to a virtual group."
4218   (interactive
4219    (list current-prefix-arg
4220          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4221                           "nnvirtual:")))
4222   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4223       (error "%s is not an nnvirtual group" vgroup))
4224   (let* ((groups (gnus-group-process-prefix n))
4225          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4226     (setcar (cdr method)
4227             (concat 
4228              (nth 1 method) "\\|"
4229              (mapconcat 
4230               (lambda (s) 
4231                 (gnus-group-remove-mark s)
4232                 (concat "\\(^" (regexp-quote s) "$\\)"))
4233               groups "\\|"))))
4234   (gnus-group-position-cursor))
4235
4236 (defun gnus-group-make-empty-virtual (group)
4237   "Create a new, fresh, empty virtual group."
4238   (interactive "sCreate new, empty virtual group: ")
4239   (let* ((method (list 'nnvirtual "^$"))
4240          (pgroup (gnus-group-prefixed-name group method)))
4241     ;; Check whether it exists already.
4242     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4243          (error "Group %s already exists." pgroup))
4244     ;; Subscribe the new group after the group on the current line.
4245     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4246     (gnus-group-update-group pgroup)
4247     (forward-line -1)
4248     (gnus-group-position-cursor)))
4249
4250 (defun gnus-group-enter-directory (dir)
4251   "Enter an ephemeral nneething group."
4252   (interactive "DDirectory to read: ")
4253   (let* ((method (list 'nneething dir))
4254          (leaf (gnus-group-prefixed-name
4255                 (file-name-nondirectory (directory-file-name dir))
4256                 method))
4257          (name (gnus-generate-new-group-name leaf)))
4258     (let ((nneething-read-only t))
4259       (or (gnus-group-read-ephemeral-group 
4260            name method t
4261            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4262                                       'summary 'group)))
4263           (error "Couldn't enter %s" dir)))))
4264
4265 ;; Group sorting commands
4266 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4267
4268 (defun gnus-group-sort-groups ()
4269   "Sort the group buffer using `gnus-group-sort-function'."
4270   (interactive)
4271   (setq gnus-newsrc-alist 
4272         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4273   (gnus-make-hashtable-from-newsrc-alist)
4274   (gnus-group-list-groups))
4275
4276 (defun gnus-group-sort-by-alphabet (info1 info2)
4277   (string< (car info1) (car info2)))
4278
4279 (defun gnus-group-sort-by-unread (info1 info2)
4280   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4281         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4282     (< (or (and (numberp n1) n1) 0)
4283        (or (and (numberp n2) n2) 0))))
4284
4285 (defun gnus-group-sort-by-level (info1 info2)
4286   (< (nth 1 info1) (nth 1 info2)))
4287
4288 ;; Group catching up.
4289
4290 (defun gnus-group-catchup-current (&optional n all)
4291   "Mark all articles not marked as unread in current newsgroup as read.
4292 If prefix argument N is numeric, the ARG next newsgroups will be
4293 caught up. If ALL is non-nil, marked articles will also be marked as
4294 read. Cross references (Xref: header) of articles are ignored.
4295 The difference between N and actual number of newsgroups that were
4296 caught up is returned."
4297   (interactive "P")
4298   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4299                gnus-expert-user
4300                (gnus-y-or-n-p
4301                 (if all
4302                     "Do you really want to mark all articles as read? "
4303                   "Mark all unread articles as read? "))))
4304       n
4305     (let ((groups (gnus-group-process-prefix n))
4306           (ret 0))
4307       (while groups
4308         ;; Virtual groups have to be given special treatment. 
4309         (let ((method (gnus-find-method-for-group (car groups))))
4310           (if (eq 'nnvirtual (car method))
4311               (nnvirtual-catchup-group
4312                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4313         (gnus-group-remove-mark (car groups))
4314         (if (prog1
4315                 (gnus-group-goto-group (car groups))
4316               (gnus-group-catchup (car groups) all))
4317             (gnus-group-update-group-line)
4318           (setq ret (1+ ret)))
4319         (setq groups (cdr groups)))
4320       (gnus-group-next-unread-group 1)
4321       ret)))
4322
4323 (defun gnus-group-catchup-current-all (&optional n)
4324   "Mark all articles in current newsgroup as read.
4325 Cross references (Xref: header) of articles are ignored."
4326   (interactive "P")
4327   (gnus-group-catchup-current n 'all))
4328
4329 (defun gnus-group-catchup (group &optional all)
4330   "Mark all articles in GROUP as read.
4331 If ALL is non-nil, all articles are marked as read.
4332 The return value is the number of articles that were marked as read,
4333 or nil if no action could be taken."
4334   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4335          (num (car entry))
4336          (marked (nth 3 (nth 2 entry))))
4337     (if (not (numberp (car entry)))
4338         (gnus-message 1 "Can't catch up; non-active group")
4339       ;; Do the updating only if the newsgroup isn't killed.
4340       (if (not entry)
4341           ()
4342         (gnus-update-read-articles 
4343          group (and (not all) (append (cdr (assq 'tick marked))
4344                                       (cdr (assq 'dormant marked))))
4345          nil (and (not all) (cdr (assq 'tick marked))))
4346         (and all 
4347              (setq marked (nth 3 (nth 2 entry)))
4348              (setcar (nthcdr 3 (nth 2 entry)) 
4349                      (delq (assq 'dormant marked) 
4350                            (nth 3 (nth 2 entry)))))))
4351     num))
4352
4353 (defun gnus-group-expire-articles (&optional n)
4354   "Expire all expirable articles in the current newsgroup."
4355   (interactive "P")
4356   (let ((groups (gnus-group-process-prefix n))
4357         group)
4358     (or groups (error "No groups to expire"))
4359     (while groups
4360       (setq group (car groups)
4361             groups (cdr groups))
4362       (gnus-group-remove-mark group)
4363       (if (not (gnus-check-backend-function 'request-expire-articles group))
4364           ()
4365         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4366                (expirable (if (memq 'total-expire (nth 5 info))
4367                               (cons nil (gnus-list-of-read-articles group))
4368                             (assq 'expire (nth 3 info)))))
4369           (and expirable 
4370                (setcdr expirable
4371                        (gnus-request-expire-articles 
4372                         (cdr expirable) group))))))))
4373
4374 (defun gnus-group-expire-all-groups ()
4375   "Expire all expirable articles in all newsgroups."
4376   (interactive)
4377   (save-excursion
4378     (gnus-message 5 "Expiring...")
4379     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4380                                      (cdr gnus-newsrc-alist))))
4381       (gnus-group-expire-articles nil)))
4382   (gnus-group-position-cursor)
4383   (gnus-message 5 "Expiring...done"))
4384
4385 (defun gnus-group-set-current-level (n level)
4386   "Set the level of the next N groups to LEVEL."
4387   (interactive "P\nnLevel: ")
4388   (or (and (>= level 1) (<= level gnus-level-killed))
4389       (error "Illegal level: %d" level))
4390   (let ((groups (gnus-group-process-prefix n))
4391         group)
4392     (while groups
4393       (setq group (car groups)
4394             groups (cdr groups))
4395       (gnus-group-remove-mark group)
4396       (gnus-message 6 "Changed level of %s from %d to %d" 
4397                     group (gnus-group-group-level) level)
4398       (gnus-group-change-level group level
4399                                (gnus-group-group-level))
4400       (gnus-group-update-group-line)))
4401   (gnus-group-position-cursor))
4402
4403 (defun gnus-group-unsubscribe-current-group (&optional n)
4404   "Toggle subscription of the current group.
4405 If given numerical prefix, toggle the N next groups."
4406   (interactive "P")
4407   (let ((groups (gnus-group-process-prefix n))
4408         group)
4409     (while groups
4410       (setq group (car groups)
4411             groups (cdr groups))
4412       (gnus-group-remove-mark group)
4413       (gnus-group-unsubscribe-group
4414        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4415                  gnus-level-default-unsubscribed
4416                gnus-level-default-subscribed))
4417       (gnus-group-update-group-line))
4418     (gnus-group-next-group 1)))
4419
4420 (defun gnus-group-unsubscribe-group (group &optional level)
4421   "Toggle subscribe from/to unsubscribe GROUP.
4422 New newsgroup is added to .newsrc automatically."
4423   (interactive
4424    (list (completing-read
4425           "Group: " gnus-active-hashtb nil 
4426           (memq gnus-select-method gnus-have-read-active-file))))
4427   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4428     (cond (newsrc
4429            ;; Toggle subscription flag.
4430            (gnus-group-change-level 
4431             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4432                                            gnus-level-subscribed) 
4433                                        (1+ gnus-level-subscribed)
4434                                      gnus-level-default-subscribed)))
4435            (gnus-group-update-group group))
4436           ((and (stringp group)
4437                 (or (not (memq gnus-select-method gnus-have-read-active-file))
4438                     (gnus-gethash group gnus-active-hashtb)))
4439            ;; Add new newsgroup.
4440            (gnus-group-change-level 
4441             group 
4442             (if level level gnus-level-default-subscribed) 
4443             (or (and (member group gnus-zombie-list) 
4444                      gnus-level-zombie) 
4445                 gnus-level-killed)
4446             (and (gnus-group-group-name)
4447                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4448            (gnus-group-update-group group))
4449           (t (error "No such newsgroup: %s" group)))
4450     (gnus-group-position-cursor)))
4451
4452 (defun gnus-group-transpose-groups (n)
4453   "Move the current newsgroup up N places.
4454 If given a negative prefix, move down instead. The difference between
4455 N and the number of steps taken is returned." 
4456   (interactive "p")
4457   (or (gnus-group-group-name)
4458       (error "No group on current line"))
4459   (gnus-group-kill-group 1)
4460   (prog1
4461       (forward-line (- n))
4462     (gnus-group-yank-group)
4463     (gnus-group-position-cursor)))
4464
4465 (defun gnus-group-kill-all-zombies ()
4466   "Kill all zombie newsgroups."
4467   (interactive)
4468   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4469   (setq gnus-zombie-list nil)
4470   (gnus-group-list-groups))
4471
4472 (defun gnus-group-kill-region (begin end)
4473   "Kill newsgroups in current region (excluding current point).
4474 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4475   (interactive "r")
4476   (let ((lines
4477          ;; Count lines.
4478          (save-excursion
4479            (count-lines
4480             (progn
4481               (goto-char begin)
4482               (beginning-of-line)
4483               (point))
4484             (progn
4485               (goto-char end)
4486               (beginning-of-line)
4487               (point))))))
4488     (goto-char begin)
4489     (beginning-of-line)                 ;Important when LINES < 1
4490     (gnus-group-kill-group lines)))
4491
4492 (defun gnus-group-kill-group (&optional n)
4493   "The the next N groups.
4494 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4495 However, only groups that were alive can be yanked; already killed 
4496 groups or zombie groups can't be yanked.
4497 The return value is the name of the (last) group that was killed."
4498   (interactive "P")
4499   (let ((buffer-read-only nil)
4500         (groups (gnus-group-process-prefix n))
4501         group entry level)
4502     (while groups
4503       (setq group (car groups)
4504             groups (cdr groups))
4505       (gnus-group-remove-mark group)
4506       (setq level (gnus-group-group-level))
4507       (gnus-delete-line)
4508       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4509           (setq gnus-list-of-killed-groups 
4510                 (cons (cons (car entry) (nth 2 entry)) 
4511                       gnus-list-of-killed-groups)))
4512       (gnus-group-change-level 
4513        (if entry entry group) gnus-level-killed (if entry nil level)))
4514     (gnus-group-position-cursor)
4515     group))
4516
4517 (defun gnus-group-yank-group (&optional arg)
4518   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4519 inserting it before the current newsgroup.  The numeric ARG specifies
4520 how many newsgroups are to be yanked.  The name of the (last)
4521 newsgroup yanked is returned."
4522   (interactive "p")
4523   (if (not arg) (setq arg 1))
4524   (let (info group prev)
4525     (while (>= (setq arg (1- arg)) 0)
4526       (if (not (setq info (car gnus-list-of-killed-groups)))
4527           (error "No more newsgroups to yank"))
4528       (setq group (nth 2 info))
4529       ;; Find which newsgroup to insert this one before - search
4530       ;; backward until something suitable is found. If there are no
4531       ;; other newsgroups in this buffer, just make this newsgroup the
4532       ;; first newsgroup.
4533       (setq prev (gnus-group-group-name))
4534       (gnus-group-change-level 
4535        info (nth 2 info) gnus-level-killed 
4536        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4537        t)
4538       (gnus-group-insert-group-line-info (nth 1 info))
4539       (setq gnus-list-of-killed-groups 
4540             (cdr gnus-list-of-killed-groups)))
4541     (forward-line -1)
4542     (gnus-group-position-cursor)
4543     group))
4544       
4545 (defun gnus-group-list-all-groups (&optional arg)
4546   "List all newsgroups with level ARG or lower.
4547 Default is gnus-level-unsubscribed, which lists all subscribed and most
4548 unsubscribed groups."
4549   (interactive "P")
4550   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4551
4552 (defun gnus-group-list-killed ()
4553   "List all killed newsgroups in the group buffer."
4554   (interactive)
4555   (if (not gnus-killed-list)
4556       (gnus-message 6 "No killed groups")
4557     (let (gnus-group-list-mode)
4558       (funcall gnus-group-prepare-function 
4559                gnus-level-killed t gnus-level-killed))
4560     (goto-char (point-min)))
4561   (gnus-group-position-cursor))
4562
4563 (defun gnus-group-list-zombies ()
4564   "List all zombie newsgroups in the group buffer."
4565   (interactive)
4566   (if (not gnus-zombie-list)
4567       (gnus-message 6 "No zombie groups")
4568     (let (gnus-group-list-mode)
4569       (funcall gnus-group-prepare-function
4570                gnus-level-zombie t gnus-level-zombie))
4571     (goto-char (point-min)))
4572   (gnus-group-position-cursor))
4573
4574 (defun gnus-group-get-new-news (&optional arg)
4575   "Get newly arrived articles.
4576 If ARG is non-nil, it should be a number between one and nine to
4577 specify which levels you are interested in re-scanning."
4578   (interactive "P")
4579   (run-hooks 'gnus-get-new-news-hook)
4580   (setq arg (gnus-group-default-level arg t))
4581   (if (and gnus-read-active-file (not arg))
4582       (progn
4583         (gnus-read-active-file)
4584         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4585     (let ((gnus-read-active-file (not arg))
4586           (gnus-have-read-active-file 
4587            (and (not arg) gnus-have-read-active-file)))
4588       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4589   (gnus-group-list-groups))
4590
4591 (defun gnus-group-get-new-news-this-group (&optional n)
4592   "Check for newly arrived news in the current group (and the N-1 next groups).
4593 The difference between N and the number of newsgroup checked is returned.
4594 If N is negative, this group and the N-1 previous groups will be checked."
4595   (interactive "P")
4596   (let* ((groups (gnus-group-process-prefix n))
4597          (ret (if (numberp n) (- n (length groups)) 0))
4598          group)
4599     (while groups
4600       (setq group (car groups)
4601             groups (cdr groups))
4602       (gnus-group-remove-mark group)
4603       (or (gnus-get-new-news-in-group group)
4604           (progn 
4605             (ding) 
4606             (message "%s error: %s" group (gnus-status-message group))
4607             (sit-for 2))))
4608     (gnus-group-next-unread-group 1 t)
4609     (gnus-summary-position-cursor)
4610     ret))
4611
4612 (defun gnus-get-new-news-in-group (group)
4613   (and group 
4614        (gnus-activate-group group)
4615        (progn
4616          (gnus-get-unread-articles-in-group 
4617           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4618           (gnus-gethash group gnus-active-hashtb))
4619          (gnus-group-update-group-line)
4620          t)))
4621
4622 (defun gnus-group-fetch-faq (group)
4623   "Fetch the FAQ for the current group."
4624   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4625   (or group (error "No group name given"))
4626   (let ((file (concat gnus-group-faq-directory group))) 
4627     (if (not (file-exists-p file))
4628         (error "No such file: %s" file)
4629       (find-file file))))
4630   
4631 (defun gnus-group-describe-group (force &optional group)
4632   "Display a description of the current newsgroup."
4633   (interactive (list current-prefix-arg (gnus-group-group-name)))
4634   (and force (setq gnus-description-hashtb nil))
4635   (let ((method (gnus-find-method-for-group group))
4636         desc)
4637     (or group (error "No group name given"))
4638     (and (or (and gnus-description-hashtb
4639                   ;; We check whether this group's method has been
4640                   ;; queried for a description file.  
4641                   (gnus-gethash 
4642                    (gnus-group-prefixed-name "" method) 
4643                    gnus-description-hashtb))
4644              (setq desc (gnus-group-get-description group))
4645              (gnus-read-descriptions-file method))
4646          (message
4647           (or desc (gnus-gethash group gnus-description-hashtb)
4648               "No description available")))))
4649
4650 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4651 (defun gnus-group-describe-all-groups (&optional force)
4652   "Pop up a buffer with descriptions of all newsgroups."
4653   (interactive "P")
4654   (and force (setq gnus-description-hashtb nil))
4655   (if (not (or gnus-description-hashtb
4656                (gnus-read-all-descriptions-files)))
4657       (error "Couldn't request descriptions file"))
4658   (let ((buffer-read-only nil)
4659         b)
4660     (erase-buffer)
4661     (mapatoms
4662      (lambda (group)
4663        (setq b (point))
4664        (insert (format "      *: %-20s %s\n" (symbol-name group)
4665                        (symbol-value group)))
4666        (add-text-properties 
4667         b (1+ b) (list 'gnus-group group
4668                        'gnus-unread t 'gnus-marked nil
4669                        'gnus-level (1+ gnus-level-subscribed))))
4670      gnus-description-hashtb)
4671     (goto-char (point-min))
4672     (gnus-group-position-cursor)))
4673
4674 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4675 (defun gnus-group-apropos (regexp &optional search-description)
4676   "List all newsgroups that have names that match a regexp."
4677   (interactive "sGnus apropos (regexp): ")
4678   (let ((prev "")
4679         (obuf (current-buffer))
4680         groups des)
4681     ;; Go through all newsgroups that are known to Gnus.
4682     (mapatoms 
4683      (lambda (group)
4684        (and (symbol-name group)
4685             (string-match regexp (symbol-name group))
4686             (setq groups (cons (symbol-name group) groups))))
4687      gnus-active-hashtb)
4688     ;; Go through all descriptions that are known to Gnus. 
4689     (if search-description
4690         (mapatoms 
4691          (lambda (group)
4692            (and (string-match regexp (symbol-value group))
4693                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4694                 (setq groups (cons (symbol-name group) groups))))
4695          gnus-description-hashtb))
4696     (if (not groups)
4697         (gnus-message 3 "No groups matched \"%s\"." regexp)
4698       ;; Print out all the groups.
4699       (save-excursion
4700         (pop-to-buffer "*Gnus Help*")
4701         (buffer-disable-undo (current-buffer))
4702         (erase-buffer)
4703         (setq groups (sort groups 'string<))
4704         (while groups
4705           ;; Groups may be entered twice into the list of groups.
4706           (if (not (string= (car groups) prev))
4707               (progn
4708                 (insert (setq prev (car groups)) "\n")
4709                 (if (and gnus-description-hashtb
4710                          (setq des (gnus-gethash (car groups) 
4711                                                  gnus-description-hashtb)))
4712                     (insert "  " des "\n"))))
4713           (setq groups (cdr groups)))
4714         (goto-char (point-min))))
4715     (pop-to-buffer obuf)))
4716
4717 (defun gnus-group-description-apropos (regexp)
4718   "List all newsgroups that have names or descriptions that match a regexp."
4719   (interactive "sGnus description apropos (regexp): ")
4720   (if (not (or gnus-description-hashtb
4721                (gnus-read-all-descriptions-files)))
4722       (error "Couldn't request descriptions file"))
4723   (gnus-group-apropos regexp t))
4724
4725 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4726 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4727   "List all groups with unread articles that match REGEXP.
4728 If the prefix LEVEL is non-nil, it should be a number that says which
4729 level to cut off listing groups. 
4730 If ALL, also list groups with no unread articles.
4731 If LOWEST, don't list groups with level lower than LOWEST."
4732   (interactive "P\nsList newsgroups matching: ")
4733   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4734                            all (or lowest 1) regexp)
4735   (goto-char (point-min))
4736   (gnus-group-position-cursor))
4737
4738 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4739   "List all groups that match REGEXP.
4740 If the prefix LEVEL is non-nil, it should be a number that says which
4741 level to cut off listing groups. 
4742 If LOWEST, don't list groups with level lower than LOWEST."
4743   (interactive "P\nsList newsgroups matching: ")
4744   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4745
4746 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4747 (defun gnus-group-save-newsrc ()
4748   "Save the Gnus startup files."
4749   (interactive)
4750   (gnus-save-newsrc-file))
4751
4752 (defun gnus-group-restart (&optional arg)
4753   "Force Gnus to read the .newsrc file."
4754   (interactive "P")
4755   (gnus-save-newsrc-file)
4756   (gnus-setup-news 'force)
4757   (gnus-group-list-groups arg))
4758
4759 (defun gnus-group-read-init-file ()
4760   "Read the Gnus elisp init file."
4761   (interactive)
4762   (gnus-read-init-file))
4763
4764 (defun gnus-group-check-bogus-groups (&optional silent)
4765   "Check bogus newsgroups.
4766 If given a prefix, don't ask for confirmation before removing a bogus
4767 group."
4768   (interactive "P")
4769   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4770   (gnus-group-list-groups))
4771
4772 (defun gnus-group-edit-global-kill (&optional article group)
4773   "Edit the global kill file.
4774 If GROUP, edit that local kill file instead."
4775   (interactive "P")
4776   (setq gnus-current-kill-article article)
4777   (gnus-kill-file-edit-file group)
4778   (gnus-message 6
4779    (substitute-command-keys
4780     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4781
4782 (defun gnus-group-edit-local-kill (article group)
4783   "Edit a local kill file."
4784   (interactive (list nil (gnus-group-group-name)))
4785   (gnus-group-edit-global-kill article group))
4786
4787 (defun gnus-group-force-update ()
4788   "Update `.newsrc' file."
4789   (interactive)
4790   (gnus-save-newsrc-file))
4791
4792 (defun gnus-group-suspend ()
4793   "Suspend the current Gnus session.
4794 In fact, cleanup buffers except for group mode buffer.
4795 The hook gnus-suspend-gnus-hook is called before actually suspending."
4796   (interactive)
4797   (run-hooks 'gnus-suspend-gnus-hook)
4798   ;; Kill Gnus buffers except for group mode buffer.
4799   (let ((group-buf (get-buffer gnus-group-buffer)))
4800     ;; Do this on a separate list in case the user does a ^G before we finish
4801     (let ((gnus-buffer-list
4802            (delq group-buf (delq gnus-dribble-buffer
4803                                  (append gnus-buffer-list nil)))))
4804       (while gnus-buffer-list
4805         (gnus-kill-buffer (car gnus-buffer-list))
4806         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4807     (if group-buf
4808         (progn
4809           (setq gnus-buffer-list (list group-buf))
4810           (bury-buffer group-buf)
4811           (delete-windows-on group-buf t)))))
4812
4813 (defun gnus-group-clear-dribble ()
4814   "Clear all information from the dribble buffer."
4815   (interactive)
4816   (gnus-dribble-clear))
4817
4818 (defun gnus-group-exit ()
4819   "Quit reading news after updating .newsrc.eld and .newsrc.
4820 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4821   (interactive)
4822   (if (or noninteractive                ;For gnus-batch-kill
4823           (zerop (buffer-size))         ;No news is good news.
4824           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4825           (not gnus-interactive-exit)   ;Without confirmation
4826           gnus-expert-user
4827           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4828       (progn
4829         (run-hooks 'gnus-exit-gnus-hook)
4830         ;; Offer to save data from non-quitted summary buffers.
4831         (gnus-offer-save-summaries)
4832         ;; Save the newsrc file(s).
4833         (gnus-save-newsrc-file)
4834         ;; Kill-em-all.
4835         (gnus-close-backends)
4836         ;; Reset everything.
4837         (gnus-clear-system))))
4838
4839 (defun gnus-close-backends ()
4840   ;; Send a close request to all backends that support such a request. 
4841   (let ((methods gnus-valid-select-methods)
4842         func)
4843     (while methods
4844       (if (fboundp (setq func (intern (concat (car (car methods))
4845                                               "-request-close"))))
4846           (funcall func))
4847       (setq methods (cdr methods)))))
4848
4849 (defun gnus-group-quit ()
4850   "Quit reading news without updating .newsrc.eld or .newsrc.
4851 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4852   (interactive)
4853   (if (or noninteractive                ;For gnus-batch-kill
4854           (zerop (buffer-size))
4855           (not (gnus-server-opened gnus-select-method))
4856           gnus-expert-user
4857           (not gnus-current-startup-file)
4858           (gnus-yes-or-no-p
4859            (format "Quit reading news without saving %s? "
4860                    (file-name-nondirectory gnus-current-startup-file))))
4861       (progn
4862         (run-hooks 'gnus-exit-gnus-hook)
4863         (if gnus-use-full-window
4864             (delete-other-windows)
4865           (gnus-remove-some-windows))
4866         (gnus-dribble-save)
4867         (gnus-close-backends)
4868         (gnus-clear-system))))
4869
4870 (defun gnus-offer-save-summaries ()
4871   (let ((buffers (buffer-list))
4872         answer)
4873     (save-excursion
4874       (while (and buffers (not (eq answer ?q)))
4875         (and 
4876          ;; We look for buffers with "Summary" in the name.
4877          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4878          (progn
4879            (set-buffer (car buffers))
4880            ;; We check that this is, indeed, a summary buffer.
4881            (eq major-mode 'gnus-summary-mode)) 
4882          ;; We ask the user whether she wants to save the info.
4883          (or (eq answer ?!)
4884              (progn
4885                (setq answer nil)
4886                (while (not (memq answer '(?y ?n ?! ?q)))
4887                  (message (format "%sUpdate summary buffer %s? (y, n, !, q)"
4888                                   (if answer "Illegal char. " "")
4889                                   (buffer-name)))
4890                  (setq answer (read-char)))
4891                (or (eq answer ?y) (eq answer ?!))))
4892          ;; We do it by simply exiting.
4893          (gnus-summary-exit))
4894         (setq buffers (cdr buffers))))))
4895
4896 (defun gnus-group-describe-briefly ()
4897   "Give a one line description of the group mode commands."
4898   (interactive)
4899   (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")))
4900
4901 (defun gnus-group-browse-foreign-server (method)
4902   "Browse a foreign news server.
4903 If called interactively, this function will ask for a select method
4904  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4905 If not, METHOD should be a list where the first element is the method
4906 and the second element is the address."
4907   (interactive
4908    (list (let ((how (completing-read 
4909                      "Which backend: "
4910                      (append gnus-valid-select-methods gnus-server-alist)
4911                      nil t "nntp")))
4912            ;; We either got a backend name or a virtual server name.
4913            ;; If the first, we also need an address.
4914            (if (assoc how gnus-valid-select-methods)
4915                (list (intern how)
4916                      ;; Suggested by mapjph@bath.ac.uk.
4917                      (completing-read 
4918                       "Address: " 
4919                       (mapcar (lambda (server) (list server))
4920                               gnus-secondary-servers)))
4921              ;; We got a server name, so we find the method.
4922              (gnus-server-to-method how)))))
4923   (gnus-browse-foreign-server method))
4924
4925 \f
4926 ;;;
4927 ;;; Browse Server Mode
4928 ;;;
4929
4930 (defvar gnus-browse-mode-hook nil)
4931 (defvar gnus-browse-mode-map nil)
4932 (put 'gnus-browse-mode 'mode-class 'special)
4933
4934 (if gnus-browse-mode-map
4935     nil
4936   (setq gnus-browse-mode-map (make-keymap))
4937   (suppress-keymap gnus-browse-mode-map)
4938   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4939   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4940   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4941   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4942   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4943   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4944   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4945   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4946   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4947   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4948   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4949   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4950   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4951   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4952   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4953   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4954   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4955   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4956   )
4957
4958 (defvar gnus-browse-current-method nil)
4959 (defvar gnus-browse-return-buffer nil)
4960
4961 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4962
4963 (defun gnus-browse-foreign-server (method &optional return-buffer)
4964   (setq gnus-browse-current-method method)
4965   (setq gnus-browse-return-buffer return-buffer)
4966   (let ((gnus-select-method method)
4967         groups group)
4968     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4969     (or (gnus-check-server method)
4970         (error "Unable to contact server: %s" (gnus-status-message method)))
4971     (or (gnus-request-list method)
4972         (error "Couldn't request list: %s" (gnus-status-message method)))
4973     (get-buffer-create gnus-browse-buffer)
4974     (gnus-add-current-to-buffer-list)
4975     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4976     (gnus-configure-windows 'browse)
4977     (buffer-disable-undo (current-buffer))
4978     (let ((buffer-read-only nil))
4979       (erase-buffer))
4980     (gnus-browse-mode)
4981     (setq mode-line-buffer-identification
4982           (format
4983            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4984     (save-excursion
4985       (set-buffer nntp-server-buffer)
4986       (let ((cur (current-buffer)))
4987         (goto-char (point-min))
4988         (or (string= gnus-ignored-newsgroups "")
4989             (delete-matching-lines gnus-ignored-newsgroups))
4990         (while (re-search-forward 
4991                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4992           (goto-char (match-end 1))
4993           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4994                                                      (match-end 1))
4995                                    (max 0 (- (1+ (read cur)) (read cur))))
4996                              groups)))))
4997     (setq groups (sort groups 
4998                        (lambda (l1 l2)
4999                          (string< (car l1) (car l2)))))
5000     (let ((buffer-read-only nil))
5001       (while groups
5002         (setq group (car groups))
5003         (insert 
5004          (format "K%7d: %s\n" (cdr group) (car group)))
5005         (setq groups (cdr groups))))
5006     (switch-to-buffer (current-buffer))
5007     (goto-char (point-min))
5008     (gnus-group-position-cursor)))
5009
5010 (defun gnus-browse-mode ()
5011   "Major mode for browsing a foreign server.
5012
5013 All normal editing commands are switched off.
5014
5015 \\<gnus-browse-mode-map>
5016 The only things you can do in this buffer is
5017
5018 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5019 The group will be inserted into the group buffer upon exit from this
5020 buffer.  
5021
5022 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5023
5024 3) `\\[gnus-browse-exit]' to return to the group buffer."
5025   (interactive)
5026   (kill-all-local-variables)
5027   (if gnus-visual (gnus-browse-make-menu-bar))
5028   (setq mode-line-modified "-- ")
5029   (make-local-variable 'mode-line-format)
5030   (setq mode-line-format (copy-sequence mode-line-format))
5031   (and (equal (nth 3 mode-line-format) "   ")
5032        (setcar (nthcdr 3 mode-line-format) ""))
5033   (setq major-mode 'gnus-browse-mode)
5034   (setq mode-name "Browse Server")
5035   (setq mode-line-process nil)
5036   (use-local-map gnus-browse-mode-map)
5037   (buffer-disable-undo (current-buffer))
5038   (setq truncate-lines t)
5039   (setq buffer-read-only t)
5040   (run-hooks 'gnus-browse-mode-hook))
5041
5042 (defun gnus-browse-read-group (&optional no-article)
5043   "Enter the group at the current line."
5044   (interactive)
5045   (let ((group (gnus-browse-group-name)))
5046     (or (gnus-group-read-ephemeral-group 
5047          group gnus-browse-current-method nil
5048          (cons (current-buffer) 'browse))
5049         (error "Couldn't enter %s" group))))
5050
5051 (defun gnus-browse-select-group ()
5052   "Select the current group."
5053   (interactive)
5054   (gnus-browse-read-group 'no))
5055
5056 (defun gnus-browse-next-group (n)
5057   "Go to the next group."
5058   (interactive "p")
5059   (prog1
5060       (forward-line n)
5061     (gnus-group-position-cursor)))
5062
5063 (defun gnus-browse-prev-group (n)
5064   "Go to the next group."
5065   (interactive "p")
5066   (gnus-browse-next-group (- n)))
5067
5068 (defun gnus-browse-unsubscribe-current-group (arg)
5069   "(Un)subscribe to the next ARG groups."
5070   (interactive "p")
5071   (and (eobp)
5072        (error "No group at current line."))
5073   (let ((ward (if (< arg 0) -1 1))
5074         (arg (abs arg)))
5075     (while (and (> arg 0)
5076                 (not (eobp))
5077                 (gnus-browse-unsubscribe-group)
5078                 (zerop (gnus-browse-next-group ward)))
5079       (setq arg (1- arg)))
5080     (gnus-group-position-cursor)
5081     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5082     arg))
5083
5084 (defun gnus-browse-group-name ()
5085   (save-excursion
5086     (beginning-of-line)
5087     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5088         ()
5089       (gnus-group-prefixed-name 
5090        (buffer-substring (match-beginning 1) (match-end 1))
5091        gnus-browse-current-method))))
5092   
5093 (defun gnus-browse-unsubscribe-group ()
5094   (let ((sub nil)
5095         (buffer-read-only nil)
5096         group)
5097     (save-excursion
5098       (beginning-of-line)
5099       (if (= (following-char) ?K) (setq sub t))
5100       (setq group (gnus-browse-group-name))
5101       (beginning-of-line)
5102       (delete-char 1)
5103       (if sub
5104           (progn
5105             (gnus-group-change-level 
5106              (list t group gnus-level-default-subscribed
5107                    nil nil gnus-browse-current-method) 
5108              gnus-level-default-subscribed gnus-level-killed
5109              (and (car (nth 1 gnus-newsrc-alist))
5110                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5111                                 gnus-newsrc-hashtb))
5112              t)
5113             (insert ? ))
5114         (gnus-group-change-level 
5115          group gnus-level-killed gnus-level-default-subscribed)
5116         (insert ?K)))
5117     t))
5118
5119 (defun gnus-browse-exit ()
5120   "Quit browsing and return to the group buffer."
5121   (interactive)
5122   (if (eq major-mode 'gnus-browse-mode)
5123       (kill-buffer (current-buffer)))
5124   (if gnus-browse-return-buffer
5125       (gnus-configure-windows 'server 'force)
5126     (gnus-configure-windows 'group 'force)
5127     (gnus-group-list-groups nil)))
5128
5129 (defun gnus-browse-describe-briefly ()
5130   "Give a one line description of the group mode commands."
5131   (interactive)
5132   (gnus-message 6
5133    (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")))
5134       
5135 \f
5136 ;;;
5137 ;;; Gnus summary mode
5138 ;;;
5139
5140 (defvar gnus-summary-mode-map nil)
5141 (defvar gnus-summary-mark-map nil)
5142 (defvar gnus-summary-mscore-map nil)
5143 (defvar gnus-summary-article-map nil)
5144 (defvar gnus-summary-thread-map nil)
5145 (defvar gnus-summary-goto-map nil)
5146 (defvar gnus-summary-exit-map nil)
5147 (defvar gnus-summary-interest-map nil)
5148 (defvar gnus-summary-sort-map nil)
5149 (defvar gnus-summary-backend-map nil)
5150 (defvar gnus-summary-save-map nil)
5151 (defvar gnus-summary-wash-map nil)
5152 (defvar gnus-summary-wash-hide-map nil)
5153 (defvar gnus-summary-wash-highlight-map nil)
5154 (defvar gnus-summary-wash-time-map nil)
5155 (defvar gnus-summary-help-map nil)
5156
5157 (put 'gnus-summary-mode 'mode-class 'special)
5158
5159 (if gnus-summary-mode-map
5160     nil
5161   (setq gnus-summary-mode-map (make-keymap))
5162   (suppress-keymap gnus-summary-mode-map)
5163
5164   ;; Non-orthogonal keys
5165
5166   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5167   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5168   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5169   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5170   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5171   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5172   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5173   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5174   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5175   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5176   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5177   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5178   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5179   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5180   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5181   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5182   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5183   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5184   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5185   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5186   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5187   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5188   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5189   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5190   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5191   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5192   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5193   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5194   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5195   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5196   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5197   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5198   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5199   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5200   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5201   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5202   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5203   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5204   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5205   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5206   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5207   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5208   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5209   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5210   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5211   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5212   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5213   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5214   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5215   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5216   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5217   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5218   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5219   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5220   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5221   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5222   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5223   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5224   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5225   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5226   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5227   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5228   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5229   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5230   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5231   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5232   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5233   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5234   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5235   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5236   (define-key gnus-summary-mode-map "V" 'gnus-version)
5237   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5238   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5239   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5240   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5241   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5242   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5243   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5244   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5245 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5246   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5247   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5248   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5249 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5250   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5251   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5252   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5253   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5254   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5255
5256
5257   ;; Sort of orthogonal keymap
5258   (define-prefix-command 'gnus-summary-mark-map)
5259   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5260   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5261   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5262   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5263   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5264   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5265   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5266   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5267   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5268   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5269   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5270   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5271   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5272   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5273   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5274   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5275   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5276   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5277   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5278   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5279   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5280   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5281   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5282   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5283
5284   (define-prefix-command 'gnus-summary-mscore-map)
5285   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5286   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5287   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5288   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5289   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5290
5291   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5292   
5293   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5294   
5295   (define-prefix-command 'gnus-summary-goto-map)
5296   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5297   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5298   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5299   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5300   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5301   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5302   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5303   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5304   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5305   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5306   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5307   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5308   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5309   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5310
5311
5312   (define-prefix-command 'gnus-summary-thread-map)
5313   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5314   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5315   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5316   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5317   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5318   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5319   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5320   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5321   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5322   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5323   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5324   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5325   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5326   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5327
5328   
5329   (define-prefix-command 'gnus-summary-exit-map)
5330   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5331   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5332   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5333   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5334   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5335   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5336   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5337   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5338   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5339   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5340   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5341
5342
5343   (define-prefix-command 'gnus-summary-article-map)
5344   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5345   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5346   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5347   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5348   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5349   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5350   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5351   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5352   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5353   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5354   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5355   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5356   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5357   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5358
5359
5360
5361   (define-prefix-command 'gnus-summary-wash-map)
5362   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5363
5364   (define-prefix-command 'gnus-summary-wash-hide-map)
5365   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5366   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5367   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5368   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5369   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5370   (define-key gnus-summary-wash-hide-map "\C-c" 'gnus-article-hide-citation-maybe)
5371
5372   (define-prefix-command 'gnus-summary-wash-highlight-map)
5373   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5374   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5375   (define-key gnus-summary-wash-highlight-map "h" 'gnus-article-highlight-headers)
5376   (define-key gnus-summary-wash-highlight-map "c" 'gnus-article-highlight-citation)
5377   (define-key gnus-summary-wash-highlight-map "s" 'gnus-article-highlight-signature)
5378
5379   (define-prefix-command 'gnus-summary-wash-time-map)
5380   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5381   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5382   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5383   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5384   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5385
5386   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5387   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5388   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5389   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5390   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5391   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5392   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5393   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5394   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5395   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5396
5397
5398   (define-prefix-command 'gnus-summary-help-map)
5399   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5400   (define-key gnus-summary-help-map "v" 'gnus-version)
5401   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5402   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5403   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5404   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5405
5406
5407   (define-prefix-command 'gnus-summary-backend-map)
5408   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5409   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5410   (define-key gnus-summary-backend-map "\M-\C-e" 
5411     'gnus-summary-expire-articles-now)
5412   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5413   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5414   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5415   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5416   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5417   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5418   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5419
5420
5421   (define-prefix-command 'gnus-summary-save-map)
5422   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5423   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5424   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5425   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5426   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5427   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5428   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5429   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5430 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5431
5432   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5433
5434   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5435 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5436 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5437 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5438 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5439 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5440 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5441   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5442 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5443 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5444
5445   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5446
5447 ;  (define-prefix-command 'gnus-summary-sort-map)
5448 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5449 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5450 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5451 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5452 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5453 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5454
5455   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5456   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5457   )
5458
5459
5460 \f
5461
5462 (defun gnus-summary-mode (&optional group)
5463   "Major mode for reading articles.
5464
5465 All normal editing commands are switched off.
5466 \\<gnus-summary-mode-map>
5467 Each line in this buffer represents one article.  To read an
5468 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5469 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5470 respectively.
5471
5472 You can also post articles and send mail from this buffer.  To 
5473 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5474 of an article, type `\\[gnus-summary-reply]'.
5475
5476 There are approx. one gazillion commands you can execute in this 
5477 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5478
5479 The following commands are available:
5480
5481 \\{gnus-summary-mode-map}"
5482   (interactive)
5483   (if gnus-visual (gnus-summary-make-menu-bar))
5484   (kill-all-local-variables)
5485   (let ((locals gnus-summary-local-variables))
5486     (while locals
5487       (if (consp (car locals))
5488           (progn
5489             (make-local-variable (car (car locals)))
5490             (set (car (car locals)) (eval (cdr (car locals)))))
5491         (make-local-variable (car locals))
5492         (set (car locals) nil))
5493       (setq locals (cdr locals))))
5494   (gnus-make-thread-indent-array)
5495   (setq mode-line-modified "-- ")
5496   (make-local-variable 'mode-line-format)
5497   (setq mode-line-format (copy-sequence mode-line-format))
5498   (and (equal (nth 3 mode-line-format) "   ")
5499        (setcar (nthcdr 3 mode-line-format) ""))
5500   (setq major-mode 'gnus-summary-mode)
5501   (setq mode-name "Summary")
5502   (make-local-variable 'minor-mode-alist)
5503   (use-local-map gnus-summary-mode-map)
5504   (buffer-disable-undo (current-buffer))
5505   (setq buffer-read-only t)             ;Disable modification
5506   (setq truncate-lines t)
5507   (setq selective-display t)
5508   (setq selective-display-ellipses t)   ;Display `...'
5509   (setq buffer-display-table gnus-summary-display-table)
5510   (setq gnus-newsgroup-name group)
5511   (run-hooks 'gnus-summary-mode-hook))
5512
5513 (defun gnus-summary-make-display-table ()
5514   ;; Change the display table.  Odd characters have a tendency to mess
5515   ;; up nicely formatted displays - we make all possible glyphs
5516   ;; display only a single character.
5517
5518   ;; We start from the standard display table, if any.
5519   (setq gnus-summary-display-table 
5520         (or (copy-sequence standard-display-table)
5521             (make-display-table)))
5522   ;; Nix out all the control chars...
5523   (let ((i 32))
5524     (while (>= (setq i (1- i)) 0)
5525       (aset gnus-summary-display-table i [??])))
5526   ;; ... but not newline and cr, of course. (cr is necessary for the
5527   ;; selective display).  
5528   (aset gnus-summary-display-table ?\n nil)
5529   (aset gnus-summary-display-table ?\r nil)
5530   ;; We nix out any glyphs over 126 that are not set already.  
5531   (let ((i 256))
5532     (while (>= (setq i (1- i)) 127)
5533       ;; Only modify if the entry is nil.
5534       (or (aref gnus-summary-display-table i) 
5535           (aset gnus-summary-display-table i [??])))))
5536
5537 (defun gnus-summary-clear-local-variables ()
5538   (let ((locals gnus-summary-local-variables))
5539     (while locals
5540       (if (consp (car locals))
5541           (and (vectorp (car (car locals)))
5542                (set (car (car locals)) nil))
5543         (and (vectorp (car locals))
5544              (set (car locals) nil)))
5545       (setq locals (cdr locals)))))
5546
5547 ;; Some summary mode macros.
5548
5549 ;; Return a header specified by a NUMBER.
5550 (defun gnus-get-header-by-number (number)
5551   (save-excursion
5552     (set-buffer gnus-summary-buffer)
5553     (or gnus-newsgroup-headers-hashtb-by-number
5554         (gnus-make-headers-hashtable-by-number))
5555     (gnus-gethash (int-to-string number)
5556                   gnus-newsgroup-headers-hashtb-by-number)))
5557
5558 ;; Fast version of the function above.
5559 (defmacro gnus-get-header-by-num (number)
5560   (` (gnus-gethash (int-to-string (, number)) 
5561                    gnus-newsgroup-headers-hashtb-by-number)))
5562
5563 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5564   "Search for article forward.
5565 If UNREAD is non-nil, only unread articles are selected.
5566 If SUBJECT is non-nil, the article which has the same subject will be
5567 searched for. 
5568 If BACKWARD is non-nil, the search will be performed backwards instead."
5569   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5570
5571 (defmacro gnus-summary-search-backward (&optional unread subject)
5572   "Search for article backward.
5573 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5574 If 2nd optional argument SUBJECT is non-nil, the article which has
5575 the same subject will be searched for."
5576   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5577
5578 (defmacro gnus-summary-article-number (&optional number-or-nil)
5579   "The article number of the article on the current line.
5580 If there isn's an article number here, then we return the current
5581 article number."
5582   (if number-or-nil
5583       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5584     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5585          gnus-current-article)))
5586
5587 (defmacro gnus-summary-thread-level ()
5588   "The thread level of the article on the current line."
5589   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5590        0))
5591
5592 (defmacro gnus-summary-article-mark ()
5593   "The mark on the current line."
5594   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5595
5596 (defun gnus-summary-subject-string ()
5597   "Return current subject string or nil if nothing."
5598   (let ((article (gnus-summary-article-number))
5599         header)
5600     (and article 
5601          (setq header (gnus-get-header-by-num article))
5602          (vectorp header)
5603          (header-subject header))))
5604
5605 ;; Various summary mode internalish functions.
5606
5607 (defun gnus-mouse-pick-article (e)
5608   (interactive "e")
5609   (mouse-set-point e)
5610   (gnus-summary-next-page nil t))
5611
5612 (defun gnus-summary-setup-buffer (group)
5613   "Initialize summary buffer."
5614   (let ((buffer (concat "*Summary " group "*")))
5615     (if (get-buffer buffer)
5616         (progn
5617           (set-buffer buffer)
5618           (not gnus-newsgroup-begin))
5619       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5620       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5621       (gnus-add-current-to-buffer-list)
5622       (gnus-summary-mode group)
5623       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5624       (setq gnus-newsgroup-name group)
5625       t)))
5626
5627 (defun gnus-set-global-variables ()
5628   ;; Set the global equivalents of the summary buffer-local variables
5629   ;; to the latest values they had. These reflect the summary buffer
5630   ;; that was in action when the last article was fetched.
5631   (if (eq major-mode 'gnus-summary-mode) 
5632       (progn
5633         (setq gnus-summary-buffer (current-buffer))
5634         (let ((name gnus-newsgroup-name)
5635               (marked gnus-newsgroup-marked)
5636               (unread gnus-newsgroup-unreads)
5637               (headers gnus-current-headers)
5638               (score-file gnus-current-score-file))
5639           (save-excursion
5640             (set-buffer gnus-group-buffer)
5641             (setq gnus-newsgroup-name name)
5642             (setq gnus-newsgroup-marked marked)
5643             (setq gnus-newsgroup-unreads unread)
5644             (setq gnus-current-headers headers)
5645             (setq gnus-current-score-file score-file))))))
5646
5647 (defun gnus-summary-insert-dummy-line (sformat subject number)
5648   (if (not sformat) 
5649       (setq sformat gnus-summary-dummy-line-format-spec))
5650   (let (b)
5651     (beginning-of-line)
5652     (setq b (point))
5653     (insert (eval sformat))
5654     (add-text-properties
5655      b (1+ b)
5656      (list 'gnus-number number 
5657            'gnus-mark gnus-dummy-mark
5658            'gnus-level 0))))
5659
5660 (defvar gnus-thread-indent-array nil)
5661 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5662 (defun gnus-make-thread-indent-array ()
5663   (let ((n 200))
5664     (if (and gnus-thread-indent-array
5665              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5666         nil
5667       (setq gnus-thread-indent-array (make-vector 201 "")
5668             gnus-thread-indent-array-level gnus-thread-indent-level)
5669       (while (>= n 0)
5670         (aset gnus-thread-indent-array n
5671               (make-string (* n gnus-thread-indent-level) ? ))
5672         (setq n (1- n))))))
5673
5674 (defun gnus-summary-insert-line 
5675   (sformat header level current unread replied expirable subject-or-nil
5676            &optional dummy score)
5677   (or sformat (setq sformat gnus-summary-line-format-spec))
5678   (let* ((indentation (aref gnus-thread-indent-array level))
5679          (lines (header-lines header))
5680          (score (or score gnus-summary-default-score 0))
5681          (score-char
5682           (if (or (null gnus-summary-default-score)
5683                   (<= (abs (- score gnus-summary-default-score))
5684                       gnus-summary-zcore-fuzz)) ? 
5685             (if (< score gnus-summary-default-score)
5686                 gnus-score-below-mark gnus-score-over-mark)))
5687          (replied (if replied gnus-replied-mark ? ))
5688          (from (header-from header))
5689          (name (cond 
5690                 ((string-match "(.+)" from)
5691                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5692                 ((string-match "<[^>]+> *$" from)
5693                  (let ((beg (match-beginning 0)))
5694                    (or (and (string-match "^\"[^\"]*\"" from)
5695                             (substring from (1+ (match-beginning 0))
5696                                        (1- (match-end 0))))
5697                        (substring from 0 beg))))
5698                 (t from)))
5699          (subject (header-subject header))
5700          (number (header-number header))
5701          (opening-bracket (if dummy ?\< ?\[))
5702          (closing-bracket (if dummy ?\> ?\]))
5703          (buffer-read-only nil)
5704          (b (progn (beginning-of-line) (point))))
5705     (or (numberp lines) (setq lines 0))
5706     (insert (eval sformat))
5707     (add-text-properties
5708      b (1+ b) (list 'gnus-number number 
5709                     'gnus-mark (or unread gnus-unread-mark)
5710                     'gnus-level level))))
5711
5712 (defun gnus-summary-update-line (&optional dont-update)
5713   ;; Update summary line after change.
5714   (or (not gnus-summary-default-score)
5715       gnus-summary-inhibit-highlight
5716       (let ((gnus-summary-inhibit-highlight t)
5717             (article (gnus-summary-article-number)))
5718         (progn
5719           (or dont-update
5720               (if (and gnus-summary-mark-below
5721                        (< (gnus-summary-article-score)
5722                           gnus-summary-mark-below))
5723                   (and (not (memq article gnus-newsgroup-marked))
5724                        (not (memq article gnus-newsgroup-dormant))
5725                        (memq article gnus-newsgroup-unreads)
5726                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5727                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5728                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5729           (and gnus-visual
5730                (run-hooks 'gnus-summary-update-hook))))))
5731
5732 (defun gnus-summary-update-lines (&optional beg end)
5733   ;; Mark article as read (or not) by taking into account scores.
5734   (let ((beg (or beg (point-min)))
5735         (end (or end (point-max))))
5736     (if (or (not gnus-summary-default-score)
5737             gnus-summary-inhibit-highlight)
5738         ()
5739       (let ((gnus-summary-inhibit-highlight t)
5740             article)
5741         (save-excursion
5742           (set-buffer gnus-summary-buffer)
5743           (goto-char beg)
5744           (beginning-of-line)
5745           (while (and (not (eobp)) (< (point) end))
5746             (if (and gnus-summary-mark-below
5747                      (< (or (cdr (assq 
5748                                   (setq article (get-text-property 
5749                                                  (point) 'gnus-number))
5750                                   gnus-newsgroup-scored))
5751                             gnus-summary-default-score 0)
5752                         gnus-summary-mark-below))
5753                 ;; We want to possibly mark it as read...
5754                 (and (not (memq article gnus-newsgroup-marked))
5755                      (not (memq article gnus-newsgroup-dormant))
5756                      (memq article gnus-newsgroup-unreads)
5757                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5758               ;; We want to possibly mark it as unread.
5759               (and (eq (get-text-property (point) 'gnus-mark)
5760                        gnus-low-score-mark)
5761                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5762             ;; Do the visual highlights at the same time.
5763             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5764             (forward-line 1)))))))
5765
5766 (defvar gnus-tmp-gathered nil)
5767
5768 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5769   ;; Sum up all elements (and sub-elements) in a list.
5770   (let* ((number
5771           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5772           (if (and (consp thread) (cdr thread))
5773               (apply
5774                '+ 1 (mapcar
5775                      (function
5776                       (lambda (arg)
5777                         (if (memq (header-number (car arg)) gnus-tmp-gathered)
5778                             0
5779                           (gnus-summary-number-of-articles-in-thread arg))))
5780                      (cdr thread)))
5781             1)))
5782     (if char 
5783         (if (> number 1) gnus-not-empty-thread-mark
5784           gnus-empty-thread-mark)
5785       number)))
5786
5787 (defun gnus-summary-read-group 
5788   (group &optional show-all no-article kill-buffer)
5789   "Start reading news in newsgroup GROUP.
5790 If SHOW-ALL is non-nil, already read articles are also listed.
5791 If NO-ARTICLE is non-nil, no article is selected initially."
5792   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5793   (let* ((new-group (gnus-summary-setup-buffer group))
5794          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5795                                                   group))))
5796          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5797     (cond 
5798      ((not new-group)
5799       (gnus-set-global-variables)
5800       (gnus-kill-buffer kill-buffer)
5801       (gnus-configure-windows 'summary)
5802       (gnus-set-mode-line 'summary)
5803       (gnus-summary-position-cursor)
5804       (message "")
5805       t)
5806      ((null did-select) 
5807       (and (eq major-mode 'gnus-summary-mode)
5808            (not (equal (current-buffer) kill-buffer))
5809            (progn
5810              (kill-buffer (current-buffer))
5811              (if (not quit-config)
5812                  (progn
5813                    (set-buffer gnus-group-buffer)
5814                    (gnus-group-jump-to-group group)
5815                    (gnus-group-next-unread-group 1))
5816                (if (not (buffer-name (car quit-config)))
5817                    (gnus-configure-windows 'group 'force)
5818                  (set-buffer (car quit-config))
5819                  (and (eq major-mode 'gnus-summary-mode)
5820                       (gnus-set-global-variables))
5821                  (gnus-configure-windows (cdr quit-config))))))
5822       (message "Can't select group")
5823       nil)
5824      ((eq did-select 'quit)
5825       (and (eq major-mode 'gnus-summary-mode)
5826            (not (equal (current-buffer) kill-buffer))
5827            (kill-buffer (current-buffer)))
5828       (gnus-kill-buffer kill-buffer)
5829       (if (not quit-config)
5830           (progn
5831             (set-buffer gnus-group-buffer)
5832             (gnus-group-jump-to-group group)
5833             (gnus-group-next-unread-group 1)
5834             (gnus-configure-windows 'group 'force))
5835         (if (not (buffer-name (car quit-config)))
5836             (gnus-configure-windows 'group 'force)
5837           (set-buffer (car quit-config))
5838           (and (eq major-mode 'gnus-summary-mode)
5839                (gnus-set-global-variables))
5840           (gnus-configure-windows (cdr quit-config))))
5841       (signal 'quit nil))
5842      (t
5843       (gnus-set-global-variables)
5844       ;; Save the active value in effect when the group was entered.
5845       (setq gnus-newsgroup-active 
5846             (gnus-copy-sequence
5847              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5848       ;; You can change the subjects in this hook.
5849       (run-hooks 'gnus-select-group-hook)
5850       ;; Do score processing.
5851       (and gnus-use-scoring (gnus-possibly-score-headers))
5852       (gnus-update-format-specifications)
5853       ;; Generate the summary buffer.
5854       (gnus-summary-prepare)
5855       ;; Create the header hashtb.
5856       (gnus-make-headers-hashtable-by-number)
5857       (if (zerop (buffer-size))
5858           (cond (gnus-newsgroup-dormant
5859                  (gnus-summary-show-all-dormant))
5860                 ((and gnus-newsgroup-scored show-all)
5861                  (gnus-summary-show-all-expunged))))
5862       ;; Function `gnus-apply-kill-file' must be called in this hook.
5863       (run-hooks 'gnus-apply-kill-hook)
5864       (if (zerop (buffer-size))
5865           (progn
5866             ;; This newsgroup is empty.
5867             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5868             (gnus-message 6 "No unread news")
5869             (gnus-kill-buffer kill-buffer)
5870             nil)
5871         ;;(save-excursion
5872         ;;  (if kill-buffer
5873         ;;      (let ((gnus-summary-buffer kill-buffer))
5874         ;;      (gnus-configure-windows 'group))))
5875         ;; Hide conversation thread subtrees.  We cannot do this in
5876         ;; gnus-summary-prepare-hook since kill processing may not
5877         ;; work with hidden articles.
5878         (and gnus-show-threads
5879              gnus-thread-hide-subtree
5880              (gnus-summary-hide-all-threads))
5881         ;; Show first unread article if requested.
5882         (goto-char (point-min))
5883         (if (and (not no-article)
5884                  gnus-auto-select-first
5885                  (gnus-summary-first-unread-article))
5886             ()
5887           (gnus-configure-windows 'summary))
5888         (gnus-set-mode-line 'summary)
5889         (gnus-summary-position-cursor)
5890         ;; If in async mode, we send some info to the backend.
5891         (and gnus-newsgroup-async
5892              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5893              (gnus-request-asynchronous 
5894               gnus-newsgroup-name
5895               (if (and gnus-asynchronous-article-function
5896                        (fboundp gnus-asynchronous-article-function))
5897                   (funcall gnus-asynchronous-article-function
5898                            gnus-newsgroup-threads)
5899                 gnus-newsgroup-threads)))
5900         (gnus-kill-buffer kill-buffer)
5901         (if (not (get-buffer-window gnus-group-buffer))
5902             ()
5903           ;; gotta use windows, because recenter does wierd stuff if
5904           ;; the current buffer ain't the displayed window.
5905           (let ((owin (selected-window))) 
5906             (select-window (get-buffer-window gnus-group-buffer))
5907             (and (gnus-group-goto-group group)
5908                  (recenter))
5909             (select-window owin))))
5910       t))))
5911
5912 (defun gnus-summary-prepare ()
5913   ;; Generate the summary buffer.
5914   (let ((buffer-read-only nil))
5915     (erase-buffer)
5916     (gnus-summary-prepare-threads 
5917      (if gnus-show-threads
5918          (gnus-gather-threads 
5919           (gnus-sort-threads 
5920            (if (and gnus-summary-expunge-below
5921                     (not gnus-fetch-old-headers))
5922                (gnus-make-threads-and-expunge)
5923              (gnus-make-threads))))
5924        gnus-newsgroup-headers)
5925      'cull)
5926     (gnus-summary-update-lines)
5927     ;; Remove the final newline.
5928     ;;(goto-char (point-max))
5929     ;;(delete-char -1)
5930     ;; Call hooks for modifying summary buffer.
5931     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5932     (goto-char (point-min))
5933     (run-hooks 'gnus-summary-prepare-hook)))
5934
5935 (defun gnus-gather-threads (threads)
5936   "Gather threads that have lost their roots."
5937   (if (not gnus-summary-make-false-root)
5938       threads 
5939     (let ((hashtb (gnus-make-hashtable 1023))
5940           (prev threads)
5941           (result threads)
5942           subject hthread whole-subject)
5943       (while threads
5944         (setq whole-subject 
5945               (setq subject (header-subject (car (car threads)))))
5946         (if gnus-summary-gather-subject-limit
5947             (or (and (numberp gnus-summary-gather-subject-limit)
5948                      (> (length subject) gnus-summary-gather-subject-limit)
5949                      (setq subject
5950                            (substring subject 0 
5951                                       gnus-summary-gather-subject-limit)))
5952                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5953                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5954           (setq subject (gnus-simplify-subject-re subject)))
5955         (if (setq hthread 
5956                   (gnus-gethash subject hashtb))
5957             (progn
5958               (or (stringp (car (car hthread)))
5959                   (setcar hthread (list whole-subject (car hthread))))
5960               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5961                                            (list (car threads))))
5962               (setcdr prev (cdr threads))
5963               (setq threads prev))
5964           (gnus-sethash subject threads hashtb))
5965         (setq prev threads)
5966         (setq threads (cdr threads)))
5967       result)))
5968
5969 (defun gnus-make-threads ()
5970   ;; This function takes the dependencies already made by 
5971   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5972   ;; through the dependecies in the hash table and finds all the
5973   ;; roots. Roots do not refer back to any valid articles.
5974   (gnus-message 6 "Threading...")
5975   (let (roots new-roots)
5976     (and gnus-fetch-old-headers
5977          (eq gnus-headers-retrieved-by 'nov)
5978          (gnus-build-old-threads))
5979     (mapatoms
5980      (lambda (refs)
5981        (if (not (car (symbol-value refs)))
5982            (setq roots (append (cdr (symbol-value refs)) roots))
5983          ;; Ok, these refer back to valid articles, but if
5984          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5985          ;; the root has the same subject as its children. The children
5986          ;; that do not are made into roots and removed from the list
5987          ;; of children. 
5988          (or gnus-thread-ignore-subject
5989              (let* ((prev (symbol-value refs))
5990                     (subject (gnus-simplify-subject-re 
5991                               (header-subject (car prev))))
5992                     (headers (cdr prev)))
5993                (while headers
5994                  (if (not (string= subject
5995                                    (gnus-simplify-subject-re 
5996                                     (header-subject (car headers)))))
5997                      (progn
5998                        (setq new-roots (cons (car headers) new-roots))
5999                        (setcdr prev (cdr headers)))
6000                    (setq prev headers))
6001                  (setq headers (cdr headers)))))))
6002      gnus-newsgroup-dependencies)
6003
6004     ;; We enter the new roots into the dependencies structure to
6005     ;; ensure that any possible later thread-regeneration will be
6006     ;; possible. 
6007     (let ((r new-roots))
6008       (while r
6009         (gnus-sethash (concat (header-id (car r)) ".boo")
6010                       (list nil (car r)) gnus-newsgroup-dependencies)
6011         (setq r (cdr r))))
6012
6013     (setq roots (nconc new-roots roots))
6014
6015     (prog1
6016         (mapcar 'gnus-trim-thread
6017                 (apply 'append
6018                        (mapcar 'gnus-cut-thread
6019                                (mapcar 'gnus-make-sub-thread roots))))
6020       (gnus-message 6 "Threading...done"))))
6021
6022   
6023 (defun gnus-make-threads-and-expunge ()
6024   ;; This function takes the dependencies already made by 
6025   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6026   ;; through the dependecies in the hash table and finds all the
6027   ;; roots. Roots do not refer back to any valid articles.
6028   (gnus-message 6 "Threading...")
6029   (let ((default (or gnus-summary-default-score 0))
6030         (below gnus-summary-expunge-below)
6031         roots article new-roots)
6032     (and gnus-fetch-old-headers
6033          (eq gnus-headers-retrieved-by 'nov)
6034          (gnus-build-old-threads))
6035     (mapatoms
6036      (lambda (refs)
6037        (if (not (car (symbol-value refs)))
6038            ;; These articles do not refer back to any other articles -
6039            ;; they are roots.
6040            (let ((headers (cdr (symbol-value refs))))
6041              ;; We weed out the low-scored articles.
6042              (while headers
6043                (if (not (< (or (cdr (assq (header-number (car headers))
6044                                           gnus-newsgroup-scored)) default)
6045                            below))
6046                    ;; It is over.
6047                    (setq roots (cons (car headers) roots))
6048                  ;; It is below, so we mark it as read.
6049                  (setq gnus-newsgroup-unreads
6050                        (delq (header-number (car headers))
6051                              gnus-newsgroup-unreads))
6052                  (setq gnus-newsgroup-reads 
6053                        (cons (cons (header-number (car headers))
6054                                    gnus-low-score-mark) 
6055                              gnus-newsgroup-reads)))
6056                (setq headers (cdr headers))))
6057          ;; Ok, these refer back to valid articles, but if
6058          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6059          ;; the root has the same subject as its children. The children
6060          ;; that do not are made into roots and removed from the list
6061          ;; of children. 
6062          (or gnus-thread-ignore-subject
6063              (let* ((prev (symbol-value refs))
6064                     (subject (gnus-simplify-subject-re 
6065                               (header-subject (car prev))))
6066                     (headers (cdr prev)))
6067                (while headers
6068                  (if (not (string= subject
6069                                    (gnus-simplify-subject-re 
6070                                     (header-subject (car headers)))))
6071                      (progn
6072                        (if (not (< (or (cdr (assq (header-number (car headers))
6073                                                   gnus-newsgroup-scored))
6074                                        default) below))
6075                            (setq new-roots (cons (car headers) new-roots))
6076                          (setq gnus-newsgroup-unreads
6077                                (delq (header-number (car headers))
6078                                      gnus-newsgroup-unreads))
6079                          (setq gnus-newsgroup-reads
6080                                (cons (cons (header-number (car headers)) 
6081                                            gnus-low-score-mark) 
6082                                      gnus-newsgroup-reads)))
6083                        (setcdr prev (cdr headers)))
6084                    (setq prev headers))
6085                  (setq headers (cdr headers)))))
6086          ;; If this article is expunged, some of the children might be
6087          ;; roots.  
6088          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6089                                gnus-newsgroup-scored)) default)
6090                 below)
6091              (let* ((prev (symbol-value refs))
6092                     (headers (cdr prev)))
6093                (while headers
6094                  (setq article (header-number (car headers)))
6095                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6096                                  default) below))
6097                      (progn (setq new-roots (cons (car headers) new-roots))
6098                             (setq prev headers))
6099                    (setq gnus-newsgroup-unreads 
6100                          (delq article gnus-newsgroup-unreads))
6101                    (setq gnus-newsgroup-reads 
6102                          (cons (cons article gnus-low-score-mark) 
6103                                gnus-newsgroup-reads))
6104                    (setcdr prev (cdr headers)))
6105                  (setq headers (cdr headers))))
6106            ;; It was not expunged, but we look at expunged children.
6107            (let* ((prev (symbol-value refs))
6108                   (headers (cdr prev))
6109                   article)
6110              (while headers
6111                (setq article (header-number (car headers)))
6112                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6113                                default) below))
6114                    (setq prev headers)
6115                  (setq gnus-newsgroup-unreads 
6116                        (delq article gnus-newsgroup-unreads))
6117                  (setq gnus-newsgroup-reads 
6118                        (cons (cons article gnus-low-score-mark)
6119                              gnus-newsgroup-reads))
6120                  (setcdr prev (cdr headers)))
6121                (setq headers (cdr headers)))))))
6122      gnus-newsgroup-dependencies)
6123
6124     ;; We enter the new roots into the dependencies structure to
6125     ;; ensure that any possible later thread-regeneration will be
6126     ;; possible. 
6127     (let ((r new-roots))
6128       (while r
6129         (gnus-sethash (concat (header-id (car r)) ".boo")
6130                       (list nil (car r)) gnus-newsgroup-dependencies)
6131         (setq r (cdr r))))
6132
6133     (setq roots (nconc new-roots roots))
6134
6135     (prog1
6136         (mapcar 'gnus-trim-thread
6137                 (apply 'append
6138                        (mapcar 'gnus-cut-thread
6139                                (mapcar 'gnus-make-sub-thread roots))))
6140       (gnus-message 6 "Threading...done"))))
6141
6142   
6143 (defun gnus-cut-thread (thread)
6144   ;; Remove leaf dormant or ancient articles from THREAD.
6145   (let ((head (car thread))
6146         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6147     (if (and (null tail)
6148              (let ((number (header-number head)))
6149                (or (memq number gnus-newsgroup-ancient)
6150                    (memq number gnus-newsgroup-dormant)
6151                    (and gnus-summary-expunge-below
6152                         (eq gnus-fetch-old-headers 'some)
6153                         (< (or (cdr (assq number gnus-newsgroup-scored))
6154                                gnus-summary-default-score 0)
6155                            gnus-summary-expunge-below)
6156                         (progn
6157                           (setq gnus-newsgroup-unreads
6158                                 (delq number gnus-newsgroup-unreads))
6159                           (setq gnus-newsgroup-reads
6160                                 (cons (cons number gnus-low-score-mark)
6161                                       gnus-newsgroup-reads))
6162                           t)))))
6163         nil
6164       (list (cons head tail)))))
6165
6166 (defun gnus-trim-thread (thread)
6167   ;; Remove root ancient articles with only one child from THREAD.
6168   (if (and (eq gnus-fetch-old-headers 'some)
6169            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6170            (= (length thread) 2))
6171       (gnus-trim-thread (nth 1 thread))
6172     thread))
6173
6174 (defun gnus-make-sub-thread (root)
6175   ;; This function makes a sub-tree for a node in the tree.
6176   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6177                                               gnus-newsgroup-dependencies)))))
6178     (cons root (mapcar 'gnus-make-sub-thread children))))
6179
6180 (defun gnus-build-old-threads ()
6181   ;; Look at all the articles that refer back to old articles, and
6182   ;; fetch the headers for the articles that aren't there. This will
6183   ;; build complete threads - if the roots haven't been expired by the
6184   ;; server, that is.
6185   (let (id heads)
6186     (mapatoms
6187      (lambda (refs)
6188        (if (not (car (symbol-value refs)))
6189            (progn
6190              (setq heads (cdr (symbol-value refs)))
6191              (while heads
6192                (if (not (memq (header-number (car heads))
6193                               gnus-newsgroup-dormant))
6194                    (progn
6195                      (setq id (symbol-name refs))
6196                      (while (and (setq id (gnus-build-get-header id))
6197                                  (not (car (gnus-gethash 
6198                                             id gnus-newsgroup-dependencies)))))
6199                      (setq heads nil))
6200                  (setq heads (cdr heads)))))))
6201      gnus-newsgroup-dependencies)))
6202
6203 (defun gnus-build-get-header (id)
6204   ;; Look through the buffer of NOV lines and find the header to
6205   ;; ID. Enter this line into the dependencies hash table, and return
6206   ;; the id of the parent article (if any).
6207   (let ((deps gnus-newsgroup-dependencies)
6208         found header)
6209     (prog1
6210         (save-excursion
6211           (set-buffer nntp-server-buffer)
6212           (goto-char (point-min))
6213           (while (and (not found) (search-forward id nil t))
6214             (beginning-of-line)
6215             (setq found (looking-at 
6216                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6217                                  (regexp-quote id))))
6218             (or found (beginning-of-line 2)))
6219           (if found
6220               (let (ref)
6221                 (beginning-of-line)
6222                 (and
6223                  (setq header (gnus-nov-parse-line 
6224                                (read (current-buffer)) deps))
6225                  (setq ref (header-references header))
6226                  (string-match "\\(<[^>]+>\\) *$" ref)
6227                  (substring ref (match-beginning 1) (match-end 1))))))
6228       (and header
6229            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6230                  gnus-newsgroup-ancient (cons (header-number header)
6231                                               gnus-newsgroup-ancient))))))
6232
6233 ;; Re-build the thread containing ID.
6234 (defun gnus-rebuild-thread (id)
6235   (let ((dep gnus-newsgroup-dependencies)
6236         (buffer-read-only nil)
6237         parent headers refs thread art)
6238     (while (and id (setq headers
6239                          (car (setq art (gnus-gethash (downcase id) dep)))))
6240       (setq parent art)
6241       (setq id (and (setq refs (header-references headers))
6242                     (string-match "\\(<[^>]+>\\) *$" refs)
6243                     (substring refs (match-beginning 1) (match-end 1)))))
6244     (setq thread (gnus-make-sub-thread (car parent)))
6245     (gnus-rebuild-remove-articles thread)
6246     (let ((beg (point)))
6247       (gnus-summary-prepare-threads (list thread))
6248       (gnus-summary-update-lines beg (point)))))
6249
6250 ;; Delete all lines in the summary buffer that correspond to articles
6251 ;; in this thread.
6252 (defun gnus-rebuild-remove-articles (thread)
6253   (and (gnus-summary-goto-subject (header-number (car thread)))
6254        (gnus-delete-line))
6255   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6256
6257 (defun gnus-sort-threads (threads)
6258   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6259   (let ((fun gnus-thread-sort-functions))
6260     (while fun
6261       (setq threads (sort threads (car fun))
6262             fun (cdr fun))))
6263   threads)
6264
6265 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6266 (defmacro gnus-thread-header (thread)
6267   ;; Return header of first article in THREAD.
6268   ;; Note that THREAD must never, evr be anything else than a variable -
6269   ;; using some other form will lead to serious barfage.
6270   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6271   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6272   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6273         (vector thread) 2))
6274
6275 (defun gnus-thread-sort-by-number (h1 h2)
6276   "Sort threads by root article number."
6277   (< (header-number (gnus-thread-header h1))
6278      (header-number (gnus-thread-header h2))))
6279
6280 (defun gnus-thread-sort-by-author (h1 h2)
6281   "Sort threads by root author."
6282   (string-lessp
6283    (let ((extract (funcall 
6284                    gnus-extract-address-components
6285                    (header-from (gnus-thread-header h1)))))
6286      (or (car extract) (cdr extract)))
6287    (let ((extract (funcall
6288                    gnus-extract-address-components 
6289                    (header-from (gnus-thread-header h2)))))
6290      (or (car extract) (cdr extract)))))
6291
6292 (defun gnus-thread-sort-by-subject (h1 h2)
6293   "Sort threads by root subject."
6294   (string-lessp
6295    (downcase (gnus-simplify-subject 
6296               (header-subject (gnus-thread-header h1))))
6297    (downcase (gnus-simplify-subject 
6298               (header-subject (gnus-thread-header h2))))))
6299
6300 (defun gnus-thread-sort-by-date (h1 h2)
6301   "Sort threads by root article date."
6302   (string-lessp
6303    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6304    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6305
6306 (defun gnus-thread-sort-by-score (h1 h2)
6307   "Sort threads by root article score.
6308 Unscored articles will be counted as having a score of zero."
6309   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6310                     gnus-newsgroup-scored))
6311          gnus-summary-default-score 0)
6312      (or (cdr (assq (header-number (gnus-thread-header h2))
6313                     gnus-newsgroup-scored))
6314          gnus-summary-default-score 0)))
6315
6316 (defun gnus-thread-sort-by-total-score (h1 h2)
6317   "Sort threads by the sum of all scores in the thread.
6318 Unscored articles will be counted as having a score of zero."
6319   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6320
6321 (defun gnus-thread-total-score (thread)
6322   ;;  This function find the total score of THREAD.
6323   (if (consp thread)
6324       (if (stringp (car thread))
6325           (apply gnus-thread-score-function 0
6326                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6327         (gnus-thread-total-score-1 thread))
6328     (gnus-thread-total-score-1 (list thread))))
6329
6330 (defun gnus-thread-total-score-1 (root)
6331   ;; This function find the total score of the thread below ROOT.
6332   (setq root (car root))
6333   (apply gnus-thread-score-function
6334          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6335              gnus-summary-default-score 0)
6336          (mapcar 'gnus-thread-total-score
6337                  (cdr (gnus-gethash (downcase (header-id root))
6338                                     gnus-newsgroup-dependencies)))))
6339
6340 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6341 (defvar gnus-tmp-prev-subject "")
6342
6343 (defun gnus-summary-prepare-threads (threads &optional cull)
6344   "Prepare summary buffer from THREADS and indentation LEVEL.  
6345 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6346 or a straight list of headers."
6347   (message "Generating summary...")
6348   (let ((level 0)
6349         thread header number subject stack state gnus-tmp-gathered)
6350     (if (vectorp (car threads))
6351         ;; If this is a straight (sic) list of headers, then a
6352         ;; threaded summary display isn't required, so we just create
6353         ;; an unthreaded one.
6354         (gnus-summary-prepare-unthreaded threads cull)
6355
6356       ;; Do the threaded display.
6357
6358       (while (or threads stack)
6359         
6360         (if threads
6361             ;; If there are some threads, we do them before the
6362             ;; threads on the stack.
6363             (setq thread threads
6364                   header (car (car thread)))
6365           ;; There were no current threads, so we pop something off
6366           ;; the stack. 
6367           (setq state (car stack)
6368                 level (car state)
6369                 thread (cdr state)
6370                 stack (cdr stack)
6371                 header (car (car thread))))
6372
6373         (if (stringp header)
6374             (progn
6375               ;; The header is a dummy root.
6376               (cond 
6377                ((eq gnus-summary-make-false-root 'adopt)
6378                 ;; We let the first article adopt the rest.
6379                 (let ((th (car (cdr (car thread)))))
6380                   (while (cdr th)
6381                     (setq th (cdr th)))
6382                   (setcdr th (cdr (cdr (car thread))))
6383                   (setq gnus-tmp-gathered 
6384                         (nconc (mapcar
6385                                 (lambda (h) (header-number (car h)))
6386                                 (cdr (cdr (car thread))))
6387                                gnus-tmp-gathered))
6388                   (setcdr (cdr (car thread)) nil))
6389                 (setq level -1))
6390                ((eq gnus-summary-make-false-root 'empty)
6391                 ;; We print adopted articles with empty subject fields.
6392                 (setq gnus-tmp-gathered 
6393                       (nconc (mapcar
6394                               (lambda (h) (header-number (car h)))
6395                               (cdr (cdr (car thread))))
6396                              gnus-tmp-gathered))
6397                 (setq level -1))
6398                ((eq gnus-summary-make-false-root 'dummy)
6399                 ;; We output a dummy root.
6400                 (gnus-summary-insert-dummy-line 
6401                  nil header (header-number (car (car (cdr (car thread)))))))
6402                (t
6403                 ;; We do not make a root for the gathered
6404                 ;; sub-threads at all.  
6405                 (setq level -1))))
6406       
6407           (setq number (header-number header)
6408                 subject (header-subject header))
6409
6410           ;; Do the async thing.
6411           (and gnus-newsgroup-async
6412                (setq gnus-newsgroup-threads
6413                      (cons (cons number (header-lines header)) 
6414                            gnus-newsgroup-threads)))
6415
6416           ;; We may have to root out some bad articles...
6417           (and cull
6418                (= level 0)
6419                (cond ((and (memq (setq number (header-number header))
6420                                  gnus-newsgroup-dormant)
6421                            (null thread))
6422                       (setq header nil))
6423                      ((and gnus-summary-expunge-below
6424                            (< (or (cdr (assq number gnus-newsgroup-scored))
6425                                   gnus-summary-default-score 0)
6426                               gnus-summary-expunge-below))
6427                       (setq header nil)
6428                       (setq gnus-newsgroup-unreads 
6429                             (delq number gnus-newsgroup-unreads))
6430                       (setq gnus-newsgroup-reads
6431                             (cons (cons number gnus-low-score-mark)
6432                                   gnus-newsgroup-reads)))))
6433           
6434           (and
6435            header
6436            (progn
6437              (inline
6438                (gnus-summary-insert-line
6439                 nil header level nil 
6440                 (cond 
6441                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6442                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6443                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6444                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6445                  (t (or (cdr (assq number gnus-newsgroup-reads))
6446                         gnus-ancient-mark)))
6447                 (memq number gnus-newsgroup-replied)
6448                 (memq number gnus-newsgroup-expirable)
6449                 (if (and (eq gnus-summary-make-false-root 'empty)
6450                          (memq number gnus-tmp-gathered))
6451                     gnus-summary-same-subject
6452                   (if (or (zerop level)
6453                           (and gnus-thread-ignore-subject
6454                                (not (string= 
6455                                      (gnus-simplify-subject-re
6456                                       gnus-tmp-prev-subject)
6457                                      (gnus-simplify-subject-re
6458                                       subject)))))
6459                       subject
6460                     gnus-summary-same-subject))
6461                 (and (eq gnus-summary-make-false-root 'adopt)
6462                      (memq number gnus-tmp-gathered))
6463                 (cdr (assq number gnus-newsgroup-scored)))
6464
6465                (setq gnus-tmp-prev-subject subject)))))
6466
6467         (if (nth 1 thread) 
6468             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6469         (setq level (1+ level))
6470         (setq threads (cdr (car thread))))))
6471   (message "Generating summary...done"))
6472
6473
6474
6475 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6476   (let (header number)
6477
6478     ;; Do the async thing, if that is required.
6479     (if gnus-newsgroup-async
6480         (setq gnus-newsgroup-threads
6481               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6482                       headers)))
6483
6484     (while headers
6485       (setq header (car headers)
6486             headers (cdr headers)
6487             number (header-number header))
6488
6489       ;; We may have to root out some bad articles...
6490       (cond 
6491        ((and cull
6492              (memq (setq number (header-number header))
6493                    gnus-newsgroup-dormant)))
6494        ((and cull gnus-summary-expunge-below
6495              (< (or (cdr (assq number gnus-newsgroup-scored))
6496                     gnus-summary-default-score 0)
6497                 gnus-summary-expunge-below))
6498         (setq gnus-newsgroup-unreads 
6499               (delq number gnus-newsgroup-unreads))
6500         (setq gnus-newsgroup-reads
6501               (cons (cons number gnus-low-score-mark)
6502                     gnus-newsgroup-reads)))
6503        (t
6504         (gnus-summary-insert-line
6505          nil header 0 nil 
6506          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6507                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6508                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6509                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6510                (t (or (cdr (assq number gnus-newsgroup-reads))
6511                       gnus-ancient-mark)))
6512          (memq number gnus-newsgroup-replied)
6513          (memq number gnus-newsgroup-expirable)
6514          (header-subject header) nil
6515          (cdr (assq number gnus-newsgroup-scored))))))))
6516
6517 (defun gnus-select-newsgroup (group &optional read-all)
6518   "Select newsgroup GROUP.
6519 If READ-ALL is non-nil, all articles in the group are selected."
6520   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6521          (info (nth 2 entry))
6522          articles)
6523     (gnus-check-server
6524      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6525
6526     (or (gnus-check-server gnus-current-select-method)
6527         (error "Couldn't open server"))
6528     
6529     (or (and (null entry)
6530              (gnus-activate-group group))
6531         (and (eq (car entry) t)
6532              (gnus-activate-group (car info)))
6533         (gnus-request-group group t)
6534         (progn
6535           (kill-buffer (current-buffer))
6536           (error "Couldn't request group %s: %s" 
6537                  group (gnus-status-message group))))
6538
6539     (setq gnus-newsgroup-name group)
6540     (setq gnus-newsgroup-unselected nil)
6541     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6542
6543     (and gnus-asynchronous
6544          (gnus-check-backend-function 
6545           'request-asynchronous gnus-newsgroup-name)
6546          (setq gnus-newsgroup-async
6547                (gnus-request-asynchronous gnus-newsgroup-name)))
6548
6549     (setq articles (gnus-articles-to-read group read-all))
6550
6551     (cond 
6552      ((null articles) 
6553       (gnus-message 3 "Couldn't select newsgroup")
6554       'quit)
6555      ((eq articles 0) nil)
6556      (t
6557       ;; Init the dependencies hash table.
6558       (setq gnus-newsgroup-dependencies 
6559             (gnus-make-hashtable (length articles)))
6560       ;; Retrieve the headers and read them in.
6561       (gnus-message 5 "Fetching headers...")
6562       (setq gnus-newsgroup-headers 
6563             (if (eq 'nov (setq gnus-headers-retrieved-by
6564                                ;; This is a naughty hack. To get the
6565                                ;; retrieval of old headers to work, we
6566                                ;; set `nntp-nov-gap' to nil (locally),
6567                                ;; and then just retrieve the headers.
6568                                ;; Mucho magic.
6569                                (if gnus-fetch-old-headers
6570                                    (let (nntp-nov-gap)
6571                                      (gnus-retrieve-headers 
6572                                       (if (not (eq 1 (car articles)))
6573                                           (cons 1 articles)
6574                                         articles)
6575                                       gnus-newsgroup-name))
6576                                  (gnus-retrieve-headers 
6577                                   articles gnus-newsgroup-name))))
6578                 (progn
6579                   (gnus-get-newsgroup-headers-xover articles))
6580               ;; If we were to fetch old headers, but the backend didn't
6581               ;; support XOVER, then it is possible we fetched one article
6582               ;; that we shouldn't have. If that's the case, we remove it.
6583               (if (or (not gnus-fetch-old-headers)
6584                       (eq 1 (car articles)))
6585                   ()
6586                 (save-excursion
6587                   (set-buffer nntp-server-buffer)
6588                   (goto-char (point-min))
6589                   (and 
6590                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6591                    (delete-region       ; So we delete this head.
6592                     (point) 
6593                     (search-forward "\n.\n" nil t)))))
6594               (gnus-get-newsgroup-headers)))
6595       (gnus-message 5 "Fetching headers...done")      
6596       ;; Remove canceled articles from the list of unread articles.
6597       (setq gnus-newsgroup-unreads
6598             (gnus-set-sorted-intersection 
6599              gnus-newsgroup-unreads
6600              (mapcar (lambda (headers) (header-number headers))
6601                      gnus-newsgroup-headers)))
6602       ;; Adjust and set lists of article marks.
6603       (and info
6604            (let (marked)
6605              (gnus-adjust-marked-articles info)
6606              (setq gnus-newsgroup-marked 
6607                    (copy-sequence
6608                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6609              (setq gnus-newsgroup-replied 
6610                    (copy-sequence (cdr (assq 'reply marked))))
6611              (setq gnus-newsgroup-expirable
6612                    (copy-sequence (cdr (assq 'expire marked))))
6613              (setq gnus-newsgroup-killed
6614                    (copy-sequence (cdr (assq 'killed marked))))
6615              (setq gnus-newsgroup-bookmarks 
6616                    (copy-sequence (cdr (assq 'bookmark marked))))
6617              (setq gnus-newsgroup-dormant 
6618                    (copy-sequence (cdr (assq 'dormant marked))))
6619              (setq gnus-newsgroup-scored 
6620                    (copy-sequence (cdr (assq 'score marked))))
6621              (setq gnus-newsgroup-processable nil)))
6622       ;; Check whether auto-expire is to be done in this group.
6623       (setq gnus-newsgroup-auto-expire
6624             (or (and (stringp gnus-auto-expirable-newsgroups)
6625                      (string-match gnus-auto-expirable-newsgroups group))
6626                 (memq 'auto-expire (nth 5 info))))
6627       ;; First and last article in this newsgroup.
6628       (and gnus-newsgroup-headers
6629            (setq gnus-newsgroup-begin 
6630                  (header-number (car gnus-newsgroup-headers)))
6631            (setq gnus-newsgroup-end
6632                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6633       (setq gnus-reffed-article-number -1)
6634       ;; GROUP is successfully selected.
6635       (or gnus-newsgroup-headers t)))))
6636
6637 (defun gnus-articles-to-read (group read-all)
6638   ;; Find out what articles the user wants to read.
6639   (let* ((articles
6640           ;; Select all articles if `read-all' is non-nil, or if all the
6641           ;; unread articles are dormant articles.
6642           (if (or (and read-all (not (numberp read-all)))
6643                   (= (length gnus-newsgroup-unreads) 
6644                      (length gnus-newsgroup-dormant)))
6645               (gnus-uncompress-range 
6646                (gnus-gethash group gnus-active-hashtb))
6647             gnus-newsgroup-unreads))
6648          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6649          (scored (length scored-list))
6650          (number (length articles))
6651          (marked (+ (length gnus-newsgroup-marked)
6652                     (length gnus-newsgroup-dormant)))
6653          (select
6654           (cond 
6655            ((numberp read-all)
6656             read-all)
6657            (t
6658             (condition-case ()
6659                 (cond ((and (or (<= scored marked)
6660                                 (= scored number))
6661                             (numberp gnus-large-newsgroup)
6662                             (> number gnus-large-newsgroup))
6663                        (let ((input
6664                               (read-string
6665                                (format
6666                                 "How many articles from %s (default %d): "
6667                                 gnus-newsgroup-name number))))
6668                          (if (string-match "^[ \t]*$" input)
6669                              number input)))
6670                       ((and (> scored marked) (< scored number))
6671                        (let ((input
6672                               (read-string
6673                                (format 
6674                                 "%s %s (%d scored, %d total): "
6675                                 "How many articles from"
6676                                 group scored number))))
6677                          (if (string-match "^[ \t]*$" input)
6678                              number input)))
6679                       (t number))
6680               (quit nil))))))
6681     (setq select (if (stringp select) (string-to-number select) select))
6682     (if (or (null select) (zerop select))
6683         select
6684       (if (and (not (zerop scored)) (<= (abs select) scored))
6685           (progn
6686             (setq articles (sort scored-list '<))
6687             (setq number (length articles)))
6688         (setq articles (copy-sequence articles)))
6689
6690       (if (< (abs select) number)
6691           (if (< select 0) 
6692               ;; Select the N oldest articles.
6693               (setcdr (nthcdr (1- (abs select)) articles) nil)
6694             ;; Select the N most recent articles.
6695             (setq articles (nthcdr (- number select) articles))))
6696       (setq gnus-newsgroup-unselected
6697             (gnus-sorted-intersection
6698              gnus-newsgroup-unreads
6699              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6700       articles)))
6701
6702 (defun gnus-killed-articles (killed articles)
6703   (let (out)
6704     (while articles
6705       (if (inline (gnus-member-of-range (car articles) killed))
6706           (setq out (cons (car articles) out)))
6707       (setq articles (cdr articles)))
6708     out))
6709
6710 (defun gnus-adjust-marked-articles (info &optional active)
6711   "Remove all marked articles that are no longer legal."
6712   (let ((marked-lists (nth 3 info))
6713         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6714         m prev)
6715     ;; There are many types of marked articles.
6716     (while marked-lists
6717       (setq m (cdr (setq prev (car marked-lists))))
6718       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6719              ;; Make sure that all ticked articles are a subset of the
6720              ;; unread/unselected articles.
6721              (while m
6722                (if (or (memq (car m) gnus-newsgroup-unreads)
6723                        (memq (car m) gnus-newsgroup-unselected))
6724                    (setq prev m)
6725                  (setcdr prev (cdr m)))
6726                (setq m (cdr m))))
6727             ((eq 'score (car prev))
6728              ;; Scored articles should be a subset of
6729              ;; unread/unselected articles. 
6730              (while m
6731                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6732                        (memq (car (car m)) gnus-newsgroup-unreads))
6733                    (setq prev m)
6734                  (setcdr prev (cdr m)))
6735                (setq m (cdr m))))
6736             ((eq 'bookmark (car prev))
6737              ;; Bookmarks should be a subset of active articles.
6738              (while m
6739                (if (< (car (car m)) (car active))
6740                    (setcdr prev (cdr m))
6741                  (setq prev m))
6742                (setq m (cdr m))))
6743             ((eq 'killed (car prev))
6744              ;; Articles that have been through the kill process are
6745              ;; to be a subset of active articles.
6746              (while (and m (< (or (and (numberp (car m)) (car m))
6747                                   (cdr (car m)))
6748                               (car active)))
6749                (setcdr prev (cdr m))
6750                (setq m (cdr m)))
6751              (if (and m (< (or (and (numberp (car m)) (car m))
6752                                (car (car m)))
6753                            (car active))) 
6754                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6755             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6756              ;; The replied and expirable articles have to be articles
6757              ;; that are active. 
6758              (while m
6759                (if (< (car m) (car active))
6760                    (setcdr prev (cdr m))
6761                  (setq prev m))
6762                (setq m (cdr m)))))
6763       (setq marked-lists (cdr marked-lists)))
6764     ;; Remove all lists that are empty.
6765     (setq marked-lists (nth 3 info))
6766     (if marked-lists
6767         (progn
6768           (while (= 1 (length (car marked-lists)))
6769             (setq marked-lists (cdr marked-lists)))
6770           (setq m (cdr (setq prev marked-lists)))
6771           (while m
6772             (if (= 1 (length (car m)))
6773                 (setcdr prev (cdr m))
6774               (setq prev m))
6775             (setq m (cdr m)))
6776           (setcar (nthcdr 3 info) marked-lists)))
6777     ;; Finally, if there are no marked lists at all left, and if there
6778     ;; are no elements after the lists in the info list, we just chop
6779     ;; the info list off before the marked lists.
6780     (and (null marked-lists) 
6781          (not (nthcdr 4 info))
6782          (setcdr (nthcdr 2 info) nil)))
6783   info)
6784
6785 (defun gnus-set-marked-articles 
6786   (info ticked replied expirable killed dormant bookmark score) 
6787   "Enter the various lists of marked articles into the newsgroup info list."
6788   (let (newmarked)
6789     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6790     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6791     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6792                                          newmarked)))
6793     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6794     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6795     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6796                                         newmarked)))
6797     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6798     (if (nthcdr 3 info)
6799         (progn
6800           (setcar (nthcdr 3 info) newmarked)
6801           (and (not newmarked)
6802                (not (nthcdr 4 info))
6803                (setcdr (nthcdr 2 info) nil)))
6804       (if newmarked
6805           (setcdr (nthcdr 2 info) (list newmarked))))))
6806
6807 (defun gnus-add-marked-articles (group type articles &optional info force)
6808   ;; Add ARTICLES of TYPE to the info of GROUP.
6809   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6810   ;; add, but replace marked articles of TYPE with ARTICLES.
6811   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6812         marked m)
6813     (or (not info)
6814         (and (not (setq marked (nthcdr 3 info)))
6815              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6816         (and (not (setq m (assq type (car marked))))
6817              (setcar marked (cons (cons type articles) (car marked))))
6818         (if force
6819             (setcdr m articles)
6820           (nconc m articles)))))
6821          
6822 (defun gnus-set-mode-line (where)
6823   "This function sets the mode line of the article or summary buffers.
6824 If WHERE is `summary', the summary mode line format will be used."
6825   (if (memq where gnus-updated-mode-lines)
6826       (let (mode-string)
6827         (save-excursion
6828           (set-buffer gnus-summary-buffer)
6829           (let* ((mformat (if (eq where 'article) 
6830                               gnus-article-mode-line-format-spec
6831                             gnus-summary-mode-line-format-spec))
6832                  (group-name gnus-newsgroup-name)
6833                  (article-number (or gnus-current-article 0))
6834                  (unread (- (length gnus-newsgroup-unreads)
6835                             (length gnus-newsgroup-dormant)))
6836                  (unread-and-unticked 
6837                   (- unread (length gnus-newsgroup-marked)))
6838                  (unselected (length gnus-newsgroup-unselected))
6839                  (unread-and-unselected
6840                   (cond ((and (zerop unread-and-unticked)
6841                               (zerop unselected)) "")
6842                         ((zerop unselected) 
6843                          (format "{%d more}" unread-and-unticked))
6844                         (t (format "{%d(+%d) more}"
6845                                    unread-and-unticked unselected))))
6846                  (subject
6847                   (if gnus-current-headers
6848                       (header-subject gnus-current-headers) ""))
6849                  (max-len (and gnus-mode-non-string-length
6850                                (- (frame-width) gnus-mode-non-string-length)))
6851                  header) ;; passed as argument to any user-format-funcs
6852             (setq mode-string (eval mformat))
6853             (or (numberp max-len)
6854                 (setq max-len (length mode-string)))
6855             (if (< max-len 4) (setq max-len 4))
6856             (if (> (length mode-string) max-len)
6857                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6858                 ;;  function `substring' might cut on a middle
6859                 ;;  of multi-octet character.
6860                 (setq mode-string 
6861                       (concat (gnus-truncate-string mode-string (- max-len 3))
6862                               "...")))
6863             (setq mode-string (format (format "%%-%ds" max-len)
6864                                       mode-string))))
6865         (setq mode-line-buffer-identification mode-string)
6866         (set-buffer-modified-p t))))
6867
6868 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6869   "Go through the HEADERS list and add all Xrefs to a hash table.
6870 The resulting hash table is returned, or nil if no Xrefs were found."
6871   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6872          (prefix (if (and 
6873                       (gnus-group-foreign-p from-newsgroup)
6874                       (not (memq 'virtual 
6875                                  (assoc (symbol-name (car from-method))
6876                                         gnus-valid-select-methods))))
6877                      (gnus-group-real-prefix from-newsgroup)))
6878          (xref-hashtb (make-vector 63 0))
6879          start group entry number xrefs header)
6880     (while headers
6881       (setq header (car headers))
6882       (if (and (setq xrefs (header-xref header))
6883                (not (memq (header-number header) unreads)))
6884           (progn
6885             (setq start 0)
6886             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6887               (setq start (match-end 0))
6888               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6889                                                     (match-end 1))))
6890               (setq number 
6891                     (string-to-int (substring xrefs (match-beginning 2) 
6892                                               (match-end 2))))
6893               (if (setq entry (gnus-gethash group xref-hashtb))
6894                   (setcdr entry (cons number (cdr entry)))
6895                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6896       (setq headers (cdr headers)))
6897     (if start xref-hashtb nil)))
6898
6899 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6900   "Look through all the headers and mark the Xrefs as read."
6901   (let ((virtual (memq 'virtual 
6902                        (assoc (symbol-name (car (gnus-find-method-for-group 
6903                                                  from-newsgroup)))
6904                               gnus-valid-select-methods)))
6905         name entry info xref-hashtb idlist method
6906         nth4)
6907     (save-excursion
6908       (set-buffer gnus-group-buffer)
6909       (if (setq xref-hashtb 
6910                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6911           (mapatoms 
6912            (lambda (group)
6913              (if (string= from-newsgroup (setq name (symbol-name group)))
6914                  ()
6915                (setq idlist (symbol-value group))
6916                ;; Dead groups are not updated.
6917                (if (and (prog1 
6918                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6919                                   info (nth 2 entry))
6920                           (if (stringp (setq nth4 (nth 4 info)))
6921                               (setq nth4 (gnus-server-to-method nth4))))
6922                         ;; Only do the xrefs if the group has the same
6923                         ;; select method as the group we have just read.
6924                         (or (gnus-methods-equal-p 
6925                              nth4 (gnus-find-method-for-group from-newsgroup))
6926                             virtual
6927                             (equal nth4 
6928                                    (setq method (gnus-find-method-for-group 
6929                                                  from-newsgroup)))
6930                             (and (equal (car nth4) (car method))
6931                                  (equal (nth 1 nth4) (nth 1 method))))
6932                         gnus-use-cross-reference
6933                         (or (not (eq gnus-use-cross-reference t))
6934                             virtual
6935                             ;; Only do cross-references on subscribed
6936                             ;; groups, if that is what is wanted.  
6937                             (<= (nth 1 info) gnus-level-subscribed)))
6938                    (gnus-group-make-articles-read name idlist expirable))))
6939            xref-hashtb)))))
6940
6941 (defun gnus-group-make-articles-read (group articles expirable)
6942   (let* ((num 0)
6943          (entry (gnus-gethash group gnus-newsrc-hashtb))
6944          (info (nth 2 entry))
6945          (active (gnus-gethash group gnus-active-hashtb))
6946          exps expirable range)
6947     ;; First peel off all illegal article numbers.
6948     (if active
6949         (let ((ids articles)
6950               (ticked (cdr (assq 'tick (nth 3 info))))
6951               (dormant (cdr (assq 'dormant (nth 3 info))))
6952               id)
6953           (setq exps nil)
6954           (while ids
6955             (setq id (car ids))
6956             (if (or (> id (cdr active))
6957                     (< id (car active))
6958                     (memq id ticked)
6959                     (memq id dormant))
6960                 (setq articles (delq id articles)))
6961             (and (memq id expirable)
6962                  (setq exps (cons id exps)))
6963             (setq ids (cdr ids)))))
6964     ;; Update expirable articles.
6965     (gnus-add-marked-articles nil 'expirable exps info)
6966     (and active
6967          (null (nth 2 info))
6968          (> (car active) 1)
6969          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6970     (setcar (nthcdr 2 info)
6971             (setq range
6972                   (gnus-add-to-range 
6973                    (nth 2 info) 
6974                    (setq articles (sort articles '<)))))
6975     ;; Then we have to re-compute how many unread
6976     ;; articles there are in this group.
6977     (if active
6978         (progn
6979           (cond 
6980            ((not range)
6981             (setq num (- (1+ (cdr active)) (car active))))
6982            ((not (listp (cdr range)))
6983             (setq num (- (cdr active) (- (1+ (cdr range)) 
6984                                          (car range)))))
6985            (t
6986             (while range
6987               (if (numberp (car range))
6988                   (setq num (1+ num))
6989                 (setq num (+ num (- (1+ (cdr (car range)))
6990                                     (car (car range))))))
6991               (setq range (cdr range)))
6992             (setq num (- (cdr active) num))))
6993           ;; Update the number of unread articles.
6994           (setcar 
6995            entry 
6996            (max 0 (- num 
6997                      (length (cdr (assq 'tick (nth 3 info))))
6998                      (length 
6999                       (cdr (assq 'dormant (nth 3 info)))))))
7000           ;; Update the group buffer.
7001           (gnus-group-update-group group t)))))
7002
7003 (defun gnus-methods-equal-p (m1 m2)
7004   (let ((m1 (or m1 gnus-select-method))
7005         (m2 (or m2 gnus-select-method)))
7006     (or (equal m1 m2)
7007         (and (eq (car m1) (car m2))
7008              (or (not (memq 'address (assoc (symbol-name (car m1))
7009                                             gnus-valid-select-methods)))
7010                  (equal (nth 1 m1) (nth 1 m2)))))))
7011
7012 (defsubst gnus-header-value ()
7013   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7014
7015 (defvar gnus-newsgroup-none-id 0)
7016
7017 (defun gnus-get-newsgroup-headers ()
7018   (setq gnus-article-internal-prepare-hook nil)
7019   (let ((cur nntp-server-buffer)
7020         (dependencies gnus-newsgroup-dependencies)
7021         headers id dep end ref)
7022     (save-excursion
7023       (set-buffer nntp-server-buffer)
7024       (goto-char (point-min))
7025       ;; Search to the beginning of the next header. Error messages
7026       ;; do not begin with 2 or 3.
7027       (while (re-search-forward "^[23][0-9]+ " nil t)
7028         (let ((header (make-vector 9 nil))
7029               (case-fold-search t)
7030               (p (point))
7031               in-reply-to)
7032           (setq id nil
7033                 ref nil)
7034           (header-set-number header (read cur))
7035           ;; This implementation of this function, with nine
7036           ;; search-forwards instead of the one re-search-forward and
7037           ;; a case (which basically was the old function) is actually
7038           ;; about twice as fast, even though it looks messier. You
7039           ;; can't have everything, I guess. Speed and elegance
7040           ;; doesn't always come hand in hand.
7041           (save-restriction
7042             (narrow-to-region (point) (or (save-excursion 
7043                                             (search-forward "\n.\n" nil t))
7044                                           (point)))
7045             (if (search-forward "\nfrom: " nil t)
7046                 (header-set-from header (gnus-header-value))
7047               (header-set-from header "(nobody)"))
7048             (goto-char p)
7049             (if (search-forward "\nsubject: " nil t)
7050                 (header-set-subject header (gnus-header-value))
7051               (header-set-subject header "(none)"))
7052             (goto-char p)
7053             (and (search-forward "\nxref: " nil t)
7054                  (header-set-xref header (gnus-header-value)))
7055             (goto-char p)
7056             (or (numberp (and (search-forward "\nlines: " nil t)
7057                               (header-set-lines header (read cur))))
7058                 (header-set-lines header 0))
7059             (goto-char p)
7060             (and (search-forward "\ndate: " nil t)
7061                  (header-set-date header (gnus-header-value)))
7062             (goto-char p)
7063             (if (search-forward "\nmessage-id: " nil t)
7064                 (header-set-id header (setq id (gnus-header-value)))
7065               ;; If there was no message-id, we just fake one to make
7066               ;; subsequent routines simpler.
7067               (header-set-id 
7068                header 
7069                (setq id (concat "none+" 
7070                                 (int-to-string 
7071                                  (setq gnus-newsgroup-none-id 
7072                                        (1+ gnus-newsgroup-none-id)))))))
7073             (goto-char p)
7074             (if (search-forward "\nreferences: " nil t)
7075                 (progn
7076                   (header-set-references header (gnus-header-value))
7077                   (setq end (match-end 0))
7078                   (save-excursion
7079                     (setq ref 
7080                           (downcase
7081                            (buffer-substring
7082                             (progn 
7083                               (end-of-line)
7084                               (search-backward ">" end t)
7085                               (1+ (point)))
7086                             (progn
7087                               (search-backward "<" end t)
7088                               (point)))))))
7089               ;; Get the references from the in-reply-to header if there
7090               ;; ware no references and the in-reply-to header looks
7091               ;; promising. 
7092               (if (and (search-forward "\nin-reply-to: " nil t)
7093                        (setq in-reply-to (gnus-header-value))
7094                        (string-match "<[^>]+>" in-reply-to))
7095                   (progn
7096                     (header-set-references 
7097                      header 
7098                      (setq ref (substring in-reply-to (match-beginning 0)
7099                                           (match-end 0))))
7100                     (setq ref (downcase ref)))
7101                 (setq ref "none")))
7102             ;; We do some threading while we read the headers. The
7103             ;; message-id and the last reference are both entered into
7104             ;; the same hash table. Some tippy-toeing around has to be
7105             ;; done in case an article has arrived before the article
7106             ;; which it refers to.
7107             (if (boundp (setq dep (intern (downcase id) dependencies)))
7108                 (if (car (symbol-value dep))
7109                     ;; An article with this Message-ID has already
7110                     ;; been seen, so we ignore this one, except we add
7111                     ;; any additional Xrefs (in case the two articles
7112                     ;; came from different servers.
7113                     (progn
7114                       (header-set-xref 
7115                        (car (symbol-value dep))
7116                        (concat (or (header-xref (car (symbol-value dep))) "")
7117                                (or (header-xref header) "")))
7118                       (setq header nil))
7119                   (setcar (symbol-value dep) header))
7120               (set dep (list header)))
7121             (if header
7122                 (progn
7123                   (if (boundp (setq dep (intern ref dependencies)))
7124                       (setcdr (symbol-value dep) 
7125                               (cons header (cdr (symbol-value dep))))
7126                     (set dep (list nil header)))
7127                   (setq headers (cons header headers))))
7128             (goto-char (point-max))))))
7129     (nreverse headers)))
7130
7131 ;; The following macros and functions were written by Felix Lee
7132 ;; <flee@cse.psu.edu>. 
7133
7134 (defmacro gnus-nov-read-integer ()
7135   '(prog1
7136        (if (= (following-char) ?\t)
7137            0
7138          (let ((num (condition-case nil (read buffer) (error nil))))
7139            (if (numberp num) num 0)))
7140      (or (eobp) (forward-char 1))))
7141
7142 (defmacro gnus-nov-skip-field ()
7143   '(search-forward "\t" eol 'move))
7144
7145 (defmacro gnus-nov-field ()
7146   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7147
7148 ;; Goes through the xover lines and returns a list of vectors
7149 (defun gnus-get-newsgroup-headers-xover (sequence)
7150   "Parse the news overview data in the server buffer, and return a
7151 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7152   ;; Get the Xref when the users reads the articles since most/some
7153   ;; NNTP servers do not include Xrefs when using XOVER.
7154   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7155   (let ((cur nntp-server-buffer)
7156         (dependencies gnus-newsgroup-dependencies)
7157         number headers header)
7158     (save-excursion
7159       (set-buffer nntp-server-buffer)
7160       (goto-char (point-min))
7161       (while (and sequence (not (eobp)))
7162         (setq number (read cur))
7163         (while (and sequence (< (car sequence) number))
7164           (setq sequence (cdr sequence)))
7165         (and sequence 
7166              (eq number (car sequence))
7167              (progn
7168                (setq sequence (cdr sequence))
7169                (if (setq header 
7170                          (inline (gnus-nov-parse-line number dependencies)))
7171                    (setq headers (cons header headers)))))
7172         (forward-line 1))
7173       (setq headers (nreverse headers)))
7174     headers))
7175
7176 ;; This function has to be called with point after the article number
7177 ;; on the beginning of the line.
7178 (defun gnus-nov-parse-line (number dependencies)
7179   (let ((none 0)
7180         (eol (gnus-point-at-eol)) 
7181         (buffer (current-buffer))
7182         header ref id dep)
7183
7184     ;; overview: [num subject from date id refs chars lines misc]
7185     (narrow-to-region (point) eol)
7186     (forward-char)
7187
7188     (condition-case nil
7189         (setq header
7190               (vector 
7191                number                   ; number
7192                (gnus-nov-field)         ; subject
7193                (gnus-nov-field)         ; from
7194                (gnus-nov-field)         ; date
7195                (setq id (or (gnus-nov-field)
7196                             (concat "none+"
7197                                     (int-to-string 
7198                                      (setq none (1+ none)))))) ; id
7199                (progn
7200                  (save-excursion
7201                    (let ((beg (point)))
7202                      (search-forward "\t" eol)
7203                      (if (search-backward ">" beg t)
7204                          (setq ref 
7205                                (downcase 
7206                                 (buffer-substring 
7207                                  (1+ (point))
7208                                  (progn
7209                                    (search-backward "<" beg t)
7210                                    (point)))))
7211                        (setq ref nil))))
7212                  (gnus-nov-field))      ; refs
7213                (gnus-nov-read-integer)  ; chars
7214                (gnus-nov-read-integer)  ; lines
7215                (if (= (following-char) ?\n)
7216                    nil
7217                  (gnus-nov-field))      ; misc
7218                ))
7219       (error (progn 
7220                (ding)
7221                (message "Strange nov line.")
7222                (setq header nil)
7223                (goto-char eol))))
7224
7225     (widen)
7226
7227     ;; We build the thread tree.
7228     (and header
7229          (if (boundp (setq dep (intern (downcase id) dependencies)))
7230              (if (car (symbol-value dep))
7231                  ;; An article with this Message-ID has already been seen,
7232                  ;; so we ignore this one, except we add any additional
7233                  ;; Xrefs (in case the two articles came from different
7234                  ;; servers.
7235                  (progn
7236                    (header-set-xref 
7237                     (car (symbol-value dep))
7238                     (concat (or (header-xref (car (symbol-value dep))) "")
7239                             (or (header-xref header) "")))
7240                    (setq header nil))
7241                (setcar (symbol-value dep) header))
7242            (set dep (list header))))
7243     (if header
7244         (progn
7245           (if (boundp (setq dep (intern (or ref "none") 
7246                                         dependencies)))
7247               (setcdr (symbol-value dep) 
7248                       (cons header (cdr (symbol-value dep))))
7249             (set dep (list nil header)))))
7250     header))
7251
7252 (defun gnus-article-get-xrefs ()
7253   "Fill in the Xref value in `gnus-current-headers', if necessary.
7254 This is meant to be called in `gnus-article-internal-prepare-hook'."
7255   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7256                                  gnus-current-headers)))
7257     (or (not gnus-use-cross-reference)
7258         (not headers)
7259         (and (header-xref headers)
7260              (not (string= (header-xref headers) "")))
7261         (let ((case-fold-search t)
7262               xref)
7263           (save-restriction
7264             (gnus-narrow-to-headers)
7265             (goto-char (point-min))
7266             (if (or (and (eq (downcase (following-char)) ?x)
7267                          (looking-at "Xref:"))
7268                     (search-forward "\nXref:" nil t))
7269                 (progn
7270                   (goto-char (1+ (match-end 0)))
7271                   (setq xref (buffer-substring (point) 
7272                                                (progn (end-of-line) (point))))
7273                   (header-set-xref headers xref))))))))
7274
7275 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7276 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7277
7278 (defun gnus-make-headers-hashtable-by-number ()
7279   "Make hashtable for the variable gnus-newsgroup-headers by number."
7280   (save-excursion
7281     (set-buffer gnus-summary-buffer)
7282     (let ((headers gnus-newsgroup-headers)
7283           header)
7284       (setq gnus-newsgroup-headers-hashtb-by-number
7285             (gnus-make-hashtable (length headers)))
7286       (while headers
7287         (setq header (car headers))
7288         (gnus-sethash (int-to-string (header-number header))
7289                       header gnus-newsgroup-headers-hashtb-by-number)
7290         (setq headers (cdr headers))))))
7291
7292 (defun gnus-more-header-backward ()
7293   "Find new header backward."
7294   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7295         (artnum gnus-newsgroup-begin)
7296         (header nil))
7297     (while (and (not header)
7298                 (> artnum first))
7299       (setq artnum (1- artnum))
7300       (setq header (gnus-read-header artnum)))
7301     header))
7302
7303 (defun gnus-more-header-forward (&optional backward)
7304   "Find new header forward.
7305 If BACKWARD, find new header backward instead."
7306   (if backward
7307       (gnus-more-header-backward)
7308     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7309           (artnum gnus-newsgroup-end)
7310           (header nil))
7311       (while (and (not header)
7312                   (< artnum last))
7313         (setq artnum (1+ artnum))
7314         (setq header (gnus-read-header artnum)))
7315       header)))
7316
7317 (defun gnus-extend-newsgroup (header &optional backward)
7318   "Extend newsgroup selection with HEADER.
7319 Optional argument BACKWARD means extend toward backward."
7320   (if header
7321       (let ((artnum (header-number header)))
7322         (setq gnus-newsgroup-headers
7323               (if backward
7324                   (cons header gnus-newsgroup-headers)
7325                 (nconc gnus-newsgroup-headers (list header))))
7326         (setq gnus-newsgroup-unselected
7327               (delq artnum gnus-newsgroup-unselected))
7328         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7329         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7330
7331 (defun gnus-summary-work-articles (n)
7332   "Return a list of articles to be worked upon. The prefix argument,
7333 the list of process marked articles, and the current article will be
7334 taken into consideration."
7335   (let (articles)
7336     (if (and n (numberp n))
7337         (let ((backward (< n 0))
7338               (n (abs n)))
7339           (save-excursion
7340             (while (and (> n 0)
7341                         (setq articles (cons (gnus-summary-article-number) 
7342                                              articles))
7343                         (gnus-summary-search-forward nil nil backward))
7344               (setq n (1- n))))
7345           (sort articles (function <)))
7346       (or (reverse gnus-newsgroup-processable)
7347           (list (gnus-summary-article-number))))))
7348
7349 (defun gnus-summary-search-group (&optional backward use-level)
7350   "Search for next unread newsgroup.
7351 If optional argument BACKWARD is non-nil, search backward instead."
7352   (save-excursion
7353     (set-buffer gnus-group-buffer)
7354     (if (gnus-group-search-forward 
7355          backward nil (if use-level (gnus-group-group-level) nil))
7356         (gnus-group-group-name))))
7357
7358 (defun gnus-summary-best-group (&optional exclude-group)
7359   "Find the name of the best unread group.
7360 If EXCLUDE-GROUP, do not go to this group."
7361   (save-excursion
7362     (set-buffer gnus-group-buffer)
7363     (save-excursion
7364       (gnus-group-best-unread-group exclude-group))))
7365
7366 (defun gnus-subject-equal (s1 s2)
7367   (cond
7368    ((null gnus-summary-gather-subject-limit)
7369     (equal (gnus-simplify-subject-re s1)
7370            (gnus-simplify-subject-re s2)))
7371    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7372     (equal (gnus-simplify-subject-fuzzy s1)
7373            (gnus-simplify-subject-fuzzy s2)))
7374    ((numberp gnus-summary-gather-subject-limit)
7375     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7376            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7377    (t
7378     (equal s1 s2))))
7379     
7380 (defun gnus-summary-search-subject (&optional backward unread subject)
7381   "Search for article forward.
7382 If BACKWARD is non-nil, search backward.
7383 If UNREAD is non-nil, only unread articles are selected.
7384 If SUBJECT is non-nil, the article which has the same subject will be
7385 searched for." 
7386   (let ((func (if backward 'previous-single-property-change
7387                 'next-single-property-change))
7388         (beg (point))
7389         (did t)
7390         pos psubject)
7391     (beginning-of-line)
7392     (and gnus-summary-check-current unread
7393          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7394          (setq did nil))
7395     (if (not did)
7396         ()
7397       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7398       (while
7399           (and 
7400            (setq pos (funcall func (point) 'gnus-number))
7401            (goto-char (if backward (1- pos) pos))
7402            (setq did
7403                  (not (and
7404                        (or (not unread)
7405                            (eq (get-text-property (point) 'gnus-mark)
7406                                gnus-unread-mark))
7407                        (or (not subject)
7408                            (and (setq psubject (inline (gnus-summary-subject-string)))
7409                                 (inline (gnus-subject-equal subject psubject)))))))
7410            (if backward (if (bobp) nil (forward-char -1) t)
7411              (if (eobp) nil (forward-char 1) t)))))
7412     (if did
7413         (progn (goto-char beg) nil)
7414       (prog1
7415           (get-text-property (point) 'gnus-number)
7416         (gnus-summary-position-cursor)))))
7417
7418 (defun gnus-summary-pseudo-article ()
7419   "The thread level of the article on the current line."
7420   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7421
7422 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7423 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7424 (defun gnus-summary-article-score ()
7425   "Return current article score."
7426   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7427       gnus-summary-default-score 0))
7428
7429 (defun gnus-summary-recenter ()
7430   "Center point in the summary window.
7431 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7432 displayed, no centering will be performed." 
7433   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7434   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7435   (let* ((top (cond ((< (window-height) 4) 0)
7436                     ((< (window-height) 7) 1)
7437                     (t 2)))
7438          (height (1- (window-height)))
7439          (bottom (save-excursion (goto-char (point-max))
7440                                  (forward-line (- height))
7441                                  (point)))
7442          (window (get-buffer-window (current-buffer))))
7443     (and 
7444      ;; The user has to want it,
7445      gnus-auto-center-summary 
7446      ;; the article buffer must be displayed,
7447      (get-buffer-window gnus-article-buffer)
7448      ;; Set the window start to either `bottom', which is the biggest
7449      ;; possible valid number, or the second line from the top,
7450      ;; whichever is the least.
7451      (set-window-start
7452       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7453
7454 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7455 (defun gnus-short-group-name (group &optional levels)
7456   "Collapse GROUP name LEVELS."
7457   (let* ((name "") (foreign "") (depth -1) (skip 1)
7458          (levels (or levels
7459                      (progn
7460                        (while (string-match "\\." group skip)
7461                          (setq skip (match-end 0)
7462                                depth (+ depth 1)))
7463                        depth))))
7464     (if (string-match ":" group)
7465         (setq foreign (substring group 0 (match-end 0))
7466               group (substring group (match-end 0))))
7467     (while group
7468       (if (and (string-match "\\." group) (> levels 0))
7469           (setq name (concat name (substring group 0 1))
7470                 group (substring group (match-end 0))
7471                 levels (- levels 1)
7472                 name (concat name "."))
7473         (setq name (concat foreign name group)
7474               group nil)))
7475     name))
7476
7477 (defun gnus-summary-jump-to-group (newsgroup)
7478   "Move point to NEWSGROUP in group mode buffer."
7479   ;; Keep update point of group mode buffer if visible.
7480   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7481       (save-window-excursion
7482         ;; Take care of tree window mode.
7483         (if (get-buffer-window gnus-group-buffer)
7484             (pop-to-buffer gnus-group-buffer))
7485         (gnus-group-jump-to-group newsgroup))
7486     (save-excursion
7487       ;; Take care of tree window mode.
7488       (if (get-buffer-window gnus-group-buffer)
7489           (pop-to-buffer gnus-group-buffer)
7490         (set-buffer gnus-group-buffer))
7491       (gnus-group-jump-to-group newsgroup))))
7492
7493 ;; This function returns a list of article numbers based on the
7494 ;; difference between the ranges of read articles in this group and
7495 ;; the range of active articles.
7496 (defun gnus-list-of-unread-articles (group)
7497   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7498          (active (gnus-gethash group gnus-active-hashtb))
7499          (last (cdr active))
7500          first nlast unread)
7501     ;; If none are read, then all are unread. 
7502     (if (not read)
7503         (setq first (car active))
7504       ;; If the range of read articles is a single range, then the
7505       ;; first unread article is the article after the last read
7506       ;; article. Sounds logical, doesn't it?
7507       (if (not (listp (cdr read)))
7508           (setq first (1+ (cdr read)))
7509         ;; `read' is a list of ranges.
7510         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7511                                 (car (car read)))) 1)
7512             (setq first 1))
7513         (while read
7514           (if first 
7515               (while (< first nlast)
7516                 (setq unread (cons first unread))
7517                 (setq first (1+ first))))
7518           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7519           (setq nlast (if (atom (car (cdr read))) 
7520                           (car (cdr read))
7521                         (car (car (cdr read)))))
7522           (setq read (cdr read)))))
7523     ;; And add the last unread articles.
7524     (while (<= first last)
7525       (setq unread (cons first unread))
7526       (setq first (1+ first)))
7527     ;; Return the list of unread articles.
7528     (nreverse unread)))
7529
7530 (defun gnus-list-of-read-articles (group)
7531   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7532         (active (gnus-gethash group gnus-active-hashtb)))
7533     (and info active
7534          (gnus-sorted-complement 
7535           (gnus-uncompress-range active) 
7536           (gnus-list-of-unread-articles group)))))
7537
7538 ;; Various summary commands
7539
7540 (defun gnus-summary-universal-argument ()
7541   "Perform any operation on all articles marked with the process mark."
7542   (interactive)
7543   (gnus-set-global-variables)
7544   (let ((articles (reverse gnus-newsgroup-processable))
7545         func)
7546     (or articles (error "No articles marked"))
7547     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7548         (error "Undefined key"))
7549     (while articles
7550       (gnus-summary-goto-subject (car articles))
7551       (command-execute func)
7552       (gnus-summary-remove-process-mark (car articles))
7553       (setq articles (cdr articles)))))
7554
7555 (defun gnus-summary-toggle-truncation (&optional arg)
7556   "Toggle truncation of summary lines.
7557 With arg, turn line truncation on iff arg is positive."
7558   (interactive "P")
7559   (setq truncate-lines
7560         (if (null arg) (not truncate-lines)
7561           (> (prefix-numeric-value arg) 0)))
7562   (redraw-display))
7563
7564 (defun gnus-summary-reselect-current-group (&optional all)
7565   "Once exit and then reselect the current newsgroup.
7566 The prefix argument ALL means to select all articles."
7567   (interactive "P")
7568   (gnus-set-global-variables)
7569   (let ((current-subject (gnus-summary-article-number))
7570         (group gnus-newsgroup-name))
7571     (setq gnus-newsgroup-begin nil)
7572     (gnus-summary-exit t)
7573     ;; We have to adjust the point of group mode buffer because the
7574     ;; current point was moved to the next unread newsgroup by
7575     ;; exiting.
7576     (gnus-summary-jump-to-group group)
7577     (gnus-group-read-group all t)
7578     (gnus-summary-goto-subject current-subject)))
7579
7580 (defun gnus-summary-rescan-group (&optional all)
7581   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7582   (interactive "P")
7583   (gnus-set-global-variables)
7584   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7585   (let ((group gnus-newsgroup-name))
7586     (gnus-summary-exit)
7587     (gnus-summary-jump-to-group group)
7588     (save-excursion
7589       (set-buffer gnus-group-buffer)
7590       (gnus-group-get-new-news-this-group 1))
7591     (gnus-summary-jump-to-group group)
7592     (gnus-group-read-group all)))
7593
7594 (defun gnus-summary-update-info ()
7595   (let* ((group gnus-newsgroup-name))
7596     (if gnus-newsgroup-kill-headers
7597         (setq gnus-newsgroup-killed
7598               (gnus-compress-sequence
7599                (nconc
7600                 (gnus-set-sorted-intersection
7601                  (gnus-uncompress-range gnus-newsgroup-killed)
7602                  (setq gnus-newsgroup-unselected
7603                        (sort gnus-newsgroup-unselected '<)))
7604                 (setq gnus-newsgroup-unreads
7605                       (sort gnus-newsgroup-unreads '<))) t)))
7606     (or (listp (cdr gnus-newsgroup-killed))
7607         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7608     (let ((headers gnus-newsgroup-headers))
7609       (gnus-close-group group)
7610       (run-hooks 'gnus-exit-group-hook)
7611       (gnus-update-read-articles 
7612        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7613        gnus-newsgroup-marked
7614        t gnus-newsgroup-replied gnus-newsgroup-expirable
7615        gnus-newsgroup-killed gnus-newsgroup-dormant
7616        gnus-newsgroup-bookmarks 
7617        (and gnus-save-score gnus-newsgroup-scored))
7618       (and gnus-use-cross-reference
7619            (gnus-mark-xrefs-as-read 
7620             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7621       ;; Do adaptive scoring, and possibly save score files.
7622       (and gnus-newsgroup-adaptive
7623            (gnus-score-adaptive))
7624       (and gnus-use-scoring 
7625            (fboundp 'gnus-score-save)
7626            (funcall 'gnus-score-save))
7627       ;; Do not switch windows but change the buffer to work.
7628       (set-buffer gnus-group-buffer)
7629       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7630           (gnus-group-update-group group)))))
7631   
7632 (defun gnus-summary-exit (&optional temporary)
7633   "Exit reading current newsgroup, and then return to group selection mode.
7634 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7635   (interactive)
7636   (gnus-set-global-variables)
7637   (gnus-kill-save-kill-buffer)
7638   (let* ((group gnus-newsgroup-name)
7639          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7640                                                   gnus-newsgroup-name))))
7641          (mode major-mode)
7642          (buf (current-buffer)))
7643     (run-hooks 'gnus-summary-prepare-exit-hook)
7644     (gnus-summary-update-info) ; Make all changes in this group permanent.
7645     (set-buffer buf)
7646     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7647     ;; Make sure where I was, and go to next newsgroup.
7648     (set-buffer gnus-group-buffer)
7649     (or quit-config
7650         (progn
7651           (gnus-group-jump-to-group group)
7652           (gnus-group-next-unread-group 1)))
7653     (if temporary
7654         nil                             ;Nothing to do.
7655       ;; We set all buffer-local variables to nil. It is unclear why
7656       ;; this is needed, but if we don't, buffer-local variables are
7657       ;; not garbage-collected, it seems. This would the lead to en
7658       ;; ever-growing Emacs.
7659       (set-buffer buf)
7660       (gnus-summary-clear-local-variables)
7661       ;; We clear the global counterparts of the buffer-local
7662       ;; variables as well, just to be on the safe side.
7663       (gnus-configure-windows 'group 'force)
7664       (gnus-summary-clear-local-variables)
7665       ;; Return to group mode buffer. 
7666       (if (eq mode 'gnus-summary-mode)
7667           (gnus-kill-buffer buf))
7668       (if (get-buffer gnus-article-buffer)
7669           (bury-buffer gnus-article-buffer))
7670       (setq gnus-current-select-method gnus-select-method)
7671       (pop-to-buffer gnus-group-buffer)
7672       (if (not quit-config)
7673           (progn
7674             (gnus-group-jump-to-group group)
7675             (gnus-group-next-unread-group 1))
7676         (if (not (buffer-name (car quit-config)))
7677             (gnus-configure-windows 'group 'force)
7678           (set-buffer (car quit-config))
7679           (and (eq major-mode 'gnus-summary-mode)
7680                (gnus-set-global-variables))
7681           (gnus-configure-windows (cdr quit-config))))
7682       (run-hooks 'gnus-summary-exit-hook))))
7683
7684 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7685 (defun gnus-summary-exit-no-update (&optional no-questions)
7686   "Quit reading current newsgroup without updating read article info."
7687   (interactive)
7688   (gnus-set-global-variables)
7689   (let* ((group gnus-newsgroup-name)
7690          (quit-config (nth 1 (assoc 'quit-config 
7691                                     (gnus-find-method-for-group group)))))
7692     (if (or no-questions
7693             gnus-expert-user
7694             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7695         (progn
7696           (gnus-close-group group)
7697           (gnus-summary-clear-local-variables)
7698           (set-buffer gnus-group-buffer)
7699           (gnus-summary-clear-local-variables)
7700           ;; Return to group selection mode.
7701           (gnus-configure-windows 'group 'force)
7702           (if (get-buffer gnus-summary-buffer)
7703               (kill-buffer gnus-summary-buffer))
7704           (if (get-buffer gnus-article-buffer)
7705               (bury-buffer gnus-article-buffer))
7706           (if (equal (gnus-group-group-name) group)
7707               (gnus-group-next-unread-group 1))
7708           (if quit-config
7709               (progn
7710                 (if (not (buffer-name (car quit-config)))
7711                     (gnus-configure-windows 'group 'force)
7712                   (set-buffer (car quit-config))
7713                   (and (eq major-mode 'gnus-summary-mode)
7714                        (gnus-set-global-variables))
7715                   (gnus-configure-windows (cdr quit-config)))))))))
7716
7717 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7718 (defun gnus-summary-fetch-faq (group)
7719   "Fetch the FAQ for the current group."
7720   (interactive (list gnus-newsgroup-name))
7721   (let ((gnus-faq-buffer 
7722          (find-file (concat gnus-group-faq-directory 
7723                             (gnus-group-real-name group)))))
7724   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7725
7726 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7727 (defun gnus-summary-describe-group (&optional force)
7728   "Describe the current newsgroup."
7729   (interactive "P")
7730   (gnus-group-describe-group force gnus-newsgroup-name))
7731
7732 (defun gnus-summary-describe-briefly ()
7733   "Describe summary mode commands briefly."
7734   (interactive)
7735   (gnus-message 6
7736     (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")))
7737
7738 ;; Walking around group mode buffer from summary mode.
7739
7740 (defun gnus-summary-next-group (&optional no-article target-group backward)
7741   "Exit current newsgroup and then select next unread newsgroup.
7742 If prefix argument NO-ARTICLE is non-nil, no article is selected
7743 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7744 previous group instead."
7745   (interactive "P")
7746   (gnus-set-global-variables)
7747   (let ((current-group gnus-newsgroup-name)
7748         (current-buffer (current-buffer))
7749         entered)
7750     ;; First we semi-exit this group to update Xrefs and all variables.
7751     ;; We can't do a real exit, because the window conf must remain
7752     ;; the same in case the user is prompted for info, and we don't
7753     ;; want the window conf to change before that...
7754     (gnus-summary-exit t)
7755     (while (not entered)
7756       ;; Then we find what group we are supposed to enter.
7757       (set-buffer gnus-group-buffer)
7758       (gnus-group-jump-to-group current-group)
7759       (setq target-group 
7760             (or target-group        
7761                 (if (eq gnus-keep-same-level 'best) 
7762                     (gnus-summary-best-group gnus-newsgroup-name)
7763                   (gnus-summary-search-group backward gnus-keep-same-level))))
7764       (if (not target-group)
7765           ;; There are no further groups, so we return to the group
7766           ;; buffer.
7767           (progn
7768             (gnus-message 5 "Returning to the group buffer")
7769             (setq entered t)
7770             (set-buffer current-buffer)
7771             (gnus-summary-exit))
7772         ;; We try to enter the target group.
7773         (gnus-group-jump-to-group target-group)
7774         (let ((unreads (gnus-group-group-unread)))
7775           (if (and (or (eq t unreads)
7776                        (and unreads (not (zerop unreads))))
7777                    (gnus-summary-read-group
7778                     target-group nil no-article current-buffer))
7779               (setq entered t)
7780             (setq current-group target-group
7781                   target-group nil)))))))
7782
7783 (defun gnus-summary-next-group-old (&optional no-article group backward)
7784   "Exit current newsgroup and then select next unread newsgroup.
7785 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7786 If BACKWARD, go to previous group instead."
7787   (interactive "P")
7788   (gnus-set-global-variables)
7789   (let ((ingroup gnus-newsgroup-name)
7790         (sumbuf (current-buffer))
7791         num)
7792     (set-buffer gnus-group-buffer)
7793     (if (and group
7794              (or (and (numberp (setq num (car (gnus-gethash
7795                                                group gnus-newsrc-hashtb))))
7796                       (< num 1))
7797                  (null num)))
7798         (progn
7799           (gnus-group-jump-to-group group)
7800           (setq group nil))
7801       (gnus-group-jump-to-group ingroup))
7802     (gnus-summary-search-group backward)
7803     (let ((group (or group (gnus-summary-search-group backward))))
7804       (set-buffer sumbuf)
7805       (gnus-summary-exit t)             ;Update all information.
7806       (if (null group)
7807           (gnus-summary-exit-no-update t)
7808         (gnus-group-jump-to-group ingroup)
7809         (setq group (gnus-summary-search-group backward))
7810         (gnus-message 5 "Selecting %s..." group)
7811         (set-buffer gnus-group-buffer)
7812         ;; We are now in group mode buffer.
7813         ;; Make sure group mode buffer point is on GROUP.
7814         (gnus-group-jump-to-group group)
7815         (if (not (eq gnus-auto-select-next 'quietly))
7816             (progn
7817               (gnus-summary-read-group group nil no-article sumbuf)
7818               (and (string= gnus-newsgroup-name ingroup)
7819                    (bufferp sumbuf) (buffer-name sumbuf)
7820                    (progn
7821                      (set-buffer (setq gnus-summary-buffer sumbuf))
7822                      (gnus-summary-exit-no-update t))))
7823           (let ((prevgroup group))
7824             (gnus-group-jump-to-group ingroup)
7825             (setq group (gnus-summary-search-group backward))
7826             (gnus-summary-read-group group nil no-article sumbuf)
7827             (while (and (string= gnus-newsgroup-name ingroup)
7828                         (bufferp sumbuf) 
7829                         (buffer-name sumbuf)
7830                         (not (string= prevgroup (gnus-group-group-name))))
7831               (set-buffer gnus-group-buffer)
7832               (gnus-summary-read-group 
7833                (setq prevgroup (gnus-group-group-name)) 
7834                nil no-article sumbuf))
7835             (and (string= prevgroup (gnus-group-group-name))
7836                  ;; We have reached the final group in the group
7837                  ;; buffer.
7838                  (progn
7839                    (if (buffer-name sumbuf)
7840                        (progn
7841                          (set-buffer sumbuf)
7842                          (gnus-summary-exit)))))))))))
7843
7844 (defun gnus-summary-prev-group (&optional no-article)
7845   "Exit current newsgroup and then select previous unread newsgroup.
7846 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7847   (interactive "P")
7848   (gnus-summary-next-group no-article nil t))
7849
7850 ;; Walking around summary lines.
7851
7852 (defun gnus-summary-first-subject (&optional unread)
7853   "Go to the first unread subject.
7854 If UNREAD is non-nil, go to the first unread article.
7855 Returns nil if there are no unread articles."
7856   (interactive "P")
7857   (prog1
7858       (cond ((not unread)
7859              (goto-char (point-min)))
7860             ((gnus-goto-char 
7861               (text-property-any 
7862                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7863              t)
7864             (t 
7865              ;; There are no unread articles.
7866              (gnus-message 3 "No more unread articles")
7867              nil))
7868     (gnus-summary-position-cursor)))
7869
7870 (defun gnus-summary-next-subject (n &optional unread dont-display)
7871   "Go to next N'th summary line.
7872 If N is negative, go to the previous N'th subject line.
7873 If UNREAD is non-nil, only unread articles are selected.
7874 The difference between N and the actual number of steps taken is
7875 returned."
7876   (interactive "p")
7877   (let ((backward (< n 0))
7878         (n (abs n)))
7879     (while (and (> n 0)
7880                 (gnus-summary-search-forward unread nil backward))
7881       (setq n (1- n)))
7882     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7883                                (if unread " unread" "")))
7884     (or dont-display
7885         (progn
7886           (gnus-summary-recenter)
7887           (gnus-summary-position-cursor)))
7888   n))
7889
7890 (defun gnus-summary-next-unread-subject (n)
7891   "Go to next N'th unread summary line."
7892   (interactive "p")
7893   (gnus-summary-next-subject n t))
7894
7895 (defun gnus-summary-prev-subject (n &optional unread)
7896   "Go to previous N'th summary line.
7897 If optional argument UNREAD is non-nil, only unread article is selected."
7898   (interactive "p")
7899   (gnus-summary-next-subject (- n) unread))
7900
7901 (defun gnus-summary-prev-unread-subject (n)
7902   "Go to previous N'th unread summary line."
7903   (interactive "p")
7904   (gnus-summary-next-subject (- n) t))
7905
7906 (defun gnus-summary-goto-subject (article)
7907   "Go the subject line of ARTICLE."
7908   (interactive
7909    (list
7910     (string-to-int
7911      (completing-read "Article number: "
7912                       (mapcar
7913                        (lambda (headers)
7914                          (list
7915                           (int-to-string (header-number headers))))
7916                        gnus-newsgroup-headers)
7917                       nil 'require-match))))
7918   (or article (error "No article number"))
7919   (let ((b (point)))
7920     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7921                                                 'gnus-number article)))
7922         ()
7923       (gnus-summary-show-thread)
7924       ;; Skip dummy articles. 
7925       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7926           (forward-line 1))
7927       (prog1
7928           (if (not (eobp))
7929               article
7930             (goto-char b)
7931             nil)
7932         (gnus-summary-position-cursor)))))
7933
7934 ;; Walking around summary lines with displaying articles.
7935
7936 (defun gnus-summary-expand-window (&optional arg)
7937   "Make the summary buffer take up the entire Emacs frame.
7938 Given a prefix, will force an `article' buffer configuration."
7939   (interactive "P")
7940   (gnus-set-global-variables)
7941   (if arg
7942       (gnus-configure-windows 'article 'force)
7943     (gnus-configure-windows 'summary 'force)))
7944
7945 (defun gnus-summary-display-article (article &optional all-header)
7946   "Display ARTICLE in article buffer."
7947   (gnus-set-global-variables)
7948   (if (null article)
7949       nil
7950     (prog1
7951         (gnus-article-prepare article all-header)
7952       (gnus-summary-show-thread)
7953       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7954           (progn
7955             (forward-line 1)
7956             (gnus-summary-position-cursor)))
7957       (run-hooks 'gnus-select-article-hook)
7958       (gnus-summary-recenter)
7959       (gnus-summary-goto-subject article)
7960       ;; Successfully display article.
7961       (gnus-summary-update-line)
7962       (gnus-article-set-window-start 
7963        (cdr (assq article gnus-newsgroup-bookmarks)))
7964       t)))
7965
7966 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7967   "Select the current article.
7968 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7969 non-nil, the article will be re-fetched even if it already present in
7970 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7971 be displayed."
7972   (and (not pseudo) (gnus-summary-pseudo-article)
7973        (error "This is a pseudo-article."))
7974   (let ((article (or article (gnus-summary-article-number)))
7975         (all-headers (not (not all-headers))) ;Must be T or NIL.
7976         did) 
7977     (prog1
7978         (save-excursion
7979           (set-buffer gnus-summary-buffer)
7980           (if (or (null gnus-current-article)
7981                   (null gnus-article-current)
7982                   (null (get-buffer gnus-article-buffer))
7983                   (not (eq article (cdr gnus-article-current)))
7984                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7985                   force)
7986               ;; The requested article is different from the current article.
7987               (progn
7988                 (gnus-summary-display-article article all-headers)
7989                 (setq did article))
7990             (if (or all-headers gnus-show-all-headers) 
7991                 (gnus-article-show-all-headers))
7992             nil))
7993       (if did 
7994           (gnus-article-set-window-start 
7995            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7996
7997 (defun gnus-summary-set-current-mark (&optional current-mark)
7998   "Obsolete function."
7999   nil)
8000
8001 (defun gnus-summary-next-article (&optional unread subject backward)
8002   "Select the next article.
8003 If UNREAD, only unread articles are selected.
8004 If SUBJECT, only articles with SUBJECT are selected.
8005 If BACKWARD, the previous article is selected instead of the next."
8006   (interactive "P")
8007   (gnus-set-global-variables)
8008   (let (header)
8009     (cond
8010      ;; Is there such an article?
8011      ((or (gnus-summary-display-article 
8012            (gnus-summary-search-forward unread subject backward))
8013           (eq (gnus-summary-article-mark) gnus-canceled-mark))
8014       (gnus-summary-position-cursor))
8015      ;; If not, we try the first unread, if that is wanted.
8016      ((and subject
8017            gnus-auto-select-same
8018            (gnus-summary-first-unread-article))
8019       (gnus-message 6 "Wrapped"))
8020      ;; Try to get next/previous article not displayed in this group.
8021      ((and gnus-auto-extend-newsgroup
8022            (not unread) (not subject)
8023            (setq header (gnus-more-header-forward backward)))
8024       (gnus-extend-newsgroup header backward)
8025       (let ((buffer-read-only nil))
8026         (goto-char (if backward (point-min) (point-max)))
8027         (gnus-summary-prepare-threads (list header)))
8028       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8029                                    gnus-newsgroup-end)))
8030      ;; Go to next/previous group.
8031      (t
8032       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8033           (gnus-summary-jump-to-group gnus-newsgroup-name))
8034       (let ((cmd (aref (this-command-keys) 0))
8035             (group 
8036              (if (eq gnus-keep-same-level 'best) 
8037                  (gnus-summary-best-group gnus-newsgroup-name)
8038                (gnus-summary-search-group backward gnus-keep-same-level))))
8039         ;; For some reason, the group window gets selected. We change
8040         ;; it back.  
8041         (select-window (get-buffer-window (current-buffer)))
8042         ;; Keep just the event type of CMD.
8043         (and (listp cmd) (setq cmd (car cmd)))
8044         ;; Select next unread newsgroup automagically.
8045         (cond 
8046          ((not gnus-auto-select-next)
8047           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8048          ((eq gnus-auto-select-next 'quietly)
8049           ;; Select quietly.
8050           (if (assoc 'quit-config (gnus-find-method-for-group 
8051                                    gnus-newsgroup-name))
8052               (gnus-summary-exit)
8053             (gnus-message 7 "No more%s articles (%s)..."
8054                           (if unread " unread" "") 
8055                           (if group (concat "selecting " group)
8056                             "exiting"))
8057             (gnus-summary-next-group nil group backward)))
8058          (t
8059           (let ((keystrokes '(?\C-n ?\C-p))
8060                 key)
8061             (while (or (null key) (memq key keystrokes))
8062               (gnus-message 
8063                7 "No more%s articles%s" (if unread " unread" "")
8064                (if (and group (not (assoc 'quit-config
8065                                           (gnus-find-method-for-group 
8066                                            gnus-newsgroup-name))))
8067                    (format " (Type %s for %s [%s])"
8068                            (single-key-description cmd) group
8069                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8070                  (format " (Type %s to exit %s)"
8071                          (single-key-description cmd)
8072                          gnus-newsgroup-name)))
8073               ;; Confirm auto selection.
8074               (let* ((event (read-event)))
8075                 (setq key (if (listp event) (car event) event))
8076                 (if (memq key keystrokes)
8077                     (let ((obuf (current-buffer)))
8078                       (switch-to-buffer gnus-group-buffer)
8079                       (and group
8080                            (gnus-group-jump-to-group group))
8081                       (condition-case ()
8082                           (execute-kbd-macro (char-to-string key))
8083                         (error (ding) nil))
8084                       (setq group (gnus-group-group-name))
8085                       (switch-to-buffer obuf)))))
8086             (if (equal key cmd)
8087                 (if (or (not group) (assoc 'quit-config
8088                                            (gnus-find-method-for-group
8089                                             gnus-newsgroup-name)))
8090                     (gnus-summary-exit)
8091                   (gnus-summary-next-group nil group backward))
8092               (setq unread-command-events (list key)))))))))))
8093
8094 (defun gnus-summary-next-unread-article ()
8095   "Select unread article after current one."
8096   (interactive)
8097   (gnus-summary-next-article t (and gnus-auto-select-same
8098                                     (gnus-summary-subject-string))))
8099
8100 (defun gnus-summary-prev-article (&optional unread subject)
8101   "Select the article after the current one.
8102 If UNREAD is non-nil, only unread articles are selected."
8103   (interactive "P")
8104   (gnus-summary-next-article unread subject t))
8105
8106 (defun gnus-summary-prev-unread-article ()
8107   "Select unred article before current one."
8108   (interactive)
8109   (gnus-summary-prev-article t (and gnus-auto-select-same
8110                                     (gnus-summary-subject-string))))
8111
8112 (defun gnus-summary-next-page (&optional lines circular)
8113   "Show next page of selected article.
8114 If end of article, select next article.
8115 Argument LINES specifies lines to be scrolled up.
8116 If CIRCULAR is non-nil, go to the start of the article instead of 
8117 instead of selecting the next article when reaching the end of the
8118 current article." 
8119   (interactive "P")
8120   (setq gnus-summary-buffer (current-buffer))
8121   (gnus-set-global-variables)
8122   (let ((article (gnus-summary-article-number))
8123         (endp nil))
8124     (gnus-configure-windows 'article)
8125     (if (or (null gnus-current-article)
8126             (null gnus-article-current)
8127             (/= article (cdr gnus-article-current))
8128             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8129         ;; Selected subject is different from current article's.
8130         (gnus-summary-display-article article)
8131       (gnus-eval-in-buffer-window
8132        gnus-article-buffer
8133        (setq endp (gnus-article-next-page lines)))
8134       (if endp
8135           (cond (circular
8136                  (gnus-summary-beginning-of-article))
8137                 (lines
8138                  (gnus-message 3 "End of message"))
8139                 ((null lines)
8140                  (gnus-summary-next-unread-article)))))
8141     (gnus-summary-recenter)
8142     (gnus-summary-position-cursor)))
8143
8144 (defun gnus-summary-prev-page (&optional lines)
8145   "Show previous page of selected article.
8146 Argument LINES specifies lines to be scrolled down."
8147   (interactive "P")
8148   (gnus-set-global-variables)
8149   (let ((article (gnus-summary-article-number)))
8150     (gnus-configure-windows 'article)
8151     (if (or (null gnus-current-article)
8152             (null gnus-article-current)
8153             (/= article (cdr gnus-article-current))
8154             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8155         ;; Selected subject is different from current article's.
8156         (gnus-summary-display-article article)
8157       (gnus-summary-recenter)
8158       (gnus-eval-in-buffer-window gnus-article-buffer
8159         (gnus-article-prev-page lines))))
8160   (gnus-summary-position-cursor))
8161
8162 (defun gnus-summary-scroll-up (lines)
8163   "Scroll up (or down) one line current article.
8164 Argument LINES specifies lines to be scrolled up (or down if negative)."
8165   (interactive "p")
8166   (gnus-set-global-variables)
8167   (gnus-configure-windows 'article)
8168   (or (gnus-summary-select-article nil nil 'pseudo)
8169       (gnus-eval-in-buffer-window 
8170        gnus-article-buffer
8171        (cond ((> lines 0)
8172               (if (gnus-article-next-page lines)
8173                   (gnus-message 3 "End of message")))
8174              ((< lines 0)
8175               (gnus-article-prev-page (- lines))))))
8176   (gnus-summary-recenter)
8177   (gnus-summary-position-cursor))
8178
8179 (defun gnus-summary-next-same-subject ()
8180   "Select next article which has the same subject as current one."
8181   (interactive)
8182   (gnus-set-global-variables)
8183   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8184
8185 (defun gnus-summary-prev-same-subject ()
8186   "Select previous article which has the same subject as current one."
8187   (interactive)
8188   (gnus-set-global-variables)
8189   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8190
8191 (defun gnus-summary-next-unread-same-subject ()
8192   "Select next unread article which has the same subject as current one."
8193   (interactive)
8194   (gnus-set-global-variables)
8195   (gnus-summary-next-article t (gnus-summary-subject-string)))
8196
8197 (defun gnus-summary-prev-unread-same-subject ()
8198   "Select previous unread article which has the same subject as current one."
8199   (interactive)
8200   (gnus-set-global-variables)
8201   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8202
8203 (defun gnus-summary-first-unread-article ()
8204   "Select the first unread article. 
8205 Return nil if there are no unread articles."
8206   (interactive)
8207   (gnus-set-global-variables)
8208   (prog1
8209       (if (gnus-summary-first-subject t)
8210           (progn
8211             (gnus-summary-show-thread)
8212             (gnus-summary-first-subject t)
8213             (gnus-summary-display-article (gnus-summary-article-number))))
8214     (gnus-summary-position-cursor)))
8215
8216 (defun gnus-summary-best-unread-article ()
8217   "Select the unread article with the highest score."
8218   (interactive)
8219   (gnus-set-global-variables)
8220   (let ((scored gnus-newsgroup-scored)
8221         (best -1000000)
8222         article art)
8223     (while scored
8224       (or (> best (cdr (car scored)))
8225           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8226                (not (memq art gnus-newsgroup-marked))
8227                (not (memq art gnus-newsgroup-dormant))
8228                (if (= best (cdr (car scored)))
8229                    (setq article (min art article))
8230                  (setq article art)
8231                  (setq best (cdr (car scored))))))
8232       (setq scored (cdr scored)))
8233     (cond
8234      ((or (not article) (null gnus-newsgroup-unreads))
8235       ;; We didn't find any scored articles, so we just jump to the
8236       ;; first article. 
8237       (gnus-summary-first-unread-article))
8238      ((> best gnus-summary-default-score)
8239       ;; We found one, and it's bigger than the default score, so we
8240       ;; select it.
8241       (gnus-summary-goto-article article))
8242      (t
8243       ;; We found an article, but it has a score lower than the
8244       ;; defaults, so we try to find an article with the default
8245       ;; score. 
8246       (goto-char (point-min))
8247       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8248                       (not (eq (cdr (assq (gnus-summary-article-number)
8249                                           gnus-newsgroup-scored))
8250                                gnus-summary-default-score)))
8251                   (zerop (forward-line 1))
8252                   (not (eobp))))
8253       (if (= (gnus-summary-article-mark) gnus-unread-mark)
8254           ;; We jump to the article we have finally found.
8255           (gnus-summary-goto-article (gnus-summary-article-number))
8256         ;; Or there were no default-scored articles.
8257         (gnus-summary-goto-article article))))
8258     (gnus-summary-position-cursor)))
8259
8260 (defun gnus-summary-goto-article (article &optional all-headers)
8261   "Fetch ARTICLE and display it if it exists.
8262 If ALL-HEADERS is non-nil, no header lines are hidden."
8263   (interactive
8264    (list
8265     (string-to-int
8266      (completing-read 
8267       "Article number: "
8268       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8269               gnus-newsgroup-headers) 
8270       nil 'require-match))))
8271   (prog1
8272       (and (gnus-summary-goto-subject article)
8273            (gnus-summary-display-article article all-headers))
8274     (gnus-summary-position-cursor)))
8275
8276 (defun gnus-summary-goto-last-article ()
8277   "Go to the previously read article."
8278   (interactive)
8279   (prog1
8280       (and gnus-last-article
8281            (gnus-summary-goto-article gnus-last-article))
8282     (gnus-summary-position-cursor)))
8283
8284 (defun gnus-summary-pop-article (number)
8285   "Pop one article off the history and go to the previous.
8286 NUMBER articles will be popped off."
8287   (interactive "p")
8288   (let (to)
8289     (setq gnus-newsgroup-history
8290           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8291     (if to
8292         (gnus-summary-goto-article (car to))
8293       (error "Article history empty")))
8294   (gnus-summary-position-cursor))
8295
8296 ;; Summary article oriented commands
8297
8298 (defun gnus-summary-refer-parent-article (n)
8299   "Refer parent article N times.
8300 The difference between N and the number of articles fetched is returned."
8301   (interactive "p")
8302   (gnus-set-global-variables)
8303   (while 
8304       (and 
8305        (> n 0)
8306        (let ((ref (header-references (gnus-get-header-by-num
8307                                       (gnus-summary-article-number)))))
8308          (if (and ref (not (equal ref ""))
8309                   (string-match "<[^<>]*>[ \t]*$" ref))
8310              (gnus-summary-refer-article 
8311               (substring ref (match-beginning 0) (match-end 0)))
8312            (gnus-message 1 "No references in article %d"
8313                          (gnus-summary-article-number))
8314            nil)))
8315     (setq n (1- n)))
8316   (gnus-summary-position-cursor)
8317   n)
8318     
8319 (defun gnus-summary-refer-article (message-id)
8320   "Refer article specified by MESSAGE-ID.
8321 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8322   (interactive "sMessage-ID: ")
8323   (if (or (not (stringp message-id))
8324           (zerop (length message-id)))
8325       ()
8326     ;; Construct the correct Message-ID if necessary.
8327     ;; Suggested by tale@pawl.rpi.edu.
8328     (or (string-match "^<" message-id)
8329         (setq message-id (concat "<" message-id)))
8330     (or (string-match ">$" message-id)
8331         (setq message-id (concat message-id ">")))
8332     (let ((header (car (gnus-gethash (downcase message-id)
8333                                      gnus-newsgroup-dependencies))))
8334       (if header
8335           (or (gnus-summary-goto-article (header-number header))
8336               ;; The header has been read, but the article had been
8337               ;; expunged, so we insert it again.
8338               (progn
8339                 (gnus-summary-insert-line
8340                  nil header 0 nil gnus-read-mark nil nil
8341                  (header-subject header))
8342                 (forward-line -1)
8343                 (header-number header)))
8344         (let ((gnus-override-method gnus-refer-article-method)
8345               (gnus-ancient-mark gnus-read-mark)
8346               (tmp-point (window-start
8347                           (get-buffer-window gnus-article-buffer)))
8348               number tmp-buf)
8349           (and gnus-refer-article-method
8350                (gnus-check-server gnus-refer-article-method))
8351           ;; Save the old article buffer.
8352           (save-excursion
8353             (set-buffer gnus-article-buffer)
8354             (gnus-kill-buffer " *temp Article*")
8355             (setq tmp-buf (rename-buffer " *temp Article*")))
8356           (prog1
8357               (if (gnus-article-prepare 
8358                    message-id nil (gnus-read-header message-id))
8359                   (progn
8360                     (setq number (header-number gnus-current-headers))
8361                     (gnus-rebuild-thread message-id)
8362                     (gnus-summary-goto-subject number)
8363                     (gnus-summary-recenter)
8364                     (gnus-article-set-window-start 
8365                      (cdr (assq number gnus-newsgroup-bookmarks)))
8366                     message-id)
8367                 ;; We restore the old article buffer.
8368                 (save-excursion
8369                   (kill-buffer gnus-article-buffer)
8370                   (set-buffer tmp-buf)
8371                   (rename-buffer gnus-article-buffer)
8372                   (let ((buffer-read-only nil))
8373                     (and tmp-point
8374                          (set-window-start (get-buffer-window (current-buffer))
8375                                            tmp-point)))))))))))
8376
8377 (defun gnus-summary-enter-digest-group ()
8378   "Enter a digest group based on the current article."
8379   (interactive)
8380   (gnus-set-global-variables)
8381   (gnus-summary-select-article)
8382   ;; We do not want a narrowed article.
8383   (gnus-summary-stop-page-breaking)
8384   (let ((name (format "%s-%d" 
8385                       (gnus-group-prefixed-name 
8386                        gnus-newsgroup-name (list 'nndoc "")) 
8387                       gnus-current-article))
8388         (ogroup gnus-newsgroup-name)
8389         (buf (current-buffer)))
8390     (if (gnus-group-read-ephemeral-group 
8391          name (list 'nndoc name
8392                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8393                     '(nndoc-article-type digest))
8394          t)
8395         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8396                 (list (list (cons 'to-group ogroup))))
8397       (switch-to-buffer buf)
8398       (gnus-set-global-variables)
8399       (gnus-configure-windows 'summary)
8400       (gnus-message 3 "Article not a digest?"))))
8401
8402 (defun gnus-summary-isearch-article ()
8403   "Do incremental search forward on current article."
8404   (interactive)
8405   (gnus-set-global-variables)
8406   (gnus-summary-select-article)
8407   (gnus-eval-in-buffer-window 
8408    gnus-article-buffer (isearch-forward)))
8409
8410 (defun gnus-summary-search-article-forward (regexp &optional backward)
8411   "Search for an article containing REGEXP forward.
8412 If BACKWARD, search backward instead."
8413   (interactive
8414    (list (read-string
8415           (format "Search article %s (regexp%s): "
8416                   (if current-prefix-arg "backward" "forward")
8417                   (if gnus-last-search-regexp
8418                       (concat ", default " gnus-last-search-regexp)
8419                     "")))
8420          current-prefix-arg))
8421   (gnus-set-global-variables)
8422   (if (string-equal regexp "")
8423       (setq regexp (or gnus-last-search-regexp ""))
8424     (setq gnus-last-search-regexp regexp))
8425   (if (gnus-summary-search-article regexp backward)
8426       (gnus-article-set-window-start 
8427        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8428     (error "Search failed: \"%s\"" regexp)))
8429
8430 (defun gnus-summary-search-article-backward (regexp)
8431   "Search for an article containing REGEXP backward."
8432   (interactive
8433    (list (read-string
8434           (format "Search article backward (regexp%s): "
8435                   (if gnus-last-search-regexp
8436                       (concat ", default " gnus-last-search-regexp)
8437                     "")))))
8438   (gnus-summary-search-article-forward regexp 'backward))
8439
8440 (defun gnus-summary-search-article (regexp &optional backward)
8441   "Search for an article containing REGEXP.
8442 Optional argument BACKWARD means do search for backward.
8443 gnus-select-article-hook is not called during the search."
8444   (let ((gnus-select-article-hook nil)  ;Disable hook.
8445         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8446         (re-search
8447          (if backward
8448              (function re-search-backward) (function re-search-forward)))
8449         (found nil)
8450         (last nil))
8451     ;; Hidden thread subtrees must be searched for ,too.
8452     (gnus-summary-show-all-threads)
8453     (if (eobp) (forward-line -1))
8454     ;; First of all, search current article.
8455     ;; We don't want to read article again from NNTP server nor reset
8456     ;; current point.
8457     (gnus-summary-select-article)
8458     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8459     (setq last gnus-current-article)
8460     (gnus-eval-in-buffer-window gnus-article-buffer
8461       (save-restriction
8462         (widen)
8463         ;; Begin search from current point.
8464         (setq found (funcall re-search regexp nil t))))
8465     ;; Then search next articles.
8466     (while (and (not found)
8467                 (gnus-summary-display-article 
8468                  (gnus-summary-search-subject backward nil nil)))
8469       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8470       (gnus-eval-in-buffer-window gnus-article-buffer
8471         (save-restriction
8472           (widen)
8473           (goto-char (if backward (point-max) (point-min)))
8474           (setq found (funcall re-search regexp nil t)))))
8475     (message "")
8476     ;; Adjust article pointer.
8477     (or (eq last gnus-current-article)
8478         (setq gnus-last-article last))
8479     ;; Return T if found such article.
8480     found))
8481
8482 (defun gnus-summary-execute-command (header regexp command &optional backward)
8483   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8484 If HEADER is an empty string (or nil), the match is done on the entire
8485 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8486   (interactive
8487    (list (let ((completion-ignore-case t))
8488            (completing-read 
8489             "Header name: "
8490             (mapcar (lambda (string) (list string))
8491                     '("Number" "Subject" "From" "Lines" "Date"
8492                       "Message-ID" "Xref" "References"))
8493             nil 'require-match))
8494          (read-string "Regexp: ")
8495          (read-key-sequence "Command: ")
8496          current-prefix-arg))
8497   (gnus-set-global-variables)
8498   ;; Hidden thread subtrees must be searched as well.
8499   (gnus-summary-show-all-threads)
8500   ;; We don't want to change current point nor window configuration.
8501   (save-excursion
8502     (save-window-excursion
8503       (gnus-message 6 "Executing %s..." (key-description command))
8504       ;; We'd like to execute COMMAND interactively so as to give arguments.
8505       (gnus-execute header regexp
8506                     (` (lambda ()
8507                          (call-interactively '(, (key-binding command)))))
8508                     backward)
8509       (gnus-message 6 "Executing %s...done" (key-description command)))))
8510
8511 (defun gnus-summary-beginning-of-article ()
8512   "Scroll the article back to the beginning."
8513   (interactive)
8514   (gnus-set-global-variables)
8515   (gnus-summary-select-article)
8516   (gnus-configure-windows 'article)
8517   (gnus-eval-in-buffer-window
8518    gnus-article-buffer
8519    (widen)
8520    (goto-char (point-min))
8521    (and gnus-break-pages (gnus-narrow-to-page))))
8522
8523 (defun gnus-summary-end-of-article ()
8524   "Scroll to the end of the article."
8525   (interactive)
8526   (gnus-set-global-variables)
8527   (gnus-summary-select-article)
8528   (gnus-configure-windows 'article)
8529   (gnus-eval-in-buffer-window 
8530    gnus-article-buffer
8531    (widen)
8532    (goto-char (point-max))
8533    (and gnus-break-pages (gnus-narrow-to-page))))
8534
8535 (defun gnus-summary-show-article ()
8536   "Force re-fetching of the current article."
8537   (interactive)
8538   (gnus-set-global-variables)
8539   (or gnus-current-article
8540       (error "There is no current article"))
8541   (gnus-summary-goto-subject gnus-current-article)
8542   (gnus-summary-select-article nil 'force)
8543   (gnus-configure-windows 'article)
8544   (gnus-summary-position-cursor))
8545
8546 (defun gnus-summary-verbose-headers (&optional arg)
8547   "Toggle permanent full header display.
8548 If ARG is a positive number, turn header display on.
8549 If ARG is a negative number, turn header display off."
8550   (interactive "P")
8551   (gnus-set-global-variables)
8552   (gnus-summary-toggle-header arg)
8553   (setq gnus-show-all-headers
8554         (cond ((or (not (numberp arg))
8555                    (zerop arg))
8556                (not gnus-show-all-headers))
8557               ((natnump arg)
8558                t))))
8559
8560 (defun gnus-summary-toggle-header (&optional arg)
8561   "Show the headers if they are hidden, or hide them if they are shown.
8562 If ARG is a positive number, show the entire header.
8563 If ARG is a negative number, hide the unwanted header lines."
8564   (interactive "P")
8565   (gnus-set-global-variables)
8566   (save-excursion
8567     (set-buffer gnus-article-buffer)
8568     (let ((buffer-read-only nil))
8569       (if (numberp arg) 
8570           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8571                                                 gnus-hidden-properties)
8572             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8573         (if (text-property-any (point-min) (point-max) 'invisible t)
8574             (remove-text-properties 
8575              (point-min) (point-max) gnus-hidden-properties)
8576           ;; We hide the headers. This song and dance act below is
8577           ;; done because `gnus-have-all-headers' is buffer-local to
8578           ;; the summary buffer, and we only want to temporarily
8579           ;; change it in that buffer. Ugh.
8580           (let ((have gnus-have-all-headers))
8581             (save-excursion
8582               (set-buffer gnus-summary-buffer)
8583               (setq gnus-have-all-headers nil)
8584               (save-excursion
8585                 (set-buffer gnus-article-buffer)
8586                 (run-hooks 'gnus-article-display-hook))
8587               (setq gnus-have-all-headers have)))))
8588       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8589
8590 (defun gnus-summary-show-all-headers ()
8591   "Make all header lines visible."
8592   (interactive)
8593   (gnus-set-global-variables)
8594   (gnus-article-show-all-headers))
8595
8596 (defun gnus-summary-toggle-mime (&optional arg)
8597   "Toggle MIME processing.
8598 If ARG is a positive number, turn MIME processing on."
8599   (interactive "P")
8600   (gnus-set-global-variables)
8601   (setq gnus-show-mime
8602         (if (null arg) (not gnus-show-mime)
8603           (> (prefix-numeric-value arg) 0)))
8604   (gnus-summary-select-article t 'force))
8605
8606 (defun gnus-summary-caesar-message (&optional arg)
8607   "Caesar rotate the current article by 13.
8608 The numerical prefix specifies how manu places to rotate each letter
8609 forward."
8610   (interactive "P")
8611   (gnus-set-global-variables)
8612   (gnus-summary-select-article)
8613   (let ((mail-header-separator ""))
8614     (gnus-eval-in-buffer-window 
8615      gnus-article-buffer
8616      (save-restriction
8617        (widen)
8618        (let ((start (window-start)))
8619          (news-caesar-buffer-body arg)
8620          (set-window-start (get-buffer-window (current-buffer)) start))))))
8621
8622 (defun gnus-summary-stop-page-breaking ()
8623   "Stop page breaking in the current article."
8624   (interactive)
8625   (gnus-set-global-variables)
8626   (gnus-summary-select-article)
8627   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8628
8629 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8630
8631 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8632   "Move the current article to a different newsgroup.
8633 If N is a positive number, move the N next articles.
8634 If N is a negative number, move the N previous articles.
8635 If N is nil and any articles have been marked with the process mark,
8636 move those articles instead.
8637 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8638 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8639 re-spool using this method.
8640 For this function to work, both the current newsgroup and the
8641 newsgroup that you want to move to have to support the `request-move'
8642 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8643   (interactive "P")
8644   (gnus-set-global-variables)
8645   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8646       (error "The current newsgroup does not support article moving"))
8647   (let ((articles (gnus-summary-work-articles n))
8648         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8649         art-group to-method sel-met)
8650     (if (and (not to-newsgroup) (not select-method))
8651         (setq to-newsgroup
8652               (completing-read 
8653                (format "Where do you want to move %s? %s"
8654                        (if (> (length articles) 1)
8655                            (format "these %d articles" (length articles))
8656                          "this article")
8657                        (if gnus-current-move-group
8658                            (format "(%s default) " gnus-current-move-group)
8659                          ""))
8660                gnus-active-hashtb nil nil prefix)))
8661     (if to-newsgroup
8662         (progn
8663           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8664               (setq to-newsgroup (or gnus-current-move-group "")))
8665           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8666               (gnus-activate-group to-newsgroup)
8667               (error "No such group: %s" to-newsgroup))
8668           (setq gnus-current-move-group to-newsgroup)))
8669     (setq to-method (if select-method (list select-method "")
8670                       (gnus-find-method-for-group to-newsgroup)))
8671     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8672         (error "%s does not support article copying" (car to-method)))
8673     (or (gnus-check-server to-method)
8674         (error "Can't open server %s" (car to-method)))
8675     (gnus-message 6 "Moving to %s: %s..." 
8676                   (or select-method to-newsgroup) articles)
8677     (while articles
8678       (if (setq art-group
8679                 (gnus-request-move-article 
8680                  (car articles)                   ; Article to move
8681                  gnus-newsgroup-name              ; From newsgrouo
8682                  (nth 1 (gnus-find-method-for-group 
8683                          gnus-newsgroup-name))    ; Server
8684                  (list 'gnus-request-accept-article 
8685                        (if select-method
8686                            (list 'quote select-method)
8687                          to-newsgroup)
8688                        (not (cdr articles)))     ; Accept form
8689                  (not (cdr articles))))          ; Only save nov last time
8690           (let* ((buffer-read-only nil)
8691                  (entry 
8692                   (or
8693                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8694                    (gnus-gethash 
8695                     (gnus-group-prefixed-name 
8696                      (car art-group) 
8697                      (if select-method (list select-method "")
8698                        (gnus-find-method-for-group to-newsgroup)))
8699                     gnus-newsrc-hashtb)))
8700                  (info (nth 2 entry))
8701                  (article (car articles)))
8702             (gnus-summary-goto-subject article)
8703             (beginning-of-line)
8704             (delete-region (point) (progn (forward-line 1) (point)))
8705             ;; Update the group that has been moved to.
8706             (if (not info)
8707                 ()                      ; This group does not exist yet.
8708               (if (not (memq article gnus-newsgroup-unreads))
8709                   (setcar (cdr (cdr info))
8710                           (gnus-add-to-range (nth 2 info) 
8711                                              (list (cdr art-group)))))
8712               ;; Copy any marks over to the new group.
8713               (let ((marks '((tick . gnus-newsgroup-marked)
8714                              (dormant . gnus-newsgroup-dormant)
8715                              (expire . gnus-newsgroup-expirable)
8716                              (bookmark . gnus-newsgroup-bookmarks)
8717                              (reply . gnus-newsgroup-replied)))
8718                     (to-article (cdr art-group)))
8719                 (while marks
8720                   (if (memq article (symbol-value (cdr (car marks))))
8721                       (gnus-add-marked-articles 
8722                        (car info) (car (car marks)) (list to-article) info))
8723                   (setq marks (cdr marks)))))
8724             ;; Update marks.
8725             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8726             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8727             (setq gnus-newsgroup-dormant
8728                   (delq article gnus-newsgroup-dormant))
8729             (setq gnus-newsgroup-reads
8730                   (cons (cons article gnus-canceled-mark)
8731                         gnus-newsgroup-reads)))
8732         (gnus-message 1 "Couldn't move article %s" (car articles)))
8733       (gnus-summary-remove-process-mark (car articles))
8734       (setq articles (cdr articles)))))
8735
8736 (defun gnus-summary-respool-article (&optional n respool-method)
8737   "Respool the current article.
8738 The article will be squeezed through the mail spooling process again,
8739 which means that it will be put in some mail newsgroup or other
8740 depending on `nnmail-split-methods'.
8741 If N is a positive number, respool the N next articles.
8742 If N is a negative number, respool the N previous articles.
8743 If N is nil and any articles have been marked with the process mark,
8744 respool those articles instead.
8745
8746 Respooling can be done both from mail groups and \"real\" newsgroups.
8747 In the former case, the articles in question will be moved from the
8748 current group into whatever groups they are destined to.  In the
8749 latter case, they will be copied into the relevant groups."
8750   (interactive "P")
8751   (gnus-set-global-variables)
8752   (let ((respool-methods (gnus-methods-using 'respool))
8753         (methname 
8754          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8755     (or respool-method
8756         (setq respool-method
8757               (completing-read
8758                "What method do you want to use when respooling? "
8759                respool-methods nil t methname)))
8760     (or (string= respool-method "")
8761         (if (assoc (symbol-name
8762                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8763                    respool-methods)
8764             (gnus-summary-move-article n nil (intern respool-method))
8765           (gnus-summary-copy-article n nil (intern respool-method))))))
8766
8767 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8768 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8769   "Move the current article to a different newsgroup.
8770 If N is a positive number, move the N next articles.
8771 If N is a negative number, move the N previous articles.
8772 If N is nil and any articles have been marked with the process mark,
8773 move those articles instead.
8774 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8775 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8776 re-spool using this method.
8777 For this function to work, the newsgroup that you want to move to have
8778 to support the `request-move' and `request-accept'
8779 functions. (Ie. mail newsgroups at present.)"
8780   (interactive "P")
8781   (gnus-set-global-variables)
8782   (let ((articles (gnus-summary-work-articles n))
8783         (copy-buf (get-buffer-create "*copy work*"))
8784         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8785         art-group to-method)
8786     (buffer-disable-undo copy-buf)
8787     (if (and (not to-newsgroup) (not select-method))
8788         (setq to-newsgroup
8789               (completing-read 
8790                (format "Where do you want to copy %s? %s"
8791                        (if (> (length articles) 1)
8792                            (format "these %d articles" (length articles))
8793                          "this article")
8794                        (if gnus-current-move-group
8795                            (format "(%s default) " gnus-current-move-group)
8796                          ""))
8797                gnus-active-hashtb nil nil prefix)))
8798     (if to-newsgroup
8799         (progn
8800           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8801               (setq to-newsgroup (or gnus-current-move-group "")))
8802           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8803               (gnus-activate-group to-newsgroup)
8804               (error "No such group: %s" to-newsgroup))
8805           (setq gnus-current-move-group to-newsgroup)))
8806     (setq to-method (if select-method (list select-method "")
8807                       (gnus-find-method-for-group to-newsgroup)))
8808     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8809         (error "%s does not support article copying" (car to-method)))
8810     (or (gnus-check-server to-method)
8811         (error "Can't open server %s" (car to-method)))
8812     (while articles
8813       (gnus-message 6 "Copying to %s: %s..." 
8814                     (or select-method to-newsgroup) articles)
8815       (if (setq art-group
8816                 (save-excursion
8817                   (set-buffer copy-buf)
8818                   (gnus-request-article-this-buffer
8819                    (car articles) gnus-newsgroup-name)
8820                   (gnus-request-accept-article
8821                    (if select-method (quote select-method) to-newsgroup)
8822                    (not (cdr articles)))))
8823           (let* ((entry 
8824                   (or
8825                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8826                    (gnus-gethash 
8827                     (gnus-group-prefixed-name 
8828                      (car art-group) 
8829                      (if select-method (list select-method "")
8830                        (gnus-find-method-for-group to-newsgroup)))
8831                     gnus-newsrc-hashtb)))
8832                  (info (nth 2 entry))
8833                  (article (car articles)))
8834             ;; We copy the info over to the new group.
8835             (if (not info)
8836                 ()                      ; This group does not exist (yet).
8837               (if (not (memq article gnus-newsgroup-unreads))
8838                   (setcar (cdr (cdr info))
8839                           (gnus-add-to-range (nth 2 info) 
8840                                              (list (cdr art-group)))))
8841               ;; Copy any marks over to the new group.
8842               (let ((marks '((tick . gnus-newsgroup-marked)
8843                              (dormant . gnus-newsgroup-dormant)
8844                              (expire . gnus-newsgroup-expirable)
8845                              (bookmark . gnus-newsgroup-bookmarks)
8846                              (reply . gnus-newsgroup-replied)))
8847                     (to-article (cdr art-group)))
8848                 (while marks
8849                   (if (memq article (symbol-value (cdr (car marks))))
8850                       (gnus-add-marked-articles 
8851                        (car info) (car (car marks)) (list to-article) info))
8852                   (setq marks (cdr marks))))))
8853         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8854       (gnus-summary-remove-process-mark (car articles))
8855       (setq articles (cdr articles)))
8856     (kill-buffer copy-buf)))
8857
8858 (defun gnus-summary-import-article (file)
8859   "Import a random file into a mail newsgroup."
8860   (interactive "fImport file: ")
8861   (let ((group gnus-newsgroup-name)
8862         atts)
8863     (or (gnus-check-backend-function 'request-accept-article group)
8864         (error "%s does not support article importing" group))
8865     (or (file-readable-p file)
8866         (not (file-regular-p file))
8867         (error "Can't read %s" file))
8868     (save-excursion
8869       (set-buffer (get-buffer-create " *import file*"))
8870       (buffer-disable-undo (current-buffer))
8871       (erase-buffer)
8872       (insert-file-contents file)
8873       (goto-char (point-min))
8874       (if (nnheader-article-p)
8875           ()
8876         (setq atts (file-attributes file))
8877         (insert "From: " (read-string "From: ") "\n"
8878                 "Subject: " (read-string "Subject: ") "\n"
8879                 "Date: " (current-time-string (nth 5 atts)) "\n"
8880                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8881       (gnus-request-accept-article group t)
8882       (kill-buffer (current-buffer)))))
8883
8884 (defun gnus-summary-expire-articles ()
8885   "Expire all articles that are marked as expirable in the current group."
8886   (interactive)
8887   (if (not (gnus-check-backend-function 
8888             'request-expire-articles gnus-newsgroup-name))
8889       ()
8890     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8891                                       gnus-newsrc-hashtb)))
8892            (total (memq 'total-expire (nth 5 info)))
8893            (expirable (if total
8894                           (gnus-list-of-read-articles gnus-newsgroup-name)
8895                         (setq gnus-newsgroup-expirable
8896                               (sort gnus-newsgroup-expirable '<))))
8897            es)
8898       (if (not expirable)
8899           ()
8900         (gnus-message 6 "Expiring articles...")
8901         ;; The list of articles that weren't expired is returned.
8902         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8903         (or total (setq gnus-newsgroup-expirable es))
8904         ;; We go through the old list of expirable, and mark all
8905         ;; really expired articles as non-existant.
8906         (or (eq es expirable) ; If nothing was expired, we don't mark.
8907             (let ((gnus-use-cache nil))
8908               (while expirable
8909                 (or (memq (car expirable) es)
8910                     (gnus-summary-mark-article
8911                      (car expirable) gnus-canceled-mark))
8912                 (setq expirable (cdr expirable)))))
8913         (gnus-message 6 "Expiring articles...done")))))
8914
8915 (defun gnus-summary-expire-articles-now ()
8916   "Expunge all expirable articles in the current group.
8917 This means that *all* articles that are marked as expirable will be
8918 deleted forever, right now."
8919   (interactive)
8920   (or gnus-expert-user
8921       (gnus-y-or-n-p
8922        "Are you really, really, really sure you want to expunge? ")
8923       (error "Phew!"))
8924   (let ((nnmail-expiry-wait -1)
8925         (nnmail-expiry-wait-function nil))
8926     (gnus-summary-expire-articles)))
8927
8928 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8929 (defun gnus-summary-delete-article (&optional n)
8930   "Delete the N next (mail) articles.
8931 This command actually deletes articles. This is not a marking
8932 command. The article will disappear forever from you life, never to
8933 return. 
8934 If N is negative, delete backwards.
8935 If N is nil and articles have been marked with the process mark,
8936 delete these instead."
8937   (interactive "P")
8938   (or (gnus-check-backend-function 'request-expire-articles 
8939                                    gnus-newsgroup-name)
8940       (error "The current newsgroup does not support article deletion."))
8941   ;; Compute the list of articles to delete.
8942   (let ((articles (gnus-summary-work-articles n))
8943         not-deleted)
8944     (if (and gnus-novice-user
8945              (not (gnus-y-or-n-p 
8946                    (format "Do you really want to delete %s forever? "
8947                            (if (> (length articles) 1) "these articles"
8948                              "this article")))))
8949         ()
8950       ;; Delete the articles.
8951       (setq not-deleted (gnus-request-expire-articles 
8952                          articles gnus-newsgroup-name 'force))
8953       (while articles
8954         (gnus-summary-remove-process-mark (car articles))       
8955         ;; The backend might not have been able to delete the article
8956         ;; after all.  
8957         (or (memq (car articles) not-deleted)
8958             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8959         (setq articles (cdr articles))))
8960     (gnus-summary-position-cursor)
8961     not-deleted))
8962
8963 (defun gnus-summary-edit-article ()
8964   "Enter into a buffer and edit the current article.
8965 This will have permanent effect only in mail groups."
8966   (interactive)
8967   (or (gnus-check-backend-function 
8968        'request-replace-article gnus-newsgroup-name)
8969       (error "The current newsgroup does not support article editing."))
8970   (gnus-summary-select-article t)
8971   (gnus-configure-windows 'article)
8972   (select-window (get-buffer-window gnus-article-buffer))
8973   (gnus-message 6 "C-c C-c to end edits")
8974   (setq buffer-read-only nil)
8975   (text-mode)
8976   (use-local-map (copy-keymap (current-local-map)))
8977   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8978   (buffer-enable-undo)
8979   (widen)
8980   (goto-char (point-min))
8981   (search-forward "\n\n" nil t))
8982
8983 (defun gnus-summary-edit-article-done ()
8984   "Make edits to the current article permanent."
8985   (interactive)
8986   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8987     (erase-buffer)
8988     (insert buf)
8989     (if (not (gnus-request-replace-article 
8990               (cdr gnus-article-current) (car gnus-article-current) 
8991               (current-buffer)))
8992         (error "Couldn't replace article.")
8993       (gnus-article-mode)
8994       (use-local-map gnus-article-mode-map)
8995       (setq buffer-read-only t)
8996       (buffer-disable-undo (current-buffer))
8997       (gnus-configure-windows 'summary))
8998       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))))
8999
9000 (defun gnus-summary-edit-article-postpone ()
9001   "Postpone changes to the current article."
9002   (interactive)
9003   (gnus-article-mode)
9004   (use-local-map gnus-article-mode-map)
9005   (setq buffer-read-only t)
9006   (buffer-disable-undo (current-buffer))
9007   (gnus-configure-windows 'summary)
9008   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9009
9010 (defun gnus-summary-fancy-query ()
9011   "Query where the fancy respool algorithm would put this article."
9012   (interactive)
9013   (gnus-summary-select-article)
9014   (save-excursion
9015     (set-buffer gnus-article-buffer)
9016     (save-restriction
9017       (goto-char (point-min))
9018       (search-forward "\n\n")
9019       (narrow-to-region (point-min) (point))
9020       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9021
9022 ;; Summary score commands.
9023
9024 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9025
9026 (defun gnus-summary-raise-score (n)
9027   "Raise the score of the current article by N."
9028   (interactive "p")
9029   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9030
9031 (defun gnus-summary-set-score (n)
9032   "Set the score of the current article to N."
9033   (interactive "p")
9034   ;; Skip dummy header line.
9035   (save-excursion
9036     (gnus-summary-show-thread)
9037     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9038         (forward-line 1))
9039     (let ((buffer-read-only nil))
9040       ;; Set score.
9041       (gnus-summary-update-mark
9042        (if (= n (or gnus-summary-default-score 0)) ? 
9043          (if (< n (or gnus-summary-default-score 0)) 
9044              gnus-score-below-mark gnus-score-over-mark)) 'score))
9045     (let* ((article (gnus-summary-article-number))
9046            (score (assq article gnus-newsgroup-scored)))
9047       (if score (setcdr score n)
9048         (setq gnus-newsgroup-scored 
9049               (cons (cons article n) gnus-newsgroup-scored))))
9050     (gnus-summary-update-line)))
9051
9052 (defun gnus-summary-current-score ()
9053   "Return the score of the current article."
9054   (interactive)
9055   (message "%s" (gnus-summary-article-score)))
9056
9057 ;; Summary marking commands.
9058
9059 (defun gnus-summary-raise-same-subject-and-select (score)
9060   "Raise articles which has the same subject with SCORE and select the next."
9061   (interactive "p")
9062   (let ((subject (gnus-summary-subject-string)))
9063     (gnus-summary-raise-score score)
9064     (while (gnus-summary-search-subject nil nil subject)
9065       (gnus-summary-raise-score score))
9066     (gnus-summary-next-article t)))
9067
9068 (defun gnus-summary-raise-same-subject (score)
9069   "Raise articles which has the same subject with SCORE."
9070   (interactive "p")
9071   (let ((subject (gnus-summary-subject-string)))
9072     (gnus-summary-raise-score score)
9073     (while (gnus-summary-search-subject nil nil subject)
9074       (gnus-summary-raise-score score))
9075     (gnus-summary-next-subject 1 t)))
9076
9077 (defun gnus-score-default (level)
9078   (if level (prefix-numeric-value level) 
9079     gnus-score-interactive-default-score))
9080
9081 (defun gnus-summary-raise-thread (&optional score)
9082   "Raise the score of the articles in the current thread with SCORE."
9083   (interactive "P")
9084   (setq score (1- (gnus-score-default score)))
9085   (let (e)
9086     (save-excursion
9087       (let ((level (gnus-summary-thread-level)))
9088         (gnus-summary-raise-score score)
9089         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9090                     (> (gnus-summary-thread-level) level))
9091           (gnus-summary-raise-score score))
9092         (setq e (point))))
9093     (let ((gnus-summary-check-current t))
9094       (or (zerop (gnus-summary-next-subject 1 t))
9095           (goto-char e))))
9096   (gnus-summary-recenter)
9097   (gnus-summary-position-cursor)
9098   (gnus-set-mode-line 'summary))
9099
9100 (defun gnus-summary-lower-same-subject-and-select (score)
9101   "Raise articles which has the same subject with SCORE and select the next."
9102   (interactive "p")
9103   (gnus-summary-raise-same-subject-and-select (- score)))
9104
9105 (defun gnus-summary-lower-same-subject (score)
9106   "Raise articles which has the same subject with SCORE."
9107   (interactive "p")
9108   (gnus-summary-raise-same-subject (- score)))
9109
9110 (defun gnus-summary-lower-thread (&optional score)
9111   "Lower score of articles in the current thread with SCORE."
9112   (interactive "P")
9113   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9114
9115 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9116   "Mark articles which has the same subject as read, and then select the next.
9117 If UNMARK is positive, remove any kind of mark.
9118 If UNMARK is negative, tick articles."
9119   (interactive "P")
9120   (if unmark
9121       (setq unmark (prefix-numeric-value unmark)))
9122   (let ((count
9123          (gnus-summary-mark-same-subject
9124           (gnus-summary-subject-string) unmark)))
9125     ;; Select next unread article. If auto-select-same mode, should
9126     ;; select the first unread article.
9127     (gnus-summary-next-article t (and gnus-auto-select-same
9128                                       (gnus-summary-subject-string)))
9129     (gnus-message 7 "%d article%s marked as %s"
9130                   count (if (= count 1) " is" "s are")
9131                   (if unmark "unread" "read"))))
9132
9133 (defun gnus-summary-kill-same-subject (&optional unmark)
9134   "Mark articles which has the same subject as read. 
9135 If UNMARK is positive, remove any kind of mark.
9136 If UNMARK is negative, tick articles."
9137   (interactive "P")
9138   (if unmark
9139       (setq unmark (prefix-numeric-value unmark)))
9140   (let ((count
9141          (gnus-summary-mark-same-subject
9142           (gnus-summary-subject-string) unmark)))
9143     ;; If marked as read, go to next unread subject.
9144     (if (null unmark)
9145         ;; Go to next unread subject.
9146         (gnus-summary-next-subject 1 t))
9147     (gnus-message 7 "%d articles are marked as %s"
9148                   count (if unmark "unread" "read"))))
9149
9150 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9151   "Mark articles with same SUBJECT as read, and return marked number.
9152 If optional argument UNMARK is positive, remove any kinds of marks.
9153 If optional argument UNMARK is negative, mark articles as unread instead."
9154   (let ((count 1))
9155     (save-excursion
9156       (cond 
9157        ((null unmark)                   ; Mark as read.
9158         (while (and 
9159                 (progn
9160                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9161                   (gnus-summary-show-thread) t)
9162                 (gnus-summary-search-forward nil subject))
9163           (setq count (1+ count))))
9164        ((> unmark 0)                    ; Tick.
9165         (while (and
9166                 (progn
9167                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9168                   (gnus-summary-show-thread) t)
9169                 (gnus-summary-search-forward nil subject))
9170           (setq count (1+ count))))
9171        (t                               ; Mark as unread.
9172         (while (and
9173                 (progn
9174                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9175                   (gnus-summary-show-thread) t)
9176                 (gnus-summary-search-forward nil subject))
9177           (setq count (1+ count)))))
9178       (gnus-set-mode-line 'summary)
9179       ;; Return the number of marked articles.
9180       count)))
9181
9182 (defun gnus-summary-mark-as-processable (n &optional unmark)
9183   "Set the process mark on the next N articles.
9184 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9185 the process mark instead.  The difference between N and the actual
9186 number of articles marked is returned."
9187   (interactive "p")
9188   (let ((backward (< n 0))
9189         (n (abs n)))
9190   (while (and 
9191           (> n 0)
9192           (if unmark
9193               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9194             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9195           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9196     (setq n (1- n)))
9197   (if (/= 0 n) (gnus-message 7 "No more articles"))
9198   (gnus-summary-recenter)
9199   (gnus-summary-position-cursor)
9200   n))
9201
9202 (defun gnus-summary-unmark-as-processable (n)
9203   "Remove the process mark from the next N articles.
9204 If N is negative, mark backward instead.  The difference between N and
9205 the actual number of articles marked is returned."
9206   (interactive "p")
9207   (gnus-summary-mark-as-processable n t))
9208
9209 (defun gnus-summary-unmark-all-processable ()
9210   "Remove the process mark from all articles."
9211   (interactive)
9212   (save-excursion
9213     (while gnus-newsgroup-processable
9214       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9215   (gnus-summary-position-cursor))
9216
9217 (defun gnus-summary-mark-as-expirable (n)
9218   "Mark N articles forward as expirable.
9219 If N is negative, mark backward instead. The difference between N and
9220 the actual number of articles marked is returned."
9221   (interactive "p")
9222   (gnus-summary-mark-forward n gnus-expirable-mark))
9223
9224 (defun gnus-summary-mark-article-as-replied (article)
9225   "Mark ARTICLE replied and update the summary line."
9226   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9227   (let ((buffer-read-only nil))
9228     (if (gnus-summary-goto-subject article)
9229         (progn
9230           (gnus-summary-update-mark gnus-replied-mark 'replied)
9231           t))))
9232
9233 (defun gnus-summary-set-bookmark (article)
9234   "Set a bookmark in current article."
9235   (interactive (list (gnus-summary-article-number)))
9236   (if (or (not (get-buffer gnus-article-buffer))
9237           (not gnus-current-article)
9238           (not gnus-article-current)
9239           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9240       (error "No current article selected"))
9241   ;; Remove old bookmark, if one exists.
9242   (let ((old (assq article gnus-newsgroup-bookmarks)))
9243     (if old (setq gnus-newsgroup-bookmarks 
9244                   (delq old gnus-newsgroup-bookmarks))))
9245   ;; Set the new bookmark, which is on the form 
9246   ;; (article-number . line-number-in-body).
9247   (setq gnus-newsgroup-bookmarks 
9248         (cons 
9249          (cons article 
9250                (save-excursion
9251                  (set-buffer gnus-article-buffer)
9252                  (count-lines
9253                   (min (point)
9254                        (save-excursion
9255                          (goto-char (point-min))
9256                          (search-forward "\n\n" nil t)
9257                          (point)))
9258                   (point))))
9259          gnus-newsgroup-bookmarks))
9260   (gnus-message 6 "A bookmark has been added to the current article."))
9261
9262 (defun gnus-summary-remove-bookmark (article)
9263   "Remove the bookmark from the current article."
9264   (interactive (list (gnus-summary-article-number)))
9265   ;; Remove old bookmark, if one exists.
9266   (let ((old (assq article gnus-newsgroup-bookmarks)))
9267     (if old 
9268         (progn
9269           (setq gnus-newsgroup-bookmarks 
9270                 (delq old gnus-newsgroup-bookmarks))
9271           (gnus-message 6 "Removed bookmark."))
9272       (gnus-message 6 "No bookmark in current article."))))
9273
9274 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9275 (defun gnus-summary-mark-as-dormant (n)
9276   "Mark N articles forward as dormant.
9277 If N is negative, mark backward instead.  The difference between N and
9278 the actual number of articles marked is returned."
9279   (interactive "p")
9280   (gnus-summary-mark-forward n gnus-dormant-mark))
9281
9282 (defun gnus-summary-set-process-mark (article)
9283   "Set the process mark on ARTICLE and update the summary line."
9284   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9285   (let ((buffer-read-only nil))
9286     (if (gnus-summary-goto-subject article)
9287         (progn
9288           (gnus-summary-show-thread)
9289           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9290                (forward-line 1))
9291           (gnus-summary-update-mark gnus-process-mark 'replied)
9292           t))))
9293
9294 (defun gnus-summary-remove-process-mark (article)
9295   "Remove the process mark from ARTICLE and update the summary line."
9296   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9297   (let ((buffer-read-only nil))
9298     (if (gnus-summary-goto-subject article)
9299         (progn
9300           (gnus-summary-show-thread)
9301           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9302                (forward-line 1))
9303           (gnus-summary-update-mark ?  'replied)
9304           (if (memq article gnus-newsgroup-replied) 
9305               (gnus-summary-update-mark gnus-replied-mark 'replied))
9306           t))))
9307
9308 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9309   "Mark N articles as read forwards.
9310 If N is negative, mark backwards instead.
9311 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9312 marked as unread. 
9313 The difference between N and the actual number of articles marked is
9314 returned."
9315   (interactive "p")
9316   (gnus-set-global-variables)
9317   (let ((backward (< n 0))
9318         (gnus-summary-goto-unread
9319          (and gnus-summary-goto-unread
9320               (not (memq mark (list gnus-unread-mark
9321                                     gnus-ticked-mark gnus-dormant-mark)))))
9322         (n (abs n))
9323         (mark (or mark gnus-del-mark)))
9324   (while (and (> n 0)
9325               (gnus-summary-mark-article nil mark no-expire)
9326               (zerop (gnus-summary-next-subject 
9327                       (if backward -1 1) gnus-summary-goto-unread t)))
9328     (setq n (1- n)))
9329   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9330   (gnus-summary-recenter)
9331   (gnus-summary-position-cursor)
9332   (gnus-set-mode-line 'summary)
9333   n))
9334
9335 (defun gnus-summary-mark-article-as-read (mark)
9336   "Mark the current article quickly as read with MARK."
9337   (let ((article (gnus-summary-article-number)))
9338     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9339     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9340     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9341     (setq gnus-newsgroup-reads
9342           (cons (cons article mark) gnus-newsgroup-reads))
9343     ;; Possibly remove from cache, if that is used. 
9344     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9345     (and gnus-newsgroup-auto-expire 
9346          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9347              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9348              (= mark gnus-read-mark))
9349          (progn
9350            (setq mark gnus-expirable-mark)
9351            (setq gnus-newsgroup-expirable 
9352                  (cons article gnus-newsgroup-expirable))))
9353     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9354       (forward-line 1))
9355     ;; Fix the mark.
9356     (gnus-summary-update-mark mark 'unread)
9357     t))
9358
9359 (defun gnus-summary-mark-article-as-unread (mark)
9360   "Mark the current article quickly as unread with MARK."
9361   (let ((article (gnus-summary-article-number)))
9362     (or (memq article gnus-newsgroup-unreads)
9363         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9364     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9365     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9366     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9367     (setq gnus-newsgroup-reads
9368           (delq (assq article gnus-newsgroup-reads)
9369                 gnus-newsgroup-reads))
9370     (if (= mark gnus-ticked-mark)
9371         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9372     (if (= mark gnus-dormant-mark)
9373         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9374
9375     ;; See whether the article is to be put in the cache.
9376     (and gnus-use-cache
9377          (vectorp (gnus-get-header-by-num article))
9378          (save-excursion
9379            (gnus-cache-possibly-enter-article 
9380             gnus-newsgroup-name article 
9381             (gnus-get-header-by-num article)
9382             (= mark gnus-ticked-mark)
9383             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9384
9385     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9386       (forward-line 1))
9387     ;; Fix the mark.
9388     (gnus-summary-update-mark mark 'unread)
9389     t))
9390
9391 (defun gnus-summary-mark-article (&optional article mark no-expire)
9392   "Mark ARTICLE with MARK.  MARK can be any character.
9393 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9394 (dormant) and `?E' (expirable).
9395 If MARK is nil, then the default character `?D' is used.
9396 If ARTICLE is nil, then the article on the current line will be
9397 marked." 
9398   (and (stringp mark)
9399        (setq mark (aref mark 0)))
9400   ;; If no mark is given, then we check auto-expiring.
9401   (and (not no-expire)
9402        gnus-newsgroup-auto-expire 
9403        (or (not mark)
9404            (and (numberp mark) 
9405                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9406                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9407                     (= mark gnus-read-mark))))
9408        (setq mark gnus-expirable-mark))
9409   (let* ((mark (or mark gnus-del-mark))
9410          (article (or article (gnus-summary-article-number))))
9411     (or article (error "No article on current line"))
9412     (if (or (= mark gnus-unread-mark) 
9413             (= mark gnus-ticked-mark) 
9414             (= mark gnus-dormant-mark))
9415         (gnus-mark-article-as-unread article mark)
9416       (gnus-mark-article-as-read article mark))
9417
9418     ;; See whether the article is to be put in the cache.
9419     (and gnus-use-cache
9420          (not (= mark gnus-canceled-mark))
9421          (vectorp (gnus-get-header-by-num article))
9422          (save-excursion
9423            (gnus-cache-possibly-enter-article 
9424             gnus-newsgroup-name article 
9425             (gnus-get-header-by-num article)
9426             (= mark gnus-ticked-mark)
9427             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9428
9429     (if (gnus-summary-goto-subject article)
9430         (let ((buffer-read-only nil))
9431           (gnus-summary-show-thread)
9432           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9433                (forward-line 1))
9434           ;; Fix the mark.
9435           (gnus-summary-update-mark mark 'unread)
9436           t))))
9437
9438 (defun gnus-summary-update-mark (mark type)
9439   (beginning-of-line)
9440   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9441         (buffer-read-only nil)
9442         plist)
9443     (if (not forward)
9444         ()
9445       (forward-char forward)
9446       (setq plist (text-properties-at (point)))
9447       (delete-char 1)
9448       (insert mark)
9449       (and plist (add-text-properties (1- (point)) (point) plist))
9450       (and (eq type 'unread)
9451            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9452       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9453   
9454 (defun gnus-mark-article-as-read (article &optional mark)
9455   "Enter ARTICLE in the pertinent lists and remove it from others."
9456   ;; Make the article expirable.
9457   (let ((mark (or mark gnus-del-mark)))
9458     (if (= mark gnus-expirable-mark)
9459         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9460       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9461     ;; Remove from unread and marked lists.
9462     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9463     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9464     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9465     (setq gnus-newsgroup-reads 
9466           (cons (cons article mark) gnus-newsgroup-reads))
9467     ;; Possibly remove from cache, if that is used. 
9468     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9469
9470 (defun gnus-mark-article-as-unread (article &optional mark)
9471   "Enter ARTICLE in the pertinent lists and remove it from others."
9472   (let ((mark (or mark gnus-ticked-mark)))
9473     ;; Add to unread list.
9474     (or (memq article gnus-newsgroup-unreads)
9475         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9476     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9477     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9478     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9479     (setq gnus-newsgroup-reads
9480           (delq (assq article gnus-newsgroup-reads)
9481                 gnus-newsgroup-reads))
9482     (if (= mark gnus-ticked-mark)
9483         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9484     (if (= mark gnus-dormant-mark)
9485         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9486
9487 (defalias 'gnus-summary-mark-as-unread-forward 
9488   'gnus-summary-tick-article-forward)
9489 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9490                'gnus-summary-tick-article-forward)
9491 (defun gnus-summary-tick-article-forward (n)
9492   "Tick N articles forwards.
9493 If N is negative, tick backwards instead.
9494 The difference between N and the number of articles ticked is returned."
9495   (interactive "p")
9496   (gnus-summary-mark-forward n gnus-ticked-mark))
9497
9498 (defalias 'gnus-summary-mark-as-unread-backward 
9499   'gnus-summary-tick-article-backward)
9500 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9501                'gnus-summary-tick-article-backward)
9502 (defun gnus-summary-tick-article-backward (n)
9503   "Tick N articles backwards.
9504 The difference between N and the number of articles ticked is returned."
9505   (interactive "p")
9506   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9507
9508 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9509 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9510 (defun gnus-summary-tick-article (&optional article clear-mark)
9511   "Mark current article as unread.
9512 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9513 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9514   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9515                                        gnus-ticked-mark)))
9516
9517 (defun gnus-summary-mark-as-read-forward (n)
9518   "Mark N articles as read forwards.
9519 If N is negative, mark backwards instead.
9520 The difference between N and the actual number of articles marked is
9521 returned."
9522   (interactive "p")
9523   (gnus-summary-mark-forward n gnus-del-mark t))
9524
9525 (defun gnus-summary-mark-as-read-backward (n)
9526   "Mark the N articles as read backwards.
9527 The difference between N and the actual number of articles marked is
9528 returned."
9529   (interactive "p")
9530   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9531
9532 (defun gnus-summary-mark-as-read (&optional article mark)
9533   "Mark current article as read.
9534 ARTICLE specifies the article to be marked as read.
9535 MARK specifies a string to be inserted at the beginning of the line."
9536   (gnus-summary-mark-article article mark))
9537
9538 (defun gnus-summary-clear-mark-forward (n)
9539   "Clear marks from N articles forward.
9540 If N is negative, clear backward instead.
9541 The difference between N and the number of marks cleared is returned."
9542   (interactive "p")
9543   (gnus-summary-mark-forward n gnus-unread-mark))
9544
9545 (defun gnus-summary-clear-mark-backward (n)
9546   "Clear marks from N articles backward.
9547 The difference between N and the number of marks cleared is returned."
9548   (interactive "p")
9549   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9550
9551 (defun gnus-summary-mark-unread-as-read ()
9552   "Intended to be used by `gnus-summary-mark-article-hook'."
9553   (or (memq gnus-current-article gnus-newsgroup-marked)
9554       (memq gnus-current-article gnus-newsgroup-dormant)
9555       (memq gnus-current-article gnus-newsgroup-expirable)
9556       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9557
9558 (defun gnus-summary-mark-region-as-read (point mark all)
9559   "Mark all unread articles between point and mark as read.
9560 If given a prefix, mark all articles between point and mark as read,
9561 even ticked and dormant ones."
9562   (interactive "r\nP")
9563   (save-excursion
9564     (goto-char point)
9565     (beginning-of-line)
9566     (while (and 
9567             (< (point) mark)
9568             (progn
9569               (and
9570                (or all
9571                    (and
9572                     (not (memq (gnus-summary-article-number)
9573                                gnus-newsgroup-marked))
9574                     (not (memq (gnus-summary-article-number)
9575                                gnus-newsgroup-dormant))))
9576                (gnus-summary-mark-article
9577                 (gnus-summary-article-number) gnus-del-mark))
9578               t)
9579             (zerop (forward-line 1))))))
9580
9581 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9582 (defalias 'gnus-summary-delete-marked-as-read 
9583   'gnus-summary-remove-lines-marked-as-read)
9584 (make-obsolete 'gnus-summary-delete-marked-as-read 
9585                'gnus-summary-remove-lines-marked-as-read)
9586 (defun gnus-summary-remove-lines-marked-as-read ()
9587   "Remove lines that are marked as read."
9588   (interactive)
9589   (gnus-summary-remove-lines-marked-with 
9590    (concat (mapconcat
9591             (lambda (char) (char-to-string (symbol-value char)))
9592             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9593               gnus-killed-mark gnus-kill-file-mark
9594               gnus-low-score-mark gnus-expirable-mark
9595               gnus-canceled-mark gnus-catchup-mark)
9596             ""))))
9597
9598 (defalias 'gnus-summary-delete-marked-with 
9599   'gnus-summary-remove-lines-marked-with)
9600 (make-obsolete 'gnus-summary-delete-marked-with 
9601                'gnus-summary-remove-lines-marked-with)
9602 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9603 (defun gnus-summary-remove-lines-marked-with (marks)
9604   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9605   (interactive "sMarks: ")
9606   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9607   (gnus-set-global-variables)
9608   (let ((buffer-read-only nil)
9609         (marks (concat "^[" marks "]")))
9610     (goto-char (point-min))
9611     (if gnus-newsgroup-adaptive
9612         (gnus-score-remove-lines-adaptive marks)
9613       (while (re-search-forward marks nil t)
9614         (gnus-delete-line)))
9615     ;; If we use dummy roots, we have to do an additional sweep over
9616     ;; the buffer.
9617     (if (not (eq gnus-summary-make-false-root 'dummy))
9618         ()
9619       (goto-char (point-min))
9620       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9621       (while (re-search-forward marks nil t)
9622         (if (gnus-subject-equal
9623              (gnus-summary-subject-string)
9624              (progn
9625                (forward-line 1)
9626                (gnus-summary-subject-string)))
9627             ()
9628           (forward-line -1)
9629           (gnus-delete-line)))))
9630   (or (zerop (buffer-size))
9631       (if (eobp)
9632           (gnus-summary-prev-subject 1)
9633         (gnus-summary-position-cursor))))
9634
9635 (defun gnus-summary-expunge-below (&optional score)
9636   "Remove articles with score less than SCORE."
9637   (interactive "P")
9638   (gnus-set-global-variables)
9639   (setq score (if score
9640                   (prefix-numeric-value score)
9641                 (or gnus-summary-default-score 0)))
9642   (save-excursion
9643     (set-buffer gnus-summary-buffer)
9644     (goto-char (point-min))
9645     (let ((buffer-read-only nil)
9646           beg)
9647       (while (not (eobp))
9648         (if (< (gnus-summary-article-score) score)
9649             (progn
9650               (setq beg (point))
9651               (forward-line 1)
9652               (delete-region beg (point)))
9653           (forward-line 1)))
9654       ;; Adjust point.
9655       (or (zerop (buffer-size))
9656           (if (eobp)
9657               (gnus-summary-prev-subject 1)
9658             (gnus-summary-position-cursor))))))
9659
9660 (defun gnus-summary-mark-below (score mark)
9661   "Mark articles with score less than SCORE with MARK."
9662   (interactive "P\ncMark: ")
9663   (gnus-set-global-variables)
9664   (setq score (if score
9665                   (prefix-numeric-value score)
9666                 (or gnus-summary-default-score 0)))
9667   (save-excursion
9668     (set-buffer gnus-summary-buffer)
9669     (goto-char (point-min))
9670     (while (not (eobp))
9671       (and (< (gnus-summary-article-score) score)
9672            (gnus-summary-mark-article nil mark))
9673       (forward-line 1))))
9674
9675 (defun gnus-summary-kill-below (&optional score)
9676   "Mark articles with score below SCORE as read."
9677   (interactive "P")
9678   (gnus-set-global-variables)
9679   (gnus-summary-mark-below score gnus-killed-mark))
9680
9681 (defun gnus-summary-clear-above (&optional score)
9682   "Clear all marks from articles with score above SCORE."
9683   (interactive "P")
9684   (gnus-set-global-variables)
9685   (gnus-summary-mark-above score gnus-unread-mark))
9686
9687 (defun gnus-summary-tick-above (&optional score)
9688   "Tick all articles with score above SCORE."
9689   (interactive "P")
9690   (gnus-set-global-variables)
9691   (gnus-summary-mark-above score gnus-ticked-mark))
9692
9693 (defun gnus-summary-mark-above (score mark)
9694   "Mark articles with score over SCORE with MARK."
9695   (interactive "P\ncMark: ")
9696   (gnus-set-global-variables)
9697   (setq score (if score
9698                   (prefix-numeric-value score)
9699                 (or gnus-summary-default-score 0)))
9700   (save-excursion
9701     (set-buffer gnus-summary-buffer)
9702     (goto-char (point-min))
9703     (while (not (eobp))
9704       (if (> (gnus-summary-article-score) score)
9705           (progn
9706             (gnus-summary-mark-article nil mark)
9707             (forward-line 1))
9708         (forward-line 1)))))
9709
9710 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9711 (defun gnus-summary-show-all-expunged ()
9712   "Display all the hidden articles that were expunged for low scores."
9713   (interactive)
9714   (gnus-set-global-variables)
9715   (let ((buffer-read-only nil))
9716     (let ((scored gnus-newsgroup-scored)
9717           headers h)
9718       (while scored
9719         (or (gnus-summary-goto-subject (car (car scored)))
9720             (and (setq h (gnus-get-header-by-num (car (car scored))))
9721                  (< (cdr (car scored)) gnus-summary-expunge-below)
9722                  (setq headers (cons h headers))))
9723         (setq scored (cdr scored)))
9724       (or headers (error "No expunged articles hidden."))
9725       (goto-char (point-min))
9726       (save-excursion 
9727         (gnus-summary-update-lines 
9728          (point)
9729          (progn
9730            (gnus-summary-prepare-unthreaded (nreverse headers))
9731            (point)))))
9732     (goto-char (point-min))
9733     (gnus-summary-position-cursor)))
9734
9735 (defun gnus-summary-show-all-dormant ()
9736   "Display all the hidden articles that are marked as dormant."
9737   (interactive)
9738   (gnus-set-global-variables)
9739   (let ((buffer-read-only nil))
9740     (let ((dormant gnus-newsgroup-dormant)
9741           headers h)
9742       (while dormant
9743         (or (gnus-summary-goto-subject (car dormant))
9744             (and (setq h (gnus-get-header-by-num (car dormant)))
9745                  (setq headers (cons h headers))))
9746         (setq dormant (cdr dormant)))
9747       (or headers (error "No dormant articles hidden."))
9748       (goto-char (point-min))
9749       (save-excursion 
9750         (gnus-summary-update-lines 
9751          (point)
9752          (progn
9753            (gnus-summary-prepare-unthreaded (nreverse headers))
9754            (point)))))
9755     (goto-char (point-min))
9756     (gnus-summary-position-cursor)))
9757
9758 (defun gnus-summary-hide-all-dormant ()
9759   "Hide all dormant articles."
9760   (interactive)
9761   (gnus-set-global-variables)
9762   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9763   (gnus-summary-position-cursor))
9764
9765 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9766   "Mark all articles not marked as unread in this newsgroup as read.
9767 If prefix argument ALL is non-nil, all articles are marked as read.
9768 If QUIETLY is non-nil, no questions will be asked.
9769 If TO-HERE is non-nil, it should be a point in the buffer. All
9770 articles before this point will be marked as read.
9771 The number of articles marked as read is returned."
9772   (interactive "P")
9773   (gnus-set-global-variables)
9774   (prog1
9775       (if (or quietly
9776               (not gnus-interactive-catchup) ;Without confirmation?
9777               gnus-expert-user
9778               (gnus-y-or-n-p
9779                (if all
9780                    "Mark absolutely all articles as read? "
9781                  "Mark all unread articles as read? ")))
9782           (if (and not-mark 
9783                    (not gnus-newsgroup-adaptive)
9784                    (not gnus-newsgroup-auto-expire))
9785               (progn
9786                 (and all (setq gnus-newsgroup-marked nil
9787                                gnus-newsgroup-dormant nil))
9788                 (setq gnus-newsgroup-unreads 
9789                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9790             ;; We actually mark all articles as canceled, which we
9791             ;; have to do when using auto-expiry or adaptive scoring. 
9792             (let ((unreads (length gnus-newsgroup-unreads)))
9793               (gnus-summary-show-all-threads)
9794               (if (gnus-summary-first-subject (not all))
9795                   (while (and 
9796                           (if to-here (< (point) to-here) t)
9797                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9798                           (gnus-summary-search-subject nil (not all)))))
9799               (- unreads (length gnus-newsgroup-unreads))
9800               (or to-here
9801                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9802     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9803       (if (and (not to-here) (eq 'nnvirtual (car method)))
9804           (nnvirtual-catchup-group
9805            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9806     (gnus-summary-position-cursor)))
9807
9808 (defun gnus-summary-catchup-to-here (&optional all)
9809   "Mark all unticked articles before the current one as read.
9810 If ALL is non-nil, also mark ticked and dormant articles as read."
9811   (interactive)
9812   (gnus-set-global-variables)
9813   (save-excursion
9814     (and (zerop (forward-line -1))
9815          (progn
9816            (end-of-line)
9817            (gnus-summary-catchup all t (point))
9818            (gnus-set-mode-line 'summary))))
9819   (gnus-summary-position-cursor))
9820
9821 (defun gnus-summary-catchup-all (&optional quietly)
9822   "Mark all articles in this newsgroup as read."
9823   (interactive)
9824   (gnus-set-global-variables)
9825   (gnus-summary-catchup t quietly))
9826
9827 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9828   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9829 If prefix argument ALL is non-nil, all articles are marked as read."
9830   (interactive "P")
9831   (gnus-set-global-variables)
9832   (gnus-summary-catchup all quietly nil 'fast)
9833   ;; Select next newsgroup or exit.
9834   (if (eq gnus-auto-select-next 'quietly)
9835       (gnus-summary-next-group nil)
9836     (gnus-summary-exit)))
9837
9838 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9839   "Mark all articles in this newsgroup as read, and then exit."
9840   (interactive)
9841   (gnus-set-global-variables)
9842   (gnus-summary-catchup-and-exit t quietly))
9843
9844 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9845 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9846   "Mark all articles in this group as read and select the next group.
9847 If given a prefix, mark all articles, unread as well as ticked, as
9848 read." 
9849   (interactive "P")
9850   (gnus-set-global-variables)
9851   (gnus-summary-catchup all)
9852   (gnus-summary-next-group))
9853
9854 ;; Thread-based commands.
9855
9856 (defun gnus-summary-toggle-threads (&optional arg)
9857   "Toggle showing conversation threads.
9858 If ARG is positive number, turn showing conversation threads on."
9859   (interactive "P")
9860   (gnus-set-global-variables)
9861   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9862     (setq gnus-show-threads
9863           (if (null arg) (not gnus-show-threads)
9864             (> (prefix-numeric-value arg) 0)))
9865     (gnus-summary-prepare)
9866     (gnus-summary-goto-subject current)
9867     (gnus-summary-position-cursor)))
9868
9869 (defun gnus-summary-show-all-threads ()
9870   "Show all threads."
9871   (interactive)
9872   (gnus-set-global-variables)
9873   (save-excursion
9874     (let ((buffer-read-only nil))
9875       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9876   (gnus-summary-position-cursor))
9877
9878 (defun gnus-summary-show-thread ()
9879   "Show thread subtrees.
9880 Returns nil if no thread was there to be shown."
9881   (interactive)
9882   (gnus-set-global-variables)
9883   (let ((buffer-read-only nil)
9884         (orig (point))
9885         ;; first goto end then to beg, to have point at beg after let
9886         (end (progn (end-of-line) (point)))
9887         (beg (progn (beginning-of-line) (point))))
9888     (prog1
9889         ;; Any hidden lines here?
9890         (search-forward "\r" end t)
9891       (subst-char-in-region beg end ?\^M ?\n t)
9892       (goto-char orig)
9893       (gnus-summary-position-cursor))))
9894
9895 (defun gnus-summary-hide-all-threads ()
9896   "Hide all thread subtrees."
9897   (interactive)
9898   (gnus-set-global-variables)
9899   (save-excursion
9900     (goto-char (point-min))
9901     (gnus-summary-hide-thread)
9902     (while (and (not (eobp)) (zerop (forward-line 1)))
9903       (gnus-summary-hide-thread)))
9904   (gnus-summary-position-cursor))
9905
9906 (defun gnus-summary-hide-thread ()
9907   "Hide thread subtrees.
9908 Returns nil if no threads were there to be hidden."
9909   (interactive)
9910   (gnus-set-global-variables)
9911   (let ((buffer-read-only nil)
9912         (start (point))
9913         (level (gnus-summary-thread-level))
9914         (end (point)))
9915     ;; Go forward until either the buffer ends or the subthread
9916     ;; ends. 
9917     (if (eobp)
9918         ()
9919       (while (and (zerop (forward-line 1))
9920                   (> (gnus-summary-thread-level) level))
9921         (setq end (point)))
9922       (prog1
9923           (save-excursion
9924             (goto-char end)
9925             (search-backward "\n" start t))
9926         (subst-char-in-region start end ?\n ?\^M t)
9927         (forward-line -1)))))
9928
9929 (defun gnus-summary-go-to-next-thread (&optional previous)
9930   "Go to the same level (or less) next thread.
9931 If PREVIOUS is non-nil, go to previous thread instead.
9932 Return the article number moved to, or nil if moving was impossible."
9933   (let ((level (gnus-summary-thread-level))
9934         (article (gnus-summary-article-number)))
9935     (if previous 
9936         (while (and (zerop (forward-line -1))
9937                     (> (gnus-summary-thread-level) level)))
9938       (while (and (save-excursion
9939                     (forward-line 1)
9940                     (not (eobp)))
9941                   (zerop (forward-line 1))
9942                   (> (gnus-summary-thread-level) level))))
9943     (gnus-summary-recenter)
9944     (gnus-summary-position-cursor)
9945     (let ((oart (gnus-summary-article-number)))
9946       (and (/= oart article) oart))))
9947
9948 (defun gnus-summary-next-thread (n)
9949   "Go to the same level next N'th thread.
9950 If N is negative, search backward instead.
9951 Returns the difference between N and the number of skips actually
9952 done."
9953   (interactive "p")
9954   (gnus-set-global-variables)
9955   (let ((backward (< n 0))
9956         (n (abs n)))
9957   (while (and (> n 0)
9958               (gnus-summary-go-to-next-thread backward))
9959     (setq n (1- n)))
9960   (gnus-summary-position-cursor)
9961   (if (/= 0 n) (gnus-message 7 "No more threads"))
9962   n))
9963
9964 (defun gnus-summary-prev-thread (n)
9965   "Go to the same level previous N'th thread.
9966 Returns the difference between N and the number of skips actually
9967 done."
9968   (interactive "p")
9969   (gnus-set-global-variables)
9970   (gnus-summary-next-thread (- n)))
9971
9972 (defun gnus-summary-go-down-thread (&optional same)
9973   "Go down one level in the current thread.
9974 If SAME is non-nil, also move to articles of the same level."
9975   (let ((level (gnus-summary-thread-level))
9976         (start (point)))
9977     (if (and (zerop (forward-line 1))
9978              (> (gnus-summary-thread-level) level))
9979         t
9980       (goto-char start)
9981       nil)))
9982
9983 (defun gnus-summary-go-up-thread ()
9984   "Go up one level in the current thread."
9985   (let ((level (gnus-summary-thread-level))
9986         (start (point)))
9987     (while (and (zerop (forward-line -1))
9988                 (>= (gnus-summary-thread-level) level)))
9989     (if (>= (gnus-summary-thread-level) level)
9990         (progn
9991           (goto-char start)
9992           nil)
9993       t)))
9994
9995 (defun gnus-summary-down-thread (n)
9996   "Go down thread N steps.
9997 If N is negative, go up instead.
9998 Returns the difference between N and how many steps down that were
9999 taken."
10000   (interactive "p")
10001   (gnus-set-global-variables)
10002   (let ((up (< n 0))
10003         (n (abs n)))
10004   (while (and (> n 0)
10005               (if up (gnus-summary-go-up-thread)
10006                 (gnus-summary-go-down-thread)))
10007     (setq n (1- n)))
10008   (gnus-summary-position-cursor)
10009   (if (/= 0 n) (gnus-message 7 "Can't go further"))
10010   n))
10011
10012 (defun gnus-summary-up-thread (n)
10013   "Go up thread N steps.
10014 If N is negative, go up instead.
10015 Returns the difference between N and how many steps down that were
10016 taken."
10017   (interactive "p")
10018   (gnus-set-global-variables)
10019   (gnus-summary-down-thread (- n)))
10020
10021 (defun gnus-summary-kill-thread (&optional unmark)
10022   "Mark articles under current thread as read.
10023 If the prefix argument is positive, remove any kinds of marks.
10024 If the prefix argument is negative, tick articles instead."
10025   (interactive "P")
10026   (gnus-set-global-variables)
10027   (if unmark
10028       (setq unmark (prefix-numeric-value unmark)))
10029   (let ((killing t)
10030         (level (gnus-summary-thread-level)))
10031     (save-excursion
10032       (while killing
10033         ;; Mark the article...
10034         (cond ((null unmark) (gnus-summary-mark-article-as-read
10035                                gnus-killed-mark))
10036               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10037                              gnus-unread-mark))
10038               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10039         ;; ...and go forward until either the buffer ends or the subtree
10040         ;; ends. 
10041         (if (not (and (zerop (forward-line 1))
10042                       (> (gnus-summary-thread-level) level)))
10043             (setq killing nil))))
10044     ;; Hide killed subtrees.
10045     (and (null unmark)
10046          gnus-thread-hide-killed
10047          (gnus-summary-hide-thread))
10048     ;; If marked as read, go to next unread subject.
10049     (if (null unmark)
10050         ;; Go to next unread subject.
10051         (gnus-summary-next-subject 1 t)))
10052   (gnus-set-mode-line 'summary))
10053
10054 ;; Summary sorting commands
10055
10056 (defun gnus-summary-sort-by-number (&optional reverse)
10057   "Sort summary buffer by article number.
10058 Argument REVERSE means reverse order."
10059   (interactive "P")
10060   (gnus-set-global-variables)
10061   (gnus-summary-sort 
10062    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10063    ;; a function, so we wrap it.
10064    (cons (lambda () (gnus-summary-article-number))
10065          'gnus-thread-sort-by-number) reverse))
10066
10067 (defun gnus-summary-sort-by-author (&optional reverse)
10068   "Sort summary buffer by author name alphabetically.
10069 If case-fold-search is non-nil, case of letters is ignored.
10070 Argument REVERSE means reverse order."
10071   (interactive "P")
10072   (gnus-set-global-variables)
10073   (gnus-summary-sort
10074    (cons
10075     (lambda ()
10076       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10077              (extract (funcall
10078                        gnus-extract-address-components
10079                        (header-from header))))
10080         (concat (or (car extract) (cdr extract))
10081                 "\r" (header-subject header))))
10082     'gnus-thread-sort-by-author)
10083    reverse))
10084
10085 (defun gnus-summary-sort-by-subject (&optional reverse)
10086   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10087 If case-fold-search is non-nil, case of letters is ignored.
10088 Argument REVERSE means reverse order."
10089   (interactive "P")
10090   (gnus-set-global-variables)
10091   (gnus-summary-sort
10092    (cons
10093     (lambda ()
10094       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10095              (extract (funcall
10096                        gnus-extract-address-components
10097                        (header-from header))))
10098         (concat 
10099          (downcase (gnus-simplify-subject (gnus-summary-subject-string)))
10100          "\r" (or (car extract) (cdr extract)))))
10101     'gnus-thread-sort-by-subject)
10102    reverse))
10103
10104 (defun gnus-summary-sort-by-date (&optional reverse)
10105   "Sort summary buffer by date.
10106 Argument REVERSE means reverse order."
10107   (interactive "P")
10108   (gnus-set-global-variables)
10109   (gnus-summary-sort
10110    (cons
10111     (lambda ()
10112       (gnus-sortable-date
10113        (header-date 
10114         (gnus-get-header-by-num (gnus-summary-article-number)))))
10115     'gnus-thread-sort-by-date)
10116    reverse))
10117
10118 (defun gnus-summary-sort-by-score (&optional reverse)
10119   "Sort summary buffer by score.
10120 Argument REVERSE means reverse order."
10121   (interactive "P")
10122   (gnus-set-global-variables)
10123   (gnus-summary-sort 
10124    (cons (lambda () (gnus-summary-article-score))
10125          'gnus-thread-sort-by-score)
10126    (not reverse)))
10127
10128 (defvar gnus-summary-already-sorted nil)
10129 (defun gnus-summary-sort (predicate reverse)
10130   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10131   (if gnus-summary-already-sorted
10132       ()
10133     (let (buffer-read-only)
10134       (if (not gnus-show-threads)
10135           ;; We do untreaded sorting...
10136           (progn
10137             (goto-char (point-min))
10138             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10139         ;; ... or we do threaded sorting.
10140         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10141               (gnus-summary-prepare-hook nil)
10142               (gnus-summary-already-sorted nil))
10143           ;; We do that by simply regenerating the threads.
10144           (gnus-summary-prepare)
10145           (and gnus-show-threads
10146                gnus-thread-hide-subtree
10147                (gnus-summary-hide-all-threads))
10148           ;; If in async mode, we send some info to the backend.
10149           (and gnus-newsgroup-async
10150                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10151                (gnus-request-asynchronous 
10152                 gnus-newsgroup-name
10153                 (if (and gnus-asynchronous-article-function
10154                          (fboundp gnus-asynchronous-article-function))
10155                     (funcall gnus-asynchronous-article-function
10156                              gnus-newsgroup-threads)))))))))
10157
10158   
10159 (defun gnus-sortable-date (date)
10160   "Make sortable string by string-lessp from DATE.
10161 Timezone package is used."
10162   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10163          (year (aref date 0))
10164          (month (aref date 1))
10165          (day (aref date 2)))
10166     (timezone-make-sortable-date 
10167      year month day 
10168      (timezone-make-time-string
10169       (aref date 3) (aref date 4) (aref date 5)))))
10170
10171
10172 ;; Summary saving commands.
10173
10174 (defun gnus-summary-save-article (&optional n)
10175   "Save the current article using the default saver function.
10176 If N is a positive number, save the N next articles.
10177 If N is a negative number, save the N previous articles.
10178 If N is nil and any articles have been marked with the process mark,
10179 save those articles instead.
10180 The variable `gnus-default-article-saver' specifies the saver function."
10181   (interactive "P")
10182   (gnus-set-global-variables)
10183   (let ((articles (gnus-summary-work-articles n)))
10184     (while articles
10185       (let ((header (gnus-get-header-by-num (car articles))))
10186         (if (vectorp header)
10187             (progn
10188               (save-window-excursion
10189                 (gnus-summary-select-article t nil nil (car articles)))
10190               (or gnus-save-all-headers
10191                   (gnus-article-hide-headers t))
10192               ;; Remove any X-Gnus lines.
10193               (save-excursion
10194                 (save-restriction
10195                   (set-buffer gnus-article-buffer)
10196                   (let ((buffer-read-only nil))
10197                     (goto-char (point-min))
10198                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10199                                                   (point-max)))
10200                     (while (re-search-forward "^X-Gnus" nil t)
10201                       (beginning-of-line)
10202                       (delete-region (point)
10203                                      (progn (forward-line 1) (point))))
10204                     (widen))))
10205               (save-window-excursion
10206                 (if gnus-default-article-saver
10207                     (funcall gnus-default-article-saver)
10208                   (error "No default saver is defined."))))
10209           (if (assq 'name header)
10210               (gnus-copy-file (cdr (assq 'name header)))
10211             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10212       (gnus-summary-remove-process-mark (car articles))
10213       (setq articles (cdr articles)))
10214     (gnus-summary-position-cursor)
10215     n))
10216
10217 (defun gnus-summary-pipe-output (&optional arg)
10218   "Pipe the current article to a subprocess.
10219 If N is a positive number, pipe the N next articles.
10220 If N is a negative number, pipe the N previous articles.
10221 If N is nil and any articles have been marked with the process mark,
10222 pipe those articles instead."
10223   (interactive "P")
10224   (gnus-set-global-variables)
10225   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10226     (gnus-summary-save-article arg)))
10227
10228 (defun gnus-summary-save-article-mail (&optional arg)
10229   "Append the current article to an mail file.
10230 If N is a positive number, save the N next articles.
10231 If N is a negative number, save the N previous articles.
10232 If N is nil and any articles have been marked with the process mark,
10233 save those articles instead."
10234   (interactive "P")
10235   (gnus-set-global-variables)
10236   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10237     (gnus-summary-save-article arg)))
10238
10239 (defun gnus-summary-save-article-rmail (&optional arg)
10240   "Append the current article to an rmail file.
10241 If N is a positive number, save the N next articles.
10242 If N is a negative number, save the N previous articles.
10243 If N is nil and any articles have been marked with the process mark,
10244 save those articles instead."
10245   (interactive "P")
10246   (gnus-set-global-variables)
10247   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10248     (gnus-summary-save-article arg)))
10249
10250 (defun gnus-summary-save-article-file (&optional arg)
10251   "Append the current article to a file.
10252 If N is a positive number, save the N next articles.
10253 If N is a negative number, save the N previous articles.
10254 If N is nil and any articles have been marked with the process mark,
10255 save those articles instead."
10256   (interactive "P")
10257   (gnus-set-global-variables)
10258   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10259     (gnus-summary-save-article arg)))
10260
10261 (defun gnus-read-save-file-name (prompt default-name)
10262   (let ((methods gnus-split-methods)
10263         split-name)
10264     (if (not gnus-split-methods)
10265         ()
10266       (save-excursion
10267         (set-buffer gnus-article-buffer)
10268         (gnus-narrow-to-headers)
10269         (while methods
10270           (goto-char (point-min))
10271           (and (condition-case () 
10272                    (re-search-forward (car (car methods)) nil t)
10273                  (error nil))
10274                (setq split-name (cons (nth 1 (car methods)) split-name)))
10275           (setq methods (cdr methods)))
10276         (widen)))
10277     (cond ((null split-name)
10278            (read-file-name
10279             (concat prompt " (default "
10280                     (file-name-nondirectory default-name) ") ")
10281             (file-name-directory default-name)
10282             default-name))
10283           ((= 1 (length split-name))
10284            (read-file-name
10285             (concat prompt " (default " (car split-name) ") ")
10286             gnus-article-save-directory
10287             (concat gnus-article-save-directory (car split-name))))
10288           (t
10289            (setq split-name (mapcar (lambda (el) (list el))
10290                                     (nreverse split-name)))
10291            (let ((result (completing-read 
10292                           (concat prompt " ")
10293                           split-name nil nil)))
10294              (concat gnus-article-save-directory
10295                      (if (string= result "")
10296                          (car (car split-name))
10297                        result)))))))
10298
10299 (defun gnus-summary-save-in-rmail (&optional filename)
10300   "Append this article to Rmail file.
10301 Optional argument FILENAME specifies file name.
10302 Directory to save to is default to `gnus-article-save-directory' which
10303 is initialized from the SAVEDIR environment variable."
10304   (interactive)
10305   (gnus-set-global-variables)
10306   (let ((default-name
10307           (funcall gnus-rmail-save-name gnus-newsgroup-name
10308                    gnus-current-headers gnus-newsgroup-last-rmail)))
10309     (or filename
10310         (setq filename (gnus-read-save-file-name 
10311                         "Save in rmail file:" default-name)))
10312     (gnus-make-directory (file-name-directory filename))
10313     (gnus-eval-in-buffer-window 
10314      gnus-article-buffer
10315      (save-excursion
10316        (save-restriction
10317          (widen)
10318          (gnus-output-to-rmail filename))))
10319     ;; Remember the directory name to save articles
10320     (setq gnus-newsgroup-last-rmail filename)))
10321
10322 (defun gnus-summary-save-in-mail (&optional filename)
10323   "Append this article to Unix mail file.
10324 Optional argument FILENAME specifies file name.
10325 Directory to save to is default to `gnus-article-save-directory' which
10326 is initialized from the SAVEDIR environment variable."
10327   (interactive)
10328   (gnus-set-global-variables)
10329   (let ((default-name
10330           (funcall gnus-mail-save-name gnus-newsgroup-name
10331                    gnus-current-headers gnus-newsgroup-last-mail)))
10332     (or filename
10333         (setq filename (gnus-read-save-file-name 
10334                         "Save in Unix mail file:" default-name)))
10335     (setq filename
10336           (expand-file-name filename
10337                             (and default-name
10338                                  (file-name-directory default-name))))
10339     (gnus-make-directory (file-name-directory filename))
10340     (gnus-eval-in-buffer-window 
10341      gnus-article-buffer
10342      (save-excursion
10343        (save-restriction
10344          (widen)
10345          (if (and (file-readable-p filename) (rmail-file-p filename))
10346              (gnus-output-to-rmail filename)
10347            (rmail-output filename 1 t t)))))
10348     ;; Remember the directory name to save articles.
10349     (setq gnus-newsgroup-last-mail filename)))
10350
10351 (defun gnus-summary-save-in-file (&optional filename)
10352   "Append this article to file.
10353 Optional argument FILENAME specifies file name.
10354 Directory to save to is default to `gnus-article-save-directory' which
10355 is initialized from the SAVEDIR environment variable."
10356   (interactive)
10357   (gnus-set-global-variables)
10358   (let ((default-name
10359           (funcall gnus-file-save-name gnus-newsgroup-name
10360                    gnus-current-headers gnus-newsgroup-last-file)))
10361     (or filename
10362         (setq filename (gnus-read-save-file-name 
10363                         "Save in file:" default-name)))
10364     (gnus-make-directory (file-name-directory filename))
10365     (gnus-eval-in-buffer-window 
10366      gnus-article-buffer
10367      (save-excursion
10368        (save-restriction
10369          (widen)
10370          (gnus-output-to-file filename))))
10371     ;; Remember the directory name to save articles.
10372     (setq gnus-newsgroup-last-file filename)))
10373
10374 (defun gnus-summary-save-in-pipe (&optional command)
10375   "Pipe this article to subprocess."
10376   (interactive)
10377   (gnus-set-global-variables)
10378   (let ((command (read-string "Shell command on article: "
10379                               gnus-last-shell-command)))
10380     (if (string-equal command "")
10381         (setq command gnus-last-shell-command))
10382     (gnus-eval-in-buffer-window 
10383      gnus-article-buffer
10384      (save-restriction
10385        (widen)
10386        (shell-command-on-region (point-min) (point-max) command nil)))
10387     (setq gnus-last-shell-command command)))
10388
10389 ;; Summary extract commands
10390
10391 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10392   (let ((buffer-read-only nil)
10393         (article (gnus-summary-article-number))
10394         b)
10395     (or (gnus-summary-goto-subject article)
10396         (error (format "No such article: %d" article)))
10397     (gnus-summary-position-cursor)
10398     ;; If all commands are to be bunched up on one line, we collect
10399     ;; them here.  
10400     (if gnus-view-pseudos-separately
10401         ()
10402       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10403             files action)
10404         (while ps
10405           (setq action (cdr (assq 'action (car ps))))
10406           (setq files (list (cdr (assq 'name (car ps)))))
10407           (while (and ps (cdr ps)
10408                       (string= (or action "1")
10409                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10410             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10411             (setcdr ps (cdr (cdr ps))))
10412           (if (not files)
10413               ()
10414             (if (not (string-match "%s" action))
10415                 (setq files (cons " " files)))
10416             (setq files (cons " " files))
10417             (and (assq 'execute (car ps))
10418                  (setcdr (assq 'execute (car ps))
10419                          (funcall (if (string-match "%s" action)
10420                                       'format 'concat)
10421                                   action 
10422                                   (mapconcat (lambda (f) f) files " ")))))
10423           (setq ps (cdr ps)))))
10424     (if (and gnus-view-pseudos (not not-view))
10425         (while pslist
10426           (and (assq 'execute (car pslist))
10427                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10428                                      (eq gnus-view-pseudos 'not-confirm)))
10429           (setq pslist (cdr pslist)))
10430       (save-excursion
10431         (while pslist
10432           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10433                                          (gnus-summary-article-number)))
10434           (forward-line 1)
10435           (setq b (point))
10436           (insert "          " (file-name-nondirectory 
10437                                 (cdr (assq 'name (car pslist))))
10438                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10439           (add-text-properties 
10440            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10441                           'gnus-mark gnus-unread-mark 
10442                           'gnus-level 0
10443                           'gnus-pseudo (car pslist)))
10444           (forward-line -1)
10445           (gnus-sethash (int-to-string gnus-reffed-article-number)
10446                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10447           (setq gnus-newsgroup-unreads
10448                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10449           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10450           (setq pslist (cdr pslist)))))))
10451
10452 (defun gnus-pseudos< (p1 p2)
10453   (let ((c1 (cdr (assq 'action p1)))
10454         (c2 (cdr (assq 'action p2))))
10455     (and c1 c2 (string< c1 c2))))
10456
10457 (defun gnus-request-pseudo-article (props)
10458   (cond ((assq 'execute props)
10459          (gnus-execute-command (cdr (assq 'execute props)))))
10460   (let ((gnus-current-article (gnus-summary-article-number)))
10461     (run-hooks 'gnus-mark-article-hook)))
10462
10463 (defun gnus-execute-command (command &optional automatic)
10464   (save-excursion
10465     (gnus-article-setup-buffer)
10466     (set-buffer gnus-article-buffer)
10467     (let ((command (if automatic command (read-string "Command: " command)))
10468           (buffer-read-only nil))
10469       (erase-buffer)
10470       (insert "$ " command "\n\n")
10471       (if gnus-view-pseudo-asynchronously
10472           (start-process "gnus-execute" nil "sh" "-c" command)
10473         (call-process "sh" nil t nil "-c" command)))))
10474
10475 (defun gnus-copy-file (file &optional to)
10476   "Copy FILE to TO."
10477   (interactive
10478    (list (read-file-name "Copy file: " default-directory)
10479          (read-file-name "Copy file to: " default-directory)))
10480   (gnus-set-global-variables)
10481   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10482   (and (file-directory-p to) 
10483        (setq to (concat (file-name-as-directory to)
10484                         (file-name-nondirectory file))))
10485   (copy-file file to))
10486
10487 ;; Summary kill commands.
10488
10489 (defun gnus-summary-edit-global-kill (article)
10490   "Edit the \"global\" kill file."
10491   (interactive (list (gnus-summary-article-number)))
10492   (gnus-set-global-variables)
10493   (gnus-group-edit-global-kill article))
10494
10495 (defun gnus-summary-edit-local-kill ()
10496   "Edit a local kill file applied to the current newsgroup."
10497   (interactive)
10498   (gnus-set-global-variables)
10499   (setq gnus-current-headers 
10500         (gnus-gethash 
10501          (int-to-string (gnus-summary-article-number))
10502          gnus-newsgroup-headers-hashtb-by-number))
10503   (gnus-set-global-variables)
10504   (gnus-group-edit-local-kill 
10505    (gnus-summary-article-number) gnus-newsgroup-name))
10506
10507 \f
10508 ;;;
10509 ;;; Gnus article mode
10510 ;;;
10511
10512 (put 'gnus-article-mode 'mode-class 'special)
10513
10514 (if gnus-article-mode-map
10515     nil
10516   (setq gnus-article-mode-map (make-keymap))
10517   (suppress-keymap gnus-article-mode-map)
10518   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10519   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10520   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10521   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10522   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10523   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10524   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10525   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10526   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10527   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10528   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10529   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10530   
10531   ;; Duplicate almost all summary keystrokes in the article mode map.
10532   (let ((commands 
10533          (list 
10534           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10535           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10536           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10537           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10538           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10539           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10540           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10541           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10542           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10543           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10544           "\C-c\C-v\C-v" "\C-d" "v" 
10545 ;;        "Mt" "M!" "Md" "Mr"
10546 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10547 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10548 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10549 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10550 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10551 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10552 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10553 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10554 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10555 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10556 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10557           )))
10558     (while (and nil commands) ; disabled
10559       (define-key gnus-article-mode-map (car commands) 
10560         'gnus-article-summary-command)
10561       (setq commands (cdr commands))))
10562
10563   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10564 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10565                          "=" "n"  "^" "\M-^")))
10566     (while (and nil commands) ; disabled
10567       (define-key gnus-article-mode-map (car commands) 
10568         'gnus-article-summary-command-nosave)
10569       (setq commands (cdr commands)))))
10570
10571
10572 (defun gnus-article-mode ()
10573   "Major mode for displaying an article.
10574
10575 All normal editing commands are switched off.
10576
10577 The following commands are available:
10578
10579 \\<gnus-article-mode-map>
10580 \\[gnus-article-next-page]\t Scroll the article one page forwards
10581 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10582 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10583 \\[gnus-article-show-summary]\t Display the summary buffer
10584 \\[gnus-article-mail]\t Send a reply to the address near point
10585 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10586 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10587 \\[gnus-info-find-node]\t Go to the Gnus info node"
10588   (interactive)
10589   (if gnus-visual (gnus-article-make-menu-bar))
10590   (kill-all-local-variables)
10591   (setq mode-line-modified "-- ")
10592   (make-local-variable 'mode-line-format)
10593   (setq mode-line-format (copy-sequence mode-line-format))
10594   (and (equal (nth 3 mode-line-format) "   ")
10595        (setcar (nthcdr 3 mode-line-format) ""))
10596   (setq mode-name "Article")
10597   (setq major-mode 'gnus-article-mode)
10598   (make-local-variable 'minor-mode-alist)
10599   (or (assq 'gnus-show-mime minor-mode-alist)
10600       (setq minor-mode-alist
10601             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10602   (use-local-map gnus-article-mode-map)
10603   (make-local-variable 'page-delimiter)
10604   (setq page-delimiter gnus-page-delimiter)
10605   (buffer-disable-undo (current-buffer))
10606   (setq buffer-read-only t)             ;Disable modification
10607   (run-hooks 'gnus-article-mode-hook))
10608
10609 (defun gnus-article-setup-buffer ()
10610   "Initialize article mode buffer."
10611   (if (get-buffer gnus-article-buffer)
10612       (save-excursion
10613         (set-buffer gnus-article-buffer)
10614         (buffer-disable-undo (current-buffer))
10615         (setq buffer-read-only t)
10616         (gnus-add-current-to-buffer-list)
10617         (or (eq major-mode 'gnus-article-mode)
10618             (gnus-article-mode)))
10619     (save-excursion
10620       (set-buffer (get-buffer-create gnus-article-buffer))
10621       (gnus-add-current-to-buffer-list)
10622       (gnus-article-mode))))
10623
10624 ;; Set article window start at LINE, where LINE is the number of lines
10625 ;; from the head of the article.
10626 (defun gnus-article-set-window-start (&optional line)
10627   (set-window-start 
10628    (get-buffer-window gnus-article-buffer)
10629    (save-excursion
10630      (set-buffer gnus-article-buffer)
10631      (goto-char (point-min))
10632      (if (not line)
10633          (point-min)
10634        (gnus-message 6 "Moved to bookmark")
10635        (search-forward "\n\n" nil t)
10636        (forward-line line)
10637        (point)))))
10638
10639 (defun gnus-request-article-this-buffer (article group)
10640   "Get an article and insert it into this buffer."
10641   (setq group (or group gnus-newsgroup-name))
10642
10643   ;; Open server if it has closed.
10644   (gnus-check-server (gnus-find-method-for-group group))
10645
10646   ;; Using `gnus-request-article' directly will insert the article into
10647   ;; `nntp-server-buffer' - so we'll save some time by not having to
10648   ;; copy it from the server buffer into the article buffer.
10649
10650   ;; We only request an article by message-id when we do not have the
10651   ;; headers for it, so we'll have to get those.
10652   (and (stringp article) 
10653        (let ((gnus-override-method gnus-refer-article-method))
10654          (gnus-read-header article)))
10655
10656   ;; If the article number is negative, that means that this article
10657   ;; doesn't belong in this newsgroup (possibly), so we find its
10658   ;; message-id and request it by id instead of number.
10659   (if (not (numberp article))
10660       ()
10661     (save-excursion
10662       (set-buffer gnus-summary-buffer)
10663       (let ((header (gnus-get-header-by-num article)))
10664         (if (< article 0)
10665             (if (vectorp header)
10666                 ;; It's a real article.
10667                 (setq article (header-id header))
10668               ;; It is an extracted pseudo-article.
10669               (setq article 'pseudo)
10670               (gnus-request-pseudo-article header)))
10671
10672         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10673           (if (not (eq (car method) 'nneething))
10674               ()
10675             (let ((dir (concat (file-name-as-directory (nth 1 method))
10676                                (header-subject header))))
10677               (if (file-directory-p dir)
10678                   (progn
10679                     (setq article 'nneething)
10680                     (gnus-group-enter-directory dir)))))))))
10681
10682   ;; Check the cache.
10683   (if (and gnus-use-cache
10684            (numberp article)
10685            (gnus-cache-request-article article group))
10686       'article
10687     ;; Get the article and into the article buffer.
10688     (if (or (stringp article) (numberp article))
10689         (progn
10690           (erase-buffer)
10691           (let ((gnus-override-method 
10692                  (and (stringp article) gnus-refer-article-method)))
10693             (and (gnus-request-article article group (current-buffer))
10694                  'article)))
10695       article)))
10696
10697 (defun gnus-read-header (id)
10698   "Read the headers of article ID and enter them into the Gnus system."
10699   (let (header)
10700     (if (not (setq header 
10701                    (car (if (let ((gnus-nov-is-evil t))
10702                               (gnus-retrieve-headers 
10703                                (list id) gnus-newsgroup-name))
10704                             (gnus-get-newsgroup-headers)))))
10705         nil
10706       (if (stringp id)
10707           (header-set-number header gnus-reffed-article-number))
10708       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10709       (gnus-sethash (int-to-string (header-number header)) header
10710                     gnus-newsgroup-headers-hashtb-by-number)
10711       (if (stringp id)
10712           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10713       (setq gnus-current-headers header)
10714       header)))
10715
10716 (defun gnus-article-prepare (article &optional all-headers header)
10717   "Prepare ARTICLE in article mode buffer.
10718 ARTICLE should either be an article number or a Message-ID.
10719 If ARTICLE is an id, HEADER should be the article headers.
10720 If ALL-HEADERS is non-nil, no headers are hidden."
10721   (save-excursion
10722     ;; Make sure we start in a summary buffer.
10723     (or (eq major-mode 'gnus-summary-mode)
10724         (set-buffer gnus-summary-buffer))
10725     (setq gnus-summary-buffer (current-buffer))
10726     ;; Make sure the connection to the server is alive.
10727     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10728         (progn
10729           (gnus-check-server 
10730            (gnus-find-method-for-group gnus-newsgroup-name))
10731           (gnus-request-group gnus-newsgroup-name t)))
10732     (let* ((article (if header (header-number header) article))
10733            (summary-buffer (current-buffer))
10734            (internal-hook gnus-article-internal-prepare-hook)
10735            (group gnus-newsgroup-name)
10736            result)
10737       (save-excursion
10738         (gnus-article-setup-buffer)
10739         (set-buffer gnus-article-buffer)
10740         (if (not (setq result (let ((buffer-read-only nil))
10741                                 (gnus-request-article-this-buffer 
10742                                  article group))))
10743             ;; There is no such article.
10744             (save-excursion
10745               (if (not (numberp article))
10746                   ()
10747                 (setq gnus-article-current 
10748                       (cons gnus-newsgroup-name article))
10749                 (set-buffer gnus-summary-buffer)
10750                 (setq gnus-current-article article)
10751                 (gnus-summary-mark-article article gnus-canceled-mark))
10752               (gnus-message 1 "No such article (may be canceled)")
10753               (ding)
10754               nil)
10755           (if (or (eq result 'pseudo) (eq result 'nneething))
10756               (progn
10757                 (save-excursion
10758                   (set-buffer summary-buffer)
10759                   (setq gnus-last-article gnus-current-article
10760                         gnus-newsgroup-history (cons gnus-current-article
10761                                                      gnus-newsgroup-history)
10762                         gnus-current-article 0
10763                         gnus-current-headers nil
10764                         gnus-article-current nil)
10765                   (if (eq result 'nneething)
10766                       (gnus-configure-windows 'summary)
10767                     (gnus-configure-windows 'article))
10768                   (gnus-set-global-variables))
10769                 (gnus-set-mode-line 'article))
10770             ;; The result from the `request' was an actual article -
10771             ;; or at least some text that is now displayed in the
10772             ;; article buffer.
10773             (if (and (numberp article)
10774                      (not (eq article gnus-current-article)))
10775                 ;; Seems like a new article has been selected.
10776                 ;; `gnus-current-article' must be an article number.
10777                 (save-excursion
10778                   (set-buffer summary-buffer)
10779                   (setq gnus-last-article gnus-current-article
10780                         gnus-newsgroup-history (cons gnus-current-article
10781                                                      gnus-newsgroup-history)
10782                         gnus-current-article article
10783                         gnus-current-headers 
10784                         (gnus-get-header-by-num gnus-current-article)
10785                         gnus-article-current 
10786                         (cons gnus-newsgroup-name gnus-current-article))
10787                   (gnus-summary-show-thread)
10788                   (run-hooks 'gnus-mark-article-hook)
10789                   (gnus-set-mode-line 'summary)
10790                   (and gnus-visual 
10791                        (run-hooks 'gnus-visual-mark-article-hook))
10792                   ;; Set the global newsgroup variables here.
10793                   ;; Suggested by Jim Sisolak
10794                   ;; <sisolak@trans4.neep.wisc.edu>.
10795                   (gnus-set-global-variables)
10796                   (setq gnus-have-all-headers 
10797                         (or all-headers gnus-show-all-headers))
10798                   (and gnus-use-cache 
10799                        (vectorp (gnus-get-header-by-number article))
10800                        (gnus-cache-possibly-enter-article
10801                         group article
10802                         (gnus-get-header-by-number article)
10803                         (memq article gnus-newsgroup-marked)
10804                         (memq article gnus-newsgroup-dormant)
10805                         (memq article gnus-newsgroup-unreads)))))
10806             ;; Hooks for getting information from the article.
10807             ;; This hook must be called before being narrowed.
10808             (let (buffer-read-only)
10809               (run-hooks 'internal-hook)
10810               (run-hooks 'gnus-article-prepare-hook)
10811               ;; Decode MIME message.
10812               (if (and gnus-show-mime
10813                        (or (not gnus-strict-mime)
10814                            (gnus-fetch-field "Mime-Version")))
10815                   (funcall gnus-show-mime-method))
10816               ;; Perform the article display hooks.
10817               (run-hooks 'gnus-article-display-hook))
10818             ;; Do page break.
10819             (goto-char (point-min))
10820             (and gnus-break-pages (gnus-narrow-to-page))
10821             (gnus-set-mode-line 'article)
10822             (gnus-configure-windows 'article)
10823             (goto-char (point-min))
10824             t))))))
10825
10826 (defun gnus-article-show-all-headers ()
10827   "Show all article headers in article mode buffer."
10828   (save-excursion 
10829     (gnus-article-setup-buffer)
10830     (set-buffer gnus-article-buffer)
10831     (let ((buffer-read-only nil))
10832       (remove-text-properties (point-min) (point-max) 
10833                               gnus-hidden-properties))))
10834
10835 (defun gnus-article-hide-headers-if-wanted ()
10836   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10837 Provided for backwards compatability."
10838   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10839       (gnus-article-hide-headers)))
10840
10841 (defun gnus-article-hide-headers (&optional delete)
10842   "Hide unwanted headers and possibly sort them as well."
10843   (interactive "P")
10844   (save-excursion
10845     (set-buffer gnus-article-buffer)
10846     (save-restriction
10847       (let ((sorted gnus-sorted-header-list)
10848             (buffer-read-only nil)
10849             want-list beg want-l)
10850         ;; First we narrow to just the headers.
10851         (widen)
10852         (goto-char (point-min))
10853         ;; Hide any "From " lines at the beginning of (mail) articles. 
10854         (while (looking-at "From ")
10855           (forward-line 1))
10856         (if (bobp) 
10857             (add-text-properties (point-min) (point) gnus-hidden-properties))
10858         ;; Then treat the rest of the header lines.
10859         (narrow-to-region 
10860          (point) 
10861          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10862         ;; Then we use the two regular expressions
10863         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10864         ;; select which header lines is to remain visible in the
10865         ;; article buffer.
10866         (goto-char (point-min))
10867         (while (re-search-forward "^[^ \t]*:" nil t)
10868           (beginning-of-line)
10869           ;; We add the headers we want to keep to a list and delete
10870           ;; them from the buffer.
10871           (if (or (and (stringp gnus-visible-headers)
10872                        (looking-at gnus-visible-headers))
10873                   (and (not (stringp gnus-visible-headers))
10874                        (stringp gnus-ignored-headers)
10875                        (not (looking-at gnus-ignored-headers))))
10876               (progn
10877                 (setq beg (point))
10878                 (forward-line 1)
10879                 ;; Be sure to get multi-line headers...
10880                 (re-search-forward "^[^ \t]*:" nil t)
10881                 (beginning-of-line)
10882                 (setq want-list 
10883                       (cons (buffer-substring beg (point)) want-list))
10884                 (delete-region beg (point))
10885                 (goto-char beg))
10886             (forward-line 1)))
10887         ;; Next we perform the sorting by looking at
10888         ;; `gnus-sorted-header-list'. 
10889         (goto-char (point-min))
10890         (while (and sorted want-list)
10891           (setq want-l want-list)
10892           (while (and want-l
10893                       (not (string-match (car sorted) (car want-l))))
10894             (setq want-l (cdr want-l)))
10895           (if want-l 
10896               (progn
10897                 (insert (car want-l))
10898                 (setq want-list (delq (car want-l) want-list))))
10899           (setq sorted (cdr sorted)))
10900         ;; Any headers that were not matched by the sorted list we
10901         ;; just tack on the end of the visible header list.
10902         (while want-list
10903           (insert (car want-list))
10904           (setq want-list (cdr want-list)))
10905         ;; And finally we make the unwanted headers invisible.
10906         (if delete
10907             (delete-region (point) (point-max))
10908           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10909           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10910
10911 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10912 (defun gnus-article-treat-overstrike ()
10913   "Translate overstrikes into bold text."
10914   (interactive)
10915   (save-excursion
10916     (set-buffer gnus-article-buffer)
10917     (let ((buffer-read-only nil))
10918       (while (search-forward "\b" nil t)
10919         (let ((next (following-char))
10920               (previous (char-after (- (point) 2))))
10921           (cond ((eq next previous)
10922                  (delete-region (- (point) 2) (point))
10923                  (put-text-property (point) (1+ (point))
10924                                     'face 'bold))
10925                 ((eq next ?_)
10926                  (delete-region (1- (point)) (1+ (point)))
10927                  (put-text-property (1- (point)) (point)
10928                                     'face 'underline))
10929                 ((eq previous ?_)
10930                  (delete-region (- (point) 2) (point))
10931                  (put-text-property (point) (1+ (point))
10932                                     'face 'underline))))))))
10933
10934 (defun gnus-article-word-wrap ()
10935   "Format too long lines."
10936   (interactive)
10937   (save-excursion
10938     (set-buffer gnus-article-buffer)
10939     (let ((buffer-read-only nil))
10940       (goto-char (point-min))
10941       (search-forward "\n\n" nil t)
10942       (end-of-line 1)
10943       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10944             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10945             (adaptive-fill-mode t))
10946         (while (not (eobp))
10947           (and (>= (current-column) (min fill-column (window-width)))
10948                (/= (preceding-char) ?:)
10949                (fill-paragraph nil))
10950           (end-of-line 2))))))
10951
10952 (defun gnus-article-remove-cr ()
10953   "Remove carriage returns from an article."
10954   (interactive)
10955   (save-excursion
10956     (set-buffer gnus-article-buffer)
10957     (let ((buffer-read-only nil))
10958       (goto-char (point-min))
10959       (while (search-forward "\r" nil t)
10960         (replace-match "" t t)))))
10961
10962 (defun gnus-article-display-x-face (&optional force)
10963   "Look for an X-Face header and display it if present."
10964   (interactive (list 'force))
10965   (save-excursion
10966     (set-buffer gnus-article-buffer)
10967     (let ((inhibit-point-motion-hooks t)
10968           (case-fold-search nil))
10969       (save-restriction
10970         (goto-char (point-min))
10971         (search-forward "\n\n")
10972         (narrow-to-region (point-min) (point))
10973         (goto-char (point-min))
10974         (if (or (not gnus-article-x-face-command)
10975                 (and (not force)
10976                      (or (not gnus-article-x-face-too-ugly)
10977                          (string-match gnus-article-x-face-too-ugly
10978                                        (mail-fetch-field "from"))))
10979                 (progn
10980                   (goto-char (point-min))
10981                   (not (re-search-forward "^X-Face: " nil t))))
10982             nil
10983           (let ((beg (point))
10984                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10985             (if (symbolp gnus-article-x-face-command)
10986                 (and (or (fboundp gnus-article-x-face-command)
10987                          (error "%s is not a function"
10988                                 gnus-article-x-face-command))
10989                      (funcall gnus-article-x-face-command beg end))
10990               (call-process-region beg end "sh" nil 0 nil
10991                                    "-c" gnus-article-x-face-command))))))))
10992
10993 (defun gnus-article-de-quoted-unreadable (&optional force)
10994   "Do a naïve translation of a quoted-printable-encoded article.
10995 This is in no way, shape or form meant as a replacement for real MIME
10996 processing, but is simply a stop-gap measure until MIME support is
10997 written.
10998 If FORCE, decode the article whether it is marked as quoted-printable
10999 or not." 
11000   (interactive (list 'force))
11001   (save-excursion
11002     (set-buffer gnus-article-buffer)
11003     (let ((case-fold-search t)
11004           (buffer-read-only nil)
11005           (type (gnus-fetch-field "content-transfer-encoding")))
11006       (if (or force (and type (string-match "quoted-printable" type)))
11007           (progn
11008             (goto-char (point-min))
11009             (search-forward "\n\n" nil 'move)
11010             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11011
11012 (defun gnus-mime-decode-quoted-printable (from to)
11013   ;; Decode quoted-printable from region between FROM and TO.
11014   (save-excursion
11015     (goto-char from)
11016     (while (search-forward "=" to t)
11017       (cond ((eq (following-char) ?\n)
11018              (delete-char -1)
11019              (delete-char 1))
11020             ((looking-at "[0-9A-F][0-9A-F]")
11021              (delete-char -1)
11022              (insert (hexl-hex-string-to-integer
11023                       (buffer-substring (point) (+ 2 (point)))))
11024              (delete-char 2))
11025             ((looking-at "=")
11026              (delete-char 1))
11027             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11028
11029 (defvar gnus-article-time-units
11030   (list (cons 'year (* 365.25 24 60 60))
11031         (cons 'week (* 7 24 60 60))
11032         (cons 'day (* 24 60 60))
11033         (cons 'hour (* 60 60))
11034         (cons 'minute 60)
11035         (cons 'second 1)))
11036
11037 (defun gnus-article-date-ut (&optional type)
11038   "Convert DATE date to universal time in the current article.
11039 If TYPE is `local', convert to local time; if it is `lapsed', output
11040 how much time has lapsed since DATE."
11041   (interactive (list 'ut))
11042   (let ((date (header-date (or gnus-current-headers 
11043                                (gnus-get-header-by-number
11044                                 (gnus-summary-article-number))"")))
11045         (date-regexp "^Date: \\|^X-Sent: "))
11046     (if (or (not date)
11047             (string= date ""))
11048         ()
11049       (save-excursion
11050         (set-buffer gnus-article-buffer)
11051         (let ((buffer-read-only nil))
11052           (goto-char (point-min))
11053           (if (and (re-search-forward date-regexp nil t)
11054                    (progn 
11055                      (beginning-of-line)
11056                      (looking-at date-regexp)))
11057               (delete-region (gnus-point-at-bol)
11058                              (progn (end-of-line) (1+ (point))))
11059             (goto-char (point-min))
11060             (goto-char (- (search-forward "\n\n") 2)))
11061           (insert
11062            (cond 
11063             ((eq type 'local)
11064              (concat "Date: " (condition-case ()
11065                                   (timezone-make-date-arpa-standard date)
11066                                 (error date))
11067                      "\n"))
11068             ((eq type 'ut)
11069              (concat "Date: "
11070                      (condition-case ()
11071                          (timezone-make-date-arpa-standard date nil "UT")
11072                        (error date))
11073                      "\n"))
11074             ((eq type 'lapsed)
11075              ;; If the date is seriously mangled, the timezone
11076              ;; functions are liable to bug out, so we condition-case
11077              ;; the entire thing.  
11078              (let* ((real-sec (condition-case ()
11079                                   (- (gnus-seconds-since-epoch 
11080                                       (timezone-make-date-arpa-standard
11081                                        (current-time-string) 
11082                                        (current-time-zone) "UT"))
11083                                      (gnus-seconds-since-epoch 
11084                                       (timezone-make-date-arpa-standard 
11085                                        date nil "UT")))
11086                                 (error 0)))
11087                     (sec (abs real-sec))
11088                     num prev)
11089                (if (zerop sec)
11090                    "X-Sent: Now\n"
11091                  (concat
11092                   "X-Sent: "
11093                   (mapconcat 
11094                    (lambda (unit)
11095                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11096                          ""
11097                        (setq sec (- sec (* num (cdr unit))))
11098                        (prog1
11099                            (concat (if prev ", " "") (int-to-string 
11100                                                       (floor num))
11101                                    " " (symbol-name (car unit))
11102                                    (if (> num 1) "s" ""))
11103                          (setq prev t))))
11104                    gnus-article-time-units "")
11105                   (if (> real-sec 0)
11106                       " ago\n"
11107                     " in the future\n")))))
11108             (t
11109              (error "Unknown conversion type: %s" type)))))))))
11110
11111 (defun gnus-article-date-local ()
11112   "Convert the current article date to the local timezone."
11113   (interactive)
11114   (gnus-article-date-ut 'local))
11115
11116 (defun gnus-article-date-lapsed ()
11117   "Convert the current article date to time lapsed since it was sent."
11118   (interactive)
11119   (gnus-article-date-ut 'lapsed))
11120
11121 (defun gnus-article-maybe-highlight ()
11122   "Do some article highlighting if `gnus-visual' is non-nil."
11123   (if gnus-visual (gnus-article-highlight-some)))
11124
11125 ;; Article savers.
11126
11127 (defun gnus-output-to-rmail (file-name)
11128   "Append the current article to an Rmail file named FILE-NAME."
11129   (require 'rmail)
11130   ;; Most of these codes are borrowed from rmailout.el.
11131   (setq file-name (expand-file-name file-name))
11132   (setq rmail-default-rmail-file file-name)
11133   (let ((artbuf (current-buffer))
11134         (tmpbuf (get-buffer-create " *Gnus-output*")))
11135     (save-excursion
11136       (or (get-file-buffer file-name)
11137           (file-exists-p file-name)
11138           (if (gnus-yes-or-no-p
11139                (concat "\"" file-name "\" does not exist, create it? "))
11140               (let ((file-buffer (create-file-buffer file-name)))
11141                 (save-excursion
11142                   (set-buffer file-buffer)
11143                   (rmail-insert-rmail-file-header)
11144                   (let ((require-final-newline nil))
11145                     (write-region (point-min) (point-max) file-name t 1)))
11146                 (kill-buffer file-buffer))
11147             (error "Output file does not exist")))
11148       (set-buffer tmpbuf)
11149       (buffer-disable-undo (current-buffer))
11150       (erase-buffer)
11151       (insert-buffer-substring artbuf)
11152       (gnus-convert-article-to-rmail)
11153       ;; Decide whether to append to a file or to an Emacs buffer.
11154       (let ((outbuf (get-file-buffer file-name)))
11155         (if (not outbuf)
11156             (append-to-file (point-min) (point-max) file-name)
11157           ;; File has been visited, in buffer OUTBUF.
11158           (set-buffer outbuf)
11159           (let ((buffer-read-only nil)
11160                 (msg (and (boundp 'rmail-current-message)
11161                           (symbol-value 'rmail-current-message))))
11162             ;; If MSG is non-nil, buffer is in RMAIL mode.
11163             (if msg
11164                 (progn (widen)
11165                        (narrow-to-region (point-max) (point-max))))
11166             (insert-buffer-substring tmpbuf)
11167             (if msg
11168                 (progn
11169                   (goto-char (point-min))
11170                   (widen)
11171                   (search-backward "\^_")
11172                   (narrow-to-region (point) (point-max))
11173                   (goto-char (1+ (point-min)))
11174                   (rmail-count-new-messages t)
11175                   (rmail-show-message msg)))))))
11176     (kill-buffer tmpbuf)))
11177
11178 (defun gnus-output-to-file (file-name)
11179   "Append the current article to a file named FILE-NAME."
11180   (setq file-name (expand-file-name file-name))
11181   (let ((artbuf (current-buffer))
11182         (tmpbuf (get-buffer-create " *Gnus-output*")))
11183     (save-excursion
11184       (set-buffer tmpbuf)
11185       (buffer-disable-undo (current-buffer))
11186       (erase-buffer)
11187       (insert-buffer-substring artbuf)
11188       ;; Append newline at end of the buffer as separator, and then
11189       ;; save it to file.
11190       (goto-char (point-max))
11191       (insert "\n")
11192       (append-to-file (point-min) (point-max) file-name))
11193     (kill-buffer tmpbuf)))
11194
11195 (defun gnus-convert-article-to-rmail ()
11196   "Convert article in current buffer to Rmail message format."
11197   (let ((buffer-read-only nil))
11198     ;; Convert article directly into Babyl format.
11199     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11200     (goto-char (point-min))
11201     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11202     (while (search-forward "\n\^_" nil t) ;single char
11203       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11204     (goto-char (point-max))
11205     (insert "\^_")))
11206
11207 (defun gnus-narrow-to-page (&optional arg)
11208   "Make text outside current page invisible except for page delimiter.
11209 A numeric arg specifies to move forward or backward by that many pages,
11210 thus showing a page other than the one point was originally in."
11211   (interactive "P")
11212   (setq arg (if arg (prefix-numeric-value arg) 0))
11213   (save-excursion
11214     (forward-page -1)                   ;Beginning of current page.
11215     (widen)
11216     (if (> arg 0)
11217         (forward-page arg)
11218       (if (< arg 0)
11219           (forward-page (1- arg))))
11220     ;; Find the end of the page.
11221     (forward-page)
11222     ;; If we stopped due to end of buffer, stay there.
11223     ;; If we stopped after a page delimiter, put end of restriction
11224     ;; at the beginning of that line.
11225     ;; These are commented out.
11226     ;;    (if (save-excursion (beginning-of-line)
11227     ;;                  (looking-at page-delimiter))
11228     ;;  (beginning-of-line))
11229     (narrow-to-region (point)
11230                       (progn
11231                         ;; Find the top of the page.
11232                         (forward-page -1)
11233                         ;; If we found beginning of buffer, stay there.
11234                         ;; If extra text follows page delimiter on same line,
11235                         ;; include it.
11236                         ;; Otherwise, show text starting with following line.
11237                         (if (and (eolp) (not (bobp)))
11238                             (forward-line 1))
11239                         (point)))))
11240
11241 (defun gnus-gmt-to-local ()
11242   "Rewrite Date header described in GMT to local in current buffer.
11243 Intended to be used with gnus-article-prepare-hook."
11244   (save-excursion
11245     (save-restriction
11246       (widen)
11247       (goto-char (point-min))
11248       (narrow-to-region (point-min)
11249                         (progn (search-forward "\n\n" nil 'move) (point)))
11250       (goto-char (point-min))
11251       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11252           (let ((buffer-read-only nil)
11253                 (date (buffer-substring-no-properties
11254                        (match-beginning 1) (match-end 1))))
11255             (delete-region (match-beginning 1) (match-end 1))
11256             (insert
11257              (timezone-make-date-arpa-standard 
11258               date nil (current-time-zone))))))))
11259
11260
11261 ;; Article mode commands
11262
11263 (defun gnus-article-next-page (&optional lines)
11264   "Show next page of current article.
11265 If end of article, return non-nil. Otherwise return nil.
11266 Argument LINES specifies lines to be scrolled up."
11267   (interactive "P")
11268   (move-to-window-line -1)
11269   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11270   (if (save-excursion
11271         (end-of-line)
11272         (and (pos-visible-in-window-p)  ;Not continuation line.
11273              (eobp)))
11274       ;; Nothing in this page.
11275       (if (or (not gnus-break-pages)
11276               (save-excursion
11277                 (save-restriction
11278                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11279           t                             ;Nothing more.
11280         (gnus-narrow-to-page 1)         ;Go to next page.
11281         nil)
11282     ;; More in this page.
11283     (condition-case ()
11284         (scroll-up lines)
11285       (end-of-buffer
11286        ;; Long lines may cause an end-of-buffer error.
11287        (goto-char (point-max))))
11288     nil))
11289
11290 (defun gnus-article-prev-page (&optional lines)
11291   "Show previous page of current article.
11292 Argument LINES specifies lines to be scrolled down."
11293   (interactive "P")
11294   (move-to-window-line 0)
11295   (if (and gnus-break-pages
11296            (bobp)
11297            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11298       (progn
11299         (gnus-narrow-to-page -1) ;Go to previous page.
11300         (goto-char (point-max))
11301         (recenter -1))
11302     (scroll-down lines)))
11303
11304 (defun gnus-article-refer-article ()
11305   "Read article specified by message-id around point."
11306   (interactive)
11307   (search-forward ">" nil t)    ;Move point to end of "<....>".
11308   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11309       (let ((message-id
11310              (buffer-substring (match-beginning 1) (match-end 1))))
11311         (set-buffer gnus-summary-buffer)
11312         (gnus-summary-refer-article message-id))
11313     (error "No references around point")))
11314
11315 (defun gnus-article-show-summary ()
11316   "Reconfigure windows to show summary buffer."
11317   (interactive)
11318   (gnus-configure-windows 'article)
11319   (gnus-summary-goto-subject gnus-current-article))
11320
11321 (defun gnus-article-describe-briefly ()
11322   "Describe article mode commands briefly."
11323   (interactive)
11324   (gnus-message 6
11325    (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")))
11326
11327 (defun gnus-article-summary-command ()
11328   "Execute the last keystroke in the summary buffer."
11329   (interactive)
11330   (let ((obuf (current-buffer))
11331         (owin (current-window-configuration))
11332         func)
11333     (switch-to-buffer gnus-summary-buffer 'norecord)
11334     (setq func (lookup-key (current-local-map) (this-command-keys)))
11335     (call-interactively func)
11336     (set-buffer obuf)
11337     (set-window-configuration owin)
11338     (set-window-point (get-buffer-window (current-buffer)) (point))))
11339
11340 (defun gnus-article-summary-command-nosave ()
11341   "Execute the last keystroke in the summary buffer."
11342   (interactive)
11343   (let (func)
11344     (pop-to-buffer gnus-summary-buffer 'norecord)
11345     (setq func (lookup-key (current-local-map) (this-command-keys)))
11346     (call-interactively func)))
11347
11348 \f
11349 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11350
11351 ;;;###autoload
11352 (defalias 'gnus-batch-kill 'gnus-batch-score)
11353 ;;;###autoload
11354 (defun gnus-batch-score ()
11355   "Run batched scoring.
11356 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11357 Newsgroups is a list of strings in Bnews format.  If you want to score
11358 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11359 score the alt hierarchy, you'd say \"!alt.all\"."
11360   (interactive)
11361   (let* ((yes-and-no
11362           (gnus-newsrc-parse-options
11363            (apply (function concat)
11364                   (mapcar (lambda (g) (concat g " "))
11365                           command-line-args-left))))
11366          (gnus-expert-user t)
11367          (nnmail-spool-file nil)
11368          (gnus-use-dribble-file nil)
11369          (yes (car yes-and-no))
11370          (no (cdr yes-and-no))
11371          group newsrc entry
11372          ;; Disable verbose message.
11373          gnus-novice-user gnus-large-newsgroup)
11374     ;; Eat all arguments.
11375     (setq command-line-args-left nil)
11376     ;; Start Gnus.
11377     (gnus)
11378     ;; Apply kills to specified newsgroups in command line arguments.
11379     (setq newsrc (cdr gnus-newsrc-alist))
11380     (while newsrc
11381       (setq group (car (car newsrc)))
11382       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11383       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11384                (and (car entry)
11385                     (or (eq (car entry) t)
11386                         (not (zerop (car entry)))))
11387                (if yes (string-match yes group) t)
11388                (or (null no) (not (string-match no group))))
11389           (progn
11390             (gnus-summary-read-group group nil t)
11391             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11392                  (gnus-summary-exit))))
11393       (setq newsrc (cdr newsrc)))
11394     ;; Exit Emacs.
11395     (switch-to-buffer gnus-group-buffer)
11396     (gnus-group-save-newsrc)))
11397
11398 (defun gnus-apply-kill-file ()
11399   "Apply a kill file to the current newsgroup.
11400 Returns the number of articles marked as read."
11401   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11402           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11403       (gnus-apply-kill-file-internal)
11404     0))
11405
11406 (defun gnus-kill-save-kill-buffer ()
11407   (save-excursion
11408     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11409       (if (get-file-buffer file)
11410           (progn
11411             (set-buffer (get-file-buffer file))
11412             (and (buffer-modified-p) (save-buffer))
11413             (kill-buffer (current-buffer)))))))
11414
11415 (defvar gnus-kill-file-name "KILL"
11416   "Suffix of the kill files.")
11417
11418 (defun gnus-newsgroup-kill-file (newsgroup)
11419   "Return the name of a kill file name for NEWSGROUP.
11420 If NEWSGROUP is nil, return the global kill file name instead."
11421   (cond ((or (null newsgroup)
11422              (string-equal newsgroup ""))
11423          ;; The global KILL file is placed at top of the directory.
11424          (expand-file-name gnus-kill-file-name
11425                            (or gnus-kill-files-directory "~/News")))
11426         ((gnus-use-long-file-name 'not-kill)
11427          ;; Append ".KILL" to newsgroup name.
11428          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11429                                    "." gnus-kill-file-name)
11430                            (or gnus-kill-files-directory "~/News")))
11431         (t
11432          ;; Place "KILL" under the hierarchical directory.
11433          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11434                                    "/" gnus-kill-file-name)
11435                            (or gnus-kill-files-directory "~/News")))))
11436
11437 \f
11438 ;;;
11439 ;;; Dribble file
11440 ;;;
11441
11442 (defvar gnus-dribble-ignore nil)
11443 (defvar gnus-dribble-eval-file nil)
11444
11445 (defun gnus-dribble-file-name ()
11446   (concat gnus-current-startup-file "-dribble"))
11447
11448 (defun gnus-dribble-enter (string)
11449   (if (and (not gnus-dribble-ignore)
11450            gnus-dribble-buffer
11451            (buffer-name gnus-dribble-buffer))
11452       (let ((obuf (current-buffer)))
11453         (set-buffer gnus-dribble-buffer)
11454         (insert string "\n")
11455         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11456         (set-buffer obuf))))
11457
11458 (defun gnus-dribble-read-file ()
11459   (let ((dribble-file (gnus-dribble-file-name)))
11460     (save-excursion 
11461       (set-buffer (setq gnus-dribble-buffer 
11462                         (get-buffer-create 
11463                          (file-name-nondirectory dribble-file))))
11464       (gnus-add-current-to-buffer-list)
11465       (erase-buffer)
11466       (set-visited-file-name dribble-file)
11467       (buffer-disable-undo (current-buffer))
11468       (bury-buffer (current-buffer))
11469       (set-buffer-modified-p nil)
11470       (let ((auto (make-auto-save-file-name))
11471             (gnus-dribble-ignore t))
11472         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11473             (progn
11474               (if (file-newer-than-file-p auto dribble-file)
11475                   (setq dribble-file auto))
11476               (insert-file-contents dribble-file)
11477               (if (not (zerop (buffer-size)))
11478                   (set-buffer-modified-p t))
11479               (if (gnus-y-or-n-p 
11480                    "Auto-save file exists. Do you want to read it? ")
11481                   (setq gnus-dribble-eval-file t))))))))
11482
11483 (defun gnus-dribble-eval-file ()
11484   (if (not gnus-dribble-eval-file)
11485       ()
11486     (setq gnus-dribble-eval-file nil)
11487     (save-excursion
11488       (let ((gnus-dribble-ignore t))
11489         (set-buffer gnus-dribble-buffer)
11490         (eval-buffer (current-buffer))))))
11491
11492 (defun gnus-dribble-delete-file ()
11493   (if (file-exists-p (gnus-dribble-file-name))
11494       (delete-file (gnus-dribble-file-name)))
11495   (if gnus-dribble-buffer
11496       (save-excursion
11497         (set-buffer gnus-dribble-buffer)
11498         (let ((auto (make-auto-save-file-name)))
11499           (if (file-exists-p auto)
11500               (delete-file auto))
11501           (erase-buffer)
11502           (set-buffer-modified-p nil)))))
11503
11504 (defun gnus-dribble-save ()
11505   (if (and gnus-dribble-buffer
11506            (buffer-name gnus-dribble-buffer))
11507       (save-excursion
11508         (set-buffer gnus-dribble-buffer)
11509         (save-buffer))))
11510
11511 (defun gnus-dribble-clear ()
11512   (save-excursion
11513     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11514         (progn
11515           (set-buffer gnus-dribble-buffer)
11516           (erase-buffer)
11517           (set-buffer-modified-p nil)
11518           (setq buffer-saved-size (buffer-size))))))
11519
11520 ;;;
11521 ;;; Server Communication
11522 ;;;
11523
11524 ;; All the Gnus backends have the same interface, and should return
11525 ;; data in a similar format. Below is an overview of what functions
11526 ;; these packages must supply and what results they should return.
11527 ;;
11528 ;; Variables:
11529 ;;
11530 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11531 ;; buffer. 
11532 ;;
11533 ;; Functions for the imaginary backend `choke':
11534 ;;
11535 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11536 ;; Should return all headers for all ARTICLES, or return NOV lines for
11537 ;; the same.
11538 ;;
11539 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11540 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11541 ;; must be returned.
11542 ;;
11543 ;; `choke-close-group GROUP &optional SERVER'
11544 ;; Close group. Most backends won't have to do anything with this
11545 ;; call, but it is an opportunity to clean up, if that is needed. It
11546 ;; is called when Gnus exits a group.
11547 ;;
11548 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11549 ;; Return ARTICLE, which is either an article number or
11550 ;; message-id. Note that not all backends can return articles based on
11551 ;; message-id. 
11552 ;;
11553 ;; `choke-request-list SERVER'
11554 ;; Return a list of all newsgroups on SERVER.
11555 ;;
11556 ;; `choke-request-list-newsgroups SERVER'
11557 ;; Return a list of descriptions of all newsgroups on SERVER.
11558 ;;
11559 ;; `choke-request-newgroups DATE &optional SERVER'
11560 ;; Return a list of all groups that have arrived after DATE on
11561 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11562 ;; always check whether the groups are old or not. Backends that do
11563 ;; not store date information may just return the entire list of
11564 ;; groups, although this might not be a good idea in general.
11565 ;;
11566 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11567 ;; Should return a buffer that is suitable for "posting". nnspool and
11568 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11569 ;; buffer. This function should fill out the appropriate headers. 
11570 ;;
11571 ;; `choke-request-post &optional SERVER'
11572 ;; Function that will be called from a buffer to be posted. 
11573 ;;
11574 ;; `choke-open-server SERVER &optional ARGUMENT'
11575 ;; Open a connection to SERVER.
11576 ;;
11577 ;; `choke-close-server &optional SERVER'
11578 ;; Close the connection to SERVER.
11579 ;;
11580 ;; `choke-server-opened &optional SERVER'
11581 ;; Whether the conenction to SERVER is opened or not.
11582 ;;
11583 ;; `choke-server-status &optional SERVER'
11584 ;; Should return a status string (not in the nntp buffer, but as the
11585 ;; result of the function).
11586 ;;
11587 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11588 ;; Optional function for retrieving active file info on all groups in
11589 ;; GROUPS.  Two return formats are supported: The normal active file
11590 ;; format, and a list of GROUP lines.  This function should return (as
11591 ;; a function value) either `active' or `group', depending on what
11592 ;; format it returns.
11593 ;;
11594 ;; The following functions are optional and apply only to backends
11595 ;; that are able to control the contents of their groups totally
11596 ;; (ie. mail backends.)  Backends that aren't able to do that
11597 ;; shouldn't define these functions at all. Gnus will check for their
11598 ;; presence before attempting to call them.
11599 ;;
11600 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11601 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11602 ;; backends will not be able to expire articles. Should return a list
11603 ;; of all articles that were not expired.
11604 ;;
11605 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11606 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11607 ;; Removes any information it has added to the article (extra headers,
11608 ;; whatever - make it as clean as possible), and then passes the
11609 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11610 ;; function described below. If the ACCEPT-FORM returns a non-nil
11611 ;; value, the article should then be deleted. If LAST is nil, that
11612 ;; means that there will be further calls to this function. This might
11613 ;; be taken as an advice not to save buffers/internal variables just
11614 ;; yet, but wait until the last call to speed things up.
11615 ;;
11616 ;; `choke-request-accept-article GROUP &optional LAST' 
11617 ;; The contents of the current buffer will be put into GROUP.  There
11618 ;; should, of course, be an article in the current buffer.  This
11619 ;; function is normally only called by the function described above,
11620 ;; and LAST works the same way as in that function.
11621 ;;
11622 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11623 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11624 ;; This provides an easy interface for allowing editing of
11625 ;; articles. Note that even headers may be edited, so the backend has
11626 ;; to update any tables (nov buffers, etc) that it maintains after
11627 ;; replacing the article.
11628 ;;
11629 ;; `choke-request-create-group GROUP &optional SERVER'
11630 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11631 ;; might be a group that already exists, but hasn't been registered
11632 ;; yet. 
11633 ;;
11634 ;; All these functions must return nil if they couldn't service the
11635 ;; request. If the optional arguments are not supplied, some "current"
11636 ;; or "default" values should be used. In short, one should emulate an
11637 ;; NNTP server, in a way.
11638 ;;
11639 ;; If you want to write a new backend, you just have to supply the
11640 ;; functions listed above. In addition, you must enter the new backend
11641 ;; into the list of valid select methods:
11642 ;; (setq gnus-valid-select-methods 
11643 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11644 ;; The first element in this list is the name of the backend. Other
11645 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11646 ;; groups), `none' (neither), `respool' (for groups that can control
11647 ;; their contents). 
11648
11649 (defun gnus-start-news-server (&optional confirm)
11650   "Open a method for getting news.
11651 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11652   (let (how)
11653     (if gnus-current-select-method
11654         ;; Stream is already opened.
11655         nil
11656       ;; Open NNTP server.
11657       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11658       (if confirm
11659           (progn
11660             ;; Read server name with completion.
11661             (setq gnus-nntp-server
11662                   (completing-read "NNTP server: "
11663                                    (mapcar (lambda (server) (list server))
11664                                            (cons (list gnus-nntp-server)
11665                                                  gnus-secondary-servers))
11666                                    nil nil gnus-nntp-server))))
11667
11668       (if (and gnus-nntp-server 
11669                (stringp gnus-nntp-server)
11670                (not (string= gnus-nntp-server "")))
11671           (setq gnus-select-method
11672                 (cond ((or (string= gnus-nntp-server "")
11673                            (string= gnus-nntp-server "::"))
11674                        (list 'nnspool (system-name)))
11675                       ((string-match "^:" gnus-nntp-server)
11676                        (list 'nnmh gnus-nntp-server 
11677                              (list 'nnmh-directory 
11678                                    (file-name-as-directory
11679                                     (expand-file-name
11680                                      (concat "~/" (substring
11681                                                    gnus-nntp-server 1)))))
11682                              (list 'nnmh-get-new-mail nil)))
11683                       (t
11684                        (list 'nntp gnus-nntp-server)))))
11685
11686       (setq how (car gnus-select-method))
11687       (cond ((eq how 'nnspool)
11688              (require 'nnspool)
11689              (gnus-message 5 "Looking up local news spool..."))
11690             ((eq how 'nnmh)
11691              (require 'nnmh)
11692              (gnus-message 5 "Looking up mh spool..."))
11693             (t
11694              (require 'nntp)))
11695       (setq gnus-current-select-method gnus-select-method)
11696       (run-hooks 'gnus-open-server-hook)
11697       (or 
11698        ;; gnus-open-server-hook might have opened it
11699        (gnus-server-opened gnus-select-method)  
11700        (gnus-open-server gnus-select-method)
11701        (gnus-y-or-n-p
11702         (format
11703          "%s server on %s can't be opened. Continue? "
11704          (car gnus-select-method) (nth 1 gnus-select-method)))
11705        (progn
11706          (gnus-message 1 "Couldn't open server on %s" 
11707                        (nth 1 gnus-select-method))
11708          (ding)
11709          nil)))))
11710
11711 (defun gnus-check-server (&optional method)
11712   "If the news server is down, start it up again."
11713   (let ((method (if method method gnus-select-method)))
11714     (and (stringp method)
11715          (setq method (gnus-server-to-method method)))
11716     (if (gnus-server-opened method)
11717         ;; Stream is already opened.
11718         t
11719       ;; Open server.
11720       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11721       (run-hooks 'gnus-open-server-hook)
11722       (prog1
11723           (gnus-open-server method)
11724         (message "")))))
11725
11726 (defun gnus-nntp-message (&optional message)
11727   "Check the status of the NNTP server.
11728 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11729 is returned insted of the status string."
11730   (let ((status (gnus-status-message (gnus-find-method-for-group 
11731                                       gnus-newsgroup-name)))
11732         (message (or message "")))
11733     (if (and (stringp status) (> (length status) 0))
11734         status message)))
11735
11736 (defun gnus-get-function (method function)
11737   (and (stringp method)
11738        (setq method (gnus-server-to-method method)))
11739   (let ((func (intern (format "%s-%s" (car method) function))))
11740     (if (not (fboundp func)) 
11741         (progn
11742           (require (car method))
11743           (if (not (fboundp func)) 
11744               (error "No such function: %s" func))))
11745     func))
11746
11747 ;;; Interface functions to the backends.
11748
11749 (defun gnus-open-server (method)
11750   (funcall (gnus-get-function method 'open-server)
11751            (nth 1 method) (nthcdr 2 method)))
11752
11753 (defun gnus-close-server (method)
11754   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11755
11756 (defun gnus-request-list (method)
11757   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11758
11759 (defun gnus-request-list-newsgroups (method)
11760   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11761
11762 (defun gnus-request-newgroups (date method)
11763   (funcall (gnus-get-function method 'request-newgroups) 
11764            date (nth 1 method)))
11765
11766 (defun gnus-server-opened (method)
11767   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11768
11769 (defun gnus-status-message (method)
11770   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11771                   method)))
11772     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11773
11774 (defun gnus-request-group (group &optional dont-check)
11775   (let ((method (gnus-find-method-for-group group)))
11776 ;    (and t (message "%s GROUP %s" (car method) group))
11777     (funcall (gnus-get-function method 'request-group) 
11778              (gnus-group-real-name group) (nth 1 method) dont-check)))
11779
11780 (defun gnus-request-asynchronous (group &optional articles)
11781   (let ((method (gnus-find-method-for-group group)))
11782     (funcall (gnus-get-function method 'request-asynchronous) 
11783              (gnus-group-real-name group) (nth 1 method) articles)))
11784
11785 (defun gnus-list-active-group (group)
11786   (let ((method (gnus-find-method-for-group group))
11787         (func 'list-active-group))
11788     (and (gnus-check-backend-function func group)
11789          (funcall (gnus-get-function method func) 
11790                   (gnus-group-real-name group) (nth 1 method)))))
11791
11792 (defun gnus-request-group-description (group)
11793   (let ((method (gnus-find-method-for-group group))
11794         (func 'request-group-description))
11795     (and (gnus-check-backend-function func group)
11796          (funcall (gnus-get-function method func) 
11797                   (gnus-group-real-name group) (nth 1 method)))))
11798
11799 (defun gnus-close-group (group)
11800   (let ((method (gnus-find-method-for-group group)))
11801     (funcall (gnus-get-function method 'close-group) 
11802              (gnus-group-real-name group) (nth 1 method))))
11803
11804 (defun gnus-retrieve-headers (articles group)
11805   (let ((method (gnus-find-method-for-group group)))
11806     (if (and gnus-use-cache (numberp (car articles)))
11807         (gnus-cache-retrieve-headers articles group)
11808       (funcall (gnus-get-function method 'retrieve-headers) 
11809                articles (gnus-group-real-name group) (nth 1 method)))))
11810
11811 (defun gnus-retrieve-groups (groups method)
11812   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11813
11814 (defun gnus-request-article (article group &optional buffer)
11815   (let ((method (gnus-find-method-for-group group)))
11816     (funcall (gnus-get-function method 'request-article) 
11817              article (gnus-group-real-name group) (nth 1 method) buffer)))
11818
11819 (defun gnus-request-head (article group)
11820   (let ((method (gnus-find-method-for-group group)))
11821     (funcall (gnus-get-function method 'request-head) 
11822              article (gnus-group-real-name group) (nth 1 method))))
11823
11824 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11825 (defun gnus-request-post-buffer (post group subject header artbuf
11826                                       info follow-to respect-poster)
11827    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11828                                             group gnus-newsrc-hashtb)))))
11829           (method
11830            (if (and gnus-post-method
11831                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11832                     (memq 'post (assoc
11833                                  (format "%s" (car (gnus-find-method-for-group
11834                                                     gnus-newsgroup-name)))
11835                                         gnus-valid-select-methods)))
11836                gnus-post-method
11837              (gnus-find-method-for-group gnus-newsgroup-name))))
11838      (or (gnus-check-server method)
11839          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11840      (let ((mail-self-blind nil)
11841            (mail-archive-file-name nil))
11842        (funcall (gnus-get-function method 'request-post-buffer) 
11843                 post group subject header artbuf info follow-to
11844                 respect-poster))))
11845
11846 (defun gnus-request-post (method &optional force)
11847   (and (stringp method)
11848        (setq method (gnus-server-to-method method)))
11849   (and (not force) gnus-post-method
11850        (memq 'post (assoc (format "%s" (car method))
11851                           gnus-valid-select-methods))
11852        (setq method gnus-post-method))
11853   (funcall (gnus-get-function method 'request-post) 
11854            (nth 1 method)))
11855
11856 (defun gnus-request-expire-articles (articles group &optional force)
11857   (let ((method (gnus-find-method-for-group group)))
11858     (funcall (gnus-get-function method 'request-expire-articles) 
11859              articles (gnus-group-real-name group) (nth 1 method)
11860              force)))
11861
11862 (defun gnus-request-move-article 
11863   (article group server accept-function &optional last)
11864   (let ((method (gnus-find-method-for-group group)))
11865     (funcall (gnus-get-function method 'request-move-article) 
11866              article (gnus-group-real-name group) 
11867              (nth 1 method) accept-function last)))
11868
11869 (defun gnus-request-accept-article (group &optional last)
11870   (let ((func (if (symbolp group) group
11871                 (car (gnus-find-method-for-group group)))))
11872     (funcall (intern (format "%s-request-accept-article" func))
11873              (if (stringp group) (gnus-group-real-name group) group)
11874              last)))
11875
11876 (defun gnus-request-replace-article (article group buffer)
11877   (let ((func (car (gnus-find-method-for-group group))))
11878     (funcall (intern (format "%s-request-replace-article" func))
11879              article (gnus-group-real-name group) buffer)))
11880
11881 (defun gnus-request-create-group (group)
11882   (let ((method (gnus-find-method-for-group group)))
11883     (funcall (gnus-get-function method 'request-create-group) 
11884              (gnus-group-real-name group) (nth 1 method))))
11885
11886 (defun gnus-member-of-valid (symbol group)
11887   (memq symbol (assoc
11888                 (format "%s" (car (gnus-find-method-for-group group)))
11889                 gnus-valid-select-methods)))
11890
11891 (defun gnus-secondary-method-p (method)
11892   (let ((methods gnus-secondary-select-methods)
11893         (gmethod (gnus-server-get-method nil method)))
11894     (while (and methods
11895                 (not (equal (gnus-server-get-method nil (car methods)) 
11896                             gmethod)))
11897       (setq methods (cdr methods)))
11898     methods))
11899
11900 (defun gnus-find-method-for-group (group &optional info)
11901   (or gnus-override-method
11902       (and (not group)
11903            gnus-select-method)
11904       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11905             method)
11906         (if (or (not info)
11907                 (not (setq method (nth 4 info))))
11908             (setq method gnus-select-method)
11909           (setq method
11910                 (cond ((stringp method)
11911                        (gnus-server-to-method method))
11912                       ((stringp (car method))
11913                        (gnus-server-extend-method group method))
11914                       (t
11915                        method))))
11916         (gnus-server-add-address method))))
11917
11918 (defun gnus-check-backend-function (func group)
11919   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11920                  group)))
11921     (fboundp (intern (format "%s-%s" method func)))))
11922
11923 (defun gnus-methods-using (method)
11924   (let ((valids gnus-valid-select-methods)
11925         outs)
11926     (while valids
11927       (if (memq method (car valids)) 
11928           (setq outs (cons (car valids) outs)))
11929       (setq valids (cdr valids)))
11930     outs))
11931
11932 ;;; 
11933 ;;; Active & Newsrc File Handling
11934 ;;;
11935
11936 ;; Newsrc related functions.
11937 ;; Gnus internal format of gnus-newsrc-alist:
11938 ;; (("alt.general" 3 (1 . 1))
11939 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11940 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11941 ;; The first item is the group name; the second is the subscription
11942 ;; level; the third is either a range of a list of ranges of read
11943 ;; articles, the optional fourth element is a list of marked articles,
11944 ;; the optional fifth element is the select method.
11945 ;;
11946 ;; Gnus internal format of gnus-newsrc-hashtb:
11947 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11948 ;; This is the entry for "alt.misc". The first element is the number
11949 ;; of unread articles in "alt.misc". The cdr of this entry is the
11950 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11951 ;; trivial to remove or add new elements into gnus-newsrc-alist
11952 ;; without scanning the entire list. So, to get the actual information
11953 ;; of "alt.misc", you'd say something like 
11954 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11955 ;;
11956 ;; Gnus internal format of gnus-active-hashtb:
11957 ;; ((1 . 1))
11958 ;;  (5 . 10))
11959 ;;  (67 . 99)) ...)
11960 ;; The only element in each entry in this hash table is a range of
11961 ;; (possibly) available articles. (Articles in this range may have
11962 ;; been expired or canceled.)
11963 ;;
11964 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11965 ;; ("alt.misc" "alt.test" "alt.general" ...)
11966
11967 (defun gnus-setup-news (&optional rawfile level)
11968   "Setup news information.
11969 If RAWFILE is non-nil, the .newsrc file will also be read.
11970 If LEVEL is non-nil, the news will be set up at level LEVEL."
11971   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11972     ;; Clear some variables to re-initialize news information.
11973     (if init (setq gnus-newsrc-alist nil 
11974                    gnus-active-hashtb nil))
11975
11976     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11977     (if init (gnus-read-newsrc-file rawfile))
11978
11979     ;; Read the active file and create `gnus-active-hashtb'.
11980     ;; If `gnus-read-active-file' is nil, then we just create an empty
11981     ;; hash table. The partial filling out of the hash table will be
11982     ;; done in `gnus-get-unread-articles'.
11983     (if (and gnus-read-active-file 
11984              (not level))
11985         (gnus-read-active-file)
11986       (setq gnus-active-hashtb (make-vector 4095 0)))
11987
11988     ;; Possibly eval the dribble file.
11989     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11990
11991     (gnus-update-format-specifications)
11992
11993     ;; Find the number of unread articles in each non-dead group.
11994     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
11995       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
11996     ;; Find new newsgroups and treat them.
11997     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11998              (gnus-server-opened gnus-select-method))
11999         (gnus-find-new-newsgroups))
12000     (if (and init gnus-check-bogus-newsgroups 
12001              gnus-read-active-file (not level)
12002              (gnus-server-opened gnus-select-method))
12003         (gnus-check-bogus-newsgroups))))
12004
12005 (defun gnus-find-new-newsgroups ()
12006   "Search for new newsgroups and add them.
12007 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12008 The `-n' option line from .newsrc is respected."
12009   (interactive)
12010   (or (gnus-check-first-time-used)
12011       (if (or (consp gnus-check-new-newsgroups)
12012               (eq gnus-check-new-newsgroups 'ask-server))
12013           (gnus-ask-server-for-new-groups)
12014         (let ((groups 0)
12015               group new-newsgroups)
12016           (gnus-message 5 "Checking for new newsgroups...")
12017           (or gnus-have-read-active-file (gnus-read-active-file))
12018           (setq gnus-newsrc-last-checked-date (current-time-string))
12019           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12020           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12021           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12022           (mapatoms
12023            (lambda (sym)
12024              (if (or (null (setq group (symbol-name sym)))
12025                      (null (symbol-value sym))
12026                      (gnus-gethash group gnus-killed-hashtb)
12027                      (gnus-gethash group gnus-newsrc-hashtb))
12028                  ()
12029                (let ((do-sub (gnus-matches-options-n group)))
12030                  (cond 
12031                   ((eq do-sub 'subscribe)
12032                    (setq groups (1+ groups))
12033                    (gnus-sethash group group gnus-killed-hashtb)
12034                    (funcall gnus-subscribe-options-newsgroup-method group))
12035                   ((eq do-sub 'ignore)
12036                    nil)
12037                   (t
12038                    (setq groups (1+ groups))
12039                    (gnus-sethash group group gnus-killed-hashtb)
12040                    (if gnus-subscribe-hierarchical-interactive
12041                        (setq new-newsgroups (cons group new-newsgroups))
12042                      (funcall gnus-subscribe-newsgroup-method group)))))))
12043            gnus-active-hashtb)
12044           (if new-newsgroups 
12045               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12046           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12047           (if (> groups 0)
12048               (gnus-message 6 "%d new newsgroup%s arrived." 
12049                             groups (if (> groups 1) "s have" " has"))
12050             (gnus-message 6 "No new newsgroups."))))))
12051
12052 (defun gnus-matches-options-n (group)
12053   ;; Returns `subscribe' if the group is to be uncoditionally
12054   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12055   ;; no match for the group.
12056
12057   ;; First we check the two user variables.
12058   (cond
12059    ((and gnus-options-subscribe
12060          (string-match gnus-options-subscribe group))
12061     'subscribe)
12062    ((and gnus-options-not-subscribe
12063          (string-match gnus-options-not-subscribe group))
12064     'ignore)
12065    ;; Then we go through the list that was retrieved from the .newsrc
12066    ;; file.  This list has elements on the form 
12067    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12068    ;; is in the reverse order of the options line) is returned.
12069    (t
12070     (let ((regs gnus-newsrc-options-n))
12071       (while (and regs
12072                   (not (string-match (car (car regs)) group)))
12073         (setq regs (cdr regs)))
12074       (and regs (cdr (car regs)))))))
12075
12076 (defun gnus-ask-server-for-new-groups ()
12077   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12078          (methods (cons gnus-select-method 
12079                         (append
12080                          (and (consp gnus-check-new-newsgroups)
12081                               gnus-check-new-newsgroups)
12082                          gnus-secondary-select-methods)))
12083          (groups 0)
12084          (new-date (current-time-string))
12085          hashtb group new-newsgroups got-new method)
12086     ;; Go thorugh both primary and secondary select methods and
12087     ;; request new newsgroups.  
12088     (while methods
12089       (setq method (gnus-server-get-method nil (car methods)))
12090       (and (gnus-check-server method)
12091            (gnus-request-newgroups date method)
12092            (save-excursion
12093              (setq got-new t)
12094              (set-buffer nntp-server-buffer)
12095              (or hashtb (setq hashtb (gnus-make-hashtable 
12096                                       (count-lines (point-min) (point-max)))))
12097              ;; Enter all the new groups in a hashtable.
12098              (gnus-active-to-gnus-format method hashtb 'ignore)))
12099       (setq methods (cdr methods)))
12100     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12101     ;; Now all new groups from all select methods are in `hashtb'.
12102     (mapatoms
12103      (lambda (group-sym)
12104        (setq group (symbol-name group-sym))
12105        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12106                (member group gnus-zombie-list)
12107                (member group gnus-killed-list))
12108            ;; The group is already known.
12109            ()
12110          (and (symbol-value group-sym)
12111               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12112          (let ((do-sub (gnus-matches-options-n group)))
12113            (cond ((eq do-sub 'subscribe)
12114                   (setq groups (1+ groups))
12115                   (gnus-sethash group group gnus-killed-hashtb)
12116                   (funcall 
12117                    gnus-subscribe-options-newsgroup-method group))
12118                  ((eq do-sub 'ignore)
12119                   nil)
12120                  (t
12121                   (setq groups (1+ groups))
12122                   (gnus-sethash group group gnus-killed-hashtb)
12123                   (if gnus-subscribe-hierarchical-interactive
12124                       (setq new-newsgroups (cons group new-newsgroups))
12125                     (funcall gnus-subscribe-newsgroup-method group)))))))
12126      hashtb)
12127     (if new-newsgroups 
12128         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12129     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12130     (if (> groups 0)
12131         (gnus-message 6 "%d new newsgroup%s arrived." 
12132                       groups (if (> groups 1) "s have" " has")))
12133     got-new))
12134
12135 (defun gnus-check-first-time-used ()
12136   (if (or (> (length gnus-newsrc-alist) 1)
12137           (file-exists-p gnus-startup-file)
12138           (file-exists-p (concat gnus-startup-file ".el"))
12139           (file-exists-p (concat gnus-startup-file ".eld")))
12140       nil
12141     (gnus-message 6 "First time user; subscribing you to default groups")
12142     (or gnus-have-read-active-file (gnus-read-active-file))
12143     (setq gnus-newsrc-last-checked-date (current-time-string))
12144     (let ((groups gnus-default-subscribed-newsgroups)
12145           group)
12146       (if (eq groups t)
12147           nil
12148         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12149         (mapatoms
12150          (lambda (sym)
12151            (if (null (setq group (symbol-name sym)))
12152                ()
12153              (let ((do-sub (gnus-matches-options-n group)))
12154                (cond 
12155                 ((eq do-sub 'subscribe)
12156                  (gnus-sethash group group gnus-killed-hashtb)
12157                  (funcall gnus-subscribe-options-newsgroup-method group))
12158                 ((eq do-sub 'ignore)
12159                  nil)
12160                 (t
12161                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12162          gnus-active-hashtb)
12163         (while groups
12164           (if (gnus-gethash (car groups) gnus-active-hashtb)
12165               (gnus-group-change-level 
12166                (car groups) gnus-level-default-subscribed gnus-level-killed))
12167           (setq groups (cdr groups)))
12168         (gnus-group-make-help-group)
12169         (and gnus-novice-user
12170              (gnus-message 7 "`A k' to list killed groups"))))))
12171
12172 (defun gnus-subscribe-group (group previous &optional method)
12173   (gnus-group-change-level 
12174    (if method
12175        (list t group gnus-level-default-subscribed nil nil method)
12176      group) 
12177    gnus-level-default-subscribed gnus-level-killed previous t))
12178
12179 ;; `gnus-group-change-level' is the fundamental function for changing
12180 ;; subscription levels of newsgroups. This might mean just changing
12181 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12182 ;; again, which subscribes/unsubscribes a group, which is equally
12183 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12184 ;; from 8-9 to 1-7 means that you remove the group from the list of
12185 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12186 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12187 ;; which is trivial.
12188 ;; ENTRY can either be a string (newsgroup name) or a list (if
12189 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12190 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12191 ;; entries. 
12192 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12193 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12194 ;; after. 
12195 (defun gnus-group-change-level (entry level &optional oldlevel
12196                                       previous fromkilled)
12197   (let (group info active num)
12198     ;; Glean what info we can from the arguments
12199     (if (consp entry)
12200         (if fromkilled (setq group (nth 1 entry))
12201           (setq group (car (nth 2 entry))))
12202       (setq group entry))
12203     (if (and (stringp entry)
12204              oldlevel 
12205              (< oldlevel gnus-level-zombie))
12206         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12207     (if (and (not oldlevel)
12208              (consp entry))
12209         (setq oldlevel (car (cdr (nth 2 entry)))))
12210     (if (stringp previous)
12211         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12212
12213     (if (and (>= oldlevel gnus-level-zombie)
12214              (gnus-gethash group gnus-newsrc-hashtb))
12215         ;; We are trying to subscribe a group that is already
12216         ;; subscribed. 
12217         () ; Do nothing. 
12218
12219       (gnus-dribble-enter
12220        (format "(gnus-group-change-level %S %S %S %S %S)" 
12221                group level oldlevel (car (nth 2 previous)) fromkilled))
12222     
12223       ;; Then we remove the newgroup from any old structures, if needed.
12224       ;; If the group was killed, we remove it from the killed or zombie
12225       ;; list. If not, and it is in fact going to be killed, we remove
12226       ;; it from the newsrc hash table and assoc.
12227       (cond ((>= oldlevel gnus-level-zombie)
12228              (if (= oldlevel gnus-level-zombie)
12229                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12230                (setq gnus-killed-list (delete group gnus-killed-list))))
12231             (t
12232              (if (and (>= level gnus-level-zombie)
12233                       entry)
12234                  (progn
12235                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12236                    (if (nth 3 entry)
12237                        (setcdr (gnus-gethash (car (nth 3 entry))
12238                                              gnus-newsrc-hashtb)
12239                                (cdr entry)))
12240                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12241
12242       ;; Finally we enter (if needed) the list where it is supposed to
12243       ;; go, and change the subscription level. If it is to be killed,
12244       ;; we enter it into the killed or zombie list.
12245       (cond ((>= level gnus-level-zombie)
12246              ;; Remove from the hash table.
12247              (gnus-sethash group nil gnus-newsrc-hashtb)
12248              (or (gnus-group-foreign-p group)
12249                  ;; We do not enter foreign groups into the list of dead
12250                  ;; groups.  
12251                  (if (= level gnus-level-zombie)
12252                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12253                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12254             (t
12255              ;; If the list is to be entered into the newsrc assoc, and
12256              ;; it was killed, we have to create an entry in the newsrc
12257              ;; hashtb format and fix the pointers in the newsrc assoc.
12258              (if (>= oldlevel gnus-level-zombie)
12259                  (progn
12260                    (if (listp entry)
12261                        (progn
12262                          (setq info (cdr entry))
12263                          (setq num (car entry)))
12264                      (setq active (gnus-gethash group gnus-active-hashtb))
12265                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12266                      ;; Check whether the group is foreign. If so, the
12267                      ;; foreign select method has to be entered into the
12268                      ;; info. 
12269                      (let ((method (gnus-group-method-name group)))
12270                        (if (eq method gnus-select-method)
12271                            (setq info (list group level nil))
12272                          (setq info (list group level nil nil method)))))
12273                    (or previous 
12274                        (setq previous 
12275                              (let ((p gnus-newsrc-alist))
12276                                (while (cdr (cdr p))
12277                                  (setq p (cdr p)))
12278                                p)))
12279                    (setq entry (cons info (cdr (cdr previous))))
12280                    (if (cdr previous)
12281                        (progn
12282                          (setcdr (cdr previous) entry)
12283                          (gnus-sethash group (cons num (cdr previous)) 
12284                                        gnus-newsrc-hashtb))
12285                      (setcdr previous entry)
12286                      (gnus-sethash group (cons num previous)
12287                                    gnus-newsrc-hashtb))
12288                    (if (cdr entry)
12289                        (setcdr (gnus-gethash (car (car (cdr entry)))
12290                                              gnus-newsrc-hashtb)
12291                                entry)))
12292                ;; It was alive, and it is going to stay alive, so we
12293                ;; just change the level and don't change any pointers or
12294                ;; hash table entries.
12295                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12296
12297 (defun gnus-kill-newsgroup (newsgroup)
12298   "Obsolete function. Kills a newsgroup."
12299   (gnus-group-change-level
12300    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12301
12302 (defun gnus-check-bogus-newsgroups (&optional confirm)
12303   "Remove bogus newsgroups.
12304 If CONFIRM is non-nil, the user has to confirm the deletion of every
12305 newsgroup." 
12306   (let ((newsrc (cdr gnus-newsrc-alist))
12307         bogus group entry)
12308     (gnus-message 5 "Checking bogus newsgroups...")
12309     (or gnus-have-read-active-file (gnus-read-active-file))
12310     ;; Find all bogus newsgroup that are subscribed.
12311     (while newsrc
12312       (setq group (car (car newsrc)))
12313       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12314               (nth 4 (car newsrc))      ; Foreign
12315               (and confirm
12316                    (not (gnus-y-or-n-p
12317                          (format "Remove bogus newsgroup: %s " group)))))
12318           ;; Don't remove.
12319           ()
12320         ;; Found a bogus newsgroup.
12321         (setq bogus (cons group bogus)))
12322       (setq newsrc (cdr newsrc)))
12323     ;; Remove all bogus subscribed groups by first killing them, and
12324     ;; then removing them from the list of killed groups.
12325     (while bogus
12326       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12327            (progn
12328              (gnus-group-change-level entry gnus-level-killed)
12329              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12330       (setq bogus (cdr bogus)))
12331     ;; Then we remove all bogus groups from the list of killed and
12332     ;; zombie groups. They are are removed without confirmation.
12333     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12334           killed)
12335       (while dead-lists
12336         (setq killed (symbol-value (car dead-lists)))
12337         (while killed
12338           (setq group (car killed))
12339           (or (gnus-gethash group gnus-active-hashtb)
12340               ;; The group is bogus.
12341               (set (car dead-lists)
12342                    (delete group (symbol-value (car dead-lists)))))
12343           (setq killed (cdr killed)))
12344         (setq dead-lists (cdr dead-lists))))
12345     (gnus-message 5 "Checking bogus newsgroups...done")))
12346
12347 (defun gnus-check-duplicate-killed-groups ()
12348   "Remove duplicates from the list of killed groups."
12349   (interactive)
12350   (let ((killed gnus-killed-list))
12351     (while killed
12352       (gnus-message 9 "%d" (length killed))
12353       (setcdr killed (delete (car killed) (cdr killed)))
12354       (setq killed (cdr killed)))))
12355
12356 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12357 ;; and compute how many unread articles there are in each group.
12358 (defun gnus-get-unread-articles (&optional level) 
12359   (let* ((newsrc (cdr gnus-newsrc-alist))
12360          (level (or level (1+ gnus-level-subscribed)))
12361          (foreign-level
12362           (min 
12363            (cond ((and gnus-activate-foreign-newsgroups 
12364                        (not (numberp gnus-activate-foreign-newsgroups)))
12365                   (1+ gnus-level-subscribed))
12366                  ((numberp gnus-activate-foreign-newsgroups)
12367                   gnus-activate-foreign-newsgroups)
12368                  (t 0))
12369            level))
12370          info group active virtuals method)
12371     (gnus-message 5 "Checking new news...")
12372
12373     (while newsrc
12374       (setq info (car newsrc)
12375             group (car info)
12376             active (gnus-gethash group gnus-active-hashtb))
12377
12378       ;; Check newsgroups. If the user doesn't want to check them, or
12379       ;; they can't be checked (for instance, if the news server can't
12380       ;; be reached) we just set the number of unread articles in this
12381       ;; newsgroup to t. This means that Gnus thinks that there are
12382       ;; unread articles, but it has no idea how many.
12383       (if (and (setq method (nth 4 info))
12384                (not (gnus-server-equal gnus-select-method
12385                                        (gnus-server-get-method nil method)))
12386                (not (gnus-secondary-method-p method)))
12387           ;; These groups are foreign. Check the level.
12388           (if (<= (nth 1 info) foreign-level)
12389               (if (eq (car (if (stringp method) 
12390                                (gnus-server-to-method method)
12391                              (nth 4 info))) 'nnvirtual)
12392                   ;; We have to activate the virtual groups after all
12393                   ;; the others, so we just pop them on a list for
12394                   ;; now. 
12395                   (setq virtuals (cons info virtuals))
12396                 (and (setq active (gnus-activate-group (car info)))
12397                      ;; Close the groups as we look at them!
12398                      (gnus-close-group group))))
12399
12400         (or gnus-read-active-file (gnus-check-server method))
12401         ;; These groups are native or secondary. 
12402         (if (and (not gnus-read-active-file)
12403                  (<= (nth 1 info) level))
12404             (setq active (gnus-activate-group (car info)))))
12405       
12406       (if active
12407           (gnus-get-unread-articles-in-group info active)
12408         ;; The group couldn't be reached, so we nix out the number of
12409         ;; unread articles and stuff.
12410         (gnus-sethash group nil gnus-active-hashtb)
12411         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12412
12413       (setq newsrc (cdr newsrc)))
12414
12415     ;; Activate the virtual groups. This has to be done after all the
12416     ;; other groups. 
12417     ;; !!! If one virtual group contains another virtual group, even
12418     ;; doing it this way might cause problems.
12419     (while virtuals
12420       (and (setq active (gnus-activate-group (car (car virtuals))))
12421            (gnus-get-unread-articles-in-group (car virtuals) active))
12422       (setq virtuals (cdr virtuals)))
12423
12424     (gnus-message 5 "Checking new news...done")))
12425
12426 ;; Create a hash table out of the newsrc alist. The `car's of the
12427 ;; alist elements are used as keys.
12428 (defun gnus-make-hashtable-from-newsrc-alist ()
12429   (let ((alist gnus-newsrc-alist)
12430         (ohashtb gnus-newsrc-hashtb)
12431         prev)
12432     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12433     (setq alist 
12434           (setq prev (setq gnus-newsrc-alist 
12435                            (if (equal (car (car gnus-newsrc-alist))
12436                                       "dummy.group")
12437                                gnus-newsrc-alist
12438                              (cons (list "dummy.group" 0 nil) alist)))))
12439     (while alist
12440       (gnus-sethash (car (car alist)) 
12441                     (cons (and ohashtb (car (gnus-gethash 
12442                                              (car (car alist)) ohashtb))) 
12443                           prev) gnus-newsrc-hashtb)
12444       (setq prev alist
12445             alist (cdr alist)))))
12446
12447 (defun gnus-make-hashtable-from-killed ()
12448   "Create a hash table from the killed and zombie lists."
12449   (let ((lists '(gnus-killed-list gnus-zombie-list))
12450         list)
12451     (setq gnus-killed-hashtb 
12452           (gnus-make-hashtable 
12453            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12454     (while lists
12455       (setq list (symbol-value (car lists)))
12456       (setq lists (cdr lists))
12457       (while list
12458         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12459         (setq list (cdr list))))))
12460
12461 (defun gnus-get-unread-articles-in-group (info active)
12462   (let* ((range (nth 2 info))
12463          (num 0)
12464          (marked (nth 3 info)))
12465     ;; If a cache is present, we may have to alter the active info.
12466     (and gnus-use-cache
12467          (gnus-cache-possibly-alter-active (car info) active))
12468     ;; Modify the list of read articles according to what articles 
12469     ;; are available; then tally the unread articles and add the
12470     ;; number to the group hash table entry.
12471     (cond 
12472      ((zerop (cdr active))
12473       (setq num 0))
12474      ((not range)
12475       (setq num (- (1+ (cdr active)) (car active))))
12476      ((not (listp (cdr range)))
12477       ;; Fix a single (num . num) range according to the
12478       ;; active hash table.
12479       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12480       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12481       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12482       ;; Compute number of unread articles.
12483       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12484      (t
12485       ;; The read list is a list of ranges. Fix them according to
12486       ;; the active hash table.
12487       ;; First peel off any elements that are below the lower
12488       ;; active limit. 
12489       (while (and (cdr range) 
12490                   (>= (car active) 
12491                       (or (and (atom (car (cdr range))) (car (cdr range)))
12492                           (car (car (cdr range))))))
12493         (if (numberp (car range))
12494             (setcar range 
12495                     (cons (car range) 
12496                           (or (and (numberp (car (cdr range)))
12497                                    (car (cdr range))) 
12498                               (cdr (car (cdr range))))))
12499           (setcdr (car range) 
12500                   (or (and (numberp (nth 1 range)) (nth 1 range))
12501                       (cdr (car (cdr range))))))
12502         (setcdr range (cdr (cdr range))))
12503       ;; Adjust the first element to be the same as the lower limit. 
12504       (if (and (not (atom (car range))) 
12505                (< (cdr (car range)) (car active)))
12506           (setcdr (car range) (1- (car active))))
12507       ;; Then we want to peel off any elements that are higher
12508       ;; than the upper active limit.  
12509       (let ((srange range))
12510         ;; Go past all legal elements.
12511         (while (and (cdr srange) 
12512                     (<= (or (and (atom (car (cdr srange)))
12513                                  (car (cdr srange)))
12514                             (car (car (cdr srange)))) (cdr active)))
12515           (setq srange (cdr srange)))
12516         (if (cdr srange)
12517             ;; Nuke all remaining illegal elements.
12518             (setcdr srange nil))
12519
12520         ;; Adjust the final element.
12521         (if (and (not (atom (car srange)))
12522                  (> (cdr (car srange)) (cdr active)))
12523             (setcdr (car srange) (cdr active))))
12524       ;; Compute the number of unread articles.
12525       (while range
12526         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12527                                     (cdr (car range))))
12528                             (or (and (atom (car range)) (car range))
12529                                 (car (car range))))))
12530         (setq range (cdr range)))
12531       (setq num (max 0 (- (cdr active) num)))))
12532     (and info
12533          (progn
12534            (and (assq 'tick marked)
12535                 (inline (gnus-remove-illegal-marked-articles
12536                          (assq 'tick marked) (nth 2 info))))
12537            (and (assq 'dormant marked)
12538                 (inline (gnus-remove-illegal-marked-articles
12539                          (assq 'dormant marked) (nth 2 info))))
12540            (setcar
12541             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12542             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12543                                 (length (cdr (assq 'dormant marked)))))))))
12544     num))
12545
12546 (defun gnus-remove-illegal-marked-articles (marked ranges)
12547   (let ((m (cdr marked)))
12548     ;; Make sure that all ticked articles are a subset of the unread
12549     ;; articles. 
12550     (while m
12551       (if (gnus-member-of-range (car m) ranges)
12552           (setcdr marked (cdr m))
12553         (setq marked m))
12554       (setq m (cdr m)))))
12555
12556 (defun gnus-activate-group (group)
12557   ;; Check whether a group has been activated or not.
12558   (let ((method (gnus-find-method-for-group group))
12559         active)
12560     (and (gnus-check-server method)
12561          ;; We escape all bugs and quits here to make it possible to
12562          ;; continue if a group is so out-there that it reports bugs
12563          ;; and stuff.
12564          (condition-case ()
12565              (gnus-request-group group)
12566            (error nil)
12567            (quit nil))
12568          (save-excursion
12569            (set-buffer nntp-server-buffer)
12570            (goto-char (point-min))
12571            ;; Parse the result we got from `gnus-request-group'.
12572            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12573                 (progn
12574                   (goto-char (match-beginning 1))
12575                   (gnus-sethash 
12576                    group (setq active (cons (read (current-buffer))
12577                                             (read (current-buffer))))
12578                    gnus-active-hashtb))
12579                 ;; Return the new active info.
12580                 active)))))
12581
12582 (defun gnus-update-read-articles 
12583   (group unread unselected ticked &optional domarks replied expirable killed
12584          dormant bookmark score)
12585   "Update the list of read and ticked articles in GROUP using the
12586 UNREAD and TICKED lists.
12587 Note: UNSELECTED has to be sorted over `<'.
12588 Returns whether the updating was successful."
12589   (let* ((active (or gnus-newsgroup-active 
12590                      (gnus-gethash group gnus-active-hashtb)))
12591          (entry (gnus-gethash group gnus-newsrc-hashtb))
12592          (info (nth 2 entry))
12593          (marked (nth 3 info))
12594          (prev 1)
12595          (unread (sort (copy-sequence unread) (function <)))
12596          read)
12597     (if (or (not info) (not active))
12598         ;; There is no info on this group if it was, in fact,
12599         ;; killed. Gnus stores no information on killed groups, so
12600         ;; there's nothing to be done. 
12601         ;; One could store the information somewhere temporarily,
12602         ;; perhaps... Hmmm... 
12603         ()
12604       ;; Remove any negative articles numbers.
12605       (while (and unread (< (car unread) 0))
12606         (setq unread (cdr unread)))
12607       ;; Remove any expired article numbers
12608       (while (and unread (< (car unread) (car active)))
12609         (setq unread (cdr unread)))
12610       (while (and ticked (< (car ticked) (car active)))
12611         (setq ticked (cdr ticked)))
12612       (while (and dormant (< (car dormant) (car active)))
12613         (setq dormant (cdr dormant)))
12614       (setq unread (sort (append unselected unread) '<))
12615       ;; Compute the ranges of read articles by looking at the list of
12616       ;; unread articles.  
12617       (while unread
12618         (if (/= (car unread) prev)
12619             (setq read (cons (if (= prev (1- (car unread))) prev
12620                                (cons prev (1- (car unread)))) read)))
12621         (setq prev (1+ (car unread)))
12622         (setq unread (cdr unread)))
12623       (if (<= prev (cdr active))
12624           (setq read (cons (cons prev (cdr active)) read)))
12625       ;; Enter this list into the group info.
12626       (setcar (cdr (cdr info)) 
12627               (if (> (length read) 1) (nreverse read) read))
12628       ;; Enter the list of ticked articles.
12629       (gnus-set-marked-articles 
12630        info ticked
12631        (if domarks replied (cdr (assq 'reply marked)))
12632        (if domarks expirable (cdr (assq 'expire marked)))
12633        (if domarks killed (cdr (assq 'killed marked)))
12634        (if domarks dormant (cdr (assq 'dormant marked)))
12635        (if domarks bookmark (cdr (assq 'bookmark marked)))
12636        (if domarks score (cdr (assq 'score marked))))
12637       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12638       (gnus-get-unread-articles-in-group 
12639        info (gnus-gethash group gnus-active-hashtb))
12640       t)))
12641
12642 (defun gnus-make-articles-unread (group articles)
12643   "Mark ARTICLES in GROUP as unread."
12644   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12645                           (gnus-gethash (gnus-group-real-name group)
12646                                         gnus-newsrc-hashtb))))
12647          (ranges (nth 2 info))
12648          news)
12649     (while articles
12650       (and (gnus-member-of-range (car articles) ranges)
12651            (setq news (cons (car articles) news)))
12652       (setq articles (cdr articles)))
12653     (if (not news)
12654         ()
12655       (setcar (nthcdr 2 info)
12656               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12657       (gnus-group-update-group group t))))
12658
12659 ;; Get the active file(s) from the backend(s).
12660 (defun gnus-read-active-file ()
12661   (gnus-group-set-mode-line)
12662   (let ((methods (if (gnus-check-server gnus-select-method)
12663                      ;; The native server is available.
12664                      (cons gnus-select-method gnus-secondary-select-methods)
12665                    ;; The native server is down, so we just do the
12666                    ;; secondary ones.   
12667                    gnus-secondary-select-methods))
12668         list-type)
12669     (setq gnus-have-read-active-file nil)
12670     (save-excursion
12671       (set-buffer nntp-server-buffer)
12672       (while methods
12673         (let* ((method (gnus-server-get-method nil (car methods)))
12674                (where (nth 1 method))
12675                (mesg (format "Reading active file%s via %s..."
12676                              (if (and where (not (zerop (length where))))
12677                                  (concat " from " where) "")
12678                              (car method))))
12679           (gnus-message 5 mesg)
12680           (gnus-check-server method)
12681           (cond 
12682            ((and (eq gnus-read-active-file 'some)
12683                  (gnus-check-backend-function 'retrieve-groups (car method)))
12684             (let ((newsrc (cdr gnus-newsrc-alist))
12685                   (gmethod (gnus-server-get-method nil method))
12686                   groups)
12687               (while newsrc
12688                 (and (gnus-server-equal 
12689                       (gnus-find-method-for-group 
12690                        (car (car newsrc)) (car newsrc))
12691                       gmethod)
12692                      (setq groups (cons (gnus-group-real-name 
12693                                          (car (car newsrc))) groups)))
12694                 (setq newsrc (cdr newsrc)))
12695               (gnus-check-server method)
12696               (setq list-type (gnus-retrieve-groups groups method))
12697               (cond ((not list-type)
12698                      (gnus-message 
12699                       1 "Cannot read partial active file from %s server." 
12700                       (car method))
12701                      (ding)
12702                      (sit-for 2))
12703                     ((eq list-type 'active)
12704                      (gnus-active-to-gnus-format method))
12705                     (t
12706                      (gnus-groups-to-gnus-format method)))))
12707            (t
12708             (if (not (gnus-request-list method))
12709                 (progn
12710                   (gnus-message 1 "Cannot read active file from %s server." 
12711                                 (car method))
12712                   (ding))
12713               (gnus-active-to-gnus-format method)
12714               ;; We mark this active file as read.
12715               (setq gnus-have-read-active-file
12716                     (cons method gnus-have-read-active-file))
12717               (gnus-message 5 "%sdone" mesg)))))
12718         (setq methods (cdr methods))))))
12719
12720 ;; Read an active file and place the results in `gnus-active-hashtb'.
12721 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12722   (let ((cur (current-buffer))
12723         (hashtb (or hashtb 
12724                     (if (and gnus-active-hashtb 
12725                              (not (equal method gnus-select-method)))
12726                         gnus-active-hashtb
12727                       (setq gnus-active-hashtb
12728                             (if (equal method gnus-select-method)
12729                                 (gnus-make-hashtable 
12730                                  (count-lines (point-min) (point-max)))
12731                               (gnus-make-hashtable 4096))))))
12732         (flag-hashtb (gnus-make-hashtable 60)))
12733     ;; Delete unnecessary lines.
12734     (goto-char (point-min))
12735     (while (search-forward "\nto." nil t)
12736       (delete-region (1+ (match-beginning 0)) 
12737                      (progn (forward-line 1) (point))))
12738     (or (string= gnus-ignored-newsgroups "")
12739         (progn
12740           (goto-char (point-min))
12741           (delete-matching-lines gnus-ignored-newsgroups)))
12742     ;; Make the group names readable as a lisp expression even if they
12743     ;; contain special characters.
12744     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12745     (goto-char (point-max))
12746     (while (re-search-backward "[][';?()#]" nil t)
12747       (insert ?\\))
12748     ;; If these are groups from a foreign select method, we insert the
12749     ;; group prefix in front of the group names. 
12750     (and method (not (gnus-server-equal
12751                       (gnus-server-get-method nil method)
12752                       (gnus-server-get-method nil gnus-select-method)))
12753          (let ((prefix (gnus-group-prefixed-name "" method)))
12754            (goto-char (point-min))
12755            (while (and (not (eobp))
12756                        (progn (insert prefix)
12757                               (zerop (forward-line 1)))))))
12758     ;; Store the active file in a hash table.
12759     (goto-char (point-min))
12760     (if (string-match "%[oO]" gnus-group-line-format)
12761         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12762         ;; If we want information on moderated groups, we use this
12763         ;; loop...   
12764         (let* ((mod-hashtb (make-vector 7 0))
12765                (m (intern "m" mod-hashtb))
12766                group max min)
12767           (while (not (eobp))
12768             (condition-case nil
12769                 (progn
12770                   (narrow-to-region (point) (gnus-point-at-eol))
12771                   (setq group (let ((obarray hashtb)) (read cur)))
12772                   (if (and (numberp (setq max (read cur)))
12773                            (numberp (setq min (read cur)))
12774                            (progn 
12775                              (skip-chars-forward " \t")
12776                              (not
12777                               (or (= (following-char) ?=)
12778                                   (= (following-char) ?x)
12779                                   (= (following-char) ?j)))))
12780                       (set group (cons min max))
12781                     (set group nil))
12782                   ;; Enter moderated groups into a list.
12783                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12784                       (setq gnus-moderated-list 
12785                             (cons (symbol-name group) gnus-moderated-list))))
12786               (error 
12787                (set group nil)))
12788             (widen)
12789             (forward-line 1)))
12790       ;; And if we do not care about moderation, we use this loop,
12791       ;; which is faster.
12792       (let (group max min)
12793         (while (not (eobp))
12794           (condition-case ()
12795               (progn
12796                 (narrow-to-region (point) (gnus-point-at-eol))
12797                 ;; group gets set to a symbol interned in the hash table
12798                 ;; (what a hack!!) - jwz
12799                 (setq group (let ((obarray hashtb)) (read cur)))
12800                 (if (and (numberp (setq max (read cur)))
12801                          (numberp (setq min (read cur)))
12802                          (progn 
12803                            (skip-chars-forward " \t")
12804                            (not
12805                             (or (= (following-char) ?=)
12806                                 (= (following-char) ?x)
12807                                 (= (following-char) ?j)))))
12808                     (set group (cons min max))
12809                   (set group nil)))
12810             (error 
12811              (progn 
12812                (set group nil)
12813                (if ignore-errors
12814                    ()
12815                  (gnus-message 3 "Warning - illegal active: %s"
12816                                (buffer-substring 
12817                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12818                  nil))))
12819           (widen)
12820           (forward-line 1))))))
12821
12822 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12823   ;; Parse a "groups" active file.
12824   (let ((cur (current-buffer))
12825         (hashtb (or hashtb 
12826                     (if (and method gnus-active-hashtb)
12827                         gnus-active-hashtb
12828                       (setq gnus-active-hashtb
12829                             (gnus-make-hashtable 
12830                              (count-lines (point-min) (point-max)))))))
12831         (prefix (and method (not (eq method gnus-select-method))
12832                      (gnus-group-prefixed-name "" method))))
12833
12834     (goto-char (point-min))
12835     ;; We split this into to separate loops, one with the prefix
12836     ;; and one without to speed the reading up somewhat.
12837     (if prefix
12838         (let (min max opoint group)
12839           (while (not (eobp))
12840             (condition-case ()
12841                 (progn
12842                   (read cur) (read cur)
12843                   (setq min (read cur)
12844                         max (read cur)
12845                         opoint (point))
12846                   (skip-chars-forward " \t")
12847                   (insert prefix)
12848                   (goto-char opoint)
12849                   (set (let ((obarray hashtb)) (read cur)) 
12850                        (cons min max)))
12851               (error (if group (set group nil))))
12852             (forward-line 1)))
12853       (let (min max group)
12854         (while (not (eobp))
12855           (condition-case ()
12856               (if (= (following-char) ?2)
12857                   (progn
12858                     (read cur) (read cur)
12859                     (setq min (read cur)
12860                           max (read cur))
12861                     (set (setq group (let ((obarray hashtb)) (read cur)))
12862                          (cons min max))))
12863             (error (if group (set group nil))))
12864           (forward-line 1))))))
12865
12866 (defun gnus-read-newsrc-file (&optional force)
12867   "Read startup file.
12868 If FORCE is non-nil, the .newsrc file is read."
12869   ;; Reset variables that might be defined in the .newsrc.eld file.
12870   (let ((variables gnus-variable-list))
12871     (while variables
12872       (set (car variables) nil)
12873       (setq variables (cdr variables))))
12874   (let* ((newsrc-file gnus-current-startup-file)
12875          (quick-file (concat newsrc-file ".el")))
12876     (save-excursion
12877       ;; We always load the .newsrc.eld file. If always contains
12878       ;; much information that can not be gotten from the .newsrc
12879       ;; file (ticked articles, killed groups, foreign methods, etc.)
12880       (gnus-read-newsrc-el-file quick-file)
12881  
12882       (if (or force
12883               (and (file-newer-than-file-p newsrc-file quick-file)
12884                    (file-newer-than-file-p newsrc-file 
12885                                            (concat quick-file "d")))
12886               (not gnus-newsrc-alist))
12887           ;; We read the .newsrc file. Note that if there if a
12888           ;; .newsrc.eld file exists, it has already been read, and
12889           ;; the `gnus-newsrc-hashtb' has been created. While reading
12890           ;; the .newsrc file, Gnus will only use the information it
12891           ;; can find there for changing the data already read -
12892           ;; ie. reading the .newsrc file will not trash the data
12893           ;; already read (except for read articles).
12894           (save-excursion
12895             (gnus-message 5 "Reading %s..." newsrc-file)
12896             (set-buffer (find-file-noselect newsrc-file))
12897             (buffer-disable-undo (current-buffer))
12898             (gnus-newsrc-to-gnus-format)
12899             (kill-buffer (current-buffer))
12900             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12901
12902 (defun gnus-read-newsrc-el-file (file)
12903   (let ((ding-file (concat file "d")))
12904     ;; We always, always read the .eld file.
12905     (gnus-message 5 "Reading %s..." ding-file)
12906     (let (gnus-newsrc-assoc)
12907       (condition-case nil
12908           (load ding-file t t t)
12909         (error nil))
12910       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12911     (let ((inhibit-quit t))
12912       (gnus-uncompress-newsrc-alist))
12913     (gnus-make-hashtable-from-newsrc-alist)
12914     (if (not (file-newer-than-file-p file ding-file))
12915         ()
12916       ;; Old format quick file
12917       (gnus-message 5 "Reading %s..." file)
12918       ;; The .el file is newer than the .eld file, so we read that one
12919       ;; as well. 
12920       (gnus-read-old-newsrc-el-file file))))
12921
12922 ;; Parse the old-style quick startup file
12923 (defun gnus-read-old-newsrc-el-file (file)
12924   (let (newsrc killed marked group m)
12925     (prog1
12926         (let ((gnus-killed-assoc nil)
12927               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12928           (prog1
12929               (condition-case nil
12930                   (load file t t t)
12931                 (error nil))
12932             (setq newsrc gnus-newsrc-assoc
12933                   killed gnus-killed-assoc
12934                   marked gnus-marked-assoc)))
12935       (setq gnus-newsrc-alist nil)
12936       (while newsrc
12937         (setq group (car newsrc))
12938         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12939           (if info
12940               (progn
12941                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12942                 (setcar (cdr info)
12943                         (if (nth 1 group) gnus-level-default-subscribed 
12944                           gnus-level-default-unsubscribed))
12945                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12946             (setq gnus-newsrc-alist
12947                   (cons 
12948                    (setq info
12949                          (list (car group)
12950                                (if (nth 1 group) gnus-level-default-subscribed
12951                                  gnus-level-default-unsubscribed) 
12952                                (cdr (cdr group))))
12953                    gnus-newsrc-alist)))
12954           (if (setq m (assoc (car group) marked))
12955             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12956         (setq newsrc (cdr newsrc)))
12957       (setq newsrc killed)
12958       (while newsrc
12959         (setcar newsrc (car (car newsrc)))
12960         (setq newsrc (cdr newsrc)))
12961       (setq gnus-killed-list killed))
12962     ;; The .el file version of this variable does not begin with
12963     ;; "options", while the .eld version does, so we just add it if it
12964     ;; isn't there.
12965     (and
12966      gnus-newsrc-options 
12967      (progn
12968        (and (not (string-match "^ *options" gnus-newsrc-options))
12969             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12970        (and (not (string-match "\n$" gnus-newsrc-options))
12971             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12972     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12973     (gnus-make-hashtable-from-newsrc-alist)))
12974       
12975 (defun gnus-make-newsrc-file (file)
12976   "Make server dependent file name by catenating FILE and server host name."
12977   (let* ((file (expand-file-name file nil))
12978          (real-file (concat file "-" (nth 1 gnus-select-method))))
12979     (if (or (file-exists-p real-file)
12980             (file-exists-p (concat real-file ".el"))
12981             (file-exists-p (concat real-file ".eld")))
12982         real-file file)))
12983
12984 (defun gnus-uncompress-newsrc-alist ()
12985   ;; Uncompress all lists of marked articles in the newsrc assoc.
12986   (let ((newsrc gnus-newsrc-alist)
12987         marked)
12988     (while newsrc
12989       (if (not (setq marked (nth 3 (car newsrc))))
12990           ()
12991         (while marked
12992           (or (eq 'score (car (car marked)))
12993               (eq 'bookmark (car (car marked)))
12994               (eq 'killed (car (car marked)))
12995               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12996           (setq marked (cdr marked))))
12997       (setq newsrc (cdr newsrc)))))
12998
12999 (defun gnus-compress-newsrc-alist ()
13000   ;; Compress all lists of marked articles in the newsrc assoc.
13001   (let ((newsrc gnus-newsrc-alist)
13002         marked)
13003     (while newsrc
13004       (if (not (setq marked (nth 3 (car newsrc))))
13005           ()
13006         (while marked
13007           (or (eq 'score (car (car marked)))
13008               (eq 'bookmark (car (car marked)))
13009               (eq 'killed (car (car marked)))
13010               (setcdr (car marked) 
13011                       (condition-case ()
13012                           (gnus-compress-sequence 
13013                            (sort (cdr (car marked)) '<) t)
13014                         (error (cdr (car marked))))))
13015           (setq marked (cdr marked))))
13016       (setq newsrc (cdr newsrc)))))
13017
13018 (defun gnus-newsrc-to-gnus-format ()
13019   (setq gnus-newsrc-options "")
13020   (setq gnus-newsrc-options-n nil)
13021
13022   (or gnus-active-hashtb
13023       (setq gnus-active-hashtb (make-vector 4095 0)))
13024   (let ((buf (current-buffer))
13025         (already-read (> (length gnus-newsrc-alist) 1))
13026         group subscribed options-symbol newsrc Options-symbol
13027         symbol reads num1)
13028     (goto-char (point-min))
13029     ;; We intern the symbol `options' in the active hashtb so that we
13030     ;; can `eq' against it later.
13031     (setq options-symbol (intern "options" gnus-active-hashtb))
13032     (setq Options-symbol (intern "Options" gnus-active-hashtb))
13033   
13034     (while (not (eobp))
13035       ;; We first read the first word on the line by narrowing and
13036       ;; then reading into `gnus-active-hashtb'.  Most groups will
13037       ;; already exist in that hashtb, so this will save some string
13038       ;; space.
13039       (narrow-to-region
13040        (point)
13041        (progn (skip-chars-forward "^ \t!:\n") (point)))
13042       (goto-char (point-min))
13043       (setq symbol 
13044             (and (/= (point-min) (point-max))
13045                  (let ((obarray gnus-active-hashtb)) (read buf))))
13046       (widen)
13047       ;; Now, the symbol we have read is either `options' or a group
13048       ;; name.  If it is an options line, we just add it to a string. 
13049       (cond 
13050        ((or (eq symbol options-symbol)
13051             (eq symbol Options-symbol))
13052         (setq gnus-newsrc-options
13053               ;; This concatting is quite inefficient, but since our
13054               ;; thorough studies show that approx 99.37% of all
13055               ;; .newsrc files only contain a single options line, we
13056               ;; don't give a damn, frankly, my dear.
13057               (concat gnus-newsrc-options
13058                       (buffer-substring 
13059                        (gnus-point-at-bol)
13060                        ;; Options may continue on the next line.
13061                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13062                                 (progn (beginning-of-line) (point)))
13063                            (point))))))
13064        (symbol
13065         ;; It was a group name.
13066         (setq subscribed (= (following-char) ?:)
13067               group (symbol-name symbol)
13068               reads nil)
13069         (if (eolp)
13070             ;; If the line ends here, this is clearly a buggy line, so
13071             ;; we put point a the beginning of line and let the cond
13072             ;; below do the error handling.
13073             (beginning-of-line)
13074           ;; We skip to the beginning of the ranges.
13075           (skip-chars-forward "!: \t"))
13076         ;; We are now at the beginning of the list of read articles.
13077         ;; We read them range by range.
13078         (while
13079             (cond 
13080              ((looking-at "[0-9]+")
13081               ;; We narrow and read a number instead of buffer-substring/
13082               ;; string-to-int because it's faster. narrow/widen is
13083               ;; faster than save-restriction/narrow, and save-restriction
13084               ;; produces a garbage object.
13085               (setq num1 (progn
13086                            (narrow-to-region (match-beginning 0) (match-end 0))
13087                            (read buf)))
13088               (widen)
13089               ;; If the next character is a dash, then this is a range.
13090               (if (= (following-char) ?-)
13091                   (progn
13092                     ;; We read the upper bound of the range.
13093                     (forward-char 1)
13094                     (if (not (looking-at "[0-9]+"))
13095                         ;; This is a buggy line, by we pretend that
13096                         ;; it's kinda OK. Perhaps the user should be
13097                         ;; dinged? 
13098                         (setq reads (cons num1 reads))
13099                       (setq reads 
13100                             (cons 
13101                              (cons num1 (progn
13102                                           (narrow-to-region (match-beginning 0) 
13103                                                             (match-end 0))
13104                                           (read buf)))
13105                              reads))
13106                       (widen)))
13107                 ;; It was just a simple number, so we add it to the
13108                 ;; list of ranges.
13109                 (setq reads (cons num1 reads)))
13110               ;; If the next char in ?\n, then we have reached the end
13111               ;; of the line and return nil.
13112               (/= (following-char) ?\n))
13113              ((= (following-char) ?\n)
13114               ;; End of line, so we end.
13115               nil)
13116              (t
13117               ;; Not numbers and not eol, so this might be a buggy
13118               ;; line... 
13119               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13120                   (progn
13121                     ;; The line was buggy.
13122                     (setq group nil)
13123                     (gnus-message 3 "Mangled line: %s" 
13124                                   (buffer-substring (gnus-point-at-bol) 
13125                                                     (gnus-point-at-eol)))
13126                     (ding)
13127                     (sit-for 1)))
13128               nil))
13129           ;; Skip past ", ". Spaces are illegal in these ranges, but
13130           ;; we allow them, because it's a common mistake to put a
13131           ;; space after the comma.
13132           (skip-chars-forward ", "))
13133
13134         ;; We have already read .newsrc.eld, so we gently update the
13135         ;; data in the hash table with the information we have just
13136         ;; read. 
13137         (if (not group)
13138             ()
13139           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13140                 level)
13141             (if info
13142                 ;; There is an entry for this file in the alist.
13143                 (progn
13144                   (setcar (nthcdr 2 info) (nreverse reads))
13145                   ;; We update the level very gently.  In fact, we
13146                   ;; only change it if there's been a status change
13147                   ;; from subscribed to unsubscribed, or vice versa.
13148                   (setq level (nth 1 info))
13149                   (cond ((and (<= level gnus-level-subscribed)
13150                               (not subscribed))
13151                          (setq level (if reads
13152                                          gnus-level-default-unsubscribed 
13153                                        (1+ gnus-level-default-unsubscribed))))
13154                         ((and (> level gnus-level-subscribed) subscribed)
13155                          (setq level gnus-level-default-subscribed)))
13156                   (setcar (cdr info) level))
13157               ;; This is a new group.
13158               (setq info (list group 
13159                                (if subscribed
13160                                    gnus-level-default-subscribed 
13161                                  (if reads
13162                                      (1+ gnus-level-subscribed)
13163                                    gnus-level-default-unsubscribed))
13164                                (nreverse reads))))
13165             (setq newsrc (cons info newsrc))))))
13166       (forward-line 1))
13167     
13168     (setq newsrc (nreverse newsrc))
13169
13170     (if (not already-read)
13171         ()
13172       ;; We now have two newsrc lists - `newsrc', which is what we
13173       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13174       ;; what we've read from .newsrc.eld. We have to merge these
13175       ;; lists. We do this by "attaching" any (foreign) groups in the
13176       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13177       (let ((rc (cdr gnus-newsrc-alist))
13178             (prev gnus-newsrc-alist)
13179             entry mentry)
13180         (while rc
13181           (or (null (nth 4 (car rc))) ; It's a native group.
13182               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13183               (if (setq entry (assoc (car (car prev)) newsrc))
13184                   (setcdr (setq mentry (memq entry newsrc))
13185                           (cons (car rc) (cdr mentry)))
13186                 (setq newsrc (cons (car rc) newsrc))))
13187           (setq prev rc
13188                 rc (cdr rc)))))
13189
13190     (setq gnus-newsrc-alist newsrc)
13191     ;; We make the newsrc hashtb.
13192     (gnus-make-hashtable-from-newsrc-alist)
13193
13194     ;; Finally, if we read some options lines, we parse them.
13195     (or (string= gnus-newsrc-options "")
13196         (gnus-newsrc-parse-options gnus-newsrc-options))))
13197
13198 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13199 ;; The return value will be a list on the form
13200 ;; ((regexp1 . ignore)
13201 ;;  (regexp2 . subscribe)...)
13202 ;; When handling new newsgroups, groups that match a `ignore' regexp
13203 ;; will be ignored, and groups that match a `subscribe' regexp will be
13204 ;; subscribed. A line like
13205 ;; options -n !all rec.all
13206 ;; will lead to a list that looks like
13207 ;; (("^rec\\..+" . subscribe) 
13208 ;;  ("^.+" . ignore))
13209 ;; So all "rec.*" groups will be subscribed, while all the other
13210 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13211 ;; different from "options -n rec.all !all". 
13212 (defun gnus-newsrc-parse-options (options)
13213   (let (out eol)
13214     (save-excursion
13215       (gnus-set-work-buffer)
13216       (insert (regexp-quote options))
13217       ;; First we treat all continuation lines.
13218       (goto-char (point-min))
13219       (while (re-search-forward "\n[ \t]+" nil t)
13220         (replace-match " " t t))
13221       ;; Then we transform all "all"s into ".+"s.
13222       (goto-char (point-min))
13223       (while (re-search-forward "\\ball\\b" nil t)
13224         (replace-match ".+" t t))
13225       (goto-char (point-min))
13226       ;; We remove all other options than the "-n" ones.
13227       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13228         (replace-match " ")
13229         (forward-char -1))
13230       (goto-char (point-min))
13231
13232       ;; We are only interested in "options -n" lines - we
13233       ;; ignore the other option lines.
13234       (while (re-search-forward "[ \t]-n" nil t)
13235         (setq eol 
13236               (or (save-excursion
13237                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13238                          (- (point) 2)))
13239                   (gnus-point-at-eol)))
13240         ;; Search for all "words"...
13241         (while (re-search-forward "[^ \t,\n]+" eol t)
13242           (if (= (char-after (match-beginning 0)) ?!)
13243               ;; If the word begins with a bang (!), this is a "not"
13244               ;; spec. We put this spec (minus the bang) and the
13245               ;; symbol `ignore' into the list.
13246               (setq out (cons (cons (concat 
13247                                      "^" (buffer-substring 
13248                                           (1+ (match-beginning 0))
13249                                           (match-end 0)))
13250                                     'ignore) out))
13251             ;; There was no bang, so this is a "yes" spec.
13252             (setq out (cons (cons (concat 
13253                                    "^" (buffer-substring (match-beginning 0)
13254                                                          (match-end 0)))
13255                                   'subscribe) out)))))
13256     
13257       (setq gnus-newsrc-options-n out))))
13258                
13259
13260 (defun gnus-save-newsrc-file ()
13261   "Save .newsrc file."
13262   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13263   ;; from the variable gnus-newsrc-alist.
13264   (and (or gnus-newsrc-alist gnus-killed-list)
13265        gnus-current-startup-file
13266        (progn
13267          (run-hooks 'gnus-save-newsrc-hook)
13268          (save-excursion
13269            (if (and gnus-use-dribble-file
13270                     (or (not gnus-dribble-buffer)
13271                         (not (buffer-name gnus-dribble-buffer))
13272                         (zerop (save-excursion
13273                                  (set-buffer gnus-dribble-buffer)
13274                                  (buffer-size)))))
13275                (gnus-message 4 "(No changes need to be saved)")
13276              (if gnus-save-newsrc-file
13277                  (progn
13278                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13279                    ;; Make backup file of master newsrc.
13280                    (gnus-gnus-to-newsrc-format)
13281                    (gnus-message 5 "Saving %s...done"
13282                                  gnus-current-startup-file)))
13283              ;; Quickly loadable .newsrc.
13284              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13285              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13286              (gnus-add-current-to-buffer-list)
13287              (buffer-disable-undo (current-buffer))
13288              (erase-buffer)
13289              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13290              (gnus-gnus-to-quick-newsrc-format)
13291              (save-buffer)
13292              (kill-buffer (current-buffer))
13293              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13294              (gnus-dribble-delete-file))))))
13295
13296 (defun gnus-gnus-to-quick-newsrc-format ()
13297   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13298   (insert ";; (ding) Gnus startup file.\n")
13299   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13300   (insert ";; to read .newsrc.\n")
13301   (insert "(setq gnus-newsrc-file-version "
13302           (prin1-to-string gnus-version) ")\n")
13303   (let ((variables gnus-variable-list)
13304         (inhibit-quit t)
13305         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13306         variable)
13307     ;; insert lisp expressions.
13308     (gnus-compress-newsrc-alist)
13309     (while variables
13310       (setq variable (car variables))
13311       (and (boundp variable)
13312            (symbol-value variable)
13313            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13314            (insert "(setq " (symbol-name variable) " '"
13315                    (prin1-to-string (symbol-value variable))
13316                    ")\n"))
13317       (setq variables (cdr variables)))
13318     (gnus-uncompress-newsrc-alist)))
13319
13320
13321 (defun gnus-gnus-to-newsrc-format ()
13322   ;; Generate and save the .newsrc file.
13323   (let ((newsrc (cdr gnus-newsrc-alist))
13324         info ranges range)
13325     (save-excursion
13326       (set-buffer (create-file-buffer gnus-current-startup-file))
13327       (set-visited-file-name gnus-current-startup-file)
13328       (buffer-disable-undo (current-buffer))
13329       (erase-buffer)
13330       ;; Write options.
13331       (if gnus-newsrc-options (insert gnus-newsrc-options))
13332       ;; Write subscribed and unsubscribed.
13333       (while newsrc
13334         (setq info (car newsrc))
13335         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13336             (progn
13337               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13338                                      "!" ":"))
13339               (if (setq ranges (nth 2 info))
13340                   (progn
13341                     (insert " ")
13342                     (if (not (listp (cdr ranges)))
13343                         (if (= (car ranges) (cdr ranges))
13344                             (insert (int-to-string (car ranges)))
13345                           (insert (int-to-string (car ranges)) "-" 
13346                                   (int-to-string (cdr ranges))))
13347                       (while ranges
13348                         (setq range (car ranges)
13349                               ranges (cdr ranges))
13350                         (if (or (atom range) (= (car range) (cdr range)))
13351                             (insert (int-to-string 
13352                                      (or (and (atom range) range) 
13353                                          (car range))))
13354                           (insert (int-to-string (car range)) "-"
13355                                   (int-to-string (cdr range))))
13356                         (if ranges (insert ","))))))
13357               (insert "\n")))
13358         (setq newsrc (cdr newsrc)))
13359       (save-buffer)
13360       (kill-buffer (current-buffer)))))
13361
13362 (defun gnus-read-all-descriptions-files ()
13363   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13364     (while methods
13365       (gnus-read-descriptions-file (car methods))
13366       (setq methods (cdr methods)))
13367     t))
13368
13369 (defun gnus-read-descriptions-file (&optional method)
13370   (let ((method (or method gnus-select-method)))
13371     ;; We create the hashtable whether we manage to read the desc file
13372     ;; to avoid trying to re-read after a failed read.
13373     (or gnus-description-hashtb
13374         (setq gnus-description-hashtb 
13375               (gnus-make-hashtable (length gnus-active-hashtb))))
13376     ;; Mark this method's desc file as read.
13377     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13378                   gnus-description-hashtb)
13379
13380     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13381     (cond 
13382      ((not (gnus-check-server method))
13383       (gnus-message 1 "Couldn't open server")
13384       nil)
13385      ((not (gnus-request-list-newsgroups method))
13386       (gnus-message 1 "Couldn't read newsgroups descriptions")
13387       nil)
13388      (t
13389       (let (group)
13390         (save-excursion
13391           (save-restriction
13392             (set-buffer nntp-server-buffer)
13393             (goto-char (point-min))
13394             (if (or (search-forward "\n.\n" nil t)
13395                     (goto-char (point-max)))
13396                 (progn
13397                   (beginning-of-line)
13398                   (narrow-to-region (point-min) (point))))
13399             (goto-char (point-min))
13400             (while (not (eobp))
13401               ;; If we get an error, we set group to 0, which is not a
13402               ;; symbol... 
13403               (setq group 
13404                     (condition-case ()
13405                         (let ((obarray gnus-description-hashtb))
13406                           ;; Group is set to a symbol interned in this
13407                           ;; hash table.
13408                           (read nntp-server-buffer))
13409                       (error 0)))
13410               (skip-chars-forward " \t")
13411               ;; ... which leads to this line being effectively ignored.
13412               (and (symbolp group)
13413                    (set group (buffer-substring 
13414                                (point) (progn (end-of-line) (point)))))
13415               (forward-line 1))))
13416         (gnus-message 5 "Reading descriptions file...done")
13417         t)))))
13418
13419 (defun gnus-group-get-description (group)
13420   ;; Get the description of a group by sending XGTITLE to the server.
13421   (and (gnus-request-group-description group)
13422        (save-excursion
13423          (set-buffer nntp-server-buffer)
13424          (goto-char (point-min))
13425          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13426               (buffer-substring (match-beginning 1) (match-end 1))))))
13427
13428 ;;;
13429 ;;; Server
13430 ;;;
13431
13432 (defvar gnus-server-mode-hook nil
13433   "Hook run in `gnus-server-mode' buffers.")
13434
13435 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13436   "Format of server lines.
13437 It works along the same lines as a normal formatting string,
13438 with some simple extensions.")
13439
13440 (defvar gnus-server-mode-line-format "(ding) List of servers"
13441   "The format specification for the server mode line.")
13442
13443 (defconst gnus-server-line-format-alist
13444   (list (list ?h 'how ?s)
13445         (list ?n 'name ?s)
13446         (list ?w 'where ?s)
13447         ))
13448
13449 (defconst gnus-server-mode-line-format-alist 
13450   (list (list ?S 'news-server ?s)
13451         (list ?M 'news-method ?s)
13452         (list ?u 'user-defined ?s)))
13453
13454 (defvar gnus-server-line-format-spec nil)
13455 (defvar gnus-server-mode-line-format-spec nil)
13456 (defvar gnus-server-killed-servers nil)
13457
13458 (defvar gnus-server-mode-map nil)
13459 (put 'gnus-server-mode 'mode-class 'special)
13460
13461 (if gnus-server-mode-map
13462     nil
13463   (setq gnus-server-mode-map (make-sparse-keymap))
13464   (suppress-keymap gnus-server-mode-map)
13465   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13466   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13467   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13468   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13469   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13470   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13471   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13472   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13473   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13474   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13475
13476 (defun gnus-server-mode ()
13477   "Major mode for listing and editing servers.
13478
13479 All normal editing commands are switched off.
13480 \\<gnus-server-mode-map>
13481
13482 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13483
13484 The following commands are available:
13485
13486 \\{gnus-server-mode-map}"
13487   (interactive)
13488   (if gnus-visual (gnus-server-make-menu-bar))
13489   (kill-all-local-variables)
13490   (setq mode-line-modified "-- ")
13491   (make-local-variable 'mode-line-format)
13492   (setq mode-line-format (copy-sequence mode-line-format))
13493   (and (equal (nth 3 mode-line-format) "   ")
13494        (setcar (nthcdr 3 mode-line-format) ""))
13495   (setq major-mode 'gnus-server-mode)
13496   (setq mode-name "Server")
13497 ;  (gnus-group-set-mode-line)
13498   (setq mode-line-process nil)
13499   (use-local-map gnus-server-mode-map)
13500   (buffer-disable-undo (current-buffer))
13501   (setq truncate-lines t)
13502   (setq buffer-read-only t)
13503   (run-hooks 'gnus-server-mode-hook))
13504
13505 (defun gnus-server-insert-server-line (sformat name method)
13506   (let* ((sformat (or sformat gnus-server-line-format-spec))
13507          (how (car method))
13508          (where (nth 1 method))
13509          b)
13510     (beginning-of-line)
13511     (setq b (point))
13512     ;; Insert the text.
13513     (insert (eval sformat))
13514     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13515
13516 (defun gnus-server-setup-buffer ()
13517   (if (get-buffer gnus-server-buffer)
13518       ()
13519     (save-excursion
13520       (set-buffer (get-buffer-create gnus-server-buffer))
13521       (gnus-server-mode)
13522       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13523
13524 (defun gnus-server-prepare ()
13525   (setq gnus-server-mode-line-format-spec 
13526         (gnus-parse-format gnus-server-mode-line-format 
13527                            gnus-server-mode-line-format-alist))
13528   (setq gnus-server-line-format-spec 
13529         (gnus-parse-format gnus-server-line-format 
13530                            gnus-server-line-format-alist))
13531   (let ((alist gnus-server-alist)
13532         (buffer-read-only nil))
13533     (erase-buffer)
13534     (while alist
13535       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13536       (setq alist (cdr alist))))
13537   (goto-char (point-min))
13538   (gnus-server-position-cursor))
13539
13540 (defun gnus-server-server-name ()
13541   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13542     (and server (symbol-name server))))
13543
13544 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13545
13546 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13547
13548 (defun gnus-server-update-server (server)
13549   (save-excursion
13550     (set-buffer gnus-server-buffer)
13551     (let ((buffer-read-only nil)
13552           (info (cdr (assoc server gnus-server-alist))))
13553       (gnus-dribble-enter 
13554        (concat "(gnus-server-set-info \"" server "\" '"
13555                (prin1-to-string info) ")"))
13556       ;; Buffer may be narrowed.
13557       (save-restriction
13558         (widen)
13559         (if (gnus-server-goto-server server)
13560             (delete-region (progn (beginning-of-line) (point))
13561                            (progn (forward-line 1) (point))))
13562         (let ((entry (assoc server gnus-server-alist)))
13563           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13564           (gnus-server-position-cursor))))))
13565
13566 (defun gnus-server-set-info (server info)
13567   ;; Enter a select method into the virtual server alist.
13568   (gnus-dribble-enter 
13569    (concat "(gnus-server-set-info \"" server "\" '"
13570            (prin1-to-string info) ")"))
13571   (let* ((server (nth 1 info))
13572          (entry (assoc server gnus-server-alist)))
13573     (if entry (setcdr entry info)
13574       (setq gnus-server-alist
13575             (nconc gnus-server-alist (list (cons server info)))))))
13576
13577 (defun gnus-server-to-method (server)
13578   ;; Map virtual server names to select methods.
13579   (or (and (equal server "native") gnus-select-method)
13580       (cdr (assoc server gnus-server-alist))))
13581
13582 (defun gnus-server-extend-method (group method)
13583   ;; This function "extends" a virtual server.  If the server is
13584   ;; "hello", and the select method is ("hello" (my-var "something")) 
13585   ;; in the group "alt.alt", this will result in a new virtual server
13586   ;; called "helly+alt.alt".
13587   (let ((entry
13588          (gnus-copy-sequence 
13589           (if (equal (car method) "native") gnus-select-method
13590               (cdr (assoc (car method) gnus-server-alist))))))
13591     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13592     (nconc entry (cdr method))))
13593
13594 (defun gnus-server-get-method (group method)
13595   ;; Input either a server name, and extended server name, or a
13596   ;; select method, and return a select method. 
13597   (cond ((stringp method)
13598          (gnus-server-to-method method))
13599         ((and (stringp (car method)) group)
13600          (gnus-server-extend-method group method))
13601         (t
13602          (gnus-server-add-address method))))
13603
13604 (defun gnus-server-add-address (method)
13605   (let ((method-name (symbol-name (car method))))
13606     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13607              (not (assq (intern (concat method-name "-address")) method)))
13608         (append method (list (list (intern (concat method-name "-address"))
13609                                    (nth 1 method))))
13610       method)))
13611
13612 (defun gnus-server-equal (s1 s2)
13613   (or (equal s1 s2)
13614       (and (= (length s1) (length s2))
13615            (progn
13616              (while (and s1 (member (car s1) s2))
13617                (setq s1 (cdr s1)))
13618              (null s1)))))
13619
13620 ;;; Interactive server functions.
13621
13622 (defun gnus-server-kill-server (server)
13623   "Kill the server on the current line."
13624   (interactive (list (gnus-server-server-name)))
13625   (or (gnus-server-goto-server server)
13626       (if server (error "No such server: %s" server)
13627         (error "No server on the current line")))
13628   (let ((buffer-read-only nil))
13629     (delete-region (progn (beginning-of-line) (point))
13630                    (progn (forward-line 1) (point))))
13631   (setq gnus-server-killed-servers 
13632         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13633   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13634                                 gnus-server-alist))
13635   (gnus-server-position-cursor))
13636
13637 (defun gnus-server-yank-server ()
13638   "Yank the previously killed server."
13639   (interactive)
13640   (or gnus-server-killed-servers
13641       (error "No killed servers to be yanked"))
13642   (let ((alist gnus-server-alist)
13643         (server (gnus-server-server-name))
13644         (killed (car gnus-server-killed-servers)))
13645     (if (not server) 
13646         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13647       (if (string= server (car (car gnus-server-alist)))
13648           (setq gnus-server-alist (cons killed gnus-server-alist))
13649         (while (and (cdr alist)
13650                     (not (string= server (car (car (cdr alist))))))
13651           (setq alist (cdr alist)))
13652         (setcdr alist (cons killed (cdr alist)))))
13653     (gnus-server-update-server (car killed))
13654     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13655     (gnus-server-position-cursor)))
13656
13657 (defun gnus-server-exit ()
13658   "Return to the group buffer."
13659   (interactive)
13660   (kill-buffer (current-buffer))
13661   (switch-to-buffer gnus-group-buffer))
13662
13663 (defun gnus-server-list-servers ()
13664   "List all available servers."
13665   (interactive)
13666   (let ((cur (gnus-server-server-name)))
13667     (gnus-server-prepare)
13668     (if cur (gnus-server-goto-server cur)
13669       (goto-char (point-max))
13670       (forward-line -1))
13671     (gnus-server-position-cursor)))
13672
13673 (defun gnus-server-copy-server (from to)
13674   (interactive
13675    (list
13676     (or (gnus-server-server-name)
13677         (error "No server on the current line"))
13678     (read-string "Copy to: ")))
13679   (or from (error "No server on current line"))
13680   (or (and to (not (string= to ""))) (error "No name to copy to"))
13681   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13682   (or (assoc from gnus-server-alist) 
13683       (error "%s: no such server" from))
13684   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13685     (setcar to-entry to)
13686     (setcar (nthcdr 2 to-entry) to)
13687     (setq gnus-server-killed-servers 
13688           (cons to-entry gnus-server-killed-servers))
13689     (gnus-server-yank-server)))
13690
13691 (defun gnus-server-add-server (how where)
13692   (interactive 
13693    (list (intern (completing-read "Server method: "
13694                                   gnus-valid-select-methods nil t))
13695          (read-string "Server name: ")))
13696   (setq gnus-server-killed-servers 
13697         (cons (list where how where) gnus-server-killed-servers))
13698   (gnus-server-yank-server))
13699
13700 (defun gnus-server-goto-server (server)
13701   "Jump to a server line."
13702   (interactive
13703    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13704   (let ((to (text-property-any (point-min) (point-max) 
13705                                'gnus-server (intern server))))
13706     (and to
13707          (progn
13708            (goto-char to) 
13709            (gnus-server-position-cursor)))))
13710
13711 (defun gnus-server-edit-server (server)
13712   "Edit the server on the current line."
13713   (interactive (list (gnus-server-server-name)))
13714   (or server
13715       (error "No server on current line"))
13716   (let ((winconf (current-window-configuration)))
13717     (get-buffer-create gnus-server-edit-buffer)
13718     (gnus-configure-windows 'edit-server)
13719     (gnus-add-current-to-buffer-list)
13720     (emacs-lisp-mode)
13721     (make-local-variable 'gnus-prev-winconf)
13722     (setq gnus-prev-winconf winconf)
13723     (use-local-map (copy-keymap (current-local-map)))
13724     (let ((done-func '(lambda () 
13725                         "Exit editing mode and update the information."
13726                         (interactive)
13727                         (gnus-server-edit-server-done 'group))))
13728       (setcar (cdr (nth 4 done-func)) server)
13729       (local-set-key "\C-c\C-c" done-func))
13730     (erase-buffer)
13731     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13732     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13733
13734 (defun gnus-server-edit-server-done (server)
13735   (interactive)
13736   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13737   (goto-char (point-min))
13738   (let ((form (read (current-buffer)))
13739         (winconf gnus-prev-winconf))
13740     (gnus-server-set-info server form)
13741     (kill-buffer (current-buffer))
13742     (and winconf (set-window-configuration winconf))
13743     (set-buffer gnus-server-buffer)
13744     (gnus-server-update-server (gnus-server-server-name))
13745     (gnus-server-position-cursor)))
13746
13747 (defun gnus-server-read-server (server)
13748   "Browse a server."
13749   (interactive (list (gnus-server-server-name)))
13750   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13751
13752 (defun gnus-mouse-pick-server (e)
13753   (interactive "e")
13754   (mouse-set-point e)
13755   (gnus-server-read-server (gnus-server-server-name)))
13756
13757 ;;;
13758 ;;; entry points into gnus-score.el
13759 ;;;
13760
13761 ;;; Finding score files. 
13762
13763 (defvar gnus-global-score-files nil
13764   "*List of global score files and directories.
13765 Set this variable if you want to use people's score files.  One entry
13766 for each score file or each score file directory.  Gnus will decide
13767 by itself what score files are applicable to which group.
13768
13769 Say you want to use the single score file
13770 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13771 score files in the \"/ftp.some-where:/pub/score\" directory.
13772
13773  (setq gnus-global-score-files
13774        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13775          \"/ftp.some-where:/pub/score\"))")
13776
13777 (defun gnus-score-score-files (group)
13778   "Return a list of all possible score files."
13779   ;; Search and set any global score files.
13780   (and gnus-global-score-files 
13781        (or gnus-internal-global-score-files
13782            (gnus-score-search-global-directories gnus-global-score-files)))
13783   ;; Fix the kill-file dir variable.
13784   (setq gnus-kill-files-directory 
13785         (file-name-as-directory
13786          (or gnus-kill-files-directory "~/News/")))
13787   ;; If we can't read it, there are no score files.
13788   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13789       (setq gnus-score-file-list nil)
13790     (if (gnus-use-long-file-name 'not-score)
13791         ;; We want long file names.
13792         (if (or (not gnus-score-file-list)
13793                 (not (car gnus-score-file-list))
13794                 (gnus-file-newer-than gnus-kill-files-directory
13795                                       (car gnus-score-file-list)))
13796               (setq gnus-score-file-list 
13797                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13798                           (nreverse 
13799                            (directory-files 
13800                             gnus-kill-files-directory t 
13801                             (gnus-score-file-regexp))))))
13802       ;; We do not use long file names, so we have to do some
13803       ;; directory traversing.  
13804       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13805             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13806             dir files suffix)
13807         (while suffixes
13808           (setq dir (expand-file-name
13809                      (concat gnus-kill-files-directory
13810                              (gnus-replace-chars-in-string group ?. ?/))))
13811           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13812           (setq suffix (car suffixes)
13813                 suffixes (cdr suffixes))
13814           (if (file-exists-p (concat dir "/" suffix))
13815               (setq files (cons (concat dir "/" suffix) files)))
13816           (while (>= (1+ (length dir)) mdir)
13817             (and (file-exists-p (concat dir "/all/" suffix))
13818                  (setq files (cons (concat dir "/all/" suffix) files)))
13819             (string-match "/[^/]*$" dir)
13820             (setq dir (substring dir 0 (match-beginning 0)))))
13821         (setq gnus-score-file-list 
13822               (cons nil (nreverse files)))))
13823     (cdr gnus-score-file-list)))
13824
13825 (defun gnus-score-file-regexp ()
13826   (concat "\\(" gnus-score-file-suffix 
13827           "\\|" gnus-adaptive-file-suffix "\\)$"))
13828         
13829 (defun gnus-score-find-bnews (group)
13830   "Return a list of score files for GROUP.
13831 The score files are those files in the ~/News directory which matches
13832 GROUP using BNews sys file syntax."
13833   (let* ((sfiles (append (gnus-score-score-files group)
13834                          gnus-internal-global-score-files))
13835          (kill-dir (file-name-as-directory 
13836                     (expand-file-name gnus-kill-files-directory)))
13837          (klen (length kill-dir))
13838          ofiles not-match regexp)
13839     (save-excursion
13840       (set-buffer (get-buffer-create "*gnus score files*"))
13841       (buffer-disable-undo (current-buffer))
13842       ;; Go through all score file names and create regexp with them
13843       ;; as the source.  
13844       (while sfiles
13845         (erase-buffer)
13846         (insert (car sfiles))
13847         (goto-char (point-min))
13848         ;; First remove the suffix itself.
13849         (re-search-forward (concat "." (gnus-score-file-regexp)))
13850         (replace-match "" t t) 
13851         (goto-char (point-min))
13852         (if (looking-at (regexp-quote kill-dir))
13853             ;; If the file name was just "SCORE", `klen' is one character
13854             ;; too much.
13855             (delete-char (min (1- (point-max)) klen))
13856           (goto-char (point-max))
13857           (search-backward "/")
13858           (delete-region (1+ (point)) (point-min)))
13859         ;; If short file names were used, we have to translate slashes.
13860         (goto-char (point-min))
13861         (while (re-search-forward "[/:]" nil t)
13862           (replace-match "." t t))
13863         ;; Translate "all" to ".*".
13864         (while (search-forward "all" nil t)
13865           (replace-match ".*" t t))
13866         (goto-char (point-min))
13867         ;; Deal with "not."s.
13868         (if (looking-at "not.")
13869             (progn
13870               (setq not-match t)
13871               (setq regexp (buffer-substring 5 (point-max))))
13872           (setq regexp (buffer-substring 1 (point-max)))
13873           (setq not-match nil))
13874         ;; Finally - if this resulting regexp matches the group name,
13875         ;; we add this score file to the list of score files
13876         ;; applicable to this group.
13877         (if (or (and not-match
13878                      (not (string-match regexp group)))
13879                 (and (not not-match)
13880                      (string-match regexp group)))
13881             (setq ofiles (cons (car sfiles) ofiles)))
13882         (setq sfiles (cdr sfiles)))
13883       (kill-buffer (current-buffer))
13884       ;; Slight kludge here - the last score file returned should be
13885       ;; the local score file, whether it exists or not. This is so
13886       ;; that any score commands the user enters will go to the right
13887       ;; file, and not end up in some global score file.
13888       (let ((localscore
13889              (expand-file-name
13890               (if (gnus-use-long-file-name 'not-score)
13891                   (concat gnus-kill-files-directory group "." 
13892                           gnus-score-file-suffix)
13893                 (concat gnus-kill-files-directory
13894                         (gnus-replace-chars-in-string group ?. ?/)
13895                         "/" gnus-score-file-suffix)))))
13896         (and (member localscore ofiles)
13897              (delete localscore ofiles))
13898         (setq ofiles (cons localscore ofiles)))
13899       (nreverse ofiles))))
13900
13901 (defun gnus-score-find-single (group)
13902   "Return list containing the score file for GROUP."
13903   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13904         (gnus-score-file-name group)))
13905
13906 (defun gnus-score-find-hierarchical (group)
13907   "Return list of score files for GROUP.
13908 This includes the score file for the group and all its parents."
13909   (let ((all (copy-sequence '(nil)))
13910         (start 0))
13911     (while (string-match "\\." group (1+ start))
13912       (setq start (match-beginning 0))
13913       (setq all (cons (substring group 0 start) all)))
13914     (setq all (cons group all))
13915     (nconc
13916      (mapcar (lambda (newsgroup)
13917                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13918              (setq all (nreverse all)))
13919      (mapcar 'gnus-score-file-name all))))
13920
13921 (defvar gnus-score-file-alist-cache nil)
13922
13923 (defun gnus-score-find-alist (group)
13924   "Return list of score files for GROUP.
13925 The list is determined from the variable gnus-score-file-alist."
13926   (let ((alist gnus-score-file-multiple-match-alist)
13927         score-files)
13928     ;; if this group has been seen before, return the cached entry
13929     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13930         (cdr score-files)      ; ensures caching of groups with no matches
13931       ;; handle the multiple match alist
13932       (while alist
13933         (and (string-match (car (car alist)) group)
13934              (setq score-files
13935                    (nconc score-files (copy-sequence (cdr (car alist))))))
13936         (setq alist (cdr alist)))
13937       (setq alist gnus-score-file-single-match-alist)
13938       ;; handle the single match alist
13939       (while alist
13940         (and (string-match (car (car alist)) group)
13941              ;; progn used just in case ("regexp") has no files
13942              ;; and score-files is still nil. -sj
13943              ;; this can be construed as a "stop searching here" feature :>
13944              ;; and used to simplify regexps in the single-alist 
13945              (progn
13946                (setq score-files
13947                      (nconc score-files (copy-sequence (cdr (car alist)))))
13948                (setq alist nil)))
13949         (setq alist (cdr alist)))
13950       ;; cache the score files
13951       (setq gnus-score-file-alist-cache
13952             (cons (cons group score-files) gnus-score-file-alist-cache))
13953       score-files)))
13954
13955
13956 (defun gnus-possibly-score-headers (&optional trace)
13957   (let ((func gnus-score-find-score-files-function)
13958         score-files)
13959     (and func (not (listp func))
13960          (setq func (list func)))
13961     ;; Go through all the functions for finding score files (or actual
13962     ;; scores) and add them to a list.
13963     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13964     (while func
13965       (and (symbolp (car func))
13966            (fboundp (car func))
13967            (setq score-files 
13968                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13969       (setq func (cdr func)))
13970     (if score-files (gnus-score-headers score-files trace))))
13971
13972 (defun gnus-score-file-name (newsgroup &optional suffix)
13973   "Return the name of a score file for NEWSGROUP."
13974   (let ((suffix (or suffix gnus-score-file-suffix)))
13975     (cond 
13976      ((or (null newsgroup)
13977           (string-equal newsgroup ""))
13978       ;; The global score file is placed at top of the directory.
13979       (expand-file-name 
13980        suffix (or gnus-kill-files-directory "~/News")))
13981      ((gnus-use-long-file-name 'not-score)
13982       ;; Append ".SCORE" to newsgroup name.
13983       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
13984                                 "." suffix)
13985                         (or gnus-kill-files-directory "~/News")))
13986      (t
13987       ;; Place "SCORE" under the hierarchical directory.
13988       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13989                                 "/" suffix)
13990                         (or gnus-kill-files-directory "~/News"))))))
13991
13992 (defun gnus-score-search-global-directories (files)
13993   "Scan all global score directories for score files."
13994   ;; Set the variable `gnus-internal-global-score-files' to all
13995   ;; available global score files.
13996   (interactive (list gnus-global-score-files))
13997   (let (out)
13998     (while files
13999       (if (string-match "/$" (car files))
14000           (setq out (nconc (directory-files 
14001                             (car files) t
14002                             (concat (gnus-score-file-regexp) "$"))))
14003         (setq out (cons (car files) out)))
14004       (setq files (cdr files)))
14005     (setq gnus-internal-global-score-files out)))
14006
14007 ;; Allow redefinition of Gnus functions.
14008
14009 (gnus-ems-redefine)
14010
14011 (provide 'gnus)
14012
14013 ;;; gnus.el ends here