*** 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 Gnus looks suspiciously like GNUS, it isn't quite the same
27 ;; beast. Most internal structures have been changed. If you have
28 ;; 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 (eval '(run-hooks 'gnus-load-hook))
37
38 (require 'mail-utils)
39 (require 'timezone)
40 (require 'nnheader)
41
42 ;; Site dependent variables. These variables should be defined in
43 ;; paths.el.
44
45 (defvar gnus-default-nntp-server nil
46   "Specify a default NNTP server.
47 This variable should be defined in paths.el, and should never be set
48 by the user.
49 If you want to change servers, you should use `gnus-select-method'.
50 See the documentation to that variable.")
51
52 (defconst gnus-backup-default-subscribed-newsgroups 
53   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
54   "Default default new newsgroups the first time Gnus is run.
55 Should be set in paths.el, and shouldn't be touched by the user.")
56
57 (defvar gnus-local-domain nil
58   "Local domain name without a host name.
59 The DOMAINNAME environment variable is used instead if it is defined.
60 If the `system-name' function returns the full Internet name, there is
61 no need to set this variable.")
62
63 (defvar gnus-local-organization nil
64   "String with a description of what organization (if any) the user belongs to.
65 The ORGANIZATION environment variable is used instead if it is defined.
66 If this variable contains a function, this function will be called
67 with the current newsgroup name as the argument. The function should
68 return a string.
69
70 In any case, if the string (either in the variable, in the environment
71 variable, or returned by the function) is a file name, the contents of
72 this file will be used as the organization.")
73
74 (defvar gnus-use-generic-from nil
75   "If nil, the full host name will be the system name prepended to the domain name.
76 If this is a string, the full host name will be this string.
77 If this is non-nil, non-string, the domain name will be used as the
78 full host name.")
79
80 (defvar gnus-use-generic-path nil
81   "If nil, use the NNTP server name in the Path header.
82 If stringp, use this; if non-nil, use no host name (user name only).")
83
84
85 ;; Customization variables
86
87 ;; Don't touch this variable.
88 (defvar gnus-nntp-service "nntp"
89   "*NNTP service name (\"nntp\" or 119).
90 This is an obsolete variable, which is scarcely used. If you use an
91 nntp server for your newsgroup and want to change the port number
92 used to 899, you would say something along these lines:
93
94  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
95
96 (defvar gnus-select-method 
97   (nconc
98    (list 'nntp (or (getenv "NNTPSERVER") 
99                    (if (and gnus-default-nntp-server
100                             (not (string= gnus-default-nntp-server "")))
101                        gnus-default-nntp-server)
102                    (system-name)))
103    (if (or (null gnus-nntp-service)
104            (equal gnus-nntp-service "nntp"))
105        nil 
106      (list gnus-nntp-service)))
107   "*Default method for selecting a newsgroup.
108 This variable should be a list, where the first element is how the
109 news is to be fetched, the second is the address. 
110
111 For instance, if you want to get your news via NNTP from
112 \"flab.flab.edu\", you could say:
113
114 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
115
116 If you want to use your local spool, say:
117
118 (setq gnus-select-method (list 'nnspool (system-name)))
119
120 If you use this variable, you must set `gnus-nntp-server' to nil.
121
122 There is a lot more to know about select methods and virtual servers -
123 see the manual for details.")
124
125 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
126 (defvar gnus-post-method nil
127   "*Preferred method for posting USENET news.
128 If this variable is nil, Gnus will use the current method to decide
129 which method to use when posting.  If it is non-nil, it will override
130 the current method.  This method will not be used in mail groups and
131 the like, only in \"real\" newsgroups.
132
133 The value must be a valid method as discussed in the documentation of
134 `gnus-select-method'.")
135
136 (defvar gnus-refer-article-method nil
137   "*Preferred method for fetching an article by Message-ID.
138 If you are reading news from the local spool (with nnspool), fetching
139 articles by Message-ID is painfully slow. By setting this method to an
140 nntp method, you might get acceptable results.
141
142 The value of this variable must be a valid select method as discussed
143 in the documentation of `gnus-select-method'")
144
145 (defvar gnus-secondary-select-methods nil
146   "*A list of secondary methods that will be used for reading news.
147 This is a list where each element is a complete select method (see
148 `gnus-select-method').  
149
150 If, for instance, you want to read your mail with the nnml backend,
151 you could set this variable:
152
153 (setq gnus-secondary-select-methods '((nnml \"\")))")
154
155 (defvar gnus-secondary-servers nil
156   "*List of NNTP servers that the user can choose between interactively.
157 To make Gnus query you for a server, you have to give `gnus' a
158 non-numeric prefix - `C-u M-x gnus', in short.")
159
160 (defvar gnus-nntp-server nil
161   "*The name of the host running the NNTP server.
162 This variable is semi-obsolete. Use the `gnus-select-method'
163 variable instead.")
164
165 (defvar gnus-startup-file "~/.newsrc"
166   "*Your `.newsrc' file.
167 `.newsrc-SERVER' will be used instead if that exists.")
168
169 (defvar gnus-init-file "~/.gnus"
170   "*Your Gnus elisp startup file.
171 If a file with the .el or .elc suffixes exist, it will be read
172 instead.") 
173
174 (defvar gnus-group-faq-directory
175   "/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
176   "*Directory where the group FAQs are stored.
177 This will most commonly be on a remote machine, and the file will be
178 fetched by ange-ftp.
179
180 Note that Gnus uses an aol machine as the default directory.  If this
181 feels fundamentally unclean, just think of it as a way to finally get
182 something of value back from them.
183
184 If the default site is too slow, try one of these:
185
186    North America: ftp.uu.net                     /usenet/news.answers
187                   mirrors.aol.com                /pub/rtfm/usenet
188                   ftp.seas.gwu.edu               /pub/rtfm
189                   rtfm.mit.edu                   /pub/usenet/news.answers
190    Europe:        ftp.uni-paderborn.de           /pub/FAQ
191                   ftp.Germany.EU.net             /pub/newsarchive/news.answers
192                   ftp.sunet.se                   /pub/usenet
193    Asia:          nctuccca.edu.tw                /USENET/FAQ
194                   hwarang.postech.ac.kr          /pub/usenet/news.answers
195                   ftp.hk.super.net               /mirror/faqs")
196
197 (defvar gnus-group-archive-directory
198   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
199   "*The address of the (ding) archives.")
200
201 (defvar gnus-group-recent-archive-directory
202   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
203   "*The address of the most recent (ding) articles.")
204
205 (defvar gnus-default-subscribed-newsgroups nil
206   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
207 It should be a list of strings.
208 If it is `t', Gnus will not do anything special the first time it is
209 started; it'll just use the normal newsgroups subscription methods.")
210
211 (defvar gnus-use-cross-reference t
212   "*Non-nil means that cross referenced articles will be marked as read.
213 If nil, ignore cross references.  If t, mark articles as read in
214 subscribed newsgroups. If neither t nor nil, mark as read in all
215 newsgroups.") 
216
217 (defvar gnus-use-dribble-file t
218   "*Non-nil means that Gnus will use a dribble file to store user updates.
219 If Emacs should crash without saving the .newsrc files, complete
220 information can be restored from the dribble file.")
221
222 (defvar gnus-asynchronous nil
223   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
224
225 (defvar gnus-asynchronous-article-function nil
226   "*Function for picking articles to pre-fetch, possibly.")
227
228 (defvar gnus-score-file-single-match-alist nil
229   "*Alist mapping regexps to lists of score files.
230 Each element of this alist should be of the form
231         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
232
233 If the name of a group is matched by REGEXP, the corresponding scorefiles
234 will be used for that group.
235 The first match found is used, subsequent matching entries are ignored (to
236 use multiple matches, see gnus-score-file-multiple-match-alist).
237
238 These score files are loaded in addition to any files returned by
239 gnus-score-find-score-files-function (which see).")
240
241 (defvar gnus-score-file-multiple-match-alist nil
242   "*Alist mapping regexps to lists of score files.
243 Each element of this alist should be of the form
244         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
245
246 If the name of a group is matched by REGEXP, the corresponding scorefiles
247 will be used for that group.
248 If multiple REGEXPs match a group, the score files corresponding to each
249 match will be used (for only one match to be used, see
250 gnus-score-file-single-match-alist).
251
252 These score files are loaded in addition to any files returned by
253 gnus-score-find-score-files-function (which see).")
254
255
256 (defvar gnus-score-file-suffix "SCORE"
257   "*Suffix of the score files.")
258
259 (defvar gnus-adaptive-file-suffix "ADAPT"
260   "*Suffix of the adaptive score files.")
261
262 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
263   "*Function used to find score files.
264 The function will be called with the group name as the argument, and
265 should return a list of score files to apply to that group.  The score
266 files do not actually have to exist.
267
268 Predefined values are:
269
270 gnus-score-find-single: Only apply the group's own score file.
271 gnus-score-find-hierarchical: Also apply score files from parent groups.
272 gnus-score-find-bnews: Apply score files whose names matches.
273
274 See the documentation to these functions for more information.
275
276 This variable can also be a list of functions to be called.  Each
277 function should either return a list of score files, or a list of
278 score alists.")
279
280 (defvar gnus-score-interactive-default-score 1000
281   "*Scoring commands will raise/lower the score with this number as the default.")
282
283 (defvar gnus-large-newsgroup 200
284   "*The number of articles which indicates a large newsgroup.
285 If the number of articles in a newsgroup is greater than this value,
286 confirmation is required for selecting the newsgroup.")
287
288 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
289 (defvar gnus-no-groups-message "No news is horrible news"
290   "*Message displayed by Gnus when no groups are available.")
291
292 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
293   "*Non-nil means that the default name of a file to save articles in is the group name.
294 If it's nil, the directory form of the group name is used instead.
295
296 If this variable is a list, and the list contains the element
297 `not-score', long file names will not be used for score files; if it
298 contains the element `not-save', long file names will not be used for
299 saving; and if it contains the element `not-kill', long file names
300 will not be used for kill files.")
301
302 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
303   "*Name of the directory articles will be saved in (default \"~/News\").
304 Initialized from the SAVEDIR environment variable.")
305
306 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
307   "*Name of the directory where kill files will be stored (default \"~/News\").
308 Initialized from the SAVEDIR environment variable.")
309
310 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
311   "*A function to save articles in your favorite format.
312 The function must be interactively callable (in other words, it must
313 be an Emacs command).
314
315 Gnus provides the following functions:
316
317 * gnus-summary-save-in-rmail (Rmail format)
318 * gnus-summary-save-in-mail (Unix mail format)
319 * gnus-summary-save-in-folder (MH folder)
320 * gnus-summary-save-in-file (article format).
321 * gnus-summary-save-in-vm (use VM's folder format).")
322
323 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
324   "*A function generating a file name to save articles in Rmail format.
325 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
326
327 (defvar gnus-mail-save-name (function gnus-plain-save-name)
328   "*A function generating a file name to save articles in Unix mail format.
329 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
330
331 (defvar gnus-folder-save-name (function gnus-folder-save-name)
332   "*A function generating a file name to save articles in MH folder.
333 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
334
335 (defvar gnus-file-save-name (function gnus-numeric-save-name)
336   "*A function generating a file name to save articles in article format.
337 The function is called with NEWSGROUP, HEADERS, and optional
338 LAST-FILE.")
339
340 (defvar gnus-split-methods nil
341   "*Variable used to suggest where articles are to be saved.
342 The syntax of this variable is the same as `nnmail-split-methods'.  
343
344 For instance, if you would like to save articles related to Gnus in
345 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
346 you could set this variable to something like:
347
348  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
349    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
350
351 (defvar gnus-save-score nil
352   "*If non-nil, save group scoring info.")
353
354 (defvar gnus-use-adaptive-scoring nil
355   "*If non-nil, use some adaptive scoring scheme.")
356
357 (defvar gnus-use-cache nil
358   "*If non-nil, Gnus will cache (some) articles locally.")
359
360 (defvar gnus-use-scoring t
361   "*If non-nil, enable scoring.")
362
363 (defvar gnus-fetch-old-headers nil
364   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
365 If an unread article in the group refers to an older, already read (or
366 just marked as read) article, the old article will not normally be
367 displayed in the Summary buffer.  If this variable is non-nil, Gnus
368 will attempt to grab the headers to the old articles, and thereby
369 build complete threads.  If it has the value `some', only enough
370 headers to connect otherwise loose threads will be displayed.
371
372 The server has to support XOVER for any of this to work.")
373
374 ;see gnus-cus.el
375 ;(defvar gnus-visual t
376 ;  "*If non-nil, will do various highlighting.
377 ;If nil, no mouse highlights (or any other highlights) will be
378 ;performed.  This might speed up Gnus some when generating large group
379 ;and summary buffers.")
380
381 (defvar gnus-novice-user t
382   "*Non-nil means that you are a usenet novice.
383 If non-nil, verbose messages may be displayed and confirmations may be
384 required.")
385
386 (defvar gnus-expert-user nil
387   "*Non-nil means that you will never be asked for confirmation about anything.
388 And that means *anything*.")
389
390 (defvar gnus-verbose 7
391   "*Integer that says how verbose Gnus should be.
392 The higher the number, the more messages Gnus will flash to say what
393 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
394 display most important messages; and at ten, Gnus will keep on
395 jabbering all the time.")
396
397 (defvar gnus-keep-same-level nil
398   "*Non-nil means that the next newsgroup after the current will be on the same level.
399 When you type, for instance, `n' after reading the last article in the
400 current newsgroup, you will go to the next newsgroup. If this variable
401 is nil, the next newsgroup will be the next from the group
402 buffer. 
403 If this variable is non-nil, Gnus will either put you in the
404 next newsgroup with the same level, or, if no such newsgroup is
405 available, the next newsgroup with the lowest possible level higher
406 than the current level.
407 If this variable is `best', Gnus will make the next newsgroup the one
408 with the best level.")
409
410 (defvar gnus-summary-make-false-root 'adopt
411   "*nil means that Gnus won't gather loose threads.
412 If the root of a thread has expired or been read in a previous
413 session, the information necessary to build a complete thread has been
414 lost. Instead of having many small sub-threads from this original thread
415 scattered all over the summary buffer, Gnus can gather them. 
416
417 If non-nil, Gnus will try to gather all loose sub-threads from an
418 original thread into one large thread.
419
420 If this variable is non-nil, it should be one of `none', `adopt',
421 `dummy' or `empty'.
422
423 If this variable is `none', Gnus will not make a false root, but just
424 present the sub-threads after another.
425 If this variable is `dummy', Gnus will create a dummy root that will
426 have all the sub-threads as children.
427 If this variable is `adopt', Gnus will make one of the \"children\"
428 the parent and mark all the step-children as such.
429 If this variable is `empty', the \"children\" are printed with empty
430 subject fields.  (Or rather, they will be printed with a string
431 given by the `gnus-summary-same-subject' variable.)")
432
433 (defvar gnus-summary-gather-subject-limit nil
434   "*Maximum length of subject comparisons when gathering loose threads.
435 Use nil to compare full subjects.  Setting this variable to a low
436 number will help gather threads that have been corrupted by
437 newsreaders chopping off subject lines, but it might also mean that
438 unrelated articles that have subject that happen to begin with the
439 same few characters will be incorrectly gathered.
440
441 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
442 comparing subjects.")
443
444 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
445 (defvar gnus-summary-same-subject ""
446   "*String indicating that the current article has the same subject as the previous.
447 This variable will only be used if the value of
448 `gnus-summary-make-false-root' is `empty'.")
449
450 (defvar gnus-summary-goto-unread t
451   "*If non-nil, marking commands will go to the next unread article.")
452
453 (defvar gnus-group-goto-unread t
454   "*If non-nil, movement commands will go to the next unread and subscribed group.")
455
456 (defvar gnus-check-new-newsgroups t
457   "*Non-nil means that Gnus will add new newsgroups at startup.
458 If this variable is `ask-server', Gnus will ask the server for new
459 groups since the last time it checked. This means that the killed list
460 is no longer necessary, so you could set `gnus-save-killed-list' to
461 nil. 
462
463 A variant is to have this variable be a list of select methods. Gnus
464 will then use the `ask-server' method on all these select methods to
465 query for new groups from all those servers.
466
467 Eg.
468   (setq gnus-check-new-newsgroups 
469         '((nntp \"some.server\") (nntp \"other.server\")))
470
471 If this variable is nil, then you have to tell Gnus explicitly to
472 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
473
474 (defvar gnus-check-bogus-newsgroups nil
475   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
476 If this variable is nil, then you have to tell Gnus explicitly to
477 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
478
479 (defvar gnus-read-active-file t
480   "*Non-nil means that Gnus will read the entire active file at startup.
481 If this variable is nil, Gnus will only know about the groups in your
482 `.newsrc' file.
483
484 If this variable is `some', Gnus will try to only read the relevant
485 parts of the active file from the server.  Not all servers support
486 this, and it might be quite slow with other servers, but this should
487 generally be faster than both the t and nil value.
488
489 If you set this variable to nil or `some', you probably still want to
490 be told about new newsgroups that arrive.  To do that, set
491 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
492 properly with all servers.")
493
494 (defvar gnus-level-subscribed 5
495   "*Groups with levels less than or equal to this variable are subscribed.")
496
497 (defvar gnus-level-unsubscribed 7
498   "*Groups with levels less than or equal to this variable are unsubscribed.
499 Groups with levels less than `gnus-level-subscribed', which should be
500 less than this variable, are subscribed.")
501
502 (defvar gnus-level-zombie 8
503   "*Groups with this level are zombie groups.")
504
505 (defvar gnus-level-killed 9
506   "*Groups with this level are killed.")
507
508 (defvar gnus-level-default-subscribed 3
509   "*New subscribed groups will be subscribed at this level.")
510
511 (defvar gnus-level-default-unsubscribed 6
512   "*New unsubscribed groups will be unsubscribed at this level.")
513
514 (defvar gnus-activate-foreign-newsgroups 4
515   "*If nil, Gnus will not check foreign newsgroups at startup.
516 If it is non-nil, it should be a number between one and nine. Foreign
517 newsgroups that have a level lower or equal to this number will be
518 activated on startup. For instance, if you want to active all
519 subscribed newsgroups, but not the rest, you'd set this variable to 
520 `gnus-level-subscribed'.
521
522 If you subscribe to lots of newsgroups from different servers, startup
523 might take a while. By setting this variable to nil, you'll save time,
524 but you won't be told how many unread articles there are in the
525 groups.")
526
527 (defvar gnus-save-newsrc-file t
528   "*Non-nil means that Gnus will save the `.newsrc' file.
529 Gnus always saves its own startup file, which is called
530 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
531 be readily understood by other newsreaders.  If you don't plan on
532 using other newsreaders, set this variable to nil to save some time on
533 exit.")
534
535 (defvar gnus-save-killed-list t
536   "*If non-nil, save the list of killed groups to the startup file.
537 This will save both time (when starting and quitting) and space (both
538 memory and disk), but it will also mean that Gnus has no record of
539 which groups are new and which are old, so the automatic new
540 newsgroups subscription methods become meaningless. You should always
541 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
542 variable to nil.")
543
544 (defvar gnus-interactive-catchup t
545   "*If non-nil, require your confirmation when catching up a group.")
546
547 (defvar gnus-interactive-post t
548   "*If non-nil, group name will be asked for when posting.")
549
550 (defvar gnus-interactive-exit t
551   "*If non-nil, require your confirmation when exiting Gnus.")
552
553 (defvar gnus-kill-killed t
554   "*If non-nil, Gnus will apply kill files to already killed articles.
555 If it is nil, Gnus will never apply kill files to articles that have
556 already been through the scoring process, which might very well save lots
557 of time.")
558
559 (defvar gnus-extract-address-components 'gnus-extract-address-components
560   "*Function for extracting address components from a From header.
561 Two pre-defined function exist: `gnus-extract-address-components',
562 which is the default, quite fast, and too simplistic solution, and
563 `mail-extract-address-components', which works much better, but is
564 slower.")
565
566 (defvar gnus-summary-default-score 0
567   "*Default article score level.
568 If this variable is nil, scoring will be disabled.")
569
570 (defvar gnus-summary-zcore-fuzz 0
571   "*Fuzziness factor for the zcore in the summary buffer.
572 Articles with scores closer than this to `gnus-summary-default-score'
573 will not be marked.")
574
575 (defvar gnus-simplify-subject-fuzzy-regexp nil
576   "*Regular expression that will be removed from subject strings if
577 fuzzy subject simplification is selected.")
578
579 (defvar gnus-group-default-list-level gnus-level-subscribed
580   "*Default listing level. 
581 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
582
583 (defvar gnus-group-use-permanent-levels nil
584   "*If non-nil, once you set a level, Gnus will use this level.")
585
586 (defvar gnus-show-mime nil
587   "*If non-nil, do mime processing of articles.
588 The articles will simply be fed to the function given by
589 `gnus-show-mime-method'.")
590
591 (defvar gnus-strict-mime t
592   "*If nil, decode MIME header even if there is not Mime-Version field.")
593  
594 (defvar gnus-show-mime-method (function metamail-buffer)
595   "*Function to process a MIME message.
596 The function is called from the article buffer.")
597
598 (defvar gnus-show-threads t
599   "*If non-nil, display threads in summary mode.")
600
601 (defvar gnus-thread-hide-subtree nil
602   "*If non-nil, hide all threads initially.
603 If threads are hidden, you have to run the command
604 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
605 to expose hidden threads.")
606
607 (defvar gnus-thread-hide-killed t
608   "*If non-nil, hide killed threads automatically.")
609
610 (defvar gnus-thread-ignore-subject nil
611   "*If non-nil, ignore subjects and do all threading based on the Reference header.
612 If nil, which is the default, articles that have different subjects
613 from their parents will start separate threads.")
614
615 (defvar gnus-thread-indent-level 4
616   "*Number that says how much each sub-thread should be indented.")
617
618 (defvar gnus-ignored-newsgroups 
619   (purecopy (mapconcat 'identity
620                        '("^to\\."       ; not "real" groups
621                          "^[0-9. \t]+ " ; all digits in name
622                          "[][\"#'()]"   ; bogus characters
623                          )
624                        "\\|"))
625   "*A regexp to match uninteresting newsgroups in the active file.
626 Any lines in the active file matching this regular expression are
627 removed from the newsgroup list before anything else is done to it,
628 thus making them effectively non-existent.")
629
630 (defvar gnus-ignored-headers
631   "^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:"
632   "*All headers that match this regexp will be hidden.
633 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
634
635 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
636   "*All headers that do not match this regexp will be hidden.
637 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
638
639 (defvar gnus-sorted-header-list
640   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
641     "^Cc:" "^Date:" "^Organization:")
642   "*This variable is a list of regular expressions.
643 If it is non-nil, headers that match the regular expressions will
644 be placed first in the article buffer in the sequence specified by
645 this list.")
646
647 (defvar gnus-show-all-headers nil
648   "*If non-nil, don't hide any headers.")
649
650 (defvar gnus-save-all-headers t
651   "*If non-nil, don't remove any headers before saving.")
652
653 (defvar gnus-inhibit-startup-message nil
654   "*If non-nil, the startup message will not be displayed.")
655
656 (defvar gnus-signature-separator "^-- *$"
657   "Regexp matching signature separator.")
658
659 (defvar gnus-auto-extend-newsgroup t
660   "*If non-nil, extend newsgroup forward and backward when requested.")
661
662 (defvar gnus-auto-select-first t
663   "*If non-nil, select the first unread article when entering a group.
664 If you want to prevent automatic selection of the first unread article
665 in some newsgroups, set the variable to nil in
666 `gnus-select-group-hook'.") 
667
668 (defvar gnus-auto-select-next t
669   "*If non-nil, offer to go to the next group from the end of the previous.
670 If the value is t and the next newsgroup is empty, Gnus will exit
671 summary mode and go back to group mode.  If the value is neither nil
672 nor t, Gnus will select the following unread newsgroup.  In
673 particular, if the value is the symbol `quietly', the next unread
674 newsgroup will be selected without any confirmations.")
675
676 (defvar gnus-auto-select-same nil
677   "*If non-nil, select the next article with the same subject.")
678
679 (defvar gnus-summary-check-current nil
680   "*If non-nil, consider the current article when moving.
681 The \"unread\" movement commands will stay on the same line if the
682 current article is unread.")
683
684 (defvar gnus-auto-center-summary t
685   "*If non-nil, always center the current summary buffer.")
686
687 (defvar gnus-break-pages t
688   "*If non-nil, do page breaking on articles.
689 The page delimiter is specified by the `gnus-page-delimiter'
690 variable.")
691
692 (defvar gnus-page-delimiter "^\^L"
693   "*Regexp describing what to use as article page delimiters.
694 The default value is \"^\^L\", which is a form linefeed at the
695 beginning of a line.")
696
697 (defvar gnus-use-full-window t
698   "*If non-nil, use the entire Emacs screen.")
699
700 (defvar gnus-window-configuration nil
701   "Obsolete variable.  See `gnus-buffer-configuration'.")
702
703 (defvar gnus-buffer-configuration
704   '((group ([group 1.0 point] 
705             (if gnus-carpal [group-carpal 4])))
706     (summary ([summary 1.0 point]
707               (if gnus-carpal [summary-carpal 4])))
708     (article ([summary 0.25 point] 
709               (if gnus-carpal [summary-carpal 4]) 
710               [article 1.0]))
711     (server ([server 1.0 point]
712              (if gnus-carpal [server-carpal 2])))
713     (browse ([browse 1.0 point]
714              (if gnus-carpal [browse-carpal 2])))
715     (group-mail ([mail 1.0 point]))
716     (summary-mail ([mail 1.0 point]))
717     (summary-reply ([article 0.5]
718                     [mail 1.0 point]))
719     (info ([nil 1.0 point]))
720     (summary-faq ([summary 0.25]
721                   [faq 1.0 point]))
722     (edit-group ([group 0.5]
723                  [edit-group 1.0 point]))
724     (edit-server ([server 0.5]
725                   [edit-server 1.0 point]))
726     (edit-score ([summary 0.25]
727                  [edit-score 1.0 point]))
728     (post ([post 1.0 point]))
729     (reply ([article 0.5]
730             [mail 1.0 point]))
731     (mail-forward ([mail 1.0 point]))
732     (post-forward ([post 1.0 point]))
733     (reply-yank ([mail 1.0 point]))
734     (followup ([article 0.5]
735                [post 1.0 point]))
736     (followup-yank ([post 1.0 point])))
737   "Window configuration for all possible Gnus buffers.
738 This variable is a list of lists.  Each of these lists has a NAME and
739 a RULE.  The NAMEs are commonsense names like `group', which names a
740 rule used when displaying the group buffer; `summary', which names a
741 rule for what happens when you enter a group and do not display an
742 article buffer; and so on.  See the value of this variable for a
743 complete list of NAMEs.
744
745 Each RULE is a list of vectors.  The first element in this vector is
746 the name of the buffer to be displayed; the second element is the
747 percentage of the screen this buffer is to occupy (a number in the
748 0.0-0.99 range); the optional third element is `point', which should
749 be present to denote which buffer point is to go to after making this
750 buffer configuration.")
751
752 (defvar gnus-window-to-buffer
753   '((group . gnus-group-buffer)
754     (summary . gnus-summary-buffer)
755     (article . gnus-article-buffer)
756     (server . gnus-server-buffer)
757     (browse . "*Gnus Browse Server*")
758     (edit-group . gnus-group-edit-buffer)
759     (edit-server . gnus-server-edit-buffer)
760     (group-carpal . gnus-carpal-group-buffer)
761     (summary-carpal . gnus-carpal-summary-buffer)
762     (server-carpal . gnus-carpal-server-buffer)
763     (browse-carpal . gnus-carpal-browse-buffer)
764     (edit-score . gnus-score-edit-buffer)
765     (mail . gnus-mail-buffer)
766     (post . gnus-post-news-buffer)
767     (faq . gnus-faq-buffer))
768   "Mapping from short symbols to buffer names or buffer variables.")
769
770 (defvar gnus-carpal nil
771   "*If non-nil, display clickable icons.")
772
773 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
774   "*Function called with a group name when new group is detected.
775 A few pre-made functions are supplied: `gnus-subscribe-randomly'
776 inserts new groups at the beginning of the list of groups;
777 `gnus-subscribe-alphabetically' inserts new groups in strict
778 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
779 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
780 for your decision.")
781
782 ;; Suggested by a bug report by Hallvard B Furuseth.
783 ;; <h.b.furuseth@usit.uio.no>. 
784 (defvar gnus-subscribe-options-newsgroup-method
785   (function gnus-subscribe-alphabetically)
786   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
787 If, for instance, you want to subscribe to all newsgroups in the
788 \"no\" and \"alt\" hierarchies, you'd put the following in your
789 .newsrc file:
790
791 options -n no.all alt.all
792
793 Gnus will the subscribe all new newsgroups in these hierarchies with
794 the subscription method in this variable.")
795
796 (defvar gnus-subscribe-hierarchical-interactive nil
797   "*If non-nil, Gnus will offer to subscribe hierarchically.
798 When a new hierarchy appears, Gnus will ask the user:
799
800 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
801
802 If the user pressed `d', Gnus will descend the hierarchy, `y' will
803 subscribe to all newsgroups in the hierarchy and `s' will skip this
804 hierarchy in its entirety.")
805
806 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
807   "*Function used for sorting the group buffer.
808 This function will be called with group info entries as the arguments
809 for the groups to be sorted.  Pre-made functions include
810 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
811 `gnus-group-sort-by-level'")
812
813 ;; Mark variables suggested by Thomas Michanek
814 ;; <Thomas.Michanek@telelogic.se>. 
815 (defvar gnus-unread-mark ? 
816   "*Mark used for unread articles.")
817 (defvar gnus-ticked-mark ?!
818   "*Mark used for ticked articles.")
819 (defvar gnus-dormant-mark ??
820   "*Mark used for dormant articles.")
821 (defvar gnus-del-mark ?r
822   "*Mark used for del'd articles.")
823 (defvar gnus-read-mark ?R
824   "*Mark used for read articles.")
825 (defvar gnus-expirable-mark ?E
826   "*Mark used for expirable articles.")
827 (defvar gnus-killed-mark ?K
828   "*Mark used for killed articles.")
829 (defvar gnus-kill-file-mark ?X
830   "*Mark used for articles killed by kill files.")
831 (defvar gnus-low-score-mark ?Y
832   "*Mark used for articles with a low score.")
833 (defvar gnus-catchup-mark ?C
834   "*Mark used for articles that are caught up.")
835 (defvar gnus-replied-mark ?A
836   "*Mark used for articles that have been replied to.")
837 (defvar gnus-process-mark ?# 
838   "*Process mark.")
839 (defvar gnus-ancient-mark ?O
840   "*Mark used for ancient articles.")
841 (defvar gnus-canceled-mark ?G
842   "*Mark used for canceled articles.")
843 (defvar gnus-score-over-mark ?+
844   "*Score mark used for articles with high scores.")
845 (defvar gnus-score-below-mark ?-
846   "*Score mark used for articles with low scores.")
847 (defvar gnus-empty-thread-mark ? 
848   "*There is no thread under the article.")
849 (defvar gnus-not-empty-thread-mark ?=
850   "*There is a thread under the article.")
851 (defvar gnus-dummy-mark ?Z
852   "*This is a dummy article.")
853
854 (defvar gnus-view-pseudo-asynchronously nil
855   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
856
857 (defvar gnus-view-pseudos nil
858   "*If `automatic', pseudo-articles will be viewed automatically.
859 If `not-confirm', pseudos will be viewed automatically, and the user
860 will not be asked to confirm the command.")
861
862 (defvar gnus-view-pseudos-separately t
863   "*If non-nil, one pseudo-article will be created for each file to be viewed.
864 If nil, all files that use the same viewing command will be given as a
865 list of parameters to that command.")
866
867 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
868   "*Format of group lines.
869 It works along the same lines as a normal formatting string,
870 with some simple extensions.
871
872 %M    Only marked articles (character, \"*\" or \" \")
873 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
874 %L    Level of subscribedness (integer)
875 %N    Number of unread articles (integer)
876 %I    Number of dormant articles (integer)
877 %i    Number of ticked and dormant (integer)
878 %T    Number of ticked articles (integer)
879 %R    Number of read articles (integer)
880 %t    Total number of articles (integer)
881 %y    Number of unread, unticked articles (integer)
882 %G    Group name (string)
883 %g    Qualified group name (string)
884 %D    Group description (string)
885 %s    Select method (string)
886 %o    Moderated group (char, \"m\")
887 %p    Process mark (char)
888 %O    Moderated group (string, \"(m)\" or \"\")
889 %n    Select from where (string)
890 %z    A string that look like `<%s:%n>' if a foreign select method is used
891 %u    User defined specifier. The next character in the format string should
892       be a letter.  Gnus will call the function gnus-user-format-function-X,
893       where X is the letter following %u. The function will be passed the
894       current header as argument. The function should return a string, which
895       will be inserted into the buffer just like information from any other
896       group specifier.
897
898 Text between %( and %) will be highlighted with `gnus-mouse-face' when
899 the mouse point move inside the area.  There can only be one such area.
900
901 Note that this format specification is not always respected. For
902 reasons of efficiency, when listing killed groups, this specification
903 is ignored altogether. If the spec is changed considerably, your
904 output may end up looking strange when listing both alive and killed
905 groups.
906
907 If you use %o or %O, reading the active file will be slower and quite
908 a bit of extra memory will be used. %D will also worsen performance.
909 Also note that if you change the format specification to include any
910 of these specs, you must probably re-start Gnus to see them go into
911 effect.") 
912
913 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
914   "*The format specification of the lines in the summary buffer.
915
916 It works along the same lines as a normal formatting string,
917 with some simple extensions.
918
919 %N   Article number, left padded with spaces (string)
920 %S   Subject (string)
921 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
922 %n   Name of the poster (string)
923 %a   Extracted name of the poster (string)
924 %A   Extracted address of the poster (string)
925 %F   Contents of the From: header (string)
926 %x   Contents of the Xref: header (string)
927 %D   Date of the article (string)
928 %d   Date of the article (string) in DD-MMM format
929 %M   Message-id of the article (string)
930 %r   References of the article (string)
931 %c   Number of characters in the article (integer)
932 %L   Number of lines in the article (integer)
933 %I   Indentation based on thread level (a string of spaces)
934 %T   A string with two possible values: 80 spaces if the article
935      is on thread level two or larger and 0 spaces on level one
936 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
937 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
938 %[   Opening bracket (character, \"[\" or \"<\")
939 %]   Closing bracket (character, \"]\" or \">\")
940 %>   Spaces of length thread-level (string)
941 %<   Spaces of length (- 20 thread-level) (string)
942 %i   Article score (number)
943 %z   Article zcore (character)
944 %t   Number of articles under the current thread (number).
945 %e   Whether the thread is empty or not (character).
946 %u   User defined specifier. The next character in the format string should
947      be a letter.  Gnus will call the function gnus-user-format-function-X,
948      where X is the letter following %u. The function will be passed the
949      current header as argument. The function should return a string, which
950      will be inserted into the summary just like information from any other
951      summary specifier.
952
953 Text between %( and %) will be highlighted with `gnus-mouse-face'
954 when the mouse point is placed inside the area.  There can only be one
955 such area.
956
957 The %U (status), %R (replied) and %z (zcore) specs have to be handled
958 with care. For reasons of efficiency, Gnus will compute what column
959 these characters will end up in, and \"hard-code\" that. This means that
960 it is illegal to have these specs after a variable-length spec. Well,
961 you might not be arrested, but your summary buffer will look strange,
962 which is bad enough.
963
964 The smart choice is to have these specs as for to the left as
965 possible. 
966
967 This restriction may disappear in later versions of Gnus.")
968
969 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
970   "*The format specification for the dummy roots in the summary buffer.
971 It works along the same lines as a normal formatting string,
972 with some simple extensions.
973
974 %S  The subject")
975
976 (defvar gnus-summary-mode-line-format "Gnus: %b [%A] %Z"
977   "*The format specification for the summary mode line.")
978
979 (defvar gnus-article-mode-line-format "Gnus: %b %S"
980   "*The format specification for the article mode line.")
981
982 (defvar gnus-group-mode-line-format "Gnus: %b {%M:%S}  "
983   "*The format specification for the group mode line.")
984
985 (defvar gnus-valid-select-methods
986   '(("nntp" post address prompt-address)
987     ("nnspool" post)
988     ("nnvirtual" none virtual prompt-address) 
989     ("nnmbox" mail respool) 
990     ("nnml" mail respool)
991     ("nnmh" mail respool) 
992     ("nndir" none prompt-address address)
993     ("nneething" none prompt-address)
994     ("nndigest" none) 
995     ("nndoc" none prompt-address) 
996     ("nnbabyl" mail respool) 
997     ("nnkiboze" post virtual) 
998     ;;("nnsoup" post)
999     ("nnfolder" mail respool))
1000   "An alist of valid select methods.
1001 The first element of each list lists should be a string with the name
1002 of the select method. The other elements may be be the category of
1003 this method (ie. `post', `mail', `none' or whatever) or other
1004 properties that this method has (like being respoolable).
1005 If you implement a new select method, all you should have to change is
1006 this variable. I think.")
1007
1008 (defvar gnus-updated-mode-lines '(group article summary)
1009   "*List of buffers that should update their mode lines.
1010 The list may contain the symbols `group', `article' and `summary'. If
1011 the corresponding symbol is present, Gnus will keep that mode line
1012 updated with information that may be pertinent. 
1013 If this variable is nil, screen refresh may be quicker.")
1014
1015 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1016 (defvar gnus-mode-non-string-length nil
1017   "*Max length of mode-line non-string contents.
1018 If this is nil, Gnus will take space as is needed, leaving the rest
1019 of the modeline intact.")
1020
1021 ;see gnus-cus.el
1022 ;(defvar gnus-mouse-face 'highlight
1023 ;  "*Face used for mouse highlighting in Gnus.
1024 ;No mouse highlights will be done if `gnus-visual' is nil.")
1025
1026 (defvar gnus-summary-mark-below nil
1027   "*Mark all articles with a score below this variable as read.
1028 This variable is local to each summary buffer and usually set by the
1029 score file.")  
1030
1031 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1032   "*List of functions used for sorting threads in the summary buffer.
1033 By default, threads are sorted by article number.
1034
1035 Each function takes two threads and return non-nil if the first thread
1036 should be sorted before the other.  If you use more than one function,
1037 the primary sort function should be the last.
1038
1039 Ready-mady functions include `gnus-thread-sort-by-number',
1040 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1041 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1042 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1043
1044 (defvar gnus-thread-score-function '+
1045   "*Function used for calculating the total score of a thread.
1046
1047 The function is called with the scores of the article and each
1048 subthread and should then return the score of the thread.
1049
1050 Some functions you can use are `+', `max', or `min'.")
1051
1052 (defvar gnus-options-subscribe nil
1053   "*All new groups matching this regexp will be subscribed unconditionally.
1054 Note that this variable deals only with new newsgroups.  This variable
1055 does not affect old newsgroups.")
1056
1057 (defvar gnus-options-not-subscribe nil
1058   "*All new groups matching this regexp will be ignored.
1059 Note that this variable deals only with new newsgroups.  This variable
1060 does not affect old (already subscribed) newsgroups.")
1061
1062 (defvar gnus-auto-expirable-newsgroups nil
1063   "*Groups in which to automatically mark read articles as expirable.
1064 If non-nil, this should be a regexp that should match all groups in
1065 which to perform auto-expiry.  This only makes sense for mail groups.")
1066
1067 (defvar gnus-hidden-properties '(invisible t intangible t)
1068   "Property list to use for hiding text.")
1069
1070 (defvar gnus-modtime-botch nil
1071   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1072 due to the bogus appearance that .newsrc was modified on disc.")
1073
1074 ;; Hooks.
1075
1076 (defvar gnus-group-mode-hook nil
1077   "*A hook for Gnus group mode.")
1078
1079 (defvar gnus-summary-mode-hook nil
1080   "*A hook for Gnus summary mode.
1081 This hook is run before any variables are set in the summary buffer.")
1082
1083 (defvar gnus-article-mode-hook nil
1084   "*A hook for Gnus article mode.")
1085
1086 (defun gnus-summary-prepare-exit-hook nil
1087   "*A hook called when preparing to exit from the summary buffer.
1088 It calls `gnus-summary-expire-articles' by default.")
1089 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1090
1091 (defun gnus-summary-exit-hook nil
1092   "*A hook called on exit from the summary buffer.")
1093
1094 (defvar gnus-open-server-hook nil
1095   "*A hook called just before opening connection to the news server.")
1096
1097 (defvar gnus-load-hook nil
1098   "*A hook run while Gnus is loaded.")
1099
1100 (defvar gnus-startup-hook nil
1101   "*A hook called at startup.
1102 This hook is called after Gnus is connected to the NNTP server.")
1103
1104 (defvar gnus-get-new-news-hook nil
1105   "*A hook run just before Gnus checks for new news.")
1106
1107 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1108   "*A function that is called to generate the group buffer.
1109 The function is called with three arguments: The first is a number;
1110 all group with a level less or equal to that number should be listed,
1111 if the second is non-nil, empty groups should also be displayed. If
1112 the third is non-nil, it is a number. No groups with a level lower
1113 than this number should be displayed.
1114
1115 The only current function implemented is `gnus-group-prepare-flat'.")
1116
1117 (defvar gnus-group-prepare-hook nil
1118   "*A hook called after the group buffer has been generated.
1119 If you want to modify the group buffer, you can use this hook.")
1120
1121 (defvar gnus-summary-prepare-hook nil
1122   "*A hook called after the summary buffer has been generated.
1123 If you want to modify the summary buffer, you can use this hook.")
1124
1125 (defvar gnus-article-prepare-hook nil
1126   "*A hook called after an article has been prepared in the article buffer.
1127 If you want to run a special decoding program like nkf, use this hook.")
1128
1129 ;(defvar gnus-article-display-hook nil
1130 ;  "*A hook called after the article is displayed in the article buffer.
1131 ;The hook is designed to change the contents of the article
1132 ;buffer. Typical functions that this hook may contain are
1133 ;`gnus-article-hide-headers' (hide selected headers),
1134 ;`gnus-article-maybe-highlight' (perform fancy article highlighting), 
1135 ;`gnus-article-hide-signature' (hide signature) and
1136 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1137 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1138 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1139 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1140
1141 (defvar gnus-article-x-face-command
1142   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1143   "String or function to be executed to display an X-Face header.
1144 If it is a string, the command will be executed in a sub-shell
1145 asynchronously. The compressed face will be piped to this command.") 
1146
1147 (defvar gnus-article-x-face-too-ugly nil
1148   "Regexp matching posters whose face shouldn't be shown automatically.")
1149
1150 (defvar gnus-select-group-hook nil
1151   "*A hook called when a newsgroup is selected.
1152
1153 If you'd like to simplify subjects like the
1154 `gnus-summary-next-same-subject' command does, you can use the
1155 following hook:
1156
1157  (setq gnus-select-group-hook
1158       (list
1159         (lambda ()
1160           (mapcar (lambda (header)
1161                      (mail-header-set-subject
1162                       header
1163                       (gnus-simplify-subject
1164                        (mail-header-subject header) 're-only)))
1165                   gnus-newsgroup-headers))))")
1166
1167 (defvar gnus-select-article-hook
1168   '(gnus-summary-show-thread)
1169   "*A hook called when an article is selected.
1170 The default hook shows conversation thread subtrees of the selected
1171 article automatically using `gnus-summary-show-thread'.")
1172
1173 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1174   "*A hook called to apply kill files to a group.
1175 This hook is intended to apply a kill file to the selected newsgroup.
1176 The function `gnus-apply-kill-file' is called by default.
1177
1178 Since a general kill file is too heavy to use only for a few
1179 newsgroups, I recommend you to use a lighter hook function. For
1180 example, if you'd like to apply a kill file to articles which contains
1181 a string `rmgroup' in subject in newsgroup `control', you can use the
1182 following hook:
1183
1184  (setq gnus-apply-kill-hook
1185       (list
1186         (lambda ()
1187           (cond ((string-match \"control\" gnus-newsgroup-name)
1188                  (gnus-kill \"Subject\" \"rmgroup\")
1189                  (gnus-expunge \"X\"))))))")
1190
1191 (defvar gnus-visual-mark-article-hook 
1192   (list 'gnus-highlight-selected-summary)
1193   "*Hook run after selecting an article in the summary buffer.
1194 It is meant to be used for highlighting the article in some way.  It
1195 is not run if `gnus-visual' is nil.")
1196
1197 (defun gnus-parse-headers-hook nil
1198   "*A hook called before parsing the headers.")
1199
1200 (defvar gnus-exit-group-hook nil
1201   "*A hook called when exiting (not quitting) summary mode.")
1202
1203 (defvar gnus-suspend-gnus-hook nil
1204   "*A hook called when suspending (not exiting) Gnus.")
1205
1206 (defvar gnus-exit-gnus-hook nil
1207   "*A hook called when exiting Gnus.")
1208
1209 (defvar gnus-save-newsrc-hook nil
1210   "*A hook called when saving the newsrc file.")
1211
1212 (defvar gnus-summary-update-hook 
1213   (list 'gnus-summary-highlight-line)
1214   "*A hook called when a summary line is changed.
1215 The hook will not be called if `gnus-visual' is nil.
1216
1217 The default function `gnus-summary-highlight-line' will
1218 highlight the line according to the `gnus-summary-highlight'
1219 variable.")
1220
1221 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1222   "*A hook called when an article is selected for the first time.
1223 The hook is intended to mark an article as read (or unread)
1224 automatically when it is selected.")
1225
1226 ;; Remove any hilit infestation.
1227 (add-hook 'gnus-startup-hook
1228           (lambda ()
1229             (remove-hook 'gnus-summary-prepare-hook
1230                          'hilit-rehighlight-buffer-quietly)
1231             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1232             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1233             (remove-hook 'gnus-article-prepare-hook
1234                          'hilit-rehighlight-buffer-quietly)))
1235
1236
1237 \f
1238 ;; Internal variables
1239
1240 ;; Avoid highlighting in kill files.
1241 (defvar gnus-summary-inhibit-highlight nil)
1242 (defvar gnus-newsgroup-selected-overlay nil)
1243
1244 (defvar gnus-article-mode-map nil)
1245 (defvar gnus-dribble-buffer nil)
1246 (defvar gnus-headers-retrieved-by nil)
1247 (defvar gnus-article-reply nil)
1248 (defvar gnus-override-method nil)
1249 (defvar gnus-article-check-size nil)
1250
1251 (defvar gnus-current-score-file nil)
1252 (defvar gnus-internal-global-score-files nil)
1253 (defvar gnus-score-file-list nil)
1254 (defvar gnus-scores-exclude-files nil)
1255
1256 (defvar gnus-current-move-group nil)
1257
1258 (defvar gnus-newsgroup-dependencies nil)
1259 (defvar gnus-newsgroup-threads nil)
1260 (defvar gnus-newsgroup-async nil)
1261 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1262
1263 (defvar gnus-newsgroup-adaptive nil)
1264
1265 (defvar gnus-summary-display-table nil)
1266
1267 (defconst gnus-group-line-format-alist
1268   (list (list ?M 'marked ?c)
1269         (list ?S 'subscribed ?c)
1270         (list ?L 'level ?d)
1271         (list ?N 'number ?s)
1272         (list ?I 'number-of-dormant ?d)
1273         (list ?T 'number-of-ticked ?d)
1274         (list ?R 'number-of-read ?s)
1275         (list ?t 'number-total ?d)
1276         (list ?y 'number-of-unread-unticked ?s)
1277         (list ?i 'number-of-ticked-and-dormant ?d)
1278         (list ?g 'group ?s)
1279         (list ?G 'qualified-group ?s)
1280         (list ?D 'newsgroup-description ?s)
1281         (list ?o 'moderated ?c)
1282         (list ?O 'moderated-string ?s)
1283         (list ?p 'process-marked ?c)
1284         (list ?s 'news-server ?s)
1285         (list ?n 'news-method ?s)
1286         (list ?z 'news-method-string ?s)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defconst gnus-summary-line-format-alist 
1290   (list (list ?N 'number ?d)
1291         (list ?S 'subject ?s)
1292         (list ?s 'subject-or-nil ?s)
1293         (list ?n 'name ?s)
1294         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1295               ?s)
1296         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1297                       from) ?s)
1298         (list ?F 'from ?s)
1299         (list ?x (macroexpand '(mail-header-xref header)) ?s)
1300         (list ?D (macroexpand '(mail-header-date header)) ?s)
1301         (list ?d '(gnus-dd-mmm (mail-header-date header)) ?s)
1302         (list ?M (macroexpand '(mail-header-id header)) ?s)
1303         (list ?r (macroexpand '(mail-header-references header)) ?s)
1304         (list ?c '(or (mail-header-chars header) 0) ?d)
1305         (list ?L 'lines ?d)
1306         (list ?I 'indentation ?s)
1307         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1308         (list ?R 'replied ?c)
1309         (list ?\[ 'opening-bracket ?c)
1310         (list ?\] 'closing-bracket ?c)
1311         (list ?\> '(make-string level ? ) ?s)
1312         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1313         (list ?i 'score ?d)
1314         (list ?z 'score-char ?c)
1315         (list ?U 'unread ?c)
1316         (list ?t '(gnus-summary-number-of-articles-in-thread 
1317                    (and (boundp 'thread) (car thread)))
1318               ?d)
1319         (list ?e '(gnus-summary-number-of-articles-in-thread 
1320                    (and (boundp 'thread) (car thread)) t)
1321               ?c)
1322         (list ?u 'user-defined ?s))
1323   "An alist of format specifications that can appear in summary lines,
1324 and what variables they correspond with, along with the type of the
1325 variable (string, integer, character, etc).")
1326
1327 (defconst gnus-summary-dummy-line-format-alist
1328   (list (list ?S 'subject ?s)
1329         (list ?N 'number ?d)
1330         (list ?u 'user-defined ?s)))
1331
1332 (defconst gnus-summary-mode-line-format-alist 
1333   (list (list ?G 'group-name ?s)
1334         (list ?g '(gnus-short-group-name group-name) ?s)
1335         (list ?A 'article-number ?d)
1336         (list ?Z 'unread-and-unselected ?s)
1337         (list ?V 'gnus-version ?s)
1338         (list ?U 'unread ?d)
1339         (list ?S 'subject ?s)
1340         (list ?e 'unselected ?d)
1341         (list ?u 'user-defined ?s)
1342         (list ?b 'buffer-name ?s)
1343         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1344
1345 (defconst gnus-group-mode-line-format-alist 
1346   (list (list ?S 'news-server ?s)
1347         (list ?M 'news-method ?s)
1348         (list ?b '(buffer-name) ?s)
1349         (list ?u 'user-defined ?s)))
1350
1351 (defvar gnus-have-read-active-file nil)
1352
1353 (defconst gnus-maintainer
1354   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1355   "The mail address of the Gnus maintainers.")
1356
1357 (defconst gnus-version "Gnus v5.0.13"
1358   "Version number for this version of Gnus.")
1359
1360 (defvar gnus-info-nodes
1361   '((gnus-group-mode            "(gnus)The Group Buffer")
1362     (gnus-summary-mode          "(gnus)The Summary Buffer")
1363     (gnus-article-mode          "(gnus)The Article Buffer"))
1364   "Assoc list of major modes and related Info nodes.")
1365
1366 (defvar gnus-group-buffer "*Group*")
1367 (defvar gnus-summary-buffer "*Summary*")
1368 (defvar gnus-article-buffer "*Article*")
1369 (defvar gnus-server-buffer "*Server*")
1370
1371 (defvar gnus-work-buffer " *gnus work*")
1372
1373 (defvar gnus-buffer-list nil
1374   "Gnus buffers that should be killed on exit.")
1375
1376 (defvar gnus-server-alist nil
1377   "List of available servers.")
1378
1379 (defvar gnus-variable-list
1380   '(gnus-newsrc-options gnus-newsrc-options-n
1381     gnus-newsrc-last-checked-date 
1382     gnus-newsrc-alist gnus-server-alist
1383     gnus-killed-list gnus-zombie-list)
1384   "Gnus variables saved in the quick startup file.")
1385
1386 (defvar gnus-overload-functions
1387   '((news-inews gnus-inews-news "rnewspost"))
1388   "Functions overloaded by gnus.
1389 It is a list of `(original overload &optional file)'.")
1390
1391 (defvar gnus-newsrc-options nil
1392   "Options line in the .newsrc file.")
1393
1394 (defvar gnus-newsrc-options-n nil
1395   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1396
1397 (defvar gnus-newsrc-last-checked-date nil
1398   "Date Gnus last asked server for new newsgroups.")
1399
1400 (defvar gnus-newsrc-alist nil
1401   "Assoc list of read articles.
1402 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1403
1404 (defvar gnus-newsrc-hashtb nil
1405   "Hashtable of gnus-newsrc-alist.")
1406
1407 (defvar gnus-killed-list nil
1408   "List of killed newsgroups.")
1409
1410 (defvar gnus-killed-hashtb nil
1411   "Hash table equivalent of gnus-killed-list.")
1412
1413 (defvar gnus-zombie-list nil
1414   "List of almost dead newsgroups.")
1415
1416 (defvar gnus-description-hashtb nil
1417   "Descriptions of newsgroups.")
1418
1419 (defvar gnus-list-of-killed-groups nil
1420   "List of newsgroups that have recently been killed by the user.")
1421
1422 (defvar gnus-active-hashtb nil
1423   "Hashtable of active articles.")
1424
1425 (defvar gnus-moderated-list nil
1426   "List of moderated newsgroups.")
1427
1428 (defvar gnus-group-marked nil)
1429
1430 (defvar gnus-current-startup-file nil
1431   "Startup file for the current host.")
1432
1433 (defvar gnus-last-search-regexp nil
1434   "Default regexp for article search command.")
1435
1436 (defvar gnus-last-shell-command nil
1437   "Default shell command on article.")
1438
1439 (defvar gnus-current-select-method nil
1440   "The current method for selecting a newsgroup.")
1441
1442 (defvar gnus-group-list-mode nil)
1443
1444 (defvar gnus-article-internal-prepare-hook nil)
1445
1446 (defvar gnus-newsgroup-name nil)
1447 (defvar gnus-newsgroup-begin nil)
1448 (defvar gnus-newsgroup-end nil)
1449 (defvar gnus-newsgroup-last-rmail nil)
1450 (defvar gnus-newsgroup-last-mail nil)
1451 (defvar gnus-newsgroup-last-folder nil)
1452 (defvar gnus-newsgroup-last-file nil)
1453 (defvar gnus-newsgroup-auto-expire nil)
1454 (defvar gnus-newsgroup-active nil)
1455
1456 (defvar gnus-newsgroup-unreads nil
1457   "List of unread articles in the current newsgroup.")
1458
1459 (defvar gnus-newsgroup-unselected nil
1460   "List of unselected unread articles in the current newsgroup.")
1461
1462 (defvar gnus-newsgroup-reads nil
1463   "Alist of read articles and article marks in the current newsgroup.")
1464
1465 (defvar gnus-newsgroup-marked nil
1466   "List of ticked articles in the current newsgroup (a subset of unread art).")
1467
1468 (defvar gnus-newsgroup-killed nil
1469   "List of ranges of articles that have been through the scoring process.")
1470
1471 (defvar gnus-newsgroup-kill-headers nil)
1472
1473 (defvar gnus-newsgroup-replied nil
1474   "List of articles that have been replied to in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expirable nil
1477   "List of articles in the current newsgroup that can be expired.")
1478
1479 (defvar gnus-newsgroup-processable nil
1480   "List of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-bookmarks nil
1483   "List of articles in the current newsgroup that have bookmarks.")
1484
1485 (defvar gnus-newsgroup-dormant nil
1486   "List of dormant articles in the current newsgroup.")
1487
1488 (defvar gnus-newsgroup-scored nil
1489   "List of scored articles in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-headers nil
1492   "List of article headers in the current newsgroup.")
1493 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1494
1495 (defvar gnus-newsgroup-ancient nil
1496   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1497
1498 (defvar gnus-current-article nil)
1499 (defvar gnus-article-current nil)
1500 (defvar gnus-current-headers nil)
1501 (defvar gnus-have-all-headers nil)
1502 (defvar gnus-last-article nil)
1503 (defvar gnus-newsgroup-history nil)
1504 (defvar gnus-current-kill-article nil)
1505
1506 ;; Save window configuration.
1507 (defvar gnus-prev-winconf nil)
1508
1509 ;; Format specs
1510 (defvar gnus-summary-line-format-spec nil)
1511 (defvar gnus-summary-dummy-line-format-spec nil)
1512 (defvar gnus-group-line-format-spec nil)
1513 (defvar gnus-summary-mode-line-format-spec nil)
1514 (defvar gnus-article-mode-line-format-spec nil)
1515 (defvar gnus-group-mode-line-format-spec nil)
1516 (defvar gnus-summary-mark-positions nil)
1517 (defvar gnus-group-mark-positions nil)
1518
1519 (defvar gnus-summary-expunge-below nil)
1520 (defvar gnus-reffed-article-number nil)
1521
1522 ; Let the byte-compiler know that we know about this variable.
1523 (defvar rmail-default-rmail-file)
1524
1525 (defvar gnus-cache-removeable-articles nil)
1526
1527 (defconst gnus-summary-local-variables 
1528   '(gnus-newsgroup-name 
1529     gnus-newsgroup-begin gnus-newsgroup-end 
1530     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1531     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1532     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1533     gnus-newsgroup-unselected gnus-newsgroup-marked
1534     gnus-newsgroup-reads
1535     gnus-newsgroup-replied gnus-newsgroup-expirable
1536     gnus-newsgroup-processable gnus-newsgroup-killed
1537     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1538     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1539     gnus-current-article gnus-current-headers gnus-have-all-headers
1540     gnus-last-article gnus-article-internal-prepare-hook
1541     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1542     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1543     gnus-newsgroup-threads gnus-newsgroup-async
1544     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1545     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1546     gnus-newsgroup-history gnus-newsgroup-ancient
1547     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1548     gnus-cache-removeable-articles)
1549   "Variables that are buffer-local to the summary buffers.")
1550
1551 (defconst gnus-bug-message
1552   "Sending a bug report to the Gnus Towers.
1553 ========================================
1554
1555 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1556 be sent to the Gnus Bug Exterminators. 
1557
1558 At the bottom of the buffer you'll see lots of variable settings.
1559 Please do not delete those.  They will tell the Bug People what your
1560 environment is, so that it will be easier to locate the bugs.
1561
1562 If you have found a bug that makes Emacs go \"beep\", set
1563 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1564 and include the backtrace in your bug report.
1565
1566 Please describe the bug in annoying, painstaking detail.
1567
1568 Thank you for your help in stamping out bugs.
1569 ")
1570
1571 ;;; End of variables.
1572
1573 ;; Define some autoload functions Gnus might use.
1574 (eval-and-compile
1575
1576   ;; Various 
1577   (autoload 'metamail-buffer "metamail")
1578   (autoload 'Info-goto-node "info")
1579   (autoload 'hexl-hex-string-to-integer "hexl")
1580   (autoload 'pp "pp")
1581   (autoload 'pp-to-string "pp")
1582   (autoload 'pp-eval-expression "pp")
1583   (autoload 'mail-extract-address-components "mail-extr")
1584
1585   (autoload 'nnmail-split-fancy "nnmail")
1586   (autoload 'nnvirtual-catchup-group "nnvirtual")
1587
1588   ;; timezone
1589   (autoload 'timezone-make-date-arpa-standard "timezone")
1590   (autoload 'timezone-fix-time "timezone")
1591   (autoload 'timezone-make-sortable-date "timezone")
1592   (autoload 'timezone-make-time-string "timezone")
1593
1594   ;; rmail & friends
1595   (autoload 'mail-position-on-field "sendmail")
1596   (autoload 'mail-setup "sendmail")
1597   (autoload 'rmail-output "rmailout")
1598   (autoload 'news-mail-other-window "rnewspost")
1599   (autoload 'news-reply-yank-original "rnewspost")
1600   (autoload 'news-caesar-buffer-body "rnewspost")
1601   (autoload 'rmail-insert-rmail-file-header "rmail")
1602   (autoload 'rmail-count-new-messages "rmail")
1603   (autoload 'rmail-show-message "rmail")
1604
1605   ;; gnus-soup
1606   ;;(autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1607   ;;(autoload 'gnus-brew-soup "gnus-soup" nil t)
1608   ;;(autoload 'gnus-soup-add-article "gnus-soup" nil t)
1609   ;;(autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1610   ;;(autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1611   ;;(autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1612   ;;(autoload 'nnsoup-pack-replies "nnsoup" nil t)
1613
1614   ;; gnus-mh
1615   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1616   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1617   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1618   (autoload 'gnus-summary-save-in-folder "gnus-mh" nil t)
1619   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1620   (autoload 'gnus-Folder-save-name "gnus-mh")
1621   (autoload 'gnus-folder-save-name "gnus-mh")
1622
1623   ;; gnus-vis misc
1624   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1625   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1626   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1627   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1628   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1629   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1630   (autoload 'gnus-summary-highlight-line "gnus-vis")
1631   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1632
1633   ;; gnus-vis article
1634   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1635   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1636   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1637   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1638   (autoload 'gnus-article-hide "gnus-vis" nil t)
1639   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1640   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1641   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1642   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1643   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1644   (autoload 'gnus-article-add-button "gnus-vis")
1645
1646   ;; gnus-cite
1647   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1648   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1649   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1650
1651   ;; gnus-kill
1652   (autoload 'gnus-kill "gnus-kill")
1653   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1654   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1655   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1656   (autoload 'gnus-execute "gnus-kill")
1657   (autoload 'gnus-expunge "gnus-kill")
1658
1659   ;; gnus-cache
1660   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1661   (autoload 'gnus-cache-save-buffers "gnus-cache")
1662   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1663   (autoload 'gnus-cache-request-article "gnus-cache")
1664   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1665   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1666   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1667   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1668
1669   ;; gnus-score
1670   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1671   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1672   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1673   (autoload 'gnus-score-save "gnus-score")
1674   (autoload 'gnus-score-headers "gnus-score")
1675   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1676   (autoload 'gnus-score-adaptive "gnus-score")
1677   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1678   (autoload 'gnus-score-find-trace "gnus-score")
1679
1680   ;; gnus-edit
1681   (autoload 'gnus-score-customize "gnus-edit" nil t)
1682
1683   ;; gnus-uu
1684   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1685   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1686   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1687   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1688   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1689   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1690   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1691   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1692   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1693   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1694   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1695   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1696   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1697   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1698   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1699   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1700   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1701   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1702   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1703   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1704   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1705   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1706
1707   ;; gnus-msg
1708   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1709   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1710   (autoload 'gnus-group-mail "gnus-msg" nil t)
1711   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1712   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1713   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1714   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1715   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1716   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1717   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1718   (autoload 'gnus-post-news "gnus-msg" nil t)
1719   (autoload 'gnus-inews-news "gnus-msg" nil t)
1720   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1721   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1722   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1723   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1724   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1725   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1726   (autoload 'gnus-mail-yank-original "gnus-msg")
1727   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1728   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1729   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1730   (autoload 'gnus-article-mail "gnus-msg")
1731   (autoload 'gnus-bug "gnus-msg" nil t)
1732
1733   ;; gnus-vm
1734   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1735   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1736   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1737   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1738   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1739   (autoload 'gnus-yank-article "gnus-vm" nil t)
1740
1741   )
1742
1743 \f
1744
1745 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1746 ;; If you want the cursor to go somewhere else, set these two
1747 ;; functions in some startup hook to whatever you want.
1748 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1749 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1750
1751 ;;; Various macros and substs.
1752
1753 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1754   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1755   (` (let ((GnusStartBufferWindow (selected-window)))
1756        (unwind-protect
1757            (progn
1758              (pop-to-buffer (, buffer))
1759              (,@ forms))
1760          (select-window GnusStartBufferWindow)))))
1761
1762 (defmacro gnus-gethash (string hashtable)
1763   "Get hash value of STRING in HASHTABLE."
1764   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1765   ;;(` (abbrev-expansion (, string) (, hashtable)))
1766   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1767
1768 (defmacro gnus-sethash (string value hashtable)
1769   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1770   ;; We cannot use define-abbrev since it only accepts string as value.
1771   ;; (set (intern string hashtable) value))
1772   (` (set (intern (, string) (, hashtable)) (, value))))
1773
1774 (defsubst gnus-buffer-substring (beg end)
1775   (buffer-substring (match-beginning beg) (match-end end)))
1776
1777 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1778 ;;   function `substring' might cut on a middle of multi-octet
1779 ;;   character.
1780 (defun gnus-truncate-string (str width)
1781   (substring str 0 width))
1782
1783 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1784 ;; to limit the length of a string. This function is necessary since
1785 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1786 (defsubst gnus-limit-string (str width)
1787   (if (> (length str) width)
1788       (substring str 0 width)
1789     str))
1790
1791 (defsubst gnus-simplify-subject-re (subject)
1792   "Remove \"Re:\" from subject lines."
1793   (let ((case-fold-search t))
1794     (if (string-match "^re: *" subject)
1795         (substring subject (match-end 0))
1796       subject)))
1797
1798 (defsubst gnus-goto-char (point)
1799   (and point (goto-char point)))
1800
1801 (defmacro gnus-buffer-exists-p (buffer)
1802   (` (and (, buffer)
1803           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1804                    (, buffer)))))
1805
1806 (defmacro gnus-kill-buffer (buffer)
1807   (` (if (gnus-buffer-exists-p (, buffer))
1808          (kill-buffer (, buffer)))))
1809
1810 (defsubst gnus-point-at-bol ()
1811   "Return point at the beginning of line."
1812   (let ((p (point)))
1813     (beginning-of-line)
1814     (prog1
1815         (point)
1816       (goto-char p))))
1817
1818 (defsubst gnus-point-at-eol ()
1819   "Return point at the beginning of line."
1820   (let ((p (point)))
1821     (end-of-line)
1822     (prog1
1823         (point)
1824       (goto-char p))))
1825
1826 ;; Delete the current line (and the next N lines.);
1827 (defmacro gnus-delete-line (&optional n)
1828   (` (delete-region (progn (beginning-of-line) (point))
1829                     (progn (forward-line (, (or n 1))) (point)))))
1830
1831 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1832 (defvar gnus-init-inhibit nil)
1833 (defun gnus-read-init-file (&optional inhibit-next)
1834   (if gnus-init-inhibit
1835       (setq gnus-init-inhibit nil)
1836     (setq gnus-init-inhibit inhibit-next)
1837     (and gnus-init-file
1838          (or (and (file-exists-p gnus-init-file) 
1839                   ;; Don't try to load a directory.
1840                   (not (file-directory-p gnus-init-file)))
1841              (file-exists-p (concat gnus-init-file ".el"))
1842              (file-exists-p (concat gnus-init-file ".elc")))
1843          (load gnus-init-file nil t))))
1844
1845 ;;; Load the user startup file.
1846 ;; (eval '(gnus-read-init-file 'inhibit))
1847
1848 ;;; Load the compatability functions. 
1849
1850 (require 'gnus-cus)
1851 (require 'gnus-ems)
1852
1853 \f
1854 ;;;
1855 ;;; Gnus Utility Functions
1856 ;;;
1857
1858 (defun gnus-extract-address-components (from)
1859   (let (name address)
1860     ;; First find the address - the thing with the @ in it.  This may
1861     ;; not be accurate in mail addresses, but does the trick most of
1862     ;; the time in news messages.
1863     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1864         (setq address (substring from (match-beginning 0) (match-end 0))))
1865     ;; Then we check whether the "name <address>" format is used.
1866     (and address
1867          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1868          ;; Linear white space is not required.
1869          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
1870          (and (setq name (substring from 0 (match-beginning 0)))
1871               ;; Strip any quotes from the name.
1872               (string-match "\".*\"" name)
1873               (setq name (substring name 1 (1- (match-end 0))))))
1874     ;; If not, then "address (name)" is used.
1875     (or name
1876         (and (string-match "(.+)" from)
1877              (setq name (substring from (1+ (match-beginning 0)) 
1878                                    (1- (match-end 0)))))
1879         (and (string-match "()" from)
1880              (setq name address))
1881         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1882         ;; XOVER might not support folded From headers.
1883         (and (string-match "(.*" from)
1884              (setq name (substring from (1+ (match-beginning 0)) 
1885                                    (match-end 0)))))
1886     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1887     (list (or name from) (or address from))))
1888
1889 (defun gnus-fetch-field (field)
1890   "Return the value of the header FIELD of current article."
1891   (save-excursion
1892     (save-restriction
1893       (let ((case-fold-search t))
1894         (gnus-narrow-to-headers)
1895         (mail-fetch-field field)))))
1896
1897 (defun gnus-goto-colon ()
1898   (beginning-of-line)
1899   (search-forward ":" (gnus-point-at-eol) t))
1900
1901 (defun gnus-narrow-to-headers ()
1902   (widen)
1903   (save-excursion
1904     (narrow-to-region
1905      (goto-char (point-min))
1906      (if (search-forward "\n\n" nil t)
1907          (1- (point))
1908        (point-max)))))
1909
1910 (defvar gnus-old-specs nil)
1911
1912 (defun gnus-update-format-specifications ()
1913   (gnus-make-thread-indent-array)
1914
1915   (let ((formats '(summary summary-dummy group 
1916                            summary-mode group-mode article-mode))
1917         old-format new-format)
1918     (while formats
1919       (setq new-format (symbol-value
1920                         (intern (format "gnus-%s-line-format" (car formats)))))
1921       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1922                (equal old-format new-format))
1923           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1924                (gnus-parse-format
1925                 new-format
1926                 (symbol-value 
1927                  (intern (format "gnus-%s-line-format-alist"
1928                                  (if (eq (car formats) 'article-mode)
1929                                      'summary-mode (car formats))))))))
1930       (setq gnus-old-specs (cons (cons (car formats) new-format)
1931                                  (delq (car formats) gnus-old-specs)))
1932       (setq formats (cdr formats))))
1933       
1934   (gnus-update-group-mark-positions)
1935   (gnus-update-summary-mark-positions)
1936
1937   (if (and (string-match "%D" gnus-group-line-format)
1938            (not gnus-description-hashtb)
1939            gnus-read-active-file)
1940       (gnus-read-all-descriptions-files)))
1941
1942 (defun gnus-update-summary-mark-positions ()
1943   (save-excursion
1944     (let ((gnus-replied-mark 129)
1945           (gnus-score-below-mark 130)
1946           (gnus-score-over-mark 130)
1947           (thread nil)
1948           pos)
1949       (gnus-set-work-buffer)
1950       (gnus-summary-insert-line 
1951        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1952       (goto-char (point-min))
1953       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1954                                          (- (point) 2)))))
1955       (goto-char (point-min))
1956       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1957                                           (- (point) 2))) pos))
1958       (goto-char (point-min))
1959       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1960                                         (- (point) 2))) pos))
1961       (setq gnus-summary-mark-positions pos))))
1962
1963 (defun gnus-update-group-mark-positions ()
1964   (save-excursion
1965     (let ((gnus-process-mark 128)
1966           (gnus-group-marked '("dummy.group")))
1967       (gnus-sethash "dummy.group" '(0 . 0) gnus-active-hashtb)
1968       (gnus-set-work-buffer)
1969       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1970       (goto-char (point-min))
1971       (setq gnus-group-mark-positions
1972             (list (cons 'process (and (search-forward "\200" nil t)
1973                                       (- (point) 2))))))))
1974
1975 (defun gnus-mouse-face-function (form)
1976   (` (let ((string (, form)))
1977        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1978        string)))
1979
1980 (defun gnus-max-width-function (el max-width)
1981   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1982   (` (let* ((val (eval (, el)))
1983             (valstr (if (numberp val)
1984                         (int-to-string val) val)))
1985        (if (> (length valstr) (, max-width))
1986            (substring valstr 0 (, max-width))
1987          valstr))))
1988
1989 (defun gnus-parse-format (format spec-alist)
1990   ;; This function parses the FORMAT string with the help of the
1991   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1992   ;; string.  If the FORMAT string contains the specifiers %( and %)
1993   ;; the text between them will have the mouse-face text property.
1994   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1995       (if (and gnus-visual gnus-mouse-face)
1996           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1997                 (button (substring format (match-beginning 2) (match-end 2)))
1998                 (post (substring format (match-beginning 3) (match-end 3))))
1999             (list 'concat
2000                   (gnus-parse-simple-format pre spec-alist)
2001                   (gnus-mouse-face-function 
2002                    (gnus-parse-simple-format button spec-alist))
2003                   (gnus-parse-simple-format post spec-alist)))
2004         (gnus-parse-simple-format
2005          (concat (substring format (match-beginning 1) (match-end 1))
2006                  (substring format (match-beginning 2) (match-end 2))
2007                  (substring format (match-beginning 3) (match-end 3)))
2008          spec-alist))
2009     (gnus-parse-simple-format format spec-alist)))
2010
2011 (defun gnus-parse-simple-format (format spec-alist)
2012   ;; This function parses the FORMAT string with the help of the
2013   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2014   ;; string. The list will consist of the symbol `format', a format
2015   ;; specification string, and a list of forms depending on the
2016   ;; SPEC-ALIST.
2017   (let ((max-width 0)
2018         spec flist fstring newspec elem beg)
2019     (save-excursion
2020       (gnus-set-work-buffer)
2021       (insert format)
2022       (goto-char (point-min))
2023       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?" nil t)
2024         (setq spec (string-to-char (buffer-substring (match-beginning 2)
2025                                                      (match-end 2))))
2026         ;; First check if there are any specs that look anything like
2027         ;; "%12,12A", ie. with a "max width specification". These have
2028         ;; to be treated specially.
2029         (if (setq beg (match-beginning 1))
2030             (setq max-width 
2031                   (string-to-int 
2032                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
2033           (setq max-width 0)
2034           (setq beg (match-beginning 2)))
2035         ;; Find the specification from `spec-alist'.
2036         (if (not (setq elem (cdr (assq spec spec-alist))))
2037             (setq elem '("*" ?s)))
2038         ;; Treat user defined format specifiers specially
2039         (and (eq (car elem) 'user-defined)
2040              (setq elem
2041                    (list 
2042                     (list (intern (concat "gnus-user-format-function-"
2043                                           (buffer-substring
2044                                            (match-beginning 3)
2045                                            (match-end 3))))
2046                           'header)
2047                     ?s))
2048              (delete-region (match-beginning 3) (match-end 3)))
2049         (if (not (zerop max-width))
2050             (let ((el (car elem)))
2051               (cond ((= (car (cdr elem)) ?c) 
2052                      (setq el (list 'char-to-string el)))
2053                     ((= (car (cdr elem)) ?d)
2054                      (numberp el) (setq el (list 'int-to-string el))))
2055               (setq flist (cons (gnus-max-width-function el max-width)
2056                                 flist))
2057               (setq newspec ?s))
2058           (setq flist (cons (car elem) flist))
2059           (setq newspec (car (cdr elem))))
2060         ;; Remove the old specification (and possibly a ",12" string).
2061         (delete-region beg (match-end 2))
2062         ;; Insert the new specification.
2063         (goto-char beg)
2064         (insert newspec))
2065       (setq fstring (buffer-substring 1 (point-max))))
2066     (cons 'format (cons fstring (nreverse flist)))))
2067
2068 (defun gnus-set-work-buffer ()
2069   (if (get-buffer gnus-work-buffer)
2070       (progn
2071         (set-buffer gnus-work-buffer)
2072         (erase-buffer))
2073     (set-buffer (get-buffer-create gnus-work-buffer))
2074     (kill-all-local-variables)
2075     (buffer-disable-undo (current-buffer))
2076     (gnus-add-current-to-buffer-list)))
2077
2078 ;; Article file names when saving.
2079
2080 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2081   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2082 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2083 Otherwise, it is like ~/News/news/group/num."
2084   (let ((default
2085           (expand-file-name
2086            (concat (if (gnus-use-long-file-name 'not-save)
2087                        (gnus-capitalize-newsgroup newsgroup)
2088                      (gnus-newsgroup-directory-form newsgroup))
2089                    "/" (int-to-string (mail-header-number headers)))
2090            (or gnus-article-save-directory "~/News"))))
2091     (if (and last-file
2092              (string-equal (file-name-directory default)
2093                            (file-name-directory last-file))
2094              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2095         default
2096       (or last-file default))))
2097
2098 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2099   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2100 If variable `gnus-use-long-file-name' is non-nil, it is
2101 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2102   (let ((default
2103           (expand-file-name
2104            (concat (if (gnus-use-long-file-name 'not-save)
2105                        newsgroup
2106                      (gnus-newsgroup-directory-form newsgroup))
2107                    "/" (int-to-string (mail-header-number headers)))
2108            (or gnus-article-save-directory "~/News"))))
2109     (if (and last-file
2110              (string-equal (file-name-directory default)
2111                            (file-name-directory last-file))
2112              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2113         default
2114       (or last-file default))))
2115
2116 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2117   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2118 If variable `gnus-use-long-file-name' is non-nil, it is
2119 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2120   (or last-file
2121       (expand-file-name
2122        (if (gnus-use-long-file-name 'not-save)
2123            (gnus-capitalize-newsgroup newsgroup)
2124          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2125        (or gnus-article-save-directory "~/News"))))
2126
2127 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2128   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2129 If variable `gnus-use-long-file-name' is non-nil, it is
2130 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2131   (or last-file
2132       (expand-file-name
2133        (if (gnus-use-long-file-name 'not-save)
2134            newsgroup
2135          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2136        (or gnus-article-save-directory "~/News"))))
2137
2138 ;; For subscribing new newsgroup
2139
2140 (defun gnus-subscribe-hierarchical-interactive (groups)
2141   (let ((groups (sort groups 'string<))
2142         prefixes prefix start ans group starts)
2143     (while groups
2144       (setq prefixes (list "^"))
2145       (while (and groups prefixes)
2146         (while (not (string-match (car prefixes) (car groups)))
2147           (setq prefixes (cdr prefixes)))
2148         (setq prefix (car prefixes))
2149         (setq start (1- (length prefix)))
2150         (if (and (string-match "[^\\.]\\." (car groups) start)
2151                  (cdr groups)
2152                  (setq prefix 
2153                        (concat "^" (substring (car groups) 0 (match-end 0))))
2154                  (string-match prefix (car (cdr groups))))
2155             (progn
2156               (setq prefixes (cons prefix prefixes))
2157               (message "Descend hierarchy %s? ([y]nsq): " 
2158                        (substring prefix 1 (1- (length prefix))))
2159               (setq ans (read-char))
2160               (cond ((= ans ?n)
2161                      (while (and groups 
2162                                  (string-match prefix 
2163                                                (setq group (car groups))))
2164                        (setq gnus-killed-list 
2165                              (cons group gnus-killed-list))
2166                        (gnus-sethash group group gnus-killed-hashtb)
2167                        (setq groups (cdr groups)))
2168                      (setq starts (cdr starts)))
2169                     ((= ans ?s)
2170                      (while (and groups 
2171                                  (string-match prefix 
2172                                                (setq group (car groups))))
2173                        (gnus-sethash group group gnus-killed-hashtb)
2174                        (gnus-subscribe-alphabetically (car groups))
2175                        (setq groups (cdr groups)))
2176                      (setq starts (cdr starts)))
2177                     ((= ans ?q)
2178                      (while groups
2179                        (setq group (car groups))
2180                        (setq gnus-killed-list (cons group gnus-killed-list))
2181                        (gnus-sethash group group gnus-killed-hashtb)
2182                        (setq groups (cdr groups))))
2183                     (t nil)))
2184           (message "Subscribe %s? ([n]yq)" (car groups))
2185           (setq ans (read-char))
2186           (setq group (car groups))
2187           (cond ((= ans ?y)
2188                  (gnus-subscribe-alphabetically (car groups))
2189                  (gnus-sethash group group gnus-killed-hashtb))
2190                 ((= ans ?q)
2191                  (while groups
2192                    (setq group (car groups))
2193                    (setq gnus-killed-list (cons group gnus-killed-list))
2194                    (gnus-sethash group group gnus-killed-hashtb)
2195                    (setq groups (cdr groups))))
2196                 (t 
2197                  (setq gnus-killed-list (cons group gnus-killed-list))
2198                  (gnus-sethash group group gnus-killed-hashtb)))
2199           (setq groups (cdr groups)))))))
2200
2201 (defun gnus-subscribe-randomly (newsgroup)
2202   "Subscribe new NEWSGROUP by making it the first newsgroup."
2203   (gnus-subscribe-newsgroup newsgroup))
2204
2205 (defun gnus-subscribe-alphabetically (newgroup)
2206   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2207   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2208   (let ((groups (cdr gnus-newsrc-alist))
2209         before)
2210     (while (and (not before) groups)
2211       (if (string< newgroup (car (car groups)))
2212           (setq before (car (car groups)))
2213         (setq groups (cdr groups))))
2214     (gnus-subscribe-newsgroup newgroup before)))
2215
2216 (defun gnus-subscribe-hierarchically (newgroup)
2217   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2218   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2219   (save-excursion
2220     (set-buffer (find-file-noselect gnus-current-startup-file))
2221     (let ((groupkey newgroup)
2222           before)
2223       (while (and (not before) groupkey)
2224         (goto-char (point-min))
2225         (let ((groupkey-re
2226                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2227           (while (and (re-search-forward groupkey-re nil t)
2228                       (progn
2229                         (setq before (buffer-substring
2230                                       (match-beginning 1) (match-end 1)))
2231                         (string< before newgroup)))))
2232         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2233         (setq groupkey
2234               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2235                   (substring groupkey (match-beginning 1) (match-end 1)))))
2236       (gnus-subscribe-newsgroup newgroup before))))
2237
2238 (defun gnus-subscribe-interactively (newsgroup)
2239   "Subscribe new NEWSGROUP interactively.
2240 It is inserted in hierarchical newsgroup order if subscribed. If not,
2241 it is killed."
2242   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2243       (gnus-subscribe-hierarchically newsgroup)
2244     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2245
2246 (defun gnus-subscribe-zombies (newsgroup)
2247   "Make new NEWSGROUP a zombie group."
2248   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2249
2250 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2251   "Subscribe new NEWSGROUP.
2252 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2253 the first newsgroup."
2254   ;; We subscribe the group by changing its level to `subscribed'.
2255   (gnus-group-change-level 
2256    newsgroup gnus-level-default-subscribed
2257    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2258   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2259
2260 ;; For directories
2261
2262 (defun gnus-newsgroup-directory-form (newsgroup)
2263   "Make hierarchical directory name from NEWSGROUP name."
2264   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2265         (len (length newsgroup))
2266         idx)
2267     ;; If this is a foreign group, we don't want to translate the
2268     ;; entire name.  
2269     (if (setq idx (string-match ":" newsgroup))
2270         (aset newsgroup idx ?/)
2271       (setq idx 0))
2272     ;; Replace all occurrences of `.' with `/'.
2273     (while (< idx len)
2274       (if (= (aref newsgroup idx) ?.)
2275           (aset newsgroup idx ?/))
2276       (setq idx (1+ idx)))
2277     newsgroup))
2278
2279 (defun gnus-newsgroup-saveable-name (group)
2280   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2281   ;; with dots.
2282   (gnus-replace-chars-in-string group ?/ ?.))
2283
2284 (defun gnus-make-directory (dir)
2285   "Make DIRECTORY recursively."
2286   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2287   ;; of the many mysteries of the universe.
2288   (let* ((dir (expand-file-name dir default-directory))
2289          dirs err)
2290     (if (string-match "/$" dir)
2291         (setq dir (substring dir 0 (match-beginning 0))))
2292     ;; First go down the path until we find a directory that exists.
2293     (while (not (file-exists-p dir))
2294       (setq dirs (cons dir dirs))
2295       (string-match "/[^/]+$" dir)
2296       (setq dir (substring dir 0 (match-beginning 0))))
2297     ;; Then create all the subdirs.
2298     (while (and dirs (not err))
2299       (condition-case ()
2300           (make-directory (car dirs))
2301         (error (setq err t)))
2302       (setq dirs (cdr dirs)))
2303     ;; We return whether we were successful or not. 
2304     (not dirs)))
2305
2306 (defun gnus-capitalize-newsgroup (newsgroup)
2307   "Capitalize NEWSGROUP name."
2308   (and (not (zerop (length newsgroup)))
2309        (concat (char-to-string (upcase (aref newsgroup 0)))
2310                (substring newsgroup 1))))
2311
2312 ;; Var
2313
2314 (defun gnus-simplify-subject (subject &optional re-only)
2315   "Remove `Re:' and words in parentheses.
2316 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2317   (let ((case-fold-search t))           ;Ignore case.
2318     ;; Remove `Re:' and `Re^N:'.
2319     (if (string-match "^re:[ \t]*" subject)
2320         (setq subject (substring subject (match-end 0))))
2321     ;; Remove words in parentheses from end.
2322     (or re-only
2323         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2324           (setq subject (substring subject 0 (match-beginning 0)))))
2325     ;; Return subject string.
2326     subject))
2327
2328 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2329 ;; all whitespace.
2330 (defun gnus-simplify-subject-fuzzy (subject)
2331   (let ((case-fold-search t))
2332     (save-excursion
2333       (gnus-set-work-buffer)
2334       (insert subject)
2335       (inline (gnus-simplify-buffer-fuzzy))
2336       (buffer-string))))
2337
2338 (defun gnus-simplify-buffer-fuzzy ()
2339   (goto-char (point-min))
2340   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2341   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2342                             nil t)
2343     (replace-match "" t t))
2344   (goto-char (point-min))
2345   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2346     (replace-match "" t t))
2347   (goto-char (point-min))
2348   (while (re-search-forward "[ \t]+" nil t)
2349     (replace-match " " t t))
2350   (goto-char (point-min))
2351   (while (re-search-forward "[ \t]+$" nil t)
2352     (replace-match "" t t))
2353   (goto-char (point-min))
2354   (while (re-search-forward "^[ \t]+" nil t)
2355     (replace-match "" t t))
2356   (if gnus-simplify-subject-fuzzy-regexp
2357       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2358         (replace-match "" t t))))
2359
2360 ;; Add the current buffer to the list of buffers to be killed on exit. 
2361 (defun gnus-add-current-to-buffer-list ()
2362   (or (memq (current-buffer) gnus-buffer-list)
2363       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2364
2365 (defun gnus-string> (s1 s2)
2366   (not (or (string< s1 s2)
2367            (string= s1 s2))))
2368
2369 ;; Functions accessing headers.
2370 ;; Functions are more convenient than macros in some cases.
2371
2372 (defun gnus-header-number (header)
2373   (mail-header-number header))
2374
2375 (defun gnus-header-subject (header)
2376   (mail-header-subject header))
2377
2378 (defun gnus-header-from (header)
2379   (mail-header-from header))
2380
2381 (defun gnus-header-xref (header)
2382   (mail-header-xref header))
2383
2384 (defun gnus-header-lines (header)
2385   (mail-header-lines header))
2386
2387 (defun gnus-header-date (header)
2388   (mail-header-date header))
2389
2390 (defun gnus-header-id (header)
2391   (mail-header-id header))
2392
2393 (defun gnus-header-message-id (header)
2394   (mail-header-id header))
2395
2396 (defun gnus-header-chars (header)
2397   (mail-header-chars header))
2398
2399 (defun gnus-header-references (header)
2400   (mail-header-references header))
2401
2402 ;;; General various misc type functions.
2403
2404 (defun gnus-clear-system ()
2405   "Clear all variables and buffers."
2406   ;; Clear Gnus variables.
2407   (let ((variables gnus-variable-list))
2408     (while variables
2409       (set (car variables) nil)
2410       (setq variables (cdr variables))))
2411   ;; Clear other internal variables.
2412   (setq gnus-list-of-killed-groups nil
2413         gnus-have-read-active-file nil
2414         gnus-newsrc-alist nil
2415         gnus-newsrc-hashtb nil
2416         gnus-killed-list nil
2417         gnus-zombie-list nil
2418         gnus-killed-hashtb nil
2419         gnus-active-hashtb nil
2420         gnus-moderated-list nil
2421         gnus-description-hashtb nil
2422         gnus-newsgroup-headers nil
2423         gnus-newsgroup-headers-hashtb-by-number nil
2424         gnus-newsgroup-name nil
2425         gnus-server-alist nil
2426         gnus-current-select-method nil)
2427   ;; Reset any score variables.
2428   (and (boundp 'gnus-score-cache)
2429        (set 'gnus-score-cache nil))
2430   (and (boundp 'gnus-internal-global-score-files)
2431        (set 'gnus-internal-global-score-files nil))
2432   ;; Kill the startup file.
2433   (and gnus-current-startup-file
2434        (get-file-buffer gnus-current-startup-file)
2435        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2436   ;; Save any cache buffers.
2437   (and gnus-use-cache (gnus-cache-save-buffers))
2438   ;; Clear the dribble buffer.
2439   (gnus-dribble-clear)
2440   ;; Kill global KILL file buffer.
2441   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2442       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2443   (gnus-kill-buffer nntp-server-buffer)
2444   ;; Kill Gnus buffers.
2445   (while gnus-buffer-list
2446     (gnus-kill-buffer (car gnus-buffer-list))
2447     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2448
2449 (defun gnus-windows-old-to-new (setting)
2450   ;; First we take care of the really, really old Gnus 3 actions.
2451   (if (symbolp setting)
2452       (setq setting 
2453             (cond ((memq setting '(SelectArticle))
2454                    'article)
2455                   ((memq setting '(SelectSubject ExpandSubject))
2456                    'summary)
2457                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
2458                    'group)
2459                   (t setting))))
2460   (if (or (listp setting)
2461           (not (and gnus-window-configuration
2462                     (memq setting '(group summary article)))))
2463       setting
2464     (let* ((setting (if (eq setting 'group) 
2465                         (if (assq 'newsgroup gnus-window-configuration)
2466                             'newsgroup
2467                           'newsgroups) setting))
2468            (elem (car (cdr (assq setting gnus-window-configuration))))
2469            (total (apply '+ elem))
2470            (types '(group summary article))
2471            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2472            (i 0)
2473            perc
2474            out)
2475       (while (< i 3)
2476         (or (not (numberp (nth i elem)))
2477             (zerop (nth i elem))
2478             (progn
2479               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2480               (setq out (cons (if (eq pbuf (nth i types))
2481                                   (vector (nth i types) perc 'point)
2482                                 (vector (nth i types) perc))
2483                               out))))
2484         (setq i (1+ i)))
2485       (list (nreverse out)))))
2486            
2487 (defun gnus-add-configuration (conf)
2488   (setq gnus-buffer-configuration 
2489         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2490                          gnus-buffer-configuration))))
2491
2492 (defun gnus-configure-windows (setting &optional force)
2493   (setq setting (gnus-windows-old-to-new setting))
2494   (let ((r (if (symbolp setting)
2495                (cdr (assq setting gnus-buffer-configuration))
2496              setting))
2497         (in-buf (current-buffer))
2498         rule val w height hor ohor heights sub jump-buffer
2499         rel total to-buf all-visible)
2500     (or r (error "No such setting: %s" setting))
2501
2502     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2503         ;; All the windows mentioned are already visible, so we just
2504         ;; put point in the assigned buffer, and do not touch the
2505         ;; winconf. 
2506         (select-window (get-buffer-window all-visible t))
2507          
2508
2509       ;; Either remove all windows or just remove all Gnus windows.
2510       (if gnus-use-full-window
2511           (delete-other-windows)
2512         (gnus-remove-some-windows)
2513         (switch-to-buffer nntp-server-buffer))
2514
2515       (while r
2516         (setq hor (car r)
2517               ohor nil)
2518
2519         ;; We have to do the (possible) horizontal splitting before the
2520         ;; vertical. 
2521         (if (and (listp (car hor)) 
2522                  (eq (car (car hor)) 'horizontal))
2523             (progn
2524               (split-window 
2525                nil
2526                (if (integerp (nth 1 (car hor)))
2527                    (nth 1 (car hor))
2528                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2529                t)
2530               (setq hor (cdr hor))))
2531
2532         ;; Go through the rules and eval the elements that are to be
2533         ;; evaled.  
2534         (while hor
2535           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2536               (progn
2537                 ;; Expand short buffer name.
2538                 (setq w (aref val 0))
2539                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2540                      (progn
2541                        (setq val (apply 'vector (mapcar 'identity val)))
2542                        (aset val 0 w)))
2543                 (setq ohor (cons val ohor))))
2544           (setq hor (cdr hor)))
2545         (setq rule (cons (nreverse ohor) rule))
2546         (setq r (cdr r)))
2547       (setq rule (nreverse rule))
2548
2549       ;; We tally the window sizes.
2550       (setq total (window-height))
2551       (while rule
2552         (setq hor (car rule))
2553         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2554             (setq hor (cdr hor)))
2555         (setq sub 0)
2556         (while hor
2557           (setq rel (aref (car hor) 1)
2558                 heights (cons
2559                          (cond ((and (floatp rel) (= 1.0 rel))
2560                                 'x)
2561                                ((integerp rel)
2562                                 rel)
2563                                (t
2564                                 (max (floor (* total rel)) 4)))
2565                          heights)
2566                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2567                 hor (cdr hor)))
2568         (setq heights (nreverse heights)
2569               hor (car rule))
2570
2571         ;; We then go through these heighs and create windows for them.
2572         (while heights
2573           (setq height (car heights)
2574                 heights (cdr heights))
2575           (and (eq height 'x)
2576                (setq height (- total sub)))
2577           (and heights
2578                (split-window nil height))
2579           (setq to-buf (aref (car hor) 0))
2580           (switch-to-buffer 
2581            (cond ((not to-buf)
2582                   in-buf)
2583                  ((symbolp to-buf)
2584                   (symbol-value (aref (car hor) 0)))
2585                  (t
2586                   (aref (car hor) 0))))
2587           (and (> (length (car hor)) 2)
2588                (eq (aref (car hor) 2) 'point)
2589                (setq jump-buffer (current-buffer)))
2590           (other-window 1)
2591           (setq hor (cdr hor)))
2592       
2593         (setq rule (cdr rule)))
2594
2595       ;; Finally, we pop to the buffer that's supposed to have point. 
2596       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2597
2598       (select-window (get-buffer-window jump-buffer t))
2599       (set-buffer jump-buffer))))
2600
2601 (defun gnus-all-windows-visible-p (rule)
2602   (let (invisible hor jump-buffer val buffer)
2603     ;; Go through the rules and eval the elements that are to be
2604     ;; evaled.  
2605     (while (and rule (not invisible))
2606       (setq hor (car rule)
2607             rule (cdr rule))
2608       (while (and hor (not invisible))
2609         (if (setq val (if (vectorp (car hor)) 
2610                           (car hor)
2611                         (if (not (eq (car (car hor)) 'horizontal))
2612                             (eval (car hor)))))
2613             (progn
2614               ;; Expand short buffer name.
2615               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2616                                (aref val 0)))
2617               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2618                              buffer))
2619               (and (> (length val) 2) (eq 'point (aref val 2))
2620                    (setq jump-buffer buffer))
2621               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2622         (setq hor (cdr hor))))
2623     (and (not invisible) jump-buffer)))
2624
2625 (defun gnus-window-top-edge (&optional window)
2626   (nth 1 (window-edges window)))
2627
2628 (defun gnus-remove-some-windows ()
2629   (let ((buffers gnus-window-to-buffer)
2630         buf bufs lowest-buf lowest)
2631     (save-excursion
2632       ;; Remove windows on all known Gnus buffers.
2633       (while buffers
2634         (setq buf (cdr (car buffers)))
2635         (if (symbolp buf)
2636             (setq buf (and (boundp buf) (symbol-value buf))))
2637         (and buf 
2638              (get-buffer-window buf)
2639              (progn
2640                (setq bufs (cons buf bufs))
2641                (pop-to-buffer buf)
2642                (if (or (not lowest)
2643                        (< (gnus-window-top-edge) lowest))
2644                    (progn
2645                      (setq lowest (gnus-window-top-edge))
2646                      (setq lowest-buf buf)))))
2647         (setq buffers (cdr buffers)))
2648       ;; Remove windows on *all* summary buffers.
2649       (let (wins)
2650         (walk-windows
2651          (lambda (win)
2652            (let ((buf (window-buffer win)))
2653              (if (string-match  "^\\*Summary" (buffer-name buf))
2654                  (progn
2655                    (setq bufs (cons buf bufs))
2656                    (pop-to-buffer buf)
2657                    (if (or (not lowest)
2658                            (< (gnus-window-top-edge) lowest))
2659                        (progn
2660                          (setq lowest-buf buf)
2661                          (setq lowest (gnus-window-top-edge))))))))))
2662       (and lowest-buf 
2663            (progn
2664              (pop-to-buffer lowest-buf)
2665              (switch-to-buffer nntp-server-buffer)))
2666       (while bufs
2667         (and (not (eq (car bufs) lowest-buf))
2668              (delete-windows-on (car bufs)))
2669         (setq bufs (cdr bufs))))))
2670                           
2671 (defun gnus-version ()
2672   "Version numbers of this version of Gnus."
2673   (interactive)
2674   (let ((methods gnus-valid-select-methods)
2675         (mess gnus-version)
2676         meth)
2677     ;; Go through all the legal select methods and add their version
2678     ;; numbers to the total version string. Only the backends that are
2679     ;; currently in use will have their message numbers taken into
2680     ;; consideration. 
2681     (while methods
2682       (setq meth (intern (concat (car (car methods)) "-version")))
2683       (and (boundp meth)
2684            (stringp (symbol-value meth))
2685            (setq mess (concat mess "; " (symbol-value meth))))
2686       (setq methods (cdr methods)))
2687     (gnus-message 2 mess)))
2688
2689 (defun gnus-info-find-node ()
2690   "Find Info documentation of Gnus."
2691   (interactive)
2692   ;; Enlarge info window if needed.
2693   (let ((mode major-mode))
2694     (gnus-configure-windows 'info)
2695     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2696
2697 (defun gnus-overload-functions (&optional overloads)
2698   "Overload functions specified by optional argument OVERLOADS.
2699 If nothing is specified, use the variable gnus-overload-functions."
2700   (let ((defs nil)
2701         (overloads (or overloads gnus-overload-functions)))
2702     (while overloads
2703       (setq defs (car overloads))
2704       (setq overloads (cdr overloads))
2705       ;; Load file before overloading function if necessary.  Make
2706       ;; sure we cannot use `require' always.
2707       (and (not (fboundp (car defs)))
2708            (car (cdr (cdr defs)))
2709            (load (car (cdr (cdr defs))) nil 'nomessage))
2710       (fset (car defs) (car (cdr defs))))))
2711
2712 (defun gnus-replace-chars-in-string (string &rest pairs)
2713   "Replace characters in STRING from FROM to TO."
2714   (let ((string (substring string 0))   ;Copy string.
2715         (len (length string))
2716         (idx 0)
2717         sym to)
2718     (or (zerop (% (length pairs) 2)) 
2719         (error "Odd number of translation pairs"))
2720     (setplist 'sym pairs)
2721     ;; Replace all occurrences of FROM with TO.
2722     (while (< idx len)
2723       (if (setq to (get 'sym (aref string idx)))
2724           (aset string idx to))
2725       (setq idx (1+ idx)))
2726     string))
2727
2728 (defun gnus-days-between (date1 date2)
2729   ;; Return the number of days between date1 and date2.
2730   (- (gnus-day-number date1) (gnus-day-number date2)))
2731
2732 (defun gnus-day-number (date)
2733   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2734                      (timezone-parse-date date))))
2735     (timezone-absolute-from-gregorian 
2736      (nth 1 dat) (nth 2 dat) (car dat))))
2737
2738 ;; Returns a floating point number that says how many seconds have
2739 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2740 (defun gnus-seconds-since-epoch (date)
2741   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2742                         (timezone-parse-date date)))
2743          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2744                         (timezone-parse-time
2745                          (aref (timezone-parse-date date) 3))))
2746          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2747                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2748          (tday (- (timezone-absolute-from-gregorian 
2749                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2750                   (timezone-absolute-from-gregorian 
2751                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2752     (+ (nth 2 ttime)
2753        (* (nth 1 ttime) 60)
2754        (* 1.0 (nth 0 ttime) 60 60)
2755        (* 1.0 tday 60 60 24))))
2756
2757 (defun gnus-file-newer-than (file date)
2758   (let ((fdate (nth 5 (file-attributes file))))
2759     (or (> (car fdate) (car date))
2760         (and (= (car fdate) (car date))
2761              (> (nth 1 fdate) (nth 1 date))))))
2762
2763 (defun gnus-group-read-only-p (&optional group)
2764   "Check whether GROUP supports editing or not.
2765 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
2766 that that variable is buffer-local to the summary buffers."
2767   (let ((group (or group gnus-newsgroup-name)))
2768     (not (gnus-check-backend-function 'request-replace-article group))))
2769
2770 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2771 ;; the echo area.
2772 (defun gnus-y-or-n-p (prompt)
2773   (prog1
2774       (y-or-n-p prompt)
2775     (message "")))
2776
2777 (defun gnus-yes-or-no-p (prompt)
2778   (prog1
2779       (yes-or-no-p prompt)
2780     (message "")))
2781
2782 ;; Check whether to use long file names.
2783 (defun gnus-use-long-file-name (symbol)
2784   ;; The variable has to be set...
2785   (and gnus-use-long-file-name
2786        ;; If it isn't a list, then we return t.
2787        (or (not (listp gnus-use-long-file-name))
2788            ;; If it is a list, and the list contains `symbol', we
2789            ;; return nil.  
2790            (not (memq symbol gnus-use-long-file-name)))))
2791
2792 ;; I suspect there's a better way, but I haven't taken the time to do
2793 ;; it yet. -erik selberg@cs.washington.edu
2794 (defun gnus-dd-mmm (messy-date)
2795   "Return a string like DD-MMM from a big messy string"
2796   (let ((datevec (timezone-parse-date messy-date)))
2797     (format "%2s-%s"
2798             (or (aref datevec 2) "??")
2799             (capitalize
2800              (or (car 
2801                   (nth (1- (string-to-number (aref datevec 1)))
2802                        timezone-months-assoc))
2803                  "???")))))
2804
2805 ;; Make a hash table (default and minimum size is 255).
2806 ;; Optional argument HASHSIZE specifies the table size.
2807 (defun gnus-make-hashtable (&optional hashsize)
2808   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2809
2810 ;; Make a number that is suitable for hashing; bigger than MIN and one
2811 ;; less than 2^x.
2812 (defun gnus-create-hash-size (min)
2813   (let ((i 1))
2814     (while (< i min)
2815       (setq i (* 2 i)))
2816     (1- i)))
2817
2818 ;; Show message if message has a lower level than `gnus-verbose'. 
2819 ;; Guide-line for numbers:
2820 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2821 ;; for things that take a long time, 7 - not very important messages
2822 ;; on stuff, 9 - messages inside loops.
2823 (defun gnus-message (level &rest args)
2824   (if (<= level gnus-verbose)
2825       (apply 'message args)
2826     ;; We have to do this format thingie here even if the result isn't
2827     ;; shown - the return value has to be the same as the return value
2828     ;; from `message'.
2829     (apply 'format args)))
2830
2831 ;; Generate a unique new group name.
2832 (defun gnus-generate-new-group-name (leaf)
2833   (let ((name leaf)
2834         (num 0))
2835     (while (gnus-gethash name gnus-newsrc-hashtb)
2836       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2837     name))
2838
2839 (defun gnus-ephemeral-group-p (group)
2840   "Say whether GROUP is ephemeral or not."
2841   (assoc 'quit-config (gnus-find-method-for-group group)))
2842
2843 (defun gnus-group-quit-config (group)
2844   "Return the quit-config of GROUP."
2845   (nth 1 (assoc 'quit-config (gnus-find-method-for-group group))))
2846
2847 (defun gnus-simplify-mode-line ()
2848   "Make mode lines a bit simpler."
2849   (setq mode-line-modified "-- ")
2850   (if (listp mode-line-format)
2851       (progn
2852         (make-local-variable 'mode-line-format)
2853         (setq mode-line-format (copy-sequence mode-line-format))
2854         (and (equal (nth 3 mode-line-format) "   ")
2855              (setcar (nthcdr 3 mode-line-format) "")))))
2856
2857 ;;; List and range functions
2858
2859 (defun gnus-last-element (list)
2860   "Return last element of LIST."
2861   (while (cdr list)
2862     (setq list (cdr list)))
2863   (car list))
2864
2865 (defun gnus-copy-sequence (list)
2866   "Do a complete, total copy of a list."
2867   (if (and (consp list) (not (consp (cdr list))))
2868       (cons (car list) (cdr list))
2869     (mapcar (lambda (elem) (if (consp elem) 
2870                                (if (consp (cdr elem))
2871                                    (gnus-copy-sequence elem)
2872                                  (cons (car elem) (cdr elem)))
2873                              elem))
2874             list)))
2875
2876 (defun gnus-set-difference (list1 list2)
2877   "Return a list of elements of LIST1 that do not appear in LIST2."
2878   (let ((list1 (copy-sequence list1)))
2879     (while list2
2880       (setq list1 (delq (car list2) list1))
2881       (setq list2 (cdr list2)))
2882     list1))
2883
2884 (defun gnus-sorted-complement (list1 list2)
2885   "Return a list of elements of LIST1 that do not appear in LIST2.
2886 Both lists have to be sorted over <."
2887   (let (out)
2888     (if (or (null list1) (null list2))
2889         (or list1 list2)
2890       (while (and list1 list2)
2891         (cond ((= (car list1) (car list2))
2892                (setq list1 (cdr list1)
2893                      list2 (cdr list2)))
2894               ((< (car list1) (car list2))
2895                (setq out (cons (car list1) out))
2896                (setq list1 (cdr list1)))
2897               (t
2898                (setq out (cons (car list2) out))
2899                (setq list2 (cdr list2)))))
2900       (nconc (nreverse out) (or list1 list2)))))
2901
2902 (defun gnus-intersection (list1 list2)      
2903   (let ((result nil))
2904     (while list2
2905       (if (memq (car list2) list1)
2906           (setq result (cons (car list2) result)))
2907       (setq list2 (cdr list2)))
2908     result))
2909
2910 (defun gnus-sorted-intersection (list1 list2)
2911   ;; LIST1 and LIST2 have to be sorted over <.
2912   (let (out)
2913     (while (and list1 list2)
2914       (cond ((= (car list1) (car list2))
2915              (setq out (cons (car list1) out)
2916                    list1 (cdr list1)
2917                    list2 (cdr list2)))
2918             ((< (car list1) (car list2))
2919              (setq list1 (cdr list1)))
2920             (t
2921              (setq list2 (cdr list2)))))
2922     (nreverse out)))
2923
2924 (defun gnus-set-sorted-intersection (list1 list2)
2925   ;; LIST1 and LIST2 have to be sorted over <.
2926   ;; This function modifies LIST1.
2927   (let* ((top (cons nil list1))
2928          (prev top))
2929     (while (and list1 list2)
2930       (cond ((= (car list1) (car list2))
2931              (setq prev list1
2932                    list1 (cdr list1)
2933                    list2 (cdr list2)))
2934             ((< (car list1) (car list2))
2935              (setcdr prev (cdr list1))
2936              (setq list1 (cdr list1)))
2937             (t
2938              (setq list2 (cdr list2)))))
2939     (setcdr prev nil)
2940     (cdr top)))
2941
2942 (defun gnus-compress-sequence (numbers &optional always-list)
2943   "Convert list of numbers to a list of ranges or a single range.
2944 If ALWAYS-LIST is non-nil, this function will always release a list of
2945 ranges."
2946   (let* ((first (car numbers))
2947          (last (car numbers))
2948          result)
2949     (if (null numbers)
2950         nil
2951       (if (not (listp (cdr numbers)))
2952           numbers
2953         (while numbers
2954           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2955                 ((= (1+ last) (car numbers)) ;Still in sequence
2956                  (setq last (car numbers)))
2957                 (t                      ;End of one sequence
2958                  (setq result 
2959                        (cons (if (= first last) first
2960                                (cons first last)) result))
2961                  (setq first (car numbers))
2962                  (setq last  (car numbers))))
2963           (setq numbers (cdr numbers)))
2964         (if (and (not always-list) (null result))
2965             (if (= first last) (list first) (cons first last))
2966           (nreverse (cons (if (= first last) first (cons first last))
2967                           result)))))))
2968
2969 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2970 (defun gnus-uncompress-range (ranges)
2971   "Expand a list of ranges into a list of numbers.
2972 RANGES is either a single range on the form `(num . num)' or a list of
2973 these ranges."
2974   (let (first last result)
2975     (cond 
2976      ((null ranges)
2977       nil)
2978      ((not (listp (cdr ranges)))
2979       (setq first (car ranges))
2980       (setq last (cdr ranges))
2981       (while (<= first last)
2982         (setq result (cons first result))
2983         (setq first (1+ first)))
2984       (nreverse result))
2985      (t
2986       (while ranges
2987         (if (atom (car ranges))
2988             (if (numberp (car ranges))
2989                 (setq result (cons (car ranges) result)))
2990           (setq first (car (car ranges)))
2991           (setq last  (cdr (car ranges)))
2992           (while (<= first last)
2993             (setq result (cons first result))
2994             (setq first (1+ first))))
2995         (setq ranges (cdr ranges)))
2996       (nreverse result)))))
2997
2998 (defun gnus-add-to-range (ranges list)
2999   "Return a list of ranges that has all articles from both RANGES and LIST.
3000 Note: LIST has to be sorted over `<'."
3001   (if (not ranges)
3002       (gnus-compress-sequence list t)
3003     (setq list (copy-sequence list))
3004     (or (listp (cdr ranges))
3005         (setq ranges (list ranges)))
3006     (let ((out ranges)
3007           ilist lowest highest temp)
3008       (while (and ranges list)
3009         (setq ilist list)
3010         (setq lowest (or (and (atom (car ranges)) (car ranges))
3011                          (car (car ranges))))
3012         (while (and list (cdr list) (< (car (cdr list)) lowest))
3013           (setq list (cdr list)))
3014         (if (< (car ilist) lowest)
3015             (progn
3016               (setq temp list)
3017               (setq list (cdr list))
3018               (setcdr temp nil)
3019               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3020         (setq highest (or (and (atom (car ranges)) (car ranges))
3021                           (cdr (car ranges))))
3022         (while (and list (<= (car list) highest))
3023           (setq list (cdr list)))
3024         (setq ranges (cdr ranges)))
3025       (if list
3026           (setq out (nconc (gnus-compress-sequence list t) out)))
3027       (setq out (sort out (lambda (r1 r2) 
3028                             (< (or (and (atom r1) r1) (car r1))
3029                                (or (and (atom r2) r2) (car r2))))))
3030       (setq ranges out)
3031       (while ranges
3032         (if (atom (car ranges))
3033             (if (cdr ranges)
3034                 (if (atom (car (cdr ranges)))
3035                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3036                         (progn
3037                           (setcar ranges (cons (car ranges) 
3038                                                (car (cdr ranges))))
3039                           (setcdr ranges (cdr (cdr ranges)))))
3040                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3041                       (progn
3042                         (setcar (car (cdr ranges)) (car ranges))
3043                         (setcar ranges (car (cdr ranges)))
3044                         (setcdr ranges (cdr (cdr ranges)))))))
3045           (if (cdr ranges)
3046               (if (atom (car (cdr ranges)))
3047                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3048                       (progn
3049                         (setcdr (car ranges) (car (cdr ranges)))
3050                         (setcdr ranges (cdr (cdr ranges)))))
3051                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3052                     (progn
3053                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3054                       (setcdr ranges (cdr (cdr ranges))))))))
3055         (setq ranges (cdr ranges)))
3056       out)))
3057
3058 (defun gnus-remove-from-range (ranges list)
3059   "Return a list of ranges that has all articles from LIST removed from RANGES.
3060 Note: LIST has to be sorted over `<'."
3061   ;; !!! This function shouldn't look like this, but I've got a headache.
3062   (gnus-compress-sequence 
3063    (gnus-sorted-complement
3064     (gnus-uncompress-range ranges) list)))
3065
3066 (defun gnus-member-of-range (number ranges)
3067   (if (not (listp (cdr ranges)))
3068       (and (>= number (car ranges)) 
3069            (<= number (cdr ranges)))
3070     (let ((not-stop t))
3071       (while (and ranges 
3072                   (if (numberp (car ranges))
3073                       (>= number (car ranges))
3074                     (>= number (car (car ranges))))
3075                   not-stop)
3076         (if (if (numberp (car ranges))
3077                 (= number (car ranges))
3078               (and (>= number (car (car ranges)))
3079                    (<= number (cdr (car ranges)))))
3080             (setq not-stop nil))
3081         (setq ranges (cdr ranges)))
3082       (not not-stop))))
3083
3084 \f
3085 ;;;
3086 ;;; Gnus group mode
3087 ;;;
3088
3089 (defvar gnus-group-mode-map nil)
3090 (defvar gnus-group-group-map nil)
3091 (defvar gnus-group-mark-map nil)
3092 (defvar gnus-group-list-map nil)
3093 (defvar gnus-group-help-map nil)
3094 (defvar gnus-group-sub-map nil)
3095 (put 'gnus-group-mode 'mode-class 'special)
3096
3097 (if gnus-group-mode-map
3098     nil
3099   (setq gnus-group-mode-map (make-keymap))
3100   (suppress-keymap gnus-group-mode-map)
3101   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3102   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3103   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3104   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3105   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3106   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3107   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3108   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3109   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3110   (define-key gnus-group-mode-map
3111     "\M-n" 'gnus-group-next-unread-group-same-level)
3112   (define-key gnus-group-mode-map 
3113     "\M-p" 'gnus-group-prev-unread-group-same-level)
3114   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3115   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3116   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3117   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3118   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3119   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3120   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3121   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3122   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3123   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3124   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3125   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3126   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3127   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3128   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3129   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3130   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3131   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3132   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3133   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3134   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3135   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3136   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3137   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3138   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3139   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3140   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3141   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3142   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3143   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3144   (define-key gnus-group-mode-map "V" 'gnus-version)
3145   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3146   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3147   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3148   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3149   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3150   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3151   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3152   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3153   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3154   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3155   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3156   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3157   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3158   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3159
3160   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3161   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3162   (define-prefix-command 'gnus-group-mark-map)
3163   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3164   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3165   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3166   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3167
3168   (define-prefix-command 'gnus-group-group-map)
3169   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3170   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3171   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3172   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3173   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3174   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3175   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3176   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3177   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3178   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3179   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3180   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3181   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3182   ;;(define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3183   ;;(define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3184   ;;(define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3185   ;;(define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3186   ;;(define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3187
3188   (define-prefix-command 'gnus-group-list-map)
3189   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3190   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3191   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3192   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3193   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3194   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3195   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3196   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3197   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3198
3199   (define-prefix-command 'gnus-group-help-map)
3200   (define-key gnus-group-mode-map "H" 'gnus-group-help-map)
3201   (define-key gnus-group-help-map "f" 'gnus-group-fetch-faq)
3202
3203   (define-prefix-command 'gnus-group-sub-map)
3204   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3205   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3206   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3207   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3208   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3209   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3210   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3211   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3212
3213 (defun gnus-group-mode ()
3214   "Major mode for reading news.
3215
3216 All normal editing commands are switched off.
3217 \\<gnus-group-mode-map>
3218 The group buffer lists (some of) the groups available.  For instance,
3219 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3220 lists all zombie groups. 
3221
3222 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3223 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3224
3225 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3226
3227 The following commands are available:
3228
3229 \\{gnus-group-mode-map}"
3230   (interactive)
3231   (if gnus-visual (gnus-group-make-menu-bar))
3232   (kill-all-local-variables)
3233   (gnus-simplify-mode-line)
3234   (setq major-mode 'gnus-group-mode)
3235   (setq mode-name "Group")
3236   (gnus-group-set-mode-line)
3237   (setq mode-line-process nil)
3238   (use-local-map gnus-group-mode-map)
3239   (buffer-disable-undo (current-buffer))
3240   (setq truncate-lines t)
3241   (setq buffer-read-only t)
3242   (run-hooks 'gnus-group-mode-hook))
3243
3244 (defun gnus-mouse-pick-group (e)
3245   (interactive "e")
3246   (mouse-set-point e)
3247   (gnus-group-read-group nil))
3248
3249 ;; Look at LEVEL and find out what the level is really supposed to be.
3250 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3251 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3252 (defun gnus-group-default-level (&optional level number-or-nil)
3253   (cond  
3254    (gnus-group-use-permanent-levels
3255     (setq gnus-group-default-list-level 
3256           (or level gnus-group-default-list-level))
3257     (or gnus-group-default-list-level gnus-level-subscribed))
3258    (number-or-nil
3259     level)
3260    (t
3261     (or level gnus-group-default-list-level gnus-level-subscribed))))
3262   
3263
3264 (defvar gnus-tmp-prev-perm nil)
3265
3266 ;;;###autoload
3267 (defun gnus-no-server (&optional arg)
3268   "Read network news.
3269 If ARG is a positive number, Gnus will use that as the
3270 startup level. If ARG is nil, Gnus will be started at level 2. 
3271 If ARG is non-nil and not a positive number, Gnus will
3272 prompt the user for the name of an NNTP server to use.
3273 As opposed to `gnus', this command will not connect to the local server."
3274   (interactive "P")
3275   (let ((perm
3276          (cons gnus-group-use-permanent-levels gnus-group-default-list-level)))
3277     (setq gnus-tmp-prev-perm nil)
3278     (setq gnus-group-use-permanent-levels t)
3279     (gnus (or arg (1- gnus-level-default-subscribed)) t)
3280     (setq gnus-tmp-prev-perm perm)))
3281
3282 ;;;###autoload
3283 (defun gnus (&optional arg dont-connect)
3284   "Read network news.
3285 If ARG is non-nil and a positive number, Gnus will use that as the
3286 startup level. If ARG is non-nil and not a positive number, Gnus will
3287 prompt the user for the name of an NNTP server to use."
3288   (interactive "P")
3289   (if (get-buffer gnus-group-buffer)
3290       (progn
3291         (switch-to-buffer gnus-group-buffer)
3292         (gnus-group-get-new-news))
3293
3294     (gnus-clear-system)
3295
3296     (nnheader-init-server-buffer)
3297     ;; We do this if `gnus-no-server' has been run.
3298     (if gnus-tmp-prev-perm 
3299         (setq gnus-group-use-permanent-levels (car gnus-tmp-prev-perm)
3300               gnus-group-default-list-level (cdr gnus-tmp-prev-perm)
3301               gnus-tmp-prev-perm nil))
3302     (gnus-read-init-file)
3303
3304     (gnus-group-setup-buffer)
3305     (let ((buffer-read-only nil))
3306       (erase-buffer)
3307       (if (not gnus-inhibit-startup-message)
3308           (progn
3309             (gnus-group-startup-message)
3310             (sit-for 0))))
3311     
3312     (let ((level (and arg (numberp arg) (> arg 0) arg))
3313           did-connect)
3314       (unwind-protect
3315           (progn
3316             (or dont-connect 
3317                 (setq did-connect
3318                       (gnus-start-news-server (and arg (not level))))))
3319         (if (and (not dont-connect) 
3320                  (not did-connect))
3321             (gnus-group-quit)
3322           (run-hooks 'gnus-startup-hook)
3323           ;; NNTP server is successfully open. 
3324
3325           ;; Find the current startup file name.
3326           (setq gnus-current-startup-file 
3327                 (gnus-make-newsrc-file gnus-startup-file))
3328
3329           ;; Read the dribble file.
3330           (and gnus-use-dribble-file (gnus-dribble-read-file))
3331
3332           (gnus-summary-make-display-table)
3333           (gnus-setup-news nil level)
3334           (gnus-group-list-groups level)
3335           (gnus-configure-windows 'group))))))
3336
3337 (defun gnus-unload ()
3338   "Unload all Gnus features."
3339   (interactive)
3340   (or (boundp 'load-history)
3341       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
3342   (let ((history load-history)
3343         feature)
3344     (while history
3345       (and (string-match "^gnus" (car (car history)))
3346            (setq feature (cdr (assq 'provide (car history))))
3347            (unload-feature feature 'force))
3348       (setq history (cdr history)))))
3349
3350 (defun gnus-group-startup-message (&optional x y)
3351   "Insert startup message in current buffer."
3352   ;; Insert the message.
3353   (erase-buffer)
3354   (insert
3355    (format "
3356           _    ___ _             _      
3357           _ ___ __ ___  __    _ ___     
3358           __   _     ___    __  ___     
3359               _           ___     _     
3360              _  _ __             _      
3361              ___   __            _      
3362                    __           _       
3363                     _      _   _        
3364                    _      _    _        
3365                       _  _    _         
3366                   __  ___               
3367                  _   _ _     _          
3368                 _   _                   
3369               _    _                    
3370              _    _                     
3371             _                         
3372           __                             
3373
3374
3375       Gnus * A newsreader for Emacsen
3376     A Praxis release * larsi@ifi.uio.no
3377
3378            gnus-version))
3379   ;; And then hack it.
3380   ;; 18 is the longest line.
3381   (indent-rigidly (point-min) (point-max) 
3382                   (/ (max (- (window-width) (or x 46)) 0) 2))
3383   (goto-char (point-min))
3384   (let* ((pheight (count-lines (point-min) (point-max)))
3385          (wheight (window-height))
3386          (rest (- wheight  pheight)))
3387     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
3388     
3389     
3390
3391   ;; Fontify some.
3392   (goto-char (point-min))
3393   (search-forward "Praxis")
3394   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3395   (goto-char (point-min)))
3396
3397 (defun gnus-group-startup-message-old (&optional x y)
3398   "Insert startup message in current buffer."
3399   ;; Insert the message.
3400   (erase-buffer)
3401   (insert
3402    (format "
3403      %s
3404            A newsreader 
3405       for GNU Emacs
3406
3407         Based on GNUS 
3408              written by 
3409      Masanobu UMEDA
3410
3411        A Praxis Release
3412       larsi@ifi.uio.no
3413
3414            gnus-version))
3415   ;; And then hack it.
3416   ;; 18 is the longest line.
3417   (indent-rigidly (point-min) (point-max) 
3418                   (/ (max (- (window-width) (or x 28)) 0) 2))
3419   (goto-char (point-min))
3420   ;; +4 is fuzzy factor.
3421   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3422
3423   ;; Fontify some.
3424   (goto-char (point-min))
3425   (search-forward "Praxis")
3426   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3427   (goto-char (point-min)))
3428
3429 (defun gnus-group-setup-buffer ()
3430   (or (get-buffer gnus-group-buffer)
3431       (progn
3432         (switch-to-buffer gnus-group-buffer)
3433         (gnus-add-current-to-buffer-list)
3434         (gnus-group-mode)
3435         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3436
3437 (defun gnus-group-list-groups (&optional level unread)
3438   "List newsgroups with level LEVEL or lower that have unread articles.
3439 Default is all subscribed groups.
3440 If argument UNREAD is non-nil, groups with no unread articles are also
3441 listed." 
3442   (interactive (list (if current-prefix-arg
3443                          (prefix-numeric-value current-prefix-arg)
3444                        (or
3445                         (gnus-group-default-level nil t)
3446                         gnus-group-default-list-level
3447                         gnus-level-subscribed))))
3448   (or level
3449       (setq level (car gnus-group-list-mode)
3450             unread (cdr gnus-group-list-mode)))
3451   (setq level (gnus-group-default-level level))
3452   (gnus-group-setup-buffer)             ;May call from out of group buffer
3453   (let ((case-fold-search nil)
3454         (group (gnus-group-group-name)))
3455     (funcall gnus-group-prepare-function level unread nil)
3456     (if (zerop (buffer-size))
3457         (gnus-message 5 gnus-no-groups-message)
3458       (goto-char (point-min))
3459       (if (not group)
3460           ;; Go to the first group with unread articles.
3461           (gnus-group-search-forward nil nil nil t)
3462         ;; Find the right group to put point on. If the current group
3463         ;; has disapeared in the new listing, try to find the next
3464         ;; one. If no next one can be found, just leave point at the
3465         ;; first newsgroup in the buffer.
3466         (if (not (gnus-goto-char
3467                   (text-property-any (point-min) (point-max) 
3468                                      'gnus-group (intern group))))
3469             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3470               (while (and newsrc
3471                           (not (gnus-goto-char 
3472                                 (text-property-any 
3473                                  (point-min) (point-max) 'gnus-group 
3474                                  (intern (car (car newsrc)))))))
3475                 (setq newsrc (cdr newsrc)))
3476               (or newsrc (progn (goto-char (point-max))
3477                                 (forward-line -1))))))
3478       ;; Adjust cursor point.
3479       (gnus-group-position-cursor))))
3480
3481 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3482   "List all newsgroups with unread articles of level LEVEL or lower.
3483 If ALL is non-nil, list groups that have no unread articles.
3484 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3485 If REGEXP, only list groups matching REGEXP."
3486   (set-buffer gnus-group-buffer)
3487   (let ((buffer-read-only nil)
3488         (newsrc (cdr gnus-newsrc-alist))
3489         (lowest (or lowest 1))
3490         info clevel unread group)
3491     (erase-buffer)
3492     (if (< lowest gnus-level-zombie)
3493         ;; List living groups.
3494         (while newsrc
3495           (setq info (car newsrc)
3496                 group (car info)
3497                 newsrc (cdr newsrc)
3498                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3499           (and unread                   ; This group might be bogus
3500                (or (not regexp)
3501                    (string-match regexp group))
3502                (<= (setq clevel (car (cdr info))) level) 
3503                (>= clevel lowest)
3504                (or all                  ; We list all groups?
3505                    (eq unread t)        ; We list unactivated groups
3506                    (> unread 0)         ; We list groups with unread articles
3507                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3508                (gnus-group-insert-group-line 
3509                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3510
3511     ;; List dead groups.
3512     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3513          (gnus-group-prepare-flat-list-dead 
3514           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3515           gnus-level-zombie ?Z
3516           regexp))
3517     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3518          (gnus-group-prepare-flat-list-dead 
3519           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3520           gnus-level-killed ?K regexp))
3521
3522     (gnus-group-set-mode-line)
3523     (setq gnus-group-list-mode (cons level all))
3524     (run-hooks 'gnus-group-prepare-hook)))
3525
3526 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3527   ;; List zombies and killed lists somehwat faster, which was
3528   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3529   ;; this by ignoring the group format specification altogether.
3530   (let (group beg)
3531     (while groups
3532       (setq group (car groups)
3533             groups (cdr groups))
3534       (if (or (not regexp)
3535               (string-match regexp group))
3536           (progn
3537             (setq beg (point))
3538             (insert (format " %c     *: %s\n" mark group))
3539             (add-text-properties 
3540              beg (1+ beg) 
3541              (list 'gnus-group (intern group)
3542                    'gnus-unread t
3543                    'gnus-level level)))))))
3544
3545 (defun gnus-group-real-name (group)
3546   "Find the real name of a foreign newsgroup."
3547   (if (string-match ":[^:]+$" group)
3548       (substring group (1+ (match-beginning 0)))
3549     group))
3550
3551 (defun gnus-group-prefixed-name (group method)
3552   "Return the whole name from GROUP and METHOD."
3553   (and (stringp method) (setq method (gnus-server-to-method method)))
3554   (concat (format "%s" (car method))
3555           (if (and 
3556                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3557                (not (string= (nth 1 method) "")))
3558               (concat "+" (nth 1 method)))
3559           ":" group))
3560
3561 (defun gnus-group-real-prefix (group)
3562   "Return the prefix of the current group name."
3563   (if (string-match "^[^:]+:" group)
3564       (substring group 0 (match-end 0))
3565     ""))
3566
3567 (defun gnus-group-method-name (group)
3568   "Return the method used for selecting GROUP."
3569   (let ((prefix (gnus-group-real-prefix group)))
3570     (if (equal prefix "")
3571         gnus-select-method
3572       (if (string-match "^[^\\+]+\\+" prefix)
3573           (list (intern (substring prefix 0 (1- (match-end 0))))
3574                 (substring prefix (match-end 0) (1- (length prefix))))
3575         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3576
3577 (defun gnus-group-foreign-p (group)
3578   "Return nil if GROUP is native, non-nil if it is foreign."
3579   (string-match ":" group))
3580
3581 (defun gnus-group-set-info (info &optional method-only-group part)
3582   (let* ((entry (gnus-gethash
3583                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3584          (part-info info)
3585          (info (if method-only-group (nth 2 entry) info)))
3586     (if (not method-only-group)
3587         ()
3588       (or entry
3589           (error "Trying to change non-existent group %s" method-only-group))
3590       ;; We have recevied parts of the actual group info - either the
3591       ;; select method or the group parameters.  We first check
3592       ;; whether we have to extend the info, and if so, do that.
3593       (let ((len (length info))
3594             (total (if (eq part 'method) 5 6)))
3595         (and (< len total)
3596              (setcdr (nthcdr (1- len) info)
3597                      (make-list (- total len) nil)))
3598         ;; Then we enter the new info.
3599         (setcar (nthcdr (1- total) info) part-info)))
3600     ;; We uncompress some lists of marked articles.
3601     (let (marked)
3602       (if (not (setq marked (nth 3 info)))
3603           ()
3604         (while marked
3605           (or (eq 'score (car (car marked)))
3606               (eq 'bookmark (car (car marked)))
3607               (eq 'killed (car (car marked)))
3608               (setcdr (car marked) 
3609                       (gnus-uncompress-range (cdr (car marked)))))
3610           (setq marked (cdr marked)))))
3611     (if entry
3612         ()
3613       ;; This is a new group, so we just create it.
3614       (save-excursion
3615         (set-buffer gnus-group-buffer)
3616         (if (nth 4 info)
3617             ;; It's a foreign group...
3618             (gnus-group-make-group 
3619              (gnus-group-real-name (car info))
3620              (prin1-to-string (car (nth 4 info)))
3621              (nth 1 (nth 4 info)))
3622           ;; It's a native group.
3623           (gnus-group-make-group (car info)))
3624         (gnus-message 6 "Note: New group created")
3625         (setq entry 
3626               (gnus-gethash (gnus-group-prefixed-name 
3627                              (gnus-group-real-name (car info))
3628                              (or (nth 4 info) gnus-select-method))
3629                             gnus-newsrc-hashtb))))
3630     ;; Whether it was a new group or not, we now have the entry, so we
3631     ;; can do the update.
3632     (if entry
3633         (progn
3634           (setcar (nthcdr 2 entry) info)
3635           (if (and (not (eq (car entry) t)) 
3636                    (gnus-gethash (car info) gnus-active-hashtb))
3637               (let ((marked (nth 3 info)))
3638                 (setcar entry 
3639                         (max 0 (- (length (gnus-list-of-unread-articles 
3640                                            (car info)))
3641                                   (length (cdr (assq 'tick marked)))
3642                                   (length (cdr (assq 'dormant marked)))))))))
3643       (error "No such group: %s" (car info)))))
3644
3645 (defun gnus-group-set-method-info (group select-method)
3646   (gnus-group-set-info select-method group 'method))
3647
3648 (defun gnus-group-set-params-info (group params)
3649   (gnus-group-set-info params group 'params))
3650
3651 (defun gnus-group-update-group-line ()
3652   "This function updates the current line in the newsgroup buffer and
3653 moves the point to the colon."
3654   (let* ((buffer-read-only nil)
3655          (group (gnus-group-group-name))
3656          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3657     (if (and entry (not (gnus-ephemeral-group-p group)))
3658         (gnus-dribble-enter 
3659          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3660                  ")")))
3661     (beginning-of-line)
3662     (delete-region (point) (progn (forward-line 1) (point)))
3663     (gnus-group-insert-group-line-info group)
3664     (forward-line -1)
3665     (gnus-group-position-cursor)))
3666
3667 (defun gnus-group-insert-group-line-info (group)
3668   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3669         active info)
3670     (if entry
3671         (progn
3672           (setq info (nth 2 entry))
3673           (gnus-group-insert-group-line 
3674            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3675       (setq active (gnus-gethash group gnus-active-hashtb))
3676       (gnus-group-insert-group-line 
3677        nil group 
3678        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3679        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3680
3681 (defun gnus-group-insert-group-line (gformat group level marked number method)
3682   (let* ((gformat (or gformat gnus-group-line-format-spec))
3683          (active (gnus-gethash group gnus-active-hashtb))
3684          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3685          (number-of-dormant (length (cdr (assq 'dormant marked))))
3686          (number-of-ticked (length (cdr (assq 'tick marked))))
3687          (number-of-ticked-and-dormant
3688           (+ number-of-ticked number-of-dormant))
3689          (number-of-unread-unticked 
3690           (if (numberp number) (int-to-string (max 0 number))
3691             "*"))
3692          (number-of-read
3693           (if (numberp number)
3694               (max 0 (- number-total number))
3695             "*"))
3696          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3697                            ((<= level gnus-level-unsubscribed) ?U)
3698                            ((= level gnus-level-zombie) ?Z)
3699                            (t ?K)))
3700          (qualified-group (gnus-group-real-name group))
3701          (newsgroup-description 
3702           (if gnus-description-hashtb
3703               (or (gnus-gethash group gnus-description-hashtb) "")
3704             ""))
3705          (moderated (if (member group gnus-moderated-list) ?m ? ))
3706          (moderated-string (if (eq moderated ?m) "(m)" ""))
3707          (method (gnus-server-get-method group method))
3708          (news-server (or (car (cdr method)) ""))
3709          (news-method (or (car method) ""))
3710          (news-method-string 
3711           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3712          (marked (if (and 
3713                       (numberp number) 
3714                       (zerop number)
3715                       (> number-of-ticked 0))
3716                      ?* ? ))
3717          (number (if (eq number t) "*" (+ number number-of-dormant 
3718                                           number-of-ticked)))
3719          (process-marked (if (member group gnus-group-marked)
3720                              gnus-process-mark ? ))
3721          (buffer-read-only nil)
3722          header                         ; passed as parameter to user-funcs.
3723          b)
3724     (beginning-of-line)
3725     (setq b (point))
3726     ;; Insert the text.
3727     (insert (eval gformat))
3728
3729     (add-text-properties 
3730      b (1+ b) (list 'gnus-group (intern group)
3731                     'gnus-unread (if (numberp number)
3732                                      (string-to-int number-of-unread-unticked)
3733                                    t)
3734                     'gnus-marked marked
3735                     'gnus-level level))))
3736
3737 (defun gnus-group-update-group (group &optional visible-only)
3738   "Update newsgroup info of GROUP.
3739 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3740   (save-excursion
3741     (set-buffer gnus-group-buffer)
3742     (let ((buffer-read-only nil)
3743           visible)
3744       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3745         (if (and entry
3746                  (not (gnus-ephemeral-group-p group)))
3747             (gnus-dribble-enter 
3748              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3749                      ")"))))
3750       ;; Buffer may be narrowed.
3751       (save-restriction
3752         (widen)
3753         ;; Search a line to modify.  If the buffer is large, the search
3754         ;; takes long time.  In most cases, current point is on the line
3755         ;; we are looking for.  So, first of all, check current line. 
3756         (if (or (progn
3757                   (beginning-of-line)
3758                   (eq (get-text-property (point) 'gnus-group)
3759                       (intern group)))
3760                 (progn
3761                   (gnus-goto-char 
3762                    (text-property-any 
3763                     (point-min) (point-max) 'gnus-group (intern group)))))
3764             ;; GROUP is listed in current buffer. So, delete old line.
3765             (progn
3766               (setq visible t)
3767               (beginning-of-line)
3768               (delete-region (point) (progn (forward-line 1) (point))))
3769           ;; No such line in the buffer, find out where it's supposed to
3770           ;; go, and insert it there (or at the end of the buffer).
3771           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3772           (or visible-only
3773               (let ((entry 
3774                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3775                 (while (and entry
3776                             (car entry)
3777                             (not
3778                              (gnus-goto-char
3779                               (text-property-any
3780                                (point-min) (point-max) 
3781                                'gnus-group (intern (car (car entry)))))))
3782                   (setq entry (cdr entry)))
3783                 (or entry (goto-char (point-max)))))))
3784       (if (or visible (not visible-only))
3785           (gnus-group-insert-group-line-info group))
3786       (gnus-group-set-mode-line))))
3787
3788 (defun gnus-group-set-mode-line ()
3789   (if (memq 'group gnus-updated-mode-lines)
3790       (let* ((gformat (or gnus-group-mode-line-format-spec
3791                           (setq gnus-group-mode-line-format-spec
3792                                 (gnus-parse-format 
3793                                  gnus-group-mode-line-format 
3794                                  gnus-group-mode-line-format-alist))))
3795              (news-server (car (cdr gnus-select-method)))
3796              (news-method (car gnus-select-method))
3797              (max-len 60)
3798              (mode-string (eval gformat)))
3799         (setq mode-string (eval gformat))
3800         (if (> (length mode-string) max-len) 
3801             (setq mode-string (substring mode-string 0 (- max-len 4))))
3802         (setq mode-line-buffer-identification mode-string)
3803         (set-buffer-modified-p t))))
3804
3805 (defun gnus-group-group-name ()
3806   "Get the name of the newsgroup on the current line."
3807   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3808     (and group (symbol-name group))))
3809
3810 (defun gnus-group-group-level ()
3811   "Get the level of the newsgroup on the current line."
3812   (get-text-property (gnus-point-at-bol) 'gnus-level))
3813
3814 (defun gnus-group-group-unread ()
3815   "Get the number of unread articles of the newsgroup on the current line."
3816   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3817
3818 (defun gnus-group-search-forward (&optional backward all level first-too)
3819   "Find the next newsgroup with unread articles.
3820 If BACKWARD is non-nil, find the previous newsgroup instead.
3821 If ALL is non-nil, just find any newsgroup.
3822 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3823 group exists.
3824 If FIRST-TOO, the current line is also eligible as a target."
3825   (let ((way (if backward -1 1))
3826         (low gnus-level-killed)
3827         (beg (point))
3828         pos found lev)
3829     (if (and backward (progn (beginning-of-line)) (bobp))
3830         nil
3831       (or first-too (forward-line way))
3832       (while (and 
3833               (not (eobp))
3834               (not (setq 
3835                     found 
3836                     (and (or all
3837                              (and
3838                               (let ((unread 
3839                                      (get-text-property (point) 'gnus-unread)))
3840                                 (or (eq unread t) (and unread (> unread 0))))
3841                               (setq lev (get-text-property (point)
3842                                                            'gnus-level))
3843                               (<= lev gnus-level-subscribed)))
3844                          (or (not level)
3845                              (and (setq lev (get-text-property (point)
3846                                                                'gnus-level))
3847                                   (or (= lev level)
3848                                       (and (< lev low)
3849                                            (< level lev)
3850                                            (progn
3851                                              (setq low lev)
3852                                              (setq pos (point))
3853                                              nil))))))))
3854               (zerop (forward-line way)))))
3855     (if found 
3856         (progn (gnus-group-position-cursor) t)
3857       (goto-char (or pos beg))
3858       (and pos t))))
3859
3860 ;;; Gnus group mode commands
3861
3862 ;; Group marking.
3863
3864 (defun gnus-group-mark-group (n &optional unmark no-advance)
3865   "Mark the current group."
3866   (interactive "p")
3867   (let ((buffer-read-only nil)
3868         group)
3869     (while 
3870         (and (> n 0) 
3871              (setq group (gnus-group-group-name))
3872              (progn
3873                (beginning-of-line)
3874                (forward-char 
3875                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3876                (delete-char 1)
3877                (if unmark
3878                    (progn
3879                      (insert " ")
3880                      (setq gnus-group-marked (delete group gnus-group-marked)))
3881                  (insert "#")
3882                  (setq gnus-group-marked
3883                        (cons group (delete group gnus-group-marked))))
3884                t)
3885              (or no-advance (zerop (gnus-group-next-group 1))))
3886       (setq n (1- n)))
3887     (gnus-summary-position-cursor)
3888     n))
3889
3890 (defun gnus-group-unmark-group (n)
3891   "Remove the mark from the current group."
3892   (interactive "p")
3893   (gnus-group-mark-group n 'unmark))
3894
3895 (defun gnus-group-mark-region (unmark beg end)
3896   "Mark all groups between point and mark.
3897 If UNMARK, remove the mark instead."
3898   (interactive "P\nr")
3899   (let ((num (count-lines beg end)))
3900     (save-excursion
3901       (goto-char beg)
3902       (- num (gnus-group-mark-group num unmark)))))
3903
3904 (defun gnus-group-remove-mark (group)
3905   (and (gnus-group-goto-group group)
3906        (save-excursion
3907          (gnus-group-mark-group 1 'unmark t))))
3908
3909 ;; Return a list of groups to work on.  Take into consideration N (the
3910 ;; prefix) and the list of marked groups.
3911 (defun gnus-group-process-prefix (n)
3912   (cond (n
3913          (setq n (prefix-numeric-value n))
3914          ;; There is a prefix, so we return a list of the N next
3915          ;; groups. 
3916          (let ((way (if (< n 0) -1 1))
3917                (n (abs n))
3918                group groups)
3919            (save-excursion
3920              (while (and (> n 0)
3921                          (setq group (gnus-group-group-name)))
3922                (setq groups (cons group groups))
3923                (setq n (1- n))
3924                (forward-line way)))
3925            (nreverse groups)))
3926         (gnus-group-marked
3927          ;; No prefix, but a list of marked articles.
3928          (reverse gnus-group-marked))
3929         (t
3930          ;; Neither marked articles or a prefix, so we return the
3931          ;; current group.
3932          (let ((group (gnus-group-group-name)))
3933            (and group (list group))))))
3934
3935 ;; Selecting groups.
3936
3937 (defun gnus-group-read-group (&optional all no-article group)
3938   "Read news in this newsgroup.
3939 If the prefix argument ALL is non-nil, already read articles become
3940 readable. If the optional argument NO-ARTICLE is non-nil, no article
3941 will be auto-selected upon group entry."
3942   (interactive "P")
3943   (let ((group (or group (gnus-group-group-name)))
3944         number active marked entry)
3945     (or group (error "No group on current line"))
3946     (setq marked 
3947           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3948     ;; This group might be a dead group. In that case we have to get
3949     ;; the number of unread articles from `gnus-active-hashtb'.
3950     (if entry
3951         (setq number (car entry))
3952       (if (setq active (gnus-gethash group gnus-active-hashtb))
3953           (setq number (- (1+ (cdr active)) (car active)))))
3954     (gnus-summary-read-group 
3955      group (or all (and (numberp number) 
3956                         (zerop (+ number (length (cdr (assq 'tick marked)))
3957                                   (length (cdr (assq 'dormant marked)))))))
3958      no-article)))
3959
3960 (defun gnus-group-select-group (&optional all)
3961   "Select this newsgroup.
3962 No article is selected automatically.
3963 If argument ALL is non-nil, already read articles become readable."
3964   (interactive "P")
3965   (gnus-group-read-group all t))
3966
3967 (defun gnus-group-select-group-all ()
3968   "Select the current group and display all articles in it."
3969   (interactive)
3970   (gnus-group-select-group 'all))
3971
3972 ;; Enter a group that is not in the group buffer. Non-nil is returned
3973 ;; if selection was successful.
3974 (defun gnus-group-read-ephemeral-group 
3975   (group method &optional activate quit-config)
3976   (let ((group (if (gnus-group-foreign-p group) group
3977                  (gnus-group-prefixed-name group method))))
3978     (gnus-sethash 
3979      group
3980      (list t nil (list group gnus-level-default-subscribed nil nil 
3981                        (append method
3982                                (list
3983                                 (list 'quit-config 
3984                                       (if quit-config quit-config
3985                                         (cons (current-buffer) 'summary)))))))
3986      gnus-newsrc-hashtb)
3987     (set-buffer gnus-group-buffer)
3988     (or (gnus-check-server method)
3989         (error "Unable to contact server: %s" (gnus-status-message method)))
3990     (if activate (or (gnus-request-group group)
3991                      (error "Couldn't request group")))
3992     (condition-case ()
3993         (gnus-group-read-group t t group)
3994       (error nil)
3995       (quit nil))
3996     (not (equal major-mode 'gnus-group-mode))))
3997   
3998 (defun gnus-group-jump-to-group (group)
3999   "Jump to newsgroup GROUP."
4000   (interactive 
4001    (list (completing-read 
4002           "Group: " gnus-active-hashtb nil 
4003           (memq gnus-select-method gnus-have-read-active-file))))
4004
4005   (if (equal group "")
4006       (error "Empty group name"))
4007
4008   (let ((b (text-property-any 
4009             (point-min) (point-max) 'gnus-group (intern group))))
4010     (if b
4011         ;; Either go to the line in the group buffer...
4012         (goto-char b)
4013       ;; ... or insert the line.
4014       (or
4015        (gnus-gethash group gnus-active-hashtb)
4016        (gnus-activate-group group)
4017        (error "%s error: %s" group (gnus-status-message group)))
4018
4019       (gnus-group-update-group group)
4020       (goto-char (text-property-any 
4021                   (point-min) (point-max) 'gnus-group (intern group)))))
4022   ;; Adjust cursor point.
4023   (gnus-group-position-cursor))
4024
4025 (defun gnus-group-goto-group (group)
4026   "Goto to newsgroup GROUP."
4027   (let ((b (text-property-any (point-min) (point-max) 
4028                               'gnus-group (intern group))))
4029     (and b (goto-char b))))
4030
4031 (defun gnus-group-next-group (n)
4032   "Go to next N'th newsgroup.
4033 If N is negative, search backward instead.
4034 Returns the difference between N and the number of skips actually
4035 done."
4036   (interactive "p")
4037   (gnus-group-next-unread-group n t))
4038
4039 (defun gnus-group-next-unread-group (n &optional all level)
4040   "Go to next N'th unread newsgroup.
4041 If N is negative, search backward instead.
4042 If ALL is non-nil, choose any newsgroup, unread or not.
4043 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4044 such group can be found, the next group with a level higher than
4045 LEVEL.
4046 Returns the difference between N and the number of skips actually
4047 made."
4048   (interactive "p")
4049   (let ((backward (< n 0))
4050         (n (abs n)))
4051     (while (and (> n 0)
4052                 (gnus-group-search-forward 
4053                  backward (or (not gnus-group-goto-unread) all) level))
4054       (setq n (1- n)))
4055     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4056                                (if level " on this level or higher" "")))
4057     n))
4058
4059 (defun gnus-group-prev-group (n)
4060   "Go to previous N'th newsgroup.
4061 Returns the difference between N and the number of skips actually
4062 done."
4063   (interactive "p")
4064   (gnus-group-next-unread-group (- n) t))
4065
4066 (defun gnus-group-prev-unread-group (n)
4067   "Go to previous N'th unread newsgroup.
4068 Returns the difference between N and the number of skips actually
4069 done."  
4070   (interactive "p")
4071   (gnus-group-next-unread-group (- n)))
4072
4073 (defun gnus-group-next-unread-group-same-level (n)
4074   "Go to next N'th unread newsgroup on the same level.
4075 If N is negative, search backward instead.
4076 Returns the difference between N and the number of skips actually
4077 done."
4078   (interactive "p")
4079   (gnus-group-next-unread-group n t (gnus-group-group-level))
4080   (gnus-group-position-cursor))
4081
4082 (defun gnus-group-prev-unread-group-same-level (n)
4083   "Go to next N'th unread newsgroup on the same level.
4084 Returns the difference between N and the number of skips actually
4085 done."
4086   (interactive "p")
4087   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
4088   (gnus-group-position-cursor))
4089
4090 (defun gnus-group-best-unread-group (&optional exclude-group)
4091   "Go to the group with the highest level.
4092 If EXCLUDE-GROUP, do not go to that group."
4093   (interactive)
4094   (goto-char (point-min))
4095   (let ((best 100000)
4096         unread best-point)
4097     (while (setq unread (get-text-property (point) 'gnus-unread))
4098       (if (and (numberp unread) (> unread 0))
4099           (progn
4100             (if (and (< (get-text-property (point) 'gnus-level) best)
4101                      (or (not exclude-group)
4102                          (not (equal exclude-group (gnus-group-group-name)))))
4103                 (progn 
4104                   (setq best (get-text-property (point) 'gnus-level))
4105                   (setq best-point (point))))))
4106       (forward-line 1))
4107     (if best-point (goto-char best-point))
4108     (gnus-summary-position-cursor)
4109     (and best-point (gnus-group-group-name))))
4110
4111 (defun gnus-group-first-unread-group ()
4112   "Go to the first group with unread articles."
4113   (interactive)
4114   (prog1
4115       (let ((opoint (point))
4116             unread)
4117         (goto-char (point-min))
4118         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4119                 (not (zerop unread))    ; Has unread articles.
4120                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4121             (point)                     ; Success.
4122           (goto-char opoint)
4123           nil))                         ; Not success.
4124     (gnus-group-position-cursor)))
4125
4126 (defun gnus-group-enter-server-mode ()
4127   "Jump to the server buffer."
4128   (interactive)
4129   (gnus-server-setup-buffer)
4130   (gnus-configure-windows 'server)
4131   (gnus-server-prepare))
4132
4133 (defun gnus-group-make-group (name &optional method address)
4134   "Add a new newsgroup.
4135 The user will be prompted for a NAME, for a select METHOD, and an
4136 ADDRESS."
4137   (interactive
4138    (cons 
4139     (read-string "Group name: ")
4140     (let ((method
4141            (completing-read 
4142             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4143             nil t)))
4144       (if (assoc method gnus-valid-select-methods)
4145           (list method
4146                 (if (memq 'prompt-address
4147                           (assoc method gnus-valid-select-methods))
4148                     (read-string "Address: ")
4149                   ""))
4150         (list method nil)))))
4151   
4152   (let* ((meth (and method (if address (list (intern method) address) method)))
4153          (nname (if method (gnus-group-prefixed-name name meth) name))
4154          info)
4155     (and (gnus-gethash nname gnus-newsrc-hashtb)
4156          (error "Group %s already exists" nname))
4157     (gnus-group-change-level 
4158      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4159      gnus-level-default-subscribed gnus-level-killed 
4160      (and (gnus-group-group-name)
4161           (gnus-gethash (gnus-group-group-name)
4162                         gnus-newsrc-hashtb))
4163      t)
4164     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4165     (or (gnus-ephemeral-group-p name)
4166         (gnus-dribble-enter 
4167          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
4168     (gnus-group-insert-group-line-info nname)
4169
4170     (if (assoc method gnus-valid-select-methods)
4171         (require (intern method)))
4172     (and (gnus-check-backend-function 'request-create-group nname)
4173          (gnus-request-create-group nname))))
4174
4175 (defun gnus-group-edit-group (group &optional part)
4176   "Edit the group on the current line."
4177   (interactive (list (gnus-group-group-name)))
4178   (let ((done-func '(lambda () 
4179                       "Exit editing mode and update the information."
4180                       (interactive)
4181                       (gnus-group-edit-group-done 'part 'group)))
4182         (part (or part 'info))
4183         (winconf (current-window-configuration))
4184         info)
4185     (or group (error "No group on current line"))
4186     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4187         (error "Killed group; can't be edited"))
4188     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4189     (gnus-configure-windows 'edit-group)
4190     (gnus-add-current-to-buffer-list)
4191     (emacs-lisp-mode)
4192     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4193     (use-local-map (copy-keymap emacs-lisp-mode-map))
4194     (local-set-key "\C-c\C-c" done-func)
4195     (make-local-variable 'gnus-prev-winconf)
4196     (setq gnus-prev-winconf winconf)
4197     ;; We modify the func to let it know what part it is editing.
4198     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4199     (setcar (cdr (cdr (nth 4 done-func))) group)
4200     (erase-buffer)
4201     (insert
4202      (cond 
4203       ((eq part 'method)
4204        ";; Type `C-c C-c' after editing the select method.\n\n")
4205       ((eq part 'params)
4206        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4207       ((eq part 'info)
4208        ";; Type `C-c C-c' after editing the group info.\n\n")))
4209     (let ((cinfo (gnus-copy-sequence info))
4210           marked)
4211       (if (not (setq marked (nth 3 cinfo)))
4212           ()
4213         (while marked
4214           (or (eq 'score (car (car marked)))
4215               (eq 'bookmark (car (car marked)))
4216               (eq 'killed (car (car marked)))
4217               (not (numberp (car (cdr (car marked)))))
4218               (setcdr (car marked) 
4219                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4220           (setq marked (cdr marked))))
4221       (insert 
4222        (pp-to-string
4223         (cond ((eq part 'method)
4224                (or (nth 4 info) "native"))
4225               ((eq part 'params)
4226                (nth 5 info))
4227               (t
4228                cinfo)))
4229        "\n"))))
4230
4231 (defun gnus-group-edit-group-method (group)
4232   "Edit the select method of GROUP."
4233   (interactive (list (gnus-group-group-name)))
4234   (gnus-group-edit-group group 'method))
4235
4236 (defun gnus-group-edit-group-parameters (group)
4237   "Edit the group parameters of GROUP."
4238   (interactive (list (gnus-group-group-name)))
4239   (gnus-group-edit-group group 'params))
4240
4241 (defun gnus-group-edit-group-done (part group)
4242   "Get info from buffer, update variables and jump to the group buffer."
4243   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4244   (goto-char (point-min))
4245   (let ((form (read (current-buffer)))
4246         (winconf gnus-prev-winconf))
4247     (if (eq part 'info) 
4248         (gnus-group-set-info form)
4249       (gnus-group-set-info form group part))
4250     (kill-buffer (current-buffer))
4251     (and winconf (set-window-configuration winconf))
4252     (set-buffer gnus-group-buffer)
4253     (gnus-group-update-group (gnus-group-group-name))
4254     (gnus-group-position-cursor)))
4255
4256 (defun gnus-group-make-help-group ()
4257   "Create the Gnus documentation group."
4258   (interactive)
4259   (let ((path (if installation-directory
4260                   (cons (concat installation-directory "etc/") load-path)
4261                 (cons data-directory load-path)))
4262         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
4263         file)
4264     (and (gnus-gethash name gnus-newsrc-hashtb)
4265          (error "Documentation group already exists"))
4266     (while (and path
4267                 (not (file-exists-p 
4268                       (setq file (concat (file-name-as-directory (car path))
4269                                          "gnus-tut.txt")))))
4270       (setq path (cdr path)))
4271     (if (not path)
4272         (message "Couldn't find doc group")
4273       (gnus-group-make-group 
4274        (gnus-group-real-name name)
4275        (list 'nndoc name
4276              (list 'nndoc-address file)
4277              (list 'nndoc-article-type 'mbox)))))
4278   (gnus-group-position-cursor))
4279
4280 (defun gnus-group-make-doc-group (file type)
4281   "Create a group that uses a single file as the source."
4282   (interactive 
4283    (list (read-file-name "File name: ") 
4284          (let ((err "")
4285                found char)
4286            (while (not found)
4287              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4288              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4289                                ((= char ?b) 'babyl)
4290                                ((= char ?d) 'digest)
4291                                (t (setq err (format "%c unknown. " char))
4292                                   nil))))
4293            found)))
4294   (let* ((file (expand-file-name file))
4295          (name (gnus-generate-new-group-name
4296                 (gnus-group-prefixed-name
4297                  (file-name-nondirectory file) '(nndoc "")))))
4298     (gnus-group-make-group 
4299      (gnus-group-real-name name)
4300      (list 'nndoc name
4301            (list 'nndoc-address file)
4302            (list 'nndoc-article-type type)))))
4303
4304 (defun gnus-group-make-archive-group (&optional all)
4305   "Create the (ding) Gnus archive group of the most recent articles.
4306 Given a prefix, create a full group."
4307   (interactive "P")
4308   (let ((group (gnus-group-prefixed-name 
4309                 (if all "ding.archives" "ding.recent") '(nndir ""))))
4310     (and (gnus-gethash group gnus-newsrc-hashtb)
4311          (error "Archive group already exists"))
4312     (gnus-group-make-group
4313      (gnus-group-real-name group)
4314      "nndir" 
4315      (if all gnus-group-archive-directory 
4316        gnus-group-recent-archive-directory)))
4317   (gnus-group-position-cursor))
4318
4319 (defun gnus-group-make-directory-group (dir)
4320   "Create an nndir group.
4321 The user will be prompted for a directory. The contents of this
4322 directory will be used as a newsgroup. The directory should contain
4323 mail messages or news articles in files that have numeric names."
4324   (interactive
4325    (list (read-file-name "Create group from directory: ")))
4326   (or (file-exists-p dir) (error "No such directory"))
4327   (or (file-directory-p dir) (error "Not a directory"))
4328   (gnus-group-make-group dir "nndir" dir)
4329   (gnus-group-position-cursor))
4330
4331 (defun gnus-group-make-kiboze-group (group address scores)
4332   "Create an nnkiboze group.
4333 The user will be prompted for a name, a regexp to match groups, and
4334 score file entries for articles to include in the group."
4335   (interactive
4336    (list
4337     (read-string "nnkiboze group name: ")
4338     (read-string "Source groups (regexp): ")
4339     (let ((headers (mapcar (lambda (group) (list group))
4340                            '("subject" "from" "number" "date" "message-id"
4341                              "references" "chars" "lines" "xref")))
4342           scores header regexp regexps)
4343       (while (not (equal "" (setq header (completing-read 
4344                                           "Match on header: " headers nil t))))
4345         (setq regexps nil)
4346         (while (not (equal "" (setq regexp (read-string 
4347                                             (format "Match on %s (string): "
4348                                                     header)))))
4349           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4350         (setq scores (cons (cons header regexps) scores)))
4351       scores)))
4352   (gnus-group-make-group group "nnkiboze" address)
4353   (save-excursion
4354     (gnus-set-work-buffer)
4355     (let (emacs-lisp-mode-hook)
4356       (pp scores (current-buffer)))
4357     (write-region (point-min) (point-max) 
4358                   (concat (or gnus-kill-files-directory "~/News")
4359                           "nnkiboze:" group "." gnus-score-file-suffix)))
4360   (gnus-group-position-cursor))
4361
4362 (defun gnus-group-add-to-virtual (n vgroup)
4363   "Add the current group to a virtual group."
4364   (interactive
4365    (list current-prefix-arg
4366          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4367                           "nnvirtual:")))
4368   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4369       (error "%s is not an nnvirtual group" vgroup))
4370   (let* ((groups (gnus-group-process-prefix n))
4371          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4372     (setcar (cdr method)
4373             (concat 
4374              (nth 1 method) "\\|"
4375              (mapconcat 
4376               (lambda (s) 
4377                 (gnus-group-remove-mark s)
4378                 (concat "\\(^" (regexp-quote s) "$\\)"))
4379               groups "\\|"))))
4380   (gnus-group-position-cursor))
4381
4382 (defun gnus-group-make-empty-virtual (group)
4383   "Create a new, fresh, empty virtual group."
4384   (interactive "sCreate new, empty virtual group: ")
4385   (let* ((method (list 'nnvirtual "^$"))
4386          (pgroup (gnus-group-prefixed-name group method)))
4387     ;; Check whether it exists already.
4388     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4389          (error "Group %s already exists." pgroup))
4390     ;; Subscribe the new group after the group on the current line.
4391     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4392     (gnus-group-update-group pgroup)
4393     (forward-line -1)
4394     (gnus-group-position-cursor)))
4395
4396 (defun gnus-group-enter-directory (dir)
4397   "Enter an ephemeral nneething group."
4398   (interactive "DDirectory to read: ")
4399   (let* ((method (list 'nneething dir))
4400          (leaf (gnus-group-prefixed-name
4401                 (file-name-nondirectory (directory-file-name dir))
4402                 method))
4403          (name (gnus-generate-new-group-name leaf)))
4404     (let ((nneething-read-only t))
4405       (or (gnus-group-read-ephemeral-group 
4406            name method t
4407            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4408                                       'summary 'group)))
4409           (error "Couldn't enter %s" dir)))))
4410
4411 ;; Group sorting commands
4412 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4413
4414 (defun gnus-group-sort-groups ()
4415   "Sort the group buffer using `gnus-group-sort-function'."
4416   (interactive)
4417   (setq gnus-newsrc-alist 
4418         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4419   (gnus-make-hashtable-from-newsrc-alist)
4420   (gnus-group-list-groups))
4421
4422 (defun gnus-group-sort-by-alphabet (info1 info2)
4423   (string< (car info1) (car info2)))
4424
4425 (defun gnus-group-sort-by-unread (info1 info2)
4426   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4427         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4428     (< (or (and (numberp n1) n1) 0)
4429        (or (and (numberp n2) n2) 0))))
4430
4431 (defun gnus-group-sort-by-level (info1 info2)
4432   (< (nth 1 info1) (nth 1 info2)))
4433
4434 ;; Group catching up.
4435
4436 (defun gnus-group-catchup-current (&optional n all)
4437   "Mark all articles not marked as unread in current newsgroup as read.
4438 If prefix argument N is numeric, the ARG next newsgroups will be
4439 caught up. If ALL is non-nil, marked articles will also be marked as
4440 read. Cross references (Xref: header) of articles are ignored.
4441 The difference between N and actual number of newsgroups that were
4442 caught up is returned."
4443   (interactive "P")
4444   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4445                gnus-expert-user
4446                (gnus-y-or-n-p
4447                 (if all
4448                     "Do you really want to mark all articles as read? "
4449                   "Mark all unread articles as read? "))))
4450       n
4451     (let ((groups (gnus-group-process-prefix n))
4452           (ret 0))
4453       (while groups
4454         ;; Virtual groups have to be given special treatment. 
4455         (let ((method (gnus-find-method-for-group (car groups))))
4456           (if (eq 'nnvirtual (car method))
4457               (nnvirtual-catchup-group
4458                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4459         (gnus-group-remove-mark (car groups))
4460         (if (prog1
4461                 (gnus-group-goto-group (car groups))
4462               (gnus-group-catchup (car groups) all))
4463             (gnus-group-update-group-line)
4464           (setq ret (1+ ret)))
4465         (setq groups (cdr groups)))
4466       (gnus-group-next-unread-group 1)
4467       ret)))
4468
4469 (defun gnus-group-catchup-current-all (&optional n)
4470   "Mark all articles in current newsgroup as read.
4471 Cross references (Xref: header) of articles are ignored."
4472   (interactive "P")
4473   (gnus-group-catchup-current n 'all))
4474
4475 (defun gnus-group-catchup (group &optional all)
4476   "Mark all articles in GROUP as read.
4477 If ALL is non-nil, all articles are marked as read.
4478 The return value is the number of articles that were marked as read,
4479 or nil if no action could be taken."
4480   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4481          (num (car entry))
4482          (marked (nth 3 (nth 2 entry))))
4483     (if (not (numberp (car entry)))
4484         (gnus-message 1 "Can't catch up; non-active group")
4485       ;; Do the updating only if the newsgroup isn't killed.
4486       (if (not entry)
4487           ()
4488         (gnus-update-read-articles 
4489          group (and (not all) (append (cdr (assq 'tick marked))
4490                                       (cdr (assq 'dormant marked))))
4491          nil (and (not all) (cdr (assq 'tick marked))))
4492         (and all 
4493              (setq marked (nth 3 (nth 2 entry)))
4494              (setcar (nthcdr 3 (nth 2 entry)) 
4495                      (delq (assq 'dormant marked) 
4496                            (nth 3 (nth 2 entry)))))))
4497     num))
4498
4499 (defun gnus-group-expire-articles (&optional n)
4500   "Expire all expirable articles in the current newsgroup."
4501   (interactive "P")
4502   (let ((groups (gnus-group-process-prefix n))
4503         group)
4504     (or groups (error "No groups to expire"))
4505     (while groups
4506       (setq group (car groups)
4507             groups (cdr groups))
4508       (gnus-group-remove-mark group)
4509       (if (not (gnus-check-backend-function 'request-expire-articles group))
4510           ()
4511         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4512                (expirable (if (memq 'total-expire (nth 5 info))
4513                               (cons nil (gnus-list-of-read-articles group))
4514                             (assq 'expire (nth 3 info)))))
4515           (and expirable 
4516                (setcdr expirable
4517                        (gnus-request-expire-articles 
4518                         (cdr expirable) group))))))))
4519
4520 (defun gnus-group-expire-all-groups ()
4521   "Expire all expirable articles in all newsgroups."
4522   (interactive)
4523   (save-excursion
4524     (gnus-message 5 "Expiring...")
4525     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4526                                      (cdr gnus-newsrc-alist))))
4527       (gnus-group-expire-articles nil)))
4528   (gnus-group-position-cursor)
4529   (gnus-message 5 "Expiring...done"))
4530
4531 (defun gnus-group-set-current-level (n level)
4532   "Set the level of the next N groups to LEVEL."
4533   (interactive "P\nnLevel: ")
4534   (or (and (>= level 1) (<= level gnus-level-killed))
4535       (error "Illegal level: %d" level))
4536   (let ((groups (gnus-group-process-prefix n))
4537         group)
4538     (while groups
4539       (setq group (car groups)
4540             groups (cdr groups))
4541       (gnus-group-remove-mark group)
4542       (gnus-message 6 "Changed level of %s from %d to %d" 
4543                     group (gnus-group-group-level) level)
4544       (gnus-group-change-level group level
4545                                (gnus-group-group-level))
4546       (gnus-group-update-group-line)))
4547   (gnus-group-position-cursor))
4548
4549 (defun gnus-group-unsubscribe-current-group (&optional n)
4550   "Toggle subscription of the current group.
4551 If given numerical prefix, toggle the N next groups."
4552   (interactive "P")
4553   (let ((groups (gnus-group-process-prefix n))
4554         group)
4555     (while groups
4556       (setq group (car groups)
4557             groups (cdr groups))
4558       (gnus-group-remove-mark group)
4559       (gnus-group-unsubscribe-group
4560        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4561                  gnus-level-default-unsubscribed
4562                gnus-level-default-subscribed))
4563       (gnus-group-update-group-line))
4564     (gnus-group-next-group 1)))
4565
4566 (defun gnus-group-unsubscribe-group (group &optional level)
4567   "Toggle subscribe from/to unsubscribe GROUP.
4568 New newsgroup is added to .newsrc automatically."
4569   (interactive
4570    (list (completing-read
4571           "Group: " gnus-active-hashtb nil 
4572           (memq gnus-select-method gnus-have-read-active-file))))
4573   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4574     (cond
4575      ((string-match "^[ \t]$" group)
4576       (error "Empty group name"))
4577      (newsrc
4578       ;; Toggle subscription flag.
4579       (gnus-group-change-level 
4580        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4581                                       gnus-level-subscribed) 
4582                                   (1+ gnus-level-subscribed)
4583                                 gnus-level-default-subscribed)))
4584       (gnus-group-update-group group))
4585      ((and (stringp group)
4586            (or (not (memq gnus-select-method gnus-have-read-active-file))
4587                (gnus-gethash group gnus-active-hashtb)))
4588       ;; Add new newsgroup.
4589       (gnus-group-change-level 
4590        group 
4591        (if level level gnus-level-default-subscribed) 
4592        (or (and (member group gnus-zombie-list) 
4593                 gnus-level-zombie) 
4594            gnus-level-killed)
4595        (and (gnus-group-group-name)
4596             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4597       (gnus-group-update-group group))
4598      (t (error "No such newsgroup: %s" group)))
4599     (gnus-group-position-cursor)))
4600
4601 (defun gnus-group-transpose-groups (n)
4602   "Move the current newsgroup up N places.
4603 If given a negative prefix, move down instead. The difference between
4604 N and the number of steps taken is returned." 
4605   (interactive "p")
4606   (or (gnus-group-group-name)
4607       (error "No group on current line"))
4608   (gnus-group-kill-group 1)
4609   (prog1
4610       (forward-line (- n))
4611     (gnus-group-yank-group)
4612     (gnus-group-position-cursor)))
4613
4614 (defun gnus-group-kill-all-zombies ()
4615   "Kill all zombie newsgroups."
4616   (interactive)
4617   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4618   (setq gnus-zombie-list nil)
4619   (gnus-group-list-groups))
4620
4621 (defun gnus-group-kill-region (begin end)
4622   "Kill newsgroups in current region (excluding current point).
4623 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4624   (interactive "r")
4625   (let ((lines
4626          ;; Count lines.
4627          (save-excursion
4628            (count-lines
4629             (progn
4630               (goto-char begin)
4631               (beginning-of-line)
4632               (point))
4633             (progn
4634               (goto-char end)
4635               (beginning-of-line)
4636               (point))))))
4637     (goto-char begin)
4638     (beginning-of-line)                 ;Important when LINES < 1
4639     (gnus-group-kill-group lines)))
4640
4641 (defun gnus-group-kill-group (&optional n)
4642   "The the next N groups.
4643 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4644 However, only groups that were alive can be yanked; already killed 
4645 groups or zombie groups can't be yanked.
4646 The return value is the name of the (last) group that was killed."
4647   (interactive "P")
4648   (let ((buffer-read-only nil)
4649         (groups (gnus-group-process-prefix n))
4650         group entry level)
4651     (while groups
4652       (setq group (car groups)
4653             groups (cdr groups))
4654       (gnus-group-remove-mark group)
4655       (setq level (gnus-group-group-level))
4656       (gnus-delete-line)
4657       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4658           (setq gnus-list-of-killed-groups 
4659                 (cons (cons (car entry) (nth 2 entry)) 
4660                       gnus-list-of-killed-groups)))
4661       (gnus-group-change-level 
4662        (if entry entry group) gnus-level-killed (if entry nil level)))
4663     (gnus-group-position-cursor)
4664     group))
4665
4666 (defun gnus-group-yank-group (&optional arg)
4667   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4668 inserting it before the current newsgroup.  The numeric ARG specifies
4669 how many newsgroups are to be yanked.  The name of the (last)
4670 newsgroup yanked is returned."
4671   (interactive "p")
4672   (if (not arg) (setq arg 1))
4673   (let (info group prev)
4674     (while (>= (setq arg (1- arg)) 0)
4675       (if (not (setq info (car gnus-list-of-killed-groups)))
4676           (error "No more newsgroups to yank"))
4677       (setq group (nth 2 info))
4678       ;; Find which newsgroup to insert this one before - search
4679       ;; backward until something suitable is found. If there are no
4680       ;; other newsgroups in this buffer, just make this newsgroup the
4681       ;; first newsgroup.
4682       (setq prev (gnus-group-group-name))
4683       (gnus-group-change-level 
4684        info (nth 2 info) gnus-level-killed 
4685        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4686        t)
4687       (gnus-group-insert-group-line-info (nth 1 info))
4688       (setq gnus-list-of-killed-groups 
4689             (cdr gnus-list-of-killed-groups)))
4690     (forward-line -1)
4691     (gnus-group-position-cursor)
4692     group))
4693       
4694 (defun gnus-group-list-all-groups (&optional arg)
4695   "List all newsgroups with level ARG or lower.
4696 Default is gnus-level-unsubscribed, which lists all subscribed and most
4697 unsubscribed groups."
4698   (interactive "P")
4699   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4700
4701 (defun gnus-group-list-killed ()
4702   "List all killed newsgroups in the group buffer."
4703   (interactive)
4704   (if (not gnus-killed-list)
4705       (gnus-message 6 "No killed groups")
4706     (let (gnus-group-list-mode)
4707       (funcall gnus-group-prepare-function 
4708                gnus-level-killed t gnus-level-killed))
4709     (goto-char (point-min)))
4710   (gnus-group-position-cursor))
4711
4712 (defun gnus-group-list-zombies ()
4713   "List all zombie newsgroups in the group buffer."
4714   (interactive)
4715   (if (not gnus-zombie-list)
4716       (gnus-message 6 "No zombie groups")
4717     (let (gnus-group-list-mode)
4718       (funcall gnus-group-prepare-function
4719                gnus-level-zombie t gnus-level-zombie))
4720     (goto-char (point-min)))
4721   (gnus-group-position-cursor))
4722
4723 (defun gnus-group-get-new-news (&optional arg)
4724   "Get newly arrived articles.
4725 If ARG is non-nil, it should be a number between one and nine to
4726 specify which levels you are interested in re-scanning."
4727   (interactive "P")
4728   (run-hooks 'gnus-get-new-news-hook)
4729   (setq arg (gnus-group-default-level arg t))
4730   (if (and gnus-read-active-file (not arg))
4731       (progn
4732         (gnus-read-active-file)
4733         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4734     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
4735       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4736   (gnus-group-list-groups))
4737
4738 (defun gnus-group-get-new-news-this-group (&optional n)
4739   "Check for newly arrived news in the current group (and the N-1 next groups).
4740 The difference between N and the number of newsgroup checked is returned.
4741 If N is negative, this group and the N-1 previous groups will be checked."
4742   (interactive "P")
4743   (let* ((groups (gnus-group-process-prefix n))
4744          (ret (if (numberp n) (- n (length groups)) 0))
4745          group)
4746     (while groups
4747       (setq group (car groups)
4748             groups (cdr groups))
4749       (gnus-group-remove-mark group)
4750       (or (gnus-get-new-news-in-group group)
4751           (progn 
4752             (ding) 
4753             (message "%s error: %s" group (gnus-status-message group))
4754             (sit-for 2))))
4755     (gnus-group-next-unread-group 1 t)
4756     (gnus-summary-position-cursor)
4757     ret))
4758
4759 (defun gnus-get-new-news-in-group (group)
4760   (and group 
4761        (gnus-activate-group group)
4762        (progn
4763          (gnus-get-unread-articles-in-group 
4764           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4765           (gnus-gethash group gnus-active-hashtb))
4766          (gnus-group-update-group-line)
4767          t)))
4768
4769 (defun gnus-group-fetch-faq (group)
4770   "Fetch the FAQ for the current group."
4771   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4772   (or group (error "No group name given"))
4773   (let ((file (concat gnus-group-faq-directory (gnus-group-real-name group))))
4774     (if (not (file-exists-p file))
4775         (error "No such file: %s" file)
4776       (find-file file))))
4777   
4778 (defun gnus-group-describe-group (force &optional group)
4779   "Display a description of the current newsgroup."
4780   (interactive (list current-prefix-arg (gnus-group-group-name)))
4781   (and force (setq gnus-description-hashtb nil))
4782   (let ((method (gnus-find-method-for-group group))
4783         desc)
4784     (or group (error "No group name given"))
4785     (and (or (and gnus-description-hashtb
4786                   ;; We check whether this group's method has been
4787                   ;; queried for a description file.  
4788                   (gnus-gethash 
4789                    (gnus-group-prefixed-name "" method) 
4790                    gnus-description-hashtb))
4791              (setq desc (gnus-group-get-description group))
4792              (gnus-read-descriptions-file method))
4793          (message
4794           (or desc (gnus-gethash group gnus-description-hashtb)
4795               "No description available")))))
4796
4797 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4798 (defun gnus-group-describe-all-groups (&optional force)
4799   "Pop up a buffer with descriptions of all newsgroups."
4800   (interactive "P")
4801   (and force (setq gnus-description-hashtb nil))
4802   (if (not (or gnus-description-hashtb
4803                (gnus-read-all-descriptions-files)))
4804       (error "Couldn't request descriptions file"))
4805   (let ((buffer-read-only nil)
4806         b)
4807     (erase-buffer)
4808     (mapatoms
4809      (lambda (group)
4810        (setq b (point))
4811        (insert (format "      *: %-20s %s\n" (symbol-name group)
4812                        (symbol-value group)))
4813        (add-text-properties 
4814         b (1+ b) (list 'gnus-group group
4815                        'gnus-unread t 'gnus-marked nil
4816                        'gnus-level (1+ gnus-level-subscribed))))
4817      gnus-description-hashtb)
4818     (goto-char (point-min))
4819     (gnus-group-position-cursor)))
4820
4821 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4822 (defun gnus-group-apropos (regexp &optional search-description)
4823   "List all newsgroups that have names that match a regexp."
4824   (interactive "sGnus apropos (regexp): ")
4825   (let ((prev "")
4826         (obuf (current-buffer))
4827         groups des)
4828     ;; Go through all newsgroups that are known to Gnus.
4829     (mapatoms 
4830      (lambda (group)
4831        (and (symbol-name group)
4832             (string-match regexp (symbol-name group))
4833             (setq groups (cons (symbol-name group) groups))))
4834      gnus-active-hashtb)
4835     ;; Go through all descriptions that are known to Gnus. 
4836     (if search-description
4837         (mapatoms 
4838          (lambda (group)
4839            (and (string-match regexp (symbol-value group))
4840                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4841                 (setq groups (cons (symbol-name group) groups))))
4842          gnus-description-hashtb))
4843     (if (not groups)
4844         (gnus-message 3 "No groups matched \"%s\"." regexp)
4845       ;; Print out all the groups.
4846       (save-excursion
4847         (pop-to-buffer "*Gnus Help*")
4848         (buffer-disable-undo (current-buffer))
4849         (erase-buffer)
4850         (setq groups (sort groups 'string<))
4851         (while groups
4852           ;; Groups may be entered twice into the list of groups.
4853           (if (not (string= (car groups) prev))
4854               (progn
4855                 (insert (setq prev (car groups)) "\n")
4856                 (if (and gnus-description-hashtb
4857                          (setq des (gnus-gethash (car groups) 
4858                                                  gnus-description-hashtb)))
4859                     (insert "  " des "\n"))))
4860           (setq groups (cdr groups)))
4861         (goto-char (point-min))))
4862     (pop-to-buffer obuf)))
4863
4864 (defun gnus-group-description-apropos (regexp)
4865   "List all newsgroups that have names or descriptions that match a regexp."
4866   (interactive "sGnus description apropos (regexp): ")
4867   (if (not (or gnus-description-hashtb
4868                (gnus-read-all-descriptions-files)))
4869       (error "Couldn't request descriptions file"))
4870   (gnus-group-apropos regexp t))
4871
4872 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4873 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4874   "List all groups with unread articles that match REGEXP.
4875 If the prefix LEVEL is non-nil, it should be a number that says which
4876 level to cut off listing groups. 
4877 If ALL, also list groups with no unread articles.
4878 If LOWEST, don't list groups with level lower than LOWEST."
4879   (interactive "P\nsList newsgroups matching: ")
4880   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4881                            all (or lowest 1) regexp)
4882   (goto-char (point-min))
4883   (gnus-group-position-cursor))
4884
4885 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4886   "List all groups that match REGEXP.
4887 If the prefix LEVEL is non-nil, it should be a number that says which
4888 level to cut off listing groups. 
4889 If LOWEST, don't list groups with level lower than LOWEST."
4890   (interactive "P\nsList newsgroups matching: ")
4891   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4892
4893 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4894 (defun gnus-group-save-newsrc ()
4895   "Save the Gnus startup files."
4896   (interactive)
4897   (gnus-save-newsrc-file))
4898
4899 (defun gnus-group-restart (&optional arg)
4900   "Force Gnus to read the .newsrc file."
4901   (interactive "P")
4902   (gnus-save-newsrc-file)
4903   (gnus-setup-news 'force)
4904   (gnus-group-list-groups arg))
4905
4906 (defun gnus-group-read-init-file ()
4907   "Read the Gnus elisp init file."
4908   (interactive)
4909   (gnus-read-init-file))
4910
4911 (defun gnus-group-check-bogus-groups (&optional silent)
4912   "Check bogus newsgroups.
4913 If given a prefix, don't ask for confirmation before removing a bogus
4914 group."
4915   (interactive "P")
4916   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4917   (gnus-group-list-groups))
4918
4919 (defun gnus-group-edit-global-kill (&optional article group)
4920   "Edit the global kill file.
4921 If GROUP, edit that local kill file instead."
4922   (interactive "P")
4923   (setq gnus-current-kill-article article)
4924   (gnus-kill-file-edit-file group)
4925   (gnus-message 
4926    6
4927    (substitute-command-keys
4928     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4929
4930 (defun gnus-group-edit-local-kill (article group)
4931   "Edit a local kill file."
4932   (interactive (list nil (gnus-group-group-name)))
4933   (gnus-group-edit-global-kill article group))
4934
4935 (defun gnus-group-force-update ()
4936   "Update `.newsrc' file."
4937   (interactive)
4938   (gnus-save-newsrc-file))
4939
4940 (defun gnus-group-suspend ()
4941   "Suspend the current Gnus session.
4942 In fact, cleanup buffers except for group mode buffer.
4943 The hook gnus-suspend-gnus-hook is called before actually suspending."
4944   (interactive)
4945   (run-hooks 'gnus-suspend-gnus-hook)
4946   ;; Kill Gnus buffers except for group mode buffer.
4947   (let ((group-buf (get-buffer gnus-group-buffer)))
4948     ;; Do this on a separate list in case the user does a ^G before we finish
4949     (let ((gnus-buffer-list
4950            (delq group-buf (delq gnus-dribble-buffer
4951                                  (append gnus-buffer-list nil)))))
4952       (while gnus-buffer-list
4953         (gnus-kill-buffer (car gnus-buffer-list))
4954         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4955     (if group-buf
4956         (progn
4957           (setq gnus-buffer-list (list group-buf))
4958           (bury-buffer group-buf)
4959           (delete-windows-on group-buf t)))))
4960
4961 (defun gnus-group-clear-dribble ()
4962   "Clear all information from the dribble buffer."
4963   (interactive)
4964   (gnus-dribble-clear))
4965
4966 (defun gnus-group-exit ()
4967   "Quit reading news after updating .newsrc.eld and .newsrc.
4968 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4969   (interactive)
4970   (if (or noninteractive                ;For gnus-batch-kill
4971           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4972           (not gnus-interactive-exit)   ;Without confirmation
4973           gnus-expert-user
4974           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4975       (progn
4976         (run-hooks 'gnus-exit-gnus-hook)
4977         ;; Offer to save data from non-quitted summary buffers.
4978         (gnus-offer-save-summaries)
4979         ;; Save the newsrc file(s).
4980         (gnus-save-newsrc-file)
4981         ;; Kill-em-all.
4982         (gnus-close-backends)
4983         ;; Reset everything.
4984         (gnus-clear-system))))
4985
4986 (defun gnus-close-backends ()
4987   ;; Send a close request to all backends that support such a request. 
4988   (let ((methods gnus-valid-select-methods)
4989         func)
4990     (while methods
4991       (if (fboundp (setq func (intern (concat (car (car methods))
4992                                               "-request-close"))))
4993           (funcall func))
4994       (setq methods (cdr methods)))))
4995
4996 (defun gnus-group-quit ()
4997   "Quit reading news without updating .newsrc.eld or .newsrc.
4998 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4999   (interactive)
5000   (if (or noninteractive                ;For gnus-batch-kill
5001           (zerop (buffer-size))
5002           (not (gnus-server-opened gnus-select-method))
5003           gnus-expert-user
5004           (not gnus-current-startup-file)
5005           (gnus-yes-or-no-p
5006            (format "Quit reading news without saving %s? "
5007                    (file-name-nondirectory gnus-current-startup-file))))
5008       (progn
5009         (run-hooks 'gnus-exit-gnus-hook)
5010         (if gnus-use-full-window
5011             (delete-other-windows)
5012           (gnus-remove-some-windows))
5013         (gnus-dribble-save)
5014         (gnus-close-backends)
5015         (gnus-clear-system))))
5016
5017 (defun gnus-offer-save-summaries ()
5018   (save-excursion
5019     (let ((buflist (buffer-list)) 
5020           buffers bufname)
5021       (while buflist
5022         (and (setq bufname (buffer-name (car buflist)))
5023              (string-match "Summary" bufname)
5024              (save-excursion
5025                (set-buffer bufname)
5026                ;; We check that this is, indeed, a summary buffer.
5027                (eq major-mode 'gnus-summary-mode))
5028              (setq buffers (cons bufname buffers)))
5029         (setq buflist (cdr buflist)))
5030       (and buffers
5031            (map-y-or-n-p 
5032             "Update summary buffer %s? "
5033             (lambda (buf)
5034               (set-buffer buf)
5035               (gnus-summary-exit))
5036             buffers)))))
5037
5038 (defun gnus-group-describe-briefly ()
5039   "Give a one line description of the group mode commands."
5040   (interactive)
5041   (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")))
5042
5043 (defun gnus-group-browse-foreign-server (method)
5044   "Browse a foreign news server.
5045 If called interactively, this function will ask for a select method
5046  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
5047 If not, METHOD should be a list where the first element is the method
5048 and the second element is the address."
5049   (interactive
5050    (list (let ((how (completing-read 
5051                      "Which backend: "
5052                      (append gnus-valid-select-methods gnus-server-alist)
5053                      nil t "nntp")))
5054            ;; We either got a backend name or a virtual server name.
5055            ;; If the first, we also need an address.
5056            (if (assoc how gnus-valid-select-methods)
5057                (list (intern how)
5058                      ;; Suggested by mapjph@bath.ac.uk.
5059                      (completing-read 
5060                       "Address: " 
5061                       (mapcar (lambda (server) (list server))
5062                               gnus-secondary-servers)))
5063              ;; We got a server name, so we find the method.
5064              (gnus-server-to-method how)))))
5065   (gnus-browse-foreign-server method))
5066
5067 \f
5068 ;;;
5069 ;;; Browse Server Mode
5070 ;;;
5071
5072 (defvar gnus-browse-mode-hook nil)
5073 (defvar gnus-browse-mode-map nil)
5074 (put 'gnus-browse-mode 'mode-class 'special)
5075
5076 (if gnus-browse-mode-map
5077     nil
5078   (setq gnus-browse-mode-map (make-keymap))
5079   (suppress-keymap gnus-browse-mode-map)
5080   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
5081   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
5082   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
5083   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
5084   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
5085   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
5086   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
5087   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
5088   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
5089   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
5090   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
5091   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
5092   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
5093   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
5094   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
5095   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
5096   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
5097   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
5098   )
5099
5100 (defvar gnus-browse-current-method nil)
5101 (defvar gnus-browse-return-buffer nil)
5102
5103 (defvar gnus-browse-buffer "*Gnus Browse Server*")
5104
5105 (defun gnus-browse-foreign-server (method &optional return-buffer)
5106   (setq gnus-browse-current-method method)
5107   (setq gnus-browse-return-buffer return-buffer)
5108   (let ((gnus-select-method method)
5109         groups group)
5110     (gnus-message 5 "Connecting to %s..." (nth 1 method))
5111     (or (gnus-check-server method)
5112         (error "Unable to contact server: %s" (gnus-status-message method)))
5113     (or (gnus-request-list method)
5114         (error "Couldn't request list: %s" (gnus-status-message method)))
5115     (get-buffer-create gnus-browse-buffer)
5116     (gnus-add-current-to-buffer-list)
5117     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
5118     (gnus-configure-windows 'browse)
5119     (buffer-disable-undo (current-buffer))
5120     (let ((buffer-read-only nil))
5121       (erase-buffer))
5122     (gnus-browse-mode)
5123     (setq mode-line-buffer-identification
5124           (format
5125            "Gnus  Browse Server {%s:%s}" (car method) (car (cdr method))))
5126     (save-excursion
5127       (set-buffer nntp-server-buffer)
5128       (let ((cur (current-buffer)))
5129         (goto-char (point-min))
5130         (or (string= gnus-ignored-newsgroups "")
5131             (delete-matching-lines gnus-ignored-newsgroups))
5132         (while (re-search-forward 
5133                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
5134           (goto-char (match-end 1))
5135           (setq groups (cons (cons (buffer-substring (match-beginning 1)
5136                                                      (match-end 1))
5137                                    (max 0 (- (1+ (read cur)) (read cur))))
5138                              groups)))))
5139     (setq groups (sort groups 
5140                        (lambda (l1 l2)
5141                          (string< (car l1) (car l2)))))
5142     (let ((buffer-read-only nil))
5143       (while groups
5144         (setq group (car groups))
5145         (insert 
5146          (format "K%7d: %s\n" (cdr group) (car group)))
5147         (setq groups (cdr groups))))
5148     (switch-to-buffer (current-buffer))
5149     (goto-char (point-min))
5150     (gnus-group-position-cursor)))
5151
5152 (defun gnus-browse-mode ()
5153   "Major mode for browsing a foreign server.
5154
5155 All normal editing commands are switched off.
5156
5157 \\<gnus-browse-mode-map>
5158 The only things you can do in this buffer is
5159
5160 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5161 The group will be inserted into the group buffer upon exit from this
5162 buffer.  
5163
5164 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5165
5166 3) `\\[gnus-browse-exit]' to return to the group buffer."
5167   (interactive)
5168   (kill-all-local-variables)
5169   (if gnus-visual (gnus-browse-make-menu-bar))
5170   (gnus-simplify-mode-line)
5171   (setq major-mode 'gnus-browse-mode)
5172   (setq mode-name "Browse Server")
5173   (setq mode-line-process nil)
5174   (use-local-map gnus-browse-mode-map)
5175   (buffer-disable-undo (current-buffer))
5176   (setq truncate-lines t)
5177   (setq buffer-read-only t)
5178   (run-hooks 'gnus-browse-mode-hook))
5179
5180 (defun gnus-browse-read-group (&optional no-article)
5181   "Enter the group at the current line."
5182   (interactive)
5183   (let ((group (gnus-browse-group-name)))
5184     (or (gnus-group-read-ephemeral-group 
5185          group gnus-browse-current-method nil
5186          (cons (current-buffer) 'browse))
5187         (error "Couldn't enter %s" group))))
5188
5189 (defun gnus-browse-select-group ()
5190   "Select the current group."
5191   (interactive)
5192   (gnus-browse-read-group 'no))
5193
5194 (defun gnus-browse-next-group (n)
5195   "Go to the next group."
5196   (interactive "p")
5197   (prog1
5198       (forward-line n)
5199     (gnus-group-position-cursor)))
5200
5201 (defun gnus-browse-prev-group (n)
5202   "Go to the next group."
5203   (interactive "p")
5204   (gnus-browse-next-group (- n)))
5205
5206 (defun gnus-browse-unsubscribe-current-group (arg)
5207   "(Un)subscribe to the next ARG groups."
5208   (interactive "p")
5209   (and (eobp)
5210        (error "No group at current line."))
5211   (let ((ward (if (< arg 0) -1 1))
5212         (arg (abs arg)))
5213     (while (and (> arg 0)
5214                 (not (eobp))
5215                 (gnus-browse-unsubscribe-group)
5216                 (zerop (gnus-browse-next-group ward)))
5217       (setq arg (1- arg)))
5218     (gnus-group-position-cursor)
5219     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5220     arg))
5221
5222 (defun gnus-browse-group-name ()
5223   (save-excursion
5224     (beginning-of-line)
5225     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5226         ()
5227       (gnus-group-prefixed-name 
5228        (buffer-substring (match-beginning 1) (match-end 1))
5229        gnus-browse-current-method))))
5230   
5231 (defun gnus-browse-unsubscribe-group ()
5232   "Toggle subscription of the current group in the browse buffer."
5233   (let ((sub nil)
5234         (buffer-read-only nil)
5235         group)
5236     (save-excursion
5237       (beginning-of-line)
5238       ;; If this group it killed, then we want to subscribe it.
5239       (if (= (following-char) ?K) (setq sub t))
5240       (setq group (gnus-browse-group-name))
5241       (delete-char 1)
5242       (if sub
5243           (progn
5244             (gnus-group-change-level 
5245              (list t group gnus-level-default-subscribed
5246                    nil nil gnus-browse-current-method) 
5247              gnus-level-default-subscribed gnus-level-killed
5248              (and (car (nth 1 gnus-newsrc-alist))
5249                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5250                                 gnus-newsrc-hashtb))
5251              t)
5252             (insert ? ))
5253         (gnus-group-change-level 
5254          group gnus-level-killed gnus-level-default-subscribed)
5255         (insert ?K)))
5256     t))
5257
5258 (defun gnus-browse-exit ()
5259   "Quit browsing and return to the group buffer."
5260   (interactive)
5261   (if (eq major-mode 'gnus-browse-mode)
5262       (kill-buffer (current-buffer)))
5263   (if gnus-browse-return-buffer
5264       (gnus-configure-windows 'server 'force)
5265     (gnus-configure-windows 'group 'force)
5266     (gnus-group-list-groups nil)))
5267
5268 (defun gnus-browse-describe-briefly ()
5269   "Give a one line description of the group mode commands."
5270   (interactive)
5271   (gnus-message 6
5272                 (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")))
5273       
5274 \f
5275 ;;;
5276 ;;; Gnus summary mode
5277 ;;;
5278
5279 (defvar gnus-summary-mode-map nil)
5280 (defvar gnus-summary-mark-map nil)
5281 (defvar gnus-summary-mscore-map nil)
5282 (defvar gnus-summary-article-map nil)
5283 (defvar gnus-summary-thread-map nil)
5284 (defvar gnus-summary-goto-map nil)
5285 (defvar gnus-summary-exit-map nil)
5286 (defvar gnus-summary-interest-map nil)
5287 (defvar gnus-summary-sort-map nil)
5288 (defvar gnus-summary-backend-map nil)
5289 (defvar gnus-summary-save-map nil)
5290 (defvar gnus-summary-wash-map nil)
5291 (defvar gnus-summary-wash-hide-map nil)
5292 (defvar gnus-summary-wash-highlight-map nil)
5293 (defvar gnus-summary-wash-time-map nil)
5294 (defvar gnus-summary-help-map nil)
5295
5296 (put 'gnus-summary-mode 'mode-class 'special)
5297
5298 (if gnus-summary-mode-map
5299     nil
5300   (setq gnus-summary-mode-map (make-keymap))
5301   (suppress-keymap gnus-summary-mode-map)
5302
5303   ;; Non-orthogonal keys
5304
5305   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5306   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5307   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5308   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5309   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5310   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5311   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5312   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5313   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5314   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5315   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5316   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5317   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5318   (define-key gnus-summary-mode-map 
5319     "\M-s" 'gnus-summary-search-article-forward)
5320   (define-key gnus-summary-mode-map 
5321     "\M-r" 'gnus-summary-search-article-backward)
5322   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5323   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5324   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5325   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5326   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5327   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5328   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5329   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5330   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5331   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5332   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5333   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5334   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5335   (define-key gnus-summary-mode-map 
5336     "k" 'gnus-summary-kill-same-subject-and-select)
5337   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5338   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5339   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5340   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5341   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5342   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5343   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5344   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5345   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5346   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5347   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5348   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5349   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5350   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5351   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5352   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5353   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5354   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5355   (define-key gnus-summary-mode-map 
5356     "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5357   (define-key gnus-summary-mode-map 
5358     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5359   (define-key gnus-summary-mode-map 
5360     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5361   (define-key gnus-summary-mode-map 
5362     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5363   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5364   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5365   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5366   (define-key gnus-summary-mode-map 
5367     "\C-x\C-s" 'gnus-summary-reselect-current-group)
5368   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5369   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5370   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5371   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5372   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5373   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5374   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5375   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5376   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5377   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5378   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5379   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5380   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5381   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5382   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5383   (define-key gnus-summary-mode-map "V" 'gnus-version)
5384   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5385   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5386   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5387   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5388   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5389   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5390   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5391   (define-key gnus-summary-mode-map 
5392     "x" 'gnus-summary-remove-lines-marked-as-read)
5393 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5394   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5395   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5396   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5397 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5398   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5399   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5400   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5401   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5402   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5403
5404
5405   ;; Sort of orthogonal keymap
5406   (define-prefix-command 'gnus-summary-mark-map)
5407   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5408   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5409   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5410   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5411   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5412   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5413   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5414   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5415   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5416   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5417   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5418   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5419   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5420   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5421   (define-key gnus-summary-mark-map 
5422     "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5423   (define-key gnus-summary-mark-map 
5424     "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5425   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5426   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5427   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5428   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5429   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5430   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5431   (define-key gnus-summary-mark-map 
5432     "k" 'gnus-summary-kill-same-subject-and-select)
5433   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5434
5435   (define-prefix-command 'gnus-summary-mscore-map)
5436   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5437   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5438   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5439   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5440   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5441
5442   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5443   
5444   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5445   
5446   (define-prefix-command 'gnus-summary-goto-map)
5447   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5448   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5449   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5450   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5451   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5452   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5453   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5454   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5455   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5456   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5457   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5458   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5459   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5460   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5461
5462
5463   (define-prefix-command 'gnus-summary-thread-map)
5464   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5465   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5466   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5467   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5468   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5469   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5470   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5471   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5472   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5473   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5474   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5475   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5476   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5477   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5478
5479   
5480   (define-prefix-command 'gnus-summary-exit-map)
5481   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5482   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5483   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5484   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5485   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5486   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5487   (define-key gnus-summary-exit-map 
5488     "n" 'gnus-summary-catchup-and-goto-next-group)
5489   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5490   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5491   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5492   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5493
5494
5495   (define-prefix-command 'gnus-summary-article-map)
5496   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5497   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5498   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5499   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5500   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5501   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5502   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5503   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5504   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5505   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5506   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5507   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5508   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5509   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5510
5511
5512
5513   (define-prefix-command 'gnus-summary-wash-map)
5514   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5515
5516   (define-prefix-command 'gnus-summary-wash-hide-map)
5517   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5518   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5519   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5520   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5521   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5522   (define-key gnus-summary-wash-hide-map 
5523     "\C-c" 'gnus-article-hide-citation-maybe)
5524
5525   (define-prefix-command 'gnus-summary-wash-highlight-map)
5526   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5527   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5528   (define-key gnus-summary-wash-highlight-map 
5529     "h" 'gnus-article-highlight-headers)
5530   (define-key gnus-summary-wash-highlight-map
5531     "c" 'gnus-article-highlight-citation)
5532   (define-key gnus-summary-wash-highlight-map
5533     "s" 'gnus-article-highlight-signature)
5534
5535   (define-prefix-command 'gnus-summary-wash-time-map)
5536   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5537   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5538   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5539   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5540   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5541
5542   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5543   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5544   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5545   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5546   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5547   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5548   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5549   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5550   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5551   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5552
5553
5554   (define-prefix-command 'gnus-summary-help-map)
5555   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5556   (define-key gnus-summary-help-map "v" 'gnus-version)
5557   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5558   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5559   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5560   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5561
5562
5563   (define-prefix-command 'gnus-summary-backend-map)
5564   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5565   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5566   (define-key gnus-summary-backend-map "\M-\C-e" 
5567     'gnus-summary-expire-articles-now)
5568   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5569   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5570   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5571   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5572   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5573   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5574   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5575
5576
5577   (define-prefix-command 'gnus-summary-save-map)
5578   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5579   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5580   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5581   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5582   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5583   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5584   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5585   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5586 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5587
5588   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5589
5590   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5591 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5592 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5593 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5594 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5595 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5596 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5597   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5598 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5599 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5600
5601   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5602
5603 ;  (define-prefix-command 'gnus-summary-sort-map)
5604 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5605 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5606 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5607 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5608 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5609 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5610
5611   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5612   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5613   )
5614
5615
5616 \f
5617
5618 (defun gnus-summary-mode (&optional group)
5619   "Major mode for reading articles.
5620
5621 All normal editing commands are switched off.
5622 \\<gnus-summary-mode-map>
5623 Each line in this buffer represents one article.  To read an
5624 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5625 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5626 respectively.
5627
5628 You can also post articles and send mail from this buffer.  To 
5629 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5630 of an article, type `\\[gnus-summary-reply]'.
5631
5632 There are approx. one gazillion commands you can execute in this 
5633 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5634
5635 The following commands are available:
5636
5637 \\{gnus-summary-mode-map}"
5638   (interactive)
5639   (if gnus-visual (gnus-summary-make-menu-bar))
5640   (kill-all-local-variables)
5641   (let ((locals gnus-summary-local-variables))
5642     (while locals
5643       (if (consp (car locals))
5644           (progn
5645             (make-local-variable (car (car locals)))
5646             (set (car (car locals)) (eval (cdr (car locals)))))
5647         (make-local-variable (car locals))
5648         (set (car locals) nil))
5649       (setq locals (cdr locals))))
5650   (gnus-make-thread-indent-array)
5651   (gnus-simplify-mode-line)
5652   (setq major-mode 'gnus-summary-mode)
5653   (setq mode-name "Summary")
5654   (make-local-variable 'minor-mode-alist)
5655   (use-local-map gnus-summary-mode-map)
5656   (buffer-disable-undo (current-buffer))
5657   (setq buffer-read-only t)             ;Disable modification
5658   (setq truncate-lines t)
5659   (setq selective-display t)
5660   (setq selective-display-ellipses t)   ;Display `...'
5661   (setq buffer-display-table gnus-summary-display-table)
5662   (setq gnus-newsgroup-name group)
5663   (run-hooks 'gnus-summary-mode-hook))
5664
5665 (defun gnus-summary-make-display-table ()
5666   ;; Change the display table.  Odd characters have a tendency to mess
5667   ;; up nicely formatted displays - we make all possible glyphs
5668   ;; display only a single character.
5669
5670   ;; We start from the standard display table, if any.
5671   (setq gnus-summary-display-table 
5672         (or (copy-sequence standard-display-table)
5673             (make-display-table)))
5674   ;; Nix out all the control chars...
5675   (let ((i 32))
5676     (while (>= (setq i (1- i)) 0)
5677       (aset gnus-summary-display-table i [??])))
5678   ;; ... but not newline and cr, of course. (cr is necessary for the
5679   ;; selective display).  
5680   (aset gnus-summary-display-table ?\n nil)
5681   (aset gnus-summary-display-table ?\r nil)
5682   ;; We nix out any glyphs over 126 that are not set already.  
5683   (let ((i 256))
5684     (while (>= (setq i (1- i)) 127)
5685       ;; Only modify if the entry is nil.
5686       (or (aref gnus-summary-display-table i) 
5687           (aset gnus-summary-display-table i [??])))))
5688
5689 (defun gnus-summary-clear-local-variables ()
5690   (let ((locals gnus-summary-local-variables))
5691     (while locals
5692       (if (consp (car locals))
5693           (and (vectorp (car (car locals)))
5694                (set (car (car locals)) nil))
5695         (and (vectorp (car locals))
5696              (set (car locals) nil)))
5697       (setq locals (cdr locals)))))
5698
5699 ;; Some summary mode macros.
5700
5701 ;; Return a header specified by a NUMBER.
5702 (defun gnus-get-header-by-number (number)
5703   (save-excursion
5704     (set-buffer gnus-summary-buffer)
5705     (or gnus-newsgroup-headers-hashtb-by-number
5706         (gnus-make-headers-hashtable-by-number))
5707     (gnus-gethash (int-to-string number)
5708                   gnus-newsgroup-headers-hashtb-by-number)))
5709
5710 ;; Fast version of the function above.
5711 (defmacro gnus-get-header-by-num (number)
5712   (` (gnus-gethash (int-to-string (, number)) 
5713                    gnus-newsgroup-headers-hashtb-by-number)))
5714
5715 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5716   "Search for article forward.
5717 If UNREAD is non-nil, only unread articles are selected.
5718 If SUBJECT is non-nil, the article which has the same subject will be
5719 searched for. 
5720 If BACKWARD is non-nil, the search will be performed backwards instead."
5721   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5722
5723 (defmacro gnus-summary-search-backward (&optional unread subject)
5724   "Search for article backward.
5725 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5726 If 2nd optional argument SUBJECT is non-nil, the article which has
5727 the same subject will be searched for."
5728   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5729
5730 (defmacro gnus-summary-article-number (&optional number-or-nil)
5731   "The article number of the article on the current line.
5732 If there isn's an article number here, then we return the current
5733 article number."
5734   (if number-or-nil
5735       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5736     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5737          gnus-current-article)))
5738
5739 (defmacro gnus-summary-thread-level ()
5740   "The thread level of the article on the current line."
5741   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5742        0))
5743
5744 (defmacro gnus-summary-article-mark ()
5745   "The mark on the current line."
5746   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5747
5748 (defun gnus-summary-subject-string ()
5749   "Return current subject string or nil if nothing."
5750   (let ((article (gnus-summary-article-number))
5751         header)
5752     (and article 
5753          (setq header (gnus-get-header-by-num article))
5754          (vectorp header)
5755          (mail-header-subject header))))
5756
5757 ;; Various summary mode internalish functions.
5758
5759 (defun gnus-mouse-pick-article (e)
5760   (interactive "e")
5761   (mouse-set-point e)
5762   (gnus-summary-next-page nil t))
5763
5764 (defun gnus-summary-setup-buffer (group)
5765   "Initialize summary buffer."
5766   (let ((buffer (concat "*Summary " group "*")))
5767     (if (get-buffer buffer)
5768         (progn
5769           (set-buffer buffer)
5770           (not gnus-newsgroup-begin))
5771       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5772       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5773       (gnus-add-current-to-buffer-list)
5774       (gnus-summary-mode group)
5775       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5776       (setq gnus-newsgroup-name group)
5777       t)))
5778
5779 (defun gnus-set-global-variables ()
5780   ;; Set the global equivalents of the summary buffer-local variables
5781   ;; to the latest values they had. These reflect the summary buffer
5782   ;; that was in action when the last article was fetched.
5783   (if (eq major-mode 'gnus-summary-mode) 
5784       (progn
5785         (setq gnus-summary-buffer (current-buffer))
5786         (let ((name gnus-newsgroup-name)
5787               (marked gnus-newsgroup-marked)
5788               (unread gnus-newsgroup-unreads)
5789               (headers gnus-current-headers)
5790               (score-file gnus-current-score-file))
5791           (save-excursion
5792             (set-buffer gnus-group-buffer)
5793             (setq gnus-newsgroup-name name)
5794             (setq gnus-newsgroup-marked marked)
5795             (setq gnus-newsgroup-unreads unread)
5796             (setq gnus-current-headers headers)
5797             (setq gnus-current-score-file score-file))))))
5798
5799 (defun gnus-summary-insert-dummy-line (sformat subject number)
5800   (if (not sformat) 
5801       (setq sformat gnus-summary-dummy-line-format-spec))
5802   (let (b)
5803     (beginning-of-line)
5804     (setq b (point))
5805     (insert (eval sformat))
5806     (add-text-properties
5807      b (1+ b)
5808      (list 'gnus-number number 
5809            'gnus-mark gnus-dummy-mark
5810            'gnus-level 0))))
5811
5812 (defvar gnus-thread-indent-array nil)
5813 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5814 (defun gnus-make-thread-indent-array ()
5815   (let ((n 200))
5816     (if (and gnus-thread-indent-array
5817              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5818         nil
5819       (setq gnus-thread-indent-array (make-vector 201 "")
5820             gnus-thread-indent-array-level gnus-thread-indent-level)
5821       (while (>= n 0)
5822         (aset gnus-thread-indent-array n
5823               (make-string (* n gnus-thread-indent-level) ? ))
5824         (setq n (1- n))))))
5825
5826 (defun gnus-summary-insert-line 
5827   (sformat header level current unread replied expirable subject-or-nil
5828            &optional dummy score process)
5829   (or sformat (setq sformat gnus-summary-line-format-spec))
5830   (let* ((indentation (aref gnus-thread-indent-array level))
5831          (lines (mail-header-lines header))
5832          (score (or score gnus-summary-default-score 0))
5833          (score-char
5834           (if (or (null gnus-summary-default-score)
5835                   (<= (abs (- score gnus-summary-default-score))
5836                       gnus-summary-zcore-fuzz)) ? 
5837             (if (< score gnus-summary-default-score)
5838                 gnus-score-below-mark gnus-score-over-mark)))
5839          (replied (cond (process gnus-process-mark)
5840                         (replied gnus-replied-mark)
5841                         (t gnus-unread-mark)))
5842          (from (mail-header-from header))
5843          (name (cond 
5844                 ((string-match "(.+)" from)
5845                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5846                 ((string-match "<[^>]+> *$" from)
5847                  (let ((beg (match-beginning 0)))
5848                    (or (and (string-match "^\"[^\"]*\"" from)
5849                             (substring from (1+ (match-beginning 0))
5850                                        (1- (match-end 0))))
5851                        (substring from 0 beg))))
5852                 (t from)))
5853          (subject (mail-header-subject header))
5854          (number (mail-header-number header))
5855          (opening-bracket (if dummy ?\< ?\[))
5856          (closing-bracket (if dummy ?\> ?\]))
5857          (buffer-read-only nil)
5858          (b (progn (beginning-of-line) (point))))
5859     (or (numberp lines) (setq lines 0))
5860     (insert (eval sformat))
5861     (add-text-properties
5862      b (1+ b) (list 'gnus-number number 
5863                     'gnus-mark (or unread gnus-unread-mark)
5864                     'gnus-level level))))
5865
5866 (defun gnus-summary-update-line (&optional dont-update)
5867   ;; Update summary line after change.
5868   (or (not gnus-summary-default-score)
5869       gnus-summary-inhibit-highlight
5870       (let ((gnus-summary-inhibit-highlight t)
5871             (article (gnus-summary-article-number)))
5872         (progn
5873           (or dont-update
5874               (if (and gnus-summary-mark-below
5875                        (< (gnus-summary-article-score)
5876                           gnus-summary-mark-below))
5877                   (and (not (memq article gnus-newsgroup-marked))
5878                        (not (memq article gnus-newsgroup-dormant))
5879                        (memq article gnus-newsgroup-unreads)
5880                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5881                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5882                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5883           (and gnus-visual
5884                (run-hooks 'gnus-summary-update-hook))))))
5885
5886 (defun gnus-summary-update-lines (&optional beg end)
5887   ;; Mark article as read (or not) by taking into account scores.
5888   (let ((beg (or beg (point-min)))
5889         (end (or end (point-max))))
5890     (if (or (not gnus-summary-default-score)
5891             gnus-summary-inhibit-highlight)
5892         ()
5893       (let ((gnus-summary-inhibit-highlight t)
5894             article)
5895         (save-excursion
5896           (set-buffer gnus-summary-buffer)
5897           (goto-char beg)
5898           (beginning-of-line)
5899           (while (and (not (eobp)) (< (point) end))
5900             (if (and gnus-summary-mark-below
5901                      (< (or (cdr (assq 
5902                                   (setq article (get-text-property 
5903                                                  (point) 'gnus-number))
5904                                   gnus-newsgroup-scored))
5905                             gnus-summary-default-score 0)
5906                         gnus-summary-mark-below))
5907                 ;; We want to possibly mark it as read...
5908                 (and (not (memq article gnus-newsgroup-marked))
5909                      (not (memq article gnus-newsgroup-dormant))
5910                      (memq article gnus-newsgroup-unreads)
5911                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5912               ;; We want to possibly mark it as unread.
5913               (and (eq (get-text-property (point) 'gnus-mark)
5914                        gnus-low-score-mark)
5915                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5916             ;; Do the visual highlights at the same time.
5917             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5918             (forward-line 1)))))))
5919
5920 (defvar gnus-tmp-gathered nil)
5921
5922 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5923   ;; Sum up all elements (and sub-elements) in a list.
5924   (let* ((number
5925           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5926           (if (and (consp thread) (cdr thread))
5927               (apply
5928                '+ 1 (mapcar
5929                      'gnus-summary-number-of-articles-in-thread 
5930                      (cdr thread)))
5931             1)))
5932     (if char 
5933         (if (> number 1) gnus-not-empty-thread-mark
5934           gnus-empty-thread-mark)
5935       number)))
5936
5937 (defun gnus-summary-read-group 
5938   (group &optional show-all no-article kill-buffer)
5939   "Start reading news in newsgroup GROUP.
5940 If SHOW-ALL is non-nil, already read articles are also listed.
5941 If NO-ARTICLE is non-nil, no article is selected initially."
5942   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5943   (let* ((new-group (gnus-summary-setup-buffer group))
5944          (quit-config (gnus-group-quit-config group))
5945          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5946     (cond 
5947      ((not new-group)
5948       (gnus-set-global-variables)
5949       (gnus-kill-buffer kill-buffer)
5950       (gnus-configure-windows 'summary 'force)
5951       (gnus-set-mode-line 'summary)
5952       (gnus-summary-position-cursor)
5953       (message "")
5954       t)
5955      ((null did-select) 
5956       (and (eq major-mode 'gnus-summary-mode)
5957            (not (equal (current-buffer) kill-buffer))
5958            (progn
5959              (kill-buffer (current-buffer))
5960              (if (not quit-config)
5961                  (progn
5962                    (set-buffer gnus-group-buffer)
5963                    (gnus-group-jump-to-group group)
5964                    (gnus-group-next-unread-group 1))
5965                (if (not (buffer-name (car quit-config)))
5966                    (gnus-configure-windows 'group 'force)
5967                  (set-buffer (car quit-config))
5968                  (and (eq major-mode 'gnus-summary-mode)
5969                       (gnus-set-global-variables))
5970                  (gnus-configure-windows (cdr quit-config))))))
5971       (message "Can't select group")
5972       nil)
5973      ((eq did-select 'quit)
5974       (and (eq major-mode 'gnus-summary-mode)
5975            (not (equal (current-buffer) kill-buffer))
5976            (kill-buffer (current-buffer)))
5977       (gnus-kill-buffer kill-buffer)
5978       (if (not quit-config)
5979           (progn
5980             (set-buffer gnus-group-buffer)
5981             (gnus-group-jump-to-group group)
5982             (gnus-group-next-unread-group 1)
5983             (gnus-configure-windows 'group 'force))
5984         (if (not (buffer-name (car quit-config)))
5985             (gnus-configure-windows 'group 'force)
5986           (set-buffer (car quit-config))
5987           (and (eq major-mode 'gnus-summary-mode)
5988                (gnus-set-global-variables))
5989           (gnus-configure-windows (cdr quit-config))))
5990       (signal 'quit nil))
5991      (t
5992       (gnus-set-global-variables)
5993       ;; Save the active value in effect when the group was entered.
5994       (setq gnus-newsgroup-active 
5995             (gnus-copy-sequence
5996              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5997       ;; You can change the subjects in this hook.
5998       (run-hooks 'gnus-select-group-hook)
5999       ;; Do score processing.
6000       (and gnus-use-scoring (gnus-possibly-score-headers))
6001       (gnus-update-format-specifications)
6002       ;; Generate the summary buffer.
6003       (gnus-summary-prepare)
6004       (if (zerop (buffer-size))
6005           (cond (gnus-newsgroup-dormant
6006                  (gnus-summary-show-all-dormant))
6007                 ((and gnus-newsgroup-scored show-all)
6008                  (gnus-summary-show-all-expunged))))
6009       ;; Function `gnus-apply-kill-file' must be called in this hook.
6010       (run-hooks 'gnus-apply-kill-hook)
6011       (if (zerop (buffer-size))
6012           (progn
6013             ;; This newsgroup is empty.
6014             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
6015             (gnus-message 6 "No unread news")
6016             (gnus-kill-buffer kill-buffer)
6017             nil)
6018         ;;(save-excursion
6019         ;;  (if kill-buffer
6020         ;;      (let ((gnus-summary-buffer kill-buffer))
6021         ;;      (gnus-configure-windows 'group))))
6022         ;; Hide conversation thread subtrees.  We cannot do this in
6023         ;; gnus-summary-prepare-hook since kill processing may not
6024         ;; work with hidden articles.
6025         (and gnus-show-threads
6026              gnus-thread-hide-subtree
6027              (gnus-summary-hide-all-threads))
6028         ;; Show first unread article if requested.
6029         (goto-char (point-min))
6030         (if (and (not no-article)
6031                  gnus-auto-select-first
6032                  (gnus-summary-first-unread-article))
6033             ()
6034           (gnus-configure-windows 'summary 'force))
6035         (gnus-set-mode-line 'summary)
6036         (gnus-summary-position-cursor)
6037         ;; If in async mode, we send some info to the backend.
6038         (and gnus-newsgroup-async
6039              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
6040              (gnus-request-asynchronous 
6041               gnus-newsgroup-name
6042               (if (and gnus-asynchronous-article-function
6043                        (fboundp gnus-asynchronous-article-function))
6044                   (funcall gnus-asynchronous-article-function
6045                            gnus-newsgroup-threads)
6046                 gnus-newsgroup-threads)))
6047         (gnus-kill-buffer kill-buffer)
6048         (if (not (get-buffer-window gnus-group-buffer))
6049             ()
6050           ;; gotta use windows, because recenter does wierd stuff if
6051           ;; the current buffer ain't the displayed window.
6052           (let ((owin (selected-window))) 
6053             (select-window (get-buffer-window gnus-group-buffer))
6054             (and (gnus-group-goto-group group)
6055                  (recenter))
6056             (select-window owin))))
6057       t))))
6058
6059 (defun gnus-summary-prepare ()
6060   ;; Generate the summary buffer.
6061   (let ((buffer-read-only nil))
6062     (erase-buffer)
6063     (gnus-summary-prepare-threads 
6064      (if gnus-show-threads
6065          (gnus-gather-threads 
6066           (gnus-sort-threads 
6067            (if (and gnus-summary-expunge-below
6068                     (not gnus-fetch-old-headers))
6069                (gnus-make-threads-and-expunge)
6070              (gnus-make-threads))))
6071        gnus-newsgroup-headers)
6072      'cull)
6073     (gnus-summary-update-lines)
6074     ;; Create the header hashtb.
6075     (gnus-make-headers-hashtable-by-number)
6076     ;; Call hooks for modifying summary buffer.
6077     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
6078     (goto-char (point-min))
6079     (run-hooks 'gnus-summary-prepare-hook)))
6080
6081 (defun gnus-gather-threads (threads)
6082   "Gather threads that have lost their roots."
6083   (if (not gnus-summary-make-false-root)
6084       threads 
6085     (let ((hashtb (gnus-make-hashtable 1023))
6086           (prev threads)
6087           (result threads)
6088           subject hthread whole-subject)
6089       (while threads
6090         (setq whole-subject 
6091               (setq subject (mail-header-subject (car (car threads)))))
6092         (if gnus-summary-gather-subject-limit
6093             (or (and (numberp gnus-summary-gather-subject-limit)
6094                      (> (length subject) gnus-summary-gather-subject-limit)
6095                      (setq subject
6096                            (substring subject 0 
6097                                       gnus-summary-gather-subject-limit)))
6098                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
6099                      (setq subject (gnus-simplify-subject-fuzzy subject))))
6100           (setq subject (gnus-simplify-subject-re subject)))
6101         (if (setq hthread 
6102                   (gnus-gethash subject hashtb))
6103             (progn
6104               (or (stringp (car (car hthread)))
6105                   (setcar hthread (list whole-subject (car hthread))))
6106               (setcdr (car hthread) (nconc (cdr (car hthread)) 
6107                                            (list (car threads))))
6108               (setcdr prev (cdr threads))
6109               (setq threads prev))
6110           (gnus-sethash subject threads hashtb))
6111         (setq prev threads)
6112         (setq threads (cdr threads)))
6113       result)))
6114
6115 (defun gnus-make-threads ()
6116   ;; This function takes the dependencies already made by 
6117   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6118   ;; through the dependecies in the hash table and finds all the
6119   ;; roots. Roots do not refer back to any valid articles.
6120   (gnus-message 6 "Threading...")
6121   (let (roots new-roots)
6122     (and gnus-fetch-old-headers
6123          (eq gnus-headers-retrieved-by 'nov)
6124          (gnus-build-old-threads))
6125     (mapatoms
6126      (lambda (refs)
6127        (if (not (car (symbol-value refs)))
6128            (setq roots (append (cdr (symbol-value refs)) roots))
6129          ;; Ok, these refer back to valid articles, but if
6130          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6131          ;; the root has the same subject as its children. The children
6132          ;; that do not are made into roots and removed from the list
6133          ;; of children. 
6134          (or gnus-thread-ignore-subject
6135              (let* ((prev (symbol-value refs))
6136                     (subject (gnus-simplify-subject-re 
6137                               (mail-header-subject (car prev))))
6138                     (headers (cdr prev)))
6139                (while headers
6140                  (if (not (string= subject
6141                                    (gnus-simplify-subject-re 
6142                                     (mail-header-subject (car headers)))))
6143                      (progn
6144                        (setq new-roots (cons (car headers) new-roots))
6145                        (setcdr prev (cdr headers)))
6146                    (setq prev headers))
6147                  (setq headers (cdr headers)))))))
6148      gnus-newsgroup-dependencies)
6149
6150     ;; We enter the new roots into the dependencies structure to
6151     ;; ensure that any possible later thread-regeneration will be
6152     ;; possible. 
6153     (let ((r new-roots))
6154       (while r
6155         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6156                       (list nil (car r)) gnus-newsgroup-dependencies)
6157         (setq r (cdr r))))
6158
6159     (setq roots (nconc new-roots roots))
6160
6161     (prog1
6162         (mapcar 'gnus-trim-thread
6163                 (apply 'append
6164                        (mapcar 'gnus-cut-thread
6165                                (mapcar 'gnus-make-sub-thread roots))))
6166       (gnus-message 6 "Threading...done"))))
6167
6168   
6169 (defun gnus-make-threads-and-expunge ()
6170   ;; This function takes the dependencies already made by 
6171   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6172   ;; through the dependecies in the hash table and finds all the
6173   ;; roots. Roots do not refer back to any valid articles.
6174   (gnus-message 6 "Threading...")
6175   (let ((default (or gnus-summary-default-score 0))
6176         (below gnus-summary-expunge-below)
6177         roots article new-roots)
6178     (and gnus-fetch-old-headers
6179          (eq gnus-headers-retrieved-by 'nov)
6180          (gnus-build-old-threads))
6181     (mapatoms
6182      (lambda (refs)
6183        (if (not (car (symbol-value refs)))
6184            ;; These articles do not refer back to any other articles -
6185            ;; they are roots.
6186            (let ((headers (cdr (symbol-value refs))))
6187              ;; We weed out the low-scored articles.
6188              (while headers
6189                (if (not (< (or (cdr (assq (mail-header-number (car headers))
6190                                           gnus-newsgroup-scored)) default)
6191                            below))
6192                    ;; It is over.
6193                    (setq roots (cons (car headers) roots))
6194                  ;; It is below, so we mark it as read.
6195                  (setq gnus-newsgroup-unreads
6196                        (delq (mail-header-number (car headers))
6197                              gnus-newsgroup-unreads))
6198                  (setq gnus-newsgroup-reads 
6199                        (cons (cons (mail-header-number (car headers))
6200                                    gnus-low-score-mark) 
6201                              gnus-newsgroup-reads)))
6202                (setq headers (cdr headers))))
6203          ;; Ok, these refer back to valid articles, but if
6204          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6205          ;; the root has the same subject as its children. The children
6206          ;; that do not are made into roots and removed from the list
6207          ;; of children. 
6208          (or gnus-thread-ignore-subject
6209              (let* ((prev (symbol-value refs))
6210                     (subject (gnus-simplify-subject-re 
6211                               (mail-header-subject (car prev))))
6212                     (headers (cdr prev)))
6213                (while headers
6214                  (if (not (string= subject
6215                                    (gnus-simplify-subject-re 
6216                                     (mail-header-subject (car headers)))))
6217                      (progn
6218                        (if (not (< (or (cdr (assq (mail-header-number
6219                                                    (car headers))
6220                                                   gnus-newsgroup-scored))
6221                                        default) below))
6222                            (setq new-roots (cons (car headers) new-roots))
6223                          (setq gnus-newsgroup-unreads
6224                                (delq (mail-header-number (car headers))
6225                                      gnus-newsgroup-unreads))
6226                          (setq gnus-newsgroup-reads
6227                                (cons (cons (mail-header-number (car headers)) 
6228                                            gnus-low-score-mark) 
6229                                      gnus-newsgroup-reads)))
6230                        (setcdr prev (cdr headers)))
6231                    (setq prev headers))
6232                  (setq headers (cdr headers)))))
6233          ;; If this article is expunged, some of the children might be
6234          ;; roots.  
6235          (if (< (or (cdr (assq (mail-header-number (car (symbol-value refs)))
6236                                gnus-newsgroup-scored)) default)
6237                 below)
6238              (let* ((prev (symbol-value refs))
6239                     (headers (cdr prev)))
6240                (while headers
6241                  (setq article (mail-header-number (car headers)))
6242                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6243                                  default) below))
6244                      (progn (setq new-roots (cons (car headers) new-roots))
6245                             (setq prev headers))
6246                    (setq gnus-newsgroup-unreads 
6247                          (delq article gnus-newsgroup-unreads))
6248                    (setq gnus-newsgroup-reads 
6249                          (cons (cons article gnus-low-score-mark) 
6250                                gnus-newsgroup-reads))
6251                    (setcdr prev (cdr headers)))
6252                  (setq headers (cdr headers))))
6253            ;; It was not expunged, but we look at expunged children.
6254            (let* ((prev (symbol-value refs))
6255                   (headers (cdr prev))
6256                   article)
6257              (while headers
6258                (setq article (mail-header-number (car headers)))
6259                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6260                                default) below))
6261                    (setq prev headers)
6262                  (setq gnus-newsgroup-unreads 
6263                        (delq article gnus-newsgroup-unreads))
6264                  (setq gnus-newsgroup-reads 
6265                        (cons (cons article gnus-low-score-mark)
6266                              gnus-newsgroup-reads))
6267                  (setcdr prev (cdr headers)))
6268                (setq headers (cdr headers)))))))
6269      gnus-newsgroup-dependencies)
6270
6271     ;; We enter the new roots into the dependencies structure to
6272     ;; ensure that any possible later thread-regeneration will be
6273     ;; possible. 
6274     (let ((r new-roots))
6275       (while r
6276         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6277                       (list nil (car r)) gnus-newsgroup-dependencies)
6278         (setq r (cdr r))))
6279
6280     (setq roots (nconc new-roots roots))
6281
6282     (prog1
6283         (mapcar 'gnus-trim-thread
6284                 (apply 'append
6285                        (mapcar 'gnus-cut-thread
6286                                (mapcar 'gnus-make-sub-thread roots))))
6287       (gnus-message 6 "Threading...done"))))
6288
6289   
6290 (defun gnus-cut-thread (thread)
6291   ;; Remove leaf dormant or ancient articles from THREAD.
6292   (let ((head (car thread))
6293         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6294     (if (and (null tail)
6295              (let ((number (mail-header-number head)))
6296                (or (memq number gnus-newsgroup-ancient)
6297                    (memq number gnus-newsgroup-dormant)
6298                    (and gnus-summary-expunge-below
6299                         (eq gnus-fetch-old-headers 'some)
6300                         (< (or (cdr (assq number gnus-newsgroup-scored))
6301                                gnus-summary-default-score 0)
6302                            gnus-summary-expunge-below)
6303                         (progn
6304                           (setq gnus-newsgroup-unreads
6305                                 (delq number gnus-newsgroup-unreads))
6306                           (setq gnus-newsgroup-reads
6307                                 (cons (cons number gnus-low-score-mark)
6308                                       gnus-newsgroup-reads))
6309                           t)))))
6310         nil
6311       (list (cons head tail)))))
6312
6313 (defun gnus-trim-thread (thread)
6314   ;; Remove root ancient articles with only one child from THREAD.
6315   (if (and (eq gnus-fetch-old-headers 'some)
6316            (memq (mail-header-number (car thread)) gnus-newsgroup-ancient)
6317            (= (length thread) 2))
6318       (gnus-trim-thread (nth 1 thread))
6319     thread))
6320
6321 (defun gnus-make-sub-thread (root)
6322   ;; This function makes a sub-tree for a node in the tree.
6323   (let ((children (reverse (cdr (gnus-gethash (downcase (mail-header-id root))
6324                                               gnus-newsgroup-dependencies)))))
6325     (cons root (mapcar 'gnus-make-sub-thread children))))
6326
6327 (defun gnus-build-old-threads ()
6328   ;; Look at all the articles that refer back to old articles, and
6329   ;; fetch the headers for the articles that aren't there. This will
6330   ;; build complete threads - if the roots haven't been expired by the
6331   ;; server, that is.
6332   (let (id heads)
6333     (mapatoms
6334      (lambda (refs)
6335        (if (not (car (symbol-value refs)))
6336            (progn
6337              (setq heads (cdr (symbol-value refs)))
6338              (while heads
6339                (if (not (memq (mail-header-number (car heads))
6340                               gnus-newsgroup-dormant))
6341                    (progn
6342                      (setq id (symbol-name refs))
6343                      (while (and (setq id (gnus-build-get-header id))
6344                                  (not (car (gnus-gethash 
6345                                             id gnus-newsgroup-dependencies)))))
6346                      (setq heads nil))
6347                  (setq heads (cdr heads)))))))
6348      gnus-newsgroup-dependencies)))
6349
6350 (defun gnus-build-get-header (id)
6351   ;; Look through the buffer of NOV lines and find the header to
6352   ;; ID. Enter this line into the dependencies hash table, and return
6353   ;; the id of the parent article (if any).
6354   (let ((deps gnus-newsgroup-dependencies)
6355         found header)
6356     (prog1
6357         (save-excursion
6358           (set-buffer nntp-server-buffer)
6359           (goto-char (point-min))
6360           (while (and (not found) (search-forward id nil t))
6361             (beginning-of-line)
6362             (setq found (looking-at 
6363                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6364                                  (regexp-quote id))))
6365             (or found (beginning-of-line 2)))
6366           (if found
6367               (let (ref)
6368                 (beginning-of-line)
6369                 (and
6370                  (setq header (gnus-nov-parse-line 
6371                                (read (current-buffer)) deps))
6372                  (setq ref (mail-header-references header))
6373                  (string-match "\\(<[^>]+>\\) *$" ref)
6374                  (substring ref (match-beginning 1) (match-end 1))))))
6375       (and header
6376            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6377                  gnus-newsgroup-ancient (cons (mail-header-number header)
6378                                               gnus-newsgroup-ancient))))))
6379
6380 ;; Re-build the thread containing ID.
6381 (defun gnus-rebuild-thread (id)
6382   (let ((dep gnus-newsgroup-dependencies)
6383         (buffer-read-only nil)
6384         parent headers refs thread art)
6385     (while (and id (setq headers
6386                          (car (setq art (gnus-gethash (downcase id) dep)))))
6387       (setq parent art)
6388       (setq id (and (setq refs (mail-header-references headers))
6389                     (string-match "\\(<[^>]+>\\) *$" refs)
6390                     (substring refs (match-beginning 1) (match-end 1)))))
6391     (setq thread (gnus-make-sub-thread (car parent)))
6392     (gnus-rebuild-remove-articles thread)
6393     (let ((beg (point)))
6394       (gnus-summary-prepare-threads (list thread))
6395       (gnus-summary-update-lines beg (point)))))
6396
6397 ;; Delete all lines in the summary buffer that correspond to articles
6398 ;; in this thread.
6399 (defun gnus-rebuild-remove-articles (thread)
6400   (and (gnus-summary-goto-subject (mail-header-number (car thread)))
6401        (gnus-delete-line))
6402   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6403
6404 (defun gnus-sort-threads (threads)
6405   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6406   (let ((fun gnus-thread-sort-functions))
6407     (while fun
6408       (gnus-message 6 "Sorting with %S..." fun)
6409       (setq threads (sort threads (car fun))
6410             fun (cdr fun))))
6411   (if gnus-thread-sort-functions
6412       (gnus-message 6 "Sorting...done"))
6413   threads)
6414
6415 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6416 (defmacro gnus-thread-header (thread)
6417   ;; Return header of first article in THREAD.
6418   ;; Note that THREAD must never, evr be anything else than a variable -
6419   ;; using some other form will lead to serious barfage.
6420   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6421   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6422   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6423         (vector thread) 2))
6424
6425 (defun gnus-thread-sort-by-number (h1 h2)
6426   "Sort threads by root article number."
6427   (< (mail-header-number (gnus-thread-header h1))
6428      (mail-header-number (gnus-thread-header h2))))
6429
6430 (defun gnus-thread-sort-by-author (h1 h2)
6431   "Sort threads by root author."
6432   (string-lessp
6433    (let ((extract (funcall 
6434                    gnus-extract-address-components
6435                    (mail-header-from (gnus-thread-header h1)))))
6436      (or (car extract) (cdr extract)))
6437    (let ((extract (funcall
6438                    gnus-extract-address-components 
6439                    (mail-header-from (gnus-thread-header h2)))))
6440      (or (car extract) (cdr extract)))))
6441
6442 (defun gnus-thread-sort-by-subject (h1 h2)
6443   "Sort threads by root subject."
6444   (string-lessp
6445    (downcase (gnus-simplify-subject-re
6446               (mail-header-subject (gnus-thread-header h1))))
6447    (downcase (gnus-simplify-subject-re 
6448               (mail-header-subject (gnus-thread-header h2))))))
6449
6450 (defun gnus-thread-sort-by-date (h1 h2)
6451   "Sort threads by root article date."
6452   (string-lessp
6453    (gnus-sortable-date (mail-header-date (gnus-thread-header h1)))
6454    (gnus-sortable-date (mail-header-date (gnus-thread-header h2)))))
6455
6456 (defun gnus-thread-sort-by-score (h1 h2)
6457   "Sort threads by root article score.
6458 Unscored articles will be counted as having a score of zero."
6459   (> (or (cdr (assq (mail-header-number (gnus-thread-header h1))
6460                     gnus-newsgroup-scored))
6461          gnus-summary-default-score 0)
6462      (or (cdr (assq (mail-header-number (gnus-thread-header h2))
6463                     gnus-newsgroup-scored))
6464          gnus-summary-default-score 0)))
6465
6466 (defun gnus-thread-sort-by-total-score (h1 h2)
6467   "Sort threads by the sum of all scores in the thread.
6468 Unscored articles will be counted as having a score of zero."
6469   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6470
6471 (defun gnus-thread-total-score (thread)
6472   ;;  This function find the total score of THREAD.
6473   (if (consp thread)
6474       (if (stringp (car thread))
6475           (apply gnus-thread-score-function 0
6476                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6477         (gnus-thread-total-score-1 thread))
6478     (gnus-thread-total-score-1 (list thread))))
6479
6480 (defun gnus-thread-total-score-1 (root)
6481   ;; This function find the total score of the thread below ROOT.
6482   (setq root (car root))
6483   (apply gnus-thread-score-function
6484          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
6485              gnus-summary-default-score 0)
6486          (mapcar 'gnus-thread-total-score
6487                  (cdr (gnus-gethash (downcase (mail-header-id root))
6488                                     gnus-newsgroup-dependencies)))))
6489
6490 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6491 (defvar gnus-tmp-prev-subject "")
6492
6493 (defun gnus-summary-prepare-threads (threads &optional cull)
6494   "Prepare summary buffer from THREADS and indentation LEVEL.  
6495 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6496 or a straight list of headers."
6497   (message "Generating summary...")
6498   (let ((level 0)
6499         thread header number subject stack state gnus-tmp-gathered)
6500     (if (vectorp (car threads))
6501         ;; If this is a straight (sic) list of headers, then a
6502         ;; threaded summary display isn't required, so we just create
6503         ;; an unthreaded one.
6504         (gnus-summary-prepare-unthreaded threads cull)
6505
6506       ;; Do the threaded display.
6507
6508       (while (or threads stack)
6509         
6510         (if threads
6511             ;; If there are some threads, we do them before the
6512             ;; threads on the stack.
6513             (setq thread threads
6514                   header (car (car thread)))
6515           ;; There were no current threads, so we pop something off
6516           ;; the stack. 
6517           (setq state (car stack)
6518                 level (car state)
6519                 thread (cdr state)
6520                 stack (cdr stack)
6521                 header (car (car thread))))
6522
6523         (if (stringp header)
6524             (progn
6525               ;; The header is a dummy root.
6526               (cond 
6527                ((eq gnus-summary-make-false-root 'adopt)
6528                 ;; We let the first article adopt the rest.
6529                 (let ((th (car (cdr (car thread)))))
6530                   (while (cdr th)
6531                     (setq th (cdr th)))
6532                   (setcdr th (cdr (cdr (car thread))))
6533                   (setq gnus-tmp-gathered 
6534                         (nconc (mapcar
6535                                 (lambda (h) (mail-header-number (car h)))
6536                                 (cdr (cdr (car thread))))
6537                                gnus-tmp-gathered))
6538                   (setcdr (cdr (car thread)) nil))
6539                 (setq level -1))
6540                ((eq gnus-summary-make-false-root 'empty)
6541                 ;; We print adopted articles with empty subject fields.
6542                 (setq gnus-tmp-gathered 
6543                       (nconc (mapcar
6544                               (lambda (h) (mail-header-number (car h)))
6545                               (cdr (cdr (car thread))))
6546                              gnus-tmp-gathered))
6547                 (setq level -1))
6548                ((eq gnus-summary-make-false-root 'dummy)
6549                 ;; We output a dummy root.
6550                 (gnus-summary-insert-dummy-line 
6551                  nil header (mail-header-number
6552                              (car (car (cdr (car thread)))))))
6553                (t
6554                 ;; We do not make a root for the gathered
6555                 ;; sub-threads at all.  
6556                 (setq level -1))))
6557       
6558           (setq number (mail-header-number header)
6559                 subject (mail-header-subject header))
6560
6561           ;; Do the async thing.
6562           (and gnus-newsgroup-async
6563                (setq gnus-newsgroup-threads
6564                      (cons (cons number (mail-header-lines header)) 
6565                            gnus-newsgroup-threads)))
6566
6567           ;; We may have to root out some bad articles...
6568           (and cull
6569                (= level 0)
6570                (cond ((and (memq (setq number (mail-header-number header))
6571                                  gnus-newsgroup-dormant)
6572                            (null thread))
6573                       (setq header nil))
6574                      ((and gnus-summary-expunge-below
6575                            (< (or (cdr (assq number gnus-newsgroup-scored))
6576                                   gnus-summary-default-score 0)
6577                               gnus-summary-expunge-below))
6578                       (setq header nil)
6579                       (setq gnus-newsgroup-unreads 
6580                             (delq number gnus-newsgroup-unreads))
6581                       (setq gnus-newsgroup-reads
6582                             (cons (cons number gnus-low-score-mark)
6583                                   gnus-newsgroup-reads)))))
6584           
6585           (and
6586            header
6587            (progn
6588              (inline
6589                (gnus-summary-insert-line
6590                 nil header level nil 
6591                 (cond 
6592                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6593                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6594                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6595                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6596                  (t (or (cdr (assq number gnus-newsgroup-reads))
6597                         gnus-ancient-mark)))
6598                 (memq number gnus-newsgroup-replied)
6599                 (memq number gnus-newsgroup-expirable)
6600                 (cond
6601                  ((and gnus-thread-ignore-subject
6602                        (not (string= 
6603                              (gnus-simplify-subject-re gnus-tmp-prev-subject)
6604                              (gnus-simplify-subject-re subject))))
6605                   subject)
6606                  ((zerop level)
6607                   (if (and (eq gnus-summary-make-false-root 'empty)
6608                            (memq number gnus-tmp-gathered))
6609                       gnus-summary-same-subject
6610                     subject))
6611                  (t gnus-summary-same-subject))
6612                 (and (eq gnus-summary-make-false-root 'adopt)
6613                      (memq number gnus-tmp-gathered))
6614                 (cdr (assq number gnus-newsgroup-scored))
6615                 (memq number gnus-newsgroup-processable))
6616
6617                (setq gnus-tmp-prev-subject subject)))))
6618
6619         (if (nth 1 thread) 
6620             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6621         (setq level (1+ level))
6622         (setq threads (cdr (car thread))))))
6623   (message "Generating summary...done"))
6624
6625
6626
6627 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6628   (let (header number)
6629
6630     ;; Do the async thing, if that is required.
6631     (if gnus-newsgroup-async
6632         (setq gnus-newsgroup-threads
6633               (mapcar (lambda (h) 
6634                         (cons (mail-header-number h) (mail-header-lines h)))
6635                       headers)))
6636
6637     (while headers
6638       (setq header (car headers)
6639             headers (cdr headers)
6640             number (mail-header-number header))
6641
6642       ;; We may have to root out some bad articles...
6643       (cond 
6644        ((and cull
6645              (memq (setq number (mail-header-number header))
6646                    gnus-newsgroup-dormant)))
6647        ((and cull gnus-summary-expunge-below
6648              (< (or (cdr (assq number gnus-newsgroup-scored))
6649                     gnus-summary-default-score 0)
6650                 gnus-summary-expunge-below))
6651         (setq gnus-newsgroup-unreads 
6652               (delq number gnus-newsgroup-unreads))
6653         (setq gnus-newsgroup-reads
6654               (cons (cons number gnus-low-score-mark)
6655                     gnus-newsgroup-reads)))
6656        (t
6657         (gnus-summary-insert-line
6658          nil header 0 nil 
6659          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6660                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6661                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6662                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6663                (t (or (cdr (assq number gnus-newsgroup-reads))
6664                       gnus-ancient-mark)))
6665          (memq number gnus-newsgroup-replied)
6666          (memq number gnus-newsgroup-expirable)
6667          (mail-header-subject header) nil
6668          (cdr (assq number gnus-newsgroup-scored))
6669          (memq number gnus-newsgroup-processable)))))))
6670
6671 (defun gnus-select-newsgroup (group &optional read-all)
6672   "Select newsgroup GROUP.
6673 If READ-ALL is non-nil, all articles in the group are selected."
6674   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6675          (info (nth 2 entry))
6676          articles)
6677
6678     (or (gnus-check-server
6679          (setq gnus-current-select-method (gnus-find-method-for-group group)))
6680         (error "Couldn't open server"))
6681     
6682     (or (and entry (not (eq (car entry) t))) ; Either it's active...
6683         (gnus-activate-group group) ; Or we can activate it...
6684         (progn ; Or we bug out.
6685           (kill-buffer (current-buffer))
6686           (error "Couldn't request group %s: %s" 
6687                  group (gnus-status-message group))))
6688
6689     (setq gnus-newsgroup-name group)
6690     (setq gnus-newsgroup-unselected nil)
6691     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6692
6693     (and gnus-asynchronous
6694          (gnus-check-backend-function 
6695           'request-asynchronous gnus-newsgroup-name)
6696          (setq gnus-newsgroup-async
6697                (gnus-request-asynchronous gnus-newsgroup-name)))
6698
6699     (setq articles (gnus-articles-to-read group read-all))
6700
6701     (cond 
6702      ((null articles) 
6703       (gnus-message 3 "Couldn't select newsgroup")
6704       'quit)
6705      ((eq articles 0) nil)
6706      (t
6707       ;; Init the dependencies hash table.
6708       (setq gnus-newsgroup-dependencies 
6709             (gnus-make-hashtable (length articles)))
6710       ;; Retrieve the headers and read them in.
6711       (gnus-message 5 "Fetching headers...")
6712       (setq gnus-newsgroup-headers 
6713             (if (eq 'nov (setq gnus-headers-retrieved-by
6714                                ;; This is a naughty hack. To get the
6715                                ;; retrieval of old headers to work, we
6716                                ;; set `nntp-nov-gap' to nil (locally),
6717                                ;; and then just retrieve the headers.
6718                                ;; Mucho magic.
6719                                (if gnus-fetch-old-headers
6720                                    (let (nntp-nov-gap)
6721                                      (gnus-retrieve-headers 
6722                                       (if (not (eq 1 (car articles)))
6723                                           (cons 1 articles)
6724                                         articles)
6725                                       gnus-newsgroup-name))
6726                                  (gnus-retrieve-headers 
6727                                   articles gnus-newsgroup-name))))
6728                 (progn
6729                   (gnus-get-newsgroup-headers-xover articles))
6730               ;; If we were to fetch old headers, but the backend didn't
6731               ;; support XOVER, then it is possible we fetched one article
6732               ;; that we shouldn't have. If that's the case, we remove it.
6733               (if (or (not gnus-fetch-old-headers)
6734                       (eq 1 (car articles)))
6735                   ()
6736                 (save-excursion
6737                   (set-buffer nntp-server-buffer)
6738                   (goto-char (point-min))
6739                   (and 
6740                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6741                    (delete-region       ; So we delete this head.
6742                     (point) 
6743                     (search-forward "\n.\n" nil t)))))
6744               (gnus-get-newsgroup-headers)))
6745       (gnus-message 5 "Fetching headers...done")      
6746       ;; Remove canceled articles from the list of unread articles.
6747       (setq gnus-newsgroup-unreads
6748             (gnus-set-sorted-intersection 
6749              gnus-newsgroup-unreads
6750              (mapcar (lambda (headers) (mail-header-number headers))
6751                      gnus-newsgroup-headers)))
6752       ;; Adjust and set lists of article marks.
6753       (and info
6754            (let (marked)
6755              (gnus-adjust-marked-articles info)
6756              (setq gnus-newsgroup-marked 
6757                    (copy-sequence
6758                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6759              (setq gnus-newsgroup-replied 
6760                    (copy-sequence (cdr (assq 'reply marked))))
6761              (setq gnus-newsgroup-expirable
6762                    (copy-sequence (cdr (assq 'expire marked))))
6763              (setq gnus-newsgroup-killed
6764                    (copy-sequence (cdr (assq 'killed marked))))
6765              (setq gnus-newsgroup-bookmarks 
6766                    (copy-sequence (cdr (assq 'bookmark marked))))
6767              (setq gnus-newsgroup-dormant 
6768                    (copy-sequence (cdr (assq 'dormant marked))))
6769              (setq gnus-newsgroup-scored 
6770                    (copy-sequence (cdr (assq 'score marked))))
6771              (setq gnus-newsgroup-processable nil)))
6772       ;; Check whether auto-expire is to be done in this group.
6773       (setq gnus-newsgroup-auto-expire
6774             (or (and (stringp gnus-auto-expirable-newsgroups)
6775                      (string-match gnus-auto-expirable-newsgroups group))
6776                 (memq 'auto-expire (nth 5 info))))
6777       ;; First and last article in this newsgroup.
6778       (and gnus-newsgroup-headers
6779            (setq gnus-newsgroup-begin 
6780                  (mail-header-number (car gnus-newsgroup-headers)))
6781            (setq gnus-newsgroup-end
6782                  (mail-header-number
6783                   (gnus-last-element gnus-newsgroup-headers))))
6784       (setq gnus-reffed-article-number -1)
6785       ;; GROUP is successfully selected.
6786       (or gnus-newsgroup-headers t)))))
6787
6788 (defun gnus-articles-to-read (group read-all)
6789   ;; Find out what articles the user wants to read.
6790   (let* ((articles
6791           ;; Select all articles if `read-all' is non-nil, or if all the
6792           ;; unread articles are dormant articles.
6793           (if (or read-all
6794                   (= (length gnus-newsgroup-unreads) 
6795                      (length gnus-newsgroup-dormant)))
6796               (gnus-uncompress-range 
6797                (gnus-gethash group gnus-active-hashtb))
6798             gnus-newsgroup-unreads))
6799          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6800          (scored (length scored-list))
6801          (number (length articles))
6802          (marked (+ (length gnus-newsgroup-marked)
6803                     (length gnus-newsgroup-dormant)))
6804          (select
6805           (cond 
6806            ((numberp read-all)
6807             read-all)
6808            (t
6809             (condition-case ()
6810                 (cond ((and (or (<= scored marked)
6811                                 (= scored number))
6812                             (numberp gnus-large-newsgroup)
6813                             (> number gnus-large-newsgroup))
6814                        (let ((input
6815                               (read-string
6816                                (format
6817                                 "How many articles from %s (default %d): "
6818                                 gnus-newsgroup-name number))))
6819                          (if (string-match "^[ \t]*$" input)
6820                              number input)))
6821                       ((and (> scored marked) (< scored number))
6822                        (let ((input
6823                               (read-string
6824                                (format 
6825                                 "%s %s (%d scored, %d total): "
6826                                 "How many articles from"
6827                                 group scored number))))
6828                          (if (string-match "^[ \t]*$" input)
6829                              number input)))
6830                       (t number))
6831               (quit nil))))))
6832     (setq select (if (stringp select) (string-to-number select) select))
6833     (if (or (null select) (zerop select))
6834         select
6835       (if (and (not (zerop scored)) (<= (abs select) scored))
6836           (progn
6837             (setq articles (sort scored-list '<))
6838             (setq number (length articles)))
6839         (setq articles (copy-sequence articles)))
6840
6841       (if (< (abs select) number)
6842           (if (< select 0) 
6843               ;; Select the N oldest articles.
6844               (setcdr (nthcdr (1- (abs select)) articles) nil)
6845             ;; Select the N most recent articles.
6846             (setq articles (nthcdr (- number select) articles))))
6847       (setq gnus-newsgroup-unselected
6848             (gnus-sorted-intersection
6849              gnus-newsgroup-unreads
6850              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6851       articles)))
6852
6853 (defun gnus-killed-articles (killed articles)
6854   (let (out)
6855     (while articles
6856       (if (inline (gnus-member-of-range (car articles) killed))
6857           (setq out (cons (car articles) out)))
6858       (setq articles (cdr articles)))
6859     out))
6860
6861 (defun gnus-adjust-marked-articles (info &optional active)
6862   "Remove all marked articles that are no longer legal."
6863   (let ((marked-lists (nth 3 info))
6864         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6865         m prev)
6866     ;; There are many types of marked articles.
6867     (while marked-lists
6868       (setq m (cdr (setq prev (car marked-lists))))
6869       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6870              ;; Make sure that all ticked articles are a subset of the
6871              ;; unread/unselected articles.
6872              (while m
6873                (if (or (memq (car m) gnus-newsgroup-unreads)
6874                        (memq (car m) gnus-newsgroup-unselected))
6875                    (setq prev m)
6876                  (setcdr prev (cdr m)))
6877                (setq m (cdr m))))
6878             ((eq 'score (car prev))
6879              ;; Scored articles should be a subset of
6880              ;; unread/unselected articles. 
6881              (while m
6882                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6883                        (memq (car (car m)) gnus-newsgroup-unreads))
6884                    (setq prev m)
6885                  (setcdr prev (cdr m)))
6886                (setq m (cdr m))))
6887             ((eq 'bookmark (car prev))
6888              ;; Bookmarks should be a subset of active articles.
6889              (while m
6890                (if (< (car (car m)) (car active))
6891                    (setcdr prev (cdr m))
6892                  (setq prev m))
6893                (setq m (cdr m))))
6894             ((eq 'killed (car prev))
6895              ;; Articles that have been through the kill process are
6896              ;; to be a subset of active articles.
6897              (while (and m (< (or (and (numberp (car m)) (car m))
6898                                   (cdr (car m)))
6899                               (car active)))
6900                (setcdr prev (cdr m))
6901                (setq m (cdr m)))
6902              (if (and m (< (or (and (numberp (car m)) (car m))
6903                                (car (car m)))
6904                            (car active))) 
6905                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6906             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6907              ;; The replied and expirable articles have to be articles
6908              ;; that are active. 
6909              (while m
6910                (if (< (car m) (car active))
6911                    (setcdr prev (cdr m))
6912                  (setq prev m))
6913                (setq m (cdr m)))))
6914       (setq marked-lists (cdr marked-lists)))
6915     ;; Remove all lists that are empty.
6916     (setq marked-lists (nth 3 info))
6917     (if marked-lists
6918         (progn
6919           (while (= 1 (length (car marked-lists)))
6920             (setq marked-lists (cdr marked-lists)))
6921           (setq m (cdr (setq prev marked-lists)))
6922           (while m
6923             (if (= 1 (length (car m)))
6924                 (setcdr prev (cdr m))
6925               (setq prev m))
6926             (setq m (cdr m)))
6927           (setcar (nthcdr 3 info) marked-lists)))
6928     ;; Finally, if there are no marked lists at all left, and if there
6929     ;; are no elements after the lists in the info list, we just chop
6930     ;; the info list off before the marked lists.
6931     (and (null marked-lists) 
6932          (not (nthcdr 4 info))
6933          (setcdr (nthcdr 2 info) nil)))
6934   info)
6935
6936 (defun gnus-set-marked-articles 
6937   (info ticked replied expirable killed dormant bookmark score) 
6938   "Enter the various lists of marked articles into the newsgroup info list."
6939   (let (newmarked)
6940     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6941     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6942     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6943                                          newmarked)))
6944     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6945     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6946     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6947                                         newmarked)))
6948     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6949     (if (nthcdr 3 info)
6950         (progn
6951           (setcar (nthcdr 3 info) newmarked)
6952           (and (not newmarked)
6953                (not (nthcdr 4 info))
6954                (setcdr (nthcdr 2 info) nil)))
6955       (if newmarked
6956           (setcdr (nthcdr 2 info) (list newmarked))))))
6957
6958 (defun gnus-add-marked-articles (group type articles &optional info force)
6959   ;; Add ARTICLES of TYPE to the info of GROUP.
6960   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6961   ;; add, but replace marked articles of TYPE with ARTICLES.
6962   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6963         marked m)
6964     (or (not info)
6965         (and (not (setq marked (nthcdr 3 info)))
6966              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6967         (and (not (setq m (assq type (car marked))))
6968              (setcar marked (cons (cons type articles) (car marked))))
6969         (if force
6970             (setcdr m articles)
6971           (nconc m articles)))))
6972          
6973 (defun gnus-set-mode-line (where)
6974   "This function sets the mode line of the article or summary buffers.
6975 If WHERE is `summary', the summary mode line format will be used."
6976   (if (memq where gnus-updated-mode-lines)
6977       (let (mode-string)
6978         (save-excursion
6979           (set-buffer gnus-summary-buffer)
6980           (let* ((mformat (if (eq where 'article) 
6981                               gnus-article-mode-line-format-spec
6982                             gnus-summary-mode-line-format-spec))
6983                  (buffer-name (if (eq where 'article)
6984                                   (buffer-name
6985                                    (get-buffer gnus-article-buffer))
6986                                 (buffer-name)))
6987                  (group-name gnus-newsgroup-name)
6988                  (article-number (or gnus-current-article 0))
6989                  (unread (- (length gnus-newsgroup-unreads)
6990                             (length gnus-newsgroup-dormant)))
6991                  (unread-and-unticked 
6992                   (- unread (length gnus-newsgroup-marked)))
6993                  (unselected (length gnus-newsgroup-unselected))
6994                  (unread-and-unselected
6995                   (cond ((and (zerop unread-and-unticked)
6996                               (zerop unselected)) "")
6997                         ((zerop unselected) 
6998                          (format "{%d more}" unread-and-unticked))
6999                         (t (format "{%d(+%d) more}"
7000                                    unread-and-unticked unselected))))
7001                  (subject
7002                   (if gnus-current-headers
7003                       (mail-header-subject gnus-current-headers) ""))
7004                  (max-len (and gnus-mode-non-string-length
7005                                (- (frame-width) gnus-mode-non-string-length)))
7006                  header);; passed as argument to any user-format-funcs
7007             (setq mode-string (eval mformat))
7008             (or (numberp max-len)
7009                 (setq max-len (length mode-string)))
7010             (if (< max-len 4) (setq max-len 4))
7011             (if (> (length mode-string) max-len)
7012                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
7013                 ;;  function `substring' might cut on a middle
7014                 ;;  of multi-octet character.
7015                 (setq mode-string 
7016                       (concat (gnus-truncate-string mode-string (- max-len 3))
7017                               "...")))
7018             (setq mode-string (format (format "%%-%ds" max-len)
7019                                       mode-string))))
7020         (setq mode-line-buffer-identification mode-string)
7021         (set-buffer-modified-p t))))
7022
7023 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
7024   "Go through the HEADERS list and add all Xrefs to a hash table.
7025 The resulting hash table is returned, or nil if no Xrefs were found."
7026   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
7027          (prefix (if (and 
7028                       (gnus-group-foreign-p from-newsgroup)
7029                       (not (memq 'virtual 
7030                                  (assoc (symbol-name (car from-method))
7031                                         gnus-valid-select-methods))))
7032                      (gnus-group-real-prefix from-newsgroup)))
7033          (xref-hashtb (make-vector 63 0))
7034          start group entry number xrefs header)
7035     (while headers
7036       (setq header (car headers))
7037       (if (and (setq xrefs (mail-header-xref header))
7038                (not (memq (mail-header-number header) unreads)))
7039           (progn
7040             (setq start 0)
7041             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
7042               (setq start (match-end 0))
7043               (setq group (concat prefix (substring xrefs (match-beginning 1) 
7044                                                     (match-end 1))))
7045               (setq number 
7046                     (string-to-int (substring xrefs (match-beginning 2) 
7047                                               (match-end 2))))
7048               (if (setq entry (gnus-gethash group xref-hashtb))
7049                   (setcdr entry (cons number (cdr entry)))
7050                 (gnus-sethash group (cons number nil) xref-hashtb)))))
7051       (setq headers (cdr headers)))
7052     (if start xref-hashtb nil)))
7053
7054 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
7055   "Look through all the headers and mark the Xrefs as read."
7056   (let ((virtual (memq 'virtual 
7057                        (assoc (symbol-name (car (gnus-find-method-for-group 
7058                                                  from-newsgroup)))
7059                               gnus-valid-select-methods)))
7060         name entry info xref-hashtb idlist method
7061         nth4)
7062     (save-excursion
7063       (set-buffer gnus-group-buffer)
7064       (if (setq xref-hashtb 
7065                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
7066           (mapatoms 
7067            (lambda (group)
7068              (if (string= from-newsgroup (setq name (symbol-name group)))
7069                  ()
7070                (setq idlist (symbol-value group))
7071                ;; Dead groups are not updated.
7072                (if (and (prog1 
7073                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
7074                                   info (nth 2 entry))
7075                           (if (stringp (setq nth4 (nth 4 info)))
7076                               (setq nth4 (gnus-server-to-method nth4))))
7077                         ;; Only do the xrefs if the group has the same
7078                         ;; select method as the group we have just read.
7079                         (or (gnus-methods-equal-p 
7080                              nth4 (gnus-find-method-for-group from-newsgroup))
7081                             virtual
7082                             (equal nth4 
7083                                    (setq method (gnus-find-method-for-group 
7084                                                  from-newsgroup)))
7085                             (and (equal (car nth4) (car method))
7086                                  (equal (nth 1 nth4) (nth 1 method))))
7087                         gnus-use-cross-reference
7088                         (or (not (eq gnus-use-cross-reference t))
7089                             virtual
7090                             ;; Only do cross-references on subscribed
7091                             ;; groups, if that is what is wanted.  
7092                             (<= (nth 1 info) gnus-level-subscribed)))
7093                    (gnus-group-make-articles-read name idlist expirable))))
7094            xref-hashtb)))))
7095
7096 (defun gnus-group-make-articles-read (group articles expirable)
7097   (let* ((num 0)
7098          (entry (gnus-gethash group gnus-newsrc-hashtb))
7099          (info (nth 2 entry))
7100          (active (gnus-gethash group gnus-active-hashtb))
7101          exps expirable range)
7102     ;; First peel off all illegal article numbers.
7103     (if active
7104         (let ((ids articles)
7105               (ticked (cdr (assq 'tick (nth 3 info))))
7106               (dormant (cdr (assq 'dormant (nth 3 info))))
7107               id first)
7108           (setq exps nil)
7109           (while ids
7110             (setq id (car ids))
7111             (if (and first (> id (cdr active)))
7112                 (progn
7113                   ;; We'll end up in this situation in one particular
7114                   ;; obscure situation. If you re-scan a group and get
7115                   ;; a new article that is cross-posted to a different
7116                   ;; group that has not been re-scanned, you might get
7117                   ;; crossposted article that has a higher number than
7118                   ;; Gnus believes possible. So we re-activate this
7119                   ;; group as well. This might mean doing the
7120                   ;; crossposting thingie will *increase* the number
7121                   ;; of articles in some groups. Tsk, tsk.
7122                   (setq active (or (gnus-activate-group group) active))))
7123             (if (or (> id (cdr active))
7124                     (< id (car active))
7125                     (memq id ticked)
7126                     (memq id dormant))
7127                 (setq articles (delq id articles)))
7128             (and (memq id expirable)
7129                  (setq exps (cons id exps)))
7130             (setq ids (cdr ids)))))
7131     ;; Update expirable articles.
7132     (gnus-add-marked-articles nil 'expirable exps info)
7133     (and active
7134          (null (nth 2 info))
7135          (> (car active) 1)
7136          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
7137     (setcar (nthcdr 2 info)
7138             (setq range
7139                   (gnus-add-to-range 
7140                    (nth 2 info) 
7141                    (setq articles (sort articles '<)))))
7142     ;; Then we have to re-compute how many unread
7143     ;; articles there are in this group.
7144     (if active
7145         (progn
7146           (cond 
7147            ((not range)
7148             (setq num (- (1+ (cdr active)) (car active))))
7149            ((not (listp (cdr range)))
7150             (setq num (- (cdr active) (- (1+ (cdr range)) 
7151                                          (car range)))))
7152            (t
7153             (while range
7154               (if (numberp (car range))
7155                   (setq num (1+ num))
7156                 (setq num (+ num (- (1+ (cdr (car range)))
7157                                     (car (car range))))))
7158               (setq range (cdr range)))
7159             (setq num (- (cdr active) num))))
7160           ;; Update the number of unread articles.
7161           (setcar 
7162            entry 
7163            (max 0 (- num 
7164                      (length (cdr (assq 'tick (nth 3 info))))
7165                      (length 
7166                       (cdr (assq 'dormant (nth 3 info)))))))
7167           ;; Update the group buffer.
7168           (gnus-group-update-group group t)))))
7169
7170 (defun gnus-methods-equal-p (m1 m2)
7171   (let ((m1 (or m1 gnus-select-method))
7172         (m2 (or m2 gnus-select-method)))
7173     (or (equal m1 m2)
7174         (and (eq (car m1) (car m2))
7175              (or (not (memq 'address (assoc (symbol-name (car m1))
7176                                             gnus-valid-select-methods)))
7177                  (equal (nth 1 m1) (nth 1 m2)))))))
7178
7179 (defsubst gnus-header-value ()
7180   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7181
7182 (defvar gnus-newsgroup-none-id 0)
7183
7184 (defun gnus-get-newsgroup-headers ()
7185   (setq gnus-article-internal-prepare-hook nil)
7186   (let ((cur nntp-server-buffer)
7187         (dependencies gnus-newsgroup-dependencies)
7188         headers id dep end ref)
7189     (save-excursion
7190       (set-buffer nntp-server-buffer)
7191       ;; Allow the user to mangle the headers before parsing them.
7192       (run-hooks 'gnus-parse-headers-hook)
7193       (goto-char (point-min))
7194       ;; Search to the beginning of the next header. Error messages
7195       ;; do not begin with 2 or 3.
7196       (while (re-search-forward "^[23][0-9]+ " nil t)
7197         (let ((header (make-vector 9 nil))
7198               (case-fold-search t)
7199               (p (point))
7200               in-reply-to)
7201           (setq id nil
7202                 ref nil)
7203           (mail-header-set-number header (read cur))
7204           ;; This implementation of this function, with nine
7205           ;; search-forwards instead of the one re-search-forward and
7206           ;; a case (which basically was the old function) is actually
7207           ;; about twice as fast, even though it looks messier. You
7208           ;; can't have everything, I guess. Speed and elegance
7209           ;; doesn't always come hand in hand.
7210           (save-restriction
7211             (narrow-to-region (point) (or (save-excursion 
7212                                             (search-forward "\n.\n" nil t))
7213                                           (point)))
7214             (if (search-forward "\nfrom: " nil t)
7215                 (mail-header-set-from header (gnus-header-value))
7216               (mail-header-set-from header "(nobody)"))
7217             (goto-char p)
7218             (if (search-forward "\nsubject: " nil t)
7219                 (mail-header-set-subject header (gnus-header-value))
7220               (mail-header-set-subject header "(none)"))
7221             (goto-char p)
7222             (and (search-forward "\nxref: " nil t)
7223                  (mail-header-set-xref header (gnus-header-value)))
7224             (goto-char p)
7225             (or (numberp (and (search-forward "\nlines: " nil t)
7226                               (mail-header-set-lines header (read cur))))
7227                 (mail-header-set-lines header 0))
7228             (goto-char p)
7229             (and (search-forward "\ndate: " nil t)
7230                  (mail-header-set-date header (gnus-header-value)))
7231             (goto-char p)
7232             (if (search-forward "\nmessage-id: " nil t)
7233                 (mail-header-set-id header (setq id (gnus-header-value)))
7234               ;; If there was no message-id, we just fake one to make
7235               ;; subsequent routines simpler.
7236               (mail-header-set-id 
7237                header 
7238                (setq id (concat "none+" 
7239                                 (int-to-string 
7240                                  (setq gnus-newsgroup-none-id 
7241                                        (1+ gnus-newsgroup-none-id)))))))
7242             (goto-char p)
7243             (if (search-forward "\nreferences: " nil t)
7244                 (progn
7245                   (mail-header-set-references header (gnus-header-value))
7246                   (setq end (match-end 0))
7247                   (save-excursion
7248                     (setq ref 
7249                           (downcase
7250                            (buffer-substring
7251                             (progn 
7252                               (end-of-line)
7253                               (search-backward ">" end t)
7254                               (1+ (point)))
7255                             (progn
7256                               (search-backward "<" end t)
7257                               (point)))))))
7258               ;; Get the references from the in-reply-to header if there
7259               ;; ware no references and the in-reply-to header looks
7260               ;; promising. 
7261               (if (and (search-forward "\nin-reply-to: " nil t)
7262                        (setq in-reply-to (gnus-header-value))
7263                        (string-match "<[^>]+>" in-reply-to))
7264                   (progn
7265                     (mail-header-set-references 
7266                      header 
7267                      (setq ref (substring in-reply-to (match-beginning 0)
7268                                           (match-end 0))))
7269                     (setq ref (downcase ref)))
7270                 (setq ref "none")))
7271             ;; We do some threading while we read the headers. The
7272             ;; message-id and the last reference are both entered into
7273             ;; the same hash table. Some tippy-toeing around has to be
7274             ;; done in case an article has arrived before the article
7275             ;; which it refers to.
7276             (if (boundp (setq dep (intern (downcase id) dependencies)))
7277                 (if (car (symbol-value dep))
7278                     ;; An article with this Message-ID has already
7279                     ;; been seen, so we ignore this one, except we add
7280                     ;; any additional Xrefs (in case the two articles
7281                     ;; came from different servers.
7282                     (progn
7283                       (mail-header-set-xref 
7284                        (car (symbol-value dep))
7285                        (concat (or (mail-header-xref 
7286                                     (car (symbol-value dep))) "")
7287                                (or (mail-header-xref header) "")))
7288                       (setq header nil))
7289                   (setcar (symbol-value dep) header))
7290               (set dep (list header)))
7291             (if header
7292                 (progn
7293                   (if (boundp (setq dep (intern ref dependencies)))
7294                       (setcdr (symbol-value dep) 
7295                               (cons header (cdr (symbol-value dep))))
7296                     (set dep (list nil header)))
7297                   (setq headers (cons header headers))))
7298             (goto-char (point-max))))))
7299     (nreverse headers)))
7300
7301 ;; The following macros and functions were written by Felix Lee
7302 ;; <flee@cse.psu.edu>. 
7303
7304 (defmacro gnus-nov-read-integer ()
7305   '(prog1
7306        (if (= (following-char) ?\t)
7307            0
7308          (let ((num (condition-case nil (read buffer) (error nil))))
7309            (if (numberp num) num 0)))
7310      (or (eobp) (forward-char 1))))
7311
7312 (defmacro gnus-nov-skip-field ()
7313   '(search-forward "\t" eol 'move))
7314
7315 (defmacro gnus-nov-field ()
7316   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7317
7318 ;; Goes through the xover lines and returns a list of vectors
7319 (defun gnus-get-newsgroup-headers-xover (sequence)
7320   "Parse the news overview data in the server buffer, and return a
7321 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7322   ;; Get the Xref when the users reads the articles since most/some
7323   ;; NNTP servers do not include Xrefs when using XOVER.
7324   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7325   (let ((cur nntp-server-buffer)
7326         (dependencies gnus-newsgroup-dependencies)
7327         number headers header)
7328     (save-excursion
7329       (set-buffer nntp-server-buffer)
7330       ;; Allow the user to mangle the headers before parsing them.
7331       (run-hooks 'gnus-parse-headers-hook)
7332       (goto-char (point-min))
7333       (while (and sequence (not (eobp)))
7334         (setq number (read cur))
7335         (while (and sequence (< (car sequence) number))
7336           (setq sequence (cdr sequence)))
7337         (and sequence 
7338              (eq number (car sequence))
7339              (progn
7340                (setq sequence (cdr sequence))
7341                (if (setq header 
7342                          (inline (gnus-nov-parse-line number dependencies)))
7343                    (setq headers (cons header headers)))))
7344         (forward-line 1))
7345       (setq headers (nreverse headers)))
7346     headers))
7347
7348 ;; This function has to be called with point after the article number
7349 ;; on the beginning of the line.
7350 (defun gnus-nov-parse-line (number dependencies)
7351   (let ((none 0)
7352         (eol (gnus-point-at-eol)) 
7353         (buffer (current-buffer))
7354         header ref id dep)
7355
7356     ;; overview: [num subject from date id refs chars lines misc]
7357     (narrow-to-region (point) eol)
7358     (or (eobp) (forward-char))
7359
7360     (condition-case nil
7361         (setq header
7362               (vector 
7363                number                   ; number
7364                (gnus-nov-field)         ; subject
7365                (gnus-nov-field)         ; from
7366                (gnus-nov-field)         ; date
7367                (setq id (or (gnus-nov-field)
7368                             (concat "none+"
7369                                     (int-to-string 
7370                                      (setq none (1+ none)))))) ; id
7371                (progn
7372                  (save-excursion
7373                    (let ((beg (point)))
7374                      (search-forward "\t" eol)
7375                      (if (search-backward ">" beg t)
7376                          (setq ref 
7377                                (downcase 
7378                                 (buffer-substring 
7379                                  (1+ (point))
7380                                  (progn
7381                                    (search-backward "<" beg t)
7382                                    (point)))))
7383                        (setq ref nil))))
7384                  (gnus-nov-field))      ; refs
7385                (gnus-nov-read-integer)  ; chars
7386                (gnus-nov-read-integer)  ; lines
7387                (if (= (following-char) ?\n)
7388                    nil
7389                  (gnus-nov-field))      ; misc
7390                ))
7391       (error (progn 
7392                (ding)
7393                (message "Strange nov line.")
7394                (setq header nil)
7395                (goto-char eol))))
7396
7397     (widen)
7398
7399     ;; We build the thread tree.
7400     (and header
7401          (if (boundp (setq dep (intern (downcase id) dependencies)))
7402              (if (car (symbol-value dep))
7403                  ;; An article with this Message-ID has already been seen,
7404                  ;; so we ignore this one, except we add any additional
7405                  ;; Xrefs (in case the two articles came from different
7406                  ;; servers.
7407                  (progn
7408                    (mail-header-set-xref 
7409                     (car (symbol-value dep))
7410                     (concat (or (mail-header-xref (car (symbol-value dep))) "")
7411                             (or (mail-header-xref header) "")))
7412                    (setq header nil))
7413                (setcar (symbol-value dep) header))
7414            (set dep (list header))))
7415     (if header
7416         (progn
7417           (if (boundp (setq dep (intern (or ref "none") 
7418                                         dependencies)))
7419               (setcdr (symbol-value dep) 
7420                       (cons header (cdr (symbol-value dep))))
7421             (set dep (list nil header)))))
7422     header))
7423
7424 (defun gnus-article-get-xrefs ()
7425   "Fill in the Xref value in `gnus-current-headers', if necessary.
7426 This is meant to be called in `gnus-article-internal-prepare-hook'."
7427   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7428                                  gnus-current-headers)))
7429     (or (not gnus-use-cross-reference)
7430         (not headers)
7431         (and (mail-header-xref headers)
7432              (not (string= (mail-header-xref headers) "")))
7433         (let ((case-fold-search t)
7434               xref)
7435           (save-restriction
7436             (gnus-narrow-to-headers)
7437             (goto-char (point-min))
7438             (if (or (and (eq (downcase (following-char)) ?x)
7439                          (looking-at "Xref:"))
7440                     (search-forward "\nXref:" nil t))
7441                 (progn
7442                   (goto-char (1+ (match-end 0)))
7443                   (setq xref (buffer-substring (point) 
7444                                                (progn (end-of-line) (point))))
7445                   (mail-header-set-xref headers xref))))))))
7446
7447 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7448 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7449
7450 (defun gnus-make-headers-hashtable-by-number ()
7451   "Make hashtable for the variable gnus-newsgroup-headers by number."
7452   (save-excursion
7453     (set-buffer gnus-summary-buffer)
7454     (let ((headers gnus-newsgroup-headers)
7455           header)
7456       (setq gnus-newsgroup-headers-hashtb-by-number
7457             (gnus-make-hashtable (length headers)))
7458       (while headers
7459         (setq header (car headers))
7460         (gnus-sethash (int-to-string (mail-header-number header))
7461                       header gnus-newsgroup-headers-hashtb-by-number)
7462         (setq headers (cdr headers))))))
7463
7464 (defun gnus-more-header-backward ()
7465   "Find new header backward."
7466   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7467         (artnum gnus-newsgroup-begin)
7468         (header nil))
7469     (while (and (not header)
7470                 (> artnum first))
7471       (setq artnum (1- artnum))
7472       (setq header (gnus-read-header artnum)))
7473     header))
7474
7475 (defun gnus-more-header-forward (&optional backward)
7476   "Find new header forward.
7477 If BACKWARD, find new header backward instead."
7478   (if backward
7479       (gnus-more-header-backward)
7480     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7481           (artnum gnus-newsgroup-end)
7482           (header nil))
7483       (while (and (not header)
7484                   (< artnum last))
7485         (setq artnum (1+ artnum))
7486         (setq header (gnus-read-header artnum)))
7487       header)))
7488
7489 (defun gnus-extend-newsgroup (header &optional backward)
7490   "Extend newsgroup selection with HEADER.
7491 Optional argument BACKWARD means extend toward backward."
7492   (if header
7493       (let ((artnum (mail-header-number header)))
7494         (setq gnus-newsgroup-headers
7495               (if backward
7496                   (cons header gnus-newsgroup-headers)
7497                 (nconc gnus-newsgroup-headers (list header))))
7498         (setq gnus-newsgroup-unselected
7499               (delq artnum gnus-newsgroup-unselected))
7500         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7501         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7502
7503 (defun gnus-summary-work-articles (n)
7504   "Return a list of articles to be worked upon. The prefix argument,
7505 the list of process marked articles, and the current article will be
7506 taken into consideration."
7507   (let (articles)
7508     (if (and n (numberp n))
7509         (let ((backward (< n 0))
7510               (n (abs n)))
7511           (save-excursion
7512             (while (and (> n 0)
7513                         (setq articles (cons (gnus-summary-article-number) 
7514                                              articles))
7515                         (gnus-summary-search-forward nil nil backward))
7516               (setq n (1- n))))
7517           (sort articles (function <)))
7518       (or (reverse gnus-newsgroup-processable)
7519           (list (gnus-summary-article-number))))))
7520
7521 (defun gnus-summary-search-group (&optional backward use-level)
7522   "Search for next unread newsgroup.
7523 If optional argument BACKWARD is non-nil, search backward instead."
7524   (save-excursion
7525     (set-buffer gnus-group-buffer)
7526     (if (gnus-group-search-forward 
7527          backward nil (if use-level (gnus-group-group-level) nil))
7528         (gnus-group-group-name))))
7529
7530 (defun gnus-summary-best-group (&optional exclude-group)
7531   "Find the name of the best unread group.
7532 If EXCLUDE-GROUP, do not go to this group."
7533   (save-excursion
7534     (set-buffer gnus-group-buffer)
7535     (save-excursion
7536       (gnus-group-best-unread-group exclude-group))))
7537
7538 (defun gnus-subject-equal (s1 s2)
7539   (cond
7540    ((null gnus-summary-gather-subject-limit)
7541     (equal (gnus-simplify-subject-re s1)
7542            (gnus-simplify-subject-re s2)))
7543    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7544     (equal (gnus-simplify-subject-fuzzy s1)
7545            (gnus-simplify-subject-fuzzy s2)))
7546    ((numberp gnus-summary-gather-subject-limit)
7547     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7548            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7549    (t
7550     (equal s1 s2))))
7551     
7552 (defun gnus-summary-search-subject (&optional backward unread subject)
7553   "Search for article forward.
7554 If BACKWARD is non-nil, search backward.
7555 If UNREAD is non-nil, only unread articles are selected.
7556 If SUBJECT is non-nil, the article which has the same subject will be
7557 searched for." 
7558   (let ((func (if backward 'previous-single-property-change
7559                 'next-single-property-change))
7560         (beg (point))
7561         (did t)
7562         pos psubject)
7563     (beginning-of-line)
7564     (and gnus-summary-check-current unread
7565          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7566          (setq did nil))
7567     (if (not did)
7568         ()
7569       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7570       (while
7571           (and 
7572            (setq pos (funcall func (point) 'gnus-number))
7573            (goto-char (if backward (1- pos) pos))
7574            (setq did
7575                  (not (and
7576                        (or (not unread)
7577                            (eq (get-text-property (point) 'gnus-mark)
7578                                gnus-unread-mark))
7579                        (or (not subject)
7580                            (and (setq psubject 
7581                                       (inline (gnus-summary-subject-string)))
7582                                 (inline 
7583                                   (gnus-subject-equal subject psubject)))))))
7584            (if backward (if (bobp) nil (forward-char -1) t)
7585              (if (eobp) nil (forward-char 1) t)))))
7586     (if did
7587         (progn (goto-char beg) nil)
7588       (prog1
7589           (get-text-property (point) 'gnus-number)
7590         (gnus-summary-show-thread)
7591         (gnus-summary-position-cursor)))))
7592
7593 (defun gnus-summary-pseudo-article ()
7594   "The thread level of the article on the current line."
7595   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7596
7597 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7598 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7599 (defun gnus-summary-article-score ()
7600   "Return current article score."
7601   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7602       gnus-summary-default-score 0))
7603
7604 (defun gnus-summary-recenter ()
7605   "Center point in the summary window.
7606 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7607 displayed, no centering will be performed." 
7608   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7609   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7610   (let* ((top (cond ((< (window-height) 4) 0)
7611                     ((< (window-height) 7) 1)
7612                     (t 2)))
7613          (height (1- (window-height)))
7614          (bottom (save-excursion (goto-char (point-max))
7615                                  (forward-line (- height))
7616                                  (point)))
7617          (window (get-buffer-window (current-buffer))))
7618     (and 
7619      ;; The user has to want it,
7620      gnus-auto-center-summary 
7621      ;; the article buffer must be displayed,
7622      (get-buffer-window gnus-article-buffer)
7623      ;; Set the window start to either `bottom', which is the biggest
7624      ;; possible valid number, or the second line from the top,
7625      ;; whichever is the least.
7626      (set-window-start
7627       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7628
7629 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7630 (defun gnus-short-group-name (group &optional levels)
7631   "Collapse GROUP name LEVELS."
7632   (let* ((name "") (foreign "") (depth -1) (skip 1)
7633          (levels (or levels
7634                      (progn
7635                        (while (string-match "\\." group skip)
7636                          (setq skip (match-end 0)
7637                                depth (+ depth 1)))
7638                        depth))))
7639     (if (string-match ":" group)
7640         (setq foreign (substring group 0 (match-end 0))
7641               group (substring group (match-end 0))))
7642     (while group
7643       (if (and (string-match "\\." group) (> levels 0))
7644           (setq name (concat name (substring group 0 1))
7645                 group (substring group (match-end 0))
7646                 levels (- levels 1)
7647                 name (concat name "."))
7648         (setq name (concat foreign name group)
7649               group nil)))
7650     name))
7651
7652 (defun gnus-summary-jump-to-group (newsgroup)
7653   "Move point to NEWSGROUP in group mode buffer."
7654   ;; Keep update point of group mode buffer if visible.
7655   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7656       (save-window-excursion
7657         ;; Take care of tree window mode.
7658         (if (get-buffer-window gnus-group-buffer)
7659             (pop-to-buffer gnus-group-buffer))
7660         (gnus-group-jump-to-group newsgroup))
7661     (save-excursion
7662       ;; Take care of tree window mode.
7663       (if (get-buffer-window gnus-group-buffer)
7664           (pop-to-buffer gnus-group-buffer)
7665         (set-buffer gnus-group-buffer))
7666       (gnus-group-jump-to-group newsgroup))))
7667
7668 ;; This function returns a list of article numbers based on the
7669 ;; difference between the ranges of read articles in this group and
7670 ;; the range of active articles.
7671 (defun gnus-list-of-unread-articles (group)
7672   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7673          (active (gnus-gethash group gnus-active-hashtb))
7674          (last (cdr active))
7675          first nlast unread)
7676     ;; If none are read, then all are unread. 
7677     (if (not read)
7678         (setq first (car active))
7679       ;; If the range of read articles is a single range, then the
7680       ;; first unread article is the article after the last read
7681       ;; article. Sounds logical, doesn't it?
7682       (if (not (listp (cdr read)))
7683           (setq first (1+ (cdr read)))
7684         ;; `read' is a list of ranges.
7685         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7686                                 (car (car read)))) 1)
7687             (setq first 1))
7688         (while read
7689           (if first 
7690               (while (< first nlast)
7691                 (setq unread (cons first unread))
7692                 (setq first (1+ first))))
7693           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7694           (setq nlast (if (atom (car (cdr read))) 
7695                           (car (cdr read))
7696                         (car (car (cdr read)))))
7697           (setq read (cdr read)))))
7698     ;; And add the last unread articles.
7699     (while (<= first last)
7700       (setq unread (cons first unread))
7701       (setq first (1+ first)))
7702     ;; Return the list of unread articles.
7703     (nreverse unread)))
7704
7705 (defun gnus-list-of-read-articles (group)
7706   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7707         (active (gnus-gethash group gnus-active-hashtb)))
7708     (and info active
7709          (gnus-sorted-complement 
7710           (gnus-uncompress-range active) 
7711           (gnus-list-of-unread-articles group)))))
7712
7713 ;; Various summary commands
7714
7715 (defun gnus-summary-universal-argument ()
7716   "Perform any operation on all articles marked with the process mark."
7717   (interactive)
7718   (gnus-set-global-variables)
7719   (let ((articles (reverse gnus-newsgroup-processable))
7720         func)
7721     (or articles (error "No articles marked"))
7722     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7723         (error "Undefined key"))
7724     (while articles
7725       (gnus-summary-goto-subject (car articles))
7726       (command-execute func)
7727       (gnus-summary-remove-process-mark (car articles))
7728       (setq articles (cdr articles)))))
7729
7730 (defun gnus-summary-toggle-truncation (&optional arg)
7731   "Toggle truncation of summary lines.
7732 With arg, turn line truncation on iff arg is positive."
7733   (interactive "P")
7734   (setq truncate-lines
7735         (if (null arg) (not truncate-lines)
7736           (> (prefix-numeric-value arg) 0)))
7737   (redraw-display))
7738
7739 (defun gnus-summary-reselect-current-group (&optional all)
7740   "Once exit and then reselect the current newsgroup.
7741 The prefix argument ALL means to select all articles."
7742   (interactive "P")
7743   (gnus-set-global-variables)
7744   (let ((current-subject (gnus-summary-article-number))
7745         (group gnus-newsgroup-name))
7746     (setq gnus-newsgroup-begin nil)
7747     (gnus-summary-exit t)
7748     ;; We have to adjust the point of group mode buffer because the
7749     ;; current point was moved to the next unread newsgroup by
7750     ;; exiting.
7751     (gnus-summary-jump-to-group group)
7752     (gnus-group-read-group all t)
7753     (gnus-summary-goto-subject current-subject)))
7754
7755 (defun gnus-summary-rescan-group (&optional all)
7756   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7757   (interactive "P")
7758   (gnus-set-global-variables)
7759   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7760   (let ((group gnus-newsgroup-name))
7761     (gnus-summary-exit)
7762     (gnus-summary-jump-to-group group)
7763     (save-excursion
7764       (set-buffer gnus-group-buffer)
7765       (gnus-group-get-new-news-this-group 1))
7766     (gnus-summary-jump-to-group group)
7767     (gnus-group-read-group all)))
7768
7769 (defun gnus-summary-update-info ()
7770   (let* ((group gnus-newsgroup-name))
7771     (if gnus-newsgroup-kill-headers
7772         (setq gnus-newsgroup-killed
7773               (gnus-compress-sequence
7774                (nconc
7775                 (gnus-set-sorted-intersection
7776                  (gnus-uncompress-range gnus-newsgroup-killed)
7777                  (setq gnus-newsgroup-unselected
7778                        (sort gnus-newsgroup-unselected '<)))
7779                 (setq gnus-newsgroup-unreads
7780                       (sort gnus-newsgroup-unreads '<))) t)))
7781     (or (listp (cdr gnus-newsgroup-killed))
7782         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7783     (let ((headers gnus-newsgroup-headers))
7784       (gnus-close-group group)
7785       (run-hooks 'gnus-exit-group-hook)
7786       (gnus-update-read-articles 
7787        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7788        gnus-newsgroup-marked
7789        t gnus-newsgroup-replied gnus-newsgroup-expirable
7790        gnus-newsgroup-killed gnus-newsgroup-dormant
7791        gnus-newsgroup-bookmarks 
7792        (and gnus-save-score gnus-newsgroup-scored))
7793       (and gnus-use-cross-reference
7794            (gnus-mark-xrefs-as-read 
7795             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7796       ;; Do adaptive scoring, and possibly save score files.
7797       (and gnus-newsgroup-adaptive
7798            (gnus-score-adaptive))
7799       (and gnus-use-scoring 
7800            (fboundp 'gnus-score-save)
7801            (funcall 'gnus-score-save))
7802       ;; Do not switch windows but change the buffer to work.
7803       (set-buffer gnus-group-buffer)
7804       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
7805           (gnus-group-update-group group)))))
7806   
7807 (defun gnus-summary-exit (&optional temporary)
7808   "Exit reading current newsgroup, and then return to group selection mode.
7809 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7810   (interactive)
7811   (gnus-set-global-variables)
7812   (gnus-kill-save-kill-buffer)
7813   (let* ((group gnus-newsgroup-name)
7814          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7815          (mode major-mode)
7816          (buf (current-buffer)))
7817     (run-hooks 'gnus-summary-prepare-exit-hook)
7818     ;; Make all changes in this group permanent.
7819     (gnus-summary-update-info)          
7820     (set-buffer buf)
7821     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7822     ;; Make sure where I was, and go to next newsgroup.
7823     (set-buffer gnus-group-buffer)
7824     (or quit-config
7825         (progn
7826           (gnus-group-jump-to-group group)
7827           (gnus-group-next-unread-group 1)))
7828     (if temporary
7829         nil                             ;Nothing to do.
7830       ;; We set all buffer-local variables to nil. It is unclear why
7831       ;; this is needed, but if we don't, buffer-local variables are
7832       ;; not garbage-collected, it seems. This would the lead to en
7833       ;; ever-growing Emacs.
7834       (set-buffer buf)
7835       (gnus-summary-clear-local-variables)
7836       ;; We clear the global counterparts of the buffer-local
7837       ;; variables as well, just to be on the safe side.
7838       (gnus-configure-windows 'group 'force)
7839       (gnus-summary-clear-local-variables)
7840       ;; Return to group mode buffer. 
7841       (if (eq mode 'gnus-summary-mode)
7842           (gnus-kill-buffer buf))
7843       (if (get-buffer gnus-article-buffer)
7844           (bury-buffer gnus-article-buffer))
7845       (setq gnus-current-select-method gnus-select-method)
7846       (pop-to-buffer gnus-group-buffer)
7847       (if (not quit-config)
7848           (progn
7849             (gnus-group-jump-to-group group)
7850             (gnus-group-next-unread-group 1))
7851         (if (not (buffer-name (car quit-config)))
7852             (gnus-configure-windows 'group 'force)
7853           (set-buffer (car quit-config))
7854           (and (eq major-mode 'gnus-summary-mode)
7855                (gnus-set-global-variables))
7856           (gnus-configure-windows (cdr quit-config))))
7857       (run-hooks 'gnus-summary-exit-hook))))
7858
7859 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7860 (defun gnus-summary-exit-no-update (&optional no-questions)
7861   "Quit reading current newsgroup without updating read article info."
7862   (interactive)
7863   (gnus-set-global-variables)
7864   (let* ((group gnus-newsgroup-name)
7865          (quit-config (gnus-group-quit-config group)))
7866     (if (or no-questions
7867             gnus-expert-user
7868             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7869         (progn
7870           (gnus-close-group group)
7871           (gnus-summary-clear-local-variables)
7872           (set-buffer gnus-group-buffer)
7873           (gnus-summary-clear-local-variables)
7874           ;; Return to group selection mode.
7875           (gnus-configure-windows 'group 'force)
7876           (if (get-buffer gnus-summary-buffer)
7877               (kill-buffer gnus-summary-buffer))
7878           (if (get-buffer gnus-article-buffer)
7879               (bury-buffer gnus-article-buffer))
7880           (if (equal (gnus-group-group-name) group)
7881               (gnus-group-next-unread-group 1))
7882           (if quit-config
7883               (progn
7884                 (if (not (buffer-name (car quit-config)))
7885                     (gnus-configure-windows 'group 'force)
7886                   (set-buffer (car quit-config))
7887                   (and (eq major-mode 'gnus-summary-mode)
7888                        (gnus-set-global-variables))
7889                   (gnus-configure-windows (cdr quit-config)))))))))
7890
7891 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7892 (defun gnus-summary-fetch-faq (group)
7893   "Fetch the FAQ for the current group."
7894   (interactive (list gnus-newsgroup-name))
7895   (let (gnus-faq-buffer)
7896     (and (setq gnus-faq-buffer (gnus-group-fetch-faq group))
7897          (gnus-configure-windows 'summary-faq))))
7898
7899 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7900 (defun gnus-summary-describe-group (&optional force)
7901   "Describe the current newsgroup."
7902   (interactive "P")
7903   (gnus-group-describe-group force gnus-newsgroup-name))
7904
7905 (defun gnus-summary-describe-briefly ()
7906   "Describe summary mode commands briefly."
7907   (interactive)
7908   (gnus-message 6
7909                 (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")))
7910
7911 ;; Walking around group mode buffer from summary mode.
7912
7913 (defun gnus-summary-next-group (&optional no-article target-group backward)
7914   "Exit current newsgroup and then select next unread newsgroup.
7915 If prefix argument NO-ARTICLE is non-nil, no article is selected
7916 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7917 previous group instead."
7918   (interactive "P")
7919   (gnus-set-global-variables)
7920   (let ((current-group gnus-newsgroup-name)
7921         (current-buffer (current-buffer))
7922         entered)
7923     ;; First we semi-exit this group to update Xrefs and all variables.
7924     ;; We can't do a real exit, because the window conf must remain
7925     ;; the same in case the user is prompted for info, and we don't
7926     ;; want the window conf to change before that...
7927     (gnus-summary-exit t)
7928     (while (not entered)
7929       ;; Then we find what group we are supposed to enter.
7930       (set-buffer gnus-group-buffer)
7931       (gnus-group-jump-to-group current-group)
7932       (setq target-group 
7933             (or target-group        
7934                 (if (eq gnus-keep-same-level 'best) 
7935                     (gnus-summary-best-group gnus-newsgroup-name)
7936                   (gnus-summary-search-group backward gnus-keep-same-level))))
7937       (if (not target-group)
7938           ;; There are no further groups, so we return to the group
7939           ;; buffer.
7940           (progn
7941             (gnus-message 5 "Returning to the group buffer")
7942             (setq entered t)
7943             (set-buffer current-buffer)
7944             (gnus-summary-exit))
7945         ;; We try to enter the target group.
7946         (gnus-group-jump-to-group target-group)
7947         (let ((unreads (gnus-group-group-unread)))
7948           (if (and (or (eq t unreads)
7949                        (and unreads (not (zerop unreads))))
7950                    (gnus-summary-read-group
7951                     target-group nil no-article current-buffer))
7952               (setq entered t)
7953             (setq current-group target-group
7954                   target-group nil)))))))
7955
7956 (defun gnus-summary-next-group-old (&optional no-article group backward)
7957   "Exit current newsgroup and then select next unread newsgroup.
7958 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7959 If BACKWARD, go to previous group instead."
7960   (interactive "P")
7961   (gnus-set-global-variables)
7962   (let ((ingroup gnus-newsgroup-name)
7963         (sumbuf (current-buffer))
7964         num)
7965     (set-buffer gnus-group-buffer)
7966     (if (and group
7967              (or (and (numberp (setq num (car (gnus-gethash
7968                                                group gnus-newsrc-hashtb))))
7969                       (< num 1))
7970                  (null num)))
7971         (progn
7972           (gnus-group-jump-to-group group)
7973           (setq group nil))
7974       (gnus-group-jump-to-group ingroup))
7975     (gnus-summary-search-group backward)
7976     (let ((group (or group (gnus-summary-search-group backward))))
7977       (set-buffer sumbuf)
7978       (gnus-summary-exit t)             ;Update all information.
7979       (if (null group)
7980           (gnus-summary-exit-no-update t)
7981         (gnus-group-jump-to-group ingroup)
7982         (setq group (gnus-summary-search-group backward))
7983         (gnus-message 5 "Selecting %s..." group)
7984         (set-buffer gnus-group-buffer)
7985         ;; We are now in group mode buffer.
7986         ;; Make sure group mode buffer point is on GROUP.
7987         (gnus-group-jump-to-group group)
7988         (if (not (eq gnus-auto-select-next 'quietly))
7989             (progn
7990               (gnus-summary-read-group group nil no-article sumbuf)
7991               (and (string= gnus-newsgroup-name ingroup)
7992                    (bufferp sumbuf) (buffer-name sumbuf)
7993                    (progn
7994                      (set-buffer (setq gnus-summary-buffer sumbuf))
7995                      (gnus-summary-exit-no-update t))))
7996           (let ((prevgroup group))
7997             (gnus-group-jump-to-group ingroup)
7998             (setq group (gnus-summary-search-group backward))
7999             (gnus-summary-read-group group nil no-article sumbuf)
8000             (while (and (string= gnus-newsgroup-name ingroup)
8001                         (bufferp sumbuf) 
8002                         (buffer-name sumbuf)
8003                         (not (string= prevgroup (gnus-group-group-name))))
8004               (set-buffer gnus-group-buffer)
8005               (gnus-summary-read-group 
8006                (setq prevgroup (gnus-group-group-name)) 
8007                nil no-article sumbuf))
8008             (and (string= prevgroup (gnus-group-group-name))
8009                  ;; We have reached the final group in the group
8010                  ;; buffer.
8011                  (progn
8012                    (if (buffer-name sumbuf)
8013                        (progn
8014                          (set-buffer sumbuf)
8015                          (gnus-summary-exit)))))))))))
8016
8017 (defun gnus-summary-prev-group (&optional no-article)
8018   "Exit current newsgroup and then select previous unread newsgroup.
8019 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
8020   (interactive "P")
8021   (gnus-summary-next-group no-article nil t))
8022
8023 ;; Walking around summary lines.
8024
8025 (defun gnus-summary-first-subject (&optional unread)
8026   "Go to the first unread subject.
8027 If UNREAD is non-nil, go to the first unread article.
8028 Returns nil if there are no unread articles."
8029   (interactive "P")
8030   (prog1
8031       (cond ((not unread)
8032              (goto-char (point-min)))
8033             ((gnus-goto-char 
8034               (text-property-any 
8035                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
8036              t)
8037             (t 
8038              ;; There are no unread articles.
8039              (gnus-message 3 "No more unread articles")
8040              nil))
8041     (gnus-summary-position-cursor)))
8042
8043 (defun gnus-summary-next-subject (n &optional unread dont-display)
8044   "Go to next N'th summary line.
8045 If N is negative, go to the previous N'th subject line.
8046 If UNREAD is non-nil, only unread articles are selected.
8047 The difference between N and the actual number of steps taken is
8048 returned."
8049   (interactive "p")
8050   (let ((backward (< n 0))
8051         (n (abs n)))
8052     (while (and (> n 0)
8053                 (gnus-summary-search-forward unread nil backward))
8054       (setq n (1- n)))
8055     (if (/= 0 n) (gnus-message 7 "No more%s articles"
8056                                (if unread " unread" "")))
8057     (or dont-display
8058         (progn
8059           (gnus-summary-recenter)
8060           (gnus-summary-position-cursor)))
8061     n))
8062
8063 (defun gnus-summary-next-unread-subject (n)
8064   "Go to next N'th unread summary line."
8065   (interactive "p")
8066   (gnus-summary-next-subject n t))
8067
8068 (defun gnus-summary-prev-subject (n &optional unread)
8069   "Go to previous N'th summary line.
8070 If optional argument UNREAD is non-nil, only unread article is selected."
8071   (interactive "p")
8072   (gnus-summary-next-subject (- n) unread))
8073
8074 (defun gnus-summary-prev-unread-subject (n)
8075   "Go to previous N'th unread summary line."
8076   (interactive "p")
8077   (gnus-summary-next-subject (- n) t))
8078
8079 (defun gnus-summary-goto-subject (article)
8080   "Go the subject line of ARTICLE."
8081   (interactive
8082    (list
8083     (string-to-int
8084      (completing-read "Article number: "
8085                       (mapcar
8086                        (lambda (headers)
8087                          (list
8088                           (int-to-string (mail-header-number headers))))
8089                        gnus-newsgroup-headers)
8090                       nil 'require-match))))
8091   (or article (error "No article number"))
8092   (let ((b (point)))
8093     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
8094                                                 'gnus-number article)))
8095         ()
8096       (gnus-summary-show-thread)
8097       ;; Skip dummy articles. 
8098       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8099           (forward-line 1))
8100       (prog1
8101           (if (not (eobp))
8102               article
8103             (goto-char b)
8104             nil)
8105         (gnus-summary-position-cursor)))))
8106
8107 ;; Walking around summary lines with displaying articles.
8108
8109 (defun gnus-summary-expand-window (&optional arg)
8110   "Make the summary buffer take up the entire Emacs frame.
8111 Given a prefix, will force an `article' buffer configuration."
8112   (interactive "P")
8113   (gnus-set-global-variables)
8114   (if arg
8115       (gnus-configure-windows 'article 'force)
8116     (gnus-configure-windows 'summary 'force)))
8117
8118 (defun gnus-summary-display-article (article &optional all-header)
8119   "Display ARTICLE in article buffer."
8120   (gnus-set-global-variables)
8121   (if (null article)
8122       nil
8123     (prog1
8124         (gnus-article-prepare article all-header)
8125       (gnus-summary-show-thread)
8126       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8127           (progn
8128             (forward-line 1)
8129             (gnus-summary-position-cursor)))
8130       (run-hooks 'gnus-select-article-hook)
8131       (gnus-summary-recenter)
8132       (gnus-summary-goto-subject article)
8133       ;; Successfully display article.
8134       (gnus-summary-update-line)
8135       (gnus-article-set-window-start 
8136        (cdr (assq article gnus-newsgroup-bookmarks)))
8137       t)))
8138
8139 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
8140   "Select the current article.
8141 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
8142 non-nil, the article will be re-fetched even if it already present in
8143 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
8144 be displayed."
8145   (and (not pseudo) (gnus-summary-pseudo-article)
8146        (error "This is a pseudo-article."))
8147   (let ((article (or article (gnus-summary-article-number)))
8148         (all-headers (not (not all-headers))) ;Must be T or NIL.
8149         did) 
8150     (prog1
8151         (save-excursion
8152           (set-buffer gnus-summary-buffer)
8153           (if (or (null gnus-current-article)
8154                   (null gnus-article-current)
8155                   (null (get-buffer gnus-article-buffer))
8156                   (not (eq article (cdr gnus-article-current)))
8157                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
8158                   force)
8159               ;; The requested article is different from the current article.
8160               (progn
8161                 (gnus-summary-display-article article all-headers)
8162                 (setq did article))
8163             (if (or all-headers gnus-show-all-headers) 
8164                 (gnus-article-show-all-headers))
8165             nil))
8166       (if did 
8167           (gnus-article-set-window-start 
8168            (cdr (assq article gnus-newsgroup-bookmarks)))))))
8169
8170 (defun gnus-summary-set-current-mark (&optional current-mark)
8171   "Obsolete function."
8172   nil)
8173
8174 (defun gnus-summary-next-article (&optional unread subject backward)
8175   "Select the next article.
8176 If UNREAD, only unread articles are selected.
8177 If SUBJECT, only articles with SUBJECT are selected.
8178 If BACKWARD, the previous article is selected instead of the next."
8179   (interactive "P")
8180   (gnus-set-global-variables)
8181   (let (header)
8182     (cond
8183      ;; Is there such an article?
8184      ((and (gnus-summary-search-forward unread subject backward)
8185            (or (gnus-summary-display-article (gnus-summary-article-number))
8186                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8187       (gnus-summary-position-cursor))
8188      ;; If not, we try the first unread, if that is wanted.
8189      ((and subject
8190            gnus-auto-select-same
8191            (or (gnus-summary-first-unread-article)
8192                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8193       (gnus-summary-position-cursor)
8194       (gnus-message 6 "Wrapped"))
8195      ;; Try to get next/previous article not displayed in this group.
8196      ((and gnus-auto-extend-newsgroup
8197            (not unread) (not subject)
8198            (setq header (gnus-more-header-forward backward)))
8199       (gnus-extend-newsgroup header backward)
8200       (let ((buffer-read-only nil))
8201         (goto-char (if backward (point-min) (point-max)))
8202         (gnus-summary-prepare-threads (list header)))
8203       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8204                                    gnus-newsgroup-end)))
8205      ;; Go to next/previous group.
8206      (t
8207       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
8208           (gnus-summary-jump-to-group gnus-newsgroup-name))
8209       (let ((cmd last-command-char)
8210             (group 
8211              (if (eq gnus-keep-same-level 'best) 
8212                  (gnus-summary-best-group gnus-newsgroup-name)
8213                (gnus-summary-search-group backward gnus-keep-same-level))))
8214         ;; For some reason, the group window gets selected. We change
8215         ;; it back.  
8216         (select-window (get-buffer-window (current-buffer)))
8217         ;; Keep just the event type of CMD.
8218                                         ;(and (listp cmd) (setq cmd (car cmd)))
8219         ;; Select next unread newsgroup automagically.
8220         (cond 
8221          ((not gnus-auto-select-next)
8222           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8223          ((eq gnus-auto-select-next 'quietly)
8224           ;; Select quietly.
8225           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
8226               (gnus-summary-exit)
8227             (gnus-message 7 "No more%s articles (%s)..."
8228                           (if unread " unread" "") 
8229                           (if group (concat "selecting " group)
8230                             "exiting"))
8231             (gnus-summary-next-group nil group backward)))
8232          (t
8233           (let ((keystrokes '(?\C-n ?\C-p))
8234                 key)
8235             (while (or (null key) (memq key keystrokes))
8236               (gnus-message 
8237                7 "No more%s articles%s" (if unread " unread" "")
8238                (if (and group 
8239                         (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
8240                    (format " (Type %s for %s [%s])"
8241                            (single-key-description cmd) group
8242                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8243                  (format " (Type %s to exit %s)"
8244                          (single-key-description cmd)
8245                          gnus-newsgroup-name)))
8246               ;; Confirm auto selection.
8247               (let* ((event (read-char)))
8248                 (setq key (if (listp event) (car event) event))
8249                 (if (memq key keystrokes)
8250                     (let ((obuf (current-buffer)))
8251                       (switch-to-buffer gnus-group-buffer)
8252                       (and group
8253                            (gnus-group-jump-to-group group))
8254                       (condition-case ()
8255                           (execute-kbd-macro (char-to-string key))
8256                         (error (ding) nil))
8257                       (setq group (gnus-group-group-name))
8258                       (switch-to-buffer obuf)))))
8259             (if (equal key cmd)
8260                 (if (or (not group)
8261                         (gnus-ephemeral-group-p gnus-newsgroup-name))
8262                     (gnus-summary-exit)
8263                   (gnus-summary-next-group nil group backward))
8264               (execute-kbd-macro (char-to-string key)))))))))))
8265
8266 (defun gnus-summary-next-unread-article ()
8267   "Select unread article after current one."
8268   (interactive)
8269   (gnus-summary-next-article t (and gnus-auto-select-same
8270                                     (gnus-summary-subject-string))))
8271
8272 (defun gnus-summary-prev-article (&optional unread subject)
8273   "Select the article after the current one.
8274 If UNREAD is non-nil, only unread articles are selected."
8275   (interactive "P")
8276   (gnus-summary-next-article unread subject t))
8277
8278 (defun gnus-summary-prev-unread-article ()
8279   "Select unred article before current one."
8280   (interactive)
8281   (gnus-summary-prev-article t (and gnus-auto-select-same
8282                                     (gnus-summary-subject-string))))
8283
8284 (defun gnus-summary-next-page (&optional lines circular)
8285   "Show next page of selected article.
8286 If end of article, select next article.
8287 Argument LINES specifies lines to be scrolled up.
8288 If CIRCULAR is non-nil, go to the start of the article instead of 
8289 instead of selecting the next article when reaching the end of the
8290 current article." 
8291   (interactive "P")
8292   (setq gnus-summary-buffer (current-buffer))
8293   (gnus-set-global-variables)
8294   (let ((article (gnus-summary-article-number))
8295         (endp nil))
8296     (gnus-configure-windows 'article)
8297     (if (or (null gnus-current-article)
8298             (null gnus-article-current)
8299             (/= article (cdr gnus-article-current))
8300             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8301         ;; Selected subject is different from current article's.
8302         (gnus-summary-display-article article)
8303       (gnus-eval-in-buffer-window
8304        gnus-article-buffer
8305        (setq endp (gnus-article-next-page lines)))
8306       (if endp
8307           (cond (circular
8308                  (gnus-summary-beginning-of-article))
8309                 (lines
8310                  (gnus-message 3 "End of message"))
8311                 ((null lines)
8312                  (gnus-summary-next-unread-article)))))
8313     (gnus-summary-recenter)
8314     (gnus-summary-position-cursor)))
8315
8316 (defun gnus-summary-prev-page (&optional lines)
8317   "Show previous page of selected article.
8318 Argument LINES specifies lines to be scrolled down."
8319   (interactive "P")
8320   (gnus-set-global-variables)
8321   (let ((article (gnus-summary-article-number)))
8322     (gnus-configure-windows 'article)
8323     (if (or (null gnus-current-article)
8324             (null gnus-article-current)
8325             (/= article (cdr gnus-article-current))
8326             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8327         ;; Selected subject is different from current article's.
8328         (gnus-summary-display-article article)
8329       (gnus-summary-recenter)
8330       (gnus-eval-in-buffer-window gnus-article-buffer
8331                                   (gnus-article-prev-page lines))))
8332   (gnus-summary-position-cursor))
8333
8334 (defun gnus-summary-scroll-up (lines)
8335   "Scroll up (or down) one line current article.
8336 Argument LINES specifies lines to be scrolled up (or down if negative)."
8337   (interactive "p")
8338   (gnus-set-global-variables)
8339   (gnus-configure-windows 'article)
8340   (or (gnus-summary-select-article nil nil 'pseudo)
8341       (gnus-eval-in-buffer-window 
8342        gnus-article-buffer
8343        (cond ((> lines 0)
8344               (if (gnus-article-next-page lines)
8345                   (gnus-message 3 "End of message")))
8346              ((< lines 0)
8347               (gnus-article-prev-page (- lines))))))
8348   (gnus-summary-recenter)
8349   (gnus-summary-position-cursor))
8350
8351 (defun gnus-summary-next-same-subject ()
8352   "Select next article which has the same subject as current one."
8353   (interactive)
8354   (gnus-set-global-variables)
8355   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8356
8357 (defun gnus-summary-prev-same-subject ()
8358   "Select previous article which has the same subject as current one."
8359   (interactive)
8360   (gnus-set-global-variables)
8361   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8362
8363 (defun gnus-summary-next-unread-same-subject ()
8364   "Select next unread article which has the same subject as current one."
8365   (interactive)
8366   (gnus-set-global-variables)
8367   (gnus-summary-next-article t (gnus-summary-subject-string)))
8368
8369 (defun gnus-summary-prev-unread-same-subject ()
8370   "Select previous unread article which has the same subject as current one."
8371   (interactive)
8372   (gnus-set-global-variables)
8373   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8374
8375 (defun gnus-summary-first-unread-article ()
8376   "Select the first unread article. 
8377 Return nil if there are no unread articles."
8378   (interactive)
8379   (gnus-set-global-variables)
8380   (prog1
8381       (if (gnus-summary-first-subject t)
8382           (progn
8383             (gnus-summary-show-thread)
8384             (gnus-summary-first-subject t)
8385             (gnus-summary-display-article (gnus-summary-article-number))))
8386     (gnus-summary-position-cursor)))
8387
8388 (defun gnus-summary-best-unread-article ()
8389   "Select the unread article with the highest score."
8390   (interactive)
8391   (gnus-set-global-variables)
8392   (let ((best -1000000)
8393         article score)
8394     (save-excursion
8395       (or (gnus-summary-first-subject t)
8396           (error "No unread articles"))
8397       (while 
8398           (and
8399            (progn
8400              (and (> (setq score (gnus-summary-article-score)) best)
8401                   (setq best score
8402                         article (gnus-summary-article-number)))
8403              t)
8404            (gnus-summary-search-subject nil t))))
8405     (if (not article)
8406         (error "No unread articles")
8407       (gnus-summary-goto-article article))
8408     (gnus-summary-position-cursor)))
8409
8410 (defun gnus-summary-goto-article (article &optional all-headers)
8411   "Fetch ARTICLE and display it if it exists.
8412 If ALL-HEADERS is non-nil, no header lines are hidden."
8413   (interactive
8414    (list
8415     (string-to-int
8416      (completing-read 
8417       "Article number: "
8418       (mapcar (lambda (headers) 
8419                 (list (int-to-string (mail-header-number headers))))
8420               gnus-newsgroup-headers) 
8421       nil 'require-match))))
8422   (prog1
8423       (and (gnus-summary-goto-subject article)
8424            (gnus-summary-display-article article all-headers))
8425     (gnus-summary-position-cursor)))
8426
8427 (defun gnus-summary-goto-last-article ()
8428   "Go to the previously read article."
8429   (interactive)
8430   (prog1
8431       (and gnus-last-article
8432            (gnus-summary-goto-article gnus-last-article))
8433     (gnus-summary-position-cursor)))
8434
8435 (defun gnus-summary-pop-article (number)
8436   "Pop one article off the history and go to the previous.
8437 NUMBER articles will be popped off."
8438   (interactive "p")
8439   (let (to)
8440     (setq gnus-newsgroup-history
8441           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8442     (if to
8443         (gnus-summary-goto-article (car to))
8444       (error "Article history empty")))
8445   (gnus-summary-position-cursor))
8446
8447 ;; Summary article oriented commands
8448
8449 (defun gnus-summary-refer-parent-article (n)
8450   "Refer parent article N times.
8451 The difference between N and the number of articles fetched is returned."
8452   (interactive "p")
8453   (gnus-set-global-variables)
8454   (while 
8455       (and 
8456        (> n 0)
8457        (let ((ref (mail-header-references (gnus-get-header-by-num
8458                                            (gnus-summary-article-number)))))
8459          (if (and ref (not (equal ref ""))
8460                   (string-match "<[^<>]*>[ \t]*$" ref))
8461              (gnus-summary-refer-article 
8462               (substring ref (match-beginning 0) (match-end 0)))
8463            (gnus-message 1 "No references in article %d"
8464                          (gnus-summary-article-number))
8465            nil)))
8466     (setq n (1- n)))
8467   (gnus-summary-position-cursor)
8468   n)
8469     
8470 (defun gnus-summary-refer-article (message-id)
8471   "Refer article specified by MESSAGE-ID.
8472 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8473   (interactive "sMessage-ID: ")
8474   (if (or (not (stringp message-id))
8475           (zerop (length message-id)))
8476       ()
8477     ;; Construct the correct Message-ID if necessary.
8478     ;; Suggested by tale@pawl.rpi.edu.
8479     (or (string-match "^<" message-id)
8480         (setq message-id (concat "<" message-id)))
8481     (or (string-match ">$" message-id)
8482         (setq message-id (concat message-id ">")))
8483     (let ((header (car (gnus-gethash (downcase message-id)
8484                                      gnus-newsgroup-dependencies))))
8485       (if header
8486           (or (gnus-summary-goto-article (mail-header-number header))
8487               ;; The header has been read, but the article had been
8488               ;; expunged, so we insert it again.
8489               (progn
8490                 (gnus-summary-insert-line
8491                  nil header 0 nil gnus-read-mark nil nil
8492                  (mail-header-subject header))
8493                 (forward-line -1)
8494                 (mail-header-number header)))
8495         (let ((gnus-override-method gnus-refer-article-method)
8496               (gnus-ancient-mark gnus-read-mark)
8497               (tmp-point (window-start
8498                           (get-buffer-window gnus-article-buffer)))
8499               number tmp-buf)
8500           (and gnus-refer-article-method
8501                (gnus-check-server gnus-refer-article-method))
8502           ;; Save the old article buffer.
8503           (save-excursion
8504             (set-buffer (gnus-article-setup-buffer))
8505             (gnus-kill-buffer " *temp Article*")
8506             (setq tmp-buf (rename-buffer " *temp Article*")))
8507           (prog1
8508               (if (gnus-article-prepare 
8509                    message-id nil (gnus-read-header message-id))
8510                   (progn
8511                     (setq number (mail-header-number gnus-current-headers))
8512                     (gnus-rebuild-thread message-id)
8513                     (gnus-summary-goto-subject number)
8514                     (if (null gnus-use-full-window)
8515                         (progn
8516                           (delete-windows-on tmp-buf)
8517                           (gnus-configure-windows 'article 'force)))
8518                     (gnus-summary-recenter)
8519                     (gnus-article-set-window-start 
8520                      (cdr (assq number gnus-newsgroup-bookmarks)))
8521                     (and gnus-visual
8522                          (run-hooks 'gnus-visual-mark-article-hook))
8523                     message-id)
8524                 ;; We restore the old article buffer.
8525                 (save-excursion
8526                   (kill-buffer gnus-article-buffer)
8527                   (set-buffer tmp-buf)
8528                   (rename-buffer gnus-article-buffer)
8529                   (let ((buffer-read-only nil))
8530                     (and tmp-point
8531                          (set-window-start (get-buffer-window (current-buffer))
8532                                            tmp-point)))))))))))
8533
8534 (defun gnus-summary-enter-digest-group ()
8535   "Enter a digest group based on the current article."
8536   (interactive)
8537   (gnus-set-global-variables)
8538   (gnus-summary-select-article)
8539   ;; We do not want a narrowed article.
8540   (gnus-summary-stop-page-breaking)
8541   (let ((name (format "%s-%d" 
8542                       (gnus-group-prefixed-name 
8543                        gnus-newsgroup-name (list 'nndoc "")) 
8544                       gnus-current-article))
8545         (ogroup gnus-newsgroup-name)
8546         (buf (current-buffer)))
8547     (if (gnus-group-read-ephemeral-group 
8548          name (list 'nndoc name
8549                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8550                     '(nndoc-article-type digest))
8551          t)
8552         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8553                 (list (list (cons 'to-group ogroup))))
8554       (switch-to-buffer buf)
8555       (gnus-set-global-variables)
8556       (gnus-configure-windows 'summary)
8557       (gnus-message 3 "Article not a digest?"))))
8558
8559 (defun gnus-summary-isearch-article ()
8560   "Do incremental search forward on current article."
8561   (interactive)
8562   (gnus-set-global-variables)
8563   (gnus-summary-select-article)
8564   (gnus-eval-in-buffer-window 
8565    gnus-article-buffer (isearch-forward)))
8566
8567 (defun gnus-summary-search-article-forward (regexp &optional backward)
8568   "Search for an article containing REGEXP forward.
8569 If BACKWARD, search backward instead."
8570   (interactive
8571    (list (read-string
8572           (format "Search article %s (regexp%s): "
8573                   (if current-prefix-arg "backward" "forward")
8574                   (if gnus-last-search-regexp
8575                       (concat ", default " gnus-last-search-regexp)
8576                     "")))
8577          current-prefix-arg))
8578   (gnus-set-global-variables)
8579   (if (string-equal regexp "")
8580       (setq regexp (or gnus-last-search-regexp ""))
8581     (setq gnus-last-search-regexp regexp))
8582   (if (gnus-summary-search-article regexp backward)
8583       (gnus-article-set-window-start 
8584        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8585     (error "Search failed: \"%s\"" regexp)))
8586
8587 (defun gnus-summary-search-article-backward (regexp)
8588   "Search for an article containing REGEXP backward."
8589   (interactive
8590    (list (read-string
8591           (format "Search article backward (regexp%s): "
8592                   (if gnus-last-search-regexp
8593                       (concat ", default " gnus-last-search-regexp)
8594                     "")))))
8595   (gnus-summary-search-article-forward regexp 'backward))
8596
8597 (defun gnus-summary-search-article (regexp &optional backward)
8598   "Search for an article containing REGEXP.
8599 Optional argument BACKWARD means do search for backward.
8600 gnus-select-article-hook is not called during the search."
8601   (let ((gnus-select-article-hook nil)  ;Disable hook.
8602         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8603         (re-search
8604          (if backward
8605              (function re-search-backward) (function re-search-forward)))
8606         (found nil)
8607         (last nil))
8608     ;; Hidden thread subtrees must be searched for ,too.
8609     (gnus-summary-show-all-threads)
8610     (if (eobp) (forward-line -1))
8611     ;; First of all, search current article.
8612     ;; We don't want to read article again from NNTP server nor reset
8613     ;; current point.
8614     (gnus-summary-select-article)
8615     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8616     (setq last gnus-current-article)
8617     (gnus-eval-in-buffer-window
8618      gnus-article-buffer
8619      (save-restriction
8620        (widen)
8621        ;; Begin search from current point.
8622        (setq found (funcall re-search regexp nil t))))
8623     ;; Then search next articles.
8624     (while (and (not found)
8625                 (gnus-summary-display-article 
8626                  (gnus-summary-search-subject backward nil nil)))
8627       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8628       (gnus-eval-in-buffer-window
8629        gnus-article-buffer
8630        (save-restriction
8631          (widen)
8632          (goto-char (if backward (point-max) (point-min)))
8633          (setq found (funcall re-search regexp nil t)))))
8634     (message "")
8635     ;; Adjust article pointer.
8636     (or (eq last gnus-current-article)
8637         (setq gnus-last-article last))
8638     ;; Return T if found such article.
8639     found))
8640
8641 (defun gnus-summary-execute-command (header regexp command &optional backward)
8642   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8643 If HEADER is an empty string (or nil), the match is done on the entire
8644 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8645   (interactive
8646    (list (let ((completion-ignore-case t))
8647            (completing-read 
8648             "Header name: "
8649             (mapcar (lambda (string) (list string))
8650                     '("Number" "Subject" "From" "Lines" "Date"
8651                       "Message-ID" "Xref" "References"))
8652             nil 'require-match))
8653          (read-string "Regexp: ")
8654          (read-key-sequence "Command: ")
8655          current-prefix-arg))
8656   (gnus-set-global-variables)
8657   ;; Hidden thread subtrees must be searched as well.
8658   (gnus-summary-show-all-threads)
8659   ;; We don't want to change current point nor window configuration.
8660   (save-excursion
8661     (save-window-excursion
8662       (gnus-message 6 "Executing %s..." (key-description command))
8663       ;; We'd like to execute COMMAND interactively so as to give arguments.
8664       (gnus-execute header regexp
8665                     (` (lambda ()
8666                          (call-interactively '(, (key-binding command)))))
8667                     backward)
8668       (gnus-message 6 "Executing %s...done" (key-description command)))))
8669
8670 (defun gnus-summary-beginning-of-article ()
8671   "Scroll the article back to the beginning."
8672   (interactive)
8673   (gnus-set-global-variables)
8674   (gnus-summary-select-article)
8675   (gnus-configure-windows 'article)
8676   (gnus-eval-in-buffer-window
8677    gnus-article-buffer
8678    (widen)
8679    (goto-char (point-min))
8680    (and gnus-break-pages (gnus-narrow-to-page))))
8681
8682 (defun gnus-summary-end-of-article ()
8683   "Scroll to the end of the article."
8684   (interactive)
8685   (gnus-set-global-variables)
8686   (gnus-summary-select-article)
8687   (gnus-configure-windows 'article)
8688   (gnus-eval-in-buffer-window 
8689    gnus-article-buffer
8690    (widen)
8691    (goto-char (point-max))
8692    (recenter -3)
8693    (and gnus-break-pages (gnus-narrow-to-page))))
8694
8695 (defun gnus-summary-show-article ()
8696   "Force re-fetching of the current article."
8697   (interactive)
8698   (gnus-set-global-variables)
8699   (gnus-summary-select-article nil 'force)
8700   (gnus-configure-windows 'article)
8701   (gnus-summary-position-cursor))
8702
8703 (defun gnus-summary-verbose-headers (&optional arg)
8704   "Toggle permanent full header display.
8705 If ARG is a positive number, turn header display on.
8706 If ARG is a negative number, turn header display off."
8707   (interactive "P")
8708   (gnus-set-global-variables)
8709   (gnus-summary-toggle-header arg)
8710   (setq gnus-show-all-headers
8711         (cond ((or (not (numberp arg))
8712                    (zerop arg))
8713                (not gnus-show-all-headers))
8714               ((natnump arg)
8715                t))))
8716
8717 (defun gnus-summary-toggle-header (&optional arg)
8718   "Show the headers if they are hidden, or hide them if they are shown.
8719 If ARG is a positive number, show the entire header.
8720 If ARG is a negative number, hide the unwanted header lines."
8721   (interactive "P")
8722   (gnus-set-global-variables)
8723   (save-excursion
8724     (set-buffer gnus-article-buffer)
8725     (let ((buffer-read-only nil))
8726       (if (numberp arg) 
8727           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8728                                                 gnus-hidden-properties)
8729             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8730         (if (text-property-any (point-min) (point-max) 'invisible t)
8731             (remove-text-properties 
8732              (point-min) (point-max) gnus-hidden-properties)
8733           ;; We hide the headers. This song and dance act below is
8734           ;; done because `gnus-have-all-headers' is buffer-local to
8735           ;; the summary buffer, and we only want to temporarily
8736           ;; change it in that buffer. Ugh.
8737           (let ((have gnus-have-all-headers))
8738             (save-excursion
8739               (set-buffer gnus-summary-buffer)
8740               (setq gnus-have-all-headers nil)
8741               (save-excursion
8742                 (set-buffer gnus-article-buffer)
8743                 (run-hooks 'gnus-article-display-hook))
8744               (setq gnus-have-all-headers have)))))
8745       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8746
8747 (defun gnus-summary-show-all-headers ()
8748   "Make all header lines visible."
8749   (interactive)
8750   (gnus-set-global-variables)
8751   (gnus-article-show-all-headers))
8752
8753 (defun gnus-summary-toggle-mime (&optional arg)
8754   "Toggle MIME processing.
8755 If ARG is a positive number, turn MIME processing on."
8756   (interactive "P")
8757   (gnus-set-global-variables)
8758   (setq gnus-show-mime
8759         (if (null arg) (not gnus-show-mime)
8760           (> (prefix-numeric-value arg) 0)))
8761   (gnus-summary-select-article t 'force))
8762
8763 (defun gnus-summary-caesar-message (&optional arg)
8764   "Caesar rotate the current article by 13.
8765 The numerical prefix specifies how manu places to rotate each letter
8766 forward."
8767   (interactive "P")
8768   (gnus-set-global-variables)
8769   (gnus-summary-select-article)
8770   (let ((mail-header-separator ""))
8771     (gnus-eval-in-buffer-window 
8772      gnus-article-buffer
8773      (save-restriction
8774        (widen)
8775        (let ((start (window-start)))
8776          (news-caesar-buffer-body arg)
8777          (set-window-start (get-buffer-window (current-buffer)) start))))))
8778
8779 (defun gnus-summary-stop-page-breaking ()
8780   "Stop page breaking in the current article."
8781   (interactive)
8782   (gnus-set-global-variables)
8783   (gnus-summary-select-article)
8784   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8785
8786 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8787
8788 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8789   "Move the current article to a different newsgroup.
8790 If N is a positive number, move the N next articles.
8791 If N is a negative number, move the N previous articles.
8792 If N is nil and any articles have been marked with the process mark,
8793 move those articles instead.
8794 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8795 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8796 re-spool using this method.
8797 For this function to work, both the current newsgroup and the
8798 newsgroup that you want to move to have to support the `request-move'
8799 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8800   (interactive "P")
8801   (gnus-set-global-variables)
8802   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8803       (error "The current newsgroup does not support article moving"))
8804   (let ((articles (gnus-summary-work-articles n))
8805         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8806         art-group to-method sel-met)
8807     (if (and (not to-newsgroup) (not select-method))
8808         (setq to-newsgroup
8809               (completing-read 
8810                (format "Where do you want to move %s? %s"
8811                        (if (> (length articles) 1)
8812                            (format "these %d articles" (length articles))
8813                          "this article")
8814                        (if gnus-current-move-group
8815                            (format "(%s default) " gnus-current-move-group)
8816                          ""))
8817                gnus-active-hashtb nil nil prefix)))
8818     (if to-newsgroup
8819         (progn
8820           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8821               (setq to-newsgroup (or gnus-current-move-group "")))
8822           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8823               (gnus-activate-group to-newsgroup)
8824               (error "No such group: %s" to-newsgroup))
8825           (setq gnus-current-move-group to-newsgroup)))
8826     (setq to-method (if select-method (list select-method "")
8827                       (gnus-find-method-for-group to-newsgroup)))
8828     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8829         (error "%s does not support article copying" (car to-method)))
8830     (or (gnus-check-server to-method)
8831         (error "Can't open server %s" (car to-method)))
8832     (gnus-message 6 "Moving to %s: %s..." 
8833                   (or select-method to-newsgroup) articles)
8834     (while articles
8835       (if (setq art-group
8836                 (gnus-request-move-article 
8837                  (car articles)         ; Article to move
8838                  gnus-newsgroup-name    ; From newsgrouo
8839                  (nth 1 (gnus-find-method-for-group 
8840                          gnus-newsgroup-name)) ; Server
8841                  (list 'gnus-request-accept-article 
8842                        (if select-method
8843                            (list 'quote select-method)
8844                          to-newsgroup)
8845                        (not (cdr articles))) ; Accept form
8846                  (not (cdr articles)))) ; Only save nov last time
8847           (let* ((buffer-read-only nil)
8848                  (entry 
8849                   (or
8850                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8851                    (gnus-gethash 
8852                     (gnus-group-prefixed-name 
8853                      (car art-group) 
8854                      (if select-method (list select-method "")
8855                        (gnus-find-method-for-group to-newsgroup)))
8856                     gnus-newsrc-hashtb)))
8857                  (info (nth 2 entry))
8858                  (article (car articles)))
8859             (gnus-summary-goto-subject article)
8860             (beginning-of-line)
8861             (delete-region (point) (progn (forward-line 1) (point)))
8862             ;; Update the group that has been moved to.
8863             (if (not info)
8864                 ()                      ; This group does not exist yet.
8865               (if (not (memq article gnus-newsgroup-unreads))
8866                   (setcar (cdr (cdr info))
8867                           (gnus-add-to-range (nth 2 info) 
8868                                              (list (cdr art-group)))))
8869               ;; Copy any marks over to the new group.
8870               (let ((marks '((tick . gnus-newsgroup-marked)
8871                              (dormant . gnus-newsgroup-dormant)
8872                              (expire . gnus-newsgroup-expirable)
8873                              (bookmark . gnus-newsgroup-bookmarks)
8874                              (reply . gnus-newsgroup-replied)))
8875                     (to-article (cdr art-group)))
8876                 (while marks
8877                   (if (memq article (symbol-value (cdr (car marks))))
8878                       (gnus-add-marked-articles 
8879                        (car info) (car (car marks)) (list to-article) info))
8880                   (setq marks (cdr marks)))))
8881             ;; Update marks.
8882             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8883             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8884             (setq gnus-newsgroup-dormant
8885                   (delq article gnus-newsgroup-dormant))
8886             (setq gnus-newsgroup-reads
8887                   (cons (cons article gnus-canceled-mark)
8888                         gnus-newsgroup-reads)))
8889         (gnus-message 1 "Couldn't move article %s" (car articles)))
8890       (gnus-summary-remove-process-mark (car articles))
8891       (setq articles (cdr articles)))
8892     (gnus-set-mode-line 'summary)))
8893
8894 (defun gnus-summary-respool-article (&optional n respool-method)
8895   "Respool the current article.
8896 The article will be squeezed through the mail spooling process again,
8897 which means that it will be put in some mail newsgroup or other
8898 depending on `nnmail-split-methods'.
8899 If N is a positive number, respool the N next articles.
8900 If N is a negative number, respool the N previous articles.
8901 If N is nil and any articles have been marked with the process mark,
8902 respool those articles instead.
8903
8904 Respooling can be done both from mail groups and \"real\" newsgroups.
8905 In the former case, the articles in question will be moved from the
8906 current group into whatever groups they are destined to.  In the
8907 latter case, they will be copied into the relevant groups."
8908   (interactive "P")
8909   (gnus-set-global-variables)
8910   (let ((respool-methods (gnus-methods-using 'respool))
8911         (methname 
8912          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8913     (or respool-method
8914         (setq respool-method
8915               (completing-read
8916                "What method do you want to use when respooling? "
8917                respool-methods nil t methname)))
8918     (or (string= respool-method "")
8919         (if (assoc (symbol-name
8920                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8921                    respool-methods)
8922             (gnus-summary-move-article n nil (intern respool-method))
8923           (gnus-summary-copy-article n nil (intern respool-method))))))
8924
8925 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8926 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8927   "Move the current article to a different newsgroup.
8928 If N is a positive number, move the N next articles.
8929 If N is a negative number, move the N previous articles.
8930 If N is nil and any articles have been marked with the process mark,
8931 move those articles instead.
8932 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8933 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8934 re-spool using this method.
8935 For this function to work, the newsgroup that you want to move to have
8936 to support the `request-move' and `request-accept'
8937 functions. (Ie. mail newsgroups at present.)"
8938   (interactive "P")
8939   (gnus-set-global-variables)
8940   (let ((articles (gnus-summary-work-articles n))
8941         (copy-buf (get-buffer-create "*copy work*"))
8942         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8943         art-group to-method)
8944     (buffer-disable-undo copy-buf)
8945     (if (and (not to-newsgroup) (not select-method))
8946         (setq to-newsgroup
8947               (completing-read 
8948                (format "Where do you want to copy %s? %s"
8949                        (if (> (length articles) 1)
8950                            (format "these %d articles" (length articles))
8951                          "this article")
8952                        (if gnus-current-move-group
8953                            (format "(%s default) " gnus-current-move-group)
8954                          ""))
8955                gnus-active-hashtb nil nil prefix)))
8956     (if to-newsgroup
8957         (progn
8958           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8959               (setq to-newsgroup (or gnus-current-move-group "")))
8960           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8961               (gnus-activate-group to-newsgroup)
8962               (error "No such group: %s" to-newsgroup))
8963           (setq gnus-current-move-group to-newsgroup)))
8964     (setq to-method (if select-method (list select-method "")
8965                       (gnus-find-method-for-group to-newsgroup)))
8966     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8967         (error "%s does not support article copying" (car to-method)))
8968     (or (gnus-check-server to-method)
8969         (error "Can't open server %s" (car to-method)))
8970     (while articles
8971       (gnus-message 6 "Copying to %s: %s..." 
8972                     (or select-method to-newsgroup) articles)
8973       (if (setq art-group
8974                 (save-excursion
8975                   (set-buffer copy-buf)
8976                   (gnus-request-article-this-buffer
8977                    (car articles) gnus-newsgroup-name)
8978                   (gnus-request-accept-article
8979                    (if select-method (list 'quote select-method) to-newsgroup)
8980                    (not (cdr articles)))))
8981           (let* ((entry 
8982                   (or
8983                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8984                    (gnus-gethash 
8985                     (gnus-group-prefixed-name 
8986                      (car art-group) 
8987                      (if select-method (list select-method "")
8988                        (gnus-find-method-for-group to-newsgroup)))
8989                     gnus-newsrc-hashtb)))
8990                  (info (nth 2 entry))
8991                  (article (car articles)))
8992             ;; We copy the info over to the new group.
8993             (if (not info)
8994                 ()                      ; This group does not exist (yet).
8995               (if (not (memq article gnus-newsgroup-unreads))
8996                   (setcar (cdr (cdr info))
8997                           (gnus-add-to-range (nth 2 info) 
8998                                              (list (cdr art-group)))))
8999               ;; Copy any marks over to the new group.
9000               (let ((marks '((tick . gnus-newsgroup-marked)
9001                              (dormant . gnus-newsgroup-dormant)
9002                              (expire . gnus-newsgroup-expirable)
9003                              (bookmark . gnus-newsgroup-bookmarks)
9004                              (reply . gnus-newsgroup-replied)))
9005                     (to-article (cdr art-group)))
9006                 (while marks
9007                   (if (memq article (symbol-value (cdr (car marks))))
9008                       (gnus-add-marked-articles 
9009                        (car info) (car (car marks)) (list to-article) info))
9010                   (setq marks (cdr marks))))))
9011         (gnus-message 1 "Couldn't copy article %s" (car articles)))
9012       (gnus-summary-remove-process-mark (car articles))
9013       (setq articles (cdr articles)))
9014     (kill-buffer copy-buf)))
9015
9016 (defun gnus-summary-import-article (file)
9017   "Import a random file into a mail newsgroup."
9018   (interactive "fImport file: ")
9019   (let ((group gnus-newsgroup-name)
9020         atts)
9021     (or (gnus-check-backend-function 'request-accept-article group)
9022         (error "%s does not support article importing" group))
9023     (or (file-readable-p file)
9024         (not (file-regular-p file))
9025         (error "Can't read %s" file))
9026     (save-excursion
9027       (set-buffer (get-buffer-create " *import file*"))
9028       (buffer-disable-undo (current-buffer))
9029       (erase-buffer)
9030       (insert-file-contents file)
9031       (goto-char (point-min))
9032       (if (nnheader-article-p)
9033           ()
9034         (setq atts (file-attributes file))
9035         (insert "From: " (read-string "From: ") "\n"
9036                 "Subject: " (read-string "Subject: ") "\n"
9037                 "Date: " (current-time-string (nth 5 atts)) "\n"
9038                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9039       (gnus-request-accept-article group t)
9040       (kill-buffer (current-buffer)))))
9041
9042 (defun gnus-summary-expire-articles ()
9043   "Expire all articles that are marked as expirable in the current group."
9044   (interactive)
9045   (if (not (gnus-check-backend-function 
9046             'request-expire-articles gnus-newsgroup-name))
9047       ()
9048     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
9049                                       gnus-newsrc-hashtb)))
9050            (total (memq 'total-expire (nth 5 info)))
9051            (expirable (if total
9052                           (gnus-list-of-read-articles gnus-newsgroup-name)
9053                         (setq gnus-newsgroup-expirable
9054                               (sort gnus-newsgroup-expirable '<))))
9055            es)
9056       (if (not expirable)
9057           ()
9058         (gnus-message 6 "Expiring articles...")
9059         ;; The list of articles that weren't expired is returned.
9060         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
9061         (or total (setq gnus-newsgroup-expirable es))
9062         ;; We go through the old list of expirable, and mark all
9063         ;; really expired articles as non-existant.
9064         (or (eq es expirable)           ;If nothing was expired, we don't mark.
9065             (let ((gnus-use-cache nil))
9066               (while expirable
9067                 (or (memq (car expirable) es)
9068                     (gnus-summary-mark-article
9069                      (car expirable) gnus-canceled-mark))
9070                 (setq expirable (cdr expirable)))))
9071         (gnus-message 6 "Expiring articles...done")))))
9072
9073 (defun gnus-summary-expire-articles-now ()
9074   "Expunge all expirable articles in the current group.
9075 This means that *all* articles that are marked as expirable will be
9076 deleted forever, right now."
9077   (interactive)
9078   (or gnus-expert-user
9079       (gnus-y-or-n-p
9080        "Are you really, really, really sure you want to expunge? ")
9081       (error "Phew!"))
9082   (let ((nnmail-expiry-wait -1)
9083         (nnmail-expiry-wait-function nil))
9084     (gnus-summary-expire-articles)))
9085
9086 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9087 (defun gnus-summary-delete-article (&optional n)
9088   "Delete the N next (mail) articles.
9089 This command actually deletes articles. This is not a marking
9090 command. The article will disappear forever from you life, never to
9091 return. 
9092 If N is negative, delete backwards.
9093 If N is nil and articles have been marked with the process mark,
9094 delete these instead."
9095   (interactive "P")
9096   (or (gnus-check-backend-function 'request-expire-articles 
9097                                    gnus-newsgroup-name)
9098       (error "The current newsgroup does not support article deletion."))
9099   ;; Compute the list of articles to delete.
9100   (let ((articles (gnus-summary-work-articles n))
9101         not-deleted)
9102     (if (and gnus-novice-user
9103              (not (gnus-y-or-n-p 
9104                    (format "Do you really want to delete %s forever? "
9105                            (if (> (length articles) 1) "these articles"
9106                              "this article")))))
9107         ()
9108       ;; Delete the articles.
9109       (setq not-deleted (gnus-request-expire-articles 
9110                          articles gnus-newsgroup-name 'force))
9111       (while articles
9112         (gnus-summary-remove-process-mark (car articles))       
9113         ;; The backend might not have been able to delete the article
9114         ;; after all.  
9115         (or (memq (car articles) not-deleted)
9116             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9117         (setq articles (cdr articles))))
9118     (gnus-summary-position-cursor)
9119     (gnus-set-mode-line 'summary)
9120     not-deleted))
9121
9122 (defun gnus-summary-edit-article (&optional force)
9123   "Enter into a buffer and edit the current article.
9124 This will have permanent effect only in mail groups.
9125 If FORCE is non-nil, allow editing of articles even in read-only
9126 groups."
9127   (interactive "P")
9128   (or force
9129       (not (gnus-group-read-only-p))
9130       (error "The current newsgroup does not support article editing."))
9131   (gnus-summary-select-article t)
9132   (gnus-configure-windows 'article)
9133   (select-window (get-buffer-window gnus-article-buffer))
9134   (gnus-message 6 "C-c C-c to end edits")
9135   (setq buffer-read-only nil)
9136   (text-mode)
9137   (use-local-map (copy-keymap (current-local-map)))
9138   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
9139   (buffer-enable-undo)
9140   (widen)
9141   (goto-char (point-min))
9142   (search-forward "\n\n" nil t))
9143
9144 (defun gnus-summary-edit-article-done ()
9145   "Make edits to the current article permanent."
9146   (interactive)
9147   (if (gnus-group-read-only-p)
9148       (progn
9149         (gnus-summary-edit-article-postpone)
9150         (message "The current newsgroup does not support article editing.")
9151         (ding))
9152     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
9153       (erase-buffer)
9154       (insert buf)
9155       (if (not (gnus-request-replace-article 
9156                 (cdr gnus-article-current) (car gnus-article-current) 
9157                 (current-buffer)))
9158           (error "Couldn't replace article.")
9159         (gnus-article-mode)
9160         (use-local-map gnus-article-mode-map)
9161         (setq buffer-read-only t)
9162         (buffer-disable-undo (current-buffer))
9163         (gnus-configure-windows 'summary))
9164       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))))
9165
9166 (defun gnus-summary-edit-article-postpone ()
9167   "Postpone changes to the current article."
9168   (interactive)
9169   (gnus-article-mode)
9170   (use-local-map gnus-article-mode-map)
9171   (setq buffer-read-only t)
9172   (buffer-disable-undo (current-buffer))
9173   (gnus-configure-windows 'summary)
9174   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9175
9176 (defun gnus-summary-fancy-query ()
9177   "Query where the fancy respool algorithm would put this article."
9178   (interactive)
9179   (gnus-summary-select-article)
9180   (save-excursion
9181     (set-buffer gnus-article-buffer)
9182     (save-restriction
9183       (goto-char (point-min))
9184       (search-forward "\n\n")
9185       (narrow-to-region (point-min) (point))
9186       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9187
9188 ;; Summary score commands.
9189
9190 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9191
9192 (defun gnus-summary-raise-score (n)
9193   "Raise the score of the current article by N."
9194   (interactive "p")
9195   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9196
9197 (defun gnus-summary-set-score (n)
9198   "Set the score of the current article to N."
9199   (interactive "p")
9200   ;; Skip dummy header line.
9201   (save-excursion
9202     (gnus-summary-show-thread)
9203     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9204         (forward-line 1))
9205     (let ((buffer-read-only nil))
9206       ;; Set score.
9207       (gnus-summary-update-mark
9208        (if (= n (or gnus-summary-default-score 0)) ? 
9209          (if (< n (or gnus-summary-default-score 0)) 
9210              gnus-score-below-mark gnus-score-over-mark)) 'score))
9211     (let* ((article (gnus-summary-article-number))
9212            (score (assq article gnus-newsgroup-scored)))
9213       (if score (setcdr score n)
9214         (setq gnus-newsgroup-scored 
9215               (cons (cons article n) gnus-newsgroup-scored))))
9216     (gnus-summary-update-line)))
9217
9218 (defun gnus-summary-current-score ()
9219   "Return the score of the current article."
9220   (interactive)
9221   (message "%s" (gnus-summary-article-score)))
9222
9223 ;; Summary marking commands.
9224
9225 (defun gnus-summary-raise-same-subject-and-select (score)
9226   "Raise articles which has the same subject with SCORE and select the next."
9227   (interactive "p")
9228   (let ((subject (gnus-summary-subject-string)))
9229     (gnus-summary-raise-score score)
9230     (while (gnus-summary-search-subject nil nil subject)
9231       (gnus-summary-raise-score score))
9232     (gnus-summary-next-article t)))
9233
9234 (defun gnus-summary-raise-same-subject (score)
9235   "Raise articles which has the same subject with SCORE."
9236   (interactive "p")
9237   (let ((subject (gnus-summary-subject-string)))
9238     (gnus-summary-raise-score score)
9239     (while (gnus-summary-search-subject nil nil subject)
9240       (gnus-summary-raise-score score))
9241     (gnus-summary-next-subject 1 t)))
9242
9243 (defun gnus-score-default (level)
9244   (if level (prefix-numeric-value level) 
9245     gnus-score-interactive-default-score))
9246
9247 (defun gnus-summary-raise-thread (&optional score)
9248   "Raise the score of the articles in the current thread with SCORE."
9249   (interactive "P")
9250   (setq score (gnus-score-default score))
9251   (let (e)
9252     (save-excursion
9253       (let ((level (gnus-summary-thread-level)))
9254         (gnus-summary-raise-score score)
9255         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9256                     (> (gnus-summary-thread-level) level))
9257           (gnus-summary-raise-score score))
9258         (setq e (point))))
9259     (let ((gnus-summary-check-current t))
9260       (or (zerop (gnus-summary-next-subject 1 t))
9261           (goto-char e))))
9262   (gnus-summary-recenter)
9263   (gnus-summary-position-cursor)
9264   (gnus-set-mode-line 'summary))
9265
9266 (defun gnus-summary-lower-same-subject-and-select (score)
9267   "Raise articles which has the same subject with SCORE and select the next."
9268   (interactive "p")
9269   (gnus-summary-raise-same-subject-and-select (- score)))
9270
9271 (defun gnus-summary-lower-same-subject (score)
9272   "Raise articles which has the same subject with SCORE."
9273   (interactive "p")
9274   (gnus-summary-raise-same-subject (- score)))
9275
9276 (defun gnus-summary-lower-thread (&optional score)
9277   "Lower score of articles in the current thread with SCORE."
9278   (interactive "P")
9279   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9280
9281 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9282   "Mark articles which has the same subject as read, and then select the next.
9283 If UNMARK is positive, remove any kind of mark.
9284 If UNMARK is negative, tick articles."
9285   (interactive "P")
9286   (if unmark
9287       (setq unmark (prefix-numeric-value unmark)))
9288   (let ((count
9289          (gnus-summary-mark-same-subject
9290           (gnus-summary-subject-string) unmark)))
9291     ;; Select next unread article. If auto-select-same mode, should
9292     ;; select the first unread article.
9293     (gnus-summary-next-article t (and gnus-auto-select-same
9294                                       (gnus-summary-subject-string)))
9295     (gnus-message 7 "%d article%s marked as %s"
9296                   count (if (= count 1) " is" "s are")
9297                   (if unmark "unread" "read"))))
9298
9299 (defun gnus-summary-kill-same-subject (&optional unmark)
9300   "Mark articles which has the same subject as read. 
9301 If UNMARK is positive, remove any kind of mark.
9302 If UNMARK is negative, tick articles."
9303   (interactive "P")
9304   (if unmark
9305       (setq unmark (prefix-numeric-value unmark)))
9306   (let ((count
9307          (gnus-summary-mark-same-subject
9308           (gnus-summary-subject-string) unmark)))
9309     ;; If marked as read, go to next unread subject.
9310     (if (null unmark)
9311         ;; Go to next unread subject.
9312         (gnus-summary-next-subject 1 t))
9313     (gnus-message 7 "%d articles are marked as %s"
9314                   count (if unmark "unread" "read"))))
9315
9316 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9317   "Mark articles with same SUBJECT as read, and return marked number.
9318 If optional argument UNMARK is positive, remove any kinds of marks.
9319 If optional argument UNMARK is negative, mark articles as unread instead."
9320   (let ((count 1))
9321     (save-excursion
9322       (cond 
9323        ((null unmark)                   ; Mark as read.
9324         (while (and 
9325                 (progn
9326                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9327                   (gnus-summary-show-thread) t)
9328                 (gnus-summary-search-forward nil subject))
9329           (setq count (1+ count))))
9330        ((> unmark 0)                    ; Tick.
9331         (while (and
9332                 (progn
9333                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9334                   (gnus-summary-show-thread) t)
9335                 (gnus-summary-search-forward nil subject))
9336           (setq count (1+ count))))
9337        (t                               ; Mark as unread.
9338         (while (and
9339                 (progn
9340                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9341                   (gnus-summary-show-thread) t)
9342                 (gnus-summary-search-forward nil subject))
9343           (setq count (1+ count)))))
9344       (gnus-set-mode-line 'summary)
9345       ;; Return the number of marked articles.
9346       count)))
9347
9348 (defun gnus-summary-mark-as-processable (n &optional unmark)
9349   "Set the process mark on the next N articles.
9350 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9351 the process mark instead.  The difference between N and the actual
9352 number of articles marked is returned."
9353   (interactive "p")
9354   (let ((backward (< n 0))
9355         (n (abs n)))
9356     (while (and 
9357             (> n 0)
9358             (if unmark
9359                 (gnus-summary-remove-process-mark
9360                  (gnus-summary-article-number))
9361               (gnus-summary-set-process-mark (gnus-summary-article-number)))
9362             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9363       (setq n (1- n)))
9364     (if (/= 0 n) (gnus-message 7 "No more articles"))
9365     (gnus-summary-recenter)
9366     (gnus-summary-position-cursor)
9367     n))
9368
9369 (defun gnus-summary-unmark-as-processable (n)
9370   "Remove the process mark from the next N articles.
9371 If N is negative, mark backward instead.  The difference between N and
9372 the actual number of articles marked is returned."
9373   (interactive "p")
9374   (gnus-summary-mark-as-processable n t))
9375
9376 (defun gnus-summary-unmark-all-processable ()
9377   "Remove the process mark from all articles."
9378   (interactive)
9379   (save-excursion
9380     (while gnus-newsgroup-processable
9381       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9382   (gnus-summary-position-cursor))
9383
9384 (defun gnus-summary-mark-as-expirable (n)
9385   "Mark N articles forward as expirable.
9386 If N is negative, mark backward instead. The difference between N and
9387 the actual number of articles marked is returned."
9388   (interactive "p")
9389   (gnus-summary-mark-forward n gnus-expirable-mark))
9390
9391 (defun gnus-summary-mark-article-as-replied (article)
9392   "Mark ARTICLE replied and update the summary line."
9393   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9394   (let ((buffer-read-only nil))
9395     (if (gnus-summary-goto-subject article)
9396         (progn
9397           (gnus-summary-update-mark gnus-replied-mark 'replied)
9398           t))))
9399
9400 (defun gnus-summary-set-bookmark (article)
9401   "Set a bookmark in current article."
9402   (interactive (list (gnus-summary-article-number)))
9403   (if (or (not (get-buffer gnus-article-buffer))
9404           (not gnus-current-article)
9405           (not gnus-article-current)
9406           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9407       (error "No current article selected"))
9408   ;; Remove old bookmark, if one exists.
9409   (let ((old (assq article gnus-newsgroup-bookmarks)))
9410     (if old (setq gnus-newsgroup-bookmarks 
9411                   (delq old gnus-newsgroup-bookmarks))))
9412   ;; Set the new bookmark, which is on the form 
9413   ;; (article-number . line-number-in-body).
9414   (setq gnus-newsgroup-bookmarks 
9415         (cons 
9416          (cons article 
9417                (save-excursion
9418                  (set-buffer gnus-article-buffer)
9419                  (count-lines
9420                   (min (point)
9421                        (save-excursion
9422                          (goto-char (point-min))
9423                          (search-forward "\n\n" nil t)
9424                          (point)))
9425                   (point))))
9426          gnus-newsgroup-bookmarks))
9427   (gnus-message 6 "A bookmark has been added to the current article."))
9428
9429 (defun gnus-summary-remove-bookmark (article)
9430   "Remove the bookmark from the current article."
9431   (interactive (list (gnus-summary-article-number)))
9432   ;; Remove old bookmark, if one exists.
9433   (let ((old (assq article gnus-newsgroup-bookmarks)))
9434     (if old 
9435         (progn
9436           (setq gnus-newsgroup-bookmarks 
9437                 (delq old gnus-newsgroup-bookmarks))
9438           (gnus-message 6 "Removed bookmark."))
9439       (gnus-message 6 "No bookmark in current article."))))
9440
9441 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9442 (defun gnus-summary-mark-as-dormant (n)
9443   "Mark N articles forward as dormant.
9444 If N is negative, mark backward instead.  The difference between N and
9445 the actual number of articles marked is returned."
9446   (interactive "p")
9447   (gnus-summary-mark-forward n gnus-dormant-mark))
9448
9449 (defun gnus-summary-set-process-mark (article)
9450   "Set the process mark on ARTICLE and update the summary line."
9451   (setq gnus-newsgroup-processable 
9452         (cons article 
9453               (delq article gnus-newsgroup-processable)))
9454   (let ((buffer-read-only nil))
9455     (if (gnus-summary-goto-subject article)
9456         (progn
9457           (gnus-summary-show-thread)
9458           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9459                (forward-line 1))
9460           (gnus-summary-update-mark gnus-process-mark 'replied)
9461           t))))
9462
9463 (defun gnus-summary-remove-process-mark (article)
9464   "Remove the process mark from ARTICLE and update the summary line."
9465   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9466   (let ((buffer-read-only nil))
9467     (if (gnus-summary-goto-subject article)
9468         (progn
9469           (gnus-summary-show-thread)
9470           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9471                (forward-line 1))
9472           (gnus-summary-update-mark ?  'replied)
9473           (if (memq article gnus-newsgroup-replied) 
9474               (gnus-summary-update-mark gnus-replied-mark 'replied))
9475           t))))
9476
9477 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9478   "Mark N articles as read forwards.
9479 If N is negative, mark backwards instead.
9480 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9481 marked as unread. 
9482 The difference between N and the actual number of articles marked is
9483 returned."
9484   (interactive "p")
9485   (gnus-set-global-variables)
9486   (let ((backward (< n 0))
9487         (gnus-summary-goto-unread
9488          (and gnus-summary-goto-unread
9489               (not (memq mark (list gnus-unread-mark
9490                                     gnus-ticked-mark gnus-dormant-mark)))))
9491         (n (abs n))
9492         (mark (or mark gnus-del-mark)))
9493     (while (and (> n 0)
9494                 (gnus-summary-mark-article nil mark no-expire)
9495                 (zerop (gnus-summary-next-subject 
9496                         (if backward -1 1) gnus-summary-goto-unread t)))
9497       (setq n (1- n)))
9498     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9499     (gnus-summary-recenter)
9500     (gnus-summary-position-cursor)
9501     (gnus-set-mode-line 'summary)
9502     n))
9503
9504 (defun gnus-summary-mark-article-as-read (mark)
9505   "Mark the current article quickly as read with MARK."
9506   (let ((article (gnus-summary-article-number)))
9507     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9508     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9509     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9510     (setq gnus-newsgroup-reads
9511           (cons (cons article mark) gnus-newsgroup-reads))
9512     ;; Possibly remove from cache, if that is used. 
9513     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9514     (and gnus-newsgroup-auto-expire 
9515          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9516              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9517              (= mark gnus-read-mark))
9518          (progn
9519            (setq mark gnus-expirable-mark)
9520            (setq gnus-newsgroup-expirable 
9521                  (cons article gnus-newsgroup-expirable))))
9522     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9523       (forward-line 1))
9524     ;; Fix the mark.
9525     (gnus-summary-update-mark mark 'unread)
9526     t))
9527
9528 (defun gnus-summary-mark-article-as-unread (mark)
9529   "Mark the current article quickly as unread with MARK."
9530   (let ((article (gnus-summary-article-number)))
9531     (or (memq article gnus-newsgroup-unreads)
9532         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9533     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9534     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9535     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9536     (setq gnus-newsgroup-reads
9537           (delq (assq article gnus-newsgroup-reads)
9538                 gnus-newsgroup-reads))
9539     (if (= mark gnus-ticked-mark)
9540         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9541     (if (= mark gnus-dormant-mark)
9542         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9543
9544     ;; See whether the article is to be put in the cache.
9545     (and gnus-use-cache
9546          (vectorp (gnus-get-header-by-num article))
9547          (save-excursion
9548            (gnus-cache-possibly-enter-article 
9549             gnus-newsgroup-name article 
9550             (gnus-get-header-by-num article)
9551             (= mark gnus-ticked-mark)
9552             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9553
9554     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9555       (forward-line 1))
9556     ;; Fix the mark.
9557     (gnus-summary-update-mark mark 'unread)
9558     t))
9559
9560 (defun gnus-summary-mark-article (&optional article mark no-expire)
9561   "Mark ARTICLE with MARK.  MARK can be any character.
9562 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9563 (dormant) and `?E' (expirable).
9564 If MARK is nil, then the default character `?D' is used.
9565 If ARTICLE is nil, then the article on the current line will be
9566 marked." 
9567   (and (stringp mark)
9568        (setq mark (aref mark 0)))
9569   ;; If no mark is given, then we check auto-expiring.
9570   (and (not no-expire)
9571        gnus-newsgroup-auto-expire 
9572        (or (not mark)
9573            (and (numberp mark) 
9574                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9575                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9576                     (= mark gnus-read-mark))))
9577        (setq mark gnus-expirable-mark))
9578   (let* ((mark (or mark gnus-del-mark))
9579          (article (or article (gnus-summary-article-number))))
9580     (or article (error "No article on current line"))
9581     (if (or (= mark gnus-unread-mark) 
9582             (= mark gnus-ticked-mark) 
9583             (= mark gnus-dormant-mark))
9584         (gnus-mark-article-as-unread article mark)
9585       (gnus-mark-article-as-read article mark))
9586
9587     ;; See whether the article is to be put in the cache.
9588     (and gnus-use-cache
9589          (not (= mark gnus-canceled-mark))
9590          (vectorp (gnus-get-header-by-num article))
9591          (save-excursion
9592            (gnus-cache-possibly-enter-article 
9593             gnus-newsgroup-name article 
9594             (gnus-get-header-by-num article)
9595             (= mark gnus-ticked-mark)
9596             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9597
9598     (if (gnus-summary-goto-subject article)
9599         (let ((buffer-read-only nil))
9600           (gnus-summary-show-thread)
9601           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9602                (forward-line 1))
9603           ;; Fix the mark.
9604           (gnus-summary-update-mark mark 'unread)
9605           t))))
9606
9607 (defun gnus-summary-update-mark (mark type)
9608   (beginning-of-line)
9609   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9610         (buffer-read-only nil)
9611         plist)
9612     (if (not forward)
9613         ()
9614       (forward-char forward)
9615       (setq plist (text-properties-at (point)))
9616       (delete-char 1)
9617       (insert mark)
9618       (and plist (add-text-properties (1- (point)) (point) plist))
9619       (and (eq type 'unread)
9620            (progn
9621              (add-text-properties (1- (point)) (point) (list 'gnus-mark mark))
9622              (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9623   
9624 (defun gnus-mark-article-as-read (article &optional mark)
9625   "Enter ARTICLE in the pertinent lists and remove it from others."
9626   ;; Make the article expirable.
9627   (let ((mark (or mark gnus-del-mark)))
9628     (if (= mark gnus-expirable-mark)
9629         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9630       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9631     ;; Remove from unread and marked lists.
9632     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9633     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9634     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9635     (setq gnus-newsgroup-reads 
9636           (cons (cons article mark) gnus-newsgroup-reads))
9637     ;; Possibly remove from cache, if that is used. 
9638     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9639
9640 (defun gnus-mark-article-as-unread (article &optional mark)
9641   "Enter ARTICLE in the pertinent lists and remove it from others."
9642   (let ((mark (or mark gnus-ticked-mark)))
9643     ;; Add to unread list.
9644     (or (memq article gnus-newsgroup-unreads)
9645         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9646     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9647     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9648     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9649     (setq gnus-newsgroup-reads
9650           (delq (assq article gnus-newsgroup-reads)
9651                 gnus-newsgroup-reads))
9652     (if (= mark gnus-ticked-mark)
9653         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9654     (if (= mark gnus-dormant-mark)
9655         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9656
9657 (defalias 'gnus-summary-mark-as-unread-forward 
9658   'gnus-summary-tick-article-forward)
9659 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9660                'gnus-summary-tick-article-forward)
9661 (defun gnus-summary-tick-article-forward (n)
9662   "Tick N articles forwards.
9663 If N is negative, tick backwards instead.
9664 The difference between N and the number of articles ticked is returned."
9665   (interactive "p")
9666   (gnus-summary-mark-forward n gnus-ticked-mark))
9667
9668 (defalias 'gnus-summary-mark-as-unread-backward 
9669   'gnus-summary-tick-article-backward)
9670 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9671                'gnus-summary-tick-article-backward)
9672 (defun gnus-summary-tick-article-backward (n)
9673   "Tick N articles backwards.
9674 The difference between N and the number of articles ticked is returned."
9675   (interactive "p")
9676   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9677
9678 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9679 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9680 (defun gnus-summary-tick-article (&optional article clear-mark)
9681   "Mark current article as unread.
9682 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9683 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9684   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9685                                        gnus-ticked-mark)))
9686
9687 (defun gnus-summary-mark-as-read-forward (n)
9688   "Mark N articles as read forwards.
9689 If N is negative, mark backwards instead.
9690 The difference between N and the actual number of articles marked is
9691 returned."
9692   (interactive "p")
9693   (gnus-summary-mark-forward n gnus-del-mark t))
9694
9695 (defun gnus-summary-mark-as-read-backward (n)
9696   "Mark the N articles as read backwards.
9697 The difference between N and the actual number of articles marked is
9698 returned."
9699   (interactive "p")
9700   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9701
9702 (defun gnus-summary-mark-as-read (&optional article mark)
9703   "Mark current article as read.
9704 ARTICLE specifies the article to be marked as read.
9705 MARK specifies a string to be inserted at the beginning of the line."
9706   (gnus-summary-mark-article article mark))
9707
9708 (defun gnus-summary-clear-mark-forward (n)
9709   "Clear marks from N articles forward.
9710 If N is negative, clear backward instead.
9711 The difference between N and the number of marks cleared is returned."
9712   (interactive "p")
9713   (gnus-summary-mark-forward n gnus-unread-mark))
9714
9715 (defun gnus-summary-clear-mark-backward (n)
9716   "Clear marks from N articles backward.
9717 The difference between N and the number of marks cleared is returned."
9718   (interactive "p")
9719   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9720
9721 (defun gnus-summary-mark-unread-as-read ()
9722   "Intended to be used by `gnus-summary-mark-article-hook'."
9723   (or (memq gnus-current-article gnus-newsgroup-marked)
9724       (memq gnus-current-article gnus-newsgroup-dormant)
9725       (memq gnus-current-article gnus-newsgroup-expirable)
9726       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9727
9728 (defun gnus-summary-mark-region-as-read (point mark all)
9729   "Mark all unread articles between point and mark as read.
9730 If given a prefix, mark all articles between point and mark as read,
9731 even ticked and dormant ones."
9732   (interactive "r\nP")
9733   (save-excursion
9734     (goto-char point)
9735     (beginning-of-line)
9736     (while (and 
9737             (< (point) mark)
9738             (progn
9739               (and
9740                (or all
9741                    (and
9742                     (not (memq (gnus-summary-article-number)
9743                                gnus-newsgroup-marked))
9744                     (not (memq (gnus-summary-article-number)
9745                                gnus-newsgroup-dormant))))
9746                (gnus-summary-mark-article
9747                 (gnus-summary-article-number) gnus-del-mark))
9748               t)
9749             (zerop (forward-line 1))))))
9750
9751 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9752 (defalias 'gnus-summary-delete-marked-as-read 
9753   'gnus-summary-remove-lines-marked-as-read)
9754 (make-obsolete 'gnus-summary-delete-marked-as-read 
9755                'gnus-summary-remove-lines-marked-as-read)
9756 (defun gnus-summary-remove-lines-marked-as-read ()
9757   "Remove lines that are marked as read."
9758   (interactive)
9759   (gnus-summary-remove-lines-marked-with 
9760    (concat (mapconcat
9761             (lambda (char) (char-to-string (symbol-value char)))
9762             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9763                             gnus-killed-mark gnus-kill-file-mark
9764                             gnus-low-score-mark gnus-expirable-mark
9765                             gnus-canceled-mark gnus-catchup-mark)
9766             ""))))
9767
9768 (defalias 'gnus-summary-delete-marked-with 
9769   'gnus-summary-remove-lines-marked-with)
9770 (make-obsolete 'gnus-summary-delete-marked-with 
9771                'gnus-summary-remove-lines-marked-with)
9772 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9773 (defun gnus-summary-remove-lines-marked-with (marks)
9774   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9775   (interactive "sMarks: ")
9776   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9777   (gnus-set-global-variables)
9778   (let ((buffer-read-only nil)
9779         (orig-article 
9780          (let ((gnus-summary-check-current t))
9781            (gnus-summary-search-forward t)
9782            (gnus-summary-article-number)))
9783         (marks (concat "^[" marks "]")))
9784     (goto-char (point-min))
9785     (if gnus-newsgroup-adaptive
9786         (gnus-score-remove-lines-adaptive marks)
9787       (while (re-search-forward marks nil t)
9788         (gnus-delete-line)))
9789     ;; If we use dummy roots, we have to do an additional sweep over
9790     ;; the buffer.
9791     (if (not (eq gnus-summary-make-false-root 'dummy))
9792         ()
9793       (goto-char (point-min))
9794       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9795       (while (re-search-forward marks nil t)
9796         (if (gnus-subject-equal
9797              (gnus-summary-subject-string)
9798              (progn
9799                (forward-line 1)
9800                (gnus-summary-subject-string)))
9801             ()
9802           (forward-line -1)
9803           (gnus-delete-line))))
9804     (or (zerop (buffer-size))
9805         (gnus-summary-goto-subject orig-article)
9806         (if (eobp)
9807             (gnus-summary-prev-subject 1)
9808           (gnus-summary-position-cursor)))))
9809
9810 (defun gnus-summary-expunge-below (&optional score)
9811   "Remove articles with score less than SCORE."
9812   (interactive "P")
9813   (gnus-set-global-variables)
9814   (setq score (if score
9815                   (prefix-numeric-value score)
9816                 (or gnus-summary-default-score 0)))
9817   (save-excursion
9818     (set-buffer gnus-summary-buffer)
9819     (goto-char (point-min))
9820     (let ((buffer-read-only nil)
9821           beg)
9822       (while (not (eobp))
9823         (if (< (gnus-summary-article-score) score)
9824             (progn
9825               (setq beg (point))
9826               (forward-line 1)
9827               (delete-region beg (point)))
9828           (forward-line 1)))
9829       ;; Adjust point.
9830       (or (zerop (buffer-size))
9831           (if (eobp)
9832               (gnus-summary-prev-subject 1)
9833             (gnus-summary-position-cursor))))))
9834
9835 (defun gnus-summary-mark-below (score mark)
9836   "Mark articles with score less than SCORE with MARK."
9837   (interactive "P\ncMark: ")
9838   (gnus-set-global-variables)
9839   (setq score (if score
9840                   (prefix-numeric-value score)
9841                 (or gnus-summary-default-score 0)))
9842   (save-excursion
9843     (set-buffer gnus-summary-buffer)
9844     (goto-char (point-min))
9845     (while (not (eobp))
9846       (and (< (gnus-summary-article-score) score)
9847            (gnus-summary-mark-article nil mark))
9848       (forward-line 1))))
9849
9850 (defun gnus-summary-kill-below (&optional score)
9851   "Mark articles with score below SCORE as read."
9852   (interactive "P")
9853   (gnus-set-global-variables)
9854   (gnus-summary-mark-below score gnus-killed-mark))
9855
9856 (defun gnus-summary-clear-above (&optional score)
9857   "Clear all marks from articles with score above SCORE."
9858   (interactive "P")
9859   (gnus-set-global-variables)
9860   (gnus-summary-mark-above score gnus-unread-mark))
9861
9862 (defun gnus-summary-tick-above (&optional score)
9863   "Tick all articles with score above SCORE."
9864   (interactive "P")
9865   (gnus-set-global-variables)
9866   (gnus-summary-mark-above score gnus-ticked-mark))
9867
9868 (defun gnus-summary-mark-above (score mark)
9869   "Mark articles with score over SCORE with MARK."
9870   (interactive "P\ncMark: ")
9871   (gnus-set-global-variables)
9872   (setq score (if score
9873                   (prefix-numeric-value score)
9874                 (or gnus-summary-default-score 0)))
9875   (save-excursion
9876     (set-buffer gnus-summary-buffer)
9877     (goto-char (point-min))
9878     (while (not (eobp))
9879       (if (> (gnus-summary-article-score) score)
9880           (progn
9881             (gnus-summary-mark-article nil mark)
9882             (forward-line 1))
9883         (forward-line 1)))))
9884
9885 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9886 (defun gnus-summary-show-all-expunged ()
9887   "Display all the hidden articles that were expunged for low scores."
9888   (interactive)
9889   (gnus-set-global-variables)
9890   (let ((buffer-read-only nil))
9891     (let ((scored gnus-newsgroup-scored)
9892           headers h)
9893       (while scored
9894         (or (gnus-summary-goto-subject (car (car scored)))
9895             (and (setq h (gnus-get-header-by-num (car (car scored))))
9896                  (< (cdr (car scored)) gnus-summary-expunge-below)
9897                  (setq headers (cons h headers))))
9898         (setq scored (cdr scored)))
9899       (or headers (error "No expunged articles hidden."))
9900       (goto-char (point-min))
9901       (save-excursion 
9902         (gnus-summary-update-lines 
9903          (point)
9904          (progn
9905            (gnus-summary-prepare-unthreaded (nreverse headers))
9906            (point)))))
9907     (goto-char (point-min))
9908     (gnus-summary-position-cursor)))
9909
9910 (defun gnus-summary-show-all-dormant ()
9911   "Display all the hidden articles that are marked as dormant."
9912   (interactive)
9913   (gnus-set-global-variables)
9914   (let ((buffer-read-only nil))
9915     (let ((dormant gnus-newsgroup-dormant)
9916           headers h)
9917       (while dormant
9918         (or (gnus-summary-goto-subject (car dormant))
9919             (and (setq h (gnus-get-header-by-num (car dormant)))
9920                  (setq headers (cons h headers))))
9921         (setq dormant (cdr dormant)))
9922       (or headers (error "No dormant articles hidden."))
9923       (goto-char (point-min))
9924       (save-excursion 
9925         (gnus-summary-update-lines 
9926          (point)
9927          (progn
9928            (gnus-summary-prepare-unthreaded (nreverse headers))
9929            (point)))))
9930     (goto-char (point-min))
9931     (gnus-summary-position-cursor)))
9932
9933 (defun gnus-summary-hide-all-dormant ()
9934   "Hide all dormant articles."
9935   (interactive)
9936   (gnus-set-global-variables)
9937   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9938   (gnus-summary-position-cursor))
9939
9940 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9941   "Mark all articles not marked as unread in this newsgroup as read.
9942 If prefix argument ALL is non-nil, all articles are marked as read.
9943 If QUIETLY is non-nil, no questions will be asked.
9944 If TO-HERE is non-nil, it should be a point in the buffer. All
9945 articles before this point will be marked as read.
9946 The number of articles marked as read is returned."
9947   (interactive "P")
9948   (gnus-set-global-variables)
9949   (prog1
9950       (if (or quietly
9951               (not gnus-interactive-catchup) ;Without confirmation?
9952               gnus-expert-user
9953               (gnus-y-or-n-p
9954                (if all
9955                    "Mark absolutely all articles as read? "
9956                  "Mark all unread articles as read? ")))
9957           (if (and not-mark 
9958                    (not gnus-newsgroup-adaptive)
9959                    (not gnus-newsgroup-auto-expire))
9960               (progn
9961                 (and all (setq gnus-newsgroup-marked nil
9962                                gnus-newsgroup-dormant nil))
9963                 (setq gnus-newsgroup-unreads 
9964                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9965             ;; We actually mark all articles as canceled, which we
9966             ;; have to do when using auto-expiry or adaptive scoring. 
9967             (gnus-summary-show-all-threads)
9968             (if (gnus-summary-first-subject (not all))
9969                 (while (and 
9970                         (if to-here (< (point) to-here) t)
9971                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9972                         (gnus-summary-search-subject nil (not all)))))
9973             (or to-here
9974                 (setq gnus-newsgroup-unreads
9975                       (append gnus-newsgroup-marked
9976                               gnus-newsgroup-dormant)))))
9977     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9978       (if (and (not to-here) (eq 'nnvirtual (car method)))
9979           (nnvirtual-catchup-group
9980            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9981     (gnus-summary-position-cursor)))
9982
9983 (defun gnus-summary-catchup-to-here (&optional all)
9984   "Mark all unticked articles before the current one as read.
9985 If ALL is non-nil, also mark ticked and dormant articles as read."
9986   (interactive "P")
9987   (gnus-set-global-variables)
9988   (save-excursion
9989     (and (zerop (forward-line -1))
9990          (progn
9991            (end-of-line)
9992            (gnus-summary-catchup all t (point))
9993            (gnus-set-mode-line 'summary))))
9994   (gnus-summary-position-cursor))
9995
9996 (defun gnus-summary-catchup-all (&optional quietly)
9997   "Mark all articles in this newsgroup as read."
9998   (interactive "P")
9999   (gnus-set-global-variables)
10000   (gnus-summary-catchup t quietly))
10001
10002 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10003   "Mark all articles not marked as unread in this newsgroup as read, then exit.
10004 If prefix argument ALL is non-nil, all articles are marked as read."
10005   (interactive "P")
10006   (gnus-set-global-variables)
10007   (gnus-summary-catchup all quietly nil 'fast)
10008   ;; Select next newsgroup or exit.
10009   (if (and (eq gnus-auto-select-next 'quietly)
10010            (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10011       (gnus-summary-next-group nil)
10012     (gnus-summary-exit)))
10013
10014 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10015   "Mark all articles in this newsgroup as read, and then exit."
10016   (interactive "P")
10017   (gnus-set-global-variables)
10018   (gnus-summary-catchup-and-exit t quietly))
10019
10020 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
10021 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10022   "Mark all articles in this group as read and select the next group.
10023 If given a prefix, mark all articles, unread as well as ticked, as
10024 read." 
10025   (interactive "P")
10026   (gnus-set-global-variables)
10027   (gnus-summary-catchup all)
10028   (gnus-summary-next-group))
10029
10030 ;; Thread-based commands.
10031
10032 (defun gnus-summary-toggle-threads (&optional arg)
10033   "Toggle showing conversation threads.
10034 If ARG is positive number, turn showing conversation threads on."
10035   (interactive "P")
10036   (gnus-set-global-variables)
10037   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10038     (setq gnus-show-threads
10039           (if (null arg) (not gnus-show-threads)
10040             (> (prefix-numeric-value arg) 0)))
10041     (gnus-summary-prepare)
10042     (gnus-summary-goto-subject current)
10043     (gnus-summary-position-cursor)))
10044
10045 (defun gnus-summary-show-all-threads ()
10046   "Show all threads."
10047   (interactive)
10048   (gnus-set-global-variables)
10049   (save-excursion
10050     (let ((buffer-read-only nil))
10051       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10052   (gnus-summary-position-cursor))
10053
10054 (defun gnus-summary-show-thread ()
10055   "Show thread subtrees.
10056 Returns nil if no thread was there to be shown."
10057   (interactive)
10058   (gnus-set-global-variables)
10059   (let ((buffer-read-only nil)
10060         (orig (prog1 (point) (gnus-summary-hide-thread)))
10061         ;; first goto end then to beg, to have point at beg after let
10062         (end (progn (end-of-line) (point)))
10063         (beg (progn (beginning-of-line) (point))))
10064     (prog1
10065         ;; Any hidden lines here?
10066         (search-forward "\r" end t)
10067       (subst-char-in-region beg end ?\^M ?\n t)
10068       (goto-char orig)
10069       (gnus-summary-position-cursor))))
10070
10071 (defun gnus-summary-hide-all-threads ()
10072   "Hide all thread subtrees."
10073   (interactive)
10074   (gnus-set-global-variables)
10075   (save-excursion
10076     (goto-char (point-min))
10077     (gnus-summary-hide-thread)
10078     (while (and (not (eobp)) (zerop (forward-line 1)))
10079       (gnus-summary-hide-thread)))
10080   (gnus-summary-position-cursor))
10081
10082 (defun gnus-summary-hide-thread ()
10083   "Hide thread subtrees.
10084 Returns nil if no threads were there to be hidden."
10085   (interactive)
10086   (gnus-set-global-variables)
10087   (let ((buffer-read-only nil)
10088         (start (point))
10089         (level (gnus-summary-thread-level))
10090         (end (point)))
10091     ;; Go forward until either the buffer ends or the subthread
10092     ;; ends. 
10093     (if (eobp)
10094         ()
10095       (while (and (zerop (forward-line 1))
10096                   (> (gnus-summary-thread-level) level))
10097         (setq end (point)))
10098       (prog1
10099           (save-excursion
10100             (goto-char end)
10101             (search-backward "\n" start t))
10102         (subst-char-in-region start end ?\n ?\^M t)
10103         (forward-line -1)
10104         (gnus-summary-position-cursor)))))
10105
10106 (defun gnus-summary-go-to-next-thread (&optional previous)
10107   "Go to the same level (or less) next thread.
10108 If PREVIOUS is non-nil, go to previous thread instead.
10109 Return the article number moved to, or nil if moving was impossible."
10110   (let ((level (gnus-summary-thread-level))
10111         (article (gnus-summary-article-number)))
10112     (if previous 
10113         (while (and (zerop (forward-line -1))
10114                     (> (gnus-summary-thread-level) level)))
10115       (while (and (save-excursion
10116                     (forward-line 1)
10117                     (not (eobp)))
10118                   (zerop (forward-line 1))
10119                   (> (gnus-summary-thread-level) level))))
10120     (gnus-summary-recenter)
10121     (gnus-summary-position-cursor)
10122     (let ((oart (gnus-summary-article-number)))
10123       (and (/= oart article) oart))))
10124
10125 (defun gnus-summary-next-thread (n)
10126   "Go to the same level next N'th thread.
10127 If N is negative, search backward instead.
10128 Returns the difference between N and the number of skips actually
10129 done."
10130   (interactive "p")
10131   (gnus-set-global-variables)
10132   (let ((backward (< n 0))
10133         (n (abs n)))
10134     (while (and (> n 0)
10135                 (gnus-summary-go-to-next-thread backward))
10136       (setq n (1- n)))
10137     (gnus-summary-position-cursor)
10138     (if (/= 0 n) (gnus-message 7 "No more threads"))
10139     n))
10140
10141 (defun gnus-summary-prev-thread (n)
10142   "Go to the same level previous N'th thread.
10143 Returns the difference between N and the number of skips actually
10144 done."
10145   (interactive "p")
10146   (gnus-set-global-variables)
10147   (gnus-summary-next-thread (- n)))
10148
10149 (defun gnus-summary-go-down-thread (&optional same)
10150   "Go down one level in the current thread.
10151 If SAME is non-nil, also move to articles of the same level."
10152   (let ((level (gnus-summary-thread-level))
10153         (start (point)))
10154     (if (and (zerop (forward-line 1))
10155              (> (gnus-summary-thread-level) level))
10156         t
10157       (goto-char start)
10158       nil)))
10159
10160 (defun gnus-summary-go-up-thread ()
10161   "Go up one level in the current thread."
10162   (let ((level (gnus-summary-thread-level))
10163         (start (point)))
10164     (while (and (zerop (forward-line -1))
10165                 (>= (gnus-summary-thread-level) level)))
10166     (if (>= (gnus-summary-thread-level) level)
10167         (progn
10168           (goto-char start)
10169           nil)
10170       t)))
10171
10172 (defun gnus-summary-down-thread (n)
10173   "Go down thread N steps.
10174 If N is negative, go up instead.
10175 Returns the difference between N and how many steps down that were
10176 taken."
10177   (interactive "p")
10178   (gnus-set-global-variables)
10179   (let ((up (< n 0))
10180         (n (abs n)))
10181     (while (and (> n 0)
10182                 (if up (gnus-summary-go-up-thread)
10183                   (gnus-summary-go-down-thread)))
10184       (setq n (1- n)))
10185     (gnus-summary-position-cursor)
10186     (if (/= 0 n) (gnus-message 7 "Can't go further"))
10187     n))
10188
10189 (defun gnus-summary-up-thread (n)
10190   "Go up thread N steps.
10191 If N is negative, go up instead.
10192 Returns the difference between N and how many steps down that were
10193 taken."
10194   (interactive "p")
10195   (gnus-set-global-variables)
10196   (gnus-summary-down-thread (- n)))
10197
10198 (defun gnus-summary-kill-thread (&optional unmark)
10199   "Mark articles under current thread as read.
10200 If the prefix argument is positive, remove any kinds of marks.
10201 If the prefix argument is negative, tick articles instead."
10202   (interactive "P")
10203   (gnus-set-global-variables)
10204   (if unmark
10205       (setq unmark (prefix-numeric-value unmark)))
10206   (let ((killing t)
10207         (level (gnus-summary-thread-level)))
10208     (save-excursion
10209       ;; Expand the thread.
10210       (gnus-summary-show-thread)
10211       (while killing
10212         ;; Mark the article...
10213         (cond ((null unmark) (gnus-summary-mark-article-as-read
10214                               gnus-killed-mark))
10215               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10216                              gnus-unread-mark))
10217               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10218         ;; ...and go forward until either the buffer ends or the subtree
10219         ;; ends. 
10220         (if (not (and (zerop (forward-line 1))
10221                       (> (gnus-summary-thread-level) level)))
10222             (setq killing nil))))
10223     ;; Hide killed subtrees.
10224     (and (null unmark)
10225          gnus-thread-hide-killed
10226          (gnus-summary-hide-thread))
10227     ;; If marked as read, go to next unread subject.
10228     (if (null unmark)
10229         ;; Go to next unread subject.
10230         (gnus-summary-next-subject 1 t)))
10231   (gnus-set-mode-line 'summary))
10232
10233 ;; Summary sorting commands
10234
10235 (defun gnus-summary-sort-by-number (&optional reverse)
10236   "Sort summary buffer by article number.
10237 Argument REVERSE means reverse order."
10238   (interactive "P")
10239   (gnus-set-global-variables)
10240   (gnus-summary-sort 
10241    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10242    ;; a function, so we wrap it.
10243    (cons (lambda () (gnus-summary-article-number))
10244          'gnus-thread-sort-by-number) reverse))
10245
10246 (defun gnus-summary-sort-by-author (&optional reverse)
10247   "Sort summary buffer by author name alphabetically.
10248 If case-fold-search is non-nil, case of letters is ignored.
10249 Argument REVERSE means reverse order."
10250   (interactive "P")
10251   (gnus-set-global-variables)
10252   (gnus-summary-sort
10253    (cons
10254     (lambda ()
10255       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10256              extract)
10257         (if (not (vectorp header))
10258             ""
10259           (setq extract (funcall gnus-extract-address-components
10260                                  (mail-header-from header)))
10261           (concat (or (car extract) (cdr extract))
10262                   "\r" (int-to-string (mail-header-number header))
10263                   "\r" (mail-header-subject header)))))
10264     'gnus-thread-sort-by-author)
10265    reverse))
10266
10267 (defun gnus-summary-sort-by-subject (&optional reverse)
10268   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10269 If case-fold-search is non-nil, case of letters is ignored.
10270 Argument REVERSE means reverse order."
10271   (interactive "P")
10272   (gnus-set-global-variables)
10273   (gnus-summary-sort
10274    (cons
10275     (lambda ()
10276       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10277              extract)
10278         (if (not (vectorp header))
10279             ""
10280           (setq extract (funcall gnus-extract-address-components
10281                                  (mail-header-from header)))
10282           (concat 
10283            (downcase (gnus-simplify-subject (gnus-summary-subject-string) t))
10284            "\r" (int-to-string (mail-header-number header))
10285            "\r" (or (car extract) (cdr extract))))))
10286     'gnus-thread-sort-by-subject)
10287    reverse))
10288
10289 (defun gnus-summary-sort-by-date (&optional reverse)
10290   "Sort summary buffer by date.
10291 Argument REVERSE means reverse order."
10292   (interactive "P")
10293   (gnus-set-global-variables)
10294   (gnus-summary-sort
10295    (cons
10296     (lambda ()
10297       (gnus-sortable-date
10298        (mail-header-date 
10299         (gnus-get-header-by-num (gnus-summary-article-number)))))
10300     'gnus-thread-sort-by-date)
10301    reverse))
10302
10303 (defun gnus-summary-sort-by-score (&optional reverse)
10304   "Sort summary buffer by score.
10305 Argument REVERSE means reverse order."
10306   (interactive "P")
10307   (gnus-set-global-variables)
10308   (gnus-summary-sort 
10309    (cons (lambda () (gnus-summary-article-score))
10310          'gnus-thread-sort-by-score)
10311    (not reverse)))
10312
10313 (defvar gnus-summary-already-sorted nil)
10314 (defun gnus-summary-sort (predicate reverse)
10315   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10316   (if gnus-summary-already-sorted
10317       ()
10318     (let (buffer-read-only)
10319       (if (not gnus-show-threads)
10320           ;; We do untreaded sorting...
10321           (progn
10322             (goto-char (point-min))
10323             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10324         ;; ... or we do threaded sorting.
10325         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10326               (gnus-summary-prepare-hook nil)
10327               (gnus-summary-already-sorted nil))
10328           ;; We do that by simply regenerating the threads.
10329           (gnus-summary-prepare)
10330           (and gnus-show-threads
10331                gnus-thread-hide-subtree
10332                (gnus-summary-hide-all-threads))
10333           ;; If in async mode, we send some info to the backend.
10334           (and gnus-newsgroup-async
10335                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10336                (gnus-request-asynchronous 
10337                 gnus-newsgroup-name
10338                 (if (and gnus-asynchronous-article-function
10339                          (fboundp gnus-asynchronous-article-function))
10340                     (funcall gnus-asynchronous-article-function
10341                              gnus-newsgroup-threads)
10342                   gnus-newsgroup-threads))))))))
10343
10344   
10345 (defun gnus-sortable-date (date)
10346   "Make sortable string by string-lessp from DATE.
10347 Timezone package is used."
10348   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10349          (year (aref date 0))
10350          (month (aref date 1))
10351          (day (aref date 2)))
10352     (timezone-make-sortable-date 
10353      year month day 
10354      (timezone-make-time-string
10355       (aref date 3) (aref date 4) (aref date 5)))))
10356
10357
10358 ;; Summary saving commands.
10359
10360 (defun gnus-summary-save-article (&optional n)
10361   "Save the current article using the default saver function.
10362 If N is a positive number, save the N next articles.
10363 If N is a negative number, save the N previous articles.
10364 If N is nil and any articles have been marked with the process mark,
10365 save those articles instead.
10366 The variable `gnus-default-article-saver' specifies the saver function."
10367   (interactive "P")
10368   (gnus-set-global-variables)
10369   (let ((articles (gnus-summary-work-articles n)))
10370     (while articles
10371       (let ((header (gnus-get-header-by-num (car articles))))
10372         (if (vectorp header)
10373             (progn
10374               (save-window-excursion
10375                 (gnus-summary-select-article t nil nil (car articles)))
10376               (or gnus-save-all-headers
10377                   (gnus-article-hide-headers t))
10378               ;; Remove any X-Gnus lines.
10379               (save-excursion
10380                 (save-restriction
10381                   (set-buffer gnus-article-buffer)
10382                   (let ((buffer-read-only nil))
10383                     (goto-char (point-min))
10384                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10385                                                   (point-max)))
10386                     (while (re-search-forward "^X-Gnus" nil t)
10387                       (beginning-of-line)
10388                       (delete-region (point)
10389                                      (progn (forward-line 1) (point))))
10390                     (widen))))
10391               (save-window-excursion
10392                 (if gnus-default-article-saver
10393                     (funcall gnus-default-article-saver)
10394                   (error "No default saver is defined."))))
10395           (if (assq 'name header)
10396               (gnus-copy-file (cdr (assq 'name header)))
10397             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10398       (gnus-summary-remove-process-mark (car articles))
10399       (setq articles (cdr articles)))
10400     (gnus-summary-position-cursor)
10401     n))
10402
10403 (defun gnus-summary-pipe-output (&optional arg)
10404   "Pipe the current article to a subprocess.
10405 If N is a positive number, pipe the N next articles.
10406 If N is a negative number, pipe the N previous articles.
10407 If N is nil and any articles have been marked with the process mark,
10408 pipe those articles instead."
10409   (interactive "P")
10410   (gnus-set-global-variables)
10411   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10412     (gnus-summary-save-article arg)))
10413
10414 (defun gnus-summary-save-article-mail (&optional arg)
10415   "Append the current article to an mail file.
10416 If N is a positive number, save the N next articles.
10417 If N is a negative number, save the N previous articles.
10418 If N is nil and any articles have been marked with the process mark,
10419 save those articles instead."
10420   (interactive "P")
10421   (gnus-set-global-variables)
10422   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10423     (gnus-summary-save-article arg)))
10424
10425 (defun gnus-summary-save-article-rmail (&optional arg)
10426   "Append the current article to an rmail file.
10427 If N is a positive number, save the N next articles.
10428 If N is a negative number, save the N previous articles.
10429 If N is nil and any articles have been marked with the process mark,
10430 save those articles instead."
10431   (interactive "P")
10432   (gnus-set-global-variables)
10433   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10434     (gnus-summary-save-article arg)))
10435
10436 (defun gnus-summary-save-article-file (&optional arg)
10437   "Append the current article to a file.
10438 If N is a positive number, save the N next articles.
10439 If N is a negative number, save the N previous articles.
10440 If N is nil and any articles have been marked with the process mark,
10441 save those articles instead."
10442   (interactive "P")
10443   (gnus-set-global-variables)
10444   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10445     (gnus-summary-save-article arg)))
10446
10447 (defun gnus-read-save-file-name (prompt default-name)
10448   (let ((methods gnus-split-methods)
10449         split-name)
10450     (if (not gnus-split-methods)
10451         ()
10452       (save-excursion
10453         (set-buffer gnus-article-buffer)
10454         (gnus-narrow-to-headers)
10455         (while methods
10456           (goto-char (point-min))
10457           (and (condition-case () 
10458                    (re-search-forward (car (car methods)) nil t)
10459                  (error nil))
10460                (setq split-name (cons (nth 1 (car methods)) split-name)))
10461           (setq methods (cdr methods)))
10462         (widen)))
10463     (cond ((null split-name)
10464            (read-file-name
10465             (concat prompt " (default "
10466                     (file-name-nondirectory default-name) ") ")
10467             (file-name-directory default-name)
10468             default-name))
10469           ((= 1 (length split-name))
10470            (read-file-name
10471             (concat prompt " (default " (car split-name) ") ")
10472             gnus-article-save-directory
10473             (concat gnus-article-save-directory (car split-name))))
10474           (t
10475            (setq split-name (mapcar (lambda (el) (list el))
10476                                     (nreverse split-name)))
10477            (let ((result (completing-read 
10478                           (concat prompt " ")
10479                           split-name nil nil)))
10480              (concat gnus-article-save-directory
10481                      (if (string= result "")
10482                          (car (car split-name))
10483                        result)))))))
10484
10485 (defun gnus-summary-save-in-rmail (&optional filename)
10486   "Append this article to Rmail file.
10487 Optional argument FILENAME specifies file name.
10488 Directory to save to is default to `gnus-article-save-directory' which
10489 is initialized from the SAVEDIR environment variable."
10490   (interactive)
10491   (gnus-set-global-variables)
10492   (let ((default-name
10493           (funcall gnus-rmail-save-name gnus-newsgroup-name
10494                    gnus-current-headers gnus-newsgroup-last-rmail)))
10495     (or filename
10496         (setq filename (gnus-read-save-file-name 
10497                         "Save in rmail file:" default-name)))
10498     (gnus-make-directory (file-name-directory filename))
10499     (gnus-eval-in-buffer-window 
10500      gnus-article-buffer
10501      (save-excursion
10502        (save-restriction
10503          (widen)
10504          (gnus-output-to-rmail filename))))
10505     ;; Remember the directory name to save articles
10506     (setq gnus-newsgroup-last-rmail filename)))
10507
10508 (defun gnus-summary-save-in-mail (&optional filename)
10509   "Append this article to Unix mail file.
10510 Optional argument FILENAME specifies file name.
10511 Directory to save to is default to `gnus-article-save-directory' which
10512 is initialized from the SAVEDIR environment variable."
10513   (interactive)
10514   (gnus-set-global-variables)
10515   (let ((default-name
10516           (funcall gnus-mail-save-name gnus-newsgroup-name
10517                    gnus-current-headers gnus-newsgroup-last-mail)))
10518     (or filename
10519         (setq filename (gnus-read-save-file-name 
10520                         "Save in Unix mail file:" default-name)))
10521     (setq filename
10522           (expand-file-name filename
10523                             (and default-name
10524                                  (file-name-directory default-name))))
10525     (gnus-make-directory (file-name-directory filename))
10526     (gnus-eval-in-buffer-window 
10527      gnus-article-buffer
10528      (save-excursion
10529        (save-restriction
10530          (widen)
10531          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
10532              (gnus-output-to-rmail filename)
10533            (rmail-output filename 1 t t)))))
10534     ;; Remember the directory name to save articles.
10535     (setq gnus-newsgroup-last-mail filename)))
10536
10537 (defun gnus-summary-save-in-file (&optional filename)
10538   "Append this article to file.
10539 Optional argument FILENAME specifies file name.
10540 Directory to save to is default to `gnus-article-save-directory' which
10541 is initialized from the SAVEDIR environment variable."
10542   (interactive)
10543   (gnus-set-global-variables)
10544   (let ((default-name
10545           (funcall gnus-file-save-name gnus-newsgroup-name
10546                    gnus-current-headers gnus-newsgroup-last-file)))
10547     (or filename
10548         (setq filename (gnus-read-save-file-name 
10549                         "Save in file:" default-name)))
10550     (gnus-make-directory (file-name-directory filename))
10551     (gnus-eval-in-buffer-window 
10552      gnus-article-buffer
10553      (save-excursion
10554        (save-restriction
10555          (widen)
10556          (gnus-output-to-file filename))))
10557     ;; Remember the directory name to save articles.
10558     (setq gnus-newsgroup-last-file filename)))
10559
10560 (defun gnus-summary-save-in-pipe (&optional command)
10561   "Pipe this article to subprocess."
10562   (interactive)
10563   (gnus-set-global-variables)
10564   (let ((command (read-string "Shell command on article: "
10565                               gnus-last-shell-command)))
10566     (if (string-equal command "")
10567         (setq command gnus-last-shell-command))
10568     (gnus-eval-in-buffer-window 
10569      gnus-article-buffer
10570      (save-restriction
10571        (widen)
10572        (shell-command-on-region (point-min) (point-max) command nil)))
10573     (setq gnus-last-shell-command command)))
10574
10575 ;; Summary extract commands
10576
10577 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10578   (let ((buffer-read-only nil)
10579         (article (gnus-summary-article-number))
10580         b)
10581     (or (gnus-summary-goto-subject article)
10582         (error (format "No such article: %d" article)))
10583     (gnus-summary-position-cursor)
10584     ;; If all commands are to be bunched up on one line, we collect
10585     ;; them here.  
10586     (if gnus-view-pseudos-separately
10587         ()
10588       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10589             files action)
10590         (while ps
10591           (setq action (cdr (assq 'action (car ps))))
10592           (setq files (list (cdr (assq 'name (car ps)))))
10593           (while (and ps (cdr ps)
10594                       (string= (or action "1")
10595                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10596             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10597             (setcdr ps (cdr (cdr ps))))
10598           (if (not files)
10599               ()
10600             (if (not (string-match "%s" action))
10601                 (setq files (cons " " files)))
10602             (setq files (cons " " files))
10603             (and (assq 'execute (car ps))
10604                  (setcdr (assq 'execute (car ps))
10605                          (funcall (if (string-match "%s" action)
10606                                       'format 'concat)
10607                                   action 
10608                                   (mapconcat (lambda (f) f) files " ")))))
10609           (setq ps (cdr ps)))))
10610     (if (and gnus-view-pseudos (not not-view))
10611         (while pslist
10612           (and (assq 'execute (car pslist))
10613                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10614                                      (eq gnus-view-pseudos 'not-confirm)))
10615           (setq pslist (cdr pslist)))
10616       (save-excursion
10617         (while pslist
10618           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10619                                          (gnus-summary-article-number)))
10620           (forward-line 1)
10621           (setq b (point))
10622           (insert "          " (file-name-nondirectory 
10623                                 (cdr (assq 'name (car pslist))))
10624                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10625           (add-text-properties 
10626            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10627                           'gnus-mark gnus-unread-mark 
10628                           'gnus-level 0
10629                           'gnus-pseudo (car pslist)))
10630           (forward-line -1)
10631           (gnus-sethash (int-to-string gnus-reffed-article-number)
10632                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10633           (setq gnus-newsgroup-unreads
10634                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10635           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10636           (setq pslist (cdr pslist)))))))
10637
10638 (defun gnus-pseudos< (p1 p2)
10639   (let ((c1 (cdr (assq 'action p1)))
10640         (c2 (cdr (assq 'action p2))))
10641     (and c1 c2 (string< c1 c2))))
10642
10643 (defun gnus-request-pseudo-article (props)
10644   (cond ((assq 'execute props)
10645          (gnus-execute-command (cdr (assq 'execute props)))))
10646   (let ((gnus-current-article (gnus-summary-article-number)))
10647     (run-hooks 'gnus-mark-article-hook)))
10648
10649 (defun gnus-execute-command (command &optional automatic)
10650   (save-excursion
10651     (gnus-article-setup-buffer)
10652     (set-buffer gnus-article-buffer)
10653     (let ((command (if automatic command (read-string "Command: " command)))
10654           (buffer-read-only nil))
10655       (erase-buffer)
10656       (insert "$ " command "\n\n")
10657       (if gnus-view-pseudo-asynchronously
10658           (start-process "gnus-execute" nil "sh" "-c" command)
10659         (call-process "sh" nil t nil "-c" command)))))
10660
10661 (defun gnus-copy-file (file &optional to)
10662   "Copy FILE to TO."
10663   (interactive
10664    (list (read-file-name "Copy file: " default-directory)
10665          (read-file-name "Copy file to: " default-directory)))
10666   (gnus-set-global-variables)
10667   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10668   (and (file-directory-p to) 
10669        (setq to (concat (file-name-as-directory to)
10670                         (file-name-nondirectory file))))
10671   (copy-file file to))
10672
10673 ;; Summary kill commands.
10674
10675 (defun gnus-summary-edit-global-kill (article)
10676   "Edit the \"global\" kill file."
10677   (interactive (list (gnus-summary-article-number)))
10678   (gnus-set-global-variables)
10679   (gnus-group-edit-global-kill article))
10680
10681 (defun gnus-summary-edit-local-kill ()
10682   "Edit a local kill file applied to the current newsgroup."
10683   (interactive)
10684   (gnus-set-global-variables)
10685   (setq gnus-current-headers 
10686         (gnus-gethash 
10687          (int-to-string (gnus-summary-article-number))
10688          gnus-newsgroup-headers-hashtb-by-number))
10689   (gnus-set-global-variables)
10690   (gnus-group-edit-local-kill 
10691    (gnus-summary-article-number) gnus-newsgroup-name))
10692
10693 \f
10694 ;;;
10695 ;;; Gnus article mode
10696 ;;;
10697
10698 (put 'gnus-article-mode 'mode-class 'special)
10699
10700 (defvar gnus-boogaboo nil)
10701
10702 (if gnus-article-mode-map
10703     nil
10704   (setq gnus-article-mode-map (make-keymap))
10705   (suppress-keymap gnus-article-mode-map)
10706   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10707   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10708   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10709   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10710   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10711   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10712   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10713   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10714   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10715   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10716   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10717   
10718   ;; Duplicate almost all summary keystrokes in the article mode map.
10719   (let ((commands 
10720          (list 
10721           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10722           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10723           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10724           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10725           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10726           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10727           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10728           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10729           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10730           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10731           "\C-c\C-v\C-v" "\C-d" "v" 
10732 ;;        "Mt" "M!" "Md" "Mr"
10733 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10734 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10735 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10736 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10737 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10738 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10739 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10740 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10741 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10742 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10743 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10744           )))
10745     (while (and gnus-boogaboo commands) ; disabled
10746       (define-key gnus-article-mode-map (car commands) 
10747         'gnus-article-summary-command)
10748       (setq commands (cdr commands))))
10749
10750   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10751 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10752                          "=" "n"  "^" "\M-^")))
10753     (while (and gnus-boogaboo commands) ; disabled
10754       (define-key gnus-article-mode-map (car commands) 
10755         'gnus-article-summary-command-nosave)
10756       (setq commands (cdr commands)))))
10757
10758
10759 (defun gnus-article-mode ()
10760   "Major mode for displaying an article.
10761
10762 All normal editing commands are switched off.
10763
10764 The following commands are available:
10765
10766 \\<gnus-article-mode-map>
10767 \\[gnus-article-next-page]\t Scroll the article one page forwards
10768 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10769 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10770 \\[gnus-article-show-summary]\t Display the summary buffer
10771 \\[gnus-article-mail]\t Send a reply to the address near point
10772 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10773 \\[gnus-info-find-node]\t Go to the Gnus info node"
10774   (interactive)
10775   (if gnus-visual (gnus-article-make-menu-bar))
10776   (kill-all-local-variables)
10777   (gnus-simplify-mode-line)
10778   (setq mode-name "Article")
10779   (setq major-mode 'gnus-article-mode)
10780   (make-local-variable 'minor-mode-alist)
10781   (or (assq 'gnus-show-mime minor-mode-alist)
10782       (setq minor-mode-alist
10783             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10784   (use-local-map gnus-article-mode-map)
10785   (make-local-variable 'page-delimiter)
10786   (setq page-delimiter gnus-page-delimiter)
10787   (buffer-disable-undo (current-buffer))
10788   (setq buffer-read-only t)             ;Disable modification
10789   (run-hooks 'gnus-article-mode-hook))
10790
10791 (defun gnus-article-setup-buffer ()
10792   "Initialize article mode buffer."
10793   ;; Returns the article buffer.
10794   (if (get-buffer gnus-article-buffer)
10795       (save-excursion
10796         (set-buffer gnus-article-buffer)
10797         (buffer-disable-undo (current-buffer))
10798         (setq buffer-read-only t)
10799         (gnus-add-current-to-buffer-list)
10800         (or (eq major-mode 'gnus-article-mode)
10801             (gnus-article-mode))
10802         (current-buffer))
10803     (save-excursion
10804       (set-buffer (get-buffer-create gnus-article-buffer))
10805       (gnus-add-current-to-buffer-list)
10806       (gnus-article-mode)
10807       (current-buffer))))
10808
10809 ;; Set article window start at LINE, where LINE is the number of lines
10810 ;; from the head of the article.
10811 (defun gnus-article-set-window-start (&optional line)
10812   (set-window-start 
10813    (get-buffer-window gnus-article-buffer)
10814    (save-excursion
10815      (set-buffer gnus-article-buffer)
10816      (goto-char (point-min))
10817      (if (not line)
10818          (point-min)
10819        (gnus-message 6 "Moved to bookmark")
10820        (search-forward "\n\n" nil t)
10821        (forward-line line)
10822        (point)))))
10823
10824 (defun gnus-request-article-this-buffer (article group)
10825   "Get an article and insert it into this buffer."
10826   (setq group (or group gnus-newsgroup-name))
10827
10828   ;; Open server if it has closed.
10829   (gnus-check-server (gnus-find-method-for-group group))
10830
10831   ;; Using `gnus-request-article' directly will insert the article into
10832   ;; `nntp-server-buffer' - so we'll save some time by not having to
10833   ;; copy it from the server buffer into the article buffer.
10834
10835   ;; We only request an article by message-id when we do not have the
10836   ;; headers for it, so we'll have to get those.
10837   (and (stringp article) 
10838        (let ((gnus-override-method gnus-refer-article-method))
10839          (gnus-read-header article)))
10840
10841   ;; If the article number is negative, that means that this article
10842   ;; doesn't belong in this newsgroup (possibly), so we find its
10843   ;; message-id and request it by id instead of number.
10844   (if (not (numberp article))
10845       ()
10846     (save-excursion
10847       (set-buffer gnus-summary-buffer)
10848       (let ((header (gnus-get-header-by-num article)))
10849         (if (< article 0)
10850             (if (vectorp header)
10851                 ;; It's a real article.
10852                 (setq article (mail-header-id header))
10853               ;; It is an extracted pseudo-article.
10854               (setq article 'pseudo)
10855               (gnus-request-pseudo-article header)))
10856
10857         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10858           (if (not (eq (car method) 'nneething))
10859               ()
10860             (let ((dir (concat (file-name-as-directory (nth 1 method))
10861                                (mail-header-subject header))))
10862               (if (file-directory-p dir)
10863                   (progn
10864                     (setq article 'nneething)
10865                     (gnus-group-enter-directory dir)))))))))
10866
10867   ;; Check the cache.
10868   (if (and gnus-use-cache
10869            (numberp article)
10870            (gnus-cache-request-article article group))
10871       'article
10872     ;; Get the article and put into the article buffer.
10873     (if (or (stringp article) (numberp article))
10874         (progn
10875           (erase-buffer)
10876           ;; There may be some overlays that we have to kill...
10877           (insert "i")
10878           (let ((overlays (and (fboundp 'overlays-at)
10879                                (overlays-at (point-min)))))
10880             (while overlays
10881               (delete-overlay (car overlays))
10882               (setq overlays (cdr overlays))))
10883           (erase-buffer)          
10884           (let ((gnus-override-method 
10885                  (and (stringp article) gnus-refer-article-method)))
10886             (and (gnus-request-article article group (current-buffer))
10887                  'article)))
10888       article)))
10889
10890 (defun gnus-read-header (id)
10891   "Read the headers of article ID and enter them into the Gnus system."
10892   (let (header)
10893     (if (not (setq header 
10894                    (car (if (let ((gnus-nov-is-evil t))
10895                               (gnus-retrieve-headers 
10896                                (list id) gnus-newsgroup-name))
10897                             (gnus-get-newsgroup-headers)))))
10898         nil
10899       (if (stringp id)
10900           (mail-header-set-number header gnus-reffed-article-number))
10901       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10902       (gnus-sethash (int-to-string (mail-header-number header)) header
10903                     gnus-newsgroup-headers-hashtb-by-number)
10904       (if (stringp id)
10905           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10906       (setq gnus-current-headers header)
10907       header)))
10908
10909 (defun gnus-article-prepare (article &optional all-headers header)
10910   "Prepare ARTICLE in article mode buffer.
10911 ARTICLE should either be an article number or a Message-ID.
10912 If ARTICLE is an id, HEADER should be the article headers.
10913 If ALL-HEADERS is non-nil, no headers are hidden."
10914   (save-excursion
10915     ;; Make sure we start in a summary buffer.
10916     (or (eq major-mode 'gnus-summary-mode)
10917         (set-buffer gnus-summary-buffer))
10918     (setq gnus-summary-buffer (current-buffer))
10919     ;; Make sure the connection to the server is alive.
10920     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10921         (progn
10922           (gnus-check-server 
10923            (gnus-find-method-for-group gnus-newsgroup-name))
10924           (gnus-request-group gnus-newsgroup-name t)))
10925     (let* ((article (if header (mail-header-number header) article))
10926            (summary-buffer (current-buffer))
10927            (internal-hook gnus-article-internal-prepare-hook)
10928            (group gnus-newsgroup-name)
10929            result)
10930       (save-excursion
10931         (gnus-article-setup-buffer)
10932         (set-buffer gnus-article-buffer)
10933         (if (not (setq result (let ((buffer-read-only nil))
10934                                 (gnus-request-article-this-buffer 
10935                                  article group))))
10936             ;; There is no such article.
10937             (save-excursion
10938               (if (not (numberp article))
10939                   ()
10940                 (setq gnus-article-current 
10941                       (cons gnus-newsgroup-name article))
10942                 (set-buffer gnus-summary-buffer)
10943                 (setq gnus-current-article article)
10944                 (gnus-summary-mark-article article gnus-canceled-mark))
10945               (gnus-message 1 "No such article (may be canceled)")
10946               (ding)
10947               nil)
10948           (if (or (eq result 'pseudo) (eq result 'nneething))
10949               (progn
10950                 (save-excursion
10951                   (set-buffer summary-buffer)
10952                   (setq gnus-last-article gnus-current-article
10953                         gnus-newsgroup-history (cons gnus-current-article
10954                                                      gnus-newsgroup-history)
10955                         gnus-current-article 0
10956                         gnus-current-headers nil
10957                         gnus-article-current nil)
10958                   (if (eq result 'nneething)
10959                       (gnus-configure-windows 'summary)
10960                     (gnus-configure-windows 'article))
10961                   (gnus-set-global-variables))
10962                 (gnus-set-mode-line 'article))
10963             ;; The result from the `request' was an actual article -
10964             ;; or at least some text that is now displayed in the
10965             ;; article buffer.
10966             (if (and (numberp article)
10967                      (not (eq article gnus-current-article)))
10968                 ;; Seems like a new article has been selected.
10969                 ;; `gnus-current-article' must be an article number.
10970                 (save-excursion
10971                   (set-buffer summary-buffer)
10972                   (setq gnus-last-article gnus-current-article
10973                         gnus-newsgroup-history (cons gnus-current-article
10974                                                      gnus-newsgroup-history)
10975                         gnus-current-article article
10976                         gnus-current-headers 
10977                         (gnus-get-header-by-num gnus-current-article)
10978                         gnus-article-current 
10979                         (cons gnus-newsgroup-name gnus-current-article))
10980                   (gnus-summary-show-thread)
10981                   (run-hooks 'gnus-mark-article-hook)
10982                   (gnus-set-mode-line 'summary)
10983                   (and gnus-visual 
10984                        (run-hooks 'gnus-visual-mark-article-hook))
10985                   ;; Set the global newsgroup variables here.
10986                   ;; Suggested by Jim Sisolak
10987                   ;; <sisolak@trans4.neep.wisc.edu>.
10988                   (gnus-set-global-variables)
10989                   (setq gnus-have-all-headers 
10990                         (or all-headers gnus-show-all-headers))
10991                   (and gnus-use-cache 
10992                        (vectorp (gnus-get-header-by-number article))
10993                        (gnus-cache-possibly-enter-article
10994                         group article
10995                         (gnus-get-header-by-number article)
10996                         (memq article gnus-newsgroup-marked)
10997                         (memq article gnus-newsgroup-dormant)
10998                         (memq article gnus-newsgroup-unreads)))))
10999             ;; Hooks for getting information from the article.
11000             ;; This hook must be called before being narrowed.
11001             (let (buffer-read-only)
11002               (run-hooks 'internal-hook)
11003               (run-hooks 'gnus-article-prepare-hook)
11004               ;; Decode MIME message.
11005               (if (and gnus-show-mime
11006                        (or (not gnus-strict-mime)
11007                            (gnus-fetch-field "Mime-Version")))
11008                   (funcall gnus-show-mime-method))
11009               ;; Perform the article display hooks.
11010               (run-hooks 'gnus-article-display-hook))
11011             ;; Do page break.
11012             (goto-char (point-min))
11013             (and gnus-break-pages (gnus-narrow-to-page))
11014             (gnus-set-mode-line 'article)
11015             (gnus-configure-windows 'article)
11016             (goto-char (point-min))
11017             t))))))
11018
11019 (defun gnus-article-show-all-headers ()
11020   "Show all article headers in article mode buffer."
11021   (save-excursion 
11022     (gnus-article-setup-buffer)
11023     (set-buffer gnus-article-buffer)
11024     (let ((buffer-read-only nil))
11025       (remove-text-properties (point-min) (point-max) 
11026                               gnus-hidden-properties))))
11027
11028 (defun gnus-article-hide-headers-if-wanted ()
11029   "Hide unwanted headers if `gnus-have-all-headers' is nil.
11030 Provided for backwards compatability."
11031   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
11032       (gnus-article-hide-headers)))
11033
11034 (defun gnus-article-hide-headers (&optional delete)
11035   "Hide unwanted headers and possibly sort them as well."
11036   (interactive "P")
11037   (save-excursion
11038     (set-buffer gnus-article-buffer)
11039     (save-restriction
11040       (let ((sorted gnus-sorted-header-list)
11041             (buffer-read-only nil)
11042             want-list beg want-l)
11043         ;; First we narrow to just the headers.
11044         (widen)
11045         (goto-char (point-min))
11046         ;; Hide any "From " lines at the beginning of (mail) articles. 
11047         (while (looking-at "From ")
11048           (forward-line 1))
11049         (or (bobp) 
11050             (add-text-properties (point-min) (point) gnus-hidden-properties))
11051         ;; Then treat the rest of the header lines.
11052         (narrow-to-region 
11053          (point) 
11054          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
11055         ;; Then we use the two regular expressions
11056         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
11057         ;; select which header lines is to remain visible in the
11058         ;; article buffer.
11059         (goto-char (point-min))
11060         (while (re-search-forward "^[^ \t]*:" nil t)
11061           (beginning-of-line)
11062           ;; We add the headers we want to keep to a list and delete
11063           ;; them from the buffer.
11064           (if (or (and (stringp gnus-visible-headers)
11065                        (looking-at gnus-visible-headers))
11066                   (and (not (stringp gnus-visible-headers))
11067                        (stringp gnus-ignored-headers)
11068                        (not (looking-at gnus-ignored-headers))))
11069               (progn
11070                 (setq beg (point))
11071                 (forward-line 1)
11072                 ;; Be sure to get multi-line headers...
11073                 (re-search-forward "^[^ \t]*:" nil t)
11074                 (beginning-of-line)
11075                 (setq want-list 
11076                       (cons (buffer-substring beg (point)) want-list))
11077                 (delete-region beg (point))
11078                 (goto-char beg))
11079             (forward-line 1)))
11080         ;; Next we perform the sorting by looking at
11081         ;; `gnus-sorted-header-list'. 
11082         (goto-char (point-min))
11083         (while (and sorted want-list)
11084           (setq want-l want-list)
11085           (while (and want-l
11086                       (not (string-match (car sorted) (car want-l))))
11087             (setq want-l (cdr want-l)))
11088           (if want-l 
11089               (progn
11090                 (insert (car want-l))
11091                 (setq want-list (delq (car want-l) want-list))))
11092           (setq sorted (cdr sorted)))
11093         ;; Any headers that were not matched by the sorted list we
11094         ;; just tack on the end of the visible header list.
11095         (while want-list
11096           (insert (car want-list))
11097           (setq want-list (cdr want-list)))
11098         ;; And finally we make the unwanted headers invisible.
11099         (if delete
11100             (delete-region (point) (point-max))
11101           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
11102           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
11103
11104 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
11105 (defun gnus-article-treat-overstrike ()
11106   "Translate overstrikes into bold text."
11107   (interactive)
11108   (save-excursion
11109     (set-buffer gnus-article-buffer)
11110     (let ((buffer-read-only nil))
11111       (while (search-forward "\b" nil t)
11112         (let ((next (following-char))
11113               (previous (char-after (- (point) 2))))
11114           (cond ((eq next previous)
11115                  (put-text-property (- (point) 2) (point)
11116                                     'invisible t)
11117                  (put-text-property (point) (1+ (point))
11118                                     'face 'bold))
11119                 ((eq next ?_)
11120                  (put-text-property (1- (point)) (1+ (point))
11121                                     'invisible t)
11122                  (put-text-property (1- (point)) (point)
11123                                     'face 'underline))
11124                 ((eq previous ?_)
11125                  (put-text-property (- (point) 2) (point)
11126                                     'invisible t)
11127                  (put-text-property (point) (1+ (point))
11128                                     'face 'underline))))))))
11129
11130 (defun gnus-article-word-wrap ()
11131   "Format too long lines."
11132   (interactive)
11133   (save-excursion
11134     (set-buffer gnus-article-buffer)
11135     (let ((buffer-read-only nil))
11136       (goto-char (point-min))
11137       (search-forward "\n\n" nil t)
11138       (end-of-line 1)
11139       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
11140             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
11141             (adaptive-fill-mode t))
11142         (while (not (eobp))
11143           (and (>= (current-column) (min fill-column (window-width)))
11144                (/= (preceding-char) ?:)
11145                (fill-paragraph nil))
11146           (end-of-line 2))))))
11147
11148 (defun gnus-article-remove-cr ()
11149   "Remove carriage returns from an article."
11150   (interactive)
11151   (save-excursion
11152     (set-buffer gnus-article-buffer)
11153     (let ((buffer-read-only nil))
11154       (goto-char (point-min))
11155       (while (search-forward "\r" nil t)
11156         (replace-match "" t t)))))
11157
11158 (defun gnus-article-display-x-face (&optional force)
11159   "Look for an X-Face header and display it if present."
11160   (interactive (list 'force))
11161   (save-excursion
11162     (set-buffer gnus-article-buffer)
11163     (let ((inhibit-point-motion-hooks t)
11164           (case-fold-search nil)
11165           from)
11166       (save-restriction
11167         (goto-char (point-min))
11168         (search-forward "\n\n")
11169         (narrow-to-region (point-min) (point))
11170         (goto-char (point-min))
11171         (setq from (mail-fetch-field "from"))
11172         (if (not (and gnus-article-x-face-command
11173                       (or force
11174                           (not gnus-article-x-face-too-ugly)
11175                           (and gnus-article-x-face-too-ugly from
11176                                (not (string-match gnus-article-x-face-too-ugly
11177                                                   from))))
11178                       (progn
11179                         (goto-char (point-min))
11180                         (re-search-forward "^X-Face: " nil t))))
11181             nil
11182           (let ((beg (point))
11183                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
11184             (if (symbolp gnus-article-x-face-command)
11185                 (and (or (fboundp gnus-article-x-face-command)
11186                          (error "%s is not a function"
11187                                 gnus-article-x-face-command))
11188                      (funcall gnus-article-x-face-command beg end))
11189               (call-process-region beg end "sh" nil 0 nil
11190                                    "-c" gnus-article-x-face-command))))))))
11191
11192 (defun gnus-article-de-quoted-unreadable (&optional force)
11193   "Do a naive translation of a quoted-printable-encoded article.
11194 This is in no way, shape or form meant as a replacement for real MIME
11195 processing, but is simply a stop-gap measure until MIME support is
11196 written.
11197 If FORCE, decode the article whether it is marked as quoted-printable
11198 or not." 
11199   (interactive (list 'force))
11200   (save-excursion
11201     (set-buffer gnus-article-buffer)
11202     (let ((case-fold-search t)
11203           (buffer-read-only nil)
11204           (type (gnus-fetch-field "content-transfer-encoding")))
11205       (if (or force (and type (string-match "quoted-printable" type)))
11206           (progn
11207             (goto-char (point-min))
11208             (search-forward "\n\n" nil 'move)
11209             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11210
11211 (defun gnus-mime-decode-quoted-printable (from to)
11212   ;; Decode quoted-printable from region between FROM and TO.
11213   (save-excursion
11214     (goto-char from)
11215     (while (search-forward "=" to t)
11216       (cond ((eq (following-char) ?\n)
11217              (delete-char -1)
11218              (delete-char 1))
11219             ((looking-at "[0-9A-F][0-9A-F]")
11220              (delete-char -1)
11221              (insert (hexl-hex-string-to-integer
11222                       (buffer-substring (point) (+ 2 (point)))))
11223              (delete-char 2))
11224             ((looking-at "=")
11225              (delete-char 1))
11226             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11227
11228 (defvar gnus-article-time-units
11229   (list (cons 'year (* 365.25 24 60 60))
11230         (cons 'week (* 7 24 60 60))
11231         (cons 'day (* 24 60 60))
11232         (cons 'hour (* 60 60))
11233         (cons 'minute 60)
11234         (cons 'second 1)))
11235
11236 (defun gnus-article-date-ut (&optional type)
11237   "Convert DATE date to universal time in the current article.
11238 If TYPE is `local', convert to local time; if it is `lapsed', output
11239 how much time has lapsed since DATE."
11240   (interactive (list 'ut))
11241   (let ((date (mail-header-date (or gnus-current-headers 
11242                                     (gnus-get-header-by-number
11243                                      (gnus-summary-article-number))"")))
11244         (date-regexp "^Date: \\|^X-Sent: "))
11245     (if (or (not date)
11246             (string= date ""))
11247         ()
11248       (save-excursion
11249         (set-buffer gnus-article-buffer)
11250         (let ((buffer-read-only nil))
11251           (goto-char (point-min))
11252           (if (and (re-search-forward date-regexp nil t)
11253                    (progn 
11254                      (beginning-of-line)
11255                      (looking-at date-regexp)))
11256               (delete-region (gnus-point-at-bol)
11257                              (progn (end-of-line) (1+ (point))))
11258             (goto-char (point-min))
11259             (goto-char (- (search-forward "\n\n") 2)))
11260           (insert
11261            (cond 
11262             ((eq type 'local)
11263              (concat "Date: " (condition-case ()
11264                                   (timezone-make-date-arpa-standard date)
11265                                 (error date))
11266                      "\n"))
11267             ((eq type 'ut)
11268              (concat "Date: "
11269                      (condition-case ()
11270                          (timezone-make-date-arpa-standard date nil "UT")
11271                        (error date))
11272                      "\n"))
11273             ((eq type 'lapsed)
11274              ;; If the date is seriously mangled, the timezone
11275              ;; functions are liable to bug out, so we condition-case
11276              ;; the entire thing.  
11277              (let* ((real-sec (condition-case ()
11278                                   (- (gnus-seconds-since-epoch 
11279                                       (timezone-make-date-arpa-standard
11280                                        (current-time-string) 
11281                                        (current-time-zone) "UT"))
11282                                      (gnus-seconds-since-epoch 
11283                                       (timezone-make-date-arpa-standard 
11284                                        date nil "UT")))
11285                                 (error 0)))
11286                     (sec (abs real-sec))
11287                     num prev)
11288                (if (zerop sec)
11289                    "X-Sent: Now\n"
11290                  (concat
11291                   "X-Sent: "
11292                   (mapconcat 
11293                    (lambda (unit)
11294                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11295                          ""
11296                        (setq sec (- sec (* num (cdr unit))))
11297                        (prog1
11298                            (concat (if prev ", " "") (int-to-string 
11299                                                       (floor num))
11300                                    " " (symbol-name (car unit))
11301                                    (if (> num 1) "s" ""))
11302                          (setq prev t))))
11303                    gnus-article-time-units "")
11304                   (if (> real-sec 0)
11305                       " ago\n"
11306                     " in the future\n")))))
11307             (t
11308              (error "Unknown conversion type: %s" type)))))))))
11309
11310 (defun gnus-article-date-local ()
11311   "Convert the current article date to the local timezone."
11312   (interactive)
11313   (gnus-article-date-ut 'local))
11314
11315 (defun gnus-article-date-lapsed ()
11316   "Convert the current article date to time lapsed since it was sent."
11317   (interactive)
11318   (gnus-article-date-ut 'lapsed))
11319
11320 (defun gnus-article-maybe-highlight ()
11321   "Do some article highlighting if `gnus-visual' is non-nil."
11322   (if gnus-visual (gnus-article-highlight-some)))
11323
11324 ;; Article savers.
11325
11326 (defun gnus-output-to-rmail (file-name)
11327   "Append the current article to an Rmail file named FILE-NAME."
11328   (require 'rmail)
11329   ;; Most of these codes are borrowed from rmailout.el.
11330   (setq file-name (expand-file-name file-name))
11331   (setq rmail-default-rmail-file file-name)
11332   (let ((artbuf (current-buffer))
11333         (tmpbuf (get-buffer-create " *Gnus-output*")))
11334     (save-excursion
11335       (or (get-file-buffer file-name)
11336           (file-exists-p file-name)
11337           (if (gnus-yes-or-no-p
11338                (concat "\"" file-name "\" does not exist, create it? "))
11339               (let ((file-buffer (create-file-buffer file-name)))
11340                 (save-excursion
11341                   (set-buffer file-buffer)
11342                   (rmail-insert-rmail-file-header)
11343                   (let ((require-final-newline nil))
11344                     (write-region (point-min) (point-max) file-name t 1)))
11345                 (kill-buffer file-buffer))
11346             (error "Output file does not exist")))
11347       (set-buffer tmpbuf)
11348       (buffer-disable-undo (current-buffer))
11349       (erase-buffer)
11350       (insert-buffer-substring artbuf)
11351       (gnus-convert-article-to-rmail)
11352       ;; Decide whether to append to a file or to an Emacs buffer.
11353       (let ((outbuf (get-file-buffer file-name)))
11354         (if (not outbuf)
11355             (append-to-file (point-min) (point-max) file-name)
11356           ;; File has been visited, in buffer OUTBUF.
11357           (set-buffer outbuf)
11358           (let ((buffer-read-only nil)
11359                 (msg (and (boundp 'rmail-current-message)
11360                           (symbol-value 'rmail-current-message))))
11361             ;; If MSG is non-nil, buffer is in RMAIL mode.
11362             (if msg
11363                 (progn (widen)
11364                        (narrow-to-region (point-max) (point-max))))
11365             (insert-buffer-substring tmpbuf)
11366             (if msg
11367                 (progn
11368                   (goto-char (point-min))
11369                   (widen)
11370                   (search-backward "\^_")
11371                   (narrow-to-region (point) (point-max))
11372                   (goto-char (1+ (point-min)))
11373                   (rmail-count-new-messages t)
11374                   (rmail-show-message msg)))))))
11375     (kill-buffer tmpbuf)))
11376
11377 (defun gnus-output-to-file (file-name)
11378   "Append the current article to a file named FILE-NAME."
11379   (setq file-name (expand-file-name file-name))
11380   (let ((artbuf (current-buffer))
11381         (tmpbuf (get-buffer-create " *Gnus-output*")))
11382     (save-excursion
11383       (set-buffer tmpbuf)
11384       (buffer-disable-undo (current-buffer))
11385       (erase-buffer)
11386       (insert-buffer-substring artbuf)
11387       ;; Append newline at end of the buffer as separator, and then
11388       ;; save it to file.
11389       (goto-char (point-max))
11390       (insert "\n")
11391       (append-to-file (point-min) (point-max) file-name))
11392     (kill-buffer tmpbuf)))
11393
11394 (defun gnus-convert-article-to-rmail ()
11395   "Convert article in current buffer to Rmail message format."
11396   (let ((buffer-read-only nil))
11397     ;; Convert article directly into Babyl format.
11398     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11399     (goto-char (point-min))
11400     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11401     (while (search-forward "\n\^_" nil t) ;single char
11402       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
11403     (goto-char (point-max))
11404     (insert "\^_")))
11405
11406 (defun gnus-narrow-to-page (&optional arg)
11407   "Make text outside current page invisible except for page delimiter.
11408 A numeric arg specifies to move forward or backward by that many pages,
11409 thus showing a page other than the one point was originally in."
11410   (interactive "P")
11411   (setq arg (if arg (prefix-numeric-value arg) 0))
11412   (save-excursion
11413     (forward-page -1)                   ;Beginning of current page.
11414     (widen)
11415     (if (> arg 0)
11416         (forward-page arg)
11417       (if (< arg 0)
11418           (forward-page (1- arg))))
11419     ;; Find the end of the page.
11420     (forward-page)
11421     ;; If we stopped due to end of buffer, stay there.
11422     ;; If we stopped after a page delimiter, put end of restriction
11423     ;; at the beginning of that line.
11424     ;; These are commented out.
11425     ;;    (if (save-excursion (beginning-of-line)
11426     ;;                  (looking-at page-delimiter))
11427     ;;  (beginning-of-line))
11428     (narrow-to-region (point)
11429                       (progn
11430                         ;; Find the top of the page.
11431                         (forward-page -1)
11432                         ;; If we found beginning of buffer, stay there.
11433                         ;; If extra text follows page delimiter on same line,
11434                         ;; include it.
11435                         ;; Otherwise, show text starting with following line.
11436                         (if (and (eolp) (not (bobp)))
11437                             (forward-line 1))
11438                         (point)))))
11439
11440 (defun gnus-gmt-to-local ()
11441   "Rewrite Date header described in GMT to local in current buffer.
11442 Intended to be used with gnus-article-prepare-hook."
11443   (save-excursion
11444     (save-restriction
11445       (widen)
11446       (goto-char (point-min))
11447       (narrow-to-region (point-min)
11448                         (progn (search-forward "\n\n" nil 'move) (point)))
11449       (goto-char (point-min))
11450       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11451           (let ((buffer-read-only nil)
11452                 (date (buffer-substring-no-properties
11453                        (match-beginning 1) (match-end 1))))
11454             (delete-region (match-beginning 1) (match-end 1))
11455             (insert
11456              (timezone-make-date-arpa-standard 
11457               date nil (current-time-zone))))))))
11458
11459
11460 ;; Article mode commands
11461
11462 (defun gnus-article-next-page (&optional lines)
11463   "Show next page of current article.
11464 If end of article, return non-nil. Otherwise return nil.
11465 Argument LINES specifies lines to be scrolled up."
11466   (interactive "P")
11467   (move-to-window-line -1)
11468   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11469   (if (save-excursion
11470         (end-of-line)
11471         (and (pos-visible-in-window-p)  ;Not continuation line.
11472              (eobp)))
11473       ;; Nothing in this page.
11474       (if (or (not gnus-break-pages)
11475               (save-excursion
11476                 (save-restriction
11477                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11478           t                             ;Nothing more.
11479         (gnus-narrow-to-page 1)         ;Go to next page.
11480         nil)
11481     ;; More in this page.
11482     (condition-case ()
11483         (scroll-up lines)
11484       (end-of-buffer
11485        ;; Long lines may cause an end-of-buffer error.
11486        (goto-char (point-max))))
11487     nil))
11488
11489 (defun gnus-article-prev-page (&optional lines)
11490   "Show previous page of current article.
11491 Argument LINES specifies lines to be scrolled down."
11492   (interactive "P")
11493   (move-to-window-line 0)
11494   (if (and gnus-break-pages
11495            (bobp)
11496            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11497       (progn
11498         (gnus-narrow-to-page -1)        ;Go to previous page.
11499         (goto-char (point-max))
11500         (recenter -1))
11501     (scroll-down lines)))
11502
11503 (defun gnus-article-refer-article ()
11504   "Read article specified by message-id around point."
11505   (interactive)
11506   (search-forward ">" nil t)            ;Move point to end of "<....>".
11507   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11508       (let ((message-id
11509              (buffer-substring (match-beginning 1) (match-end 1))))
11510         (set-buffer gnus-summary-buffer)
11511         (gnus-summary-refer-article message-id))
11512     (error "No references around point")))
11513
11514 (defun gnus-article-show-summary ()
11515   "Reconfigure windows to show summary buffer."
11516   (interactive)
11517   (gnus-configure-windows 'article)
11518   (gnus-summary-goto-subject gnus-current-article))
11519
11520 (defun gnus-article-describe-briefly ()
11521   "Describe article mode commands briefly."
11522   (interactive)
11523   (gnus-message 6
11524                 (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")))
11525
11526 (defun gnus-article-summary-command ()
11527   "Execute the last keystroke in the summary buffer."
11528   (interactive)
11529   (let ((obuf (current-buffer))
11530         (owin (current-window-configuration))
11531         func)
11532     (switch-to-buffer gnus-summary-buffer 'norecord)
11533     (setq func (lookup-key (current-local-map) (this-command-keys)))
11534     (call-interactively func)
11535     (set-buffer obuf)
11536     (set-window-configuration owin)
11537     (set-window-point (get-buffer-window (current-buffer)) (point))))
11538
11539 (defun gnus-article-summary-command-nosave ()
11540   "Execute the last keystroke in the summary buffer."
11541   (interactive)
11542   (let (func)
11543     (pop-to-buffer gnus-summary-buffer 'norecord)
11544     (setq func (lookup-key (current-local-map) (this-command-keys)))
11545     (call-interactively func)))
11546
11547 \f
11548 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11549
11550 ;;;###autoload
11551 (defalias 'gnus-batch-kill 'gnus-batch-score)
11552 ;;;###autoload
11553 (defun gnus-batch-score ()
11554   "Run batched scoring.
11555 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11556 Newsgroups is a list of strings in Bnews format.  If you want to score
11557 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11558 score the alt hierarchy, you'd say \"!alt.all\"."
11559   (interactive)
11560   (let* ((yes-and-no
11561           (gnus-newsrc-parse-options
11562            (apply (function concat)
11563                   (mapcar (lambda (g) (concat g " "))
11564                           command-line-args-left))))
11565          (gnus-expert-user t)
11566          (nnmail-spool-file nil)
11567          (gnus-use-dribble-file nil)
11568          (yes (car yes-and-no))
11569          (no (cdr yes-and-no))
11570          group newsrc entry
11571          ;; Disable verbose message.
11572          gnus-novice-user gnus-large-newsgroup)
11573     ;; Eat all arguments.
11574     (setq command-line-args-left nil)
11575     ;; Start Gnus.
11576     (gnus)
11577     ;; Apply kills to specified newsgroups in command line arguments.
11578     (setq newsrc (cdr gnus-newsrc-alist))
11579     (while newsrc
11580       (setq group (car (car newsrc)))
11581       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11582       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11583                (and (car entry)
11584                     (or (eq (car entry) t)
11585                         (not (zerop (car entry)))))
11586                (if yes (string-match yes group) t)
11587                (or (null no) (not (string-match no group))))
11588           (progn
11589             (gnus-summary-read-group group nil t)
11590             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11591                  (gnus-summary-exit))))
11592       (setq newsrc (cdr newsrc)))
11593     ;; Exit Emacs.
11594     (switch-to-buffer gnus-group-buffer)
11595     (gnus-group-save-newsrc)))
11596
11597 (defun gnus-apply-kill-file ()
11598   "Apply a kill file to the current newsgroup.
11599 Returns the number of articles marked as read."
11600   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11601           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11602       (gnus-apply-kill-file-internal)
11603     0))
11604
11605 (defun gnus-kill-save-kill-buffer ()
11606   (save-excursion
11607     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11608       (if (get-file-buffer file)
11609           (progn
11610             (set-buffer (get-file-buffer file))
11611             (and (buffer-modified-p) (save-buffer))
11612             (kill-buffer (current-buffer)))))))
11613
11614 (defvar gnus-kill-file-name "KILL"
11615   "Suffix of the kill files.")
11616
11617 (defun gnus-newsgroup-kill-file (newsgroup)
11618   "Return the name of a kill file name for NEWSGROUP.
11619 If NEWSGROUP is nil, return the global kill file name instead."
11620   (cond ((or (null newsgroup)
11621              (string-equal newsgroup ""))
11622          ;; The global KILL file is placed at top of the directory.
11623          (expand-file-name gnus-kill-file-name
11624                            (or gnus-kill-files-directory "~/News")))
11625         ((gnus-use-long-file-name 'not-kill)
11626          ;; Append ".KILL" to newsgroup name.
11627          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11628                                    "." gnus-kill-file-name)
11629                            (or gnus-kill-files-directory "~/News")))
11630         (t
11631          ;; Place "KILL" under the hierarchical directory.
11632          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11633                                    "/" gnus-kill-file-name)
11634                            (or gnus-kill-files-directory "~/News")))))
11635
11636 \f
11637 ;;;
11638 ;;; Dribble file
11639 ;;;
11640
11641 (defvar gnus-dribble-ignore nil)
11642 (defvar gnus-dribble-eval-file nil)
11643
11644 (defun gnus-dribble-file-name ()
11645   (concat gnus-current-startup-file "-dribble"))
11646
11647 (defun gnus-dribble-enter (string)
11648   (if (and (not gnus-dribble-ignore)
11649            gnus-dribble-buffer
11650            (buffer-name gnus-dribble-buffer))
11651       (let ((obuf (current-buffer)))
11652         (set-buffer gnus-dribble-buffer)
11653         (insert string "\n")
11654         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11655         (set-buffer obuf))))
11656
11657 (defun gnus-dribble-read-file ()
11658   (let ((dribble-file (gnus-dribble-file-name)))
11659     (save-excursion 
11660       (set-buffer (setq gnus-dribble-buffer 
11661                         (get-buffer-create 
11662                          (file-name-nondirectory dribble-file))))
11663       (gnus-add-current-to-buffer-list)
11664       (erase-buffer)
11665       (set-visited-file-name dribble-file)
11666       (buffer-disable-undo (current-buffer))
11667       (bury-buffer (current-buffer))
11668       (set-buffer-modified-p nil)
11669       (let ((auto (make-auto-save-file-name))
11670             (gnus-dribble-ignore t))
11671         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11672             (progn
11673               (if (file-newer-than-file-p auto dribble-file)
11674                   (setq dribble-file auto))
11675               (insert-file-contents dribble-file)
11676               (if (not (zerop (buffer-size)))
11677                   (set-buffer-modified-p t))
11678               (if (gnus-y-or-n-p 
11679                    "Auto-save file exists. Do you want to read it? ")
11680                   (setq gnus-dribble-eval-file t))))))))
11681
11682 (defun gnus-dribble-eval-file ()
11683   (if (not gnus-dribble-eval-file)
11684       ()
11685     (setq gnus-dribble-eval-file nil)
11686     (save-excursion
11687       (let ((gnus-dribble-ignore t))
11688         (set-buffer gnus-dribble-buffer)
11689         (eval-buffer (current-buffer))))))
11690
11691 (defun gnus-dribble-delete-file ()
11692   (if (file-exists-p (gnus-dribble-file-name))
11693       (delete-file (gnus-dribble-file-name)))
11694   (if gnus-dribble-buffer
11695       (save-excursion
11696         (set-buffer gnus-dribble-buffer)
11697         (let ((auto (make-auto-save-file-name)))
11698           (if (file-exists-p auto)
11699               (delete-file auto))
11700           (erase-buffer)
11701           (set-buffer-modified-p nil)))))
11702
11703 (defun gnus-dribble-save ()
11704   (if (and gnus-dribble-buffer
11705            (buffer-name gnus-dribble-buffer))
11706       (save-excursion
11707         (set-buffer gnus-dribble-buffer)
11708         (save-buffer))))
11709
11710 (defun gnus-dribble-clear ()
11711   (save-excursion
11712     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11713         (progn
11714           (set-buffer gnus-dribble-buffer)
11715           (erase-buffer)
11716           (set-buffer-modified-p nil)
11717           (setq buffer-saved-size (buffer-size))))))
11718
11719 ;;;
11720 ;;; Server Communication
11721 ;;;
11722
11723 (defun gnus-start-news-server (&optional confirm)
11724   "Open a method for getting news.
11725 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11726   (let (how)
11727     (if gnus-current-select-method
11728         ;; Stream is already opened.
11729         nil
11730       ;; Open NNTP server.
11731       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11732       (if confirm
11733           (progn
11734             ;; Read server name with completion.
11735             (setq gnus-nntp-server
11736                   (completing-read "NNTP server: "
11737                                    (mapcar (lambda (server) (list server))
11738                                            (cons (list gnus-nntp-server)
11739                                                  gnus-secondary-servers))
11740                                    nil nil gnus-nntp-server))))
11741
11742       (if (and gnus-nntp-server 
11743                (stringp gnus-nntp-server)
11744                (not (string= gnus-nntp-server "")))
11745           (setq gnus-select-method
11746                 (cond ((or (string= gnus-nntp-server "")
11747                            (string= gnus-nntp-server "::"))
11748                        (list 'nnspool (system-name)))
11749                       ((string-match "^:" gnus-nntp-server)
11750                        (list 'nnmh gnus-nntp-server 
11751                              (list 'nnmh-directory 
11752                                    (file-name-as-directory
11753                                     (expand-file-name
11754                                      (concat "~/" (substring
11755                                                    gnus-nntp-server 1)))))
11756                              (list 'nnmh-get-new-mail nil)))
11757                       (t
11758                        (list 'nntp gnus-nntp-server)))))
11759
11760       (setq how (car gnus-select-method))
11761       (cond ((eq how 'nnspool)
11762              (require 'nnspool)
11763              (gnus-message 5 "Looking up local news spool..."))
11764             ((eq how 'nnmh)
11765              (require 'nnmh)
11766              (gnus-message 5 "Looking up mh spool..."))
11767             (t
11768              (require 'nntp)))
11769       (setq gnus-current-select-method gnus-select-method)
11770       (run-hooks 'gnus-open-server-hook)
11771       (or 
11772        ;; gnus-open-server-hook might have opened it
11773        (gnus-server-opened gnus-select-method)  
11774        (gnus-open-server gnus-select-method)
11775        (gnus-y-or-n-p
11776         (format
11777          "%s open error: '%s'. Continue? "
11778          (nth 1 gnus-select-method)
11779          (gnus-status-message gnus-select-method)))
11780        (progn
11781          (gnus-message 1 "Couldn't open server on %s" 
11782                        (nth 1 gnus-select-method))
11783          (ding)
11784          nil)))))
11785
11786 (defun gnus-check-server (&optional method)
11787   "If the news server is down, start it up again."
11788   (let ((method (if method method gnus-select-method)))
11789     (and (stringp method)
11790          (setq method (gnus-server-to-method method)))
11791     (if (gnus-server-opened method)
11792         ;; Stream is already opened.
11793         t
11794       ;; Open server.
11795       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11796       (run-hooks 'gnus-open-server-hook)
11797       (prog1
11798           (gnus-open-server method)
11799         (message "")))))
11800
11801 (defun gnus-nntp-message (&optional message)
11802   "Check the status of the NNTP server.
11803 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11804 is returned insted of the status string."
11805   (let ((status (gnus-status-message (gnus-find-method-for-group 
11806                                       gnus-newsgroup-name)))
11807         (message (or message "")))
11808     (if (and (stringp status) (> (length status) 0))
11809         status message)))
11810
11811 (defun gnus-get-function (method function)
11812   (and (stringp method)
11813        (setq method (gnus-server-to-method method)))
11814   (let ((func (intern (format "%s-%s" (car method) function))))
11815     (if (not (fboundp func)) 
11816         (progn
11817           (require (car method))
11818           (if (not (fboundp func)) 
11819               (error "No such function: %s" func))))
11820     func))
11821
11822 ;;; Interface functions to the backends.
11823
11824 (defun gnus-open-server (method)
11825   (funcall (gnus-get-function method 'open-server)
11826            (nth 1 method) (nthcdr 2 method)))
11827
11828 (defun gnus-close-server (method)
11829   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11830
11831 (defun gnus-request-list (method)
11832   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11833
11834 (defun gnus-request-list-newsgroups (method)
11835   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11836
11837 (defun gnus-request-newgroups (date method)
11838   (funcall (gnus-get-function method 'request-newgroups) 
11839            date (nth 1 method)))
11840
11841 (defun gnus-server-opened (method)
11842   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11843
11844 (defun gnus-status-message (method)
11845   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11846                   method)))
11847     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11848
11849 (defun gnus-request-group (group &optional dont-check)
11850   (let ((method (gnus-find-method-for-group group)))
11851     (funcall (gnus-get-function method 'request-group) 
11852              (gnus-group-real-name group) (nth 1 method) dont-check)))
11853
11854 (defun gnus-request-asynchronous (group &optional articles)
11855   (let ((method (gnus-find-method-for-group group)))
11856     (funcall (gnus-get-function method 'request-asynchronous) 
11857              (gnus-group-real-name group) (nth 1 method) articles)))
11858
11859 (defun gnus-list-active-group (group)
11860   (let ((method (gnus-find-method-for-group group))
11861         (func 'list-active-group))
11862     (and (gnus-check-backend-function func group)
11863          (funcall (gnus-get-function method func) 
11864                   (gnus-group-real-name group) (nth 1 method)))))
11865
11866 (defun gnus-request-group-description (group)
11867   (let ((method (gnus-find-method-for-group group))
11868         (func 'request-group-description))
11869     (and (gnus-check-backend-function func group)
11870          (funcall (gnus-get-function method func) 
11871                   (gnus-group-real-name group) (nth 1 method)))))
11872
11873 (defun gnus-close-group (group)
11874   (let ((method (gnus-find-method-for-group group)))
11875     (funcall (gnus-get-function method 'close-group) 
11876              (gnus-group-real-name group) (nth 1 method))))
11877
11878 (defun gnus-retrieve-headers (articles group)
11879   (let ((method (gnus-find-method-for-group group)))
11880     (if (and gnus-use-cache (numberp (car articles)))
11881         (gnus-cache-retrieve-headers articles group)
11882       (funcall (gnus-get-function method 'retrieve-headers) 
11883                articles (gnus-group-real-name group) (nth 1 method)))))
11884
11885 (defun gnus-retrieve-groups (groups method)
11886   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11887
11888 (defun gnus-request-article (article group &optional buffer)
11889   (let ((method (gnus-find-method-for-group group)))
11890     (funcall (gnus-get-function method 'request-article) 
11891              article (gnus-group-real-name group) (nth 1 method) buffer)))
11892
11893 (defun gnus-request-head (article group)
11894   (let ((method (gnus-find-method-for-group group)))
11895     (funcall (gnus-get-function method 'request-head) 
11896              article (gnus-group-real-name group) (nth 1 method))))
11897
11898 (defun gnus-request-body (article group)
11899   (let ((method (gnus-find-method-for-group group)))
11900     (funcall (gnus-get-function method 'request-body) 
11901              article (gnus-group-real-name group) (nth 1 method))))
11902
11903 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11904 (defun gnus-request-post-buffer (post group subject header artbuf
11905                                       info follow-to respect-poster)
11906   (let* ((info (or info (and group (nth 2 (gnus-gethash 
11907                                            group gnus-newsrc-hashtb)))))
11908          (method
11909           (if (and gnus-post-method
11910                    ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11911                    (memq 'post (assoc
11912                                 (format "%s" (car (gnus-find-method-for-group
11913                                                    gnus-newsgroup-name)))
11914                                 gnus-valid-select-methods)))
11915               gnus-post-method
11916             (gnus-find-method-for-group gnus-newsgroup-name))))
11917     (or (gnus-check-server method)
11918         (error "Can't open server %s:%s" (car method) (nth 1 method)))
11919     (let ((mail-self-blind nil)
11920           (mail-archive-file-name nil))
11921       (funcall (gnus-get-function method 'request-post-buffer) 
11922                post group subject header artbuf info follow-to
11923                respect-poster))))
11924
11925 (defun gnus-request-post (method &optional force)
11926   (and (stringp method)
11927        (setq method (gnus-server-to-method method)))
11928   (and (not force) gnus-post-method
11929        (memq 'post (assoc (format "%s" (car method))
11930                           gnus-valid-select-methods))
11931        (setq method gnus-post-method))
11932   (funcall (gnus-get-function method 'request-post) 
11933            (nth 1 method)))
11934
11935 (defun gnus-request-expire-articles (articles group &optional force)
11936   (let ((method (gnus-find-method-for-group group)))
11937     (funcall (gnus-get-function method 'request-expire-articles) 
11938              articles (gnus-group-real-name group) (nth 1 method)
11939              force)))
11940
11941 (defun gnus-request-move-article 
11942   (article group server accept-function &optional last)
11943   (let ((method (gnus-find-method-for-group group)))
11944     (funcall (gnus-get-function method 'request-move-article) 
11945              article (gnus-group-real-name group) 
11946              (nth 1 method) accept-function last)))
11947
11948 (defun gnus-request-accept-article (group &optional last)
11949   (goto-char (point-max))
11950   (or (bolp) (insert "\n"))
11951   (let ((func (if (symbolp group) group
11952                 (car (gnus-find-method-for-group group)))))
11953     (funcall (intern (format "%s-request-accept-article" func))
11954              (if (stringp group) (gnus-group-real-name group) group)
11955              last)))
11956
11957 (defun gnus-request-replace-article (article group buffer)
11958   (let ((func (car (gnus-find-method-for-group group))))
11959     (funcall (intern (format "%s-request-replace-article" func))
11960              article (gnus-group-real-name group) buffer)))
11961
11962 (defun gnus-request-create-group (group)
11963   (let ((method (gnus-find-method-for-group group)))
11964     (funcall (gnus-get-function method 'request-create-group) 
11965              (gnus-group-real-name group) (nth 1 method))))
11966
11967 (defun gnus-member-of-valid (symbol group)
11968   (memq symbol (assoc
11969                 (format "%s" (car (gnus-find-method-for-group group)))
11970                 gnus-valid-select-methods)))
11971
11972 (defun gnus-secondary-method-p (method)
11973   (let ((methods gnus-secondary-select-methods)
11974         (gmethod (gnus-server-get-method nil method)))
11975     (while (and methods
11976                 (not (equal (gnus-server-get-method nil (car methods)) 
11977                             gmethod)))
11978       (setq methods (cdr methods)))
11979     methods))
11980
11981 (defun gnus-find-method-for-group (group &optional info)
11982   (or gnus-override-method
11983       (and (not group)
11984            gnus-select-method)
11985       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11986             method)
11987         (if (or (not info)
11988                 (not (setq method (nth 4 info))))
11989             (setq method gnus-select-method)
11990           (setq method
11991                 (cond ((stringp method)
11992                        (gnus-server-to-method method))
11993                       ((stringp (car method))
11994                        (gnus-server-extend-method group method))
11995                       (t
11996                        method))))
11997         (gnus-server-add-address method))))
11998
11999 (defun gnus-check-backend-function (func group)
12000   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
12001                   group)))
12002     (fboundp (intern (format "%s-%s" method func)))))
12003
12004 (defun gnus-methods-using (method)
12005   (let ((valids gnus-valid-select-methods)
12006         outs)
12007     (while valids
12008       (if (memq method (car valids)) 
12009           (setq outs (cons (car valids) outs)))
12010       (setq valids (cdr valids)))
12011     outs))
12012
12013 ;;; 
12014 ;;; Active & Newsrc File Handling
12015 ;;;
12016
12017 ;; Newsrc related functions.
12018 ;; Gnus internal format of gnus-newsrc-alist:
12019 ;; (("alt.general" 3 (1 . 1))
12020 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
12021 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
12022 ;; The first item is the group name; the second is the subscription
12023 ;; level; the third is either a range of a list of ranges of read
12024 ;; articles, the optional fourth element is a list of marked articles,
12025 ;; the optional fifth element is the select method.
12026 ;;
12027 ;; Gnus internal format of gnus-newsrc-hashtb:
12028 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
12029 ;; This is the entry for "alt.misc". The first element is the number
12030 ;; of unread articles in "alt.misc". The cdr of this entry is the
12031 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
12032 ;; trivial to remove or add new elements into gnus-newsrc-alist
12033 ;; without scanning the entire list. So, to get the actual information
12034 ;; of "alt.misc", you'd say something like 
12035 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
12036 ;;
12037 ;; Gnus internal format of gnus-active-hashtb:
12038 ;; ((1 . 1))
12039 ;;  (5 . 10))
12040 ;;  (67 . 99)) ...)
12041 ;; The only element in each entry in this hash table is a range of
12042 ;; (possibly) available articles. (Articles in this range may have
12043 ;; been expired or canceled.)
12044 ;;
12045 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12046 ;; ("alt.misc" "alt.test" "alt.general" ...)
12047
12048 (defun gnus-setup-news (&optional rawfile level)
12049   "Setup news information.
12050 If RAWFILE is non-nil, the .newsrc file will also be read.
12051 If LEVEL is non-nil, the news will be set up at level LEVEL."
12052   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
12053     ;; Clear some variables to re-initialize news information.
12054     (if init (setq gnus-newsrc-alist nil 
12055                    gnus-active-hashtb nil))
12056
12057     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12058     (if init (gnus-read-newsrc-file rawfile))
12059
12060     ;; If we don't read the complete active file, we fill in the
12061     ;; hashtb here. 
12062     (if (or (null gnus-read-active-file)
12063             (eq gnus-read-active-file 'some))
12064         (gnus-update-active-hashtb-from-killed))
12065
12066     ;; Read the active file and create `gnus-active-hashtb'.
12067     ;; If `gnus-read-active-file' is nil, then we just create an empty
12068     ;; hash table. The partial filling out of the hash table will be
12069     ;; done in `gnus-get-unread-articles'.
12070     (and gnus-read-active-file 
12071          (not level)
12072          (gnus-read-active-file))
12073
12074     (or gnus-active-hashtb
12075         (setq gnus-active-hashtb (make-vector 4095 0)))
12076
12077     ;; Possibly eval the dribble file.
12078     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
12079
12080     (gnus-update-format-specifications)
12081
12082     ;; Find new newsgroups and treat them.
12083     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12084              (gnus-check-server gnus-select-method))
12085         (gnus-find-new-newsgroups))
12086
12087     ;; Find the number of unread articles in each non-dead group.
12088     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12089       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12090
12091     (if (and init gnus-check-bogus-newsgroups 
12092              gnus-read-active-file (not level)
12093              (gnus-server-opened gnus-select-method))
12094         (gnus-check-bogus-newsgroups))))
12095
12096 (defun gnus-find-new-newsgroups ()
12097   "Search for new newsgroups and add them.
12098 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12099 The `-n' option line from .newsrc is respected."
12100   (interactive)
12101   (or (gnus-check-first-time-used)
12102       (if (or (consp gnus-check-new-newsgroups)
12103               (eq gnus-check-new-newsgroups 'ask-server))
12104           (gnus-ask-server-for-new-groups)
12105         (let ((groups 0)
12106               group new-newsgroups)
12107           (gnus-message 5 "Looking for new newsgroups...")
12108           (or gnus-have-read-active-file (gnus-read-active-file))
12109           (setq gnus-newsrc-last-checked-date (current-time-string))
12110           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12111           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12112           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12113           (mapatoms
12114            (lambda (sym)
12115              (if (or (null (setq group (symbol-name sym)))
12116                      (null (symbol-value sym))
12117                      (gnus-gethash group gnus-killed-hashtb)
12118                      (gnus-gethash group gnus-newsrc-hashtb))
12119                  ()
12120                (let ((do-sub (gnus-matches-options-n group)))
12121                  (cond 
12122                   ((eq do-sub 'subscribe)
12123                    (setq groups (1+ groups))
12124                    (gnus-sethash group group gnus-killed-hashtb)
12125                    (funcall gnus-subscribe-options-newsgroup-method group))
12126                   ((eq do-sub 'ignore)
12127                    nil)
12128                   (t
12129                    (setq groups (1+ groups))
12130                    (gnus-sethash group group gnus-killed-hashtb)
12131                    (if gnus-subscribe-hierarchical-interactive
12132                        (setq new-newsgroups (cons group new-newsgroups))
12133                      (funcall gnus-subscribe-newsgroup-method group)))))))
12134            gnus-active-hashtb)
12135           (if new-newsgroups 
12136               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12137           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12138           (if (> groups 0)
12139               (gnus-message 6 "%d new newsgroup%s arrived." 
12140                             groups (if (> groups 1) "s have" " has"))
12141             (gnus-message 6 "No new newsgroups."))))))
12142
12143 (defun gnus-matches-options-n (group)
12144   ;; Returns `subscribe' if the group is to be uncoditionally
12145   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12146   ;; no match for the group.
12147
12148   ;; First we check the two user variables.
12149   (cond
12150    ((and gnus-options-subscribe
12151          (string-match gnus-options-subscribe group))
12152     'subscribe)
12153    ((and gnus-options-not-subscribe
12154          (string-match gnus-options-not-subscribe group))
12155     'ignore)
12156    ;; Then we go through the list that was retrieved from the .newsrc
12157    ;; file.  This list has elements on the form 
12158    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12159    ;; is in the reverse order of the options line) is returned.
12160    (t
12161     (let ((regs gnus-newsrc-options-n))
12162       (while (and regs
12163                   (not (string-match (car (car regs)) group)))
12164         (setq regs (cdr regs)))
12165       (and regs (cdr (car regs)))))))
12166
12167 (defun gnus-ask-server-for-new-groups ()
12168   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12169          (methods (cons gnus-select-method 
12170                         (append
12171                          (and (consp gnus-check-new-newsgroups)
12172                               gnus-check-new-newsgroups)
12173                          gnus-secondary-select-methods)))
12174          (groups 0)
12175          (new-date (current-time-string))
12176          (hashtb (gnus-make-hashtable 100))
12177          group new-newsgroups got-new method)
12178     ;; Go through both primary and secondary select methods and
12179     ;; request new newsgroups.  
12180     (while methods
12181       (setq method (gnus-server-get-method nil (car methods)))
12182       (and (gnus-check-server method)
12183            (gnus-request-newgroups date method)
12184            (save-excursion
12185              (setq got-new t)
12186              (set-buffer nntp-server-buffer)
12187              ;; Enter all the new groups in a hashtable.
12188              (gnus-active-to-gnus-format method hashtb 'ignore)))
12189       (setq methods (cdr methods)))
12190     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12191     ;; Now all new groups from all select methods are in `hashtb'.
12192     (mapatoms
12193      (lambda (group-sym)
12194        (setq group (symbol-name group-sym))
12195        (if (or (null group)
12196                (null (symbol-value group-sym))
12197                (gnus-gethash group gnus-newsrc-hashtb)
12198                (member group gnus-zombie-list)
12199                (member group gnus-killed-list))
12200            ;; The group is already known.
12201            ()
12202          (and (symbol-value group-sym)
12203               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12204          (let ((do-sub (gnus-matches-options-n group)))
12205            (cond ((eq do-sub 'subscribe)
12206                   (setq groups (1+ groups))
12207                   (gnus-sethash group group gnus-killed-hashtb)
12208                   (funcall 
12209                    gnus-subscribe-options-newsgroup-method group))
12210                  ((eq do-sub 'ignore)
12211                   nil)
12212                  (t
12213                   (setq groups (1+ groups))
12214                   (gnus-sethash group group gnus-killed-hashtb)
12215                   (if gnus-subscribe-hierarchical-interactive
12216                       (setq new-newsgroups (cons group new-newsgroups))
12217                     (funcall gnus-subscribe-newsgroup-method group)))))))
12218      hashtb)
12219     (if new-newsgroups 
12220         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12221     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12222     (if (> groups 0)
12223         (gnus-message 6 "%d new newsgroup%s arrived." 
12224                       groups (if (> groups 1) "s have" " has")))
12225     got-new))
12226
12227 (defun gnus-check-first-time-used ()
12228   (if (or (> (length gnus-newsrc-alist) 1)
12229           (file-exists-p gnus-startup-file)
12230           (file-exists-p (concat gnus-startup-file ".el"))
12231           (file-exists-p (concat gnus-startup-file ".eld")))
12232       nil
12233     (gnus-message 6 "First time user; subscribing you to default groups")
12234     (or gnus-have-read-active-file (gnus-read-active-file))
12235     (setq gnus-newsrc-last-checked-date (current-time-string))
12236     (let ((groups gnus-default-subscribed-newsgroups)
12237           group)
12238       (if (eq groups t)
12239           nil
12240         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12241         (mapatoms
12242          (lambda (sym)
12243            (if (null (setq group (symbol-name sym)))
12244                ()
12245              (let ((do-sub (gnus-matches-options-n group)))
12246                (cond 
12247                 ((eq do-sub 'subscribe)
12248                  (gnus-sethash group group gnus-killed-hashtb)
12249                  (funcall gnus-subscribe-options-newsgroup-method group))
12250                 ((eq do-sub 'ignore)
12251                  nil)
12252                 (t
12253                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12254          gnus-active-hashtb)
12255         (while groups
12256           (if (gnus-gethash (car groups) gnus-active-hashtb)
12257               (gnus-group-change-level 
12258                (car groups) gnus-level-default-subscribed gnus-level-killed))
12259           (setq groups (cdr groups)))
12260         (gnus-group-make-help-group)
12261         (and gnus-novice-user
12262              (gnus-message 7 "`A k' to list killed groups"))))))
12263
12264 (defun gnus-subscribe-group (group previous &optional method)
12265   (gnus-group-change-level 
12266    (if method
12267        (list t group gnus-level-default-subscribed nil nil method)
12268      group) 
12269    gnus-level-default-subscribed gnus-level-killed previous t))
12270
12271 ;; `gnus-group-change-level' is the fundamental function for changing
12272 ;; subscription levels of newsgroups. This might mean just changing
12273 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12274 ;; again, which subscribes/unsubscribes a group, which is equally
12275 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12276 ;; from 8-9 to 1-7 means that you remove the group from the list of
12277 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12278 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12279 ;; which is trivial.
12280 ;; ENTRY can either be a string (newsgroup name) or a list (if
12281 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12282 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12283 ;; entries. 
12284 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12285 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12286 ;; after. 
12287 (defun gnus-group-change-level (entry level &optional oldlevel
12288                                       previous fromkilled)
12289   (let (group info active num)
12290     ;; Glean what info we can from the arguments
12291     (if (consp entry)
12292         (if fromkilled (setq group (nth 1 entry))
12293           (setq group (car (nth 2 entry))))
12294       (setq group entry))
12295     (if (and (stringp entry)
12296              oldlevel 
12297              (< oldlevel gnus-level-zombie))
12298         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12299     (if (and (not oldlevel)
12300              (consp entry))
12301         (setq oldlevel (car (cdr (nth 2 entry)))))
12302     (if (stringp previous)
12303         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12304
12305     (if (and (>= oldlevel gnus-level-zombie)
12306              (gnus-gethash group gnus-newsrc-hashtb))
12307         ;; We are trying to subscribe a group that is already
12308         ;; subscribed. 
12309         ()                              ; Do nothing. 
12310
12311       (or (gnus-ephemeral-group-p group)
12312           (gnus-dribble-enter
12313            (format "(gnus-group-change-level %S %S %S %S %S)" 
12314                    group level oldlevel (car (nth 2 previous)) fromkilled)))
12315     
12316       ;; Then we remove the newgroup from any old structures, if needed.
12317       ;; If the group was killed, we remove it from the killed or zombie
12318       ;; list. If not, and it is in fact going to be killed, we remove
12319       ;; it from the newsrc hash table and assoc.
12320       (cond ((>= oldlevel gnus-level-zombie)
12321              (if (= oldlevel gnus-level-zombie)
12322                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12323                (setq gnus-killed-list (delete group gnus-killed-list))))
12324             (t
12325              (if (and (>= level gnus-level-zombie)
12326                       entry)
12327                  (progn
12328                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12329                    (if (nth 3 entry)
12330                        (setcdr (gnus-gethash (car (nth 3 entry))
12331                                              gnus-newsrc-hashtb)
12332                                (cdr entry)))
12333                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12334
12335       ;; Finally we enter (if needed) the list where it is supposed to
12336       ;; go, and change the subscription level. If it is to be killed,
12337       ;; we enter it into the killed or zombie list.
12338       (cond ((>= level gnus-level-zombie)
12339              ;; Remove from the hash table.
12340              (gnus-sethash group nil gnus-newsrc-hashtb)
12341              (or (gnus-group-foreign-p group)
12342                  ;; We do not enter foreign groups into the list of dead
12343                  ;; groups.  
12344                  (if (= level gnus-level-zombie)
12345                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12346                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12347             (t
12348              ;; If the list is to be entered into the newsrc assoc, and
12349              ;; it was killed, we have to create an entry in the newsrc
12350              ;; hashtb format and fix the pointers in the newsrc assoc.
12351              (if (>= oldlevel gnus-level-zombie)
12352                  (progn
12353                    (if (listp entry)
12354                        (progn
12355                          (setq info (cdr entry))
12356                          (setq num (car entry)))
12357                      (setq active (gnus-gethash group gnus-active-hashtb))
12358                      (setq num 
12359                            (if active (- (1+ (cdr active)) (car active)) t))
12360                      ;; Check whether the group is foreign. If so, the
12361                      ;; foreign select method has to be entered into the
12362                      ;; info. 
12363                      (let ((method (gnus-group-method-name group)))
12364                        (if (eq method gnus-select-method)
12365                            (setq info (list group level nil))
12366                          (setq info (list group level nil nil method)))))
12367                    (or previous 
12368                        (setq previous 
12369                              (let ((p gnus-newsrc-alist))
12370                                (while (cdr (cdr p))
12371                                  (setq p (cdr p)))
12372                                p)))
12373                    (setq entry (cons info (cdr (cdr previous))))
12374                    (if (cdr previous)
12375                        (progn
12376                          (setcdr (cdr previous) entry)
12377                          (gnus-sethash group (cons num (cdr previous)) 
12378                                        gnus-newsrc-hashtb))
12379                      (setcdr previous entry)
12380                      (gnus-sethash group (cons num previous)
12381                                    gnus-newsrc-hashtb))
12382                    (if (cdr entry)
12383                        (setcdr (gnus-gethash (car (car (cdr entry)))
12384                                              gnus-newsrc-hashtb)
12385                                entry)))
12386                ;; It was alive, and it is going to stay alive, so we
12387                ;; just change the level and don't change any pointers or
12388                ;; hash table entries.
12389                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12390
12391 (defun gnus-kill-newsgroup (newsgroup)
12392   "Obsolete function. Kills a newsgroup."
12393   (gnus-group-change-level
12394    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12395
12396 (defun gnus-check-bogus-newsgroups (&optional confirm)
12397   "Remove bogus newsgroups.
12398 If CONFIRM is non-nil, the user has to confirm the deletion of every
12399 newsgroup." 
12400   (let ((newsrc (cdr gnus-newsrc-alist))
12401         bogus group entry)
12402     (gnus-message 5 "Checking bogus newsgroups...")
12403     (or gnus-have-read-active-file (gnus-read-active-file))
12404     ;; Find all bogus newsgroup that are subscribed.
12405     (while newsrc
12406       (setq group (car (car newsrc)))
12407       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12408               (nth 4 (car newsrc))      ; Foreign
12409               (and confirm
12410                    (not (gnus-y-or-n-p
12411                          (format "Remove bogus newsgroup: %s " group)))))
12412           ;; Don't remove.
12413           ()
12414         ;; Found a bogus newsgroup.
12415         (setq bogus (cons group bogus)))
12416       (setq newsrc (cdr newsrc)))
12417     ;; Remove all bogus subscribed groups by first killing them, and
12418     ;; then removing them from the list of killed groups.
12419     (while bogus
12420       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12421            (progn
12422              (gnus-group-change-level entry gnus-level-killed)
12423              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12424       (setq bogus (cdr bogus)))
12425     ;; Then we remove all bogus groups from the list of killed and
12426     ;; zombie groups. They are are removed without confirmation.
12427     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12428           killed)
12429       (while dead-lists
12430         (setq killed (symbol-value (car dead-lists)))
12431         (while killed
12432           (setq group (car killed))
12433           (or (gnus-gethash group gnus-active-hashtb)
12434               ;; The group is bogus.
12435               (set (car dead-lists)
12436                    (delete group (symbol-value (car dead-lists)))))
12437           (setq killed (cdr killed)))
12438         (setq dead-lists (cdr dead-lists))))
12439     (gnus-message 5 "Checking bogus newsgroups...done")))
12440
12441 (defun gnus-check-duplicate-killed-groups ()
12442   "Remove duplicates from the list of killed groups."
12443   (interactive)
12444   (let ((killed gnus-killed-list))
12445     (while killed
12446       (gnus-message 9 "%d" (length killed))
12447       (setcdr killed (delete (car killed) (cdr killed)))
12448       (setq killed (cdr killed)))))
12449
12450 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12451 ;; and compute how many unread articles there are in each group.
12452 (defun gnus-get-unread-articles (&optional level) 
12453   (let* ((newsrc (cdr gnus-newsrc-alist))
12454          (level (or level (1+ gnus-level-subscribed)))
12455          (foreign-level
12456           (min 
12457            (cond ((and gnus-activate-foreign-newsgroups 
12458                        (not (numberp gnus-activate-foreign-newsgroups)))
12459                   (1+ gnus-level-subscribed))
12460                  ((numberp gnus-activate-foreign-newsgroups)
12461                   gnus-activate-foreign-newsgroups)
12462                  (t 0))
12463            level))
12464          info group active virtuals method)
12465     (gnus-message 5 "Checking new news...")
12466
12467     (while newsrc
12468       (setq info (car newsrc)
12469             group (car info)
12470             active (gnus-gethash group gnus-active-hashtb))
12471
12472       ;; Check newsgroups. If the user doesn't want to check them, or
12473       ;; they can't be checked (for instance, if the news server can't
12474       ;; be reached) we just set the number of unread articles in this
12475       ;; newsgroup to t. This means that Gnus thinks that there are
12476       ;; unread articles, but it has no idea how many.
12477       (if (and (setq method (nth 4 info))
12478                (not (gnus-server-equal gnus-select-method
12479                                        (gnus-server-get-method nil method)))
12480                (not (gnus-secondary-method-p method)))
12481           ;; These groups are foreign. Check the level.
12482           (if (<= (nth 1 info) foreign-level)
12483               (if (eq (car (if (stringp method) 
12484                                (gnus-server-to-method method)
12485                              (nth 4 info))) 'nnvirtual)
12486                   ;; We have to activate the virtual groups after all
12487                   ;; the others, so we just pop them on a list for
12488                   ;; now. 
12489                   (setq virtuals (cons info virtuals))
12490                 (and (setq active (gnus-activate-group (car info)))
12491                      ;; Close the groups as we look at them!
12492                      (gnus-close-group group))))
12493
12494         ;; These groups are native or secondary. 
12495         (if (and (not gnus-read-active-file)
12496                  (<= (nth 1 info) level))
12497             (progn
12498               (or gnus-read-active-file (gnus-check-server method))
12499               (setq active (gnus-activate-group (car info))))))
12500       
12501       (if active
12502           (gnus-get-unread-articles-in-group info active)
12503         ;; The group couldn't be reached, so we nix out the number of
12504         ;; unread articles and stuff.
12505         (gnus-sethash group nil gnus-active-hashtb)
12506         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12507
12508       (setq newsrc (cdr newsrc)))
12509
12510     ;; Activate the virtual groups. This has to be done after all the
12511     ;; other groups. 
12512     ;; !!! If one virtual group contains another virtual group, even
12513     ;; doing it this way might cause problems.
12514     (while virtuals
12515       (and (setq active (gnus-activate-group (car (car virtuals))))
12516            (gnus-get-unread-articles-in-group (car virtuals) active))
12517       (setq virtuals (cdr virtuals)))
12518
12519     (gnus-message 5 "Checking new news...done")))
12520
12521 ;; Create a hash table out of the newsrc alist. The `car's of the
12522 ;; alist elements are used as keys.
12523 (defun gnus-make-hashtable-from-newsrc-alist ()
12524   (let ((alist gnus-newsrc-alist)
12525         (ohashtb gnus-newsrc-hashtb)
12526         prev)
12527     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12528     (setq alist 
12529           (setq prev (setq gnus-newsrc-alist 
12530                            (if (equal (car (car gnus-newsrc-alist))
12531                                       "dummy.group")
12532                                gnus-newsrc-alist
12533                              (cons (list "dummy.group" 0 nil) alist)))))
12534     (while alist
12535       (gnus-sethash (car (car alist)) 
12536                     (cons (and ohashtb (car (gnus-gethash 
12537                                              (car (car alist)) ohashtb))) 
12538                           prev) gnus-newsrc-hashtb)
12539       (setq prev alist
12540             alist (cdr alist)))))
12541
12542 (defun gnus-make-hashtable-from-killed ()
12543   "Create a hash table from the killed and zombie lists."
12544   (let ((lists '(gnus-killed-list gnus-zombie-list))
12545         list)
12546     (setq gnus-killed-hashtb 
12547           (gnus-make-hashtable 
12548            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12549     (while lists
12550       (setq list (symbol-value (car lists)))
12551       (setq lists (cdr lists))
12552       (while list
12553         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12554         (setq list (cdr list))))))
12555
12556 (defun gnus-get-unread-articles-in-group (info active)
12557   (let* ((range (nth 2 info))
12558          (num 0)
12559          (marked (nth 3 info)))
12560     ;; If a cache is present, we may have to alter the active info.
12561     (and gnus-use-cache
12562          (gnus-cache-possibly-alter-active (car info) active))
12563     ;; Modify the list of read articles according to what articles 
12564     ;; are available; then tally the unread articles and add the
12565     ;; number to the group hash table entry.
12566     (cond 
12567      ((zerop (cdr active))
12568       (setq num 0))
12569      ((not range)
12570       (setq num (- (1+ (cdr active)) (car active))))
12571      ((not (listp (cdr range)))
12572       ;; Fix a single (num . num) range according to the
12573       ;; active hash table.
12574       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12575       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12576       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12577       ;; Compute number of unread articles.
12578       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12579      (t
12580       ;; The read list is a list of ranges. Fix them according to
12581       ;; the active hash table.
12582       ;; First peel off any elements that are below the lower
12583       ;; active limit. 
12584       (while (and (cdr range) 
12585                   (>= (car active) 
12586                       (or (and (atom (car (cdr range))) (car (cdr range)))
12587                           (car (car (cdr range))))))
12588         (if (numberp (car range))
12589             (setcar range 
12590                     (cons (car range) 
12591                           (or (and (numberp (car (cdr range)))
12592                                    (car (cdr range))) 
12593                               (cdr (car (cdr range))))))
12594           (setcdr (car range) 
12595                   (or (and (numberp (nth 1 range)) (nth 1 range))
12596                       (cdr (car (cdr range))))))
12597         (setcdr range (cdr (cdr range))))
12598       ;; Adjust the first element to be the same as the lower limit. 
12599       (if (and (not (atom (car range))) 
12600                (< (cdr (car range)) (car active)))
12601           (setcdr (car range) (1- (car active))))
12602       ;; Then we want to peel off any elements that are higher
12603       ;; than the upper active limit.  
12604       (let ((srange range))
12605         ;; Go past all legal elements.
12606         (while (and (cdr srange) 
12607                     (<= (or (and (atom (car (cdr srange)))
12608                                  (car (cdr srange)))
12609                             (car (car (cdr srange)))) (cdr active)))
12610           (setq srange (cdr srange)))
12611         (if (cdr srange)
12612             ;; Nuke all remaining illegal elements.
12613             (setcdr srange nil))
12614
12615         ;; Adjust the final element.
12616         (if (and (not (atom (car srange)))
12617                  (> (cdr (car srange)) (cdr active)))
12618             (setcdr (car srange) (cdr active))))
12619       ;; Compute the number of unread articles.
12620       (while range
12621         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12622                                     (cdr (car range))))
12623                             (or (and (atom (car range)) (car range))
12624                                 (car (car range))))))
12625         (setq range (cdr range)))
12626       (setq num (max 0 (- (cdr active) num)))))
12627     (and info
12628          (progn
12629            (and (assq 'tick marked)
12630                 (inline (gnus-remove-illegal-marked-articles
12631                          (assq 'tick marked) (nth 2 info))))
12632            (and (assq 'dormant marked)
12633                 (inline (gnus-remove-illegal-marked-articles
12634                          (assq 'dormant marked) (nth 2 info))))
12635            (setcar
12636             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12637             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12638                                 (length (cdr (assq 'dormant marked)))))))))
12639     num))
12640
12641 (defun gnus-remove-illegal-marked-articles (marked ranges)
12642   (let ((m (cdr marked)))
12643     ;; Make sure that all ticked articles are a subset of the unread
12644     ;; articles. 
12645     (while m
12646       (if (gnus-member-of-range (car m) ranges)
12647           (setcdr marked (cdr m))
12648         (setq marked m))
12649       (setq m (cdr m)))))
12650
12651 (defun gnus-activate-group (group)
12652   ;; Check whether a group has been activated or not.
12653   (let ((method (gnus-find-method-for-group group))
12654         active)
12655     (and (gnus-check-server method)
12656          ;; We escape all bugs and quit here to make it possible to
12657          ;; continue if a group is so out-there that it reports bugs
12658          ;; and stuff.
12659          (condition-case ()
12660              (gnus-request-group group)
12661            (error nil)
12662            (quit nil))
12663          (save-excursion
12664            (set-buffer nntp-server-buffer)
12665            (goto-char (point-min))
12666            ;; Parse the result we got from `gnus-request-group'.
12667            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12668                 (progn
12669                   (goto-char (match-beginning 1))
12670                   (gnus-sethash 
12671                    group (setq active (cons (read (current-buffer))
12672                                             (read (current-buffer))))
12673                    gnus-active-hashtb))
12674                 ;; Return the new active info.
12675                 active)))))
12676
12677 (defun gnus-update-read-articles 
12678   (group unread unselected ticked &optional domarks replied expirable killed
12679          dormant bookmark score)
12680   "Update the list of read and ticked articles in GROUP using the
12681 UNREAD and TICKED lists.
12682 Note: UNSELECTED has to be sorted over `<'.
12683 Returns whether the updating was successful."
12684   (let* ((active (or gnus-newsgroup-active 
12685                      (gnus-gethash group gnus-active-hashtb)))
12686          (entry (gnus-gethash group gnus-newsrc-hashtb))
12687          (info (nth 2 entry))
12688          (marked (nth 3 info))
12689          (prev 1)
12690          (unread (sort (copy-sequence unread) (function <)))
12691          read)
12692     (if (or (not info) (not active))
12693         ;; There is no info on this group if it was, in fact,
12694         ;; killed. Gnus stores no information on killed groups, so
12695         ;; there's nothing to be done. 
12696         ;; One could store the information somewhere temporarily,
12697         ;; perhaps... Hmmm... 
12698         ()
12699       ;; Remove any negative articles numbers.
12700       (while (and unread (< (car unread) 0))
12701         (setq unread (cdr unread)))
12702       ;; Remove any expired article numbers
12703       (while (and unread (< (car unread) (car active)))
12704         (setq unread (cdr unread)))
12705       (while (and ticked (< (car ticked) (car active)))
12706         (setq ticked (cdr ticked)))
12707       (while (and dormant (< (car dormant) (car active)))
12708         (setq dormant (cdr dormant)))
12709       (setq unread (sort (append unselected unread) '<))
12710       ;; Weed out duplicates.
12711       (let ((un unread))
12712         (while (cdr un)
12713           (if (eq (car un) (car (cdr un)))
12714               (setcdr un (cdr (cdr un)))
12715             (setq un (cdr un)))))
12716       ;; Compute the ranges of read articles by looking at the list of
12717       ;; unread articles.  
12718       (while unread
12719         (if (/= (car unread) prev)
12720             (setq read (cons (if (= prev (1- (car unread))) prev
12721                                (cons prev (1- (car unread)))) read)))
12722         (setq prev (1+ (car unread)))
12723         (setq unread (cdr unread)))
12724       (if (<= prev (cdr active))
12725           (setq read (cons (cons prev (cdr active)) read)))
12726       ;; Enter this list into the group info.
12727       (setcar (cdr (cdr info)) 
12728               (if (> (length read) 1) (nreverse read) read))
12729       ;; Enter the list of ticked articles.
12730       (gnus-set-marked-articles 
12731        info ticked
12732        (if domarks replied (cdr (assq 'reply marked)))
12733        (if domarks expirable (cdr (assq 'expire marked)))
12734        (if domarks killed (cdr (assq 'killed marked)))
12735        (if domarks dormant (cdr (assq 'dormant marked)))
12736        (if domarks bookmark (cdr (assq 'bookmark marked)))
12737        (if domarks score (cdr (assq 'score marked))))
12738       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12739       (gnus-get-unread-articles-in-group 
12740        info (gnus-gethash group gnus-active-hashtb))
12741       t)))
12742
12743 (defun gnus-make-articles-unread (group articles)
12744   "Mark ARTICLES in GROUP as unread."
12745   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12746                           (gnus-gethash (gnus-group-real-name group)
12747                                         gnus-newsrc-hashtb))))
12748          (ranges (nth 2 info))
12749          news)
12750     (while articles
12751       (and (gnus-member-of-range (car articles) ranges)
12752            (setq news (cons (car articles) news)))
12753       (setq articles (cdr articles)))
12754     (if (not news)
12755         ()
12756       (setcar (nthcdr 2 info)
12757               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12758       (gnus-group-update-group group t))))
12759
12760 ;; Enter all dead groups into the hashtb.
12761 (defun gnus-update-active-hashtb-from-killed ()
12762   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12763         (lists (list gnus-killed-list gnus-zombie-list))
12764         killed)
12765     (while lists
12766       (setq killed (car lists))
12767       (while killed
12768         (gnus-sethash (car killed) nil hashtb)
12769         (setq killed (cdr killed)))
12770       (setq lists (cdr lists)))))
12771
12772 ;; Get the active file(s) from the backend(s).
12773 (defun gnus-read-active-file ()
12774   (gnus-group-set-mode-line)
12775   (let ((methods (if (gnus-check-server gnus-select-method)
12776                      ;; The native server is available.
12777                      (cons gnus-select-method gnus-secondary-select-methods)
12778                    ;; The native server is down, so we just do the
12779                    ;; secondary ones.   
12780                    gnus-secondary-select-methods))
12781         list-type)
12782     (setq gnus-have-read-active-file nil)
12783     (save-excursion
12784       (set-buffer nntp-server-buffer)
12785       (while methods
12786         (let* ((method (gnus-server-get-method nil (car methods)))
12787                (where (nth 1 method))
12788                (mesg (format "Reading active file%s via %s..."
12789                              (if (and where (not (zerop (length where))))
12790                                  (concat " from " where) "")
12791                              (car method))))
12792           (gnus-message 5 mesg)
12793           (if (not (gnus-check-server method))
12794               ()
12795             (cond 
12796              ((and (eq gnus-read-active-file 'some)
12797                    (gnus-check-backend-function 'retrieve-groups (car method)))
12798               (let ((newsrc (cdr gnus-newsrc-alist))
12799                     (gmethod (gnus-server-get-method nil method))
12800                     groups)
12801                 (while newsrc
12802                   (and (gnus-server-equal 
12803                         (gnus-find-method-for-group 
12804                          (car (car newsrc)) (car newsrc))
12805                         gmethod)
12806                        (setq groups (cons (gnus-group-real-name 
12807                                            (car (car newsrc))) groups)))
12808                   (setq newsrc (cdr newsrc)))
12809                 (gnus-check-server method)
12810                 (setq list-type (gnus-retrieve-groups groups method))
12811                 (cond 
12812                  ((not list-type)
12813                   (gnus-message 
12814                    1 "Cannot read partial active file from %s server." 
12815                    (car method))
12816                   (ding)
12817                   (sit-for 2))
12818                  ((eq list-type 'active)
12819                   (gnus-active-to-gnus-format method gnus-active-hashtb))
12820                  (t
12821                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12822              (t
12823               (if (not (gnus-request-list method))
12824                   (progn
12825                     (gnus-message 1 "Cannot read active file from %s server." 
12826                                   (car method))
12827                     (ding))
12828                 (gnus-active-to-gnus-format method)
12829                 ;; We mark this active file as read.
12830                 (setq gnus-have-read-active-file
12831                       (cons method gnus-have-read-active-file))
12832                 (gnus-message 5 "%sdone" mesg))))))
12833         (setq methods (cdr methods))))))
12834
12835 ;; Read an active file and place the results in `gnus-active-hashtb'.
12836 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12837   (let ((cur (current-buffer))
12838         (hashtb (or hashtb 
12839                     (if (and gnus-active-hashtb 
12840                              (not (equal method gnus-select-method)))
12841                         gnus-active-hashtb
12842                       (setq gnus-active-hashtb
12843                             (if (equal method gnus-select-method)
12844                                 (gnus-make-hashtable 
12845                                  (count-lines (point-min) (point-max)))
12846                               (gnus-make-hashtable 4096))))))
12847         (flag-hashtb (gnus-make-hashtable 60)))
12848     ;; Delete unnecessary lines.
12849     (goto-char (point-min))
12850     (while (search-forward "\nto." nil t)
12851       (delete-region (1+ (match-beginning 0)) 
12852                      (progn (forward-line 1) (point))))
12853     (or (string= gnus-ignored-newsgroups "")
12854         (progn
12855           (goto-char (point-min))
12856           (delete-matching-lines gnus-ignored-newsgroups)))
12857     ;; Make the group names readable as a lisp expression even if they
12858     ;; contain special characters.
12859     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12860     (goto-char (point-max))
12861     (while (re-search-backward "[][';?()#]" nil t)
12862       (insert ?\\))
12863     ;; If these are groups from a foreign select method, we insert the
12864     ;; group prefix in front of the group names. 
12865     (and method (not (gnus-server-equal
12866                       (gnus-server-get-method nil method)
12867                       (gnus-server-get-method nil gnus-select-method)))
12868          (let ((prefix (gnus-group-prefixed-name "" method)))
12869            (goto-char (point-min))
12870            (while (and (not (eobp))
12871                        (progn (insert prefix)
12872                               (zerop (forward-line 1)))))))
12873     ;; Store the active file in a hash table.
12874     (goto-char (point-min))
12875     (if (string-match "%[oO]" gnus-group-line-format)
12876         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12877         ;; If we want information on moderated groups, we use this
12878         ;; loop...   
12879         (let* ((mod-hashtb (make-vector 7 0))
12880                (m (intern "m" mod-hashtb))
12881                group max min)
12882           (while (not (eobp))
12883             (condition-case nil
12884                 (progn
12885                   (narrow-to-region (point) (gnus-point-at-eol))
12886                   (setq group (let ((obarray hashtb)) (read cur)))
12887                   (if (and (numberp (setq max (read cur)))
12888                            (numberp (setq min (read cur)))
12889                            (progn 
12890                              (skip-chars-forward " \t")
12891                              (not
12892                               (or (= (following-char) ?=)
12893                                   (= (following-char) ?x)
12894                                   (= (following-char) ?j)))))
12895                       (set group (cons min max))
12896                     (set group nil))
12897                   ;; Enter moderated groups into a list.
12898                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12899                       (setq gnus-moderated-list 
12900                             (cons (symbol-name group) gnus-moderated-list))))
12901               (error 
12902                (and group
12903                     (symbolp group)
12904                     (set group nil))))
12905             (widen)
12906             (forward-line 1)))
12907       ;; And if we do not care about moderation, we use this loop,
12908       ;; which is faster.
12909       (let (group max min)
12910         (while (not (eobp))
12911           (condition-case ()
12912               (progn
12913                 (narrow-to-region (point) (gnus-point-at-eol))
12914                 ;; group gets set to a symbol interned in the hash table
12915                 ;; (what a hack!!) - jwz
12916                 (setq group (let ((obarray hashtb)) (read cur)))
12917                 (if (and (numberp (setq max (read cur)))
12918                          (numberp (setq min (read cur)))
12919                          (progn 
12920                            (skip-chars-forward " \t")
12921                            (not
12922                             (or (= (following-char) ?=)
12923                                 (= (following-char) ?x)
12924                                 (= (following-char) ?j)))))
12925                     (set group (cons min max))
12926                   (set group nil)))
12927             (error 
12928              (progn 
12929                (and group
12930                     (symbolp group)
12931                     (set group nil))
12932                (or ignore-errors
12933                    (gnus-message 3 "Warning - illegal active: %s"
12934                                  (buffer-substring 
12935                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
12936           (widen)
12937           (forward-line 1))))))
12938
12939 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12940   ;; Parse a "groups" active file.
12941   (let ((cur (current-buffer))
12942         (hashtb (or hashtb 
12943                     (if (and method gnus-active-hashtb)
12944                         gnus-active-hashtb
12945                       (setq gnus-active-hashtb
12946                             (gnus-make-hashtable 
12947                              (count-lines (point-min) (point-max)))))))
12948         (prefix (and method 
12949                      (not (gnus-server-equal
12950                            (gnus-server-get-method nil method)
12951                            (gnus-server-get-method nil gnus-select-method)))
12952                      (gnus-group-prefixed-name "" method))))
12953
12954     (goto-char (point-min))
12955     ;; We split this into to separate loops, one with the prefix
12956     ;; and one without to speed the reading up somewhat.
12957     (if prefix
12958         (let (min max opoint group)
12959           (while (not (eobp))
12960             (condition-case ()
12961                 (progn
12962                   (read cur) (read cur)
12963                   (setq min (read cur)
12964                         max (read cur)
12965                         opoint (point))
12966                   (skip-chars-forward " \t")
12967                   (insert prefix)
12968                   (goto-char opoint)
12969                   (set (let ((obarray hashtb)) (read cur)) 
12970                        (cons min max)))
12971               (error (and group (symbolp group) (set group nil))))
12972             (forward-line 1)))
12973       (let (min max group)
12974         (while (not (eobp))
12975           (condition-case ()
12976               (if (= (following-char) ?2)
12977                   (progn
12978                     (read cur) (read cur)
12979                     (setq min (read cur)
12980                           max (read cur))
12981                     (set (setq group (let ((obarray hashtb)) (read cur)))
12982                          (cons min max))))
12983             (error (and group (symbolp group) (set group nil))))
12984           (forward-line 1))))))
12985
12986 (defun gnus-read-newsrc-file (&optional force)
12987   "Read startup file.
12988 If FORCE is non-nil, the .newsrc file is read."
12989   ;; Reset variables that might be defined in the .newsrc.eld file.
12990   (let ((variables gnus-variable-list))
12991     (while variables
12992       (set (car variables) nil)
12993       (setq variables (cdr variables))))
12994   (let* ((newsrc-file gnus-current-startup-file)
12995          (quick-file (concat newsrc-file ".el")))
12996     (save-excursion
12997       ;; We always load the .newsrc.eld file. If always contains
12998       ;; much information that can not be gotten from the .newsrc
12999       ;; file (ticked articles, killed groups, foreign methods, etc.)
13000       (gnus-read-newsrc-el-file quick-file)
13001  
13002       (if (or force
13003               (and (file-newer-than-file-p newsrc-file quick-file)
13004                    (file-newer-than-file-p newsrc-file 
13005                                            (concat quick-file "d")))
13006               (not gnus-newsrc-alist))
13007           ;; We read the .newsrc file. Note that if there if a
13008           ;; .newsrc.eld file exists, it has already been read, and
13009           ;; the `gnus-newsrc-hashtb' has been created. While reading
13010           ;; the .newsrc file, Gnus will only use the information it
13011           ;; can find there for changing the data already read -
13012           ;; ie. reading the .newsrc file will not trash the data
13013           ;; already read (except for read articles).
13014           (save-excursion
13015             (gnus-message 5 "Reading %s..." newsrc-file)
13016             (set-buffer (find-file-noselect newsrc-file))
13017             (buffer-disable-undo (current-buffer))
13018             (gnus-newsrc-to-gnus-format)
13019             (kill-buffer (current-buffer))
13020             (gnus-message 5 "Reading %s...done" newsrc-file))))))
13021
13022 (defun gnus-read-newsrc-el-file (file)
13023   (let ((ding-file (concat file "d")))
13024     ;; We always, always read the .eld file.
13025     (gnus-message 5 "Reading %s..." ding-file)
13026     (let (gnus-newsrc-assoc)
13027       (condition-case nil
13028           (load ding-file t t t)
13029         (error nil))
13030       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
13031     (let ((inhibit-quit t))
13032       (gnus-uncompress-newsrc-alist))
13033     (gnus-make-hashtable-from-newsrc-alist)
13034     (if (not (file-newer-than-file-p file ding-file))
13035         ()
13036       ;; Old format quick file
13037       (gnus-message 5 "Reading %s..." file)
13038       ;; The .el file is newer than the .eld file, so we read that one
13039       ;; as well. 
13040       (gnus-read-old-newsrc-el-file file))))
13041
13042 ;; Parse the old-style quick startup file
13043 (defun gnus-read-old-newsrc-el-file (file)
13044   (let (newsrc killed marked group m)
13045     (prog1
13046         (let ((gnus-killed-assoc nil)
13047               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
13048           (prog1
13049               (condition-case nil
13050                   (load file t t t)
13051                 (error nil))
13052             (setq newsrc gnus-newsrc-assoc
13053                   killed gnus-killed-assoc
13054                   marked gnus-marked-assoc)))
13055       (setq gnus-newsrc-alist nil)
13056       (while newsrc
13057         (setq group (car newsrc))
13058         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13059           (if info
13060               (progn
13061                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13062                 (setcar (cdr info)
13063                         (if (nth 1 group) gnus-level-default-subscribed 
13064                           gnus-level-default-unsubscribed))
13065                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
13066             (setq gnus-newsrc-alist
13067                   (cons 
13068                    (setq info
13069                          (list (car group)
13070                                (if (nth 1 group) gnus-level-default-subscribed
13071                                  gnus-level-default-unsubscribed) 
13072                                (cdr (cdr group))))
13073                    gnus-newsrc-alist)))
13074           (if (setq m (assoc (car group) marked))
13075               (setcdr (cdr (cdr info))
13076                       (cons (list (cons 'tick (cdr m))) nil))))
13077         (setq newsrc (cdr newsrc)))
13078       (setq newsrc killed)
13079       (while newsrc
13080         (setcar newsrc (car (car newsrc)))
13081         (setq newsrc (cdr newsrc)))
13082       (setq gnus-killed-list killed))
13083     ;; The .el file version of this variable does not begin with
13084     ;; "options", while the .eld version does, so we just add it if it
13085     ;; isn't there.
13086     (and
13087      gnus-newsrc-options 
13088      (progn
13089        (and (not (string-match "^ *options" gnus-newsrc-options))
13090             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
13091        (and (not (string-match "\n$" gnus-newsrc-options))
13092             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
13093        ;; Finally, if we read some options lines, we parse them.
13094        (or (string= gnus-newsrc-options "")
13095            (gnus-newsrc-parse-options gnus-newsrc-options))))
13096
13097     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
13098     (gnus-make-hashtable-from-newsrc-alist)))
13099       
13100 (defun gnus-make-newsrc-file (file)
13101   "Make server dependent file name by catenating FILE and server host name."
13102   (let* ((file (expand-file-name file nil))
13103          (real-file (concat file "-" (nth 1 gnus-select-method))))
13104     (if (or (file-exists-p real-file)
13105             (file-exists-p (concat real-file ".el"))
13106             (file-exists-p (concat real-file ".eld")))
13107         real-file file)))
13108
13109 (defun gnus-uncompress-newsrc-alist ()
13110   ;; Uncompress all lists of marked articles in the newsrc assoc.
13111   (let ((newsrc gnus-newsrc-alist)
13112         marked)
13113     (while newsrc
13114       (if (not (setq marked (nth 3 (car newsrc))))
13115           ()
13116         (while marked
13117           (or (eq 'score (car (car marked)))
13118               (eq 'bookmark (car (car marked)))
13119               (eq 'killed (car (car marked)))
13120               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13121           (setq marked (cdr marked))))
13122       (setq newsrc (cdr newsrc)))))
13123
13124 (defun gnus-compress-newsrc-alist ()
13125   ;; Compress all lists of marked articles in the newsrc assoc.
13126   (let ((newsrc gnus-newsrc-alist)
13127         marked)
13128     (while newsrc
13129       (if (not (setq marked (nth 3 (car newsrc))))
13130           ()
13131         (while marked
13132           (or (eq 'score (car (car marked)))
13133               (eq 'bookmark (car (car marked)))
13134               (eq 'killed (car (car marked)))
13135               (setcdr (car marked) 
13136                       (condition-case ()
13137                           (gnus-compress-sequence 
13138                            (sort (cdr (car marked)) '<) t)
13139                         (error (cdr (car marked))))))
13140           (setq marked (cdr marked))))
13141       (setq newsrc (cdr newsrc)))))
13142
13143 (defun gnus-newsrc-to-gnus-format ()
13144   (setq gnus-newsrc-options "")
13145   (setq gnus-newsrc-options-n nil)
13146
13147   (or gnus-active-hashtb
13148       (setq gnus-active-hashtb (make-vector 4095 0)))
13149   (let ((buf (current-buffer))
13150         (already-read (> (length gnus-newsrc-alist) 1))
13151         group subscribed options-symbol newsrc Options-symbol
13152         symbol reads num1)
13153     (goto-char (point-min))
13154     ;; We intern the symbol `options' in the active hashtb so that we
13155     ;; can `eq' against it later.
13156     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
13157     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
13158   
13159     (while (not (eobp))
13160       ;; We first read the first word on the line by narrowing and
13161       ;; then reading into `gnus-active-hashtb'.  Most groups will
13162       ;; already exist in that hashtb, so this will save some string
13163       ;; space.
13164       (narrow-to-region
13165        (point)
13166        (progn (skip-chars-forward "^ \t!:\n") (point)))
13167       (goto-char (point-min))
13168       (setq symbol 
13169             (and (/= (point-min) (point-max))
13170                  (let ((obarray gnus-active-hashtb)) (read buf))))
13171       (widen)
13172       ;; Now, the symbol we have read is either `options' or a group
13173       ;; name.  If it is an options line, we just add it to a string. 
13174       (cond 
13175        ((or (eq symbol options-symbol)
13176             (eq symbol Options-symbol))
13177         (setq gnus-newsrc-options
13178               ;; This concatting is quite inefficient, but since our
13179               ;; thorough studies show that approx 99.37% of all
13180               ;; .newsrc files only contain a single options line, we
13181               ;; don't give a damn, frankly, my dear.
13182               (concat gnus-newsrc-options
13183                       (buffer-substring 
13184                        (gnus-point-at-bol)
13185                        ;; Options may continue on the next line.
13186                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13187                                 (progn (beginning-of-line) (point)))
13188                            (point)))))
13189         (forward-line -1))
13190        (symbol
13191         (or (boundp symbol) (set symbol nil))
13192         ;; It was a group name.
13193         (setq subscribed (= (following-char) ?:)
13194               group (symbol-name symbol)
13195               reads nil)
13196         (if (eolp)
13197             ;; If the line ends here, this is clearly a buggy line, so
13198             ;; we put point a the beginning of line and let the cond
13199             ;; below do the error handling.
13200             (beginning-of-line)
13201           ;; We skip to the beginning of the ranges.
13202           (skip-chars-forward "!: \t"))
13203         ;; We are now at the beginning of the list of read articles.
13204         ;; We read them range by range.
13205         (while
13206             (cond 
13207              ((looking-at "[0-9]+")
13208               ;; We narrow and read a number instead of buffer-substring/
13209               ;; string-to-int because it's faster. narrow/widen is
13210               ;; faster than save-restriction/narrow, and save-restriction
13211               ;; produces a garbage object.
13212               (setq num1 (progn
13213                            (narrow-to-region (match-beginning 0) (match-end 0))
13214                            (read buf)))
13215               (widen)
13216               ;; If the next character is a dash, then this is a range.
13217               (if (= (following-char) ?-)
13218                   (progn
13219                     ;; We read the upper bound of the range.
13220                     (forward-char 1)
13221                     (if (not (looking-at "[0-9]+"))
13222                         ;; This is a buggy line, by we pretend that
13223                         ;; it's kinda OK. Perhaps the user should be
13224                         ;; dinged? 
13225                         (setq reads (cons num1 reads))
13226                       (setq reads 
13227                             (cons 
13228                              (cons num1
13229                                    (progn
13230                                      (narrow-to-region (match-beginning 0) 
13231                                                        (match-end 0))
13232                                      (read buf)))
13233                              reads))
13234                       (widen)))
13235                 ;; It was just a simple number, so we add it to the
13236                 ;; list of ranges.
13237                 (setq reads (cons num1 reads)))
13238               ;; If the next char in ?\n, then we have reached the end
13239               ;; of the line and return nil.
13240               (/= (following-char) ?\n))
13241              ((= (following-char) ?\n)
13242               ;; End of line, so we end.
13243               nil)
13244              (t
13245               ;; Not numbers and not eol, so this might be a buggy
13246               ;; line... 
13247               (or (eobp)                
13248                   ;; If it was eob instead of ?\n, we allow it.
13249                   (progn
13250                     ;; The line was buggy.
13251                     (setq group nil)
13252                     (gnus-message 3 "Mangled line: %s" 
13253                                   (buffer-substring (gnus-point-at-bol) 
13254                                                     (gnus-point-at-eol)))
13255                     (ding)
13256                     (sit-for 1)))
13257               nil))
13258           ;; Skip past ", ". Spaces are illegal in these ranges, but
13259           ;; we allow them, because it's a common mistake to put a
13260           ;; space after the comma.
13261           (skip-chars-forward ", "))
13262
13263         ;; We have already read .newsrc.eld, so we gently update the
13264         ;; data in the hash table with the information we have just
13265         ;; read. 
13266         (if (not group)
13267             ()
13268           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13269                 level)
13270             (if info
13271                 ;; There is an entry for this file in the alist.
13272                 (progn
13273                   (setcar (nthcdr 2 info) (nreverse reads))
13274                   ;; We update the level very gently.  In fact, we
13275                   ;; only change it if there's been a status change
13276                   ;; from subscribed to unsubscribed, or vice versa.
13277                   (setq level (nth 1 info))
13278                   (cond ((and (<= level gnus-level-subscribed)
13279                               (not subscribed))
13280                          (setq level (if reads
13281                                          gnus-level-default-unsubscribed 
13282                                        (1+ gnus-level-default-unsubscribed))))
13283                         ((and (> level gnus-level-subscribed) subscribed)
13284                          (setq level gnus-level-default-subscribed)))
13285                   (setcar (cdr info) level))
13286               ;; This is a new group.
13287               (setq info (list group 
13288                                (if subscribed
13289                                    gnus-level-default-subscribed 
13290                                  (if reads
13291                                      (1+ gnus-level-subscribed)
13292                                    gnus-level-default-unsubscribed))
13293                                (nreverse reads))))
13294             (setq newsrc (cons info newsrc))))))
13295       (forward-line 1))
13296     
13297     (setq newsrc (nreverse newsrc))
13298
13299     (if (not already-read)
13300         ()
13301       ;; We now have two newsrc lists - `newsrc', which is what we
13302       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13303       ;; what we've read from .newsrc.eld. We have to merge these
13304       ;; lists. We do this by "attaching" any (foreign) groups in the
13305       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13306       (let ((rc (cdr gnus-newsrc-alist))
13307             (prev gnus-newsrc-alist)
13308             entry mentry)
13309         (while rc
13310           (or (null (nth 4 (car rc)))   ; It's a native group.
13311               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13312               (if (setq entry (assoc (car (car prev)) newsrc))
13313                   (setcdr (setq mentry (memq entry newsrc))
13314                           (cons (car rc) (cdr mentry)))
13315                 (setq newsrc (cons (car rc) newsrc))))
13316           (setq prev rc
13317                 rc (cdr rc)))))
13318
13319     (setq gnus-newsrc-alist newsrc)
13320     ;; We make the newsrc hashtb.
13321     (gnus-make-hashtable-from-newsrc-alist)
13322
13323     ;; Finally, if we read some options lines, we parse them.
13324     (or (string= gnus-newsrc-options "")
13325         (gnus-newsrc-parse-options gnus-newsrc-options))))
13326
13327 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13328 ;; The return value will be a list on the form
13329 ;; ((regexp1 . ignore)
13330 ;;  (regexp2 . subscribe)...)
13331 ;; When handling new newsgroups, groups that match a `ignore' regexp
13332 ;; will be ignored, and groups that match a `subscribe' regexp will be
13333 ;; subscribed. A line like
13334 ;; options -n !all rec.all
13335 ;; will lead to a list that looks like
13336 ;; (("^rec\\..+" . subscribe) 
13337 ;;  ("^.+" . ignore))
13338 ;; So all "rec.*" groups will be subscribed, while all the other
13339 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13340 ;; different from "options -n rec.all !all". 
13341 (defun gnus-newsrc-parse-options (options)
13342   (let (out eol)
13343     (save-excursion
13344       (gnus-set-work-buffer)
13345       (insert (regexp-quote options))
13346       ;; First we treat all continuation lines.
13347       (goto-char (point-min))
13348       (while (re-search-forward "\n[ \t]+" nil t)
13349         (replace-match " " t t))
13350       ;; Then we transform all "all"s into ".+"s.
13351       (goto-char (point-min))
13352       (while (re-search-forward "\\ball\\b" nil t)
13353         (replace-match ".+" t t))
13354       (goto-char (point-min))
13355       ;; We remove all other options than the "-n" ones.
13356       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13357         (replace-match " ")
13358         (forward-char -1))
13359       (goto-char (point-min))
13360
13361       ;; We are only interested in "options -n" lines - we
13362       ;; ignore the other option lines.
13363       (while (re-search-forward "[ \t]-n" nil t)
13364         (setq eol 
13365               (or (save-excursion
13366                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13367                          (- (point) 2)))
13368                   (gnus-point-at-eol)))
13369         ;; Search for all "words"...
13370         (while (re-search-forward "[^ \t,\n]+" eol t)
13371           (if (= (char-after (match-beginning 0)) ?!)
13372               ;; If the word begins with a bang (!), this is a "not"
13373               ;; spec. We put this spec (minus the bang) and the
13374               ;; symbol `ignore' into the list.
13375               (setq out (cons (cons (concat 
13376                                      "^" (buffer-substring 
13377                                           (1+ (match-beginning 0))
13378                                           (match-end 0)))
13379                                     'ignore) out))
13380             ;; There was no bang, so this is a "yes" spec.
13381             (setq out (cons (cons (concat 
13382                                    "^" (buffer-substring (match-beginning 0)
13383                                                          (match-end 0)))
13384                                   'subscribe) out)))))
13385     
13386       (setq gnus-newsrc-options-n out))))
13387
13388                
13389 (defun gnus-save-newsrc-file ()
13390   "Save .newsrc file."
13391   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13392   ;; from the variable gnus-newsrc-alist.
13393   (and (or gnus-newsrc-alist gnus-killed-list)
13394        gnus-current-startup-file
13395        (progn
13396          (run-hooks 'gnus-save-newsrc-hook)
13397          (save-excursion
13398            (if (and gnus-use-dribble-file
13399                     (or (not gnus-dribble-buffer)
13400                         (not (buffer-name gnus-dribble-buffer))
13401                         (zerop (save-excursion
13402                                  (set-buffer gnus-dribble-buffer)
13403                                  (buffer-size)))))
13404                (gnus-message 4 "(No changes need to be saved)")
13405              (if gnus-save-newsrc-file
13406                  (progn
13407                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13408                    ;; Make backup file of master newsrc.
13409                    (gnus-gnus-to-newsrc-format)
13410                    (gnus-message 5 "Saving %s...done"
13411                                  gnus-current-startup-file)))
13412              ;; Quickly loadable .newsrc.
13413              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13414              (make-local-variable 'version-control)
13415              (setq version-control 'never)
13416              (setq buffer-file-name (concat gnus-current-startup-file ".eld"))
13417              (gnus-add-current-to-buffer-list)
13418              (buffer-disable-undo (current-buffer))
13419              (erase-buffer)
13420              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13421              (gnus-gnus-to-quick-newsrc-format)
13422              (save-buffer)
13423              (kill-buffer (current-buffer))
13424              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13425              (gnus-dribble-delete-file))))))
13426
13427 (defun gnus-gnus-to-quick-newsrc-format ()
13428   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13429   (insert ";; Gnus startup file.\n")
13430   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13431   (insert ";; to read .newsrc.\n")
13432   (insert "(setq gnus-newsrc-file-version "
13433           (prin1-to-string gnus-version) ")\n")
13434   (let ((variables gnus-variable-list)
13435         (inhibit-quit t)
13436         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13437         variable)
13438     ;; insert lisp expressions.
13439     (gnus-compress-newsrc-alist)
13440     (while variables
13441       (setq variable (car variables))
13442       (and (boundp variable)
13443            (symbol-value variable)
13444            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13445            (insert "(setq " (symbol-name variable) " '"
13446                    (prin1-to-string (symbol-value variable))
13447                    ")\n"))
13448       (setq variables (cdr variables)))
13449     (gnus-uncompress-newsrc-alist)))
13450
13451
13452 (defun gnus-gnus-to-newsrc-format ()
13453   ;; Generate and save the .newsrc file.
13454   (let ((newsrc (cdr gnus-newsrc-alist))
13455         info ranges range)
13456     (save-excursion
13457       (set-buffer (create-file-buffer gnus-current-startup-file))
13458       (setq buffer-file-name gnus-current-startup-file)
13459       (buffer-disable-undo (current-buffer))
13460       (erase-buffer)
13461       ;; Write options.
13462       (if gnus-newsrc-options (insert gnus-newsrc-options))
13463       ;; Write subscribed and unsubscribed.
13464       (while newsrc
13465         (setq info (car newsrc))
13466         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13467             (progn
13468               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13469                                      "!" ":"))
13470               (if (setq ranges (nth 2 info))
13471                   (progn
13472                     (insert " ")
13473                     (if (not (listp (cdr ranges)))
13474                         (if (= (car ranges) (cdr ranges))
13475                             (insert (int-to-string (car ranges)))
13476                           (insert (int-to-string (car ranges)) "-" 
13477                                   (int-to-string (cdr ranges))))
13478                       (while ranges
13479                         (setq range (car ranges)
13480                               ranges (cdr ranges))
13481                         (if (or (atom range) (= (car range) (cdr range)))
13482                             (insert (int-to-string 
13483                                      (or (and (atom range) range) 
13484                                          (car range))))
13485                           (insert (int-to-string (car range)) "-"
13486                                   (int-to-string (cdr range))))
13487                         (if ranges (insert ","))))))
13488               (insert "\n")))
13489         (setq newsrc (cdr newsrc)))
13490       (make-local-variable 'version-control)
13491       (setq version-control 'never)
13492       ;; It has been reported that sometime the modtime on the .newsrc
13493       ;; file seems to be off. We really do want to overwrite it, so
13494       ;; we clear the modtime here before saving. It's a bit odd,
13495       ;; though... 
13496       ;; sometimes the modtime clear isn't sufficient.  most brute force:
13497       ;; delete the silly thing entirely first.  but this fails to provide
13498       ;; such niceties as .newsrc~ creation.
13499       (if gnus-modtime-botch
13500           (delete-file gnus-startup-file)
13501         (clear-visited-file-modtime))
13502       (save-buffer)
13503       (kill-buffer (current-buffer)))))
13504
13505 (defun gnus-read-all-descriptions-files ()
13506   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13507     (while methods
13508       (gnus-read-descriptions-file (car methods))
13509       (setq methods (cdr methods)))
13510     t))
13511
13512 (defun gnus-read-descriptions-file (&optional method)
13513   (let ((method (or method gnus-select-method)))
13514     ;; We create the hashtable whether we manage to read the desc file
13515     ;; to avoid trying to re-read after a failed read.
13516     (or gnus-description-hashtb
13517         (setq gnus-description-hashtb 
13518               (gnus-make-hashtable (length gnus-active-hashtb))))
13519     ;; Mark this method's desc file as read.
13520     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13521                   gnus-description-hashtb)
13522
13523     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13524     (cond 
13525      ((not (gnus-check-server method))
13526       (gnus-message 1 "Couldn't open server")
13527       nil)
13528      ((not (gnus-request-list-newsgroups method))
13529       (gnus-message 1 "Couldn't read newsgroups descriptions")
13530       nil)
13531      (t
13532       (let (group)
13533         (save-excursion
13534           (save-restriction
13535             (set-buffer nntp-server-buffer)
13536             (goto-char (point-min))
13537             (if (or (search-forward "\n.\n" nil t)
13538                     (goto-char (point-max)))
13539                 (progn
13540                   (beginning-of-line)
13541                   (narrow-to-region (point-min) (point))))
13542             (goto-char (point-min))
13543             (while (not (eobp))
13544               ;; If we get an error, we set group to 0, which is not a
13545               ;; symbol... 
13546               (setq group 
13547                     (condition-case ()
13548                         (let ((obarray gnus-description-hashtb))
13549                           ;; Group is set to a symbol interned in this
13550                           ;; hash table.
13551                           (read nntp-server-buffer))
13552                       (error 0)))
13553               (skip-chars-forward " \t")
13554               ;; ... which leads to this line being effectively ignored.
13555               (and (symbolp group)
13556                    (set group (buffer-substring 
13557                                (point) (progn (end-of-line) (point)))))
13558               (forward-line 1))))
13559         (gnus-message 5 "Reading descriptions file...done")
13560         t)))))
13561
13562 (defun gnus-group-get-description (group)
13563   ;; Get the description of a group by sending XGTITLE to the server.
13564   (and (gnus-request-group-description group)
13565        (save-excursion
13566          (set-buffer nntp-server-buffer)
13567          (goto-char (point-min))
13568          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13569               (buffer-substring (match-beginning 1) (match-end 1))))))
13570
13571 ;;;
13572 ;;; Server
13573 ;;;
13574
13575 (defvar gnus-server-mode-hook nil
13576   "Hook run in `gnus-server-mode' buffers.")
13577
13578 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13579   "Format of server lines.
13580 It works along the same lines as a normal formatting string,
13581 with some simple extensions.")
13582
13583 (defvar gnus-server-mode-line-format "Gnus  List of servers"
13584   "The format specification for the server mode line.")
13585
13586 (defconst gnus-server-line-format-alist
13587   (list (list ?h 'how ?s)
13588         (list ?n 'name ?s)
13589         (list ?w 'where ?s)
13590         ))
13591
13592 (defconst gnus-server-mode-line-format-alist 
13593   (list (list ?S 'news-server ?s)
13594         (list ?M 'news-method ?s)
13595         (list ?u 'user-defined ?s)))
13596
13597 (defvar gnus-server-line-format-spec nil)
13598 (defvar gnus-server-mode-line-format-spec nil)
13599 (defvar gnus-server-killed-servers nil)
13600
13601 (defvar gnus-server-mode-map nil)
13602 (put 'gnus-server-mode 'mode-class 'special)
13603
13604 (if gnus-server-mode-map
13605     nil
13606   (setq gnus-server-mode-map (make-sparse-keymap))
13607   (suppress-keymap gnus-server-mode-map)
13608   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13609   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13610   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13611   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13612   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13613   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13614   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13615   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13616   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13617   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13618
13619 (defun gnus-server-mode ()
13620   "Major mode for listing and editing servers.
13621
13622 All normal editing commands are switched off.
13623 \\<gnus-server-mode-map>
13624
13625 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13626
13627 The following commands are available:
13628
13629 \\{gnus-server-mode-map}"
13630   (interactive)
13631   (if gnus-visual (gnus-server-make-menu-bar))
13632   (kill-all-local-variables)
13633   (gnus-simplify-mode-line)
13634   (setq major-mode 'gnus-server-mode)
13635   (setq mode-name "Server")
13636                                         ;  (gnus-group-set-mode-line)
13637   (setq mode-line-process nil)
13638   (use-local-map gnus-server-mode-map)
13639   (buffer-disable-undo (current-buffer))
13640   (setq truncate-lines t)
13641   (setq buffer-read-only t)
13642   (run-hooks 'gnus-server-mode-hook))
13643
13644 (defun gnus-server-insert-server-line (sformat name method)
13645   (let* ((sformat (or sformat gnus-server-line-format-spec))
13646          (how (car method))
13647          (where (nth 1 method))
13648          b)
13649     (beginning-of-line)
13650     (setq b (point))
13651     ;; Insert the text.
13652     (insert (eval sformat))
13653     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13654
13655 (defun gnus-server-setup-buffer ()
13656   (if (get-buffer gnus-server-buffer)
13657       ()
13658     (save-excursion
13659       (set-buffer (get-buffer-create gnus-server-buffer))
13660       (gnus-server-mode)
13661       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13662
13663 (defun gnus-server-prepare ()
13664   (setq gnus-server-mode-line-format-spec 
13665         (gnus-parse-format gnus-server-mode-line-format 
13666                            gnus-server-mode-line-format-alist))
13667   (setq gnus-server-line-format-spec 
13668         (gnus-parse-format gnus-server-line-format 
13669                            gnus-server-line-format-alist))
13670   (let ((alist gnus-server-alist)
13671         (buffer-read-only nil))
13672     (erase-buffer)
13673     (while alist
13674       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13675       (setq alist (cdr alist))))
13676   (goto-char (point-min))
13677   (gnus-server-position-cursor))
13678
13679 (defun gnus-server-server-name ()
13680   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13681     (and server (symbol-name server))))
13682
13683 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13684
13685 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13686
13687 (defun gnus-server-update-server (server)
13688   (save-excursion
13689     (set-buffer gnus-server-buffer)
13690     (let ((buffer-read-only nil)
13691           (info (cdr (assoc server gnus-server-alist))))
13692       (gnus-dribble-enter 
13693        (concat "(gnus-server-set-info \"" server "\" '"
13694                (prin1-to-string info) ")"))
13695       ;; Buffer may be narrowed.
13696       (save-restriction
13697         (widen)
13698         (if (gnus-server-goto-server server)
13699             (delete-region (progn (beginning-of-line) (point))
13700                            (progn (forward-line 1) (point))))
13701         (let ((entry (assoc server gnus-server-alist)))
13702           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13703           (gnus-server-position-cursor))))))
13704
13705 (defun gnus-server-set-info (server info)
13706   ;; Enter a select method into the virtual server alist.
13707   (gnus-dribble-enter 
13708    (concat "(gnus-server-set-info \"" server "\" '"
13709            (prin1-to-string info) ")"))
13710   (let* ((server (nth 1 info))
13711          (entry (assoc server gnus-server-alist)))
13712     (if entry (setcdr entry info)
13713       (setq gnus-server-alist
13714             (nconc gnus-server-alist (list (cons server info)))))))
13715
13716 (defun gnus-server-to-method (server)
13717   ;; Map virtual server names to select methods.
13718   (or (and (equal server "native") gnus-select-method)
13719       (cdr (assoc server gnus-server-alist))))
13720
13721 (defun gnus-server-extend-method (group method)
13722   ;; This function "extends" a virtual server.  If the server is
13723   ;; "hello", and the select method is ("hello" (my-var "something")) 
13724   ;; in the group "alt.alt", this will result in a new virtual server
13725   ;; called "helly+alt.alt".
13726   (let ((entry
13727          (gnus-copy-sequence 
13728           (if (equal (car method) "native") gnus-select-method
13729             (cdr (assoc (car method) gnus-server-alist))))))
13730     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13731     (nconc entry (cdr method))))
13732
13733 (defun gnus-server-get-method (group method)
13734   ;; Input either a server name, and extended server name, or a
13735   ;; select method, and return a select method. 
13736   (cond ((stringp method)
13737          (gnus-server-to-method method))
13738         ((and (stringp (car method)) group)
13739          (gnus-server-extend-method group method))
13740         (t
13741          (gnus-server-add-address method))))
13742
13743 (defun gnus-server-add-address (method)
13744   (let ((method-name (symbol-name (car method))))
13745     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13746              (not (assq (intern (concat method-name "-address")) method)))
13747         (append method (list (list (intern (concat method-name "-address"))
13748                                    (nth 1 method))))
13749       method)))
13750
13751 (defun gnus-server-equal (s1 s2)
13752   (or (equal s1 s2)
13753       (and (= (length s1) (length s2))
13754            (progn
13755              (while (and s1 (member (car s1) s2))
13756                (setq s1 (cdr s1)))
13757              (null s1)))))
13758
13759 ;;; Interactive server functions.
13760
13761 (defun gnus-server-kill-server (server)
13762   "Kill the server on the current line."
13763   (interactive (list (gnus-server-server-name)))
13764   (or (gnus-server-goto-server server)
13765       (if server (error "No such server: %s" server)
13766         (error "No server on the current line")))
13767   (gnus-dribble-enter "")
13768   (let ((buffer-read-only nil))
13769     (delete-region (progn (beginning-of-line) (point))
13770                    (progn (forward-line 1) (point))))
13771   (setq gnus-server-killed-servers 
13772         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13773   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13774                                 gnus-server-alist))
13775   (gnus-server-position-cursor))
13776
13777 (defun gnus-server-yank-server ()
13778   "Yank the previously killed server."
13779   (interactive)
13780   (or gnus-server-killed-servers
13781       (error "No killed servers to be yanked"))
13782   (let ((alist gnus-server-alist)
13783         (server (gnus-server-server-name))
13784         (killed (car gnus-server-killed-servers)))
13785     (if (not server) 
13786         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13787       (if (string= server (car (car gnus-server-alist)))
13788           (setq gnus-server-alist (cons killed gnus-server-alist))
13789         (while (and (cdr alist)
13790                     (not (string= server (car (car (cdr alist))))))
13791           (setq alist (cdr alist)))
13792         (setcdr alist (cons killed (cdr alist)))))
13793     (gnus-server-update-server (car killed))
13794     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13795     (gnus-server-position-cursor)))
13796
13797 (defun gnus-server-exit ()
13798   "Return to the group buffer."
13799   (interactive)
13800   (kill-buffer (current-buffer))
13801   (switch-to-buffer gnus-group-buffer))
13802
13803 (defun gnus-server-list-servers ()
13804   "List all available servers."
13805   (interactive)
13806   (let ((cur (gnus-server-server-name)))
13807     (gnus-server-prepare)
13808     (if cur (gnus-server-goto-server cur)
13809       (goto-char (point-max))
13810       (forward-line -1))
13811     (gnus-server-position-cursor)))
13812
13813 (defun gnus-server-copy-server (from to)
13814   (interactive
13815    (list
13816     (or (gnus-server-server-name)
13817         (error "No server on the current line"))
13818     (read-string "Copy to: ")))
13819   (or from (error "No server on current line"))
13820   (or (and to (not (string= to ""))) (error "No name to copy to"))
13821   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13822   (or (assoc from gnus-server-alist) 
13823       (error "%s: no such server" from))
13824   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13825     (setcar to-entry to)
13826     (setcar (nthcdr 2 to-entry) to)
13827     (setq gnus-server-killed-servers 
13828           (cons to-entry gnus-server-killed-servers))
13829     (gnus-server-yank-server)))
13830
13831 (defun gnus-server-add-server (how where)
13832   (interactive 
13833    (list (intern (completing-read "Server method: "
13834                                   gnus-valid-select-methods nil t))
13835          (read-string "Server name: ")))
13836   (setq gnus-server-killed-servers 
13837         (cons (list where how where) gnus-server-killed-servers))
13838   (gnus-server-yank-server))
13839
13840 (defun gnus-server-goto-server (server)
13841   "Jump to a server line."
13842   (interactive
13843    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13844   (let ((to (text-property-any (point-min) (point-max) 
13845                                'gnus-server (intern server))))
13846     (and to
13847          (progn
13848            (goto-char to) 
13849            (gnus-server-position-cursor)))))
13850
13851 (defun gnus-server-edit-server (server)
13852   "Edit the server on the current line."
13853   (interactive (list (gnus-server-server-name)))
13854   (or server
13855       (error "No server on current line"))
13856   (let ((winconf (current-window-configuration)))
13857     (get-buffer-create gnus-server-edit-buffer)
13858     (gnus-configure-windows 'edit-server)
13859     (gnus-add-current-to-buffer-list)
13860     (emacs-lisp-mode)
13861     (make-local-variable 'gnus-prev-winconf)
13862     (setq gnus-prev-winconf winconf)
13863     (use-local-map (copy-keymap (current-local-map)))
13864     (let ((done-func '(lambda () 
13865                         "Exit editing mode and update the information."
13866                         (interactive)
13867                         (gnus-server-edit-server-done 'group))))
13868       (setcar (cdr (nth 4 done-func)) server)
13869       (local-set-key "\C-c\C-c" done-func))
13870     (erase-buffer)
13871     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13872     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13873
13874 (defun gnus-server-edit-server-done (server)
13875   (interactive)
13876   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13877   (goto-char (point-min))
13878   (let ((form (read (current-buffer)))
13879         (winconf gnus-prev-winconf))
13880     (gnus-server-set-info server form)
13881     (kill-buffer (current-buffer))
13882     (and winconf (set-window-configuration winconf))
13883     (set-buffer gnus-server-buffer)
13884     (gnus-server-update-server (gnus-server-server-name))
13885     (gnus-server-list-servers)
13886     (gnus-server-position-cursor)))
13887
13888 (defun gnus-server-read-server (server)
13889   "Browse a server."
13890   (interactive (list (gnus-server-server-name)))
13891   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13892
13893 (defun gnus-mouse-pick-server (e)
13894   (interactive "e")
13895   (mouse-set-point e)
13896   (gnus-server-read-server (gnus-server-server-name)))
13897
13898 ;;;
13899 ;;; entry points into gnus-score.el
13900 ;;;
13901
13902 ;;; Finding score files. 
13903
13904 (defvar gnus-global-score-files nil
13905   "*List of global score files and directories.
13906 Set this variable if you want to use people's score files.  One entry
13907 for each score file or each score file directory.  Gnus will decide
13908 by itself what score files are applicable to which group.
13909
13910 Say you want to use the single score file
13911 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13912 score files in the \"/ftp.some-where:/pub/score\" directory.
13913
13914  (setq gnus-global-score-files
13915        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13916          \"/ftp.some-where:/pub/score\"))")
13917
13918 (defun gnus-score-score-files (group)
13919   "Return a list of all possible score files."
13920   ;; Search and set any global score files.
13921   (and gnus-global-score-files 
13922        (or gnus-internal-global-score-files
13923            (gnus-score-search-global-directories gnus-global-score-files)))
13924   ;; Fix the kill-file dir variable.
13925   (setq gnus-kill-files-directory 
13926         (file-name-as-directory
13927          (or gnus-kill-files-directory "~/News/")))
13928   ;; If we can't read it, there are no score files.
13929   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13930       (setq gnus-score-file-list nil)
13931     (if (gnus-use-long-file-name 'not-score)
13932         ;; We want long file names.
13933         (if (or (not gnus-score-file-list)
13934                 (not (car gnus-score-file-list))
13935                 (gnus-file-newer-than gnus-kill-files-directory
13936                                       (car gnus-score-file-list)))
13937             (setq gnus-score-file-list 
13938                   (cons (nth 5 (file-attributes gnus-kill-files-directory))
13939                         (nreverse 
13940                          (directory-files 
13941                           gnus-kill-files-directory t 
13942                           (gnus-score-file-regexp))))))
13943       ;; We do not use long file names, so we have to do some
13944       ;; directory traversing.  
13945       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13946             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13947             dir files suffix)
13948         (while suffixes
13949           (setq dir (expand-file-name
13950                      (concat gnus-kill-files-directory
13951                              (gnus-replace-chars-in-string group ?. ?/))))
13952           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13953           (setq suffix (car suffixes)
13954                 suffixes (cdr suffixes))
13955           (if (file-exists-p (concat dir "/" suffix))
13956               (setq files (cons (concat dir "/" suffix) files)))
13957           (while (>= (1+ (length dir)) mdir)
13958             (and (file-exists-p (concat dir "/all/" suffix))
13959                  (setq files (cons (concat dir "/all/" suffix) files)))
13960             (string-match "/[^/]*$" dir)
13961             (setq dir (substring dir 0 (match-beginning 0)))))
13962         (setq gnus-score-file-list 
13963               (cons nil (nreverse files)))))
13964     (cdr gnus-score-file-list)))
13965
13966 (defun gnus-score-file-regexp ()
13967   (concat "\\(" gnus-score-file-suffix 
13968           "\\|" gnus-adaptive-file-suffix "\\)$"))
13969         
13970 (defun gnus-score-find-bnews (group)
13971   "Return a list of score files for GROUP.
13972 The score files are those files in the ~/News directory which matches
13973 GROUP using BNews sys file syntax."
13974   (let* ((sfiles (append (gnus-score-score-files group)
13975                          gnus-internal-global-score-files))
13976          (kill-dir (file-name-as-directory 
13977                     (expand-file-name gnus-kill-files-directory)))
13978          (klen (length kill-dir))
13979          ofiles not-match regexp)
13980     (save-excursion
13981       (set-buffer (get-buffer-create "*gnus score files*"))
13982       (buffer-disable-undo (current-buffer))
13983       ;; Go through all score file names and create regexp with them
13984       ;; as the source.  
13985       (while sfiles
13986         (erase-buffer)
13987         (insert (car sfiles))
13988         (goto-char (point-min))
13989         ;; First remove the suffix itself.
13990         (re-search-forward (concat "." (gnus-score-file-regexp)))
13991         (replace-match "" t t) 
13992         (goto-char (point-min))
13993         (if (looking-at (regexp-quote kill-dir))
13994             ;; If the file name was just "SCORE", `klen' is one character
13995             ;; too much.
13996             (delete-char (min (1- (point-max)) klen))
13997           (goto-char (point-max))
13998           (search-backward "/")
13999           (delete-region (1+ (point)) (point-min)))
14000         ;; If short file names were used, we have to translate slashes.
14001         (goto-char (point-min))
14002         (while (re-search-forward "[/:]" nil t)
14003           (replace-match "." t t))
14004         ;; Cludge to get rid of "nntp+" problems.
14005         (goto-char (point-min))
14006         (and (looking-at "nn[a-z]+\\+")
14007              (progn
14008                (search-forward "+")
14009                (forward-char -1)
14010                (insert "\\")))
14011         ;; Translate ".all" to "[./].*";
14012         (while (search-forward ".all" nil t)
14013           (replace-match "[./:].*" t t))
14014         (goto-char (point-min))
14015         ;; Translate "all" to ".*".
14016         (while (search-forward "all" nil t)
14017           (replace-match ".*" t t))
14018         (goto-char (point-min))
14019         ;; Deal with "not."s.
14020         (if (looking-at "not.")
14021             (progn
14022               (setq not-match t)
14023               (setq regexp (buffer-substring 5 (point-max))))
14024           (setq regexp (buffer-substring 1 (point-max)))
14025           (setq not-match nil))
14026         ;; Finally - if this resulting regexp matches the group name,
14027         ;; we add this score file to the list of score files
14028         ;; applicable to this group.
14029         (if (or (and not-match
14030                      (not (string-match regexp group)))
14031                 (and (not not-match)
14032                      (string-match regexp group)))
14033             (setq ofiles (cons (car sfiles) ofiles)))
14034         (setq sfiles (cdr sfiles)))
14035       (kill-buffer (current-buffer))
14036       ;; Slight kludge here - the last score file returned should be
14037       ;; the local score file, whether it exists or not. This is so
14038       ;; that any score commands the user enters will go to the right
14039       ;; file, and not end up in some global score file.
14040       (let ((localscore
14041              (expand-file-name
14042               (if (gnus-use-long-file-name 'not-score)
14043                   (concat gnus-kill-files-directory group "." 
14044                           gnus-score-file-suffix)
14045                 (concat gnus-kill-files-directory
14046                         (gnus-replace-chars-in-string group ?. ?/ ?: ?/)
14047                         "/" gnus-score-file-suffix)))))
14048         ;; The localest score file might already be there, but it's
14049         ;; supposed to be the very last file, so we delete it from the
14050         ;; list if it's already there, and add it to the head of the
14051         ;; list. 
14052         (setq ofiles (cons localscore (delete localscore ofiles))))
14053       (nreverse ofiles))))
14054
14055 (defun gnus-score-find-single (group)
14056   "Return list containing the score file for GROUP."
14057   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
14058         (gnus-score-file-name group)))
14059
14060 (defun gnus-score-find-hierarchical (group)
14061   "Return list of score files for GROUP.
14062 This includes the score file for the group and all its parents."
14063   (let ((all (copy-sequence '(nil)))
14064         (start 0))
14065     (while (string-match "\\." group (1+ start))
14066       (setq start (match-beginning 0))
14067       (setq all (cons (substring group 0 start) all)))
14068     (setq all (cons group all))
14069     (nconc
14070      (mapcar (lambda (newsgroup)
14071                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
14072              (setq all (nreverse all)))
14073      (mapcar 'gnus-score-file-name all))))
14074
14075 (defvar gnus-score-file-alist-cache nil)
14076
14077 (defun gnus-score-find-alist (group)
14078   "Return list of score files for GROUP.
14079 The list is determined from the variable gnus-score-file-alist."
14080   (let ((alist gnus-score-file-multiple-match-alist)
14081         score-files)
14082     ;; if this group has been seen before, return the cached entry
14083     (if (setq score-files (assoc group gnus-score-file-alist-cache))
14084         (cdr score-files)               ;ensures caching groups with no matches
14085       ;; handle the multiple match alist
14086       (while alist
14087         (and (string-match (car (car alist)) group)
14088              (setq score-files
14089                    (nconc score-files (copy-sequence (cdr (car alist))))))
14090         (setq alist (cdr alist)))
14091       (setq alist gnus-score-file-single-match-alist)
14092       ;; handle the single match alist
14093       (while alist
14094         (and (string-match (car (car alist)) group)
14095              ;; progn used just in case ("regexp") has no files
14096              ;; and score-files is still nil. -sj
14097              ;; this can be construed as a "stop searching here" feature :>
14098              ;; and used to simplify regexps in the single-alist 
14099              (progn
14100                (setq score-files
14101                      (nconc score-files (copy-sequence (cdr (car alist)))))
14102                (setq alist nil)))
14103         (setq alist (cdr alist)))
14104       ;; cache the score files
14105       (setq gnus-score-file-alist-cache
14106             (cons (cons group score-files) gnus-score-file-alist-cache))
14107       score-files)))
14108
14109
14110 (defun gnus-possibly-score-headers (&optional trace)
14111   (let ((func gnus-score-find-score-files-function)
14112         score-files)
14113     (and func (not (listp func))
14114          (setq func (list func)))
14115     ;; Go through all the functions for finding score files (or actual
14116     ;; scores) and add them to a list.
14117     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
14118     (while func
14119       (and (symbolp (car func))
14120            (fboundp (car func))
14121            (setq score-files 
14122                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
14123       (setq func (cdr func)))
14124     (if score-files (gnus-score-headers score-files trace))))
14125
14126 (defun gnus-score-file-name (newsgroup &optional suffix)
14127   "Return the name of a score file for NEWSGROUP."
14128   (let ((suffix (or suffix gnus-score-file-suffix)))
14129     (cond 
14130      ((or (null newsgroup)
14131           (string-equal newsgroup ""))
14132       ;; The global score file is placed at top of the directory.
14133       (expand-file-name 
14134        suffix (or gnus-kill-files-directory "~/News")))
14135      ((gnus-use-long-file-name 'not-score)
14136       ;; Append ".SCORE" to newsgroup name.
14137       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14138                                 "." suffix)
14139                         (or gnus-kill-files-directory "~/News")))
14140      (t
14141       ;; Place "SCORE" under the hierarchical directory.
14142       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14143                                 "/" suffix)
14144                         (or gnus-kill-files-directory "~/News"))))))
14145
14146 (defun gnus-score-search-global-directories (files)
14147   "Scan all global score directories for score files."
14148   ;; Set the variable `gnus-internal-global-score-files' to all
14149   ;; available global score files.
14150   (interactive (list gnus-global-score-files))
14151   (let (out)
14152     (while files
14153       (if (string-match "/$" (car files))
14154           (setq out (nconc (directory-files 
14155                             (car files) t
14156                             (concat (gnus-score-file-regexp) "$"))))
14157         (setq out (cons (car files) out)))
14158       (setq files (cdr files)))
14159     (setq gnus-internal-global-score-files out)))
14160
14161 ;; Allow redefinition of Gnus functions.
14162
14163 (gnus-ems-redefine)
14164
14165 (provide 'gnus)
14166
14167 ;;; gnus.el ends here