850916cd8923eb1abaf111f59d8f1269b3291817
[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   \"R\" if this article has been replied to, \" \" otherwise (character)
937 %U   Status of this article (character, \"D\", \"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  %G/%A %Z"
977   "*The format specification for the summary mode line.")
978
979 (defvar gnus-article-mode-line-format "Gnus  %G/%A %S"
980   "*The format specification for the article mode line.")
981
982 (defvar gnus-group-mode-line-format "Gnus  List of groups   {%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 21
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 (defvar gnus-exit-group-hook nil
1198   "*A hook called when exiting (not quitting) summary mode.")
1199
1200 (defvar gnus-suspend-gnus-hook nil
1201   "*A hook called when suspending (not exiting) Gnus.")
1202
1203 (defvar gnus-exit-gnus-hook nil
1204   "*A hook called when exiting Gnus.")
1205
1206 (defvar gnus-save-newsrc-hook nil
1207   "*A hook called when saving the newsrc file.")
1208
1209 (defvar gnus-summary-update-hook 
1210   (list 'gnus-summary-highlight-line)
1211   "*A hook called when a summary line is changed.
1212 The hook will not be called if `gnus-visual' is nil.
1213
1214 The default function `gnus-summary-highlight-line' will
1215 highlight the line according to the `gnus-summary-highlight'
1216 variable.")
1217
1218 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1219   "*A hook called when an article is selected for the first time.
1220 The hook is intended to mark an article as read (or unread)
1221 automatically when it is selected.")
1222
1223 ;; Remove any hilit infestation.
1224 (add-hook 'gnus-startup-hook
1225           (lambda ()
1226             (remove-hook 'gnus-summary-prepare-hook
1227                          'hilit-rehighlight-buffer-quietly)
1228             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1229             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1230             (remove-hook 'gnus-article-prepare-hook
1231                          'hilit-rehighlight-buffer-quietly)))
1232
1233
1234 \f
1235 ;; Internal variables
1236
1237 ;; Avoid highlighting in kill files.
1238 (defvar gnus-summary-inhibit-highlight nil)
1239 (defvar gnus-newsgroup-selected-overlay nil)
1240
1241 (defvar gnus-article-mode-map nil)
1242 (defvar gnus-dribble-buffer nil)
1243 (defvar gnus-headers-retrieved-by nil)
1244 (defvar gnus-article-reply nil)
1245 (defvar gnus-override-method nil)
1246 (defvar gnus-article-check-size nil)
1247
1248 (defvar gnus-current-score-file nil)
1249 (defvar gnus-internal-global-score-files nil)
1250 (defvar gnus-score-file-list nil)
1251 (defvar gnus-scores-exclude-files nil)
1252
1253 (defvar gnus-current-move-group nil)
1254
1255 (defvar gnus-newsgroup-dependencies nil)
1256 (defvar gnus-newsgroup-threads nil)
1257 (defvar gnus-newsgroup-async nil)
1258 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1259
1260 (defvar gnus-newsgroup-adaptive nil)
1261
1262 (defvar gnus-summary-display-table nil)
1263
1264 (defconst gnus-group-line-format-alist
1265   (list (list ?M 'marked ?c)
1266         (list ?S 'subscribed ?c)
1267         (list ?L 'level ?d)
1268         (list ?N 'number ?s)
1269         (list ?I 'number-of-dormant ?d)
1270         (list ?T 'number-of-ticked ?d)
1271         (list ?R 'number-of-read ?s)
1272         (list ?t 'number-total ?d)
1273         (list ?y 'number-of-unread-unticked ?s)
1274         (list ?i 'number-of-ticked-and-dormant ?d)
1275         (list ?g 'group ?s)
1276         (list ?G 'qualified-group ?s)
1277         (list ?D 'newsgroup-description ?s)
1278         (list ?o 'moderated ?c)
1279         (list ?O 'moderated-string ?s)
1280         (list ?p 'process-marked ?c)
1281         (list ?s 'news-server ?s)
1282         (list ?n 'news-method ?s)
1283         (list ?z 'news-method-string ?s)
1284         (list ?u 'user-defined ?s)))
1285
1286 (defconst gnus-summary-line-format-alist 
1287   (list (list ?N 'number ?d)
1288         (list ?S 'subject ?s)
1289         (list ?s 'subject-or-nil ?s)
1290         (list ?n 'name ?s)
1291         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1292               ?s)
1293         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1294                       from) ?s)
1295         (list ?F 'from ?s)
1296         (list ?x (macroexpand '(mail-header-xref header)) ?s)
1297         (list ?D (macroexpand '(mail-header-date header)) ?s)
1298         (list ?d '(gnus-dd-mmm (mail-header-date header)) ?s)
1299         (list ?M (macroexpand '(mail-header-id header)) ?s)
1300         (list ?r (macroexpand '(mail-header-references header)) ?s)
1301         (list ?c '(or (mail-header-chars header) 0) ?d)
1302         (list ?L 'lines ?d)
1303         (list ?I 'indentation ?s)
1304         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1305         (list ?R 'replied ?c)
1306         (list ?\[ 'opening-bracket ?c)
1307         (list ?\] 'closing-bracket ?c)
1308         (list ?\> '(make-string level ? ) ?s)
1309         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1310         (list ?i 'score ?d)
1311         (list ?z 'score-char ?c)
1312         (list ?U 'unread ?c)
1313         (list ?t '(gnus-summary-number-of-articles-in-thread 
1314                    (and (boundp 'thread) (car thread)))
1315               ?d)
1316         (list ?e '(gnus-summary-number-of-articles-in-thread 
1317                    (and (boundp 'thread) (car thread)) t)
1318               ?c)
1319         (list ?u 'user-defined ?s))
1320   "An alist of format specifications that can appear in summary lines,
1321 and what variables they correspond with, along with the type of the
1322 variable (string, integer, character, etc).")
1323
1324 (defconst gnus-summary-dummy-line-format-alist
1325   (list (list ?S 'subject ?s)
1326         (list ?N 'number ?d)
1327         (list ?u 'user-defined ?s)))
1328
1329 (defconst gnus-summary-mode-line-format-alist 
1330   (list (list ?G 'group-name ?s)
1331         (list ?g '(gnus-short-group-name group-name) ?s)
1332         (list ?A 'article-number ?d)
1333         (list ?Z 'unread-and-unselected ?s)
1334         (list ?V 'gnus-version ?s)
1335         (list ?U 'unread ?d)
1336         (list ?S 'subject ?s)
1337         (list ?e 'unselected ?d)
1338         (list ?u 'user-defined ?s)
1339         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1340
1341 (defconst gnus-group-mode-line-format-alist 
1342   (list (list ?S 'news-server ?s)
1343         (list ?M 'news-method ?s)
1344         (list ?u 'user-defined ?s)))
1345
1346 (defvar gnus-have-read-active-file nil)
1347
1348 (defconst gnus-maintainer
1349   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1350   "The mail address of the Gnus maintainers.")
1351
1352 (defconst gnus-version "Gnus v5.0.10"
1353   "Version number for this version of Gnus.")
1354
1355 (defvar gnus-info-nodes
1356   '((gnus-group-mode            "(gnus)The Group Buffer")
1357     (gnus-summary-mode          "(gnus)The Summary Buffer")
1358     (gnus-article-mode          "(gnus)The Article Buffer"))
1359   "Assoc list of major modes and related Info nodes.")
1360
1361 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1362   "The location of the Gnus documentation group.")
1363
1364 (defvar gnus-group-buffer "*Group*")
1365 (defvar gnus-summary-buffer "*Summary*")
1366 (defvar gnus-article-buffer "*Article*")
1367 (defvar gnus-server-buffer "*Server*")
1368
1369 (defvar gnus-work-buffer " *gnus work*")
1370
1371 (defvar gnus-buffer-list nil
1372   "Gnus buffers that should be killed on exit.")
1373
1374 (defvar gnus-server-alist nil
1375   "List of available servers.")
1376
1377 (defvar gnus-variable-list
1378   '(gnus-newsrc-options gnus-newsrc-options-n
1379     gnus-newsrc-last-checked-date 
1380     gnus-newsrc-alist gnus-server-alist
1381     gnus-killed-list gnus-zombie-list)
1382   "Gnus variables saved in the quick startup file.")
1383
1384 (defvar gnus-overload-functions
1385   '((news-inews gnus-inews-news "rnewspost"))
1386   "Functions overloaded by gnus.
1387 It is a list of `(original overload &optional file)'.")
1388
1389 (defvar gnus-newsrc-options nil
1390   "Options line in the .newsrc file.")
1391
1392 (defvar gnus-newsrc-options-n nil
1393   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1394
1395 (defvar gnus-newsrc-last-checked-date nil
1396   "Date Gnus last asked server for new newsgroups.")
1397
1398 (defvar gnus-newsrc-alist nil
1399   "Assoc list of read articles.
1400 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1401
1402 (defvar gnus-newsrc-hashtb nil
1403   "Hashtable of gnus-newsrc-alist.")
1404
1405 (defvar gnus-killed-list nil
1406   "List of killed newsgroups.")
1407
1408 (defvar gnus-killed-hashtb nil
1409   "Hash table equivalent of gnus-killed-list.")
1410
1411 (defvar gnus-zombie-list nil
1412   "List of almost dead newsgroups.")
1413
1414 (defvar gnus-description-hashtb nil
1415   "Descriptions of newsgroups.")
1416
1417 (defvar gnus-list-of-killed-groups nil
1418   "List of newsgroups that have recently been killed by the user.")
1419
1420 (defvar gnus-active-hashtb nil
1421   "Hashtable of active articles.")
1422
1423 (defvar gnus-moderated-list nil
1424   "List of moderated newsgroups.")
1425
1426 (defvar gnus-group-marked nil)
1427
1428 (defvar gnus-current-startup-file nil
1429   "Startup file for the current host.")
1430
1431 (defvar gnus-last-search-regexp nil
1432   "Default regexp for article search command.")
1433
1434 (defvar gnus-last-shell-command nil
1435   "Default shell command on article.")
1436
1437 (defvar gnus-current-select-method nil
1438   "The current method for selecting a newsgroup.")
1439
1440 (defvar gnus-group-list-mode nil)
1441
1442 (defvar gnus-article-internal-prepare-hook nil)
1443
1444 (defvar gnus-newsgroup-name nil)
1445 (defvar gnus-newsgroup-begin nil)
1446 (defvar gnus-newsgroup-end nil)
1447 (defvar gnus-newsgroup-last-rmail nil)
1448 (defvar gnus-newsgroup-last-mail nil)
1449 (defvar gnus-newsgroup-last-folder nil)
1450 (defvar gnus-newsgroup-last-file nil)
1451 (defvar gnus-newsgroup-auto-expire nil)
1452 (defvar gnus-newsgroup-active nil)
1453
1454 (defvar gnus-newsgroup-unreads nil
1455   "List of unread articles in the current newsgroup.")
1456
1457 (defvar gnus-newsgroup-unselected nil
1458   "List of unselected unread articles in the current newsgroup.")
1459
1460 (defvar gnus-newsgroup-reads nil
1461   "Alist of read articles and article marks in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-marked nil
1464   "List of ticked articles in the current newsgroup (a subset of unread art).")
1465
1466 (defvar gnus-newsgroup-killed nil
1467   "List of ranges of articles that have been through the scoring process.")
1468
1469 (defvar gnus-newsgroup-kill-headers nil)
1470
1471 (defvar gnus-newsgroup-replied nil
1472   "List of articles that have been replied to in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-expirable nil
1475   "List of articles in the current newsgroup that can be expired.")
1476
1477 (defvar gnus-newsgroup-processable nil
1478   "List of articles in the current newsgroup that can be processed.")
1479
1480 (defvar gnus-newsgroup-bookmarks nil
1481   "List of articles in the current newsgroup that have bookmarks.")
1482
1483 (defvar gnus-newsgroup-dormant nil
1484   "List of dormant articles in the current newsgroup.")
1485
1486 (defvar gnus-newsgroup-scored nil
1487   "List of scored articles in the current newsgroup.")
1488
1489 (defvar gnus-newsgroup-headers nil
1490   "List of article headers in the current newsgroup.")
1491 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1492
1493 (defvar gnus-newsgroup-ancient nil
1494   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1495
1496 (defvar gnus-current-article nil)
1497 (defvar gnus-article-current nil)
1498 (defvar gnus-current-headers nil)
1499 (defvar gnus-have-all-headers nil)
1500 (defvar gnus-last-article nil)
1501 (defvar gnus-newsgroup-history nil)
1502 (defvar gnus-current-kill-article nil)
1503
1504 ;; Save window configuration.
1505 (defvar gnus-prev-winconf nil)
1506
1507 ;; Format specs
1508 (defvar gnus-summary-line-format-spec nil)
1509 (defvar gnus-summary-dummy-line-format-spec nil)
1510 (defvar gnus-group-line-format-spec nil)
1511 (defvar gnus-summary-mode-line-format-spec nil)
1512 (defvar gnus-article-mode-line-format-spec nil)
1513 (defvar gnus-group-mode-line-format-spec nil)
1514 (defvar gnus-summary-mark-positions nil)
1515 (defvar gnus-group-mark-positions nil)
1516
1517 (defvar gnus-summary-expunge-below nil)
1518 (defvar gnus-reffed-article-number nil)
1519
1520 ; Let the byte-compiler know that we know about this variable.
1521 (defvar rmail-default-rmail-file)
1522
1523 (defvar gnus-cache-removeable-articles nil)
1524
1525 (defconst gnus-summary-local-variables 
1526   '(gnus-newsgroup-name 
1527     gnus-newsgroup-begin gnus-newsgroup-end 
1528     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1529     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1530     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1531     gnus-newsgroup-unselected gnus-newsgroup-marked
1532     gnus-newsgroup-reads
1533     gnus-newsgroup-replied gnus-newsgroup-expirable
1534     gnus-newsgroup-processable gnus-newsgroup-killed
1535     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1536     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1537     gnus-current-article gnus-current-headers gnus-have-all-headers
1538     gnus-last-article gnus-article-internal-prepare-hook
1539     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1540     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1541     gnus-newsgroup-threads gnus-newsgroup-async
1542     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1543     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1544     gnus-newsgroup-history gnus-newsgroup-ancient
1545     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1546     gnus-cache-removeable-articles)
1547   "Variables that are buffer-local to the summary buffers.")
1548
1549 (defconst gnus-bug-message
1550   "Sending a bug report to the Gnus Towers.
1551 ========================================
1552
1553 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1554 be sent to the Gnus Bug Exterminators. 
1555
1556 At the bottom of the buffer you'll see lots of variable settings.
1557 Please do not delete those.  They will tell the Bug People what your
1558 environment is, so that it will be easier to locate the bugs.
1559
1560 If you have found a bug that makes Emacs go \"beep\", set
1561 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1562 and include the backtrace in your bug report.
1563
1564 Please describe the bug in annoying, painstaking detail.
1565
1566 Thank you for your help in stamping out bugs.
1567 ")
1568
1569 ;;; End of variables.
1570
1571 ;; Define some autoload functions Gnus might use.
1572 (eval-and-compile
1573
1574   ;; Various 
1575   (autoload 'metamail-buffer "metamail")
1576   (autoload 'Info-goto-node "info")
1577   (autoload 'hexl-hex-string-to-integer "hexl")
1578   (autoload 'pp "pp")
1579   (autoload 'pp-to-string "pp")
1580   (autoload 'pp-eval-expression "pp")
1581   (autoload 'mail-extract-address-components "mail-extr")
1582
1583   (autoload 'nnmail-split-fancy "nnmail")
1584   (autoload 'nnvirtual-catchup-group "nnvirtual")
1585
1586   ;; timezone
1587   (autoload 'timezone-make-date-arpa-standard "timezone")
1588   (autoload 'timezone-fix-time "timezone")
1589   (autoload 'timezone-make-sortable-date "timezone")
1590   (autoload 'timezone-make-time-string "timezone")
1591
1592   ;; rmail & friends
1593   (autoload 'mail-position-on-field "sendmail")
1594   (autoload 'mail-setup "sendmail")
1595   (autoload 'rmail-output "rmailout")
1596   (autoload 'news-mail-other-window "rnewspost")
1597   (autoload 'news-reply-yank-original "rnewspost")
1598   (autoload 'news-caesar-buffer-body "rnewspost")
1599   (autoload 'rmail-insert-rmail-file-header "rmail")
1600   (autoload 'rmail-count-new-messages "rmail")
1601   (autoload 'rmail-show-message "rmail")
1602
1603   ;; gnus-soup
1604   ;;(autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1605   ;;(autoload 'gnus-brew-soup "gnus-soup" nil t)
1606   ;;(autoload 'gnus-soup-add-article "gnus-soup" nil t)
1607   ;;(autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1608   ;;(autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1609   ;;(autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1610   ;;(autoload 'nnsoup-pack-replies "nnsoup" nil t)
1611
1612   ;; gnus-mh
1613   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1614   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1615   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1616   (autoload 'gnus-summary-save-in-folder "gnus-mh" nil t)
1617   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1618   (autoload 'gnus-Folder-save-name "gnus-mh")
1619   (autoload 'gnus-folder-save-name "gnus-mh")
1620
1621   ;; gnus-vis misc
1622   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1623   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1624   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1625   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1626   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1627   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1628   (autoload 'gnus-summary-highlight-line "gnus-vis")
1629   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1630
1631   ;; gnus-vis article
1632   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1633   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1634   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1635   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1636   (autoload 'gnus-article-hide "gnus-vis" nil t)
1637   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1638   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1639   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1640   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1641   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1642   (autoload 'gnus-article-add-button "gnus-vis")
1643
1644   ;; gnus-cite
1645   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1646   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1647   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1648
1649   ;; gnus-kill
1650   (autoload 'gnus-kill "gnus-kill")
1651   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1652   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1653   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1654   (autoload 'gnus-execute "gnus-kill")
1655   (autoload 'gnus-expunge "gnus-kill")
1656
1657   ;; gnus-cache
1658   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1659   (autoload 'gnus-cache-save-buffers "gnus-cache")
1660   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1661   (autoload 'gnus-cache-request-article "gnus-cache")
1662   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1663   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1664   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1665   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1666
1667   ;; gnus-score
1668   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1669   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1670   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1671   (autoload 'gnus-score-save "gnus-score")
1672   (autoload 'gnus-score-headers "gnus-score")
1673   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1674   (autoload 'gnus-score-adaptive "gnus-score")
1675   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1676   (autoload 'gnus-score-find-trace "gnus-score")
1677
1678   ;; gnus-edit
1679   (autoload 'gnus-score-customize "gnus-edit" nil t)
1680
1681   ;; gnus-uu
1682   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1683   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1684   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1685   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1686   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1687   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1688   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1689   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1690   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1691   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1692   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1693   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1694   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1695   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1696   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1697   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1698   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1699   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1700   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1701   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1702   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1703   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1704
1705   ;; gnus-msg
1706   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1707   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1708   (autoload 'gnus-group-mail "gnus-msg" nil t)
1709   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1710   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1711   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1712   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1713   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1714   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1715   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1716   (autoload 'gnus-post-news "gnus-msg" nil t)
1717   (autoload 'gnus-inews-news "gnus-msg" nil t)
1718   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1719   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1720   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1721   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1722   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1723   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1724   (autoload 'gnus-mail-yank-original "gnus-msg")
1725   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1726   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1727   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1728   (autoload 'gnus-article-mail "gnus-msg")
1729   (autoload 'gnus-bug "gnus-msg" nil t)
1730
1731   ;; gnus-vm
1732   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1733   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1734   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1735   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1736   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1737   (autoload 'gnus-yank-article "gnus-vm" nil t)
1738
1739   )
1740
1741 \f
1742
1743 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1744 ;; If you want the cursor to go somewhere else, set these two
1745 ;; functions in some startup hook to whatever you want.
1746 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1747 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1748
1749 ;;; Various macros and substs.
1750
1751 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1752   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1753   (` (let ((GnusStartBufferWindow (selected-window)))
1754        (unwind-protect
1755            (progn
1756              (pop-to-buffer (, buffer))
1757              (,@ forms))
1758          (select-window GnusStartBufferWindow)))))
1759
1760 (defmacro gnus-gethash (string hashtable)
1761   "Get hash value of STRING in HASHTABLE."
1762   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1763   ;;(` (abbrev-expansion (, string) (, hashtable)))
1764   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1765
1766 (defmacro gnus-sethash (string value hashtable)
1767   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1768   ;; We cannot use define-abbrev since it only accepts string as value.
1769   ;; (set (intern string hashtable) value))
1770   (` (set (intern (, string) (, hashtable)) (, value))))
1771
1772 (defsubst gnus-buffer-substring (beg end)
1773   (buffer-substring (match-beginning beg) (match-end end)))
1774
1775 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1776 ;;   function `substring' might cut on a middle of multi-octet
1777 ;;   character.
1778 (defun gnus-truncate-string (str width)
1779   (substring str 0 width))
1780
1781 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1782 ;; to limit the length of a string. This function is necessary since
1783 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1784 (defsubst gnus-limit-string (str width)
1785   (if (> (length str) width)
1786       (substring str 0 width)
1787     str))
1788
1789 (defsubst gnus-simplify-subject-re (subject)
1790   "Remove \"Re:\" from subject lines."
1791   (let ((case-fold-search t))
1792     (if (string-match "^re: *" subject)
1793         (substring subject (match-end 0))
1794       subject)))
1795
1796 (defsubst gnus-goto-char (point)
1797   (and point (goto-char point)))
1798
1799 (defmacro gnus-buffer-exists-p (buffer)
1800   (` (and (, buffer)
1801           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1802                    (, buffer)))))
1803
1804 (defmacro gnus-kill-buffer (buffer)
1805   (` (if (gnus-buffer-exists-p (, buffer))
1806          (kill-buffer (, buffer)))))
1807
1808 (defsubst gnus-point-at-bol ()
1809   "Return point at the beginning of line."
1810   (let ((p (point)))
1811     (beginning-of-line)
1812     (prog1
1813         (point)
1814       (goto-char p))))
1815
1816 (defsubst gnus-point-at-eol ()
1817   "Return point at the beginning of line."
1818   (let ((p (point)))
1819     (end-of-line)
1820     (prog1
1821         (point)
1822       (goto-char p))))
1823
1824 ;; Delete the current line (and the next N lines.);
1825 (defmacro gnus-delete-line (&optional n)
1826   (` (delete-region (progn (beginning-of-line) (point))
1827                     (progn (forward-line (, (or n 1))) (point)))))
1828
1829 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1830 (defvar gnus-init-inhibit nil)
1831 (defun gnus-read-init-file (&optional inhibit-next)
1832   (if gnus-init-inhibit
1833       (setq gnus-init-inhibit nil)
1834     (setq gnus-init-inhibit inhibit-next)
1835     (and gnus-init-file
1836          (or (and (file-exists-p gnus-init-file) 
1837                   ;; Don't try to load a directory.
1838                   (not (file-directory-p gnus-init-file)))
1839              (file-exists-p (concat gnus-init-file ".el"))
1840              (file-exists-p (concat gnus-init-file ".elc")))
1841          (load gnus-init-file nil t))))
1842
1843 ;;; Load the user startup file.
1844 ;; (eval '(gnus-read-init-file 'inhibit))
1845
1846 ;;; Load the compatability functions. 
1847
1848 (require 'gnus-cus)
1849 (require 'gnus-ems)
1850
1851 \f
1852 ;;;
1853 ;;; Gnus Utility Functions
1854 ;;;
1855
1856 (defun gnus-extract-address-components (from)
1857   (let (name address)
1858     ;; First find the address - the thing with the @ in it.  This may
1859     ;; not be accurate in mail addresses, but does the trick most of
1860     ;; the time in news messages.
1861     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1862         (setq address (substring from (match-beginning 0) (match-end 0))))
1863     ;; Then we check whether the "name <address>" format is used.
1864     (and address
1865          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1866          ;; Linear white space is not required.
1867          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
1868          (and (setq name (substring from 0 (match-beginning 0)))
1869               ;; Strip any quotes from the name.
1870               (string-match "\".*\"" name)
1871               (setq name (substring name 1 (1- (match-end 0))))))
1872     ;; If not, then "address (name)" is used.
1873     (or name
1874         (and (string-match "(.+)" from)
1875              (setq name (substring from (1+ (match-beginning 0)) 
1876                                    (1- (match-end 0)))))
1877         (and (string-match "()" from)
1878              (setq name address))
1879         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1880         ;; XOVER might not support folded From headers.
1881         (and (string-match "(.*" from)
1882              (setq name (substring from (1+ (match-beginning 0)) 
1883                                    (match-end 0)))))
1884     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1885     (list (or name from) (or address from))))
1886
1887 (defun gnus-fetch-field (field)
1888   "Return the value of the header FIELD of current article."
1889   (save-excursion
1890     (save-restriction
1891       (let ((case-fold-search t))
1892         (gnus-narrow-to-headers)
1893         (mail-fetch-field field)))))
1894
1895 (defun gnus-goto-colon ()
1896   (beginning-of-line)
1897   (search-forward ":" (gnus-point-at-eol) t))
1898
1899 (defun gnus-narrow-to-headers ()
1900   (widen)
1901   (save-excursion
1902     (narrow-to-region
1903      (goto-char (point-min))
1904      (if (search-forward "\n\n" nil t)
1905          (1- (point))
1906        (point-max)))))
1907
1908 (defvar gnus-old-specs nil)
1909
1910 (defun gnus-update-format-specifications ()
1911   (gnus-make-thread-indent-array)
1912
1913   (let ((formats '(summary summary-dummy group 
1914                            summary-mode group-mode article-mode))
1915         old-format new-format)
1916     (while formats
1917       (setq new-format (symbol-value
1918                         (intern (format "gnus-%s-line-format" (car formats)))))
1919       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1920                (equal old-format new-format))
1921           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1922                (gnus-parse-format
1923                 new-format
1924                 (symbol-value 
1925                  (intern (format "gnus-%s-line-format-alist"
1926                                  (if (eq (car formats) 'article-mode)
1927                                      'summary-mode (car formats))))))))
1928       (setq gnus-old-specs (cons (cons (car formats) new-format)
1929                                  (delq (car formats) gnus-old-specs)))
1930       (setq formats (cdr formats))))
1931       
1932   (gnus-update-group-mark-positions)
1933   (gnus-update-summary-mark-positions)
1934
1935   (if (and (string-match "%D" gnus-group-line-format)
1936            (not gnus-description-hashtb)
1937            gnus-read-active-file)
1938       (gnus-read-all-descriptions-files)))
1939
1940 (defun gnus-update-summary-mark-positions ()
1941   (save-excursion
1942     (let ((gnus-replied-mark 129)
1943           (gnus-score-below-mark 130)
1944           (gnus-score-over-mark 130)
1945           (thread nil)
1946           pos)
1947       (gnus-set-work-buffer)
1948       (gnus-summary-insert-line 
1949        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1950       (goto-char (point-min))
1951       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1952                                          (- (point) 2)))))
1953       (goto-char (point-min))
1954       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1955                                           (- (point) 2))) pos))
1956       (goto-char (point-min))
1957       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1958                                         (- (point) 2))) pos))
1959       (setq gnus-summary-mark-positions pos))))
1960
1961 (defun gnus-update-group-mark-positions ()
1962   (save-excursion
1963     (let ((gnus-process-mark 128)
1964           (gnus-group-marked '("dummy.group")))
1965       (gnus-sethash "dummy.group" '(0 . 0) gnus-active-hashtb)
1966       (gnus-set-work-buffer)
1967       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1968       (goto-char (point-min))
1969       (setq gnus-group-mark-positions
1970             (list (cons 'process (and (search-forward "\200" nil t)
1971                                       (- (point) 2))))))))
1972
1973 (defun gnus-mouse-face-function (form)
1974   (` (let ((string (, form)))
1975        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1976        string)))
1977
1978 (defun gnus-max-width-function (el max-width)
1979   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1980   (` (let* ((val (eval (, el)))
1981             (valstr (if (numberp val)
1982                         (int-to-string val) val)))
1983        (if (> (length valstr) (, max-width))
1984            (substring valstr 0 (, max-width))
1985          valstr))))
1986
1987 (defun gnus-parse-format (format spec-alist)
1988   ;; This function parses the FORMAT string with the help of the
1989   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1990   ;; string.  If the FORMAT string contains the specifiers %( and %)
1991   ;; the text between them will have the mouse-face text property.
1992   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1993       (if (and gnus-visual gnus-mouse-face)
1994           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1995                 (button (substring format (match-beginning 2) (match-end 2)))
1996                 (post (substring format (match-beginning 3) (match-end 3))))
1997             (list 'concat
1998                   (gnus-parse-simple-format pre spec-alist)
1999                   (gnus-mouse-face-function 
2000                    (gnus-parse-simple-format button spec-alist))
2001                   (gnus-parse-simple-format post spec-alist)))
2002         (gnus-parse-simple-format
2003          (concat (substring format (match-beginning 1) (match-end 1))
2004                  (substring format (match-beginning 2) (match-end 2))
2005                  (substring format (match-beginning 3) (match-end 3)))
2006          spec-alist))
2007     (gnus-parse-simple-format format spec-alist)))
2008
2009 (defun gnus-parse-simple-format (format spec-alist)
2010   ;; This function parses the FORMAT string with the help of the
2011   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2012   ;; string. The list will consist of the symbol `format', a format
2013   ;; specification string, and a list of forms depending on the
2014   ;; SPEC-ALIST.
2015   (let ((max-width 0)
2016         spec flist fstring newspec elem beg)
2017     (save-excursion
2018       (gnus-set-work-buffer)
2019       (insert format)
2020       (goto-char (point-min))
2021       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?" nil t)
2022         (setq spec (string-to-char (buffer-substring (match-beginning 2)
2023                                                      (match-end 2))))
2024         ;; First check if there are any specs that look anything like
2025         ;; "%12,12A", ie. with a "max width specification". These have
2026         ;; to be treated specially.
2027         (if (setq beg (match-beginning 1))
2028             (setq max-width 
2029                   (string-to-int 
2030                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
2031           (setq max-width 0)
2032           (setq beg (match-beginning 2)))
2033         ;; Find the specification from `spec-alist'.
2034         (if (not (setq elem (cdr (assq spec spec-alist))))
2035             (setq elem '("*" ?s)))
2036         ;; Treat user defined format specifiers specially
2037         (and (eq (car elem) 'user-defined)
2038              (setq elem
2039                    (list 
2040                     (list (intern (concat "gnus-user-format-function-"
2041                                           (buffer-substring
2042                                            (match-beginning 3)
2043                                            (match-end 3))))
2044                           'header)
2045                     ?s))
2046              (delete-region (match-beginning 3) (match-end 3)))
2047         (if (not (zerop max-width))
2048             (let ((el (car elem)))
2049               (cond ((= (car (cdr elem)) ?c) 
2050                      (setq el (list 'char-to-string el)))
2051                     ((= (car (cdr elem)) ?d)
2052                      (numberp el) (setq el (list 'int-to-string el))))
2053               (setq flist (cons (gnus-max-width-function el max-width)
2054                                 flist))
2055               (setq newspec ?s))
2056           (setq flist (cons (car elem) flist))
2057           (setq newspec (car (cdr elem))))
2058         ;; Remove the old specification (and possibly a ",12" string).
2059         (delete-region beg (match-end 2))
2060         ;; Insert the new specification.
2061         (goto-char beg)
2062         (insert newspec))
2063       (setq fstring (buffer-substring 1 (point-max))))
2064     (cons 'format (cons fstring (nreverse flist)))))
2065
2066 (defun gnus-set-work-buffer ()
2067   (if (get-buffer gnus-work-buffer)
2068       (progn
2069         (set-buffer gnus-work-buffer)
2070         (erase-buffer))
2071     (set-buffer (get-buffer-create gnus-work-buffer))
2072     (kill-all-local-variables)
2073     (buffer-disable-undo (current-buffer))
2074     (gnus-add-current-to-buffer-list)))
2075
2076 ;; Article file names when saving.
2077
2078 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2079   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2080 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2081 Otherwise, it is like ~/News/news/group/num."
2082   (let ((default
2083           (expand-file-name
2084            (concat (if (gnus-use-long-file-name 'not-save)
2085                        (gnus-capitalize-newsgroup newsgroup)
2086                      (gnus-newsgroup-directory-form newsgroup))
2087                    "/" (int-to-string (mail-header-number headers)))
2088            (or gnus-article-save-directory "~/News"))))
2089     (if (and last-file
2090              (string-equal (file-name-directory default)
2091                            (file-name-directory last-file))
2092              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2093         default
2094       (or last-file default))))
2095
2096 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2097   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2098 If variable `gnus-use-long-file-name' is non-nil, it is
2099 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2100   (let ((default
2101           (expand-file-name
2102            (concat (if (gnus-use-long-file-name 'not-save)
2103                        newsgroup
2104                      (gnus-newsgroup-directory-form newsgroup))
2105                    "/" (int-to-string (mail-header-number headers)))
2106            (or gnus-article-save-directory "~/News"))))
2107     (if (and last-file
2108              (string-equal (file-name-directory default)
2109                            (file-name-directory last-file))
2110              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2111         default
2112       (or last-file default))))
2113
2114 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2115   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2116 If variable `gnus-use-long-file-name' is non-nil, it is
2117 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2118   (or last-file
2119       (expand-file-name
2120        (if (gnus-use-long-file-name 'not-save)
2121            (gnus-capitalize-newsgroup newsgroup)
2122          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2123        (or gnus-article-save-directory "~/News"))))
2124
2125 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2126   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2127 If variable `gnus-use-long-file-name' is non-nil, it is
2128 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2129   (or last-file
2130       (expand-file-name
2131        (if (gnus-use-long-file-name 'not-save)
2132            newsgroup
2133          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2134        (or gnus-article-save-directory "~/News"))))
2135
2136 ;; For subscribing new newsgroup
2137
2138 (defun gnus-subscribe-hierarchical-interactive (groups)
2139   (let ((groups (sort groups 'string<))
2140         prefixes prefix start ans group starts)
2141     (while groups
2142       (setq prefixes (list "^"))
2143       (while (and groups prefixes)
2144         (while (not (string-match (car prefixes) (car groups)))
2145           (setq prefixes (cdr prefixes)))
2146         (setq prefix (car prefixes))
2147         (setq start (1- (length prefix)))
2148         (if (and (string-match "[^\\.]\\." (car groups) start)
2149                  (cdr groups)
2150                  (setq prefix 
2151                        (concat "^" (substring (car groups) 0 (match-end 0))))
2152                  (string-match prefix (car (cdr groups))))
2153             (progn
2154               (setq prefixes (cons prefix prefixes))
2155               (message "Descend hierarchy %s? ([y]nsq): " 
2156                        (substring prefix 1 (1- (length prefix))))
2157               (setq ans (read-char))
2158               (cond ((= ans ?n)
2159                      (while (and groups 
2160                                  (string-match prefix 
2161                                                (setq group (car groups))))
2162                        (setq gnus-killed-list 
2163                              (cons group gnus-killed-list))
2164                        (gnus-sethash group group gnus-killed-hashtb)
2165                        (setq groups (cdr groups)))
2166                      (setq starts (cdr starts)))
2167                     ((= ans ?s)
2168                      (while (and groups 
2169                                  (string-match prefix 
2170                                                (setq group (car groups))))
2171                        (gnus-sethash group group gnus-killed-hashtb)
2172                        (gnus-subscribe-alphabetically (car groups))
2173                        (setq groups (cdr groups)))
2174                      (setq starts (cdr starts)))
2175                     ((= ans ?q)
2176                      (while groups
2177                        (setq group (car groups))
2178                        (setq gnus-killed-list (cons group gnus-killed-list))
2179                        (gnus-sethash group group gnus-killed-hashtb)
2180                        (setq groups (cdr groups))))
2181                     (t nil)))
2182           (message "Subscribe %s? ([n]yq)" (car groups))
2183           (setq ans (read-char))
2184           (setq group (car groups))
2185           (cond ((= ans ?y)
2186                  (gnus-subscribe-alphabetically (car groups))
2187                  (gnus-sethash group group gnus-killed-hashtb))
2188                 ((= ans ?q)
2189                  (while groups
2190                    (setq group (car groups))
2191                    (setq gnus-killed-list (cons group gnus-killed-list))
2192                    (gnus-sethash group group gnus-killed-hashtb)
2193                    (setq groups (cdr groups))))
2194                 (t 
2195                  (setq gnus-killed-list (cons group gnus-killed-list))
2196                  (gnus-sethash group group gnus-killed-hashtb)))
2197           (setq groups (cdr groups)))))))
2198
2199 (defun gnus-subscribe-randomly (newsgroup)
2200   "Subscribe new NEWSGROUP by making it the first newsgroup."
2201   (gnus-subscribe-newsgroup newsgroup))
2202
2203 (defun gnus-subscribe-alphabetically (newgroup)
2204   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2205   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2206   (let ((groups (cdr gnus-newsrc-alist))
2207         before)
2208     (while (and (not before) groups)
2209       (if (string< newgroup (car (car groups)))
2210           (setq before (car (car groups)))
2211         (setq groups (cdr groups))))
2212     (gnus-subscribe-newsgroup newgroup before)))
2213
2214 (defun gnus-subscribe-hierarchically (newgroup)
2215   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2216   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2217   (save-excursion
2218     (set-buffer (find-file-noselect gnus-current-startup-file))
2219     (let ((groupkey newgroup)
2220           before)
2221       (while (and (not before) groupkey)
2222         (goto-char (point-min))
2223         (let ((groupkey-re
2224                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2225           (while (and (re-search-forward groupkey-re nil t)
2226                       (progn
2227                         (setq before (buffer-substring
2228                                       (match-beginning 1) (match-end 1)))
2229                         (string< before newgroup)))))
2230         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2231         (setq groupkey
2232               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2233                   (substring groupkey (match-beginning 1) (match-end 1)))))
2234       (gnus-subscribe-newsgroup newgroup before))))
2235
2236 (defun gnus-subscribe-interactively (newsgroup)
2237   "Subscribe new NEWSGROUP interactively.
2238 It is inserted in hierarchical newsgroup order if subscribed. If not,
2239 it is killed."
2240   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2241       (gnus-subscribe-hierarchically newsgroup)
2242     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2243
2244 (defun gnus-subscribe-zombies (newsgroup)
2245   "Make new NEWSGROUP a zombie group."
2246   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2247
2248 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2249   "Subscribe new NEWSGROUP.
2250 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2251 the first newsgroup."
2252   ;; We subscribe the group by changing its level to `subscribed'.
2253   (gnus-group-change-level 
2254    newsgroup gnus-level-default-subscribed
2255    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2256   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2257
2258 ;; For directories
2259
2260 (defun gnus-newsgroup-directory-form (newsgroup)
2261   "Make hierarchical directory name from NEWSGROUP name."
2262   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2263         (len (length newsgroup))
2264         idx)
2265     ;; If this is a foreign group, we don't want to translate the
2266     ;; entire name.  
2267     (if (setq idx (string-match ":" newsgroup))
2268         (aset newsgroup idx ?/)
2269       (setq idx 0))
2270     ;; Replace all occurrences of `.' with `/'.
2271     (while (< idx len)
2272       (if (= (aref newsgroup idx) ?.)
2273           (aset newsgroup idx ?/))
2274       (setq idx (1+ idx)))
2275     newsgroup))
2276
2277 (defun gnus-newsgroup-saveable-name (group)
2278   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2279   ;; with dots.
2280   (gnus-replace-chars-in-string group ?/ ?.))
2281
2282 (defun gnus-make-directory (dir)
2283   "Make DIRECTORY recursively."
2284   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2285   ;; of the many mysteries of the universe.
2286   (let* ((dir (expand-file-name dir default-directory))
2287          dirs err)
2288     (if (string-match "/$" dir)
2289         (setq dir (substring dir 0 (match-beginning 0))))
2290     ;; First go down the path until we find a directory that exists.
2291     (while (not (file-exists-p dir))
2292       (setq dirs (cons dir dirs))
2293       (string-match "/[^/]+$" dir)
2294       (setq dir (substring dir 0 (match-beginning 0))))
2295     ;; Then create all the subdirs.
2296     (while (and dirs (not err))
2297       (condition-case ()
2298           (make-directory (car dirs))
2299         (error (setq err t)))
2300       (setq dirs (cdr dirs)))
2301     ;; We return whether we were successful or not. 
2302     (not dirs)))
2303
2304 (defun gnus-capitalize-newsgroup (newsgroup)
2305   "Capitalize NEWSGROUP name."
2306   (and (not (zerop (length newsgroup)))
2307        (concat (char-to-string (upcase (aref newsgroup 0)))
2308                (substring newsgroup 1))))
2309
2310 ;; Var
2311
2312 (defun gnus-simplify-subject (subject &optional re-only)
2313   "Remove `Re:' and words in parentheses.
2314 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2315   (let ((case-fold-search t))           ;Ignore case.
2316     ;; Remove `Re:' and `Re^N:'.
2317     (if (string-match "^re:[ \t]*" subject)
2318         (setq subject (substring subject (match-end 0))))
2319     ;; Remove words in parentheses from end.
2320     (or re-only
2321         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2322           (setq subject (substring subject 0 (match-beginning 0)))))
2323     ;; Return subject string.
2324     subject))
2325
2326 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2327 ;; all whitespace.
2328 (defun gnus-simplify-subject-fuzzy (subject)
2329   (let ((case-fold-search t))
2330     (save-excursion
2331       (gnus-set-work-buffer)
2332       (insert subject)
2333       (inline (gnus-simplify-buffer-fuzzy))
2334       (buffer-string))))
2335
2336 (defun gnus-simplify-buffer-fuzzy ()
2337   (goto-char (point-min))
2338   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2339   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2340                             nil t)
2341     (replace-match "" t t))
2342   (goto-char (point-min))
2343   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2344     (replace-match "" t t))
2345   (goto-char (point-min))
2346   (while (re-search-forward "[ \t]+" nil t)
2347     (replace-match " " t t))
2348   (goto-char (point-min))
2349   (while (re-search-forward "[ \t]+$" nil t)
2350     (replace-match "" t t))
2351   (goto-char (point-min))
2352   (while (re-search-forward "^[ \t]+" nil t)
2353     (replace-match "" t t))
2354   (if gnus-simplify-subject-fuzzy-regexp
2355       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2356         (replace-match "" t t))))
2357
2358 ;; Add the current buffer to the list of buffers to be killed on exit. 
2359 (defun gnus-add-current-to-buffer-list ()
2360   (or (memq (current-buffer) gnus-buffer-list)
2361       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2362
2363 (defun gnus-string> (s1 s2)
2364   (not (or (string< s1 s2)
2365            (string= s1 s2))))
2366
2367 ;; Functions accessing headers.
2368 ;; Functions are more convenient than macros in some cases.
2369
2370 (defun gnus-header-number (header)
2371   (mail-header-number header))
2372
2373 (defun gnus-header-subject (header)
2374   (mail-header-subject header))
2375
2376 (defun gnus-header-from (header)
2377   (mail-header-from header))
2378
2379 (defun gnus-header-xref (header)
2380   (mail-header-xref header))
2381
2382 (defun gnus-header-lines (header)
2383   (mail-header-lines header))
2384
2385 (defun gnus-header-date (header)
2386   (mail-header-date header))
2387
2388 (defun gnus-header-id (header)
2389   (mail-header-id header))
2390
2391 (defun gnus-header-message-id (header)
2392   (mail-header-id header))
2393
2394 (defun gnus-header-chars (header)
2395   (mail-header-chars header))
2396
2397 (defun gnus-header-references (header)
2398   (mail-header-references header))
2399
2400 ;;; General various misc type functions.
2401
2402 (defun gnus-clear-system ()
2403   "Clear all variables and buffers."
2404   ;; Clear Gnus variables.
2405   (let ((variables gnus-variable-list))
2406     (while variables
2407       (set (car variables) nil)
2408       (setq variables (cdr variables))))
2409   ;; Clear other internal variables.
2410   (setq gnus-list-of-killed-groups nil
2411         gnus-have-read-active-file nil
2412         gnus-newsrc-alist nil
2413         gnus-newsrc-hashtb nil
2414         gnus-killed-list nil
2415         gnus-zombie-list nil
2416         gnus-killed-hashtb nil
2417         gnus-active-hashtb nil
2418         gnus-moderated-list nil
2419         gnus-description-hashtb nil
2420         gnus-newsgroup-headers nil
2421         gnus-newsgroup-headers-hashtb-by-number nil
2422         gnus-newsgroup-name nil
2423         gnus-server-alist nil
2424         gnus-current-select-method nil)
2425   ;; Reset any score variables.
2426   (and (boundp 'gnus-score-cache)
2427        (set 'gnus-score-cache nil))
2428   (and (boundp 'gnus-internal-global-score-files)
2429        (set 'gnus-internal-global-score-files nil))
2430   ;; Kill the startup file.
2431   (and gnus-current-startup-file
2432        (get-file-buffer gnus-current-startup-file)
2433        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2434   ;; Save any cache buffers.
2435   (and gnus-use-cache (gnus-cache-save-buffers))
2436   ;; Clear the dribble buffer.
2437   (gnus-dribble-clear)
2438   ;; Kill global KILL file buffer.
2439   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2440       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2441   (gnus-kill-buffer nntp-server-buffer)
2442   ;; Kill Gnus buffers.
2443   (while gnus-buffer-list
2444     (gnus-kill-buffer (car gnus-buffer-list))
2445     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2446
2447 (defun gnus-windows-old-to-new (setting)
2448   ;; First we take care of the really, really old Gnus 3 actions.
2449   (if (symbolp setting)
2450       (setq setting 
2451             (cond ((memq setting '(SelectArticle))
2452                    'article)
2453                   ((memq setting '(SelectSubject ExpandSubject))
2454                    'summary)
2455                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
2456                    'group)
2457                   (t setting))))
2458   (if (or (listp setting)
2459           (not (and gnus-window-configuration
2460                     (memq setting '(group summary article)))))
2461       setting
2462     (let* ((setting (if (eq setting 'group) 
2463                         (if (assq 'newsgroup gnus-window-configuration)
2464                             'newsgroup
2465                           'newsgroups) setting))
2466            (elem (car (cdr (assq setting gnus-window-configuration))))
2467            (total (apply '+ elem))
2468            (types '(group summary article))
2469            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2470            (i 0)
2471            perc
2472            out)
2473       (while (< i 3)
2474         (or (not (numberp (nth i elem)))
2475             (zerop (nth i elem))
2476             (progn
2477               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2478               (setq out (cons (if (eq pbuf (nth i types))
2479                                   (vector (nth i types) perc 'point)
2480                                 (vector (nth i types) perc))
2481                               out))))
2482         (setq i (1+ i)))
2483       (list (nreverse out)))))
2484            
2485 (defun gnus-add-configuration (conf)
2486   (setq gnus-buffer-configuration 
2487         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2488                          gnus-buffer-configuration))))
2489
2490 (defun gnus-configure-windows (setting &optional force)
2491   (setq setting (gnus-windows-old-to-new setting))
2492   (let ((r (if (symbolp setting)
2493                (cdr (assq setting gnus-buffer-configuration))
2494              setting))
2495         (in-buf (current-buffer))
2496         rule val w height hor ohor heights sub jump-buffer
2497         rel total to-buf all-visible)
2498     (or r (error "No such setting: %s" setting))
2499
2500     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2501         ;; All the windows mentioned are already visible, so we just
2502         ;; put point in the assigned buffer, and do not touch the
2503         ;; winconf. 
2504         (select-window (get-buffer-window all-visible t))
2505          
2506
2507       ;; Either remove all windows or just remove all Gnus windows.
2508       (if gnus-use-full-window
2509           (delete-other-windows)
2510         (gnus-remove-some-windows)
2511         (switch-to-buffer nntp-server-buffer))
2512
2513       (while r
2514         (setq hor (car r)
2515               ohor nil)
2516
2517         ;; We have to do the (possible) horizontal splitting before the
2518         ;; vertical. 
2519         (if (and (listp (car hor)) 
2520                  (eq (car (car hor)) 'horizontal))
2521             (progn
2522               (split-window 
2523                nil
2524                (if (integerp (nth 1 (car hor)))
2525                    (nth 1 (car hor))
2526                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2527                t)
2528               (setq hor (cdr hor))))
2529
2530         ;; Go through the rules and eval the elements that are to be
2531         ;; evaled.  
2532         (while hor
2533           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2534               (progn
2535                 ;; Expand short buffer name.
2536                 (setq w (aref val 0))
2537                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2538                      (progn
2539                        (setq val (apply 'vector (mapcar 'identity val)))
2540                        (aset val 0 w)))
2541                 (setq ohor (cons val ohor))))
2542           (setq hor (cdr hor)))
2543         (setq rule (cons (nreverse ohor) rule))
2544         (setq r (cdr r)))
2545       (setq rule (nreverse rule))
2546
2547       ;; We tally the window sizes.
2548       (setq total (window-height))
2549       (while rule
2550         (setq hor (car rule))
2551         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2552             (setq hor (cdr hor)))
2553         (setq sub 0)
2554         (while hor
2555           (setq rel (aref (car hor) 1)
2556                 heights (cons
2557                          (cond ((and (floatp rel) (= 1.0 rel))
2558                                 'x)
2559                                ((integerp rel)
2560                                 rel)
2561                                (t
2562                                 (max (floor (* total rel)) 4)))
2563                          heights)
2564                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2565                 hor (cdr hor)))
2566         (setq heights (nreverse heights)
2567               hor (car rule))
2568
2569         ;; We then go through these heighs and create windows for them.
2570         (while heights
2571           (setq height (car heights)
2572                 heights (cdr heights))
2573           (and (eq height 'x)
2574                (setq height (- total sub)))
2575           (and heights
2576                (split-window nil height))
2577           (setq to-buf (aref (car hor) 0))
2578           (switch-to-buffer 
2579            (cond ((not to-buf)
2580                   in-buf)
2581                  ((symbolp to-buf)
2582                   (symbol-value (aref (car hor) 0)))
2583                  (t
2584                   (aref (car hor) 0))))
2585           (and (> (length (car hor)) 2)
2586                (eq (aref (car hor) 2) 'point)
2587                (setq jump-buffer (current-buffer)))
2588           (other-window 1)
2589           (setq hor (cdr hor)))
2590       
2591         (setq rule (cdr rule)))
2592
2593       ;; Finally, we pop to the buffer that's supposed to have point. 
2594       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2595
2596       (select-window (get-buffer-window jump-buffer t))
2597       (set-buffer jump-buffer))))
2598
2599 (defun gnus-all-windows-visible-p (rule)
2600   (let (invisible hor jump-buffer val buffer)
2601     ;; Go through the rules and eval the elements that are to be
2602     ;; evaled.  
2603     (while (and rule (not invisible))
2604       (setq hor (car rule)
2605             rule (cdr rule))
2606       (while (and hor (not invisible))
2607         (if (setq val (if (vectorp (car hor)) 
2608                           (car hor)
2609                         (if (not (eq (car (car hor)) 'horizontal))
2610                             (eval (car hor)))))
2611             (progn
2612               ;; Expand short buffer name.
2613               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2614                                (aref val 0)))
2615               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2616                              buffer))
2617               (and (> (length val) 2) (eq 'point (aref val 2))
2618                    (setq jump-buffer buffer))
2619               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2620         (setq hor (cdr hor))))
2621     (and (not invisible) jump-buffer)))
2622
2623 (defun gnus-window-top-edge (&optional window)
2624   (nth 1 (window-edges window)))
2625
2626 (defun gnus-remove-some-windows ()
2627   (let ((buffers gnus-window-to-buffer)
2628         buf bufs lowest-buf lowest)
2629     (save-excursion
2630       ;; Remove windows on all known Gnus buffers.
2631       (while buffers
2632         (setq buf (cdr (car buffers)))
2633         (if (symbolp buf)
2634             (setq buf (and (boundp buf) (symbol-value buf))))
2635         (and buf 
2636              (get-buffer-window buf)
2637              (progn
2638                (setq bufs (cons buf bufs))
2639                (pop-to-buffer buf)
2640                (if (or (not lowest)
2641                        (< (gnus-window-top-edge) lowest))
2642                    (progn
2643                      (setq lowest (gnus-window-top-edge))
2644                      (setq lowest-buf buf)))))
2645         (setq buffers (cdr buffers)))
2646       ;; Remove windows on *all* summary buffers.
2647       (let (wins)
2648         (walk-windows
2649          (lambda (win)
2650            (let ((buf (window-buffer win)))
2651              (if (string-match  "^\\*Summary" (buffer-name buf))
2652                  (progn
2653                    (setq bufs (cons buf bufs))
2654                    (pop-to-buffer buf)
2655                    (if (or (not lowest)
2656                            (< (gnus-window-top-edge) lowest))
2657                        (progn
2658                          (setq lowest-buf buf)
2659                          (setq lowest (gnus-window-top-edge))))))))))
2660       (and lowest-buf 
2661            (progn
2662              (pop-to-buffer lowest-buf)
2663              (switch-to-buffer nntp-server-buffer)))
2664       (while bufs
2665         (and (not (eq (car bufs) lowest-buf))
2666              (delete-windows-on (car bufs)))
2667         (setq bufs (cdr bufs))))))
2668                           
2669 (defun gnus-version ()
2670   "Version numbers of this version of Gnus."
2671   (interactive)
2672   (let ((methods gnus-valid-select-methods)
2673         (mess gnus-version)
2674         meth)
2675     ;; Go through all the legal select methods and add their version
2676     ;; numbers to the total version string. Only the backends that are
2677     ;; currently in use will have their message numbers taken into
2678     ;; consideration. 
2679     (while methods
2680       (setq meth (intern (concat (car (car methods)) "-version")))
2681       (and (boundp meth)
2682            (stringp (symbol-value meth))
2683            (setq mess (concat mess "; " (symbol-value meth))))
2684       (setq methods (cdr methods)))
2685     (gnus-message 2 mess)))
2686
2687 (defun gnus-info-find-node ()
2688   "Find Info documentation of Gnus."
2689   (interactive)
2690   ;; Enlarge info window if needed.
2691   (let ((mode major-mode))
2692     (gnus-configure-windows 'info)
2693     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2694
2695 (defun gnus-overload-functions (&optional overloads)
2696   "Overload functions specified by optional argument OVERLOADS.
2697 If nothing is specified, use the variable gnus-overload-functions."
2698   (let ((defs nil)
2699         (overloads (or overloads gnus-overload-functions)))
2700     (while overloads
2701       (setq defs (car overloads))
2702       (setq overloads (cdr overloads))
2703       ;; Load file before overloading function if necessary.  Make
2704       ;; sure we cannot use `require' always.
2705       (and (not (fboundp (car defs)))
2706            (car (cdr (cdr defs)))
2707            (load (car (cdr (cdr defs))) nil 'nomessage))
2708       (fset (car defs) (car (cdr defs))))))
2709
2710 (defun gnus-replace-chars-in-string (string &rest pairs)
2711   "Replace characters in STRING from FROM to TO."
2712   (let ((string (substring string 0))   ;Copy string.
2713         (len (length string))
2714         (idx 0)
2715         sym to)
2716     (or (zerop (% (length pairs) 2)) 
2717         (error "Odd number of translation pairs"))
2718     (setplist 'sym pairs)
2719     ;; Replace all occurrences of FROM with TO.
2720     (while (< idx len)
2721       (if (setq to (get 'sym (aref string idx)))
2722           (aset string idx to))
2723       (setq idx (1+ idx)))
2724     string))
2725
2726 (defun gnus-days-between (date1 date2)
2727   ;; Return the number of days between date1 and date2.
2728   (- (gnus-day-number date1) (gnus-day-number date2)))
2729
2730 (defun gnus-day-number (date)
2731   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2732                      (timezone-parse-date date))))
2733     (timezone-absolute-from-gregorian 
2734      (nth 1 dat) (nth 2 dat) (car dat))))
2735
2736 ;; Returns a floating point number that says how many seconds have
2737 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2738 (defun gnus-seconds-since-epoch (date)
2739   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2740                         (timezone-parse-date date)))
2741          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2742                         (timezone-parse-time
2743                          (aref (timezone-parse-date date) 3))))
2744          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2745                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2746          (tday (- (timezone-absolute-from-gregorian 
2747                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2748                   (timezone-absolute-from-gregorian 
2749                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2750     (+ (nth 2 ttime)
2751        (* (nth 1 ttime) 60)
2752        (* 1.0 (nth 0 ttime) 60 60)
2753        (* 1.0 tday 60 60 24))))
2754
2755 (defun gnus-file-newer-than (file date)
2756   (let ((fdate (nth 5 (file-attributes file))))
2757     (or (> (car fdate) (car date))
2758         (and (= (car fdate) (car date))
2759              (> (nth 1 fdate) (nth 1 date))))))
2760
2761 (defun gnus-group-read-only-p (&optional group)
2762   "Check whether GROUP supports editing or not.
2763 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
2764 that that variable is buffer-local to the summary buffers."
2765   (let ((group (or group gnus-newsgroup-name)))
2766     (not (gnus-check-backend-function 'request-replace-article group))))
2767
2768 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2769 ;; the echo area.
2770 (defun gnus-y-or-n-p (prompt)
2771   (prog1
2772       (y-or-n-p prompt)
2773     (message "")))
2774
2775 (defun gnus-yes-or-no-p (prompt)
2776   (prog1
2777       (yes-or-no-p prompt)
2778     (message "")))
2779
2780 ;; Check whether to use long file names.
2781 (defun gnus-use-long-file-name (symbol)
2782   ;; The variable has to be set...
2783   (and gnus-use-long-file-name
2784        ;; If it isn't a list, then we return t.
2785        (or (not (listp gnus-use-long-file-name))
2786            ;; If it is a list, and the list contains `symbol', we
2787            ;; return nil.  
2788            (not (memq symbol gnus-use-long-file-name)))))
2789
2790 ;; I suspect there's a better way, but I haven't taken the time to do
2791 ;; it yet. -erik selberg@cs.washington.edu
2792 (defun gnus-dd-mmm (messy-date)
2793   "Return a string like DD-MMM from a big messy string"
2794   (let ((datevec (timezone-parse-date messy-date)))
2795     (format "%2s-%s"
2796             (or (aref datevec 2) "??")
2797             (capitalize
2798              (or (car 
2799                   (nth (1- (string-to-number (aref datevec 1)))
2800                        timezone-months-assoc))
2801                  "???")))))
2802
2803 ;; Make a hash table (default and minimum size is 255).
2804 ;; Optional argument HASHSIZE specifies the table size.
2805 (defun gnus-make-hashtable (&optional hashsize)
2806   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2807
2808 ;; Make a number that is suitable for hashing; bigger than MIN and one
2809 ;; less than 2^x.
2810 (defun gnus-create-hash-size (min)
2811   (let ((i 1))
2812     (while (< i min)
2813       (setq i (* 2 i)))
2814     (1- i)))
2815
2816 ;; Show message if message has a lower level than `gnus-verbose'. 
2817 ;; Guide-line for numbers:
2818 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2819 ;; for things that take a long time, 7 - not very important messages
2820 ;; on stuff, 9 - messages inside loops.
2821 (defun gnus-message (level &rest args)
2822   (if (<= level gnus-verbose)
2823       (apply 'message args)
2824     ;; We have to do this format thingie here even if the result isn't
2825     ;; shown - the return value has to be the same as the return value
2826     ;; from `message'.
2827     (apply 'format args)))
2828
2829 ;; Generate a unique new group name.
2830 (defun gnus-generate-new-group-name (leaf)
2831   (let ((name leaf)
2832         (num 0))
2833     (while (gnus-gethash name gnus-newsrc-hashtb)
2834       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2835     name))
2836
2837 (defun gnus-ephemeral-group-p (group)
2838   "Say whether GROUP is ephemeral or not."
2839   (assoc 'quit-config (gnus-find-method-for-group group)))
2840
2841 ;;; List and range functions
2842
2843 (defun gnus-last-element (list)
2844   "Return last element of LIST."
2845   (while (cdr list)
2846     (setq list (cdr list)))
2847   (car list))
2848
2849 (defun gnus-copy-sequence (list)
2850   "Do a complete, total copy of a list."
2851   (if (and (consp list) (not (consp (cdr list))))
2852       (cons (car list) (cdr list))
2853     (mapcar (lambda (elem) (if (consp elem) 
2854                                (if (consp (cdr elem))
2855                                    (gnus-copy-sequence elem)
2856                                  (cons (car elem) (cdr elem)))
2857                              elem))
2858             list)))
2859
2860 (defun gnus-set-difference (list1 list2)
2861   "Return a list of elements of LIST1 that do not appear in LIST2."
2862   (let ((list1 (copy-sequence list1)))
2863     (while list2
2864       (setq list1 (delq (car list2) list1))
2865       (setq list2 (cdr list2)))
2866     list1))
2867
2868 (defun gnus-sorted-complement (list1 list2)
2869   "Return a list of elements of LIST1 that do not appear in LIST2.
2870 Both lists have to be sorted over <."
2871   (let (out)
2872     (if (or (null list1) (null list2))
2873         (or list1 list2)
2874       (while (and list1 list2)
2875         (cond ((= (car list1) (car list2))
2876                (setq list1 (cdr list1)
2877                      list2 (cdr list2)))
2878               ((< (car list1) (car list2))
2879                (setq out (cons (car list1) out))
2880                (setq list1 (cdr list1)))
2881               (t
2882                (setq out (cons (car list2) out))
2883                (setq list2 (cdr list2)))))
2884       (nconc (nreverse out) (or list1 list2)))))
2885
2886 (defun gnus-intersection (list1 list2)      
2887   (let ((result nil))
2888     (while list2
2889       (if (memq (car list2) list1)
2890           (setq result (cons (car list2) result)))
2891       (setq list2 (cdr list2)))
2892     result))
2893
2894 (defun gnus-sorted-intersection (list1 list2)
2895   ;; LIST1 and LIST2 have to be sorted over <.
2896   (let (out)
2897     (while (and list1 list2)
2898       (cond ((= (car list1) (car list2))
2899              (setq out (cons (car list1) out)
2900                    list1 (cdr list1)
2901                    list2 (cdr list2)))
2902             ((< (car list1) (car list2))
2903              (setq list1 (cdr list1)))
2904             (t
2905              (setq list2 (cdr list2)))))
2906     (nreverse out)))
2907
2908 (defun gnus-set-sorted-intersection (list1 list2)
2909   ;; LIST1 and LIST2 have to be sorted over <.
2910   ;; This function modifies LIST1.
2911   (let* ((top (cons nil list1))
2912          (prev top))
2913     (while (and list1 list2)
2914       (cond ((= (car list1) (car list2))
2915              (setq prev list1
2916                    list1 (cdr list1)
2917                    list2 (cdr list2)))
2918             ((< (car list1) (car list2))
2919              (setcdr prev (cdr list1))
2920              (setq list1 (cdr list1)))
2921             (t
2922              (setq list2 (cdr list2)))))
2923     (setcdr prev nil)
2924     (cdr top)))
2925
2926 (defun gnus-compress-sequence (numbers &optional always-list)
2927   "Convert list of numbers to a list of ranges or a single range.
2928 If ALWAYS-LIST is non-nil, this function will always release a list of
2929 ranges."
2930   (let* ((first (car numbers))
2931          (last (car numbers))
2932          result)
2933     (if (null numbers)
2934         nil
2935       (if (not (listp (cdr numbers)))
2936           numbers
2937         (while numbers
2938           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2939                 ((= (1+ last) (car numbers)) ;Still in sequence
2940                  (setq last (car numbers)))
2941                 (t                      ;End of one sequence
2942                  (setq result 
2943                        (cons (if (= first last) first
2944                                (cons first last)) result))
2945                  (setq first (car numbers))
2946                  (setq last  (car numbers))))
2947           (setq numbers (cdr numbers)))
2948         (if (and (not always-list) (null result))
2949             (if (= first last) (list first) (cons first last))
2950           (nreverse (cons (if (= first last) first (cons first last))
2951                           result)))))))
2952
2953 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2954 (defun gnus-uncompress-range (ranges)
2955   "Expand a list of ranges into a list of numbers.
2956 RANGES is either a single range on the form `(num . num)' or a list of
2957 these ranges."
2958   (let (first last result)
2959     (cond 
2960      ((null ranges)
2961       nil)
2962      ((not (listp (cdr ranges)))
2963       (setq first (car ranges))
2964       (setq last (cdr ranges))
2965       (while (<= first last)
2966         (setq result (cons first result))
2967         (setq first (1+ first)))
2968       (nreverse result))
2969      (t
2970       (while ranges
2971         (if (atom (car ranges))
2972             (if (numberp (car ranges))
2973                 (setq result (cons (car ranges) result)))
2974           (setq first (car (car ranges)))
2975           (setq last  (cdr (car ranges)))
2976           (while (<= first last)
2977             (setq result (cons first result))
2978             (setq first (1+ first))))
2979         (setq ranges (cdr ranges)))
2980       (nreverse result)))))
2981
2982 (defun gnus-add-to-range (ranges list)
2983   "Return a list of ranges that has all articles from both RANGES and LIST.
2984 Note: LIST has to be sorted over `<'."
2985   (if (not ranges)
2986       (gnus-compress-sequence list t)
2987     (setq list (copy-sequence list))
2988     (or (listp (cdr ranges))
2989         (setq ranges (list ranges)))
2990     (let ((out ranges)
2991           ilist lowest highest temp)
2992       (while (and ranges list)
2993         (setq ilist list)
2994         (setq lowest (or (and (atom (car ranges)) (car ranges))
2995                          (car (car ranges))))
2996         (while (and list (cdr list) (< (car (cdr list)) lowest))
2997           (setq list (cdr list)))
2998         (if (< (car ilist) lowest)
2999             (progn
3000               (setq temp list)
3001               (setq list (cdr list))
3002               (setcdr temp nil)
3003               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3004         (setq highest (or (and (atom (car ranges)) (car ranges))
3005                           (cdr (car ranges))))
3006         (while (and list (<= (car list) highest))
3007           (setq list (cdr list)))
3008         (setq ranges (cdr ranges)))
3009       (if list
3010           (setq out (nconc (gnus-compress-sequence list t) out)))
3011       (setq out (sort out (lambda (r1 r2) 
3012                             (< (or (and (atom r1) r1) (car r1))
3013                                (or (and (atom r2) r2) (car r2))))))
3014       (setq ranges out)
3015       (while ranges
3016         (if (atom (car ranges))
3017             (if (cdr ranges)
3018                 (if (atom (car (cdr ranges)))
3019                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3020                         (progn
3021                           (setcar ranges (cons (car ranges) 
3022                                                (car (cdr ranges))))
3023                           (setcdr ranges (cdr (cdr ranges)))))
3024                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3025                       (progn
3026                         (setcar (car (cdr ranges)) (car ranges))
3027                         (setcar ranges (car (cdr ranges)))
3028                         (setcdr ranges (cdr (cdr ranges)))))))
3029           (if (cdr ranges)
3030               (if (atom (car (cdr ranges)))
3031                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3032                       (progn
3033                         (setcdr (car ranges) (car (cdr ranges)))
3034                         (setcdr ranges (cdr (cdr ranges)))))
3035                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3036                     (progn
3037                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3038                       (setcdr ranges (cdr (cdr ranges))))))))
3039         (setq ranges (cdr ranges)))
3040       out)))
3041
3042 (defun gnus-remove-from-range (ranges list)
3043   "Return a list of ranges that has all articles from LIST removed from RANGES.
3044 Note: LIST has to be sorted over `<'."
3045   ;; !!! This function shouldn't look like this, but I've got a headache.
3046   (gnus-compress-sequence 
3047    (gnus-sorted-complement
3048     (gnus-uncompress-range ranges) list)))
3049
3050 (defun gnus-member-of-range (number ranges)
3051   (if (not (listp (cdr ranges)))
3052       (and (>= number (car ranges)) 
3053            (<= number (cdr ranges)))
3054     (let ((not-stop t))
3055       (while (and ranges 
3056                   (if (numberp (car ranges))
3057                       (>= number (car ranges))
3058                     (>= number (car (car ranges))))
3059                   not-stop)
3060         (if (if (numberp (car ranges))
3061                 (= number (car ranges))
3062               (and (>= number (car (car ranges)))
3063                    (<= number (cdr (car ranges)))))
3064             (setq not-stop nil))
3065         (setq ranges (cdr ranges)))
3066       (not not-stop))))
3067
3068 \f
3069 ;;;
3070 ;;; Gnus group mode
3071 ;;;
3072
3073 (defvar gnus-group-mode-map nil)
3074 (defvar gnus-group-group-map nil)
3075 (defvar gnus-group-mark-map nil)
3076 (defvar gnus-group-list-map nil)
3077 (defvar gnus-group-sub-map nil)
3078 (put 'gnus-group-mode 'mode-class 'special)
3079
3080 (if gnus-group-mode-map
3081     nil
3082   (setq gnus-group-mode-map (make-keymap))
3083   (suppress-keymap gnus-group-mode-map)
3084   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3085   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3086   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3087   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3088   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3089   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3090   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3091   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3092   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3093   (define-key gnus-group-mode-map
3094     "\M-n" 'gnus-group-next-unread-group-same-level)
3095   (define-key gnus-group-mode-map 
3096     "\M-p" 'gnus-group-prev-unread-group-same-level)
3097   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3098   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3099   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3100   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3101   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3102   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3103   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3104   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3105   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3106   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3107   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3108   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3109   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3110   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3111   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3112   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3113   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3114   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3115   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3116   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3117   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3118   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3119   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3120   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3121   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3122   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3123   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3124   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3125   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3126   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3127   (define-key gnus-group-mode-map "V" 'gnus-version)
3128   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3129   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3130   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3131   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3132   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3133   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3134   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3135   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3136   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3137   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3138   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3139   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3140   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3141   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3142   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3143
3144   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3145   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3146   (define-prefix-command 'gnus-group-mark-map)
3147   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3148   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3149   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3150   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3151
3152   (define-prefix-command 'gnus-group-group-map)
3153   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3154   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3155   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3156   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3157   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3158   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3159   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3160   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3161   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3162   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3163   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3164   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3165   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3166   ;;(define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3167   ;;(define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3168   ;;(define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3169   ;;(define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3170   ;;(define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3171
3172   (define-prefix-command 'gnus-group-list-map)
3173   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3174   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3175   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3176   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3177   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3178   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3179   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3180   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3181   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3182
3183   (define-prefix-command 'gnus-group-sub-map)
3184   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3185   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3186   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3187   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3188   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3189   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3190   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3191   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3192
3193 (defun gnus-group-mode ()
3194   "Major mode for reading news.
3195
3196 All normal editing commands are switched off.
3197 \\<gnus-group-mode-map>
3198 The group buffer lists (some of) the groups available.  For instance,
3199 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3200 lists all zombie groups. 
3201
3202 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3203 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3204
3205 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3206
3207 The following commands are available:
3208
3209 \\{gnus-group-mode-map}"
3210   (interactive)
3211   (if gnus-visual (gnus-group-make-menu-bar))
3212   (kill-all-local-variables)
3213   (setq mode-line-modified "-- ")
3214   (make-local-variable 'mode-line-format)
3215   (setq mode-line-format (copy-sequence mode-line-format))
3216   (and (equal (nth 3 mode-line-format) "   ")
3217        (setcar (nthcdr 3 mode-line-format) ""))
3218   (setq major-mode 'gnus-group-mode)
3219   (setq mode-name "Group")
3220   (gnus-group-set-mode-line)
3221   (setq mode-line-process nil)
3222   (use-local-map gnus-group-mode-map)
3223   (buffer-disable-undo (current-buffer))
3224   (setq truncate-lines t)
3225   (setq buffer-read-only t)
3226   (run-hooks 'gnus-group-mode-hook))
3227
3228 (defun gnus-mouse-pick-group (e)
3229   (interactive "e")
3230   (mouse-set-point e)
3231   (gnus-group-read-group nil))
3232
3233 ;; Look at LEVEL and find out what the level is really supposed to be.
3234 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3235 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3236 (defun gnus-group-default-level (&optional level number-or-nil)
3237   (cond  
3238    (gnus-group-use-permanent-levels
3239     (setq gnus-group-default-list-level 
3240           (or level gnus-group-default-list-level))
3241     (or gnus-group-default-list-level gnus-level-subscribed))
3242    (number-or-nil
3243     level)
3244    (t
3245     (or level gnus-group-default-list-level gnus-level-subscribed))))
3246   
3247
3248 (defvar gnus-tmp-prev-perm nil)
3249
3250 ;;;###autoload
3251 (defun gnus-no-server (&optional arg)
3252   "Read network news.
3253 If ARG is a positive number, Gnus will use that as the
3254 startup level. If ARG is nil, Gnus will be started at level 2. 
3255 If ARG is non-nil and not a positive number, Gnus will
3256 prompt the user for the name of an NNTP server to use.
3257 As opposed to `gnus', this command will not connect to the local server."
3258   (interactive "P")
3259   (let ((perm
3260          (cons gnus-group-use-permanent-levels gnus-group-default-list-level)))
3261     (setq gnus-tmp-prev-perm nil)
3262     (setq gnus-group-use-permanent-levels t)
3263     (gnus (or arg (1- gnus-level-default-subscribed)) t)
3264     (setq gnus-tmp-prev-perm perm)))
3265
3266 ;;;###autoload
3267 (defun gnus (&optional arg dont-connect)
3268   "Read network news.
3269 If ARG is non-nil and a positive number, Gnus will use that as the
3270 startup level. If ARG is non-nil and not a positive number, Gnus will
3271 prompt the user for the name of an NNTP server to use."
3272   (interactive "P")
3273   (if (get-buffer gnus-group-buffer)
3274       (progn
3275         (switch-to-buffer gnus-group-buffer)
3276         (gnus-group-get-new-news))
3277
3278     (gnus-clear-system)
3279
3280     (nnheader-init-server-buffer)
3281     ;; We do this if `gnus-no-server' has been run.
3282     (if gnus-tmp-prev-perm 
3283         (setq gnus-group-use-permanent-levels (car gnus-tmp-prev-perm)
3284               gnus-group-default-list-level (cdr gnus-tmp-prev-perm)
3285               gnus-tmp-prev-perm nil))
3286     (gnus-read-init-file)
3287
3288     (gnus-group-setup-buffer)
3289     (let ((buffer-read-only nil))
3290       (erase-buffer)
3291       (if (not gnus-inhibit-startup-message)
3292           (progn
3293             (gnus-group-startup-message)
3294             (sit-for 0))))
3295     
3296     (let ((level (and arg (numberp arg) (> arg 0) arg))
3297           did-connect)
3298       (unwind-protect
3299           (progn
3300             (or dont-connect 
3301                 (setq did-connect
3302                       (gnus-start-news-server (and arg (not level))))))
3303         (if (and (not dont-connect) 
3304                  (not did-connect))
3305             (gnus-group-quit)
3306           (run-hooks 'gnus-startup-hook)
3307           ;; NNTP server is successfully open. 
3308
3309           ;; Find the current startup file name.
3310           (setq gnus-current-startup-file 
3311                 (gnus-make-newsrc-file gnus-startup-file))
3312
3313           ;; Read the dribble file.
3314           (and gnus-use-dribble-file (gnus-dribble-read-file))
3315
3316           (gnus-summary-make-display-table)
3317           (gnus-setup-news nil level)
3318           (gnus-group-list-groups level)
3319           (gnus-configure-windows 'group))))))
3320
3321 (defun gnus-unload ()
3322   "Unload all Gnus features."
3323   (interactive)
3324   (or (boundp 'load-history)
3325       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
3326   (let ((history load-history)
3327         feature)
3328     (while history
3329       (and (string-match "^gnus" (car (car history)))
3330            (setq feature (cdr (assq 'provide (car history))))
3331            (unload-feature feature 'force))
3332       (setq history (cdr history)))))
3333
3334 (defun gnus-group-startup-message (&optional x y)
3335   "Insert startup message in current buffer."
3336   ;; Insert the message.
3337   (erase-buffer)
3338   (insert
3339    (format "
3340           _    ___ _             _      
3341           _ ___ __ ___  __    _ ___     
3342           __   _     ___    __  ___     
3343               _           ___     _     
3344              _  _ __             _      
3345              ___   __            _      
3346                    __           _       
3347                     _      _   _        
3348                    _      _    _        
3349                       _  _    _         
3350                   __  ___               
3351                  _   _ _     _          
3352                 _   _                   
3353               _    _                    
3354              _    _                     
3355             _                         
3356           __                             
3357
3358
3359       Gnus * A newsreader for Emacsen
3360     A Praxis release * larsi@ifi.uio.no
3361
3362            gnus-version))
3363   ;; And then hack it.
3364   ;; 18 is the longest line.
3365   (indent-rigidly (point-min) (point-max) 
3366                   (/ (max (- (window-width) (or x 46)) 0) 2))
3367   (goto-char (point-min))
3368   (let* ((pheight (count-lines (point-min) (point-max)))
3369          (wheight (window-height))
3370          (rest (- wheight  pheight)))
3371     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
3372     
3373     
3374
3375   ;; Fontify some.
3376   (goto-char (point-min))
3377   (search-forward "Praxis")
3378   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3379   (goto-char (point-min)))
3380
3381 (defun gnus-group-startup-message-old (&optional x y)
3382   "Insert startup message in current buffer."
3383   ;; Insert the message.
3384   (erase-buffer)
3385   (insert
3386    (format "
3387      %s
3388            A newsreader 
3389       for GNU Emacs
3390
3391         Based on GNUS 
3392              written by 
3393      Masanobu UMEDA
3394
3395        A Praxis Release
3396       larsi@ifi.uio.no
3397
3398            gnus-version))
3399   ;; And then hack it.
3400   ;; 18 is the longest line.
3401   (indent-rigidly (point-min) (point-max) 
3402                   (/ (max (- (window-width) (or x 28)) 0) 2))
3403   (goto-char (point-min))
3404   ;; +4 is fuzzy factor.
3405   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3406
3407   ;; Fontify some.
3408   (goto-char (point-min))
3409   (search-forward "Praxis")
3410   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3411   (goto-char (point-min)))
3412
3413 (defun gnus-group-setup-buffer ()
3414   (or (get-buffer gnus-group-buffer)
3415       (progn
3416         (switch-to-buffer gnus-group-buffer)
3417         (gnus-add-current-to-buffer-list)
3418         (gnus-group-mode)
3419         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3420
3421 (defun gnus-group-list-groups (&optional level unread)
3422   "List newsgroups with level LEVEL or lower that have unread articles.
3423 Default is all subscribed groups.
3424 If argument UNREAD is non-nil, groups with no unread articles are also
3425 listed." 
3426   (interactive (list (if current-prefix-arg
3427                          (prefix-numeric-value current-prefix-arg)
3428                        (or
3429                         (gnus-group-default-level nil t)
3430                         gnus-group-default-list-level
3431                         gnus-level-subscribed))))
3432   (or level
3433       (setq level (car gnus-group-list-mode)
3434             unread (cdr gnus-group-list-mode)))
3435   (setq level (gnus-group-default-level level))
3436   (gnus-group-setup-buffer)             ;May call from out of group buffer
3437   (let ((case-fold-search nil)
3438         (group (gnus-group-group-name)))
3439     (funcall gnus-group-prepare-function level unread nil)
3440     (if (zerop (buffer-size))
3441         (gnus-message 5 gnus-no-groups-message)
3442       (goto-char (point-min))
3443       (if (not group)
3444           ;; Go to the first group with unread articles.
3445           (gnus-group-search-forward nil nil nil t)
3446         ;; Find the right group to put point on. If the current group
3447         ;; has disapeared in the new listing, try to find the next
3448         ;; one. If no next one can be found, just leave point at the
3449         ;; first newsgroup in the buffer.
3450         (if (not (gnus-goto-char
3451                   (text-property-any (point-min) (point-max) 
3452                                      'gnus-group (intern group))))
3453             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3454               (while (and newsrc
3455                           (not (gnus-goto-char 
3456                                 (text-property-any 
3457                                  (point-min) (point-max) 'gnus-group 
3458                                  (intern (car (car newsrc)))))))
3459                 (setq newsrc (cdr newsrc)))
3460               (or newsrc (progn (goto-char (point-max))
3461                                 (forward-line -1))))))
3462       ;; Adjust cursor point.
3463       (gnus-group-position-cursor))))
3464
3465 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3466   "List all newsgroups with unread articles of level LEVEL or lower.
3467 If ALL is non-nil, list groups that have no unread articles.
3468 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3469 If REGEXP, only list groups matching REGEXP."
3470   (set-buffer gnus-group-buffer)
3471   (let ((buffer-read-only nil)
3472         (newsrc (cdr gnus-newsrc-alist))
3473         (lowest (or lowest 1))
3474         info clevel unread group)
3475     (erase-buffer)
3476     (if (< lowest gnus-level-zombie)
3477         ;; List living groups.
3478         (while newsrc
3479           (setq info (car newsrc)
3480                 group (car info)
3481                 newsrc (cdr newsrc)
3482                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3483           (and unread                   ; This group might be bogus
3484                (or (not regexp)
3485                    (string-match regexp group))
3486                (<= (setq clevel (car (cdr info))) level) 
3487                (>= clevel lowest)
3488                (or all                  ; We list all groups?
3489                    (eq unread t)        ; We list unactivated groups
3490                    (> unread 0)         ; We list groups with unread articles
3491                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3492                (gnus-group-insert-group-line 
3493                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3494
3495     ;; List dead groups.
3496     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3497          (gnus-group-prepare-flat-list-dead 
3498           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3499           gnus-level-zombie ?Z
3500           regexp))
3501     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3502          (gnus-group-prepare-flat-list-dead 
3503           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3504           gnus-level-killed ?K regexp))
3505
3506     (gnus-group-set-mode-line)
3507     (setq gnus-group-list-mode (cons level all))
3508     (run-hooks 'gnus-group-prepare-hook)))
3509
3510 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3511   ;; List zombies and killed lists somehwat faster, which was
3512   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3513   ;; this by ignoring the group format specification altogether.
3514   (let (group beg)
3515     (while groups
3516       (setq group (car groups)
3517             groups (cdr groups))
3518       (if (or (not regexp)
3519               (string-match regexp group))
3520           (progn
3521             (setq beg (point))
3522             (insert (format " %c     *: %s\n" mark group))
3523             (add-text-properties 
3524              beg (1+ beg) 
3525              (list 'gnus-group (intern group)
3526                    'gnus-unread t
3527                    'gnus-level level)))))))
3528
3529 (defun gnus-group-real-name (group)
3530   "Find the real name of a foreign newsgroup."
3531   (if (string-match ":[^:]+$" group)
3532       (substring group (1+ (match-beginning 0)))
3533     group))
3534
3535 (defun gnus-group-prefixed-name (group method)
3536   "Return the whole name from GROUP and METHOD."
3537   (and (stringp method) (setq method (gnus-server-to-method method)))
3538   (concat (format "%s" (car method))
3539           (if (and 
3540                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3541                (not (string= (nth 1 method) "")))
3542               (concat "+" (nth 1 method)))
3543           ":" group))
3544
3545 (defun gnus-group-real-prefix (group)
3546   "Return the prefix of the current group name."
3547   (if (string-match "^[^:]+:" group)
3548       (substring group 0 (match-end 0))
3549     ""))
3550
3551 (defun gnus-group-method-name (group)
3552   "Return the method used for selecting GROUP."
3553   (let ((prefix (gnus-group-real-prefix group)))
3554     (if (equal prefix "")
3555         gnus-select-method
3556       (if (string-match "^[^\\+]+\\+" prefix)
3557           (list (intern (substring prefix 0 (1- (match-end 0))))
3558                 (substring prefix (match-end 0) (1- (length prefix))))
3559         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3560
3561 (defun gnus-group-foreign-p (group)
3562   "Return nil if GROUP is native, non-nil if it is foreign."
3563   (string-match ":" group))
3564
3565 (defun gnus-group-set-info (info &optional method-only-group part)
3566   (let* ((entry (gnus-gethash
3567                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3568          (part-info info)
3569          (info (if method-only-group (nth 2 entry) info)))
3570     (if (not method-only-group)
3571         ()
3572       (or entry
3573           (error "Trying to change non-existent group %s" method-only-group))
3574       ;; We have recevied parts of the actual group info - either the
3575       ;; select method or the group parameters.  We first check
3576       ;; whether we have to extend the info, and if so, do that.
3577       (let ((len (length info))
3578             (total (if (eq part 'method) 5 6)))
3579         (and (< len total)
3580              (setcdr (nthcdr (1- len) info)
3581                      (make-list (- total len) nil)))
3582         ;; Then we enter the new info.
3583         (setcar (nthcdr (1- total) info) part-info)))
3584     ;; We uncompress some lists of marked articles.
3585     (let (marked)
3586       (if (not (setq marked (nth 3 info)))
3587           ()
3588         (while marked
3589           (or (eq 'score (car (car marked)))
3590               (eq 'bookmark (car (car marked)))
3591               (eq 'killed (car (car marked)))
3592               (setcdr (car marked) 
3593                       (gnus-uncompress-range (cdr (car marked)))))
3594           (setq marked (cdr marked)))))
3595     (if entry
3596         ()
3597       ;; This is a new group, so we just create it.
3598       (save-excursion
3599         (set-buffer gnus-group-buffer)
3600         (if (nth 4 info)
3601             ;; It's a foreign group...
3602             (gnus-group-make-group 
3603              (gnus-group-real-name (car info))
3604              (prin1-to-string (car (nth 4 info)))
3605              (nth 1 (nth 4 info)))
3606           ;; It's a native group.
3607           (gnus-group-make-group (car info)))
3608         (gnus-message 6 "Note: New group created")
3609         (setq entry 
3610               (gnus-gethash (gnus-group-prefixed-name 
3611                              (gnus-group-real-name (car info))
3612                              (or (nth 4 info) gnus-select-method))
3613                             gnus-newsrc-hashtb))))
3614     ;; Whether it was a new group or not, we now have the entry, so we
3615     ;; can do the update.
3616     (if entry
3617         (progn
3618           (setcar (nthcdr 2 entry) info)
3619           (if (and (not (eq (car entry) t)) 
3620                    (gnus-gethash (car info) gnus-active-hashtb))
3621               (let ((marked (nth 3 info)))
3622                 (setcar entry 
3623                         (max 0 (- (length (gnus-list-of-unread-articles 
3624                                            (car info)))
3625                                   (length (cdr (assq 'tick marked)))
3626                                   (length (cdr (assq 'dormant marked)))))))))
3627       (error "No such group: %s" (car info)))))
3628
3629 (defun gnus-group-set-method-info (group select-method)
3630   (gnus-group-set-info select-method group 'method))
3631
3632 (defun gnus-group-set-params-info (group params)
3633   (gnus-group-set-info params group 'params))
3634
3635 (defun gnus-group-update-group-line ()
3636   "This function updates the current line in the newsgroup buffer and
3637 moves the point to the colon."
3638   (let* ((buffer-read-only nil)
3639          (group (gnus-group-group-name))
3640          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3641     (if entry
3642         (gnus-dribble-enter 
3643          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3644                  ")")))
3645     (beginning-of-line)
3646     (delete-region (point) (progn (forward-line 1) (point)))
3647     (gnus-group-insert-group-line-info group)
3648     (forward-line -1)
3649     (gnus-group-position-cursor)))
3650
3651 (defun gnus-group-insert-group-line-info (group)
3652   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3653         active info)
3654     (if entry
3655         (progn
3656           (setq info (nth 2 entry))
3657           (gnus-group-insert-group-line 
3658            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3659       (setq active (gnus-gethash group gnus-active-hashtb))
3660       (gnus-group-insert-group-line 
3661        nil group 
3662        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3663        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3664
3665 (defun gnus-group-insert-group-line (gformat group level marked number method)
3666   (let* ((gformat (or gformat gnus-group-line-format-spec))
3667          (active (gnus-gethash group gnus-active-hashtb))
3668          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3669          (number-of-dormant (length (cdr (assq 'dormant marked))))
3670          (number-of-ticked (length (cdr (assq 'tick marked))))
3671          (number-of-ticked-and-dormant
3672           (+ number-of-ticked number-of-dormant))
3673          (number-of-unread-unticked 
3674           (if (numberp number) (int-to-string (max 0 number))
3675             "*"))
3676          (number-of-read
3677           (if (numberp number)
3678               (max 0 (- number-total number))
3679             "*"))
3680          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3681                            ((<= level gnus-level-unsubscribed) ?U)
3682                            ((= level gnus-level-zombie) ?Z)
3683                            (t ?K)))
3684          (qualified-group (gnus-group-real-name group))
3685          (newsgroup-description 
3686           (if gnus-description-hashtb
3687               (or (gnus-gethash group gnus-description-hashtb) "")
3688             ""))
3689          (moderated (if (member group gnus-moderated-list) ?m ? ))
3690          (moderated-string (if (eq moderated ?m) "(m)" ""))
3691          (method (gnus-server-get-method group method))
3692          (news-server (or (car (cdr method)) ""))
3693          (news-method (or (car method) ""))
3694          (news-method-string 
3695           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3696          (marked (if (and 
3697                       (numberp number) 
3698                       (zerop number)
3699                       (> number-of-ticked 0))
3700                      ?* ? ))
3701          (number (if (eq number t) "*" (+ number number-of-dormant 
3702                                           number-of-ticked)))
3703          (process-marked (if (member group gnus-group-marked)
3704                              gnus-process-mark ? ))
3705          (buffer-read-only nil)
3706          header                         ; passed as parameter to user-funcs.
3707          b)
3708     (beginning-of-line)
3709     (setq b (point))
3710     ;; Insert the text.
3711     (insert (eval gformat))
3712
3713     (add-text-properties 
3714      b (1+ b) (list 'gnus-group (intern group)
3715                     'gnus-unread (if (numberp number)
3716                                      (string-to-int number-of-unread-unticked)
3717                                    t)
3718                     'gnus-marked marked
3719                     'gnus-level level))))
3720
3721 (defun gnus-group-update-group (group &optional visible-only)
3722   "Update newsgroup info of GROUP.
3723 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3724   (save-excursion
3725     (set-buffer gnus-group-buffer)
3726     (let ((buffer-read-only nil)
3727           visible)
3728       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3729         (if entry
3730             (gnus-dribble-enter 
3731              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3732                      ")"))))
3733       ;; Buffer may be narrowed.
3734       (save-restriction
3735         (widen)
3736         ;; Search a line to modify.  If the buffer is large, the search
3737         ;; takes long time.  In most cases, current point is on the line
3738         ;; we are looking for.  So, first of all, check current line. 
3739         (if (or (progn
3740                   (beginning-of-line)
3741                   (eq (get-text-property (point) 'gnus-group)
3742                       (intern group)))
3743                 (progn
3744                   (gnus-goto-char 
3745                    (text-property-any 
3746                     (point-min) (point-max) 'gnus-group (intern group)))))
3747             ;; GROUP is listed in current buffer. So, delete old line.
3748             (progn
3749               (setq visible t)
3750               (beginning-of-line)
3751               (delete-region (point) (progn (forward-line 1) (point))))
3752           ;; No such line in the buffer, find out where it's supposed to
3753           ;; go, and insert it there (or at the end of the buffer).
3754           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3755           (or visible-only
3756               (let ((entry 
3757                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3758                 (while (and entry
3759                             (car entry)
3760                             (not
3761                              (gnus-goto-char
3762                               (text-property-any
3763                                (point-min) (point-max) 
3764                                'gnus-group (intern (car (car entry)))))))
3765                   (setq entry (cdr entry)))
3766                 (or entry (goto-char (point-max)))))))
3767       (if (or visible (not visible-only))
3768           (gnus-group-insert-group-line-info group))
3769       (gnus-group-set-mode-line))))
3770
3771 (defun gnus-group-set-mode-line ()
3772   (if (memq 'group gnus-updated-mode-lines)
3773       (let* ((gformat (or gnus-group-mode-line-format-spec
3774                           (setq gnus-group-mode-line-format-spec
3775                                 (gnus-parse-format 
3776                                  gnus-group-mode-line-format 
3777                                  gnus-group-mode-line-format-alist))))
3778              (news-server (car (cdr gnus-select-method)))
3779              (news-method (car gnus-select-method))
3780              (max-len 60)
3781              (mode-string (eval gformat)))
3782         (setq mode-string (eval gformat))
3783         (if (> (length mode-string) max-len) 
3784             (setq mode-string (substring mode-string 0 (- max-len 4))))
3785         (setq mode-line-buffer-identification mode-string)
3786         (set-buffer-modified-p t))))
3787
3788 (defun gnus-group-group-name ()
3789   "Get the name of the newsgroup on the current line."
3790   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3791     (and group (symbol-name group))))
3792
3793 (defun gnus-group-group-level ()
3794   "Get the level of the newsgroup on the current line."
3795   (get-text-property (gnus-point-at-bol) 'gnus-level))
3796
3797 (defun gnus-group-group-unread ()
3798   "Get the number of unread articles of the newsgroup on the current line."
3799   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3800
3801 (defun gnus-group-search-forward (&optional backward all level first-too)
3802   "Find the next newsgroup with unread articles.
3803 If BACKWARD is non-nil, find the previous newsgroup instead.
3804 If ALL is non-nil, just find any newsgroup.
3805 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3806 group exists.
3807 If FIRST-TOO, the current line is also eligible as a target."
3808   (let ((way (if backward -1 1))
3809         (low gnus-level-killed)
3810         (beg (point))
3811         pos found lev)
3812     (if (and backward (progn (beginning-of-line)) (bobp))
3813         nil
3814       (or first-too (forward-line way))
3815       (while (and 
3816               (not (eobp))
3817               (not (setq 
3818                     found 
3819                     (and (or all
3820                              (and
3821                               (let ((unread 
3822                                      (get-text-property (point) 'gnus-unread)))
3823                                 (or (eq unread t) (and unread (> unread 0))))
3824                               (setq lev (get-text-property (point)
3825                                                            'gnus-level))
3826                               (<= lev gnus-level-subscribed)))
3827                          (or (not level)
3828                              (and (setq lev (get-text-property (point)
3829                                                                'gnus-level))
3830                                   (or (= lev level)
3831                                       (and (< lev low)
3832                                            (< level lev)
3833                                            (progn
3834                                              (setq low lev)
3835                                              (setq pos (point))
3836                                              nil))))))))
3837               (zerop (forward-line way)))))
3838     (if found 
3839         (progn (gnus-group-position-cursor) t)
3840       (goto-char (or pos beg))
3841       (and pos t))))
3842
3843 ;;; Gnus group mode commands
3844
3845 ;; Group marking.
3846
3847 (defun gnus-group-mark-group (n &optional unmark no-advance)
3848   "Mark the current group."
3849   (interactive "p")
3850   (let ((buffer-read-only nil)
3851         group)
3852     (while 
3853         (and (> n 0) 
3854              (setq group (gnus-group-group-name))
3855              (progn
3856                (beginning-of-line)
3857                (forward-char 
3858                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3859                (delete-char 1)
3860                (if unmark
3861                    (progn
3862                      (insert " ")
3863                      (setq gnus-group-marked (delete group gnus-group-marked)))
3864                  (insert "#")
3865                  (setq gnus-group-marked
3866                        (cons group (delete group gnus-group-marked))))
3867                t)
3868              (or no-advance (zerop (gnus-group-next-group 1))))
3869       (setq n (1- n)))
3870     (gnus-summary-position-cursor)
3871     n))
3872
3873 (defun gnus-group-unmark-group (n)
3874   "Remove the mark from the current group."
3875   (interactive "p")
3876   (gnus-group-mark-group n 'unmark))
3877
3878 (defun gnus-group-mark-region (unmark beg end)
3879   "Mark all groups between point and mark.
3880 If UNMARK, remove the mark instead."
3881   (interactive "P\nr")
3882   (let ((num (count-lines beg end)))
3883     (save-excursion
3884       (goto-char beg)
3885       (- num (gnus-group-mark-group num unmark)))))
3886
3887 (defun gnus-group-remove-mark (group)
3888   (and (gnus-group-goto-group group)
3889        (save-excursion
3890          (gnus-group-mark-group 1 'unmark t))))
3891
3892 ;; Return a list of groups to work on.  Take into consideration N (the
3893 ;; prefix) and the list of marked groups.
3894 (defun gnus-group-process-prefix (n)
3895   (cond (n
3896          (setq n (prefix-numeric-value n))
3897          ;; There is a prefix, so we return a list of the N next
3898          ;; groups. 
3899          (let ((way (if (< n 0) -1 1))
3900                (n (abs n))
3901                group groups)
3902            (save-excursion
3903              (while (and (> n 0)
3904                          (setq group (gnus-group-group-name)))
3905                (setq groups (cons group groups))
3906                (setq n (1- n))
3907                (forward-line way)))
3908            (nreverse groups)))
3909         (gnus-group-marked
3910          ;; No prefix, but a list of marked articles.
3911          (reverse gnus-group-marked))
3912         (t
3913          ;; Neither marked articles or a prefix, so we return the
3914          ;; current group.
3915          (let ((group (gnus-group-group-name)))
3916            (and group (list group))))))
3917
3918 ;; Selecting groups.
3919
3920 (defun gnus-group-read-group (&optional all no-article group)
3921   "Read news in this newsgroup.
3922 If the prefix argument ALL is non-nil, already read articles become
3923 readable. If the optional argument NO-ARTICLE is non-nil, no article
3924 will be auto-selected upon group entry."
3925   (interactive "P")
3926   (let ((group (or group (gnus-group-group-name)))
3927         number active marked entry)
3928     (or group (error "No group on current line"))
3929     (setq marked 
3930           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3931     ;; This group might be a dead group. In that case we have to get
3932     ;; the number of unread articles from `gnus-active-hashtb'.
3933     (if entry
3934         (setq number (car entry))
3935       (if (setq active (gnus-gethash group gnus-active-hashtb))
3936           (setq number (- (1+ (cdr active)) (car active)))))
3937     (gnus-summary-read-group 
3938      group (or all (and (numberp number) 
3939                         (zerop (+ number (length (cdr (assq 'tick marked)))
3940                                   (length (cdr (assq 'dormant marked)))))))
3941      no-article)))
3942
3943 (defun gnus-group-select-group (&optional all)
3944   "Select this newsgroup.
3945 No article is selected automatically.
3946 If argument ALL is non-nil, already read articles become readable."
3947   (interactive "P")
3948   (gnus-group-read-group all t))
3949
3950 (defun gnus-group-select-group-all ()
3951   "Select the current group and display all articles in it."
3952   (interactive)
3953   (gnus-group-select-group 'all))
3954
3955 ;; Enter a group that is not in the group buffer. Non-nil is returned
3956 ;; if selection was successful.
3957 (defun gnus-group-read-ephemeral-group 
3958   (group method &optional activate quit-config)
3959   (let ((group (if (gnus-group-foreign-p group) group
3960                  (gnus-group-prefixed-name group method))))
3961     (gnus-sethash 
3962      group
3963      (list t nil (list group gnus-level-default-subscribed nil nil 
3964                        (append method
3965                                (list
3966                                 (list 'quit-config 
3967                                       (if quit-config quit-config
3968                                         (cons (current-buffer) 'summary)))))))
3969      gnus-newsrc-hashtb)
3970     (set-buffer gnus-group-buffer)
3971     (or (gnus-check-server method)
3972         (error "Unable to contact server: %s" (gnus-status-message method)))
3973     (if activate (or (gnus-request-group group)
3974                      (error "Couldn't request group")))
3975     (condition-case ()
3976         (gnus-group-read-group t t group)
3977       (error nil)
3978       (quit nil))
3979     (not (equal major-mode 'gnus-group-mode))))
3980   
3981 (defun gnus-group-jump-to-group (group)
3982   "Jump to newsgroup GROUP."
3983   (interactive 
3984    (list (completing-read 
3985           "Group: " gnus-active-hashtb nil 
3986           (memq gnus-select-method gnus-have-read-active-file))))
3987
3988   (if (equal group "")
3989       (error "Empty group name"))
3990
3991   (let ((b (text-property-any 
3992             (point-min) (point-max) 'gnus-group (intern group))))
3993     (if b
3994         ;; Either go to the line in the group buffer...
3995         (goto-char b)
3996       ;; ... or insert the line.
3997       (or
3998        (gnus-gethash group gnus-active-hashtb)
3999        (gnus-activate-group group)
4000        (error "%s error: %s" group (gnus-status-message group)))
4001
4002       (gnus-group-update-group group)
4003       (goto-char (text-property-any 
4004                   (point-min) (point-max) 'gnus-group (intern group)))))
4005   ;; Adjust cursor point.
4006   (gnus-group-position-cursor))
4007
4008 (defun gnus-group-goto-group (group)
4009   "Goto to newsgroup GROUP."
4010   (let ((b (text-property-any (point-min) (point-max) 
4011                               'gnus-group (intern group))))
4012     (and b (goto-char b))))
4013
4014 (defun gnus-group-next-group (n)
4015   "Go to next N'th newsgroup.
4016 If N is negative, search backward instead.
4017 Returns the difference between N and the number of skips actually
4018 done."
4019   (interactive "p")
4020   (gnus-group-next-unread-group n t))
4021
4022 (defun gnus-group-next-unread-group (n &optional all level)
4023   "Go to next N'th unread newsgroup.
4024 If N is negative, search backward instead.
4025 If ALL is non-nil, choose any newsgroup, unread or not.
4026 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4027 such group can be found, the next group with a level higher than
4028 LEVEL.
4029 Returns the difference between N and the number of skips actually
4030 made."
4031   (interactive "p")
4032   (let ((backward (< n 0))
4033         (n (abs n)))
4034     (while (and (> n 0)
4035                 (gnus-group-search-forward 
4036                  backward (or (not gnus-group-goto-unread) all) level))
4037       (setq n (1- n)))
4038     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4039                                (if level " on this level or higher" "")))
4040     n))
4041
4042 (defun gnus-group-prev-group (n)
4043   "Go to previous N'th newsgroup.
4044 Returns the difference between N and the number of skips actually
4045 done."
4046   (interactive "p")
4047   (gnus-group-next-unread-group (- n) t))
4048
4049 (defun gnus-group-prev-unread-group (n)
4050   "Go to previous N'th unread newsgroup.
4051 Returns the difference between N and the number of skips actually
4052 done."  
4053   (interactive "p")
4054   (gnus-group-next-unread-group (- n)))
4055
4056 (defun gnus-group-next-unread-group-same-level (n)
4057   "Go to next N'th unread newsgroup on the same level.
4058 If N is negative, search backward instead.
4059 Returns the difference between N and the number of skips actually
4060 done."
4061   (interactive "p")
4062   (gnus-group-next-unread-group n t (gnus-group-group-level))
4063   (gnus-group-position-cursor))
4064
4065 (defun gnus-group-prev-unread-group-same-level (n)
4066   "Go to next N'th unread newsgroup on the same level.
4067 Returns the difference between N and the number of skips actually
4068 done."
4069   (interactive "p")
4070   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
4071   (gnus-group-position-cursor))
4072
4073 (defun gnus-group-best-unread-group (&optional exclude-group)
4074   "Go to the group with the highest level.
4075 If EXCLUDE-GROUP, do not go to that group."
4076   (interactive)
4077   (goto-char (point-min))
4078   (let ((best 100000)
4079         unread best-point)
4080     (while (setq unread (get-text-property (point) 'gnus-unread))
4081       (if (and (numberp unread) (> unread 0))
4082           (progn
4083             (if (and (< (get-text-property (point) 'gnus-level) best)
4084                      (or (not exclude-group)
4085                          (not (equal exclude-group (gnus-group-group-name)))))
4086                 (progn 
4087                   (setq best (get-text-property (point) 'gnus-level))
4088                   (setq best-point (point))))))
4089       (forward-line 1))
4090     (if best-point (goto-char best-point))
4091     (gnus-summary-position-cursor)
4092     (and best-point (gnus-group-group-name))))
4093
4094 (defun gnus-group-first-unread-group ()
4095   "Go to the first group with unread articles."
4096   (interactive)
4097   (prog1
4098       (let ((opoint (point))
4099             unread)
4100         (goto-char (point-min))
4101         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4102                 (not (zerop unread))    ; Has unread articles.
4103                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4104             (point)                     ; Success.
4105           (goto-char opoint)
4106           nil))                         ; Not success.
4107     (gnus-group-position-cursor)))
4108
4109 (defun gnus-group-enter-server-mode ()
4110   "Jump to the server buffer."
4111   (interactive)
4112   (gnus-server-setup-buffer)
4113   (gnus-configure-windows 'server)
4114   (gnus-server-prepare))
4115
4116 (defun gnus-group-make-group (name &optional method address)
4117   "Add a new newsgroup.
4118 The user will be prompted for a NAME, for a select METHOD, and an
4119 ADDRESS."
4120   (interactive
4121    (cons 
4122     (read-string "Group name: ")
4123     (let ((method
4124            (completing-read 
4125             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4126             nil t)))
4127       (if (assoc method gnus-valid-select-methods)
4128           (list method
4129                 (if (memq 'prompt-address
4130                           (assoc method gnus-valid-select-methods))
4131                     (read-string "Address: ")
4132                   ""))
4133         (list method nil)))))
4134   
4135   (let* ((meth (and method (if address (list (intern method) address) method)))
4136          (nname (if method (gnus-group-prefixed-name name meth) name))
4137          info)
4138     (and (gnus-gethash nname gnus-newsrc-hashtb)
4139          (error "Group %s already exists" nname))
4140     (gnus-group-change-level 
4141      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4142      gnus-level-default-subscribed gnus-level-killed 
4143      (and (gnus-group-group-name)
4144           (gnus-gethash (gnus-group-group-name)
4145                         gnus-newsrc-hashtb))
4146      t)
4147     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4148     (gnus-dribble-enter 
4149      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4150     (gnus-group-insert-group-line-info nname)
4151
4152     (if (assoc method gnus-valid-select-methods)
4153         (require (intern method)))
4154     (and (gnus-check-backend-function 'request-create-group nname)
4155          (gnus-request-create-group nname))))
4156
4157 (defun gnus-group-edit-group (group &optional part)
4158   "Edit the group on the current line."
4159   (interactive (list (gnus-group-group-name)))
4160   (let ((done-func '(lambda () 
4161                       "Exit editing mode and update the information."
4162                       (interactive)
4163                       (gnus-group-edit-group-done 'part 'group)))
4164         (part (or part 'info))
4165         (winconf (current-window-configuration))
4166         info)
4167     (or group (error "No group on current line"))
4168     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4169         (error "Killed group; can't be edited"))
4170     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4171     (gnus-configure-windows 'edit-group)
4172     (gnus-add-current-to-buffer-list)
4173     (emacs-lisp-mode)
4174     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4175     (use-local-map (copy-keymap emacs-lisp-mode-map))
4176     (local-set-key "\C-c\C-c" done-func)
4177     (make-local-variable 'gnus-prev-winconf)
4178     (setq gnus-prev-winconf winconf)
4179     ;; We modify the func to let it know what part it is editing.
4180     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4181     (setcar (cdr (cdr (nth 4 done-func))) group)
4182     (erase-buffer)
4183     (insert
4184      (cond 
4185       ((eq part 'method)
4186        ";; Type `C-c C-c' after editing the select method.\n\n")
4187       ((eq part 'params)
4188        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4189       ((eq part 'info)
4190        ";; Type `C-c C-c' after editing the group info.\n\n")))
4191     (let ((cinfo (gnus-copy-sequence info))
4192           marked)
4193       (if (not (setq marked (nth 3 cinfo)))
4194           ()
4195         (while marked
4196           (or (eq 'score (car (car marked)))
4197               (eq 'bookmark (car (car marked)))
4198               (eq 'killed (car (car marked)))
4199               (not (numberp (car (cdr (car marked)))))
4200               (setcdr (car marked) 
4201                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4202           (setq marked (cdr marked))))
4203       (insert 
4204        (pp-to-string
4205         (cond ((eq part 'method)
4206                (or (nth 4 info) "native"))
4207               ((eq part 'params)
4208                (nth 5 info))
4209               (t
4210                cinfo)))
4211        "\n"))))
4212
4213 (defun gnus-group-edit-group-method (group)
4214   "Edit the select method of GROUP."
4215   (interactive (list (gnus-group-group-name)))
4216   (gnus-group-edit-group group 'method))
4217
4218 (defun gnus-group-edit-group-parameters (group)
4219   "Edit the group parameters of GROUP."
4220   (interactive (list (gnus-group-group-name)))
4221   (gnus-group-edit-group group 'params))
4222
4223 (defun gnus-group-edit-group-done (part group)
4224   "Get info from buffer, update variables and jump to the group buffer."
4225   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4226   (goto-char (point-min))
4227   (let ((form (read (current-buffer)))
4228         (winconf gnus-prev-winconf))
4229     (if (eq part 'info) 
4230         (gnus-group-set-info form)
4231       (gnus-group-set-info form group part))
4232     (kill-buffer (current-buffer))
4233     (and winconf (set-window-configuration winconf))
4234     (set-buffer gnus-group-buffer)
4235     (gnus-group-update-group (gnus-group-group-name))
4236     (gnus-group-position-cursor)))
4237
4238 (defun gnus-group-make-help-group ()
4239   "Create the Gnus documentation group."
4240   (interactive)
4241   (let ((path load-path)
4242         name)
4243     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4244                                    "gnus-help" '(nndoc "gnus-help")))
4245                        gnus-newsrc-hashtb)
4246          (error "Documentation group already exists"))
4247     (while (and path
4248                 (not (file-exists-p (concat (file-name-as-directory (car path))
4249                                             "doc.txt"))))
4250       (setq path (cdr path)))
4251     (if (not path)
4252         (message "Couldn't find doc group")
4253       (gnus-group-make-group 
4254        (gnus-group-real-name name)
4255        (list 'nndoc name
4256              (list 'nndoc-address 
4257                    (concat (file-name-as-directory (car path)) "doc.txt"))
4258              (list 'nndoc-article-type 'mbox)))))
4259   (gnus-group-position-cursor))
4260
4261 (defun gnus-group-make-doc-group (file type)
4262   "Create a group that uses a single file as the source."
4263   (interactive 
4264    (list (read-file-name "File name: ") 
4265          (let ((err "")
4266                found char)
4267            (while (not found)
4268              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4269              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4270                                ((= char ?b) 'babyl)
4271                                ((= char ?d) 'digest)
4272                                (t (setq err (format "%c unknown. " char))
4273                                   nil))))
4274            found)))
4275   (let* ((file (expand-file-name file))
4276          (name (gnus-generate-new-group-name
4277                 (gnus-group-prefixed-name
4278                  (file-name-nondirectory file) '(nndoc "")))))
4279     (gnus-group-make-group 
4280      (gnus-group-real-name name)
4281      (list 'nndoc name
4282            (list 'nndoc-address file)
4283            (list 'nndoc-article-type type)))))
4284
4285 (defun gnus-group-make-archive-group (&optional all)
4286   "Create the (ding) Gnus archive group of the most recent articles.
4287 Given a prefix, create a full group."
4288   (interactive "P")
4289   (let ((group (gnus-group-prefixed-name 
4290                 (if all "ding.archives" "ding.recent") '(nndir ""))))
4291     (and (gnus-gethash group gnus-newsrc-hashtb)
4292          (error "Archive group already exists"))
4293     (gnus-group-make-group
4294      (gnus-group-real-name group)
4295      "nndir" 
4296      (if all gnus-group-archive-directory 
4297        gnus-group-recent-archive-directory)))
4298   (gnus-group-position-cursor))
4299
4300 (defun gnus-group-make-directory-group (dir)
4301   "Create an nndir group.
4302 The user will be prompted for a directory. The contents of this
4303 directory will be used as a newsgroup. The directory should contain
4304 mail messages or news articles in files that have numeric names."
4305   (interactive
4306    (list (read-file-name "Create group from directory: ")))
4307   (or (file-exists-p dir) (error "No such directory"))
4308   (or (file-directory-p dir) (error "Not a directory"))
4309   (gnus-group-make-group dir "nndir" dir)
4310   (gnus-group-position-cursor))
4311
4312 (defun gnus-group-make-kiboze-group (group address scores)
4313   "Create an nnkiboze group.
4314 The user will be prompted for a name, a regexp to match groups, and
4315 score file entries for articles to include in the group."
4316   (interactive
4317    (list
4318     (read-string "nnkiboze group name: ")
4319     (read-string "Source groups (regexp): ")
4320     (let ((headers (mapcar (lambda (group) (list group))
4321                            '("subject" "from" "number" "date" "message-id"
4322                              "references" "chars" "lines" "xref")))
4323           scores header regexp regexps)
4324       (while (not (equal "" (setq header (completing-read 
4325                                           "Match on header: " headers nil t))))
4326         (setq regexps nil)
4327         (while (not (equal "" (setq regexp (read-string 
4328                                             (format "Match on %s (string): "
4329                                                     header)))))
4330           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4331         (setq scores (cons (cons header regexps) scores)))
4332       scores)))
4333   (gnus-group-make-group group "nnkiboze" address)
4334   (save-excursion
4335     (gnus-set-work-buffer)
4336     (let (emacs-lisp-mode-hook)
4337       (pp scores (current-buffer)))
4338     (write-region (point-min) (point-max) 
4339                   (concat (or gnus-kill-files-directory "~/News")
4340                           "nnkiboze:" group "." gnus-score-file-suffix)))
4341   (gnus-group-position-cursor))
4342
4343 (defun gnus-group-add-to-virtual (n vgroup)
4344   "Add the current group to a virtual group."
4345   (interactive
4346    (list current-prefix-arg
4347          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4348                           "nnvirtual:")))
4349   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4350       (error "%s is not an nnvirtual group" vgroup))
4351   (let* ((groups (gnus-group-process-prefix n))
4352          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4353     (setcar (cdr method)
4354             (concat 
4355              (nth 1 method) "\\|"
4356              (mapconcat 
4357               (lambda (s) 
4358                 (gnus-group-remove-mark s)
4359                 (concat "\\(^" (regexp-quote s) "$\\)"))
4360               groups "\\|"))))
4361   (gnus-group-position-cursor))
4362
4363 (defun gnus-group-make-empty-virtual (group)
4364   "Create a new, fresh, empty virtual group."
4365   (interactive "sCreate new, empty virtual group: ")
4366   (let* ((method (list 'nnvirtual "^$"))
4367          (pgroup (gnus-group-prefixed-name group method)))
4368     ;; Check whether it exists already.
4369     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4370          (error "Group %s already exists." pgroup))
4371     ;; Subscribe the new group after the group on the current line.
4372     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4373     (gnus-group-update-group pgroup)
4374     (forward-line -1)
4375     (gnus-group-position-cursor)))
4376
4377 (defun gnus-group-enter-directory (dir)
4378   "Enter an ephemeral nneething group."
4379   (interactive "DDirectory to read: ")
4380   (let* ((method (list 'nneething dir))
4381          (leaf (gnus-group-prefixed-name
4382                 (file-name-nondirectory (directory-file-name dir))
4383                 method))
4384          (name (gnus-generate-new-group-name leaf)))
4385     (let ((nneething-read-only t))
4386       (or (gnus-group-read-ephemeral-group 
4387            name method t
4388            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4389                                       'summary 'group)))
4390           (error "Couldn't enter %s" dir)))))
4391
4392 ;; Group sorting commands
4393 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4394
4395 (defun gnus-group-sort-groups ()
4396   "Sort the group buffer using `gnus-group-sort-function'."
4397   (interactive)
4398   (setq gnus-newsrc-alist 
4399         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4400   (gnus-make-hashtable-from-newsrc-alist)
4401   (gnus-group-list-groups))
4402
4403 (defun gnus-group-sort-by-alphabet (info1 info2)
4404   (string< (car info1) (car info2)))
4405
4406 (defun gnus-group-sort-by-unread (info1 info2)
4407   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4408         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4409     (< (or (and (numberp n1) n1) 0)
4410        (or (and (numberp n2) n2) 0))))
4411
4412 (defun gnus-group-sort-by-level (info1 info2)
4413   (< (nth 1 info1) (nth 1 info2)))
4414
4415 ;; Group catching up.
4416
4417 (defun gnus-group-catchup-current (&optional n all)
4418   "Mark all articles not marked as unread in current newsgroup as read.
4419 If prefix argument N is numeric, the ARG next newsgroups will be
4420 caught up. If ALL is non-nil, marked articles will also be marked as
4421 read. Cross references (Xref: header) of articles are ignored.
4422 The difference between N and actual number of newsgroups that were
4423 caught up is returned."
4424   (interactive "P")
4425   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4426                gnus-expert-user
4427                (gnus-y-or-n-p
4428                 (if all
4429                     "Do you really want to mark all articles as read? "
4430                   "Mark all unread articles as read? "))))
4431       n
4432     (let ((groups (gnus-group-process-prefix n))
4433           (ret 0))
4434       (while groups
4435         ;; Virtual groups have to be given special treatment. 
4436         (let ((method (gnus-find-method-for-group (car groups))))
4437           (if (eq 'nnvirtual (car method))
4438               (nnvirtual-catchup-group
4439                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4440         (gnus-group-remove-mark (car groups))
4441         (if (prog1
4442                 (gnus-group-goto-group (car groups))
4443               (gnus-group-catchup (car groups) all))
4444             (gnus-group-update-group-line)
4445           (setq ret (1+ ret)))
4446         (setq groups (cdr groups)))
4447       (gnus-group-next-unread-group 1)
4448       ret)))
4449
4450 (defun gnus-group-catchup-current-all (&optional n)
4451   "Mark all articles in current newsgroup as read.
4452 Cross references (Xref: header) of articles are ignored."
4453   (interactive "P")
4454   (gnus-group-catchup-current n 'all))
4455
4456 (defun gnus-group-catchup (group &optional all)
4457   "Mark all articles in GROUP as read.
4458 If ALL is non-nil, all articles are marked as read.
4459 The return value is the number of articles that were marked as read,
4460 or nil if no action could be taken."
4461   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4462          (num (car entry))
4463          (marked (nth 3 (nth 2 entry))))
4464     (if (not (numberp (car entry)))
4465         (gnus-message 1 "Can't catch up; non-active group")
4466       ;; Do the updating only if the newsgroup isn't killed.
4467       (if (not entry)
4468           ()
4469         (gnus-update-read-articles 
4470          group (and (not all) (append (cdr (assq 'tick marked))
4471                                       (cdr (assq 'dormant marked))))
4472          nil (and (not all) (cdr (assq 'tick marked))))
4473         (and all 
4474              (setq marked (nth 3 (nth 2 entry)))
4475              (setcar (nthcdr 3 (nth 2 entry)) 
4476                      (delq (assq 'dormant marked) 
4477                            (nth 3 (nth 2 entry)))))))
4478     num))
4479
4480 (defun gnus-group-expire-articles (&optional n)
4481   "Expire all expirable articles in the current newsgroup."
4482   (interactive "P")
4483   (let ((groups (gnus-group-process-prefix n))
4484         group)
4485     (or groups (error "No groups to expire"))
4486     (while groups
4487       (setq group (car groups)
4488             groups (cdr groups))
4489       (gnus-group-remove-mark group)
4490       (if (not (gnus-check-backend-function 'request-expire-articles group))
4491           ()
4492         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4493                (expirable (if (memq 'total-expire (nth 5 info))
4494                               (cons nil (gnus-list-of-read-articles group))
4495                             (assq 'expire (nth 3 info)))))
4496           (and expirable 
4497                (setcdr expirable
4498                        (gnus-request-expire-articles 
4499                         (cdr expirable) group))))))))
4500
4501 (defun gnus-group-expire-all-groups ()
4502   "Expire all expirable articles in all newsgroups."
4503   (interactive)
4504   (save-excursion
4505     (gnus-message 5 "Expiring...")
4506     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4507                                      (cdr gnus-newsrc-alist))))
4508       (gnus-group-expire-articles nil)))
4509   (gnus-group-position-cursor)
4510   (gnus-message 5 "Expiring...done"))
4511
4512 (defun gnus-group-set-current-level (n level)
4513   "Set the level of the next N groups to LEVEL."
4514   (interactive "P\nnLevel: ")
4515   (or (and (>= level 1) (<= level gnus-level-killed))
4516       (error "Illegal level: %d" level))
4517   (let ((groups (gnus-group-process-prefix n))
4518         group)
4519     (while groups
4520       (setq group (car groups)
4521             groups (cdr groups))
4522       (gnus-group-remove-mark group)
4523       (gnus-message 6 "Changed level of %s from %d to %d" 
4524                     group (gnus-group-group-level) level)
4525       (gnus-group-change-level group level
4526                                (gnus-group-group-level))
4527       (gnus-group-update-group-line)))
4528   (gnus-group-position-cursor))
4529
4530 (defun gnus-group-unsubscribe-current-group (&optional n)
4531   "Toggle subscription of the current group.
4532 If given numerical prefix, toggle the N next groups."
4533   (interactive "P")
4534   (let ((groups (gnus-group-process-prefix n))
4535         group)
4536     (while groups
4537       (setq group (car groups)
4538             groups (cdr groups))
4539       (gnus-group-remove-mark group)
4540       (gnus-group-unsubscribe-group
4541        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4542                  gnus-level-default-unsubscribed
4543                gnus-level-default-subscribed))
4544       (gnus-group-update-group-line))
4545     (gnus-group-next-group 1)))
4546
4547 (defun gnus-group-unsubscribe-group (group &optional level)
4548   "Toggle subscribe from/to unsubscribe GROUP.
4549 New newsgroup is added to .newsrc automatically."
4550   (interactive
4551    (list (completing-read
4552           "Group: " gnus-active-hashtb nil 
4553           (memq gnus-select-method gnus-have-read-active-file))))
4554   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4555     (cond
4556      ((string-match "^[ \t]$" group)
4557       (error "Empty group name"))
4558      (newsrc
4559       ;; Toggle subscription flag.
4560       (gnus-group-change-level 
4561        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4562                                       gnus-level-subscribed) 
4563                                   (1+ gnus-level-subscribed)
4564                                 gnus-level-default-subscribed)))
4565       (gnus-group-update-group group))
4566      ((and (stringp group)
4567            (or (not (memq gnus-select-method gnus-have-read-active-file))
4568                (gnus-gethash group gnus-active-hashtb)))
4569       ;; Add new newsgroup.
4570       (gnus-group-change-level 
4571        group 
4572        (if level level gnus-level-default-subscribed) 
4573        (or (and (member group gnus-zombie-list) 
4574                 gnus-level-zombie) 
4575            gnus-level-killed)
4576        (and (gnus-group-group-name)
4577             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4578       (gnus-group-update-group group))
4579      (t (error "No such newsgroup: %s" group)))
4580     (gnus-group-position-cursor)))
4581
4582 (defun gnus-group-transpose-groups (n)
4583   "Move the current newsgroup up N places.
4584 If given a negative prefix, move down instead. The difference between
4585 N and the number of steps taken is returned." 
4586   (interactive "p")
4587   (or (gnus-group-group-name)
4588       (error "No group on current line"))
4589   (gnus-group-kill-group 1)
4590   (prog1
4591       (forward-line (- n))
4592     (gnus-group-yank-group)
4593     (gnus-group-position-cursor)))
4594
4595 (defun gnus-group-kill-all-zombies ()
4596   "Kill all zombie newsgroups."
4597   (interactive)
4598   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4599   (setq gnus-zombie-list nil)
4600   (gnus-group-list-groups))
4601
4602 (defun gnus-group-kill-region (begin end)
4603   "Kill newsgroups in current region (excluding current point).
4604 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4605   (interactive "r")
4606   (let ((lines
4607          ;; Count lines.
4608          (save-excursion
4609            (count-lines
4610             (progn
4611               (goto-char begin)
4612               (beginning-of-line)
4613               (point))
4614             (progn
4615               (goto-char end)
4616               (beginning-of-line)
4617               (point))))))
4618     (goto-char begin)
4619     (beginning-of-line)                 ;Important when LINES < 1
4620     (gnus-group-kill-group lines)))
4621
4622 (defun gnus-group-kill-group (&optional n)
4623   "The the next N groups.
4624 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4625 However, only groups that were alive can be yanked; already killed 
4626 groups or zombie groups can't be yanked.
4627 The return value is the name of the (last) group that was killed."
4628   (interactive "P")
4629   (let ((buffer-read-only nil)
4630         (groups (gnus-group-process-prefix n))
4631         group entry level)
4632     (while groups
4633       (setq group (car groups)
4634             groups (cdr groups))
4635       (gnus-group-remove-mark group)
4636       (setq level (gnus-group-group-level))
4637       (gnus-delete-line)
4638       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4639           (setq gnus-list-of-killed-groups 
4640                 (cons (cons (car entry) (nth 2 entry)) 
4641                       gnus-list-of-killed-groups)))
4642       (gnus-group-change-level 
4643        (if entry entry group) gnus-level-killed (if entry nil level)))
4644     (gnus-group-position-cursor)
4645     group))
4646
4647 (defun gnus-group-yank-group (&optional arg)
4648   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4649 inserting it before the current newsgroup.  The numeric ARG specifies
4650 how many newsgroups are to be yanked.  The name of the (last)
4651 newsgroup yanked is returned."
4652   (interactive "p")
4653   (if (not arg) (setq arg 1))
4654   (let (info group prev)
4655     (while (>= (setq arg (1- arg)) 0)
4656       (if (not (setq info (car gnus-list-of-killed-groups)))
4657           (error "No more newsgroups to yank"))
4658       (setq group (nth 2 info))
4659       ;; Find which newsgroup to insert this one before - search
4660       ;; backward until something suitable is found. If there are no
4661       ;; other newsgroups in this buffer, just make this newsgroup the
4662       ;; first newsgroup.
4663       (setq prev (gnus-group-group-name))
4664       (gnus-group-change-level 
4665        info (nth 2 info) gnus-level-killed 
4666        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4667        t)
4668       (gnus-group-insert-group-line-info (nth 1 info))
4669       (setq gnus-list-of-killed-groups 
4670             (cdr gnus-list-of-killed-groups)))
4671     (forward-line -1)
4672     (gnus-group-position-cursor)
4673     group))
4674       
4675 (defun gnus-group-list-all-groups (&optional arg)
4676   "List all newsgroups with level ARG or lower.
4677 Default is gnus-level-unsubscribed, which lists all subscribed and most
4678 unsubscribed groups."
4679   (interactive "P")
4680   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4681
4682 (defun gnus-group-list-killed ()
4683   "List all killed newsgroups in the group buffer."
4684   (interactive)
4685   (if (not gnus-killed-list)
4686       (gnus-message 6 "No killed groups")
4687     (let (gnus-group-list-mode)
4688       (funcall gnus-group-prepare-function 
4689                gnus-level-killed t gnus-level-killed))
4690     (goto-char (point-min)))
4691   (gnus-group-position-cursor))
4692
4693 (defun gnus-group-list-zombies ()
4694   "List all zombie newsgroups in the group buffer."
4695   (interactive)
4696   (if (not gnus-zombie-list)
4697       (gnus-message 6 "No zombie groups")
4698     (let (gnus-group-list-mode)
4699       (funcall gnus-group-prepare-function
4700                gnus-level-zombie t gnus-level-zombie))
4701     (goto-char (point-min)))
4702   (gnus-group-position-cursor))
4703
4704 (defun gnus-group-get-new-news (&optional arg)
4705   "Get newly arrived articles.
4706 If ARG is non-nil, it should be a number between one and nine to
4707 specify which levels you are interested in re-scanning."
4708   (interactive "P")
4709   (run-hooks 'gnus-get-new-news-hook)
4710   (setq arg (gnus-group-default-level arg t))
4711   (if (and gnus-read-active-file (not arg))
4712       (progn
4713         (gnus-read-active-file)
4714         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4715     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
4716       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4717   (gnus-group-list-groups))
4718
4719 (defun gnus-group-get-new-news-this-group (&optional n)
4720   "Check for newly arrived news in the current group (and the N-1 next groups).
4721 The difference between N and the number of newsgroup checked is returned.
4722 If N is negative, this group and the N-1 previous groups will be checked."
4723   (interactive "P")
4724   (let* ((groups (gnus-group-process-prefix n))
4725          (ret (if (numberp n) (- n (length groups)) 0))
4726          group)
4727     (while groups
4728       (setq group (car groups)
4729             groups (cdr groups))
4730       (gnus-group-remove-mark group)
4731       (or (gnus-get-new-news-in-group group)
4732           (progn 
4733             (ding) 
4734             (message "%s error: %s" group (gnus-status-message group))
4735             (sit-for 2))))
4736     (gnus-group-next-unread-group 1 t)
4737     (gnus-summary-position-cursor)
4738     ret))
4739
4740 (defun gnus-get-new-news-in-group (group)
4741   (and group 
4742        (gnus-activate-group group)
4743        (progn
4744          (gnus-get-unread-articles-in-group 
4745           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4746           (gnus-gethash group gnus-active-hashtb))
4747          (gnus-group-update-group-line)
4748          t)))
4749
4750 (defun gnus-group-fetch-faq (group)
4751   "Fetch the FAQ for the current group."
4752   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4753   (or group (error "No group name given"))
4754   (let ((file (concat gnus-group-faq-directory (gnus-group-real-name group))))
4755     (if (not (file-exists-p file))
4756         (error "No such file: %s" file)
4757       (find-file file))))
4758   
4759 (defun gnus-group-describe-group (force &optional group)
4760   "Display a description of the current newsgroup."
4761   (interactive (list current-prefix-arg (gnus-group-group-name)))
4762   (and force (setq gnus-description-hashtb nil))
4763   (let ((method (gnus-find-method-for-group group))
4764         desc)
4765     (or group (error "No group name given"))
4766     (and (or (and gnus-description-hashtb
4767                   ;; We check whether this group's method has been
4768                   ;; queried for a description file.  
4769                   (gnus-gethash 
4770                    (gnus-group-prefixed-name "" method) 
4771                    gnus-description-hashtb))
4772              (setq desc (gnus-group-get-description group))
4773              (gnus-read-descriptions-file method))
4774          (message
4775           (or desc (gnus-gethash group gnus-description-hashtb)
4776               "No description available")))))
4777
4778 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4779 (defun gnus-group-describe-all-groups (&optional force)
4780   "Pop up a buffer with descriptions of all newsgroups."
4781   (interactive "P")
4782   (and force (setq gnus-description-hashtb nil))
4783   (if (not (or gnus-description-hashtb
4784                (gnus-read-all-descriptions-files)))
4785       (error "Couldn't request descriptions file"))
4786   (let ((buffer-read-only nil)
4787         b)
4788     (erase-buffer)
4789     (mapatoms
4790      (lambda (group)
4791        (setq b (point))
4792        (insert (format "      *: %-20s %s\n" (symbol-name group)
4793                        (symbol-value group)))
4794        (add-text-properties 
4795         b (1+ b) (list 'gnus-group group
4796                        'gnus-unread t 'gnus-marked nil
4797                        'gnus-level (1+ gnus-level-subscribed))))
4798      gnus-description-hashtb)
4799     (goto-char (point-min))
4800     (gnus-group-position-cursor)))
4801
4802 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4803 (defun gnus-group-apropos (regexp &optional search-description)
4804   "List all newsgroups that have names that match a regexp."
4805   (interactive "sGnus apropos (regexp): ")
4806   (let ((prev "")
4807         (obuf (current-buffer))
4808         groups des)
4809     ;; Go through all newsgroups that are known to Gnus.
4810     (mapatoms 
4811      (lambda (group)
4812        (and (symbol-name group)
4813             (string-match regexp (symbol-name group))
4814             (setq groups (cons (symbol-name group) groups))))
4815      gnus-active-hashtb)
4816     ;; Go through all descriptions that are known to Gnus. 
4817     (if search-description
4818         (mapatoms 
4819          (lambda (group)
4820            (and (string-match regexp (symbol-value group))
4821                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4822                 (setq groups (cons (symbol-name group) groups))))
4823          gnus-description-hashtb))
4824     (if (not groups)
4825         (gnus-message 3 "No groups matched \"%s\"." regexp)
4826       ;; Print out all the groups.
4827       (save-excursion
4828         (pop-to-buffer "*Gnus Help*")
4829         (buffer-disable-undo (current-buffer))
4830         (erase-buffer)
4831         (setq groups (sort groups 'string<))
4832         (while groups
4833           ;; Groups may be entered twice into the list of groups.
4834           (if (not (string= (car groups) prev))
4835               (progn
4836                 (insert (setq prev (car groups)) "\n")
4837                 (if (and gnus-description-hashtb
4838                          (setq des (gnus-gethash (car groups) 
4839                                                  gnus-description-hashtb)))
4840                     (insert "  " des "\n"))))
4841           (setq groups (cdr groups)))
4842         (goto-char (point-min))))
4843     (pop-to-buffer obuf)))
4844
4845 (defun gnus-group-description-apropos (regexp)
4846   "List all newsgroups that have names or descriptions that match a regexp."
4847   (interactive "sGnus description apropos (regexp): ")
4848   (if (not (or gnus-description-hashtb
4849                (gnus-read-all-descriptions-files)))
4850       (error "Couldn't request descriptions file"))
4851   (gnus-group-apropos regexp t))
4852
4853 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4854 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4855   "List all groups with unread articles that match REGEXP.
4856 If the prefix LEVEL is non-nil, it should be a number that says which
4857 level to cut off listing groups. 
4858 If ALL, also list groups with no unread articles.
4859 If LOWEST, don't list groups with level lower than LOWEST."
4860   (interactive "P\nsList newsgroups matching: ")
4861   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4862                            all (or lowest 1) regexp)
4863   (goto-char (point-min))
4864   (gnus-group-position-cursor))
4865
4866 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4867   "List all groups that match REGEXP.
4868 If the prefix LEVEL is non-nil, it should be a number that says which
4869 level to cut off listing groups. 
4870 If LOWEST, don't list groups with level lower than LOWEST."
4871   (interactive "P\nsList newsgroups matching: ")
4872   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4873
4874 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4875 (defun gnus-group-save-newsrc ()
4876   "Save the Gnus startup files."
4877   (interactive)
4878   (gnus-save-newsrc-file))
4879
4880 (defun gnus-group-restart (&optional arg)
4881   "Force Gnus to read the .newsrc file."
4882   (interactive "P")
4883   (gnus-save-newsrc-file)
4884   (gnus-setup-news 'force)
4885   (gnus-group-list-groups arg))
4886
4887 (defun gnus-group-read-init-file ()
4888   "Read the Gnus elisp init file."
4889   (interactive)
4890   (gnus-read-init-file))
4891
4892 (defun gnus-group-check-bogus-groups (&optional silent)
4893   "Check bogus newsgroups.
4894 If given a prefix, don't ask for confirmation before removing a bogus
4895 group."
4896   (interactive "P")
4897   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4898   (gnus-group-list-groups))
4899
4900 (defun gnus-group-edit-global-kill (&optional article group)
4901   "Edit the global kill file.
4902 If GROUP, edit that local kill file instead."
4903   (interactive "P")
4904   (setq gnus-current-kill-article article)
4905   (gnus-kill-file-edit-file group)
4906   (gnus-message 
4907    6
4908    (substitute-command-keys
4909     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4910
4911 (defun gnus-group-edit-local-kill (article group)
4912   "Edit a local kill file."
4913   (interactive (list nil (gnus-group-group-name)))
4914   (gnus-group-edit-global-kill article group))
4915
4916 (defun gnus-group-force-update ()
4917   "Update `.newsrc' file."
4918   (interactive)
4919   (gnus-save-newsrc-file))
4920
4921 (defun gnus-group-suspend ()
4922   "Suspend the current Gnus session.
4923 In fact, cleanup buffers except for group mode buffer.
4924 The hook gnus-suspend-gnus-hook is called before actually suspending."
4925   (interactive)
4926   (run-hooks 'gnus-suspend-gnus-hook)
4927   ;; Kill Gnus buffers except for group mode buffer.
4928   (let ((group-buf (get-buffer gnus-group-buffer)))
4929     ;; Do this on a separate list in case the user does a ^G before we finish
4930     (let ((gnus-buffer-list
4931            (delq group-buf (delq gnus-dribble-buffer
4932                                  (append gnus-buffer-list nil)))))
4933       (while gnus-buffer-list
4934         (gnus-kill-buffer (car gnus-buffer-list))
4935         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4936     (if group-buf
4937         (progn
4938           (setq gnus-buffer-list (list group-buf))
4939           (bury-buffer group-buf)
4940           (delete-windows-on group-buf t)))))
4941
4942 (defun gnus-group-clear-dribble ()
4943   "Clear all information from the dribble buffer."
4944   (interactive)
4945   (gnus-dribble-clear))
4946
4947 (defun gnus-group-exit ()
4948   "Quit reading news after updating .newsrc.eld and .newsrc.
4949 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4950   (interactive)
4951   (if (or noninteractive                ;For gnus-batch-kill
4952           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4953           (not gnus-interactive-exit)   ;Without confirmation
4954           gnus-expert-user
4955           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4956       (progn
4957         (run-hooks 'gnus-exit-gnus-hook)
4958         ;; Offer to save data from non-quitted summary buffers.
4959         (gnus-offer-save-summaries)
4960         ;; Save the newsrc file(s).
4961         (gnus-save-newsrc-file)
4962         ;; Kill-em-all.
4963         (gnus-close-backends)
4964         ;; Reset everything.
4965         (gnus-clear-system))))
4966
4967 (defun gnus-close-backends ()
4968   ;; Send a close request to all backends that support such a request. 
4969   (let ((methods gnus-valid-select-methods)
4970         func)
4971     (while methods
4972       (if (fboundp (setq func (intern (concat (car (car methods))
4973                                               "-request-close"))))
4974           (funcall func))
4975       (setq methods (cdr methods)))))
4976
4977 (defun gnus-group-quit ()
4978   "Quit reading news without updating .newsrc.eld or .newsrc.
4979 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4980   (interactive)
4981   (if (or noninteractive                ;For gnus-batch-kill
4982           (zerop (buffer-size))
4983           (not (gnus-server-opened gnus-select-method))
4984           gnus-expert-user
4985           (not gnus-current-startup-file)
4986           (gnus-yes-or-no-p
4987            (format "Quit reading news without saving %s? "
4988                    (file-name-nondirectory gnus-current-startup-file))))
4989       (progn
4990         (run-hooks 'gnus-exit-gnus-hook)
4991         (if gnus-use-full-window
4992             (delete-other-windows)
4993           (gnus-remove-some-windows))
4994         (gnus-dribble-save)
4995         (gnus-close-backends)
4996         (gnus-clear-system))))
4997
4998 (defun gnus-offer-save-summaries ()
4999   (save-excursion
5000     (let ((buflist (buffer-list)) 
5001           buffers bufname)
5002       (while buflist
5003         (and (setq bufname (buffer-name (car buflist)))
5004              (string-match "Summary" bufname)
5005              (save-excursion
5006                (set-buffer bufname)
5007                ;; We check that this is, indeed, a summary buffer.
5008                (eq major-mode 'gnus-summary-mode))
5009              (setq buffers (cons bufname buffers)))
5010         (setq buflist (cdr buflist)))
5011       (and buffers
5012            (map-y-or-n-p 
5013             "Update summary buffer %s? "
5014             (lambda (buf)
5015               (set-buffer buf)
5016               (gnus-summary-exit))
5017             buffers)))))
5018
5019 (defun gnus-group-describe-briefly ()
5020   "Give a one line description of the group mode commands."
5021   (interactive)
5022   (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")))
5023
5024 (defun gnus-group-browse-foreign-server (method)
5025   "Browse a foreign news server.
5026 If called interactively, this function will ask for a select method
5027  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
5028 If not, METHOD should be a list where the first element is the method
5029 and the second element is the address."
5030   (interactive
5031    (list (let ((how (completing-read 
5032                      "Which backend: "
5033                      (append gnus-valid-select-methods gnus-server-alist)
5034                      nil t "nntp")))
5035            ;; We either got a backend name or a virtual server name.
5036            ;; If the first, we also need an address.
5037            (if (assoc how gnus-valid-select-methods)
5038                (list (intern how)
5039                      ;; Suggested by mapjph@bath.ac.uk.
5040                      (completing-read 
5041                       "Address: " 
5042                       (mapcar (lambda (server) (list server))
5043                               gnus-secondary-servers)))
5044              ;; We got a server name, so we find the method.
5045              (gnus-server-to-method how)))))
5046   (gnus-browse-foreign-server method))
5047
5048 \f
5049 ;;;
5050 ;;; Browse Server Mode
5051 ;;;
5052
5053 (defvar gnus-browse-mode-hook nil)
5054 (defvar gnus-browse-mode-map nil)
5055 (put 'gnus-browse-mode 'mode-class 'special)
5056
5057 (if gnus-browse-mode-map
5058     nil
5059   (setq gnus-browse-mode-map (make-keymap))
5060   (suppress-keymap gnus-browse-mode-map)
5061   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
5062   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
5063   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
5064   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
5065   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
5066   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
5067   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
5068   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
5069   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
5070   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
5071   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
5072   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
5073   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
5074   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
5075   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
5076   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
5077   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
5078   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
5079   )
5080
5081 (defvar gnus-browse-current-method nil)
5082 (defvar gnus-browse-return-buffer nil)
5083
5084 (defvar gnus-browse-buffer "*Gnus Browse Server*")
5085
5086 (defun gnus-browse-foreign-server (method &optional return-buffer)
5087   (setq gnus-browse-current-method method)
5088   (setq gnus-browse-return-buffer return-buffer)
5089   (let ((gnus-select-method method)
5090         groups group)
5091     (gnus-message 5 "Connecting to %s..." (nth 1 method))
5092     (or (gnus-check-server method)
5093         (error "Unable to contact server: %s" (gnus-status-message method)))
5094     (or (gnus-request-list method)
5095         (error "Couldn't request list: %s" (gnus-status-message method)))
5096     (get-buffer-create gnus-browse-buffer)
5097     (gnus-add-current-to-buffer-list)
5098     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
5099     (gnus-configure-windows 'browse)
5100     (buffer-disable-undo (current-buffer))
5101     (let ((buffer-read-only nil))
5102       (erase-buffer))
5103     (gnus-browse-mode)
5104     (setq mode-line-buffer-identification
5105           (format
5106            "Gnus  Browse Server {%s:%s}" (car method) (car (cdr method))))
5107     (save-excursion
5108       (set-buffer nntp-server-buffer)
5109       (let ((cur (current-buffer)))
5110         (goto-char (point-min))
5111         (or (string= gnus-ignored-newsgroups "")
5112             (delete-matching-lines gnus-ignored-newsgroups))
5113         (while (re-search-forward 
5114                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
5115           (goto-char (match-end 1))
5116           (setq groups (cons (cons (buffer-substring (match-beginning 1)
5117                                                      (match-end 1))
5118                                    (max 0 (- (1+ (read cur)) (read cur))))
5119                              groups)))))
5120     (setq groups (sort groups 
5121                        (lambda (l1 l2)
5122                          (string< (car l1) (car l2)))))
5123     (let ((buffer-read-only nil))
5124       (while groups
5125         (setq group (car groups))
5126         (insert 
5127          (format "K%7d: %s\n" (cdr group) (car group)))
5128         (setq groups (cdr groups))))
5129     (switch-to-buffer (current-buffer))
5130     (goto-char (point-min))
5131     (gnus-group-position-cursor)))
5132
5133 (defun gnus-browse-mode ()
5134   "Major mode for browsing a foreign server.
5135
5136 All normal editing commands are switched off.
5137
5138 \\<gnus-browse-mode-map>
5139 The only things you can do in this buffer is
5140
5141 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5142 The group will be inserted into the group buffer upon exit from this
5143 buffer.  
5144
5145 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5146
5147 3) `\\[gnus-browse-exit]' to return to the group buffer."
5148   (interactive)
5149   (kill-all-local-variables)
5150   (if gnus-visual (gnus-browse-make-menu-bar))
5151   (setq mode-line-modified "-- ")
5152   (make-local-variable 'mode-line-format)
5153   (setq mode-line-format (copy-sequence mode-line-format))
5154   (and (equal (nth 3 mode-line-format) "   ")
5155        (setcar (nthcdr 3 mode-line-format) ""))
5156   (setq major-mode 'gnus-browse-mode)
5157   (setq mode-name "Browse Server")
5158   (setq mode-line-process nil)
5159   (use-local-map gnus-browse-mode-map)
5160   (buffer-disable-undo (current-buffer))
5161   (setq truncate-lines t)
5162   (setq buffer-read-only t)
5163   (run-hooks 'gnus-browse-mode-hook))
5164
5165 (defun gnus-browse-read-group (&optional no-article)
5166   "Enter the group at the current line."
5167   (interactive)
5168   (let ((group (gnus-browse-group-name)))
5169     (or (gnus-group-read-ephemeral-group 
5170          group gnus-browse-current-method nil
5171          (cons (current-buffer) 'browse))
5172         (error "Couldn't enter %s" group))))
5173
5174 (defun gnus-browse-select-group ()
5175   "Select the current group."
5176   (interactive)
5177   (gnus-browse-read-group 'no))
5178
5179 (defun gnus-browse-next-group (n)
5180   "Go to the next group."
5181   (interactive "p")
5182   (prog1
5183       (forward-line n)
5184     (gnus-group-position-cursor)))
5185
5186 (defun gnus-browse-prev-group (n)
5187   "Go to the next group."
5188   (interactive "p")
5189   (gnus-browse-next-group (- n)))
5190
5191 (defun gnus-browse-unsubscribe-current-group (arg)
5192   "(Un)subscribe to the next ARG groups."
5193   (interactive "p")
5194   (and (eobp)
5195        (error "No group at current line."))
5196   (let ((ward (if (< arg 0) -1 1))
5197         (arg (abs arg)))
5198     (while (and (> arg 0)
5199                 (not (eobp))
5200                 (gnus-browse-unsubscribe-group)
5201                 (zerop (gnus-browse-next-group ward)))
5202       (setq arg (1- arg)))
5203     (gnus-group-position-cursor)
5204     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5205     arg))
5206
5207 (defun gnus-browse-group-name ()
5208   (save-excursion
5209     (beginning-of-line)
5210     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5211         ()
5212       (gnus-group-prefixed-name 
5213        (buffer-substring (match-beginning 1) (match-end 1))
5214        gnus-browse-current-method))))
5215   
5216 (defun gnus-browse-unsubscribe-group ()
5217   (let ((sub nil)
5218         (buffer-read-only nil)
5219         group)
5220     (save-excursion
5221       (beginning-of-line)
5222       (if (= (following-char) ?K) (setq sub t))
5223       (setq group (gnus-browse-group-name))
5224       (beginning-of-line)
5225       (delete-char 1)
5226       (if sub
5227           (progn
5228             (gnus-group-change-level 
5229              (list t group gnus-level-default-subscribed
5230                    nil nil gnus-browse-current-method) 
5231              gnus-level-default-subscribed gnus-level-killed
5232              (and (car (nth 1 gnus-newsrc-alist))
5233                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5234                                 gnus-newsrc-hashtb))
5235              t)
5236             (insert ? ))
5237         (gnus-group-change-level 
5238          group gnus-level-killed gnus-level-default-subscribed)
5239         (insert ?K)))
5240     t))
5241
5242 (defun gnus-browse-exit ()
5243   "Quit browsing and return to the group buffer."
5244   (interactive)
5245   (if (eq major-mode 'gnus-browse-mode)
5246       (kill-buffer (current-buffer)))
5247   (if gnus-browse-return-buffer
5248       (gnus-configure-windows 'server 'force)
5249     (gnus-configure-windows 'group 'force)
5250     (gnus-group-list-groups nil)))
5251
5252 (defun gnus-browse-describe-briefly ()
5253   "Give a one line description of the group mode commands."
5254   (interactive)
5255   (gnus-message 6
5256                 (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")))
5257       
5258 \f
5259 ;;;
5260 ;;; Gnus summary mode
5261 ;;;
5262
5263 (defvar gnus-summary-mode-map nil)
5264 (defvar gnus-summary-mark-map nil)
5265 (defvar gnus-summary-mscore-map nil)
5266 (defvar gnus-summary-article-map nil)
5267 (defvar gnus-summary-thread-map nil)
5268 (defvar gnus-summary-goto-map nil)
5269 (defvar gnus-summary-exit-map nil)
5270 (defvar gnus-summary-interest-map nil)
5271 (defvar gnus-summary-sort-map nil)
5272 (defvar gnus-summary-backend-map nil)
5273 (defvar gnus-summary-save-map nil)
5274 (defvar gnus-summary-wash-map nil)
5275 (defvar gnus-summary-wash-hide-map nil)
5276 (defvar gnus-summary-wash-highlight-map nil)
5277 (defvar gnus-summary-wash-time-map nil)
5278 (defvar gnus-summary-help-map nil)
5279
5280 (put 'gnus-summary-mode 'mode-class 'special)
5281
5282 (if gnus-summary-mode-map
5283     nil
5284   (setq gnus-summary-mode-map (make-keymap))
5285   (suppress-keymap gnus-summary-mode-map)
5286
5287   ;; Non-orthogonal keys
5288
5289   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5290   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5291   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5292   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5293   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5294   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5295   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5296   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5297   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5298   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5299   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5300   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5301   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5302   (define-key gnus-summary-mode-map 
5303     "\M-s" 'gnus-summary-search-article-forward)
5304   (define-key gnus-summary-mode-map 
5305     "\M-r" 'gnus-summary-search-article-backward)
5306   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5307   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5308   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5309   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5310   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5311   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5312   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5313   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5314   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5315   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5316   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5317   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5318   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5319   (define-key gnus-summary-mode-map 
5320     "k" 'gnus-summary-kill-same-subject-and-select)
5321   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5322   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5323   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5324   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5325   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5326   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5327   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5328   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5329   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5330   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5331   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5332   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5333   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5334   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5335   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5336   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5337   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5338   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5339   (define-key gnus-summary-mode-map 
5340     "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5341   (define-key gnus-summary-mode-map 
5342     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5343   (define-key gnus-summary-mode-map 
5344     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5345   (define-key gnus-summary-mode-map 
5346     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5347   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5348   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5349   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5350   (define-key gnus-summary-mode-map 
5351     "\C-x\C-s" 'gnus-summary-reselect-current-group)
5352   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5353   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5354   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5355   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5356   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5357   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5358   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5359   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5360   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5361   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5362   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5363   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5364   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5365   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5366   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5367   (define-key gnus-summary-mode-map "V" 'gnus-version)
5368   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5369   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5370   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5371   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5372   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5373   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5374   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5375   (define-key gnus-summary-mode-map 
5376     "x" 'gnus-summary-remove-lines-marked-as-read)
5377 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5378   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5379   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5380   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5381 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5382   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5383   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5384   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5385   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5386   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5387
5388
5389   ;; Sort of orthogonal keymap
5390   (define-prefix-command 'gnus-summary-mark-map)
5391   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5392   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5393   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5394   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5395   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5396   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5397   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5398   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5399   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5400   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5401   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5402   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5403   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5404   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5405   (define-key gnus-summary-mark-map 
5406     "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5407   (define-key gnus-summary-mark-map 
5408     "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5409   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5410   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5411   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5412   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5413   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5414   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5415   (define-key gnus-summary-mark-map 
5416     "k" 'gnus-summary-kill-same-subject-and-select)
5417   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5418
5419   (define-prefix-command 'gnus-summary-mscore-map)
5420   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5421   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5422   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5423   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5424   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5425
5426   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5427   
5428   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5429   
5430   (define-prefix-command 'gnus-summary-goto-map)
5431   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5432   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5433   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5434   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5435   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5436   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5437   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5438   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5439   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5440   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5441   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5442   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5443   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5444   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5445
5446
5447   (define-prefix-command 'gnus-summary-thread-map)
5448   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5449   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5450   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5451   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5452   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5453   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5454   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5455   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5456   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5457   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5458   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5459   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5460   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5461   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5462
5463   
5464   (define-prefix-command 'gnus-summary-exit-map)
5465   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5466   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5467   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5468   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5469   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5470   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5471   (define-key gnus-summary-exit-map 
5472     "n" 'gnus-summary-catchup-and-goto-next-group)
5473   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5474   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5475   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5476   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5477
5478
5479   (define-prefix-command 'gnus-summary-article-map)
5480   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5481   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5482   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5483   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5484   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5485   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5486   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5487   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5488   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5489   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5490   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5491   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5492   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5493   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5494
5495
5496
5497   (define-prefix-command 'gnus-summary-wash-map)
5498   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5499
5500   (define-prefix-command 'gnus-summary-wash-hide-map)
5501   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5502   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5503   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5504   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5505   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5506   (define-key gnus-summary-wash-hide-map 
5507     "\C-c" 'gnus-article-hide-citation-maybe)
5508
5509   (define-prefix-command 'gnus-summary-wash-highlight-map)
5510   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5511   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5512   (define-key gnus-summary-wash-highlight-map 
5513     "h" 'gnus-article-highlight-headers)
5514   (define-key gnus-summary-wash-highlight-map
5515     "c" 'gnus-article-highlight-citation)
5516   (define-key gnus-summary-wash-highlight-map
5517     "s" 'gnus-article-highlight-signature)
5518
5519   (define-prefix-command 'gnus-summary-wash-time-map)
5520   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5521   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5522   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5523   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5524   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5525
5526   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5527   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5528   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5529   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5530   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5531   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5532   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5533   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5534   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5535   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5536
5537
5538   (define-prefix-command 'gnus-summary-help-map)
5539   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5540   (define-key gnus-summary-help-map "v" 'gnus-version)
5541   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5542   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5543   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5544   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5545
5546
5547   (define-prefix-command 'gnus-summary-backend-map)
5548   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5549   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5550   (define-key gnus-summary-backend-map "\M-\C-e" 
5551     'gnus-summary-expire-articles-now)
5552   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5553   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5554   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5555   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5556   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5557   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5558   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5559
5560
5561   (define-prefix-command 'gnus-summary-save-map)
5562   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5563   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5564   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5565   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5566   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5567   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5568   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5569   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5570 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5571
5572   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5573
5574   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5575 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5576 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5577 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5578 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5579 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5580 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5581   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5582 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5583 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5584
5585   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5586
5587 ;  (define-prefix-command 'gnus-summary-sort-map)
5588 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5589 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5590 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5591 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5592 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5593 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5594
5595   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5596   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5597   )
5598
5599
5600 \f
5601
5602 (defun gnus-summary-mode (&optional group)
5603   "Major mode for reading articles.
5604
5605 All normal editing commands are switched off.
5606 \\<gnus-summary-mode-map>
5607 Each line in this buffer represents one article.  To read an
5608 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5609 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5610 respectively.
5611
5612 You can also post articles and send mail from this buffer.  To 
5613 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5614 of an article, type `\\[gnus-summary-reply]'.
5615
5616 There are approx. one gazillion commands you can execute in this 
5617 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5618
5619 The following commands are available:
5620
5621 \\{gnus-summary-mode-map}"
5622   (interactive)
5623   (if gnus-visual (gnus-summary-make-menu-bar))
5624   (kill-all-local-variables)
5625   (let ((locals gnus-summary-local-variables))
5626     (while locals
5627       (if (consp (car locals))
5628           (progn
5629             (make-local-variable (car (car locals)))
5630             (set (car (car locals)) (eval (cdr (car locals)))))
5631         (make-local-variable (car locals))
5632         (set (car locals) nil))
5633       (setq locals (cdr locals))))
5634   (gnus-make-thread-indent-array)
5635   (setq mode-line-modified "-- ")
5636   (make-local-variable 'mode-line-format)
5637   (setq mode-line-format (copy-sequence mode-line-format))
5638   (and (equal (nth 3 mode-line-format) "   ")
5639        (setcar (nthcdr 3 mode-line-format) ""))
5640   (setq major-mode 'gnus-summary-mode)
5641   (setq mode-name "Summary")
5642   (make-local-variable 'minor-mode-alist)
5643   (use-local-map gnus-summary-mode-map)
5644   (buffer-disable-undo (current-buffer))
5645   (setq buffer-read-only t)             ;Disable modification
5646   (setq truncate-lines t)
5647   (setq selective-display t)
5648   (setq selective-display-ellipses t)   ;Display `...'
5649   (setq buffer-display-table gnus-summary-display-table)
5650   (setq gnus-newsgroup-name group)
5651   (run-hooks 'gnus-summary-mode-hook))
5652
5653 (defun gnus-summary-make-display-table ()
5654   ;; Change the display table.  Odd characters have a tendency to mess
5655   ;; up nicely formatted displays - we make all possible glyphs
5656   ;; display only a single character.
5657
5658   ;; We start from the standard display table, if any.
5659   (setq gnus-summary-display-table 
5660         (or (copy-sequence standard-display-table)
5661             (make-display-table)))
5662   ;; Nix out all the control chars...
5663   (let ((i 32))
5664     (while (>= (setq i (1- i)) 0)
5665       (aset gnus-summary-display-table i [??])))
5666   ;; ... but not newline and cr, of course. (cr is necessary for the
5667   ;; selective display).  
5668   (aset gnus-summary-display-table ?\n nil)
5669   (aset gnus-summary-display-table ?\r nil)
5670   ;; We nix out any glyphs over 126 that are not set already.  
5671   (let ((i 256))
5672     (while (>= (setq i (1- i)) 127)
5673       ;; Only modify if the entry is nil.
5674       (or (aref gnus-summary-display-table i) 
5675           (aset gnus-summary-display-table i [??])))))
5676
5677 (defun gnus-summary-clear-local-variables ()
5678   (let ((locals gnus-summary-local-variables))
5679     (while locals
5680       (if (consp (car locals))
5681           (and (vectorp (car (car locals)))
5682                (set (car (car locals)) nil))
5683         (and (vectorp (car locals))
5684              (set (car locals) nil)))
5685       (setq locals (cdr locals)))))
5686
5687 ;; Some summary mode macros.
5688
5689 ;; Return a header specified by a NUMBER.
5690 (defun gnus-get-header-by-number (number)
5691   (save-excursion
5692     (set-buffer gnus-summary-buffer)
5693     (or gnus-newsgroup-headers-hashtb-by-number
5694         (gnus-make-headers-hashtable-by-number))
5695     (gnus-gethash (int-to-string number)
5696                   gnus-newsgroup-headers-hashtb-by-number)))
5697
5698 ;; Fast version of the function above.
5699 (defmacro gnus-get-header-by-num (number)
5700   (` (gnus-gethash (int-to-string (, number)) 
5701                    gnus-newsgroup-headers-hashtb-by-number)))
5702
5703 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5704   "Search for article forward.
5705 If UNREAD is non-nil, only unread articles are selected.
5706 If SUBJECT is non-nil, the article which has the same subject will be
5707 searched for. 
5708 If BACKWARD is non-nil, the search will be performed backwards instead."
5709   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5710
5711 (defmacro gnus-summary-search-backward (&optional unread subject)
5712   "Search for article backward.
5713 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5714 If 2nd optional argument SUBJECT is non-nil, the article which has
5715 the same subject will be searched for."
5716   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5717
5718 (defmacro gnus-summary-article-number (&optional number-or-nil)
5719   "The article number of the article on the current line.
5720 If there isn's an article number here, then we return the current
5721 article number."
5722   (if number-or-nil
5723       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5724     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5725          gnus-current-article)))
5726
5727 (defmacro gnus-summary-thread-level ()
5728   "The thread level of the article on the current line."
5729   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5730        0))
5731
5732 (defmacro gnus-summary-article-mark ()
5733   "The mark on the current line."
5734   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5735
5736 (defun gnus-summary-subject-string ()
5737   "Return current subject string or nil if nothing."
5738   (let ((article (gnus-summary-article-number))
5739         header)
5740     (and article 
5741          (setq header (gnus-get-header-by-num article))
5742          (vectorp header)
5743          (mail-header-subject header))))
5744
5745 ;; Various summary mode internalish functions.
5746
5747 (defun gnus-mouse-pick-article (e)
5748   (interactive "e")
5749   (mouse-set-point e)
5750   (gnus-summary-next-page nil t))
5751
5752 (defun gnus-summary-setup-buffer (group)
5753   "Initialize summary buffer."
5754   (let ((buffer (concat "*Summary " group "*")))
5755     (if (get-buffer buffer)
5756         (progn
5757           (set-buffer buffer)
5758           (not gnus-newsgroup-begin))
5759       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5760       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5761       (gnus-add-current-to-buffer-list)
5762       (gnus-summary-mode group)
5763       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5764       (setq gnus-newsgroup-name group)
5765       t)))
5766
5767 (defun gnus-set-global-variables ()
5768   ;; Set the global equivalents of the summary buffer-local variables
5769   ;; to the latest values they had. These reflect the summary buffer
5770   ;; that was in action when the last article was fetched.
5771   (if (eq major-mode 'gnus-summary-mode) 
5772       (progn
5773         (setq gnus-summary-buffer (current-buffer))
5774         (let ((name gnus-newsgroup-name)
5775               (marked gnus-newsgroup-marked)
5776               (unread gnus-newsgroup-unreads)
5777               (headers gnus-current-headers)
5778               (score-file gnus-current-score-file))
5779           (save-excursion
5780             (set-buffer gnus-group-buffer)
5781             (setq gnus-newsgroup-name name)
5782             (setq gnus-newsgroup-marked marked)
5783             (setq gnus-newsgroup-unreads unread)
5784             (setq gnus-current-headers headers)
5785             (setq gnus-current-score-file score-file))))))
5786
5787 (defun gnus-summary-insert-dummy-line (sformat subject number)
5788   (if (not sformat) 
5789       (setq sformat gnus-summary-dummy-line-format-spec))
5790   (let (b)
5791     (beginning-of-line)
5792     (setq b (point))
5793     (insert (eval sformat))
5794     (add-text-properties
5795      b (1+ b)
5796      (list 'gnus-number number 
5797            'gnus-mark gnus-dummy-mark
5798            'gnus-level 0))))
5799
5800 (defvar gnus-thread-indent-array nil)
5801 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5802 (defun gnus-make-thread-indent-array ()
5803   (let ((n 200))
5804     (if (and gnus-thread-indent-array
5805              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5806         nil
5807       (setq gnus-thread-indent-array (make-vector 201 "")
5808             gnus-thread-indent-array-level gnus-thread-indent-level)
5809       (while (>= n 0)
5810         (aset gnus-thread-indent-array n
5811               (make-string (* n gnus-thread-indent-level) ? ))
5812         (setq n (1- n))))))
5813
5814 (defun gnus-summary-insert-line 
5815   (sformat header level current unread replied expirable subject-or-nil
5816            &optional dummy score process)
5817   (or sformat (setq sformat gnus-summary-line-format-spec))
5818   (let* ((indentation (aref gnus-thread-indent-array level))
5819          (lines (mail-header-lines header))
5820          (score (or score gnus-summary-default-score 0))
5821          (score-char
5822           (if (or (null gnus-summary-default-score)
5823                   (<= (abs (- score gnus-summary-default-score))
5824                       gnus-summary-zcore-fuzz)) ? 
5825             (if (< score gnus-summary-default-score)
5826                 gnus-score-below-mark gnus-score-over-mark)))
5827          (replied (cond (process gnus-process-mark)
5828                         (replied gnus-replied-mark)
5829                         (t gnus-unread-mark)))
5830          (from (mail-header-from header))
5831          (name (cond 
5832                 ((string-match "(.+)" from)
5833                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5834                 ((string-match "<[^>]+> *$" from)
5835                  (let ((beg (match-beginning 0)))
5836                    (or (and (string-match "^\"[^\"]*\"" from)
5837                             (substring from (1+ (match-beginning 0))
5838                                        (1- (match-end 0))))
5839                        (substring from 0 beg))))
5840                 (t from)))
5841          (subject (mail-header-subject header))
5842          (number (mail-header-number header))
5843          (opening-bracket (if dummy ?\< ?\[))
5844          (closing-bracket (if dummy ?\> ?\]))
5845          (buffer-read-only nil)
5846          (b (progn (beginning-of-line) (point))))
5847     (or (numberp lines) (setq lines 0))
5848     (insert (eval sformat))
5849     (add-text-properties
5850      b (1+ b) (list 'gnus-number number 
5851                     'gnus-mark (or unread gnus-unread-mark)
5852                     'gnus-level level))))
5853
5854 (defun gnus-summary-update-line (&optional dont-update)
5855   ;; Update summary line after change.
5856   (or (not gnus-summary-default-score)
5857       gnus-summary-inhibit-highlight
5858       (let ((gnus-summary-inhibit-highlight t)
5859             (article (gnus-summary-article-number)))
5860         (progn
5861           (or dont-update
5862               (if (and gnus-summary-mark-below
5863                        (< (gnus-summary-article-score)
5864                           gnus-summary-mark-below))
5865                   (and (not (memq article gnus-newsgroup-marked))
5866                        (not (memq article gnus-newsgroup-dormant))
5867                        (memq article gnus-newsgroup-unreads)
5868                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5869                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5870                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5871           (and gnus-visual
5872                (run-hooks 'gnus-summary-update-hook))))))
5873
5874 (defun gnus-summary-update-lines (&optional beg end)
5875   ;; Mark article as read (or not) by taking into account scores.
5876   (let ((beg (or beg (point-min)))
5877         (end (or end (point-max))))
5878     (if (or (not gnus-summary-default-score)
5879             gnus-summary-inhibit-highlight)
5880         ()
5881       (let ((gnus-summary-inhibit-highlight t)
5882             article)
5883         (save-excursion
5884           (set-buffer gnus-summary-buffer)
5885           (goto-char beg)
5886           (beginning-of-line)
5887           (while (and (not (eobp)) (< (point) end))
5888             (if (and gnus-summary-mark-below
5889                      (< (or (cdr (assq 
5890                                   (setq article (get-text-property 
5891                                                  (point) 'gnus-number))
5892                                   gnus-newsgroup-scored))
5893                             gnus-summary-default-score 0)
5894                         gnus-summary-mark-below))
5895                 ;; We want to possibly mark it as read...
5896                 (and (not (memq article gnus-newsgroup-marked))
5897                      (not (memq article gnus-newsgroup-dormant))
5898                      (memq article gnus-newsgroup-unreads)
5899                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5900               ;; We want to possibly mark it as unread.
5901               (and (eq (get-text-property (point) 'gnus-mark)
5902                        gnus-low-score-mark)
5903                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5904             ;; Do the visual highlights at the same time.
5905             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5906             (forward-line 1)))))))
5907
5908 (defvar gnus-tmp-gathered nil)
5909
5910 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5911   ;; Sum up all elements (and sub-elements) in a list.
5912   (let* ((number
5913           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5914           (if (and (consp thread) (cdr thread))
5915               (apply
5916                '+ 1 (mapcar
5917                      'gnus-summary-number-of-articles-in-thread 
5918                      (cdr thread)))
5919             1)))
5920     (if char 
5921         (if (> number 1) gnus-not-empty-thread-mark
5922           gnus-empty-thread-mark)
5923       number)))
5924
5925 (defun gnus-summary-read-group 
5926   (group &optional show-all no-article kill-buffer)
5927   "Start reading news in newsgroup GROUP.
5928 If SHOW-ALL is non-nil, already read articles are also listed.
5929 If NO-ARTICLE is non-nil, no article is selected initially."
5930   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5931   (let* ((new-group (gnus-summary-setup-buffer group))
5932          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5933                                                   group))))
5934          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5935     (cond 
5936      ((not new-group)
5937       (gnus-set-global-variables)
5938       (gnus-kill-buffer kill-buffer)
5939       (gnus-configure-windows 'summary 'force)
5940       (gnus-set-mode-line 'summary)
5941       (gnus-summary-position-cursor)
5942       (message "")
5943       t)
5944      ((null did-select) 
5945       (and (eq major-mode 'gnus-summary-mode)
5946            (not (equal (current-buffer) kill-buffer))
5947            (progn
5948              (kill-buffer (current-buffer))
5949              (if (not quit-config)
5950                  (progn
5951                    (set-buffer gnus-group-buffer)
5952                    (gnus-group-jump-to-group group)
5953                    (gnus-group-next-unread-group 1))
5954                (if (not (buffer-name (car quit-config)))
5955                    (gnus-configure-windows 'group 'force)
5956                  (set-buffer (car quit-config))
5957                  (and (eq major-mode 'gnus-summary-mode)
5958                       (gnus-set-global-variables))
5959                  (gnus-configure-windows (cdr quit-config))))))
5960       (message "Can't select group")
5961       nil)
5962      ((eq did-select 'quit)
5963       (and (eq major-mode 'gnus-summary-mode)
5964            (not (equal (current-buffer) kill-buffer))
5965            (kill-buffer (current-buffer)))
5966       (gnus-kill-buffer kill-buffer)
5967       (if (not quit-config)
5968           (progn
5969             (set-buffer gnus-group-buffer)
5970             (gnus-group-jump-to-group group)
5971             (gnus-group-next-unread-group 1)
5972             (gnus-configure-windows 'group 'force))
5973         (if (not (buffer-name (car quit-config)))
5974             (gnus-configure-windows 'group 'force)
5975           (set-buffer (car quit-config))
5976           (and (eq major-mode 'gnus-summary-mode)
5977                (gnus-set-global-variables))
5978           (gnus-configure-windows (cdr quit-config))))
5979       (signal 'quit nil))
5980      (t
5981       (gnus-set-global-variables)
5982       ;; Save the active value in effect when the group was entered.
5983       (setq gnus-newsgroup-active 
5984             (gnus-copy-sequence
5985              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5986       ;; You can change the subjects in this hook.
5987       (run-hooks 'gnus-select-group-hook)
5988       ;; Do score processing.
5989       (and gnus-use-scoring (gnus-possibly-score-headers))
5990       (gnus-update-format-specifications)
5991       ;; Generate the summary buffer.
5992       (gnus-summary-prepare)
5993       (if (zerop (buffer-size))
5994           (cond (gnus-newsgroup-dormant
5995                  (gnus-summary-show-all-dormant))
5996                 ((and gnus-newsgroup-scored show-all)
5997                  (gnus-summary-show-all-expunged))))
5998       ;; Function `gnus-apply-kill-file' must be called in this hook.
5999       (run-hooks 'gnus-apply-kill-hook)
6000       (if (zerop (buffer-size))
6001           (progn
6002             ;; This newsgroup is empty.
6003             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
6004             (gnus-message 6 "No unread news")
6005             (gnus-kill-buffer kill-buffer)
6006             nil)
6007         ;;(save-excursion
6008         ;;  (if kill-buffer
6009         ;;      (let ((gnus-summary-buffer kill-buffer))
6010         ;;      (gnus-configure-windows 'group))))
6011         ;; Hide conversation thread subtrees.  We cannot do this in
6012         ;; gnus-summary-prepare-hook since kill processing may not
6013         ;; work with hidden articles.
6014         (and gnus-show-threads
6015              gnus-thread-hide-subtree
6016              (gnus-summary-hide-all-threads))
6017         ;; Show first unread article if requested.
6018         (goto-char (point-min))
6019         (if (and (not no-article)
6020                  gnus-auto-select-first
6021                  (gnus-summary-first-unread-article))
6022             ()
6023           (gnus-configure-windows 'summary 'force))
6024         (gnus-set-mode-line 'summary)
6025         (gnus-summary-position-cursor)
6026         ;; If in async mode, we send some info to the backend.
6027         (and gnus-newsgroup-async
6028              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
6029              (gnus-request-asynchronous 
6030               gnus-newsgroup-name
6031               (if (and gnus-asynchronous-article-function
6032                        (fboundp gnus-asynchronous-article-function))
6033                   (funcall gnus-asynchronous-article-function
6034                            gnus-newsgroup-threads)
6035                 gnus-newsgroup-threads)))
6036         (gnus-kill-buffer kill-buffer)
6037         (if (not (get-buffer-window gnus-group-buffer))
6038             ()
6039           ;; gotta use windows, because recenter does wierd stuff if
6040           ;; the current buffer ain't the displayed window.
6041           (let ((owin (selected-window))) 
6042             (select-window (get-buffer-window gnus-group-buffer))
6043             (and (gnus-group-goto-group group)
6044                  (recenter))
6045             (select-window owin))))
6046       t))))
6047
6048 (defun gnus-summary-prepare ()
6049   ;; Generate the summary buffer.
6050   (let ((buffer-read-only nil))
6051     (erase-buffer)
6052     (gnus-summary-prepare-threads 
6053      (if gnus-show-threads
6054          (gnus-gather-threads 
6055           (gnus-sort-threads 
6056            (if (and gnus-summary-expunge-below
6057                     (not gnus-fetch-old-headers))
6058                (gnus-make-threads-and-expunge)
6059              (gnus-make-threads))))
6060        gnus-newsgroup-headers)
6061      'cull)
6062     (gnus-summary-update-lines)
6063     ;; Create the header hashtb.
6064     (gnus-make-headers-hashtable-by-number)
6065     ;; Call hooks for modifying summary buffer.
6066     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
6067     (goto-char (point-min))
6068     (run-hooks 'gnus-summary-prepare-hook)))
6069
6070 (defun gnus-gather-threads (threads)
6071   "Gather threads that have lost their roots."
6072   (if (not gnus-summary-make-false-root)
6073       threads 
6074     (let ((hashtb (gnus-make-hashtable 1023))
6075           (prev threads)
6076           (result threads)
6077           subject hthread whole-subject)
6078       (while threads
6079         (setq whole-subject 
6080               (setq subject (mail-header-subject (car (car threads)))))
6081         (if gnus-summary-gather-subject-limit
6082             (or (and (numberp gnus-summary-gather-subject-limit)
6083                      (> (length subject) gnus-summary-gather-subject-limit)
6084                      (setq subject
6085                            (substring subject 0 
6086                                       gnus-summary-gather-subject-limit)))
6087                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
6088                      (setq subject (gnus-simplify-subject-fuzzy subject))))
6089           (setq subject (gnus-simplify-subject-re subject)))
6090         (if (setq hthread 
6091                   (gnus-gethash subject hashtb))
6092             (progn
6093               (or (stringp (car (car hthread)))
6094                   (setcar hthread (list whole-subject (car hthread))))
6095               (setcdr (car hthread) (nconc (cdr (car hthread)) 
6096                                            (list (car threads))))
6097               (setcdr prev (cdr threads))
6098               (setq threads prev))
6099           (gnus-sethash subject threads hashtb))
6100         (setq prev threads)
6101         (setq threads (cdr threads)))
6102       result)))
6103
6104 (defun gnus-make-threads ()
6105   ;; This function takes the dependencies already made by 
6106   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6107   ;; through the dependecies in the hash table and finds all the
6108   ;; roots. Roots do not refer back to any valid articles.
6109   (gnus-message 6 "Threading...")
6110   (let (roots new-roots)
6111     (and gnus-fetch-old-headers
6112          (eq gnus-headers-retrieved-by 'nov)
6113          (gnus-build-old-threads))
6114     (mapatoms
6115      (lambda (refs)
6116        (if (not (car (symbol-value refs)))
6117            (setq roots (append (cdr (symbol-value refs)) roots))
6118          ;; Ok, these refer back to valid articles, but if
6119          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6120          ;; the root has the same subject as its children. The children
6121          ;; that do not are made into roots and removed from the list
6122          ;; of children. 
6123          (or gnus-thread-ignore-subject
6124              (let* ((prev (symbol-value refs))
6125                     (subject (gnus-simplify-subject-re 
6126                               (mail-header-subject (car prev))))
6127                     (headers (cdr prev)))
6128                (while headers
6129                  (if (not (string= subject
6130                                    (gnus-simplify-subject-re 
6131                                     (mail-header-subject (car headers)))))
6132                      (progn
6133                        (setq new-roots (cons (car headers) new-roots))
6134                        (setcdr prev (cdr headers)))
6135                    (setq prev headers))
6136                  (setq headers (cdr headers)))))))
6137      gnus-newsgroup-dependencies)
6138
6139     ;; We enter the new roots into the dependencies structure to
6140     ;; ensure that any possible later thread-regeneration will be
6141     ;; possible. 
6142     (let ((r new-roots))
6143       (while r
6144         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6145                       (list nil (car r)) gnus-newsgroup-dependencies)
6146         (setq r (cdr r))))
6147
6148     (setq roots (nconc new-roots roots))
6149
6150     (prog1
6151         (mapcar 'gnus-trim-thread
6152                 (apply 'append
6153                        (mapcar 'gnus-cut-thread
6154                                (mapcar 'gnus-make-sub-thread roots))))
6155       (gnus-message 6 "Threading...done"))))
6156
6157   
6158 (defun gnus-make-threads-and-expunge ()
6159   ;; This function takes the dependencies already made by 
6160   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6161   ;; through the dependecies in the hash table and finds all the
6162   ;; roots. Roots do not refer back to any valid articles.
6163   (gnus-message 6 "Threading...")
6164   (let ((default (or gnus-summary-default-score 0))
6165         (below gnus-summary-expunge-below)
6166         roots article new-roots)
6167     (and gnus-fetch-old-headers
6168          (eq gnus-headers-retrieved-by 'nov)
6169          (gnus-build-old-threads))
6170     (mapatoms
6171      (lambda (refs)
6172        (if (not (car (symbol-value refs)))
6173            ;; These articles do not refer back to any other articles -
6174            ;; they are roots.
6175            (let ((headers (cdr (symbol-value refs))))
6176              ;; We weed out the low-scored articles.
6177              (while headers
6178                (if (not (< (or (cdr (assq (mail-header-number (car headers))
6179                                           gnus-newsgroup-scored)) default)
6180                            below))
6181                    ;; It is over.
6182                    (setq roots (cons (car headers) roots))
6183                  ;; It is below, so we mark it as read.
6184                  (setq gnus-newsgroup-unreads
6185                        (delq (mail-header-number (car headers))
6186                              gnus-newsgroup-unreads))
6187                  (setq gnus-newsgroup-reads 
6188                        (cons (cons (mail-header-number (car headers))
6189                                    gnus-low-score-mark) 
6190                              gnus-newsgroup-reads)))
6191                (setq headers (cdr headers))))
6192          ;; Ok, these refer back to valid articles, but if
6193          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6194          ;; the root has the same subject as its children. The children
6195          ;; that do not are made into roots and removed from the list
6196          ;; of children. 
6197          (or gnus-thread-ignore-subject
6198              (let* ((prev (symbol-value refs))
6199                     (subject (gnus-simplify-subject-re 
6200                               (mail-header-subject (car prev))))
6201                     (headers (cdr prev)))
6202                (while headers
6203                  (if (not (string= subject
6204                                    (gnus-simplify-subject-re 
6205                                     (mail-header-subject (car headers)))))
6206                      (progn
6207                        (if (not (< (or (cdr (assq (mail-header-number
6208                                                    (car headers))
6209                                                   gnus-newsgroup-scored))
6210                                        default) below))
6211                            (setq new-roots (cons (car headers) new-roots))
6212                          (setq gnus-newsgroup-unreads
6213                                (delq (mail-header-number (car headers))
6214                                      gnus-newsgroup-unreads))
6215                          (setq gnus-newsgroup-reads
6216                                (cons (cons (mail-header-number (car headers)) 
6217                                            gnus-low-score-mark) 
6218                                      gnus-newsgroup-reads)))
6219                        (setcdr prev (cdr headers)))
6220                    (setq prev headers))
6221                  (setq headers (cdr headers)))))
6222          ;; If this article is expunged, some of the children might be
6223          ;; roots.  
6224          (if (< (or (cdr (assq (mail-header-number (car (symbol-value refs)))
6225                                gnus-newsgroup-scored)) default)
6226                 below)
6227              (let* ((prev (symbol-value refs))
6228                     (headers (cdr prev)))
6229                (while headers
6230                  (setq article (mail-header-number (car headers)))
6231                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6232                                  default) below))
6233                      (progn (setq new-roots (cons (car headers) new-roots))
6234                             (setq prev headers))
6235                    (setq gnus-newsgroup-unreads 
6236                          (delq article gnus-newsgroup-unreads))
6237                    (setq gnus-newsgroup-reads 
6238                          (cons (cons article gnus-low-score-mark) 
6239                                gnus-newsgroup-reads))
6240                    (setcdr prev (cdr headers)))
6241                  (setq headers (cdr headers))))
6242            ;; It was not expunged, but we look at expunged children.
6243            (let* ((prev (symbol-value refs))
6244                   (headers (cdr prev))
6245                   article)
6246              (while headers
6247                (setq article (mail-header-number (car headers)))
6248                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6249                                default) below))
6250                    (setq prev headers)
6251                  (setq gnus-newsgroup-unreads 
6252                        (delq article gnus-newsgroup-unreads))
6253                  (setq gnus-newsgroup-reads 
6254                        (cons (cons article gnus-low-score-mark)
6255                              gnus-newsgroup-reads))
6256                  (setcdr prev (cdr headers)))
6257                (setq headers (cdr headers)))))))
6258      gnus-newsgroup-dependencies)
6259
6260     ;; We enter the new roots into the dependencies structure to
6261     ;; ensure that any possible later thread-regeneration will be
6262     ;; possible. 
6263     (let ((r new-roots))
6264       (while r
6265         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6266                       (list nil (car r)) gnus-newsgroup-dependencies)
6267         (setq r (cdr r))))
6268
6269     (setq roots (nconc new-roots roots))
6270
6271     (prog1
6272         (mapcar 'gnus-trim-thread
6273                 (apply 'append
6274                        (mapcar 'gnus-cut-thread
6275                                (mapcar 'gnus-make-sub-thread roots))))
6276       (gnus-message 6 "Threading...done"))))
6277
6278   
6279 (defun gnus-cut-thread (thread)
6280   ;; Remove leaf dormant or ancient articles from THREAD.
6281   (let ((head (car thread))
6282         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6283     (if (and (null tail)
6284              (let ((number (mail-header-number head)))
6285                (or (memq number gnus-newsgroup-ancient)
6286                    (memq number gnus-newsgroup-dormant)
6287                    (and gnus-summary-expunge-below
6288                         (eq gnus-fetch-old-headers 'some)
6289                         (< (or (cdr (assq number gnus-newsgroup-scored))
6290                                gnus-summary-default-score 0)
6291                            gnus-summary-expunge-below)
6292                         (progn
6293                           (setq gnus-newsgroup-unreads
6294                                 (delq number gnus-newsgroup-unreads))
6295                           (setq gnus-newsgroup-reads
6296                                 (cons (cons number gnus-low-score-mark)
6297                                       gnus-newsgroup-reads))
6298                           t)))))
6299         nil
6300       (list (cons head tail)))))
6301
6302 (defun gnus-trim-thread (thread)
6303   ;; Remove root ancient articles with only one child from THREAD.
6304   (if (and (eq gnus-fetch-old-headers 'some)
6305            (memq (mail-header-number (car thread)) gnus-newsgroup-ancient)
6306            (= (length thread) 2))
6307       (gnus-trim-thread (nth 1 thread))
6308     thread))
6309
6310 (defun gnus-make-sub-thread (root)
6311   ;; This function makes a sub-tree for a node in the tree.
6312   (let ((children (reverse (cdr (gnus-gethash (downcase (mail-header-id root))
6313                                               gnus-newsgroup-dependencies)))))
6314     (cons root (mapcar 'gnus-make-sub-thread children))))
6315
6316 (defun gnus-build-old-threads ()
6317   ;; Look at all the articles that refer back to old articles, and
6318   ;; fetch the headers for the articles that aren't there. This will
6319   ;; build complete threads - if the roots haven't been expired by the
6320   ;; server, that is.
6321   (let (id heads)
6322     (mapatoms
6323      (lambda (refs)
6324        (if (not (car (symbol-value refs)))
6325            (progn
6326              (setq heads (cdr (symbol-value refs)))
6327              (while heads
6328                (if (not (memq (mail-header-number (car heads))
6329                               gnus-newsgroup-dormant))
6330                    (progn
6331                      (setq id (symbol-name refs))
6332                      (while (and (setq id (gnus-build-get-header id))
6333                                  (not (car (gnus-gethash 
6334                                             id gnus-newsgroup-dependencies)))))
6335                      (setq heads nil))
6336                  (setq heads (cdr heads)))))))
6337      gnus-newsgroup-dependencies)))
6338
6339 (defun gnus-build-get-header (id)
6340   ;; Look through the buffer of NOV lines and find the header to
6341   ;; ID. Enter this line into the dependencies hash table, and return
6342   ;; the id of the parent article (if any).
6343   (let ((deps gnus-newsgroup-dependencies)
6344         found header)
6345     (prog1
6346         (save-excursion
6347           (set-buffer nntp-server-buffer)
6348           (goto-char (point-min))
6349           (while (and (not found) (search-forward id nil t))
6350             (beginning-of-line)
6351             (setq found (looking-at 
6352                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6353                                  (regexp-quote id))))
6354             (or found (beginning-of-line 2)))
6355           (if found
6356               (let (ref)
6357                 (beginning-of-line)
6358                 (and
6359                  (setq header (gnus-nov-parse-line 
6360                                (read (current-buffer)) deps))
6361                  (setq ref (mail-header-references header))
6362                  (string-match "\\(<[^>]+>\\) *$" ref)
6363                  (substring ref (match-beginning 1) (match-end 1))))))
6364       (and header
6365            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6366                  gnus-newsgroup-ancient (cons (mail-header-number header)
6367                                               gnus-newsgroup-ancient))))))
6368
6369 ;; Re-build the thread containing ID.
6370 (defun gnus-rebuild-thread (id)
6371   (let ((dep gnus-newsgroup-dependencies)
6372         (buffer-read-only nil)
6373         parent headers refs thread art)
6374     (while (and id (setq headers
6375                          (car (setq art (gnus-gethash (downcase id) dep)))))
6376       (setq parent art)
6377       (setq id (and (setq refs (mail-header-references headers))
6378                     (string-match "\\(<[^>]+>\\) *$" refs)
6379                     (substring refs (match-beginning 1) (match-end 1)))))
6380     (setq thread (gnus-make-sub-thread (car parent)))
6381     (gnus-rebuild-remove-articles thread)
6382     (let ((beg (point)))
6383       (gnus-summary-prepare-threads (list thread))
6384       (gnus-summary-update-lines beg (point)))))
6385
6386 ;; Delete all lines in the summary buffer that correspond to articles
6387 ;; in this thread.
6388 (defun gnus-rebuild-remove-articles (thread)
6389   (and (gnus-summary-goto-subject (mail-header-number (car thread)))
6390        (gnus-delete-line))
6391   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6392
6393 (defun gnus-sort-threads (threads)
6394   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6395   (let ((fun gnus-thread-sort-functions))
6396     (while fun
6397       (gnus-message 6 "Sorting with %S..." fun)
6398       (setq threads (sort threads (car fun))
6399             fun (cdr fun))))
6400   (if gnus-thread-sort-functions
6401       (gnus-message 6 "Sorting...done"))
6402   threads)
6403
6404 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6405 (defmacro gnus-thread-header (thread)
6406   ;; Return header of first article in THREAD.
6407   ;; Note that THREAD must never, evr be anything else than a variable -
6408   ;; using some other form will lead to serious barfage.
6409   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6410   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6411   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6412         (vector thread) 2))
6413
6414 (defun gnus-thread-sort-by-number (h1 h2)
6415   "Sort threads by root article number."
6416   (< (mail-header-number (gnus-thread-header h1))
6417      (mail-header-number (gnus-thread-header h2))))
6418
6419 (defun gnus-thread-sort-by-author (h1 h2)
6420   "Sort threads by root author."
6421   (string-lessp
6422    (let ((extract (funcall 
6423                    gnus-extract-address-components
6424                    (mail-header-from (gnus-thread-header h1)))))
6425      (or (car extract) (cdr extract)))
6426    (let ((extract (funcall
6427                    gnus-extract-address-components 
6428                    (mail-header-from (gnus-thread-header h2)))))
6429      (or (car extract) (cdr extract)))))
6430
6431 (defun gnus-thread-sort-by-subject (h1 h2)
6432   "Sort threads by root subject."
6433   (string-lessp
6434    (downcase (gnus-simplify-subject-re
6435               (mail-header-subject (gnus-thread-header h1))))
6436    (downcase (gnus-simplify-subject-re 
6437               (mail-header-subject (gnus-thread-header h2))))))
6438
6439 (defun gnus-thread-sort-by-date (h1 h2)
6440   "Sort threads by root article date."
6441   (string-lessp
6442    (gnus-sortable-date (mail-header-date (gnus-thread-header h1)))
6443    (gnus-sortable-date (mail-header-date (gnus-thread-header h2)))))
6444
6445 (defun gnus-thread-sort-by-score (h1 h2)
6446   "Sort threads by root article score.
6447 Unscored articles will be counted as having a score of zero."
6448   (> (or (cdr (assq (mail-header-number (gnus-thread-header h1))
6449                     gnus-newsgroup-scored))
6450          gnus-summary-default-score 0)
6451      (or (cdr (assq (mail-header-number (gnus-thread-header h2))
6452                     gnus-newsgroup-scored))
6453          gnus-summary-default-score 0)))
6454
6455 (defun gnus-thread-sort-by-total-score (h1 h2)
6456   "Sort threads by the sum of all scores in the thread.
6457 Unscored articles will be counted as having a score of zero."
6458   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6459
6460 (defun gnus-thread-total-score (thread)
6461   ;;  This function find the total score of THREAD.
6462   (if (consp thread)
6463       (if (stringp (car thread))
6464           (apply gnus-thread-score-function 0
6465                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6466         (gnus-thread-total-score-1 thread))
6467     (gnus-thread-total-score-1 (list thread))))
6468
6469 (defun gnus-thread-total-score-1 (root)
6470   ;; This function find the total score of the thread below ROOT.
6471   (setq root (car root))
6472   (apply gnus-thread-score-function
6473          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
6474              gnus-summary-default-score 0)
6475          (mapcar 'gnus-thread-total-score
6476                  (cdr (gnus-gethash (downcase (mail-header-id root))
6477                                     gnus-newsgroup-dependencies)))))
6478
6479 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6480 (defvar gnus-tmp-prev-subject "")
6481
6482 (defun gnus-summary-prepare-threads (threads &optional cull)
6483   "Prepare summary buffer from THREADS and indentation LEVEL.  
6484 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6485 or a straight list of headers."
6486   (message "Generating summary...")
6487   (let ((level 0)
6488         thread header number subject stack state gnus-tmp-gathered)
6489     (if (vectorp (car threads))
6490         ;; If this is a straight (sic) list of headers, then a
6491         ;; threaded summary display isn't required, so we just create
6492         ;; an unthreaded one.
6493         (gnus-summary-prepare-unthreaded threads cull)
6494
6495       ;; Do the threaded display.
6496
6497       (while (or threads stack)
6498         
6499         (if threads
6500             ;; If there are some threads, we do them before the
6501             ;; threads on the stack.
6502             (setq thread threads
6503                   header (car (car thread)))
6504           ;; There were no current threads, so we pop something off
6505           ;; the stack. 
6506           (setq state (car stack)
6507                 level (car state)
6508                 thread (cdr state)
6509                 stack (cdr stack)
6510                 header (car (car thread))))
6511
6512         (if (stringp header)
6513             (progn
6514               ;; The header is a dummy root.
6515               (cond 
6516                ((eq gnus-summary-make-false-root 'adopt)
6517                 ;; We let the first article adopt the rest.
6518                 (let ((th (car (cdr (car thread)))))
6519                   (while (cdr th)
6520                     (setq th (cdr th)))
6521                   (setcdr th (cdr (cdr (car thread))))
6522                   (setq gnus-tmp-gathered 
6523                         (nconc (mapcar
6524                                 (lambda (h) (mail-header-number (car h)))
6525                                 (cdr (cdr (car thread))))
6526                                gnus-tmp-gathered))
6527                   (setcdr (cdr (car thread)) nil))
6528                 (setq level -1))
6529                ((eq gnus-summary-make-false-root 'empty)
6530                 ;; We print adopted articles with empty subject fields.
6531                 (setq gnus-tmp-gathered 
6532                       (nconc (mapcar
6533                               (lambda (h) (mail-header-number (car h)))
6534                               (cdr (cdr (car thread))))
6535                              gnus-tmp-gathered))
6536                 (setq level -1))
6537                ((eq gnus-summary-make-false-root 'dummy)
6538                 ;; We output a dummy root.
6539                 (gnus-summary-insert-dummy-line 
6540                  nil header (mail-header-number
6541                              (car (car (cdr (car thread)))))))
6542                (t
6543                 ;; We do not make a root for the gathered
6544                 ;; sub-threads at all.  
6545                 (setq level -1))))
6546       
6547           (setq number (mail-header-number header)
6548                 subject (mail-header-subject header))
6549
6550           ;; Do the async thing.
6551           (and gnus-newsgroup-async
6552                (setq gnus-newsgroup-threads
6553                      (cons (cons number (mail-header-lines header)) 
6554                            gnus-newsgroup-threads)))
6555
6556           ;; We may have to root out some bad articles...
6557           (and cull
6558                (= level 0)
6559                (cond ((and (memq (setq number (mail-header-number header))
6560                                  gnus-newsgroup-dormant)
6561                            (null thread))
6562                       (setq header nil))
6563                      ((and gnus-summary-expunge-below
6564                            (< (or (cdr (assq number gnus-newsgroup-scored))
6565                                   gnus-summary-default-score 0)
6566                               gnus-summary-expunge-below))
6567                       (setq header nil)
6568                       (setq gnus-newsgroup-unreads 
6569                             (delq number gnus-newsgroup-unreads))
6570                       (setq gnus-newsgroup-reads
6571                             (cons (cons number gnus-low-score-mark)
6572                                   gnus-newsgroup-reads)))))
6573           
6574           (and
6575            header
6576            (progn
6577              (inline
6578                (gnus-summary-insert-line
6579                 nil header level nil 
6580                 (cond 
6581                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6582                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6583                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6584                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6585                  (t (or (cdr (assq number gnus-newsgroup-reads))
6586                         gnus-ancient-mark)))
6587                 (memq number gnus-newsgroup-replied)
6588                 (memq number gnus-newsgroup-expirable)
6589                 (cond
6590                  ((and gnus-thread-ignore-subject
6591                        (not (string= 
6592                              (gnus-simplify-subject-re gnus-tmp-prev-subject)
6593                              (gnus-simplify-subject-re subject))))
6594                   subject)
6595                  ((zerop level)
6596                   (if (and (eq gnus-summary-make-false-root 'empty)
6597                            (memq number gnus-tmp-gathered))
6598                       gnus-summary-same-subject
6599                     subject))
6600                  (t gnus-summary-same-subject))
6601                 (and (eq gnus-summary-make-false-root 'adopt)
6602                      (memq number gnus-tmp-gathered))
6603                 (cdr (assq number gnus-newsgroup-scored))
6604                 (memq number gnus-newsgroup-processable))
6605
6606                (setq gnus-tmp-prev-subject subject)))))
6607
6608         (if (nth 1 thread) 
6609             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6610         (setq level (1+ level))
6611         (setq threads (cdr (car thread))))))
6612   (message "Generating summary...done"))
6613
6614
6615
6616 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6617   (let (header number)
6618
6619     ;; Do the async thing, if that is required.
6620     (if gnus-newsgroup-async
6621         (setq gnus-newsgroup-threads
6622               (mapcar (lambda (h) 
6623                         (cons (mail-header-number h) (mail-header-lines h)))
6624                       headers)))
6625
6626     (while headers
6627       (setq header (car headers)
6628             headers (cdr headers)
6629             number (mail-header-number header))
6630
6631       ;; We may have to root out some bad articles...
6632       (cond 
6633        ((and cull
6634              (memq (setq number (mail-header-number header))
6635                    gnus-newsgroup-dormant)))
6636        ((and cull gnus-summary-expunge-below
6637              (< (or (cdr (assq number gnus-newsgroup-scored))
6638                     gnus-summary-default-score 0)
6639                 gnus-summary-expunge-below))
6640         (setq gnus-newsgroup-unreads 
6641               (delq number gnus-newsgroup-unreads))
6642         (setq gnus-newsgroup-reads
6643               (cons (cons number gnus-low-score-mark)
6644                     gnus-newsgroup-reads)))
6645        (t
6646         (gnus-summary-insert-line
6647          nil header 0 nil 
6648          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6649                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6650                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6651                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6652                (t (or (cdr (assq number gnus-newsgroup-reads))
6653                       gnus-ancient-mark)))
6654          (memq number gnus-newsgroup-replied)
6655          (memq number gnus-newsgroup-expirable)
6656          (mail-header-subject header) nil
6657          (cdr (assq number gnus-newsgroup-scored))
6658          (memq number gnus-newsgroup-processable)))))))
6659
6660 (defun gnus-select-newsgroup (group &optional read-all)
6661   "Select newsgroup GROUP.
6662 If READ-ALL is non-nil, all articles in the group are selected."
6663   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6664          (info (nth 2 entry))
6665          articles)
6666
6667     (or (gnus-check-server
6668          (setq gnus-current-select-method (gnus-find-method-for-group group)))
6669         (error "Couldn't open server"))
6670     
6671     (or (and entry (not (eq (car entry) t))) ; Either it's active...
6672         (gnus-activate-group group) ; Or we can activate it...
6673         (progn ; Or we bug out.
6674           (kill-buffer (current-buffer))
6675           (error "Couldn't request group %s: %s" 
6676                  group (gnus-status-message group))))
6677
6678     (setq gnus-newsgroup-name group)
6679     (setq gnus-newsgroup-unselected nil)
6680     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6681
6682     (and gnus-asynchronous
6683          (gnus-check-backend-function 
6684           'request-asynchronous gnus-newsgroup-name)
6685          (setq gnus-newsgroup-async
6686                (gnus-request-asynchronous gnus-newsgroup-name)))
6687
6688     (setq articles (gnus-articles-to-read group read-all))
6689
6690     (cond 
6691      ((null articles) 
6692       (gnus-message 3 "Couldn't select newsgroup")
6693       'quit)
6694      ((eq articles 0) nil)
6695      (t
6696       ;; Init the dependencies hash table.
6697       (setq gnus-newsgroup-dependencies 
6698             (gnus-make-hashtable (length articles)))
6699       ;; Retrieve the headers and read them in.
6700       (gnus-message 5 "Fetching headers...")
6701       (setq gnus-newsgroup-headers 
6702             (if (eq 'nov (setq gnus-headers-retrieved-by
6703                                ;; This is a naughty hack. To get the
6704                                ;; retrieval of old headers to work, we
6705                                ;; set `nntp-nov-gap' to nil (locally),
6706                                ;; and then just retrieve the headers.
6707                                ;; Mucho magic.
6708                                (if gnus-fetch-old-headers
6709                                    (let (nntp-nov-gap)
6710                                      (gnus-retrieve-headers 
6711                                       (if (not (eq 1 (car articles)))
6712                                           (cons 1 articles)
6713                                         articles)
6714                                       gnus-newsgroup-name))
6715                                  (gnus-retrieve-headers 
6716                                   articles gnus-newsgroup-name))))
6717                 (progn
6718                   (gnus-get-newsgroup-headers-xover articles))
6719               ;; If we were to fetch old headers, but the backend didn't
6720               ;; support XOVER, then it is possible we fetched one article
6721               ;; that we shouldn't have. If that's the case, we remove it.
6722               (if (or (not gnus-fetch-old-headers)
6723                       (eq 1 (car articles)))
6724                   ()
6725                 (save-excursion
6726                   (set-buffer nntp-server-buffer)
6727                   (goto-char (point-min))
6728                   (and 
6729                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6730                    (delete-region       ; So we delete this head.
6731                     (point) 
6732                     (search-forward "\n.\n" nil t)))))
6733               (gnus-get-newsgroup-headers)))
6734       (gnus-message 5 "Fetching headers...done")      
6735       ;; Remove canceled articles from the list of unread articles.
6736       (setq gnus-newsgroup-unreads
6737             (gnus-set-sorted-intersection 
6738              gnus-newsgroup-unreads
6739              (mapcar (lambda (headers) (mail-header-number headers))
6740                      gnus-newsgroup-headers)))
6741       ;; Adjust and set lists of article marks.
6742       (and info
6743            (let (marked)
6744              (gnus-adjust-marked-articles info)
6745              (setq gnus-newsgroup-marked 
6746                    (copy-sequence
6747                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6748              (setq gnus-newsgroup-replied 
6749                    (copy-sequence (cdr (assq 'reply marked))))
6750              (setq gnus-newsgroup-expirable
6751                    (copy-sequence (cdr (assq 'expire marked))))
6752              (setq gnus-newsgroup-killed
6753                    (copy-sequence (cdr (assq 'killed marked))))
6754              (setq gnus-newsgroup-bookmarks 
6755                    (copy-sequence (cdr (assq 'bookmark marked))))
6756              (setq gnus-newsgroup-dormant 
6757                    (copy-sequence (cdr (assq 'dormant marked))))
6758              (setq gnus-newsgroup-scored 
6759                    (copy-sequence (cdr (assq 'score marked))))
6760              (setq gnus-newsgroup-processable nil)))
6761       ;; Check whether auto-expire is to be done in this group.
6762       (setq gnus-newsgroup-auto-expire
6763             (or (and (stringp gnus-auto-expirable-newsgroups)
6764                      (string-match gnus-auto-expirable-newsgroups group))
6765                 (memq 'auto-expire (nth 5 info))))
6766       ;; First and last article in this newsgroup.
6767       (and gnus-newsgroup-headers
6768            (setq gnus-newsgroup-begin 
6769                  (mail-header-number (car gnus-newsgroup-headers)))
6770            (setq gnus-newsgroup-end
6771                  (mail-header-number
6772                   (gnus-last-element gnus-newsgroup-headers))))
6773       (setq gnus-reffed-article-number -1)
6774       ;; GROUP is successfully selected.
6775       (or gnus-newsgroup-headers t)))))
6776
6777 (defun gnus-articles-to-read (group read-all)
6778   ;; Find out what articles the user wants to read.
6779   (let* ((articles
6780           ;; Select all articles if `read-all' is non-nil, or if all the
6781           ;; unread articles are dormant articles.
6782           (if (or (and read-all (not (numberp read-all)))
6783                   (= (length gnus-newsgroup-unreads) 
6784                      (length gnus-newsgroup-dormant)))
6785               (gnus-uncompress-range 
6786                (gnus-gethash group gnus-active-hashtb))
6787             gnus-newsgroup-unreads))
6788          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6789          (scored (length scored-list))
6790          (number (length articles))
6791          (marked (+ (length gnus-newsgroup-marked)
6792                     (length gnus-newsgroup-dormant)))
6793          (select
6794           (cond 
6795            ((numberp read-all)
6796             read-all)
6797            (t
6798             (condition-case ()
6799                 (cond ((and (or (<= scored marked)
6800                                 (= scored number))
6801                             (numberp gnus-large-newsgroup)
6802                             (> number gnus-large-newsgroup))
6803                        (let ((input
6804                               (read-string
6805                                (format
6806                                 "How many articles from %s (default %d): "
6807                                 gnus-newsgroup-name number))))
6808                          (if (string-match "^[ \t]*$" input)
6809                              number input)))
6810                       ((and (> scored marked) (< scored number))
6811                        (let ((input
6812                               (read-string
6813                                (format 
6814                                 "%s %s (%d scored, %d total): "
6815                                 "How many articles from"
6816                                 group scored number))))
6817                          (if (string-match "^[ \t]*$" input)
6818                              number input)))
6819                       (t number))
6820               (quit nil))))))
6821     (setq select (if (stringp select) (string-to-number select) select))
6822     (if (or (null select) (zerop select))
6823         select
6824       (if (and (not (zerop scored)) (<= (abs select) scored))
6825           (progn
6826             (setq articles (sort scored-list '<))
6827             (setq number (length articles)))
6828         (setq articles (copy-sequence articles)))
6829
6830       (if (< (abs select) number)
6831           (if (< select 0) 
6832               ;; Select the N oldest articles.
6833               (setcdr (nthcdr (1- (abs select)) articles) nil)
6834             ;; Select the N most recent articles.
6835             (setq articles (nthcdr (- number select) articles))))
6836       (setq gnus-newsgroup-unselected
6837             (gnus-sorted-intersection
6838              gnus-newsgroup-unreads
6839              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6840       articles)))
6841
6842 (defun gnus-killed-articles (killed articles)
6843   (let (out)
6844     (while articles
6845       (if (inline (gnus-member-of-range (car articles) killed))
6846           (setq out (cons (car articles) out)))
6847       (setq articles (cdr articles)))
6848     out))
6849
6850 (defun gnus-adjust-marked-articles (info &optional active)
6851   "Remove all marked articles that are no longer legal."
6852   (let ((marked-lists (nth 3 info))
6853         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6854         m prev)
6855     ;; There are many types of marked articles.
6856     (while marked-lists
6857       (setq m (cdr (setq prev (car marked-lists))))
6858       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6859              ;; Make sure that all ticked articles are a subset of the
6860              ;; unread/unselected articles.
6861              (while m
6862                (if (or (memq (car m) gnus-newsgroup-unreads)
6863                        (memq (car m) gnus-newsgroup-unselected))
6864                    (setq prev m)
6865                  (setcdr prev (cdr m)))
6866                (setq m (cdr m))))
6867             ((eq 'score (car prev))
6868              ;; Scored articles should be a subset of
6869              ;; unread/unselected articles. 
6870              (while m
6871                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6872                        (memq (car (car m)) gnus-newsgroup-unreads))
6873                    (setq prev m)
6874                  (setcdr prev (cdr m)))
6875                (setq m (cdr m))))
6876             ((eq 'bookmark (car prev))
6877              ;; Bookmarks should be a subset of active articles.
6878              (while m
6879                (if (< (car (car m)) (car active))
6880                    (setcdr prev (cdr m))
6881                  (setq prev m))
6882                (setq m (cdr m))))
6883             ((eq 'killed (car prev))
6884              ;; Articles that have been through the kill process are
6885              ;; to be a subset of active articles.
6886              (while (and m (< (or (and (numberp (car m)) (car m))
6887                                   (cdr (car m)))
6888                               (car active)))
6889                (setcdr prev (cdr m))
6890                (setq m (cdr m)))
6891              (if (and m (< (or (and (numberp (car m)) (car m))
6892                                (car (car m)))
6893                            (car active))) 
6894                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6895             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6896              ;; The replied and expirable articles have to be articles
6897              ;; that are active. 
6898              (while m
6899                (if (< (car m) (car active))
6900                    (setcdr prev (cdr m))
6901                  (setq prev m))
6902                (setq m (cdr m)))))
6903       (setq marked-lists (cdr marked-lists)))
6904     ;; Remove all lists that are empty.
6905     (setq marked-lists (nth 3 info))
6906     (if marked-lists
6907         (progn
6908           (while (= 1 (length (car marked-lists)))
6909             (setq marked-lists (cdr marked-lists)))
6910           (setq m (cdr (setq prev marked-lists)))
6911           (while m
6912             (if (= 1 (length (car m)))
6913                 (setcdr prev (cdr m))
6914               (setq prev m))
6915             (setq m (cdr m)))
6916           (setcar (nthcdr 3 info) marked-lists)))
6917     ;; Finally, if there are no marked lists at all left, and if there
6918     ;; are no elements after the lists in the info list, we just chop
6919     ;; the info list off before the marked lists.
6920     (and (null marked-lists) 
6921          (not (nthcdr 4 info))
6922          (setcdr (nthcdr 2 info) nil)))
6923   info)
6924
6925 (defun gnus-set-marked-articles 
6926   (info ticked replied expirable killed dormant bookmark score) 
6927   "Enter the various lists of marked articles into the newsgroup info list."
6928   (let (newmarked)
6929     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6930     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6931     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6932                                          newmarked)))
6933     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6934     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6935     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6936                                         newmarked)))
6937     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6938     (if (nthcdr 3 info)
6939         (progn
6940           (setcar (nthcdr 3 info) newmarked)
6941           (and (not newmarked)
6942                (not (nthcdr 4 info))
6943                (setcdr (nthcdr 2 info) nil)))
6944       (if newmarked
6945           (setcdr (nthcdr 2 info) (list newmarked))))))
6946
6947 (defun gnus-add-marked-articles (group type articles &optional info force)
6948   ;; Add ARTICLES of TYPE to the info of GROUP.
6949   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6950   ;; add, but replace marked articles of TYPE with ARTICLES.
6951   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6952         marked m)
6953     (or (not info)
6954         (and (not (setq marked (nthcdr 3 info)))
6955              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6956         (and (not (setq m (assq type (car marked))))
6957              (setcar marked (cons (cons type articles) (car marked))))
6958         (if force
6959             (setcdr m articles)
6960           (nconc m articles)))))
6961          
6962 (defun gnus-set-mode-line (where)
6963   "This function sets the mode line of the article or summary buffers.
6964 If WHERE is `summary', the summary mode line format will be used."
6965   (if (memq where gnus-updated-mode-lines)
6966       (let (mode-string)
6967         (save-excursion
6968           (set-buffer gnus-summary-buffer)
6969           (let* ((mformat (if (eq where 'article) 
6970                               gnus-article-mode-line-format-spec
6971                             gnus-summary-mode-line-format-spec))
6972                  (group-name gnus-newsgroup-name)
6973                  (article-number (or gnus-current-article 0))
6974                  (unread (- (length gnus-newsgroup-unreads)
6975                             (length gnus-newsgroup-dormant)))
6976                  (unread-and-unticked 
6977                   (- unread (length gnus-newsgroup-marked)))
6978                  (unselected (length gnus-newsgroup-unselected))
6979                  (unread-and-unselected
6980                   (cond ((and (zerop unread-and-unticked)
6981                               (zerop unselected)) "")
6982                         ((zerop unselected) 
6983                          (format "{%d more}" unread-and-unticked))
6984                         (t (format "{%d(+%d) more}"
6985                                    unread-and-unticked unselected))))
6986                  (subject
6987                   (if gnus-current-headers
6988                       (mail-header-subject gnus-current-headers) ""))
6989                  (max-len (and gnus-mode-non-string-length
6990                                (- (frame-width) gnus-mode-non-string-length)))
6991                  header);; passed as argument to any user-format-funcs
6992             (setq mode-string (eval mformat))
6993             (or (numberp max-len)
6994                 (setq max-len (length mode-string)))
6995             (if (< max-len 4) (setq max-len 4))
6996             (if (> (length mode-string) max-len)
6997                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6998                 ;;  function `substring' might cut on a middle
6999                 ;;  of multi-octet character.
7000                 (setq mode-string 
7001                       (concat (gnus-truncate-string mode-string (- max-len 3))
7002                               "...")))
7003             (setq mode-string (format (format "%%-%ds" max-len)
7004                                       mode-string))))
7005         (setq mode-line-buffer-identification mode-string)
7006         (set-buffer-modified-p t))))
7007
7008 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
7009   "Go through the HEADERS list and add all Xrefs to a hash table.
7010 The resulting hash table is returned, or nil if no Xrefs were found."
7011   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
7012          (prefix (if (and 
7013                       (gnus-group-foreign-p from-newsgroup)
7014                       (not (memq 'virtual 
7015                                  (assoc (symbol-name (car from-method))
7016                                         gnus-valid-select-methods))))
7017                      (gnus-group-real-prefix from-newsgroup)))
7018          (xref-hashtb (make-vector 63 0))
7019          start group entry number xrefs header)
7020     (while headers
7021       (setq header (car headers))
7022       (if (and (setq xrefs (mail-header-xref header))
7023                (not (memq (mail-header-number header) unreads)))
7024           (progn
7025             (setq start 0)
7026             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
7027               (setq start (match-end 0))
7028               (setq group (concat prefix (substring xrefs (match-beginning 1) 
7029                                                     (match-end 1))))
7030               (setq number 
7031                     (string-to-int (substring xrefs (match-beginning 2) 
7032                                               (match-end 2))))
7033               (if (setq entry (gnus-gethash group xref-hashtb))
7034                   (setcdr entry (cons number (cdr entry)))
7035                 (gnus-sethash group (cons number nil) xref-hashtb)))))
7036       (setq headers (cdr headers)))
7037     (if start xref-hashtb nil)))
7038
7039 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
7040   "Look through all the headers and mark the Xrefs as read."
7041   (let ((virtual (memq 'virtual 
7042                        (assoc (symbol-name (car (gnus-find-method-for-group 
7043                                                  from-newsgroup)))
7044                               gnus-valid-select-methods)))
7045         name entry info xref-hashtb idlist method
7046         nth4)
7047     (save-excursion
7048       (set-buffer gnus-group-buffer)
7049       (if (setq xref-hashtb 
7050                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
7051           (mapatoms 
7052            (lambda (group)
7053              (if (string= from-newsgroup (setq name (symbol-name group)))
7054                  ()
7055                (setq idlist (symbol-value group))
7056                ;; Dead groups are not updated.
7057                (if (and (prog1 
7058                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
7059                                   info (nth 2 entry))
7060                           (if (stringp (setq nth4 (nth 4 info)))
7061                               (setq nth4 (gnus-server-to-method nth4))))
7062                         ;; Only do the xrefs if the group has the same
7063                         ;; select method as the group we have just read.
7064                         (or (gnus-methods-equal-p 
7065                              nth4 (gnus-find-method-for-group from-newsgroup))
7066                             virtual
7067                             (equal nth4 
7068                                    (setq method (gnus-find-method-for-group 
7069                                                  from-newsgroup)))
7070                             (and (equal (car nth4) (car method))
7071                                  (equal (nth 1 nth4) (nth 1 method))))
7072                         gnus-use-cross-reference
7073                         (or (not (eq gnus-use-cross-reference t))
7074                             virtual
7075                             ;; Only do cross-references on subscribed
7076                             ;; groups, if that is what is wanted.  
7077                             (<= (nth 1 info) gnus-level-subscribed)))
7078                    (gnus-group-make-articles-read name idlist expirable))))
7079            xref-hashtb)))))
7080
7081 (defun gnus-group-make-articles-read (group articles expirable)
7082   (let* ((num 0)
7083          (entry (gnus-gethash group gnus-newsrc-hashtb))
7084          (info (nth 2 entry))
7085          (active (gnus-gethash group gnus-active-hashtb))
7086          exps expirable range)
7087     ;; First peel off all illegal article numbers.
7088     (if active
7089         (let ((ids articles)
7090               (ticked (cdr (assq 'tick (nth 3 info))))
7091               (dormant (cdr (assq 'dormant (nth 3 info))))
7092               id first)
7093           (setq exps nil)
7094           (while ids
7095             (setq id (car ids))
7096             (if (and first (> id (cdr active)))
7097                 (progn
7098                   ;; We'll end up in this situation in one particular
7099                   ;; obscure situation. If you re-scan a group and get
7100                   ;; a new article that is cross-posted to a different
7101                   ;; group that has not been re-scanned, you might get
7102                   ;; crossposted article that has a higher number than
7103                   ;; Gnus believes possible. So we re-activate this
7104                   ;; group as well. This might mean doing the
7105                   ;; crossposting thingie will *increase* the number
7106                   ;; of articles in some groups. Tsk, tsk.
7107                   (setq active (or (gnus-activate-group group) active))))
7108             (if (or (> id (cdr active))
7109                     (< id (car active))
7110                     (memq id ticked)
7111                     (memq id dormant))
7112                 (setq articles (delq id articles)))
7113             (and (memq id expirable)
7114                  (setq exps (cons id exps)))
7115             (setq ids (cdr ids)))))
7116     ;; Update expirable articles.
7117     (gnus-add-marked-articles nil 'expirable exps info)
7118     (and active
7119          (null (nth 2 info))
7120          (> (car active) 1)
7121          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
7122     (setcar (nthcdr 2 info)
7123             (setq range
7124                   (gnus-add-to-range 
7125                    (nth 2 info) 
7126                    (setq articles (sort articles '<)))))
7127     ;; Then we have to re-compute how many unread
7128     ;; articles there are in this group.
7129     (if active
7130         (progn
7131           (cond 
7132            ((not range)
7133             (setq num (- (1+ (cdr active)) (car active))))
7134            ((not (listp (cdr range)))
7135             (setq num (- (cdr active) (- (1+ (cdr range)) 
7136                                          (car range)))))
7137            (t
7138             (while range
7139               (if (numberp (car range))
7140                   (setq num (1+ num))
7141                 (setq num (+ num (- (1+ (cdr (car range)))
7142                                     (car (car range))))))
7143               (setq range (cdr range)))
7144             (setq num (- (cdr active) num))))
7145           ;; Update the number of unread articles.
7146           (setcar 
7147            entry 
7148            (max 0 (- num 
7149                      (length (cdr (assq 'tick (nth 3 info))))
7150                      (length 
7151                       (cdr (assq 'dormant (nth 3 info)))))))
7152           ;; Update the group buffer.
7153           (gnus-group-update-group group t)))))
7154
7155 (defun gnus-methods-equal-p (m1 m2)
7156   (let ((m1 (or m1 gnus-select-method))
7157         (m2 (or m2 gnus-select-method)))
7158     (or (equal m1 m2)
7159         (and (eq (car m1) (car m2))
7160              (or (not (memq 'address (assoc (symbol-name (car m1))
7161                                             gnus-valid-select-methods)))
7162                  (equal (nth 1 m1) (nth 1 m2)))))))
7163
7164 (defsubst gnus-header-value ()
7165   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7166
7167 (defvar gnus-newsgroup-none-id 0)
7168
7169 (defun gnus-get-newsgroup-headers ()
7170   (setq gnus-article-internal-prepare-hook nil)
7171   (let ((cur nntp-server-buffer)
7172         (dependencies gnus-newsgroup-dependencies)
7173         headers id dep end ref)
7174     (save-excursion
7175       (set-buffer nntp-server-buffer)
7176       (goto-char (point-min))
7177       ;; Search to the beginning of the next header. Error messages
7178       ;; do not begin with 2 or 3.
7179       (while (re-search-forward "^[23][0-9]+ " nil t)
7180         (let ((header (make-vector 9 nil))
7181               (case-fold-search t)
7182               (p (point))
7183               in-reply-to)
7184           (setq id nil
7185                 ref nil)
7186           (mail-header-set-number header (read cur))
7187           ;; This implementation of this function, with nine
7188           ;; search-forwards instead of the one re-search-forward and
7189           ;; a case (which basically was the old function) is actually
7190           ;; about twice as fast, even though it looks messier. You
7191           ;; can't have everything, I guess. Speed and elegance
7192           ;; doesn't always come hand in hand.
7193           (save-restriction
7194             (narrow-to-region (point) (or (save-excursion 
7195                                             (search-forward "\n.\n" nil t))
7196                                           (point)))
7197             (if (search-forward "\nfrom: " nil t)
7198                 (mail-header-set-from header (gnus-header-value))
7199               (mail-header-set-from header "(nobody)"))
7200             (goto-char p)
7201             (if (search-forward "\nsubject: " nil t)
7202                 (mail-header-set-subject header (gnus-header-value))
7203               (mail-header-set-subject header "(none)"))
7204             (goto-char p)
7205             (and (search-forward "\nxref: " nil t)
7206                  (mail-header-set-xref header (gnus-header-value)))
7207             (goto-char p)
7208             (or (numberp (and (search-forward "\nlines: " nil t)
7209                               (mail-header-set-lines header (read cur))))
7210                 (mail-header-set-lines header 0))
7211             (goto-char p)
7212             (and (search-forward "\ndate: " nil t)
7213                  (mail-header-set-date header (gnus-header-value)))
7214             (goto-char p)
7215             (if (search-forward "\nmessage-id: " nil t)
7216                 (mail-header-set-id header (setq id (gnus-header-value)))
7217               ;; If there was no message-id, we just fake one to make
7218               ;; subsequent routines simpler.
7219               (mail-header-set-id 
7220                header 
7221                (setq id (concat "none+" 
7222                                 (int-to-string 
7223                                  (setq gnus-newsgroup-none-id 
7224                                        (1+ gnus-newsgroup-none-id)))))))
7225             (goto-char p)
7226             (if (search-forward "\nreferences: " nil t)
7227                 (progn
7228                   (mail-header-set-references header (gnus-header-value))
7229                   (setq end (match-end 0))
7230                   (save-excursion
7231                     (setq ref 
7232                           (downcase
7233                            (buffer-substring
7234                             (progn 
7235                               (end-of-line)
7236                               (search-backward ">" end t)
7237                               (1+ (point)))
7238                             (progn
7239                               (search-backward "<" end t)
7240                               (point)))))))
7241               ;; Get the references from the in-reply-to header if there
7242               ;; ware no references and the in-reply-to header looks
7243               ;; promising. 
7244               (if (and (search-forward "\nin-reply-to: " nil t)
7245                        (setq in-reply-to (gnus-header-value))
7246                        (string-match "<[^>]+>" in-reply-to))
7247                   (progn
7248                     (mail-header-set-references 
7249                      header 
7250                      (setq ref (substring in-reply-to (match-beginning 0)
7251                                           (match-end 0))))
7252                     (setq ref (downcase ref)))
7253                 (setq ref "none")))
7254             ;; We do some threading while we read the headers. The
7255             ;; message-id and the last reference are both entered into
7256             ;; the same hash table. Some tippy-toeing around has to be
7257             ;; done in case an article has arrived before the article
7258             ;; which it refers to.
7259             (if (boundp (setq dep (intern (downcase id) dependencies)))
7260                 (if (car (symbol-value dep))
7261                     ;; An article with this Message-ID has already
7262                     ;; been seen, so we ignore this one, except we add
7263                     ;; any additional Xrefs (in case the two articles
7264                     ;; came from different servers.
7265                     (progn
7266                       (mail-header-set-xref 
7267                        (car (symbol-value dep))
7268                        (concat (or (mail-header-xref 
7269                                     (car (symbol-value dep))) "")
7270                                (or (mail-header-xref header) "")))
7271                       (setq header nil))
7272                   (setcar (symbol-value dep) header))
7273               (set dep (list header)))
7274             (if header
7275                 (progn
7276                   (if (boundp (setq dep (intern ref dependencies)))
7277                       (setcdr (symbol-value dep) 
7278                               (cons header (cdr (symbol-value dep))))
7279                     (set dep (list nil header)))
7280                   (setq headers (cons header headers))))
7281             (goto-char (point-max))))))
7282     (nreverse headers)))
7283
7284 ;; The following macros and functions were written by Felix Lee
7285 ;; <flee@cse.psu.edu>. 
7286
7287 (defmacro gnus-nov-read-integer ()
7288   '(prog1
7289        (if (= (following-char) ?\t)
7290            0
7291          (let ((num (condition-case nil (read buffer) (error nil))))
7292            (if (numberp num) num 0)))
7293      (or (eobp) (forward-char 1))))
7294
7295 (defmacro gnus-nov-skip-field ()
7296   '(search-forward "\t" eol 'move))
7297
7298 (defmacro gnus-nov-field ()
7299   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7300
7301 ;; Goes through the xover lines and returns a list of vectors
7302 (defun gnus-get-newsgroup-headers-xover (sequence)
7303   "Parse the news overview data in the server buffer, and return a
7304 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7305   ;; Get the Xref when the users reads the articles since most/some
7306   ;; NNTP servers do not include Xrefs when using XOVER.
7307   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7308   (let ((cur nntp-server-buffer)
7309         (dependencies gnus-newsgroup-dependencies)
7310         number headers header)
7311     (save-excursion
7312       (set-buffer nntp-server-buffer)
7313       (goto-char (point-min))
7314       (while (and sequence (not (eobp)))
7315         (setq number (read cur))
7316         (while (and sequence (< (car sequence) number))
7317           (setq sequence (cdr sequence)))
7318         (and sequence 
7319              (eq number (car sequence))
7320              (progn
7321                (setq sequence (cdr sequence))
7322                (if (setq header 
7323                          (inline (gnus-nov-parse-line number dependencies)))
7324                    (setq headers (cons header headers)))))
7325         (forward-line 1))
7326       (setq headers (nreverse headers)))
7327     headers))
7328
7329 ;; This function has to be called with point after the article number
7330 ;; on the beginning of the line.
7331 (defun gnus-nov-parse-line (number dependencies)
7332   (let ((none 0)
7333         (eol (gnus-point-at-eol)) 
7334         (buffer (current-buffer))
7335         header ref id dep)
7336
7337     ;; overview: [num subject from date id refs chars lines misc]
7338     (narrow-to-region (point) eol)
7339     (or (eobp) (forward-char))
7340
7341     (condition-case nil
7342         (setq header
7343               (vector 
7344                number                   ; number
7345                (gnus-nov-field)         ; subject
7346                (gnus-nov-field)         ; from
7347                (gnus-nov-field)         ; date
7348                (setq id (or (gnus-nov-field)
7349                             (concat "none+"
7350                                     (int-to-string 
7351                                      (setq none (1+ none)))))) ; id
7352                (progn
7353                  (save-excursion
7354                    (let ((beg (point)))
7355                      (search-forward "\t" eol)
7356                      (if (search-backward ">" beg t)
7357                          (setq ref 
7358                                (downcase 
7359                                 (buffer-substring 
7360                                  (1+ (point))
7361                                  (progn
7362                                    (search-backward "<" beg t)
7363                                    (point)))))
7364                        (setq ref nil))))
7365                  (gnus-nov-field))      ; refs
7366                (gnus-nov-read-integer)  ; chars
7367                (gnus-nov-read-integer)  ; lines
7368                (if (= (following-char) ?\n)
7369                    nil
7370                  (gnus-nov-field))      ; misc
7371                ))
7372       (error (progn 
7373                (ding)
7374                (message "Strange nov line.")
7375                (setq header nil)
7376                (goto-char eol))))
7377
7378     (widen)
7379
7380     ;; We build the thread tree.
7381     (and header
7382          (if (boundp (setq dep (intern (downcase id) dependencies)))
7383              (if (car (symbol-value dep))
7384                  ;; An article with this Message-ID has already been seen,
7385                  ;; so we ignore this one, except we add any additional
7386                  ;; Xrefs (in case the two articles came from different
7387                  ;; servers.
7388                  (progn
7389                    (mail-header-set-xref 
7390                     (car (symbol-value dep))
7391                     (concat (or (mail-header-xref (car (symbol-value dep))) "")
7392                             (or (mail-header-xref header) "")))
7393                    (setq header nil))
7394                (setcar (symbol-value dep) header))
7395            (set dep (list header))))
7396     (if header
7397         (progn
7398           (if (boundp (setq dep (intern (or ref "none") 
7399                                         dependencies)))
7400               (setcdr (symbol-value dep) 
7401                       (cons header (cdr (symbol-value dep))))
7402             (set dep (list nil header)))))
7403     header))
7404
7405 (defun gnus-article-get-xrefs ()
7406   "Fill in the Xref value in `gnus-current-headers', if necessary.
7407 This is meant to be called in `gnus-article-internal-prepare-hook'."
7408   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7409                                  gnus-current-headers)))
7410     (or (not gnus-use-cross-reference)
7411         (not headers)
7412         (and (mail-header-xref headers)
7413              (not (string= (mail-header-xref headers) "")))
7414         (let ((case-fold-search t)
7415               xref)
7416           (save-restriction
7417             (gnus-narrow-to-headers)
7418             (goto-char (point-min))
7419             (if (or (and (eq (downcase (following-char)) ?x)
7420                          (looking-at "Xref:"))
7421                     (search-forward "\nXref:" nil t))
7422                 (progn
7423                   (goto-char (1+ (match-end 0)))
7424                   (setq xref (buffer-substring (point) 
7425                                                (progn (end-of-line) (point))))
7426                   (mail-header-set-xref headers xref))))))))
7427
7428 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7429 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7430
7431 (defun gnus-make-headers-hashtable-by-number ()
7432   "Make hashtable for the variable gnus-newsgroup-headers by number."
7433   (save-excursion
7434     (set-buffer gnus-summary-buffer)
7435     (let ((headers gnus-newsgroup-headers)
7436           header)
7437       (setq gnus-newsgroup-headers-hashtb-by-number
7438             (gnus-make-hashtable (length headers)))
7439       (while headers
7440         (setq header (car headers))
7441         (gnus-sethash (int-to-string (mail-header-number header))
7442                       header gnus-newsgroup-headers-hashtb-by-number)
7443         (setq headers (cdr headers))))))
7444
7445 (defun gnus-more-header-backward ()
7446   "Find new header backward."
7447   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7448         (artnum gnus-newsgroup-begin)
7449         (header nil))
7450     (while (and (not header)
7451                 (> artnum first))
7452       (setq artnum (1- artnum))
7453       (setq header (gnus-read-header artnum)))
7454     header))
7455
7456 (defun gnus-more-header-forward (&optional backward)
7457   "Find new header forward.
7458 If BACKWARD, find new header backward instead."
7459   (if backward
7460       (gnus-more-header-backward)
7461     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7462           (artnum gnus-newsgroup-end)
7463           (header nil))
7464       (while (and (not header)
7465                   (< artnum last))
7466         (setq artnum (1+ artnum))
7467         (setq header (gnus-read-header artnum)))
7468       header)))
7469
7470 (defun gnus-extend-newsgroup (header &optional backward)
7471   "Extend newsgroup selection with HEADER.
7472 Optional argument BACKWARD means extend toward backward."
7473   (if header
7474       (let ((artnum (mail-header-number header)))
7475         (setq gnus-newsgroup-headers
7476               (if backward
7477                   (cons header gnus-newsgroup-headers)
7478                 (nconc gnus-newsgroup-headers (list header))))
7479         (setq gnus-newsgroup-unselected
7480               (delq artnum gnus-newsgroup-unselected))
7481         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7482         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7483
7484 (defun gnus-summary-work-articles (n)
7485   "Return a list of articles to be worked upon. The prefix argument,
7486 the list of process marked articles, and the current article will be
7487 taken into consideration."
7488   (let (articles)
7489     (if (and n (numberp n))
7490         (let ((backward (< n 0))
7491               (n (abs n)))
7492           (save-excursion
7493             (while (and (> n 0)
7494                         (setq articles (cons (gnus-summary-article-number) 
7495                                              articles))
7496                         (gnus-summary-search-forward nil nil backward))
7497               (setq n (1- n))))
7498           (sort articles (function <)))
7499       (or (reverse gnus-newsgroup-processable)
7500           (list (gnus-summary-article-number))))))
7501
7502 (defun gnus-summary-search-group (&optional backward use-level)
7503   "Search for next unread newsgroup.
7504 If optional argument BACKWARD is non-nil, search backward instead."
7505   (save-excursion
7506     (set-buffer gnus-group-buffer)
7507     (if (gnus-group-search-forward 
7508          backward nil (if use-level (gnus-group-group-level) nil))
7509         (gnus-group-group-name))))
7510
7511 (defun gnus-summary-best-group (&optional exclude-group)
7512   "Find the name of the best unread group.
7513 If EXCLUDE-GROUP, do not go to this group."
7514   (save-excursion
7515     (set-buffer gnus-group-buffer)
7516     (save-excursion
7517       (gnus-group-best-unread-group exclude-group))))
7518
7519 (defun gnus-subject-equal (s1 s2)
7520   (cond
7521    ((null gnus-summary-gather-subject-limit)
7522     (equal (gnus-simplify-subject-re s1)
7523            (gnus-simplify-subject-re s2)))
7524    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7525     (equal (gnus-simplify-subject-fuzzy s1)
7526            (gnus-simplify-subject-fuzzy s2)))
7527    ((numberp gnus-summary-gather-subject-limit)
7528     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7529            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7530    (t
7531     (equal s1 s2))))
7532     
7533 (defun gnus-summary-search-subject (&optional backward unread subject)
7534   "Search for article forward.
7535 If BACKWARD is non-nil, search backward.
7536 If UNREAD is non-nil, only unread articles are selected.
7537 If SUBJECT is non-nil, the article which has the same subject will be
7538 searched for." 
7539   (let ((func (if backward 'previous-single-property-change
7540                 'next-single-property-change))
7541         (beg (point))
7542         (did t)
7543         pos psubject)
7544     (beginning-of-line)
7545     (and gnus-summary-check-current unread
7546          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7547          (setq did nil))
7548     (if (not did)
7549         ()
7550       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7551       (while
7552           (and 
7553            (setq pos (funcall func (point) 'gnus-number))
7554            (goto-char (if backward (1- pos) pos))
7555            (setq did
7556                  (not (and
7557                        (or (not unread)
7558                            (eq (get-text-property (point) 'gnus-mark)
7559                                gnus-unread-mark))
7560                        (or (not subject)
7561                            (and (setq psubject 
7562                                       (inline (gnus-summary-subject-string)))
7563                                 (inline 
7564                                   (gnus-subject-equal subject psubject)))))))
7565            (if backward (if (bobp) nil (forward-char -1) t)
7566              (if (eobp) nil (forward-char 1) t)))))
7567     (if did
7568         (progn (goto-char beg) nil)
7569       (prog1
7570           (get-text-property (point) 'gnus-number)
7571         (gnus-summary-show-thread)
7572         (gnus-summary-position-cursor)))))
7573
7574 (defun gnus-summary-pseudo-article ()
7575   "The thread level of the article on the current line."
7576   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7577
7578 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7579 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7580 (defun gnus-summary-article-score ()
7581   "Return current article score."
7582   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7583       gnus-summary-default-score 0))
7584
7585 (defun gnus-summary-recenter ()
7586   "Center point in the summary window.
7587 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7588 displayed, no centering will be performed." 
7589   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7590   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7591   (let* ((top (cond ((< (window-height) 4) 0)
7592                     ((< (window-height) 7) 1)
7593                     (t 2)))
7594          (height (1- (window-height)))
7595          (bottom (save-excursion (goto-char (point-max))
7596                                  (forward-line (- height))
7597                                  (point)))
7598          (window (get-buffer-window (current-buffer))))
7599     (and 
7600      ;; The user has to want it,
7601      gnus-auto-center-summary 
7602      ;; the article buffer must be displayed,
7603      (get-buffer-window gnus-article-buffer)
7604      ;; Set the window start to either `bottom', which is the biggest
7605      ;; possible valid number, or the second line from the top,
7606      ;; whichever is the least.
7607      (set-window-start
7608       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7609
7610 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7611 (defun gnus-short-group-name (group &optional levels)
7612   "Collapse GROUP name LEVELS."
7613   (let* ((name "") (foreign "") (depth -1) (skip 1)
7614          (levels (or levels
7615                      (progn
7616                        (while (string-match "\\." group skip)
7617                          (setq skip (match-end 0)
7618                                depth (+ depth 1)))
7619                        depth))))
7620     (if (string-match ":" group)
7621         (setq foreign (substring group 0 (match-end 0))
7622               group (substring group (match-end 0))))
7623     (while group
7624       (if (and (string-match "\\." group) (> levels 0))
7625           (setq name (concat name (substring group 0 1))
7626                 group (substring group (match-end 0))
7627                 levels (- levels 1)
7628                 name (concat name "."))
7629         (setq name (concat foreign name group)
7630               group nil)))
7631     name))
7632
7633 (defun gnus-summary-jump-to-group (newsgroup)
7634   "Move point to NEWSGROUP in group mode buffer."
7635   ;; Keep update point of group mode buffer if visible.
7636   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7637       (save-window-excursion
7638         ;; Take care of tree window mode.
7639         (if (get-buffer-window gnus-group-buffer)
7640             (pop-to-buffer gnus-group-buffer))
7641         (gnus-group-jump-to-group newsgroup))
7642     (save-excursion
7643       ;; Take care of tree window mode.
7644       (if (get-buffer-window gnus-group-buffer)
7645           (pop-to-buffer gnus-group-buffer)
7646         (set-buffer gnus-group-buffer))
7647       (gnus-group-jump-to-group newsgroup))))
7648
7649 ;; This function returns a list of article numbers based on the
7650 ;; difference between the ranges of read articles in this group and
7651 ;; the range of active articles.
7652 (defun gnus-list-of-unread-articles (group)
7653   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7654          (active (gnus-gethash group gnus-active-hashtb))
7655          (last (cdr active))
7656          first nlast unread)
7657     ;; If none are read, then all are unread. 
7658     (if (not read)
7659         (setq first (car active))
7660       ;; If the range of read articles is a single range, then the
7661       ;; first unread article is the article after the last read
7662       ;; article. Sounds logical, doesn't it?
7663       (if (not (listp (cdr read)))
7664           (setq first (1+ (cdr read)))
7665         ;; `read' is a list of ranges.
7666         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7667                                 (car (car read)))) 1)
7668             (setq first 1))
7669         (while read
7670           (if first 
7671               (while (< first nlast)
7672                 (setq unread (cons first unread))
7673                 (setq first (1+ first))))
7674           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7675           (setq nlast (if (atom (car (cdr read))) 
7676                           (car (cdr read))
7677                         (car (car (cdr read)))))
7678           (setq read (cdr read)))))
7679     ;; And add the last unread articles.
7680     (while (<= first last)
7681       (setq unread (cons first unread))
7682       (setq first (1+ first)))
7683     ;; Return the list of unread articles.
7684     (nreverse unread)))
7685
7686 (defun gnus-list-of-read-articles (group)
7687   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7688         (active (gnus-gethash group gnus-active-hashtb)))
7689     (and info active
7690          (gnus-sorted-complement 
7691           (gnus-uncompress-range active) 
7692           (gnus-list-of-unread-articles group)))))
7693
7694 ;; Various summary commands
7695
7696 (defun gnus-summary-universal-argument ()
7697   "Perform any operation on all articles marked with the process mark."
7698   (interactive)
7699   (gnus-set-global-variables)
7700   (let ((articles (reverse gnus-newsgroup-processable))
7701         func)
7702     (or articles (error "No articles marked"))
7703     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7704         (error "Undefined key"))
7705     (while articles
7706       (gnus-summary-goto-subject (car articles))
7707       (command-execute func)
7708       (gnus-summary-remove-process-mark (car articles))
7709       (setq articles (cdr articles)))))
7710
7711 (defun gnus-summary-toggle-truncation (&optional arg)
7712   "Toggle truncation of summary lines.
7713 With arg, turn line truncation on iff arg is positive."
7714   (interactive "P")
7715   (setq truncate-lines
7716         (if (null arg) (not truncate-lines)
7717           (> (prefix-numeric-value arg) 0)))
7718   (redraw-display))
7719
7720 (defun gnus-summary-reselect-current-group (&optional all)
7721   "Once exit and then reselect the current newsgroup.
7722 The prefix argument ALL means to select all articles."
7723   (interactive "P")
7724   (gnus-set-global-variables)
7725   (let ((current-subject (gnus-summary-article-number))
7726         (group gnus-newsgroup-name))
7727     (setq gnus-newsgroup-begin nil)
7728     (gnus-summary-exit t)
7729     ;; We have to adjust the point of group mode buffer because the
7730     ;; current point was moved to the next unread newsgroup by
7731     ;; exiting.
7732     (gnus-summary-jump-to-group group)
7733     (gnus-group-read-group all t)
7734     (gnus-summary-goto-subject current-subject)))
7735
7736 (defun gnus-summary-rescan-group (&optional all)
7737   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7738   (interactive "P")
7739   (gnus-set-global-variables)
7740   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7741   (let ((group gnus-newsgroup-name))
7742     (gnus-summary-exit)
7743     (gnus-summary-jump-to-group group)
7744     (save-excursion
7745       (set-buffer gnus-group-buffer)
7746       (gnus-group-get-new-news-this-group 1))
7747     (gnus-summary-jump-to-group group)
7748     (gnus-group-read-group all)))
7749
7750 (defun gnus-summary-update-info ()
7751   (let* ((group gnus-newsgroup-name))
7752     (if gnus-newsgroup-kill-headers
7753         (setq gnus-newsgroup-killed
7754               (gnus-compress-sequence
7755                (nconc
7756                 (gnus-set-sorted-intersection
7757                  (gnus-uncompress-range gnus-newsgroup-killed)
7758                  (setq gnus-newsgroup-unselected
7759                        (sort gnus-newsgroup-unselected '<)))
7760                 (setq gnus-newsgroup-unreads
7761                       (sort gnus-newsgroup-unreads '<))) t)))
7762     (or (listp (cdr gnus-newsgroup-killed))
7763         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7764     (let ((headers gnus-newsgroup-headers))
7765       (gnus-close-group group)
7766       (run-hooks 'gnus-exit-group-hook)
7767       (gnus-update-read-articles 
7768        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7769        gnus-newsgroup-marked
7770        t gnus-newsgroup-replied gnus-newsgroup-expirable
7771        gnus-newsgroup-killed gnus-newsgroup-dormant
7772        gnus-newsgroup-bookmarks 
7773        (and gnus-save-score gnus-newsgroup-scored))
7774       (and gnus-use-cross-reference
7775            (gnus-mark-xrefs-as-read 
7776             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7777       ;; Do adaptive scoring, and possibly save score files.
7778       (and gnus-newsgroup-adaptive
7779            (gnus-score-adaptive))
7780       (and gnus-use-scoring 
7781            (fboundp 'gnus-score-save)
7782            (funcall 'gnus-score-save))
7783       ;; Do not switch windows but change the buffer to work.
7784       (set-buffer gnus-group-buffer)
7785       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7786           (gnus-group-update-group group)))))
7787   
7788 (defun gnus-summary-exit (&optional temporary)
7789   "Exit reading current newsgroup, and then return to group selection mode.
7790 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7791   (interactive)
7792   (gnus-set-global-variables)
7793   (gnus-kill-save-kill-buffer)
7794   (let* ((group gnus-newsgroup-name)
7795          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7796                                                   gnus-newsgroup-name))))
7797          (mode major-mode)
7798          (buf (current-buffer)))
7799     (run-hooks 'gnus-summary-prepare-exit-hook)
7800     ;; Make all changes in this group permanent.
7801     (gnus-summary-update-info)          
7802     (set-buffer buf)
7803     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7804     ;; Make sure where I was, and go to next newsgroup.
7805     (set-buffer gnus-group-buffer)
7806     (or quit-config
7807         (progn
7808           (gnus-group-jump-to-group group)
7809           (gnus-group-next-unread-group 1)))
7810     (if temporary
7811         nil                             ;Nothing to do.
7812       ;; We set all buffer-local variables to nil. It is unclear why
7813       ;; this is needed, but if we don't, buffer-local variables are
7814       ;; not garbage-collected, it seems. This would the lead to en
7815       ;; ever-growing Emacs.
7816       (set-buffer buf)
7817       (gnus-summary-clear-local-variables)
7818       ;; We clear the global counterparts of the buffer-local
7819       ;; variables as well, just to be on the safe side.
7820       (gnus-configure-windows 'group 'force)
7821       (gnus-summary-clear-local-variables)
7822       ;; Return to group mode buffer. 
7823       (if (eq mode 'gnus-summary-mode)
7824           (gnus-kill-buffer buf))
7825       (if (get-buffer gnus-article-buffer)
7826           (bury-buffer gnus-article-buffer))
7827       (setq gnus-current-select-method gnus-select-method)
7828       (pop-to-buffer gnus-group-buffer)
7829       (if (not quit-config)
7830           (progn
7831             (gnus-group-jump-to-group group)
7832             (gnus-group-next-unread-group 1))
7833         (if (not (buffer-name (car quit-config)))
7834             (gnus-configure-windows 'group 'force)
7835           (set-buffer (car quit-config))
7836           (and (eq major-mode 'gnus-summary-mode)
7837                (gnus-set-global-variables))
7838           (gnus-configure-windows (cdr quit-config))))
7839       (run-hooks 'gnus-summary-exit-hook))))
7840
7841 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7842 (defun gnus-summary-exit-no-update (&optional no-questions)
7843   "Quit reading current newsgroup without updating read article info."
7844   (interactive)
7845   (gnus-set-global-variables)
7846   (let* ((group gnus-newsgroup-name)
7847          (quit-config (nth 1 (assoc 'quit-config 
7848                                     (gnus-find-method-for-group group)))))
7849     (if (or no-questions
7850             gnus-expert-user
7851             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7852         (progn
7853           (gnus-close-group group)
7854           (gnus-summary-clear-local-variables)
7855           (set-buffer gnus-group-buffer)
7856           (gnus-summary-clear-local-variables)
7857           ;; Return to group selection mode.
7858           (gnus-configure-windows 'group 'force)
7859           (if (get-buffer gnus-summary-buffer)
7860               (kill-buffer gnus-summary-buffer))
7861           (if (get-buffer gnus-article-buffer)
7862               (bury-buffer gnus-article-buffer))
7863           (if (equal (gnus-group-group-name) group)
7864               (gnus-group-next-unread-group 1))
7865           (if quit-config
7866               (progn
7867                 (if (not (buffer-name (car quit-config)))
7868                     (gnus-configure-windows 'group 'force)
7869                   (set-buffer (car quit-config))
7870                   (and (eq major-mode 'gnus-summary-mode)
7871                        (gnus-set-global-variables))
7872                   (gnus-configure-windows (cdr quit-config)))))))))
7873
7874 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7875 (defun gnus-summary-fetch-faq (group)
7876   "Fetch the FAQ for the current group."
7877   (interactive (list gnus-newsgroup-name))
7878   (let (gnus-faq-buffer)
7879     (and (setq gnus-faq-buffer (gnus-group-fetch-faq group))
7880          (gnus-configure-windows 'summary-faq))))
7881
7882 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7883 (defun gnus-summary-describe-group (&optional force)
7884   "Describe the current newsgroup."
7885   (interactive "P")
7886   (gnus-group-describe-group force gnus-newsgroup-name))
7887
7888 (defun gnus-summary-describe-briefly ()
7889   "Describe summary mode commands briefly."
7890   (interactive)
7891   (gnus-message 6
7892                 (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")))
7893
7894 ;; Walking around group mode buffer from summary mode.
7895
7896 (defun gnus-summary-next-group (&optional no-article target-group backward)
7897   "Exit current newsgroup and then select next unread newsgroup.
7898 If prefix argument NO-ARTICLE is non-nil, no article is selected
7899 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7900 previous group instead."
7901   (interactive "P")
7902   (gnus-set-global-variables)
7903   (let ((current-group gnus-newsgroup-name)
7904         (current-buffer (current-buffer))
7905         entered)
7906     ;; First we semi-exit this group to update Xrefs and all variables.
7907     ;; We can't do a real exit, because the window conf must remain
7908     ;; the same in case the user is prompted for info, and we don't
7909     ;; want the window conf to change before that...
7910     (gnus-summary-exit t)
7911     (while (not entered)
7912       ;; Then we find what group we are supposed to enter.
7913       (set-buffer gnus-group-buffer)
7914       (gnus-group-jump-to-group current-group)
7915       (setq target-group 
7916             (or target-group        
7917                 (if (eq gnus-keep-same-level 'best) 
7918                     (gnus-summary-best-group gnus-newsgroup-name)
7919                   (gnus-summary-search-group backward gnus-keep-same-level))))
7920       (if (not target-group)
7921           ;; There are no further groups, so we return to the group
7922           ;; buffer.
7923           (progn
7924             (gnus-message 5 "Returning to the group buffer")
7925             (setq entered t)
7926             (set-buffer current-buffer)
7927             (gnus-summary-exit))
7928         ;; We try to enter the target group.
7929         (gnus-group-jump-to-group target-group)
7930         (let ((unreads (gnus-group-group-unread)))
7931           (if (and (or (eq t unreads)
7932                        (and unreads (not (zerop unreads))))
7933                    (gnus-summary-read-group
7934                     target-group nil no-article current-buffer))
7935               (setq entered t)
7936             (setq current-group target-group
7937                   target-group nil)))))))
7938
7939 (defun gnus-summary-next-group-old (&optional no-article group backward)
7940   "Exit current newsgroup and then select next unread newsgroup.
7941 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7942 If BACKWARD, go to previous group instead."
7943   (interactive "P")
7944   (gnus-set-global-variables)
7945   (let ((ingroup gnus-newsgroup-name)
7946         (sumbuf (current-buffer))
7947         num)
7948     (set-buffer gnus-group-buffer)
7949     (if (and group
7950              (or (and (numberp (setq num (car (gnus-gethash
7951                                                group gnus-newsrc-hashtb))))
7952                       (< num 1))
7953                  (null num)))
7954         (progn
7955           (gnus-group-jump-to-group group)
7956           (setq group nil))
7957       (gnus-group-jump-to-group ingroup))
7958     (gnus-summary-search-group backward)
7959     (let ((group (or group (gnus-summary-search-group backward))))
7960       (set-buffer sumbuf)
7961       (gnus-summary-exit t)             ;Update all information.
7962       (if (null group)
7963           (gnus-summary-exit-no-update t)
7964         (gnus-group-jump-to-group ingroup)
7965         (setq group (gnus-summary-search-group backward))
7966         (gnus-message 5 "Selecting %s..." group)
7967         (set-buffer gnus-group-buffer)
7968         ;; We are now in group mode buffer.
7969         ;; Make sure group mode buffer point is on GROUP.
7970         (gnus-group-jump-to-group group)
7971         (if (not (eq gnus-auto-select-next 'quietly))
7972             (progn
7973               (gnus-summary-read-group group nil no-article sumbuf)
7974               (and (string= gnus-newsgroup-name ingroup)
7975                    (bufferp sumbuf) (buffer-name sumbuf)
7976                    (progn
7977                      (set-buffer (setq gnus-summary-buffer sumbuf))
7978                      (gnus-summary-exit-no-update t))))
7979           (let ((prevgroup group))
7980             (gnus-group-jump-to-group ingroup)
7981             (setq group (gnus-summary-search-group backward))
7982             (gnus-summary-read-group group nil no-article sumbuf)
7983             (while (and (string= gnus-newsgroup-name ingroup)
7984                         (bufferp sumbuf) 
7985                         (buffer-name sumbuf)
7986                         (not (string= prevgroup (gnus-group-group-name))))
7987               (set-buffer gnus-group-buffer)
7988               (gnus-summary-read-group 
7989                (setq prevgroup (gnus-group-group-name)) 
7990                nil no-article sumbuf))
7991             (and (string= prevgroup (gnus-group-group-name))
7992                  ;; We have reached the final group in the group
7993                  ;; buffer.
7994                  (progn
7995                    (if (buffer-name sumbuf)
7996                        (progn
7997                          (set-buffer sumbuf)
7998                          (gnus-summary-exit)))))))))))
7999
8000 (defun gnus-summary-prev-group (&optional no-article)
8001   "Exit current newsgroup and then select previous unread newsgroup.
8002 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
8003   (interactive "P")
8004   (gnus-summary-next-group no-article nil t))
8005
8006 ;; Walking around summary lines.
8007
8008 (defun gnus-summary-first-subject (&optional unread)
8009   "Go to the first unread subject.
8010 If UNREAD is non-nil, go to the first unread article.
8011 Returns nil if there are no unread articles."
8012   (interactive "P")
8013   (prog1
8014       (cond ((not unread)
8015              (goto-char (point-min)))
8016             ((gnus-goto-char 
8017               (text-property-any 
8018                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
8019              t)
8020             (t 
8021              ;; There are no unread articles.
8022              (gnus-message 3 "No more unread articles")
8023              nil))
8024     (gnus-summary-position-cursor)))
8025
8026 (defun gnus-summary-next-subject (n &optional unread dont-display)
8027   "Go to next N'th summary line.
8028 If N is negative, go to the previous N'th subject line.
8029 If UNREAD is non-nil, only unread articles are selected.
8030 The difference between N and the actual number of steps taken is
8031 returned."
8032   (interactive "p")
8033   (let ((backward (< n 0))
8034         (n (abs n)))
8035     (while (and (> n 0)
8036                 (gnus-summary-search-forward unread nil backward))
8037       (setq n (1- n)))
8038     (if (/= 0 n) (gnus-message 7 "No more%s articles"
8039                                (if unread " unread" "")))
8040     (or dont-display
8041         (progn
8042           (gnus-summary-recenter)
8043           (gnus-summary-position-cursor)))
8044     n))
8045
8046 (defun gnus-summary-next-unread-subject (n)
8047   "Go to next N'th unread summary line."
8048   (interactive "p")
8049   (gnus-summary-next-subject n t))
8050
8051 (defun gnus-summary-prev-subject (n &optional unread)
8052   "Go to previous N'th summary line.
8053 If optional argument UNREAD is non-nil, only unread article is selected."
8054   (interactive "p")
8055   (gnus-summary-next-subject (- n) unread))
8056
8057 (defun gnus-summary-prev-unread-subject (n)
8058   "Go to previous N'th unread summary line."
8059   (interactive "p")
8060   (gnus-summary-next-subject (- n) t))
8061
8062 (defun gnus-summary-goto-subject (article)
8063   "Go the subject line of ARTICLE."
8064   (interactive
8065    (list
8066     (string-to-int
8067      (completing-read "Article number: "
8068                       (mapcar
8069                        (lambda (headers)
8070                          (list
8071                           (int-to-string (mail-header-number headers))))
8072                        gnus-newsgroup-headers)
8073                       nil 'require-match))))
8074   (or article (error "No article number"))
8075   (let ((b (point)))
8076     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
8077                                                 'gnus-number article)))
8078         ()
8079       (gnus-summary-show-thread)
8080       ;; Skip dummy articles. 
8081       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8082           (forward-line 1))
8083       (prog1
8084           (if (not (eobp))
8085               article
8086             (goto-char b)
8087             nil)
8088         (gnus-summary-position-cursor)))))
8089
8090 ;; Walking around summary lines with displaying articles.
8091
8092 (defun gnus-summary-expand-window (&optional arg)
8093   "Make the summary buffer take up the entire Emacs frame.
8094 Given a prefix, will force an `article' buffer configuration."
8095   (interactive "P")
8096   (gnus-set-global-variables)
8097   (if arg
8098       (gnus-configure-windows 'article 'force)
8099     (gnus-configure-windows 'summary 'force)))
8100
8101 (defun gnus-summary-display-article (article &optional all-header)
8102   "Display ARTICLE in article buffer."
8103   (gnus-set-global-variables)
8104   (if (null article)
8105       nil
8106     (prog1
8107         (gnus-article-prepare article all-header)
8108       (gnus-summary-show-thread)
8109       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8110           (progn
8111             (forward-line 1)
8112             (gnus-summary-position-cursor)))
8113       (run-hooks 'gnus-select-article-hook)
8114       (gnus-summary-recenter)
8115       (gnus-summary-goto-subject article)
8116       ;; Successfully display article.
8117       (gnus-summary-update-line)
8118       (gnus-article-set-window-start 
8119        (cdr (assq article gnus-newsgroup-bookmarks)))
8120       t)))
8121
8122 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
8123   "Select the current article.
8124 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
8125 non-nil, the article will be re-fetched even if it already present in
8126 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
8127 be displayed."
8128   (and (not pseudo) (gnus-summary-pseudo-article)
8129        (error "This is a pseudo-article."))
8130   (let ((article (or article (gnus-summary-article-number)))
8131         (all-headers (not (not all-headers))) ;Must be T or NIL.
8132         did) 
8133     (prog1
8134         (save-excursion
8135           (set-buffer gnus-summary-buffer)
8136           (if (or (null gnus-current-article)
8137                   (null gnus-article-current)
8138                   (null (get-buffer gnus-article-buffer))
8139                   (not (eq article (cdr gnus-article-current)))
8140                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
8141                   force)
8142               ;; The requested article is different from the current article.
8143               (progn
8144                 (gnus-summary-display-article article all-headers)
8145                 (setq did article))
8146             (if (or all-headers gnus-show-all-headers) 
8147                 (gnus-article-show-all-headers))
8148             nil))
8149       (if did 
8150           (gnus-article-set-window-start 
8151            (cdr (assq article gnus-newsgroup-bookmarks)))))))
8152
8153 (defun gnus-summary-set-current-mark (&optional current-mark)
8154   "Obsolete function."
8155   nil)
8156
8157 (defun gnus-summary-next-article (&optional unread subject backward)
8158   "Select the next article.
8159 If UNREAD, only unread articles are selected.
8160 If SUBJECT, only articles with SUBJECT are selected.
8161 If BACKWARD, the previous article is selected instead of the next."
8162   (interactive "P")
8163   (gnus-set-global-variables)
8164   (let (header)
8165     (cond
8166      ;; Is there such an article?
8167      ((and (gnus-summary-search-forward unread subject backward)
8168            (or (gnus-summary-display-article (gnus-summary-article-number))
8169                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8170       (gnus-summary-position-cursor))
8171      ;; If not, we try the first unread, if that is wanted.
8172      ((and subject
8173            gnus-auto-select-same
8174            (or (gnus-summary-first-unread-article)
8175                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8176       (gnus-summary-position-cursor)
8177       (gnus-message 6 "Wrapped"))
8178      ;; Try to get next/previous article not displayed in this group.
8179      ((and gnus-auto-extend-newsgroup
8180            (not unread) (not subject)
8181            (setq header (gnus-more-header-forward backward)))
8182       (gnus-extend-newsgroup header backward)
8183       (let ((buffer-read-only nil))
8184         (goto-char (if backward (point-min) (point-max)))
8185         (gnus-summary-prepare-threads (list header)))
8186       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8187                                    gnus-newsgroup-end)))
8188      ;; Go to next/previous group.
8189      (t
8190       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8191           (gnus-summary-jump-to-group gnus-newsgroup-name))
8192       (let ((cmd last-command-char)
8193             (group 
8194              (if (eq gnus-keep-same-level 'best) 
8195                  (gnus-summary-best-group gnus-newsgroup-name)
8196                (gnus-summary-search-group backward gnus-keep-same-level))))
8197         ;; For some reason, the group window gets selected. We change
8198         ;; it back.  
8199         (select-window (get-buffer-window (current-buffer)))
8200         ;; Keep just the event type of CMD.
8201                                         ;(and (listp cmd) (setq cmd (car cmd)))
8202         ;; Select next unread newsgroup automagically.
8203         (cond 
8204          ((not gnus-auto-select-next)
8205           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8206          ((eq gnus-auto-select-next 'quietly)
8207           ;; Select quietly.
8208           (if (assoc 'quit-config (gnus-find-method-for-group 
8209                                    gnus-newsgroup-name))
8210               (gnus-summary-exit)
8211             (gnus-message 7 "No more%s articles (%s)..."
8212                           (if unread " unread" "") 
8213                           (if group (concat "selecting " group)
8214                             "exiting"))
8215             (gnus-summary-next-group nil group backward)))
8216          (t
8217           (let ((keystrokes '(?\C-n ?\C-p))
8218                 key)
8219             (while (or (null key) (memq key keystrokes))
8220               (gnus-message 
8221                7 "No more%s articles%s" (if unread " unread" "")
8222                (if (and group (not (assoc 'quit-config
8223                                           (gnus-find-method-for-group 
8224                                            gnus-newsgroup-name))))
8225                    (format " (Type %s for %s [%s])"
8226                            (single-key-description cmd) group
8227                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8228                  (format " (Type %s to exit %s)"
8229                          (single-key-description cmd)
8230                          gnus-newsgroup-name)))
8231               ;; Confirm auto selection.
8232               (let* ((event (read-char)))
8233                 (setq key (if (listp event) (car event) event))
8234                 (if (memq key keystrokes)
8235                     (let ((obuf (current-buffer)))
8236                       (switch-to-buffer gnus-group-buffer)
8237                       (and group
8238                            (gnus-group-jump-to-group group))
8239                       (condition-case ()
8240                           (execute-kbd-macro (char-to-string key))
8241                         (error (ding) nil))
8242                       (setq group (gnus-group-group-name))
8243                       (switch-to-buffer obuf)))))
8244             (if (equal key cmd)
8245                 (if (or (not group) (assoc 'quit-config
8246                                            (gnus-find-method-for-group
8247                                             gnus-newsgroup-name)))
8248                     (gnus-summary-exit)
8249                   (gnus-summary-next-group nil group backward))
8250               (execute-kbd-macro (char-to-string key)))))))))))
8251
8252 (defun gnus-summary-next-unread-article ()
8253   "Select unread article after current one."
8254   (interactive)
8255   (gnus-summary-next-article t (and gnus-auto-select-same
8256                                     (gnus-summary-subject-string))))
8257
8258 (defun gnus-summary-prev-article (&optional unread subject)
8259   "Select the article after the current one.
8260 If UNREAD is non-nil, only unread articles are selected."
8261   (interactive "P")
8262   (gnus-summary-next-article unread subject t))
8263
8264 (defun gnus-summary-prev-unread-article ()
8265   "Select unred article before current one."
8266   (interactive)
8267   (gnus-summary-prev-article t (and gnus-auto-select-same
8268                                     (gnus-summary-subject-string))))
8269
8270 (defun gnus-summary-next-page (&optional lines circular)
8271   "Show next page of selected article.
8272 If end of article, select next article.
8273 Argument LINES specifies lines to be scrolled up.
8274 If CIRCULAR is non-nil, go to the start of the article instead of 
8275 instead of selecting the next article when reaching the end of the
8276 current article." 
8277   (interactive "P")
8278   (setq gnus-summary-buffer (current-buffer))
8279   (gnus-set-global-variables)
8280   (let ((article (gnus-summary-article-number))
8281         (endp nil))
8282     (gnus-configure-windows 'article)
8283     (if (or (null gnus-current-article)
8284             (null gnus-article-current)
8285             (/= article (cdr gnus-article-current))
8286             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8287         ;; Selected subject is different from current article's.
8288         (gnus-summary-display-article article)
8289       (gnus-eval-in-buffer-window
8290        gnus-article-buffer
8291        (setq endp (gnus-article-next-page lines)))
8292       (if endp
8293           (cond (circular
8294                  (gnus-summary-beginning-of-article))
8295                 (lines
8296                  (gnus-message 3 "End of message"))
8297                 ((null lines)
8298                  (gnus-summary-next-unread-article)))))
8299     (gnus-summary-recenter)
8300     (gnus-summary-position-cursor)))
8301
8302 (defun gnus-summary-prev-page (&optional lines)
8303   "Show previous page of selected article.
8304 Argument LINES specifies lines to be scrolled down."
8305   (interactive "P")
8306   (gnus-set-global-variables)
8307   (let ((article (gnus-summary-article-number)))
8308     (gnus-configure-windows 'article)
8309     (if (or (null gnus-current-article)
8310             (null gnus-article-current)
8311             (/= article (cdr gnus-article-current))
8312             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8313         ;; Selected subject is different from current article's.
8314         (gnus-summary-display-article article)
8315       (gnus-summary-recenter)
8316       (gnus-eval-in-buffer-window gnus-article-buffer
8317                                   (gnus-article-prev-page lines))))
8318   (gnus-summary-position-cursor))
8319
8320 (defun gnus-summary-scroll-up (lines)
8321   "Scroll up (or down) one line current article.
8322 Argument LINES specifies lines to be scrolled up (or down if negative)."
8323   (interactive "p")
8324   (gnus-set-global-variables)
8325   (gnus-configure-windows 'article)
8326   (or (gnus-summary-select-article nil nil 'pseudo)
8327       (gnus-eval-in-buffer-window 
8328        gnus-article-buffer
8329        (cond ((> lines 0)
8330               (if (gnus-article-next-page lines)
8331                   (gnus-message 3 "End of message")))
8332              ((< lines 0)
8333               (gnus-article-prev-page (- lines))))))
8334   (gnus-summary-recenter)
8335   (gnus-summary-position-cursor))
8336
8337 (defun gnus-summary-next-same-subject ()
8338   "Select next article which has the same subject as current one."
8339   (interactive)
8340   (gnus-set-global-variables)
8341   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8342
8343 (defun gnus-summary-prev-same-subject ()
8344   "Select previous article which has the same subject as current one."
8345   (interactive)
8346   (gnus-set-global-variables)
8347   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8348
8349 (defun gnus-summary-next-unread-same-subject ()
8350   "Select next unread article which has the same subject as current one."
8351   (interactive)
8352   (gnus-set-global-variables)
8353   (gnus-summary-next-article t (gnus-summary-subject-string)))
8354
8355 (defun gnus-summary-prev-unread-same-subject ()
8356   "Select previous unread article which has the same subject as current one."
8357   (interactive)
8358   (gnus-set-global-variables)
8359   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8360
8361 (defun gnus-summary-first-unread-article ()
8362   "Select the first unread article. 
8363 Return nil if there are no unread articles."
8364   (interactive)
8365   (gnus-set-global-variables)
8366   (prog1
8367       (if (gnus-summary-first-subject t)
8368           (progn
8369             (gnus-summary-show-thread)
8370             (gnus-summary-first-subject t)
8371             (gnus-summary-display-article (gnus-summary-article-number))))
8372     (gnus-summary-position-cursor)))
8373
8374 (defun gnus-summary-best-unread-article ()
8375   "Select the unread article with the highest score."
8376   (interactive)
8377   (gnus-set-global-variables)
8378   (let ((best -1000000)
8379         article score)
8380     (save-excursion
8381       (or (gnus-summary-first-subject t)
8382           (error "No unread articles"))
8383       (while 
8384           (and
8385            (progn
8386              (and (> (setq score (gnus-summary-article-score)) best)
8387                   (setq best score
8388                         article (gnus-summary-article-number)))
8389              t)
8390            (gnus-summary-search-subject nil t))))
8391     (if (not article)
8392         (error "No unread articles")
8393       (gnus-summary-goto-article article))
8394     (gnus-summary-position-cursor)))
8395
8396 (defun gnus-summary-goto-article (article &optional all-headers)
8397   "Fetch ARTICLE and display it if it exists.
8398 If ALL-HEADERS is non-nil, no header lines are hidden."
8399   (interactive
8400    (list
8401     (string-to-int
8402      (completing-read 
8403       "Article number: "
8404       (mapcar (lambda (headers) 
8405                 (list (int-to-string (mail-header-number headers))))
8406               gnus-newsgroup-headers) 
8407       nil 'require-match))))
8408   (prog1
8409       (and (gnus-summary-goto-subject article)
8410            (gnus-summary-display-article article all-headers))
8411     (gnus-summary-position-cursor)))
8412
8413 (defun gnus-summary-goto-last-article ()
8414   "Go to the previously read article."
8415   (interactive)
8416   (prog1
8417       (and gnus-last-article
8418            (gnus-summary-goto-article gnus-last-article))
8419     (gnus-summary-position-cursor)))
8420
8421 (defun gnus-summary-pop-article (number)
8422   "Pop one article off the history and go to the previous.
8423 NUMBER articles will be popped off."
8424   (interactive "p")
8425   (let (to)
8426     (setq gnus-newsgroup-history
8427           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8428     (if to
8429         (gnus-summary-goto-article (car to))
8430       (error "Article history empty")))
8431   (gnus-summary-position-cursor))
8432
8433 ;; Summary article oriented commands
8434
8435 (defun gnus-summary-refer-parent-article (n)
8436   "Refer parent article N times.
8437 The difference between N and the number of articles fetched is returned."
8438   (interactive "p")
8439   (gnus-set-global-variables)
8440   (while 
8441       (and 
8442        (> n 0)
8443        (let ((ref (mail-header-references (gnus-get-header-by-num
8444                                            (gnus-summary-article-number)))))
8445          (if (and ref (not (equal ref ""))
8446                   (string-match "<[^<>]*>[ \t]*$" ref))
8447              (gnus-summary-refer-article 
8448               (substring ref (match-beginning 0) (match-end 0)))
8449            (gnus-message 1 "No references in article %d"
8450                          (gnus-summary-article-number))
8451            nil)))
8452     (setq n (1- n)))
8453   (gnus-summary-position-cursor)
8454   n)
8455     
8456 (defun gnus-summary-refer-article (message-id)
8457   "Refer article specified by MESSAGE-ID.
8458 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8459   (interactive "sMessage-ID: ")
8460   (if (or (not (stringp message-id))
8461           (zerop (length message-id)))
8462       ()
8463     ;; Construct the correct Message-ID if necessary.
8464     ;; Suggested by tale@pawl.rpi.edu.
8465     (or (string-match "^<" message-id)
8466         (setq message-id (concat "<" message-id)))
8467     (or (string-match ">$" message-id)
8468         (setq message-id (concat message-id ">")))
8469     (let ((header (car (gnus-gethash (downcase message-id)
8470                                      gnus-newsgroup-dependencies))))
8471       (if header
8472           (or (gnus-summary-goto-article (mail-header-number header))
8473               ;; The header has been read, but the article had been
8474               ;; expunged, so we insert it again.
8475               (progn
8476                 (gnus-summary-insert-line
8477                  nil header 0 nil gnus-read-mark nil nil
8478                  (mail-header-subject header))
8479                 (forward-line -1)
8480                 (mail-header-number header)))
8481         (let ((gnus-override-method gnus-refer-article-method)
8482               (gnus-ancient-mark gnus-read-mark)
8483               (tmp-point (window-start
8484                           (get-buffer-window gnus-article-buffer)))
8485               number tmp-buf)
8486           (and gnus-refer-article-method
8487                (gnus-check-server gnus-refer-article-method))
8488           ;; Save the old article buffer.
8489           (save-excursion
8490             (set-buffer (gnus-article-setup-buffer))
8491             (gnus-kill-buffer " *temp Article*")
8492             (setq tmp-buf (rename-buffer " *temp Article*")))
8493           (prog1
8494               (if (gnus-article-prepare 
8495                    message-id nil (gnus-read-header message-id))
8496                   (progn
8497                     (setq number (mail-header-number gnus-current-headers))
8498                     (gnus-rebuild-thread message-id)
8499                     (gnus-summary-goto-subject number)
8500                     (if (null gnus-use-full-window)
8501                         (progn
8502                           (delete-windows-on tmp-buf)
8503                           (gnus-configure-windows 'article 'force)))
8504                     (gnus-summary-recenter)
8505                     (gnus-article-set-window-start 
8506                      (cdr (assq number gnus-newsgroup-bookmarks)))
8507                     message-id)
8508                 ;; We restore the old article buffer.
8509                 (save-excursion
8510                   (kill-buffer gnus-article-buffer)
8511                   (set-buffer tmp-buf)
8512                   (rename-buffer gnus-article-buffer)
8513                   (let ((buffer-read-only nil))
8514                     (and tmp-point
8515                          (set-window-start (get-buffer-window (current-buffer))
8516                                            tmp-point)))))))))))
8517
8518 (defun gnus-summary-enter-digest-group ()
8519   "Enter a digest group based on the current article."
8520   (interactive)
8521   (gnus-set-global-variables)
8522   (gnus-summary-select-article)
8523   ;; We do not want a narrowed article.
8524   (gnus-summary-stop-page-breaking)
8525   (let ((name (format "%s-%d" 
8526                       (gnus-group-prefixed-name 
8527                        gnus-newsgroup-name (list 'nndoc "")) 
8528                       gnus-current-article))
8529         (ogroup gnus-newsgroup-name)
8530         (buf (current-buffer)))
8531     (if (gnus-group-read-ephemeral-group 
8532          name (list 'nndoc name
8533                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8534                     '(nndoc-article-type digest))
8535          t)
8536         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8537                 (list (list (cons 'to-group ogroup))))
8538       (switch-to-buffer buf)
8539       (gnus-set-global-variables)
8540       (gnus-configure-windows 'summary)
8541       (gnus-message 3 "Article not a digest?"))))
8542
8543 (defun gnus-summary-isearch-article ()
8544   "Do incremental search forward on current article."
8545   (interactive)
8546   (gnus-set-global-variables)
8547   (gnus-summary-select-article)
8548   (gnus-eval-in-buffer-window 
8549    gnus-article-buffer (isearch-forward)))
8550
8551 (defun gnus-summary-search-article-forward (regexp &optional backward)
8552   "Search for an article containing REGEXP forward.
8553 If BACKWARD, search backward instead."
8554   (interactive
8555    (list (read-string
8556           (format "Search article %s (regexp%s): "
8557                   (if current-prefix-arg "backward" "forward")
8558                   (if gnus-last-search-regexp
8559                       (concat ", default " gnus-last-search-regexp)
8560                     "")))
8561          current-prefix-arg))
8562   (gnus-set-global-variables)
8563   (if (string-equal regexp "")
8564       (setq regexp (or gnus-last-search-regexp ""))
8565     (setq gnus-last-search-regexp regexp))
8566   (if (gnus-summary-search-article regexp backward)
8567       (gnus-article-set-window-start 
8568        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8569     (error "Search failed: \"%s\"" regexp)))
8570
8571 (defun gnus-summary-search-article-backward (regexp)
8572   "Search for an article containing REGEXP backward."
8573   (interactive
8574    (list (read-string
8575           (format "Search article backward (regexp%s): "
8576                   (if gnus-last-search-regexp
8577                       (concat ", default " gnus-last-search-regexp)
8578                     "")))))
8579   (gnus-summary-search-article-forward regexp 'backward))
8580
8581 (defun gnus-summary-search-article (regexp &optional backward)
8582   "Search for an article containing REGEXP.
8583 Optional argument BACKWARD means do search for backward.
8584 gnus-select-article-hook is not called during the search."
8585   (let ((gnus-select-article-hook nil)  ;Disable hook.
8586         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8587         (re-search
8588          (if backward
8589              (function re-search-backward) (function re-search-forward)))
8590         (found nil)
8591         (last nil))
8592     ;; Hidden thread subtrees must be searched for ,too.
8593     (gnus-summary-show-all-threads)
8594     (if (eobp) (forward-line -1))
8595     ;; First of all, search current article.
8596     ;; We don't want to read article again from NNTP server nor reset
8597     ;; current point.
8598     (gnus-summary-select-article)
8599     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8600     (setq last gnus-current-article)
8601     (gnus-eval-in-buffer-window
8602      gnus-article-buffer
8603      (save-restriction
8604        (widen)
8605        ;; Begin search from current point.
8606        (setq found (funcall re-search regexp nil t))))
8607     ;; Then search next articles.
8608     (while (and (not found)
8609                 (gnus-summary-display-article 
8610                  (gnus-summary-search-subject backward nil nil)))
8611       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8612       (gnus-eval-in-buffer-window
8613        gnus-article-buffer
8614        (save-restriction
8615          (widen)
8616          (goto-char (if backward (point-max) (point-min)))
8617          (setq found (funcall re-search regexp nil t)))))
8618     (message "")
8619     ;; Adjust article pointer.
8620     (or (eq last gnus-current-article)
8621         (setq gnus-last-article last))
8622     ;; Return T if found such article.
8623     found))
8624
8625 (defun gnus-summary-execute-command (header regexp command &optional backward)
8626   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8627 If HEADER is an empty string (or nil), the match is done on the entire
8628 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8629   (interactive
8630    (list (let ((completion-ignore-case t))
8631            (completing-read 
8632             "Header name: "
8633             (mapcar (lambda (string) (list string))
8634                     '("Number" "Subject" "From" "Lines" "Date"
8635                       "Message-ID" "Xref" "References"))
8636             nil 'require-match))
8637          (read-string "Regexp: ")
8638          (read-key-sequence "Command: ")
8639          current-prefix-arg))
8640   (gnus-set-global-variables)
8641   ;; Hidden thread subtrees must be searched as well.
8642   (gnus-summary-show-all-threads)
8643   ;; We don't want to change current point nor window configuration.
8644   (save-excursion
8645     (save-window-excursion
8646       (gnus-message 6 "Executing %s..." (key-description command))
8647       ;; We'd like to execute COMMAND interactively so as to give arguments.
8648       (gnus-execute header regexp
8649                     (` (lambda ()
8650                          (call-interactively '(, (key-binding command)))))
8651                     backward)
8652       (gnus-message 6 "Executing %s...done" (key-description command)))))
8653
8654 (defun gnus-summary-beginning-of-article ()
8655   "Scroll the article back to the beginning."
8656   (interactive)
8657   (gnus-set-global-variables)
8658   (gnus-summary-select-article)
8659   (gnus-configure-windows 'article)
8660   (gnus-eval-in-buffer-window
8661    gnus-article-buffer
8662    (widen)
8663    (goto-char (point-min))
8664    (and gnus-break-pages (gnus-narrow-to-page))))
8665
8666 (defun gnus-summary-end-of-article ()
8667   "Scroll to the end of the article."
8668   (interactive)
8669   (gnus-set-global-variables)
8670   (gnus-summary-select-article)
8671   (gnus-configure-windows 'article)
8672   (gnus-eval-in-buffer-window 
8673    gnus-article-buffer
8674    (widen)
8675    (goto-char (point-max))
8676    (recenter -3)
8677    (and gnus-break-pages (gnus-narrow-to-page))))
8678
8679 (defun gnus-summary-show-article ()
8680   "Force re-fetching of the current article."
8681   (interactive)
8682   (gnus-set-global-variables)
8683   (gnus-summary-select-article nil 'force)
8684   (gnus-configure-windows 'article)
8685   (gnus-summary-position-cursor))
8686
8687 (defun gnus-summary-verbose-headers (&optional arg)
8688   "Toggle permanent full header display.
8689 If ARG is a positive number, turn header display on.
8690 If ARG is a negative number, turn header display off."
8691   (interactive "P")
8692   (gnus-set-global-variables)
8693   (gnus-summary-toggle-header arg)
8694   (setq gnus-show-all-headers
8695         (cond ((or (not (numberp arg))
8696                    (zerop arg))
8697                (not gnus-show-all-headers))
8698               ((natnump arg)
8699                t))))
8700
8701 (defun gnus-summary-toggle-header (&optional arg)
8702   "Show the headers if they are hidden, or hide them if they are shown.
8703 If ARG is a positive number, show the entire header.
8704 If ARG is a negative number, hide the unwanted header lines."
8705   (interactive "P")
8706   (gnus-set-global-variables)
8707   (save-excursion
8708     (set-buffer gnus-article-buffer)
8709     (let ((buffer-read-only nil))
8710       (if (numberp arg) 
8711           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8712                                                 gnus-hidden-properties)
8713             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8714         (if (text-property-any (point-min) (point-max) 'invisible t)
8715             (remove-text-properties 
8716              (point-min) (point-max) gnus-hidden-properties)
8717           ;; We hide the headers. This song and dance act below is
8718           ;; done because `gnus-have-all-headers' is buffer-local to
8719           ;; the summary buffer, and we only want to temporarily
8720           ;; change it in that buffer. Ugh.
8721           (let ((have gnus-have-all-headers))
8722             (save-excursion
8723               (set-buffer gnus-summary-buffer)
8724               (setq gnus-have-all-headers nil)
8725               (save-excursion
8726                 (set-buffer gnus-article-buffer)
8727                 (run-hooks 'gnus-article-display-hook))
8728               (setq gnus-have-all-headers have)))))
8729       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8730
8731 (defun gnus-summary-show-all-headers ()
8732   "Make all header lines visible."
8733   (interactive)
8734   (gnus-set-global-variables)
8735   (gnus-article-show-all-headers))
8736
8737 (defun gnus-summary-toggle-mime (&optional arg)
8738   "Toggle MIME processing.
8739 If ARG is a positive number, turn MIME processing on."
8740   (interactive "P")
8741   (gnus-set-global-variables)
8742   (setq gnus-show-mime
8743         (if (null arg) (not gnus-show-mime)
8744           (> (prefix-numeric-value arg) 0)))
8745   (gnus-summary-select-article t 'force))
8746
8747 (defun gnus-summary-caesar-message (&optional arg)
8748   "Caesar rotate the current article by 13.
8749 The numerical prefix specifies how manu places to rotate each letter
8750 forward."
8751   (interactive "P")
8752   (gnus-set-global-variables)
8753   (gnus-summary-select-article)
8754   (let ((mail-header-separator ""))
8755     (gnus-eval-in-buffer-window 
8756      gnus-article-buffer
8757      (save-restriction
8758        (widen)
8759        (let ((start (window-start)))
8760          (news-caesar-buffer-body arg)
8761          (set-window-start (get-buffer-window (current-buffer)) start))))))
8762
8763 (defun gnus-summary-stop-page-breaking ()
8764   "Stop page breaking in the current article."
8765   (interactive)
8766   (gnus-set-global-variables)
8767   (gnus-summary-select-article)
8768   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8769
8770 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8771
8772 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8773   "Move the current article to a different newsgroup.
8774 If N is a positive number, move the N next articles.
8775 If N is a negative number, move the N previous articles.
8776 If N is nil and any articles have been marked with the process mark,
8777 move those articles instead.
8778 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8779 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8780 re-spool using this method.
8781 For this function to work, both the current newsgroup and the
8782 newsgroup that you want to move to have to support the `request-move'
8783 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8784   (interactive "P")
8785   (gnus-set-global-variables)
8786   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8787       (error "The current newsgroup does not support article moving"))
8788   (let ((articles (gnus-summary-work-articles n))
8789         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8790         art-group to-method sel-met)
8791     (if (and (not to-newsgroup) (not select-method))
8792         (setq to-newsgroup
8793               (completing-read 
8794                (format "Where do you want to move %s? %s"
8795                        (if (> (length articles) 1)
8796                            (format "these %d articles" (length articles))
8797                          "this article")
8798                        (if gnus-current-move-group
8799                            (format "(%s default) " gnus-current-move-group)
8800                          ""))
8801                gnus-active-hashtb nil nil prefix)))
8802     (if to-newsgroup
8803         (progn
8804           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8805               (setq to-newsgroup (or gnus-current-move-group "")))
8806           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8807               (gnus-activate-group to-newsgroup)
8808               (error "No such group: %s" to-newsgroup))
8809           (setq gnus-current-move-group to-newsgroup)))
8810     (setq to-method (if select-method (list select-method "")
8811                       (gnus-find-method-for-group to-newsgroup)))
8812     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8813         (error "%s does not support article copying" (car to-method)))
8814     (or (gnus-check-server to-method)
8815         (error "Can't open server %s" (car to-method)))
8816     (gnus-message 6 "Moving to %s: %s..." 
8817                   (or select-method to-newsgroup) articles)
8818     (while articles
8819       (if (setq art-group
8820                 (gnus-request-move-article 
8821                  (car articles)         ; Article to move
8822                  gnus-newsgroup-name    ; From newsgrouo
8823                  (nth 1 (gnus-find-method-for-group 
8824                          gnus-newsgroup-name)) ; Server
8825                  (list 'gnus-request-accept-article 
8826                        (if select-method
8827                            (list 'quote select-method)
8828                          to-newsgroup)
8829                        (not (cdr articles))) ; Accept form
8830                  (not (cdr articles)))) ; Only save nov last time
8831           (let* ((buffer-read-only nil)
8832                  (entry 
8833                   (or
8834                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8835                    (gnus-gethash 
8836                     (gnus-group-prefixed-name 
8837                      (car art-group) 
8838                      (if select-method (list select-method "")
8839                        (gnus-find-method-for-group to-newsgroup)))
8840                     gnus-newsrc-hashtb)))
8841                  (info (nth 2 entry))
8842                  (article (car articles)))
8843             (gnus-summary-goto-subject article)
8844             (beginning-of-line)
8845             (delete-region (point) (progn (forward-line 1) (point)))
8846             ;; Update the group that has been moved to.
8847             (if (not info)
8848                 ()                      ; This group does not exist yet.
8849               (if (not (memq article gnus-newsgroup-unreads))
8850                   (setcar (cdr (cdr info))
8851                           (gnus-add-to-range (nth 2 info) 
8852                                              (list (cdr art-group)))))
8853               ;; Copy any marks over to the new group.
8854               (let ((marks '((tick . gnus-newsgroup-marked)
8855                              (dormant . gnus-newsgroup-dormant)
8856                              (expire . gnus-newsgroup-expirable)
8857                              (bookmark . gnus-newsgroup-bookmarks)
8858                              (reply . gnus-newsgroup-replied)))
8859                     (to-article (cdr art-group)))
8860                 (while marks
8861                   (if (memq article (symbol-value (cdr (car marks))))
8862                       (gnus-add-marked-articles 
8863                        (car info) (car (car marks)) (list to-article) info))
8864                   (setq marks (cdr marks)))))
8865             ;; Update marks.
8866             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8867             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8868             (setq gnus-newsgroup-dormant
8869                   (delq article gnus-newsgroup-dormant))
8870             (setq gnus-newsgroup-reads
8871                   (cons (cons article gnus-canceled-mark)
8872                         gnus-newsgroup-reads)))
8873         (gnus-message 1 "Couldn't move article %s" (car articles)))
8874       (gnus-summary-remove-process-mark (car articles))
8875       (setq articles (cdr articles)))))
8876
8877 (defun gnus-summary-respool-article (&optional n respool-method)
8878   "Respool the current article.
8879 The article will be squeezed through the mail spooling process again,
8880 which means that it will be put in some mail newsgroup or other
8881 depending on `nnmail-split-methods'.
8882 If N is a positive number, respool the N next articles.
8883 If N is a negative number, respool the N previous articles.
8884 If N is nil and any articles have been marked with the process mark,
8885 respool those articles instead.
8886
8887 Respooling can be done both from mail groups and \"real\" newsgroups.
8888 In the former case, the articles in question will be moved from the
8889 current group into whatever groups they are destined to.  In the
8890 latter case, they will be copied into the relevant groups."
8891   (interactive "P")
8892   (gnus-set-global-variables)
8893   (let ((respool-methods (gnus-methods-using 'respool))
8894         (methname 
8895          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8896     (or respool-method
8897         (setq respool-method
8898               (completing-read
8899                "What method do you want to use when respooling? "
8900                respool-methods nil t methname)))
8901     (or (string= respool-method "")
8902         (if (assoc (symbol-name
8903                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8904                    respool-methods)
8905             (gnus-summary-move-article n nil (intern respool-method))
8906           (gnus-summary-copy-article n nil (intern respool-method))))))
8907
8908 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8909 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8910   "Move the current article to a different newsgroup.
8911 If N is a positive number, move the N next articles.
8912 If N is a negative number, move the N previous articles.
8913 If N is nil and any articles have been marked with the process mark,
8914 move those articles instead.
8915 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8916 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8917 re-spool using this method.
8918 For this function to work, the newsgroup that you want to move to have
8919 to support the `request-move' and `request-accept'
8920 functions. (Ie. mail newsgroups at present.)"
8921   (interactive "P")
8922   (gnus-set-global-variables)
8923   (let ((articles (gnus-summary-work-articles n))
8924         (copy-buf (get-buffer-create "*copy work*"))
8925         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8926         art-group to-method)
8927     (buffer-disable-undo copy-buf)
8928     (if (and (not to-newsgroup) (not select-method))
8929         (setq to-newsgroup
8930               (completing-read 
8931                (format "Where do you want to copy %s? %s"
8932                        (if (> (length articles) 1)
8933                            (format "these %d articles" (length articles))
8934                          "this article")
8935                        (if gnus-current-move-group
8936                            (format "(%s default) " gnus-current-move-group)
8937                          ""))
8938                gnus-active-hashtb nil nil prefix)))
8939     (if to-newsgroup
8940         (progn
8941           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8942               (setq to-newsgroup (or gnus-current-move-group "")))
8943           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8944               (gnus-activate-group to-newsgroup)
8945               (error "No such group: %s" to-newsgroup))
8946           (setq gnus-current-move-group to-newsgroup)))
8947     (setq to-method (if select-method (list select-method "")
8948                       (gnus-find-method-for-group to-newsgroup)))
8949     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8950         (error "%s does not support article copying" (car to-method)))
8951     (or (gnus-check-server to-method)
8952         (error "Can't open server %s" (car to-method)))
8953     (while articles
8954       (gnus-message 6 "Copying to %s: %s..." 
8955                     (or select-method to-newsgroup) articles)
8956       (if (setq art-group
8957                 (save-excursion
8958                   (set-buffer copy-buf)
8959                   (gnus-request-article-this-buffer
8960                    (car articles) gnus-newsgroup-name)
8961                   (gnus-request-accept-article
8962                    (if select-method (quote select-method) to-newsgroup)
8963                    (not (cdr articles)))))
8964           (let* ((entry 
8965                   (or
8966                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8967                    (gnus-gethash 
8968                     (gnus-group-prefixed-name 
8969                      (car art-group) 
8970                      (if select-method (list select-method "")
8971                        (gnus-find-method-for-group to-newsgroup)))
8972                     gnus-newsrc-hashtb)))
8973                  (info (nth 2 entry))
8974                  (article (car articles)))
8975             ;; We copy the info over to the new group.
8976             (if (not info)
8977                 ()                      ; This group does not exist (yet).
8978               (if (not (memq article gnus-newsgroup-unreads))
8979                   (setcar (cdr (cdr info))
8980                           (gnus-add-to-range (nth 2 info) 
8981                                              (list (cdr art-group)))))
8982               ;; Copy any marks over to the new group.
8983               (let ((marks '((tick . gnus-newsgroup-marked)
8984                              (dormant . gnus-newsgroup-dormant)
8985                              (expire . gnus-newsgroup-expirable)
8986                              (bookmark . gnus-newsgroup-bookmarks)
8987                              (reply . gnus-newsgroup-replied)))
8988                     (to-article (cdr art-group)))
8989                 (while marks
8990                   (if (memq article (symbol-value (cdr (car marks))))
8991                       (gnus-add-marked-articles 
8992                        (car info) (car (car marks)) (list to-article) info))
8993                   (setq marks (cdr marks))))))
8994         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8995       (gnus-summary-remove-process-mark (car articles))
8996       (setq articles (cdr articles)))
8997     (kill-buffer copy-buf)))
8998
8999 (defun gnus-summary-import-article (file)
9000   "Import a random file into a mail newsgroup."
9001   (interactive "fImport file: ")
9002   (let ((group gnus-newsgroup-name)
9003         atts)
9004     (or (gnus-check-backend-function 'request-accept-article group)
9005         (error "%s does not support article importing" group))
9006     (or (file-readable-p file)
9007         (not (file-regular-p file))
9008         (error "Can't read %s" file))
9009     (save-excursion
9010       (set-buffer (get-buffer-create " *import file*"))
9011       (buffer-disable-undo (current-buffer))
9012       (erase-buffer)
9013       (insert-file-contents file)
9014       (goto-char (point-min))
9015       (if (nnheader-article-p)
9016           ()
9017         (setq atts (file-attributes file))
9018         (insert "From: " (read-string "From: ") "\n"
9019                 "Subject: " (read-string "Subject: ") "\n"
9020                 "Date: " (current-time-string (nth 5 atts)) "\n"
9021                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9022       (gnus-request-accept-article group t)
9023       (kill-buffer (current-buffer)))))
9024
9025 (defun gnus-summary-expire-articles ()
9026   "Expire all articles that are marked as expirable in the current group."
9027   (interactive)
9028   (if (not (gnus-check-backend-function 
9029             'request-expire-articles gnus-newsgroup-name))
9030       ()
9031     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
9032                                       gnus-newsrc-hashtb)))
9033            (total (memq 'total-expire (nth 5 info)))
9034            (expirable (if total
9035                           (gnus-list-of-read-articles gnus-newsgroup-name)
9036                         (setq gnus-newsgroup-expirable
9037                               (sort gnus-newsgroup-expirable '<))))
9038            es)
9039       (if (not expirable)
9040           ()
9041         (gnus-message 6 "Expiring articles...")
9042         ;; The list of articles that weren't expired is returned.
9043         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
9044         (or total (setq gnus-newsgroup-expirable es))
9045         ;; We go through the old list of expirable, and mark all
9046         ;; really expired articles as non-existant.
9047         (or (eq es expirable)           ;If nothing was expired, we don't mark.
9048             (let ((gnus-use-cache nil))
9049               (while expirable
9050                 (or (memq (car expirable) es)
9051                     (gnus-summary-mark-article
9052                      (car expirable) gnus-canceled-mark))
9053                 (setq expirable (cdr expirable)))))
9054         (gnus-message 6 "Expiring articles...done")))))
9055
9056 (defun gnus-summary-expire-articles-now ()
9057   "Expunge all expirable articles in the current group.
9058 This means that *all* articles that are marked as expirable will be
9059 deleted forever, right now."
9060   (interactive)
9061   (or gnus-expert-user
9062       (gnus-y-or-n-p
9063        "Are you really, really, really sure you want to expunge? ")
9064       (error "Phew!"))
9065   (let ((nnmail-expiry-wait -1)
9066         (nnmail-expiry-wait-function nil))
9067     (gnus-summary-expire-articles)))
9068
9069 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9070 (defun gnus-summary-delete-article (&optional n)
9071   "Delete the N next (mail) articles.
9072 This command actually deletes articles. This is not a marking
9073 command. The article will disappear forever from you life, never to
9074 return. 
9075 If N is negative, delete backwards.
9076 If N is nil and articles have been marked with the process mark,
9077 delete these instead."
9078   (interactive "P")
9079   (or (gnus-check-backend-function 'request-expire-articles 
9080                                    gnus-newsgroup-name)
9081       (error "The current newsgroup does not support article deletion."))
9082   ;; Compute the list of articles to delete.
9083   (let ((articles (gnus-summary-work-articles n))
9084         not-deleted)
9085     (if (and gnus-novice-user
9086              (not (gnus-y-or-n-p 
9087                    (format "Do you really want to delete %s forever? "
9088                            (if (> (length articles) 1) "these articles"
9089                              "this article")))))
9090         ()
9091       ;; Delete the articles.
9092       (setq not-deleted (gnus-request-expire-articles 
9093                          articles gnus-newsgroup-name 'force))
9094       (while articles
9095         (gnus-summary-remove-process-mark (car articles))       
9096         ;; The backend might not have been able to delete the article
9097         ;; after all.  
9098         (or (memq (car articles) not-deleted)
9099             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9100         (setq articles (cdr articles))))
9101     (gnus-summary-position-cursor)
9102     not-deleted))
9103
9104 (defun gnus-summary-edit-article (&optional force)
9105   "Enter into a buffer and edit the current article.
9106 This will have permanent effect only in mail groups.
9107 If FORCE is non-nil, allow editing of articles even in read-only
9108 groups."
9109   (interactive "P")
9110   (or force
9111       (not (gnus-group-read-only-p))
9112       (error "The current newsgroup does not support article editing."))
9113   (gnus-summary-select-article t)
9114   (gnus-configure-windows 'article)
9115   (select-window (get-buffer-window gnus-article-buffer))
9116   (gnus-message 6 "C-c C-c to end edits")
9117   (setq buffer-read-only nil)
9118   (text-mode)
9119   (use-local-map (copy-keymap (current-local-map)))
9120   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
9121   (buffer-enable-undo)
9122   (widen)
9123   (goto-char (point-min))
9124   (search-forward "\n\n" nil t))
9125
9126 (defun gnus-summary-edit-article-done ()
9127   "Make edits to the current article permanent."
9128   (interactive)
9129   (if (gnus-group-read-only-p)
9130       (progn
9131         (gnus-summary-edit-article-postpone)
9132         (message "The current newsgroup does not support article editing.")
9133         (ding))
9134     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
9135       (erase-buffer)
9136       (insert buf)
9137       (if (not (gnus-request-replace-article 
9138                 (cdr gnus-article-current) (car gnus-article-current) 
9139                 (current-buffer)))
9140           (error "Couldn't replace article.")
9141         (gnus-article-mode)
9142         (use-local-map gnus-article-mode-map)
9143         (setq buffer-read-only t)
9144         (buffer-disable-undo (current-buffer))
9145         (gnus-configure-windows 'summary))
9146       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))))
9147
9148 (defun gnus-summary-edit-article-postpone ()
9149   "Postpone changes to the current article."
9150   (interactive)
9151   (gnus-article-mode)
9152   (use-local-map gnus-article-mode-map)
9153   (setq buffer-read-only t)
9154   (buffer-disable-undo (current-buffer))
9155   (gnus-configure-windows 'summary)
9156   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9157
9158 (defun gnus-summary-fancy-query ()
9159   "Query where the fancy respool algorithm would put this article."
9160   (interactive)
9161   (gnus-summary-select-article)
9162   (save-excursion
9163     (set-buffer gnus-article-buffer)
9164     (save-restriction
9165       (goto-char (point-min))
9166       (search-forward "\n\n")
9167       (narrow-to-region (point-min) (point))
9168       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9169
9170 ;; Summary score commands.
9171
9172 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9173
9174 (defun gnus-summary-raise-score (n)
9175   "Raise the score of the current article by N."
9176   (interactive "p")
9177   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9178
9179 (defun gnus-summary-set-score (n)
9180   "Set the score of the current article to N."
9181   (interactive "p")
9182   ;; Skip dummy header line.
9183   (save-excursion
9184     (gnus-summary-show-thread)
9185     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9186         (forward-line 1))
9187     (let ((buffer-read-only nil))
9188       ;; Set score.
9189       (gnus-summary-update-mark
9190        (if (= n (or gnus-summary-default-score 0)) ? 
9191          (if (< n (or gnus-summary-default-score 0)) 
9192              gnus-score-below-mark gnus-score-over-mark)) 'score))
9193     (let* ((article (gnus-summary-article-number))
9194            (score (assq article gnus-newsgroup-scored)))
9195       (if score (setcdr score n)
9196         (setq gnus-newsgroup-scored 
9197               (cons (cons article n) gnus-newsgroup-scored))))
9198     (gnus-summary-update-line)))
9199
9200 (defun gnus-summary-current-score ()
9201   "Return the score of the current article."
9202   (interactive)
9203   (message "%s" (gnus-summary-article-score)))
9204
9205 ;; Summary marking commands.
9206
9207 (defun gnus-summary-raise-same-subject-and-select (score)
9208   "Raise articles which has the same subject with SCORE and select the next."
9209   (interactive "p")
9210   (let ((subject (gnus-summary-subject-string)))
9211     (gnus-summary-raise-score score)
9212     (while (gnus-summary-search-subject nil nil subject)
9213       (gnus-summary-raise-score score))
9214     (gnus-summary-next-article t)))
9215
9216 (defun gnus-summary-raise-same-subject (score)
9217   "Raise articles which has the same subject with SCORE."
9218   (interactive "p")
9219   (let ((subject (gnus-summary-subject-string)))
9220     (gnus-summary-raise-score score)
9221     (while (gnus-summary-search-subject nil nil subject)
9222       (gnus-summary-raise-score score))
9223     (gnus-summary-next-subject 1 t)))
9224
9225 (defun gnus-score-default (level)
9226   (if level (prefix-numeric-value level) 
9227     gnus-score-interactive-default-score))
9228
9229 (defun gnus-summary-raise-thread (&optional score)
9230   "Raise the score of the articles in the current thread with SCORE."
9231   (interactive "P")
9232   (setq score (gnus-score-default score))
9233   (let (e)
9234     (save-excursion
9235       (let ((level (gnus-summary-thread-level)))
9236         (gnus-summary-raise-score score)
9237         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9238                     (> (gnus-summary-thread-level) level))
9239           (gnus-summary-raise-score score))
9240         (setq e (point))))
9241     (let ((gnus-summary-check-current t))
9242       (or (zerop (gnus-summary-next-subject 1 t))
9243           (goto-char e))))
9244   (gnus-summary-recenter)
9245   (gnus-summary-position-cursor)
9246   (gnus-set-mode-line 'summary))
9247
9248 (defun gnus-summary-lower-same-subject-and-select (score)
9249   "Raise articles which has the same subject with SCORE and select the next."
9250   (interactive "p")
9251   (gnus-summary-raise-same-subject-and-select (- score)))
9252
9253 (defun gnus-summary-lower-same-subject (score)
9254   "Raise articles which has the same subject with SCORE."
9255   (interactive "p")
9256   (gnus-summary-raise-same-subject (- score)))
9257
9258 (defun gnus-summary-lower-thread (&optional score)
9259   "Lower score of articles in the current thread with SCORE."
9260   (interactive "P")
9261   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9262
9263 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9264   "Mark articles which has the same subject as read, and then select the next.
9265 If UNMARK is positive, remove any kind of mark.
9266 If UNMARK is negative, tick articles."
9267   (interactive "P")
9268   (if unmark
9269       (setq unmark (prefix-numeric-value unmark)))
9270   (let ((count
9271          (gnus-summary-mark-same-subject
9272           (gnus-summary-subject-string) unmark)))
9273     ;; Select next unread article. If auto-select-same mode, should
9274     ;; select the first unread article.
9275     (gnus-summary-next-article t (and gnus-auto-select-same
9276                                       (gnus-summary-subject-string)))
9277     (gnus-message 7 "%d article%s marked as %s"
9278                   count (if (= count 1) " is" "s are")
9279                   (if unmark "unread" "read"))))
9280
9281 (defun gnus-summary-kill-same-subject (&optional unmark)
9282   "Mark articles which has the same subject as read. 
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     ;; If marked as read, go to next unread subject.
9292     (if (null unmark)
9293         ;; Go to next unread subject.
9294         (gnus-summary-next-subject 1 t))
9295     (gnus-message 7 "%d articles are marked as %s"
9296                   count (if unmark "unread" "read"))))
9297
9298 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9299   "Mark articles with same SUBJECT as read, and return marked number.
9300 If optional argument UNMARK is positive, remove any kinds of marks.
9301 If optional argument UNMARK is negative, mark articles as unread instead."
9302   (let ((count 1))
9303     (save-excursion
9304       (cond 
9305        ((null unmark)                   ; Mark as read.
9306         (while (and 
9307                 (progn
9308                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9309                   (gnus-summary-show-thread) t)
9310                 (gnus-summary-search-forward nil subject))
9311           (setq count (1+ count))))
9312        ((> unmark 0)                    ; Tick.
9313         (while (and
9314                 (progn
9315                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9316                   (gnus-summary-show-thread) t)
9317                 (gnus-summary-search-forward nil subject))
9318           (setq count (1+ count))))
9319        (t                               ; Mark as unread.
9320         (while (and
9321                 (progn
9322                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9323                   (gnus-summary-show-thread) t)
9324                 (gnus-summary-search-forward nil subject))
9325           (setq count (1+ count)))))
9326       (gnus-set-mode-line 'summary)
9327       ;; Return the number of marked articles.
9328       count)))
9329
9330 (defun gnus-summary-mark-as-processable (n &optional unmark)
9331   "Set the process mark on the next N articles.
9332 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9333 the process mark instead.  The difference between N and the actual
9334 number of articles marked is returned."
9335   (interactive "p")
9336   (let ((backward (< n 0))
9337         (n (abs n)))
9338     (while (and 
9339             (> n 0)
9340             (if unmark
9341                 (gnus-summary-remove-process-mark
9342                  (gnus-summary-article-number))
9343               (gnus-summary-set-process-mark (gnus-summary-article-number)))
9344             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9345       (setq n (1- n)))
9346     (if (/= 0 n) (gnus-message 7 "No more articles"))
9347     (gnus-summary-recenter)
9348     (gnus-summary-position-cursor)
9349     n))
9350
9351 (defun gnus-summary-unmark-as-processable (n)
9352   "Remove the process mark from the next N articles.
9353 If N is negative, mark backward instead.  The difference between N and
9354 the actual number of articles marked is returned."
9355   (interactive "p")
9356   (gnus-summary-mark-as-processable n t))
9357
9358 (defun gnus-summary-unmark-all-processable ()
9359   "Remove the process mark from all articles."
9360   (interactive)
9361   (save-excursion
9362     (while gnus-newsgroup-processable
9363       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9364   (gnus-summary-position-cursor))
9365
9366 (defun gnus-summary-mark-as-expirable (n)
9367   "Mark N articles forward as expirable.
9368 If N is negative, mark backward instead. The difference between N and
9369 the actual number of articles marked is returned."
9370   (interactive "p")
9371   (gnus-summary-mark-forward n gnus-expirable-mark))
9372
9373 (defun gnus-summary-mark-article-as-replied (article)
9374   "Mark ARTICLE replied and update the summary line."
9375   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9376   (let ((buffer-read-only nil))
9377     (if (gnus-summary-goto-subject article)
9378         (progn
9379           (gnus-summary-update-mark gnus-replied-mark 'replied)
9380           t))))
9381
9382 (defun gnus-summary-set-bookmark (article)
9383   "Set a bookmark in current article."
9384   (interactive (list (gnus-summary-article-number)))
9385   (if (or (not (get-buffer gnus-article-buffer))
9386           (not gnus-current-article)
9387           (not gnus-article-current)
9388           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9389       (error "No current article selected"))
9390   ;; Remove old bookmark, if one exists.
9391   (let ((old (assq article gnus-newsgroup-bookmarks)))
9392     (if old (setq gnus-newsgroup-bookmarks 
9393                   (delq old gnus-newsgroup-bookmarks))))
9394   ;; Set the new bookmark, which is on the form 
9395   ;; (article-number . line-number-in-body).
9396   (setq gnus-newsgroup-bookmarks 
9397         (cons 
9398          (cons article 
9399                (save-excursion
9400                  (set-buffer gnus-article-buffer)
9401                  (count-lines
9402                   (min (point)
9403                        (save-excursion
9404                          (goto-char (point-min))
9405                          (search-forward "\n\n" nil t)
9406                          (point)))
9407                   (point))))
9408          gnus-newsgroup-bookmarks))
9409   (gnus-message 6 "A bookmark has been added to the current article."))
9410
9411 (defun gnus-summary-remove-bookmark (article)
9412   "Remove the bookmark from the current article."
9413   (interactive (list (gnus-summary-article-number)))
9414   ;; Remove old bookmark, if one exists.
9415   (let ((old (assq article gnus-newsgroup-bookmarks)))
9416     (if old 
9417         (progn
9418           (setq gnus-newsgroup-bookmarks 
9419                 (delq old gnus-newsgroup-bookmarks))
9420           (gnus-message 6 "Removed bookmark."))
9421       (gnus-message 6 "No bookmark in current article."))))
9422
9423 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9424 (defun gnus-summary-mark-as-dormant (n)
9425   "Mark N articles forward as dormant.
9426 If N is negative, mark backward instead.  The difference between N and
9427 the actual number of articles marked is returned."
9428   (interactive "p")
9429   (gnus-summary-mark-forward n gnus-dormant-mark))
9430
9431 (defun gnus-summary-set-process-mark (article)
9432   "Set the process mark on ARTICLE and update the summary line."
9433   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9434   (let ((buffer-read-only nil))
9435     (if (gnus-summary-goto-subject article)
9436         (progn
9437           (gnus-summary-show-thread)
9438           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9439                (forward-line 1))
9440           (gnus-summary-update-mark gnus-process-mark 'replied)
9441           t))))
9442
9443 (defun gnus-summary-remove-process-mark (article)
9444   "Remove the process mark from ARTICLE and update the summary line."
9445   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9446   (let ((buffer-read-only nil))
9447     (if (gnus-summary-goto-subject article)
9448         (progn
9449           (gnus-summary-show-thread)
9450           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9451                (forward-line 1))
9452           (gnus-summary-update-mark ?  'replied)
9453           (if (memq article gnus-newsgroup-replied) 
9454               (gnus-summary-update-mark gnus-replied-mark 'replied))
9455           t))))
9456
9457 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9458   "Mark N articles as read forwards.
9459 If N is negative, mark backwards instead.
9460 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9461 marked as unread. 
9462 The difference between N and the actual number of articles marked is
9463 returned."
9464   (interactive "p")
9465   (gnus-set-global-variables)
9466   (let ((backward (< n 0))
9467         (gnus-summary-goto-unread
9468          (and gnus-summary-goto-unread
9469               (not (memq mark (list gnus-unread-mark
9470                                     gnus-ticked-mark gnus-dormant-mark)))))
9471         (n (abs n))
9472         (mark (or mark gnus-del-mark)))
9473     (while (and (> n 0)
9474                 (gnus-summary-mark-article nil mark no-expire)
9475                 (zerop (gnus-summary-next-subject 
9476                         (if backward -1 1) gnus-summary-goto-unread t)))
9477       (setq n (1- n)))
9478     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9479     (gnus-summary-recenter)
9480     (gnus-summary-position-cursor)
9481     (gnus-set-mode-line 'summary)
9482     n))
9483
9484 (defun gnus-summary-mark-article-as-read (mark)
9485   "Mark the current article quickly as read with MARK."
9486   (let ((article (gnus-summary-article-number)))
9487     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9488     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9489     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9490     (setq gnus-newsgroup-reads
9491           (cons (cons article mark) gnus-newsgroup-reads))
9492     ;; Possibly remove from cache, if that is used. 
9493     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9494     (and gnus-newsgroup-auto-expire 
9495          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9496              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9497              (= mark gnus-read-mark))
9498          (progn
9499            (setq mark gnus-expirable-mark)
9500            (setq gnus-newsgroup-expirable 
9501                  (cons article gnus-newsgroup-expirable))))
9502     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9503       (forward-line 1))
9504     ;; Fix the mark.
9505     (gnus-summary-update-mark mark 'unread)
9506     t))
9507
9508 (defun gnus-summary-mark-article-as-unread (mark)
9509   "Mark the current article quickly as unread with MARK."
9510   (let ((article (gnus-summary-article-number)))
9511     (or (memq article gnus-newsgroup-unreads)
9512         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9513     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9514     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9515     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9516     (setq gnus-newsgroup-reads
9517           (delq (assq article gnus-newsgroup-reads)
9518                 gnus-newsgroup-reads))
9519     (if (= mark gnus-ticked-mark)
9520         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9521     (if (= mark gnus-dormant-mark)
9522         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9523
9524     ;; See whether the article is to be put in the cache.
9525     (and gnus-use-cache
9526          (vectorp (gnus-get-header-by-num article))
9527          (save-excursion
9528            (gnus-cache-possibly-enter-article 
9529             gnus-newsgroup-name article 
9530             (gnus-get-header-by-num article)
9531             (= mark gnus-ticked-mark)
9532             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9533
9534     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9535       (forward-line 1))
9536     ;; Fix the mark.
9537     (gnus-summary-update-mark mark 'unread)
9538     t))
9539
9540 (defun gnus-summary-mark-article (&optional article mark no-expire)
9541   "Mark ARTICLE with MARK.  MARK can be any character.
9542 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9543 (dormant) and `?E' (expirable).
9544 If MARK is nil, then the default character `?D' is used.
9545 If ARTICLE is nil, then the article on the current line will be
9546 marked." 
9547   (and (stringp mark)
9548        (setq mark (aref mark 0)))
9549   ;; If no mark is given, then we check auto-expiring.
9550   (and (not no-expire)
9551        gnus-newsgroup-auto-expire 
9552        (or (not mark)
9553            (and (numberp mark) 
9554                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9555                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9556                     (= mark gnus-read-mark))))
9557        (setq mark gnus-expirable-mark))
9558   (let* ((mark (or mark gnus-del-mark))
9559          (article (or article (gnus-summary-article-number))))
9560     (or article (error "No article on current line"))
9561     (if (or (= mark gnus-unread-mark) 
9562             (= mark gnus-ticked-mark) 
9563             (= mark gnus-dormant-mark))
9564         (gnus-mark-article-as-unread article mark)
9565       (gnus-mark-article-as-read article mark))
9566
9567     ;; See whether the article is to be put in the cache.
9568     (and gnus-use-cache
9569          (not (= mark gnus-canceled-mark))
9570          (vectorp (gnus-get-header-by-num article))
9571          (save-excursion
9572            (gnus-cache-possibly-enter-article 
9573             gnus-newsgroup-name article 
9574             (gnus-get-header-by-num article)
9575             (= mark gnus-ticked-mark)
9576             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9577
9578     (if (gnus-summary-goto-subject article)
9579         (let ((buffer-read-only nil))
9580           (gnus-summary-show-thread)
9581           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9582                (forward-line 1))
9583           ;; Fix the mark.
9584           (gnus-summary-update-mark mark 'unread)
9585           t))))
9586
9587 (defun gnus-summary-update-mark (mark type)
9588   (beginning-of-line)
9589   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9590         (buffer-read-only nil)
9591         plist)
9592     (if (not forward)
9593         ()
9594       (forward-char forward)
9595       (setq plist (text-properties-at (point)))
9596       (delete-char 1)
9597       (insert mark)
9598       (and plist (add-text-properties (1- (point)) (point) plist))
9599       (and (eq type 'unread)
9600            (progn
9601              (add-text-properties (1- (point)) (point) (list 'gnus-mark mark))
9602              (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9603   
9604 (defun gnus-mark-article-as-read (article &optional mark)
9605   "Enter ARTICLE in the pertinent lists and remove it from others."
9606   ;; Make the article expirable.
9607   (let ((mark (or mark gnus-del-mark)))
9608     (if (= mark gnus-expirable-mark)
9609         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9610       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9611     ;; Remove from unread and marked lists.
9612     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9613     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9614     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9615     (setq gnus-newsgroup-reads 
9616           (cons (cons article mark) gnus-newsgroup-reads))
9617     ;; Possibly remove from cache, if that is used. 
9618     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9619
9620 (defun gnus-mark-article-as-unread (article &optional mark)
9621   "Enter ARTICLE in the pertinent lists and remove it from others."
9622   (let ((mark (or mark gnus-ticked-mark)))
9623     ;; Add to unread list.
9624     (or (memq article gnus-newsgroup-unreads)
9625         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9626     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9627     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9628     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9629     (setq gnus-newsgroup-reads
9630           (delq (assq article gnus-newsgroup-reads)
9631                 gnus-newsgroup-reads))
9632     (if (= mark gnus-ticked-mark)
9633         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9634     (if (= mark gnus-dormant-mark)
9635         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9636
9637 (defalias 'gnus-summary-mark-as-unread-forward 
9638   'gnus-summary-tick-article-forward)
9639 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9640                'gnus-summary-tick-article-forward)
9641 (defun gnus-summary-tick-article-forward (n)
9642   "Tick N articles forwards.
9643 If N is negative, tick backwards instead.
9644 The difference between N and the number of articles ticked is returned."
9645   (interactive "p")
9646   (gnus-summary-mark-forward n gnus-ticked-mark))
9647
9648 (defalias 'gnus-summary-mark-as-unread-backward 
9649   'gnus-summary-tick-article-backward)
9650 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9651                'gnus-summary-tick-article-backward)
9652 (defun gnus-summary-tick-article-backward (n)
9653   "Tick N articles backwards.
9654 The difference between N and the number of articles ticked is returned."
9655   (interactive "p")
9656   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9657
9658 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9659 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9660 (defun gnus-summary-tick-article (&optional article clear-mark)
9661   "Mark current article as unread.
9662 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9663 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9664   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9665                                        gnus-ticked-mark)))
9666
9667 (defun gnus-summary-mark-as-read-forward (n)
9668   "Mark N articles as read forwards.
9669 If N is negative, mark backwards instead.
9670 The difference between N and the actual number of articles marked is
9671 returned."
9672   (interactive "p")
9673   (gnus-summary-mark-forward n gnus-del-mark t))
9674
9675 (defun gnus-summary-mark-as-read-backward (n)
9676   "Mark the N articles as read backwards.
9677 The difference between N and the actual number of articles marked is
9678 returned."
9679   (interactive "p")
9680   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9681
9682 (defun gnus-summary-mark-as-read (&optional article mark)
9683   "Mark current article as read.
9684 ARTICLE specifies the article to be marked as read.
9685 MARK specifies a string to be inserted at the beginning of the line."
9686   (gnus-summary-mark-article article mark))
9687
9688 (defun gnus-summary-clear-mark-forward (n)
9689   "Clear marks from N articles forward.
9690 If N is negative, clear backward instead.
9691 The difference between N and the number of marks cleared is returned."
9692   (interactive "p")
9693   (gnus-summary-mark-forward n gnus-unread-mark))
9694
9695 (defun gnus-summary-clear-mark-backward (n)
9696   "Clear marks from N articles backward.
9697 The difference between N and the number of marks cleared is returned."
9698   (interactive "p")
9699   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9700
9701 (defun gnus-summary-mark-unread-as-read ()
9702   "Intended to be used by `gnus-summary-mark-article-hook'."
9703   (or (memq gnus-current-article gnus-newsgroup-marked)
9704       (memq gnus-current-article gnus-newsgroup-dormant)
9705       (memq gnus-current-article gnus-newsgroup-expirable)
9706       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9707
9708 (defun gnus-summary-mark-region-as-read (point mark all)
9709   "Mark all unread articles between point and mark as read.
9710 If given a prefix, mark all articles between point and mark as read,
9711 even ticked and dormant ones."
9712   (interactive "r\nP")
9713   (save-excursion
9714     (goto-char point)
9715     (beginning-of-line)
9716     (while (and 
9717             (< (point) mark)
9718             (progn
9719               (and
9720                (or all
9721                    (and
9722                     (not (memq (gnus-summary-article-number)
9723                                gnus-newsgroup-marked))
9724                     (not (memq (gnus-summary-article-number)
9725                                gnus-newsgroup-dormant))))
9726                (gnus-summary-mark-article
9727                 (gnus-summary-article-number) gnus-del-mark))
9728               t)
9729             (zerop (forward-line 1))))))
9730
9731 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9732 (defalias 'gnus-summary-delete-marked-as-read 
9733   'gnus-summary-remove-lines-marked-as-read)
9734 (make-obsolete 'gnus-summary-delete-marked-as-read 
9735                'gnus-summary-remove-lines-marked-as-read)
9736 (defun gnus-summary-remove-lines-marked-as-read ()
9737   "Remove lines that are marked as read."
9738   (interactive)
9739   (gnus-summary-remove-lines-marked-with 
9740    (concat (mapconcat
9741             (lambda (char) (char-to-string (symbol-value char)))
9742             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9743                             gnus-killed-mark gnus-kill-file-mark
9744                             gnus-low-score-mark gnus-expirable-mark
9745                             gnus-canceled-mark gnus-catchup-mark)
9746             ""))))
9747
9748 (defalias 'gnus-summary-delete-marked-with 
9749   'gnus-summary-remove-lines-marked-with)
9750 (make-obsolete 'gnus-summary-delete-marked-with 
9751                'gnus-summary-remove-lines-marked-with)
9752 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9753 (defun gnus-summary-remove-lines-marked-with (marks)
9754   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9755   (interactive "sMarks: ")
9756   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9757   (gnus-set-global-variables)
9758   (let ((buffer-read-only nil)
9759         (orig-article (gnus-summary-article-number))
9760         (marks (concat "^[" marks "]")))
9761     (goto-char (point-min))
9762     (if gnus-newsgroup-adaptive
9763         (gnus-score-remove-lines-adaptive marks)
9764       (while (re-search-forward marks nil t)
9765         (gnus-delete-line)))
9766     ;; If we use dummy roots, we have to do an additional sweep over
9767     ;; the buffer.
9768     (if (not (eq gnus-summary-make-false-root 'dummy))
9769         ()
9770       (goto-char (point-min))
9771       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9772       (while (re-search-forward marks nil t)
9773         (if (gnus-subject-equal
9774              (gnus-summary-subject-string)
9775              (progn
9776                (forward-line 1)
9777                (gnus-summary-subject-string)))
9778             ()
9779           (forward-line -1)
9780           (gnus-delete-line))))
9781     (or (zerop (buffer-size))
9782         (gnus-summary-goto-subject orig-article)
9783         (if (eobp)
9784             (gnus-summary-prev-subject 1)
9785           (gnus-summary-position-cursor)))))
9786
9787 (defun gnus-summary-expunge-below (&optional score)
9788   "Remove articles with score less than SCORE."
9789   (interactive "P")
9790   (gnus-set-global-variables)
9791   (setq score (if score
9792                   (prefix-numeric-value score)
9793                 (or gnus-summary-default-score 0)))
9794   (save-excursion
9795     (set-buffer gnus-summary-buffer)
9796     (goto-char (point-min))
9797     (let ((buffer-read-only nil)
9798           beg)
9799       (while (not (eobp))
9800         (if (< (gnus-summary-article-score) score)
9801             (progn
9802               (setq beg (point))
9803               (forward-line 1)
9804               (delete-region beg (point)))
9805           (forward-line 1)))
9806       ;; Adjust point.
9807       (or (zerop (buffer-size))
9808           (if (eobp)
9809               (gnus-summary-prev-subject 1)
9810             (gnus-summary-position-cursor))))))
9811
9812 (defun gnus-summary-mark-below (score mark)
9813   "Mark articles with score less than SCORE with MARK."
9814   (interactive "P\ncMark: ")
9815   (gnus-set-global-variables)
9816   (setq score (if score
9817                   (prefix-numeric-value score)
9818                 (or gnus-summary-default-score 0)))
9819   (save-excursion
9820     (set-buffer gnus-summary-buffer)
9821     (goto-char (point-min))
9822     (while (not (eobp))
9823       (and (< (gnus-summary-article-score) score)
9824            (gnus-summary-mark-article nil mark))
9825       (forward-line 1))))
9826
9827 (defun gnus-summary-kill-below (&optional score)
9828   "Mark articles with score below SCORE as read."
9829   (interactive "P")
9830   (gnus-set-global-variables)
9831   (gnus-summary-mark-below score gnus-killed-mark))
9832
9833 (defun gnus-summary-clear-above (&optional score)
9834   "Clear all marks from articles with score above SCORE."
9835   (interactive "P")
9836   (gnus-set-global-variables)
9837   (gnus-summary-mark-above score gnus-unread-mark))
9838
9839 (defun gnus-summary-tick-above (&optional score)
9840   "Tick all articles with score above SCORE."
9841   (interactive "P")
9842   (gnus-set-global-variables)
9843   (gnus-summary-mark-above score gnus-ticked-mark))
9844
9845 (defun gnus-summary-mark-above (score mark)
9846   "Mark articles with score over SCORE with MARK."
9847   (interactive "P\ncMark: ")
9848   (gnus-set-global-variables)
9849   (setq score (if score
9850                   (prefix-numeric-value score)
9851                 (or gnus-summary-default-score 0)))
9852   (save-excursion
9853     (set-buffer gnus-summary-buffer)
9854     (goto-char (point-min))
9855     (while (not (eobp))
9856       (if (> (gnus-summary-article-score) score)
9857           (progn
9858             (gnus-summary-mark-article nil mark)
9859             (forward-line 1))
9860         (forward-line 1)))))
9861
9862 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9863 (defun gnus-summary-show-all-expunged ()
9864   "Display all the hidden articles that were expunged for low scores."
9865   (interactive)
9866   (gnus-set-global-variables)
9867   (let ((buffer-read-only nil))
9868     (let ((scored gnus-newsgroup-scored)
9869           headers h)
9870       (while scored
9871         (or (gnus-summary-goto-subject (car (car scored)))
9872             (and (setq h (gnus-get-header-by-num (car (car scored))))
9873                  (< (cdr (car scored)) gnus-summary-expunge-below)
9874                  (setq headers (cons h headers))))
9875         (setq scored (cdr scored)))
9876       (or headers (error "No expunged articles hidden."))
9877       (goto-char (point-min))
9878       (save-excursion 
9879         (gnus-summary-update-lines 
9880          (point)
9881          (progn
9882            (gnus-summary-prepare-unthreaded (nreverse headers))
9883            (point)))))
9884     (goto-char (point-min))
9885     (gnus-summary-position-cursor)))
9886
9887 (defun gnus-summary-show-all-dormant ()
9888   "Display all the hidden articles that are marked as dormant."
9889   (interactive)
9890   (gnus-set-global-variables)
9891   (let ((buffer-read-only nil))
9892     (let ((dormant gnus-newsgroup-dormant)
9893           headers h)
9894       (while dormant
9895         (or (gnus-summary-goto-subject (car dormant))
9896             (and (setq h (gnus-get-header-by-num (car dormant)))
9897                  (setq headers (cons h headers))))
9898         (setq dormant (cdr dormant)))
9899       (or headers (error "No dormant 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-hide-all-dormant ()
9911   "Hide all dormant articles."
9912   (interactive)
9913   (gnus-set-global-variables)
9914   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9915   (gnus-summary-position-cursor))
9916
9917 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9918   "Mark all articles not marked as unread in this newsgroup as read.
9919 If prefix argument ALL is non-nil, all articles are marked as read.
9920 If QUIETLY is non-nil, no questions will be asked.
9921 If TO-HERE is non-nil, it should be a point in the buffer. All
9922 articles before this point will be marked as read.
9923 The number of articles marked as read is returned."
9924   (interactive "P")
9925   (gnus-set-global-variables)
9926   (prog1
9927       (if (or quietly
9928               (not gnus-interactive-catchup) ;Without confirmation?
9929               gnus-expert-user
9930               (gnus-y-or-n-p
9931                (if all
9932                    "Mark absolutely all articles as read? "
9933                  "Mark all unread articles as read? ")))
9934           (if (and not-mark 
9935                    (not gnus-newsgroup-adaptive)
9936                    (not gnus-newsgroup-auto-expire))
9937               (progn
9938                 (and all (setq gnus-newsgroup-marked nil
9939                                gnus-newsgroup-dormant nil))
9940                 (setq gnus-newsgroup-unreads 
9941                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9942             ;; We actually mark all articles as canceled, which we
9943             ;; have to do when using auto-expiry or adaptive scoring. 
9944             (gnus-summary-show-all-threads)
9945             (if (gnus-summary-first-subject (not all))
9946                 (while (and 
9947                         (if to-here (< (point) to-here) t)
9948                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9949                         (gnus-summary-search-subject nil (not all)))))
9950             (or to-here
9951                 (setq gnus-newsgroup-unreads
9952                       (append gnus-newsgroup-marked
9953                               gnus-newsgroup-dormant)))))
9954     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9955       (if (and (not to-here) (eq 'nnvirtual (car method)))
9956           (nnvirtual-catchup-group
9957            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9958     (gnus-summary-position-cursor)))
9959
9960 (defun gnus-summary-catchup-to-here (&optional all)
9961   "Mark all unticked articles before the current one as read.
9962 If ALL is non-nil, also mark ticked and dormant articles as read."
9963   (interactive "P")
9964   (gnus-set-global-variables)
9965   (save-excursion
9966     (and (zerop (forward-line -1))
9967          (progn
9968            (end-of-line)
9969            (gnus-summary-catchup all t (point))
9970            (gnus-set-mode-line 'summary))))
9971   (gnus-summary-position-cursor))
9972
9973 (defun gnus-summary-catchup-all (&optional quietly)
9974   "Mark all articles in this newsgroup as read."
9975   (interactive "P")
9976   (gnus-set-global-variables)
9977   (gnus-summary-catchup t quietly))
9978
9979 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9980   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9981 If prefix argument ALL is non-nil, all articles are marked as read."
9982   (interactive "P")
9983   (gnus-set-global-variables)
9984   (gnus-summary-catchup all quietly nil 'fast)
9985   ;; Select next newsgroup or exit.
9986   (if (and (eq gnus-auto-select-next 'quietly)
9987            (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
9988       (gnus-summary-next-group nil)
9989     (gnus-summary-exit)))
9990
9991 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9992   "Mark all articles in this newsgroup as read, and then exit."
9993   (interactive "P")
9994   (gnus-set-global-variables)
9995   (gnus-summary-catchup-and-exit t quietly))
9996
9997 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9998 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9999   "Mark all articles in this group as read and select the next group.
10000 If given a prefix, mark all articles, unread as well as ticked, as
10001 read." 
10002   (interactive "P")
10003   (gnus-set-global-variables)
10004   (gnus-summary-catchup all)
10005   (gnus-summary-next-group))
10006
10007 ;; Thread-based commands.
10008
10009 (defun gnus-summary-toggle-threads (&optional arg)
10010   "Toggle showing conversation threads.
10011 If ARG is positive number, turn showing conversation threads on."
10012   (interactive "P")
10013   (gnus-set-global-variables)
10014   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10015     (setq gnus-show-threads
10016           (if (null arg) (not gnus-show-threads)
10017             (> (prefix-numeric-value arg) 0)))
10018     (gnus-summary-prepare)
10019     (gnus-summary-goto-subject current)
10020     (gnus-summary-position-cursor)))
10021
10022 (defun gnus-summary-show-all-threads ()
10023   "Show all threads."
10024   (interactive)
10025   (gnus-set-global-variables)
10026   (save-excursion
10027     (let ((buffer-read-only nil))
10028       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10029   (gnus-summary-position-cursor))
10030
10031 (defun gnus-summary-show-thread ()
10032   "Show thread subtrees.
10033 Returns nil if no thread was there to be shown."
10034   (interactive)
10035   (gnus-set-global-variables)
10036   (let ((buffer-read-only nil)
10037         (orig (prog1 (point) (gnus-summary-hide-thread)))
10038         ;; first goto end then to beg, to have point at beg after let
10039         (end (progn (end-of-line) (point)))
10040         (beg (progn (beginning-of-line) (point))))
10041     (prog1
10042         ;; Any hidden lines here?
10043         (search-forward "\r" end t)
10044       (subst-char-in-region beg end ?\^M ?\n t)
10045       (goto-char orig)
10046       (gnus-summary-position-cursor))))
10047
10048 (defun gnus-summary-hide-all-threads ()
10049   "Hide all thread subtrees."
10050   (interactive)
10051   (gnus-set-global-variables)
10052   (save-excursion
10053     (goto-char (point-min))
10054     (gnus-summary-hide-thread)
10055     (while (and (not (eobp)) (zerop (forward-line 1)))
10056       (gnus-summary-hide-thread)))
10057   (gnus-summary-position-cursor))
10058
10059 (defun gnus-summary-hide-thread ()
10060   "Hide thread subtrees.
10061 Returns nil if no threads were there to be hidden."
10062   (interactive)
10063   (gnus-set-global-variables)
10064   (let ((buffer-read-only nil)
10065         (start (point))
10066         (level (gnus-summary-thread-level))
10067         (end (point)))
10068     ;; Go forward until either the buffer ends or the subthread
10069     ;; ends. 
10070     (if (eobp)
10071         ()
10072       (while (and (zerop (forward-line 1))
10073                   (> (gnus-summary-thread-level) level))
10074         (setq end (point)))
10075       (prog1
10076           (save-excursion
10077             (goto-char end)
10078             (search-backward "\n" start t))
10079         (subst-char-in-region start end ?\n ?\^M t)
10080         (forward-line -1)
10081         (gnus-summary-position-cursor)))))
10082
10083 (defun gnus-summary-go-to-next-thread (&optional previous)
10084   "Go to the same level (or less) next thread.
10085 If PREVIOUS is non-nil, go to previous thread instead.
10086 Return the article number moved to, or nil if moving was impossible."
10087   (let ((level (gnus-summary-thread-level))
10088         (article (gnus-summary-article-number)))
10089     (if previous 
10090         (while (and (zerop (forward-line -1))
10091                     (> (gnus-summary-thread-level) level)))
10092       (while (and (save-excursion
10093                     (forward-line 1)
10094                     (not (eobp)))
10095                   (zerop (forward-line 1))
10096                   (> (gnus-summary-thread-level) level))))
10097     (gnus-summary-recenter)
10098     (gnus-summary-position-cursor)
10099     (let ((oart (gnus-summary-article-number)))
10100       (and (/= oart article) oart))))
10101
10102 (defun gnus-summary-next-thread (n)
10103   "Go to the same level next N'th thread.
10104 If N is negative, search backward instead.
10105 Returns the difference between N and the number of skips actually
10106 done."
10107   (interactive "p")
10108   (gnus-set-global-variables)
10109   (let ((backward (< n 0))
10110         (n (abs n)))
10111     (while (and (> n 0)
10112                 (gnus-summary-go-to-next-thread backward))
10113       (setq n (1- n)))
10114     (gnus-summary-position-cursor)
10115     (if (/= 0 n) (gnus-message 7 "No more threads"))
10116     n))
10117
10118 (defun gnus-summary-prev-thread (n)
10119   "Go to the same level previous N'th thread.
10120 Returns the difference between N and the number of skips actually
10121 done."
10122   (interactive "p")
10123   (gnus-set-global-variables)
10124   (gnus-summary-next-thread (- n)))
10125
10126 (defun gnus-summary-go-down-thread (&optional same)
10127   "Go down one level in the current thread.
10128 If SAME is non-nil, also move to articles of the same level."
10129   (let ((level (gnus-summary-thread-level))
10130         (start (point)))
10131     (if (and (zerop (forward-line 1))
10132              (> (gnus-summary-thread-level) level))
10133         t
10134       (goto-char start)
10135       nil)))
10136
10137 (defun gnus-summary-go-up-thread ()
10138   "Go up one level in the current thread."
10139   (let ((level (gnus-summary-thread-level))
10140         (start (point)))
10141     (while (and (zerop (forward-line -1))
10142                 (>= (gnus-summary-thread-level) level)))
10143     (if (>= (gnus-summary-thread-level) level)
10144         (progn
10145           (goto-char start)
10146           nil)
10147       t)))
10148
10149 (defun gnus-summary-down-thread (n)
10150   "Go down thread N steps.
10151 If N is negative, go up instead.
10152 Returns the difference between N and how many steps down that were
10153 taken."
10154   (interactive "p")
10155   (gnus-set-global-variables)
10156   (let ((up (< n 0))
10157         (n (abs n)))
10158     (while (and (> n 0)
10159                 (if up (gnus-summary-go-up-thread)
10160                   (gnus-summary-go-down-thread)))
10161       (setq n (1- n)))
10162     (gnus-summary-position-cursor)
10163     (if (/= 0 n) (gnus-message 7 "Can't go further"))
10164     n))
10165
10166 (defun gnus-summary-up-thread (n)
10167   "Go up thread N steps.
10168 If N is negative, go up instead.
10169 Returns the difference between N and how many steps down that were
10170 taken."
10171   (interactive "p")
10172   (gnus-set-global-variables)
10173   (gnus-summary-down-thread (- n)))
10174
10175 (defun gnus-summary-kill-thread (&optional unmark)
10176   "Mark articles under current thread as read.
10177 If the prefix argument is positive, remove any kinds of marks.
10178 If the prefix argument is negative, tick articles instead."
10179   (interactive "P")
10180   (gnus-set-global-variables)
10181   (if unmark
10182       (setq unmark (prefix-numeric-value unmark)))
10183   (let ((killing t)
10184         (level (gnus-summary-thread-level)))
10185     (save-excursion
10186       ;; Expand the thread.
10187       (gnus-summary-show-thread)
10188       (while killing
10189         ;; Mark the article...
10190         (cond ((null unmark) (gnus-summary-mark-article-as-read
10191                               gnus-killed-mark))
10192               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10193                              gnus-unread-mark))
10194               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10195         ;; ...and go forward until either the buffer ends or the subtree
10196         ;; ends. 
10197         (if (not (and (zerop (forward-line 1))
10198                       (> (gnus-summary-thread-level) level)))
10199             (setq killing nil))))
10200     ;; Hide killed subtrees.
10201     (and (null unmark)
10202          gnus-thread-hide-killed
10203          (gnus-summary-hide-thread))
10204     ;; If marked as read, go to next unread subject.
10205     (if (null unmark)
10206         ;; Go to next unread subject.
10207         (gnus-summary-next-subject 1 t)))
10208   (gnus-set-mode-line 'summary))
10209
10210 ;; Summary sorting commands
10211
10212 (defun gnus-summary-sort-by-number (&optional reverse)
10213   "Sort summary buffer by article number.
10214 Argument REVERSE means reverse order."
10215   (interactive "P")
10216   (gnus-set-global-variables)
10217   (gnus-summary-sort 
10218    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10219    ;; a function, so we wrap it.
10220    (cons (lambda () (gnus-summary-article-number))
10221          'gnus-thread-sort-by-number) reverse))
10222
10223 (defun gnus-summary-sort-by-author (&optional reverse)
10224   "Sort summary buffer by author name alphabetically.
10225 If case-fold-search is non-nil, case of letters is ignored.
10226 Argument REVERSE means reverse order."
10227   (interactive "P")
10228   (gnus-set-global-variables)
10229   (gnus-summary-sort
10230    (cons
10231     (lambda ()
10232       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10233              (extract (funcall
10234                        gnus-extract-address-components
10235                        (mail-header-from header))))
10236         (concat (or (car extract) (cdr extract))
10237                 "\r" (int-to-string (mail-header-number header))
10238                 "\r" (mail-header-subject header))))
10239     'gnus-thread-sort-by-author)
10240    reverse))
10241
10242 (defun gnus-summary-sort-by-subject (&optional reverse)
10243   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10244 If case-fold-search is non-nil, case of letters is ignored.
10245 Argument REVERSE means reverse order."
10246   (interactive "P")
10247   (gnus-set-global-variables)
10248   (gnus-summary-sort
10249    (cons
10250     (lambda ()
10251       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10252              (extract (funcall
10253                        gnus-extract-address-components
10254                        (mail-header-from header))))
10255         (concat 
10256          (downcase (gnus-simplify-subject (gnus-summary-subject-string) t))
10257          "\r" (int-to-string (mail-header-number header))
10258          "\r" (or (car extract) (cdr extract)))))
10259     'gnus-thread-sort-by-subject)
10260    reverse))
10261
10262 (defun gnus-summary-sort-by-date (&optional reverse)
10263   "Sort summary buffer by date.
10264 Argument REVERSE means reverse order."
10265   (interactive "P")
10266   (gnus-set-global-variables)
10267   (gnus-summary-sort
10268    (cons
10269     (lambda ()
10270       (gnus-sortable-date
10271        (mail-header-date 
10272         (gnus-get-header-by-num (gnus-summary-article-number)))))
10273     'gnus-thread-sort-by-date)
10274    reverse))
10275
10276 (defun gnus-summary-sort-by-score (&optional reverse)
10277   "Sort summary buffer by score.
10278 Argument REVERSE means reverse order."
10279   (interactive "P")
10280   (gnus-set-global-variables)
10281   (gnus-summary-sort 
10282    (cons (lambda () (gnus-summary-article-score))
10283          'gnus-thread-sort-by-score)
10284    (not reverse)))
10285
10286 (defvar gnus-summary-already-sorted nil)
10287 (defun gnus-summary-sort (predicate reverse)
10288   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10289   (if gnus-summary-already-sorted
10290       ()
10291     (let (buffer-read-only)
10292       (if (not gnus-show-threads)
10293           ;; We do untreaded sorting...
10294           (progn
10295             (goto-char (point-min))
10296             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10297         ;; ... or we do threaded sorting.
10298         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10299               (gnus-summary-prepare-hook nil)
10300               (gnus-summary-already-sorted nil))
10301           ;; We do that by simply regenerating the threads.
10302           (gnus-summary-prepare)
10303           (and gnus-show-threads
10304                gnus-thread-hide-subtree
10305                (gnus-summary-hide-all-threads))
10306           ;; If in async mode, we send some info to the backend.
10307           (and gnus-newsgroup-async
10308                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10309                (gnus-request-asynchronous 
10310                 gnus-newsgroup-name
10311                 (if (and gnus-asynchronous-article-function
10312                          (fboundp gnus-asynchronous-article-function))
10313                     (funcall gnus-asynchronous-article-function
10314                              gnus-newsgroup-threads)
10315                   gnus-newsgroup-threads))))))))
10316
10317   
10318 (defun gnus-sortable-date (date)
10319   "Make sortable string by string-lessp from DATE.
10320 Timezone package is used."
10321   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10322          (year (aref date 0))
10323          (month (aref date 1))
10324          (day (aref date 2)))
10325     (timezone-make-sortable-date 
10326      year month day 
10327      (timezone-make-time-string
10328       (aref date 3) (aref date 4) (aref date 5)))))
10329
10330
10331 ;; Summary saving commands.
10332
10333 (defun gnus-summary-save-article (&optional n)
10334   "Save the current article using the default saver function.
10335 If N is a positive number, save the N next articles.
10336 If N is a negative number, save the N previous articles.
10337 If N is nil and any articles have been marked with the process mark,
10338 save those articles instead.
10339 The variable `gnus-default-article-saver' specifies the saver function."
10340   (interactive "P")
10341   (gnus-set-global-variables)
10342   (let ((articles (gnus-summary-work-articles n)))
10343     (while articles
10344       (let ((header (gnus-get-header-by-num (car articles))))
10345         (if (vectorp header)
10346             (progn
10347               (save-window-excursion
10348                 (gnus-summary-select-article t nil nil (car articles)))
10349               (or gnus-save-all-headers
10350                   (gnus-article-hide-headers t))
10351               ;; Remove any X-Gnus lines.
10352               (save-excursion
10353                 (save-restriction
10354                   (set-buffer gnus-article-buffer)
10355                   (let ((buffer-read-only nil))
10356                     (goto-char (point-min))
10357                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10358                                                   (point-max)))
10359                     (while (re-search-forward "^X-Gnus" nil t)
10360                       (beginning-of-line)
10361                       (delete-region (point)
10362                                      (progn (forward-line 1) (point))))
10363                     (widen))))
10364               (save-window-excursion
10365                 (if gnus-default-article-saver
10366                     (funcall gnus-default-article-saver)
10367                   (error "No default saver is defined."))))
10368           (if (assq 'name header)
10369               (gnus-copy-file (cdr (assq 'name header)))
10370             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10371       (gnus-summary-remove-process-mark (car articles))
10372       (setq articles (cdr articles)))
10373     (gnus-summary-position-cursor)
10374     n))
10375
10376 (defun gnus-summary-pipe-output (&optional arg)
10377   "Pipe the current article to a subprocess.
10378 If N is a positive number, pipe the N next articles.
10379 If N is a negative number, pipe the N previous articles.
10380 If N is nil and any articles have been marked with the process mark,
10381 pipe those articles instead."
10382   (interactive "P")
10383   (gnus-set-global-variables)
10384   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10385     (gnus-summary-save-article arg)))
10386
10387 (defun gnus-summary-save-article-mail (&optional arg)
10388   "Append the current article to an mail file.
10389 If N is a positive number, save the N next articles.
10390 If N is a negative number, save the N previous articles.
10391 If N is nil and any articles have been marked with the process mark,
10392 save those articles instead."
10393   (interactive "P")
10394   (gnus-set-global-variables)
10395   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10396     (gnus-summary-save-article arg)))
10397
10398 (defun gnus-summary-save-article-rmail (&optional arg)
10399   "Append the current article to an rmail file.
10400 If N is a positive number, save the N next articles.
10401 If N is a negative number, save the N previous articles.
10402 If N is nil and any articles have been marked with the process mark,
10403 save those articles instead."
10404   (interactive "P")
10405   (gnus-set-global-variables)
10406   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10407     (gnus-summary-save-article arg)))
10408
10409 (defun gnus-summary-save-article-file (&optional arg)
10410   "Append the current article to a file.
10411 If N is a positive number, save the N next articles.
10412 If N is a negative number, save the N previous articles.
10413 If N is nil and any articles have been marked with the process mark,
10414 save those articles instead."
10415   (interactive "P")
10416   (gnus-set-global-variables)
10417   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10418     (gnus-summary-save-article arg)))
10419
10420 (defun gnus-read-save-file-name (prompt default-name)
10421   (let ((methods gnus-split-methods)
10422         split-name)
10423     (if (not gnus-split-methods)
10424         ()
10425       (save-excursion
10426         (set-buffer gnus-article-buffer)
10427         (gnus-narrow-to-headers)
10428         (while methods
10429           (goto-char (point-min))
10430           (and (condition-case () 
10431                    (re-search-forward (car (car methods)) nil t)
10432                  (error nil))
10433                (setq split-name (cons (nth 1 (car methods)) split-name)))
10434           (setq methods (cdr methods)))
10435         (widen)))
10436     (cond ((null split-name)
10437            (read-file-name
10438             (concat prompt " (default "
10439                     (file-name-nondirectory default-name) ") ")
10440             (file-name-directory default-name)
10441             default-name))
10442           ((= 1 (length split-name))
10443            (read-file-name
10444             (concat prompt " (default " (car split-name) ") ")
10445             gnus-article-save-directory
10446             (concat gnus-article-save-directory (car split-name))))
10447           (t
10448            (setq split-name (mapcar (lambda (el) (list el))
10449                                     (nreverse split-name)))
10450            (let ((result (completing-read 
10451                           (concat prompt " ")
10452                           split-name nil nil)))
10453              (concat gnus-article-save-directory
10454                      (if (string= result "")
10455                          (car (car split-name))
10456                        result)))))))
10457
10458 (defun gnus-summary-save-in-rmail (&optional filename)
10459   "Append this article to Rmail file.
10460 Optional argument FILENAME specifies file name.
10461 Directory to save to is default to `gnus-article-save-directory' which
10462 is initialized from the SAVEDIR environment variable."
10463   (interactive)
10464   (gnus-set-global-variables)
10465   (let ((default-name
10466           (funcall gnus-rmail-save-name gnus-newsgroup-name
10467                    gnus-current-headers gnus-newsgroup-last-rmail)))
10468     (or filename
10469         (setq filename (gnus-read-save-file-name 
10470                         "Save in rmail file:" default-name)))
10471     (gnus-make-directory (file-name-directory filename))
10472     (gnus-eval-in-buffer-window 
10473      gnus-article-buffer
10474      (save-excursion
10475        (save-restriction
10476          (widen)
10477          (gnus-output-to-rmail filename))))
10478     ;; Remember the directory name to save articles
10479     (setq gnus-newsgroup-last-rmail filename)))
10480
10481 (defun gnus-summary-save-in-mail (&optional filename)
10482   "Append this article to Unix mail file.
10483 Optional argument FILENAME specifies file name.
10484 Directory to save to is default to `gnus-article-save-directory' which
10485 is initialized from the SAVEDIR environment variable."
10486   (interactive)
10487   (gnus-set-global-variables)
10488   (let ((default-name
10489           (funcall gnus-mail-save-name gnus-newsgroup-name
10490                    gnus-current-headers gnus-newsgroup-last-mail)))
10491     (or filename
10492         (setq filename (gnus-read-save-file-name 
10493                         "Save in Unix mail file:" default-name)))
10494     (setq filename
10495           (expand-file-name filename
10496                             (and default-name
10497                                  (file-name-directory 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          (if (and (file-readable-p filename) (rmail-file-p filename))
10505              (gnus-output-to-rmail filename)
10506            (rmail-output filename 1 t t)))))
10507     ;; Remember the directory name to save articles.
10508     (setq gnus-newsgroup-last-mail filename)))
10509
10510 (defun gnus-summary-save-in-file (&optional filename)
10511   "Append this article to file.
10512 Optional argument FILENAME specifies file name.
10513 Directory to save to is default to `gnus-article-save-directory' which
10514 is initialized from the SAVEDIR environment variable."
10515   (interactive)
10516   (gnus-set-global-variables)
10517   (let ((default-name
10518           (funcall gnus-file-save-name gnus-newsgroup-name
10519                    gnus-current-headers gnus-newsgroup-last-file)))
10520     (or filename
10521         (setq filename (gnus-read-save-file-name 
10522                         "Save in file:" default-name)))
10523     (gnus-make-directory (file-name-directory filename))
10524     (gnus-eval-in-buffer-window 
10525      gnus-article-buffer
10526      (save-excursion
10527        (save-restriction
10528          (widen)
10529          (gnus-output-to-file filename))))
10530     ;; Remember the directory name to save articles.
10531     (setq gnus-newsgroup-last-file filename)))
10532
10533 (defun gnus-summary-save-in-pipe (&optional command)
10534   "Pipe this article to subprocess."
10535   (interactive)
10536   (gnus-set-global-variables)
10537   (let ((command (read-string "Shell command on article: "
10538                               gnus-last-shell-command)))
10539     (if (string-equal command "")
10540         (setq command gnus-last-shell-command))
10541     (gnus-eval-in-buffer-window 
10542      gnus-article-buffer
10543      (save-restriction
10544        (widen)
10545        (shell-command-on-region (point-min) (point-max) command nil)))
10546     (setq gnus-last-shell-command command)))
10547
10548 ;; Summary extract commands
10549
10550 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10551   (let ((buffer-read-only nil)
10552         (article (gnus-summary-article-number))
10553         b)
10554     (or (gnus-summary-goto-subject article)
10555         (error (format "No such article: %d" article)))
10556     (gnus-summary-position-cursor)
10557     ;; If all commands are to be bunched up on one line, we collect
10558     ;; them here.  
10559     (if gnus-view-pseudos-separately
10560         ()
10561       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10562             files action)
10563         (while ps
10564           (setq action (cdr (assq 'action (car ps))))
10565           (setq files (list (cdr (assq 'name (car ps)))))
10566           (while (and ps (cdr ps)
10567                       (string= (or action "1")
10568                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10569             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10570             (setcdr ps (cdr (cdr ps))))
10571           (if (not files)
10572               ()
10573             (if (not (string-match "%s" action))
10574                 (setq files (cons " " files)))
10575             (setq files (cons " " files))
10576             (and (assq 'execute (car ps))
10577                  (setcdr (assq 'execute (car ps))
10578                          (funcall (if (string-match "%s" action)
10579                                       'format 'concat)
10580                                   action 
10581                                   (mapconcat (lambda (f) f) files " ")))))
10582           (setq ps (cdr ps)))))
10583     (if (and gnus-view-pseudos (not not-view))
10584         (while pslist
10585           (and (assq 'execute (car pslist))
10586                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10587                                      (eq gnus-view-pseudos 'not-confirm)))
10588           (setq pslist (cdr pslist)))
10589       (save-excursion
10590         (while pslist
10591           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10592                                          (gnus-summary-article-number)))
10593           (forward-line 1)
10594           (setq b (point))
10595           (insert "          " (file-name-nondirectory 
10596                                 (cdr (assq 'name (car pslist))))
10597                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10598           (add-text-properties 
10599            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10600                           'gnus-mark gnus-unread-mark 
10601                           'gnus-level 0
10602                           'gnus-pseudo (car pslist)))
10603           (forward-line -1)
10604           (gnus-sethash (int-to-string gnus-reffed-article-number)
10605                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10606           (setq gnus-newsgroup-unreads
10607                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10608           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10609           (setq pslist (cdr pslist)))))))
10610
10611 (defun gnus-pseudos< (p1 p2)
10612   (let ((c1 (cdr (assq 'action p1)))
10613         (c2 (cdr (assq 'action p2))))
10614     (and c1 c2 (string< c1 c2))))
10615
10616 (defun gnus-request-pseudo-article (props)
10617   (cond ((assq 'execute props)
10618          (gnus-execute-command (cdr (assq 'execute props)))))
10619   (let ((gnus-current-article (gnus-summary-article-number)))
10620     (run-hooks 'gnus-mark-article-hook)))
10621
10622 (defun gnus-execute-command (command &optional automatic)
10623   (save-excursion
10624     (gnus-article-setup-buffer)
10625     (set-buffer gnus-article-buffer)
10626     (let ((command (if automatic command (read-string "Command: " command)))
10627           (buffer-read-only nil))
10628       (erase-buffer)
10629       (insert "$ " command "\n\n")
10630       (if gnus-view-pseudo-asynchronously
10631           (start-process "gnus-execute" nil "sh" "-c" command)
10632         (call-process "sh" nil t nil "-c" command)))))
10633
10634 (defun gnus-copy-file (file &optional to)
10635   "Copy FILE to TO."
10636   (interactive
10637    (list (read-file-name "Copy file: " default-directory)
10638          (read-file-name "Copy file to: " default-directory)))
10639   (gnus-set-global-variables)
10640   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10641   (and (file-directory-p to) 
10642        (setq to (concat (file-name-as-directory to)
10643                         (file-name-nondirectory file))))
10644   (copy-file file to))
10645
10646 ;; Summary kill commands.
10647
10648 (defun gnus-summary-edit-global-kill (article)
10649   "Edit the \"global\" kill file."
10650   (interactive (list (gnus-summary-article-number)))
10651   (gnus-set-global-variables)
10652   (gnus-group-edit-global-kill article))
10653
10654 (defun gnus-summary-edit-local-kill ()
10655   "Edit a local kill file applied to the current newsgroup."
10656   (interactive)
10657   (gnus-set-global-variables)
10658   (setq gnus-current-headers 
10659         (gnus-gethash 
10660          (int-to-string (gnus-summary-article-number))
10661          gnus-newsgroup-headers-hashtb-by-number))
10662   (gnus-set-global-variables)
10663   (gnus-group-edit-local-kill 
10664    (gnus-summary-article-number) gnus-newsgroup-name))
10665
10666 \f
10667 ;;;
10668 ;;; Gnus article mode
10669 ;;;
10670
10671 (put 'gnus-article-mode 'mode-class 'special)
10672
10673 (defvar gnus-boogaboo nil)
10674
10675 (if gnus-article-mode-map
10676     nil
10677   (setq gnus-article-mode-map (make-keymap))
10678   (suppress-keymap gnus-article-mode-map)
10679   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10680   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10681   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10682   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10683   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10684   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10685   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10686   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10687   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10688   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10689   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10690   
10691   ;; Duplicate almost all summary keystrokes in the article mode map.
10692   (let ((commands 
10693          (list 
10694           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10695           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10696           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10697           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10698           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10699           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10700           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10701           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10702           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10703           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10704           "\C-c\C-v\C-v" "\C-d" "v" 
10705 ;;        "Mt" "M!" "Md" "Mr"
10706 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10707 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10708 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10709 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10710 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10711 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10712 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10713 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10714 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10715 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10716 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10717           )))
10718     (while (and gnus-boogaboo commands) ; disabled
10719       (define-key gnus-article-mode-map (car commands) 
10720         'gnus-article-summary-command)
10721       (setq commands (cdr commands))))
10722
10723   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10724 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10725                          "=" "n"  "^" "\M-^")))
10726     (while (and gnus-boogaboo commands) ; disabled
10727       (define-key gnus-article-mode-map (car commands) 
10728         'gnus-article-summary-command-nosave)
10729       (setq commands (cdr commands)))))
10730
10731
10732 (defun gnus-article-mode ()
10733   "Major mode for displaying an article.
10734
10735 All normal editing commands are switched off.
10736
10737 The following commands are available:
10738
10739 \\<gnus-article-mode-map>
10740 \\[gnus-article-next-page]\t Scroll the article one page forwards
10741 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10742 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10743 \\[gnus-article-show-summary]\t Display the summary buffer
10744 \\[gnus-article-mail]\t Send a reply to the address near point
10745 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10746 \\[gnus-info-find-node]\t Go to the Gnus info node"
10747   (interactive)
10748   (if gnus-visual (gnus-article-make-menu-bar))
10749   (kill-all-local-variables)
10750   (setq mode-line-modified "-- ")
10751   (make-local-variable 'mode-line-format)
10752   (setq mode-line-format (copy-sequence mode-line-format))
10753   (and (equal (nth 3 mode-line-format) "   ")
10754        (setcar (nthcdr 3 mode-line-format) ""))
10755   (setq mode-name "Article")
10756   (setq major-mode 'gnus-article-mode)
10757   (make-local-variable 'minor-mode-alist)
10758   (or (assq 'gnus-show-mime minor-mode-alist)
10759       (setq minor-mode-alist
10760             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10761   (use-local-map gnus-article-mode-map)
10762   (make-local-variable 'page-delimiter)
10763   (setq page-delimiter gnus-page-delimiter)
10764   (buffer-disable-undo (current-buffer))
10765   (setq buffer-read-only t)             ;Disable modification
10766   (run-hooks 'gnus-article-mode-hook))
10767
10768 (defun gnus-article-setup-buffer ()
10769   "Initialize article mode buffer."
10770   ;; Returns the article buffer.
10771   (if (get-buffer gnus-article-buffer)
10772       (save-excursion
10773         (set-buffer gnus-article-buffer)
10774         (buffer-disable-undo (current-buffer))
10775         (setq buffer-read-only t)
10776         (gnus-add-current-to-buffer-list)
10777         (or (eq major-mode 'gnus-article-mode)
10778             (gnus-article-mode))
10779         (current-buffer))
10780     (save-excursion
10781       (set-buffer (get-buffer-create gnus-article-buffer))
10782       (gnus-add-current-to-buffer-list)
10783       (gnus-article-mode)
10784       (current-buffer))))
10785
10786 ;; Set article window start at LINE, where LINE is the number of lines
10787 ;; from the head of the article.
10788 (defun gnus-article-set-window-start (&optional line)
10789   (set-window-start 
10790    (get-buffer-window gnus-article-buffer)
10791    (save-excursion
10792      (set-buffer gnus-article-buffer)
10793      (goto-char (point-min))
10794      (if (not line)
10795          (point-min)
10796        (gnus-message 6 "Moved to bookmark")
10797        (search-forward "\n\n" nil t)
10798        (forward-line line)
10799        (point)))))
10800
10801 (defun gnus-request-article-this-buffer (article group)
10802   "Get an article and insert it into this buffer."
10803   (setq group (or group gnus-newsgroup-name))
10804
10805   ;; Open server if it has closed.
10806   (gnus-check-server (gnus-find-method-for-group group))
10807
10808   ;; Using `gnus-request-article' directly will insert the article into
10809   ;; `nntp-server-buffer' - so we'll save some time by not having to
10810   ;; copy it from the server buffer into the article buffer.
10811
10812   ;; We only request an article by message-id when we do not have the
10813   ;; headers for it, so we'll have to get those.
10814   (and (stringp article) 
10815        (let ((gnus-override-method gnus-refer-article-method))
10816          (gnus-read-header article)))
10817
10818   ;; If the article number is negative, that means that this article
10819   ;; doesn't belong in this newsgroup (possibly), so we find its
10820   ;; message-id and request it by id instead of number.
10821   (if (not (numberp article))
10822       ()
10823     (save-excursion
10824       (set-buffer gnus-summary-buffer)
10825       (let ((header (gnus-get-header-by-num article)))
10826         (if (< article 0)
10827             (if (vectorp header)
10828                 ;; It's a real article.
10829                 (setq article (mail-header-id header))
10830               ;; It is an extracted pseudo-article.
10831               (setq article 'pseudo)
10832               (gnus-request-pseudo-article header)))
10833
10834         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10835           (if (not (eq (car method) 'nneething))
10836               ()
10837             (let ((dir (concat (file-name-as-directory (nth 1 method))
10838                                (mail-header-subject header))))
10839               (if (file-directory-p dir)
10840                   (progn
10841                     (setq article 'nneething)
10842                     (gnus-group-enter-directory dir)))))))))
10843
10844   ;; Check the cache.
10845   (if (and gnus-use-cache
10846            (numberp article)
10847            (gnus-cache-request-article article group))
10848       'article
10849     ;; Get the article and into the article buffer.
10850     (if (or (stringp article) (numberp article))
10851         (progn
10852           (erase-buffer)
10853           (let ((gnus-override-method 
10854                  (and (stringp article) gnus-refer-article-method)))
10855             (and (gnus-request-article article group (current-buffer))
10856                  'article)))
10857       article)))
10858
10859 (defun gnus-read-header (id)
10860   "Read the headers of article ID and enter them into the Gnus system."
10861   (let (header)
10862     (if (not (setq header 
10863                    (car (if (let ((gnus-nov-is-evil t))
10864                               (gnus-retrieve-headers 
10865                                (list id) gnus-newsgroup-name))
10866                             (gnus-get-newsgroup-headers)))))
10867         nil
10868       (if (stringp id)
10869           (mail-header-set-number header gnus-reffed-article-number))
10870       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10871       (gnus-sethash (int-to-string (mail-header-number header)) header
10872                     gnus-newsgroup-headers-hashtb-by-number)
10873       (if (stringp id)
10874           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10875       (setq gnus-current-headers header)
10876       header)))
10877
10878 (defun gnus-article-prepare (article &optional all-headers header)
10879   "Prepare ARTICLE in article mode buffer.
10880 ARTICLE should either be an article number or a Message-ID.
10881 If ARTICLE is an id, HEADER should be the article headers.
10882 If ALL-HEADERS is non-nil, no headers are hidden."
10883   (save-excursion
10884     ;; Make sure we start in a summary buffer.
10885     (or (eq major-mode 'gnus-summary-mode)
10886         (set-buffer gnus-summary-buffer))
10887     (setq gnus-summary-buffer (current-buffer))
10888     ;; Make sure the connection to the server is alive.
10889     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10890         (progn
10891           (gnus-check-server 
10892            (gnus-find-method-for-group gnus-newsgroup-name))
10893           (gnus-request-group gnus-newsgroup-name t)))
10894     (let* ((article (if header (mail-header-number header) article))
10895            (summary-buffer (current-buffer))
10896            (internal-hook gnus-article-internal-prepare-hook)
10897            (group gnus-newsgroup-name)
10898            result)
10899       (save-excursion
10900         (gnus-article-setup-buffer)
10901         (set-buffer gnus-article-buffer)
10902         (if (not (setq result (let ((buffer-read-only nil))
10903                                 (gnus-request-article-this-buffer 
10904                                  article group))))
10905             ;; There is no such article.
10906             (save-excursion
10907               (if (not (numberp article))
10908                   ()
10909                 (setq gnus-article-current 
10910                       (cons gnus-newsgroup-name article))
10911                 (set-buffer gnus-summary-buffer)
10912                 (setq gnus-current-article article)
10913                 (gnus-summary-mark-article article gnus-canceled-mark))
10914               (gnus-message 1 "No such article (may be canceled)")
10915               (ding)
10916               nil)
10917           (if (or (eq result 'pseudo) (eq result 'nneething))
10918               (progn
10919                 (save-excursion
10920                   (set-buffer summary-buffer)
10921                   (setq gnus-last-article gnus-current-article
10922                         gnus-newsgroup-history (cons gnus-current-article
10923                                                      gnus-newsgroup-history)
10924                         gnus-current-article 0
10925                         gnus-current-headers nil
10926                         gnus-article-current nil)
10927                   (if (eq result 'nneething)
10928                       (gnus-configure-windows 'summary)
10929                     (gnus-configure-windows 'article))
10930                   (gnus-set-global-variables))
10931                 (gnus-set-mode-line 'article))
10932             ;; The result from the `request' was an actual article -
10933             ;; or at least some text that is now displayed in the
10934             ;; article buffer.
10935             (if (and (numberp article)
10936                      (not (eq article gnus-current-article)))
10937                 ;; Seems like a new article has been selected.
10938                 ;; `gnus-current-article' must be an article number.
10939                 (save-excursion
10940                   (set-buffer summary-buffer)
10941                   (setq gnus-last-article gnus-current-article
10942                         gnus-newsgroup-history (cons gnus-current-article
10943                                                      gnus-newsgroup-history)
10944                         gnus-current-article article
10945                         gnus-current-headers 
10946                         (gnus-get-header-by-num gnus-current-article)
10947                         gnus-article-current 
10948                         (cons gnus-newsgroup-name gnus-current-article))
10949                   (gnus-summary-show-thread)
10950                   (run-hooks 'gnus-mark-article-hook)
10951                   (gnus-set-mode-line 'summary)
10952                   (and gnus-visual 
10953                        (run-hooks 'gnus-visual-mark-article-hook))
10954                   ;; Set the global newsgroup variables here.
10955                   ;; Suggested by Jim Sisolak
10956                   ;; <sisolak@trans4.neep.wisc.edu>.
10957                   (gnus-set-global-variables)
10958                   (setq gnus-have-all-headers 
10959                         (or all-headers gnus-show-all-headers))
10960                   (and gnus-use-cache 
10961                        (vectorp (gnus-get-header-by-number article))
10962                        (gnus-cache-possibly-enter-article
10963                         group article
10964                         (gnus-get-header-by-number article)
10965                         (memq article gnus-newsgroup-marked)
10966                         (memq article gnus-newsgroup-dormant)
10967                         (memq article gnus-newsgroup-unreads)))))
10968             ;; Hooks for getting information from the article.
10969             ;; This hook must be called before being narrowed.
10970             (let (buffer-read-only)
10971               (run-hooks 'internal-hook)
10972               (run-hooks 'gnus-article-prepare-hook)
10973               ;; Decode MIME message.
10974               (if (and gnus-show-mime
10975                        (or (not gnus-strict-mime)
10976                            (gnus-fetch-field "Mime-Version")))
10977                   (funcall gnus-show-mime-method))
10978               ;; Perform the article display hooks.
10979               (run-hooks 'gnus-article-display-hook))
10980             ;; Do page break.
10981             (goto-char (point-min))
10982             (and gnus-break-pages (gnus-narrow-to-page))
10983             (gnus-set-mode-line 'article)
10984             (gnus-configure-windows 'article)
10985             (goto-char (point-min))
10986             t))))))
10987
10988 (defun gnus-article-show-all-headers ()
10989   "Show all article headers in article mode buffer."
10990   (save-excursion 
10991     (gnus-article-setup-buffer)
10992     (set-buffer gnus-article-buffer)
10993     (let ((buffer-read-only nil))
10994       (remove-text-properties (point-min) (point-max) 
10995                               gnus-hidden-properties))))
10996
10997 (defun gnus-article-hide-headers-if-wanted ()
10998   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10999 Provided for backwards compatability."
11000   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
11001       (gnus-article-hide-headers)))
11002
11003 (defun gnus-article-hide-headers (&optional delete)
11004   "Hide unwanted headers and possibly sort them as well."
11005   (interactive "P")
11006   (save-excursion
11007     (set-buffer gnus-article-buffer)
11008     (save-restriction
11009       (let ((sorted gnus-sorted-header-list)
11010             (buffer-read-only nil)
11011             want-list beg want-l)
11012         ;; First we narrow to just the headers.
11013         (widen)
11014         (goto-char (point-min))
11015         ;; Hide any "From " lines at the beginning of (mail) articles. 
11016         (while (looking-at "From ")
11017           (forward-line 1))
11018         (or (bobp) 
11019             (add-text-properties (point-min) (point) gnus-hidden-properties))
11020         ;; Then treat the rest of the header lines.
11021         (narrow-to-region 
11022          (point) 
11023          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
11024         ;; Then we use the two regular expressions
11025         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
11026         ;; select which header lines is to remain visible in the
11027         ;; article buffer.
11028         (goto-char (point-min))
11029         (while (re-search-forward "^[^ \t]*:" nil t)
11030           (beginning-of-line)
11031           ;; We add the headers we want to keep to a list and delete
11032           ;; them from the buffer.
11033           (if (or (and (stringp gnus-visible-headers)
11034                        (looking-at gnus-visible-headers))
11035                   (and (not (stringp gnus-visible-headers))
11036                        (stringp gnus-ignored-headers)
11037                        (not (looking-at gnus-ignored-headers))))
11038               (progn
11039                 (setq beg (point))
11040                 (forward-line 1)
11041                 ;; Be sure to get multi-line headers...
11042                 (re-search-forward "^[^ \t]*:" nil t)
11043                 (beginning-of-line)
11044                 (setq want-list 
11045                       (cons (buffer-substring beg (point)) want-list))
11046                 (delete-region beg (point))
11047                 (goto-char beg))
11048             (forward-line 1)))
11049         ;; Next we perform the sorting by looking at
11050         ;; `gnus-sorted-header-list'. 
11051         (goto-char (point-min))
11052         (while (and sorted want-list)
11053           (setq want-l want-list)
11054           (while (and want-l
11055                       (not (string-match (car sorted) (car want-l))))
11056             (setq want-l (cdr want-l)))
11057           (if want-l 
11058               (progn
11059                 (insert (car want-l))
11060                 (setq want-list (delq (car want-l) want-list))))
11061           (setq sorted (cdr sorted)))
11062         ;; Any headers that were not matched by the sorted list we
11063         ;; just tack on the end of the visible header list.
11064         (while want-list
11065           (insert (car want-list))
11066           (setq want-list (cdr want-list)))
11067         ;; And finally we make the unwanted headers invisible.
11068         (if delete
11069             (delete-region (point) (point-max))
11070           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
11071           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
11072
11073 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
11074 (defun gnus-article-treat-overstrike ()
11075   "Translate overstrikes into bold text."
11076   (interactive)
11077   (save-excursion
11078     (set-buffer gnus-article-buffer)
11079     (let ((buffer-read-only nil))
11080       (while (search-forward "\b" nil t)
11081         (let ((next (following-char))
11082               (previous (char-after (- (point) 2))))
11083           (cond ((eq next previous)
11084                  (put-text-property (- (point) 2) (point)
11085                                     'invisible t)
11086                  (put-text-property (point) (1+ (point))
11087                                     'face 'bold))
11088                 ((eq next ?_)
11089                  (put-text-property (1- (point)) (1+ (point))
11090                                     'invisible t)
11091                  (put-text-property (1- (point)) (point)
11092                                     'face 'underline))
11093                 ((eq previous ?_)
11094                  (put-text-property (- (point) 2) (point)
11095                                     'invisible t)
11096                  (put-text-property (point) (1+ (point))
11097                                     'face 'underline))))))))
11098
11099 (defun gnus-article-word-wrap ()
11100   "Format too long lines."
11101   (interactive)
11102   (save-excursion
11103     (set-buffer gnus-article-buffer)
11104     (let ((buffer-read-only nil))
11105       (goto-char (point-min))
11106       (search-forward "\n\n" nil t)
11107       (end-of-line 1)
11108       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
11109             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
11110             (adaptive-fill-mode t))
11111         (while (not (eobp))
11112           (and (>= (current-column) (min fill-column (window-width)))
11113                (/= (preceding-char) ?:)
11114                (fill-paragraph nil))
11115           (end-of-line 2))))))
11116
11117 (defun gnus-article-remove-cr ()
11118   "Remove carriage returns from an article."
11119   (interactive)
11120   (save-excursion
11121     (set-buffer gnus-article-buffer)
11122     (let ((buffer-read-only nil))
11123       (goto-char (point-min))
11124       (while (search-forward "\r" nil t)
11125         (replace-match "" t t)))))
11126
11127 (defun gnus-article-display-x-face (&optional force)
11128   "Look for an X-Face header and display it if present."
11129   (interactive (list 'force))
11130   (save-excursion
11131     (set-buffer gnus-article-buffer)
11132     (let ((inhibit-point-motion-hooks t)
11133           (case-fold-search nil)
11134           from)
11135       (save-restriction
11136         (goto-char (point-min))
11137         (search-forward "\n\n")
11138         (narrow-to-region (point-min) (point))
11139         (goto-char (point-min))
11140         (setq from (mail-fetch-field "from"))
11141         (if (not (and gnus-article-x-face-command
11142                       (or force
11143                           (not gnus-article-x-face-too-ugly)
11144                           (and gnus-article-x-face-too-ugly from
11145                                (not (string-match gnus-article-x-face-too-ugly
11146                                                   from))))
11147                       (progn
11148                         (goto-char (point-min))
11149                         (re-search-forward "^X-Face: " nil t))))
11150             nil
11151           (let ((beg (point))
11152                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
11153             (if (symbolp gnus-article-x-face-command)
11154                 (and (or (fboundp gnus-article-x-face-command)
11155                          (error "%s is not a function"
11156                                 gnus-article-x-face-command))
11157                      (funcall gnus-article-x-face-command beg end))
11158               (call-process-region beg end "sh" nil 0 nil
11159                                    "-c" gnus-article-x-face-command))))))))
11160
11161 (defun gnus-article-de-quoted-unreadable (&optional force)
11162   "Do a naive translation of a quoted-printable-encoded article.
11163 This is in no way, shape or form meant as a replacement for real MIME
11164 processing, but is simply a stop-gap measure until MIME support is
11165 written.
11166 If FORCE, decode the article whether it is marked as quoted-printable
11167 or not." 
11168   (interactive (list 'force))
11169   (save-excursion
11170     (set-buffer gnus-article-buffer)
11171     (let ((case-fold-search t)
11172           (buffer-read-only nil)
11173           (type (gnus-fetch-field "content-transfer-encoding")))
11174       (if (or force (and type (string-match "quoted-printable" type)))
11175           (progn
11176             (goto-char (point-min))
11177             (search-forward "\n\n" nil 'move)
11178             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11179
11180 (defun gnus-mime-decode-quoted-printable (from to)
11181   ;; Decode quoted-printable from region between FROM and TO.
11182   (save-excursion
11183     (goto-char from)
11184     (while (search-forward "=" to t)
11185       (cond ((eq (following-char) ?\n)
11186              (delete-char -1)
11187              (delete-char 1))
11188             ((looking-at "[0-9A-F][0-9A-F]")
11189              (delete-char -1)
11190              (insert (hexl-hex-string-to-integer
11191                       (buffer-substring (point) (+ 2 (point)))))
11192              (delete-char 2))
11193             ((looking-at "=")
11194              (delete-char 1))
11195             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11196
11197 (defvar gnus-article-time-units
11198   (list (cons 'year (* 365.25 24 60 60))
11199         (cons 'week (* 7 24 60 60))
11200         (cons 'day (* 24 60 60))
11201         (cons 'hour (* 60 60))
11202         (cons 'minute 60)
11203         (cons 'second 1)))
11204
11205 (defun gnus-article-date-ut (&optional type)
11206   "Convert DATE date to universal time in the current article.
11207 If TYPE is `local', convert to local time; if it is `lapsed', output
11208 how much time has lapsed since DATE."
11209   (interactive (list 'ut))
11210   (let ((date (mail-header-date (or gnus-current-headers 
11211                                     (gnus-get-header-by-number
11212                                      (gnus-summary-article-number))"")))
11213         (date-regexp "^Date: \\|^X-Sent: "))
11214     (if (or (not date)
11215             (string= date ""))
11216         ()
11217       (save-excursion
11218         (set-buffer gnus-article-buffer)
11219         (let ((buffer-read-only nil))
11220           (goto-char (point-min))
11221           (if (and (re-search-forward date-regexp nil t)
11222                    (progn 
11223                      (beginning-of-line)
11224                      (looking-at date-regexp)))
11225               (delete-region (gnus-point-at-bol)
11226                              (progn (end-of-line) (1+ (point))))
11227             (goto-char (point-min))
11228             (goto-char (- (search-forward "\n\n") 2)))
11229           (insert
11230            (cond 
11231             ((eq type 'local)
11232              (concat "Date: " (condition-case ()
11233                                   (timezone-make-date-arpa-standard date)
11234                                 (error date))
11235                      "\n"))
11236             ((eq type 'ut)
11237              (concat "Date: "
11238                      (condition-case ()
11239                          (timezone-make-date-arpa-standard date nil "UT")
11240                        (error date))
11241                      "\n"))
11242             ((eq type 'lapsed)
11243              ;; If the date is seriously mangled, the timezone
11244              ;; functions are liable to bug out, so we condition-case
11245              ;; the entire thing.  
11246              (let* ((real-sec (condition-case ()
11247                                   (- (gnus-seconds-since-epoch 
11248                                       (timezone-make-date-arpa-standard
11249                                        (current-time-string) 
11250                                        (current-time-zone) "UT"))
11251                                      (gnus-seconds-since-epoch 
11252                                       (timezone-make-date-arpa-standard 
11253                                        date nil "UT")))
11254                                 (error 0)))
11255                     (sec (abs real-sec))
11256                     num prev)
11257                (if (zerop sec)
11258                    "X-Sent: Now\n"
11259                  (concat
11260                   "X-Sent: "
11261                   (mapconcat 
11262                    (lambda (unit)
11263                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11264                          ""
11265                        (setq sec (- sec (* num (cdr unit))))
11266                        (prog1
11267                            (concat (if prev ", " "") (int-to-string 
11268                                                       (floor num))
11269                                    " " (symbol-name (car unit))
11270                                    (if (> num 1) "s" ""))
11271                          (setq prev t))))
11272                    gnus-article-time-units "")
11273                   (if (> real-sec 0)
11274                       " ago\n"
11275                     " in the future\n")))))
11276             (t
11277              (error "Unknown conversion type: %s" type)))))))))
11278
11279 (defun gnus-article-date-local ()
11280   "Convert the current article date to the local timezone."
11281   (interactive)
11282   (gnus-article-date-ut 'local))
11283
11284 (defun gnus-article-date-lapsed ()
11285   "Convert the current article date to time lapsed since it was sent."
11286   (interactive)
11287   (gnus-article-date-ut 'lapsed))
11288
11289 (defun gnus-article-maybe-highlight ()
11290   "Do some article highlighting if `gnus-visual' is non-nil."
11291   (if gnus-visual (gnus-article-highlight-some)))
11292
11293 ;; Article savers.
11294
11295 (defun gnus-output-to-rmail (file-name)
11296   "Append the current article to an Rmail file named FILE-NAME."
11297   (require 'rmail)
11298   ;; Most of these codes are borrowed from rmailout.el.
11299   (setq file-name (expand-file-name file-name))
11300   (setq rmail-default-rmail-file file-name)
11301   (let ((artbuf (current-buffer))
11302         (tmpbuf (get-buffer-create " *Gnus-output*")))
11303     (save-excursion
11304       (or (get-file-buffer file-name)
11305           (file-exists-p file-name)
11306           (if (gnus-yes-or-no-p
11307                (concat "\"" file-name "\" does not exist, create it? "))
11308               (let ((file-buffer (create-file-buffer file-name)))
11309                 (save-excursion
11310                   (set-buffer file-buffer)
11311                   (rmail-insert-rmail-file-header)
11312                   (let ((require-final-newline nil))
11313                     (write-region (point-min) (point-max) file-name t 1)))
11314                 (kill-buffer file-buffer))
11315             (error "Output file does not exist")))
11316       (set-buffer tmpbuf)
11317       (buffer-disable-undo (current-buffer))
11318       (erase-buffer)
11319       (insert-buffer-substring artbuf)
11320       (gnus-convert-article-to-rmail)
11321       ;; Decide whether to append to a file or to an Emacs buffer.
11322       (let ((outbuf (get-file-buffer file-name)))
11323         (if (not outbuf)
11324             (append-to-file (point-min) (point-max) file-name)
11325           ;; File has been visited, in buffer OUTBUF.
11326           (set-buffer outbuf)
11327           (let ((buffer-read-only nil)
11328                 (msg (and (boundp 'rmail-current-message)
11329                           (symbol-value 'rmail-current-message))))
11330             ;; If MSG is non-nil, buffer is in RMAIL mode.
11331             (if msg
11332                 (progn (widen)
11333                        (narrow-to-region (point-max) (point-max))))
11334             (insert-buffer-substring tmpbuf)
11335             (if msg
11336                 (progn
11337                   (goto-char (point-min))
11338                   (widen)
11339                   (search-backward "\^_")
11340                   (narrow-to-region (point) (point-max))
11341                   (goto-char (1+ (point-min)))
11342                   (rmail-count-new-messages t)
11343                   (rmail-show-message msg)))))))
11344     (kill-buffer tmpbuf)))
11345
11346 (defun gnus-output-to-file (file-name)
11347   "Append the current article to a file named FILE-NAME."
11348   (setq file-name (expand-file-name file-name))
11349   (let ((artbuf (current-buffer))
11350         (tmpbuf (get-buffer-create " *Gnus-output*")))
11351     (save-excursion
11352       (set-buffer tmpbuf)
11353       (buffer-disable-undo (current-buffer))
11354       (erase-buffer)
11355       (insert-buffer-substring artbuf)
11356       ;; Append newline at end of the buffer as separator, and then
11357       ;; save it to file.
11358       (goto-char (point-max))
11359       (insert "\n")
11360       (append-to-file (point-min) (point-max) file-name))
11361     (kill-buffer tmpbuf)))
11362
11363 (defun gnus-convert-article-to-rmail ()
11364   "Convert article in current buffer to Rmail message format."
11365   (let ((buffer-read-only nil))
11366     ;; Convert article directly into Babyl format.
11367     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11368     (goto-char (point-min))
11369     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11370     (while (search-forward "\n\^_" nil t) ;single char
11371       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
11372     (goto-char (point-max))
11373     (insert "\^_")))
11374
11375 (defun gnus-narrow-to-page (&optional arg)
11376   "Make text outside current page invisible except for page delimiter.
11377 A numeric arg specifies to move forward or backward by that many pages,
11378 thus showing a page other than the one point was originally in."
11379   (interactive "P")
11380   (setq arg (if arg (prefix-numeric-value arg) 0))
11381   (save-excursion
11382     (forward-page -1)                   ;Beginning of current page.
11383     (widen)
11384     (if (> arg 0)
11385         (forward-page arg)
11386       (if (< arg 0)
11387           (forward-page (1- arg))))
11388     ;; Find the end of the page.
11389     (forward-page)
11390     ;; If we stopped due to end of buffer, stay there.
11391     ;; If we stopped after a page delimiter, put end of restriction
11392     ;; at the beginning of that line.
11393     ;; These are commented out.
11394     ;;    (if (save-excursion (beginning-of-line)
11395     ;;                  (looking-at page-delimiter))
11396     ;;  (beginning-of-line))
11397     (narrow-to-region (point)
11398                       (progn
11399                         ;; Find the top of the page.
11400                         (forward-page -1)
11401                         ;; If we found beginning of buffer, stay there.
11402                         ;; If extra text follows page delimiter on same line,
11403                         ;; include it.
11404                         ;; Otherwise, show text starting with following line.
11405                         (if (and (eolp) (not (bobp)))
11406                             (forward-line 1))
11407                         (point)))))
11408
11409 (defun gnus-gmt-to-local ()
11410   "Rewrite Date header described in GMT to local in current buffer.
11411 Intended to be used with gnus-article-prepare-hook."
11412   (save-excursion
11413     (save-restriction
11414       (widen)
11415       (goto-char (point-min))
11416       (narrow-to-region (point-min)
11417                         (progn (search-forward "\n\n" nil 'move) (point)))
11418       (goto-char (point-min))
11419       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11420           (let ((buffer-read-only nil)
11421                 (date (buffer-substring-no-properties
11422                        (match-beginning 1) (match-end 1))))
11423             (delete-region (match-beginning 1) (match-end 1))
11424             (insert
11425              (timezone-make-date-arpa-standard 
11426               date nil (current-time-zone))))))))
11427
11428
11429 ;; Article mode commands
11430
11431 (defun gnus-article-next-page (&optional lines)
11432   "Show next page of current article.
11433 If end of article, return non-nil. Otherwise return nil.
11434 Argument LINES specifies lines to be scrolled up."
11435   (interactive "P")
11436   (move-to-window-line -1)
11437   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11438   (if (save-excursion
11439         (end-of-line)
11440         (and (pos-visible-in-window-p)  ;Not continuation line.
11441              (eobp)))
11442       ;; Nothing in this page.
11443       (if (or (not gnus-break-pages)
11444               (save-excursion
11445                 (save-restriction
11446                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11447           t                             ;Nothing more.
11448         (gnus-narrow-to-page 1)         ;Go to next page.
11449         nil)
11450     ;; More in this page.
11451     (condition-case ()
11452         (scroll-up lines)
11453       (end-of-buffer
11454        ;; Long lines may cause an end-of-buffer error.
11455        (goto-char (point-max))))
11456     nil))
11457
11458 (defun gnus-article-prev-page (&optional lines)
11459   "Show previous page of current article.
11460 Argument LINES specifies lines to be scrolled down."
11461   (interactive "P")
11462   (move-to-window-line 0)
11463   (if (and gnus-break-pages
11464            (bobp)
11465            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11466       (progn
11467         (gnus-narrow-to-page -1)        ;Go to previous page.
11468         (goto-char (point-max))
11469         (recenter -1))
11470     (scroll-down lines)))
11471
11472 (defun gnus-article-refer-article ()
11473   "Read article specified by message-id around point."
11474   (interactive)
11475   (search-forward ">" nil t)            ;Move point to end of "<....>".
11476   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11477       (let ((message-id
11478              (buffer-substring (match-beginning 1) (match-end 1))))
11479         (set-buffer gnus-summary-buffer)
11480         (gnus-summary-refer-article message-id))
11481     (error "No references around point")))
11482
11483 (defun gnus-article-show-summary ()
11484   "Reconfigure windows to show summary buffer."
11485   (interactive)
11486   (gnus-configure-windows 'article)
11487   (gnus-summary-goto-subject gnus-current-article))
11488
11489 (defun gnus-article-describe-briefly ()
11490   "Describe article mode commands briefly."
11491   (interactive)
11492   (gnus-message 6
11493                 (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")))
11494
11495 (defun gnus-article-summary-command ()
11496   "Execute the last keystroke in the summary buffer."
11497   (interactive)
11498   (let ((obuf (current-buffer))
11499         (owin (current-window-configuration))
11500         func)
11501     (switch-to-buffer gnus-summary-buffer 'norecord)
11502     (setq func (lookup-key (current-local-map) (this-command-keys)))
11503     (call-interactively func)
11504     (set-buffer obuf)
11505     (set-window-configuration owin)
11506     (set-window-point (get-buffer-window (current-buffer)) (point))))
11507
11508 (defun gnus-article-summary-command-nosave ()
11509   "Execute the last keystroke in the summary buffer."
11510   (interactive)
11511   (let (func)
11512     (pop-to-buffer gnus-summary-buffer 'norecord)
11513     (setq func (lookup-key (current-local-map) (this-command-keys)))
11514     (call-interactively func)))
11515
11516 \f
11517 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11518
11519 ;;;###autoload
11520 (defalias 'gnus-batch-kill 'gnus-batch-score)
11521 ;;;###autoload
11522 (defun gnus-batch-score ()
11523   "Run batched scoring.
11524 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11525 Newsgroups is a list of strings in Bnews format.  If you want to score
11526 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11527 score the alt hierarchy, you'd say \"!alt.all\"."
11528   (interactive)
11529   (let* ((yes-and-no
11530           (gnus-newsrc-parse-options
11531            (apply (function concat)
11532                   (mapcar (lambda (g) (concat g " "))
11533                           command-line-args-left))))
11534          (gnus-expert-user t)
11535          (nnmail-spool-file nil)
11536          (gnus-use-dribble-file nil)
11537          (yes (car yes-and-no))
11538          (no (cdr yes-and-no))
11539          group newsrc entry
11540          ;; Disable verbose message.
11541          gnus-novice-user gnus-large-newsgroup)
11542     ;; Eat all arguments.
11543     (setq command-line-args-left nil)
11544     ;; Start Gnus.
11545     (gnus)
11546     ;; Apply kills to specified newsgroups in command line arguments.
11547     (setq newsrc (cdr gnus-newsrc-alist))
11548     (while newsrc
11549       (setq group (car (car newsrc)))
11550       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11551       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11552                (and (car entry)
11553                     (or (eq (car entry) t)
11554                         (not (zerop (car entry)))))
11555                (if yes (string-match yes group) t)
11556                (or (null no) (not (string-match no group))))
11557           (progn
11558             (gnus-summary-read-group group nil t)
11559             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11560                  (gnus-summary-exit))))
11561       (setq newsrc (cdr newsrc)))
11562     ;; Exit Emacs.
11563     (switch-to-buffer gnus-group-buffer)
11564     (gnus-group-save-newsrc)))
11565
11566 (defun gnus-apply-kill-file ()
11567   "Apply a kill file to the current newsgroup.
11568 Returns the number of articles marked as read."
11569   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11570           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11571       (gnus-apply-kill-file-internal)
11572     0))
11573
11574 (defun gnus-kill-save-kill-buffer ()
11575   (save-excursion
11576     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11577       (if (get-file-buffer file)
11578           (progn
11579             (set-buffer (get-file-buffer file))
11580             (and (buffer-modified-p) (save-buffer))
11581             (kill-buffer (current-buffer)))))))
11582
11583 (defvar gnus-kill-file-name "KILL"
11584   "Suffix of the kill files.")
11585
11586 (defun gnus-newsgroup-kill-file (newsgroup)
11587   "Return the name of a kill file name for NEWSGROUP.
11588 If NEWSGROUP is nil, return the global kill file name instead."
11589   (cond ((or (null newsgroup)
11590              (string-equal newsgroup ""))
11591          ;; The global KILL file is placed at top of the directory.
11592          (expand-file-name gnus-kill-file-name
11593                            (or gnus-kill-files-directory "~/News")))
11594         ((gnus-use-long-file-name 'not-kill)
11595          ;; Append ".KILL" to newsgroup name.
11596          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11597                                    "." gnus-kill-file-name)
11598                            (or gnus-kill-files-directory "~/News")))
11599         (t
11600          ;; Place "KILL" under the hierarchical directory.
11601          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11602                                    "/" gnus-kill-file-name)
11603                            (or gnus-kill-files-directory "~/News")))))
11604
11605 \f
11606 ;;;
11607 ;;; Dribble file
11608 ;;;
11609
11610 (defvar gnus-dribble-ignore nil)
11611 (defvar gnus-dribble-eval-file nil)
11612
11613 (defun gnus-dribble-file-name ()
11614   (concat gnus-current-startup-file "-dribble"))
11615
11616 (defun gnus-dribble-enter (string)
11617   (if (and (not gnus-dribble-ignore)
11618            gnus-dribble-buffer
11619            (buffer-name gnus-dribble-buffer))
11620       (let ((obuf (current-buffer)))
11621         (set-buffer gnus-dribble-buffer)
11622         (insert string "\n")
11623         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11624         (set-buffer obuf))))
11625
11626 (defun gnus-dribble-read-file ()
11627   (let ((dribble-file (gnus-dribble-file-name)))
11628     (save-excursion 
11629       (set-buffer (setq gnus-dribble-buffer 
11630                         (get-buffer-create 
11631                          (file-name-nondirectory dribble-file))))
11632       (gnus-add-current-to-buffer-list)
11633       (erase-buffer)
11634       (set-visited-file-name dribble-file)
11635       (buffer-disable-undo (current-buffer))
11636       (bury-buffer (current-buffer))
11637       (set-buffer-modified-p nil)
11638       (let ((auto (make-auto-save-file-name))
11639             (gnus-dribble-ignore t))
11640         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11641             (progn
11642               (if (file-newer-than-file-p auto dribble-file)
11643                   (setq dribble-file auto))
11644               (insert-file-contents dribble-file)
11645               (if (not (zerop (buffer-size)))
11646                   (set-buffer-modified-p t))
11647               (if (gnus-y-or-n-p 
11648                    "Auto-save file exists. Do you want to read it? ")
11649                   (setq gnus-dribble-eval-file t))))))))
11650
11651 (defun gnus-dribble-eval-file ()
11652   (if (not gnus-dribble-eval-file)
11653       ()
11654     (setq gnus-dribble-eval-file nil)
11655     (save-excursion
11656       (let ((gnus-dribble-ignore t))
11657         (set-buffer gnus-dribble-buffer)
11658         (eval-buffer (current-buffer))))))
11659
11660 (defun gnus-dribble-delete-file ()
11661   (if (file-exists-p (gnus-dribble-file-name))
11662       (delete-file (gnus-dribble-file-name)))
11663   (if gnus-dribble-buffer
11664       (save-excursion
11665         (set-buffer gnus-dribble-buffer)
11666         (let ((auto (make-auto-save-file-name)))
11667           (if (file-exists-p auto)
11668               (delete-file auto))
11669           (erase-buffer)
11670           (set-buffer-modified-p nil)))))
11671
11672 (defun gnus-dribble-save ()
11673   (if (and gnus-dribble-buffer
11674            (buffer-name gnus-dribble-buffer))
11675       (save-excursion
11676         (set-buffer gnus-dribble-buffer)
11677         (save-buffer))))
11678
11679 (defun gnus-dribble-clear ()
11680   (save-excursion
11681     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11682         (progn
11683           (set-buffer gnus-dribble-buffer)
11684           (erase-buffer)
11685           (set-buffer-modified-p nil)
11686           (setq buffer-saved-size (buffer-size))))))
11687
11688 ;;;
11689 ;;; Server Communication
11690 ;;;
11691
11692 (defun gnus-start-news-server (&optional confirm)
11693   "Open a method for getting news.
11694 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11695   (let (how)
11696     (if gnus-current-select-method
11697         ;; Stream is already opened.
11698         nil
11699       ;; Open NNTP server.
11700       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11701       (if confirm
11702           (progn
11703             ;; Read server name with completion.
11704             (setq gnus-nntp-server
11705                   (completing-read "NNTP server: "
11706                                    (mapcar (lambda (server) (list server))
11707                                            (cons (list gnus-nntp-server)
11708                                                  gnus-secondary-servers))
11709                                    nil nil gnus-nntp-server))))
11710
11711       (if (and gnus-nntp-server 
11712                (stringp gnus-nntp-server)
11713                (not (string= gnus-nntp-server "")))
11714           (setq gnus-select-method
11715                 (cond ((or (string= gnus-nntp-server "")
11716                            (string= gnus-nntp-server "::"))
11717                        (list 'nnspool (system-name)))
11718                       ((string-match "^:" gnus-nntp-server)
11719                        (list 'nnmh gnus-nntp-server 
11720                              (list 'nnmh-directory 
11721                                    (file-name-as-directory
11722                                     (expand-file-name
11723                                      (concat "~/" (substring
11724                                                    gnus-nntp-server 1)))))
11725                              (list 'nnmh-get-new-mail nil)))
11726                       (t
11727                        (list 'nntp gnus-nntp-server)))))
11728
11729       (setq how (car gnus-select-method))
11730       (cond ((eq how 'nnspool)
11731              (require 'nnspool)
11732              (gnus-message 5 "Looking up local news spool..."))
11733             ((eq how 'nnmh)
11734              (require 'nnmh)
11735              (gnus-message 5 "Looking up mh spool..."))
11736             (t
11737              (require 'nntp)))
11738       (setq gnus-current-select-method gnus-select-method)
11739       (run-hooks 'gnus-open-server-hook)
11740       (or 
11741        ;; gnus-open-server-hook might have opened it
11742        (gnus-server-opened gnus-select-method)  
11743        (gnus-open-server gnus-select-method)
11744        (gnus-y-or-n-p
11745         (format
11746          "%s open error: '%s'. Continue? "
11747          (nth 1 gnus-select-method)
11748          (gnus-status-message gnus-select-method)))
11749        (progn
11750          (gnus-message 1 "Couldn't open server on %s" 
11751                        (nth 1 gnus-select-method))
11752          (ding)
11753          nil)))))
11754
11755 (defun gnus-check-server (&optional method)
11756   "If the news server is down, start it up again."
11757   (let ((method (if method method gnus-select-method)))
11758     (and (stringp method)
11759          (setq method (gnus-server-to-method method)))
11760     (if (gnus-server-opened method)
11761         ;; Stream is already opened.
11762         t
11763       ;; Open server.
11764       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11765       (run-hooks 'gnus-open-server-hook)
11766       (prog1
11767           (gnus-open-server method)
11768         (message "")))))
11769
11770 (defun gnus-nntp-message (&optional message)
11771   "Check the status of the NNTP server.
11772 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11773 is returned insted of the status string."
11774   (let ((status (gnus-status-message (gnus-find-method-for-group 
11775                                       gnus-newsgroup-name)))
11776         (message (or message "")))
11777     (if (and (stringp status) (> (length status) 0))
11778         status message)))
11779
11780 (defun gnus-get-function (method function)
11781   (and (stringp method)
11782        (setq method (gnus-server-to-method method)))
11783   (let ((func (intern (format "%s-%s" (car method) function))))
11784     (if (not (fboundp func)) 
11785         (progn
11786           (require (car method))
11787           (if (not (fboundp func)) 
11788               (error "No such function: %s" func))))
11789     func))
11790
11791 ;;; Interface functions to the backends.
11792
11793 (defun gnus-open-server (method)
11794   (funcall (gnus-get-function method 'open-server)
11795            (nth 1 method) (nthcdr 2 method)))
11796
11797 (defun gnus-close-server (method)
11798   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11799
11800 (defun gnus-request-list (method)
11801   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11802
11803 (defun gnus-request-list-newsgroups (method)
11804   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11805
11806 (defun gnus-request-newgroups (date method)
11807   (funcall (gnus-get-function method 'request-newgroups) 
11808            date (nth 1 method)))
11809
11810 (defun gnus-server-opened (method)
11811   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11812
11813 (defun gnus-status-message (method)
11814   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11815                   method)))
11816     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11817
11818 (defun gnus-request-group (group &optional dont-check)
11819   (let ((method (gnus-find-method-for-group group)))
11820     (funcall (gnus-get-function method 'request-group) 
11821              (gnus-group-real-name group) (nth 1 method) dont-check)))
11822
11823 (defun gnus-request-asynchronous (group &optional articles)
11824   (let ((method (gnus-find-method-for-group group)))
11825     (funcall (gnus-get-function method 'request-asynchronous) 
11826              (gnus-group-real-name group) (nth 1 method) articles)))
11827
11828 (defun gnus-list-active-group (group)
11829   (let ((method (gnus-find-method-for-group group))
11830         (func 'list-active-group))
11831     (and (gnus-check-backend-function func group)
11832          (funcall (gnus-get-function method func) 
11833                   (gnus-group-real-name group) (nth 1 method)))))
11834
11835 (defun gnus-request-group-description (group)
11836   (let ((method (gnus-find-method-for-group group))
11837         (func 'request-group-description))
11838     (and (gnus-check-backend-function func group)
11839          (funcall (gnus-get-function method func) 
11840                   (gnus-group-real-name group) (nth 1 method)))))
11841
11842 (defun gnus-close-group (group)
11843   (let ((method (gnus-find-method-for-group group)))
11844     (funcall (gnus-get-function method 'close-group) 
11845              (gnus-group-real-name group) (nth 1 method))))
11846
11847 (defun gnus-retrieve-headers (articles group)
11848   (let ((method (gnus-find-method-for-group group)))
11849     (if (and gnus-use-cache (numberp (car articles)))
11850         (gnus-cache-retrieve-headers articles group)
11851       (funcall (gnus-get-function method 'retrieve-headers) 
11852                articles (gnus-group-real-name group) (nth 1 method)))))
11853
11854 (defun gnus-retrieve-groups (groups method)
11855   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11856
11857 (defun gnus-request-article (article group &optional buffer)
11858   (let ((method (gnus-find-method-for-group group)))
11859     (funcall (gnus-get-function method 'request-article) 
11860              article (gnus-group-real-name group) (nth 1 method) buffer)))
11861
11862 (defun gnus-request-head (article group)
11863   (let ((method (gnus-find-method-for-group group)))
11864     (funcall (gnus-get-function method 'request-head) 
11865              article (gnus-group-real-name group) (nth 1 method))))
11866
11867 (defun gnus-request-body (article group)
11868   (let ((method (gnus-find-method-for-group group)))
11869     (funcall (gnus-get-function method 'request-body) 
11870              article (gnus-group-real-name group) (nth 1 method))))
11871
11872 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11873 (defun gnus-request-post-buffer (post group subject header artbuf
11874                                       info follow-to respect-poster)
11875   (let* ((info (or info (and group (nth 2 (gnus-gethash 
11876                                            group gnus-newsrc-hashtb)))))
11877          (method
11878           (if (and gnus-post-method
11879                    ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11880                    (memq 'post (assoc
11881                                 (format "%s" (car (gnus-find-method-for-group
11882                                                    gnus-newsgroup-name)))
11883                                 gnus-valid-select-methods)))
11884               gnus-post-method
11885             (gnus-find-method-for-group gnus-newsgroup-name))))
11886     (or (gnus-check-server method)
11887         (error "Can't open server %s:%s" (car method) (nth 1 method)))
11888     (let ((mail-self-blind nil)
11889           (mail-archive-file-name nil))
11890       (funcall (gnus-get-function method 'request-post-buffer) 
11891                post group subject header artbuf info follow-to
11892                respect-poster))))
11893
11894 (defun gnus-request-post (method &optional force)
11895   (and (stringp method)
11896        (setq method (gnus-server-to-method method)))
11897   (and (not force) gnus-post-method
11898        (memq 'post (assoc (format "%s" (car method))
11899                           gnus-valid-select-methods))
11900        (setq method gnus-post-method))
11901   (funcall (gnus-get-function method 'request-post) 
11902            (nth 1 method)))
11903
11904 (defun gnus-request-expire-articles (articles group &optional force)
11905   (let ((method (gnus-find-method-for-group group)))
11906     (funcall (gnus-get-function method 'request-expire-articles) 
11907              articles (gnus-group-real-name group) (nth 1 method)
11908              force)))
11909
11910 (defun gnus-request-move-article 
11911   (article group server accept-function &optional last)
11912   (let ((method (gnus-find-method-for-group group)))
11913     (funcall (gnus-get-function method 'request-move-article) 
11914              article (gnus-group-real-name group) 
11915              (nth 1 method) accept-function last)))
11916
11917 (defun gnus-request-accept-article (group &optional last)
11918   (let ((func (if (symbolp group) group
11919                 (car (gnus-find-method-for-group group)))))
11920     (funcall (intern (format "%s-request-accept-article" func))
11921              (if (stringp group) (gnus-group-real-name group) group)
11922              last)))
11923
11924 (defun gnus-request-replace-article (article group buffer)
11925   (let ((func (car (gnus-find-method-for-group group))))
11926     (funcall (intern (format "%s-request-replace-article" func))
11927              article (gnus-group-real-name group) buffer)))
11928
11929 (defun gnus-request-create-group (group)
11930   (let ((method (gnus-find-method-for-group group)))
11931     (funcall (gnus-get-function method 'request-create-group) 
11932              (gnus-group-real-name group) (nth 1 method))))
11933
11934 (defun gnus-member-of-valid (symbol group)
11935   (memq symbol (assoc
11936                 (format "%s" (car (gnus-find-method-for-group group)))
11937                 gnus-valid-select-methods)))
11938
11939 (defun gnus-secondary-method-p (method)
11940   (let ((methods gnus-secondary-select-methods)
11941         (gmethod (gnus-server-get-method nil method)))
11942     (while (and methods
11943                 (not (equal (gnus-server-get-method nil (car methods)) 
11944                             gmethod)))
11945       (setq methods (cdr methods)))
11946     methods))
11947
11948 (defun gnus-find-method-for-group (group &optional info)
11949   (or gnus-override-method
11950       (and (not group)
11951            gnus-select-method)
11952       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11953             method)
11954         (if (or (not info)
11955                 (not (setq method (nth 4 info))))
11956             (setq method gnus-select-method)
11957           (setq method
11958                 (cond ((stringp method)
11959                        (gnus-server-to-method method))
11960                       ((stringp (car method))
11961                        (gnus-server-extend-method group method))
11962                       (t
11963                        method))))
11964         (gnus-server-add-address method))))
11965
11966 (defun gnus-check-backend-function (func group)
11967   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11968                   group)))
11969     (fboundp (intern (format "%s-%s" method func)))))
11970
11971 (defun gnus-methods-using (method)
11972   (let ((valids gnus-valid-select-methods)
11973         outs)
11974     (while valids
11975       (if (memq method (car valids)) 
11976           (setq outs (cons (car valids) outs)))
11977       (setq valids (cdr valids)))
11978     outs))
11979
11980 ;;; 
11981 ;;; Active & Newsrc File Handling
11982 ;;;
11983
11984 ;; Newsrc related functions.
11985 ;; Gnus internal format of gnus-newsrc-alist:
11986 ;; (("alt.general" 3 (1 . 1))
11987 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11988 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11989 ;; The first item is the group name; the second is the subscription
11990 ;; level; the third is either a range of a list of ranges of read
11991 ;; articles, the optional fourth element is a list of marked articles,
11992 ;; the optional fifth element is the select method.
11993 ;;
11994 ;; Gnus internal format of gnus-newsrc-hashtb:
11995 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11996 ;; This is the entry for "alt.misc". The first element is the number
11997 ;; of unread articles in "alt.misc". The cdr of this entry is the
11998 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11999 ;; trivial to remove or add new elements into gnus-newsrc-alist
12000 ;; without scanning the entire list. So, to get the actual information
12001 ;; of "alt.misc", you'd say something like 
12002 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
12003 ;;
12004 ;; Gnus internal format of gnus-active-hashtb:
12005 ;; ((1 . 1))
12006 ;;  (5 . 10))
12007 ;;  (67 . 99)) ...)
12008 ;; The only element in each entry in this hash table is a range of
12009 ;; (possibly) available articles. (Articles in this range may have
12010 ;; been expired or canceled.)
12011 ;;
12012 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12013 ;; ("alt.misc" "alt.test" "alt.general" ...)
12014
12015 (defun gnus-setup-news (&optional rawfile level)
12016   "Setup news information.
12017 If RAWFILE is non-nil, the .newsrc file will also be read.
12018 If LEVEL is non-nil, the news will be set up at level LEVEL."
12019   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
12020     ;; Clear some variables to re-initialize news information.
12021     (if init (setq gnus-newsrc-alist nil 
12022                    gnus-active-hashtb nil))
12023
12024     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12025     (if init (gnus-read-newsrc-file rawfile))
12026
12027     ;; If we don't read the complete active file, we fill in the
12028     ;; hashtb here. 
12029     (if (or (null gnus-read-active-file)
12030             (eq gnus-read-active-file 'some))
12031         (gnus-update-active-hashtb-from-killed))
12032
12033     ;; Read the active file and create `gnus-active-hashtb'.
12034     ;; If `gnus-read-active-file' is nil, then we just create an empty
12035     ;; hash table. The partial filling out of the hash table will be
12036     ;; done in `gnus-get-unread-articles'.
12037     (and gnus-read-active-file 
12038          (not level)
12039          (gnus-read-active-file))
12040
12041     (or gnus-active-hashtb
12042         (setq gnus-active-hashtb (make-vector 4095 0)))
12043
12044     ;; Possibly eval the dribble file.
12045     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
12046
12047     (gnus-update-format-specifications)
12048
12049     ;; Find new newsgroups and treat them.
12050     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12051              (gnus-server-opened gnus-select-method))
12052         (gnus-find-new-newsgroups))
12053
12054     ;; Find the number of unread articles in each non-dead group.
12055     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12056       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12057
12058     (if (and init gnus-check-bogus-newsgroups 
12059              gnus-read-active-file (not level)
12060              (gnus-server-opened gnus-select-method))
12061         (gnus-check-bogus-newsgroups))))
12062
12063 (defun gnus-find-new-newsgroups ()
12064   "Search for new newsgroups and add them.
12065 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12066 The `-n' option line from .newsrc is respected."
12067   (interactive)
12068   (or (gnus-check-first-time-used)
12069       (if (or (consp gnus-check-new-newsgroups)
12070               (eq gnus-check-new-newsgroups 'ask-server))
12071           (gnus-ask-server-for-new-groups)
12072         (let ((groups 0)
12073               group new-newsgroups)
12074           (gnus-message 5 "Looking for new newsgroups...")
12075           (or gnus-have-read-active-file (gnus-read-active-file))
12076           (setq gnus-newsrc-last-checked-date (current-time-string))
12077           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12078           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12079           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12080           (mapatoms
12081            (lambda (sym)
12082              (if (or (null (setq group (symbol-name sym)))
12083                      (null (symbol-value sym))
12084                      (gnus-gethash group gnus-killed-hashtb)
12085                      (gnus-gethash group gnus-newsrc-hashtb))
12086                  ()
12087                (let ((do-sub (gnus-matches-options-n group)))
12088                  (cond 
12089                   ((eq do-sub 'subscribe)
12090                    (setq groups (1+ groups))
12091                    (gnus-sethash group group gnus-killed-hashtb)
12092                    (funcall gnus-subscribe-options-newsgroup-method group))
12093                   ((eq do-sub 'ignore)
12094                    nil)
12095                   (t
12096                    (setq groups (1+ groups))
12097                    (gnus-sethash group group gnus-killed-hashtb)
12098                    (if gnus-subscribe-hierarchical-interactive
12099                        (setq new-newsgroups (cons group new-newsgroups))
12100                      (funcall gnus-subscribe-newsgroup-method group)))))))
12101            gnus-active-hashtb)
12102           (if new-newsgroups 
12103               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12104           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12105           (if (> groups 0)
12106               (gnus-message 6 "%d new newsgroup%s arrived." 
12107                             groups (if (> groups 1) "s have" " has"))
12108             (gnus-message 6 "No new newsgroups."))))))
12109
12110 (defun gnus-matches-options-n (group)
12111   ;; Returns `subscribe' if the group is to be uncoditionally
12112   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12113   ;; no match for the group.
12114
12115   ;; First we check the two user variables.
12116   (cond
12117    ((and gnus-options-subscribe
12118          (string-match gnus-options-subscribe group))
12119     'subscribe)
12120    ((and gnus-options-not-subscribe
12121          (string-match gnus-options-not-subscribe group))
12122     'ignore)
12123    ;; Then we go through the list that was retrieved from the .newsrc
12124    ;; file.  This list has elements on the form 
12125    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12126    ;; is in the reverse order of the options line) is returned.
12127    (t
12128     (let ((regs gnus-newsrc-options-n))
12129       (while (and regs
12130                   (not (string-match (car (car regs)) group)))
12131         (setq regs (cdr regs)))
12132       (and regs (cdr (car regs)))))))
12133
12134 (defun gnus-ask-server-for-new-groups ()
12135   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12136          (methods (cons gnus-select-method 
12137                         (append
12138                          (and (consp gnus-check-new-newsgroups)
12139                               gnus-check-new-newsgroups)
12140                          gnus-secondary-select-methods)))
12141          (groups 0)
12142          (new-date (current-time-string))
12143          (hashtb (gnus-make-hashtable 100))
12144          group new-newsgroups got-new method)
12145     ;; Go through both primary and secondary select methods and
12146     ;; request new newsgroups.  
12147     (while methods
12148       (setq method (gnus-server-get-method nil (car methods)))
12149       (and (gnus-check-server method)
12150            (gnus-request-newgroups date method)
12151            (save-excursion
12152              (setq got-new t)
12153              (set-buffer nntp-server-buffer)
12154              ;; Enter all the new groups in a hashtable.
12155              (gnus-active-to-gnus-format method hashtb 'ignore)))
12156       (setq methods (cdr methods)))
12157     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12158     ;; Now all new groups from all select methods are in `hashtb'.
12159     (mapatoms
12160      (lambda (group-sym)
12161        (setq group (symbol-name group-sym))
12162        (if (or (null group)
12163                (null (symbol-value group-sym))
12164                (gnus-gethash group gnus-newsrc-hashtb)
12165                (member group gnus-zombie-list)
12166                (member group gnus-killed-list))
12167            ;; The group is already known.
12168            ()
12169          (and (symbol-value group-sym)
12170               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12171          (let ((do-sub (gnus-matches-options-n group)))
12172            (cond ((eq do-sub 'subscribe)
12173                   (setq groups (1+ groups))
12174                   (gnus-sethash group group gnus-killed-hashtb)
12175                   (funcall 
12176                    gnus-subscribe-options-newsgroup-method group))
12177                  ((eq do-sub 'ignore)
12178                   nil)
12179                  (t
12180                   (setq groups (1+ groups))
12181                   (gnus-sethash group group gnus-killed-hashtb)
12182                   (if gnus-subscribe-hierarchical-interactive
12183                       (setq new-newsgroups (cons group new-newsgroups))
12184                     (funcall gnus-subscribe-newsgroup-method group)))))))
12185      hashtb)
12186     (if new-newsgroups 
12187         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12188     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12189     (if (> groups 0)
12190         (gnus-message 6 "%d new newsgroup%s arrived." 
12191                       groups (if (> groups 1) "s have" " has")))
12192     got-new))
12193
12194 (defun gnus-check-first-time-used ()
12195   (if (or (> (length gnus-newsrc-alist) 1)
12196           (file-exists-p gnus-startup-file)
12197           (file-exists-p (concat gnus-startup-file ".el"))
12198           (file-exists-p (concat gnus-startup-file ".eld")))
12199       nil
12200     (gnus-message 6 "First time user; subscribing you to default groups")
12201     (or gnus-have-read-active-file (gnus-read-active-file))
12202     (setq gnus-newsrc-last-checked-date (current-time-string))
12203     (let ((groups gnus-default-subscribed-newsgroups)
12204           group)
12205       (if (eq groups t)
12206           nil
12207         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12208         (mapatoms
12209          (lambda (sym)
12210            (if (null (setq group (symbol-name sym)))
12211                ()
12212              (let ((do-sub (gnus-matches-options-n group)))
12213                (cond 
12214                 ((eq do-sub 'subscribe)
12215                  (gnus-sethash group group gnus-killed-hashtb)
12216                  (funcall gnus-subscribe-options-newsgroup-method group))
12217                 ((eq do-sub 'ignore)
12218                  nil)
12219                 (t
12220                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12221          gnus-active-hashtb)
12222         (while groups
12223           (if (gnus-gethash (car groups) gnus-active-hashtb)
12224               (gnus-group-change-level 
12225                (car groups) gnus-level-default-subscribed gnus-level-killed))
12226           (setq groups (cdr groups)))
12227         (gnus-group-make-help-group)
12228         (and gnus-novice-user
12229              (gnus-message 7 "`A k' to list killed groups"))))))
12230
12231 (defun gnus-subscribe-group (group previous &optional method)
12232   (gnus-group-change-level 
12233    (if method
12234        (list t group gnus-level-default-subscribed nil nil method)
12235      group) 
12236    gnus-level-default-subscribed gnus-level-killed previous t))
12237
12238 ;; `gnus-group-change-level' is the fundamental function for changing
12239 ;; subscription levels of newsgroups. This might mean just changing
12240 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12241 ;; again, which subscribes/unsubscribes a group, which is equally
12242 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12243 ;; from 8-9 to 1-7 means that you remove the group from the list of
12244 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12245 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12246 ;; which is trivial.
12247 ;; ENTRY can either be a string (newsgroup name) or a list (if
12248 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12249 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12250 ;; entries. 
12251 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12252 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12253 ;; after. 
12254 (defun gnus-group-change-level (entry level &optional oldlevel
12255                                       previous fromkilled)
12256   (let (group info active num)
12257     ;; Glean what info we can from the arguments
12258     (if (consp entry)
12259         (if fromkilled (setq group (nth 1 entry))
12260           (setq group (car (nth 2 entry))))
12261       (setq group entry))
12262     (if (and (stringp entry)
12263              oldlevel 
12264              (< oldlevel gnus-level-zombie))
12265         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12266     (if (and (not oldlevel)
12267              (consp entry))
12268         (setq oldlevel (car (cdr (nth 2 entry)))))
12269     (if (stringp previous)
12270         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12271
12272     (if (and (>= oldlevel gnus-level-zombie)
12273              (gnus-gethash group gnus-newsrc-hashtb))
12274         ;; We are trying to subscribe a group that is already
12275         ;; subscribed. 
12276         ()                              ; Do nothing. 
12277
12278       (gnus-dribble-enter
12279        (format "(gnus-group-change-level %S %S %S %S %S)" 
12280                group level oldlevel (car (nth 2 previous)) fromkilled))
12281     
12282       ;; Then we remove the newgroup from any old structures, if needed.
12283       ;; If the group was killed, we remove it from the killed or zombie
12284       ;; list. If not, and it is in fact going to be killed, we remove
12285       ;; it from the newsrc hash table and assoc.
12286       (cond ((>= oldlevel gnus-level-zombie)
12287              (if (= oldlevel gnus-level-zombie)
12288                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12289                (setq gnus-killed-list (delete group gnus-killed-list))))
12290             (t
12291              (if (and (>= level gnus-level-zombie)
12292                       entry)
12293                  (progn
12294                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12295                    (if (nth 3 entry)
12296                        (setcdr (gnus-gethash (car (nth 3 entry))
12297                                              gnus-newsrc-hashtb)
12298                                (cdr entry)))
12299                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12300
12301       ;; Finally we enter (if needed) the list where it is supposed to
12302       ;; go, and change the subscription level. If it is to be killed,
12303       ;; we enter it into the killed or zombie list.
12304       (cond ((>= level gnus-level-zombie)
12305              ;; Remove from the hash table.
12306              (gnus-sethash group nil gnus-newsrc-hashtb)
12307              (or (gnus-group-foreign-p group)
12308                  ;; We do not enter foreign groups into the list of dead
12309                  ;; groups.  
12310                  (if (= level gnus-level-zombie)
12311                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12312                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12313             (t
12314              ;; If the list is to be entered into the newsrc assoc, and
12315              ;; it was killed, we have to create an entry in the newsrc
12316              ;; hashtb format and fix the pointers in the newsrc assoc.
12317              (if (>= oldlevel gnus-level-zombie)
12318                  (progn
12319                    (if (listp entry)
12320                        (progn
12321                          (setq info (cdr entry))
12322                          (setq num (car entry)))
12323                      (setq active (gnus-gethash group gnus-active-hashtb))
12324                      (setq num 
12325                            (if active (- (1+ (cdr active)) (car active)) t))
12326                      ;; Check whether the group is foreign. If so, the
12327                      ;; foreign select method has to be entered into the
12328                      ;; info. 
12329                      (let ((method (gnus-group-method-name group)))
12330                        (if (eq method gnus-select-method)
12331                            (setq info (list group level nil))
12332                          (setq info (list group level nil nil method)))))
12333                    (or previous 
12334                        (setq previous 
12335                              (let ((p gnus-newsrc-alist))
12336                                (while (cdr (cdr p))
12337                                  (setq p (cdr p)))
12338                                p)))
12339                    (setq entry (cons info (cdr (cdr previous))))
12340                    (if (cdr previous)
12341                        (progn
12342                          (setcdr (cdr previous) entry)
12343                          (gnus-sethash group (cons num (cdr previous)) 
12344                                        gnus-newsrc-hashtb))
12345                      (setcdr previous entry)
12346                      (gnus-sethash group (cons num previous)
12347                                    gnus-newsrc-hashtb))
12348                    (if (cdr entry)
12349                        (setcdr (gnus-gethash (car (car (cdr entry)))
12350                                              gnus-newsrc-hashtb)
12351                                entry)))
12352                ;; It was alive, and it is going to stay alive, so we
12353                ;; just change the level and don't change any pointers or
12354                ;; hash table entries.
12355                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12356
12357 (defun gnus-kill-newsgroup (newsgroup)
12358   "Obsolete function. Kills a newsgroup."
12359   (gnus-group-change-level
12360    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12361
12362 (defun gnus-check-bogus-newsgroups (&optional confirm)
12363   "Remove bogus newsgroups.
12364 If CONFIRM is non-nil, the user has to confirm the deletion of every
12365 newsgroup." 
12366   (let ((newsrc (cdr gnus-newsrc-alist))
12367         bogus group entry)
12368     (gnus-message 5 "Checking bogus newsgroups...")
12369     (or gnus-have-read-active-file (gnus-read-active-file))
12370     ;; Find all bogus newsgroup that are subscribed.
12371     (while newsrc
12372       (setq group (car (car newsrc)))
12373       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12374               (nth 4 (car newsrc))      ; Foreign
12375               (and confirm
12376                    (not (gnus-y-or-n-p
12377                          (format "Remove bogus newsgroup: %s " group)))))
12378           ;; Don't remove.
12379           ()
12380         ;; Found a bogus newsgroup.
12381         (setq bogus (cons group bogus)))
12382       (setq newsrc (cdr newsrc)))
12383     ;; Remove all bogus subscribed groups by first killing them, and
12384     ;; then removing them from the list of killed groups.
12385     (while bogus
12386       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12387            (progn
12388              (gnus-group-change-level entry gnus-level-killed)
12389              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12390       (setq bogus (cdr bogus)))
12391     ;; Then we remove all bogus groups from the list of killed and
12392     ;; zombie groups. They are are removed without confirmation.
12393     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12394           killed)
12395       (while dead-lists
12396         (setq killed (symbol-value (car dead-lists)))
12397         (while killed
12398           (setq group (car killed))
12399           (or (gnus-gethash group gnus-active-hashtb)
12400               ;; The group is bogus.
12401               (set (car dead-lists)
12402                    (delete group (symbol-value (car dead-lists)))))
12403           (setq killed (cdr killed)))
12404         (setq dead-lists (cdr dead-lists))))
12405     (gnus-message 5 "Checking bogus newsgroups...done")))
12406
12407 (defun gnus-check-duplicate-killed-groups ()
12408   "Remove duplicates from the list of killed groups."
12409   (interactive)
12410   (let ((killed gnus-killed-list))
12411     (while killed
12412       (gnus-message 9 "%d" (length killed))
12413       (setcdr killed (delete (car killed) (cdr killed)))
12414       (setq killed (cdr killed)))))
12415
12416 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12417 ;; and compute how many unread articles there are in each group.
12418 (defun gnus-get-unread-articles (&optional level) 
12419   (let* ((newsrc (cdr gnus-newsrc-alist))
12420          (level (or level (1+ gnus-level-subscribed)))
12421          (foreign-level
12422           (min 
12423            (cond ((and gnus-activate-foreign-newsgroups 
12424                        (not (numberp gnus-activate-foreign-newsgroups)))
12425                   (1+ gnus-level-subscribed))
12426                  ((numberp gnus-activate-foreign-newsgroups)
12427                   gnus-activate-foreign-newsgroups)
12428                  (t 0))
12429            level))
12430          info group active virtuals method)
12431     (gnus-message 5 "Checking new news...")
12432
12433     (while newsrc
12434       (setq info (car newsrc)
12435             group (car info)
12436             active (gnus-gethash group gnus-active-hashtb))
12437
12438       ;; Check newsgroups. If the user doesn't want to check them, or
12439       ;; they can't be checked (for instance, if the news server can't
12440       ;; be reached) we just set the number of unread articles in this
12441       ;; newsgroup to t. This means that Gnus thinks that there are
12442       ;; unread articles, but it has no idea how many.
12443       (if (and (setq method (nth 4 info))
12444                (not (gnus-server-equal gnus-select-method
12445                                        (gnus-server-get-method nil method)))
12446                (not (gnus-secondary-method-p method)))
12447           ;; These groups are foreign. Check the level.
12448           (if (<= (nth 1 info) foreign-level)
12449               (if (eq (car (if (stringp method) 
12450                                (gnus-server-to-method method)
12451                              (nth 4 info))) 'nnvirtual)
12452                   ;; We have to activate the virtual groups after all
12453                   ;; the others, so we just pop them on a list for
12454                   ;; now. 
12455                   (setq virtuals (cons info virtuals))
12456                 (and (setq active (gnus-activate-group (car info)))
12457                      ;; Close the groups as we look at them!
12458                      (gnus-close-group group))))
12459
12460         ;; These groups are native or secondary. 
12461         (if (and (not gnus-read-active-file)
12462                  (<= (nth 1 info) level))
12463             (progn
12464               (or gnus-read-active-file (gnus-check-server method))
12465               (setq active (gnus-activate-group (car info))))))
12466       
12467       (if active
12468           (gnus-get-unread-articles-in-group info active)
12469         ;; The group couldn't be reached, so we nix out the number of
12470         ;; unread articles and stuff.
12471         (gnus-sethash group nil gnus-active-hashtb)
12472         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12473
12474       (setq newsrc (cdr newsrc)))
12475
12476     ;; Activate the virtual groups. This has to be done after all the
12477     ;; other groups. 
12478     ;; !!! If one virtual group contains another virtual group, even
12479     ;; doing it this way might cause problems.
12480     (while virtuals
12481       (and (setq active (gnus-activate-group (car (car virtuals))))
12482            (gnus-get-unread-articles-in-group (car virtuals) active))
12483       (setq virtuals (cdr virtuals)))
12484
12485     (gnus-message 5 "Checking new news...done")))
12486
12487 ;; Create a hash table out of the newsrc alist. The `car's of the
12488 ;; alist elements are used as keys.
12489 (defun gnus-make-hashtable-from-newsrc-alist ()
12490   (let ((alist gnus-newsrc-alist)
12491         (ohashtb gnus-newsrc-hashtb)
12492         prev)
12493     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12494     (setq alist 
12495           (setq prev (setq gnus-newsrc-alist 
12496                            (if (equal (car (car gnus-newsrc-alist))
12497                                       "dummy.group")
12498                                gnus-newsrc-alist
12499                              (cons (list "dummy.group" 0 nil) alist)))))
12500     (while alist
12501       (gnus-sethash (car (car alist)) 
12502                     (cons (and ohashtb (car (gnus-gethash 
12503                                              (car (car alist)) ohashtb))) 
12504                           prev) gnus-newsrc-hashtb)
12505       (setq prev alist
12506             alist (cdr alist)))))
12507
12508 (defun gnus-make-hashtable-from-killed ()
12509   "Create a hash table from the killed and zombie lists."
12510   (let ((lists '(gnus-killed-list gnus-zombie-list))
12511         list)
12512     (setq gnus-killed-hashtb 
12513           (gnus-make-hashtable 
12514            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12515     (while lists
12516       (setq list (symbol-value (car lists)))
12517       (setq lists (cdr lists))
12518       (while list
12519         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12520         (setq list (cdr list))))))
12521
12522 (defun gnus-get-unread-articles-in-group (info active)
12523   (let* ((range (nth 2 info))
12524          (num 0)
12525          (marked (nth 3 info)))
12526     ;; If a cache is present, we may have to alter the active info.
12527     (and gnus-use-cache
12528          (gnus-cache-possibly-alter-active (car info) active))
12529     ;; Modify the list of read articles according to what articles 
12530     ;; are available; then tally the unread articles and add the
12531     ;; number to the group hash table entry.
12532     (cond 
12533      ((zerop (cdr active))
12534       (setq num 0))
12535      ((not range)
12536       (setq num (- (1+ (cdr active)) (car active))))
12537      ((not (listp (cdr range)))
12538       ;; Fix a single (num . num) range according to the
12539       ;; active hash table.
12540       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12541       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12542       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12543       ;; Compute number of unread articles.
12544       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12545      (t
12546       ;; The read list is a list of ranges. Fix them according to
12547       ;; the active hash table.
12548       ;; First peel off any elements that are below the lower
12549       ;; active limit. 
12550       (while (and (cdr range) 
12551                   (>= (car active) 
12552                       (or (and (atom (car (cdr range))) (car (cdr range)))
12553                           (car (car (cdr range))))))
12554         (if (numberp (car range))
12555             (setcar range 
12556                     (cons (car range) 
12557                           (or (and (numberp (car (cdr range)))
12558                                    (car (cdr range))) 
12559                               (cdr (car (cdr range))))))
12560           (setcdr (car range) 
12561                   (or (and (numberp (nth 1 range)) (nth 1 range))
12562                       (cdr (car (cdr range))))))
12563         (setcdr range (cdr (cdr range))))
12564       ;; Adjust the first element to be the same as the lower limit. 
12565       (if (and (not (atom (car range))) 
12566                (< (cdr (car range)) (car active)))
12567           (setcdr (car range) (1- (car active))))
12568       ;; Then we want to peel off any elements that are higher
12569       ;; than the upper active limit.  
12570       (let ((srange range))
12571         ;; Go past all legal elements.
12572         (while (and (cdr srange) 
12573                     (<= (or (and (atom (car (cdr srange)))
12574                                  (car (cdr srange)))
12575                             (car (car (cdr srange)))) (cdr active)))
12576           (setq srange (cdr srange)))
12577         (if (cdr srange)
12578             ;; Nuke all remaining illegal elements.
12579             (setcdr srange nil))
12580
12581         ;; Adjust the final element.
12582         (if (and (not (atom (car srange)))
12583                  (> (cdr (car srange)) (cdr active)))
12584             (setcdr (car srange) (cdr active))))
12585       ;; Compute the number of unread articles.
12586       (while range
12587         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12588                                     (cdr (car range))))
12589                             (or (and (atom (car range)) (car range))
12590                                 (car (car range))))))
12591         (setq range (cdr range)))
12592       (setq num (max 0 (- (cdr active) num)))))
12593     (and info
12594          (progn
12595            (and (assq 'tick marked)
12596                 (inline (gnus-remove-illegal-marked-articles
12597                          (assq 'tick marked) (nth 2 info))))
12598            (and (assq 'dormant marked)
12599                 (inline (gnus-remove-illegal-marked-articles
12600                          (assq 'dormant marked) (nth 2 info))))
12601            (setcar
12602             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12603             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12604                                 (length (cdr (assq 'dormant marked)))))))))
12605     num))
12606
12607 (defun gnus-remove-illegal-marked-articles (marked ranges)
12608   (let ((m (cdr marked)))
12609     ;; Make sure that all ticked articles are a subset of the unread
12610     ;; articles. 
12611     (while m
12612       (if (gnus-member-of-range (car m) ranges)
12613           (setcdr marked (cdr m))
12614         (setq marked m))
12615       (setq m (cdr m)))))
12616
12617 (defun gnus-activate-group (group)
12618   ;; Check whether a group has been activated or not.
12619   (let ((method (gnus-find-method-for-group group))
12620         active)
12621     (and (gnus-check-server method)
12622          ;; We escape all bugs and quit here to make it possible to
12623          ;; continue if a group is so out-there that it reports bugs
12624          ;; and stuff.
12625          (condition-case ()
12626              (gnus-request-group group)
12627            (error nil)
12628            (quit nil))
12629          (save-excursion
12630            (set-buffer nntp-server-buffer)
12631            (goto-char (point-min))
12632            ;; Parse the result we got from `gnus-request-group'.
12633            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12634                 (progn
12635                   (goto-char (match-beginning 1))
12636                   (gnus-sethash 
12637                    group (setq active (cons (read (current-buffer))
12638                                             (read (current-buffer))))
12639                    gnus-active-hashtb))
12640                 ;; Return the new active info.
12641                 active)))))
12642
12643 (defun gnus-update-read-articles 
12644   (group unread unselected ticked &optional domarks replied expirable killed
12645          dormant bookmark score)
12646   "Update the list of read and ticked articles in GROUP using the
12647 UNREAD and TICKED lists.
12648 Note: UNSELECTED has to be sorted over `<'.
12649 Returns whether the updating was successful."
12650   (let* ((active (or gnus-newsgroup-active 
12651                      (gnus-gethash group gnus-active-hashtb)))
12652          (entry (gnus-gethash group gnus-newsrc-hashtb))
12653          (info (nth 2 entry))
12654          (marked (nth 3 info))
12655          (prev 1)
12656          (unread (sort (copy-sequence unread) (function <)))
12657          read)
12658     (if (or (not info) (not active))
12659         ;; There is no info on this group if it was, in fact,
12660         ;; killed. Gnus stores no information on killed groups, so
12661         ;; there's nothing to be done. 
12662         ;; One could store the information somewhere temporarily,
12663         ;; perhaps... Hmmm... 
12664         ()
12665       ;; Remove any negative articles numbers.
12666       (while (and unread (< (car unread) 0))
12667         (setq unread (cdr unread)))
12668       ;; Remove any expired article numbers
12669       (while (and unread (< (car unread) (car active)))
12670         (setq unread (cdr unread)))
12671       (while (and ticked (< (car ticked) (car active)))
12672         (setq ticked (cdr ticked)))
12673       (while (and dormant (< (car dormant) (car active)))
12674         (setq dormant (cdr dormant)))
12675       (setq unread (sort (append unselected unread) '<))
12676       ;; Compute the ranges of read articles by looking at the list of
12677       ;; unread articles.  
12678       (while unread
12679         (if (/= (car unread) prev)
12680             (setq read (cons (if (= prev (1- (car unread))) prev
12681                                (cons prev (1- (car unread)))) read)))
12682         (setq prev (1+ (car unread)))
12683         (setq unread (cdr unread)))
12684       (if (<= prev (cdr active))
12685           (setq read (cons (cons prev (cdr active)) read)))
12686       ;; Enter this list into the group info.
12687       (setcar (cdr (cdr info)) 
12688               (if (> (length read) 1) (nreverse read) read))
12689       ;; Enter the list of ticked articles.
12690       (gnus-set-marked-articles 
12691        info ticked
12692        (if domarks replied (cdr (assq 'reply marked)))
12693        (if domarks expirable (cdr (assq 'expire marked)))
12694        (if domarks killed (cdr (assq 'killed marked)))
12695        (if domarks dormant (cdr (assq 'dormant marked)))
12696        (if domarks bookmark (cdr (assq 'bookmark marked)))
12697        (if domarks score (cdr (assq 'score marked))))
12698       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12699       (gnus-get-unread-articles-in-group 
12700        info (gnus-gethash group gnus-active-hashtb))
12701       t)))
12702
12703 (defun gnus-make-articles-unread (group articles)
12704   "Mark ARTICLES in GROUP as unread."
12705   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12706                           (gnus-gethash (gnus-group-real-name group)
12707                                         gnus-newsrc-hashtb))))
12708          (ranges (nth 2 info))
12709          news)
12710     (while articles
12711       (and (gnus-member-of-range (car articles) ranges)
12712            (setq news (cons (car articles) news)))
12713       (setq articles (cdr articles)))
12714     (if (not news)
12715         ()
12716       (setcar (nthcdr 2 info)
12717               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12718       (gnus-group-update-group group t))))
12719
12720 ;; Enter all dead groups into the hashtb.
12721 (defun gnus-update-active-hashtb-from-killed ()
12722   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12723         (lists (list gnus-killed-list gnus-zombie-list))
12724         killed)
12725     (while lists
12726       (setq killed (car lists))
12727       (while killed
12728         (gnus-sethash (car killed) nil hashtb)
12729         (setq killed (cdr killed)))
12730       (setq lists (cdr lists)))))
12731
12732 ;; Get the active file(s) from the backend(s).
12733 (defun gnus-read-active-file ()
12734   (gnus-group-set-mode-line)
12735   (let ((methods (if (gnus-check-server gnus-select-method)
12736                      ;; The native server is available.
12737                      (cons gnus-select-method gnus-secondary-select-methods)
12738                    ;; The native server is down, so we just do the
12739                    ;; secondary ones.   
12740                    gnus-secondary-select-methods))
12741         list-type)
12742     (setq gnus-have-read-active-file nil)
12743     (save-excursion
12744       (set-buffer nntp-server-buffer)
12745       (while methods
12746         (let* ((method (gnus-server-get-method nil (car methods)))
12747                (where (nth 1 method))
12748                (mesg (format "Reading active file%s via %s..."
12749                              (if (and where (not (zerop (length where))))
12750                                  (concat " from " where) "")
12751                              (car method))))
12752           (gnus-message 5 mesg)
12753           (if (not (gnus-check-server method))
12754               ()
12755             (cond 
12756              ((and (eq gnus-read-active-file 'some)
12757                    (gnus-check-backend-function 'retrieve-groups (car method)))
12758               (let ((newsrc (cdr gnus-newsrc-alist))
12759                     (gmethod (gnus-server-get-method nil method))
12760                     groups)
12761                 (while newsrc
12762                   (and (gnus-server-equal 
12763                         (gnus-find-method-for-group 
12764                          (car (car newsrc)) (car newsrc))
12765                         gmethod)
12766                        (setq groups (cons (gnus-group-real-name 
12767                                            (car (car newsrc))) groups)))
12768                   (setq newsrc (cdr newsrc)))
12769                 (gnus-check-server method)
12770                 (setq list-type (gnus-retrieve-groups groups method))
12771                 (cond 
12772                  ((not list-type)
12773                   (gnus-message 
12774                    1 "Cannot read partial active file from %s server." 
12775                    (car method))
12776                   (ding)
12777                   (sit-for 2))
12778                  ((eq list-type 'active)
12779                   (gnus-active-to-gnus-format method gnus-active-hashtb))
12780                  (t
12781                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12782              (t
12783               (if (not (gnus-request-list method))
12784                   (progn
12785                     (gnus-message 1 "Cannot read active file from %s server." 
12786                                   (car method))
12787                     (ding))
12788                 (gnus-active-to-gnus-format method)
12789                 ;; We mark this active file as read.
12790                 (setq gnus-have-read-active-file
12791                       (cons method gnus-have-read-active-file))
12792                 (gnus-message 5 "%sdone" mesg))))))
12793         (setq methods (cdr methods))))))
12794
12795 ;; Read an active file and place the results in `gnus-active-hashtb'.
12796 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12797   (let ((cur (current-buffer))
12798         (hashtb (or hashtb 
12799                     (if (and gnus-active-hashtb 
12800                              (not (equal method gnus-select-method)))
12801                         gnus-active-hashtb
12802                       (setq gnus-active-hashtb
12803                             (if (equal method gnus-select-method)
12804                                 (gnus-make-hashtable 
12805                                  (count-lines (point-min) (point-max)))
12806                               (gnus-make-hashtable 4096))))))
12807         (flag-hashtb (gnus-make-hashtable 60)))
12808     ;; Delete unnecessary lines.
12809     (goto-char (point-min))
12810     (while (search-forward "\nto." nil t)
12811       (delete-region (1+ (match-beginning 0)) 
12812                      (progn (forward-line 1) (point))))
12813     (or (string= gnus-ignored-newsgroups "")
12814         (progn
12815           (goto-char (point-min))
12816           (delete-matching-lines gnus-ignored-newsgroups)))
12817     ;; Make the group names readable as a lisp expression even if they
12818     ;; contain special characters.
12819     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12820     (goto-char (point-max))
12821     (while (re-search-backward "[][';?()#]" nil t)
12822       (insert ?\\))
12823     ;; If these are groups from a foreign select method, we insert the
12824     ;; group prefix in front of the group names. 
12825     (and method (not (gnus-server-equal
12826                       (gnus-server-get-method nil method)
12827                       (gnus-server-get-method nil gnus-select-method)))
12828          (let ((prefix (gnus-group-prefixed-name "" method)))
12829            (goto-char (point-min))
12830            (while (and (not (eobp))
12831                        (progn (insert prefix)
12832                               (zerop (forward-line 1)))))))
12833     ;; Store the active file in a hash table.
12834     (goto-char (point-min))
12835     (if (string-match "%[oO]" gnus-group-line-format)
12836         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12837         ;; If we want information on moderated groups, we use this
12838         ;; loop...   
12839         (let* ((mod-hashtb (make-vector 7 0))
12840                (m (intern "m" mod-hashtb))
12841                group max min)
12842           (while (not (eobp))
12843             (condition-case nil
12844                 (progn
12845                   (narrow-to-region (point) (gnus-point-at-eol))
12846                   (setq group (let ((obarray hashtb)) (read cur)))
12847                   (if (and (numberp (setq max (read cur)))
12848                            (numberp (setq min (read cur)))
12849                            (progn 
12850                              (skip-chars-forward " \t")
12851                              (not
12852                               (or (= (following-char) ?=)
12853                                   (= (following-char) ?x)
12854                                   (= (following-char) ?j)))))
12855                       (set group (cons min max))
12856                     (set group nil))
12857                   ;; Enter moderated groups into a list.
12858                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12859                       (setq gnus-moderated-list 
12860                             (cons (symbol-name group) gnus-moderated-list))))
12861               (error 
12862                (and group
12863                     (symbolp group)
12864                     (set group nil))))
12865             (widen)
12866             (forward-line 1)))
12867       ;; And if we do not care about moderation, we use this loop,
12868       ;; which is faster.
12869       (let (group max min)
12870         (while (not (eobp))
12871           (condition-case ()
12872               (progn
12873                 (narrow-to-region (point) (gnus-point-at-eol))
12874                 ;; group gets set to a symbol interned in the hash table
12875                 ;; (what a hack!!) - jwz
12876                 (setq group (let ((obarray hashtb)) (read cur)))
12877                 (if (and (numberp (setq max (read cur)))
12878                          (numberp (setq min (read cur)))
12879                          (progn 
12880                            (skip-chars-forward " \t")
12881                            (not
12882                             (or (= (following-char) ?=)
12883                                 (= (following-char) ?x)
12884                                 (= (following-char) ?j)))))
12885                     (set group (cons min max))
12886                   (set group nil)))
12887             (error 
12888              (progn 
12889                (and group
12890                     (symbolp group)
12891                     (set group nil))
12892                (or ignore-errors
12893                    (gnus-message 3 "Warning - illegal active: %s"
12894                                  (buffer-substring 
12895                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
12896           (widen)
12897           (forward-line 1))))))
12898
12899 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12900   ;; Parse a "groups" active file.
12901   (let ((cur (current-buffer))
12902         (hashtb (or hashtb 
12903                     (if (and method gnus-active-hashtb)
12904                         gnus-active-hashtb
12905                       (setq gnus-active-hashtb
12906                             (gnus-make-hashtable 
12907                              (count-lines (point-min) (point-max)))))))
12908         (prefix (and method 
12909                      (not (gnus-server-equal
12910                            (gnus-server-get-method nil method)
12911                            (gnus-server-get-method nil gnus-select-method)))
12912                      (gnus-group-prefixed-name "" method))))
12913
12914     (goto-char (point-min))
12915     ;; We split this into to separate loops, one with the prefix
12916     ;; and one without to speed the reading up somewhat.
12917     (if prefix
12918         (let (min max opoint group)
12919           (while (not (eobp))
12920             (condition-case ()
12921                 (progn
12922                   (read cur) (read cur)
12923                   (setq min (read cur)
12924                         max (read cur)
12925                         opoint (point))
12926                   (skip-chars-forward " \t")
12927                   (insert prefix)
12928                   (goto-char opoint)
12929                   (set (let ((obarray hashtb)) (read cur)) 
12930                        (cons min max)))
12931               (error (and group (symbolp group) (set group nil))))
12932             (forward-line 1)))
12933       (let (min max group)
12934         (while (not (eobp))
12935           (condition-case ()
12936               (if (= (following-char) ?2)
12937                   (progn
12938                     (read cur) (read cur)
12939                     (setq min (read cur)
12940                           max (read cur))
12941                     (set (setq group (let ((obarray hashtb)) (read cur)))
12942                          (cons min max))))
12943             (error (and group (symbolp group) (set group nil))))
12944           (forward-line 1))))))
12945
12946 (defun gnus-read-newsrc-file (&optional force)
12947   "Read startup file.
12948 If FORCE is non-nil, the .newsrc file is read."
12949   ;; Reset variables that might be defined in the .newsrc.eld file.
12950   (let ((variables gnus-variable-list))
12951     (while variables
12952       (set (car variables) nil)
12953       (setq variables (cdr variables))))
12954   (let* ((newsrc-file gnus-current-startup-file)
12955          (quick-file (concat newsrc-file ".el")))
12956     (save-excursion
12957       ;; We always load the .newsrc.eld file. If always contains
12958       ;; much information that can not be gotten from the .newsrc
12959       ;; file (ticked articles, killed groups, foreign methods, etc.)
12960       (gnus-read-newsrc-el-file quick-file)
12961  
12962       (if (or force
12963               (and (file-newer-than-file-p newsrc-file quick-file)
12964                    (file-newer-than-file-p newsrc-file 
12965                                            (concat quick-file "d")))
12966               (not gnus-newsrc-alist))
12967           ;; We read the .newsrc file. Note that if there if a
12968           ;; .newsrc.eld file exists, it has already been read, and
12969           ;; the `gnus-newsrc-hashtb' has been created. While reading
12970           ;; the .newsrc file, Gnus will only use the information it
12971           ;; can find there for changing the data already read -
12972           ;; ie. reading the .newsrc file will not trash the data
12973           ;; already read (except for read articles).
12974           (save-excursion
12975             (gnus-message 5 "Reading %s..." newsrc-file)
12976             (set-buffer (find-file-noselect newsrc-file))
12977             (buffer-disable-undo (current-buffer))
12978             (gnus-newsrc-to-gnus-format)
12979             (kill-buffer (current-buffer))
12980             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12981
12982 (defun gnus-read-newsrc-el-file (file)
12983   (let ((ding-file (concat file "d")))
12984     ;; We always, always read the .eld file.
12985     (gnus-message 5 "Reading %s..." ding-file)
12986     (let (gnus-newsrc-assoc)
12987       (condition-case nil
12988           (load ding-file t t t)
12989         (error nil))
12990       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12991     (let ((inhibit-quit t))
12992       (gnus-uncompress-newsrc-alist))
12993     (gnus-make-hashtable-from-newsrc-alist)
12994     (if (not (file-newer-than-file-p file ding-file))
12995         ()
12996       ;; Old format quick file
12997       (gnus-message 5 "Reading %s..." file)
12998       ;; The .el file is newer than the .eld file, so we read that one
12999       ;; as well. 
13000       (gnus-read-old-newsrc-el-file file))))
13001
13002 ;; Parse the old-style quick startup file
13003 (defun gnus-read-old-newsrc-el-file (file)
13004   (let (newsrc killed marked group m)
13005     (prog1
13006         (let ((gnus-killed-assoc nil)
13007               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
13008           (prog1
13009               (condition-case nil
13010                   (load file t t t)
13011                 (error nil))
13012             (setq newsrc gnus-newsrc-assoc
13013                   killed gnus-killed-assoc
13014                   marked gnus-marked-assoc)))
13015       (setq gnus-newsrc-alist nil)
13016       (while newsrc
13017         (setq group (car newsrc))
13018         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13019           (if info
13020               (progn
13021                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13022                 (setcar (cdr info)
13023                         (if (nth 1 group) gnus-level-default-subscribed 
13024                           gnus-level-default-unsubscribed))
13025                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
13026             (setq gnus-newsrc-alist
13027                   (cons 
13028                    (setq info
13029                          (list (car group)
13030                                (if (nth 1 group) gnus-level-default-subscribed
13031                                  gnus-level-default-unsubscribed) 
13032                                (cdr (cdr group))))
13033                    gnus-newsrc-alist)))
13034           (if (setq m (assoc (car group) marked))
13035               (setcdr (cdr (cdr info))
13036                       (cons (list (cons 'tick (cdr m))) nil))))
13037         (setq newsrc (cdr newsrc)))
13038       (setq newsrc killed)
13039       (while newsrc
13040         (setcar newsrc (car (car newsrc)))
13041         (setq newsrc (cdr newsrc)))
13042       (setq gnus-killed-list killed))
13043     ;; The .el file version of this variable does not begin with
13044     ;; "options", while the .eld version does, so we just add it if it
13045     ;; isn't there.
13046     (and
13047      gnus-newsrc-options 
13048      (progn
13049        (and (not (string-match "^ *options" gnus-newsrc-options))
13050             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
13051        (and (not (string-match "\n$" gnus-newsrc-options))
13052             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
13053        ;; Finally, if we read some options lines, we parse them.
13054        (or (string= gnus-newsrc-options "")
13055            (gnus-newsrc-parse-options gnus-newsrc-options))))
13056
13057     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
13058     (gnus-make-hashtable-from-newsrc-alist)))
13059       
13060 (defun gnus-make-newsrc-file (file)
13061   "Make server dependent file name by catenating FILE and server host name."
13062   (let* ((file (expand-file-name file nil))
13063          (real-file (concat file "-" (nth 1 gnus-select-method))))
13064     (if (or (file-exists-p real-file)
13065             (file-exists-p (concat real-file ".el"))
13066             (file-exists-p (concat real-file ".eld")))
13067         real-file file)))
13068
13069 (defun gnus-uncompress-newsrc-alist ()
13070   ;; Uncompress all lists of marked articles in the newsrc assoc.
13071   (let ((newsrc gnus-newsrc-alist)
13072         marked)
13073     (while newsrc
13074       (if (not (setq marked (nth 3 (car newsrc))))
13075           ()
13076         (while marked
13077           (or (eq 'score (car (car marked)))
13078               (eq 'bookmark (car (car marked)))
13079               (eq 'killed (car (car marked)))
13080               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13081           (setq marked (cdr marked))))
13082       (setq newsrc (cdr newsrc)))))
13083
13084 (defun gnus-compress-newsrc-alist ()
13085   ;; Compress all lists of marked articles in the newsrc assoc.
13086   (let ((newsrc gnus-newsrc-alist)
13087         marked)
13088     (while newsrc
13089       (if (not (setq marked (nth 3 (car newsrc))))
13090           ()
13091         (while marked
13092           (or (eq 'score (car (car marked)))
13093               (eq 'bookmark (car (car marked)))
13094               (eq 'killed (car (car marked)))
13095               (setcdr (car marked) 
13096                       (condition-case ()
13097                           (gnus-compress-sequence 
13098                            (sort (cdr (car marked)) '<) t)
13099                         (error (cdr (car marked))))))
13100           (setq marked (cdr marked))))
13101       (setq newsrc (cdr newsrc)))))
13102
13103 (defun gnus-newsrc-to-gnus-format ()
13104   (setq gnus-newsrc-options "")
13105   (setq gnus-newsrc-options-n nil)
13106
13107   (or gnus-active-hashtb
13108       (setq gnus-active-hashtb (make-vector 4095 0)))
13109   (let ((buf (current-buffer))
13110         (already-read (> (length gnus-newsrc-alist) 1))
13111         group subscribed options-symbol newsrc Options-symbol
13112         symbol reads num1)
13113     (goto-char (point-min))
13114     ;; We intern the symbol `options' in the active hashtb so that we
13115     ;; can `eq' against it later.
13116     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
13117     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
13118   
13119     (while (not (eobp))
13120       ;; We first read the first word on the line by narrowing and
13121       ;; then reading into `gnus-active-hashtb'.  Most groups will
13122       ;; already exist in that hashtb, so this will save some string
13123       ;; space.
13124       (narrow-to-region
13125        (point)
13126        (progn (skip-chars-forward "^ \t!:\n") (point)))
13127       (goto-char (point-min))
13128       (setq symbol 
13129             (and (/= (point-min) (point-max))
13130                  (let ((obarray gnus-active-hashtb)) (read buf))))
13131       (widen)
13132       ;; Now, the symbol we have read is either `options' or a group
13133       ;; name.  If it is an options line, we just add it to a string. 
13134       (cond 
13135        ((or (eq symbol options-symbol)
13136             (eq symbol Options-symbol))
13137         (setq gnus-newsrc-options
13138               ;; This concatting is quite inefficient, but since our
13139               ;; thorough studies show that approx 99.37% of all
13140               ;; .newsrc files only contain a single options line, we
13141               ;; don't give a damn, frankly, my dear.
13142               (concat gnus-newsrc-options
13143                       (buffer-substring 
13144                        (gnus-point-at-bol)
13145                        ;; Options may continue on the next line.
13146                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13147                                 (progn (beginning-of-line) (point)))
13148                            (point)))))
13149         (forward-line -1))
13150        (symbol
13151         (or (boundp symbol) (set symbol nil))
13152         ;; It was a group name.
13153         (setq subscribed (= (following-char) ?:)
13154               group (symbol-name symbol)
13155               reads nil)
13156         (if (eolp)
13157             ;; If the line ends here, this is clearly a buggy line, so
13158             ;; we put point a the beginning of line and let the cond
13159             ;; below do the error handling.
13160             (beginning-of-line)
13161           ;; We skip to the beginning of the ranges.
13162           (skip-chars-forward "!: \t"))
13163         ;; We are now at the beginning of the list of read articles.
13164         ;; We read them range by range.
13165         (while
13166             (cond 
13167              ((looking-at "[0-9]+")
13168               ;; We narrow and read a number instead of buffer-substring/
13169               ;; string-to-int because it's faster. narrow/widen is
13170               ;; faster than save-restriction/narrow, and save-restriction
13171               ;; produces a garbage object.
13172               (setq num1 (progn
13173                            (narrow-to-region (match-beginning 0) (match-end 0))
13174                            (read buf)))
13175               (widen)
13176               ;; If the next character is a dash, then this is a range.
13177               (if (= (following-char) ?-)
13178                   (progn
13179                     ;; We read the upper bound of the range.
13180                     (forward-char 1)
13181                     (if (not (looking-at "[0-9]+"))
13182                         ;; This is a buggy line, by we pretend that
13183                         ;; it's kinda OK. Perhaps the user should be
13184                         ;; dinged? 
13185                         (setq reads (cons num1 reads))
13186                       (setq reads 
13187                             (cons 
13188                              (cons num1
13189                                    (progn
13190                                      (narrow-to-region (match-beginning 0) 
13191                                                        (match-end 0))
13192                                      (read buf)))
13193                              reads))
13194                       (widen)))
13195                 ;; It was just a simple number, so we add it to the
13196                 ;; list of ranges.
13197                 (setq reads (cons num1 reads)))
13198               ;; If the next char in ?\n, then we have reached the end
13199               ;; of the line and return nil.
13200               (/= (following-char) ?\n))
13201              ((= (following-char) ?\n)
13202               ;; End of line, so we end.
13203               nil)
13204              (t
13205               ;; Not numbers and not eol, so this might be a buggy
13206               ;; line... 
13207               (or (eobp)                
13208                   ;; If it was eob instead of ?\n, we allow it.
13209                   (progn
13210                     ;; The line was buggy.
13211                     (setq group nil)
13212                     (gnus-message 3 "Mangled line: %s" 
13213                                   (buffer-substring (gnus-point-at-bol) 
13214                                                     (gnus-point-at-eol)))
13215                     (ding)
13216                     (sit-for 1)))
13217               nil))
13218           ;; Skip past ", ". Spaces are illegal in these ranges, but
13219           ;; we allow them, because it's a common mistake to put a
13220           ;; space after the comma.
13221           (skip-chars-forward ", "))
13222
13223         ;; We have already read .newsrc.eld, so we gently update the
13224         ;; data in the hash table with the information we have just
13225         ;; read. 
13226         (if (not group)
13227             ()
13228           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13229                 level)
13230             (if info
13231                 ;; There is an entry for this file in the alist.
13232                 (progn
13233                   (setcar (nthcdr 2 info) (nreverse reads))
13234                   ;; We update the level very gently.  In fact, we
13235                   ;; only change it if there's been a status change
13236                   ;; from subscribed to unsubscribed, or vice versa.
13237                   (setq level (nth 1 info))
13238                   (cond ((and (<= level gnus-level-subscribed)
13239                               (not subscribed))
13240                          (setq level (if reads
13241                                          gnus-level-default-unsubscribed 
13242                                        (1+ gnus-level-default-unsubscribed))))
13243                         ((and (> level gnus-level-subscribed) subscribed)
13244                          (setq level gnus-level-default-subscribed)))
13245                   (setcar (cdr info) level))
13246               ;; This is a new group.
13247               (setq info (list group 
13248                                (if subscribed
13249                                    gnus-level-default-subscribed 
13250                                  (if reads
13251                                      (1+ gnus-level-subscribed)
13252                                    gnus-level-default-unsubscribed))
13253                                (nreverse reads))))
13254             (setq newsrc (cons info newsrc))))))
13255       (forward-line 1))
13256     
13257     (setq newsrc (nreverse newsrc))
13258
13259     (if (not already-read)
13260         ()
13261       ;; We now have two newsrc lists - `newsrc', which is what we
13262       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13263       ;; what we've read from .newsrc.eld. We have to merge these
13264       ;; lists. We do this by "attaching" any (foreign) groups in the
13265       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13266       (let ((rc (cdr gnus-newsrc-alist))
13267             (prev gnus-newsrc-alist)
13268             entry mentry)
13269         (while rc
13270           (or (null (nth 4 (car rc)))   ; It's a native group.
13271               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13272               (if (setq entry (assoc (car (car prev)) newsrc))
13273                   (setcdr (setq mentry (memq entry newsrc))
13274                           (cons (car rc) (cdr mentry)))
13275                 (setq newsrc (cons (car rc) newsrc))))
13276           (setq prev rc
13277                 rc (cdr rc)))))
13278
13279     (setq gnus-newsrc-alist newsrc)
13280     ;; We make the newsrc hashtb.
13281     (gnus-make-hashtable-from-newsrc-alist)
13282
13283     ;; Finally, if we read some options lines, we parse them.
13284     (or (string= gnus-newsrc-options "")
13285         (gnus-newsrc-parse-options gnus-newsrc-options))))
13286
13287 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13288 ;; The return value will be a list on the form
13289 ;; ((regexp1 . ignore)
13290 ;;  (regexp2 . subscribe)...)
13291 ;; When handling new newsgroups, groups that match a `ignore' regexp
13292 ;; will be ignored, and groups that match a `subscribe' regexp will be
13293 ;; subscribed. A line like
13294 ;; options -n !all rec.all
13295 ;; will lead to a list that looks like
13296 ;; (("^rec\\..+" . subscribe) 
13297 ;;  ("^.+" . ignore))
13298 ;; So all "rec.*" groups will be subscribed, while all the other
13299 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13300 ;; different from "options -n rec.all !all". 
13301 (defun gnus-newsrc-parse-options (options)
13302   (let (out eol)
13303     (save-excursion
13304       (gnus-set-work-buffer)
13305       (insert (regexp-quote options))
13306       ;; First we treat all continuation lines.
13307       (goto-char (point-min))
13308       (while (re-search-forward "\n[ \t]+" nil t)
13309         (replace-match " " t t))
13310       ;; Then we transform all "all"s into ".+"s.
13311       (goto-char (point-min))
13312       (while (re-search-forward "\\ball\\b" nil t)
13313         (replace-match ".+" t t))
13314       (goto-char (point-min))
13315       ;; We remove all other options than the "-n" ones.
13316       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13317         (replace-match " ")
13318         (forward-char -1))
13319       (goto-char (point-min))
13320
13321       ;; We are only interested in "options -n" lines - we
13322       ;; ignore the other option lines.
13323       (while (re-search-forward "[ \t]-n" nil t)
13324         (setq eol 
13325               (or (save-excursion
13326                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13327                          (- (point) 2)))
13328                   (gnus-point-at-eol)))
13329         ;; Search for all "words"...
13330         (while (re-search-forward "[^ \t,\n]+" eol t)
13331           (if (= (char-after (match-beginning 0)) ?!)
13332               ;; If the word begins with a bang (!), this is a "not"
13333               ;; spec. We put this spec (minus the bang) and the
13334               ;; symbol `ignore' into the list.
13335               (setq out (cons (cons (concat 
13336                                      "^" (buffer-substring 
13337                                           (1+ (match-beginning 0))
13338                                           (match-end 0)))
13339                                     'ignore) out))
13340             ;; There was no bang, so this is a "yes" spec.
13341             (setq out (cons (cons (concat 
13342                                    "^" (buffer-substring (match-beginning 0)
13343                                                          (match-end 0)))
13344                                   'subscribe) out)))))
13345     
13346       (setq gnus-newsrc-options-n out))))
13347                
13348
13349 (defun gnus-save-newsrc-file ()
13350   "Save .newsrc file."
13351   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13352   ;; from the variable gnus-newsrc-alist.
13353   (and (or gnus-newsrc-alist gnus-killed-list)
13354        gnus-current-startup-file
13355        (progn
13356          (run-hooks 'gnus-save-newsrc-hook)
13357          (save-excursion
13358            (if (and gnus-use-dribble-file
13359                     (or (not gnus-dribble-buffer)
13360                         (not (buffer-name gnus-dribble-buffer))
13361                         (zerop (save-excursion
13362                                  (set-buffer gnus-dribble-buffer)
13363                                  (buffer-size)))))
13364                (gnus-message 4 "(No changes need to be saved)")
13365              (if gnus-save-newsrc-file
13366                  (progn
13367                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13368                    ;; Make backup file of master newsrc.
13369                    (gnus-gnus-to-newsrc-format)
13370                    (gnus-message 5 "Saving %s...done"
13371                                  gnus-current-startup-file)))
13372              ;; Quickly loadable .newsrc.
13373              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13374              (setq buffer-file-name (concat gnus-current-startup-file ".eld"))
13375              (gnus-add-current-to-buffer-list)
13376              (buffer-disable-undo (current-buffer))
13377              (erase-buffer)
13378              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13379              (gnus-gnus-to-quick-newsrc-format)
13380              (save-buffer)
13381              (kill-buffer (current-buffer))
13382              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13383              (gnus-dribble-delete-file))))))
13384
13385 (defun gnus-gnus-to-quick-newsrc-format ()
13386   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13387   (insert ";; Gnus startup file.\n")
13388   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13389   (insert ";; to read .newsrc.\n")
13390   (insert "(setq gnus-newsrc-file-version "
13391           (prin1-to-string gnus-version) ")\n")
13392   (let ((variables gnus-variable-list)
13393         (inhibit-quit t)
13394         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13395         variable)
13396     ;; insert lisp expressions.
13397     (gnus-compress-newsrc-alist)
13398     (while variables
13399       (setq variable (car variables))
13400       (and (boundp variable)
13401            (symbol-value variable)
13402            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13403            (insert "(setq " (symbol-name variable) " '"
13404                    (prin1-to-string (symbol-value variable))
13405                    ")\n"))
13406       (setq variables (cdr variables)))
13407     (gnus-uncompress-newsrc-alist)))
13408
13409
13410 (defun gnus-gnus-to-newsrc-format ()
13411   ;; Generate and save the .newsrc file.
13412   (let ((newsrc (cdr gnus-newsrc-alist))
13413         info ranges range)
13414     (save-excursion
13415       (set-buffer (create-file-buffer gnus-current-startup-file))
13416       (setq buffer-file-name gnus-current-startup-file)
13417       (buffer-disable-undo (current-buffer))
13418       (erase-buffer)
13419       ;; Write options.
13420       (if gnus-newsrc-options (insert gnus-newsrc-options))
13421       ;; Write subscribed and unsubscribed.
13422       (while newsrc
13423         (setq info (car newsrc))
13424         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13425             (progn
13426               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13427                                      "!" ":"))
13428               (if (setq ranges (nth 2 info))
13429                   (progn
13430                     (insert " ")
13431                     (if (not (listp (cdr ranges)))
13432                         (if (= (car ranges) (cdr ranges))
13433                             (insert (int-to-string (car ranges)))
13434                           (insert (int-to-string (car ranges)) "-" 
13435                                   (int-to-string (cdr ranges))))
13436                       (while ranges
13437                         (setq range (car ranges)
13438                               ranges (cdr ranges))
13439                         (if (or (atom range) (= (car range) (cdr range)))
13440                             (insert (int-to-string 
13441                                      (or (and (atom range) range) 
13442                                          (car range))))
13443                           (insert (int-to-string (car range)) "-"
13444                                   (int-to-string (cdr range))))
13445                         (if ranges (insert ","))))))
13446               (insert "\n")))
13447         (setq newsrc (cdr newsrc)))
13448       ;; It has been reported that sometime the modtime on the .newsrc
13449       ;; file seems to be off. We really do want to overwrite it, so
13450       ;; we clear the modtime here before saving. It's a bit odd,
13451       ;; though... 
13452       ;; sometimes the modtime clear isn't sufficient.  most brute force:
13453       ;; delete the silly thing entirely first.  but this fails to provide
13454       ;; such niceties as .newsrc~ creation.
13455       (if gnus-modtime-botch
13456           (delete-file gnus-startup-file)
13457         (clear-visited-file-modtime))
13458       (save-buffer)
13459       (kill-buffer (current-buffer)))))
13460
13461 (defun gnus-read-all-descriptions-files ()
13462   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13463     (while methods
13464       (gnus-read-descriptions-file (car methods))
13465       (setq methods (cdr methods)))
13466     t))
13467
13468 (defun gnus-read-descriptions-file (&optional method)
13469   (let ((method (or method gnus-select-method)))
13470     ;; We create the hashtable whether we manage to read the desc file
13471     ;; to avoid trying to re-read after a failed read.
13472     (or gnus-description-hashtb
13473         (setq gnus-description-hashtb 
13474               (gnus-make-hashtable (length gnus-active-hashtb))))
13475     ;; Mark this method's desc file as read.
13476     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13477                   gnus-description-hashtb)
13478
13479     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13480     (cond 
13481      ((not (gnus-check-server method))
13482       (gnus-message 1 "Couldn't open server")
13483       nil)
13484      ((not (gnus-request-list-newsgroups method))
13485       (gnus-message 1 "Couldn't read newsgroups descriptions")
13486       nil)
13487      (t
13488       (let (group)
13489         (save-excursion
13490           (save-restriction
13491             (set-buffer nntp-server-buffer)
13492             (goto-char (point-min))
13493             (if (or (search-forward "\n.\n" nil t)
13494                     (goto-char (point-max)))
13495                 (progn
13496                   (beginning-of-line)
13497                   (narrow-to-region (point-min) (point))))
13498             (goto-char (point-min))
13499             (while (not (eobp))
13500               ;; If we get an error, we set group to 0, which is not a
13501               ;; symbol... 
13502               (setq group 
13503                     (condition-case ()
13504                         (let ((obarray gnus-description-hashtb))
13505                           ;; Group is set to a symbol interned in this
13506                           ;; hash table.
13507                           (read nntp-server-buffer))
13508                       (error 0)))
13509               (skip-chars-forward " \t")
13510               ;; ... which leads to this line being effectively ignored.
13511               (and (symbolp group)
13512                    (set group (buffer-substring 
13513                                (point) (progn (end-of-line) (point)))))
13514               (forward-line 1))))
13515         (gnus-message 5 "Reading descriptions file...done")
13516         t)))))
13517
13518 (defun gnus-group-get-description (group)
13519   ;; Get the description of a group by sending XGTITLE to the server.
13520   (and (gnus-request-group-description group)
13521        (save-excursion
13522          (set-buffer nntp-server-buffer)
13523          (goto-char (point-min))
13524          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13525               (buffer-substring (match-beginning 1) (match-end 1))))))
13526
13527 ;;;
13528 ;;; Server
13529 ;;;
13530
13531 (defvar gnus-server-mode-hook nil
13532   "Hook run in `gnus-server-mode' buffers.")
13533
13534 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13535   "Format of server lines.
13536 It works along the same lines as a normal formatting string,
13537 with some simple extensions.")
13538
13539 (defvar gnus-server-mode-line-format "Gnus  List of servers"
13540   "The format specification for the server mode line.")
13541
13542 (defconst gnus-server-line-format-alist
13543   (list (list ?h 'how ?s)
13544         (list ?n 'name ?s)
13545         (list ?w 'where ?s)
13546         ))
13547
13548 (defconst gnus-server-mode-line-format-alist 
13549   (list (list ?S 'news-server ?s)
13550         (list ?M 'news-method ?s)
13551         (list ?u 'user-defined ?s)))
13552
13553 (defvar gnus-server-line-format-spec nil)
13554 (defvar gnus-server-mode-line-format-spec nil)
13555 (defvar gnus-server-killed-servers nil)
13556
13557 (defvar gnus-server-mode-map nil)
13558 (put 'gnus-server-mode 'mode-class 'special)
13559
13560 (if gnus-server-mode-map
13561     nil
13562   (setq gnus-server-mode-map (make-sparse-keymap))
13563   (suppress-keymap gnus-server-mode-map)
13564   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13565   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13566   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13567   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13568   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13569   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13570   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13571   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13572   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13573   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13574
13575 (defun gnus-server-mode ()
13576   "Major mode for listing and editing servers.
13577
13578 All normal editing commands are switched off.
13579 \\<gnus-server-mode-map>
13580
13581 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13582
13583 The following commands are available:
13584
13585 \\{gnus-server-mode-map}"
13586   (interactive)
13587   (if gnus-visual (gnus-server-make-menu-bar))
13588   (kill-all-local-variables)
13589   (setq mode-line-modified "-- ")
13590   (make-local-variable 'mode-line-format)
13591   (setq mode-line-format (copy-sequence mode-line-format))
13592   (and (equal (nth 3 mode-line-format) "   ")
13593        (setcar (nthcdr 3 mode-line-format) ""))
13594   (setq major-mode 'gnus-server-mode)
13595   (setq mode-name "Server")
13596                                         ;  (gnus-group-set-mode-line)
13597   (setq mode-line-process nil)
13598   (use-local-map gnus-server-mode-map)
13599   (buffer-disable-undo (current-buffer))
13600   (setq truncate-lines t)
13601   (setq buffer-read-only t)
13602   (run-hooks 'gnus-server-mode-hook))
13603
13604 (defun gnus-server-insert-server-line (sformat name method)
13605   (let* ((sformat (or sformat gnus-server-line-format-spec))
13606          (how (car method))
13607          (where (nth 1 method))
13608          b)
13609     (beginning-of-line)
13610     (setq b (point))
13611     ;; Insert the text.
13612     (insert (eval sformat))
13613     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13614
13615 (defun gnus-server-setup-buffer ()
13616   (if (get-buffer gnus-server-buffer)
13617       ()
13618     (save-excursion
13619       (set-buffer (get-buffer-create gnus-server-buffer))
13620       (gnus-server-mode)
13621       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13622
13623 (defun gnus-server-prepare ()
13624   (setq gnus-server-mode-line-format-spec 
13625         (gnus-parse-format gnus-server-mode-line-format 
13626                            gnus-server-mode-line-format-alist))
13627   (setq gnus-server-line-format-spec 
13628         (gnus-parse-format gnus-server-line-format 
13629                            gnus-server-line-format-alist))
13630   (let ((alist gnus-server-alist)
13631         (buffer-read-only nil))
13632     (erase-buffer)
13633     (while alist
13634       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13635       (setq alist (cdr alist))))
13636   (goto-char (point-min))
13637   (gnus-server-position-cursor))
13638
13639 (defun gnus-server-server-name ()
13640   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13641     (and server (symbol-name server))))
13642
13643 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13644
13645 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13646
13647 (defun gnus-server-update-server (server)
13648   (save-excursion
13649     (set-buffer gnus-server-buffer)
13650     (let ((buffer-read-only nil)
13651           (info (cdr (assoc server gnus-server-alist))))
13652       (gnus-dribble-enter 
13653        (concat "(gnus-server-set-info \"" server "\" '"
13654                (prin1-to-string info) ")"))
13655       ;; Buffer may be narrowed.
13656       (save-restriction
13657         (widen)
13658         (if (gnus-server-goto-server server)
13659             (delete-region (progn (beginning-of-line) (point))
13660                            (progn (forward-line 1) (point))))
13661         (let ((entry (assoc server gnus-server-alist)))
13662           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13663           (gnus-server-position-cursor))))))
13664
13665 (defun gnus-server-set-info (server info)
13666   ;; Enter a select method into the virtual server alist.
13667   (gnus-dribble-enter 
13668    (concat "(gnus-server-set-info \"" server "\" '"
13669            (prin1-to-string info) ")"))
13670   (let* ((server (nth 1 info))
13671          (entry (assoc server gnus-server-alist)))
13672     (if entry (setcdr entry info)
13673       (setq gnus-server-alist
13674             (nconc gnus-server-alist (list (cons server info)))))))
13675
13676 (defun gnus-server-to-method (server)
13677   ;; Map virtual server names to select methods.
13678   (or (and (equal server "native") gnus-select-method)
13679       (cdr (assoc server gnus-server-alist))))
13680
13681 (defun gnus-server-extend-method (group method)
13682   ;; This function "extends" a virtual server.  If the server is
13683   ;; "hello", and the select method is ("hello" (my-var "something")) 
13684   ;; in the group "alt.alt", this will result in a new virtual server
13685   ;; called "helly+alt.alt".
13686   (let ((entry
13687          (gnus-copy-sequence 
13688           (if (equal (car method) "native") gnus-select-method
13689             (cdr (assoc (car method) gnus-server-alist))))))
13690     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13691     (nconc entry (cdr method))))
13692
13693 (defun gnus-server-get-method (group method)
13694   ;; Input either a server name, and extended server name, or a
13695   ;; select method, and return a select method. 
13696   (cond ((stringp method)
13697          (gnus-server-to-method method))
13698         ((and (stringp (car method)) group)
13699          (gnus-server-extend-method group method))
13700         (t
13701          (gnus-server-add-address method))))
13702
13703 (defun gnus-server-add-address (method)
13704   (let ((method-name (symbol-name (car method))))
13705     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13706              (not (assq (intern (concat method-name "-address")) method)))
13707         (append method (list (list (intern (concat method-name "-address"))
13708                                    (nth 1 method))))
13709       method)))
13710
13711 (defun gnus-server-equal (s1 s2)
13712   (or (equal s1 s2)
13713       (and (= (length s1) (length s2))
13714            (progn
13715              (while (and s1 (member (car s1) s2))
13716                (setq s1 (cdr s1)))
13717              (null s1)))))
13718
13719 ;;; Interactive server functions.
13720
13721 (defun gnus-server-kill-server (server)
13722   "Kill the server on the current line."
13723   (interactive (list (gnus-server-server-name)))
13724   (or (gnus-server-goto-server server)
13725       (if server (error "No such server: %s" server)
13726         (error "No server on the current line")))
13727   (gnus-dribble-enter "")
13728   (let ((buffer-read-only nil))
13729     (delete-region (progn (beginning-of-line) (point))
13730                    (progn (forward-line 1) (point))))
13731   (setq gnus-server-killed-servers 
13732         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13733   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13734                                 gnus-server-alist))
13735   (gnus-server-position-cursor))
13736
13737 (defun gnus-server-yank-server ()
13738   "Yank the previously killed server."
13739   (interactive)
13740   (or gnus-server-killed-servers
13741       (error "No killed servers to be yanked"))
13742   (let ((alist gnus-server-alist)
13743         (server (gnus-server-server-name))
13744         (killed (car gnus-server-killed-servers)))
13745     (if (not server) 
13746         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13747       (if (string= server (car (car gnus-server-alist)))
13748           (setq gnus-server-alist (cons killed gnus-server-alist))
13749         (while (and (cdr alist)
13750                     (not (string= server (car (car (cdr alist))))))
13751           (setq alist (cdr alist)))
13752         (setcdr alist (cons killed (cdr alist)))))
13753     (gnus-server-update-server (car killed))
13754     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13755     (gnus-server-position-cursor)))
13756
13757 (defun gnus-server-exit ()
13758   "Return to the group buffer."
13759   (interactive)
13760   (kill-buffer (current-buffer))
13761   (switch-to-buffer gnus-group-buffer))
13762
13763 (defun gnus-server-list-servers ()
13764   "List all available servers."
13765   (interactive)
13766   (let ((cur (gnus-server-server-name)))
13767     (gnus-server-prepare)
13768     (if cur (gnus-server-goto-server cur)
13769       (goto-char (point-max))
13770       (forward-line -1))
13771     (gnus-server-position-cursor)))
13772
13773 (defun gnus-server-copy-server (from to)
13774   (interactive
13775    (list
13776     (or (gnus-server-server-name)
13777         (error "No server on the current line"))
13778     (read-string "Copy to: ")))
13779   (or from (error "No server on current line"))
13780   (or (and to (not (string= to ""))) (error "No name to copy to"))
13781   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13782   (or (assoc from gnus-server-alist) 
13783       (error "%s: no such server" from))
13784   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13785     (setcar to-entry to)
13786     (setcar (nthcdr 2 to-entry) to)
13787     (setq gnus-server-killed-servers 
13788           (cons to-entry gnus-server-killed-servers))
13789     (gnus-server-yank-server)))
13790
13791 (defun gnus-server-add-server (how where)
13792   (interactive 
13793    (list (intern (completing-read "Server method: "
13794                                   gnus-valid-select-methods nil t))
13795          (read-string "Server name: ")))
13796   (setq gnus-server-killed-servers 
13797         (cons (list where how where) gnus-server-killed-servers))
13798   (gnus-server-yank-server))
13799
13800 (defun gnus-server-goto-server (server)
13801   "Jump to a server line."
13802   (interactive
13803    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13804   (let ((to (text-property-any (point-min) (point-max) 
13805                                'gnus-server (intern server))))
13806     (and to
13807          (progn
13808            (goto-char to) 
13809            (gnus-server-position-cursor)))))
13810
13811 (defun gnus-server-edit-server (server)
13812   "Edit the server on the current line."
13813   (interactive (list (gnus-server-server-name)))
13814   (or server
13815       (error "No server on current line"))
13816   (let ((winconf (current-window-configuration)))
13817     (get-buffer-create gnus-server-edit-buffer)
13818     (gnus-configure-windows 'edit-server)
13819     (gnus-add-current-to-buffer-list)
13820     (emacs-lisp-mode)
13821     (make-local-variable 'gnus-prev-winconf)
13822     (setq gnus-prev-winconf winconf)
13823     (use-local-map (copy-keymap (current-local-map)))
13824     (let ((done-func '(lambda () 
13825                         "Exit editing mode and update the information."
13826                         (interactive)
13827                         (gnus-server-edit-server-done 'group))))
13828       (setcar (cdr (nth 4 done-func)) server)
13829       (local-set-key "\C-c\C-c" done-func))
13830     (erase-buffer)
13831     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13832     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13833
13834 (defun gnus-server-edit-server-done (server)
13835   (interactive)
13836   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13837   (goto-char (point-min))
13838   (let ((form (read (current-buffer)))
13839         (winconf gnus-prev-winconf))
13840     (gnus-server-set-info server form)
13841     (kill-buffer (current-buffer))
13842     (and winconf (set-window-configuration winconf))
13843     (set-buffer gnus-server-buffer)
13844     (gnus-server-update-server (gnus-server-server-name))
13845     (gnus-server-list-servers)
13846     (gnus-server-position-cursor)))
13847
13848 (defun gnus-server-read-server (server)
13849   "Browse a server."
13850   (interactive (list (gnus-server-server-name)))
13851   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13852
13853 (defun gnus-mouse-pick-server (e)
13854   (interactive "e")
13855   (mouse-set-point e)
13856   (gnus-server-read-server (gnus-server-server-name)))
13857
13858 ;;;
13859 ;;; entry points into gnus-score.el
13860 ;;;
13861
13862 ;;; Finding score files. 
13863
13864 (defvar gnus-global-score-files nil
13865   "*List of global score files and directories.
13866 Set this variable if you want to use people's score files.  One entry
13867 for each score file or each score file directory.  Gnus will decide
13868 by itself what score files are applicable to which group.
13869
13870 Say you want to use the single score file
13871 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13872 score files in the \"/ftp.some-where:/pub/score\" directory.
13873
13874  (setq gnus-global-score-files
13875        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13876          \"/ftp.some-where:/pub/score\"))")
13877
13878 (defun gnus-score-score-files (group)
13879   "Return a list of all possible score files."
13880   ;; Search and set any global score files.
13881   (and gnus-global-score-files 
13882        (or gnus-internal-global-score-files
13883            (gnus-score-search-global-directories gnus-global-score-files)))
13884   ;; Fix the kill-file dir variable.
13885   (setq gnus-kill-files-directory 
13886         (file-name-as-directory
13887          (or gnus-kill-files-directory "~/News/")))
13888   ;; If we can't read it, there are no score files.
13889   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13890       (setq gnus-score-file-list nil)
13891     (if (gnus-use-long-file-name 'not-score)
13892         ;; We want long file names.
13893         (if (or (not gnus-score-file-list)
13894                 (not (car gnus-score-file-list))
13895                 (gnus-file-newer-than gnus-kill-files-directory
13896                                       (car gnus-score-file-list)))
13897             (setq gnus-score-file-list 
13898                   (cons (nth 5 (file-attributes gnus-kill-files-directory))
13899                         (nreverse 
13900                          (directory-files 
13901                           gnus-kill-files-directory t 
13902                           (gnus-score-file-regexp))))))
13903       ;; We do not use long file names, so we have to do some
13904       ;; directory traversing.  
13905       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13906             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13907             dir files suffix)
13908         (while suffixes
13909           (setq dir (expand-file-name
13910                      (concat gnus-kill-files-directory
13911                              (gnus-replace-chars-in-string group ?. ?/))))
13912           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13913           (setq suffix (car suffixes)
13914                 suffixes (cdr suffixes))
13915           (if (file-exists-p (concat dir "/" suffix))
13916               (setq files (cons (concat dir "/" suffix) files)))
13917           (while (>= (1+ (length dir)) mdir)
13918             (and (file-exists-p (concat dir "/all/" suffix))
13919                  (setq files (cons (concat dir "/all/" suffix) files)))
13920             (string-match "/[^/]*$" dir)
13921             (setq dir (substring dir 0 (match-beginning 0)))))
13922         (setq gnus-score-file-list 
13923               (cons nil (nreverse files)))))
13924     (cdr gnus-score-file-list)))
13925
13926 (defun gnus-score-file-regexp ()
13927   (concat "\\(" gnus-score-file-suffix 
13928           "\\|" gnus-adaptive-file-suffix "\\)$"))
13929         
13930 (defun gnus-score-find-bnews (group)
13931   "Return a list of score files for GROUP.
13932 The score files are those files in the ~/News directory which matches
13933 GROUP using BNews sys file syntax."
13934   (let* ((sfiles (append (gnus-score-score-files group)
13935                          gnus-internal-global-score-files))
13936          (kill-dir (file-name-as-directory 
13937                     (expand-file-name gnus-kill-files-directory)))
13938          (klen (length kill-dir))
13939          ofiles not-match regexp)
13940     (save-excursion
13941       (set-buffer (get-buffer-create "*gnus score files*"))
13942       (buffer-disable-undo (current-buffer))
13943       ;; Go through all score file names and create regexp with them
13944       ;; as the source.  
13945       (while sfiles
13946         (erase-buffer)
13947         (insert (car sfiles))
13948         (goto-char (point-min))
13949         ;; First remove the suffix itself.
13950         (re-search-forward (concat "." (gnus-score-file-regexp)))
13951         (replace-match "" t t) 
13952         (goto-char (point-min))
13953         (if (looking-at (regexp-quote kill-dir))
13954             ;; If the file name was just "SCORE", `klen' is one character
13955             ;; too much.
13956             (delete-char (min (1- (point-max)) klen))
13957           (goto-char (point-max))
13958           (search-backward "/")
13959           (delete-region (1+ (point)) (point-min)))
13960         ;; If short file names were used, we have to translate slashes.
13961         (goto-char (point-min))
13962         (while (re-search-forward "[/:]" nil t)
13963           (replace-match "." t t))
13964         ;; Cludge to get rid of "nntp+" problems.
13965         (goto-char (point-min))
13966         (and (looking-at "nn[a-z]+\\+")
13967              (progn
13968                (search-forward "+")
13969                (forward-char -1)
13970                (insert "\\")))
13971         ;; Translate ".all" to "[./].*";
13972         (while (search-forward ".all" nil t)
13973           (replace-match "[./].*" t t))
13974         (goto-char (point-min))
13975         ;; Translate "all" to ".*".
13976         (while (search-forward "all" nil t)
13977           (replace-match ".*" t t))
13978         (goto-char (point-min))
13979         ;; Deal with "not."s.
13980         (if (looking-at "not.")
13981             (progn
13982               (setq not-match t)
13983               (setq regexp (buffer-substring 5 (point-max))))
13984           (setq regexp (buffer-substring 1 (point-max)))
13985           (setq not-match nil))
13986         ;; Finally - if this resulting regexp matches the group name,
13987         ;; we add this score file to the list of score files
13988         ;; applicable to this group.
13989         (if (or (and not-match
13990                      (not (string-match regexp group)))
13991                 (and (not not-match)
13992                      (string-match regexp group)))
13993             (setq ofiles (cons (car sfiles) ofiles)))
13994         (setq sfiles (cdr sfiles)))
13995       (kill-buffer (current-buffer))
13996       ;; Slight kludge here - the last score file returned should be
13997       ;; the local score file, whether it exists or not. This is so
13998       ;; that any score commands the user enters will go to the right
13999       ;; file, and not end up in some global score file.
14000       (let ((localscore
14001              (expand-file-name
14002               (if (gnus-use-long-file-name 'not-score)
14003                   (concat gnus-kill-files-directory group "." 
14004                           gnus-score-file-suffix)
14005                 (concat gnus-kill-files-directory
14006                         (gnus-replace-chars-in-string group ?. ?/ ?: ?/)
14007                         "/" gnus-score-file-suffix)))))
14008         (and (member localscore ofiles)
14009              (delete localscore ofiles))
14010         (setq ofiles (cons localscore ofiles)))
14011       (nreverse ofiles))))
14012
14013 (defun gnus-score-find-single (group)
14014   "Return list containing the score file for GROUP."
14015   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
14016         (gnus-score-file-name group)))
14017
14018 (defun gnus-score-find-hierarchical (group)
14019   "Return list of score files for GROUP.
14020 This includes the score file for the group and all its parents."
14021   (let ((all (copy-sequence '(nil)))
14022         (start 0))
14023     (while (string-match "\\." group (1+ start))
14024       (setq start (match-beginning 0))
14025       (setq all (cons (substring group 0 start) all)))
14026     (setq all (cons group all))
14027     (nconc
14028      (mapcar (lambda (newsgroup)
14029                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
14030              (setq all (nreverse all)))
14031      (mapcar 'gnus-score-file-name all))))
14032
14033 (defvar gnus-score-file-alist-cache nil)
14034
14035 (defun gnus-score-find-alist (group)
14036   "Return list of score files for GROUP.
14037 The list is determined from the variable gnus-score-file-alist."
14038   (let ((alist gnus-score-file-multiple-match-alist)
14039         score-files)
14040     ;; if this group has been seen before, return the cached entry
14041     (if (setq score-files (assoc group gnus-score-file-alist-cache))
14042         (cdr score-files)               ;ensures caching groups with no matches
14043       ;; handle the multiple match alist
14044       (while alist
14045         (and (string-match (car (car alist)) group)
14046              (setq score-files
14047                    (nconc score-files (copy-sequence (cdr (car alist))))))
14048         (setq alist (cdr alist)))
14049       (setq alist gnus-score-file-single-match-alist)
14050       ;; handle the single match alist
14051       (while alist
14052         (and (string-match (car (car alist)) group)
14053              ;; progn used just in case ("regexp") has no files
14054              ;; and score-files is still nil. -sj
14055              ;; this can be construed as a "stop searching here" feature :>
14056              ;; and used to simplify regexps in the single-alist 
14057              (progn
14058                (setq score-files
14059                      (nconc score-files (copy-sequence (cdr (car alist)))))
14060                (setq alist nil)))
14061         (setq alist (cdr alist)))
14062       ;; cache the score files
14063       (setq gnus-score-file-alist-cache
14064             (cons (cons group score-files) gnus-score-file-alist-cache))
14065       score-files)))
14066
14067
14068 (defun gnus-possibly-score-headers (&optional trace)
14069   (let ((func gnus-score-find-score-files-function)
14070         score-files)
14071     (and func (not (listp func))
14072          (setq func (list func)))
14073     ;; Go through all the functions for finding score files (or actual
14074     ;; scores) and add them to a list.
14075     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
14076     (while func
14077       (and (symbolp (car func))
14078            (fboundp (car func))
14079            (setq score-files 
14080                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
14081       (setq func (cdr func)))
14082     (if score-files (gnus-score-headers score-files trace))))
14083
14084 (defun gnus-score-file-name (newsgroup &optional suffix)
14085   "Return the name of a score file for NEWSGROUP."
14086   (let ((suffix (or suffix gnus-score-file-suffix)))
14087     (cond 
14088      ((or (null newsgroup)
14089           (string-equal newsgroup ""))
14090       ;; The global score file is placed at top of the directory.
14091       (expand-file-name 
14092        suffix (or gnus-kill-files-directory "~/News")))
14093      ((gnus-use-long-file-name 'not-score)
14094       ;; Append ".SCORE" to newsgroup name.
14095       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14096                                 "." suffix)
14097                         (or gnus-kill-files-directory "~/News")))
14098      (t
14099       ;; Place "SCORE" under the hierarchical directory.
14100       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14101                                 "/" suffix)
14102                         (or gnus-kill-files-directory "~/News"))))))
14103
14104 (defun gnus-score-search-global-directories (files)
14105   "Scan all global score directories for score files."
14106   ;; Set the variable `gnus-internal-global-score-files' to all
14107   ;; available global score files.
14108   (interactive (list gnus-global-score-files))
14109   (let (out)
14110     (while files
14111       (if (string-match "/$" (car files))
14112           (setq out (nconc (directory-files 
14113                             (car files) t
14114                             (concat (gnus-score-file-regexp) "$"))))
14115         (setq out (cons (car files) out)))
14116       (setq files (cdr files)))
14117     (setq gnus-internal-global-score-files out)))
14118
14119 ;; Allow redefinition of Gnus functions.
14120
14121 (gnus-ems-redefine)
14122
14123 (provide 'gnus)
14124
14125 ;;; gnus.el ends here