*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although Gnus looks suspiciously like GNUS, it isn't quite the same
27 ;; beast. Most internal structures have been changed. If you have
28 ;; written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (eval '(run-hooks 'gnus-load-hook))
37
38 (require 'mail-utils)
39 (require 'timezone)
40 (require 'nnheader)
41
42 ;; Site dependent variables. These variables should be defined in
43 ;; paths.el.
44
45 (defvar gnus-default-nntp-server nil
46   "Specify a default NNTP server.
47 This variable should be defined in paths.el, and should never be set
48 by the user.
49 If you want to change servers, you should use `gnus-select-method'.
50 See the documentation to that variable.")
51
52 (defconst gnus-backup-default-subscribed-newsgroups 
53   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
54   "Default default new newsgroups the first time Gnus is run.
55 Should be set in paths.el, and shouldn't be touched by the user.")
56
57 (defvar gnus-local-domain nil
58   "Local domain name without a host name.
59 The DOMAINNAME environment variable is used instead if it is defined.
60 If the `system-name' function returns the full Internet name, there is
61 no need to set this variable.")
62
63 (defvar gnus-local-organization nil
64   "String with a description of what organization (if any) the user belongs to.
65 The ORGANIZATION environment variable is used instead if it is defined.
66 If this variable contains a function, this function will be called
67 with the current newsgroup name as the argument. The function should
68 return a string.
69
70 In any case, if the string (either in the variable, in the environment
71 variable, or returned by the function) is a file name, the contents of
72 this file will be used as the organization.")
73
74 (defvar gnus-use-generic-from nil
75   "If nil, the full host name will be the system name prepended to the domain name.
76 If this is a string, the full host name will be this string.
77 If this is non-nil, non-string, the domain name will be used as the
78 full host name.")
79
80 (defvar gnus-use-generic-path nil
81   "If nil, use the NNTP server name in the Path header.
82 If stringp, use this; if non-nil, use no host name (user name only).")
83
84
85 ;; Customization variables
86
87 ;; Don't touch this variable.
88 (defvar gnus-nntp-service "nntp"
89   "*NNTP service name (\"nntp\" or 119).
90 This is an obsolete variable, which is scarcely used. If you use an
91 nntp server for your newsgroup and want to change the port number
92 used to 899, you would say something along these lines:
93
94  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
95
96 (defvar gnus-select-method 
97   (nconc
98    (list 'nntp (or (getenv "NNTPSERVER") 
99                    (if (and gnus-default-nntp-server
100                             (not (string= gnus-default-nntp-server "")))
101                        gnus-default-nntp-server)
102                    (system-name)))
103    (if (or (null gnus-nntp-service)
104            (equal gnus-nntp-service "nntp"))
105        nil 
106      (list gnus-nntp-service)))
107   "*Default method for selecting a newsgroup.
108 This variable should be a list, where the first element is how the
109 news is to be fetched, the second is the address. 
110
111 For instance, if you want to get your news via NNTP from
112 \"flab.flab.edu\", you could say:
113
114 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
115
116 If you want to use your local spool, say:
117
118 (setq gnus-select-method (list 'nnspool (system-name)))
119
120 If you use this variable, you must set `gnus-nntp-server' to nil.
121
122 There is a lot more to know about select methods and virtual servers -
123 see the manual for details.")
124
125 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
126 (defvar gnus-post-method nil
127   "*Preferred method for posting USENET news.
128 If this variable is nil, Gnus will use the current method to decide
129 which method to use when posting.  If it is non-nil, it will override
130 the current method.  This method will not be used in mail groups and
131 the like, only in \"real\" newsgroups.
132
133 The value must be a valid method as discussed in the documentation of
134 `gnus-select-method'.")
135
136 (defvar gnus-refer-article-method nil
137   "*Preferred method for fetching an article by Message-ID.
138 If you are reading news from the local spool (with nnspool), fetching
139 articles by Message-ID is painfully slow. By setting this method to an
140 nntp method, you might get acceptable results.
141
142 The value of this variable must be a valid select method as discussed
143 in the documentation of `gnus-select-method'")
144
145 (defvar gnus-secondary-select-methods nil
146   "*A list of secondary methods that will be used for reading news.
147 This is a list where each element is a complete select method (see
148 `gnus-select-method').  
149
150 If, for instance, you want to read your mail with the nnml backend,
151 you could set this variable:
152
153 (setq gnus-secondary-select-methods '((nnml \"\")))")
154
155 (defvar gnus-secondary-servers nil
156   "*List of NNTP servers that the user can choose between interactively.
157 To make Gnus query you for a server, you have to give `gnus' a
158 non-numeric prefix - `C-u M-x gnus', in short.")
159
160 (defvar gnus-nntp-server nil
161   "*The name of the host running the NNTP server.
162 This variable is semi-obsolete. Use the `gnus-select-method'
163 variable instead.")
164
165 (defvar gnus-startup-file "~/.newsrc"
166   "*Your `.newsrc' file.
167 `.newsrc-SERVER' will be used instead if that exists.")
168
169 (defvar gnus-init-file "~/.gnus"
170   "*Your Gnus elisp startup file.
171 If a file with the .el or .elc suffixes exist, it will be read
172 instead.") 
173
174 (defvar gnus-group-faq-directory
175   "/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
176   "*Directory where the group FAQs are stored.
177 This will most commonly be on a remote machine, and the file will be
178 fetched by ange-ftp.
179
180 Note that Gnus uses an aol machine as the default directory.  If this
181 feels fundamentally unclean, just think of it as a way to finally get
182 something of value back from them.
183
184 If the default site is too slow, try one of these:
185
186    North America: ftp.uu.net                     /usenet/news.answers
187                   mirrors.aol.com                /pub/rtfm/usenet
188                   ftp.seas.gwu.edu               /pub/rtfm
189                   rtfm.mit.edu                   /pub/usenet/news.answers
190    Europe:        ftp.uni-paderborn.de           /pub/FAQ
191                   ftp.Germany.EU.net             /pub/newsarchive/news.answers
192                   ftp.sunet.se                   /pub/usenet
193    Asia:          nctuccca.edu.tw                /USENET/FAQ
194                   hwarang.postech.ac.kr          /pub/usenet/news.answers
195                   ftp.hk.super.net               /mirror/faqs")
196
197 (defvar gnus-group-archive-directory
198   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
199   "*The address of the (ding) archives.")
200
201 (defvar gnus-group-recent-archive-directory
202   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
203   "*The address of the most recent (ding) articles.")
204
205 (defvar gnus-default-subscribed-newsgroups nil
206   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
207 It should be a list of strings.
208 If it is `t', Gnus will not do anything special the first time it is
209 started; it'll just use the normal newsgroups subscription methods.")
210
211 (defvar gnus-use-cross-reference t
212   "*Non-nil means that cross referenced articles will be marked as read.
213 If nil, ignore cross references.  If t, mark articles as read in
214 subscribed newsgroups. If neither t nor nil, mark as read in all
215 newsgroups.") 
216
217 (defvar gnus-use-dribble-file t
218   "*Non-nil means that Gnus will use a dribble file to store user updates.
219 If Emacs should crash without saving the .newsrc files, complete
220 information can be restored from the dribble file.")
221
222 (defvar gnus-asynchronous nil
223   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
224
225 (defvar gnus-asynchronous-article-function nil
226   "*Function for picking articles to pre-fetch, possibly.")
227
228 (defvar gnus-score-file-single-match-alist nil
229   "*Alist mapping regexps to lists of score files.
230 Each element of this alist should be of the form
231         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
232
233 If the name of a group is matched by REGEXP, the corresponding scorefiles
234 will be used for that group.
235 The first match found is used, subsequent matching entries are ignored (to
236 use multiple matches, see gnus-score-file-multiple-match-alist).
237
238 These score files are loaded in addition to any files returned by
239 gnus-score-find-score-files-function (which see).")
240
241 (defvar gnus-score-file-multiple-match-alist nil
242   "*Alist mapping regexps to lists of score files.
243 Each element of this alist should be of the form
244         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
245
246 If the name of a group is matched by REGEXP, the corresponding scorefiles
247 will be used for that group.
248 If multiple REGEXPs match a group, the score files corresponding to each
249 match will be used (for only one match to be used, see
250 gnus-score-file-single-match-alist).
251
252 These score files are loaded in addition to any files returned by
253 gnus-score-find-score-files-function (which see).")
254
255
256 (defvar gnus-score-file-suffix "SCORE"
257   "*Suffix of the score files.")
258
259 (defvar gnus-adaptive-file-suffix "ADAPT"
260   "*Suffix of the adaptive score files.")
261
262 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
263   "*Function used to find score files.
264 The function will be called with the group name as the argument, and
265 should return a list of score files to apply to that group.  The score
266 files do not actually have to exist.
267
268 Predefined values are:
269
270 gnus-score-find-single: Only apply the group's own score file.
271 gnus-score-find-hierarchical: Also apply score files from parent groups.
272 gnus-score-find-bnews: Apply score files whose names matches.
273
274 See the documentation to these functions for more information.
275
276 This variable can also be a list of functions to be called.  Each
277 function should either return a list of score files, or a list of
278 score alists.")
279
280 (defvar gnus-score-interactive-default-score 1000
281   "*Scoring commands will raise/lower the score with this number as the default.")
282
283 (defvar gnus-large-newsgroup 200
284   "*The number of articles which indicates a large newsgroup.
285 If the number of articles in a newsgroup is greater than this value,
286 confirmation is required for selecting the newsgroup.")
287
288 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
289 (defvar gnus-no-groups-message "No news is horrible news"
290   "*Message displayed by Gnus when no groups are available.")
291
292 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
293   "*Non-nil means that the default name of a file to save articles in is the group name.
294 If it's nil, the directory form of the group name is used instead.
295
296 If this variable is a list, and the list contains the element
297 `not-score', long file names will not be used for score files; if it
298 contains the element `not-save', long file names will not be used for
299 saving; and if it contains the element `not-kill', long file names
300 will not be used for kill files.")
301
302 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
303   "*Name of the directory articles will be saved in (default \"~/News\").
304 Initialized from the SAVEDIR environment variable.")
305
306 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
307   "*Name of the directory where kill files will be stored (default \"~/News\").
308 Initialized from the SAVEDIR environment variable.")
309
310 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
311   "*A function to save articles in your favorite format.
312 The function must be interactively callable (in other words, it must
313 be an Emacs command).
314
315 Gnus provides the following functions:
316
317 * gnus-summary-save-in-rmail (Rmail format)
318 * gnus-summary-save-in-mail (Unix mail format)
319 * gnus-summary-save-in-folder (MH folder)
320 * gnus-summary-save-in-file (article format).
321 * gnus-summary-save-in-vm (use VM's folder format).")
322
323 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
324   "*A function generating a file name to save articles in Rmail format.
325 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
326
327 (defvar gnus-mail-save-name (function gnus-plain-save-name)
328   "*A function generating a file name to save articles in Unix mail format.
329 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
330
331 (defvar gnus-folder-save-name (function gnus-folder-save-name)
332   "*A function generating a file name to save articles in MH folder.
333 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
334
335 (defvar gnus-file-save-name (function gnus-numeric-save-name)
336   "*A function generating a file name to save articles in article format.
337 The function is called with NEWSGROUP, HEADERS, and optional
338 LAST-FILE.")
339
340 (defvar gnus-split-methods nil
341   "*Variable used to suggest where articles are to be saved.
342 The syntax of this variable is the same as `nnmail-split-methods'.  
343
344 For instance, if you would like to save articles related to Gnus in
345 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
346 you could set this variable to something like:
347
348  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
349    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
350
351 (defvar gnus-save-score nil
352   "*If non-nil, save group scoring info.")
353
354 (defvar gnus-use-adaptive-scoring nil
355   "*If non-nil, use some adaptive scoring scheme.")
356
357 (defvar gnus-use-cache nil
358   "*If non-nil, Gnus will cache (some) articles locally.")
359
360 (defvar gnus-use-scoring t
361   "*If non-nil, enable scoring.")
362
363 (defvar gnus-fetch-old-headers nil
364   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
365 If an unread article in the group refers to an older, already read (or
366 just marked as read) article, the old article will not normally be
367 displayed in the Summary buffer.  If this variable is non-nil, Gnus
368 will attempt to grab the headers to the old articles, and thereby
369 build complete threads.  If it has the value `some', only enough
370 headers to connect otherwise loose threads will be displayed.
371
372 The server has to support XOVER for any of this to work.")
373
374 ;see gnus-cus.el
375 ;(defvar gnus-visual t
376 ;  "*If non-nil, will do various highlighting.
377 ;If nil, no mouse highlights (or any other highlights) will be
378 ;performed.  This might speed up Gnus some when generating large group
379 ;and summary buffers.")
380
381 (defvar gnus-novice-user t
382   "*Non-nil means that you are a usenet novice.
383 If non-nil, verbose messages may be displayed and confirmations may be
384 required.")
385
386 (defvar gnus-expert-user nil
387   "*Non-nil means that you will never be asked for confirmation about anything.
388 And that means *anything*.")
389
390 (defvar gnus-verbose 7
391   "*Integer that says how verbose Gnus should be.
392 The higher the number, the more messages Gnus will flash to say what
393 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
394 display most important messages; and at ten, Gnus will keep on
395 jabbering all the time.")
396
397 (defvar gnus-keep-same-level nil
398   "*Non-nil means that the next newsgroup after the current will be on the same level.
399 When you type, for instance, `n' after reading the last article in the
400 current newsgroup, you will go to the next newsgroup. If this variable
401 is nil, the next newsgroup will be the next from the group
402 buffer. 
403 If this variable is non-nil, Gnus will either put you in the
404 next newsgroup with the same level, or, if no such newsgroup is
405 available, the next newsgroup with the lowest possible level higher
406 than the current level.
407 If this variable is `best', Gnus will make the next newsgroup the one
408 with the best level.")
409
410 (defvar gnus-summary-make-false-root 'adopt
411   "*nil means that Gnus won't gather loose threads.
412 If the root of a thread has expired or been read in a previous
413 session, the information necessary to build a complete thread has been
414 lost. Instead of having many small sub-threads from this original thread
415 scattered all over the summary buffer, Gnus can gather them. 
416
417 If non-nil, Gnus will try to gather all loose sub-threads from an
418 original thread into one large thread.
419
420 If this variable is non-nil, it should be one of `none', `adopt',
421 `dummy' or `empty'.
422
423 If this variable is `none', Gnus will not make a false root, but just
424 present the sub-threads after another.
425 If this variable is `dummy', Gnus will create a dummy root that will
426 have all the sub-threads as children.
427 If this variable is `adopt', Gnus will make one of the \"children\"
428 the parent and mark all the step-children as such.
429 If this variable is `empty', the \"children\" are printed with empty
430 subject fields.  (Or rather, they will be printed with a string
431 given by the `gnus-summary-same-subject' variable.)")
432
433 (defvar gnus-summary-gather-subject-limit nil
434   "*Maximum length of subject comparisons when gathering loose threads.
435 Use nil to compare full subjects.  Setting this variable to a low
436 number will help gather threads that have been corrupted by
437 newsreaders chopping off subject lines, but it might also mean that
438 unrelated articles that have subject that happen to begin with the
439 same few characters will be incorrectly gathered.
440
441 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
442 comparing subjects.")
443
444 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
445 (defvar gnus-summary-same-subject ""
446   "*String indicating that the current article has the same subject as the previous.
447 This variable will only be used if the value of
448 `gnus-summary-make-false-root' is `empty'.")
449
450 (defvar gnus-summary-goto-unread t
451   "*If non-nil, marking commands will go to the next unread article.")
452
453 (defvar gnus-group-goto-unread t
454   "*If non-nil, movement commands will go to the next unread and subscribed group.")
455
456 (defvar gnus-check-new-newsgroups t
457   "*Non-nil means that Gnus will add new newsgroups at startup.
458 If this variable is `ask-server', Gnus will ask the server for new
459 groups since the last time it checked. This means that the killed list
460 is no longer necessary, so you could set `gnus-save-killed-list' to
461 nil. 
462
463 A variant is to have this variable be a list of select methods. Gnus
464 will then use the `ask-server' method on all these select methods to
465 query for new groups from all those servers.
466
467 Eg.
468   (setq gnus-check-new-newsgroups 
469         '((nntp \"some.server\") (nntp \"other.server\")))
470
471 If this variable is nil, then you have to tell Gnus explicitly to
472 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
473
474 (defvar gnus-check-bogus-newsgroups nil
475   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
476 If this variable is nil, then you have to tell Gnus explicitly to
477 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
478
479 (defvar gnus-read-active-file t
480   "*Non-nil means that Gnus will read the entire active file at startup.
481 If this variable is nil, Gnus will only know about the groups in your
482 `.newsrc' file.
483
484 If this variable is `some', Gnus will try to only read the relevant
485 parts of the active file from the server.  Not all servers support
486 this, and it might be quite slow with other servers, but this should
487 generally be faster than both the t and nil value.
488
489 If you set this variable to nil or `some', you probably still want to
490 be told about new newsgroups that arrive.  To do that, set
491 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
492 properly with all servers.")
493
494 (defvar gnus-level-subscribed 5
495   "*Groups with levels less than or equal to this variable are subscribed.")
496
497 (defvar gnus-level-unsubscribed 7
498   "*Groups with levels less than or equal to this variable are unsubscribed.
499 Groups with levels less than `gnus-level-subscribed', which should be
500 less than this variable, are subscribed.")
501
502 (defvar gnus-level-zombie 8
503   "*Groups with this level are zombie groups.")
504
505 (defvar gnus-level-killed 9
506   "*Groups with this level are killed.")
507
508 (defvar gnus-level-default-subscribed 3
509   "*New subscribed groups will be subscribed at this level.")
510
511 (defvar gnus-level-default-unsubscribed 6
512   "*New unsubscribed groups will be unsubscribed at this level.")
513
514 (defvar gnus-activate-foreign-newsgroups 4
515   "*If nil, Gnus will not check foreign newsgroups at startup.
516 If it is non-nil, it should be a number between one and nine. Foreign
517 newsgroups that have a level lower or equal to this number will be
518 activated on startup. For instance, if you want to active all
519 subscribed newsgroups, but not the rest, you'd set this variable to 
520 `gnus-level-subscribed'.
521
522 If you subscribe to lots of newsgroups from different servers, startup
523 might take a while. By setting this variable to nil, you'll save time,
524 but you won't be told how many unread articles there are in the
525 groups.")
526
527 (defvar gnus-save-newsrc-file t
528   "*Non-nil means that Gnus will save the `.newsrc' file.
529 Gnus always saves its own startup file, which is called
530 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
531 be readily understood by other newsreaders.  If you don't plan on
532 using other newsreaders, set this variable to nil to save some time on
533 exit.")
534
535 (defvar gnus-save-killed-list t
536   "*If non-nil, save the list of killed groups to the startup file.
537 This will save both time (when starting and quitting) and space (both
538 memory and disk), but it will also mean that Gnus has no record of
539 which groups are new and which are old, so the automatic new
540 newsgroups subscription methods become meaningless. You should always
541 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
542 variable to nil.")
543
544 (defvar gnus-interactive-catchup t
545   "*If non-nil, require your confirmation when catching up a group.")
546
547 (defvar gnus-interactive-post t
548   "*If non-nil, group name will be asked for when posting.")
549
550 (defvar gnus-interactive-exit t
551   "*If non-nil, require your confirmation when exiting Gnus.")
552
553 (defvar gnus-kill-killed t
554   "*If non-nil, Gnus will apply kill files to already killed articles.
555 If it is nil, Gnus will never apply kill files to articles that have
556 already been through the scoring process, which might very well save lots
557 of time.")
558
559 (defvar gnus-extract-address-components 'gnus-extract-address-components
560   "*Function for extracting address components from a From header.
561 Two pre-defined function exist: `gnus-extract-address-components',
562 which is the default, quite fast, and too simplistic solution, and
563 `mail-extract-address-components', which works much better, but is
564 slower.")
565
566 (defvar gnus-summary-default-score 0
567   "*Default article score level.
568 If this variable is nil, scoring will be disabled.")
569
570 (defvar gnus-summary-zcore-fuzz 0
571   "*Fuzziness factor for the zcore in the summary buffer.
572 Articles with scores closer than this to `gnus-summary-default-score'
573 will not be marked.")
574
575 (defvar gnus-simplify-subject-fuzzy-regexp nil
576   "*Regular expression that will be removed from subject strings if
577 fuzzy subject simplification is selected.")
578
579 (defvar gnus-group-default-list-level gnus-level-subscribed
580   "*Default listing level. 
581 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
582
583 (defvar gnus-group-use-permanent-levels nil
584   "*If non-nil, once you set a level, Gnus will use this level.")
585
586 (defvar gnus-show-mime nil
587   "*If non-nil, do mime processing of articles.
588 The articles will simply be fed to the function given by
589 `gnus-show-mime-method'.")
590
591 (defvar gnus-strict-mime t
592   "*If nil, decode MIME header even if there is not Mime-Version field.")
593  
594 (defvar gnus-show-mime-method (function metamail-buffer)
595   "*Function to process a MIME message.
596 The function is called from the article buffer.")
597
598 (defvar gnus-show-threads t
599   "*If non-nil, display threads in summary mode.")
600
601 (defvar gnus-thread-hide-subtree nil
602   "*If non-nil, hide all threads initially.
603 If threads are hidden, you have to run the command
604 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
605 to expose hidden threads.")
606
607 (defvar gnus-thread-hide-killed t
608   "*If non-nil, hide killed threads automatically.")
609
610 (defvar gnus-thread-ignore-subject nil
611   "*If non-nil, ignore subjects and do all threading based on the Reference header.
612 If nil, which is the default, articles that have different subjects
613 from their parents will start separate threads.")
614
615 (defvar gnus-thread-indent-level 4
616   "*Number that says how much each sub-thread should be indented.")
617
618 (defvar gnus-ignored-newsgroups 
619   (purecopy (mapconcat 'identity
620                        '("^to\\."       ; not "real" groups
621                          "^[0-9. \t]+ " ; all digits in name
622                          "[][\"#'()]"   ; bogus characters
623                          )
624                        "\\|"))
625   "*A regexp to match uninteresting newsgroups in the active file.
626 Any lines in the active file matching this regular expression are
627 removed from the newsgroup list before anything else is done to it,
628 thus making them effectively non-existent.")
629
630 (defvar gnus-ignored-headers
631   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
632   "*All headers that match this regexp will be hidden.
633 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
634
635 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
636   "*All headers that do not match this regexp will be hidden.
637 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
638
639 (defvar gnus-sorted-header-list
640   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
641     "^Cc:" "^Date:" "^Organization:")
642   "*This variable is a list of regular expressions.
643 If it is non-nil, headers that match the regular expressions will
644 be placed first in the article buffer in the sequence specified by
645 this list.")
646
647 (defvar gnus-show-all-headers nil
648   "*If non-nil, don't hide any headers.")
649
650 (defvar gnus-save-all-headers t
651   "*If non-nil, don't remove any headers before saving.")
652
653 (defvar gnus-inhibit-startup-message nil
654   "*If non-nil, the startup message will not be displayed.")
655
656 (defvar gnus-signature-separator "^-- *$"
657   "Regexp matching signature separator.")
658
659 (defvar gnus-auto-extend-newsgroup t
660   "*If non-nil, extend newsgroup forward and backward when requested.")
661
662 (defvar gnus-auto-select-first t
663   "*If non-nil, select the first unread article when entering a group.
664 If you want to prevent automatic selection of the first unread article
665 in some newsgroups, set the variable to nil in
666 `gnus-select-group-hook'.") 
667
668 (defvar gnus-auto-select-next t
669   "*If non-nil, offer to go to the next group from the end of the previous.
670 If the value is t and the next newsgroup is empty, Gnus will exit
671 summary mode and go back to group mode.  If the value is neither nil
672 nor t, Gnus will select the following unread newsgroup.  In
673 particular, if the value is the symbol `quietly', the next unread
674 newsgroup will be selected without any confirmations.")
675
676 (defvar gnus-auto-select-same nil
677   "*If non-nil, select the next article with the same subject.")
678
679 (defvar gnus-summary-check-current nil
680   "*If non-nil, consider the current article when moving.
681 The \"unread\" movement commands will stay on the same line if the
682 current article is unread.")
683
684 (defvar gnus-auto-center-summary t
685   "*If non-nil, always center the current summary buffer.")
686
687 (defvar gnus-break-pages t
688   "*If non-nil, do page breaking on articles.
689 The page delimiter is specified by the `gnus-page-delimiter'
690 variable.")
691
692 (defvar gnus-page-delimiter "^\^L"
693   "*Regexp describing what to use as article page delimiters.
694 The default value is \"^\^L\", which is a form linefeed at the
695 beginning of a line.")
696
697 (defvar gnus-use-full-window t
698   "*If non-nil, use the entire Emacs screen.")
699
700 (defvar gnus-window-configuration nil
701   "Obsolete variable.  See `gnus-buffer-configuration'.")
702
703 (defvar gnus-buffer-configuration
704   '((group ([group 1.0 point] 
705             (if gnus-carpal [group-carpal 4])))
706     (summary ([summary 1.0 point]
707               (if gnus-carpal [summary-carpal 4])))
708     (article ([summary 0.25 point] 
709               (if gnus-carpal [summary-carpal 4]) 
710               [article 1.0]))
711     (server ([server 1.0 point]
712              (if gnus-carpal [server-carpal 2])))
713     (browse ([browse 1.0 point]
714              (if gnus-carpal [browse-carpal 2])))
715     (group-mail ([mail 1.0 point]))
716     (summary-mail ([mail 1.0 point]))
717     (summary-reply ([article 0.5]
718                     [mail 1.0 point]))
719     (info ([nil 1.0 point]))
720     (summary-faq ([summary 0.25]
721                   [faq 1.0 point]))
722     (edit-group ([group 0.5]
723                  [edit-group 1.0 point]))
724     (edit-server ([server 0.5]
725                   [edit-server 1.0 point]))
726     (edit-score ([summary 0.25]
727                  [edit-score 1.0 point]))
728     (post ([post 1.0 point]))
729     (reply ([article 0.5]
730             [mail 1.0 point]))
731     (mail-forward ([mail 1.0 point]))
732     (post-forward ([post 1.0 point]))
733     (reply-yank ([mail 1.0 point]))
734     (followup ([article 0.5]
735                [post 1.0 point]))
736     (followup-yank ([post 1.0 point])))
737   "Window configuration for all possible Gnus buffers.
738 This variable is a list of lists.  Each of these lists has a NAME and
739 a RULE.  The NAMEs are commonsense names like `group', which names a
740 rule used when displaying the group buffer; `summary', which names a
741 rule for what happens when you enter a group and do not display an
742 article buffer; and so on.  See the value of this variable for a
743 complete list of NAMEs.
744
745 Each RULE is a list of vectors.  The first element in this vector is
746 the name of the buffer to be displayed; the second element is the
747 percentage of the screen this buffer is to occupy (a number in the
748 0.0-0.99 range); the optional third element is `point', which should
749 be present to denote which buffer point is to go to after making this
750 buffer configuration.")
751
752 (defvar gnus-window-to-buffer
753   '((group . gnus-group-buffer)
754     (summary . gnus-summary-buffer)
755     (article . gnus-article-buffer)
756     (server . gnus-server-buffer)
757     (browse . "*Gnus Browse Server*")
758     (edit-group . gnus-group-edit-buffer)
759     (edit-server . gnus-server-edit-buffer)
760     (group-carpal . gnus-carpal-group-buffer)
761     (summary-carpal . gnus-carpal-summary-buffer)
762     (server-carpal . gnus-carpal-server-buffer)
763     (browse-carpal . gnus-carpal-browse-buffer)
764     (edit-score . gnus-score-edit-buffer)
765     (mail . gnus-mail-buffer)
766     (post . gnus-post-news-buffer)
767     (faq . gnus-faq-buffer))
768   "Mapping from short symbols to buffer names or buffer variables.")
769
770 (defvar gnus-carpal nil
771   "*If non-nil, display clickable icons.")
772
773 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
774   "*Function called with a group name when new group is detected.
775 A few pre-made functions are supplied: `gnus-subscribe-randomly'
776 inserts new groups at the beginning of the list of groups;
777 `gnus-subscribe-alphabetically' inserts new groups in strict
778 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
779 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
780 for your decision.")
781
782 ;; Suggested by a bug report by Hallvard B Furuseth.
783 ;; <h.b.furuseth@usit.uio.no>. 
784 (defvar gnus-subscribe-options-newsgroup-method
785   (function gnus-subscribe-alphabetically)
786   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
787 If, for instance, you want to subscribe to all newsgroups in the
788 \"no\" and \"alt\" hierarchies, you'd put the following in your
789 .newsrc file:
790
791 options -n no.all alt.all
792
793 Gnus will the subscribe all new newsgroups in these hierarchies with
794 the subscription method in this variable.")
795
796 (defvar gnus-subscribe-hierarchical-interactive nil
797   "*If non-nil, Gnus will offer to subscribe hierarchically.
798 When a new hierarchy appears, Gnus will ask the user:
799
800 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
801
802 If the user pressed `d', Gnus will descend the hierarchy, `y' will
803 subscribe to all newsgroups in the hierarchy and `s' will skip this
804 hierarchy in its entirety.")
805
806 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
807   "*Function used for sorting the group buffer.
808 This function will be called with group info entries as the arguments
809 for the groups to be sorted.  Pre-made functions include
810 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
811 `gnus-group-sort-by-level'")
812
813 ;; Mark variables suggested by Thomas Michanek
814 ;; <Thomas.Michanek@telelogic.se>. 
815 (defvar gnus-unread-mark ? 
816   "*Mark used for unread articles.")
817 (defvar gnus-ticked-mark ?!
818   "*Mark used for ticked articles.")
819 (defvar gnus-dormant-mark ??
820   "*Mark used for dormant articles.")
821 (defvar gnus-del-mark ?r
822   "*Mark used for del'd articles.")
823 (defvar gnus-read-mark ?R
824   "*Mark used for read articles.")
825 (defvar gnus-expirable-mark ?E
826   "*Mark used for expirable articles.")
827 (defvar gnus-killed-mark ?K
828   "*Mark used for killed articles.")
829 (defvar gnus-kill-file-mark ?X
830   "*Mark used for articles killed by kill files.")
831 (defvar gnus-low-score-mark ?Y
832   "*Mark used for articles with a low score.")
833 (defvar gnus-catchup-mark ?C
834   "*Mark used for articles that are caught up.")
835 (defvar gnus-replied-mark ?A
836   "*Mark used for articles that have been replied to.")
837 (defvar gnus-process-mark ?# 
838   "*Process mark.")
839 (defvar gnus-ancient-mark ?O
840   "*Mark used for ancient articles.")
841 (defvar gnus-canceled-mark ?G
842   "*Mark used for canceled articles.")
843 (defvar gnus-score-over-mark ?+
844   "*Score mark used for articles with high scores.")
845 (defvar gnus-score-below-mark ?-
846   "*Score mark used for articles with low scores.")
847 (defvar gnus-empty-thread-mark ? 
848   "*There is no thread under the article.")
849 (defvar gnus-not-empty-thread-mark ?=
850   "*There is a thread under the article.")
851 (defvar gnus-dummy-mark ?Z
852   "*This is a dummy article.")
853
854 (defvar gnus-view-pseudo-asynchronously nil
855   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
856
857 (defvar gnus-view-pseudos nil
858   "*If `automatic', pseudo-articles will be viewed automatically.
859 If `not-confirm', pseudos will be viewed automatically, and the user
860 will not be asked to confirm the command.")
861
862 (defvar gnus-view-pseudos-separately t
863   "*If non-nil, one pseudo-article will be created for each file to be viewed.
864 If nil, all files that use the same viewing command will be given as a
865 list of parameters to that command.")
866
867 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
868   "*Format of group lines.
869 It works along the same lines as a normal formatting string,
870 with some simple extensions.
871
872 %M    Only marked articles (character, \"*\" or \" \")
873 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
874 %L    Level of subscribedness (integer)
875 %N    Number of unread articles (integer)
876 %I    Number of dormant articles (integer)
877 %i    Number of ticked and dormant (integer)
878 %T    Number of ticked articles (integer)
879 %R    Number of read articles (integer)
880 %t    Total number of articles (integer)
881 %y    Number of unread, unticked articles (integer)
882 %G    Group name (string)
883 %g    Qualified group name (string)
884 %D    Group description (string)
885 %s    Select method (string)
886 %o    Moderated group (char, \"m\")
887 %p    Process mark (char)
888 %O    Moderated group (string, \"(m)\" or \"\")
889 %n    Select from where (string)
890 %z    A string that look like `<%s:%n>' if a foreign select method is used
891 %u    User defined specifier. The next character in the format string should
892       be a letter.  Gnus will call the function gnus-user-format-function-X,
893       where X is the letter following %u. The function will be passed the
894       current header as argument. The function should return a string, which
895       will be inserted into the buffer just like information from any other
896       group specifier.
897
898 Text between %( and %) will be highlighted with `gnus-mouse-face' when
899 the mouse point move inside the area.  There can only be one such area.
900
901 Note that this format specification is not always respected. For
902 reasons of efficiency, when listing killed groups, this specification
903 is ignored altogether. If the spec is changed considerably, your
904 output may end up looking strange when listing both alive and killed
905 groups.
906
907 If you use %o or %O, reading the active file will be slower and quite
908 a bit of extra memory will be used. %D will also worsen performance.
909 Also note that if you change the format specification to include any
910 of these specs, you must probably re-start Gnus to see them go into
911 effect.") 
912
913 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
914   "*The format specification of the lines in the summary buffer.
915
916 It works along the same lines as a normal formatting string,
917 with some simple extensions.
918
919 %N   Article number, left padded with spaces (string)
920 %S   Subject (string)
921 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
922 %n   Name of the poster (string)
923 %a   Extracted name of the poster (string)
924 %A   Extracted address of the poster (string)
925 %F   Contents of the From: header (string)
926 %x   Contents of the Xref: header (string)
927 %D   Date of the article (string)
928 %d   Date of the article (string) in DD-MMM format
929 %M   Message-id of the article (string)
930 %r   References of the article (string)
931 %c   Number of characters in the article (integer)
932 %L   Number of lines in the article (integer)
933 %I   Indentation based on thread level (a string of spaces)
934 %T   A string with two possible values: 80 spaces if the article
935      is on thread level two or larger and 0 spaces on level one
936 %R   \"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
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.8"
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")
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          (string-match (concat "<" (regexp-quote address) ">") from)
1866          (and (setq name (substring from 0 (1- (match-beginning 0))))
1867               ;; Strip any quotes from the name.
1868               (string-match "\".*\"" name)
1869               (setq name (substring name 1 (1- (match-end 0))))))
1870     ;; If not, then "address (name)" is used.
1871     (or name
1872         (and (string-match "(.+)" from)
1873              (setq name (substring from (1+ (match-beginning 0)) 
1874                                    (1- (match-end 0)))))
1875         (and (string-match "()" from)
1876              (setq name address))
1877         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1878         ;; XOVER might not support folded From headers.
1879         (and (string-match "(.*" from)
1880              (setq name (substring from (1+ (match-beginning 0)) 
1881                                    (match-end 0)))))
1882     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1883     (list (or name from) (or address from))))
1884
1885 (defun gnus-fetch-field (field)
1886   "Return the value of the header FIELD of current article."
1887   (save-excursion
1888     (save-restriction
1889       (let ((case-fold-search t))
1890         (gnus-narrow-to-headers)
1891         (mail-fetch-field field)))))
1892
1893 (defun gnus-goto-colon ()
1894   (beginning-of-line)
1895   (search-forward ":" (gnus-point-at-eol) t))
1896
1897 (defun gnus-narrow-to-headers ()
1898   (widen)
1899   (save-excursion
1900     (narrow-to-region
1901      (goto-char (point-min))
1902      (if (search-forward "\n\n" nil t)
1903          (1- (point))
1904        (point-max)))))
1905
1906 (defvar gnus-old-specs nil)
1907
1908 (defun gnus-update-format-specifications ()
1909   (gnus-make-thread-indent-array)
1910
1911   (let ((formats '(summary summary-dummy group 
1912                            summary-mode group-mode article-mode))
1913         old-format new-format)
1914     (while formats
1915       (setq new-format (symbol-value
1916                         (intern (format "gnus-%s-line-format" (car formats)))))
1917       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1918                (equal old-format new-format))
1919           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1920                (gnus-parse-format
1921                 new-format
1922                 (symbol-value 
1923                  (intern (format "gnus-%s-line-format-alist"
1924                                  (if (eq (car formats) 'article-mode)
1925                                      'summary-mode (car formats))))))))
1926       (setq gnus-old-specs (cons (cons (car formats) new-format)
1927                                  (delq (car formats) gnus-old-specs)))
1928       (setq formats (cdr formats))))
1929       
1930   (gnus-update-group-mark-positions)
1931   (gnus-update-summary-mark-positions)
1932
1933   (if (and (string-match "%D" gnus-group-line-format)
1934            (not gnus-description-hashtb)
1935            gnus-read-active-file)
1936       (gnus-read-all-descriptions-files)))
1937
1938 (defun gnus-update-summary-mark-positions ()
1939   (save-excursion
1940     (let ((gnus-replied-mark 129)
1941           (gnus-score-below-mark 130)
1942           (gnus-score-over-mark 130)
1943           (thread nil)
1944           pos)
1945       (gnus-set-work-buffer)
1946       (gnus-summary-insert-line 
1947        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1948       (goto-char (point-min))
1949       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1950                                          (- (point) 2)))))
1951       (goto-char (point-min))
1952       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1953                                           (- (point) 2))) pos))
1954       (goto-char (point-min))
1955       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1956                                         (- (point) 2))) pos))
1957       (setq gnus-summary-mark-positions pos))))
1958
1959 (defun gnus-update-group-mark-positions ()
1960   (save-excursion
1961     (let ((gnus-process-mark 128)
1962           (gnus-group-marked '("dummy.group")))
1963       (gnus-sethash "dummy.group" '(0 . 0) gnus-active-hashtb)
1964       (gnus-set-work-buffer)
1965       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1966       (goto-char (point-min))
1967       (setq gnus-group-mark-positions
1968             (list (cons 'process (and (search-forward "\200" nil t)
1969                                       (- (point) 2))))))))
1970
1971 (defun gnus-mouse-face-function (form)
1972   (` (let ((string (, form)))
1973        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1974        string)))
1975
1976 (defun gnus-max-width-function (el max-width)
1977   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1978   (` (let* ((val (eval (, el)))
1979             (valstr (if (numberp val)
1980                         (int-to-string val) val)))
1981        (if (> (length valstr) (, max-width))
1982            (substring valstr 0 (, max-width))
1983          valstr))))
1984
1985 (defun gnus-parse-format (format spec-alist)
1986   ;; This function parses the FORMAT string with the help of the
1987   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1988   ;; string.  If the FORMAT string contains the specifiers %( and %)
1989   ;; the text between them will have the mouse-face text property.
1990   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1991       (if (and gnus-visual gnus-mouse-face)
1992           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1993                 (button (substring format (match-beginning 2) (match-end 2)))
1994                 (post (substring format (match-beginning 3) (match-end 3))))
1995             (list 'concat
1996                   (gnus-parse-simple-format pre spec-alist)
1997                   (gnus-mouse-face-function 
1998                    (gnus-parse-simple-format button spec-alist))
1999                   (gnus-parse-simple-format post spec-alist)))
2000         (gnus-parse-simple-format
2001          (concat (substring format (match-beginning 1) (match-end 1))
2002                  (substring format (match-beginning 2) (match-end 2))
2003                  (substring format (match-beginning 3) (match-end 3)))
2004          spec-alist))
2005     (gnus-parse-simple-format format spec-alist)))
2006
2007 (defun gnus-parse-simple-format (format spec-alist)
2008   ;; This function parses the FORMAT string with the help of the
2009   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2010   ;; string. The list will consist of the symbol `format', a format
2011   ;; specification string, and a list of forms depending on the
2012   ;; SPEC-ALIST.
2013   (let ((max-width 0)
2014         spec flist fstring newspec elem beg)
2015     (save-excursion
2016       (gnus-set-work-buffer)
2017       (insert format)
2018       (goto-char (point-min))
2019       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
2020         (setq spec (string-to-char (buffer-substring (match-beginning 2)
2021                                                      (match-end 2))))
2022         ;; First check if there are any specs that look anything like
2023         ;; "%12,12A", ie. with a "max width specification". These have
2024         ;; to be treated specially.
2025         (if (setq beg (match-beginning 1))
2026             (setq max-width 
2027                   (string-to-int 
2028                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
2029           (setq max-width 0)
2030           (setq beg (match-beginning 2)))
2031         ;; Find the specification from `spec-alist'.
2032         (if (not (setq elem (cdr (assq spec spec-alist))))
2033             (setq elem '("*" ?s)))
2034         ;; Treat user defined format specifiers specially
2035         (and (eq (car elem) 'user-defined)
2036              (setq elem
2037                    (list 
2038                     (list (intern (concat "gnus-user-format-function-"
2039                                           (buffer-substring
2040                                            (match-beginning 3)
2041                                            (match-end 3))))
2042                           'header)
2043                     ?s))
2044              (delete-region (match-beginning 3) (match-end 3)))
2045         (if (not (zerop max-width))
2046             (let ((el (car elem)))
2047               (cond ((= (car (cdr elem)) ?c) 
2048                      (setq el (list 'char-to-string el)))
2049                     ((= (car (cdr elem)) ?d)
2050                      (numberp el) (setq el (list 'int-to-string el))))
2051               (setq flist (cons (gnus-max-width-function el max-width)
2052                                 flist))
2053               (setq newspec ?s))
2054           (setq flist (cons (car elem) flist))
2055           (setq newspec (car (cdr elem))))
2056         ;; Remove the old specification (and possibly a ",12" string).
2057         (delete-region beg (match-end 2))
2058         ;; Insert the new specification.
2059         (goto-char beg)
2060         (insert newspec))
2061       (setq fstring (buffer-substring 1 (point-max))))
2062     (cons 'format (cons fstring (nreverse flist)))))
2063
2064 (defun gnus-set-work-buffer ()
2065   (if (get-buffer gnus-work-buffer)
2066       (progn
2067         (set-buffer gnus-work-buffer)
2068         (erase-buffer))
2069     (set-buffer (get-buffer-create gnus-work-buffer))
2070     (kill-all-local-variables)
2071     (buffer-disable-undo (current-buffer))
2072     (gnus-add-current-to-buffer-list)))
2073
2074 ;; Article file names when saving.
2075
2076 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2077   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2078 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2079 Otherwise, it is like ~/News/news/group/num."
2080   (let ((default
2081           (expand-file-name
2082            (concat (if (gnus-use-long-file-name 'not-save)
2083                        (gnus-capitalize-newsgroup newsgroup)
2084                      (gnus-newsgroup-directory-form newsgroup))
2085                    "/" (int-to-string (mail-header-number headers)))
2086            (or gnus-article-save-directory "~/News"))))
2087     (if (and last-file
2088              (string-equal (file-name-directory default)
2089                            (file-name-directory last-file))
2090              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2091         default
2092       (or last-file default))))
2093
2094 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2095   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2096 If variable `gnus-use-long-file-name' is non-nil, it is
2097 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2098   (let ((default
2099           (expand-file-name
2100            (concat (if (gnus-use-long-file-name 'not-save)
2101                        newsgroup
2102                      (gnus-newsgroup-directory-form newsgroup))
2103                    "/" (int-to-string (mail-header-number headers)))
2104            (or gnus-article-save-directory "~/News"))))
2105     (if (and last-file
2106              (string-equal (file-name-directory default)
2107                            (file-name-directory last-file))
2108              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2109         default
2110       (or last-file default))))
2111
2112 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2113   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2114 If variable `gnus-use-long-file-name' is non-nil, it is
2115 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2116   (or last-file
2117       (expand-file-name
2118        (if (gnus-use-long-file-name 'not-save)
2119            (gnus-capitalize-newsgroup newsgroup)
2120          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2121        (or gnus-article-save-directory "~/News"))))
2122
2123 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2124   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2125 If variable `gnus-use-long-file-name' is non-nil, it is
2126 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2127   (or last-file
2128       (expand-file-name
2129        (if (gnus-use-long-file-name 'not-save)
2130            newsgroup
2131          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2132        (or gnus-article-save-directory "~/News"))))
2133
2134 ;; For subscribing new newsgroup
2135
2136 (defun gnus-subscribe-hierarchical-interactive (groups)
2137   (let ((groups (sort groups 'string<))
2138         prefixes prefix start ans group starts)
2139     (while groups
2140       (setq prefixes (list "^"))
2141       (while (and groups prefixes)
2142         (while (not (string-match (car prefixes) (car groups)))
2143           (setq prefixes (cdr prefixes)))
2144         (setq prefix (car prefixes))
2145         (setq start (1- (length prefix)))
2146         (if (and (string-match "[^\\.]\\." (car groups) start)
2147                  (cdr groups)
2148                  (setq prefix 
2149                        (concat "^" (substring (car groups) 0 (match-end 0))))
2150                  (string-match prefix (car (cdr groups))))
2151             (progn
2152               (setq prefixes (cons prefix prefixes))
2153               (message "Descend hierarchy %s? ([y]nsq): " 
2154                        (substring prefix 1 (1- (length prefix))))
2155               (setq ans (read-char))
2156               (cond ((= ans ?n)
2157                      (while (and groups 
2158                                  (string-match prefix 
2159                                                (setq group (car groups))))
2160                        (setq gnus-killed-list 
2161                              (cons group gnus-killed-list))
2162                        (gnus-sethash group group gnus-killed-hashtb)
2163                        (setq groups (cdr groups)))
2164                      (setq starts (cdr starts)))
2165                     ((= ans ?s)
2166                      (while (and groups 
2167                                  (string-match prefix 
2168                                                (setq group (car groups))))
2169                        (gnus-sethash group group gnus-killed-hashtb)
2170                        (gnus-subscribe-alphabetically (car groups))
2171                        (setq groups (cdr groups)))
2172                      (setq starts (cdr starts)))
2173                     ((= ans ?q)
2174                      (while groups
2175                        (setq group (car groups))
2176                        (setq gnus-killed-list (cons group gnus-killed-list))
2177                        (gnus-sethash group group gnus-killed-hashtb)
2178                        (setq groups (cdr groups))))
2179                     (t nil)))
2180           (message "Subscribe %s? ([n]yq)" (car groups))
2181           (setq ans (read-char))
2182           (setq group (car groups))
2183           (cond ((= ans ?y)
2184                  (gnus-subscribe-alphabetically (car groups))
2185                  (gnus-sethash group group gnus-killed-hashtb))
2186                 ((= ans ?q)
2187                  (while groups
2188                    (setq group (car groups))
2189                    (setq gnus-killed-list (cons group gnus-killed-list))
2190                    (gnus-sethash group group gnus-killed-hashtb)
2191                    (setq groups (cdr groups))))
2192                 (t 
2193                  (setq gnus-killed-list (cons group gnus-killed-list))
2194                  (gnus-sethash group group gnus-killed-hashtb)))
2195           (setq groups (cdr groups)))))))
2196
2197 (defun gnus-subscribe-randomly (newsgroup)
2198   "Subscribe new NEWSGROUP by making it the first newsgroup."
2199   (gnus-subscribe-newsgroup newsgroup))
2200
2201 (defun gnus-subscribe-alphabetically (newgroup)
2202   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2203   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2204   (let ((groups (cdr gnus-newsrc-alist))
2205         before)
2206     (while (and (not before) groups)
2207       (if (string< newgroup (car (car groups)))
2208           (setq before (car (car groups)))
2209         (setq groups (cdr groups))))
2210     (gnus-subscribe-newsgroup newgroup before)))
2211
2212 (defun gnus-subscribe-hierarchically (newgroup)
2213   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2214   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2215   (save-excursion
2216     (set-buffer (find-file-noselect gnus-current-startup-file))
2217     (let ((groupkey newgroup)
2218           before)
2219       (while (and (not before) groupkey)
2220         (goto-char (point-min))
2221         (let ((groupkey-re
2222                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2223           (while (and (re-search-forward groupkey-re nil t)
2224                       (progn
2225                         (setq before (buffer-substring
2226                                       (match-beginning 1) (match-end 1)))
2227                         (string< before newgroup)))))
2228         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2229         (setq groupkey
2230               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2231                   (substring groupkey (match-beginning 1) (match-end 1)))))
2232       (gnus-subscribe-newsgroup newgroup before))))
2233
2234 (defun gnus-subscribe-interactively (newsgroup)
2235   "Subscribe new NEWSGROUP interactively.
2236 It is inserted in hierarchical newsgroup order if subscribed. If not,
2237 it is killed."
2238   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2239       (gnus-subscribe-hierarchically newsgroup)
2240     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2241
2242 (defun gnus-subscribe-zombies (newsgroup)
2243   "Make new NEWSGROUP a zombie group."
2244   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2245
2246 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2247   "Subscribe new NEWSGROUP.
2248 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2249 the first newsgroup."
2250   ;; We subscribe the group by changing its level to `subscribed'.
2251   (gnus-group-change-level 
2252    newsgroup gnus-level-default-subscribed
2253    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2254   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2255
2256 ;; For directories
2257
2258 (defun gnus-newsgroup-directory-form (newsgroup)
2259   "Make hierarchical directory name from NEWSGROUP name."
2260   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2261         (len (length newsgroup))
2262         idx)
2263     ;; If this is a foreign group, we don't want to translate the
2264     ;; entire name.  
2265     (if (setq idx (string-match ":" newsgroup))
2266         (aset newsgroup idx ?/)
2267       (setq idx 0))
2268     ;; Replace all occurrences of `.' with `/'.
2269     (while (< idx len)
2270       (if (= (aref newsgroup idx) ?.)
2271           (aset newsgroup idx ?/))
2272       (setq idx (1+ idx)))
2273     newsgroup))
2274
2275 (defun gnus-newsgroup-saveable-name (group)
2276   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2277   ;; with dots.
2278   (gnus-replace-chars-in-string group ?/ ?.))
2279
2280 (defun gnus-make-directory (dir)
2281   "Make DIRECTORY recursively."
2282   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2283   ;; of the many mysteries of the universe.
2284   (let* ((dir (expand-file-name dir default-directory))
2285          dirs err)
2286     (if (string-match "/$" dir)
2287         (setq dir (substring dir 0 (match-beginning 0))))
2288     ;; First go down the path until we find a directory that exists.
2289     (while (not (file-exists-p dir))
2290       (setq dirs (cons dir dirs))
2291       (string-match "/[^/]+$" dir)
2292       (setq dir (substring dir 0 (match-beginning 0))))
2293     ;; Then create all the subdirs.
2294     (while (and dirs (not err))
2295       (condition-case ()
2296           (make-directory (car dirs))
2297         (error (setq err t)))
2298       (setq dirs (cdr dirs)))
2299     ;; We return whether we were successful or not. 
2300     (not dirs)))
2301
2302 (defun gnus-capitalize-newsgroup (newsgroup)
2303   "Capitalize NEWSGROUP name."
2304   (and (not (zerop (length newsgroup)))
2305        (concat (char-to-string (upcase (aref newsgroup 0)))
2306                (substring newsgroup 1))))
2307
2308 ;; Var
2309
2310 (defun gnus-simplify-subject (subject &optional re-only)
2311   "Remove `Re:' and words in parentheses.
2312 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2313   (let ((case-fold-search t))           ;Ignore case.
2314     ;; Remove `Re:' and `Re^N:'.
2315     (if (string-match "^re:[ \t]*" subject)
2316         (setq subject (substring subject (match-end 0))))
2317     ;; Remove words in parentheses from end.
2318     (or re-only
2319         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2320           (setq subject (substring subject 0 (match-beginning 0)))))
2321     ;; Return subject string.
2322     subject))
2323
2324 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2325 ;; all whitespace.
2326 (defun gnus-simplify-subject-fuzzy (subject)
2327   (let ((case-fold-search t))
2328     (save-excursion
2329       (gnus-set-work-buffer)
2330       (insert subject)
2331       (inline (gnus-simplify-buffer-fuzzy))
2332       (buffer-string))))
2333
2334 (defun gnus-simplify-buffer-fuzzy ()
2335   (goto-char (point-min))
2336   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2337   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2338                             nil t)
2339     (replace-match "" t t))
2340   (goto-char (point-min))
2341   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2342     (replace-match "" t t))
2343   (goto-char (point-min))
2344   (while (re-search-forward "[ \t]+" nil t)
2345     (replace-match " " t t))
2346   (goto-char (point-min))
2347   (while (re-search-forward "[ \t]+$" nil t)
2348     (replace-match "" t t))
2349   (goto-char (point-min))
2350   (while (re-search-forward "^[ \t]+" nil t)
2351     (replace-match "" t t))
2352   (if gnus-simplify-subject-fuzzy-regexp
2353       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2354         (replace-match "" t t))))
2355
2356 ;; Add the current buffer to the list of buffers to be killed on exit. 
2357 (defun gnus-add-current-to-buffer-list ()
2358   (or (memq (current-buffer) gnus-buffer-list)
2359       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2360
2361 (defun gnus-string> (s1 s2)
2362   (not (or (string< s1 s2)
2363            (string= s1 s2))))
2364
2365 ;; Functions accessing headers.
2366 ;; Functions are more convenient than macros in some cases.
2367
2368 (defun gnus-header-number (header)
2369   (mail-header-number header))
2370
2371 (defun gnus-header-subject (header)
2372   (mail-header-subject header))
2373
2374 (defun gnus-header-from (header)
2375   (mail-header-from header))
2376
2377 (defun gnus-header-xref (header)
2378   (mail-header-xref header))
2379
2380 (defun gnus-header-lines (header)
2381   (mail-header-lines header))
2382
2383 (defun gnus-header-date (header)
2384   (mail-header-date header))
2385
2386 (defun gnus-header-id (header)
2387   (mail-header-id header))
2388
2389 (defun gnus-header-message-id (header)
2390   (mail-header-id header))
2391
2392 (defun gnus-header-chars (header)
2393   (mail-header-chars header))
2394
2395 (defun gnus-header-references (header)
2396   (mail-header-references header))
2397
2398 ;;; General various misc type functions.
2399
2400 (defun gnus-clear-system ()
2401   "Clear all variables and buffers."
2402   ;; Clear Gnus variables.
2403   (let ((variables gnus-variable-list))
2404     (while variables
2405       (set (car variables) nil)
2406       (setq variables (cdr variables))))
2407   ;; Clear other internal variables.
2408   (setq gnus-list-of-killed-groups nil
2409         gnus-have-read-active-file nil
2410         gnus-newsrc-alist nil
2411         gnus-newsrc-hashtb nil
2412         gnus-killed-list nil
2413         gnus-zombie-list nil
2414         gnus-killed-hashtb nil
2415         gnus-active-hashtb nil
2416         gnus-moderated-list nil
2417         gnus-description-hashtb nil
2418         gnus-newsgroup-headers nil
2419         gnus-newsgroup-headers-hashtb-by-number nil
2420         gnus-newsgroup-name nil
2421         gnus-server-alist nil
2422         gnus-current-select-method nil)
2423   ;; Reset any score variables.
2424   (and (boundp 'gnus-score-cache)
2425        (set 'gnus-score-cache nil))
2426   (and (boundp 'gnus-internal-global-score-files)
2427        (set 'gnus-internal-global-score-files nil))
2428   ;; Kill the startup file.
2429   (and gnus-current-startup-file
2430        (get-file-buffer gnus-current-startup-file)
2431        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2432   ;; Save any cache buffers.
2433   (and gnus-use-cache (gnus-cache-save-buffers))
2434   ;; Clear the dribble buffer.
2435   (gnus-dribble-clear)
2436   ;; Kill global KILL file buffer.
2437   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2438       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2439   (gnus-kill-buffer nntp-server-buffer)
2440   ;; Kill Gnus buffers.
2441   (while gnus-buffer-list
2442     (gnus-kill-buffer (car gnus-buffer-list))
2443     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2444
2445 (defun gnus-windows-old-to-new (setting)
2446   (if (symbolp setting)
2447       (setq setting 
2448             (cond ((eq setting 'SelectArticle)
2449                    'article)
2450                   ((eq setting 'SelectSubject)
2451                    'summary)
2452                   ((eq setting 'SelectNewsgroup)
2453                    'group)
2454                   (t setting))))
2455   (if (or (listp setting)
2456           (not (and gnus-window-configuration
2457                     (memq setting '(group summary article)))))
2458       setting
2459     (let* ((setting (if (eq setting 'group) 
2460                         (if (assq 'newsgroup gnus-window-configuration)
2461                             'newsgroup
2462                           'newsgroups) setting))
2463            (elem (car (cdr (assq setting gnus-window-configuration))))
2464            (total (apply '+ elem))
2465            (types '(group summary article))
2466            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2467            (i 0)
2468            perc
2469            out)
2470       (while (< i 3)
2471         (or (zerop (nth i elem))
2472             (progn
2473               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2474               (setq out (cons (if (eq pbuf (nth i types))
2475                                   (vector (nth i types) perc 'point)
2476                                 (vector (nth i types) perc))
2477                               out))))
2478         (setq i (1+ i)))
2479       (list (nreverse out)))))
2480            
2481 (defun gnus-add-configuration (conf)
2482   (setq gnus-buffer-configuration 
2483         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2484                          gnus-buffer-configuration))))
2485
2486 (defun gnus-configure-windows (setting &optional force)
2487   (setq setting (gnus-windows-old-to-new setting))
2488   (let ((r (if (symbolp setting)
2489                (cdr (assq setting gnus-buffer-configuration))
2490              setting))
2491         (in-buf (current-buffer))
2492         rule val w height hor ohor heights sub jump-buffer
2493         rel total to-buf all-visible)
2494     (or r (error "No such setting: %s" setting))
2495
2496     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2497         ;; All the windows mentioned are already visible, so we just
2498         ;; put point in the assigned buffer, and do not touch the
2499         ;; winconf. 
2500         (select-window (get-buffer-window all-visible t))
2501          
2502
2503       ;; Either remove all windows or just remove all Gnus windows.
2504       (if gnus-use-full-window
2505           (delete-other-windows)
2506         (gnus-remove-some-windows)
2507         (switch-to-buffer nntp-server-buffer))
2508
2509       (while r
2510         (setq hor (car r)
2511               ohor nil)
2512
2513         ;; We have to do the (possible) horizontal splitting before the
2514         ;; vertical. 
2515         (if (and (listp (car hor)) 
2516                  (eq (car (car hor)) 'horizontal))
2517             (progn
2518               (split-window 
2519                nil
2520                (if (integerp (nth 1 (car hor)))
2521                    (nth 1 (car hor))
2522                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2523                t)
2524               (setq hor (cdr hor))))
2525
2526         ;; Go through the rules and eval the elements that are to be
2527         ;; evaled.  
2528         (while hor
2529           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2530               (progn
2531                 ;; Expand short buffer name.
2532                 (setq w (aref val 0))
2533                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2534                      (progn
2535                        (setq val (apply 'vector (mapcar 'identity val)))
2536                        (aset val 0 w)))
2537                 (setq ohor (cons val ohor))))
2538           (setq hor (cdr hor)))
2539         (setq rule (cons (nreverse ohor) rule))
2540         (setq r (cdr r)))
2541       (setq rule (nreverse rule))
2542
2543       ;; We tally the window sizes.
2544       (setq total (window-height))
2545       (while rule
2546         (setq hor (car rule))
2547         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2548             (setq hor (cdr hor)))
2549         (setq sub 0)
2550         (while hor
2551           (setq rel (aref (car hor) 1)
2552                 heights (cons
2553                          (cond ((and (floatp rel) (= 1.0 rel))
2554                                 'x)
2555                                ((integerp rel)
2556                                 rel)
2557                                (t
2558                                 (max (floor (* total rel)) 4)))
2559                          heights)
2560                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2561                 hor (cdr hor)))
2562         (setq heights (nreverse heights)
2563               hor (car rule))
2564
2565         ;; We then go through these heighs and create windows for them.
2566         (while heights
2567           (setq height (car heights)
2568                 heights (cdr heights))
2569           (and (eq height 'x)
2570                (setq height (- total sub)))
2571           (and heights
2572                (split-window nil height))
2573           (setq to-buf (aref (car hor) 0))
2574           (switch-to-buffer 
2575            (cond ((not to-buf)
2576                   in-buf)
2577                  ((symbolp to-buf)
2578                   (symbol-value (aref (car hor) 0)))
2579                  (t
2580                   (aref (car hor) 0))))
2581           (and (> (length (car hor)) 2)
2582                (eq (aref (car hor) 2) 'point)
2583                (setq jump-buffer (current-buffer)))
2584           (other-window 1)
2585           (setq hor (cdr hor)))
2586       
2587         (setq rule (cdr rule)))
2588
2589       ;; Finally, we pop to the buffer that's supposed to have point. 
2590       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2591
2592       (select-window (get-buffer-window jump-buffer t))
2593       (set-buffer jump-buffer))))
2594
2595 (defun gnus-all-windows-visible-p (rule)
2596   (let (invisible hor jump-buffer val buffer)
2597     ;; Go through the rules and eval the elements that are to be
2598     ;; evaled.  
2599     (while (and rule (not invisible))
2600       (setq hor (car rule)
2601             rule (cdr rule))
2602       (while (and hor (not invisible))
2603         (if (setq val (if (vectorp (car hor)) 
2604                           (car hor)
2605                         (if (not (eq (car (car hor)) 'horizontal))
2606                             (eval (car hor)))))
2607             (progn
2608               ;; Expand short buffer name.
2609               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2610                                (aref val 0)))
2611               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2612                              buffer))
2613               (and (> (length val) 2) (eq 'point (aref val 2))
2614                    (setq jump-buffer buffer))
2615               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2616         (setq hor (cdr hor))))
2617     (and (not invisible) jump-buffer)))
2618
2619 (defun gnus-window-top-edge (&optional window)
2620   (nth 1 (window-edges window)))
2621
2622 (defun gnus-remove-some-windows ()
2623   (let ((buffers gnus-window-to-buffer)
2624         buf bufs lowest-buf lowest)
2625     (save-excursion
2626       ;; Remove windows on all known Gnus buffers.
2627       (while buffers
2628         (setq buf (cdr (car buffers)))
2629         (if (symbolp buf)
2630             (setq buf (and (boundp buf) (symbol-value buf))))
2631         (and buf 
2632              (get-buffer-window buf)
2633              (progn
2634                (setq bufs (cons buf bufs))
2635                (pop-to-buffer buf)
2636                (if (or (not lowest)
2637                        (< (gnus-window-top-edge) lowest))
2638                    (progn
2639                      (setq lowest (gnus-window-top-edge))
2640                      (setq lowest-buf buf)))))
2641         (setq buffers (cdr buffers)))
2642       ;; Remove windows on *all* summary buffers.
2643       (let (wins)
2644         (walk-windows
2645          (lambda (win)
2646            (let ((buf (window-buffer win)))
2647              (if (string-match  "^\\*Summary" (buffer-name buf))
2648                  (progn
2649                    (setq bufs (cons buf bufs))
2650                    (pop-to-buffer buf)
2651                    (if (or (not lowest)
2652                            (< (gnus-window-top-edge) lowest))
2653                        (progn
2654                          (setq lowest-buf buf)
2655                          (setq lowest (gnus-window-top-edge))))))))))
2656       (and lowest-buf 
2657            (progn
2658              (pop-to-buffer lowest-buf)
2659              (switch-to-buffer nntp-server-buffer)))
2660       (while bufs
2661         (and (not (eq (car bufs) lowest-buf))
2662              (delete-windows-on (car bufs)))
2663         (setq bufs (cdr bufs))))))
2664                           
2665 (defun gnus-version ()
2666   "Version numbers of this version of Gnus."
2667   (interactive)
2668   (let ((methods gnus-valid-select-methods)
2669         (mess gnus-version)
2670         meth)
2671     ;; Go through all the legal select methods and add their version
2672     ;; numbers to the total version string. Only the backends that are
2673     ;; currently in use will have their message numbers taken into
2674     ;; consideration. 
2675     (while methods
2676       (setq meth (intern (concat (car (car methods)) "-version")))
2677       (and (boundp meth)
2678            (stringp (symbol-value meth))
2679            (setq mess (concat mess "; " (symbol-value meth))))
2680       (setq methods (cdr methods)))
2681     (gnus-message 2 mess)))
2682
2683 (defun gnus-info-find-node ()
2684   "Find Info documentation of Gnus."
2685   (interactive)
2686   ;; Enlarge info window if needed.
2687   (let ((mode major-mode))
2688     (gnus-configure-windows 'info)
2689     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2690
2691 (defun gnus-overload-functions (&optional overloads)
2692   "Overload functions specified by optional argument OVERLOADS.
2693 If nothing is specified, use the variable gnus-overload-functions."
2694   (let ((defs nil)
2695         (overloads (or overloads gnus-overload-functions)))
2696     (while overloads
2697       (setq defs (car overloads))
2698       (setq overloads (cdr overloads))
2699       ;; Load file before overloading function if necessary.  Make
2700       ;; sure we cannot use `require' always.
2701       (and (not (fboundp (car defs)))
2702            (car (cdr (cdr defs)))
2703            (load (car (cdr (cdr defs))) nil 'nomessage))
2704       (fset (car defs) (car (cdr defs))))))
2705
2706 (defun gnus-replace-chars-in-string (string &rest pairs)
2707   "Replace characters in STRING from FROM to TO."
2708   (let ((string (substring string 0))   ;Copy string.
2709         (len (length string))
2710         (idx 0)
2711         sym to)
2712     (or (zerop (% (length pairs) 2)) 
2713         (error "Odd number of translation pairs"))
2714     (setplist 'sym pairs)
2715     ;; Replace all occurrences of FROM with TO.
2716     (while (< idx len)
2717       (if (setq to (get 'sym (aref string idx)))
2718           (aset string idx to))
2719       (setq idx (1+ idx)))
2720     string))
2721
2722 (defun gnus-days-between (date1 date2)
2723   ;; Return the number of days between date1 and date2.
2724   (- (gnus-day-number date1) (gnus-day-number date2)))
2725
2726 (defun gnus-day-number (date)
2727   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2728                      (timezone-parse-date date))))
2729     (timezone-absolute-from-gregorian 
2730      (nth 1 dat) (nth 2 dat) (car dat))))
2731
2732 ;; Returns a floating point number that says how many seconds have
2733 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2734 (defun gnus-seconds-since-epoch (date)
2735   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2736                         (timezone-parse-date date)))
2737          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2738                         (timezone-parse-time
2739                          (aref (timezone-parse-date date) 3))))
2740          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2741                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2742          (tday (- (timezone-absolute-from-gregorian 
2743                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2744                   (timezone-absolute-from-gregorian 
2745                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2746     (+ (nth 2 ttime)
2747        (* (nth 1 ttime) 60)
2748        (* 1.0 (nth 0 ttime) 60 60)
2749        (* 1.0 tday 60 60 24))))
2750
2751 (defun gnus-file-newer-than (file date)
2752   (let ((fdate (nth 5 (file-attributes file))))
2753     (or (> (car fdate) (car date))
2754         (and (= (car fdate) (car date))
2755              (> (nth 1 fdate) (nth 1 date))))))
2756
2757 (defun gnus-group-read-only-p (&optional group)
2758   "Check whether GROUP supports editing or not.
2759 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
2760 that that variable is buffer-local to the summary buffers."
2761   (let ((group (or group gnus-newsgroup-name)))
2762     (not (gnus-check-backend-function 'request-replace-article group))))
2763
2764 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2765 ;; the echo area.
2766 (defun gnus-y-or-n-p (prompt)
2767   (prog1
2768       (y-or-n-p prompt)
2769     (message "")))
2770
2771 (defun gnus-yes-or-no-p (prompt)
2772   (prog1
2773       (yes-or-no-p prompt)
2774     (message "")))
2775
2776 ;; Check whether to use long file names.
2777 (defun gnus-use-long-file-name (symbol)
2778   ;; The variable has to be set...
2779   (and gnus-use-long-file-name
2780        ;; If it isn't a list, then we return t.
2781        (or (not (listp gnus-use-long-file-name))
2782            ;; If it is a list, and the list contains `symbol', we
2783            ;; return nil.  
2784            (not (memq symbol gnus-use-long-file-name)))))
2785
2786 ;; I suspect there's a better way, but I haven't taken the time to do
2787 ;; it yet. -erik selberg@cs.washington.edu
2788 (defun gnus-dd-mmm (messy-date)
2789   "Return a string like DD-MMM from a big messy string"
2790   (let ((datevec (timezone-parse-date messy-date)))
2791     (format "%2s-%s"
2792             (or (aref datevec 2) "??")
2793             (capitalize
2794              (or (car 
2795                   (nth (1- (string-to-number (aref datevec 1)))
2796                        timezone-months-assoc))
2797                  "???")))))
2798
2799 ;; Make a hash table (default and minimum size is 255).
2800 ;; Optional argument HASHSIZE specifies the table size.
2801 (defun gnus-make-hashtable (&optional hashsize)
2802   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2803
2804 ;; Make a number that is suitable for hashing; bigger than MIN and one
2805 ;; less than 2^x.
2806 (defun gnus-create-hash-size (min)
2807   (let ((i 1))
2808     (while (< i min)
2809       (setq i (* 2 i)))
2810     (1- i)))
2811
2812 ;; Show message if message has a lower level than `gnus-verbose'. 
2813 ;; Guide-line for numbers:
2814 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2815 ;; for things that take a long time, 7 - not very important messages
2816 ;; on stuff, 9 - messages inside loops.
2817 (defun gnus-message (level &rest args)
2818   (if (<= level gnus-verbose)
2819       (apply 'message args)
2820     ;; We have to do this format thingie here even if the result isn't
2821     ;; shown - the return value has to be the same as the return value
2822     ;; from `message'.
2823     (apply 'format args)))
2824
2825 ;; Generate a unique new group name.
2826 (defun gnus-generate-new-group-name (leaf)
2827   (let ((name leaf)
2828         (num 0))
2829     (while (gnus-gethash name gnus-newsrc-hashtb)
2830       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2831     name))
2832
2833 ;;; List and range functions
2834
2835 (defun gnus-last-element (list)
2836   "Return last element of LIST."
2837   (while (cdr list)
2838     (setq list (cdr list)))
2839   (car list))
2840
2841 (defun gnus-copy-sequence (list)
2842   "Do a complete, total copy of a list."
2843   (if (and (consp list) (not (consp (cdr list))))
2844       (cons (car list) (cdr list))
2845     (mapcar (lambda (elem) (if (consp elem) 
2846                                (if (consp (cdr elem))
2847                                    (gnus-copy-sequence elem)
2848                                  (cons (car elem) (cdr elem)))
2849                              elem))
2850             list)))
2851
2852 (defun gnus-set-difference (list1 list2)
2853   "Return a list of elements of LIST1 that do not appear in LIST2."
2854   (let ((list1 (copy-sequence list1)))
2855     (while list2
2856       (setq list1 (delq (car list2) list1))
2857       (setq list2 (cdr list2)))
2858     list1))
2859
2860 (defun gnus-sorted-complement (list1 list2)
2861   "Return a list of elements of LIST1 that do not appear in LIST2.
2862 Both lists have to be sorted over <."
2863   (let (out)
2864     (if (or (null list1) (null list2))
2865         (or list1 list2)
2866       (while (and list1 list2)
2867         (cond ((= (car list1) (car list2))
2868                (setq list1 (cdr list1)
2869                      list2 (cdr list2)))
2870               ((< (car list1) (car list2))
2871                (setq out (cons (car list1) out))
2872                (setq list1 (cdr list1)))
2873               (t
2874                (setq out (cons (car list2) out))
2875                (setq list2 (cdr list2)))))
2876       (nconc (nreverse out) (or list1 list2)))))
2877
2878 (defun gnus-intersection (list1 list2)      
2879   (let ((result nil))
2880     (while list2
2881       (if (memq (car list2) list1)
2882           (setq result (cons (car list2) result)))
2883       (setq list2 (cdr list2)))
2884     result))
2885
2886 (defun gnus-sorted-intersection (list1 list2)
2887   ;; LIST1 and LIST2 have to be sorted over <.
2888   (let (out)
2889     (while (and list1 list2)
2890       (cond ((= (car list1) (car list2))
2891              (setq out (cons (car list1) out)
2892                    list1 (cdr list1)
2893                    list2 (cdr list2)))
2894             ((< (car list1) (car list2))
2895              (setq list1 (cdr list1)))
2896             (t
2897              (setq list2 (cdr list2)))))
2898     (nreverse out)))
2899
2900 (defun gnus-set-sorted-intersection (list1 list2)
2901   ;; LIST1 and LIST2 have to be sorted over <.
2902   ;; This function modifies LIST1.
2903   (let* ((top (cons nil list1))
2904          (prev top))
2905     (while (and list1 list2)
2906       (cond ((= (car list1) (car list2))
2907              (setq prev list1
2908                    list1 (cdr list1)
2909                    list2 (cdr list2)))
2910             ((< (car list1) (car list2))
2911              (setcdr prev (cdr list1))
2912              (setq list1 (cdr list1)))
2913             (t
2914              (setq list2 (cdr list2)))))
2915     (setcdr prev nil)
2916     (cdr top)))
2917
2918 (defun gnus-compress-sequence (numbers &optional always-list)
2919   "Convert list of numbers to a list of ranges or a single range.
2920 If ALWAYS-LIST is non-nil, this function will always release a list of
2921 ranges."
2922   (let* ((first (car numbers))
2923          (last (car numbers))
2924          result)
2925     (if (null numbers)
2926         nil
2927       (if (not (listp (cdr numbers)))
2928           numbers
2929         (while numbers
2930           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2931                 ((= (1+ last) (car numbers)) ;Still in sequence
2932                  (setq last (car numbers)))
2933                 (t                      ;End of one sequence
2934                  (setq result 
2935                        (cons (if (= first last) first
2936                                (cons first last)) result))
2937                  (setq first (car numbers))
2938                  (setq last  (car numbers))))
2939           (setq numbers (cdr numbers)))
2940         (if (and (not always-list) (null result))
2941             (if (= first last) (list first) (cons first last))
2942           (nreverse (cons (if (= first last) first (cons first last))
2943                           result)))))))
2944
2945 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2946 (defun gnus-uncompress-range (ranges)
2947   "Expand a list of ranges into a list of numbers.
2948 RANGES is either a single range on the form `(num . num)' or a list of
2949 these ranges."
2950   (let (first last result)
2951     (cond 
2952      ((null ranges)
2953       nil)
2954      ((not (listp (cdr ranges)))
2955       (setq first (car ranges))
2956       (setq last (cdr ranges))
2957       (while (<= first last)
2958         (setq result (cons first result))
2959         (setq first (1+ first)))
2960       (nreverse result))
2961      (t
2962       (while ranges
2963         (if (atom (car ranges))
2964             (if (numberp (car ranges))
2965                 (setq result (cons (car ranges) result)))
2966           (setq first (car (car ranges)))
2967           (setq last  (cdr (car ranges)))
2968           (while (<= first last)
2969             (setq result (cons first result))
2970             (setq first (1+ first))))
2971         (setq ranges (cdr ranges)))
2972       (nreverse result)))))
2973
2974 (defun gnus-add-to-range (ranges list)
2975   "Return a list of ranges that has all articles from both RANGES and LIST.
2976 Note: LIST has to be sorted over `<'."
2977   (if (not ranges)
2978       (gnus-compress-sequence list t)
2979     (setq list (copy-sequence list))
2980     (or (listp (cdr ranges))
2981         (setq ranges (list ranges)))
2982     (let ((out ranges)
2983           ilist lowest highest temp)
2984       (while (and ranges list)
2985         (setq ilist list)
2986         (setq lowest (or (and (atom (car ranges)) (car ranges))
2987                          (car (car ranges))))
2988         (while (and list (cdr list) (< (car (cdr list)) lowest))
2989           (setq list (cdr list)))
2990         (if (< (car ilist) lowest)
2991             (progn
2992               (setq temp list)
2993               (setq list (cdr list))
2994               (setcdr temp nil)
2995               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2996         (setq highest (or (and (atom (car ranges)) (car ranges))
2997                           (cdr (car ranges))))
2998         (while (and list (<= (car list) highest))
2999           (setq list (cdr list)))
3000         (setq ranges (cdr ranges)))
3001       (if list
3002           (setq out (nconc (gnus-compress-sequence list t) out)))
3003       (setq out (sort out (lambda (r1 r2) 
3004                             (< (or (and (atom r1) r1) (car r1))
3005                                (or (and (atom r2) r2) (car r2))))))
3006       (setq ranges out)
3007       (while ranges
3008         (if (atom (car ranges))
3009             (if (cdr ranges)
3010                 (if (atom (car (cdr ranges)))
3011                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3012                         (progn
3013                           (setcar ranges (cons (car ranges) 
3014                                                (car (cdr ranges))))
3015                           (setcdr ranges (cdr (cdr ranges)))))
3016                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3017                       (progn
3018                         (setcar (car (cdr ranges)) (car ranges))
3019                         (setcar ranges (car (cdr ranges)))
3020                         (setcdr ranges (cdr (cdr ranges)))))))
3021           (if (cdr ranges)
3022               (if (atom (car (cdr ranges)))
3023                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3024                       (progn
3025                         (setcdr (car ranges) (car (cdr ranges)))
3026                         (setcdr ranges (cdr (cdr ranges)))))
3027                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3028                     (progn
3029                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3030                       (setcdr ranges (cdr (cdr ranges))))))))
3031         (setq ranges (cdr ranges)))
3032       out)))
3033
3034 (defun gnus-remove-from-range (ranges list)
3035   "Return a list of ranges that has all articles from LIST removed from RANGES.
3036 Note: LIST has to be sorted over `<'."
3037   ;; !!! This function shouldn't look like this, but I've got a headache.
3038   (gnus-compress-sequence 
3039    (gnus-sorted-complement
3040     (gnus-uncompress-range ranges) list)))
3041
3042 (defun gnus-member-of-range (number ranges)
3043   (if (not (listp (cdr ranges)))
3044       (and (>= number (car ranges)) 
3045            (<= number (cdr ranges)))
3046     (let ((not-stop t))
3047       (while (and ranges 
3048                   (if (numberp (car ranges))
3049                       (>= number (car ranges))
3050                     (>= number (car (car ranges))))
3051                   not-stop)
3052         (if (if (numberp (car ranges))
3053                 (= number (car ranges))
3054               (and (>= number (car (car ranges)))
3055                    (<= number (cdr (car ranges)))))
3056             (setq not-stop nil))
3057         (setq ranges (cdr ranges)))
3058       (not not-stop))))
3059
3060 \f
3061 ;;;
3062 ;;; Gnus group mode
3063 ;;;
3064
3065 (defvar gnus-group-mode-map nil)
3066 (defvar gnus-group-group-map nil)
3067 (defvar gnus-group-mark-map nil)
3068 (defvar gnus-group-list-map nil)
3069 (defvar gnus-group-sub-map nil)
3070 (put 'gnus-group-mode 'mode-class 'special)
3071
3072 (if gnus-group-mode-map
3073     nil
3074   (setq gnus-group-mode-map (make-keymap))
3075   (suppress-keymap gnus-group-mode-map)
3076   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3077   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3078   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3079   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3080   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3081   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3082   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3083   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3084   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3085   (define-key gnus-group-mode-map
3086     "\M-n" 'gnus-group-next-unread-group-same-level)
3087   (define-key gnus-group-mode-map 
3088     "\M-p" 'gnus-group-prev-unread-group-same-level)
3089   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3090   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3091   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3092   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3093   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3094   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3095   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3096   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3097   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3098   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3099   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3100   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3101   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3102   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3103   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3104   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3105   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3106   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3107   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3108   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3109   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3110   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3111   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3112   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3113   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3114   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3115   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3116   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3117   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3118   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3119   (define-key gnus-group-mode-map "V" 'gnus-version)
3120   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3121   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3122   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3123   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3124   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3125   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3126   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3127   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3128   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3129   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3130   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3131   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3132   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3133   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3134   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3135
3136   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3137   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3138   (define-prefix-command 'gnus-group-mark-map)
3139   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3140   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3141   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3142   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3143
3144   (define-prefix-command 'gnus-group-group-map)
3145   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3146   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3147   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3148   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3149   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3150   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3151   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3152   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3153   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3154   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3155   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3156   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3157   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3158   ;;(define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3159   ;;(define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3160   ;;(define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3161   ;;(define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3162   ;;(define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3163
3164   (define-prefix-command 'gnus-group-list-map)
3165   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3166   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3167   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3168   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3169   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3170   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3171   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3172   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3173   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3174
3175   (define-prefix-command 'gnus-group-sub-map)
3176   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3177   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3178   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3179   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3180   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3181   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3182   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3183   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3184
3185 (defun gnus-group-mode ()
3186   "Major mode for reading news.
3187
3188 All normal editing commands are switched off.
3189 \\<gnus-group-mode-map>
3190 The group buffer lists (some of) the groups available.  For instance,
3191 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3192 lists all zombie groups. 
3193
3194 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3195 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3196
3197 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3198
3199 The following commands are available:
3200
3201 \\{gnus-group-mode-map}"
3202   (interactive)
3203   (if gnus-visual (gnus-group-make-menu-bar))
3204   (kill-all-local-variables)
3205   (setq mode-line-modified "-- ")
3206   (make-local-variable 'mode-line-format)
3207   (setq mode-line-format (copy-sequence mode-line-format))
3208   (and (equal (nth 3 mode-line-format) "   ")
3209        (setcar (nthcdr 3 mode-line-format) ""))
3210   (setq major-mode 'gnus-group-mode)
3211   (setq mode-name "Group")
3212   (gnus-group-set-mode-line)
3213   (setq mode-line-process nil)
3214   (use-local-map gnus-group-mode-map)
3215   (buffer-disable-undo (current-buffer))
3216   (setq truncate-lines t)
3217   (setq buffer-read-only t)
3218   (run-hooks 'gnus-group-mode-hook))
3219
3220 (defun gnus-mouse-pick-group (e)
3221   (interactive "e")
3222   (mouse-set-point e)
3223   (gnus-group-read-group nil))
3224
3225 ;; Look at LEVEL and find out what the level is really supposed to be.
3226 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3227 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3228 (defun gnus-group-default-level (&optional level number-or-nil)
3229   (cond  
3230    (gnus-group-use-permanent-levels
3231     (setq gnus-group-default-list-level 
3232           (or level gnus-group-default-list-level))
3233     (or gnus-group-default-list-level gnus-level-subscribed))
3234    (number-or-nil
3235     level)
3236    (t
3237     (or level gnus-group-default-list-level gnus-level-subscribed))))
3238   
3239
3240 (defvar gnus-tmp-prev-perm nil)
3241
3242 ;;;###autoload
3243 (defun gnus-no-server (&optional arg)
3244   "Read network news.
3245 If ARG is a positive number, Gnus will use that as the
3246 startup level. If ARG is nil, Gnus will be started at level 2. 
3247 If ARG is non-nil and not a positive number, Gnus will
3248 prompt the user for the name of an NNTP server to use.
3249 As opposed to `gnus', this command will not connect to the local server."
3250   (interactive "P")
3251   (let ((perm
3252          (cons gnus-group-use-permanent-levels gnus-group-default-list-level)))
3253     (setq gnus-tmp-prev-perm nil)
3254     (setq gnus-group-use-permanent-levels t)
3255     (gnus (or arg (1- gnus-level-default-subscribed)) t)
3256     (setq gnus-tmp-prev-perm perm)))
3257
3258 ;;;###autoload
3259 (defun gnus (&optional arg dont-connect)
3260   "Read network news.
3261 If ARG is non-nil and a positive number, Gnus will use that as the
3262 startup level. If ARG is non-nil and not a positive number, Gnus will
3263 prompt the user for the name of an NNTP server to use."
3264   (interactive "P")
3265   (if (get-buffer gnus-group-buffer)
3266       (progn
3267         (switch-to-buffer gnus-group-buffer)
3268         (gnus-group-get-new-news))
3269
3270     (gnus-clear-system)
3271
3272     (nnheader-init-server-buffer)
3273     ;; We do this if `gnus-no-server' has been run.
3274     (if gnus-tmp-prev-perm 
3275         (setq gnus-group-use-permanent-levels (car gnus-tmp-prev-perm)
3276               gnus-group-default-list-level (cdr gnus-tmp-prev-perm)
3277               gnus-tmp-prev-perm nil))
3278     (gnus-read-init-file)
3279
3280     (gnus-group-setup-buffer)
3281     (let ((buffer-read-only nil))
3282       (erase-buffer)
3283       (if (not gnus-inhibit-startup-message)
3284           (progn
3285             (gnus-group-startup-message)
3286             (sit-for 0))))
3287     
3288     (let ((level (and arg (numberp arg) (> arg 0) arg))
3289           did-connect)
3290       (unwind-protect
3291           (progn
3292             (or dont-connect 
3293                 (setq did-connect
3294                       (gnus-start-news-server (and arg (not level))))))
3295         (if (and (not dont-connect) 
3296                  (not did-connect))
3297             (gnus-group-quit)
3298           (run-hooks 'gnus-startup-hook)
3299           ;; NNTP server is successfully open. 
3300
3301           ;; Find the current startup file name.
3302           (setq gnus-current-startup-file 
3303                 (gnus-make-newsrc-file gnus-startup-file))
3304
3305           ;; Read the dribble file.
3306           (and gnus-use-dribble-file (gnus-dribble-read-file))
3307
3308           (gnus-summary-make-display-table)
3309           (gnus-setup-news nil level)
3310           (gnus-group-list-groups level)
3311           (gnus-configure-windows 'group))))))
3312
3313 (defun gnus-unload ()
3314   "Unload all Gnus features."
3315   (interactive)
3316   (or (boundp 'load-history)
3317       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
3318   (let ((history load-history)
3319         feature)
3320     (while history
3321       (and (string-match "^gnus" (car (car history)))
3322            (setq feature (cdr (assq 'provide (car history))))
3323            (unload-feature feature 'force))
3324       (setq history (cdr history)))))
3325
3326 (defun gnus-group-startup-message (&optional x y)
3327   "Insert startup message in current buffer."
3328   ;; Insert the message.
3329   (erase-buffer)
3330   (insert
3331    (format "
3332           _    ___ _             _      
3333           _ ___ __ ___  __    _ ___     
3334           __   _     ___    __  ___     
3335               _           ___     _     
3336              _  _ __             _      
3337              ___   __            _      
3338                    __           _       
3339                     _      _   _        
3340                    _      _    _        
3341                       _  _    _         
3342                   __  ___               
3343                  _   _ _     _          
3344                 _   _                   
3345               _    _                    
3346              _    _                     
3347             _                         
3348           __                             
3349
3350
3351       Gnus * A newsreader for Emacsen
3352     A Praxis release * larsi@ifi.uio.no
3353
3354            gnus-version))
3355   ;; And then hack it.
3356   ;; 18 is the longest line.
3357   (indent-rigidly (point-min) (point-max) 
3358                   (/ (max (- (window-width) (or x 46)) 0) 2))
3359   (goto-char (point-min))
3360   (let* ((pheight (count-lines (point-min) (point-max)))
3361          (wheight (window-height))
3362          (rest (- wheight  pheight)))
3363     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
3364     
3365     
3366
3367   ;; Fontify some.
3368   (goto-char (point-min))
3369   (search-forward "Praxis")
3370   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3371   (goto-char (point-min)))
3372
3373 (defun gnus-group-startup-message-old (&optional x y)
3374   "Insert startup message in current buffer."
3375   ;; Insert the message.
3376   (erase-buffer)
3377   (insert
3378    (format "
3379      %s
3380            A newsreader 
3381       for GNU Emacs
3382
3383         Based on GNUS 
3384              written by 
3385      Masanobu UMEDA
3386
3387        A Praxis Release
3388       larsi@ifi.uio.no
3389
3390            gnus-version))
3391   ;; And then hack it.
3392   ;; 18 is the longest line.
3393   (indent-rigidly (point-min) (point-max) 
3394                   (/ (max (- (window-width) (or x 28)) 0) 2))
3395   (goto-char (point-min))
3396   ;; +4 is fuzzy factor.
3397   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3398
3399   ;; Fontify some.
3400   (goto-char (point-min))
3401   (search-forward "Praxis")
3402   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3403   (goto-char (point-min)))
3404
3405 (defun gnus-group-setup-buffer ()
3406   (or (get-buffer gnus-group-buffer)
3407       (progn
3408         (switch-to-buffer gnus-group-buffer)
3409         (gnus-add-current-to-buffer-list)
3410         (gnus-group-mode)
3411         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3412
3413 (defun gnus-group-list-groups (&optional level unread)
3414   "List newsgroups with level LEVEL or lower that have unread articles.
3415 Default is all subscribed groups.
3416 If argument UNREAD is non-nil, groups with no unread articles are also
3417 listed." 
3418   (interactive (list (if current-prefix-arg
3419                          (prefix-numeric-value current-prefix-arg)
3420                        (or
3421                         (gnus-group-default-level nil t)
3422                         gnus-group-default-list-level
3423                         gnus-level-subscribed))))
3424   (or level
3425       (setq level (car gnus-group-list-mode)
3426             unread (cdr gnus-group-list-mode)))
3427   (setq level (gnus-group-default-level level))
3428   (gnus-group-setup-buffer)             ;May call from out of group buffer
3429   (let ((case-fold-search nil)
3430         (group (gnus-group-group-name)))
3431     (funcall gnus-group-prepare-function level unread nil)
3432     (if (zerop (buffer-size))
3433         (gnus-message 5 gnus-no-groups-message)
3434       (goto-char (point-min))
3435       (if (not group)
3436           ;; Go to the first group with unread articles.
3437           (gnus-group-search-forward nil nil nil t)
3438         ;; Find the right group to put point on. If the current group
3439         ;; has disapeared in the new listing, try to find the next
3440         ;; one. If no next one can be found, just leave point at the
3441         ;; first newsgroup in the buffer.
3442         (if (not (gnus-goto-char
3443                   (text-property-any (point-min) (point-max) 
3444                                      'gnus-group (intern group))))
3445             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3446               (while (and newsrc
3447                           (not (gnus-goto-char 
3448                                 (text-property-any 
3449                                  (point-min) (point-max) 'gnus-group 
3450                                  (intern (car (car newsrc)))))))
3451                 (setq newsrc (cdr newsrc)))
3452               (or newsrc (progn (goto-char (point-max))
3453                                 (forward-line -1))))))
3454       ;; Adjust cursor point.
3455       (gnus-group-position-cursor))))
3456
3457 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3458   "List all newsgroups with unread articles of level LEVEL or lower.
3459 If ALL is non-nil, list groups that have no unread articles.
3460 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3461 If REGEXP, only list groups matching REGEXP."
3462   (set-buffer gnus-group-buffer)
3463   (let ((buffer-read-only nil)
3464         (newsrc (cdr gnus-newsrc-alist))
3465         (lowest (or lowest 1))
3466         info clevel unread group)
3467     (erase-buffer)
3468     (if (< lowest gnus-level-zombie)
3469         ;; List living groups.
3470         (while newsrc
3471           (setq info (car newsrc)
3472                 group (car info)
3473                 newsrc (cdr newsrc)
3474                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3475           (and unread                   ; This group might be bogus
3476                (or (not regexp)
3477                    (string-match regexp group))
3478                (<= (setq clevel (car (cdr info))) level) 
3479                (>= clevel lowest)
3480                (or all                  ; We list all groups?
3481                    (eq unread t)        ; We list unactivated groups
3482                    (> unread 0)         ; We list groups with unread articles
3483                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3484                (gnus-group-insert-group-line 
3485                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3486
3487     ;; List dead groups.
3488     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3489          (gnus-group-prepare-flat-list-dead 
3490           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3491           gnus-level-zombie ?Z
3492           regexp))
3493     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3494          (gnus-group-prepare-flat-list-dead 
3495           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3496           gnus-level-killed ?K regexp))
3497
3498     (gnus-group-set-mode-line)
3499     (setq gnus-group-list-mode (cons level all))
3500     (run-hooks 'gnus-group-prepare-hook)))
3501
3502 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3503   ;; List zombies and killed lists somehwat faster, which was
3504   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3505   ;; this by ignoring the group format specification altogether.
3506   (let (group beg)
3507     (while groups
3508       (setq group (car groups)
3509             groups (cdr groups))
3510       (if (or (not regexp)
3511               (string-match regexp group))
3512           (progn
3513             (setq beg (point))
3514             (insert (format " %c     *: %s\n" mark group))
3515             (add-text-properties 
3516              beg (1+ beg) 
3517              (list 'gnus-group (intern group)
3518                    'gnus-unread t
3519                    'gnus-level level)))))))
3520
3521 (defun gnus-group-real-name (group)
3522   "Find the real name of a foreign newsgroup."
3523   (if (string-match ":[^:]+$" group)
3524       (substring group (1+ (match-beginning 0)))
3525     group))
3526
3527 (defun gnus-group-prefixed-name (group method)
3528   "Return the whole name from GROUP and METHOD."
3529   (and (stringp method) (setq method (gnus-server-to-method method)))
3530   (concat (format "%s" (car method))
3531           (if (and 
3532                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3533                (not (string= (nth 1 method) "")))
3534               (concat "+" (nth 1 method)))
3535           ":" group))
3536
3537 (defun gnus-group-real-prefix (group)
3538   "Return the prefix of the current group name."
3539   (if (string-match "^[^:]+:" group)
3540       (substring group 0 (match-end 0))
3541     ""))
3542
3543 (defun gnus-group-method-name (group)
3544   "Return the method used for selecting GROUP."
3545   (let ((prefix (gnus-group-real-prefix group)))
3546     (if (equal prefix "")
3547         gnus-select-method
3548       (if (string-match "^[^\\+]+\\+" prefix)
3549           (list (intern (substring prefix 0 (1- (match-end 0))))
3550                 (substring prefix (match-end 0) (1- (length prefix))))
3551         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3552
3553 (defun gnus-group-foreign-p (group)
3554   "Return nil if GROUP is native, non-nil if it is foreign."
3555   (string-match ":" group))
3556
3557 (defun gnus-group-set-info (info &optional method-only-group part)
3558   (let* ((entry (gnus-gethash
3559                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3560          (part-info info)
3561          (info (if method-only-group (nth 2 entry) info)))
3562     (if (not method-only-group)
3563         ()
3564       (or entry
3565           (error "Trying to change non-existent group %s" method-only-group))
3566       ;; We have recevied parts of the actual group info - either the
3567       ;; select method or the group parameters.  We first check
3568       ;; whether we have to extend the info, and if so, do that.
3569       (let ((len (length info))
3570             (total (if (eq part 'method) 5 6)))
3571         (and (< len total)
3572              (setcdr (nthcdr (1- len) info)
3573                      (make-list (- total len) nil)))
3574         ;; Then we enter the new info.
3575         (setcar (nthcdr (1- total) info) part-info)))
3576     ;; We uncompress some lists of marked articles.
3577     (let (marked)
3578       (if (not (setq marked (nth 3 info)))
3579           ()
3580         (while marked
3581           (or (eq 'score (car (car marked)))
3582               (eq 'bookmark (car (car marked)))
3583               (eq 'killed (car (car marked)))
3584               (setcdr (car marked) 
3585                       (gnus-uncompress-range (cdr (car marked)))))
3586           (setq marked (cdr marked)))))
3587     (if entry
3588         ()
3589       ;; This is a new group, so we just create it.
3590       (save-excursion
3591         (set-buffer gnus-group-buffer)
3592         (if (nth 4 info)
3593             ;; It's a foreign group...
3594             (gnus-group-make-group 
3595              (gnus-group-real-name (car info))
3596              (prin1-to-string (car (nth 4 info)))
3597              (nth 1 (nth 4 info)))
3598           ;; It's a native group.
3599           (gnus-group-make-group (car info)))
3600         (gnus-message 6 "Note: New group created")
3601         (setq entry 
3602               (gnus-gethash (gnus-group-prefixed-name 
3603                              (gnus-group-real-name (car info))
3604                              (or (nth 4 info) gnus-select-method))
3605                             gnus-newsrc-hashtb))))
3606     ;; Whether it was a new group or not, we now have the entry, so we
3607     ;; can do the update.
3608     (if entry
3609         (progn
3610           (setcar (nthcdr 2 entry) info)
3611           (if (and (not (eq (car entry) t)) 
3612                    (gnus-gethash (car info) gnus-active-hashtb))
3613               (let ((marked (nth 3 info)))
3614                 (setcar entry 
3615                         (max 0 (- (length (gnus-list-of-unread-articles 
3616                                            (car info)))
3617                                   (length (cdr (assq 'tick marked)))
3618                                   (length (cdr (assq 'dormant marked)))))))))
3619       (error "No such group: %s" (car info)))))
3620
3621 (defun gnus-group-set-method-info (group select-method)
3622   (gnus-group-set-info select-method group 'method))
3623
3624 (defun gnus-group-set-params-info (group params)
3625   (gnus-group-set-info params group 'params))
3626
3627 (defun gnus-group-update-group-line ()
3628   "This function updates the current line in the newsgroup buffer and
3629 moves the point to the colon."
3630   (let* ((buffer-read-only nil)
3631          (group (gnus-group-group-name))
3632          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3633     (if entry
3634         (gnus-dribble-enter 
3635          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3636                  ")")))
3637     (beginning-of-line)
3638     (delete-region (point) (progn (forward-line 1) (point)))
3639     (gnus-group-insert-group-line-info group)
3640     (forward-line -1)
3641     (gnus-group-position-cursor)))
3642
3643 (defun gnus-group-insert-group-line-info (group)
3644   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3645         active info)
3646     (if entry
3647         (progn
3648           (setq info (nth 2 entry))
3649           (gnus-group-insert-group-line 
3650            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3651       (setq active (gnus-gethash group gnus-active-hashtb))
3652       (gnus-group-insert-group-line 
3653        nil group 
3654        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3655        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3656
3657 (defun gnus-group-insert-group-line (gformat group level marked number method)
3658   (let* ((gformat (or gformat gnus-group-line-format-spec))
3659          (active (gnus-gethash group gnus-active-hashtb))
3660          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3661          (number-of-dormant (length (cdr (assq 'dormant marked))))
3662          (number-of-ticked (length (cdr (assq 'tick marked))))
3663          (number-of-ticked-and-dormant
3664           (+ number-of-ticked number-of-dormant))
3665          (number-of-unread-unticked 
3666           (if (numberp number) (int-to-string (max 0 number))
3667             "*"))
3668          (number-of-read
3669           (if (numberp number)
3670               (max 0 (- number-total number))
3671             "*"))
3672          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3673                            ((<= level gnus-level-unsubscribed) ?U)
3674                            ((= level gnus-level-zombie) ?Z)
3675                            (t ?K)))
3676          (qualified-group (gnus-group-real-name group))
3677          (newsgroup-description 
3678           (if gnus-description-hashtb
3679               (or (gnus-gethash group gnus-description-hashtb) "")
3680             ""))
3681          (moderated (if (member group gnus-moderated-list) ?m ? ))
3682          (moderated-string (if (eq moderated ?m) "(m)" ""))
3683          (method (gnus-server-get-method group method))
3684          (news-server (or (car (cdr method)) ""))
3685          (news-method (or (car method) ""))
3686          (news-method-string 
3687           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3688          (marked (if (and 
3689                       (numberp number) 
3690                       (zerop number)
3691                       (> number-of-ticked 0))
3692                      ?* ? ))
3693          (number (if (eq number t) "*" (+ number number-of-dormant 
3694                                           number-of-ticked)))
3695          (process-marked (if (member group gnus-group-marked)
3696                              gnus-process-mark ? ))
3697          (buffer-read-only nil)
3698          header                         ; passed as parameter to user-funcs.
3699          b)
3700     (beginning-of-line)
3701     (setq b (point))
3702     ;; Insert the text.
3703     (insert (eval gformat))
3704
3705     (add-text-properties 
3706      b (1+ b) (list 'gnus-group (intern group)
3707                     'gnus-unread (if (numberp number)
3708                                      (string-to-int number-of-unread-unticked)
3709                                    t)
3710                     'gnus-marked marked
3711                     'gnus-level level))))
3712
3713 (defun gnus-group-update-group (group &optional visible-only)
3714   "Update newsgroup info of GROUP.
3715 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3716   (save-excursion
3717     (set-buffer gnus-group-buffer)
3718     (let ((buffer-read-only nil)
3719           visible)
3720       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3721         (if entry
3722             (gnus-dribble-enter 
3723              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3724                      ")"))))
3725       ;; Buffer may be narrowed.
3726       (save-restriction
3727         (widen)
3728         ;; Search a line to modify.  If the buffer is large, the search
3729         ;; takes long time.  In most cases, current point is on the line
3730         ;; we are looking for.  So, first of all, check current line. 
3731         (if (or (progn
3732                   (beginning-of-line)
3733                   (eq (get-text-property (point) 'gnus-group)
3734                       (intern group)))
3735                 (progn
3736                   (gnus-goto-char 
3737                    (text-property-any 
3738                     (point-min) (point-max) 'gnus-group (intern group)))))
3739             ;; GROUP is listed in current buffer. So, delete old line.
3740             (progn
3741               (setq visible t)
3742               (beginning-of-line)
3743               (delete-region (point) (progn (forward-line 1) (point))))
3744           ;; No such line in the buffer, find out where it's supposed to
3745           ;; go, and insert it there (or at the end of the buffer).
3746           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3747           (or visible-only
3748               (let ((entry 
3749                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3750                 (while (and entry
3751                             (car entry)
3752                             (not
3753                              (gnus-goto-char
3754                               (text-property-any
3755                                (point-min) (point-max) 
3756                                'gnus-group (intern (car (car entry)))))))
3757                   (setq entry (cdr entry)))
3758                 (or entry (goto-char (point-max)))))))
3759       (if (or visible (not visible-only))
3760           (gnus-group-insert-group-line-info group))
3761       (gnus-group-set-mode-line))))
3762
3763 (defun gnus-group-set-mode-line ()
3764   (if (memq 'group gnus-updated-mode-lines)
3765       (let* ((gformat (or gnus-group-mode-line-format-spec
3766                           (setq gnus-group-mode-line-format-spec
3767                                 (gnus-parse-format 
3768                                  gnus-group-mode-line-format 
3769                                  gnus-group-mode-line-format-alist))))
3770              (news-server (car (cdr gnus-select-method)))
3771              (news-method (car gnus-select-method))
3772              (max-len 60)
3773              (mode-string (eval gformat)))
3774         (setq mode-string (eval gformat))
3775         (if (> (length mode-string) max-len) 
3776             (setq mode-string (substring mode-string 0 (- max-len 4))))
3777         (setq mode-line-buffer-identification mode-string)
3778         (set-buffer-modified-p t))))
3779
3780 (defun gnus-group-group-name ()
3781   "Get the name of the newsgroup on the current line."
3782   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3783     (and group (symbol-name group))))
3784
3785 (defun gnus-group-group-level ()
3786   "Get the level of the newsgroup on the current line."
3787   (get-text-property (gnus-point-at-bol) 'gnus-level))
3788
3789 (defun gnus-group-group-unread ()
3790   "Get the number of unread articles of the newsgroup on the current line."
3791   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3792
3793 (defun gnus-group-search-forward (&optional backward all level first-too)
3794   "Find the next newsgroup with unread articles.
3795 If BACKWARD is non-nil, find the previous newsgroup instead.
3796 If ALL is non-nil, just find any newsgroup.
3797 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3798 group exists.
3799 If FIRST-TOO, the current line is also eligible as a target."
3800   (let ((way (if backward -1 1))
3801         (low gnus-level-killed)
3802         (beg (point))
3803         pos found lev)
3804     (if (and backward (progn (beginning-of-line)) (bobp))
3805         nil
3806       (or first-too (forward-line way))
3807       (while (and 
3808               (not (eobp))
3809               (not (setq 
3810                     found 
3811                     (and (or all
3812                              (and
3813                               (let ((unread 
3814                                      (get-text-property (point) 'gnus-unread)))
3815                                 (or (eq unread t) (and unread (> unread 0))))
3816                               (setq lev (get-text-property (point)
3817                                                            'gnus-level))
3818                               (<= lev gnus-level-subscribed)))
3819                          (or (not level)
3820                              (and (setq lev (get-text-property (point)
3821                                                                'gnus-level))
3822                                   (or (= lev level)
3823                                       (and (< lev low)
3824                                            (< level lev)
3825                                            (progn
3826                                              (setq low lev)
3827                                              (setq pos (point))
3828                                              nil))))))))
3829               (zerop (forward-line way)))))
3830     (if found 
3831         (progn (gnus-group-position-cursor) t)
3832       (goto-char (or pos beg))
3833       (and pos t))))
3834
3835 ;;; Gnus group mode commands
3836
3837 ;; Group marking.
3838
3839 (defun gnus-group-mark-group (n &optional unmark no-advance)
3840   "Mark the current group."
3841   (interactive "p")
3842   (let ((buffer-read-only nil)
3843         group)
3844     (while 
3845         (and (> n 0) 
3846              (setq group (gnus-group-group-name))
3847              (progn
3848                (beginning-of-line)
3849                (forward-char 
3850                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3851                (delete-char 1)
3852                (if unmark
3853                    (progn
3854                      (insert " ")
3855                      (setq gnus-group-marked (delete group gnus-group-marked)))
3856                  (insert "#")
3857                  (setq gnus-group-marked
3858                        (cons group (delete group gnus-group-marked))))
3859                t)
3860              (or no-advance (zerop (gnus-group-next-group 1))))
3861       (setq n (1- n)))
3862     (gnus-summary-position-cursor)
3863     n))
3864
3865 (defun gnus-group-unmark-group (n)
3866   "Remove the mark from the current group."
3867   (interactive "p")
3868   (gnus-group-mark-group n 'unmark))
3869
3870 (defun gnus-group-mark-region (unmark beg end)
3871   "Mark all groups between point and mark.
3872 If UNMARK, remove the mark instead."
3873   (interactive "P\nr")
3874   (let ((num (count-lines beg end)))
3875     (save-excursion
3876       (goto-char beg)
3877       (- num (gnus-group-mark-group num unmark)))))
3878
3879 (defun gnus-group-remove-mark (group)
3880   (and (gnus-group-goto-group group)
3881        (save-excursion
3882          (gnus-group-mark-group 1 'unmark t))))
3883
3884 ;; Return a list of groups to work on.  Take into consideration N (the
3885 ;; prefix) and the list of marked groups.
3886 (defun gnus-group-process-prefix (n)
3887   (cond (n
3888          (setq n (prefix-numeric-value n))
3889          ;; There is a prefix, so we return a list of the N next
3890          ;; groups. 
3891          (let ((way (if (< n 0) -1 1))
3892                (n (abs n))
3893                group groups)
3894            (save-excursion
3895              (while (and (> n 0)
3896                          (setq group (gnus-group-group-name)))
3897                (setq groups (cons group groups))
3898                (setq n (1- n))
3899                (forward-line way)))
3900            (nreverse groups)))
3901         (gnus-group-marked
3902          ;; No prefix, but a list of marked articles.
3903          (reverse gnus-group-marked))
3904         (t
3905          ;; Neither marked articles or a prefix, so we return the
3906          ;; current group.
3907          (let ((group (gnus-group-group-name)))
3908            (and group (list group))))))
3909
3910 ;; Selecting groups.
3911
3912 (defun gnus-group-read-group (&optional all no-article group)
3913   "Read news in this newsgroup.
3914 If the prefix argument ALL is non-nil, already read articles become
3915 readable. If the optional argument NO-ARTICLE is non-nil, no article
3916 will be auto-selected upon group entry."
3917   (interactive "P")
3918   (let ((group (or group (gnus-group-group-name)))
3919         number active marked entry)
3920     (or group (error "No group on current line"))
3921     (setq marked 
3922           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3923     ;; This group might be a dead group. In that case we have to get
3924     ;; the number of unread articles from `gnus-active-hashtb'.
3925     (if entry
3926         (setq number (car entry))
3927       (if (setq active (gnus-gethash group gnus-active-hashtb))
3928           (setq number (- (1+ (cdr active)) (car active)))))
3929     (gnus-summary-read-group 
3930      group (or all (and (numberp number) 
3931                         (zerop (+ number (length (cdr (assq 'tick marked)))
3932                                   (length (cdr (assq 'dormant marked)))))))
3933      no-article)))
3934
3935 (defun gnus-group-select-group (&optional all)
3936   "Select this newsgroup.
3937 No article is selected automatically.
3938 If argument ALL is non-nil, already read articles become readable."
3939   (interactive "P")
3940   (gnus-group-read-group all t))
3941
3942 (defun gnus-group-select-group-all ()
3943   "Select the current group and display all articles in it."
3944   (interactive)
3945   (gnus-group-select-group 'all))
3946
3947 ;; Enter a group that is not in the group buffer. Non-nil is returned
3948 ;; if selection was successful.
3949 (defun gnus-group-read-ephemeral-group 
3950   (group method &optional activate quit-config)
3951   (let ((group (if (gnus-group-foreign-p group) group
3952                  (gnus-group-prefixed-name group method))))
3953     (gnus-sethash 
3954      group
3955      (list t nil (list group gnus-level-default-subscribed nil nil 
3956                        (append method
3957                                (list
3958                                 (list 'quit-config 
3959                                       (if quit-config quit-config
3960                                         (cons (current-buffer) 'summary)))))))
3961      gnus-newsrc-hashtb)
3962     (set-buffer gnus-group-buffer)
3963     (or (gnus-check-server method)
3964         (error "Unable to contact server: %s" (gnus-status-message method)))
3965     (if activate (or (gnus-request-group group)
3966                      (error "Couldn't request group")))
3967     (condition-case ()
3968         (gnus-group-read-group t t group)
3969       (error nil)
3970       (quit nil))
3971     (not (equal major-mode 'gnus-group-mode))))
3972   
3973 (defun gnus-group-jump-to-group (group)
3974   "Jump to newsgroup GROUP."
3975   (interactive 
3976    (list (completing-read 
3977           "Group: " gnus-active-hashtb nil 
3978           (memq gnus-select-method gnus-have-read-active-file))))
3979
3980   (if (equal group "")
3981       (error "Empty group name"))
3982
3983   (let ((b (text-property-any 
3984             (point-min) (point-max) 'gnus-group (intern group))))
3985     (if b
3986         ;; Either go to the line in the group buffer...
3987         (goto-char b)
3988       ;; ... or insert the line.
3989       (or
3990        (gnus-gethash group gnus-active-hashtb)
3991        (gnus-activate-group group)
3992        (error "%s error: %s" group (gnus-status-message group)))
3993
3994       (gnus-group-update-group group)
3995       (goto-char (text-property-any 
3996                   (point-min) (point-max) 'gnus-group (intern group)))))
3997   ;; Adjust cursor point.
3998   (gnus-group-position-cursor))
3999
4000 (defun gnus-group-goto-group (group)
4001   "Goto to newsgroup GROUP."
4002   (let ((b (text-property-any (point-min) (point-max) 
4003                               'gnus-group (intern group))))
4004     (and b (goto-char b))))
4005
4006 (defun gnus-group-next-group (n)
4007   "Go to next N'th newsgroup.
4008 If N is negative, search backward instead.
4009 Returns the difference between N and the number of skips actually
4010 done."
4011   (interactive "p")
4012   (gnus-group-next-unread-group n t))
4013
4014 (defun gnus-group-next-unread-group (n &optional all level)
4015   "Go to next N'th unread newsgroup.
4016 If N is negative, search backward instead.
4017 If ALL is non-nil, choose any newsgroup, unread or not.
4018 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4019 such group can be found, the next group with a level higher than
4020 LEVEL.
4021 Returns the difference between N and the number of skips actually
4022 made."
4023   (interactive "p")
4024   (let ((backward (< n 0))
4025         (n (abs n)))
4026     (while (and (> n 0)
4027                 (gnus-group-search-forward 
4028                  backward (or (not gnus-group-goto-unread) all) level))
4029       (setq n (1- n)))
4030     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4031                                (if level " on this level or higher" "")))
4032     n))
4033
4034 (defun gnus-group-prev-group (n)
4035   "Go to previous N'th newsgroup.
4036 Returns the difference between N and the number of skips actually
4037 done."
4038   (interactive "p")
4039   (gnus-group-next-unread-group (- n) t))
4040
4041 (defun gnus-group-prev-unread-group (n)
4042   "Go to previous N'th unread newsgroup.
4043 Returns the difference between N and the number of skips actually
4044 done."  
4045   (interactive "p")
4046   (gnus-group-next-unread-group (- n)))
4047
4048 (defun gnus-group-next-unread-group-same-level (n)
4049   "Go to next N'th unread newsgroup on the same level.
4050 If N is negative, search backward instead.
4051 Returns the difference between N and the number of skips actually
4052 done."
4053   (interactive "p")
4054   (gnus-group-next-unread-group n t (gnus-group-group-level))
4055   (gnus-group-position-cursor))
4056
4057 (defun gnus-group-prev-unread-group-same-level (n)
4058   "Go to next N'th unread newsgroup on the same level.
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-best-unread-group (&optional exclude-group)
4066   "Go to the group with the highest level.
4067 If EXCLUDE-GROUP, do not go to that group."
4068   (interactive)
4069   (goto-char (point-min))
4070   (let ((best 100000)
4071         unread best-point)
4072     (while (setq unread (get-text-property (point) 'gnus-unread))
4073       (if (and (numberp unread) (> unread 0))
4074           (progn
4075             (if (and (< (get-text-property (point) 'gnus-level) best)
4076                      (or (not exclude-group)
4077                          (not (equal exclude-group (gnus-group-group-name)))))
4078                 (progn 
4079                   (setq best (get-text-property (point) 'gnus-level))
4080                   (setq best-point (point))))))
4081       (forward-line 1))
4082     (if best-point (goto-char best-point))
4083     (gnus-summary-position-cursor)
4084     (and best-point (gnus-group-group-name))))
4085
4086 (defun gnus-group-first-unread-group ()
4087   "Go to the first group with unread articles."
4088   (interactive)
4089   (prog1
4090       (let ((opoint (point))
4091             unread)
4092         (goto-char (point-min))
4093         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4094                 (not (zerop unread))    ; Has unread articles.
4095                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4096             (point)                     ; Success.
4097           (goto-char opoint)
4098           nil))                         ; Not success.
4099     (gnus-group-position-cursor)))
4100
4101 (defun gnus-group-enter-server-mode ()
4102   "Jump to the server buffer."
4103   (interactive)
4104   (gnus-server-setup-buffer)
4105   (gnus-configure-windows 'server)
4106   (gnus-server-prepare))
4107
4108 (defun gnus-group-make-group (name &optional method address)
4109   "Add a new newsgroup.
4110 The user will be prompted for a NAME, for a select METHOD, and an
4111 ADDRESS."
4112   (interactive
4113    (cons 
4114     (read-string "Group name: ")
4115     (let ((method
4116            (completing-read 
4117             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4118             nil t)))
4119       (if (assoc method gnus-valid-select-methods)
4120           (list method
4121                 (if (memq 'prompt-address
4122                           (assoc method gnus-valid-select-methods))
4123                     (read-string "Address: ")
4124                   ""))
4125         (list method nil)))))
4126   
4127   (let* ((meth (and method (if address (list (intern method) address) method)))
4128          (nname (if method (gnus-group-prefixed-name name meth) name))
4129          info)
4130     (and (gnus-gethash nname gnus-newsrc-hashtb)
4131          (error "Group %s already exists" nname))
4132     (gnus-group-change-level 
4133      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4134      gnus-level-default-subscribed gnus-level-killed 
4135      (and (gnus-group-group-name)
4136           (gnus-gethash (gnus-group-group-name)
4137                         gnus-newsrc-hashtb))
4138      t)
4139     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4140     (gnus-dribble-enter 
4141      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4142     (gnus-group-insert-group-line-info nname)
4143
4144     (if (assoc method gnus-valid-select-methods)
4145         (require (intern method)))
4146     (and (gnus-check-backend-function 'request-create-group nname)
4147          (gnus-request-create-group nname))))
4148
4149 (defun gnus-group-edit-group (group &optional part)
4150   "Edit the group on the current line."
4151   (interactive (list (gnus-group-group-name)))
4152   (let ((done-func '(lambda () 
4153                       "Exit editing mode and update the information."
4154                       (interactive)
4155                       (gnus-group-edit-group-done 'part 'group)))
4156         (part (or part 'info))
4157         (winconf (current-window-configuration))
4158         info)
4159     (or group (error "No group on current line"))
4160     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4161         (error "Killed group; can't be edited"))
4162     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4163     (gnus-configure-windows 'edit-group)
4164     (gnus-add-current-to-buffer-list)
4165     (emacs-lisp-mode)
4166     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4167     (use-local-map (copy-keymap emacs-lisp-mode-map))
4168     (local-set-key "\C-c\C-c" done-func)
4169     (make-local-variable 'gnus-prev-winconf)
4170     (setq gnus-prev-winconf winconf)
4171     ;; We modify the func to let it know what part it is editing.
4172     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4173     (setcar (cdr (cdr (nth 4 done-func))) group)
4174     (erase-buffer)
4175     (insert
4176      (cond 
4177       ((eq part 'method)
4178        ";; Type `C-c C-c' after editing the select method.\n\n")
4179       ((eq part 'params)
4180        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4181       ((eq part 'info)
4182        ";; Type `C-c C-c' after editing the group info.\n\n")))
4183     (let ((cinfo (gnus-copy-sequence info))
4184           marked)
4185       (if (not (setq marked (nth 3 cinfo)))
4186           ()
4187         (while marked
4188           (or (eq 'score (car (car marked)))
4189               (eq 'bookmark (car (car marked)))
4190               (eq 'killed (car (car marked)))
4191               (not (numberp (car (cdr (car marked)))))
4192               (setcdr (car marked) 
4193                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4194           (setq marked (cdr marked))))
4195       (insert 
4196        (pp-to-string
4197         (cond ((eq part 'method)
4198                (or (nth 4 info) "native"))
4199               ((eq part 'params)
4200                (nth 5 info))
4201               (t
4202                cinfo)))
4203        "\n"))))
4204
4205 (defun gnus-group-edit-group-method (group)
4206   "Edit the select method of GROUP."
4207   (interactive (list (gnus-group-group-name)))
4208   (gnus-group-edit-group group 'method))
4209
4210 (defun gnus-group-edit-group-parameters (group)
4211   "Edit the group parameters of GROUP."
4212   (interactive (list (gnus-group-group-name)))
4213   (gnus-group-edit-group group 'params))
4214
4215 (defun gnus-group-edit-group-done (part group)
4216   "Get info from buffer, update variables and jump to the group buffer."
4217   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4218   (goto-char (point-min))
4219   (let ((form (read (current-buffer)))
4220         (winconf gnus-prev-winconf))
4221     (if (eq part 'info) 
4222         (gnus-group-set-info form)
4223       (gnus-group-set-info form group part))
4224     (kill-buffer (current-buffer))
4225     (and winconf (set-window-configuration winconf))
4226     (set-buffer gnus-group-buffer)
4227     (gnus-group-update-group (gnus-group-group-name))
4228     (gnus-group-position-cursor)))
4229
4230 (defun gnus-group-make-help-group ()
4231   "Create the Gnus documentation group."
4232   (interactive)
4233   (let ((path load-path)
4234         name)
4235     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4236                                    "gnus-help" '(nndoc "gnus-help")))
4237                        gnus-newsrc-hashtb)
4238          (error "Documentation group already exists"))
4239     (while (and path
4240                 (not (file-exists-p (concat (file-name-as-directory (car path))
4241                                             "doc.txt"))))
4242       (setq path (cdr path)))
4243     (or path (error "Couldn't find doc group"))
4244     (gnus-group-make-group 
4245      (gnus-group-real-name name)
4246      (list 'nndoc name
4247            (list 'nndoc-address 
4248                  (concat (file-name-as-directory (car path)) "doc.txt"))
4249            (list 'nndoc-article-type 'mbox))))
4250   (gnus-group-position-cursor))
4251
4252 (defun gnus-group-make-doc-group (file type)
4253   "Create a group that uses a single file as the source."
4254   (interactive 
4255    (list (read-file-name "File name: ") 
4256          (let ((err "")
4257                found char)
4258            (while (not found)
4259              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4260              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4261                                ((= char ?b) 'babyl)
4262                                ((= char ?d) 'digest)
4263                                (t (setq err (format "%c unknown. " char))
4264                                   nil))))
4265            found)))
4266   (let* ((file (expand-file-name file))
4267          (name (gnus-generate-new-group-name
4268                 (gnus-group-prefixed-name
4269                  (file-name-nondirectory file) '(nndoc "")))))
4270     (gnus-group-make-group 
4271      (gnus-group-real-name name)
4272      (list 'nndoc name
4273            (list 'nndoc-address file)
4274            (list 'nndoc-article-type type)))))
4275
4276 (defun gnus-group-make-archive-group (&optional all)
4277   "Create the (ding) Gnus archive group of the most recent articles.
4278 Given a prefix, create a full group."
4279   (interactive "P")
4280   (let ((group (gnus-group-prefixed-name 
4281                 (if all "ding.archives" "ding.recent") '(nndir ""))))
4282     (and (gnus-gethash group gnus-newsrc-hashtb)
4283          (error "Archive group already exists"))
4284     (gnus-group-make-group
4285      (gnus-group-real-name group)
4286      "nndir" 
4287      (if all gnus-group-archive-directory 
4288        gnus-group-recent-archive-directory)))
4289   (gnus-group-position-cursor))
4290
4291 (defun gnus-group-make-directory-group (dir)
4292   "Create an nndir group.
4293 The user will be prompted for a directory. The contents of this
4294 directory will be used as a newsgroup. The directory should contain
4295 mail messages or news articles in files that have numeric names."
4296   (interactive
4297    (list (read-file-name "Create group from directory: ")))
4298   (or (file-exists-p dir) (error "No such directory"))
4299   (or (file-directory-p dir) (error "Not a directory"))
4300   (gnus-group-make-group dir "nndir" dir)
4301   (gnus-group-position-cursor))
4302
4303 (defun gnus-group-make-kiboze-group (group address scores)
4304   "Create an nnkiboze group.
4305 The user will be prompted for a name, a regexp to match groups, and
4306 score file entries for articles to include in the group."
4307   (interactive
4308    (list
4309     (read-string "nnkiboze group name: ")
4310     (read-string "Source groups (regexp): ")
4311     (let ((headers (mapcar (lambda (group) (list group))
4312                            '("subject" "from" "number" "date" "message-id"
4313                              "references" "chars" "lines" "xref")))
4314           scores header regexp regexps)
4315       (while (not (equal "" (setq header (completing-read 
4316                                           "Match on header: " headers nil t))))
4317         (setq regexps nil)
4318         (while (not (equal "" (setq regexp (read-string 
4319                                             (format "Match on %s (string): "
4320                                                     header)))))
4321           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4322         (setq scores (cons (cons header regexps) scores)))
4323       scores)))
4324   (gnus-group-make-group group "nnkiboze" address)
4325   (save-excursion
4326     (gnus-set-work-buffer)
4327     (let (emacs-lisp-mode-hook)
4328       (pp scores (current-buffer)))
4329     (write-region (point-min) (point-max) 
4330                   (concat (or gnus-kill-files-directory "~/News")
4331                           "nnkiboze:" group "." gnus-score-file-suffix)))
4332   (gnus-group-position-cursor))
4333
4334 (defun gnus-group-add-to-virtual (n vgroup)
4335   "Add the current group to a virtual group."
4336   (interactive
4337    (list current-prefix-arg
4338          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4339                           "nnvirtual:")))
4340   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4341       (error "%s is not an nnvirtual group" vgroup))
4342   (let* ((groups (gnus-group-process-prefix n))
4343          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4344     (setcar (cdr method)
4345             (concat 
4346              (nth 1 method) "\\|"
4347              (mapconcat 
4348               (lambda (s) 
4349                 (gnus-group-remove-mark s)
4350                 (concat "\\(^" (regexp-quote s) "$\\)"))
4351               groups "\\|"))))
4352   (gnus-group-position-cursor))
4353
4354 (defun gnus-group-make-empty-virtual (group)
4355   "Create a new, fresh, empty virtual group."
4356   (interactive "sCreate new, empty virtual group: ")
4357   (let* ((method (list 'nnvirtual "^$"))
4358          (pgroup (gnus-group-prefixed-name group method)))
4359     ;; Check whether it exists already.
4360     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4361          (error "Group %s already exists." pgroup))
4362     ;; Subscribe the new group after the group on the current line.
4363     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4364     (gnus-group-update-group pgroup)
4365     (forward-line -1)
4366     (gnus-group-position-cursor)))
4367
4368 (defun gnus-group-enter-directory (dir)
4369   "Enter an ephemeral nneething group."
4370   (interactive "DDirectory to read: ")
4371   (let* ((method (list 'nneething dir))
4372          (leaf (gnus-group-prefixed-name
4373                 (file-name-nondirectory (directory-file-name dir))
4374                 method))
4375          (name (gnus-generate-new-group-name leaf)))
4376     (let ((nneething-read-only t))
4377       (or (gnus-group-read-ephemeral-group 
4378            name method t
4379            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4380                                       'summary 'group)))
4381           (error "Couldn't enter %s" dir)))))
4382
4383 ;; Group sorting commands
4384 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4385
4386 (defun gnus-group-sort-groups ()
4387   "Sort the group buffer using `gnus-group-sort-function'."
4388   (interactive)
4389   (setq gnus-newsrc-alist 
4390         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4391   (gnus-make-hashtable-from-newsrc-alist)
4392   (gnus-group-list-groups))
4393
4394 (defun gnus-group-sort-by-alphabet (info1 info2)
4395   (string< (car info1) (car info2)))
4396
4397 (defun gnus-group-sort-by-unread (info1 info2)
4398   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4399         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4400     (< (or (and (numberp n1) n1) 0)
4401        (or (and (numberp n2) n2) 0))))
4402
4403 (defun gnus-group-sort-by-level (info1 info2)
4404   (< (nth 1 info1) (nth 1 info2)))
4405
4406 ;; Group catching up.
4407
4408 (defun gnus-group-catchup-current (&optional n all)
4409   "Mark all articles not marked as unread in current newsgroup as read.
4410 If prefix argument N is numeric, the ARG next newsgroups will be
4411 caught up. If ALL is non-nil, marked articles will also be marked as
4412 read. Cross references (Xref: header) of articles are ignored.
4413 The difference between N and actual number of newsgroups that were
4414 caught up is returned."
4415   (interactive "P")
4416   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4417                gnus-expert-user
4418                (gnus-y-or-n-p
4419                 (if all
4420                     "Do you really want to mark all articles as read? "
4421                   "Mark all unread articles as read? "))))
4422       n
4423     (let ((groups (gnus-group-process-prefix n))
4424           (ret 0))
4425       (while groups
4426         ;; Virtual groups have to be given special treatment. 
4427         (let ((method (gnus-find-method-for-group (car groups))))
4428           (if (eq 'nnvirtual (car method))
4429               (nnvirtual-catchup-group
4430                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4431         (gnus-group-remove-mark (car groups))
4432         (if (prog1
4433                 (gnus-group-goto-group (car groups))
4434               (gnus-group-catchup (car groups) all))
4435             (gnus-group-update-group-line)
4436           (setq ret (1+ ret)))
4437         (setq groups (cdr groups)))
4438       (gnus-group-next-unread-group 1)
4439       ret)))
4440
4441 (defun gnus-group-catchup-current-all (&optional n)
4442   "Mark all articles in current newsgroup as read.
4443 Cross references (Xref: header) of articles are ignored."
4444   (interactive "P")
4445   (gnus-group-catchup-current n 'all))
4446
4447 (defun gnus-group-catchup (group &optional all)
4448   "Mark all articles in GROUP as read.
4449 If ALL is non-nil, all articles are marked as read.
4450 The return value is the number of articles that were marked as read,
4451 or nil if no action could be taken."
4452   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4453          (num (car entry))
4454          (marked (nth 3 (nth 2 entry))))
4455     (if (not (numberp (car entry)))
4456         (gnus-message 1 "Can't catch up; non-active group")
4457       ;; Do the updating only if the newsgroup isn't killed.
4458       (if (not entry)
4459           ()
4460         (gnus-update-read-articles 
4461          group (and (not all) (append (cdr (assq 'tick marked))
4462                                       (cdr (assq 'dormant marked))))
4463          nil (and (not all) (cdr (assq 'tick marked))))
4464         (and all 
4465              (setq marked (nth 3 (nth 2 entry)))
4466              (setcar (nthcdr 3 (nth 2 entry)) 
4467                      (delq (assq 'dormant marked) 
4468                            (nth 3 (nth 2 entry)))))))
4469     num))
4470
4471 (defun gnus-group-expire-articles (&optional n)
4472   "Expire all expirable articles in the current newsgroup."
4473   (interactive "P")
4474   (let ((groups (gnus-group-process-prefix n))
4475         group)
4476     (or groups (error "No groups to expire"))
4477     (while groups
4478       (setq group (car groups)
4479             groups (cdr groups))
4480       (gnus-group-remove-mark group)
4481       (if (not (gnus-check-backend-function 'request-expire-articles group))
4482           ()
4483         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4484                (expirable (if (memq 'total-expire (nth 5 info))
4485                               (cons nil (gnus-list-of-read-articles group))
4486                             (assq 'expire (nth 3 info)))))
4487           (and expirable 
4488                (setcdr expirable
4489                        (gnus-request-expire-articles 
4490                         (cdr expirable) group))))))))
4491
4492 (defun gnus-group-expire-all-groups ()
4493   "Expire all expirable articles in all newsgroups."
4494   (interactive)
4495   (save-excursion
4496     (gnus-message 5 "Expiring...")
4497     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4498                                      (cdr gnus-newsrc-alist))))
4499       (gnus-group-expire-articles nil)))
4500   (gnus-group-position-cursor)
4501   (gnus-message 5 "Expiring...done"))
4502
4503 (defun gnus-group-set-current-level (n level)
4504   "Set the level of the next N groups to LEVEL."
4505   (interactive "P\nnLevel: ")
4506   (or (and (>= level 1) (<= level gnus-level-killed))
4507       (error "Illegal level: %d" level))
4508   (let ((groups (gnus-group-process-prefix n))
4509         group)
4510     (while groups
4511       (setq group (car groups)
4512             groups (cdr groups))
4513       (gnus-group-remove-mark group)
4514       (gnus-message 6 "Changed level of %s from %d to %d" 
4515                     group (gnus-group-group-level) level)
4516       (gnus-group-change-level group level
4517                                (gnus-group-group-level))
4518       (gnus-group-update-group-line)))
4519   (gnus-group-position-cursor))
4520
4521 (defun gnus-group-unsubscribe-current-group (&optional n)
4522   "Toggle subscription of the current group.
4523 If given numerical prefix, toggle the N next groups."
4524   (interactive "P")
4525   (let ((groups (gnus-group-process-prefix n))
4526         group)
4527     (while groups
4528       (setq group (car groups)
4529             groups (cdr groups))
4530       (gnus-group-remove-mark group)
4531       (gnus-group-unsubscribe-group
4532        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4533                  gnus-level-default-unsubscribed
4534                gnus-level-default-subscribed))
4535       (gnus-group-update-group-line))
4536     (gnus-group-next-group 1)))
4537
4538 (defun gnus-group-unsubscribe-group (group &optional level)
4539   "Toggle subscribe from/to unsubscribe GROUP.
4540 New newsgroup is added to .newsrc automatically."
4541   (interactive
4542    (list (completing-read
4543           "Group: " gnus-active-hashtb nil 
4544           (memq gnus-select-method gnus-have-read-active-file))))
4545   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4546     (cond
4547      ((string-match "^[ \t]$" group)
4548       (error "Empty group name"))
4549      (newsrc
4550       ;; Toggle subscription flag.
4551       (gnus-group-change-level 
4552        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4553                                       gnus-level-subscribed) 
4554                                   (1+ gnus-level-subscribed)
4555                                 gnus-level-default-subscribed)))
4556       (gnus-group-update-group group))
4557      ((and (stringp group)
4558            (or (not (memq gnus-select-method gnus-have-read-active-file))
4559                (gnus-gethash group gnus-active-hashtb)))
4560       ;; Add new newsgroup.
4561       (gnus-group-change-level 
4562        group 
4563        (if level level gnus-level-default-subscribed) 
4564        (or (and (member group gnus-zombie-list) 
4565                 gnus-level-zombie) 
4566            gnus-level-killed)
4567        (and (gnus-group-group-name)
4568             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4569       (gnus-group-update-group group))
4570      (t (error "No such newsgroup: %s" group)))
4571     (gnus-group-position-cursor)))
4572
4573 (defun gnus-group-transpose-groups (n)
4574   "Move the current newsgroup up N places.
4575 If given a negative prefix, move down instead. The difference between
4576 N and the number of steps taken is returned." 
4577   (interactive "p")
4578   (or (gnus-group-group-name)
4579       (error "No group on current line"))
4580   (gnus-group-kill-group 1)
4581   (prog1
4582       (forward-line (- n))
4583     (gnus-group-yank-group)
4584     (gnus-group-position-cursor)))
4585
4586 (defun gnus-group-kill-all-zombies ()
4587   "Kill all zombie newsgroups."
4588   (interactive)
4589   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4590   (setq gnus-zombie-list nil)
4591   (gnus-group-list-groups))
4592
4593 (defun gnus-group-kill-region (begin end)
4594   "Kill newsgroups in current region (excluding current point).
4595 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4596   (interactive "r")
4597   (let ((lines
4598          ;; Count lines.
4599          (save-excursion
4600            (count-lines
4601             (progn
4602               (goto-char begin)
4603               (beginning-of-line)
4604               (point))
4605             (progn
4606               (goto-char end)
4607               (beginning-of-line)
4608               (point))))))
4609     (goto-char begin)
4610     (beginning-of-line)                 ;Important when LINES < 1
4611     (gnus-group-kill-group lines)))
4612
4613 (defun gnus-group-kill-group (&optional n)
4614   "The the next N groups.
4615 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4616 However, only groups that were alive can be yanked; already killed 
4617 groups or zombie groups can't be yanked.
4618 The return value is the name of the (last) group that was killed."
4619   (interactive "P")
4620   (let ((buffer-read-only nil)
4621         (groups (gnus-group-process-prefix n))
4622         group entry level)
4623     (while groups
4624       (setq group (car groups)
4625             groups (cdr groups))
4626       (gnus-group-remove-mark group)
4627       (setq level (gnus-group-group-level))
4628       (gnus-delete-line)
4629       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4630           (setq gnus-list-of-killed-groups 
4631                 (cons (cons (car entry) (nth 2 entry)) 
4632                       gnus-list-of-killed-groups)))
4633       (gnus-group-change-level 
4634        (if entry entry group) gnus-level-killed (if entry nil level)))
4635     (gnus-group-position-cursor)
4636     group))
4637
4638 (defun gnus-group-yank-group (&optional arg)
4639   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4640 inserting it before the current newsgroup.  The numeric ARG specifies
4641 how many newsgroups are to be yanked.  The name of the (last)
4642 newsgroup yanked is returned."
4643   (interactive "p")
4644   (if (not arg) (setq arg 1))
4645   (let (info group prev)
4646     (while (>= (setq arg (1- arg)) 0)
4647       (if (not (setq info (car gnus-list-of-killed-groups)))
4648           (error "No more newsgroups to yank"))
4649       (setq group (nth 2 info))
4650       ;; Find which newsgroup to insert this one before - search
4651       ;; backward until something suitable is found. If there are no
4652       ;; other newsgroups in this buffer, just make this newsgroup the
4653       ;; first newsgroup.
4654       (setq prev (gnus-group-group-name))
4655       (gnus-group-change-level 
4656        info (nth 2 info) gnus-level-killed 
4657        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4658        t)
4659       (gnus-group-insert-group-line-info (nth 1 info))
4660       (setq gnus-list-of-killed-groups 
4661             (cdr gnus-list-of-killed-groups)))
4662     (forward-line -1)
4663     (gnus-group-position-cursor)
4664     group))
4665       
4666 (defun gnus-group-list-all-groups (&optional arg)
4667   "List all newsgroups with level ARG or lower.
4668 Default is gnus-level-unsubscribed, which lists all subscribed and most
4669 unsubscribed groups."
4670   (interactive "P")
4671   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4672
4673 (defun gnus-group-list-killed ()
4674   "List all killed newsgroups in the group buffer."
4675   (interactive)
4676   (if (not gnus-killed-list)
4677       (gnus-message 6 "No killed groups")
4678     (let (gnus-group-list-mode)
4679       (funcall gnus-group-prepare-function 
4680                gnus-level-killed t gnus-level-killed))
4681     (goto-char (point-min)))
4682   (gnus-group-position-cursor))
4683
4684 (defun gnus-group-list-zombies ()
4685   "List all zombie newsgroups in the group buffer."
4686   (interactive)
4687   (if (not gnus-zombie-list)
4688       (gnus-message 6 "No zombie groups")
4689     (let (gnus-group-list-mode)
4690       (funcall gnus-group-prepare-function
4691                gnus-level-zombie t gnus-level-zombie))
4692     (goto-char (point-min)))
4693   (gnus-group-position-cursor))
4694
4695 (defun gnus-group-get-new-news (&optional arg)
4696   "Get newly arrived articles.
4697 If ARG is non-nil, it should be a number between one and nine to
4698 specify which levels you are interested in re-scanning."
4699   (interactive "P")
4700   (run-hooks 'gnus-get-new-news-hook)
4701   (setq arg (gnus-group-default-level arg t))
4702   (if (and gnus-read-active-file (not arg))
4703       (progn
4704         (gnus-read-active-file)
4705         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4706     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
4707       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4708   (gnus-group-list-groups))
4709
4710 (defun gnus-group-get-new-news-this-group (&optional n)
4711   "Check for newly arrived news in the current group (and the N-1 next groups).
4712 The difference between N and the number of newsgroup checked is returned.
4713 If N is negative, this group and the N-1 previous groups will be checked."
4714   (interactive "P")
4715   (let* ((groups (gnus-group-process-prefix n))
4716          (ret (if (numberp n) (- n (length groups)) 0))
4717          group)
4718     (while groups
4719       (setq group (car groups)
4720             groups (cdr groups))
4721       (gnus-group-remove-mark group)
4722       (or (gnus-get-new-news-in-group group)
4723           (progn 
4724             (ding) 
4725             (message "%s error: %s" group (gnus-status-message group))
4726             (sit-for 2))))
4727     (gnus-group-next-unread-group 1 t)
4728     (gnus-summary-position-cursor)
4729     ret))
4730
4731 (defun gnus-get-new-news-in-group (group)
4732   (and group 
4733        (gnus-activate-group group)
4734        (progn
4735          (gnus-get-unread-articles-in-group 
4736           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4737           (gnus-gethash group gnus-active-hashtb))
4738          (gnus-group-update-group-line)
4739          t)))
4740
4741 (defun gnus-group-fetch-faq (group)
4742   "Fetch the FAQ for the current group."
4743   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4744   (or group (error "No group name given"))
4745   (let ((file (concat gnus-group-faq-directory (gnus-group-real-name group))))
4746     (if (not (file-exists-p file))
4747         (error "No such file: %s" file)
4748       (find-file file))))
4749   
4750 (defun gnus-group-describe-group (force &optional group)
4751   "Display a description of the current newsgroup."
4752   (interactive (list current-prefix-arg (gnus-group-group-name)))
4753   (and force (setq gnus-description-hashtb nil))
4754   (let ((method (gnus-find-method-for-group group))
4755         desc)
4756     (or group (error "No group name given"))
4757     (and (or (and gnus-description-hashtb
4758                   ;; We check whether this group's method has been
4759                   ;; queried for a description file.  
4760                   (gnus-gethash 
4761                    (gnus-group-prefixed-name "" method) 
4762                    gnus-description-hashtb))
4763              (setq desc (gnus-group-get-description group))
4764              (gnus-read-descriptions-file method))
4765          (message
4766           (or desc (gnus-gethash group gnus-description-hashtb)
4767               "No description available")))))
4768
4769 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4770 (defun gnus-group-describe-all-groups (&optional force)
4771   "Pop up a buffer with descriptions of all newsgroups."
4772   (interactive "P")
4773   (and force (setq gnus-description-hashtb nil))
4774   (if (not (or gnus-description-hashtb
4775                (gnus-read-all-descriptions-files)))
4776       (error "Couldn't request descriptions file"))
4777   (let ((buffer-read-only nil)
4778         b)
4779     (erase-buffer)
4780     (mapatoms
4781      (lambda (group)
4782        (setq b (point))
4783        (insert (format "      *: %-20s %s\n" (symbol-name group)
4784                        (symbol-value group)))
4785        (add-text-properties 
4786         b (1+ b) (list 'gnus-group group
4787                        'gnus-unread t 'gnus-marked nil
4788                        'gnus-level (1+ gnus-level-subscribed))))
4789      gnus-description-hashtb)
4790     (goto-char (point-min))
4791     (gnus-group-position-cursor)))
4792
4793 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4794 (defun gnus-group-apropos (regexp &optional search-description)
4795   "List all newsgroups that have names that match a regexp."
4796   (interactive "sGnus apropos (regexp): ")
4797   (let ((prev "")
4798         (obuf (current-buffer))
4799         groups des)
4800     ;; Go through all newsgroups that are known to Gnus.
4801     (mapatoms 
4802      (lambda (group)
4803        (and (symbol-name group)
4804             (string-match regexp (symbol-name group))
4805             (setq groups (cons (symbol-name group) groups))))
4806      gnus-active-hashtb)
4807     ;; Go through all descriptions that are known to Gnus. 
4808     (if search-description
4809         (mapatoms 
4810          (lambda (group)
4811            (and (string-match regexp (symbol-value group))
4812                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4813                 (setq groups (cons (symbol-name group) groups))))
4814          gnus-description-hashtb))
4815     (if (not groups)
4816         (gnus-message 3 "No groups matched \"%s\"." regexp)
4817       ;; Print out all the groups.
4818       (save-excursion
4819         (pop-to-buffer "*Gnus Help*")
4820         (buffer-disable-undo (current-buffer))
4821         (erase-buffer)
4822         (setq groups (sort groups 'string<))
4823         (while groups
4824           ;; Groups may be entered twice into the list of groups.
4825           (if (not (string= (car groups) prev))
4826               (progn
4827                 (insert (setq prev (car groups)) "\n")
4828                 (if (and gnus-description-hashtb
4829                          (setq des (gnus-gethash (car groups) 
4830                                                  gnus-description-hashtb)))
4831                     (insert "  " des "\n"))))
4832           (setq groups (cdr groups)))
4833         (goto-char (point-min))))
4834     (pop-to-buffer obuf)))
4835
4836 (defun gnus-group-description-apropos (regexp)
4837   "List all newsgroups that have names or descriptions that match a regexp."
4838   (interactive "sGnus description apropos (regexp): ")
4839   (if (not (or gnus-description-hashtb
4840                (gnus-read-all-descriptions-files)))
4841       (error "Couldn't request descriptions file"))
4842   (gnus-group-apropos regexp t))
4843
4844 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4845 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4846   "List all groups with unread articles that match REGEXP.
4847 If the prefix LEVEL is non-nil, it should be a number that says which
4848 level to cut off listing groups. 
4849 If ALL, also list groups with no unread articles.
4850 If LOWEST, don't list groups with level lower than LOWEST."
4851   (interactive "P\nsList newsgroups matching: ")
4852   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4853                            all (or lowest 1) regexp)
4854   (goto-char (point-min))
4855   (gnus-group-position-cursor))
4856
4857 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4858   "List all groups that match REGEXP.
4859 If the prefix LEVEL is non-nil, it should be a number that says which
4860 level to cut off listing groups. 
4861 If LOWEST, don't list groups with level lower than LOWEST."
4862   (interactive "P\nsList newsgroups matching: ")
4863   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4864
4865 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4866 (defun gnus-group-save-newsrc ()
4867   "Save the Gnus startup files."
4868   (interactive)
4869   (gnus-save-newsrc-file))
4870
4871 (defun gnus-group-restart (&optional arg)
4872   "Force Gnus to read the .newsrc file."
4873   (interactive "P")
4874   (gnus-save-newsrc-file)
4875   (gnus-setup-news 'force)
4876   (gnus-group-list-groups arg))
4877
4878 (defun gnus-group-read-init-file ()
4879   "Read the Gnus elisp init file."
4880   (interactive)
4881   (gnus-read-init-file))
4882
4883 (defun gnus-group-check-bogus-groups (&optional silent)
4884   "Check bogus newsgroups.
4885 If given a prefix, don't ask for confirmation before removing a bogus
4886 group."
4887   (interactive "P")
4888   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4889   (gnus-group-list-groups))
4890
4891 (defun gnus-group-edit-global-kill (&optional article group)
4892   "Edit the global kill file.
4893 If GROUP, edit that local kill file instead."
4894   (interactive "P")
4895   (setq gnus-current-kill-article article)
4896   (gnus-kill-file-edit-file group)
4897   (gnus-message 
4898    6
4899    (substitute-command-keys
4900     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4901
4902 (defun gnus-group-edit-local-kill (article group)
4903   "Edit a local kill file."
4904   (interactive (list nil (gnus-group-group-name)))
4905   (gnus-group-edit-global-kill article group))
4906
4907 (defun gnus-group-force-update ()
4908   "Update `.newsrc' file."
4909   (interactive)
4910   (gnus-save-newsrc-file))
4911
4912 (defun gnus-group-suspend ()
4913   "Suspend the current Gnus session.
4914 In fact, cleanup buffers except for group mode buffer.
4915 The hook gnus-suspend-gnus-hook is called before actually suspending."
4916   (interactive)
4917   (run-hooks 'gnus-suspend-gnus-hook)
4918   ;; Kill Gnus buffers except for group mode buffer.
4919   (let ((group-buf (get-buffer gnus-group-buffer)))
4920     ;; Do this on a separate list in case the user does a ^G before we finish
4921     (let ((gnus-buffer-list
4922            (delq group-buf (delq gnus-dribble-buffer
4923                                  (append gnus-buffer-list nil)))))
4924       (while gnus-buffer-list
4925         (gnus-kill-buffer (car gnus-buffer-list))
4926         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4927     (if group-buf
4928         (progn
4929           (setq gnus-buffer-list (list group-buf))
4930           (bury-buffer group-buf)
4931           (delete-windows-on group-buf t)))))
4932
4933 (defun gnus-group-clear-dribble ()
4934   "Clear all information from the dribble buffer."
4935   (interactive)
4936   (gnus-dribble-clear))
4937
4938 (defun gnus-group-exit ()
4939   "Quit reading news after updating .newsrc.eld and .newsrc.
4940 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4941   (interactive)
4942   (if (or noninteractive                ;For gnus-batch-kill
4943           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4944           (not gnus-interactive-exit)   ;Without confirmation
4945           gnus-expert-user
4946           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4947       (progn
4948         (run-hooks 'gnus-exit-gnus-hook)
4949         ;; Offer to save data from non-quitted summary buffers.
4950         (gnus-offer-save-summaries)
4951         ;; Save the newsrc file(s).
4952         (gnus-save-newsrc-file)
4953         ;; Kill-em-all.
4954         (gnus-close-backends)
4955         ;; Reset everything.
4956         (gnus-clear-system))))
4957
4958 (defun gnus-close-backends ()
4959   ;; Send a close request to all backends that support such a request. 
4960   (let ((methods gnus-valid-select-methods)
4961         func)
4962     (while methods
4963       (if (fboundp (setq func (intern (concat (car (car methods))
4964                                               "-request-close"))))
4965           (funcall func))
4966       (setq methods (cdr methods)))))
4967
4968 (defun gnus-group-quit ()
4969   "Quit reading news without updating .newsrc.eld or .newsrc.
4970 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4971   (interactive)
4972   (if (or noninteractive                ;For gnus-batch-kill
4973           (zerop (buffer-size))
4974           (not (gnus-server-opened gnus-select-method))
4975           gnus-expert-user
4976           (not gnus-current-startup-file)
4977           (gnus-yes-or-no-p
4978            (format "Quit reading news without saving %s? "
4979                    (file-name-nondirectory gnus-current-startup-file))))
4980       (progn
4981         (run-hooks 'gnus-exit-gnus-hook)
4982         (if gnus-use-full-window
4983             (delete-other-windows)
4984           (gnus-remove-some-windows))
4985         (gnus-dribble-save)
4986         (gnus-close-backends)
4987         (gnus-clear-system))))
4988
4989 (defun gnus-offer-save-summaries ()
4990   (save-excursion
4991     (let ((buflist (buffer-list)) 
4992           buffers bufname)
4993       (while buflist
4994         (and (setq bufname (buffer-name (car buflist)))
4995              (string-match "Summary" bufname)
4996              (save-excursion
4997                (set-buffer bufname)
4998                ;; We check that this is, indeed, a summary buffer.
4999                (eq major-mode 'gnus-summary-mode))
5000              (setq buffers (cons bufname buffers)))
5001         (setq buflist (cdr buflist)))
5002       (and buffers
5003            (map-y-or-n-p 
5004             "Update summary buffer %s? "
5005             (lambda (buf)
5006               (set-buffer buf)
5007               (gnus-summary-exit))
5008             buffers)))))
5009
5010 (defun gnus-group-describe-briefly ()
5011   "Give a one line description of the group mode commands."
5012   (interactive)
5013   (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")))
5014
5015 (defun gnus-group-browse-foreign-server (method)
5016   "Browse a foreign news server.
5017 If called interactively, this function will ask for a select method
5018  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
5019 If not, METHOD should be a list where the first element is the method
5020 and the second element is the address."
5021   (interactive
5022    (list (let ((how (completing-read 
5023                      "Which backend: "
5024                      (append gnus-valid-select-methods gnus-server-alist)
5025                      nil t "nntp")))
5026            ;; We either got a backend name or a virtual server name.
5027            ;; If the first, we also need an address.
5028            (if (assoc how gnus-valid-select-methods)
5029                (list (intern how)
5030                      ;; Suggested by mapjph@bath.ac.uk.
5031                      (completing-read 
5032                       "Address: " 
5033                       (mapcar (lambda (server) (list server))
5034                               gnus-secondary-servers)))
5035              ;; We got a server name, so we find the method.
5036              (gnus-server-to-method how)))))
5037   (gnus-browse-foreign-server method))
5038
5039 \f
5040 ;;;
5041 ;;; Browse Server Mode
5042 ;;;
5043
5044 (defvar gnus-browse-mode-hook nil)
5045 (defvar gnus-browse-mode-map nil)
5046 (put 'gnus-browse-mode 'mode-class 'special)
5047
5048 (if gnus-browse-mode-map
5049     nil
5050   (setq gnus-browse-mode-map (make-keymap))
5051   (suppress-keymap gnus-browse-mode-map)
5052   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
5053   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
5054   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
5055   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
5056   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
5057   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
5058   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
5059   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
5060   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
5061   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
5062   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
5063   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
5064   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
5065   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
5066   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
5067   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
5068   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
5069   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
5070   )
5071
5072 (defvar gnus-browse-current-method nil)
5073 (defvar gnus-browse-return-buffer nil)
5074
5075 (defvar gnus-browse-buffer "*Gnus Browse Server*")
5076
5077 (defun gnus-browse-foreign-server (method &optional return-buffer)
5078   (setq gnus-browse-current-method method)
5079   (setq gnus-browse-return-buffer return-buffer)
5080   (let ((gnus-select-method method)
5081         groups group)
5082     (gnus-message 5 "Connecting to %s..." (nth 1 method))
5083     (or (gnus-check-server method)
5084         (error "Unable to contact server: %s" (gnus-status-message method)))
5085     (or (gnus-request-list method)
5086         (error "Couldn't request list: %s" (gnus-status-message method)))
5087     (get-buffer-create gnus-browse-buffer)
5088     (gnus-add-current-to-buffer-list)
5089     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
5090     (gnus-configure-windows 'browse)
5091     (buffer-disable-undo (current-buffer))
5092     (let ((buffer-read-only nil))
5093       (erase-buffer))
5094     (gnus-browse-mode)
5095     (setq mode-line-buffer-identification
5096           (format
5097            "Gnus  Browse Server {%s:%s}" (car method) (car (cdr method))))
5098     (save-excursion
5099       (set-buffer nntp-server-buffer)
5100       (let ((cur (current-buffer)))
5101         (goto-char (point-min))
5102         (or (string= gnus-ignored-newsgroups "")
5103             (delete-matching-lines gnus-ignored-newsgroups))
5104         (while (re-search-forward 
5105                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
5106           (goto-char (match-end 1))
5107           (setq groups (cons (cons (buffer-substring (match-beginning 1)
5108                                                      (match-end 1))
5109                                    (max 0 (- (1+ (read cur)) (read cur))))
5110                              groups)))))
5111     (setq groups (sort groups 
5112                        (lambda (l1 l2)
5113                          (string< (car l1) (car l2)))))
5114     (let ((buffer-read-only nil))
5115       (while groups
5116         (setq group (car groups))
5117         (insert 
5118          (format "K%7d: %s\n" (cdr group) (car group)))
5119         (setq groups (cdr groups))))
5120     (switch-to-buffer (current-buffer))
5121     (goto-char (point-min))
5122     (gnus-group-position-cursor)))
5123
5124 (defun gnus-browse-mode ()
5125   "Major mode for browsing a foreign server.
5126
5127 All normal editing commands are switched off.
5128
5129 \\<gnus-browse-mode-map>
5130 The only things you can do in this buffer is
5131
5132 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5133 The group will be inserted into the group buffer upon exit from this
5134 buffer.  
5135
5136 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5137
5138 3) `\\[gnus-browse-exit]' to return to the group buffer."
5139   (interactive)
5140   (kill-all-local-variables)
5141   (if gnus-visual (gnus-browse-make-menu-bar))
5142   (setq mode-line-modified "-- ")
5143   (make-local-variable 'mode-line-format)
5144   (setq mode-line-format (copy-sequence mode-line-format))
5145   (and (equal (nth 3 mode-line-format) "   ")
5146        (setcar (nthcdr 3 mode-line-format) ""))
5147   (setq major-mode 'gnus-browse-mode)
5148   (setq mode-name "Browse Server")
5149   (setq mode-line-process nil)
5150   (use-local-map gnus-browse-mode-map)
5151   (buffer-disable-undo (current-buffer))
5152   (setq truncate-lines t)
5153   (setq buffer-read-only t)
5154   (run-hooks 'gnus-browse-mode-hook))
5155
5156 (defun gnus-browse-read-group (&optional no-article)
5157   "Enter the group at the current line."
5158   (interactive)
5159   (let ((group (gnus-browse-group-name)))
5160     (or (gnus-group-read-ephemeral-group 
5161          group gnus-browse-current-method nil
5162          (cons (current-buffer) 'browse))
5163         (error "Couldn't enter %s" group))))
5164
5165 (defun gnus-browse-select-group ()
5166   "Select the current group."
5167   (interactive)
5168   (gnus-browse-read-group 'no))
5169
5170 (defun gnus-browse-next-group (n)
5171   "Go to the next group."
5172   (interactive "p")
5173   (prog1
5174       (forward-line n)
5175     (gnus-group-position-cursor)))
5176
5177 (defun gnus-browse-prev-group (n)
5178   "Go to the next group."
5179   (interactive "p")
5180   (gnus-browse-next-group (- n)))
5181
5182 (defun gnus-browse-unsubscribe-current-group (arg)
5183   "(Un)subscribe to the next ARG groups."
5184   (interactive "p")
5185   (and (eobp)
5186        (error "No group at current line."))
5187   (let ((ward (if (< arg 0) -1 1))
5188         (arg (abs arg)))
5189     (while (and (> arg 0)
5190                 (not (eobp))
5191                 (gnus-browse-unsubscribe-group)
5192                 (zerop (gnus-browse-next-group ward)))
5193       (setq arg (1- arg)))
5194     (gnus-group-position-cursor)
5195     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5196     arg))
5197
5198 (defun gnus-browse-group-name ()
5199   (save-excursion
5200     (beginning-of-line)
5201     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5202         ()
5203       (gnus-group-prefixed-name 
5204        (buffer-substring (match-beginning 1) (match-end 1))
5205        gnus-browse-current-method))))
5206   
5207 (defun gnus-browse-unsubscribe-group ()
5208   (let ((sub nil)
5209         (buffer-read-only nil)
5210         group)
5211     (save-excursion
5212       (beginning-of-line)
5213       (if (= (following-char) ?K) (setq sub t))
5214       (setq group (gnus-browse-group-name))
5215       (beginning-of-line)
5216       (delete-char 1)
5217       (if sub
5218           (progn
5219             (gnus-group-change-level 
5220              (list t group gnus-level-default-subscribed
5221                    nil nil gnus-browse-current-method) 
5222              gnus-level-default-subscribed gnus-level-killed
5223              (and (car (nth 1 gnus-newsrc-alist))
5224                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5225                                 gnus-newsrc-hashtb))
5226              t)
5227             (insert ? ))
5228         (gnus-group-change-level 
5229          group gnus-level-killed gnus-level-default-subscribed)
5230         (insert ?K)))
5231     t))
5232
5233 (defun gnus-browse-exit ()
5234   "Quit browsing and return to the group buffer."
5235   (interactive)
5236   (if (eq major-mode 'gnus-browse-mode)
5237       (kill-buffer (current-buffer)))
5238   (if gnus-browse-return-buffer
5239       (gnus-configure-windows 'server 'force)
5240     (gnus-configure-windows 'group 'force)
5241     (gnus-group-list-groups nil)))
5242
5243 (defun gnus-browse-describe-briefly ()
5244   "Give a one line description of the group mode commands."
5245   (interactive)
5246   (gnus-message 6
5247                 (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")))
5248       
5249 \f
5250 ;;;
5251 ;;; Gnus summary mode
5252 ;;;
5253
5254 (defvar gnus-summary-mode-map nil)
5255 (defvar gnus-summary-mark-map nil)
5256 (defvar gnus-summary-mscore-map nil)
5257 (defvar gnus-summary-article-map nil)
5258 (defvar gnus-summary-thread-map nil)
5259 (defvar gnus-summary-goto-map nil)
5260 (defvar gnus-summary-exit-map nil)
5261 (defvar gnus-summary-interest-map nil)
5262 (defvar gnus-summary-sort-map nil)
5263 (defvar gnus-summary-backend-map nil)
5264 (defvar gnus-summary-save-map nil)
5265 (defvar gnus-summary-wash-map nil)
5266 (defvar gnus-summary-wash-hide-map nil)
5267 (defvar gnus-summary-wash-highlight-map nil)
5268 (defvar gnus-summary-wash-time-map nil)
5269 (defvar gnus-summary-help-map nil)
5270
5271 (put 'gnus-summary-mode 'mode-class 'special)
5272
5273 (if gnus-summary-mode-map
5274     nil
5275   (setq gnus-summary-mode-map (make-keymap))
5276   (suppress-keymap gnus-summary-mode-map)
5277
5278   ;; Non-orthogonal keys
5279
5280   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5281   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5282   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5283   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5284   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5285   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5286   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5287   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5288   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5289   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5290   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5291   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5292   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5293   (define-key gnus-summary-mode-map 
5294     "\M-s" 'gnus-summary-search-article-forward)
5295   (define-key gnus-summary-mode-map 
5296     "\M-r" 'gnus-summary-search-article-backward)
5297   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5298   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5299   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5300   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5301   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5302   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5303   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5304   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5305   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5306   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5307   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5308   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5309   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5310   (define-key gnus-summary-mode-map 
5311     "k" 'gnus-summary-kill-same-subject-and-select)
5312   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5313   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5314   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5315   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5316   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5317   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5318   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5319   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5320   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5321   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5322   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5323   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5324   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5325   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5326   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5327   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5328   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5329   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5330   (define-key gnus-summary-mode-map 
5331     "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5332   (define-key gnus-summary-mode-map 
5333     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5334   (define-key gnus-summary-mode-map 
5335     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5336   (define-key gnus-summary-mode-map 
5337     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5338   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5339   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5340   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5341   (define-key gnus-summary-mode-map 
5342     "\C-x\C-s" 'gnus-summary-reselect-current-group)
5343   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5344   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5345   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5346   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5347   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5348   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5349   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5350   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5351   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5352   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5353   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5354   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5355   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5356   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5357   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5358   (define-key gnus-summary-mode-map "V" 'gnus-version)
5359   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5360   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5361   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5362   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5363   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5364   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5365   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5366   (define-key gnus-summary-mode-map 
5367     "x" 'gnus-summary-remove-lines-marked-as-read)
5368 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5369   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5370   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5371   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5372 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5373   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5374   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5375   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5376   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5377   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5378
5379
5380   ;; Sort of orthogonal keymap
5381   (define-prefix-command 'gnus-summary-mark-map)
5382   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5383   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5384   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5385   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5386   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5387   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5388   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5389   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5390   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5391   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5392   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5393   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5394   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5395   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5396   (define-key gnus-summary-mark-map 
5397     "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5398   (define-key gnus-summary-mark-map 
5399     "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5400   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5401   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5402   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5403   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5404   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5405   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5406   (define-key gnus-summary-mark-map 
5407     "k" 'gnus-summary-kill-same-subject-and-select)
5408   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5409
5410   (define-prefix-command 'gnus-summary-mscore-map)
5411   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5412   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5413   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5414   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5415   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5416
5417   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5418   
5419   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5420   
5421   (define-prefix-command 'gnus-summary-goto-map)
5422   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5423   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5424   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5425   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5426   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5427   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5428   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5429   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5430   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5431   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5432   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5433   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5434   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5435   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5436
5437
5438   (define-prefix-command 'gnus-summary-thread-map)
5439   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5440   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5441   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5442   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5443   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5444   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5445   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5446   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5447   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5448   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5449   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5450   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5451   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5452   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5453
5454   
5455   (define-prefix-command 'gnus-summary-exit-map)
5456   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5457   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5458   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5459   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5460   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5461   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5462   (define-key gnus-summary-exit-map 
5463     "n" 'gnus-summary-catchup-and-goto-next-group)
5464   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5465   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5466   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5467   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5468
5469
5470   (define-prefix-command 'gnus-summary-article-map)
5471   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5472   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5473   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5474   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5475   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5476   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5477   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5478   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5479   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5480   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5481   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5482   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5483   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5484   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5485
5486
5487
5488   (define-prefix-command 'gnus-summary-wash-map)
5489   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5490
5491   (define-prefix-command 'gnus-summary-wash-hide-map)
5492   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5493   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5494   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5495   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5496   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5497   (define-key gnus-summary-wash-hide-map 
5498     "\C-c" 'gnus-article-hide-citation-maybe)
5499
5500   (define-prefix-command 'gnus-summary-wash-highlight-map)
5501   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5502   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5503   (define-key gnus-summary-wash-highlight-map 
5504     "h" 'gnus-article-highlight-headers)
5505   (define-key gnus-summary-wash-highlight-map
5506     "c" 'gnus-article-highlight-citation)
5507   (define-key gnus-summary-wash-highlight-map
5508     "s" 'gnus-article-highlight-signature)
5509
5510   (define-prefix-command 'gnus-summary-wash-time-map)
5511   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5512   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5513   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5514   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5515   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5516
5517   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5518   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5519   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5520   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5521   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5522   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5523   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5524   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5525   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5526   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5527
5528
5529   (define-prefix-command 'gnus-summary-help-map)
5530   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5531   (define-key gnus-summary-help-map "v" 'gnus-version)
5532   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5533   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5534   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5535   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5536
5537
5538   (define-prefix-command 'gnus-summary-backend-map)
5539   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5540   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5541   (define-key gnus-summary-backend-map "\M-\C-e" 
5542     'gnus-summary-expire-articles-now)
5543   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5544   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5545   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5546   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5547   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5548   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5549   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5550
5551
5552   (define-prefix-command 'gnus-summary-save-map)
5553   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5554   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5555   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5556   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5557   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5558   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5559   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5560   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5561 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5562
5563   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5564
5565   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5566 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5567 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5568 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5569 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5570 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5571 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5572   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5573 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5574 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5575
5576   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5577
5578 ;  (define-prefix-command 'gnus-summary-sort-map)
5579 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5580 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5581 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5582 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5583 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5584 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5585
5586   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5587   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5588   )
5589
5590
5591 \f
5592
5593 (defun gnus-summary-mode (&optional group)
5594   "Major mode for reading articles.
5595
5596 All normal editing commands are switched off.
5597 \\<gnus-summary-mode-map>
5598 Each line in this buffer represents one article.  To read an
5599 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5600 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5601 respectively.
5602
5603 You can also post articles and send mail from this buffer.  To 
5604 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5605 of an article, type `\\[gnus-summary-reply]'.
5606
5607 There are approx. one gazillion commands you can execute in this 
5608 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5609
5610 The following commands are available:
5611
5612 \\{gnus-summary-mode-map}"
5613   (interactive)
5614   (if gnus-visual (gnus-summary-make-menu-bar))
5615   (kill-all-local-variables)
5616   (let ((locals gnus-summary-local-variables))
5617     (while locals
5618       (if (consp (car locals))
5619           (progn
5620             (make-local-variable (car (car locals)))
5621             (set (car (car locals)) (eval (cdr (car locals)))))
5622         (make-local-variable (car locals))
5623         (set (car locals) nil))
5624       (setq locals (cdr locals))))
5625   (gnus-make-thread-indent-array)
5626   (setq mode-line-modified "-- ")
5627   (make-local-variable 'mode-line-format)
5628   (setq mode-line-format (copy-sequence mode-line-format))
5629   (and (equal (nth 3 mode-line-format) "   ")
5630        (setcar (nthcdr 3 mode-line-format) ""))
5631   (setq major-mode 'gnus-summary-mode)
5632   (setq mode-name "Summary")
5633   (make-local-variable 'minor-mode-alist)
5634   (use-local-map gnus-summary-mode-map)
5635   (buffer-disable-undo (current-buffer))
5636   (setq buffer-read-only t)             ;Disable modification
5637   (setq truncate-lines t)
5638   (setq selective-display t)
5639   (setq selective-display-ellipses t)   ;Display `...'
5640   (setq buffer-display-table gnus-summary-display-table)
5641   (setq gnus-newsgroup-name group)
5642   (run-hooks 'gnus-summary-mode-hook))
5643
5644 (defun gnus-summary-make-display-table ()
5645   ;; Change the display table.  Odd characters have a tendency to mess
5646   ;; up nicely formatted displays - we make all possible glyphs
5647   ;; display only a single character.
5648
5649   ;; We start from the standard display table, if any.
5650   (setq gnus-summary-display-table 
5651         (or (copy-sequence standard-display-table)
5652             (make-display-table)))
5653   ;; Nix out all the control chars...
5654   (let ((i 32))
5655     (while (>= (setq i (1- i)) 0)
5656       (aset gnus-summary-display-table i [??])))
5657   ;; ... but not newline and cr, of course. (cr is necessary for the
5658   ;; selective display).  
5659   (aset gnus-summary-display-table ?\n nil)
5660   (aset gnus-summary-display-table ?\r nil)
5661   ;; We nix out any glyphs over 126 that are not set already.  
5662   (let ((i 256))
5663     (while (>= (setq i (1- i)) 127)
5664       ;; Only modify if the entry is nil.
5665       (or (aref gnus-summary-display-table i) 
5666           (aset gnus-summary-display-table i [??])))))
5667
5668 (defun gnus-summary-clear-local-variables ()
5669   (let ((locals gnus-summary-local-variables))
5670     (while locals
5671       (if (consp (car locals))
5672           (and (vectorp (car (car locals)))
5673                (set (car (car locals)) nil))
5674         (and (vectorp (car locals))
5675              (set (car locals) nil)))
5676       (setq locals (cdr locals)))))
5677
5678 ;; Some summary mode macros.
5679
5680 ;; Return a header specified by a NUMBER.
5681 (defun gnus-get-header-by-number (number)
5682   (save-excursion
5683     (set-buffer gnus-summary-buffer)
5684     (or gnus-newsgroup-headers-hashtb-by-number
5685         (gnus-make-headers-hashtable-by-number))
5686     (gnus-gethash (int-to-string number)
5687                   gnus-newsgroup-headers-hashtb-by-number)))
5688
5689 ;; Fast version of the function above.
5690 (defmacro gnus-get-header-by-num (number)
5691   (` (gnus-gethash (int-to-string (, number)) 
5692                    gnus-newsgroup-headers-hashtb-by-number)))
5693
5694 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5695   "Search for article forward.
5696 If UNREAD is non-nil, only unread articles are selected.
5697 If SUBJECT is non-nil, the article which has the same subject will be
5698 searched for. 
5699 If BACKWARD is non-nil, the search will be performed backwards instead."
5700   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5701
5702 (defmacro gnus-summary-search-backward (&optional unread subject)
5703   "Search for article backward.
5704 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5705 If 2nd optional argument SUBJECT is non-nil, the article which has
5706 the same subject will be searched for."
5707   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5708
5709 (defmacro gnus-summary-article-number (&optional number-or-nil)
5710   "The article number of the article on the current line.
5711 If there isn's an article number here, then we return the current
5712 article number."
5713   (if number-or-nil
5714       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5715     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5716          gnus-current-article)))
5717
5718 (defmacro gnus-summary-thread-level ()
5719   "The thread level of the article on the current line."
5720   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5721        0))
5722
5723 (defmacro gnus-summary-article-mark ()
5724   "The mark on the current line."
5725   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5726
5727 (defun gnus-summary-subject-string ()
5728   "Return current subject string or nil if nothing."
5729   (let ((article (gnus-summary-article-number))
5730         header)
5731     (and article 
5732          (setq header (gnus-get-header-by-num article))
5733          (vectorp header)
5734          (mail-header-subject header))))
5735
5736 ;; Various summary mode internalish functions.
5737
5738 (defun gnus-mouse-pick-article (e)
5739   (interactive "e")
5740   (mouse-set-point e)
5741   (gnus-summary-next-page nil t))
5742
5743 (defun gnus-summary-setup-buffer (group)
5744   "Initialize summary buffer."
5745   (let ((buffer (concat "*Summary " group "*")))
5746     (if (get-buffer buffer)
5747         (progn
5748           (set-buffer buffer)
5749           (not gnus-newsgroup-begin))
5750       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5751       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5752       (gnus-add-current-to-buffer-list)
5753       (gnus-summary-mode group)
5754       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5755       (setq gnus-newsgroup-name group)
5756       t)))
5757
5758 (defun gnus-set-global-variables ()
5759   ;; Set the global equivalents of the summary buffer-local variables
5760   ;; to the latest values they had. These reflect the summary buffer
5761   ;; that was in action when the last article was fetched.
5762   (if (eq major-mode 'gnus-summary-mode) 
5763       (progn
5764         (setq gnus-summary-buffer (current-buffer))
5765         (let ((name gnus-newsgroup-name)
5766               (marked gnus-newsgroup-marked)
5767               (unread gnus-newsgroup-unreads)
5768               (headers gnus-current-headers)
5769               (score-file gnus-current-score-file))
5770           (save-excursion
5771             (set-buffer gnus-group-buffer)
5772             (setq gnus-newsgroup-name name)
5773             (setq gnus-newsgroup-marked marked)
5774             (setq gnus-newsgroup-unreads unread)
5775             (setq gnus-current-headers headers)
5776             (setq gnus-current-score-file score-file))))))
5777
5778 (defun gnus-summary-insert-dummy-line (sformat subject number)
5779   (if (not sformat) 
5780       (setq sformat gnus-summary-dummy-line-format-spec))
5781   (let (b)
5782     (beginning-of-line)
5783     (setq b (point))
5784     (insert (eval sformat))
5785     (add-text-properties
5786      b (1+ b)
5787      (list 'gnus-number number 
5788            'gnus-mark gnus-dummy-mark
5789            'gnus-level 0))))
5790
5791 (defvar gnus-thread-indent-array nil)
5792 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5793 (defun gnus-make-thread-indent-array ()
5794   (let ((n 200))
5795     (if (and gnus-thread-indent-array
5796              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5797         nil
5798       (setq gnus-thread-indent-array (make-vector 201 "")
5799             gnus-thread-indent-array-level gnus-thread-indent-level)
5800       (while (>= n 0)
5801         (aset gnus-thread-indent-array n
5802               (make-string (* n gnus-thread-indent-level) ? ))
5803         (setq n (1- n))))))
5804
5805 (defun gnus-summary-insert-line 
5806   (sformat header level current unread replied expirable subject-or-nil
5807            &optional dummy score process)
5808   (or sformat (setq sformat gnus-summary-line-format-spec))
5809   (let* ((indentation (aref gnus-thread-indent-array level))
5810          (lines (mail-header-lines header))
5811          (score (or score gnus-summary-default-score 0))
5812          (score-char
5813           (if (or (null gnus-summary-default-score)
5814                   (<= (abs (- score gnus-summary-default-score))
5815                       gnus-summary-zcore-fuzz)) ? 
5816             (if (< score gnus-summary-default-score)
5817                 gnus-score-below-mark gnus-score-over-mark)))
5818          (replied (cond (process gnus-process-mark)
5819                         (replied gnus-replied-mark)
5820                         (t gnus-unread-mark)))
5821          (from (mail-header-from header))
5822          (name (cond 
5823                 ((string-match "(.+)" from)
5824                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5825                 ((string-match "<[^>]+> *$" from)
5826                  (let ((beg (match-beginning 0)))
5827                    (or (and (string-match "^\"[^\"]*\"" from)
5828                             (substring from (1+ (match-beginning 0))
5829                                        (1- (match-end 0))))
5830                        (substring from 0 beg))))
5831                 (t from)))
5832          (subject (mail-header-subject header))
5833          (number (mail-header-number header))
5834          (opening-bracket (if dummy ?\< ?\[))
5835          (closing-bracket (if dummy ?\> ?\]))
5836          (buffer-read-only nil)
5837          (b (progn (beginning-of-line) (point))))
5838     (or (numberp lines) (setq lines 0))
5839     (insert (eval sformat))
5840     (add-text-properties
5841      b (1+ b) (list 'gnus-number number 
5842                     'gnus-mark (or unread gnus-unread-mark)
5843                     'gnus-level level))))
5844
5845 (defun gnus-summary-update-line (&optional dont-update)
5846   ;; Update summary line after change.
5847   (or (not gnus-summary-default-score)
5848       gnus-summary-inhibit-highlight
5849       (let ((gnus-summary-inhibit-highlight t)
5850             (article (gnus-summary-article-number)))
5851         (progn
5852           (or dont-update
5853               (if (and gnus-summary-mark-below
5854                        (< (gnus-summary-article-score)
5855                           gnus-summary-mark-below))
5856                   (and (not (memq article gnus-newsgroup-marked))
5857                        (not (memq article gnus-newsgroup-dormant))
5858                        (memq article gnus-newsgroup-unreads)
5859                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5860                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5861                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5862           (and gnus-visual
5863                (run-hooks 'gnus-summary-update-hook))))))
5864
5865 (defun gnus-summary-update-lines (&optional beg end)
5866   ;; Mark article as read (or not) by taking into account scores.
5867   (let ((beg (or beg (point-min)))
5868         (end (or end (point-max))))
5869     (if (or (not gnus-summary-default-score)
5870             gnus-summary-inhibit-highlight)
5871         ()
5872       (let ((gnus-summary-inhibit-highlight t)
5873             article)
5874         (save-excursion
5875           (set-buffer gnus-summary-buffer)
5876           (goto-char beg)
5877           (beginning-of-line)
5878           (while (and (not (eobp)) (< (point) end))
5879             (if (and gnus-summary-mark-below
5880                      (< (or (cdr (assq 
5881                                   (setq article (get-text-property 
5882                                                  (point) 'gnus-number))
5883                                   gnus-newsgroup-scored))
5884                             gnus-summary-default-score 0)
5885                         gnus-summary-mark-below))
5886                 ;; We want to possibly mark it as read...
5887                 (and (not (memq article gnus-newsgroup-marked))
5888                      (not (memq article gnus-newsgroup-dormant))
5889                      (memq article gnus-newsgroup-unreads)
5890                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5891               ;; We want to possibly mark it as unread.
5892               (and (eq (get-text-property (point) 'gnus-mark)
5893                        gnus-low-score-mark)
5894                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5895             ;; Do the visual highlights at the same time.
5896             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5897             (forward-line 1)))))))
5898
5899 (defvar gnus-tmp-gathered nil)
5900
5901 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5902   ;; Sum up all elements (and sub-elements) in a list.
5903   (let* ((number
5904           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5905           (if (and (consp thread) (cdr thread))
5906               (apply
5907                '+ 1 (mapcar
5908                      'gnus-summary-number-of-articles-in-thread 
5909                      (cdr thread)))
5910             1)))
5911     (if char 
5912         (if (> number 1) gnus-not-empty-thread-mark
5913           gnus-empty-thread-mark)
5914       number)))
5915
5916 (defun gnus-summary-read-group 
5917   (group &optional show-all no-article kill-buffer)
5918   "Start reading news in newsgroup GROUP.
5919 If SHOW-ALL is non-nil, already read articles are also listed.
5920 If NO-ARTICLE is non-nil, no article is selected initially."
5921   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5922   (let* ((new-group (gnus-summary-setup-buffer group))
5923          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5924                                                   group))))
5925          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5926     (cond 
5927      ((not new-group)
5928       (gnus-set-global-variables)
5929       (gnus-kill-buffer kill-buffer)
5930       (gnus-configure-windows 'summary 'force)
5931       (gnus-set-mode-line 'summary)
5932       (gnus-summary-position-cursor)
5933       (message "")
5934       t)
5935      ((null did-select) 
5936       (and (eq major-mode 'gnus-summary-mode)
5937            (not (equal (current-buffer) kill-buffer))
5938            (progn
5939              (kill-buffer (current-buffer))
5940              (if (not quit-config)
5941                  (progn
5942                    (set-buffer gnus-group-buffer)
5943                    (gnus-group-jump-to-group group)
5944                    (gnus-group-next-unread-group 1))
5945                (if (not (buffer-name (car quit-config)))
5946                    (gnus-configure-windows 'group 'force)
5947                  (set-buffer (car quit-config))
5948                  (and (eq major-mode 'gnus-summary-mode)
5949                       (gnus-set-global-variables))
5950                  (gnus-configure-windows (cdr quit-config))))))
5951       (message "Can't select group")
5952       nil)
5953      ((eq did-select 'quit)
5954       (and (eq major-mode 'gnus-summary-mode)
5955            (not (equal (current-buffer) kill-buffer))
5956            (kill-buffer (current-buffer)))
5957       (gnus-kill-buffer kill-buffer)
5958       (if (not quit-config)
5959           (progn
5960             (set-buffer gnus-group-buffer)
5961             (gnus-group-jump-to-group group)
5962             (gnus-group-next-unread-group 1)
5963             (gnus-configure-windows 'group 'force))
5964         (if (not (buffer-name (car quit-config)))
5965             (gnus-configure-windows 'group 'force)
5966           (set-buffer (car quit-config))
5967           (and (eq major-mode 'gnus-summary-mode)
5968                (gnus-set-global-variables))
5969           (gnus-configure-windows (cdr quit-config))))
5970       (signal 'quit nil))
5971      (t
5972       (gnus-set-global-variables)
5973       ;; Save the active value in effect when the group was entered.
5974       (setq gnus-newsgroup-active 
5975             (gnus-copy-sequence
5976              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5977       ;; You can change the subjects in this hook.
5978       (run-hooks 'gnus-select-group-hook)
5979       ;; Do score processing.
5980       (and gnus-use-scoring (gnus-possibly-score-headers))
5981       (gnus-update-format-specifications)
5982       ;; Generate the summary buffer.
5983       (gnus-summary-prepare)
5984       (if (zerop (buffer-size))
5985           (cond (gnus-newsgroup-dormant
5986                  (gnus-summary-show-all-dormant))
5987                 ((and gnus-newsgroup-scored show-all)
5988                  (gnus-summary-show-all-expunged))))
5989       ;; Function `gnus-apply-kill-file' must be called in this hook.
5990       (run-hooks 'gnus-apply-kill-hook)
5991       (if (zerop (buffer-size))
5992           (progn
5993             ;; This newsgroup is empty.
5994             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5995             (gnus-message 6 "No unread news")
5996             (gnus-kill-buffer kill-buffer)
5997             nil)
5998         ;;(save-excursion
5999         ;;  (if kill-buffer
6000         ;;      (let ((gnus-summary-buffer kill-buffer))
6001         ;;      (gnus-configure-windows 'group))))
6002         ;; Hide conversation thread subtrees.  We cannot do this in
6003         ;; gnus-summary-prepare-hook since kill processing may not
6004         ;; work with hidden articles.
6005         (and gnus-show-threads
6006              gnus-thread-hide-subtree
6007              (gnus-summary-hide-all-threads))
6008         ;; Show first unread article if requested.
6009         (goto-char (point-min))
6010         (if (and (not no-article)
6011                  gnus-auto-select-first
6012                  (gnus-summary-first-unread-article))
6013             ()
6014           (gnus-configure-windows 'summary 'force))
6015         (gnus-set-mode-line 'summary)
6016         (gnus-summary-position-cursor)
6017         ;; If in async mode, we send some info to the backend.
6018         (and gnus-newsgroup-async
6019              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
6020              (gnus-request-asynchronous 
6021               gnus-newsgroup-name
6022               (if (and gnus-asynchronous-article-function
6023                        (fboundp gnus-asynchronous-article-function))
6024                   (funcall gnus-asynchronous-article-function
6025                            gnus-newsgroup-threads)
6026                 gnus-newsgroup-threads)))
6027         (gnus-kill-buffer kill-buffer)
6028         (if (not (get-buffer-window gnus-group-buffer))
6029             ()
6030           ;; gotta use windows, because recenter does wierd stuff if
6031           ;; the current buffer ain't the displayed window.
6032           (let ((owin (selected-window))) 
6033             (select-window (get-buffer-window gnus-group-buffer))
6034             (and (gnus-group-goto-group group)
6035                  (recenter))
6036             (select-window owin))))
6037       t))))
6038
6039 (defun gnus-summary-prepare ()
6040   ;; Generate the summary buffer.
6041   (let ((buffer-read-only nil))
6042     (erase-buffer)
6043     (gnus-summary-prepare-threads 
6044      (if gnus-show-threads
6045          (gnus-gather-threads 
6046           (gnus-sort-threads 
6047            (if (and gnus-summary-expunge-below
6048                     (not gnus-fetch-old-headers))
6049                (gnus-make-threads-and-expunge)
6050              (gnus-make-threads))))
6051        gnus-newsgroup-headers)
6052      'cull)
6053     (gnus-summary-update-lines)
6054     ;; Create the header hashtb.
6055     (gnus-make-headers-hashtable-by-number)
6056     ;; Call hooks for modifying summary buffer.
6057     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
6058     (goto-char (point-min))
6059     (run-hooks 'gnus-summary-prepare-hook)))
6060
6061 (defun gnus-gather-threads (threads)
6062   "Gather threads that have lost their roots."
6063   (if (not gnus-summary-make-false-root)
6064       threads 
6065     (let ((hashtb (gnus-make-hashtable 1023))
6066           (prev threads)
6067           (result threads)
6068           subject hthread whole-subject)
6069       (while threads
6070         (setq whole-subject 
6071               (setq subject (mail-header-subject (car (car threads)))))
6072         (if gnus-summary-gather-subject-limit
6073             (or (and (numberp gnus-summary-gather-subject-limit)
6074                      (> (length subject) gnus-summary-gather-subject-limit)
6075                      (setq subject
6076                            (substring subject 0 
6077                                       gnus-summary-gather-subject-limit)))
6078                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
6079                      (setq subject (gnus-simplify-subject-fuzzy subject))))
6080           (setq subject (gnus-simplify-subject-re subject)))
6081         (if (setq hthread 
6082                   (gnus-gethash subject hashtb))
6083             (progn
6084               (or (stringp (car (car hthread)))
6085                   (setcar hthread (list whole-subject (car hthread))))
6086               (setcdr (car hthread) (nconc (cdr (car hthread)) 
6087                                            (list (car threads))))
6088               (setcdr prev (cdr threads))
6089               (setq threads prev))
6090           (gnus-sethash subject threads hashtb))
6091         (setq prev threads)
6092         (setq threads (cdr threads)))
6093       result)))
6094
6095 (defun gnus-make-threads ()
6096   ;; This function takes the dependencies already made by 
6097   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6098   ;; through the dependecies in the hash table and finds all the
6099   ;; roots. Roots do not refer back to any valid articles.
6100   (gnus-message 6 "Threading...")
6101   (let (roots new-roots)
6102     (and gnus-fetch-old-headers
6103          (eq gnus-headers-retrieved-by 'nov)
6104          (gnus-build-old-threads))
6105     (mapatoms
6106      (lambda (refs)
6107        (if (not (car (symbol-value refs)))
6108            (setq roots (append (cdr (symbol-value refs)) roots))
6109          ;; Ok, these refer back to valid articles, but if
6110          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6111          ;; the root has the same subject as its children. The children
6112          ;; that do not are made into roots and removed from the list
6113          ;; of children. 
6114          (or gnus-thread-ignore-subject
6115              (let* ((prev (symbol-value refs))
6116                     (subject (gnus-simplify-subject-re 
6117                               (mail-header-subject (car prev))))
6118                     (headers (cdr prev)))
6119                (while headers
6120                  (if (not (string= subject
6121                                    (gnus-simplify-subject-re 
6122                                     (mail-header-subject (car headers)))))
6123                      (progn
6124                        (setq new-roots (cons (car headers) new-roots))
6125                        (setcdr prev (cdr headers)))
6126                    (setq prev headers))
6127                  (setq headers (cdr headers)))))))
6128      gnus-newsgroup-dependencies)
6129
6130     ;; We enter the new roots into the dependencies structure to
6131     ;; ensure that any possible later thread-regeneration will be
6132     ;; possible. 
6133     (let ((r new-roots))
6134       (while r
6135         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6136                       (list nil (car r)) gnus-newsgroup-dependencies)
6137         (setq r (cdr r))))
6138
6139     (setq roots (nconc new-roots roots))
6140
6141     (prog1
6142         (mapcar 'gnus-trim-thread
6143                 (apply 'append
6144                        (mapcar 'gnus-cut-thread
6145                                (mapcar 'gnus-make-sub-thread roots))))
6146       (gnus-message 6 "Threading...done"))))
6147
6148   
6149 (defun gnus-make-threads-and-expunge ()
6150   ;; This function takes the dependencies already made by 
6151   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6152   ;; through the dependecies in the hash table and finds all the
6153   ;; roots. Roots do not refer back to any valid articles.
6154   (gnus-message 6 "Threading...")
6155   (let ((default (or gnus-summary-default-score 0))
6156         (below gnus-summary-expunge-below)
6157         roots article new-roots)
6158     (and gnus-fetch-old-headers
6159          (eq gnus-headers-retrieved-by 'nov)
6160          (gnus-build-old-threads))
6161     (mapatoms
6162      (lambda (refs)
6163        (if (not (car (symbol-value refs)))
6164            ;; These articles do not refer back to any other articles -
6165            ;; they are roots.
6166            (let ((headers (cdr (symbol-value refs))))
6167              ;; We weed out the low-scored articles.
6168              (while headers
6169                (if (not (< (or (cdr (assq (mail-header-number (car headers))
6170                                           gnus-newsgroup-scored)) default)
6171                            below))
6172                    ;; It is over.
6173                    (setq roots (cons (car headers) roots))
6174                  ;; It is below, so we mark it as read.
6175                  (setq gnus-newsgroup-unreads
6176                        (delq (mail-header-number (car headers))
6177                              gnus-newsgroup-unreads))
6178                  (setq gnus-newsgroup-reads 
6179                        (cons (cons (mail-header-number (car headers))
6180                                    gnus-low-score-mark) 
6181                              gnus-newsgroup-reads)))
6182                (setq headers (cdr headers))))
6183          ;; Ok, these refer back to valid articles, but if
6184          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6185          ;; the root has the same subject as its children. The children
6186          ;; that do not are made into roots and removed from the list
6187          ;; of children. 
6188          (or gnus-thread-ignore-subject
6189              (let* ((prev (symbol-value refs))
6190                     (subject (gnus-simplify-subject-re 
6191                               (mail-header-subject (car prev))))
6192                     (headers (cdr prev)))
6193                (while headers
6194                  (if (not (string= subject
6195                                    (gnus-simplify-subject-re 
6196                                     (mail-header-subject (car headers)))))
6197                      (progn
6198                        (if (not (< (or (cdr (assq (mail-header-number
6199                                                    (car headers))
6200                                                   gnus-newsgroup-scored))
6201                                        default) below))
6202                            (setq new-roots (cons (car headers) new-roots))
6203                          (setq gnus-newsgroup-unreads
6204                                (delq (mail-header-number (car headers))
6205                                      gnus-newsgroup-unreads))
6206                          (setq gnus-newsgroup-reads
6207                                (cons (cons (mail-header-number (car headers)) 
6208                                            gnus-low-score-mark) 
6209                                      gnus-newsgroup-reads)))
6210                        (setcdr prev (cdr headers)))
6211                    (setq prev headers))
6212                  (setq headers (cdr headers)))))
6213          ;; If this article is expunged, some of the children might be
6214          ;; roots.  
6215          (if (< (or (cdr (assq (mail-header-number (car (symbol-value refs)))
6216                                gnus-newsgroup-scored)) default)
6217                 below)
6218              (let* ((prev (symbol-value refs))
6219                     (headers (cdr prev)))
6220                (while headers
6221                  (setq article (mail-header-number (car headers)))
6222                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6223                                  default) below))
6224                      (progn (setq new-roots (cons (car headers) new-roots))
6225                             (setq prev headers))
6226                    (setq gnus-newsgroup-unreads 
6227                          (delq article gnus-newsgroup-unreads))
6228                    (setq gnus-newsgroup-reads 
6229                          (cons (cons article gnus-low-score-mark) 
6230                                gnus-newsgroup-reads))
6231                    (setcdr prev (cdr headers)))
6232                  (setq headers (cdr headers))))
6233            ;; It was not expunged, but we look at expunged children.
6234            (let* ((prev (symbol-value refs))
6235                   (headers (cdr prev))
6236                   article)
6237              (while headers
6238                (setq article (mail-header-number (car headers)))
6239                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6240                                default) below))
6241                    (setq prev headers)
6242                  (setq gnus-newsgroup-unreads 
6243                        (delq article gnus-newsgroup-unreads))
6244                  (setq gnus-newsgroup-reads 
6245                        (cons (cons article gnus-low-score-mark)
6246                              gnus-newsgroup-reads))
6247                  (setcdr prev (cdr headers)))
6248                (setq headers (cdr headers)))))))
6249      gnus-newsgroup-dependencies)
6250
6251     ;; We enter the new roots into the dependencies structure to
6252     ;; ensure that any possible later thread-regeneration will be
6253     ;; possible. 
6254     (let ((r new-roots))
6255       (while r
6256         (gnus-sethash (concat (mail-header-id (car r)) ".boo")
6257                       (list nil (car r)) gnus-newsgroup-dependencies)
6258         (setq r (cdr r))))
6259
6260     (setq roots (nconc new-roots roots))
6261
6262     (prog1
6263         (mapcar 'gnus-trim-thread
6264                 (apply 'append
6265                        (mapcar 'gnus-cut-thread
6266                                (mapcar 'gnus-make-sub-thread roots))))
6267       (gnus-message 6 "Threading...done"))))
6268
6269   
6270 (defun gnus-cut-thread (thread)
6271   ;; Remove leaf dormant or ancient articles from THREAD.
6272   (let ((head (car thread))
6273         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6274     (if (and (null tail)
6275              (let ((number (mail-header-number head)))
6276                (or (memq number gnus-newsgroup-ancient)
6277                    (memq number gnus-newsgroup-dormant)
6278                    (and gnus-summary-expunge-below
6279                         (eq gnus-fetch-old-headers 'some)
6280                         (< (or (cdr (assq number gnus-newsgroup-scored))
6281                                gnus-summary-default-score 0)
6282                            gnus-summary-expunge-below)
6283                         (progn
6284                           (setq gnus-newsgroup-unreads
6285                                 (delq number gnus-newsgroup-unreads))
6286                           (setq gnus-newsgroup-reads
6287                                 (cons (cons number gnus-low-score-mark)
6288                                       gnus-newsgroup-reads))
6289                           t)))))
6290         nil
6291       (list (cons head tail)))))
6292
6293 (defun gnus-trim-thread (thread)
6294   ;; Remove root ancient articles with only one child from THREAD.
6295   (if (and (eq gnus-fetch-old-headers 'some)
6296            (memq (mail-header-number (car thread)) gnus-newsgroup-ancient)
6297            (= (length thread) 2))
6298       (gnus-trim-thread (nth 1 thread))
6299     thread))
6300
6301 (defun gnus-make-sub-thread (root)
6302   ;; This function makes a sub-tree for a node in the tree.
6303   (let ((children (reverse (cdr (gnus-gethash (downcase (mail-header-id root))
6304                                               gnus-newsgroup-dependencies)))))
6305     (cons root (mapcar 'gnus-make-sub-thread children))))
6306
6307 (defun gnus-build-old-threads ()
6308   ;; Look at all the articles that refer back to old articles, and
6309   ;; fetch the headers for the articles that aren't there. This will
6310   ;; build complete threads - if the roots haven't been expired by the
6311   ;; server, that is.
6312   (let (id heads)
6313     (mapatoms
6314      (lambda (refs)
6315        (if (not (car (symbol-value refs)))
6316            (progn
6317              (setq heads (cdr (symbol-value refs)))
6318              (while heads
6319                (if (not (memq (mail-header-number (car heads))
6320                               gnus-newsgroup-dormant))
6321                    (progn
6322                      (setq id (symbol-name refs))
6323                      (while (and (setq id (gnus-build-get-header id))
6324                                  (not (car (gnus-gethash 
6325                                             id gnus-newsgroup-dependencies)))))
6326                      (setq heads nil))
6327                  (setq heads (cdr heads)))))))
6328      gnus-newsgroup-dependencies)))
6329
6330 (defun gnus-build-get-header (id)
6331   ;; Look through the buffer of NOV lines and find the header to
6332   ;; ID. Enter this line into the dependencies hash table, and return
6333   ;; the id of the parent article (if any).
6334   (let ((deps gnus-newsgroup-dependencies)
6335         found header)
6336     (prog1
6337         (save-excursion
6338           (set-buffer nntp-server-buffer)
6339           (goto-char (point-min))
6340           (while (and (not found) (search-forward id nil t))
6341             (beginning-of-line)
6342             (setq found (looking-at 
6343                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6344                                  (regexp-quote id))))
6345             (or found (beginning-of-line 2)))
6346           (if found
6347               (let (ref)
6348                 (beginning-of-line)
6349                 (and
6350                  (setq header (gnus-nov-parse-line 
6351                                (read (current-buffer)) deps))
6352                  (setq ref (mail-header-references header))
6353                  (string-match "\\(<[^>]+>\\) *$" ref)
6354                  (substring ref (match-beginning 1) (match-end 1))))))
6355       (and header
6356            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6357                  gnus-newsgroup-ancient (cons (mail-header-number header)
6358                                               gnus-newsgroup-ancient))))))
6359
6360 ;; Re-build the thread containing ID.
6361 (defun gnus-rebuild-thread (id)
6362   (let ((dep gnus-newsgroup-dependencies)
6363         (buffer-read-only nil)
6364         parent headers refs thread art)
6365     (while (and id (setq headers
6366                          (car (setq art (gnus-gethash (downcase id) dep)))))
6367       (setq parent art)
6368       (setq id (and (setq refs (mail-header-references headers))
6369                     (string-match "\\(<[^>]+>\\) *$" refs)
6370                     (substring refs (match-beginning 1) (match-end 1)))))
6371     (setq thread (gnus-make-sub-thread (car parent)))
6372     (gnus-rebuild-remove-articles thread)
6373     (let ((beg (point)))
6374       (gnus-summary-prepare-threads (list thread))
6375       (gnus-summary-update-lines beg (point)))))
6376
6377 ;; Delete all lines in the summary buffer that correspond to articles
6378 ;; in this thread.
6379 (defun gnus-rebuild-remove-articles (thread)
6380   (and (gnus-summary-goto-subject (mail-header-number (car thread)))
6381        (gnus-delete-line))
6382   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6383
6384 (defun gnus-sort-threads (threads)
6385   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6386   (let ((fun gnus-thread-sort-functions))
6387     (while fun
6388       (gnus-message 6 "Sorting with %S..." fun)
6389       (setq threads (sort threads (car fun))
6390             fun (cdr fun))))
6391   (if gnus-thread-sort-functions
6392       (gnus-message 6 "Sorting...done"))
6393   threads)
6394
6395 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6396 (defmacro gnus-thread-header (thread)
6397   ;; Return header of first article in THREAD.
6398   ;; Note that THREAD must never, evr be anything else than a variable -
6399   ;; using some other form will lead to serious barfage.
6400   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6401   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6402   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6403         (vector thread) 2))
6404
6405 (defun gnus-thread-sort-by-number (h1 h2)
6406   "Sort threads by root article number."
6407   (< (mail-header-number (gnus-thread-header h1))
6408      (mail-header-number (gnus-thread-header h2))))
6409
6410 (defun gnus-thread-sort-by-author (h1 h2)
6411   "Sort threads by root author."
6412   (string-lessp
6413    (let ((extract (funcall 
6414                    gnus-extract-address-components
6415                    (mail-header-from (gnus-thread-header h1)))))
6416      (or (car extract) (cdr extract)))
6417    (let ((extract (funcall
6418                    gnus-extract-address-components 
6419                    (mail-header-from (gnus-thread-header h2)))))
6420      (or (car extract) (cdr extract)))))
6421
6422 (defun gnus-thread-sort-by-subject (h1 h2)
6423   "Sort threads by root subject."
6424   (string-lessp
6425    (downcase (gnus-simplify-subject-re
6426               (mail-header-subject (gnus-thread-header h1))))
6427    (downcase (gnus-simplify-subject-re 
6428               (mail-header-subject (gnus-thread-header h2))))))
6429
6430 (defun gnus-thread-sort-by-date (h1 h2)
6431   "Sort threads by root article date."
6432   (string-lessp
6433    (gnus-sortable-date (mail-header-date (gnus-thread-header h1)))
6434    (gnus-sortable-date (mail-header-date (gnus-thread-header h2)))))
6435
6436 (defun gnus-thread-sort-by-score (h1 h2)
6437   "Sort threads by root article score.
6438 Unscored articles will be counted as having a score of zero."
6439   (> (or (cdr (assq (mail-header-number (gnus-thread-header h1))
6440                     gnus-newsgroup-scored))
6441          gnus-summary-default-score 0)
6442      (or (cdr (assq (mail-header-number (gnus-thread-header h2))
6443                     gnus-newsgroup-scored))
6444          gnus-summary-default-score 0)))
6445
6446 (defun gnus-thread-sort-by-total-score (h1 h2)
6447   "Sort threads by the sum of all scores in the thread.
6448 Unscored articles will be counted as having a score of zero."
6449   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6450
6451 (defun gnus-thread-total-score (thread)
6452   ;;  This function find the total score of THREAD.
6453   (if (consp thread)
6454       (if (stringp (car thread))
6455           (apply gnus-thread-score-function 0
6456                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6457         (gnus-thread-total-score-1 thread))
6458     (gnus-thread-total-score-1 (list thread))))
6459
6460 (defun gnus-thread-total-score-1 (root)
6461   ;; This function find the total score of the thread below ROOT.
6462   (setq root (car root))
6463   (apply gnus-thread-score-function
6464          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
6465              gnus-summary-default-score 0)
6466          (mapcar 'gnus-thread-total-score
6467                  (cdr (gnus-gethash (downcase (mail-header-id root))
6468                                     gnus-newsgroup-dependencies)))))
6469
6470 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6471 (defvar gnus-tmp-prev-subject "")
6472
6473 (defun gnus-summary-prepare-threads (threads &optional cull)
6474   "Prepare summary buffer from THREADS and indentation LEVEL.  
6475 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6476 or a straight list of headers."
6477   (message "Generating summary...")
6478   (let ((level 0)
6479         thread header number subject stack state gnus-tmp-gathered)
6480     (if (vectorp (car threads))
6481         ;; If this is a straight (sic) list of headers, then a
6482         ;; threaded summary display isn't required, so we just create
6483         ;; an unthreaded one.
6484         (gnus-summary-prepare-unthreaded threads cull)
6485
6486       ;; Do the threaded display.
6487
6488       (while (or threads stack)
6489         
6490         (if threads
6491             ;; If there are some threads, we do them before the
6492             ;; threads on the stack.
6493             (setq thread threads
6494                   header (car (car thread)))
6495           ;; There were no current threads, so we pop something off
6496           ;; the stack. 
6497           (setq state (car stack)
6498                 level (car state)
6499                 thread (cdr state)
6500                 stack (cdr stack)
6501                 header (car (car thread))))
6502
6503         (if (stringp header)
6504             (progn
6505               ;; The header is a dummy root.
6506               (cond 
6507                ((eq gnus-summary-make-false-root 'adopt)
6508                 ;; We let the first article adopt the rest.
6509                 (let ((th (car (cdr (car thread)))))
6510                   (while (cdr th)
6511                     (setq th (cdr th)))
6512                   (setcdr th (cdr (cdr (car thread))))
6513                   (setq gnus-tmp-gathered 
6514                         (nconc (mapcar
6515                                 (lambda (h) (mail-header-number (car h)))
6516                                 (cdr (cdr (car thread))))
6517                                gnus-tmp-gathered))
6518                   (setcdr (cdr (car thread)) nil))
6519                 (setq level -1))
6520                ((eq gnus-summary-make-false-root 'empty)
6521                 ;; We print adopted articles with empty subject fields.
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                 (setq level -1))
6528                ((eq gnus-summary-make-false-root 'dummy)
6529                 ;; We output a dummy root.
6530                 (gnus-summary-insert-dummy-line 
6531                  nil header (mail-header-number
6532                              (car (car (cdr (car thread)))))))
6533                (t
6534                 ;; We do not make a root for the gathered
6535                 ;; sub-threads at all.  
6536                 (setq level -1))))
6537       
6538           (setq number (mail-header-number header)
6539                 subject (mail-header-subject header))
6540
6541           ;; Do the async thing.
6542           (and gnus-newsgroup-async
6543                (setq gnus-newsgroup-threads
6544                      (cons (cons number (mail-header-lines header)) 
6545                            gnus-newsgroup-threads)))
6546
6547           ;; We may have to root out some bad articles...
6548           (and cull
6549                (= level 0)
6550                (cond ((and (memq (setq number (mail-header-number header))
6551                                  gnus-newsgroup-dormant)
6552                            (null thread))
6553                       (setq header nil))
6554                      ((and gnus-summary-expunge-below
6555                            (< (or (cdr (assq number gnus-newsgroup-scored))
6556                                   gnus-summary-default-score 0)
6557                               gnus-summary-expunge-below))
6558                       (setq header nil)
6559                       (setq gnus-newsgroup-unreads 
6560                             (delq number gnus-newsgroup-unreads))
6561                       (setq gnus-newsgroup-reads
6562                             (cons (cons number gnus-low-score-mark)
6563                                   gnus-newsgroup-reads)))))
6564           
6565           (and
6566            header
6567            (progn
6568              (inline
6569                (gnus-summary-insert-line
6570                 nil header level nil 
6571                 (cond 
6572                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6573                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6574                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6575                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6576                  (t (or (cdr (assq number gnus-newsgroup-reads))
6577                         gnus-ancient-mark)))
6578                 (memq number gnus-newsgroup-replied)
6579                 (memq number gnus-newsgroup-expirable)
6580                 (cond
6581                  ((and gnus-thread-ignore-subject
6582                        (not (string= 
6583                              (gnus-simplify-subject-re gnus-tmp-prev-subject)
6584                              (gnus-simplify-subject-re subject))))
6585                   subject)
6586                  ((zerop level)
6587                   (if (and (eq gnus-summary-make-false-root 'empty)
6588                            (memq number gnus-tmp-gathered))
6589                       gnus-summary-same-subject
6590                     subject))
6591                  (t gnus-summary-same-subject))
6592                 (and (eq gnus-summary-make-false-root 'adopt)
6593                      (memq number gnus-tmp-gathered))
6594                 (cdr (assq number gnus-newsgroup-scored))
6595                 (memq number gnus-newsgroup-processable))
6596
6597                (setq gnus-tmp-prev-subject subject)))))
6598
6599         (if (nth 1 thread) 
6600             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6601         (setq level (1+ level))
6602         (setq threads (cdr (car thread))))))
6603   (message "Generating summary...done"))
6604
6605
6606
6607 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6608   (let (header number)
6609
6610     ;; Do the async thing, if that is required.
6611     (if gnus-newsgroup-async
6612         (setq gnus-newsgroup-threads
6613               (mapcar (lambda (h) 
6614                         (cons (mail-header-number h) (mail-header-lines h)))
6615                       headers)))
6616
6617     (while headers
6618       (setq header (car headers)
6619             headers (cdr headers)
6620             number (mail-header-number header))
6621
6622       ;; We may have to root out some bad articles...
6623       (cond 
6624        ((and cull
6625              (memq (setq number (mail-header-number header))
6626                    gnus-newsgroup-dormant)))
6627        ((and cull gnus-summary-expunge-below
6628              (< (or (cdr (assq number gnus-newsgroup-scored))
6629                     gnus-summary-default-score 0)
6630                 gnus-summary-expunge-below))
6631         (setq gnus-newsgroup-unreads 
6632               (delq number gnus-newsgroup-unreads))
6633         (setq gnus-newsgroup-reads
6634               (cons (cons number gnus-low-score-mark)
6635                     gnus-newsgroup-reads)))
6636        (t
6637         (gnus-summary-insert-line
6638          nil header 0 nil 
6639          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6640                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6641                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6642                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6643                (t (or (cdr (assq number gnus-newsgroup-reads))
6644                       gnus-ancient-mark)))
6645          (memq number gnus-newsgroup-replied)
6646          (memq number gnus-newsgroup-expirable)
6647          (mail-header-subject header) nil
6648          (cdr (assq number gnus-newsgroup-scored))
6649          (memq number gnus-newsgroup-processable)))))))
6650
6651 (defun gnus-select-newsgroup (group &optional read-all)
6652   "Select newsgroup GROUP.
6653 If READ-ALL is non-nil, all articles in the group are selected."
6654   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6655          (info (nth 2 entry))
6656          articles)
6657
6658     (or (gnus-check-server
6659          (setq gnus-current-select-method (gnus-find-method-for-group group)))
6660         (error "Couldn't open server"))
6661     
6662     (or (and entry (not (eq (car entry) t))) ; Either it's active...
6663         (gnus-activate-group group) ; Or we can activate it...
6664         (progn ; Or we bug out.
6665           (kill-buffer (current-buffer))
6666           (error "Couldn't request group %s: %s" 
6667                  group (gnus-status-message group))))
6668
6669     (setq gnus-newsgroup-name group)
6670     (setq gnus-newsgroup-unselected nil)
6671     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6672
6673     (and gnus-asynchronous
6674          (gnus-check-backend-function 
6675           'request-asynchronous gnus-newsgroup-name)
6676          (setq gnus-newsgroup-async
6677                (gnus-request-asynchronous gnus-newsgroup-name)))
6678
6679     (setq articles (gnus-articles-to-read group read-all))
6680
6681     (cond 
6682      ((null articles) 
6683       (gnus-message 3 "Couldn't select newsgroup")
6684       'quit)
6685      ((eq articles 0) nil)
6686      (t
6687       ;; Init the dependencies hash table.
6688       (setq gnus-newsgroup-dependencies 
6689             (gnus-make-hashtable (length articles)))
6690       ;; Retrieve the headers and read them in.
6691       (gnus-message 5 "Fetching headers...")
6692       (setq gnus-newsgroup-headers 
6693             (if (eq 'nov (setq gnus-headers-retrieved-by
6694                                ;; This is a naughty hack. To get the
6695                                ;; retrieval of old headers to work, we
6696                                ;; set `nntp-nov-gap' to nil (locally),
6697                                ;; and then just retrieve the headers.
6698                                ;; Mucho magic.
6699                                (if gnus-fetch-old-headers
6700                                    (let (nntp-nov-gap)
6701                                      (gnus-retrieve-headers 
6702                                       (if (not (eq 1 (car articles)))
6703                                           (cons 1 articles)
6704                                         articles)
6705                                       gnus-newsgroup-name))
6706                                  (gnus-retrieve-headers 
6707                                   articles gnus-newsgroup-name))))
6708                 (progn
6709                   (gnus-get-newsgroup-headers-xover articles))
6710               ;; If we were to fetch old headers, but the backend didn't
6711               ;; support XOVER, then it is possible we fetched one article
6712               ;; that we shouldn't have. If that's the case, we remove it.
6713               (if (or (not gnus-fetch-old-headers)
6714                       (eq 1 (car articles)))
6715                   ()
6716                 (save-excursion
6717                   (set-buffer nntp-server-buffer)
6718                   (goto-char (point-min))
6719                   (and 
6720                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6721                    (delete-region       ; So we delete this head.
6722                     (point) 
6723                     (search-forward "\n.\n" nil t)))))
6724               (gnus-get-newsgroup-headers)))
6725       (gnus-message 5 "Fetching headers...done")      
6726       ;; Remove canceled articles from the list of unread articles.
6727       (setq gnus-newsgroup-unreads
6728             (gnus-set-sorted-intersection 
6729              gnus-newsgroup-unreads
6730              (mapcar (lambda (headers) (mail-header-number headers))
6731                      gnus-newsgroup-headers)))
6732       ;; Adjust and set lists of article marks.
6733       (and info
6734            (let (marked)
6735              (gnus-adjust-marked-articles info)
6736              (setq gnus-newsgroup-marked 
6737                    (copy-sequence
6738                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6739              (setq gnus-newsgroup-replied 
6740                    (copy-sequence (cdr (assq 'reply marked))))
6741              (setq gnus-newsgroup-expirable
6742                    (copy-sequence (cdr (assq 'expire marked))))
6743              (setq gnus-newsgroup-killed
6744                    (copy-sequence (cdr (assq 'killed marked))))
6745              (setq gnus-newsgroup-bookmarks 
6746                    (copy-sequence (cdr (assq 'bookmark marked))))
6747              (setq gnus-newsgroup-dormant 
6748                    (copy-sequence (cdr (assq 'dormant marked))))
6749              (setq gnus-newsgroup-scored 
6750                    (copy-sequence (cdr (assq 'score marked))))
6751              (setq gnus-newsgroup-processable nil)))
6752       ;; Check whether auto-expire is to be done in this group.
6753       (setq gnus-newsgroup-auto-expire
6754             (or (and (stringp gnus-auto-expirable-newsgroups)
6755                      (string-match gnus-auto-expirable-newsgroups group))
6756                 (memq 'auto-expire (nth 5 info))))
6757       ;; First and last article in this newsgroup.
6758       (and gnus-newsgroup-headers
6759            (setq gnus-newsgroup-begin 
6760                  (mail-header-number (car gnus-newsgroup-headers)))
6761            (setq gnus-newsgroup-end
6762                  (mail-header-number
6763                   (gnus-last-element gnus-newsgroup-headers))))
6764       (setq gnus-reffed-article-number -1)
6765       ;; GROUP is successfully selected.
6766       (or gnus-newsgroup-headers t)))))
6767
6768 (defun gnus-articles-to-read (group read-all)
6769   ;; Find out what articles the user wants to read.
6770   (let* ((articles
6771           ;; Select all articles if `read-all' is non-nil, or if all the
6772           ;; unread articles are dormant articles.
6773           (if (or (and read-all (not (numberp read-all)))
6774                   (= (length gnus-newsgroup-unreads) 
6775                      (length gnus-newsgroup-dormant)))
6776               (gnus-uncompress-range 
6777                (gnus-gethash group gnus-active-hashtb))
6778             gnus-newsgroup-unreads))
6779          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6780          (scored (length scored-list))
6781          (number (length articles))
6782          (marked (+ (length gnus-newsgroup-marked)
6783                     (length gnus-newsgroup-dormant)))
6784          (select
6785           (cond 
6786            ((numberp read-all)
6787             read-all)
6788            (t
6789             (condition-case ()
6790                 (cond ((and (or (<= scored marked)
6791                                 (= scored number))
6792                             (numberp gnus-large-newsgroup)
6793                             (> number gnus-large-newsgroup))
6794                        (let ((input
6795                               (read-string
6796                                (format
6797                                 "How many articles from %s (default %d): "
6798                                 gnus-newsgroup-name number))))
6799                          (if (string-match "^[ \t]*$" input)
6800                              number input)))
6801                       ((and (> scored marked) (< scored number))
6802                        (let ((input
6803                               (read-string
6804                                (format 
6805                                 "%s %s (%d scored, %d total): "
6806                                 "How many articles from"
6807                                 group scored number))))
6808                          (if (string-match "^[ \t]*$" input)
6809                              number input)))
6810                       (t number))
6811               (quit nil))))))
6812     (setq select (if (stringp select) (string-to-number select) select))
6813     (if (or (null select) (zerop select))
6814         select
6815       (if (and (not (zerop scored)) (<= (abs select) scored))
6816           (progn
6817             (setq articles (sort scored-list '<))
6818             (setq number (length articles)))
6819         (setq articles (copy-sequence articles)))
6820
6821       (if (< (abs select) number)
6822           (if (< select 0) 
6823               ;; Select the N oldest articles.
6824               (setcdr (nthcdr (1- (abs select)) articles) nil)
6825             ;; Select the N most recent articles.
6826             (setq articles (nthcdr (- number select) articles))))
6827       (setq gnus-newsgroup-unselected
6828             (gnus-sorted-intersection
6829              gnus-newsgroup-unreads
6830              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6831       articles)))
6832
6833 (defun gnus-killed-articles (killed articles)
6834   (let (out)
6835     (while articles
6836       (if (inline (gnus-member-of-range (car articles) killed))
6837           (setq out (cons (car articles) out)))
6838       (setq articles (cdr articles)))
6839     out))
6840
6841 (defun gnus-adjust-marked-articles (info &optional active)
6842   "Remove all marked articles that are no longer legal."
6843   (let ((marked-lists (nth 3 info))
6844         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6845         m prev)
6846     ;; There are many types of marked articles.
6847     (while marked-lists
6848       (setq m (cdr (setq prev (car marked-lists))))
6849       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6850              ;; Make sure that all ticked articles are a subset of the
6851              ;; unread/unselected articles.
6852              (while m
6853                (if (or (memq (car m) gnus-newsgroup-unreads)
6854                        (memq (car m) gnus-newsgroup-unselected))
6855                    (setq prev m)
6856                  (setcdr prev (cdr m)))
6857                (setq m (cdr m))))
6858             ((eq 'score (car prev))
6859              ;; Scored articles should be a subset of
6860              ;; unread/unselected articles. 
6861              (while m
6862                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6863                        (memq (car (car m)) gnus-newsgroup-unreads))
6864                    (setq prev m)
6865                  (setcdr prev (cdr m)))
6866                (setq m (cdr m))))
6867             ((eq 'bookmark (car prev))
6868              ;; Bookmarks should be a subset of active articles.
6869              (while m
6870                (if (< (car (car m)) (car active))
6871                    (setcdr prev (cdr m))
6872                  (setq prev m))
6873                (setq m (cdr m))))
6874             ((eq 'killed (car prev))
6875              ;; Articles that have been through the kill process are
6876              ;; to be a subset of active articles.
6877              (while (and m (< (or (and (numberp (car m)) (car m))
6878                                   (cdr (car m)))
6879                               (car active)))
6880                (setcdr prev (cdr m))
6881                (setq m (cdr m)))
6882              (if (and m (< (or (and (numberp (car m)) (car m))
6883                                (car (car m)))
6884                            (car active))) 
6885                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6886             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6887              ;; The replied and expirable articles have to be articles
6888              ;; that are active. 
6889              (while m
6890                (if (< (car m) (car active))
6891                    (setcdr prev (cdr m))
6892                  (setq prev m))
6893                (setq m (cdr m)))))
6894       (setq marked-lists (cdr marked-lists)))
6895     ;; Remove all lists that are empty.
6896     (setq marked-lists (nth 3 info))
6897     (if marked-lists
6898         (progn
6899           (while (= 1 (length (car marked-lists)))
6900             (setq marked-lists (cdr marked-lists)))
6901           (setq m (cdr (setq prev marked-lists)))
6902           (while m
6903             (if (= 1 (length (car m)))
6904                 (setcdr prev (cdr m))
6905               (setq prev m))
6906             (setq m (cdr m)))
6907           (setcar (nthcdr 3 info) marked-lists)))
6908     ;; Finally, if there are no marked lists at all left, and if there
6909     ;; are no elements after the lists in the info list, we just chop
6910     ;; the info list off before the marked lists.
6911     (and (null marked-lists) 
6912          (not (nthcdr 4 info))
6913          (setcdr (nthcdr 2 info) nil)))
6914   info)
6915
6916 (defun gnus-set-marked-articles 
6917   (info ticked replied expirable killed dormant bookmark score) 
6918   "Enter the various lists of marked articles into the newsgroup info list."
6919   (let (newmarked)
6920     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6921     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6922     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6923                                          newmarked)))
6924     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6925     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6926     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6927                                         newmarked)))
6928     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6929     (if (nthcdr 3 info)
6930         (progn
6931           (setcar (nthcdr 3 info) newmarked)
6932           (and (not newmarked)
6933                (not (nthcdr 4 info))
6934                (setcdr (nthcdr 2 info) nil)))
6935       (if newmarked
6936           (setcdr (nthcdr 2 info) (list newmarked))))))
6937
6938 (defun gnus-add-marked-articles (group type articles &optional info force)
6939   ;; Add ARTICLES of TYPE to the info of GROUP.
6940   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6941   ;; add, but replace marked articles of TYPE with ARTICLES.
6942   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6943         marked m)
6944     (or (not info)
6945         (and (not (setq marked (nthcdr 3 info)))
6946              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6947         (and (not (setq m (assq type (car marked))))
6948              (setcar marked (cons (cons type articles) (car marked))))
6949         (if force
6950             (setcdr m articles)
6951           (nconc m articles)))))
6952          
6953 (defun gnus-set-mode-line (where)
6954   "This function sets the mode line of the article or summary buffers.
6955 If WHERE is `summary', the summary mode line format will be used."
6956   (if (memq where gnus-updated-mode-lines)
6957       (let (mode-string)
6958         (save-excursion
6959           (set-buffer gnus-summary-buffer)
6960           (let* ((mformat (if (eq where 'article) 
6961                               gnus-article-mode-line-format-spec
6962                             gnus-summary-mode-line-format-spec))
6963                  (group-name gnus-newsgroup-name)
6964                  (article-number (or gnus-current-article 0))
6965                  (unread (- (length gnus-newsgroup-unreads)
6966                             (length gnus-newsgroup-dormant)))
6967                  (unread-and-unticked 
6968                   (- unread (length gnus-newsgroup-marked)))
6969                  (unselected (length gnus-newsgroup-unselected))
6970                  (unread-and-unselected
6971                   (cond ((and (zerop unread-and-unticked)
6972                               (zerop unselected)) "")
6973                         ((zerop unselected) 
6974                          (format "{%d more}" unread-and-unticked))
6975                         (t (format "{%d(+%d) more}"
6976                                    unread-and-unticked unselected))))
6977                  (subject
6978                   (if gnus-current-headers
6979                       (mail-header-subject gnus-current-headers) ""))
6980                  (max-len (and gnus-mode-non-string-length
6981                                (- (frame-width) gnus-mode-non-string-length)))
6982                  header);; passed as argument to any user-format-funcs
6983             (setq mode-string (eval mformat))
6984             (or (numberp max-len)
6985                 (setq max-len (length mode-string)))
6986             (if (< max-len 4) (setq max-len 4))
6987             (if (> (length mode-string) max-len)
6988                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6989                 ;;  function `substring' might cut on a middle
6990                 ;;  of multi-octet character.
6991                 (setq mode-string 
6992                       (concat (gnus-truncate-string mode-string (- max-len 3))
6993                               "...")))
6994             (setq mode-string (format (format "%%-%ds" max-len)
6995                                       mode-string))))
6996         (setq mode-line-buffer-identification mode-string)
6997         (set-buffer-modified-p t))))
6998
6999 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
7000   "Go through the HEADERS list and add all Xrefs to a hash table.
7001 The resulting hash table is returned, or nil if no Xrefs were found."
7002   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
7003          (prefix (if (and 
7004                       (gnus-group-foreign-p from-newsgroup)
7005                       (not (memq 'virtual 
7006                                  (assoc (symbol-name (car from-method))
7007                                         gnus-valid-select-methods))))
7008                      (gnus-group-real-prefix from-newsgroup)))
7009          (xref-hashtb (make-vector 63 0))
7010          start group entry number xrefs header)
7011     (while headers
7012       (setq header (car headers))
7013       (if (and (setq xrefs (mail-header-xref header))
7014                (not (memq (mail-header-number header) unreads)))
7015           (progn
7016             (setq start 0)
7017             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
7018               (setq start (match-end 0))
7019               (setq group (concat prefix (substring xrefs (match-beginning 1) 
7020                                                     (match-end 1))))
7021               (setq number 
7022                     (string-to-int (substring xrefs (match-beginning 2) 
7023                                               (match-end 2))))
7024               (if (setq entry (gnus-gethash group xref-hashtb))
7025                   (setcdr entry (cons number (cdr entry)))
7026                 (gnus-sethash group (cons number nil) xref-hashtb)))))
7027       (setq headers (cdr headers)))
7028     (if start xref-hashtb nil)))
7029
7030 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
7031   "Look through all the headers and mark the Xrefs as read."
7032   (let ((virtual (memq 'virtual 
7033                        (assoc (symbol-name (car (gnus-find-method-for-group 
7034                                                  from-newsgroup)))
7035                               gnus-valid-select-methods)))
7036         name entry info xref-hashtb idlist method
7037         nth4)
7038     (save-excursion
7039       (set-buffer gnus-group-buffer)
7040       (if (setq xref-hashtb 
7041                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
7042           (mapatoms 
7043            (lambda (group)
7044              (if (string= from-newsgroup (setq name (symbol-name group)))
7045                  ()
7046                (setq idlist (symbol-value group))
7047                ;; Dead groups are not updated.
7048                (if (and (prog1 
7049                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
7050                                   info (nth 2 entry))
7051                           (if (stringp (setq nth4 (nth 4 info)))
7052                               (setq nth4 (gnus-server-to-method nth4))))
7053                         ;; Only do the xrefs if the group has the same
7054                         ;; select method as the group we have just read.
7055                         (or (gnus-methods-equal-p 
7056                              nth4 (gnus-find-method-for-group from-newsgroup))
7057                             virtual
7058                             (equal nth4 
7059                                    (setq method (gnus-find-method-for-group 
7060                                                  from-newsgroup)))
7061                             (and (equal (car nth4) (car method))
7062                                  (equal (nth 1 nth4) (nth 1 method))))
7063                         gnus-use-cross-reference
7064                         (or (not (eq gnus-use-cross-reference t))
7065                             virtual
7066                             ;; Only do cross-references on subscribed
7067                             ;; groups, if that is what is wanted.  
7068                             (<= (nth 1 info) gnus-level-subscribed)))
7069                    (gnus-group-make-articles-read name idlist expirable))))
7070            xref-hashtb)))))
7071
7072 (defun gnus-group-make-articles-read (group articles expirable)
7073   (let* ((num 0)
7074          (entry (gnus-gethash group gnus-newsrc-hashtb))
7075          (info (nth 2 entry))
7076          (active (gnus-gethash group gnus-active-hashtb))
7077          exps expirable range)
7078     ;; First peel off all illegal article numbers.
7079     (if active
7080         (let ((ids articles)
7081               (ticked (cdr (assq 'tick (nth 3 info))))
7082               (dormant (cdr (assq 'dormant (nth 3 info))))
7083               id first)
7084           (setq exps nil)
7085           (while ids
7086             (setq id (car ids))
7087             (if (and first (> id (cdr active)))
7088                 (progn
7089                   ;; We'll end up in this situation in one particular
7090                   ;; obscure situation. If you re-scan a group and get
7091                   ;; a new article that is cross-posted to a different
7092                   ;; group that has not been re-scanned, you might get
7093                   ;; crossposted article that has a higher number than
7094                   ;; Gnus believes possible. So we re-activate this
7095                   ;; group as well. This might mean doing the
7096                   ;; crossposting thingie will *increase* the number
7097                   ;; of articles in some groups. Tsk, tsk.
7098                   (setq active (or (gnus-activate-group group) active))))
7099             (if (or (> id (cdr active))
7100                     (< id (car active))
7101                     (memq id ticked)
7102                     (memq id dormant))
7103                 (setq articles (delq id articles)))
7104             (and (memq id expirable)
7105                  (setq exps (cons id exps)))
7106             (setq ids (cdr ids)))))
7107     ;; Update expirable articles.
7108     (gnus-add-marked-articles nil 'expirable exps info)
7109     (and active
7110          (null (nth 2 info))
7111          (> (car active) 1)
7112          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
7113     (setcar (nthcdr 2 info)
7114             (setq range
7115                   (gnus-add-to-range 
7116                    (nth 2 info) 
7117                    (setq articles (sort articles '<)))))
7118     ;; Then we have to re-compute how many unread
7119     ;; articles there are in this group.
7120     (if active
7121         (progn
7122           (cond 
7123            ((not range)
7124             (setq num (- (1+ (cdr active)) (car active))))
7125            ((not (listp (cdr range)))
7126             (setq num (- (cdr active) (- (1+ (cdr range)) 
7127                                          (car range)))))
7128            (t
7129             (while range
7130               (if (numberp (car range))
7131                   (setq num (1+ num))
7132                 (setq num (+ num (- (1+ (cdr (car range)))
7133                                     (car (car range))))))
7134               (setq range (cdr range)))
7135             (setq num (- (cdr active) num))))
7136           ;; Update the number of unread articles.
7137           (setcar 
7138            entry 
7139            (max 0 (- num 
7140                      (length (cdr (assq 'tick (nth 3 info))))
7141                      (length 
7142                       (cdr (assq 'dormant (nth 3 info)))))))
7143           ;; Update the group buffer.
7144           (gnus-group-update-group group t)))))
7145
7146 (defun gnus-methods-equal-p (m1 m2)
7147   (let ((m1 (or m1 gnus-select-method))
7148         (m2 (or m2 gnus-select-method)))
7149     (or (equal m1 m2)
7150         (and (eq (car m1) (car m2))
7151              (or (not (memq 'address (assoc (symbol-name (car m1))
7152                                             gnus-valid-select-methods)))
7153                  (equal (nth 1 m1) (nth 1 m2)))))))
7154
7155 (defsubst gnus-header-value ()
7156   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7157
7158 (defvar gnus-newsgroup-none-id 0)
7159
7160 (defun gnus-get-newsgroup-headers ()
7161   (setq gnus-article-internal-prepare-hook nil)
7162   (let ((cur nntp-server-buffer)
7163         (dependencies gnus-newsgroup-dependencies)
7164         headers id dep end ref)
7165     (save-excursion
7166       (set-buffer nntp-server-buffer)
7167       (goto-char (point-min))
7168       ;; Search to the beginning of the next header. Error messages
7169       ;; do not begin with 2 or 3.
7170       (while (re-search-forward "^[23][0-9]+ " nil t)
7171         (let ((header (make-vector 9 nil))
7172               (case-fold-search t)
7173               (p (point))
7174               in-reply-to)
7175           (setq id nil
7176                 ref nil)
7177           (mail-header-set-number header (read cur))
7178           ;; This implementation of this function, with nine
7179           ;; search-forwards instead of the one re-search-forward and
7180           ;; a case (which basically was the old function) is actually
7181           ;; about twice as fast, even though it looks messier. You
7182           ;; can't have everything, I guess. Speed and elegance
7183           ;; doesn't always come hand in hand.
7184           (save-restriction
7185             (narrow-to-region (point) (or (save-excursion 
7186                                             (search-forward "\n.\n" nil t))
7187                                           (point)))
7188             (if (search-forward "\nfrom: " nil t)
7189                 (mail-header-set-from header (gnus-header-value))
7190               (mail-header-set-from header "(nobody)"))
7191             (goto-char p)
7192             (if (search-forward "\nsubject: " nil t)
7193                 (mail-header-set-subject header (gnus-header-value))
7194               (mail-header-set-subject header "(none)"))
7195             (goto-char p)
7196             (and (search-forward "\nxref: " nil t)
7197                  (mail-header-set-xref header (gnus-header-value)))
7198             (goto-char p)
7199             (or (numberp (and (search-forward "\nlines: " nil t)
7200                               (mail-header-set-lines header (read cur))))
7201                 (mail-header-set-lines header 0))
7202             (goto-char p)
7203             (and (search-forward "\ndate: " nil t)
7204                  (mail-header-set-date header (gnus-header-value)))
7205             (goto-char p)
7206             (if (search-forward "\nmessage-id: " nil t)
7207                 (mail-header-set-id header (setq id (gnus-header-value)))
7208               ;; If there was no message-id, we just fake one to make
7209               ;; subsequent routines simpler.
7210               (mail-header-set-id 
7211                header 
7212                (setq id (concat "none+" 
7213                                 (int-to-string 
7214                                  (setq gnus-newsgroup-none-id 
7215                                        (1+ gnus-newsgroup-none-id)))))))
7216             (goto-char p)
7217             (if (search-forward "\nreferences: " nil t)
7218                 (progn
7219                   (mail-header-set-references header (gnus-header-value))
7220                   (setq end (match-end 0))
7221                   (save-excursion
7222                     (setq ref 
7223                           (downcase
7224                            (buffer-substring
7225                             (progn 
7226                               (end-of-line)
7227                               (search-backward ">" end t)
7228                               (1+ (point)))
7229                             (progn
7230                               (search-backward "<" end t)
7231                               (point)))))))
7232               ;; Get the references from the in-reply-to header if there
7233               ;; ware no references and the in-reply-to header looks
7234               ;; promising. 
7235               (if (and (search-forward "\nin-reply-to: " nil t)
7236                        (setq in-reply-to (gnus-header-value))
7237                        (string-match "<[^>]+>" in-reply-to))
7238                   (progn
7239                     (mail-header-set-references 
7240                      header 
7241                      (setq ref (substring in-reply-to (match-beginning 0)
7242                                           (match-end 0))))
7243                     (setq ref (downcase ref)))
7244                 (setq ref "none")))
7245             ;; We do some threading while we read the headers. The
7246             ;; message-id and the last reference are both entered into
7247             ;; the same hash table. Some tippy-toeing around has to be
7248             ;; done in case an article has arrived before the article
7249             ;; which it refers to.
7250             (if (boundp (setq dep (intern (downcase id) dependencies)))
7251                 (if (car (symbol-value dep))
7252                     ;; An article with this Message-ID has already
7253                     ;; been seen, so we ignore this one, except we add
7254                     ;; any additional Xrefs (in case the two articles
7255                     ;; came from different servers.
7256                     (progn
7257                       (mail-header-set-xref 
7258                        (car (symbol-value dep))
7259                        (concat (or (mail-header-xref 
7260                                     (car (symbol-value dep))) "")
7261                                (or (mail-header-xref header) "")))
7262                       (setq header nil))
7263                   (setcar (symbol-value dep) header))
7264               (set dep (list header)))
7265             (if header
7266                 (progn
7267                   (if (boundp (setq dep (intern ref dependencies)))
7268                       (setcdr (symbol-value dep) 
7269                               (cons header (cdr (symbol-value dep))))
7270                     (set dep (list nil header)))
7271                   (setq headers (cons header headers))))
7272             (goto-char (point-max))))))
7273     (nreverse headers)))
7274
7275 ;; The following macros and functions were written by Felix Lee
7276 ;; <flee@cse.psu.edu>. 
7277
7278 (defmacro gnus-nov-read-integer ()
7279   '(prog1
7280        (if (= (following-char) ?\t)
7281            0
7282          (let ((num (condition-case nil (read buffer) (error nil))))
7283            (if (numberp num) num 0)))
7284      (or (eobp) (forward-char 1))))
7285
7286 (defmacro gnus-nov-skip-field ()
7287   '(search-forward "\t" eol 'move))
7288
7289 (defmacro gnus-nov-field ()
7290   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7291
7292 ;; Goes through the xover lines and returns a list of vectors
7293 (defun gnus-get-newsgroup-headers-xover (sequence)
7294   "Parse the news overview data in the server buffer, and return a
7295 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7296   ;; Get the Xref when the users reads the articles since most/some
7297   ;; NNTP servers do not include Xrefs when using XOVER.
7298   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7299   (let ((cur nntp-server-buffer)
7300         (dependencies gnus-newsgroup-dependencies)
7301         number headers header)
7302     (save-excursion
7303       (set-buffer nntp-server-buffer)
7304       (goto-char (point-min))
7305       (while (and sequence (not (eobp)))
7306         (setq number (read cur))
7307         (while (and sequence (< (car sequence) number))
7308           (setq sequence (cdr sequence)))
7309         (and sequence 
7310              (eq number (car sequence))
7311              (progn
7312                (setq sequence (cdr sequence))
7313                (if (setq header 
7314                          (inline (gnus-nov-parse-line number dependencies)))
7315                    (setq headers (cons header headers)))))
7316         (forward-line 1))
7317       (setq headers (nreverse headers)))
7318     headers))
7319
7320 ;; This function has to be called with point after the article number
7321 ;; on the beginning of the line.
7322 (defun gnus-nov-parse-line (number dependencies)
7323   (let ((none 0)
7324         (eol (gnus-point-at-eol)) 
7325         (buffer (current-buffer))
7326         header ref id dep)
7327
7328     ;; overview: [num subject from date id refs chars lines misc]
7329     (narrow-to-region (point) eol)
7330     (or (eobp) (forward-char))
7331
7332     (condition-case nil
7333         (setq header
7334               (vector 
7335                number                   ; number
7336                (gnus-nov-field)         ; subject
7337                (gnus-nov-field)         ; from
7338                (gnus-nov-field)         ; date
7339                (setq id (or (gnus-nov-field)
7340                             (concat "none+"
7341                                     (int-to-string 
7342                                      (setq none (1+ none)))))) ; id
7343                (progn
7344                  (save-excursion
7345                    (let ((beg (point)))
7346                      (search-forward "\t" eol)
7347                      (if (search-backward ">" beg t)
7348                          (setq ref 
7349                                (downcase 
7350                                 (buffer-substring 
7351                                  (1+ (point))
7352                                  (progn
7353                                    (search-backward "<" beg t)
7354                                    (point)))))
7355                        (setq ref nil))))
7356                  (gnus-nov-field))      ; refs
7357                (gnus-nov-read-integer)  ; chars
7358                (gnus-nov-read-integer)  ; lines
7359                (if (= (following-char) ?\n)
7360                    nil
7361                  (gnus-nov-field))      ; misc
7362                ))
7363       (error (progn 
7364                (ding)
7365                (message "Strange nov line.")
7366                (setq header nil)
7367                (goto-char eol))))
7368
7369     (widen)
7370
7371     ;; We build the thread tree.
7372     (and header
7373          (if (boundp (setq dep (intern (downcase id) dependencies)))
7374              (if (car (symbol-value dep))
7375                  ;; An article with this Message-ID has already been seen,
7376                  ;; so we ignore this one, except we add any additional
7377                  ;; Xrefs (in case the two articles came from different
7378                  ;; servers.
7379                  (progn
7380                    (mail-header-set-xref 
7381                     (car (symbol-value dep))
7382                     (concat (or (mail-header-xref (car (symbol-value dep))) "")
7383                             (or (mail-header-xref header) "")))
7384                    (setq header nil))
7385                (setcar (symbol-value dep) header))
7386            (set dep (list header))))
7387     (if header
7388         (progn
7389           (if (boundp (setq dep (intern (or ref "none") 
7390                                         dependencies)))
7391               (setcdr (symbol-value dep) 
7392                       (cons header (cdr (symbol-value dep))))
7393             (set dep (list nil header)))))
7394     header))
7395
7396 (defun gnus-article-get-xrefs ()
7397   "Fill in the Xref value in `gnus-current-headers', if necessary.
7398 This is meant to be called in `gnus-article-internal-prepare-hook'."
7399   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7400                                  gnus-current-headers)))
7401     (or (not gnus-use-cross-reference)
7402         (not headers)
7403         (and (mail-header-xref headers)
7404              (not (string= (mail-header-xref headers) "")))
7405         (let ((case-fold-search t)
7406               xref)
7407           (save-restriction
7408             (gnus-narrow-to-headers)
7409             (goto-char (point-min))
7410             (if (or (and (eq (downcase (following-char)) ?x)
7411                          (looking-at "Xref:"))
7412                     (search-forward "\nXref:" nil t))
7413                 (progn
7414                   (goto-char (1+ (match-end 0)))
7415                   (setq xref (buffer-substring (point) 
7416                                                (progn (end-of-line) (point))))
7417                   (mail-header-set-xref headers xref))))))))
7418
7419 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7420 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7421
7422 (defun gnus-make-headers-hashtable-by-number ()
7423   "Make hashtable for the variable gnus-newsgroup-headers by number."
7424   (save-excursion
7425     (set-buffer gnus-summary-buffer)
7426     (let ((headers gnus-newsgroup-headers)
7427           header)
7428       (setq gnus-newsgroup-headers-hashtb-by-number
7429             (gnus-make-hashtable (length headers)))
7430       (while headers
7431         (setq header (car headers))
7432         (gnus-sethash (int-to-string (mail-header-number header))
7433                       header gnus-newsgroup-headers-hashtb-by-number)
7434         (setq headers (cdr headers))))))
7435
7436 (defun gnus-more-header-backward ()
7437   "Find new header backward."
7438   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7439         (artnum gnus-newsgroup-begin)
7440         (header nil))
7441     (while (and (not header)
7442                 (> artnum first))
7443       (setq artnum (1- artnum))
7444       (setq header (gnus-read-header artnum)))
7445     header))
7446
7447 (defun gnus-more-header-forward (&optional backward)
7448   "Find new header forward.
7449 If BACKWARD, find new header backward instead."
7450   (if backward
7451       (gnus-more-header-backward)
7452     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7453           (artnum gnus-newsgroup-end)
7454           (header nil))
7455       (while (and (not header)
7456                   (< artnum last))
7457         (setq artnum (1+ artnum))
7458         (setq header (gnus-read-header artnum)))
7459       header)))
7460
7461 (defun gnus-extend-newsgroup (header &optional backward)
7462   "Extend newsgroup selection with HEADER.
7463 Optional argument BACKWARD means extend toward backward."
7464   (if header
7465       (let ((artnum (mail-header-number header)))
7466         (setq gnus-newsgroup-headers
7467               (if backward
7468                   (cons header gnus-newsgroup-headers)
7469                 (nconc gnus-newsgroup-headers (list header))))
7470         (setq gnus-newsgroup-unselected
7471               (delq artnum gnus-newsgroup-unselected))
7472         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7473         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7474
7475 (defun gnus-summary-work-articles (n)
7476   "Return a list of articles to be worked upon. The prefix argument,
7477 the list of process marked articles, and the current article will be
7478 taken into consideration."
7479   (let (articles)
7480     (if (and n (numberp n))
7481         (let ((backward (< n 0))
7482               (n (abs n)))
7483           (save-excursion
7484             (while (and (> n 0)
7485                         (setq articles (cons (gnus-summary-article-number) 
7486                                              articles))
7487                         (gnus-summary-search-forward nil nil backward))
7488               (setq n (1- n))))
7489           (sort articles (function <)))
7490       (or (reverse gnus-newsgroup-processable)
7491           (list (gnus-summary-article-number))))))
7492
7493 (defun gnus-summary-search-group (&optional backward use-level)
7494   "Search for next unread newsgroup.
7495 If optional argument BACKWARD is non-nil, search backward instead."
7496   (save-excursion
7497     (set-buffer gnus-group-buffer)
7498     (if (gnus-group-search-forward 
7499          backward nil (if use-level (gnus-group-group-level) nil))
7500         (gnus-group-group-name))))
7501
7502 (defun gnus-summary-best-group (&optional exclude-group)
7503   "Find the name of the best unread group.
7504 If EXCLUDE-GROUP, do not go to this group."
7505   (save-excursion
7506     (set-buffer gnus-group-buffer)
7507     (save-excursion
7508       (gnus-group-best-unread-group exclude-group))))
7509
7510 (defun gnus-subject-equal (s1 s2)
7511   (cond
7512    ((null gnus-summary-gather-subject-limit)
7513     (equal (gnus-simplify-subject-re s1)
7514            (gnus-simplify-subject-re s2)))
7515    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7516     (equal (gnus-simplify-subject-fuzzy s1)
7517            (gnus-simplify-subject-fuzzy s2)))
7518    ((numberp gnus-summary-gather-subject-limit)
7519     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7520            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7521    (t
7522     (equal s1 s2))))
7523     
7524 (defun gnus-summary-search-subject (&optional backward unread subject)
7525   "Search for article forward.
7526 If BACKWARD is non-nil, search backward.
7527 If UNREAD is non-nil, only unread articles are selected.
7528 If SUBJECT is non-nil, the article which has the same subject will be
7529 searched for." 
7530   (let ((func (if backward 'previous-single-property-change
7531                 'next-single-property-change))
7532         (beg (point))
7533         (did t)
7534         pos psubject)
7535     (beginning-of-line)
7536     (and gnus-summary-check-current unread
7537          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7538          (setq did nil))
7539     (if (not did)
7540         ()
7541       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7542       (while
7543           (and 
7544            (setq pos (funcall func (point) 'gnus-number))
7545            (goto-char (if backward (1- pos) pos))
7546            (setq did
7547                  (not (and
7548                        (or (not unread)
7549                            (eq (get-text-property (point) 'gnus-mark)
7550                                gnus-unread-mark))
7551                        (or (not subject)
7552                            (and (setq psubject 
7553                                       (inline (gnus-summary-subject-string)))
7554                                 (inline 
7555                                   (gnus-subject-equal subject psubject)))))))
7556            (if backward (if (bobp) nil (forward-char -1) t)
7557              (if (eobp) nil (forward-char 1) t)))))
7558     (if did
7559         (progn (goto-char beg) nil)
7560       (prog1
7561           (get-text-property (point) 'gnus-number)
7562         (gnus-summary-show-thread)
7563         (gnus-summary-position-cursor)))))
7564
7565 (defun gnus-summary-pseudo-article ()
7566   "The thread level of the article on the current line."
7567   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7568
7569 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7570 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7571 (defun gnus-summary-article-score ()
7572   "Return current article score."
7573   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7574       gnus-summary-default-score 0))
7575
7576 (defun gnus-summary-recenter ()
7577   "Center point in the summary window.
7578 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7579 displayed, no centering will be performed." 
7580   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7581   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7582   (let* ((top (cond ((< (window-height) 4) 0)
7583                     ((< (window-height) 7) 1)
7584                     (t 2)))
7585          (height (1- (window-height)))
7586          (bottom (save-excursion (goto-char (point-max))
7587                                  (forward-line (- height))
7588                                  (point)))
7589          (window (get-buffer-window (current-buffer))))
7590     (and 
7591      ;; The user has to want it,
7592      gnus-auto-center-summary 
7593      ;; the article buffer must be displayed,
7594      (get-buffer-window gnus-article-buffer)
7595      ;; Set the window start to either `bottom', which is the biggest
7596      ;; possible valid number, or the second line from the top,
7597      ;; whichever is the least.
7598      (set-window-start
7599       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7600
7601 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7602 (defun gnus-short-group-name (group &optional levels)
7603   "Collapse GROUP name LEVELS."
7604   (let* ((name "") (foreign "") (depth -1) (skip 1)
7605          (levels (or levels
7606                      (progn
7607                        (while (string-match "\\." group skip)
7608                          (setq skip (match-end 0)
7609                                depth (+ depth 1)))
7610                        depth))))
7611     (if (string-match ":" group)
7612         (setq foreign (substring group 0 (match-end 0))
7613               group (substring group (match-end 0))))
7614     (while group
7615       (if (and (string-match "\\." group) (> levels 0))
7616           (setq name (concat name (substring group 0 1))
7617                 group (substring group (match-end 0))
7618                 levels (- levels 1)
7619                 name (concat name "."))
7620         (setq name (concat foreign name group)
7621               group nil)))
7622     name))
7623
7624 (defun gnus-summary-jump-to-group (newsgroup)
7625   "Move point to NEWSGROUP in group mode buffer."
7626   ;; Keep update point of group mode buffer if visible.
7627   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7628       (save-window-excursion
7629         ;; Take care of tree window mode.
7630         (if (get-buffer-window gnus-group-buffer)
7631             (pop-to-buffer gnus-group-buffer))
7632         (gnus-group-jump-to-group newsgroup))
7633     (save-excursion
7634       ;; Take care of tree window mode.
7635       (if (get-buffer-window gnus-group-buffer)
7636           (pop-to-buffer gnus-group-buffer)
7637         (set-buffer gnus-group-buffer))
7638       (gnus-group-jump-to-group newsgroup))))
7639
7640 ;; This function returns a list of article numbers based on the
7641 ;; difference between the ranges of read articles in this group and
7642 ;; the range of active articles.
7643 (defun gnus-list-of-unread-articles (group)
7644   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7645          (active (gnus-gethash group gnus-active-hashtb))
7646          (last (cdr active))
7647          first nlast unread)
7648     ;; If none are read, then all are unread. 
7649     (if (not read)
7650         (setq first (car active))
7651       ;; If the range of read articles is a single range, then the
7652       ;; first unread article is the article after the last read
7653       ;; article. Sounds logical, doesn't it?
7654       (if (not (listp (cdr read)))
7655           (setq first (1+ (cdr read)))
7656         ;; `read' is a list of ranges.
7657         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7658                                 (car (car read)))) 1)
7659             (setq first 1))
7660         (while read
7661           (if first 
7662               (while (< first nlast)
7663                 (setq unread (cons first unread))
7664                 (setq first (1+ first))))
7665           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7666           (setq nlast (if (atom (car (cdr read))) 
7667                           (car (cdr read))
7668                         (car (car (cdr read)))))
7669           (setq read (cdr read)))))
7670     ;; And add the last unread articles.
7671     (while (<= first last)
7672       (setq unread (cons first unread))
7673       (setq first (1+ first)))
7674     ;; Return the list of unread articles.
7675     (nreverse unread)))
7676
7677 (defun gnus-list-of-read-articles (group)
7678   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7679         (active (gnus-gethash group gnus-active-hashtb)))
7680     (and info active
7681          (gnus-sorted-complement 
7682           (gnus-uncompress-range active) 
7683           (gnus-list-of-unread-articles group)))))
7684
7685 ;; Various summary commands
7686
7687 (defun gnus-summary-universal-argument ()
7688   "Perform any operation on all articles marked with the process mark."
7689   (interactive)
7690   (gnus-set-global-variables)
7691   (let ((articles (reverse gnus-newsgroup-processable))
7692         func)
7693     (or articles (error "No articles marked"))
7694     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7695         (error "Undefined key"))
7696     (while articles
7697       (gnus-summary-goto-subject (car articles))
7698       (command-execute func)
7699       (gnus-summary-remove-process-mark (car articles))
7700       (setq articles (cdr articles)))))
7701
7702 (defun gnus-summary-toggle-truncation (&optional arg)
7703   "Toggle truncation of summary lines.
7704 With arg, turn line truncation on iff arg is positive."
7705   (interactive "P")
7706   (setq truncate-lines
7707         (if (null arg) (not truncate-lines)
7708           (> (prefix-numeric-value arg) 0)))
7709   (redraw-display))
7710
7711 (defun gnus-summary-reselect-current-group (&optional all)
7712   "Once exit and then reselect the current newsgroup.
7713 The prefix argument ALL means to select all articles."
7714   (interactive "P")
7715   (gnus-set-global-variables)
7716   (let ((current-subject (gnus-summary-article-number))
7717         (group gnus-newsgroup-name))
7718     (setq gnus-newsgroup-begin nil)
7719     (gnus-summary-exit t)
7720     ;; We have to adjust the point of group mode buffer because the
7721     ;; current point was moved to the next unread newsgroup by
7722     ;; exiting.
7723     (gnus-summary-jump-to-group group)
7724     (gnus-group-read-group all t)
7725     (gnus-summary-goto-subject current-subject)))
7726
7727 (defun gnus-summary-rescan-group (&optional all)
7728   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7729   (interactive "P")
7730   (gnus-set-global-variables)
7731   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7732   (let ((group gnus-newsgroup-name))
7733     (gnus-summary-exit)
7734     (gnus-summary-jump-to-group group)
7735     (save-excursion
7736       (set-buffer gnus-group-buffer)
7737       (gnus-group-get-new-news-this-group 1))
7738     (gnus-summary-jump-to-group group)
7739     (gnus-group-read-group all)))
7740
7741 (defun gnus-summary-update-info ()
7742   (let* ((group gnus-newsgroup-name))
7743     (if gnus-newsgroup-kill-headers
7744         (setq gnus-newsgroup-killed
7745               (gnus-compress-sequence
7746                (nconc
7747                 (gnus-set-sorted-intersection
7748                  (gnus-uncompress-range gnus-newsgroup-killed)
7749                  (setq gnus-newsgroup-unselected
7750                        (sort gnus-newsgroup-unselected '<)))
7751                 (setq gnus-newsgroup-unreads
7752                       (sort gnus-newsgroup-unreads '<))) t)))
7753     (or (listp (cdr gnus-newsgroup-killed))
7754         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7755     (let ((headers gnus-newsgroup-headers))
7756       (gnus-close-group group)
7757       (run-hooks 'gnus-exit-group-hook)
7758       (gnus-update-read-articles 
7759        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7760        gnus-newsgroup-marked
7761        t gnus-newsgroup-replied gnus-newsgroup-expirable
7762        gnus-newsgroup-killed gnus-newsgroup-dormant
7763        gnus-newsgroup-bookmarks 
7764        (and gnus-save-score gnus-newsgroup-scored))
7765       (and gnus-use-cross-reference
7766            (gnus-mark-xrefs-as-read 
7767             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7768       ;; Do adaptive scoring, and possibly save score files.
7769       (and gnus-newsgroup-adaptive
7770            (gnus-score-adaptive))
7771       (and gnus-use-scoring 
7772            (fboundp 'gnus-score-save)
7773            (funcall 'gnus-score-save))
7774       ;; Do not switch windows but change the buffer to work.
7775       (set-buffer gnus-group-buffer)
7776       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7777           (gnus-group-update-group group)))))
7778   
7779 (defun gnus-summary-exit (&optional temporary)
7780   "Exit reading current newsgroup, and then return to group selection mode.
7781 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7782   (interactive)
7783   (gnus-set-global-variables)
7784   (gnus-kill-save-kill-buffer)
7785   (let* ((group gnus-newsgroup-name)
7786          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7787                                                   gnus-newsgroup-name))))
7788          (mode major-mode)
7789          (buf (current-buffer)))
7790     (run-hooks 'gnus-summary-prepare-exit-hook)
7791     ;; Make all changes in this group permanent.
7792     (gnus-summary-update-info)          
7793     (set-buffer buf)
7794     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7795     ;; Make sure where I was, and go to next newsgroup.
7796     (set-buffer gnus-group-buffer)
7797     (or quit-config
7798         (progn
7799           (gnus-group-jump-to-group group)
7800           (gnus-group-next-unread-group 1)))
7801     (if temporary
7802         nil                             ;Nothing to do.
7803       ;; We set all buffer-local variables to nil. It is unclear why
7804       ;; this is needed, but if we don't, buffer-local variables are
7805       ;; not garbage-collected, it seems. This would the lead to en
7806       ;; ever-growing Emacs.
7807       (set-buffer buf)
7808       (gnus-summary-clear-local-variables)
7809       ;; We clear the global counterparts of the buffer-local
7810       ;; variables as well, just to be on the safe side.
7811       (gnus-configure-windows 'group 'force)
7812       (gnus-summary-clear-local-variables)
7813       ;; Return to group mode buffer. 
7814       (if (eq mode 'gnus-summary-mode)
7815           (gnus-kill-buffer buf))
7816       (if (get-buffer gnus-article-buffer)
7817           (bury-buffer gnus-article-buffer))
7818       (setq gnus-current-select-method gnus-select-method)
7819       (pop-to-buffer gnus-group-buffer)
7820       (if (not quit-config)
7821           (progn
7822             (gnus-group-jump-to-group group)
7823             (gnus-group-next-unread-group 1))
7824         (if (not (buffer-name (car quit-config)))
7825             (gnus-configure-windows 'group 'force)
7826           (set-buffer (car quit-config))
7827           (and (eq major-mode 'gnus-summary-mode)
7828                (gnus-set-global-variables))
7829           (gnus-configure-windows (cdr quit-config))))
7830       (run-hooks 'gnus-summary-exit-hook))))
7831
7832 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7833 (defun gnus-summary-exit-no-update (&optional no-questions)
7834   "Quit reading current newsgroup without updating read article info."
7835   (interactive)
7836   (gnus-set-global-variables)
7837   (let* ((group gnus-newsgroup-name)
7838          (quit-config (nth 1 (assoc 'quit-config 
7839                                     (gnus-find-method-for-group group)))))
7840     (if (or no-questions
7841             gnus-expert-user
7842             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7843         (progn
7844           (gnus-close-group group)
7845           (gnus-summary-clear-local-variables)
7846           (set-buffer gnus-group-buffer)
7847           (gnus-summary-clear-local-variables)
7848           ;; Return to group selection mode.
7849           (gnus-configure-windows 'group 'force)
7850           (if (get-buffer gnus-summary-buffer)
7851               (kill-buffer gnus-summary-buffer))
7852           (if (get-buffer gnus-article-buffer)
7853               (bury-buffer gnus-article-buffer))
7854           (if (equal (gnus-group-group-name) group)
7855               (gnus-group-next-unread-group 1))
7856           (if quit-config
7857               (progn
7858                 (if (not (buffer-name (car quit-config)))
7859                     (gnus-configure-windows 'group 'force)
7860                   (set-buffer (car quit-config))
7861                   (and (eq major-mode 'gnus-summary-mode)
7862                        (gnus-set-global-variables))
7863                   (gnus-configure-windows (cdr quit-config)))))))))
7864
7865 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7866 (defun gnus-summary-fetch-faq (group)
7867   "Fetch the FAQ for the current group."
7868   (interactive (list gnus-newsgroup-name))
7869   (let (gnus-faq-buffer)
7870     (and (setq gnus-faq-buffer (gnus-group-fetch-faq group))
7871          (gnus-configure-windows 'summary-faq))))
7872
7873 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7874 (defun gnus-summary-describe-group (&optional force)
7875   "Describe the current newsgroup."
7876   (interactive "P")
7877   (gnus-group-describe-group force gnus-newsgroup-name))
7878
7879 (defun gnus-summary-describe-briefly ()
7880   "Describe summary mode commands briefly."
7881   (interactive)
7882   (gnus-message 6
7883                 (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")))
7884
7885 ;; Walking around group mode buffer from summary mode.
7886
7887 (defun gnus-summary-next-group (&optional no-article target-group backward)
7888   "Exit current newsgroup and then select next unread newsgroup.
7889 If prefix argument NO-ARTICLE is non-nil, no article is selected
7890 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7891 previous group instead."
7892   (interactive "P")
7893   (gnus-set-global-variables)
7894   (let ((current-group gnus-newsgroup-name)
7895         (current-buffer (current-buffer))
7896         entered)
7897     ;; First we semi-exit this group to update Xrefs and all variables.
7898     ;; We can't do a real exit, because the window conf must remain
7899     ;; the same in case the user is prompted for info, and we don't
7900     ;; want the window conf to change before that...
7901     (gnus-summary-exit t)
7902     (while (not entered)
7903       ;; Then we find what group we are supposed to enter.
7904       (set-buffer gnus-group-buffer)
7905       (gnus-group-jump-to-group current-group)
7906       (setq target-group 
7907             (or target-group        
7908                 (if (eq gnus-keep-same-level 'best) 
7909                     (gnus-summary-best-group gnus-newsgroup-name)
7910                   (gnus-summary-search-group backward gnus-keep-same-level))))
7911       (if (not target-group)
7912           ;; There are no further groups, so we return to the group
7913           ;; buffer.
7914           (progn
7915             (gnus-message 5 "Returning to the group buffer")
7916             (setq entered t)
7917             (set-buffer current-buffer)
7918             (gnus-summary-exit))
7919         ;; We try to enter the target group.
7920         (gnus-group-jump-to-group target-group)
7921         (let ((unreads (gnus-group-group-unread)))
7922           (if (and (or (eq t unreads)
7923                        (and unreads (not (zerop unreads))))
7924                    (gnus-summary-read-group
7925                     target-group nil no-article current-buffer))
7926               (setq entered t)
7927             (setq current-group target-group
7928                   target-group nil)))))))
7929
7930 (defun gnus-summary-next-group-old (&optional no-article group backward)
7931   "Exit current newsgroup and then select next unread newsgroup.
7932 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7933 If BACKWARD, go to previous group instead."
7934   (interactive "P")
7935   (gnus-set-global-variables)
7936   (let ((ingroup gnus-newsgroup-name)
7937         (sumbuf (current-buffer))
7938         num)
7939     (set-buffer gnus-group-buffer)
7940     (if (and group
7941              (or (and (numberp (setq num (car (gnus-gethash
7942                                                group gnus-newsrc-hashtb))))
7943                       (< num 1))
7944                  (null num)))
7945         (progn
7946           (gnus-group-jump-to-group group)
7947           (setq group nil))
7948       (gnus-group-jump-to-group ingroup))
7949     (gnus-summary-search-group backward)
7950     (let ((group (or group (gnus-summary-search-group backward))))
7951       (set-buffer sumbuf)
7952       (gnus-summary-exit t)             ;Update all information.
7953       (if (null group)
7954           (gnus-summary-exit-no-update t)
7955         (gnus-group-jump-to-group ingroup)
7956         (setq group (gnus-summary-search-group backward))
7957         (gnus-message 5 "Selecting %s..." group)
7958         (set-buffer gnus-group-buffer)
7959         ;; We are now in group mode buffer.
7960         ;; Make sure group mode buffer point is on GROUP.
7961         (gnus-group-jump-to-group group)
7962         (if (not (eq gnus-auto-select-next 'quietly))
7963             (progn
7964               (gnus-summary-read-group group nil no-article sumbuf)
7965               (and (string= gnus-newsgroup-name ingroup)
7966                    (bufferp sumbuf) (buffer-name sumbuf)
7967                    (progn
7968                      (set-buffer (setq gnus-summary-buffer sumbuf))
7969                      (gnus-summary-exit-no-update t))))
7970           (let ((prevgroup group))
7971             (gnus-group-jump-to-group ingroup)
7972             (setq group (gnus-summary-search-group backward))
7973             (gnus-summary-read-group group nil no-article sumbuf)
7974             (while (and (string= gnus-newsgroup-name ingroup)
7975                         (bufferp sumbuf) 
7976                         (buffer-name sumbuf)
7977                         (not (string= prevgroup (gnus-group-group-name))))
7978               (set-buffer gnus-group-buffer)
7979               (gnus-summary-read-group 
7980                (setq prevgroup (gnus-group-group-name)) 
7981                nil no-article sumbuf))
7982             (and (string= prevgroup (gnus-group-group-name))
7983                  ;; We have reached the final group in the group
7984                  ;; buffer.
7985                  (progn
7986                    (if (buffer-name sumbuf)
7987                        (progn
7988                          (set-buffer sumbuf)
7989                          (gnus-summary-exit)))))))))))
7990
7991 (defun gnus-summary-prev-group (&optional no-article)
7992   "Exit current newsgroup and then select previous unread newsgroup.
7993 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7994   (interactive "P")
7995   (gnus-summary-next-group no-article nil t))
7996
7997 ;; Walking around summary lines.
7998
7999 (defun gnus-summary-first-subject (&optional unread)
8000   "Go to the first unread subject.
8001 If UNREAD is non-nil, go to the first unread article.
8002 Returns nil if there are no unread articles."
8003   (interactive "P")
8004   (prog1
8005       (cond ((not unread)
8006              (goto-char (point-min)))
8007             ((gnus-goto-char 
8008               (text-property-any 
8009                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
8010              t)
8011             (t 
8012              ;; There are no unread articles.
8013              (gnus-message 3 "No more unread articles")
8014              nil))
8015     (gnus-summary-position-cursor)))
8016
8017 (defun gnus-summary-next-subject (n &optional unread dont-display)
8018   "Go to next N'th summary line.
8019 If N is negative, go to the previous N'th subject line.
8020 If UNREAD is non-nil, only unread articles are selected.
8021 The difference between N and the actual number of steps taken is
8022 returned."
8023   (interactive "p")
8024   (let ((backward (< n 0))
8025         (n (abs n)))
8026     (while (and (> n 0)
8027                 (gnus-summary-search-forward unread nil backward))
8028       (setq n (1- n)))
8029     (if (/= 0 n) (gnus-message 7 "No more%s articles"
8030                                (if unread " unread" "")))
8031     (or dont-display
8032         (progn
8033           (gnus-summary-recenter)
8034           (gnus-summary-position-cursor)))
8035     n))
8036
8037 (defun gnus-summary-next-unread-subject (n)
8038   "Go to next N'th unread summary line."
8039   (interactive "p")
8040   (gnus-summary-next-subject n t))
8041
8042 (defun gnus-summary-prev-subject (n &optional unread)
8043   "Go to previous N'th summary line.
8044 If optional argument UNREAD is non-nil, only unread article is selected."
8045   (interactive "p")
8046   (gnus-summary-next-subject (- n) unread))
8047
8048 (defun gnus-summary-prev-unread-subject (n)
8049   "Go to previous N'th unread summary line."
8050   (interactive "p")
8051   (gnus-summary-next-subject (- n) t))
8052
8053 (defun gnus-summary-goto-subject (article)
8054   "Go the subject line of ARTICLE."
8055   (interactive
8056    (list
8057     (string-to-int
8058      (completing-read "Article number: "
8059                       (mapcar
8060                        (lambda (headers)
8061                          (list
8062                           (int-to-string (mail-header-number headers))))
8063                        gnus-newsgroup-headers)
8064                       nil 'require-match))))
8065   (or article (error "No article number"))
8066   (let ((b (point)))
8067     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
8068                                                 'gnus-number article)))
8069         ()
8070       (gnus-summary-show-thread)
8071       ;; Skip dummy articles. 
8072       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8073           (forward-line 1))
8074       (prog1
8075           (if (not (eobp))
8076               article
8077             (goto-char b)
8078             nil)
8079         (gnus-summary-position-cursor)))))
8080
8081 ;; Walking around summary lines with displaying articles.
8082
8083 (defun gnus-summary-expand-window (&optional arg)
8084   "Make the summary buffer take up the entire Emacs frame.
8085 Given a prefix, will force an `article' buffer configuration."
8086   (interactive "P")
8087   (gnus-set-global-variables)
8088   (if arg
8089       (gnus-configure-windows 'article 'force)
8090     (gnus-configure-windows 'summary 'force)))
8091
8092 (defun gnus-summary-display-article (article &optional all-header)
8093   "Display ARTICLE in article buffer."
8094   (gnus-set-global-variables)
8095   (if (null article)
8096       nil
8097     (prog1
8098         (gnus-article-prepare article all-header)
8099       (gnus-summary-show-thread)
8100       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8101           (progn
8102             (forward-line 1)
8103             (gnus-summary-position-cursor)))
8104       (run-hooks 'gnus-select-article-hook)
8105       (gnus-summary-recenter)
8106       (gnus-summary-goto-subject article)
8107       ;; Successfully display article.
8108       (gnus-summary-update-line)
8109       (gnus-article-set-window-start 
8110        (cdr (assq article gnus-newsgroup-bookmarks)))
8111       t)))
8112
8113 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
8114   "Select the current article.
8115 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
8116 non-nil, the article will be re-fetched even if it already present in
8117 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
8118 be displayed."
8119   (and (not pseudo) (gnus-summary-pseudo-article)
8120        (error "This is a pseudo-article."))
8121   (let ((article (or article (gnus-summary-article-number)))
8122         (all-headers (not (not all-headers))) ;Must be T or NIL.
8123         did) 
8124     (prog1
8125         (save-excursion
8126           (set-buffer gnus-summary-buffer)
8127           (if (or (null gnus-current-article)
8128                   (null gnus-article-current)
8129                   (null (get-buffer gnus-article-buffer))
8130                   (not (eq article (cdr gnus-article-current)))
8131                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
8132                   force)
8133               ;; The requested article is different from the current article.
8134               (progn
8135                 (gnus-summary-display-article article all-headers)
8136                 (setq did article))
8137             (if (or all-headers gnus-show-all-headers) 
8138                 (gnus-article-show-all-headers))
8139             nil))
8140       (if did 
8141           (gnus-article-set-window-start 
8142            (cdr (assq article gnus-newsgroup-bookmarks)))))))
8143
8144 (defun gnus-summary-set-current-mark (&optional current-mark)
8145   "Obsolete function."
8146   nil)
8147
8148 (defun gnus-summary-next-article (&optional unread subject backward)
8149   "Select the next article.
8150 If UNREAD, only unread articles are selected.
8151 If SUBJECT, only articles with SUBJECT are selected.
8152 If BACKWARD, the previous article is selected instead of the next."
8153   (interactive "P")
8154   (gnus-set-global-variables)
8155   (let (header)
8156     (cond
8157      ;; Is there such an article?
8158      ((and (gnus-summary-search-forward unread subject backward)
8159            (or (gnus-summary-display-article (gnus-summary-article-number))
8160                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8161       (gnus-summary-position-cursor))
8162      ;; If not, we try the first unread, if that is wanted.
8163      ((and subject
8164            gnus-auto-select-same
8165            (or (gnus-summary-first-unread-article)
8166                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8167       (gnus-summary-position-cursor)
8168       (gnus-message 6 "Wrapped"))
8169      ;; Try to get next/previous article not displayed in this group.
8170      ((and gnus-auto-extend-newsgroup
8171            (not unread) (not subject)
8172            (setq header (gnus-more-header-forward backward)))
8173       (gnus-extend-newsgroup header backward)
8174       (let ((buffer-read-only nil))
8175         (goto-char (if backward (point-min) (point-max)))
8176         (gnus-summary-prepare-threads (list header)))
8177       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8178                                    gnus-newsgroup-end)))
8179      ;; Go to next/previous group.
8180      (t
8181       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8182           (gnus-summary-jump-to-group gnus-newsgroup-name))
8183       (let ((cmd last-command-char)
8184             (group 
8185              (if (eq gnus-keep-same-level 'best) 
8186                  (gnus-summary-best-group gnus-newsgroup-name)
8187                (gnus-summary-search-group backward gnus-keep-same-level))))
8188         ;; For some reason, the group window gets selected. We change
8189         ;; it back.  
8190         (select-window (get-buffer-window (current-buffer)))
8191         ;; Keep just the event type of CMD.
8192                                         ;(and (listp cmd) (setq cmd (car cmd)))
8193         ;; Select next unread newsgroup automagically.
8194         (cond 
8195          ((not gnus-auto-select-next)
8196           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8197          ((eq gnus-auto-select-next 'quietly)
8198           ;; Select quietly.
8199           (if (assoc 'quit-config (gnus-find-method-for-group 
8200                                    gnus-newsgroup-name))
8201               (gnus-summary-exit)
8202             (gnus-message 7 "No more%s articles (%s)..."
8203                           (if unread " unread" "") 
8204                           (if group (concat "selecting " group)
8205                             "exiting"))
8206             (gnus-summary-next-group nil group backward)))
8207          (t
8208           (let ((keystrokes '(?\C-n ?\C-p))
8209                 key)
8210             (while (or (null key) (memq key keystrokes))
8211               (gnus-message 
8212                7 "No more%s articles%s" (if unread " unread" "")
8213                (if (and group (not (assoc 'quit-config
8214                                           (gnus-find-method-for-group 
8215                                            gnus-newsgroup-name))))
8216                    (format " (Type %s for %s [%s])"
8217                            (single-key-description cmd) group
8218                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8219                  (format " (Type %s to exit %s)"
8220                          (single-key-description cmd)
8221                          gnus-newsgroup-name)))
8222               ;; Confirm auto selection.
8223               (let* ((event (read-char)))
8224                 (setq key (if (listp event) (car event) event))
8225                 (if (memq key keystrokes)
8226                     (let ((obuf (current-buffer)))
8227                       (switch-to-buffer gnus-group-buffer)
8228                       (and group
8229                            (gnus-group-jump-to-group group))
8230                       (condition-case ()
8231                           (execute-kbd-macro (char-to-string key))
8232                         (error (ding) nil))
8233                       (setq group (gnus-group-group-name))
8234                       (switch-to-buffer obuf)))))
8235             (if (equal key cmd)
8236                 (if (or (not group) (assoc 'quit-config
8237                                            (gnus-find-method-for-group
8238                                             gnus-newsgroup-name)))
8239                     (gnus-summary-exit)
8240                   (gnus-summary-next-group nil group backward))
8241               (execute-kbd-macro (char-to-string key)))))))))))
8242
8243 (defun gnus-summary-next-unread-article ()
8244   "Select unread article after current one."
8245   (interactive)
8246   (gnus-summary-next-article t (and gnus-auto-select-same
8247                                     (gnus-summary-subject-string))))
8248
8249 (defun gnus-summary-prev-article (&optional unread subject)
8250   "Select the article after the current one.
8251 If UNREAD is non-nil, only unread articles are selected."
8252   (interactive "P")
8253   (gnus-summary-next-article unread subject t))
8254
8255 (defun gnus-summary-prev-unread-article ()
8256   "Select unred article before current one."
8257   (interactive)
8258   (gnus-summary-prev-article t (and gnus-auto-select-same
8259                                     (gnus-summary-subject-string))))
8260
8261 (defun gnus-summary-next-page (&optional lines circular)
8262   "Show next page of selected article.
8263 If end of article, select next article.
8264 Argument LINES specifies lines to be scrolled up.
8265 If CIRCULAR is non-nil, go to the start of the article instead of 
8266 instead of selecting the next article when reaching the end of the
8267 current article." 
8268   (interactive "P")
8269   (setq gnus-summary-buffer (current-buffer))
8270   (gnus-set-global-variables)
8271   (let ((article (gnus-summary-article-number))
8272         (endp nil))
8273     (gnus-configure-windows 'article)
8274     (if (or (null gnus-current-article)
8275             (null gnus-article-current)
8276             (/= article (cdr gnus-article-current))
8277             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8278         ;; Selected subject is different from current article's.
8279         (gnus-summary-display-article article)
8280       (gnus-eval-in-buffer-window
8281        gnus-article-buffer
8282        (setq endp (gnus-article-next-page lines)))
8283       (if endp
8284           (cond (circular
8285                  (gnus-summary-beginning-of-article))
8286                 (lines
8287                  (gnus-message 3 "End of message"))
8288                 ((null lines)
8289                  (gnus-summary-next-unread-article)))))
8290     (gnus-summary-recenter)
8291     (gnus-summary-position-cursor)))
8292
8293 (defun gnus-summary-prev-page (&optional lines)
8294   "Show previous page of selected article.
8295 Argument LINES specifies lines to be scrolled down."
8296   (interactive "P")
8297   (gnus-set-global-variables)
8298   (let ((article (gnus-summary-article-number)))
8299     (gnus-configure-windows 'article)
8300     (if (or (null gnus-current-article)
8301             (null gnus-article-current)
8302             (/= article (cdr gnus-article-current))
8303             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8304         ;; Selected subject is different from current article's.
8305         (gnus-summary-display-article article)
8306       (gnus-summary-recenter)
8307       (gnus-eval-in-buffer-window gnus-article-buffer
8308                                   (gnus-article-prev-page lines))))
8309   (gnus-summary-position-cursor))
8310
8311 (defun gnus-summary-scroll-up (lines)
8312   "Scroll up (or down) one line current article.
8313 Argument LINES specifies lines to be scrolled up (or down if negative)."
8314   (interactive "p")
8315   (gnus-set-global-variables)
8316   (gnus-configure-windows 'article)
8317   (or (gnus-summary-select-article nil nil 'pseudo)
8318       (gnus-eval-in-buffer-window 
8319        gnus-article-buffer
8320        (cond ((> lines 0)
8321               (if (gnus-article-next-page lines)
8322                   (gnus-message 3 "End of message")))
8323              ((< lines 0)
8324               (gnus-article-prev-page (- lines))))))
8325   (gnus-summary-recenter)
8326   (gnus-summary-position-cursor))
8327
8328 (defun gnus-summary-next-same-subject ()
8329   "Select next article which has the same subject as current one."
8330   (interactive)
8331   (gnus-set-global-variables)
8332   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8333
8334 (defun gnus-summary-prev-same-subject ()
8335   "Select previous article which has the same subject as current one."
8336   (interactive)
8337   (gnus-set-global-variables)
8338   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8339
8340 (defun gnus-summary-next-unread-same-subject ()
8341   "Select next unread article which has the same subject as current one."
8342   (interactive)
8343   (gnus-set-global-variables)
8344   (gnus-summary-next-article t (gnus-summary-subject-string)))
8345
8346 (defun gnus-summary-prev-unread-same-subject ()
8347   "Select previous unread article which has the same subject as current one."
8348   (interactive)
8349   (gnus-set-global-variables)
8350   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8351
8352 (defun gnus-summary-first-unread-article ()
8353   "Select the first unread article. 
8354 Return nil if there are no unread articles."
8355   (interactive)
8356   (gnus-set-global-variables)
8357   (prog1
8358       (if (gnus-summary-first-subject t)
8359           (progn
8360             (gnus-summary-show-thread)
8361             (gnus-summary-first-subject t)
8362             (gnus-summary-display-article (gnus-summary-article-number))))
8363     (gnus-summary-position-cursor)))
8364
8365 (defun gnus-summary-best-unread-article ()
8366   "Select the unread article with the highest score."
8367   (interactive)
8368   (gnus-set-global-variables)
8369   (let ((best -1000000)
8370         article score)
8371     (save-excursion
8372       (or (gnus-summary-first-subject t)
8373           (error "No unread articles"))
8374       (while 
8375           (and
8376            (progn
8377              (and (> (setq score (gnus-summary-article-score)) best)
8378                   (setq best score
8379                         article (gnus-summary-article-number)))
8380              t)
8381            (gnus-summary-search-subject nil t))))
8382     (if (not article)
8383         (error "No unread articles")
8384       (gnus-summary-goto-article article))
8385     (gnus-summary-position-cursor)))
8386
8387 (defun gnus-summary-goto-article (article &optional all-headers)
8388   "Fetch ARTICLE and display it if it exists.
8389 If ALL-HEADERS is non-nil, no header lines are hidden."
8390   (interactive
8391    (list
8392     (string-to-int
8393      (completing-read 
8394       "Article number: "
8395       (mapcar (lambda (headers) 
8396                 (list (int-to-string (mail-header-number headers))))
8397               gnus-newsgroup-headers) 
8398       nil 'require-match))))
8399   (prog1
8400       (and (gnus-summary-goto-subject article)
8401            (gnus-summary-display-article article all-headers))
8402     (gnus-summary-position-cursor)))
8403
8404 (defun gnus-summary-goto-last-article ()
8405   "Go to the previously read article."
8406   (interactive)
8407   (prog1
8408       (and gnus-last-article
8409            (gnus-summary-goto-article gnus-last-article))
8410     (gnus-summary-position-cursor)))
8411
8412 (defun gnus-summary-pop-article (number)
8413   "Pop one article off the history and go to the previous.
8414 NUMBER articles will be popped off."
8415   (interactive "p")
8416   (let (to)
8417     (setq gnus-newsgroup-history
8418           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8419     (if to
8420         (gnus-summary-goto-article (car to))
8421       (error "Article history empty")))
8422   (gnus-summary-position-cursor))
8423
8424 ;; Summary article oriented commands
8425
8426 (defun gnus-summary-refer-parent-article (n)
8427   "Refer parent article N times.
8428 The difference between N and the number of articles fetched is returned."
8429   (interactive "p")
8430   (gnus-set-global-variables)
8431   (while 
8432       (and 
8433        (> n 0)
8434        (let ((ref (mail-header-references (gnus-get-header-by-num
8435                                            (gnus-summary-article-number)))))
8436          (if (and ref (not (equal ref ""))
8437                   (string-match "<[^<>]*>[ \t]*$" ref))
8438              (gnus-summary-refer-article 
8439               (substring ref (match-beginning 0) (match-end 0)))
8440            (gnus-message 1 "No references in article %d"
8441                          (gnus-summary-article-number))
8442            nil)))
8443     (setq n (1- n)))
8444   (gnus-summary-position-cursor)
8445   n)
8446     
8447 (defun gnus-summary-refer-article (message-id)
8448   "Refer article specified by MESSAGE-ID.
8449 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8450   (interactive "sMessage-ID: ")
8451   (if (or (not (stringp message-id))
8452           (zerop (length message-id)))
8453       ()
8454     ;; Construct the correct Message-ID if necessary.
8455     ;; Suggested by tale@pawl.rpi.edu.
8456     (or (string-match "^<" message-id)
8457         (setq message-id (concat "<" message-id)))
8458     (or (string-match ">$" message-id)
8459         (setq message-id (concat message-id ">")))
8460     (let ((header (car (gnus-gethash (downcase message-id)
8461                                      gnus-newsgroup-dependencies))))
8462       (if header
8463           (or (gnus-summary-goto-article (mail-header-number header))
8464               ;; The header has been read, but the article had been
8465               ;; expunged, so we insert it again.
8466               (progn
8467                 (gnus-summary-insert-line
8468                  nil header 0 nil gnus-read-mark nil nil
8469                  (mail-header-subject header))
8470                 (forward-line -1)
8471                 (mail-header-number header)))
8472         (let ((gnus-override-method gnus-refer-article-method)
8473               (gnus-ancient-mark gnus-read-mark)
8474               (tmp-point (window-start
8475                           (get-buffer-window gnus-article-buffer)))
8476               number tmp-buf)
8477           (and gnus-refer-article-method
8478                (gnus-check-server gnus-refer-article-method))
8479           ;; Save the old article buffer.
8480           (save-excursion
8481             (set-buffer (gnus-article-setup-buffer))
8482             (gnus-kill-buffer " *temp Article*")
8483             (setq tmp-buf (rename-buffer " *temp Article*")))
8484           (prog1
8485               (if (gnus-article-prepare 
8486                    message-id nil (gnus-read-header message-id))
8487                   (progn
8488                     (setq number (mail-header-number gnus-current-headers))
8489                     (gnus-rebuild-thread message-id)
8490                     (gnus-summary-goto-subject number)
8491                     (if (null gnus-use-full-window)
8492                         (progn
8493                           (delete-windows-on tmp-buf)
8494                           (gnus-configure-windows 'article 'force)))
8495                     (gnus-summary-recenter)
8496                     (gnus-article-set-window-start 
8497                      (cdr (assq number gnus-newsgroup-bookmarks)))
8498                     message-id)
8499                 ;; We restore the old article buffer.
8500                 (save-excursion
8501                   (kill-buffer gnus-article-buffer)
8502                   (set-buffer tmp-buf)
8503                   (rename-buffer gnus-article-buffer)
8504                   (let ((buffer-read-only nil))
8505                     (and tmp-point
8506                          (set-window-start (get-buffer-window (current-buffer))
8507                                            tmp-point)))))))))))
8508
8509 (defun gnus-summary-enter-digest-group ()
8510   "Enter a digest group based on the current article."
8511   (interactive)
8512   (gnus-set-global-variables)
8513   (gnus-summary-select-article)
8514   ;; We do not want a narrowed article.
8515   (gnus-summary-stop-page-breaking)
8516   (let ((name (format "%s-%d" 
8517                       (gnus-group-prefixed-name 
8518                        gnus-newsgroup-name (list 'nndoc "")) 
8519                       gnus-current-article))
8520         (ogroup gnus-newsgroup-name)
8521         (buf (current-buffer)))
8522     (if (gnus-group-read-ephemeral-group 
8523          name (list 'nndoc name
8524                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8525                     '(nndoc-article-type digest))
8526          t)
8527         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8528                 (list (list (cons 'to-group ogroup))))
8529       (switch-to-buffer buf)
8530       (gnus-set-global-variables)
8531       (gnus-configure-windows 'summary)
8532       (gnus-message 3 "Article not a digest?"))))
8533
8534 (defun gnus-summary-isearch-article ()
8535   "Do incremental search forward on current article."
8536   (interactive)
8537   (gnus-set-global-variables)
8538   (gnus-summary-select-article)
8539   (gnus-eval-in-buffer-window 
8540    gnus-article-buffer (isearch-forward)))
8541
8542 (defun gnus-summary-search-article-forward (regexp &optional backward)
8543   "Search for an article containing REGEXP forward.
8544 If BACKWARD, search backward instead."
8545   (interactive
8546    (list (read-string
8547           (format "Search article %s (regexp%s): "
8548                   (if current-prefix-arg "backward" "forward")
8549                   (if gnus-last-search-regexp
8550                       (concat ", default " gnus-last-search-regexp)
8551                     "")))
8552          current-prefix-arg))
8553   (gnus-set-global-variables)
8554   (if (string-equal regexp "")
8555       (setq regexp (or gnus-last-search-regexp ""))
8556     (setq gnus-last-search-regexp regexp))
8557   (if (gnus-summary-search-article regexp backward)
8558       (gnus-article-set-window-start 
8559        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8560     (error "Search failed: \"%s\"" regexp)))
8561
8562 (defun gnus-summary-search-article-backward (regexp)
8563   "Search for an article containing REGEXP backward."
8564   (interactive
8565    (list (read-string
8566           (format "Search article backward (regexp%s): "
8567                   (if gnus-last-search-regexp
8568                       (concat ", default " gnus-last-search-regexp)
8569                     "")))))
8570   (gnus-summary-search-article-forward regexp 'backward))
8571
8572 (defun gnus-summary-search-article (regexp &optional backward)
8573   "Search for an article containing REGEXP.
8574 Optional argument BACKWARD means do search for backward.
8575 gnus-select-article-hook is not called during the search."
8576   (let ((gnus-select-article-hook nil)  ;Disable hook.
8577         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8578         (re-search
8579          (if backward
8580              (function re-search-backward) (function re-search-forward)))
8581         (found nil)
8582         (last nil))
8583     ;; Hidden thread subtrees must be searched for ,too.
8584     (gnus-summary-show-all-threads)
8585     (if (eobp) (forward-line -1))
8586     ;; First of all, search current article.
8587     ;; We don't want to read article again from NNTP server nor reset
8588     ;; current point.
8589     (gnus-summary-select-article)
8590     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8591     (setq last gnus-current-article)
8592     (gnus-eval-in-buffer-window
8593      gnus-article-buffer
8594      (save-restriction
8595        (widen)
8596        ;; Begin search from current point.
8597        (setq found (funcall re-search regexp nil t))))
8598     ;; Then search next articles.
8599     (while (and (not found)
8600                 (gnus-summary-display-article 
8601                  (gnus-summary-search-subject backward nil nil)))
8602       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8603       (gnus-eval-in-buffer-window
8604        gnus-article-buffer
8605        (save-restriction
8606          (widen)
8607          (goto-char (if backward (point-max) (point-min)))
8608          (setq found (funcall re-search regexp nil t)))))
8609     (message "")
8610     ;; Adjust article pointer.
8611     (or (eq last gnus-current-article)
8612         (setq gnus-last-article last))
8613     ;; Return T if found such article.
8614     found))
8615
8616 (defun gnus-summary-execute-command (header regexp command &optional backward)
8617   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8618 If HEADER is an empty string (or nil), the match is done on the entire
8619 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8620   (interactive
8621    (list (let ((completion-ignore-case t))
8622            (completing-read 
8623             "Header name: "
8624             (mapcar (lambda (string) (list string))
8625                     '("Number" "Subject" "From" "Lines" "Date"
8626                       "Message-ID" "Xref" "References"))
8627             nil 'require-match))
8628          (read-string "Regexp: ")
8629          (read-key-sequence "Command: ")
8630          current-prefix-arg))
8631   (gnus-set-global-variables)
8632   ;; Hidden thread subtrees must be searched as well.
8633   (gnus-summary-show-all-threads)
8634   ;; We don't want to change current point nor window configuration.
8635   (save-excursion
8636     (save-window-excursion
8637       (gnus-message 6 "Executing %s..." (key-description command))
8638       ;; We'd like to execute COMMAND interactively so as to give arguments.
8639       (gnus-execute header regexp
8640                     (` (lambda ()
8641                          (call-interactively '(, (key-binding command)))))
8642                     backward)
8643       (gnus-message 6 "Executing %s...done" (key-description command)))))
8644
8645 (defun gnus-summary-beginning-of-article ()
8646   "Scroll the article back to the beginning."
8647   (interactive)
8648   (gnus-set-global-variables)
8649   (gnus-summary-select-article)
8650   (gnus-configure-windows 'article)
8651   (gnus-eval-in-buffer-window
8652    gnus-article-buffer
8653    (widen)
8654    (goto-char (point-min))
8655    (and gnus-break-pages (gnus-narrow-to-page))))
8656
8657 (defun gnus-summary-end-of-article ()
8658   "Scroll to the end of the article."
8659   (interactive)
8660   (gnus-set-global-variables)
8661   (gnus-summary-select-article)
8662   (gnus-configure-windows 'article)
8663   (gnus-eval-in-buffer-window 
8664    gnus-article-buffer
8665    (widen)
8666    (goto-char (point-max))
8667    (recenter -3)
8668    (and gnus-break-pages (gnus-narrow-to-page))))
8669
8670 (defun gnus-summary-show-article ()
8671   "Force re-fetching of the current article."
8672   (interactive)
8673   (gnus-set-global-variables)
8674   (gnus-summary-select-article nil 'force)
8675   (gnus-configure-windows 'article)
8676   (gnus-summary-position-cursor))
8677
8678 (defun gnus-summary-verbose-headers (&optional arg)
8679   "Toggle permanent full header display.
8680 If ARG is a positive number, turn header display on.
8681 If ARG is a negative number, turn header display off."
8682   (interactive "P")
8683   (gnus-set-global-variables)
8684   (gnus-summary-toggle-header arg)
8685   (setq gnus-show-all-headers
8686         (cond ((or (not (numberp arg))
8687                    (zerop arg))
8688                (not gnus-show-all-headers))
8689               ((natnump arg)
8690                t))))
8691
8692 (defun gnus-summary-toggle-header (&optional arg)
8693   "Show the headers if they are hidden, or hide them if they are shown.
8694 If ARG is a positive number, show the entire header.
8695 If ARG is a negative number, hide the unwanted header lines."
8696   (interactive "P")
8697   (gnus-set-global-variables)
8698   (save-excursion
8699     (set-buffer gnus-article-buffer)
8700     (let ((buffer-read-only nil))
8701       (if (numberp arg) 
8702           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8703                                                 gnus-hidden-properties)
8704             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8705         (if (text-property-any (point-min) (point-max) 'invisible t)
8706             (remove-text-properties 
8707              (point-min) (point-max) gnus-hidden-properties)
8708           ;; We hide the headers. This song and dance act below is
8709           ;; done because `gnus-have-all-headers' is buffer-local to
8710           ;; the summary buffer, and we only want to temporarily
8711           ;; change it in that buffer. Ugh.
8712           (let ((have gnus-have-all-headers))
8713             (save-excursion
8714               (set-buffer gnus-summary-buffer)
8715               (setq gnus-have-all-headers nil)
8716               (save-excursion
8717                 (set-buffer gnus-article-buffer)
8718                 (run-hooks 'gnus-article-display-hook))
8719               (setq gnus-have-all-headers have)))))
8720       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8721
8722 (defun gnus-summary-show-all-headers ()
8723   "Make all header lines visible."
8724   (interactive)
8725   (gnus-set-global-variables)
8726   (gnus-article-show-all-headers))
8727
8728 (defun gnus-summary-toggle-mime (&optional arg)
8729   "Toggle MIME processing.
8730 If ARG is a positive number, turn MIME processing on."
8731   (interactive "P")
8732   (gnus-set-global-variables)
8733   (setq gnus-show-mime
8734         (if (null arg) (not gnus-show-mime)
8735           (> (prefix-numeric-value arg) 0)))
8736   (gnus-summary-select-article t 'force))
8737
8738 (defun gnus-summary-caesar-message (&optional arg)
8739   "Caesar rotate the current article by 13.
8740 The numerical prefix specifies how manu places to rotate each letter
8741 forward."
8742   (interactive "P")
8743   (gnus-set-global-variables)
8744   (gnus-summary-select-article)
8745   (let ((mail-header-separator ""))
8746     (gnus-eval-in-buffer-window 
8747      gnus-article-buffer
8748      (save-restriction
8749        (widen)
8750        (let ((start (window-start)))
8751          (news-caesar-buffer-body arg)
8752          (set-window-start (get-buffer-window (current-buffer)) start))))))
8753
8754 (defun gnus-summary-stop-page-breaking ()
8755   "Stop page breaking in the current article."
8756   (interactive)
8757   (gnus-set-global-variables)
8758   (gnus-summary-select-article)
8759   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8760
8761 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8762
8763 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8764   "Move the current article to a different newsgroup.
8765 If N is a positive number, move the N next articles.
8766 If N is a negative number, move the N previous articles.
8767 If N is nil and any articles have been marked with the process mark,
8768 move those articles instead.
8769 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8770 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8771 re-spool using this method.
8772 For this function to work, both the current newsgroup and the
8773 newsgroup that you want to move to have to support the `request-move'
8774 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8775   (interactive "P")
8776   (gnus-set-global-variables)
8777   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8778       (error "The current newsgroup does not support article moving"))
8779   (let ((articles (gnus-summary-work-articles n))
8780         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8781         art-group to-method sel-met)
8782     (if (and (not to-newsgroup) (not select-method))
8783         (setq to-newsgroup
8784               (completing-read 
8785                (format "Where do you want to move %s? %s"
8786                        (if (> (length articles) 1)
8787                            (format "these %d articles" (length articles))
8788                          "this article")
8789                        (if gnus-current-move-group
8790                            (format "(%s default) " gnus-current-move-group)
8791                          ""))
8792                gnus-active-hashtb nil nil prefix)))
8793     (if to-newsgroup
8794         (progn
8795           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8796               (setq to-newsgroup (or gnus-current-move-group "")))
8797           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8798               (gnus-activate-group to-newsgroup)
8799               (error "No such group: %s" to-newsgroup))
8800           (setq gnus-current-move-group to-newsgroup)))
8801     (setq to-method (if select-method (list select-method "")
8802                       (gnus-find-method-for-group to-newsgroup)))
8803     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8804         (error "%s does not support article copying" (car to-method)))
8805     (or (gnus-check-server to-method)
8806         (error "Can't open server %s" (car to-method)))
8807     (gnus-message 6 "Moving to %s: %s..." 
8808                   (or select-method to-newsgroup) articles)
8809     (while articles
8810       (if (setq art-group
8811                 (gnus-request-move-article 
8812                  (car articles)         ; Article to move
8813                  gnus-newsgroup-name    ; From newsgrouo
8814                  (nth 1 (gnus-find-method-for-group 
8815                          gnus-newsgroup-name)) ; Server
8816                  (list 'gnus-request-accept-article 
8817                        (if select-method
8818                            (list 'quote select-method)
8819                          to-newsgroup)
8820                        (not (cdr articles))) ; Accept form
8821                  (not (cdr articles)))) ; Only save nov last time
8822           (let* ((buffer-read-only nil)
8823                  (entry 
8824                   (or
8825                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8826                    (gnus-gethash 
8827                     (gnus-group-prefixed-name 
8828                      (car art-group) 
8829                      (if select-method (list select-method "")
8830                        (gnus-find-method-for-group to-newsgroup)))
8831                     gnus-newsrc-hashtb)))
8832                  (info (nth 2 entry))
8833                  (article (car articles)))
8834             (gnus-summary-goto-subject article)
8835             (beginning-of-line)
8836             (delete-region (point) (progn (forward-line 1) (point)))
8837             ;; Update the group that has been moved to.
8838             (if (not info)
8839                 ()                      ; This group does not exist yet.
8840               (if (not (memq article gnus-newsgroup-unreads))
8841                   (setcar (cdr (cdr info))
8842                           (gnus-add-to-range (nth 2 info) 
8843                                              (list (cdr art-group)))))
8844               ;; Copy any marks over to the new group.
8845               (let ((marks '((tick . gnus-newsgroup-marked)
8846                              (dormant . gnus-newsgroup-dormant)
8847                              (expire . gnus-newsgroup-expirable)
8848                              (bookmark . gnus-newsgroup-bookmarks)
8849                              (reply . gnus-newsgroup-replied)))
8850                     (to-article (cdr art-group)))
8851                 (while marks
8852                   (if (memq article (symbol-value (cdr (car marks))))
8853                       (gnus-add-marked-articles 
8854                        (car info) (car (car marks)) (list to-article) info))
8855                   (setq marks (cdr marks)))))
8856             ;; Update marks.
8857             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8858             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8859             (setq gnus-newsgroup-dormant
8860                   (delq article gnus-newsgroup-dormant))
8861             (setq gnus-newsgroup-reads
8862                   (cons (cons article gnus-canceled-mark)
8863                         gnus-newsgroup-reads)))
8864         (gnus-message 1 "Couldn't move article %s" (car articles)))
8865       (gnus-summary-remove-process-mark (car articles))
8866       (setq articles (cdr articles)))))
8867
8868 (defun gnus-summary-respool-article (&optional n respool-method)
8869   "Respool the current article.
8870 The article will be squeezed through the mail spooling process again,
8871 which means that it will be put in some mail newsgroup or other
8872 depending on `nnmail-split-methods'.
8873 If N is a positive number, respool the N next articles.
8874 If N is a negative number, respool the N previous articles.
8875 If N is nil and any articles have been marked with the process mark,
8876 respool those articles instead.
8877
8878 Respooling can be done both from mail groups and \"real\" newsgroups.
8879 In the former case, the articles in question will be moved from the
8880 current group into whatever groups they are destined to.  In the
8881 latter case, they will be copied into the relevant groups."
8882   (interactive "P")
8883   (gnus-set-global-variables)
8884   (let ((respool-methods (gnus-methods-using 'respool))
8885         (methname 
8886          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8887     (or respool-method
8888         (setq respool-method
8889               (completing-read
8890                "What method do you want to use when respooling? "
8891                respool-methods nil t methname)))
8892     (or (string= respool-method "")
8893         (if (assoc (symbol-name
8894                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8895                    respool-methods)
8896             (gnus-summary-move-article n nil (intern respool-method))
8897           (gnus-summary-copy-article n nil (intern respool-method))))))
8898
8899 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8900 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8901   "Move the current article to a different newsgroup.
8902 If N is a positive number, move the N next articles.
8903 If N is a negative number, move the N previous articles.
8904 If N is nil and any articles have been marked with the process mark,
8905 move those articles instead.
8906 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8907 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8908 re-spool using this method.
8909 For this function to work, the newsgroup that you want to move to have
8910 to support the `request-move' and `request-accept'
8911 functions. (Ie. mail newsgroups at present.)"
8912   (interactive "P")
8913   (gnus-set-global-variables)
8914   (let ((articles (gnus-summary-work-articles n))
8915         (copy-buf (get-buffer-create "*copy work*"))
8916         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8917         art-group to-method)
8918     (buffer-disable-undo copy-buf)
8919     (if (and (not to-newsgroup) (not select-method))
8920         (setq to-newsgroup
8921               (completing-read 
8922                (format "Where do you want to copy %s? %s"
8923                        (if (> (length articles) 1)
8924                            (format "these %d articles" (length articles))
8925                          "this article")
8926                        (if gnus-current-move-group
8927                            (format "(%s default) " gnus-current-move-group)
8928                          ""))
8929                gnus-active-hashtb nil nil prefix)))
8930     (if to-newsgroup
8931         (progn
8932           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8933               (setq to-newsgroup (or gnus-current-move-group "")))
8934           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8935               (gnus-activate-group to-newsgroup)
8936               (error "No such group: %s" to-newsgroup))
8937           (setq gnus-current-move-group to-newsgroup)))
8938     (setq to-method (if select-method (list select-method "")
8939                       (gnus-find-method-for-group to-newsgroup)))
8940     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8941         (error "%s does not support article copying" (car to-method)))
8942     (or (gnus-check-server to-method)
8943         (error "Can't open server %s" (car to-method)))
8944     (while articles
8945       (gnus-message 6 "Copying to %s: %s..." 
8946                     (or select-method to-newsgroup) articles)
8947       (if (setq art-group
8948                 (save-excursion
8949                   (set-buffer copy-buf)
8950                   (gnus-request-article-this-buffer
8951                    (car articles) gnus-newsgroup-name)
8952                   (gnus-request-accept-article
8953                    (if select-method (quote select-method) to-newsgroup)
8954                    (not (cdr articles)))))
8955           (let* ((entry 
8956                   (or
8957                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8958                    (gnus-gethash 
8959                     (gnus-group-prefixed-name 
8960                      (car art-group) 
8961                      (if select-method (list select-method "")
8962                        (gnus-find-method-for-group to-newsgroup)))
8963                     gnus-newsrc-hashtb)))
8964                  (info (nth 2 entry))
8965                  (article (car articles)))
8966             ;; We copy the info over to the new group.
8967             (if (not info)
8968                 ()                      ; This group does not exist (yet).
8969               (if (not (memq article gnus-newsgroup-unreads))
8970                   (setcar (cdr (cdr info))
8971                           (gnus-add-to-range (nth 2 info) 
8972                                              (list (cdr art-group)))))
8973               ;; Copy any marks over to the new group.
8974               (let ((marks '((tick . gnus-newsgroup-marked)
8975                              (dormant . gnus-newsgroup-dormant)
8976                              (expire . gnus-newsgroup-expirable)
8977                              (bookmark . gnus-newsgroup-bookmarks)
8978                              (reply . gnus-newsgroup-replied)))
8979                     (to-article (cdr art-group)))
8980                 (while marks
8981                   (if (memq article (symbol-value (cdr (car marks))))
8982                       (gnus-add-marked-articles 
8983                        (car info) (car (car marks)) (list to-article) info))
8984                   (setq marks (cdr marks))))))
8985         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8986       (gnus-summary-remove-process-mark (car articles))
8987       (setq articles (cdr articles)))
8988     (kill-buffer copy-buf)))
8989
8990 (defun gnus-summary-import-article (file)
8991   "Import a random file into a mail newsgroup."
8992   (interactive "fImport file: ")
8993   (let ((group gnus-newsgroup-name)
8994         atts)
8995     (or (gnus-check-backend-function 'request-accept-article group)
8996         (error "%s does not support article importing" group))
8997     (or (file-readable-p file)
8998         (not (file-regular-p file))
8999         (error "Can't read %s" file))
9000     (save-excursion
9001       (set-buffer (get-buffer-create " *import file*"))
9002       (buffer-disable-undo (current-buffer))
9003       (erase-buffer)
9004       (insert-file-contents file)
9005       (goto-char (point-min))
9006       (if (nnheader-article-p)
9007           ()
9008         (setq atts (file-attributes file))
9009         (insert "From: " (read-string "From: ") "\n"
9010                 "Subject: " (read-string "Subject: ") "\n"
9011                 "Date: " (current-time-string (nth 5 atts)) "\n"
9012                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9013       (gnus-request-accept-article group t)
9014       (kill-buffer (current-buffer)))))
9015
9016 (defun gnus-summary-expire-articles ()
9017   "Expire all articles that are marked as expirable in the current group."
9018   (interactive)
9019   (if (not (gnus-check-backend-function 
9020             'request-expire-articles gnus-newsgroup-name))
9021       ()
9022     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
9023                                       gnus-newsrc-hashtb)))
9024            (total (memq 'total-expire (nth 5 info)))
9025            (expirable (if total
9026                           (gnus-list-of-read-articles gnus-newsgroup-name)
9027                         (setq gnus-newsgroup-expirable
9028                               (sort gnus-newsgroup-expirable '<))))
9029            es)
9030       (if (not expirable)
9031           ()
9032         (gnus-message 6 "Expiring articles...")
9033         ;; The list of articles that weren't expired is returned.
9034         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
9035         (or total (setq gnus-newsgroup-expirable es))
9036         ;; We go through the old list of expirable, and mark all
9037         ;; really expired articles as non-existant.
9038         (or (eq es expirable)           ;If nothing was expired, we don't mark.
9039             (let ((gnus-use-cache nil))
9040               (while expirable
9041                 (or (memq (car expirable) es)
9042                     (gnus-summary-mark-article
9043                      (car expirable) gnus-canceled-mark))
9044                 (setq expirable (cdr expirable)))))
9045         (gnus-message 6 "Expiring articles...done")))))
9046
9047 (defun gnus-summary-expire-articles-now ()
9048   "Expunge all expirable articles in the current group.
9049 This means that *all* articles that are marked as expirable will be
9050 deleted forever, right now."
9051   (interactive)
9052   (or gnus-expert-user
9053       (gnus-y-or-n-p
9054        "Are you really, really, really sure you want to expunge? ")
9055       (error "Phew!"))
9056   (let ((nnmail-expiry-wait -1)
9057         (nnmail-expiry-wait-function nil))
9058     (gnus-summary-expire-articles)))
9059
9060 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9061 (defun gnus-summary-delete-article (&optional n)
9062   "Delete the N next (mail) articles.
9063 This command actually deletes articles. This is not a marking
9064 command. The article will disappear forever from you life, never to
9065 return. 
9066 If N is negative, delete backwards.
9067 If N is nil and articles have been marked with the process mark,
9068 delete these instead."
9069   (interactive "P")
9070   (or (gnus-check-backend-function 'request-expire-articles 
9071                                    gnus-newsgroup-name)
9072       (error "The current newsgroup does not support article deletion."))
9073   ;; Compute the list of articles to delete.
9074   (let ((articles (gnus-summary-work-articles n))
9075         not-deleted)
9076     (if (and gnus-novice-user
9077              (not (gnus-y-or-n-p 
9078                    (format "Do you really want to delete %s forever? "
9079                            (if (> (length articles) 1) "these articles"
9080                              "this article")))))
9081         ()
9082       ;; Delete the articles.
9083       (setq not-deleted (gnus-request-expire-articles 
9084                          articles gnus-newsgroup-name 'force))
9085       (while articles
9086         (gnus-summary-remove-process-mark (car articles))       
9087         ;; The backend might not have been able to delete the article
9088         ;; after all.  
9089         (or (memq (car articles) not-deleted)
9090             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9091         (setq articles (cdr articles))))
9092     (gnus-summary-position-cursor)
9093     not-deleted))
9094
9095 (defun gnus-summary-edit-article (&optional force)
9096   "Enter into a buffer and edit the current article.
9097 This will have permanent effect only in mail groups.
9098 If FORCE is non-nil, allow editing of articles even in read-only
9099 groups."
9100   (interactive "P")
9101   (or force
9102       (not (gnus-group-read-only-p))
9103       (error "The current newsgroup does not support article editing."))
9104   (gnus-summary-select-article t)
9105   (gnus-configure-windows 'article)
9106   (select-window (get-buffer-window gnus-article-buffer))
9107   (gnus-message 6 "C-c C-c to end edits")
9108   (setq buffer-read-only nil)
9109   (text-mode)
9110   (use-local-map (copy-keymap (current-local-map)))
9111   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
9112   (buffer-enable-undo)
9113   (widen)
9114   (goto-char (point-min))
9115   (search-forward "\n\n" nil t))
9116
9117 (defun gnus-summary-edit-article-done ()
9118   "Make edits to the current article permanent."
9119   (interactive)
9120   (if (gnus-group-read-only-p)
9121       (progn
9122         (gnus-summary-edit-article-postpone)
9123         (message "The current newsgroup does not support article editing.")
9124         (ding))
9125     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
9126       (erase-buffer)
9127       (insert buf)
9128       (if (not (gnus-request-replace-article 
9129                 (cdr gnus-article-current) (car gnus-article-current) 
9130                 (current-buffer)))
9131           (error "Couldn't replace article.")
9132         (gnus-article-mode)
9133         (use-local-map gnus-article-mode-map)
9134         (setq buffer-read-only t)
9135         (buffer-disable-undo (current-buffer))
9136         (gnus-configure-windows 'summary))
9137       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))))
9138
9139 (defun gnus-summary-edit-article-postpone ()
9140   "Postpone changes to the current article."
9141   (interactive)
9142   (gnus-article-mode)
9143   (use-local-map gnus-article-mode-map)
9144   (setq buffer-read-only t)
9145   (buffer-disable-undo (current-buffer))
9146   (gnus-configure-windows 'summary)
9147   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9148
9149 (defun gnus-summary-fancy-query ()
9150   "Query where the fancy respool algorithm would put this article."
9151   (interactive)
9152   (gnus-summary-select-article)
9153   (save-excursion
9154     (set-buffer gnus-article-buffer)
9155     (save-restriction
9156       (goto-char (point-min))
9157       (search-forward "\n\n")
9158       (narrow-to-region (point-min) (point))
9159       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9160
9161 ;; Summary score commands.
9162
9163 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9164
9165 (defun gnus-summary-raise-score (n)
9166   "Raise the score of the current article by N."
9167   (interactive "p")
9168   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9169
9170 (defun gnus-summary-set-score (n)
9171   "Set the score of the current article to N."
9172   (interactive "p")
9173   ;; Skip dummy header line.
9174   (save-excursion
9175     (gnus-summary-show-thread)
9176     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9177         (forward-line 1))
9178     (let ((buffer-read-only nil))
9179       ;; Set score.
9180       (gnus-summary-update-mark
9181        (if (= n (or gnus-summary-default-score 0)) ? 
9182          (if (< n (or gnus-summary-default-score 0)) 
9183              gnus-score-below-mark gnus-score-over-mark)) 'score))
9184     (let* ((article (gnus-summary-article-number))
9185            (score (assq article gnus-newsgroup-scored)))
9186       (if score (setcdr score n)
9187         (setq gnus-newsgroup-scored 
9188               (cons (cons article n) gnus-newsgroup-scored))))
9189     (gnus-summary-update-line)))
9190
9191 (defun gnus-summary-current-score ()
9192   "Return the score of the current article."
9193   (interactive)
9194   (message "%s" (gnus-summary-article-score)))
9195
9196 ;; Summary marking commands.
9197
9198 (defun gnus-summary-raise-same-subject-and-select (score)
9199   "Raise articles which has the same subject with SCORE and select the next."
9200   (interactive "p")
9201   (let ((subject (gnus-summary-subject-string)))
9202     (gnus-summary-raise-score score)
9203     (while (gnus-summary-search-subject nil nil subject)
9204       (gnus-summary-raise-score score))
9205     (gnus-summary-next-article t)))
9206
9207 (defun gnus-summary-raise-same-subject (score)
9208   "Raise articles which has the same subject with SCORE."
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-subject 1 t)))
9215
9216 (defun gnus-score-default (level)
9217   (if level (prefix-numeric-value level) 
9218     gnus-score-interactive-default-score))
9219
9220 (defun gnus-summary-raise-thread (&optional score)
9221   "Raise the score of the articles in the current thread with SCORE."
9222   (interactive "P")
9223   (setq score (gnus-score-default score))
9224   (let (e)
9225     (save-excursion
9226       (let ((level (gnus-summary-thread-level)))
9227         (gnus-summary-raise-score score)
9228         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9229                     (> (gnus-summary-thread-level) level))
9230           (gnus-summary-raise-score score))
9231         (setq e (point))))
9232     (let ((gnus-summary-check-current t))
9233       (or (zerop (gnus-summary-next-subject 1 t))
9234           (goto-char e))))
9235   (gnus-summary-recenter)
9236   (gnus-summary-position-cursor)
9237   (gnus-set-mode-line 'summary))
9238
9239 (defun gnus-summary-lower-same-subject-and-select (score)
9240   "Raise articles which has the same subject with SCORE and select the next."
9241   (interactive "p")
9242   (gnus-summary-raise-same-subject-and-select (- score)))
9243
9244 (defun gnus-summary-lower-same-subject (score)
9245   "Raise articles which has the same subject with SCORE."
9246   (interactive "p")
9247   (gnus-summary-raise-same-subject (- score)))
9248
9249 (defun gnus-summary-lower-thread (&optional score)
9250   "Lower score of articles in the current thread with SCORE."
9251   (interactive "P")
9252   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9253
9254 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9255   "Mark articles which has the same subject as read, and then select the next.
9256 If UNMARK is positive, remove any kind of mark.
9257 If UNMARK is negative, tick articles."
9258   (interactive "P")
9259   (if unmark
9260       (setq unmark (prefix-numeric-value unmark)))
9261   (let ((count
9262          (gnus-summary-mark-same-subject
9263           (gnus-summary-subject-string) unmark)))
9264     ;; Select next unread article. If auto-select-same mode, should
9265     ;; select the first unread article.
9266     (gnus-summary-next-article t (and gnus-auto-select-same
9267                                       (gnus-summary-subject-string)))
9268     (gnus-message 7 "%d article%s marked as %s"
9269                   count (if (= count 1) " is" "s are")
9270                   (if unmark "unread" "read"))))
9271
9272 (defun gnus-summary-kill-same-subject (&optional unmark)
9273   "Mark articles which has the same subject as read. 
9274 If UNMARK is positive, remove any kind of mark.
9275 If UNMARK is negative, tick articles."
9276   (interactive "P")
9277   (if unmark
9278       (setq unmark (prefix-numeric-value unmark)))
9279   (let ((count
9280          (gnus-summary-mark-same-subject
9281           (gnus-summary-subject-string) unmark)))
9282     ;; If marked as read, go to next unread subject.
9283     (if (null unmark)
9284         ;; Go to next unread subject.
9285         (gnus-summary-next-subject 1 t))
9286     (gnus-message 7 "%d articles are marked as %s"
9287                   count (if unmark "unread" "read"))))
9288
9289 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9290   "Mark articles with same SUBJECT as read, and return marked number.
9291 If optional argument UNMARK is positive, remove any kinds of marks.
9292 If optional argument UNMARK is negative, mark articles as unread instead."
9293   (let ((count 1))
9294     (save-excursion
9295       (cond 
9296        ((null unmark)                   ; Mark as read.
9297         (while (and 
9298                 (progn
9299                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9300                   (gnus-summary-show-thread) t)
9301                 (gnus-summary-search-forward nil subject))
9302           (setq count (1+ count))))
9303        ((> unmark 0)                    ; Tick.
9304         (while (and
9305                 (progn
9306                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9307                   (gnus-summary-show-thread) t)
9308                 (gnus-summary-search-forward nil subject))
9309           (setq count (1+ count))))
9310        (t                               ; Mark as unread.
9311         (while (and
9312                 (progn
9313                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9314                   (gnus-summary-show-thread) t)
9315                 (gnus-summary-search-forward nil subject))
9316           (setq count (1+ count)))))
9317       (gnus-set-mode-line 'summary)
9318       ;; Return the number of marked articles.
9319       count)))
9320
9321 (defun gnus-summary-mark-as-processable (n &optional unmark)
9322   "Set the process mark on the next N articles.
9323 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9324 the process mark instead.  The difference between N and the actual
9325 number of articles marked is returned."
9326   (interactive "p")
9327   (let ((backward (< n 0))
9328         (n (abs n)))
9329     (while (and 
9330             (> n 0)
9331             (if unmark
9332                 (gnus-summary-remove-process-mark
9333                  (gnus-summary-article-number))
9334               (gnus-summary-set-process-mark (gnus-summary-article-number)))
9335             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9336       (setq n (1- n)))
9337     (if (/= 0 n) (gnus-message 7 "No more articles"))
9338     (gnus-summary-recenter)
9339     (gnus-summary-position-cursor)
9340     n))
9341
9342 (defun gnus-summary-unmark-as-processable (n)
9343   "Remove the process mark from the next N articles.
9344 If N is negative, mark backward instead.  The difference between N and
9345 the actual number of articles marked is returned."
9346   (interactive "p")
9347   (gnus-summary-mark-as-processable n t))
9348
9349 (defun gnus-summary-unmark-all-processable ()
9350   "Remove the process mark from all articles."
9351   (interactive)
9352   (save-excursion
9353     (while gnus-newsgroup-processable
9354       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9355   (gnus-summary-position-cursor))
9356
9357 (defun gnus-summary-mark-as-expirable (n)
9358   "Mark N articles forward as expirable.
9359 If N is negative, mark backward instead. The difference between N and
9360 the actual number of articles marked is returned."
9361   (interactive "p")
9362   (gnus-summary-mark-forward n gnus-expirable-mark))
9363
9364 (defun gnus-summary-mark-article-as-replied (article)
9365   "Mark ARTICLE replied and update the summary line."
9366   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9367   (let ((buffer-read-only nil))
9368     (if (gnus-summary-goto-subject article)
9369         (progn
9370           (gnus-summary-update-mark gnus-replied-mark 'replied)
9371           t))))
9372
9373 (defun gnus-summary-set-bookmark (article)
9374   "Set a bookmark in current article."
9375   (interactive (list (gnus-summary-article-number)))
9376   (if (or (not (get-buffer gnus-article-buffer))
9377           (not gnus-current-article)
9378           (not gnus-article-current)
9379           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9380       (error "No current article selected"))
9381   ;; Remove old bookmark, if one exists.
9382   (let ((old (assq article gnus-newsgroup-bookmarks)))
9383     (if old (setq gnus-newsgroup-bookmarks 
9384                   (delq old gnus-newsgroup-bookmarks))))
9385   ;; Set the new bookmark, which is on the form 
9386   ;; (article-number . line-number-in-body).
9387   (setq gnus-newsgroup-bookmarks 
9388         (cons 
9389          (cons article 
9390                (save-excursion
9391                  (set-buffer gnus-article-buffer)
9392                  (count-lines
9393                   (min (point)
9394                        (save-excursion
9395                          (goto-char (point-min))
9396                          (search-forward "\n\n" nil t)
9397                          (point)))
9398                   (point))))
9399          gnus-newsgroup-bookmarks))
9400   (gnus-message 6 "A bookmark has been added to the current article."))
9401
9402 (defun gnus-summary-remove-bookmark (article)
9403   "Remove the bookmark from the current article."
9404   (interactive (list (gnus-summary-article-number)))
9405   ;; Remove old bookmark, if one exists.
9406   (let ((old (assq article gnus-newsgroup-bookmarks)))
9407     (if old 
9408         (progn
9409           (setq gnus-newsgroup-bookmarks 
9410                 (delq old gnus-newsgroup-bookmarks))
9411           (gnus-message 6 "Removed bookmark."))
9412       (gnus-message 6 "No bookmark in current article."))))
9413
9414 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9415 (defun gnus-summary-mark-as-dormant (n)
9416   "Mark N articles forward as dormant.
9417 If N is negative, mark backward instead.  The difference between N and
9418 the actual number of articles marked is returned."
9419   (interactive "p")
9420   (gnus-summary-mark-forward n gnus-dormant-mark))
9421
9422 (defun gnus-summary-set-process-mark (article)
9423   "Set the process mark on ARTICLE and update the summary line."
9424   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9425   (let ((buffer-read-only nil))
9426     (if (gnus-summary-goto-subject article)
9427         (progn
9428           (gnus-summary-show-thread)
9429           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9430                (forward-line 1))
9431           (gnus-summary-update-mark gnus-process-mark 'replied)
9432           t))))
9433
9434 (defun gnus-summary-remove-process-mark (article)
9435   "Remove the process mark from ARTICLE and update the summary line."
9436   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9437   (let ((buffer-read-only nil))
9438     (if (gnus-summary-goto-subject article)
9439         (progn
9440           (gnus-summary-show-thread)
9441           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9442                (forward-line 1))
9443           (gnus-summary-update-mark ?  'replied)
9444           (if (memq article gnus-newsgroup-replied) 
9445               (gnus-summary-update-mark gnus-replied-mark 'replied))
9446           t))))
9447
9448 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9449   "Mark N articles as read forwards.
9450 If N is negative, mark backwards instead.
9451 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9452 marked as unread. 
9453 The difference between N and the actual number of articles marked is
9454 returned."
9455   (interactive "p")
9456   (gnus-set-global-variables)
9457   (let ((backward (< n 0))
9458         (gnus-summary-goto-unread
9459          (and gnus-summary-goto-unread
9460               (not (memq mark (list gnus-unread-mark
9461                                     gnus-ticked-mark gnus-dormant-mark)))))
9462         (n (abs n))
9463         (mark (or mark gnus-del-mark)))
9464     (while (and (> n 0)
9465                 (gnus-summary-mark-article nil mark no-expire)
9466                 (zerop (gnus-summary-next-subject 
9467                         (if backward -1 1) gnus-summary-goto-unread t)))
9468       (setq n (1- n)))
9469     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9470     (gnus-summary-recenter)
9471     (gnus-summary-position-cursor)
9472     (gnus-set-mode-line 'summary)
9473     n))
9474
9475 (defun gnus-summary-mark-article-as-read (mark)
9476   "Mark the current article quickly as read with MARK."
9477   (let ((article (gnus-summary-article-number)))
9478     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9479     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9480     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9481     (setq gnus-newsgroup-reads
9482           (cons (cons article mark) gnus-newsgroup-reads))
9483     ;; Possibly remove from cache, if that is used. 
9484     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9485     (and gnus-newsgroup-auto-expire 
9486          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9487              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9488              (= mark gnus-read-mark))
9489          (progn
9490            (setq mark gnus-expirable-mark)
9491            (setq gnus-newsgroup-expirable 
9492                  (cons article gnus-newsgroup-expirable))))
9493     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9494       (forward-line 1))
9495     ;; Fix the mark.
9496     (gnus-summary-update-mark mark 'unread)
9497     t))
9498
9499 (defun gnus-summary-mark-article-as-unread (mark)
9500   "Mark the current article quickly as unread with MARK."
9501   (let ((article (gnus-summary-article-number)))
9502     (or (memq article gnus-newsgroup-unreads)
9503         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9504     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9505     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9506     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9507     (setq gnus-newsgroup-reads
9508           (delq (assq article gnus-newsgroup-reads)
9509                 gnus-newsgroup-reads))
9510     (if (= mark gnus-ticked-mark)
9511         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9512     (if (= mark gnus-dormant-mark)
9513         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9514
9515     ;; See whether the article is to be put in the cache.
9516     (and gnus-use-cache
9517          (vectorp (gnus-get-header-by-num article))
9518          (save-excursion
9519            (gnus-cache-possibly-enter-article 
9520             gnus-newsgroup-name article 
9521             (gnus-get-header-by-num article)
9522             (= mark gnus-ticked-mark)
9523             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9524
9525     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9526       (forward-line 1))
9527     ;; Fix the mark.
9528     (gnus-summary-update-mark mark 'unread)
9529     t))
9530
9531 (defun gnus-summary-mark-article (&optional article mark no-expire)
9532   "Mark ARTICLE with MARK.  MARK can be any character.
9533 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9534 (dormant) and `?E' (expirable).
9535 If MARK is nil, then the default character `?D' is used.
9536 If ARTICLE is nil, then the article on the current line will be
9537 marked." 
9538   (and (stringp mark)
9539        (setq mark (aref mark 0)))
9540   ;; If no mark is given, then we check auto-expiring.
9541   (and (not no-expire)
9542        gnus-newsgroup-auto-expire 
9543        (or (not mark)
9544            (and (numberp mark) 
9545                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9546                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9547                     (= mark gnus-read-mark))))
9548        (setq mark gnus-expirable-mark))
9549   (let* ((mark (or mark gnus-del-mark))
9550          (article (or article (gnus-summary-article-number))))
9551     (or article (error "No article on current line"))
9552     (if (or (= mark gnus-unread-mark) 
9553             (= mark gnus-ticked-mark) 
9554             (= mark gnus-dormant-mark))
9555         (gnus-mark-article-as-unread article mark)
9556       (gnus-mark-article-as-read article mark))
9557
9558     ;; See whether the article is to be put in the cache.
9559     (and gnus-use-cache
9560          (not (= mark gnus-canceled-mark))
9561          (vectorp (gnus-get-header-by-num article))
9562          (save-excursion
9563            (gnus-cache-possibly-enter-article 
9564             gnus-newsgroup-name article 
9565             (gnus-get-header-by-num article)
9566             (= mark gnus-ticked-mark)
9567             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9568
9569     (if (gnus-summary-goto-subject article)
9570         (let ((buffer-read-only nil))
9571           (gnus-summary-show-thread)
9572           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9573                (forward-line 1))
9574           ;; Fix the mark.
9575           (gnus-summary-update-mark mark 'unread)
9576           t))))
9577
9578 (defun gnus-summary-update-mark (mark type)
9579   (beginning-of-line)
9580   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9581         (buffer-read-only nil)
9582         plist)
9583     (if (not forward)
9584         ()
9585       (forward-char forward)
9586       (setq plist (text-properties-at (point)))
9587       (delete-char 1)
9588       (insert mark)
9589       (and plist (add-text-properties (1- (point)) (point) plist))
9590       (and (eq type 'unread)
9591            (progn
9592              (add-text-properties (1- (point)) (point) (list 'gnus-mark mark))
9593              (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9594   
9595 (defun gnus-mark-article-as-read (article &optional mark)
9596   "Enter ARTICLE in the pertinent lists and remove it from others."
9597   ;; Make the article expirable.
9598   (let ((mark (or mark gnus-del-mark)))
9599     (if (= mark gnus-expirable-mark)
9600         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9601       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9602     ;; Remove from unread and marked lists.
9603     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9604     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9605     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9606     (setq gnus-newsgroup-reads 
9607           (cons (cons article mark) gnus-newsgroup-reads))
9608     ;; Possibly remove from cache, if that is used. 
9609     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9610
9611 (defun gnus-mark-article-as-unread (article &optional mark)
9612   "Enter ARTICLE in the pertinent lists and remove it from others."
9613   (let ((mark (or mark gnus-ticked-mark)))
9614     ;; Add to unread list.
9615     (or (memq article gnus-newsgroup-unreads)
9616         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9617     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9618     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9619     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9620     (setq gnus-newsgroup-reads
9621           (delq (assq article gnus-newsgroup-reads)
9622                 gnus-newsgroup-reads))
9623     (if (= mark gnus-ticked-mark)
9624         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9625     (if (= mark gnus-dormant-mark)
9626         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9627
9628 (defalias 'gnus-summary-mark-as-unread-forward 
9629   'gnus-summary-tick-article-forward)
9630 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9631                'gnus-summary-tick-article-forward)
9632 (defun gnus-summary-tick-article-forward (n)
9633   "Tick N articles forwards.
9634 If N is negative, tick backwards instead.
9635 The difference between N and the number of articles ticked is returned."
9636   (interactive "p")
9637   (gnus-summary-mark-forward n gnus-ticked-mark))
9638
9639 (defalias 'gnus-summary-mark-as-unread-backward 
9640   'gnus-summary-tick-article-backward)
9641 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9642                'gnus-summary-tick-article-backward)
9643 (defun gnus-summary-tick-article-backward (n)
9644   "Tick N articles backwards.
9645 The difference between N and the number of articles ticked is returned."
9646   (interactive "p")
9647   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9648
9649 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9650 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9651 (defun gnus-summary-tick-article (&optional article clear-mark)
9652   "Mark current article as unread.
9653 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9654 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9655   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9656                                        gnus-ticked-mark)))
9657
9658 (defun gnus-summary-mark-as-read-forward (n)
9659   "Mark N articles as read forwards.
9660 If N is negative, mark backwards instead.
9661 The difference between N and the actual number of articles marked is
9662 returned."
9663   (interactive "p")
9664   (gnus-summary-mark-forward n gnus-del-mark t))
9665
9666 (defun gnus-summary-mark-as-read-backward (n)
9667   "Mark the N articles as read backwards.
9668 The difference between N and the actual number of articles marked is
9669 returned."
9670   (interactive "p")
9671   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9672
9673 (defun gnus-summary-mark-as-read (&optional article mark)
9674   "Mark current article as read.
9675 ARTICLE specifies the article to be marked as read.
9676 MARK specifies a string to be inserted at the beginning of the line."
9677   (gnus-summary-mark-article article mark))
9678
9679 (defun gnus-summary-clear-mark-forward (n)
9680   "Clear marks from N articles forward.
9681 If N is negative, clear backward instead.
9682 The difference between N and the number of marks cleared is returned."
9683   (interactive "p")
9684   (gnus-summary-mark-forward n gnus-unread-mark))
9685
9686 (defun gnus-summary-clear-mark-backward (n)
9687   "Clear marks from N articles backward.
9688 The difference between N and the number of marks cleared is returned."
9689   (interactive "p")
9690   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9691
9692 (defun gnus-summary-mark-unread-as-read ()
9693   "Intended to be used by `gnus-summary-mark-article-hook'."
9694   (or (memq gnus-current-article gnus-newsgroup-marked)
9695       (memq gnus-current-article gnus-newsgroup-dormant)
9696       (memq gnus-current-article gnus-newsgroup-expirable)
9697       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9698
9699 (defun gnus-summary-mark-region-as-read (point mark all)
9700   "Mark all unread articles between point and mark as read.
9701 If given a prefix, mark all articles between point and mark as read,
9702 even ticked and dormant ones."
9703   (interactive "r\nP")
9704   (save-excursion
9705     (goto-char point)
9706     (beginning-of-line)
9707     (while (and 
9708             (< (point) mark)
9709             (progn
9710               (and
9711                (or all
9712                    (and
9713                     (not (memq (gnus-summary-article-number)
9714                                gnus-newsgroup-marked))
9715                     (not (memq (gnus-summary-article-number)
9716                                gnus-newsgroup-dormant))))
9717                (gnus-summary-mark-article
9718                 (gnus-summary-article-number) gnus-del-mark))
9719               t)
9720             (zerop (forward-line 1))))))
9721
9722 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9723 (defalias 'gnus-summary-delete-marked-as-read 
9724   'gnus-summary-remove-lines-marked-as-read)
9725 (make-obsolete 'gnus-summary-delete-marked-as-read 
9726                'gnus-summary-remove-lines-marked-as-read)
9727 (defun gnus-summary-remove-lines-marked-as-read ()
9728   "Remove lines that are marked as read."
9729   (interactive)
9730   (gnus-summary-remove-lines-marked-with 
9731    (concat (mapconcat
9732             (lambda (char) (char-to-string (symbol-value char)))
9733             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9734                             gnus-killed-mark gnus-kill-file-mark
9735                             gnus-low-score-mark gnus-expirable-mark
9736                             gnus-canceled-mark gnus-catchup-mark)
9737             ""))))
9738
9739 (defalias 'gnus-summary-delete-marked-with 
9740   'gnus-summary-remove-lines-marked-with)
9741 (make-obsolete 'gnus-summary-delete-marked-with 
9742                'gnus-summary-remove-lines-marked-with)
9743 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9744 (defun gnus-summary-remove-lines-marked-with (marks)
9745   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9746   (interactive "sMarks: ")
9747   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9748   (gnus-set-global-variables)
9749   (let ((buffer-read-only nil)
9750         (orig-article (gnus-summary-article-number))
9751         (marks (concat "^[" marks "]")))
9752     (goto-char (point-min))
9753     (if gnus-newsgroup-adaptive
9754         (gnus-score-remove-lines-adaptive marks)
9755       (while (re-search-forward marks nil t)
9756         (gnus-delete-line)))
9757     ;; If we use dummy roots, we have to do an additional sweep over
9758     ;; the buffer.
9759     (if (not (eq gnus-summary-make-false-root 'dummy))
9760         ()
9761       (goto-char (point-min))
9762       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9763       (while (re-search-forward marks nil t)
9764         (if (gnus-subject-equal
9765              (gnus-summary-subject-string)
9766              (progn
9767                (forward-line 1)
9768                (gnus-summary-subject-string)))
9769             ()
9770           (forward-line -1)
9771           (gnus-delete-line))))
9772     (or (zerop (buffer-size))
9773         (gnus-summary-goto-subject orig-article)
9774         (if (eobp)
9775             (gnus-summary-prev-subject 1)
9776           (gnus-summary-position-cursor)))))
9777
9778 (defun gnus-summary-expunge-below (&optional score)
9779   "Remove articles with score less than SCORE."
9780   (interactive "P")
9781   (gnus-set-global-variables)
9782   (setq score (if score
9783                   (prefix-numeric-value score)
9784                 (or gnus-summary-default-score 0)))
9785   (save-excursion
9786     (set-buffer gnus-summary-buffer)
9787     (goto-char (point-min))
9788     (let ((buffer-read-only nil)
9789           beg)
9790       (while (not (eobp))
9791         (if (< (gnus-summary-article-score) score)
9792             (progn
9793               (setq beg (point))
9794               (forward-line 1)
9795               (delete-region beg (point)))
9796           (forward-line 1)))
9797       ;; Adjust point.
9798       (or (zerop (buffer-size))
9799           (if (eobp)
9800               (gnus-summary-prev-subject 1)
9801             (gnus-summary-position-cursor))))))
9802
9803 (defun gnus-summary-mark-below (score mark)
9804   "Mark articles with score less than SCORE with MARK."
9805   (interactive "P\ncMark: ")
9806   (gnus-set-global-variables)
9807   (setq score (if score
9808                   (prefix-numeric-value score)
9809                 (or gnus-summary-default-score 0)))
9810   (save-excursion
9811     (set-buffer gnus-summary-buffer)
9812     (goto-char (point-min))
9813     (while (not (eobp))
9814       (and (< (gnus-summary-article-score) score)
9815            (gnus-summary-mark-article nil mark))
9816       (forward-line 1))))
9817
9818 (defun gnus-summary-kill-below (&optional score)
9819   "Mark articles with score below SCORE as read."
9820   (interactive "P")
9821   (gnus-set-global-variables)
9822   (gnus-summary-mark-below score gnus-killed-mark))
9823
9824 (defun gnus-summary-clear-above (&optional score)
9825   "Clear all marks from articles with score above SCORE."
9826   (interactive "P")
9827   (gnus-set-global-variables)
9828   (gnus-summary-mark-above score gnus-unread-mark))
9829
9830 (defun gnus-summary-tick-above (&optional score)
9831   "Tick all articles with score above SCORE."
9832   (interactive "P")
9833   (gnus-set-global-variables)
9834   (gnus-summary-mark-above score gnus-ticked-mark))
9835
9836 (defun gnus-summary-mark-above (score mark)
9837   "Mark articles with score over SCORE with MARK."
9838   (interactive "P\ncMark: ")
9839   (gnus-set-global-variables)
9840   (setq score (if score
9841                   (prefix-numeric-value score)
9842                 (or gnus-summary-default-score 0)))
9843   (save-excursion
9844     (set-buffer gnus-summary-buffer)
9845     (goto-char (point-min))
9846     (while (not (eobp))
9847       (if (> (gnus-summary-article-score) score)
9848           (progn
9849             (gnus-summary-mark-article nil mark)
9850             (forward-line 1))
9851         (forward-line 1)))))
9852
9853 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9854 (defun gnus-summary-show-all-expunged ()
9855   "Display all the hidden articles that were expunged for low scores."
9856   (interactive)
9857   (gnus-set-global-variables)
9858   (let ((buffer-read-only nil))
9859     (let ((scored gnus-newsgroup-scored)
9860           headers h)
9861       (while scored
9862         (or (gnus-summary-goto-subject (car (car scored)))
9863             (and (setq h (gnus-get-header-by-num (car (car scored))))
9864                  (< (cdr (car scored)) gnus-summary-expunge-below)
9865                  (setq headers (cons h headers))))
9866         (setq scored (cdr scored)))
9867       (or headers (error "No expunged articles hidden."))
9868       (goto-char (point-min))
9869       (save-excursion 
9870         (gnus-summary-update-lines 
9871          (point)
9872          (progn
9873            (gnus-summary-prepare-unthreaded (nreverse headers))
9874            (point)))))
9875     (goto-char (point-min))
9876     (gnus-summary-position-cursor)))
9877
9878 (defun gnus-summary-show-all-dormant ()
9879   "Display all the hidden articles that are marked as dormant."
9880   (interactive)
9881   (gnus-set-global-variables)
9882   (let ((buffer-read-only nil))
9883     (let ((dormant gnus-newsgroup-dormant)
9884           headers h)
9885       (while dormant
9886         (or (gnus-summary-goto-subject (car dormant))
9887             (and (setq h (gnus-get-header-by-num (car dormant)))
9888                  (setq headers (cons h headers))))
9889         (setq dormant (cdr dormant)))
9890       (or headers (error "No dormant articles hidden."))
9891       (goto-char (point-min))
9892       (save-excursion 
9893         (gnus-summary-update-lines 
9894          (point)
9895          (progn
9896            (gnus-summary-prepare-unthreaded (nreverse headers))
9897            (point)))))
9898     (goto-char (point-min))
9899     (gnus-summary-position-cursor)))
9900
9901 (defun gnus-summary-hide-all-dormant ()
9902   "Hide all dormant articles."
9903   (interactive)
9904   (gnus-set-global-variables)
9905   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9906   (gnus-summary-position-cursor))
9907
9908 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9909   "Mark all articles not marked as unread in this newsgroup as read.
9910 If prefix argument ALL is non-nil, all articles are marked as read.
9911 If QUIETLY is non-nil, no questions will be asked.
9912 If TO-HERE is non-nil, it should be a point in the buffer. All
9913 articles before this point will be marked as read.
9914 The number of articles marked as read is returned."
9915   (interactive "P")
9916   (gnus-set-global-variables)
9917   (prog1
9918       (if (or quietly
9919               (not gnus-interactive-catchup) ;Without confirmation?
9920               gnus-expert-user
9921               (gnus-y-or-n-p
9922                (if all
9923                    "Mark absolutely all articles as read? "
9924                  "Mark all unread articles as read? ")))
9925           (if (and not-mark 
9926                    (not gnus-newsgroup-adaptive)
9927                    (not gnus-newsgroup-auto-expire))
9928               (progn
9929                 (and all (setq gnus-newsgroup-marked nil
9930                                gnus-newsgroup-dormant nil))
9931                 (setq gnus-newsgroup-unreads 
9932                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9933             ;; We actually mark all articles as canceled, which we
9934             ;; have to do when using auto-expiry or adaptive scoring. 
9935             (gnus-summary-show-all-threads)
9936             (if (gnus-summary-first-subject (not all))
9937                 (while (and 
9938                         (if to-here (< (point) to-here) t)
9939                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9940                         (gnus-summary-search-subject nil (not all)))))
9941             (or to-here
9942                 (setq gnus-newsgroup-unreads
9943                       (append gnus-newsgroup-marked
9944                               gnus-newsgroup-dormant)))))
9945     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9946       (if (and (not to-here) (eq 'nnvirtual (car method)))
9947           (nnvirtual-catchup-group
9948            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9949     (gnus-summary-position-cursor)))
9950
9951 (defun gnus-summary-catchup-to-here (&optional all)
9952   "Mark all unticked articles before the current one as read.
9953 If ALL is non-nil, also mark ticked and dormant articles as read."
9954   (interactive "P")
9955   (gnus-set-global-variables)
9956   (save-excursion
9957     (and (zerop (forward-line -1))
9958          (progn
9959            (end-of-line)
9960            (gnus-summary-catchup all t (point))
9961            (gnus-set-mode-line 'summary))))
9962   (gnus-summary-position-cursor))
9963
9964 (defun gnus-summary-catchup-all (&optional quietly)
9965   "Mark all articles in this newsgroup as read."
9966   (interactive "P")
9967   (gnus-set-global-variables)
9968   (gnus-summary-catchup t quietly))
9969
9970 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9971   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9972 If prefix argument ALL is non-nil, all articles are marked as read."
9973   (interactive "P")
9974   (gnus-set-global-variables)
9975   (gnus-summary-catchup all quietly nil 'fast)
9976   ;; Select next newsgroup or exit.
9977   (if (eq gnus-auto-select-next 'quietly)
9978       (gnus-summary-next-group nil)
9979     (gnus-summary-exit)))
9980
9981 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9982   "Mark all articles in this newsgroup as read, and then exit."
9983   (interactive "P")
9984   (gnus-set-global-variables)
9985   (gnus-summary-catchup-and-exit t quietly))
9986
9987 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9988 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9989   "Mark all articles in this group as read and select the next group.
9990 If given a prefix, mark all articles, unread as well as ticked, as
9991 read." 
9992   (interactive "P")
9993   (gnus-set-global-variables)
9994   (gnus-summary-catchup all)
9995   (gnus-summary-next-group))
9996
9997 ;; Thread-based commands.
9998
9999 (defun gnus-summary-toggle-threads (&optional arg)
10000   "Toggle showing conversation threads.
10001 If ARG is positive number, turn showing conversation threads on."
10002   (interactive "P")
10003   (gnus-set-global-variables)
10004   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10005     (setq gnus-show-threads
10006           (if (null arg) (not gnus-show-threads)
10007             (> (prefix-numeric-value arg) 0)))
10008     (gnus-summary-prepare)
10009     (gnus-summary-goto-subject current)
10010     (gnus-summary-position-cursor)))
10011
10012 (defun gnus-summary-show-all-threads ()
10013   "Show all threads."
10014   (interactive)
10015   (gnus-set-global-variables)
10016   (save-excursion
10017     (let ((buffer-read-only nil))
10018       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10019   (gnus-summary-position-cursor))
10020
10021 (defun gnus-summary-show-thread ()
10022   "Show thread subtrees.
10023 Returns nil if no thread was there to be shown."
10024   (interactive)
10025   (gnus-set-global-variables)
10026   (let ((buffer-read-only nil)
10027         (orig (prog1 (point) (gnus-summary-hide-thread)))
10028         ;; first goto end then to beg, to have point at beg after let
10029         (end (progn (end-of-line) (point)))
10030         (beg (progn (beginning-of-line) (point))))
10031     (prog1
10032         ;; Any hidden lines here?
10033         (search-forward "\r" end t)
10034       (subst-char-in-region beg end ?\^M ?\n t)
10035       (goto-char orig)
10036       (gnus-summary-position-cursor))))
10037
10038 (defun gnus-summary-hide-all-threads ()
10039   "Hide all thread subtrees."
10040   (interactive)
10041   (gnus-set-global-variables)
10042   (save-excursion
10043     (goto-char (point-min))
10044     (gnus-summary-hide-thread)
10045     (while (and (not (eobp)) (zerop (forward-line 1)))
10046       (gnus-summary-hide-thread)))
10047   (gnus-summary-position-cursor))
10048
10049 (defun gnus-summary-hide-thread ()
10050   "Hide thread subtrees.
10051 Returns nil if no threads were there to be hidden."
10052   (interactive)
10053   (gnus-set-global-variables)
10054   (let ((buffer-read-only nil)
10055         (start (point))
10056         (level (gnus-summary-thread-level))
10057         (end (point)))
10058     ;; Go forward until either the buffer ends or the subthread
10059     ;; ends. 
10060     (if (eobp)
10061         ()
10062       (while (and (zerop (forward-line 1))
10063                   (> (gnus-summary-thread-level) level))
10064         (setq end (point)))
10065       (prog1
10066           (save-excursion
10067             (goto-char end)
10068             (search-backward "\n" start t))
10069         (subst-char-in-region start end ?\n ?\^M t)
10070         (forward-line -1)
10071         (gnus-summary-position-cursor)))))
10072
10073 (defun gnus-summary-go-to-next-thread (&optional previous)
10074   "Go to the same level (or less) next thread.
10075 If PREVIOUS is non-nil, go to previous thread instead.
10076 Return the article number moved to, or nil if moving was impossible."
10077   (let ((level (gnus-summary-thread-level))
10078         (article (gnus-summary-article-number)))
10079     (if previous 
10080         (while (and (zerop (forward-line -1))
10081                     (> (gnus-summary-thread-level) level)))
10082       (while (and (save-excursion
10083                     (forward-line 1)
10084                     (not (eobp)))
10085                   (zerop (forward-line 1))
10086                   (> (gnus-summary-thread-level) level))))
10087     (gnus-summary-recenter)
10088     (gnus-summary-position-cursor)
10089     (let ((oart (gnus-summary-article-number)))
10090       (and (/= oart article) oart))))
10091
10092 (defun gnus-summary-next-thread (n)
10093   "Go to the same level next N'th thread.
10094 If N is negative, search backward instead.
10095 Returns the difference between N and the number of skips actually
10096 done."
10097   (interactive "p")
10098   (gnus-set-global-variables)
10099   (let ((backward (< n 0))
10100         (n (abs n)))
10101     (while (and (> n 0)
10102                 (gnus-summary-go-to-next-thread backward))
10103       (setq n (1- n)))
10104     (gnus-summary-position-cursor)
10105     (if (/= 0 n) (gnus-message 7 "No more threads"))
10106     n))
10107
10108 (defun gnus-summary-prev-thread (n)
10109   "Go to the same level previous N'th thread.
10110 Returns the difference between N and the number of skips actually
10111 done."
10112   (interactive "p")
10113   (gnus-set-global-variables)
10114   (gnus-summary-next-thread (- n)))
10115
10116 (defun gnus-summary-go-down-thread (&optional same)
10117   "Go down one level in the current thread.
10118 If SAME is non-nil, also move to articles of the same level."
10119   (let ((level (gnus-summary-thread-level))
10120         (start (point)))
10121     (if (and (zerop (forward-line 1))
10122              (> (gnus-summary-thread-level) level))
10123         t
10124       (goto-char start)
10125       nil)))
10126
10127 (defun gnus-summary-go-up-thread ()
10128   "Go up one level in the current thread."
10129   (let ((level (gnus-summary-thread-level))
10130         (start (point)))
10131     (while (and (zerop (forward-line -1))
10132                 (>= (gnus-summary-thread-level) level)))
10133     (if (>= (gnus-summary-thread-level) level)
10134         (progn
10135           (goto-char start)
10136           nil)
10137       t)))
10138
10139 (defun gnus-summary-down-thread (n)
10140   "Go down thread N steps.
10141 If N is negative, go up instead.
10142 Returns the difference between N and how many steps down that were
10143 taken."
10144   (interactive "p")
10145   (gnus-set-global-variables)
10146   (let ((up (< n 0))
10147         (n (abs n)))
10148     (while (and (> n 0)
10149                 (if up (gnus-summary-go-up-thread)
10150                   (gnus-summary-go-down-thread)))
10151       (setq n (1- n)))
10152     (gnus-summary-position-cursor)
10153     (if (/= 0 n) (gnus-message 7 "Can't go further"))
10154     n))
10155
10156 (defun gnus-summary-up-thread (n)
10157   "Go up thread N steps.
10158 If N is negative, go up instead.
10159 Returns the difference between N and how many steps down that were
10160 taken."
10161   (interactive "p")
10162   (gnus-set-global-variables)
10163   (gnus-summary-down-thread (- n)))
10164
10165 (defun gnus-summary-kill-thread (&optional unmark)
10166   "Mark articles under current thread as read.
10167 If the prefix argument is positive, remove any kinds of marks.
10168 If the prefix argument is negative, tick articles instead."
10169   (interactive "P")
10170   (gnus-set-global-variables)
10171   (if unmark
10172       (setq unmark (prefix-numeric-value unmark)))
10173   (let ((killing t)
10174         (level (gnus-summary-thread-level)))
10175     (save-excursion
10176       ;; Expand the thread.
10177       (gnus-summary-show-thread)
10178       (while killing
10179         ;; Mark the article...
10180         (cond ((null unmark) (gnus-summary-mark-article-as-read
10181                               gnus-killed-mark))
10182               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10183                              gnus-unread-mark))
10184               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10185         ;; ...and go forward until either the buffer ends or the subtree
10186         ;; ends. 
10187         (if (not (and (zerop (forward-line 1))
10188                       (> (gnus-summary-thread-level) level)))
10189             (setq killing nil))))
10190     ;; Hide killed subtrees.
10191     (and (null unmark)
10192          gnus-thread-hide-killed
10193          (gnus-summary-hide-thread))
10194     ;; If marked as read, go to next unread subject.
10195     (if (null unmark)
10196         ;; Go to next unread subject.
10197         (gnus-summary-next-subject 1 t)))
10198   (gnus-set-mode-line 'summary))
10199
10200 ;; Summary sorting commands
10201
10202 (defun gnus-summary-sort-by-number (&optional reverse)
10203   "Sort summary buffer by article number.
10204 Argument REVERSE means reverse order."
10205   (interactive "P")
10206   (gnus-set-global-variables)
10207   (gnus-summary-sort 
10208    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10209    ;; a function, so we wrap it.
10210    (cons (lambda () (gnus-summary-article-number))
10211          'gnus-thread-sort-by-number) reverse))
10212
10213 (defun gnus-summary-sort-by-author (&optional reverse)
10214   "Sort summary buffer by author name alphabetically.
10215 If case-fold-search is non-nil, case of letters is ignored.
10216 Argument REVERSE means reverse order."
10217   (interactive "P")
10218   (gnus-set-global-variables)
10219   (gnus-summary-sort
10220    (cons
10221     (lambda ()
10222       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10223              (extract (funcall
10224                        gnus-extract-address-components
10225                        (mail-header-from header))))
10226         (concat (or (car extract) (cdr extract))
10227                 "\r" (mail-header-subject header))))
10228     'gnus-thread-sort-by-author)
10229    reverse))
10230
10231 (defun gnus-summary-sort-by-subject (&optional reverse)
10232   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10233 If case-fold-search is non-nil, case of letters is ignored.
10234 Argument REVERSE means reverse order."
10235   (interactive "P")
10236   (gnus-set-global-variables)
10237   (gnus-summary-sort
10238    (cons
10239     (lambda ()
10240       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10241              (extract (funcall
10242                        gnus-extract-address-components
10243                        (mail-header-from header))))
10244         (concat 
10245          (downcase (gnus-simplify-subject (gnus-summary-subject-string) t))
10246          "\r" (or (car extract) (cdr extract)))))
10247     'gnus-thread-sort-by-subject)
10248    reverse))
10249
10250 (defun gnus-summary-sort-by-date (&optional reverse)
10251   "Sort summary buffer by date.
10252 Argument REVERSE means reverse order."
10253   (interactive "P")
10254   (gnus-set-global-variables)
10255   (gnus-summary-sort
10256    (cons
10257     (lambda ()
10258       (gnus-sortable-date
10259        (mail-header-date 
10260         (gnus-get-header-by-num (gnus-summary-article-number)))))
10261     'gnus-thread-sort-by-date)
10262    reverse))
10263
10264 (defun gnus-summary-sort-by-score (&optional reverse)
10265   "Sort summary buffer by score.
10266 Argument REVERSE means reverse order."
10267   (interactive "P")
10268   (gnus-set-global-variables)
10269   (gnus-summary-sort 
10270    (cons (lambda () (gnus-summary-article-score))
10271          'gnus-thread-sort-by-score)
10272    (not reverse)))
10273
10274 (defvar gnus-summary-already-sorted nil)
10275 (defun gnus-summary-sort (predicate reverse)
10276   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10277   (if gnus-summary-already-sorted
10278       ()
10279     (let (buffer-read-only)
10280       (if (not gnus-show-threads)
10281           ;; We do untreaded sorting...
10282           (progn
10283             (goto-char (point-min))
10284             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10285         ;; ... or we do threaded sorting.
10286         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10287               (gnus-summary-prepare-hook nil)
10288               (gnus-summary-already-sorted nil))
10289           ;; We do that by simply regenerating the threads.
10290           (gnus-summary-prepare)
10291           (and gnus-show-threads
10292                gnus-thread-hide-subtree
10293                (gnus-summary-hide-all-threads))
10294           ;; If in async mode, we send some info to the backend.
10295           (and gnus-newsgroup-async
10296                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10297                (gnus-request-asynchronous 
10298                 gnus-newsgroup-name
10299                 (if (and gnus-asynchronous-article-function
10300                          (fboundp gnus-asynchronous-article-function))
10301                     (funcall gnus-asynchronous-article-function
10302                              gnus-newsgroup-threads)
10303                   gnus-newsgroup-threads))))))))
10304
10305   
10306 (defun gnus-sortable-date (date)
10307   "Make sortable string by string-lessp from DATE.
10308 Timezone package is used."
10309   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10310          (year (aref date 0))
10311          (month (aref date 1))
10312          (day (aref date 2)))
10313     (timezone-make-sortable-date 
10314      year month day 
10315      (timezone-make-time-string
10316       (aref date 3) (aref date 4) (aref date 5)))))
10317
10318
10319 ;; Summary saving commands.
10320
10321 (defun gnus-summary-save-article (&optional n)
10322   "Save the current article using the default saver function.
10323 If N is a positive number, save the N next articles.
10324 If N is a negative number, save the N previous articles.
10325 If N is nil and any articles have been marked with the process mark,
10326 save those articles instead.
10327 The variable `gnus-default-article-saver' specifies the saver function."
10328   (interactive "P")
10329   (gnus-set-global-variables)
10330   (let ((articles (gnus-summary-work-articles n)))
10331     (while articles
10332       (let ((header (gnus-get-header-by-num (car articles))))
10333         (if (vectorp header)
10334             (progn
10335               (save-window-excursion
10336                 (gnus-summary-select-article t nil nil (car articles)))
10337               (or gnus-save-all-headers
10338                   (gnus-article-hide-headers t))
10339               ;; Remove any X-Gnus lines.
10340               (save-excursion
10341                 (save-restriction
10342                   (set-buffer gnus-article-buffer)
10343                   (let ((buffer-read-only nil))
10344                     (goto-char (point-min))
10345                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10346                                                   (point-max)))
10347                     (while (re-search-forward "^X-Gnus" nil t)
10348                       (beginning-of-line)
10349                       (delete-region (point)
10350                                      (progn (forward-line 1) (point))))
10351                     (widen))))
10352               (save-window-excursion
10353                 (if gnus-default-article-saver
10354                     (funcall gnus-default-article-saver)
10355                   (error "No default saver is defined."))))
10356           (if (assq 'name header)
10357               (gnus-copy-file (cdr (assq 'name header)))
10358             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10359       (gnus-summary-remove-process-mark (car articles))
10360       (setq articles (cdr articles)))
10361     (gnus-summary-position-cursor)
10362     n))
10363
10364 (defun gnus-summary-pipe-output (&optional arg)
10365   "Pipe the current article to a subprocess.
10366 If N is a positive number, pipe the N next articles.
10367 If N is a negative number, pipe the N previous articles.
10368 If N is nil and any articles have been marked with the process mark,
10369 pipe those articles instead."
10370   (interactive "P")
10371   (gnus-set-global-variables)
10372   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10373     (gnus-summary-save-article arg)))
10374
10375 (defun gnus-summary-save-article-mail (&optional arg)
10376   "Append the current article to an mail file.
10377 If N is a positive number, save the N next articles.
10378 If N is a negative number, save the N previous articles.
10379 If N is nil and any articles have been marked with the process mark,
10380 save those articles instead."
10381   (interactive "P")
10382   (gnus-set-global-variables)
10383   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10384     (gnus-summary-save-article arg)))
10385
10386 (defun gnus-summary-save-article-rmail (&optional arg)
10387   "Append the current article to an rmail file.
10388 If N is a positive number, save the N next articles.
10389 If N is a negative number, save the N previous articles.
10390 If N is nil and any articles have been marked with the process mark,
10391 save those articles instead."
10392   (interactive "P")
10393   (gnus-set-global-variables)
10394   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10395     (gnus-summary-save-article arg)))
10396
10397 (defun gnus-summary-save-article-file (&optional arg)
10398   "Append the current article to a file.
10399 If N is a positive number, save the N next articles.
10400 If N is a negative number, save the N previous articles.
10401 If N is nil and any articles have been marked with the process mark,
10402 save those articles instead."
10403   (interactive "P")
10404   (gnus-set-global-variables)
10405   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10406     (gnus-summary-save-article arg)))
10407
10408 (defun gnus-read-save-file-name (prompt default-name)
10409   (let ((methods gnus-split-methods)
10410         split-name)
10411     (if (not gnus-split-methods)
10412         ()
10413       (save-excursion
10414         (set-buffer gnus-article-buffer)
10415         (gnus-narrow-to-headers)
10416         (while methods
10417           (goto-char (point-min))
10418           (and (condition-case () 
10419                    (re-search-forward (car (car methods)) nil t)
10420                  (error nil))
10421                (setq split-name (cons (nth 1 (car methods)) split-name)))
10422           (setq methods (cdr methods)))
10423         (widen)))
10424     (cond ((null split-name)
10425            (read-file-name
10426             (concat prompt " (default "
10427                     (file-name-nondirectory default-name) ") ")
10428             (file-name-directory default-name)
10429             default-name))
10430           ((= 1 (length split-name))
10431            (read-file-name
10432             (concat prompt " (default " (car split-name) ") ")
10433             gnus-article-save-directory
10434             (concat gnus-article-save-directory (car split-name))))
10435           (t
10436            (setq split-name (mapcar (lambda (el) (list el))
10437                                     (nreverse split-name)))
10438            (let ((result (completing-read 
10439                           (concat prompt " ")
10440                           split-name nil nil)))
10441              (concat gnus-article-save-directory
10442                      (if (string= result "")
10443                          (car (car split-name))
10444                        result)))))))
10445
10446 (defun gnus-summary-save-in-rmail (&optional filename)
10447   "Append this article to Rmail file.
10448 Optional argument FILENAME specifies file name.
10449 Directory to save to is default to `gnus-article-save-directory' which
10450 is initialized from the SAVEDIR environment variable."
10451   (interactive)
10452   (gnus-set-global-variables)
10453   (let ((default-name
10454           (funcall gnus-rmail-save-name gnus-newsgroup-name
10455                    gnus-current-headers gnus-newsgroup-last-rmail)))
10456     (or filename
10457         (setq filename (gnus-read-save-file-name 
10458                         "Save in rmail file:" default-name)))
10459     (gnus-make-directory (file-name-directory filename))
10460     (gnus-eval-in-buffer-window 
10461      gnus-article-buffer
10462      (save-excursion
10463        (save-restriction
10464          (widen)
10465          (gnus-output-to-rmail filename))))
10466     ;; Remember the directory name to save articles
10467     (setq gnus-newsgroup-last-rmail filename)))
10468
10469 (defun gnus-summary-save-in-mail (&optional filename)
10470   "Append this article to Unix mail file.
10471 Optional argument FILENAME specifies file name.
10472 Directory to save to is default to `gnus-article-save-directory' which
10473 is initialized from the SAVEDIR environment variable."
10474   (interactive)
10475   (gnus-set-global-variables)
10476   (let ((default-name
10477           (funcall gnus-mail-save-name gnus-newsgroup-name
10478                    gnus-current-headers gnus-newsgroup-last-mail)))
10479     (or filename
10480         (setq filename (gnus-read-save-file-name 
10481                         "Save in Unix mail file:" default-name)))
10482     (setq filename
10483           (expand-file-name filename
10484                             (and default-name
10485                                  (file-name-directory default-name))))
10486     (gnus-make-directory (file-name-directory filename))
10487     (gnus-eval-in-buffer-window 
10488      gnus-article-buffer
10489      (save-excursion
10490        (save-restriction
10491          (widen)
10492          (if (and (file-readable-p filename) (rmail-file-p filename))
10493              (gnus-output-to-rmail filename)
10494            (rmail-output filename 1 t t)))))
10495     ;; Remember the directory name to save articles.
10496     (setq gnus-newsgroup-last-mail filename)))
10497
10498 (defun gnus-summary-save-in-file (&optional filename)
10499   "Append this article to file.
10500 Optional argument FILENAME specifies file name.
10501 Directory to save to is default to `gnus-article-save-directory' which
10502 is initialized from the SAVEDIR environment variable."
10503   (interactive)
10504   (gnus-set-global-variables)
10505   (let ((default-name
10506           (funcall gnus-file-save-name gnus-newsgroup-name
10507                    gnus-current-headers gnus-newsgroup-last-file)))
10508     (or filename
10509         (setq filename (gnus-read-save-file-name 
10510                         "Save in file:" default-name)))
10511     (gnus-make-directory (file-name-directory filename))
10512     (gnus-eval-in-buffer-window 
10513      gnus-article-buffer
10514      (save-excursion
10515        (save-restriction
10516          (widen)
10517          (gnus-output-to-file filename))))
10518     ;; Remember the directory name to save articles.
10519     (setq gnus-newsgroup-last-file filename)))
10520
10521 (defun gnus-summary-save-in-pipe (&optional command)
10522   "Pipe this article to subprocess."
10523   (interactive)
10524   (gnus-set-global-variables)
10525   (let ((command (read-string "Shell command on article: "
10526                               gnus-last-shell-command)))
10527     (if (string-equal command "")
10528         (setq command gnus-last-shell-command))
10529     (gnus-eval-in-buffer-window 
10530      gnus-article-buffer
10531      (save-restriction
10532        (widen)
10533        (shell-command-on-region (point-min) (point-max) command nil)))
10534     (setq gnus-last-shell-command command)))
10535
10536 ;; Summary extract commands
10537
10538 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10539   (let ((buffer-read-only nil)
10540         (article (gnus-summary-article-number))
10541         b)
10542     (or (gnus-summary-goto-subject article)
10543         (error (format "No such article: %d" article)))
10544     (gnus-summary-position-cursor)
10545     ;; If all commands are to be bunched up on one line, we collect
10546     ;; them here.  
10547     (if gnus-view-pseudos-separately
10548         ()
10549       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10550             files action)
10551         (while ps
10552           (setq action (cdr (assq 'action (car ps))))
10553           (setq files (list (cdr (assq 'name (car ps)))))
10554           (while (and ps (cdr ps)
10555                       (string= (or action "1")
10556                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10557             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10558             (setcdr ps (cdr (cdr ps))))
10559           (if (not files)
10560               ()
10561             (if (not (string-match "%s" action))
10562                 (setq files (cons " " files)))
10563             (setq files (cons " " files))
10564             (and (assq 'execute (car ps))
10565                  (setcdr (assq 'execute (car ps))
10566                          (funcall (if (string-match "%s" action)
10567                                       'format 'concat)
10568                                   action 
10569                                   (mapconcat (lambda (f) f) files " ")))))
10570           (setq ps (cdr ps)))))
10571     (if (and gnus-view-pseudos (not not-view))
10572         (while pslist
10573           (and (assq 'execute (car pslist))
10574                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10575                                      (eq gnus-view-pseudos 'not-confirm)))
10576           (setq pslist (cdr pslist)))
10577       (save-excursion
10578         (while pslist
10579           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10580                                          (gnus-summary-article-number)))
10581           (forward-line 1)
10582           (setq b (point))
10583           (insert "          " (file-name-nondirectory 
10584                                 (cdr (assq 'name (car pslist))))
10585                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10586           (add-text-properties 
10587            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10588                           'gnus-mark gnus-unread-mark 
10589                           'gnus-level 0
10590                           'gnus-pseudo (car pslist)))
10591           (forward-line -1)
10592           (gnus-sethash (int-to-string gnus-reffed-article-number)
10593                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10594           (setq gnus-newsgroup-unreads
10595                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10596           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10597           (setq pslist (cdr pslist)))))))
10598
10599 (defun gnus-pseudos< (p1 p2)
10600   (let ((c1 (cdr (assq 'action p1)))
10601         (c2 (cdr (assq 'action p2))))
10602     (and c1 c2 (string< c1 c2))))
10603
10604 (defun gnus-request-pseudo-article (props)
10605   (cond ((assq 'execute props)
10606          (gnus-execute-command (cdr (assq 'execute props)))))
10607   (let ((gnus-current-article (gnus-summary-article-number)))
10608     (run-hooks 'gnus-mark-article-hook)))
10609
10610 (defun gnus-execute-command (command &optional automatic)
10611   (save-excursion
10612     (gnus-article-setup-buffer)
10613     (set-buffer gnus-article-buffer)
10614     (let ((command (if automatic command (read-string "Command: " command)))
10615           (buffer-read-only nil))
10616       (erase-buffer)
10617       (insert "$ " command "\n\n")
10618       (if gnus-view-pseudo-asynchronously
10619           (start-process "gnus-execute" nil "sh" "-c" command)
10620         (call-process "sh" nil t nil "-c" command)))))
10621
10622 (defun gnus-copy-file (file &optional to)
10623   "Copy FILE to TO."
10624   (interactive
10625    (list (read-file-name "Copy file: " default-directory)
10626          (read-file-name "Copy file to: " default-directory)))
10627   (gnus-set-global-variables)
10628   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10629   (and (file-directory-p to) 
10630        (setq to (concat (file-name-as-directory to)
10631                         (file-name-nondirectory file))))
10632   (copy-file file to))
10633
10634 ;; Summary kill commands.
10635
10636 (defun gnus-summary-edit-global-kill (article)
10637   "Edit the \"global\" kill file."
10638   (interactive (list (gnus-summary-article-number)))
10639   (gnus-set-global-variables)
10640   (gnus-group-edit-global-kill article))
10641
10642 (defun gnus-summary-edit-local-kill ()
10643   "Edit a local kill file applied to the current newsgroup."
10644   (interactive)
10645   (gnus-set-global-variables)
10646   (setq gnus-current-headers 
10647         (gnus-gethash 
10648          (int-to-string (gnus-summary-article-number))
10649          gnus-newsgroup-headers-hashtb-by-number))
10650   (gnus-set-global-variables)
10651   (gnus-group-edit-local-kill 
10652    (gnus-summary-article-number) gnus-newsgroup-name))
10653
10654 \f
10655 ;;;
10656 ;;; Gnus article mode
10657 ;;;
10658
10659 (put 'gnus-article-mode 'mode-class 'special)
10660
10661 (defvar gnus-boogaboo nil)
10662
10663 (if gnus-article-mode-map
10664     nil
10665   (setq gnus-article-mode-map (make-keymap))
10666   (suppress-keymap gnus-article-mode-map)
10667   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10668   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10669   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10670   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10671   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10672   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10673   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10674   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10675   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10676   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10677   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10678   
10679   ;; Duplicate almost all summary keystrokes in the article mode map.
10680   (let ((commands 
10681          (list 
10682           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10683           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10684           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10685           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10686           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10687           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10688           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10689           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10690           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10691           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10692           "\C-c\C-v\C-v" "\C-d" "v" 
10693 ;;        "Mt" "M!" "Md" "Mr"
10694 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10695 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10696 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10697 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10698 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10699 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10700 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10701 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10702 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10703 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10704 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10705           )))
10706     (while (and gnus-boogaboo commands) ; disabled
10707       (define-key gnus-article-mode-map (car commands) 
10708         'gnus-article-summary-command)
10709       (setq commands (cdr commands))))
10710
10711   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10712 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10713                          "=" "n"  "^" "\M-^")))
10714     (while (and gnus-boogaboo commands) ; disabled
10715       (define-key gnus-article-mode-map (car commands) 
10716         'gnus-article-summary-command-nosave)
10717       (setq commands (cdr commands)))))
10718
10719
10720 (defun gnus-article-mode ()
10721   "Major mode for displaying an article.
10722
10723 All normal editing commands are switched off.
10724
10725 The following commands are available:
10726
10727 \\<gnus-article-mode-map>
10728 \\[gnus-article-next-page]\t Scroll the article one page forwards
10729 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10730 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10731 \\[gnus-article-show-summary]\t Display the summary buffer
10732 \\[gnus-article-mail]\t Send a reply to the address near point
10733 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10734 \\[gnus-info-find-node]\t Go to the Gnus info node"
10735   (interactive)
10736   (if gnus-visual (gnus-article-make-menu-bar))
10737   (kill-all-local-variables)
10738   (setq mode-line-modified "-- ")
10739   (make-local-variable 'mode-line-format)
10740   (setq mode-line-format (copy-sequence mode-line-format))
10741   (and (equal (nth 3 mode-line-format) "   ")
10742        (setcar (nthcdr 3 mode-line-format) ""))
10743   (setq mode-name "Article")
10744   (setq major-mode 'gnus-article-mode)
10745   (make-local-variable 'minor-mode-alist)
10746   (or (assq 'gnus-show-mime minor-mode-alist)
10747       (setq minor-mode-alist
10748             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10749   (use-local-map gnus-article-mode-map)
10750   (make-local-variable 'page-delimiter)
10751   (setq page-delimiter gnus-page-delimiter)
10752   (buffer-disable-undo (current-buffer))
10753   (setq buffer-read-only t)             ;Disable modification
10754   (run-hooks 'gnus-article-mode-hook))
10755
10756 (defun gnus-article-setup-buffer ()
10757   "Initialize article mode buffer."
10758   ;; Returns the article buffer.
10759   (if (get-buffer gnus-article-buffer)
10760       (save-excursion
10761         (set-buffer gnus-article-buffer)
10762         (buffer-disable-undo (current-buffer))
10763         (setq buffer-read-only t)
10764         (gnus-add-current-to-buffer-list)
10765         (or (eq major-mode 'gnus-article-mode)
10766             (gnus-article-mode))
10767         (current-buffer))
10768     (save-excursion
10769       (set-buffer (get-buffer-create gnus-article-buffer))
10770       (gnus-add-current-to-buffer-list)
10771       (gnus-article-mode)
10772       (current-buffer))))
10773
10774 ;; Set article window start at LINE, where LINE is the number of lines
10775 ;; from the head of the article.
10776 (defun gnus-article-set-window-start (&optional line)
10777   (set-window-start 
10778    (get-buffer-window gnus-article-buffer)
10779    (save-excursion
10780      (set-buffer gnus-article-buffer)
10781      (goto-char (point-min))
10782      (if (not line)
10783          (point-min)
10784        (gnus-message 6 "Moved to bookmark")
10785        (search-forward "\n\n" nil t)
10786        (forward-line line)
10787        (point)))))
10788
10789 (defun gnus-request-article-this-buffer (article group)
10790   "Get an article and insert it into this buffer."
10791   (setq group (or group gnus-newsgroup-name))
10792
10793   ;; Open server if it has closed.
10794   (gnus-check-server (gnus-find-method-for-group group))
10795
10796   ;; Using `gnus-request-article' directly will insert the article into
10797   ;; `nntp-server-buffer' - so we'll save some time by not having to
10798   ;; copy it from the server buffer into the article buffer.
10799
10800   ;; We only request an article by message-id when we do not have the
10801   ;; headers for it, so we'll have to get those.
10802   (and (stringp article) 
10803        (let ((gnus-override-method gnus-refer-article-method))
10804          (gnus-read-header article)))
10805
10806   ;; If the article number is negative, that means that this article
10807   ;; doesn't belong in this newsgroup (possibly), so we find its
10808   ;; message-id and request it by id instead of number.
10809   (if (not (numberp article))
10810       ()
10811     (save-excursion
10812       (set-buffer gnus-summary-buffer)
10813       (let ((header (gnus-get-header-by-num article)))
10814         (if (< article 0)
10815             (if (vectorp header)
10816                 ;; It's a real article.
10817                 (setq article (mail-header-id header))
10818               ;; It is an extracted pseudo-article.
10819               (setq article 'pseudo)
10820               (gnus-request-pseudo-article header)))
10821
10822         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10823           (if (not (eq (car method) 'nneething))
10824               ()
10825             (let ((dir (concat (file-name-as-directory (nth 1 method))
10826                                (mail-header-subject header))))
10827               (if (file-directory-p dir)
10828                   (progn
10829                     (setq article 'nneething)
10830                     (gnus-group-enter-directory dir)))))))))
10831
10832   ;; Check the cache.
10833   (if (and gnus-use-cache
10834            (numberp article)
10835            (gnus-cache-request-article article group))
10836       'article
10837     ;; Get the article and into the article buffer.
10838     (if (or (stringp article) (numberp article))
10839         (progn
10840           (erase-buffer)
10841           (let ((gnus-override-method 
10842                  (and (stringp article) gnus-refer-article-method)))
10843             (and (gnus-request-article article group (current-buffer))
10844                  'article)))
10845       article)))
10846
10847 (defun gnus-read-header (id)
10848   "Read the headers of article ID and enter them into the Gnus system."
10849   (let (header)
10850     (if (not (setq header 
10851                    (car (if (let ((gnus-nov-is-evil t))
10852                               (gnus-retrieve-headers 
10853                                (list id) gnus-newsgroup-name))
10854                             (gnus-get-newsgroup-headers)))))
10855         nil
10856       (if (stringp id)
10857           (mail-header-set-number header gnus-reffed-article-number))
10858       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10859       (gnus-sethash (int-to-string (mail-header-number header)) header
10860                     gnus-newsgroup-headers-hashtb-by-number)
10861       (if (stringp id)
10862           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10863       (setq gnus-current-headers header)
10864       header)))
10865
10866 (defun gnus-article-prepare (article &optional all-headers header)
10867   "Prepare ARTICLE in article mode buffer.
10868 ARTICLE should either be an article number or a Message-ID.
10869 If ARTICLE is an id, HEADER should be the article headers.
10870 If ALL-HEADERS is non-nil, no headers are hidden."
10871   (save-excursion
10872     ;; Make sure we start in a summary buffer.
10873     (or (eq major-mode 'gnus-summary-mode)
10874         (set-buffer gnus-summary-buffer))
10875     (setq gnus-summary-buffer (current-buffer))
10876     ;; Make sure the connection to the server is alive.
10877     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10878         (progn
10879           (gnus-check-server 
10880            (gnus-find-method-for-group gnus-newsgroup-name))
10881           (gnus-request-group gnus-newsgroup-name t)))
10882     (let* ((article (if header (mail-header-number header) article))
10883            (summary-buffer (current-buffer))
10884            (internal-hook gnus-article-internal-prepare-hook)
10885            (group gnus-newsgroup-name)
10886            result)
10887       (save-excursion
10888         (gnus-article-setup-buffer)
10889         (set-buffer gnus-article-buffer)
10890         (if (not (setq result (let ((buffer-read-only nil))
10891                                 (gnus-request-article-this-buffer 
10892                                  article group))))
10893             ;; There is no such article.
10894             (save-excursion
10895               (if (not (numberp article))
10896                   ()
10897                 (setq gnus-article-current 
10898                       (cons gnus-newsgroup-name article))
10899                 (set-buffer gnus-summary-buffer)
10900                 (setq gnus-current-article article)
10901                 (gnus-summary-mark-article article gnus-canceled-mark))
10902               (gnus-message 1 "No such article (may be canceled)")
10903               (ding)
10904               nil)
10905           (if (or (eq result 'pseudo) (eq result 'nneething))
10906               (progn
10907                 (save-excursion
10908                   (set-buffer summary-buffer)
10909                   (setq gnus-last-article gnus-current-article
10910                         gnus-newsgroup-history (cons gnus-current-article
10911                                                      gnus-newsgroup-history)
10912                         gnus-current-article 0
10913                         gnus-current-headers nil
10914                         gnus-article-current nil)
10915                   (if (eq result 'nneething)
10916                       (gnus-configure-windows 'summary)
10917                     (gnus-configure-windows 'article))
10918                   (gnus-set-global-variables))
10919                 (gnus-set-mode-line 'article))
10920             ;; The result from the `request' was an actual article -
10921             ;; or at least some text that is now displayed in the
10922             ;; article buffer.
10923             (if (and (numberp article)
10924                      (not (eq article gnus-current-article)))
10925                 ;; Seems like a new article has been selected.
10926                 ;; `gnus-current-article' must be an article number.
10927                 (save-excursion
10928                   (set-buffer summary-buffer)
10929                   (setq gnus-last-article gnus-current-article
10930                         gnus-newsgroup-history (cons gnus-current-article
10931                                                      gnus-newsgroup-history)
10932                         gnus-current-article article
10933                         gnus-current-headers 
10934                         (gnus-get-header-by-num gnus-current-article)
10935                         gnus-article-current 
10936                         (cons gnus-newsgroup-name gnus-current-article))
10937                   (gnus-summary-show-thread)
10938                   (run-hooks 'gnus-mark-article-hook)
10939                   (gnus-set-mode-line 'summary)
10940                   (and gnus-visual 
10941                        (run-hooks 'gnus-visual-mark-article-hook))
10942                   ;; Set the global newsgroup variables here.
10943                   ;; Suggested by Jim Sisolak
10944                   ;; <sisolak@trans4.neep.wisc.edu>.
10945                   (gnus-set-global-variables)
10946                   (setq gnus-have-all-headers 
10947                         (or all-headers gnus-show-all-headers))
10948                   (and gnus-use-cache 
10949                        (vectorp (gnus-get-header-by-number article))
10950                        (gnus-cache-possibly-enter-article
10951                         group article
10952                         (gnus-get-header-by-number article)
10953                         (memq article gnus-newsgroup-marked)
10954                         (memq article gnus-newsgroup-dormant)
10955                         (memq article gnus-newsgroup-unreads)))))
10956             ;; Hooks for getting information from the article.
10957             ;; This hook must be called before being narrowed.
10958             (let (buffer-read-only)
10959               (run-hooks 'internal-hook)
10960               (run-hooks 'gnus-article-prepare-hook)
10961               ;; Decode MIME message.
10962               (if (and gnus-show-mime
10963                        (or (not gnus-strict-mime)
10964                            (gnus-fetch-field "Mime-Version")))
10965                   (funcall gnus-show-mime-method))
10966               ;; Perform the article display hooks.
10967               (run-hooks 'gnus-article-display-hook))
10968             ;; Do page break.
10969             (goto-char (point-min))
10970             (and gnus-break-pages (gnus-narrow-to-page))
10971             (gnus-set-mode-line 'article)
10972             (gnus-configure-windows 'article)
10973             (goto-char (point-min))
10974             t))))))
10975
10976 (defun gnus-article-show-all-headers ()
10977   "Show all article headers in article mode buffer."
10978   (save-excursion 
10979     (gnus-article-setup-buffer)
10980     (set-buffer gnus-article-buffer)
10981     (let ((buffer-read-only nil))
10982       (remove-text-properties (point-min) (point-max) 
10983                               gnus-hidden-properties))))
10984
10985 (defun gnus-article-hide-headers-if-wanted ()
10986   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10987 Provided for backwards compatability."
10988   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10989       (gnus-article-hide-headers)))
10990
10991 (defun gnus-article-hide-headers (&optional delete)
10992   "Hide unwanted headers and possibly sort them as well."
10993   (interactive "P")
10994   (save-excursion
10995     (set-buffer gnus-article-buffer)
10996     (save-restriction
10997       (let ((sorted gnus-sorted-header-list)
10998             (buffer-read-only nil)
10999             want-list beg want-l)
11000         ;; First we narrow to just the headers.
11001         (widen)
11002         (goto-char (point-min))
11003         ;; Hide any "From " lines at the beginning of (mail) articles. 
11004         (while (looking-at "From ")
11005           (forward-line 1))
11006         (or (bobp) 
11007             (add-text-properties (point-min) (point) gnus-hidden-properties))
11008         ;; Then treat the rest of the header lines.
11009         (narrow-to-region 
11010          (point) 
11011          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
11012         ;; Then we use the two regular expressions
11013         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
11014         ;; select which header lines is to remain visible in the
11015         ;; article buffer.
11016         (goto-char (point-min))
11017         (while (re-search-forward "^[^ \t]*:" nil t)
11018           (beginning-of-line)
11019           ;; We add the headers we want to keep to a list and delete
11020           ;; them from the buffer.
11021           (if (or (and (stringp gnus-visible-headers)
11022                        (looking-at gnus-visible-headers))
11023                   (and (not (stringp gnus-visible-headers))
11024                        (stringp gnus-ignored-headers)
11025                        (not (looking-at gnus-ignored-headers))))
11026               (progn
11027                 (setq beg (point))
11028                 (forward-line 1)
11029                 ;; Be sure to get multi-line headers...
11030                 (re-search-forward "^[^ \t]*:" nil t)
11031                 (beginning-of-line)
11032                 (setq want-list 
11033                       (cons (buffer-substring beg (point)) want-list))
11034                 (delete-region beg (point))
11035                 (goto-char beg))
11036             (forward-line 1)))
11037         ;; Next we perform the sorting by looking at
11038         ;; `gnus-sorted-header-list'. 
11039         (goto-char (point-min))
11040         (while (and sorted want-list)
11041           (setq want-l want-list)
11042           (while (and want-l
11043                       (not (string-match (car sorted) (car want-l))))
11044             (setq want-l (cdr want-l)))
11045           (if want-l 
11046               (progn
11047                 (insert (car want-l))
11048                 (setq want-list (delq (car want-l) want-list))))
11049           (setq sorted (cdr sorted)))
11050         ;; Any headers that were not matched by the sorted list we
11051         ;; just tack on the end of the visible header list.
11052         (while want-list
11053           (insert (car want-list))
11054           (setq want-list (cdr want-list)))
11055         ;; And finally we make the unwanted headers invisible.
11056         (if delete
11057             (delete-region (point) (point-max))
11058           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
11059           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
11060
11061 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
11062 (defun gnus-article-treat-overstrike ()
11063   "Translate overstrikes into bold text."
11064   (interactive)
11065   (save-excursion
11066     (set-buffer gnus-article-buffer)
11067     (let ((buffer-read-only nil))
11068       (while (search-forward "\b" nil t)
11069         (let ((next (following-char))
11070               (previous (char-after (- (point) 2))))
11071           (cond ((eq next previous)
11072                  (put-text-property (- (point) 2) (point)
11073                                     'invisible t)
11074                  (put-text-property (point) (1+ (point))
11075                                     'face 'bold))
11076                 ((eq next ?_)
11077                  (put-text-property (1- (point)) (1+ (point))
11078                                     'invisible t)
11079                  (put-text-property (1- (point)) (point)
11080                                     'face 'underline))
11081                 ((eq previous ?_)
11082                  (put-text-property (- (point) 2) (point)
11083                                     'invisible t)
11084                  (put-text-property (point) (1+ (point))
11085                                     'face 'underline))))))))
11086
11087 (defun gnus-article-word-wrap ()
11088   "Format too long lines."
11089   (interactive)
11090   (save-excursion
11091     (set-buffer gnus-article-buffer)
11092     (let ((buffer-read-only nil))
11093       (goto-char (point-min))
11094       (search-forward "\n\n" nil t)
11095       (end-of-line 1)
11096       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
11097             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
11098             (adaptive-fill-mode t))
11099         (while (not (eobp))
11100           (and (>= (current-column) (min fill-column (window-width)))
11101                (/= (preceding-char) ?:)
11102                (fill-paragraph nil))
11103           (end-of-line 2))))))
11104
11105 (defun gnus-article-remove-cr ()
11106   "Remove carriage returns from an article."
11107   (interactive)
11108   (save-excursion
11109     (set-buffer gnus-article-buffer)
11110     (let ((buffer-read-only nil))
11111       (goto-char (point-min))
11112       (while (search-forward "\r" nil t)
11113         (replace-match "" t t)))))
11114
11115 (defun gnus-article-display-x-face (&optional force)
11116   "Look for an X-Face header and display it if present."
11117   (interactive (list 'force))
11118   (save-excursion
11119     (set-buffer gnus-article-buffer)
11120     (let ((inhibit-point-motion-hooks t)
11121           (case-fold-search nil)
11122           from)
11123       (save-restriction
11124         (goto-char (point-min))
11125         (search-forward "\n\n")
11126         (narrow-to-region (point-min) (point))
11127         (goto-char (point-min))
11128         (setq from (mail-fetch-field "from"))
11129         (if (not (and gnus-article-x-face-command
11130                       (or force
11131                           (not gnus-article-x-face-too-ugly)
11132                           (and gnus-article-x-face-too-ugly from
11133                                (not (string-match gnus-article-x-face-too-ugly
11134                                                   from))))
11135                       (progn
11136                         (goto-char (point-min))
11137                         (re-search-forward "^X-Face: " nil t))))
11138             nil
11139           (let ((beg (point))
11140                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
11141             (if (symbolp gnus-article-x-face-command)
11142                 (and (or (fboundp gnus-article-x-face-command)
11143                          (error "%s is not a function"
11144                                 gnus-article-x-face-command))
11145                      (funcall gnus-article-x-face-command beg end))
11146               (call-process-region beg end "sh" nil 0 nil
11147                                    "-c" gnus-article-x-face-command))))))))
11148
11149 (defun gnus-article-de-quoted-unreadable (&optional force)
11150   "Do a naive translation of a quoted-printable-encoded article.
11151 This is in no way, shape or form meant as a replacement for real MIME
11152 processing, but is simply a stop-gap measure until MIME support is
11153 written.
11154 If FORCE, decode the article whether it is marked as quoted-printable
11155 or not." 
11156   (interactive (list 'force))
11157   (save-excursion
11158     (set-buffer gnus-article-buffer)
11159     (let ((case-fold-search t)
11160           (buffer-read-only nil)
11161           (type (gnus-fetch-field "content-transfer-encoding")))
11162       (if (or force (and type (string-match "quoted-printable" type)))
11163           (progn
11164             (goto-char (point-min))
11165             (search-forward "\n\n" nil 'move)
11166             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11167
11168 (defun gnus-mime-decode-quoted-printable (from to)
11169   ;; Decode quoted-printable from region between FROM and TO.
11170   (save-excursion
11171     (goto-char from)
11172     (while (search-forward "=" to t)
11173       (cond ((eq (following-char) ?\n)
11174              (delete-char -1)
11175              (delete-char 1))
11176             ((looking-at "[0-9A-F][0-9A-F]")
11177              (delete-char -1)
11178              (insert (hexl-hex-string-to-integer
11179                       (buffer-substring (point) (+ 2 (point)))))
11180              (delete-char 2))
11181             ((looking-at "=")
11182              (delete-char 1))
11183             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11184
11185 (defvar gnus-article-time-units
11186   (list (cons 'year (* 365.25 24 60 60))
11187         (cons 'week (* 7 24 60 60))
11188         (cons 'day (* 24 60 60))
11189         (cons 'hour (* 60 60))
11190         (cons 'minute 60)
11191         (cons 'second 1)))
11192
11193 (defun gnus-article-date-ut (&optional type)
11194   "Convert DATE date to universal time in the current article.
11195 If TYPE is `local', convert to local time; if it is `lapsed', output
11196 how much time has lapsed since DATE."
11197   (interactive (list 'ut))
11198   (let ((date (mail-header-date (or gnus-current-headers 
11199                                     (gnus-get-header-by-number
11200                                      (gnus-summary-article-number))"")))
11201         (date-regexp "^Date: \\|^X-Sent: "))
11202     (if (or (not date)
11203             (string= date ""))
11204         ()
11205       (save-excursion
11206         (set-buffer gnus-article-buffer)
11207         (let ((buffer-read-only nil))
11208           (goto-char (point-min))
11209           (if (and (re-search-forward date-regexp nil t)
11210                    (progn 
11211                      (beginning-of-line)
11212                      (looking-at date-regexp)))
11213               (delete-region (gnus-point-at-bol)
11214                              (progn (end-of-line) (1+ (point))))
11215             (goto-char (point-min))
11216             (goto-char (- (search-forward "\n\n") 2)))
11217           (insert
11218            (cond 
11219             ((eq type 'local)
11220              (concat "Date: " (condition-case ()
11221                                   (timezone-make-date-arpa-standard date)
11222                                 (error date))
11223                      "\n"))
11224             ((eq type 'ut)
11225              (concat "Date: "
11226                      (condition-case ()
11227                          (timezone-make-date-arpa-standard date nil "UT")
11228                        (error date))
11229                      "\n"))
11230             ((eq type 'lapsed)
11231              ;; If the date is seriously mangled, the timezone
11232              ;; functions are liable to bug out, so we condition-case
11233              ;; the entire thing.  
11234              (let* ((real-sec (condition-case ()
11235                                   (- (gnus-seconds-since-epoch 
11236                                       (timezone-make-date-arpa-standard
11237                                        (current-time-string) 
11238                                        (current-time-zone) "UT"))
11239                                      (gnus-seconds-since-epoch 
11240                                       (timezone-make-date-arpa-standard 
11241                                        date nil "UT")))
11242                                 (error 0)))
11243                     (sec (abs real-sec))
11244                     num prev)
11245                (if (zerop sec)
11246                    "X-Sent: Now\n"
11247                  (concat
11248                   "X-Sent: "
11249                   (mapconcat 
11250                    (lambda (unit)
11251                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11252                          ""
11253                        (setq sec (- sec (* num (cdr unit))))
11254                        (prog1
11255                            (concat (if prev ", " "") (int-to-string 
11256                                                       (floor num))
11257                                    " " (symbol-name (car unit))
11258                                    (if (> num 1) "s" ""))
11259                          (setq prev t))))
11260                    gnus-article-time-units "")
11261                   (if (> real-sec 0)
11262                       " ago\n"
11263                     " in the future\n")))))
11264             (t
11265              (error "Unknown conversion type: %s" type)))))))))
11266
11267 (defun gnus-article-date-local ()
11268   "Convert the current article date to the local timezone."
11269   (interactive)
11270   (gnus-article-date-ut 'local))
11271
11272 (defun gnus-article-date-lapsed ()
11273   "Convert the current article date to time lapsed since it was sent."
11274   (interactive)
11275   (gnus-article-date-ut 'lapsed))
11276
11277 (defun gnus-article-maybe-highlight ()
11278   "Do some article highlighting if `gnus-visual' is non-nil."
11279   (if gnus-visual (gnus-article-highlight-some)))
11280
11281 ;; Article savers.
11282
11283 (defun gnus-output-to-rmail (file-name)
11284   "Append the current article to an Rmail file named FILE-NAME."
11285   (require 'rmail)
11286   ;; Most of these codes are borrowed from rmailout.el.
11287   (setq file-name (expand-file-name file-name))
11288   (setq rmail-default-rmail-file file-name)
11289   (let ((artbuf (current-buffer))
11290         (tmpbuf (get-buffer-create " *Gnus-output*")))
11291     (save-excursion
11292       (or (get-file-buffer file-name)
11293           (file-exists-p file-name)
11294           (if (gnus-yes-or-no-p
11295                (concat "\"" file-name "\" does not exist, create it? "))
11296               (let ((file-buffer (create-file-buffer file-name)))
11297                 (save-excursion
11298                   (set-buffer file-buffer)
11299                   (rmail-insert-rmail-file-header)
11300                   (let ((require-final-newline nil))
11301                     (write-region (point-min) (point-max) file-name t 1)))
11302                 (kill-buffer file-buffer))
11303             (error "Output file does not exist")))
11304       (set-buffer tmpbuf)
11305       (buffer-disable-undo (current-buffer))
11306       (erase-buffer)
11307       (insert-buffer-substring artbuf)
11308       (gnus-convert-article-to-rmail)
11309       ;; Decide whether to append to a file or to an Emacs buffer.
11310       (let ((outbuf (get-file-buffer file-name)))
11311         (if (not outbuf)
11312             (append-to-file (point-min) (point-max) file-name)
11313           ;; File has been visited, in buffer OUTBUF.
11314           (set-buffer outbuf)
11315           (let ((buffer-read-only nil)
11316                 (msg (and (boundp 'rmail-current-message)
11317                           (symbol-value 'rmail-current-message))))
11318             ;; If MSG is non-nil, buffer is in RMAIL mode.
11319             (if msg
11320                 (progn (widen)
11321                        (narrow-to-region (point-max) (point-max))))
11322             (insert-buffer-substring tmpbuf)
11323             (if msg
11324                 (progn
11325                   (goto-char (point-min))
11326                   (widen)
11327                   (search-backward "\^_")
11328                   (narrow-to-region (point) (point-max))
11329                   (goto-char (1+ (point-min)))
11330                   (rmail-count-new-messages t)
11331                   (rmail-show-message msg)))))))
11332     (kill-buffer tmpbuf)))
11333
11334 (defun gnus-output-to-file (file-name)
11335   "Append the current article to a file named FILE-NAME."
11336   (setq file-name (expand-file-name file-name))
11337   (let ((artbuf (current-buffer))
11338         (tmpbuf (get-buffer-create " *Gnus-output*")))
11339     (save-excursion
11340       (set-buffer tmpbuf)
11341       (buffer-disable-undo (current-buffer))
11342       (erase-buffer)
11343       (insert-buffer-substring artbuf)
11344       ;; Append newline at end of the buffer as separator, and then
11345       ;; save it to file.
11346       (goto-char (point-max))
11347       (insert "\n")
11348       (append-to-file (point-min) (point-max) file-name))
11349     (kill-buffer tmpbuf)))
11350
11351 (defun gnus-convert-article-to-rmail ()
11352   "Convert article in current buffer to Rmail message format."
11353   (let ((buffer-read-only nil))
11354     ;; Convert article directly into Babyl format.
11355     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11356     (goto-char (point-min))
11357     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11358     (while (search-forward "\n\^_" nil t) ;single char
11359       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
11360     (goto-char (point-max))
11361     (insert "\^_")))
11362
11363 (defun gnus-narrow-to-page (&optional arg)
11364   "Make text outside current page invisible except for page delimiter.
11365 A numeric arg specifies to move forward or backward by that many pages,
11366 thus showing a page other than the one point was originally in."
11367   (interactive "P")
11368   (setq arg (if arg (prefix-numeric-value arg) 0))
11369   (save-excursion
11370     (forward-page -1)                   ;Beginning of current page.
11371     (widen)
11372     (if (> arg 0)
11373         (forward-page arg)
11374       (if (< arg 0)
11375           (forward-page (1- arg))))
11376     ;; Find the end of the page.
11377     (forward-page)
11378     ;; If we stopped due to end of buffer, stay there.
11379     ;; If we stopped after a page delimiter, put end of restriction
11380     ;; at the beginning of that line.
11381     ;; These are commented out.
11382     ;;    (if (save-excursion (beginning-of-line)
11383     ;;                  (looking-at page-delimiter))
11384     ;;  (beginning-of-line))
11385     (narrow-to-region (point)
11386                       (progn
11387                         ;; Find the top of the page.
11388                         (forward-page -1)
11389                         ;; If we found beginning of buffer, stay there.
11390                         ;; If extra text follows page delimiter on same line,
11391                         ;; include it.
11392                         ;; Otherwise, show text starting with following line.
11393                         (if (and (eolp) (not (bobp)))
11394                             (forward-line 1))
11395                         (point)))))
11396
11397 (defun gnus-gmt-to-local ()
11398   "Rewrite Date header described in GMT to local in current buffer.
11399 Intended to be used with gnus-article-prepare-hook."
11400   (save-excursion
11401     (save-restriction
11402       (widen)
11403       (goto-char (point-min))
11404       (narrow-to-region (point-min)
11405                         (progn (search-forward "\n\n" nil 'move) (point)))
11406       (goto-char (point-min))
11407       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11408           (let ((buffer-read-only nil)
11409                 (date (buffer-substring-no-properties
11410                        (match-beginning 1) (match-end 1))))
11411             (delete-region (match-beginning 1) (match-end 1))
11412             (insert
11413              (timezone-make-date-arpa-standard 
11414               date nil (current-time-zone))))))))
11415
11416
11417 ;; Article mode commands
11418
11419 (defun gnus-article-next-page (&optional lines)
11420   "Show next page of current article.
11421 If end of article, return non-nil. Otherwise return nil.
11422 Argument LINES specifies lines to be scrolled up."
11423   (interactive "P")
11424   (move-to-window-line -1)
11425   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11426   (if (save-excursion
11427         (end-of-line)
11428         (and (pos-visible-in-window-p)  ;Not continuation line.
11429              (eobp)))
11430       ;; Nothing in this page.
11431       (if (or (not gnus-break-pages)
11432               (save-excursion
11433                 (save-restriction
11434                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11435           t                             ;Nothing more.
11436         (gnus-narrow-to-page 1)         ;Go to next page.
11437         nil)
11438     ;; More in this page.
11439     (condition-case ()
11440         (scroll-up lines)
11441       (end-of-buffer
11442        ;; Long lines may cause an end-of-buffer error.
11443        (goto-char (point-max))))
11444     nil))
11445
11446 (defun gnus-article-prev-page (&optional lines)
11447   "Show previous page of current article.
11448 Argument LINES specifies lines to be scrolled down."
11449   (interactive "P")
11450   (move-to-window-line 0)
11451   (if (and gnus-break-pages
11452            (bobp)
11453            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11454       (progn
11455         (gnus-narrow-to-page -1)        ;Go to previous page.
11456         (goto-char (point-max))
11457         (recenter -1))
11458     (scroll-down lines)))
11459
11460 (defun gnus-article-refer-article ()
11461   "Read article specified by message-id around point."
11462   (interactive)
11463   (search-forward ">" nil t)            ;Move point to end of "<....>".
11464   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11465       (let ((message-id
11466              (buffer-substring (match-beginning 1) (match-end 1))))
11467         (set-buffer gnus-summary-buffer)
11468         (gnus-summary-refer-article message-id))
11469     (error "No references around point")))
11470
11471 (defun gnus-article-show-summary ()
11472   "Reconfigure windows to show summary buffer."
11473   (interactive)
11474   (gnus-configure-windows 'article)
11475   (gnus-summary-goto-subject gnus-current-article))
11476
11477 (defun gnus-article-describe-briefly ()
11478   "Describe article mode commands briefly."
11479   (interactive)
11480   (gnus-message 6
11481                 (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")))
11482
11483 (defun gnus-article-summary-command ()
11484   "Execute the last keystroke in the summary buffer."
11485   (interactive)
11486   (let ((obuf (current-buffer))
11487         (owin (current-window-configuration))
11488         func)
11489     (switch-to-buffer gnus-summary-buffer 'norecord)
11490     (setq func (lookup-key (current-local-map) (this-command-keys)))
11491     (call-interactively func)
11492     (set-buffer obuf)
11493     (set-window-configuration owin)
11494     (set-window-point (get-buffer-window (current-buffer)) (point))))
11495
11496 (defun gnus-article-summary-command-nosave ()
11497   "Execute the last keystroke in the summary buffer."
11498   (interactive)
11499   (let (func)
11500     (pop-to-buffer gnus-summary-buffer 'norecord)
11501     (setq func (lookup-key (current-local-map) (this-command-keys)))
11502     (call-interactively func)))
11503
11504 \f
11505 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11506
11507 ;;;###autoload
11508 (defalias 'gnus-batch-kill 'gnus-batch-score)
11509 ;;;###autoload
11510 (defun gnus-batch-score ()
11511   "Run batched scoring.
11512 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11513 Newsgroups is a list of strings in Bnews format.  If you want to score
11514 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11515 score the alt hierarchy, you'd say \"!alt.all\"."
11516   (interactive)
11517   (let* ((yes-and-no
11518           (gnus-newsrc-parse-options
11519            (apply (function concat)
11520                   (mapcar (lambda (g) (concat g " "))
11521                           command-line-args-left))))
11522          (gnus-expert-user t)
11523          (nnmail-spool-file nil)
11524          (gnus-use-dribble-file nil)
11525          (yes (car yes-and-no))
11526          (no (cdr yes-and-no))
11527          group newsrc entry
11528          ;; Disable verbose message.
11529          gnus-novice-user gnus-large-newsgroup)
11530     ;; Eat all arguments.
11531     (setq command-line-args-left nil)
11532     ;; Start Gnus.
11533     (gnus)
11534     ;; Apply kills to specified newsgroups in command line arguments.
11535     (setq newsrc (cdr gnus-newsrc-alist))
11536     (while newsrc
11537       (setq group (car (car newsrc)))
11538       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11539       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11540                (and (car entry)
11541                     (or (eq (car entry) t)
11542                         (not (zerop (car entry)))))
11543                (if yes (string-match yes group) t)
11544                (or (null no) (not (string-match no group))))
11545           (progn
11546             (gnus-summary-read-group group nil t)
11547             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11548                  (gnus-summary-exit))))
11549       (setq newsrc (cdr newsrc)))
11550     ;; Exit Emacs.
11551     (switch-to-buffer gnus-group-buffer)
11552     (gnus-group-save-newsrc)))
11553
11554 (defun gnus-apply-kill-file ()
11555   "Apply a kill file to the current newsgroup.
11556 Returns the number of articles marked as read."
11557   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11558           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11559       (gnus-apply-kill-file-internal)
11560     0))
11561
11562 (defun gnus-kill-save-kill-buffer ()
11563   (save-excursion
11564     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11565       (if (get-file-buffer file)
11566           (progn
11567             (set-buffer (get-file-buffer file))
11568             (and (buffer-modified-p) (save-buffer))
11569             (kill-buffer (current-buffer)))))))
11570
11571 (defvar gnus-kill-file-name "KILL"
11572   "Suffix of the kill files.")
11573
11574 (defun gnus-newsgroup-kill-file (newsgroup)
11575   "Return the name of a kill file name for NEWSGROUP.
11576 If NEWSGROUP is nil, return the global kill file name instead."
11577   (cond ((or (null newsgroup)
11578              (string-equal newsgroup ""))
11579          ;; The global KILL file is placed at top of the directory.
11580          (expand-file-name gnus-kill-file-name
11581                            (or gnus-kill-files-directory "~/News")))
11582         ((gnus-use-long-file-name 'not-kill)
11583          ;; Append ".KILL" to newsgroup name.
11584          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11585                                    "." gnus-kill-file-name)
11586                            (or gnus-kill-files-directory "~/News")))
11587         (t
11588          ;; Place "KILL" under the hierarchical directory.
11589          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11590                                    "/" gnus-kill-file-name)
11591                            (or gnus-kill-files-directory "~/News")))))
11592
11593 \f
11594 ;;;
11595 ;;; Dribble file
11596 ;;;
11597
11598 (defvar gnus-dribble-ignore nil)
11599 (defvar gnus-dribble-eval-file nil)
11600
11601 (defun gnus-dribble-file-name ()
11602   (concat gnus-current-startup-file "-dribble"))
11603
11604 (defun gnus-dribble-enter (string)
11605   (if (and (not gnus-dribble-ignore)
11606            gnus-dribble-buffer
11607            (buffer-name gnus-dribble-buffer))
11608       (let ((obuf (current-buffer)))
11609         (set-buffer gnus-dribble-buffer)
11610         (insert string "\n")
11611         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11612         (set-buffer obuf))))
11613
11614 (defun gnus-dribble-read-file ()
11615   (let ((dribble-file (gnus-dribble-file-name)))
11616     (save-excursion 
11617       (set-buffer (setq gnus-dribble-buffer 
11618                         (get-buffer-create 
11619                          (file-name-nondirectory dribble-file))))
11620       (gnus-add-current-to-buffer-list)
11621       (erase-buffer)
11622       (set-visited-file-name dribble-file)
11623       (buffer-disable-undo (current-buffer))
11624       (bury-buffer (current-buffer))
11625       (set-buffer-modified-p nil)
11626       (let ((auto (make-auto-save-file-name))
11627             (gnus-dribble-ignore t))
11628         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11629             (progn
11630               (if (file-newer-than-file-p auto dribble-file)
11631                   (setq dribble-file auto))
11632               (insert-file-contents dribble-file)
11633               (if (not (zerop (buffer-size)))
11634                   (set-buffer-modified-p t))
11635               (if (gnus-y-or-n-p 
11636                    "Auto-save file exists. Do you want to read it? ")
11637                   (setq gnus-dribble-eval-file t))))))))
11638
11639 (defun gnus-dribble-eval-file ()
11640   (if (not gnus-dribble-eval-file)
11641       ()
11642     (setq gnus-dribble-eval-file nil)
11643     (save-excursion
11644       (let ((gnus-dribble-ignore t))
11645         (set-buffer gnus-dribble-buffer)
11646         (eval-buffer (current-buffer))))))
11647
11648 (defun gnus-dribble-delete-file ()
11649   (if (file-exists-p (gnus-dribble-file-name))
11650       (delete-file (gnus-dribble-file-name)))
11651   (if gnus-dribble-buffer
11652       (save-excursion
11653         (set-buffer gnus-dribble-buffer)
11654         (let ((auto (make-auto-save-file-name)))
11655           (if (file-exists-p auto)
11656               (delete-file auto))
11657           (erase-buffer)
11658           (set-buffer-modified-p nil)))))
11659
11660 (defun gnus-dribble-save ()
11661   (if (and gnus-dribble-buffer
11662            (buffer-name gnus-dribble-buffer))
11663       (save-excursion
11664         (set-buffer gnus-dribble-buffer)
11665         (save-buffer))))
11666
11667 (defun gnus-dribble-clear ()
11668   (save-excursion
11669     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11670         (progn
11671           (set-buffer gnus-dribble-buffer)
11672           (erase-buffer)
11673           (set-buffer-modified-p nil)
11674           (setq buffer-saved-size (buffer-size))))))
11675
11676 ;;;
11677 ;;; Server Communication
11678 ;;;
11679
11680 (defun gnus-start-news-server (&optional confirm)
11681   "Open a method for getting news.
11682 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11683   (let (how)
11684     (if gnus-current-select-method
11685         ;; Stream is already opened.
11686         nil
11687       ;; Open NNTP server.
11688       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11689       (if confirm
11690           (progn
11691             ;; Read server name with completion.
11692             (setq gnus-nntp-server
11693                   (completing-read "NNTP server: "
11694                                    (mapcar (lambda (server) (list server))
11695                                            (cons (list gnus-nntp-server)
11696                                                  gnus-secondary-servers))
11697                                    nil nil gnus-nntp-server))))
11698
11699       (if (and gnus-nntp-server 
11700                (stringp gnus-nntp-server)
11701                (not (string= gnus-nntp-server "")))
11702           (setq gnus-select-method
11703                 (cond ((or (string= gnus-nntp-server "")
11704                            (string= gnus-nntp-server "::"))
11705                        (list 'nnspool (system-name)))
11706                       ((string-match "^:" gnus-nntp-server)
11707                        (list 'nnmh gnus-nntp-server 
11708                              (list 'nnmh-directory 
11709                                    (file-name-as-directory
11710                                     (expand-file-name
11711                                      (concat "~/" (substring
11712                                                    gnus-nntp-server 1)))))
11713                              (list 'nnmh-get-new-mail nil)))
11714                       (t
11715                        (list 'nntp gnus-nntp-server)))))
11716
11717       (setq how (car gnus-select-method))
11718       (cond ((eq how 'nnspool)
11719              (require 'nnspool)
11720              (gnus-message 5 "Looking up local news spool..."))
11721             ((eq how 'nnmh)
11722              (require 'nnmh)
11723              (gnus-message 5 "Looking up mh spool..."))
11724             (t
11725              (require 'nntp)))
11726       (setq gnus-current-select-method gnus-select-method)
11727       (run-hooks 'gnus-open-server-hook)
11728       (or 
11729        ;; gnus-open-server-hook might have opened it
11730        (gnus-server-opened gnus-select-method)  
11731        (gnus-open-server gnus-select-method)
11732        (gnus-y-or-n-p
11733         (format
11734          "%s open error: '%s'. Continue? "
11735          (nth 1 gnus-select-method)
11736          (gnus-status-message gnus-select-method)))
11737        (progn
11738          (gnus-message 1 "Couldn't open server on %s" 
11739                        (nth 1 gnus-select-method))
11740          (ding)
11741          nil)))))
11742
11743 (defun gnus-check-server (&optional method)
11744   "If the news server is down, start it up again."
11745   (let ((method (if method method gnus-select-method)))
11746     (and (stringp method)
11747          (setq method (gnus-server-to-method method)))
11748     (if (gnus-server-opened method)
11749         ;; Stream is already opened.
11750         t
11751       ;; Open server.
11752       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11753       (run-hooks 'gnus-open-server-hook)
11754       (prog1
11755           (gnus-open-server method)
11756         (message "")))))
11757
11758 (defun gnus-nntp-message (&optional message)
11759   "Check the status of the NNTP server.
11760 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11761 is returned insted of the status string."
11762   (let ((status (gnus-status-message (gnus-find-method-for-group 
11763                                       gnus-newsgroup-name)))
11764         (message (or message "")))
11765     (if (and (stringp status) (> (length status) 0))
11766         status message)))
11767
11768 (defun gnus-get-function (method function)
11769   (and (stringp method)
11770        (setq method (gnus-server-to-method method)))
11771   (let ((func (intern (format "%s-%s" (car method) function))))
11772     (if (not (fboundp func)) 
11773         (progn
11774           (require (car method))
11775           (if (not (fboundp func)) 
11776               (error "No such function: %s" func))))
11777     func))
11778
11779 ;;; Interface functions to the backends.
11780
11781 (defun gnus-open-server (method)
11782   (funcall (gnus-get-function method 'open-server)
11783            (nth 1 method) (nthcdr 2 method)))
11784
11785 (defun gnus-close-server (method)
11786   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11787
11788 (defun gnus-request-list (method)
11789   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11790
11791 (defun gnus-request-list-newsgroups (method)
11792   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11793
11794 (defun gnus-request-newgroups (date method)
11795   (funcall (gnus-get-function method 'request-newgroups) 
11796            date (nth 1 method)))
11797
11798 (defun gnus-server-opened (method)
11799   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11800
11801 (defun gnus-status-message (method)
11802   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11803                   method)))
11804     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11805
11806 (defun gnus-request-group (group &optional dont-check)
11807   (let ((method (gnus-find-method-for-group group)))
11808     (funcall (gnus-get-function method 'request-group) 
11809              (gnus-group-real-name group) (nth 1 method) dont-check)))
11810
11811 (defun gnus-request-asynchronous (group &optional articles)
11812   (let ((method (gnus-find-method-for-group group)))
11813     (funcall (gnus-get-function method 'request-asynchronous) 
11814              (gnus-group-real-name group) (nth 1 method) articles)))
11815
11816 (defun gnus-list-active-group (group)
11817   (let ((method (gnus-find-method-for-group group))
11818         (func 'list-active-group))
11819     (and (gnus-check-backend-function func group)
11820          (funcall (gnus-get-function method func) 
11821                   (gnus-group-real-name group) (nth 1 method)))))
11822
11823 (defun gnus-request-group-description (group)
11824   (let ((method (gnus-find-method-for-group group))
11825         (func 'request-group-description))
11826     (and (gnus-check-backend-function func group)
11827          (funcall (gnus-get-function method func) 
11828                   (gnus-group-real-name group) (nth 1 method)))))
11829
11830 (defun gnus-close-group (group)
11831   (let ((method (gnus-find-method-for-group group)))
11832     (funcall (gnus-get-function method 'close-group) 
11833              (gnus-group-real-name group) (nth 1 method))))
11834
11835 (defun gnus-retrieve-headers (articles group)
11836   (let ((method (gnus-find-method-for-group group)))
11837     (if (and gnus-use-cache (numberp (car articles)))
11838         (gnus-cache-retrieve-headers articles group)
11839       (funcall (gnus-get-function method 'retrieve-headers) 
11840                articles (gnus-group-real-name group) (nth 1 method)))))
11841
11842 (defun gnus-retrieve-groups (groups method)
11843   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11844
11845 (defun gnus-request-article (article group &optional buffer)
11846   (let ((method (gnus-find-method-for-group group)))
11847     (funcall (gnus-get-function method 'request-article) 
11848              article (gnus-group-real-name group) (nth 1 method) buffer)))
11849
11850 (defun gnus-request-head (article group)
11851   (let ((method (gnus-find-method-for-group group)))
11852     (funcall (gnus-get-function method 'request-head) 
11853              article (gnus-group-real-name group) (nth 1 method))))
11854
11855 (defun gnus-request-body (article group)
11856   (let ((method (gnus-find-method-for-group group)))
11857     (funcall (gnus-get-function method 'request-body) 
11858              article (gnus-group-real-name group) (nth 1 method))))
11859
11860 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11861 (defun gnus-request-post-buffer (post group subject header artbuf
11862                                       info follow-to respect-poster)
11863   (let* ((info (or info (and group (nth 2 (gnus-gethash 
11864                                            group gnus-newsrc-hashtb)))))
11865          (method
11866           (if (and gnus-post-method
11867                    ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11868                    (memq 'post (assoc
11869                                 (format "%s" (car (gnus-find-method-for-group
11870                                                    gnus-newsgroup-name)))
11871                                 gnus-valid-select-methods)))
11872               gnus-post-method
11873             (gnus-find-method-for-group gnus-newsgroup-name))))
11874     (or (gnus-check-server method)
11875         (error "Can't open server %s:%s" (car method) (nth 1 method)))
11876     (let ((mail-self-blind nil)
11877           (mail-archive-file-name nil))
11878       (funcall (gnus-get-function method 'request-post-buffer) 
11879                post group subject header artbuf info follow-to
11880                respect-poster))))
11881
11882 (defun gnus-request-post (method &optional force)
11883   (and (stringp method)
11884        (setq method (gnus-server-to-method method)))
11885   (and (not force) gnus-post-method
11886        (memq 'post (assoc (format "%s" (car method))
11887                           gnus-valid-select-methods))
11888        (setq method gnus-post-method))
11889   (funcall (gnus-get-function method 'request-post) 
11890            (nth 1 method)))
11891
11892 (defun gnus-request-expire-articles (articles group &optional force)
11893   (let ((method (gnus-find-method-for-group group)))
11894     (funcall (gnus-get-function method 'request-expire-articles) 
11895              articles (gnus-group-real-name group) (nth 1 method)
11896              force)))
11897
11898 (defun gnus-request-move-article 
11899   (article group server accept-function &optional last)
11900   (let ((method (gnus-find-method-for-group group)))
11901     (funcall (gnus-get-function method 'request-move-article) 
11902              article (gnus-group-real-name group) 
11903              (nth 1 method) accept-function last)))
11904
11905 (defun gnus-request-accept-article (group &optional last)
11906   (let ((func (if (symbolp group) group
11907                 (car (gnus-find-method-for-group group)))))
11908     (funcall (intern (format "%s-request-accept-article" func))
11909              (if (stringp group) (gnus-group-real-name group) group)
11910              last)))
11911
11912 (defun gnus-request-replace-article (article group buffer)
11913   (let ((func (car (gnus-find-method-for-group group))))
11914     (funcall (intern (format "%s-request-replace-article" func))
11915              article (gnus-group-real-name group) buffer)))
11916
11917 (defun gnus-request-create-group (group)
11918   (let ((method (gnus-find-method-for-group group)))
11919     (funcall (gnus-get-function method 'request-create-group) 
11920              (gnus-group-real-name group) (nth 1 method))))
11921
11922 (defun gnus-member-of-valid (symbol group)
11923   (memq symbol (assoc
11924                 (format "%s" (car (gnus-find-method-for-group group)))
11925                 gnus-valid-select-methods)))
11926
11927 (defun gnus-secondary-method-p (method)
11928   (let ((methods gnus-secondary-select-methods)
11929         (gmethod (gnus-server-get-method nil method)))
11930     (while (and methods
11931                 (not (equal (gnus-server-get-method nil (car methods)) 
11932                             gmethod)))
11933       (setq methods (cdr methods)))
11934     methods))
11935
11936 (defun gnus-find-method-for-group (group &optional info)
11937   (or gnus-override-method
11938       (and (not group)
11939            gnus-select-method)
11940       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11941             method)
11942         (if (or (not info)
11943                 (not (setq method (nth 4 info))))
11944             (setq method gnus-select-method)
11945           (setq method
11946                 (cond ((stringp method)
11947                        (gnus-server-to-method method))
11948                       ((stringp (car method))
11949                        (gnus-server-extend-method group method))
11950                       (t
11951                        method))))
11952         (gnus-server-add-address method))))
11953
11954 (defun gnus-check-backend-function (func group)
11955   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11956                   group)))
11957     (fboundp (intern (format "%s-%s" method func)))))
11958
11959 (defun gnus-methods-using (method)
11960   (let ((valids gnus-valid-select-methods)
11961         outs)
11962     (while valids
11963       (if (memq method (car valids)) 
11964           (setq outs (cons (car valids) outs)))
11965       (setq valids (cdr valids)))
11966     outs))
11967
11968 ;;; 
11969 ;;; Active & Newsrc File Handling
11970 ;;;
11971
11972 ;; Newsrc related functions.
11973 ;; Gnus internal format of gnus-newsrc-alist:
11974 ;; (("alt.general" 3 (1 . 1))
11975 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11976 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11977 ;; The first item is the group name; the second is the subscription
11978 ;; level; the third is either a range of a list of ranges of read
11979 ;; articles, the optional fourth element is a list of marked articles,
11980 ;; the optional fifth element is the select method.
11981 ;;
11982 ;; Gnus internal format of gnus-newsrc-hashtb:
11983 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11984 ;; This is the entry for "alt.misc". The first element is the number
11985 ;; of unread articles in "alt.misc". The cdr of this entry is the
11986 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11987 ;; trivial to remove or add new elements into gnus-newsrc-alist
11988 ;; without scanning the entire list. So, to get the actual information
11989 ;; of "alt.misc", you'd say something like 
11990 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11991 ;;
11992 ;; Gnus internal format of gnus-active-hashtb:
11993 ;; ((1 . 1))
11994 ;;  (5 . 10))
11995 ;;  (67 . 99)) ...)
11996 ;; The only element in each entry in this hash table is a range of
11997 ;; (possibly) available articles. (Articles in this range may have
11998 ;; been expired or canceled.)
11999 ;;
12000 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12001 ;; ("alt.misc" "alt.test" "alt.general" ...)
12002
12003 (defun gnus-setup-news (&optional rawfile level)
12004   "Setup news information.
12005 If RAWFILE is non-nil, the .newsrc file will also be read.
12006 If LEVEL is non-nil, the news will be set up at level LEVEL."
12007   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
12008     ;; Clear some variables to re-initialize news information.
12009     (if init (setq gnus-newsrc-alist nil 
12010                    gnus-active-hashtb nil))
12011
12012     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12013     (if init (gnus-read-newsrc-file rawfile))
12014
12015     ;; If we don't read the complete active file, we fill in the
12016     ;; hashtb here. 
12017     (if (or (null gnus-read-active-file)
12018             (eq gnus-read-active-file 'some))
12019         (gnus-update-active-hashtb-from-killed))
12020
12021     ;; Read the active file and create `gnus-active-hashtb'.
12022     ;; If `gnus-read-active-file' is nil, then we just create an empty
12023     ;; hash table. The partial filling out of the hash table will be
12024     ;; done in `gnus-get-unread-articles'.
12025     (and gnus-read-active-file 
12026          (not level)
12027          (gnus-read-active-file))
12028
12029     (or gnus-active-hashtb
12030         (setq gnus-active-hashtb (make-vector 4095 0)))
12031
12032     ;; Possibly eval the dribble file.
12033     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
12034
12035     (gnus-update-format-specifications)
12036
12037     ;; Find new newsgroups and treat them.
12038     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12039              (gnus-server-opened gnus-select-method))
12040         (gnus-find-new-newsgroups))
12041
12042     ;; Find the number of unread articles in each non-dead group.
12043     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12044       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12045
12046     (if (and init gnus-check-bogus-newsgroups 
12047              gnus-read-active-file (not level)
12048              (gnus-server-opened gnus-select-method))
12049         (gnus-check-bogus-newsgroups))))
12050
12051 (defun gnus-find-new-newsgroups ()
12052   "Search for new newsgroups and add them.
12053 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12054 The `-n' option line from .newsrc is respected."
12055   (interactive)
12056   (or (gnus-check-first-time-used)
12057       (if (or (consp gnus-check-new-newsgroups)
12058               (eq gnus-check-new-newsgroups 'ask-server))
12059           (gnus-ask-server-for-new-groups)
12060         (let ((groups 0)
12061               group new-newsgroups)
12062           (gnus-message 5 "Looking for new newsgroups...")
12063           (or gnus-have-read-active-file (gnus-read-active-file))
12064           (setq gnus-newsrc-last-checked-date (current-time-string))
12065           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12066           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12067           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12068           (mapatoms
12069            (lambda (sym)
12070              (if (or (null (setq group (symbol-name sym)))
12071                      (null (symbol-value sym))
12072                      (gnus-gethash group gnus-killed-hashtb)
12073                      (gnus-gethash group gnus-newsrc-hashtb))
12074                  ()
12075                (let ((do-sub (gnus-matches-options-n group)))
12076                  (cond 
12077                   ((eq do-sub 'subscribe)
12078                    (setq groups (1+ groups))
12079                    (gnus-sethash group group gnus-killed-hashtb)
12080                    (funcall gnus-subscribe-options-newsgroup-method group))
12081                   ((eq do-sub 'ignore)
12082                    nil)
12083                   (t
12084                    (setq groups (1+ groups))
12085                    (gnus-sethash group group gnus-killed-hashtb)
12086                    (if gnus-subscribe-hierarchical-interactive
12087                        (setq new-newsgroups (cons group new-newsgroups))
12088                      (funcall gnus-subscribe-newsgroup-method group)))))))
12089            gnus-active-hashtb)
12090           (if new-newsgroups 
12091               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12092           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12093           (if (> groups 0)
12094               (gnus-message 6 "%d new newsgroup%s arrived." 
12095                             groups (if (> groups 1) "s have" " has"))
12096             (gnus-message 6 "No new newsgroups."))))))
12097
12098 (defun gnus-matches-options-n (group)
12099   ;; Returns `subscribe' if the group is to be uncoditionally
12100   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12101   ;; no match for the group.
12102
12103   ;; First we check the two user variables.
12104   (cond
12105    ((and gnus-options-subscribe
12106          (string-match gnus-options-subscribe group))
12107     'subscribe)
12108    ((and gnus-options-not-subscribe
12109          (string-match gnus-options-not-subscribe group))
12110     'ignore)
12111    ;; Then we go through the list that was retrieved from the .newsrc
12112    ;; file.  This list has elements on the form 
12113    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12114    ;; is in the reverse order of the options line) is returned.
12115    (t
12116     (let ((regs gnus-newsrc-options-n))
12117       (while (and regs
12118                   (not (string-match (car (car regs)) group)))
12119         (setq regs (cdr regs)))
12120       (and regs (cdr (car regs)))))))
12121
12122 (defun gnus-ask-server-for-new-groups ()
12123   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12124          (methods (cons gnus-select-method 
12125                         (append
12126                          (and (consp gnus-check-new-newsgroups)
12127                               gnus-check-new-newsgroups)
12128                          gnus-secondary-select-methods)))
12129          (groups 0)
12130          (new-date (current-time-string))
12131          (hashtb (gnus-make-hashtable 100))
12132          group new-newsgroups got-new method)
12133     ;; Go through both primary and secondary select methods and
12134     ;; request new newsgroups.  
12135     (while methods
12136       (setq method (gnus-server-get-method nil (car methods)))
12137       (and (gnus-check-server method)
12138            (gnus-request-newgroups date method)
12139            (save-excursion
12140              (setq got-new t)
12141              (set-buffer nntp-server-buffer)
12142              ;; Enter all the new groups in a hashtable.
12143              (gnus-active-to-gnus-format method hashtb 'ignore)))
12144       (setq methods (cdr methods)))
12145     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12146     ;; Now all new groups from all select methods are in `hashtb'.
12147     (mapatoms
12148      (lambda (group-sym)
12149        (setq group (symbol-name group-sym))
12150        (if (or (null group)
12151                (null (symbol-value group-sym))
12152                (gnus-gethash group gnus-newsrc-hashtb)
12153                (member group gnus-zombie-list)
12154                (member group gnus-killed-list))
12155            ;; The group is already known.
12156            ()
12157          (and (symbol-value group-sym)
12158               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12159          (let ((do-sub (gnus-matches-options-n group)))
12160            (cond ((eq do-sub 'subscribe)
12161                   (setq groups (1+ groups))
12162                   (gnus-sethash group group gnus-killed-hashtb)
12163                   (funcall 
12164                    gnus-subscribe-options-newsgroup-method group))
12165                  ((eq do-sub 'ignore)
12166                   nil)
12167                  (t
12168                   (setq groups (1+ groups))
12169                   (gnus-sethash group group gnus-killed-hashtb)
12170                   (if gnus-subscribe-hierarchical-interactive
12171                       (setq new-newsgroups (cons group new-newsgroups))
12172                     (funcall gnus-subscribe-newsgroup-method group)))))))
12173      hashtb)
12174     (if new-newsgroups 
12175         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12176     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12177     (if (> groups 0)
12178         (gnus-message 6 "%d new newsgroup%s arrived." 
12179                       groups (if (> groups 1) "s have" " has")))
12180     got-new))
12181
12182 (defun gnus-check-first-time-used ()
12183   (if (or (> (length gnus-newsrc-alist) 1)
12184           (file-exists-p gnus-startup-file)
12185           (file-exists-p (concat gnus-startup-file ".el"))
12186           (file-exists-p (concat gnus-startup-file ".eld")))
12187       nil
12188     (gnus-message 6 "First time user; subscribing you to default groups")
12189     (or gnus-have-read-active-file (gnus-read-active-file))
12190     (setq gnus-newsrc-last-checked-date (current-time-string))
12191     (let ((groups gnus-default-subscribed-newsgroups)
12192           group)
12193       (if (eq groups t)
12194           nil
12195         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12196         (mapatoms
12197          (lambda (sym)
12198            (if (null (setq group (symbol-name sym)))
12199                ()
12200              (let ((do-sub (gnus-matches-options-n group)))
12201                (cond 
12202                 ((eq do-sub 'subscribe)
12203                  (gnus-sethash group group gnus-killed-hashtb)
12204                  (funcall gnus-subscribe-options-newsgroup-method group))
12205                 ((eq do-sub 'ignore)
12206                  nil)
12207                 (t
12208                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12209          gnus-active-hashtb)
12210         (while groups
12211           (if (gnus-gethash (car groups) gnus-active-hashtb)
12212               (gnus-group-change-level 
12213                (car groups) gnus-level-default-subscribed gnus-level-killed))
12214           (setq groups (cdr groups)))
12215         (gnus-group-make-help-group)
12216         (and gnus-novice-user
12217              (gnus-message 7 "`A k' to list killed groups"))))))
12218
12219 (defun gnus-subscribe-group (group previous &optional method)
12220   (gnus-group-change-level 
12221    (if method
12222        (list t group gnus-level-default-subscribed nil nil method)
12223      group) 
12224    gnus-level-default-subscribed gnus-level-killed previous t))
12225
12226 ;; `gnus-group-change-level' is the fundamental function for changing
12227 ;; subscription levels of newsgroups. This might mean just changing
12228 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12229 ;; again, which subscribes/unsubscribes a group, which is equally
12230 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12231 ;; from 8-9 to 1-7 means that you remove the group from the list of
12232 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12233 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12234 ;; which is trivial.
12235 ;; ENTRY can either be a string (newsgroup name) or a list (if
12236 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12237 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12238 ;; entries. 
12239 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12240 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12241 ;; after. 
12242 (defun gnus-group-change-level (entry level &optional oldlevel
12243                                       previous fromkilled)
12244   (let (group info active num)
12245     ;; Glean what info we can from the arguments
12246     (if (consp entry)
12247         (if fromkilled (setq group (nth 1 entry))
12248           (setq group (car (nth 2 entry))))
12249       (setq group entry))
12250     (if (and (stringp entry)
12251              oldlevel 
12252              (< oldlevel gnus-level-zombie))
12253         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12254     (if (and (not oldlevel)
12255              (consp entry))
12256         (setq oldlevel (car (cdr (nth 2 entry)))))
12257     (if (stringp previous)
12258         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12259
12260     (if (and (>= oldlevel gnus-level-zombie)
12261              (gnus-gethash group gnus-newsrc-hashtb))
12262         ;; We are trying to subscribe a group that is already
12263         ;; subscribed. 
12264         ()                              ; Do nothing. 
12265
12266       (gnus-dribble-enter
12267        (format "(gnus-group-change-level %S %S %S %S %S)" 
12268                group level oldlevel (car (nth 2 previous)) fromkilled))
12269     
12270       ;; Then we remove the newgroup from any old structures, if needed.
12271       ;; If the group was killed, we remove it from the killed or zombie
12272       ;; list. If not, and it is in fact going to be killed, we remove
12273       ;; it from the newsrc hash table and assoc.
12274       (cond ((>= oldlevel gnus-level-zombie)
12275              (if (= oldlevel gnus-level-zombie)
12276                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12277                (setq gnus-killed-list (delete group gnus-killed-list))))
12278             (t
12279              (if (and (>= level gnus-level-zombie)
12280                       entry)
12281                  (progn
12282                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12283                    (if (nth 3 entry)
12284                        (setcdr (gnus-gethash (car (nth 3 entry))
12285                                              gnus-newsrc-hashtb)
12286                                (cdr entry)))
12287                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12288
12289       ;; Finally we enter (if needed) the list where it is supposed to
12290       ;; go, and change the subscription level. If it is to be killed,
12291       ;; we enter it into the killed or zombie list.
12292       (cond ((>= level gnus-level-zombie)
12293              ;; Remove from the hash table.
12294              (gnus-sethash group nil gnus-newsrc-hashtb)
12295              (or (gnus-group-foreign-p group)
12296                  ;; We do not enter foreign groups into the list of dead
12297                  ;; groups.  
12298                  (if (= level gnus-level-zombie)
12299                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12300                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12301             (t
12302              ;; If the list is to be entered into the newsrc assoc, and
12303              ;; it was killed, we have to create an entry in the newsrc
12304              ;; hashtb format and fix the pointers in the newsrc assoc.
12305              (if (>= oldlevel gnus-level-zombie)
12306                  (progn
12307                    (if (listp entry)
12308                        (progn
12309                          (setq info (cdr entry))
12310                          (setq num (car entry)))
12311                      (setq active (gnus-gethash group gnus-active-hashtb))
12312                      (setq num 
12313                            (if active (- (1+ (cdr active)) (car active)) t))
12314                      ;; Check whether the group is foreign. If so, the
12315                      ;; foreign select method has to be entered into the
12316                      ;; info. 
12317                      (let ((method (gnus-group-method-name group)))
12318                        (if (eq method gnus-select-method)
12319                            (setq info (list group level nil))
12320                          (setq info (list group level nil nil method)))))
12321                    (or previous 
12322                        (setq previous 
12323                              (let ((p gnus-newsrc-alist))
12324                                (while (cdr (cdr p))
12325                                  (setq p (cdr p)))
12326                                p)))
12327                    (setq entry (cons info (cdr (cdr previous))))
12328                    (if (cdr previous)
12329                        (progn
12330                          (setcdr (cdr previous) entry)
12331                          (gnus-sethash group (cons num (cdr previous)) 
12332                                        gnus-newsrc-hashtb))
12333                      (setcdr previous entry)
12334                      (gnus-sethash group (cons num previous)
12335                                    gnus-newsrc-hashtb))
12336                    (if (cdr entry)
12337                        (setcdr (gnus-gethash (car (car (cdr entry)))
12338                                              gnus-newsrc-hashtb)
12339                                entry)))
12340                ;; It was alive, and it is going to stay alive, so we
12341                ;; just change the level and don't change any pointers or
12342                ;; hash table entries.
12343                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12344
12345 (defun gnus-kill-newsgroup (newsgroup)
12346   "Obsolete function. Kills a newsgroup."
12347   (gnus-group-change-level
12348    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12349
12350 (defun gnus-check-bogus-newsgroups (&optional confirm)
12351   "Remove bogus newsgroups.
12352 If CONFIRM is non-nil, the user has to confirm the deletion of every
12353 newsgroup." 
12354   (let ((newsrc (cdr gnus-newsrc-alist))
12355         bogus group entry)
12356     (gnus-message 5 "Checking bogus newsgroups...")
12357     (or gnus-have-read-active-file (gnus-read-active-file))
12358     ;; Find all bogus newsgroup that are subscribed.
12359     (while newsrc
12360       (setq group (car (car newsrc)))
12361       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12362               (nth 4 (car newsrc))      ; Foreign
12363               (and confirm
12364                    (not (gnus-y-or-n-p
12365                          (format "Remove bogus newsgroup: %s " group)))))
12366           ;; Don't remove.
12367           ()
12368         ;; Found a bogus newsgroup.
12369         (setq bogus (cons group bogus)))
12370       (setq newsrc (cdr newsrc)))
12371     ;; Remove all bogus subscribed groups by first killing them, and
12372     ;; then removing them from the list of killed groups.
12373     (while bogus
12374       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12375            (progn
12376              (gnus-group-change-level entry gnus-level-killed)
12377              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12378       (setq bogus (cdr bogus)))
12379     ;; Then we remove all bogus groups from the list of killed and
12380     ;; zombie groups. They are are removed without confirmation.
12381     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12382           killed)
12383       (while dead-lists
12384         (setq killed (symbol-value (car dead-lists)))
12385         (while killed
12386           (setq group (car killed))
12387           (or (gnus-gethash group gnus-active-hashtb)
12388               ;; The group is bogus.
12389               (set (car dead-lists)
12390                    (delete group (symbol-value (car dead-lists)))))
12391           (setq killed (cdr killed)))
12392         (setq dead-lists (cdr dead-lists))))
12393     (gnus-message 5 "Checking bogus newsgroups...done")))
12394
12395 (defun gnus-check-duplicate-killed-groups ()
12396   "Remove duplicates from the list of killed groups."
12397   (interactive)
12398   (let ((killed gnus-killed-list))
12399     (while killed
12400       (gnus-message 9 "%d" (length killed))
12401       (setcdr killed (delete (car killed) (cdr killed)))
12402       (setq killed (cdr killed)))))
12403
12404 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12405 ;; and compute how many unread articles there are in each group.
12406 (defun gnus-get-unread-articles (&optional level) 
12407   (let* ((newsrc (cdr gnus-newsrc-alist))
12408          (level (or level (1+ gnus-level-subscribed)))
12409          (foreign-level
12410           (min 
12411            (cond ((and gnus-activate-foreign-newsgroups 
12412                        (not (numberp gnus-activate-foreign-newsgroups)))
12413                   (1+ gnus-level-subscribed))
12414                  ((numberp gnus-activate-foreign-newsgroups)
12415                   gnus-activate-foreign-newsgroups)
12416                  (t 0))
12417            level))
12418          info group active virtuals method)
12419     (gnus-message 5 "Checking new news...")
12420
12421     (while newsrc
12422       (setq info (car newsrc)
12423             group (car info)
12424             active (gnus-gethash group gnus-active-hashtb))
12425
12426       ;; Check newsgroups. If the user doesn't want to check them, or
12427       ;; they can't be checked (for instance, if the news server can't
12428       ;; be reached) we just set the number of unread articles in this
12429       ;; newsgroup to t. This means that Gnus thinks that there are
12430       ;; unread articles, but it has no idea how many.
12431       (if (and (setq method (nth 4 info))
12432                (not (gnus-server-equal gnus-select-method
12433                                        (gnus-server-get-method nil method)))
12434                (not (gnus-secondary-method-p method)))
12435           ;; These groups are foreign. Check the level.
12436           (if (<= (nth 1 info) foreign-level)
12437               (if (eq (car (if (stringp method) 
12438                                (gnus-server-to-method method)
12439                              (nth 4 info))) 'nnvirtual)
12440                   ;; We have to activate the virtual groups after all
12441                   ;; the others, so we just pop them on a list for
12442                   ;; now. 
12443                   (setq virtuals (cons info virtuals))
12444                 (and (setq active (gnus-activate-group (car info)))
12445                      ;; Close the groups as we look at them!
12446                      (gnus-close-group group))))
12447
12448         ;; These groups are native or secondary. 
12449         (if (and (not gnus-read-active-file)
12450                  (<= (nth 1 info) level))
12451             (progn
12452               (or gnus-read-active-file (gnus-check-server method))
12453               (setq active (gnus-activate-group (car info))))))
12454       
12455       (if active
12456           (gnus-get-unread-articles-in-group info active)
12457         ;; The group couldn't be reached, so we nix out the number of
12458         ;; unread articles and stuff.
12459         (gnus-sethash group nil gnus-active-hashtb)
12460         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12461
12462       (setq newsrc (cdr newsrc)))
12463
12464     ;; Activate the virtual groups. This has to be done after all the
12465     ;; other groups. 
12466     ;; !!! If one virtual group contains another virtual group, even
12467     ;; doing it this way might cause problems.
12468     (while virtuals
12469       (and (setq active (gnus-activate-group (car (car virtuals))))
12470            (gnus-get-unread-articles-in-group (car virtuals) active))
12471       (setq virtuals (cdr virtuals)))
12472
12473     (gnus-message 5 "Checking new news...done")))
12474
12475 ;; Create a hash table out of the newsrc alist. The `car's of the
12476 ;; alist elements are used as keys.
12477 (defun gnus-make-hashtable-from-newsrc-alist ()
12478   (let ((alist gnus-newsrc-alist)
12479         (ohashtb gnus-newsrc-hashtb)
12480         prev)
12481     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12482     (setq alist 
12483           (setq prev (setq gnus-newsrc-alist 
12484                            (if (equal (car (car gnus-newsrc-alist))
12485                                       "dummy.group")
12486                                gnus-newsrc-alist
12487                              (cons (list "dummy.group" 0 nil) alist)))))
12488     (while alist
12489       (gnus-sethash (car (car alist)) 
12490                     (cons (and ohashtb (car (gnus-gethash 
12491                                              (car (car alist)) ohashtb))) 
12492                           prev) gnus-newsrc-hashtb)
12493       (setq prev alist
12494             alist (cdr alist)))))
12495
12496 (defun gnus-make-hashtable-from-killed ()
12497   "Create a hash table from the killed and zombie lists."
12498   (let ((lists '(gnus-killed-list gnus-zombie-list))
12499         list)
12500     (setq gnus-killed-hashtb 
12501           (gnus-make-hashtable 
12502            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12503     (while lists
12504       (setq list (symbol-value (car lists)))
12505       (setq lists (cdr lists))
12506       (while list
12507         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12508         (setq list (cdr list))))))
12509
12510 (defun gnus-get-unread-articles-in-group (info active)
12511   (let* ((range (nth 2 info))
12512          (num 0)
12513          (marked (nth 3 info)))
12514     ;; If a cache is present, we may have to alter the active info.
12515     (and gnus-use-cache
12516          (gnus-cache-possibly-alter-active (car info) active))
12517     ;; Modify the list of read articles according to what articles 
12518     ;; are available; then tally the unread articles and add the
12519     ;; number to the group hash table entry.
12520     (cond 
12521      ((zerop (cdr active))
12522       (setq num 0))
12523      ((not range)
12524       (setq num (- (1+ (cdr active)) (car active))))
12525      ((not (listp (cdr range)))
12526       ;; Fix a single (num . num) range according to the
12527       ;; active hash table.
12528       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12529       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12530       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12531       ;; Compute number of unread articles.
12532       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12533      (t
12534       ;; The read list is a list of ranges. Fix them according to
12535       ;; the active hash table.
12536       ;; First peel off any elements that are below the lower
12537       ;; active limit. 
12538       (while (and (cdr range) 
12539                   (>= (car active) 
12540                       (or (and (atom (car (cdr range))) (car (cdr range)))
12541                           (car (car (cdr range))))))
12542         (if (numberp (car range))
12543             (setcar range 
12544                     (cons (car range) 
12545                           (or (and (numberp (car (cdr range)))
12546                                    (car (cdr range))) 
12547                               (cdr (car (cdr range))))))
12548           (setcdr (car range) 
12549                   (or (and (numberp (nth 1 range)) (nth 1 range))
12550                       (cdr (car (cdr range))))))
12551         (setcdr range (cdr (cdr range))))
12552       ;; Adjust the first element to be the same as the lower limit. 
12553       (if (and (not (atom (car range))) 
12554                (< (cdr (car range)) (car active)))
12555           (setcdr (car range) (1- (car active))))
12556       ;; Then we want to peel off any elements that are higher
12557       ;; than the upper active limit.  
12558       (let ((srange range))
12559         ;; Go past all legal elements.
12560         (while (and (cdr srange) 
12561                     (<= (or (and (atom (car (cdr srange)))
12562                                  (car (cdr srange)))
12563                             (car (car (cdr srange)))) (cdr active)))
12564           (setq srange (cdr srange)))
12565         (if (cdr srange)
12566             ;; Nuke all remaining illegal elements.
12567             (setcdr srange nil))
12568
12569         ;; Adjust the final element.
12570         (if (and (not (atom (car srange)))
12571                  (> (cdr (car srange)) (cdr active)))
12572             (setcdr (car srange) (cdr active))))
12573       ;; Compute the number of unread articles.
12574       (while range
12575         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12576                                     (cdr (car range))))
12577                             (or (and (atom (car range)) (car range))
12578                                 (car (car range))))))
12579         (setq range (cdr range)))
12580       (setq num (max 0 (- (cdr active) num)))))
12581     (and info
12582          (progn
12583            (and (assq 'tick marked)
12584                 (inline (gnus-remove-illegal-marked-articles
12585                          (assq 'tick marked) (nth 2 info))))
12586            (and (assq 'dormant marked)
12587                 (inline (gnus-remove-illegal-marked-articles
12588                          (assq 'dormant marked) (nth 2 info))))
12589            (setcar
12590             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12591             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12592                                 (length (cdr (assq 'dormant marked)))))))))
12593     num))
12594
12595 (defun gnus-remove-illegal-marked-articles (marked ranges)
12596   (let ((m (cdr marked)))
12597     ;; Make sure that all ticked articles are a subset of the unread
12598     ;; articles. 
12599     (while m
12600       (if (gnus-member-of-range (car m) ranges)
12601           (setcdr marked (cdr m))
12602         (setq marked m))
12603       (setq m (cdr m)))))
12604
12605 (defun gnus-activate-group (group)
12606   ;; Check whether a group has been activated or not.
12607   (let ((method (gnus-find-method-for-group group))
12608         active)
12609     (and (gnus-check-server method)
12610          ;; We escape all bugs and quit here to make it possible to
12611          ;; continue if a group is so out-there that it reports bugs
12612          ;; and stuff.
12613          (condition-case ()
12614              (gnus-request-group group)
12615            (error nil)
12616            (quit nil))
12617          (save-excursion
12618            (set-buffer nntp-server-buffer)
12619            (goto-char (point-min))
12620            ;; Parse the result we got from `gnus-request-group'.
12621            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12622                 (progn
12623                   (goto-char (match-beginning 1))
12624                   (gnus-sethash 
12625                    group (setq active (cons (read (current-buffer))
12626                                             (read (current-buffer))))
12627                    gnus-active-hashtb))
12628                 ;; Return the new active info.
12629                 active)))))
12630
12631 (defun gnus-update-read-articles 
12632   (group unread unselected ticked &optional domarks replied expirable killed
12633          dormant bookmark score)
12634   "Update the list of read and ticked articles in GROUP using the
12635 UNREAD and TICKED lists.
12636 Note: UNSELECTED has to be sorted over `<'.
12637 Returns whether the updating was successful."
12638   (let* ((active (or gnus-newsgroup-active 
12639                      (gnus-gethash group gnus-active-hashtb)))
12640          (entry (gnus-gethash group gnus-newsrc-hashtb))
12641          (info (nth 2 entry))
12642          (marked (nth 3 info))
12643          (prev 1)
12644          (unread (sort (copy-sequence unread) (function <)))
12645          read)
12646     (if (or (not info) (not active))
12647         ;; There is no info on this group if it was, in fact,
12648         ;; killed. Gnus stores no information on killed groups, so
12649         ;; there's nothing to be done. 
12650         ;; One could store the information somewhere temporarily,
12651         ;; perhaps... Hmmm... 
12652         ()
12653       ;; Remove any negative articles numbers.
12654       (while (and unread (< (car unread) 0))
12655         (setq unread (cdr unread)))
12656       ;; Remove any expired article numbers
12657       (while (and unread (< (car unread) (car active)))
12658         (setq unread (cdr unread)))
12659       (while (and ticked (< (car ticked) (car active)))
12660         (setq ticked (cdr ticked)))
12661       (while (and dormant (< (car dormant) (car active)))
12662         (setq dormant (cdr dormant)))
12663       (setq unread (sort (append unselected unread) '<))
12664       ;; Compute the ranges of read articles by looking at the list of
12665       ;; unread articles.  
12666       (while unread
12667         (if (/= (car unread) prev)
12668             (setq read (cons (if (= prev (1- (car unread))) prev
12669                                (cons prev (1- (car unread)))) read)))
12670         (setq prev (1+ (car unread)))
12671         (setq unread (cdr unread)))
12672       (if (<= prev (cdr active))
12673           (setq read (cons (cons prev (cdr active)) read)))
12674       ;; Enter this list into the group info.
12675       (setcar (cdr (cdr info)) 
12676               (if (> (length read) 1) (nreverse read) read))
12677       ;; Enter the list of ticked articles.
12678       (gnus-set-marked-articles 
12679        info ticked
12680        (if domarks replied (cdr (assq 'reply marked)))
12681        (if domarks expirable (cdr (assq 'expire marked)))
12682        (if domarks killed (cdr (assq 'killed marked)))
12683        (if domarks dormant (cdr (assq 'dormant marked)))
12684        (if domarks bookmark (cdr (assq 'bookmark marked)))
12685        (if domarks score (cdr (assq 'score marked))))
12686       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12687       (gnus-get-unread-articles-in-group 
12688        info (gnus-gethash group gnus-active-hashtb))
12689       t)))
12690
12691 (defun gnus-make-articles-unread (group articles)
12692   "Mark ARTICLES in GROUP as unread."
12693   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12694                           (gnus-gethash (gnus-group-real-name group)
12695                                         gnus-newsrc-hashtb))))
12696          (ranges (nth 2 info))
12697          news)
12698     (while articles
12699       (and (gnus-member-of-range (car articles) ranges)
12700            (setq news (cons (car articles) news)))
12701       (setq articles (cdr articles)))
12702     (if (not news)
12703         ()
12704       (setcar (nthcdr 2 info)
12705               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12706       (gnus-group-update-group group t))))
12707
12708 ;; Enter all dead groups into the hashtb.
12709 (defun gnus-update-active-hashtb-from-killed ()
12710   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12711         (lists (list gnus-killed-list gnus-zombie-list))
12712         killed)
12713     (while lists
12714       (setq killed (car lists))
12715       (while killed
12716         (gnus-sethash (car killed) nil hashtb)
12717         (setq killed (cdr killed)))
12718       (setq lists (cdr lists)))))
12719
12720 ;; Get the active file(s) from the backend(s).
12721 (defun gnus-read-active-file ()
12722   (gnus-group-set-mode-line)
12723   (let ((methods (if (gnus-check-server gnus-select-method)
12724                      ;; The native server is available.
12725                      (cons gnus-select-method gnus-secondary-select-methods)
12726                    ;; The native server is down, so we just do the
12727                    ;; secondary ones.   
12728                    gnus-secondary-select-methods))
12729         list-type)
12730     (setq gnus-have-read-active-file nil)
12731     (save-excursion
12732       (set-buffer nntp-server-buffer)
12733       (while methods
12734         (let* ((method (gnus-server-get-method nil (car methods)))
12735                (where (nth 1 method))
12736                (mesg (format "Reading active file%s via %s..."
12737                              (if (and where (not (zerop (length where))))
12738                                  (concat " from " where) "")
12739                              (car method))))
12740           (gnus-message 5 mesg)
12741           (if (not (gnus-check-server method))
12742               ()
12743             (cond 
12744              ((and (eq gnus-read-active-file 'some)
12745                    (gnus-check-backend-function 'retrieve-groups (car method)))
12746               (let ((newsrc (cdr gnus-newsrc-alist))
12747                     (gmethod (gnus-server-get-method nil method))
12748                     groups)
12749                 (while newsrc
12750                   (and (gnus-server-equal 
12751                         (gnus-find-method-for-group 
12752                          (car (car newsrc)) (car newsrc))
12753                         gmethod)
12754                        (setq groups (cons (gnus-group-real-name 
12755                                            (car (car newsrc))) groups)))
12756                   (setq newsrc (cdr newsrc)))
12757                 (gnus-check-server method)
12758                 (setq list-type (gnus-retrieve-groups groups method))
12759                 (cond 
12760                  ((not list-type)
12761                   (gnus-message 
12762                    1 "Cannot read partial active file from %s server." 
12763                    (car method))
12764                   (ding)
12765                   (sit-for 2))
12766                  ((eq list-type 'active)
12767                   (gnus-active-to-gnus-format method gnus-active-hashtb))
12768                  (t
12769                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12770              (t
12771               (if (not (gnus-request-list method))
12772                   (progn
12773                     (gnus-message 1 "Cannot read active file from %s server." 
12774                                   (car method))
12775                     (ding))
12776                 (gnus-active-to-gnus-format method)
12777                 ;; We mark this active file as read.
12778                 (setq gnus-have-read-active-file
12779                       (cons method gnus-have-read-active-file))
12780                 (gnus-message 5 "%sdone" mesg))))))
12781         (setq methods (cdr methods))))))
12782
12783 ;; Read an active file and place the results in `gnus-active-hashtb'.
12784 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12785   (let ((cur (current-buffer))
12786         (hashtb (or hashtb 
12787                     (if (and gnus-active-hashtb 
12788                              (not (equal method gnus-select-method)))
12789                         gnus-active-hashtb
12790                       (setq gnus-active-hashtb
12791                             (if (equal method gnus-select-method)
12792                                 (gnus-make-hashtable 
12793                                  (count-lines (point-min) (point-max)))
12794                               (gnus-make-hashtable 4096))))))
12795         (flag-hashtb (gnus-make-hashtable 60)))
12796     ;; Delete unnecessary lines.
12797     (goto-char (point-min))
12798     (while (search-forward "\nto." nil t)
12799       (delete-region (1+ (match-beginning 0)) 
12800                      (progn (forward-line 1) (point))))
12801     (or (string= gnus-ignored-newsgroups "")
12802         (progn
12803           (goto-char (point-min))
12804           (delete-matching-lines gnus-ignored-newsgroups)))
12805     ;; Make the group names readable as a lisp expression even if they
12806     ;; contain special characters.
12807     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12808     (goto-char (point-max))
12809     (while (re-search-backward "[][';?()#]" nil t)
12810       (insert ?\\))
12811     ;; If these are groups from a foreign select method, we insert the
12812     ;; group prefix in front of the group names. 
12813     (and method (not (gnus-server-equal
12814                       (gnus-server-get-method nil method)
12815                       (gnus-server-get-method nil gnus-select-method)))
12816          (let ((prefix (gnus-group-prefixed-name "" method)))
12817            (goto-char (point-min))
12818            (while (and (not (eobp))
12819                        (progn (insert prefix)
12820                               (zerop (forward-line 1)))))))
12821     ;; Store the active file in a hash table.
12822     (goto-char (point-min))
12823     (if (string-match "%[oO]" gnus-group-line-format)
12824         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12825         ;; If we want information on moderated groups, we use this
12826         ;; loop...   
12827         (let* ((mod-hashtb (make-vector 7 0))
12828                (m (intern "m" mod-hashtb))
12829                group max min)
12830           (while (not (eobp))
12831             (condition-case nil
12832                 (progn
12833                   (narrow-to-region (point) (gnus-point-at-eol))
12834                   (setq group (let ((obarray hashtb)) (read cur)))
12835                   (if (and (numberp (setq max (read cur)))
12836                            (numberp (setq min (read cur)))
12837                            (progn 
12838                              (skip-chars-forward " \t")
12839                              (not
12840                               (or (= (following-char) ?=)
12841                                   (= (following-char) ?x)
12842                                   (= (following-char) ?j)))))
12843                       (set group (cons min max))
12844                     (set group nil))
12845                   ;; Enter moderated groups into a list.
12846                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12847                       (setq gnus-moderated-list 
12848                             (cons (symbol-name group) gnus-moderated-list))))
12849               (error 
12850                (and group
12851                     (symbolp group)
12852                     (set group nil))))
12853             (widen)
12854             (forward-line 1)))
12855       ;; And if we do not care about moderation, we use this loop,
12856       ;; which is faster.
12857       (let (group max min)
12858         (while (not (eobp))
12859           (condition-case ()
12860               (progn
12861                 (narrow-to-region (point) (gnus-point-at-eol))
12862                 ;; group gets set to a symbol interned in the hash table
12863                 ;; (what a hack!!) - jwz
12864                 (setq group (let ((obarray hashtb)) (read cur)))
12865                 (if (and (numberp (setq max (read cur)))
12866                          (numberp (setq min (read cur)))
12867                          (progn 
12868                            (skip-chars-forward " \t")
12869                            (not
12870                             (or (= (following-char) ?=)
12871                                 (= (following-char) ?x)
12872                                 (= (following-char) ?j)))))
12873                     (set group (cons min max))
12874                   (set group nil)))
12875             (error 
12876              (progn 
12877                (and group
12878                     (symbolp group)
12879                     (set group nil))
12880                (or ignore-errors
12881                    (gnus-message 3 "Warning - illegal active: %s"
12882                                  (buffer-substring 
12883                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
12884           (widen)
12885           (forward-line 1))))))
12886
12887 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12888   ;; Parse a "groups" active file.
12889   (let ((cur (current-buffer))
12890         (hashtb (or hashtb 
12891                     (if (and method gnus-active-hashtb)
12892                         gnus-active-hashtb
12893                       (setq gnus-active-hashtb
12894                             (gnus-make-hashtable 
12895                              (count-lines (point-min) (point-max)))))))
12896         (prefix (and method 
12897                      (not (gnus-server-equal
12898                            (gnus-server-get-method nil method)
12899                            (gnus-server-get-method nil gnus-select-method)))
12900                      (gnus-group-prefixed-name "" method))))
12901
12902     (goto-char (point-min))
12903     ;; We split this into to separate loops, one with the prefix
12904     ;; and one without to speed the reading up somewhat.
12905     (if prefix
12906         (let (min max opoint group)
12907           (while (not (eobp))
12908             (condition-case ()
12909                 (progn
12910                   (read cur) (read cur)
12911                   (setq min (read cur)
12912                         max (read cur)
12913                         opoint (point))
12914                   (skip-chars-forward " \t")
12915                   (insert prefix)
12916                   (goto-char opoint)
12917                   (set (let ((obarray hashtb)) (read cur)) 
12918                        (cons min max)))
12919               (error (and group (symbolp group) (set group nil))))
12920             (forward-line 1)))
12921       (let (min max group)
12922         (while (not (eobp))
12923           (condition-case ()
12924               (if (= (following-char) ?2)
12925                   (progn
12926                     (read cur) (read cur)
12927                     (setq min (read cur)
12928                           max (read cur))
12929                     (set (setq group (let ((obarray hashtb)) (read cur)))
12930                          (cons min max))))
12931             (error (and group (symbolp group) (set group nil))))
12932           (forward-line 1))))))
12933
12934 (defun gnus-read-newsrc-file (&optional force)
12935   "Read startup file.
12936 If FORCE is non-nil, the .newsrc file is read."
12937   ;; Reset variables that might be defined in the .newsrc.eld file.
12938   (let ((variables gnus-variable-list))
12939     (while variables
12940       (set (car variables) nil)
12941       (setq variables (cdr variables))))
12942   (let* ((newsrc-file gnus-current-startup-file)
12943          (quick-file (concat newsrc-file ".el")))
12944     (save-excursion
12945       ;; We always load the .newsrc.eld file. If always contains
12946       ;; much information that can not be gotten from the .newsrc
12947       ;; file (ticked articles, killed groups, foreign methods, etc.)
12948       (gnus-read-newsrc-el-file quick-file)
12949  
12950       (if (or force
12951               (and (file-newer-than-file-p newsrc-file quick-file)
12952                    (file-newer-than-file-p newsrc-file 
12953                                            (concat quick-file "d")))
12954               (not gnus-newsrc-alist))
12955           ;; We read the .newsrc file. Note that if there if a
12956           ;; .newsrc.eld file exists, it has already been read, and
12957           ;; the `gnus-newsrc-hashtb' has been created. While reading
12958           ;; the .newsrc file, Gnus will only use the information it
12959           ;; can find there for changing the data already read -
12960           ;; ie. reading the .newsrc file will not trash the data
12961           ;; already read (except for read articles).
12962           (save-excursion
12963             (gnus-message 5 "Reading %s..." newsrc-file)
12964             (set-buffer (find-file-noselect newsrc-file))
12965             (buffer-disable-undo (current-buffer))
12966             (gnus-newsrc-to-gnus-format)
12967             (kill-buffer (current-buffer))
12968             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12969
12970 (defun gnus-read-newsrc-el-file (file)
12971   (let ((ding-file (concat file "d")))
12972     ;; We always, always read the .eld file.
12973     (gnus-message 5 "Reading %s..." ding-file)
12974     (let (gnus-newsrc-assoc)
12975       (condition-case nil
12976           (load ding-file t t t)
12977         (error nil))
12978       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12979     (let ((inhibit-quit t))
12980       (gnus-uncompress-newsrc-alist))
12981     (gnus-make-hashtable-from-newsrc-alist)
12982     (if (not (file-newer-than-file-p file ding-file))
12983         ()
12984       ;; Old format quick file
12985       (gnus-message 5 "Reading %s..." file)
12986       ;; The .el file is newer than the .eld file, so we read that one
12987       ;; as well. 
12988       (gnus-read-old-newsrc-el-file file))))
12989
12990 ;; Parse the old-style quick startup file
12991 (defun gnus-read-old-newsrc-el-file (file)
12992   (let (newsrc killed marked group m)
12993     (prog1
12994         (let ((gnus-killed-assoc nil)
12995               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12996           (prog1
12997               (condition-case nil
12998                   (load file t t t)
12999                 (error nil))
13000             (setq newsrc gnus-newsrc-assoc
13001                   killed gnus-killed-assoc
13002                   marked gnus-marked-assoc)))
13003       (setq gnus-newsrc-alist nil)
13004       (while newsrc
13005         (setq group (car newsrc))
13006         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13007           (if info
13008               (progn
13009                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13010                 (setcar (cdr info)
13011                         (if (nth 1 group) gnus-level-default-subscribed 
13012                           gnus-level-default-unsubscribed))
13013                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
13014             (setq gnus-newsrc-alist
13015                   (cons 
13016                    (setq info
13017                          (list (car group)
13018                                (if (nth 1 group) gnus-level-default-subscribed
13019                                  gnus-level-default-unsubscribed) 
13020                                (cdr (cdr group))))
13021                    gnus-newsrc-alist)))
13022           (if (setq m (assoc (car group) marked))
13023               (setcdr (cdr (cdr info))
13024                       (cons (list (cons 'tick (cdr m))) nil))))
13025         (setq newsrc (cdr newsrc)))
13026       (setq newsrc killed)
13027       (while newsrc
13028         (setcar newsrc (car (car newsrc)))
13029         (setq newsrc (cdr newsrc)))
13030       (setq gnus-killed-list killed))
13031     ;; The .el file version of this variable does not begin with
13032     ;; "options", while the .eld version does, so we just add it if it
13033     ;; isn't there.
13034     (and
13035      gnus-newsrc-options 
13036      (progn
13037        (and (not (string-match "^ *options" gnus-newsrc-options))
13038             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
13039        (and (not (string-match "\n$" gnus-newsrc-options))
13040             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
13041        ;; Finally, if we read some options lines, we parse them.
13042        (or (string= gnus-newsrc-options "")
13043            (gnus-newsrc-parse-options gnus-newsrc-options))))
13044
13045     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
13046     (gnus-make-hashtable-from-newsrc-alist)))
13047       
13048 (defun gnus-make-newsrc-file (file)
13049   "Make server dependent file name by catenating FILE and server host name."
13050   (let* ((file (expand-file-name file nil))
13051          (real-file (concat file "-" (nth 1 gnus-select-method))))
13052     (if (or (file-exists-p real-file)
13053             (file-exists-p (concat real-file ".el"))
13054             (file-exists-p (concat real-file ".eld")))
13055         real-file file)))
13056
13057 (defun gnus-uncompress-newsrc-alist ()
13058   ;; Uncompress all lists of marked articles in the newsrc assoc.
13059   (let ((newsrc gnus-newsrc-alist)
13060         marked)
13061     (while newsrc
13062       (if (not (setq marked (nth 3 (car newsrc))))
13063           ()
13064         (while marked
13065           (or (eq 'score (car (car marked)))
13066               (eq 'bookmark (car (car marked)))
13067               (eq 'killed (car (car marked)))
13068               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13069           (setq marked (cdr marked))))
13070       (setq newsrc (cdr newsrc)))))
13071
13072 (defun gnus-compress-newsrc-alist ()
13073   ;; Compress all lists of marked articles in the newsrc assoc.
13074   (let ((newsrc gnus-newsrc-alist)
13075         marked)
13076     (while newsrc
13077       (if (not (setq marked (nth 3 (car newsrc))))
13078           ()
13079         (while marked
13080           (or (eq 'score (car (car marked)))
13081               (eq 'bookmark (car (car marked)))
13082               (eq 'killed (car (car marked)))
13083               (setcdr (car marked) 
13084                       (condition-case ()
13085                           (gnus-compress-sequence 
13086                            (sort (cdr (car marked)) '<) t)
13087                         (error (cdr (car marked))))))
13088           (setq marked (cdr marked))))
13089       (setq newsrc (cdr newsrc)))))
13090
13091 (defun gnus-newsrc-to-gnus-format ()
13092   (setq gnus-newsrc-options "")
13093   (setq gnus-newsrc-options-n nil)
13094
13095   (or gnus-active-hashtb
13096       (setq gnus-active-hashtb (make-vector 4095 0)))
13097   (let ((buf (current-buffer))
13098         (already-read (> (length gnus-newsrc-alist) 1))
13099         group subscribed options-symbol newsrc Options-symbol
13100         symbol reads num1)
13101     (goto-char (point-min))
13102     ;; We intern the symbol `options' in the active hashtb so that we
13103     ;; can `eq' against it later.
13104     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
13105     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
13106   
13107     (while (not (eobp))
13108       ;; We first read the first word on the line by narrowing and
13109       ;; then reading into `gnus-active-hashtb'.  Most groups will
13110       ;; already exist in that hashtb, so this will save some string
13111       ;; space.
13112       (narrow-to-region
13113        (point)
13114        (progn (skip-chars-forward "^ \t!:\n") (point)))
13115       (goto-char (point-min))
13116       (setq symbol 
13117             (and (/= (point-min) (point-max))
13118                  (let ((obarray gnus-active-hashtb)) (read buf))))
13119       (widen)
13120       ;; Now, the symbol we have read is either `options' or a group
13121       ;; name.  If it is an options line, we just add it to a string. 
13122       (cond 
13123        ((or (eq symbol options-symbol)
13124             (eq symbol Options-symbol))
13125         (setq gnus-newsrc-options
13126               ;; This concatting is quite inefficient, but since our
13127               ;; thorough studies show that approx 99.37% of all
13128               ;; .newsrc files only contain a single options line, we
13129               ;; don't give a damn, frankly, my dear.
13130               (concat gnus-newsrc-options
13131                       (buffer-substring 
13132                        (gnus-point-at-bol)
13133                        ;; Options may continue on the next line.
13134                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13135                                 (progn (beginning-of-line) (point)))
13136                            (point))))))
13137        (symbol
13138         (or (boundp symbol) (set symbol nil))
13139         ;; It was a group name.
13140         (setq subscribed (= (following-char) ?:)
13141               group (symbol-name symbol)
13142               reads nil)
13143         (if (eolp)
13144             ;; If the line ends here, this is clearly a buggy line, so
13145             ;; we put point a the beginning of line and let the cond
13146             ;; below do the error handling.
13147             (beginning-of-line)
13148           ;; We skip to the beginning of the ranges.
13149           (skip-chars-forward "!: \t"))
13150         ;; We are now at the beginning of the list of read articles.
13151         ;; We read them range by range.
13152         (while
13153             (cond 
13154              ((looking-at "[0-9]+")
13155               ;; We narrow and read a number instead of buffer-substring/
13156               ;; string-to-int because it's faster. narrow/widen is
13157               ;; faster than save-restriction/narrow, and save-restriction
13158               ;; produces a garbage object.
13159               (setq num1 (progn
13160                            (narrow-to-region (match-beginning 0) (match-end 0))
13161                            (read buf)))
13162               (widen)
13163               ;; If the next character is a dash, then this is a range.
13164               (if (= (following-char) ?-)
13165                   (progn
13166                     ;; We read the upper bound of the range.
13167                     (forward-char 1)
13168                     (if (not (looking-at "[0-9]+"))
13169                         ;; This is a buggy line, by we pretend that
13170                         ;; it's kinda OK. Perhaps the user should be
13171                         ;; dinged? 
13172                         (setq reads (cons num1 reads))
13173                       (setq reads 
13174                             (cons 
13175                              (cons num1
13176                                    (progn
13177                                      (narrow-to-region (match-beginning 0) 
13178                                                        (match-end 0))
13179                                      (read buf)))
13180                              reads))
13181                       (widen)))
13182                 ;; It was just a simple number, so we add it to the
13183                 ;; list of ranges.
13184                 (setq reads (cons num1 reads)))
13185               ;; If the next char in ?\n, then we have reached the end
13186               ;; of the line and return nil.
13187               (/= (following-char) ?\n))
13188              ((= (following-char) ?\n)
13189               ;; End of line, so we end.
13190               nil)
13191              (t
13192               ;; Not numbers and not eol, so this might be a buggy
13193               ;; line... 
13194               (or (eobp)                
13195                   ;; If it was eob instead of ?\n, we allow it.
13196                   (progn
13197                     ;; The line was buggy.
13198                     (setq group nil)
13199                     (gnus-message 3 "Mangled line: %s" 
13200                                   (buffer-substring (gnus-point-at-bol) 
13201                                                     (gnus-point-at-eol)))
13202                     (ding)
13203                     (sit-for 1)))
13204               nil))
13205           ;; Skip past ", ". Spaces are illegal in these ranges, but
13206           ;; we allow them, because it's a common mistake to put a
13207           ;; space after the comma.
13208           (skip-chars-forward ", "))
13209
13210         ;; We have already read .newsrc.eld, so we gently update the
13211         ;; data in the hash table with the information we have just
13212         ;; read. 
13213         (if (not group)
13214             ()
13215           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13216                 level)
13217             (if info
13218                 ;; There is an entry for this file in the alist.
13219                 (progn
13220                   (setcar (nthcdr 2 info) (nreverse reads))
13221                   ;; We update the level very gently.  In fact, we
13222                   ;; only change it if there's been a status change
13223                   ;; from subscribed to unsubscribed, or vice versa.
13224                   (setq level (nth 1 info))
13225                   (cond ((and (<= level gnus-level-subscribed)
13226                               (not subscribed))
13227                          (setq level (if reads
13228                                          gnus-level-default-unsubscribed 
13229                                        (1+ gnus-level-default-unsubscribed))))
13230                         ((and (> level gnus-level-subscribed) subscribed)
13231                          (setq level gnus-level-default-subscribed)))
13232                   (setcar (cdr info) level))
13233               ;; This is a new group.
13234               (setq info (list group 
13235                                (if subscribed
13236                                    gnus-level-default-subscribed 
13237                                  (if reads
13238                                      (1+ gnus-level-subscribed)
13239                                    gnus-level-default-unsubscribed))
13240                                (nreverse reads))))
13241             (setq newsrc (cons info newsrc))))))
13242       (forward-line 1))
13243     
13244     (setq newsrc (nreverse newsrc))
13245
13246     (if (not already-read)
13247         ()
13248       ;; We now have two newsrc lists - `newsrc', which is what we
13249       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13250       ;; what we've read from .newsrc.eld. We have to merge these
13251       ;; lists. We do this by "attaching" any (foreign) groups in the
13252       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13253       (let ((rc (cdr gnus-newsrc-alist))
13254             (prev gnus-newsrc-alist)
13255             entry mentry)
13256         (while rc
13257           (or (null (nth 4 (car rc)))   ; It's a native group.
13258               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13259               (if (setq entry (assoc (car (car prev)) newsrc))
13260                   (setcdr (setq mentry (memq entry newsrc))
13261                           (cons (car rc) (cdr mentry)))
13262                 (setq newsrc (cons (car rc) newsrc))))
13263           (setq prev rc
13264                 rc (cdr rc)))))
13265
13266     (setq gnus-newsrc-alist newsrc)
13267     ;; We make the newsrc hashtb.
13268     (gnus-make-hashtable-from-newsrc-alist)
13269
13270     ;; Finally, if we read some options lines, we parse them.
13271     (or (string= gnus-newsrc-options "")
13272         (gnus-newsrc-parse-options gnus-newsrc-options))))
13273
13274 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13275 ;; The return value will be a list on the form
13276 ;; ((regexp1 . ignore)
13277 ;;  (regexp2 . subscribe)...)
13278 ;; When handling new newsgroups, groups that match a `ignore' regexp
13279 ;; will be ignored, and groups that match a `subscribe' regexp will be
13280 ;; subscribed. A line like
13281 ;; options -n !all rec.all
13282 ;; will lead to a list that looks like
13283 ;; (("^rec\\..+" . subscribe) 
13284 ;;  ("^.+" . ignore))
13285 ;; So all "rec.*" groups will be subscribed, while all the other
13286 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13287 ;; different from "options -n rec.all !all". 
13288 (defun gnus-newsrc-parse-options (options)
13289   (let (out eol)
13290     (save-excursion
13291       (gnus-set-work-buffer)
13292       (insert (regexp-quote options))
13293       ;; First we treat all continuation lines.
13294       (goto-char (point-min))
13295       (while (re-search-forward "\n[ \t]+" nil t)
13296         (replace-match " " t t))
13297       ;; Then we transform all "all"s into ".+"s.
13298       (goto-char (point-min))
13299       (while (re-search-forward "\\ball\\b" nil t)
13300         (replace-match ".+" t t))
13301       (goto-char (point-min))
13302       ;; We remove all other options than the "-n" ones.
13303       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13304         (replace-match " ")
13305         (forward-char -1))
13306       (goto-char (point-min))
13307
13308       ;; We are only interested in "options -n" lines - we
13309       ;; ignore the other option lines.
13310       (while (re-search-forward "[ \t]-n" nil t)
13311         (setq eol 
13312               (or (save-excursion
13313                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13314                          (- (point) 2)))
13315                   (gnus-point-at-eol)))
13316         ;; Search for all "words"...
13317         (while (re-search-forward "[^ \t,\n]+" eol t)
13318           (if (= (char-after (match-beginning 0)) ?!)
13319               ;; If the word begins with a bang (!), this is a "not"
13320               ;; spec. We put this spec (minus the bang) and the
13321               ;; symbol `ignore' into the list.
13322               (setq out (cons (cons (concat 
13323                                      "^" (buffer-substring 
13324                                           (1+ (match-beginning 0))
13325                                           (match-end 0)))
13326                                     'ignore) out))
13327             ;; There was no bang, so this is a "yes" spec.
13328             (setq out (cons (cons (concat 
13329                                    "^" (buffer-substring (match-beginning 0)
13330                                                          (match-end 0)))
13331                                   'subscribe) out)))))
13332     
13333       (setq gnus-newsrc-options-n out))))
13334                
13335
13336 (defun gnus-save-newsrc-file ()
13337   "Save .newsrc file."
13338   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13339   ;; from the variable gnus-newsrc-alist.
13340   (and (or gnus-newsrc-alist gnus-killed-list)
13341        gnus-current-startup-file
13342        (progn
13343          (run-hooks 'gnus-save-newsrc-hook)
13344          (save-excursion
13345            (if (and gnus-use-dribble-file
13346                     (or (not gnus-dribble-buffer)
13347                         (not (buffer-name gnus-dribble-buffer))
13348                         (zerop (save-excursion
13349                                  (set-buffer gnus-dribble-buffer)
13350                                  (buffer-size)))))
13351                (gnus-message 4 "(No changes need to be saved)")
13352              (if gnus-save-newsrc-file
13353                  (progn
13354                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13355                    ;; Make backup file of master newsrc.
13356                    (gnus-gnus-to-newsrc-format)
13357                    (gnus-message 5 "Saving %s...done"
13358                                  gnus-current-startup-file)))
13359              ;; Quickly loadable .newsrc.
13360              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13361              (setq buffer-file-name (concat gnus-current-startup-file ".eld"))
13362              (gnus-add-current-to-buffer-list)
13363              (buffer-disable-undo (current-buffer))
13364              (erase-buffer)
13365              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13366              (gnus-gnus-to-quick-newsrc-format)
13367              (save-buffer)
13368              (kill-buffer (current-buffer))
13369              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13370              (gnus-dribble-delete-file))))))
13371
13372 (defun gnus-gnus-to-quick-newsrc-format ()
13373   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13374   (insert ";; Gnus startup file.\n")
13375   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13376   (insert ";; to read .newsrc.\n")
13377   (insert "(setq gnus-newsrc-file-version "
13378           (prin1-to-string gnus-version) ")\n")
13379   (let ((variables gnus-variable-list)
13380         (inhibit-quit t)
13381         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13382         variable)
13383     ;; insert lisp expressions.
13384     (gnus-compress-newsrc-alist)
13385     (while variables
13386       (setq variable (car variables))
13387       (and (boundp variable)
13388            (symbol-value variable)
13389            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13390            (insert "(setq " (symbol-name variable) " '"
13391                    (prin1-to-string (symbol-value variable))
13392                    ")\n"))
13393       (setq variables (cdr variables)))
13394     (gnus-uncompress-newsrc-alist)))
13395
13396
13397 (defun gnus-gnus-to-newsrc-format ()
13398   ;; Generate and save the .newsrc file.
13399   (let ((newsrc (cdr gnus-newsrc-alist))
13400         info ranges range)
13401     (save-excursion
13402       (set-buffer (create-file-buffer gnus-current-startup-file))
13403       (setq buffer-file-name gnus-current-startup-file)
13404       (buffer-disable-undo (current-buffer))
13405       (erase-buffer)
13406       ;; Write options.
13407       (if gnus-newsrc-options (insert gnus-newsrc-options))
13408       ;; Write subscribed and unsubscribed.
13409       (while newsrc
13410         (setq info (car newsrc))
13411         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13412             (progn
13413               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13414                                      "!" ":"))
13415               (if (setq ranges (nth 2 info))
13416                   (progn
13417                     (insert " ")
13418                     (if (not (listp (cdr ranges)))
13419                         (if (= (car ranges) (cdr ranges))
13420                             (insert (int-to-string (car ranges)))
13421                           (insert (int-to-string (car ranges)) "-" 
13422                                   (int-to-string (cdr ranges))))
13423                       (while ranges
13424                         (setq range (car ranges)
13425                               ranges (cdr ranges))
13426                         (if (or (atom range) (= (car range) (cdr range)))
13427                             (insert (int-to-string 
13428                                      (or (and (atom range) range) 
13429                                          (car range))))
13430                           (insert (int-to-string (car range)) "-"
13431                                   (int-to-string (cdr range))))
13432                         (if ranges (insert ","))))))
13433               (insert "\n")))
13434         (setq newsrc (cdr newsrc)))
13435       ;; It has been reported that sometime the modtime on the .newsrc
13436       ;; file seems to be off. We really do want to overwrite it, so
13437       ;; we clear the modtime here before saving. It's a bit odd,
13438       ;; though... 
13439       ;; sometimes the modtime clear isn't sufficient.  most brute force:
13440       ;; delete the silly thing entirely first.  but this fails to provide
13441       ;; such niceties as .newsrc~ creation.
13442       (if gnus-modtime-botch
13443           (delete-file gnus-startup-file)
13444         (clear-visited-file-modtime))
13445       (save-buffer)
13446       (kill-buffer (current-buffer)))))
13447
13448 (defun gnus-read-all-descriptions-files ()
13449   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13450     (while methods
13451       (gnus-read-descriptions-file (car methods))
13452       (setq methods (cdr methods)))
13453     t))
13454
13455 (defun gnus-read-descriptions-file (&optional method)
13456   (let ((method (or method gnus-select-method)))
13457     ;; We create the hashtable whether we manage to read the desc file
13458     ;; to avoid trying to re-read after a failed read.
13459     (or gnus-description-hashtb
13460         (setq gnus-description-hashtb 
13461               (gnus-make-hashtable (length gnus-active-hashtb))))
13462     ;; Mark this method's desc file as read.
13463     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13464                   gnus-description-hashtb)
13465
13466     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13467     (cond 
13468      ((not (gnus-check-server method))
13469       (gnus-message 1 "Couldn't open server")
13470       nil)
13471      ((not (gnus-request-list-newsgroups method))
13472       (gnus-message 1 "Couldn't read newsgroups descriptions")
13473       nil)
13474      (t
13475       (let (group)
13476         (save-excursion
13477           (save-restriction
13478             (set-buffer nntp-server-buffer)
13479             (goto-char (point-min))
13480             (if (or (search-forward "\n.\n" nil t)
13481                     (goto-char (point-max)))
13482                 (progn
13483                   (beginning-of-line)
13484                   (narrow-to-region (point-min) (point))))
13485             (goto-char (point-min))
13486             (while (not (eobp))
13487               ;; If we get an error, we set group to 0, which is not a
13488               ;; symbol... 
13489               (setq group 
13490                     (condition-case ()
13491                         (let ((obarray gnus-description-hashtb))
13492                           ;; Group is set to a symbol interned in this
13493                           ;; hash table.
13494                           (read nntp-server-buffer))
13495                       (error 0)))
13496               (skip-chars-forward " \t")
13497               ;; ... which leads to this line being effectively ignored.
13498               (and (symbolp group)
13499                    (set group (buffer-substring 
13500                                (point) (progn (end-of-line) (point)))))
13501               (forward-line 1))))
13502         (gnus-message 5 "Reading descriptions file...done")
13503         t)))))
13504
13505 (defun gnus-group-get-description (group)
13506   ;; Get the description of a group by sending XGTITLE to the server.
13507   (and (gnus-request-group-description group)
13508        (save-excursion
13509          (set-buffer nntp-server-buffer)
13510          (goto-char (point-min))
13511          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13512               (buffer-substring (match-beginning 1) (match-end 1))))))
13513
13514 ;;;
13515 ;;; Server
13516 ;;;
13517
13518 (defvar gnus-server-mode-hook nil
13519   "Hook run in `gnus-server-mode' buffers.")
13520
13521 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13522   "Format of server lines.
13523 It works along the same lines as a normal formatting string,
13524 with some simple extensions.")
13525
13526 (defvar gnus-server-mode-line-format "Gnus  List of servers"
13527   "The format specification for the server mode line.")
13528
13529 (defconst gnus-server-line-format-alist
13530   (list (list ?h 'how ?s)
13531         (list ?n 'name ?s)
13532         (list ?w 'where ?s)
13533         ))
13534
13535 (defconst gnus-server-mode-line-format-alist 
13536   (list (list ?S 'news-server ?s)
13537         (list ?M 'news-method ?s)
13538         (list ?u 'user-defined ?s)))
13539
13540 (defvar gnus-server-line-format-spec nil)
13541 (defvar gnus-server-mode-line-format-spec nil)
13542 (defvar gnus-server-killed-servers nil)
13543
13544 (defvar gnus-server-mode-map nil)
13545 (put 'gnus-server-mode 'mode-class 'special)
13546
13547 (if gnus-server-mode-map
13548     nil
13549   (setq gnus-server-mode-map (make-sparse-keymap))
13550   (suppress-keymap gnus-server-mode-map)
13551   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13552   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13553   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13554   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13555   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13556   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13557   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13558   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13559   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13560   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13561
13562 (defun gnus-server-mode ()
13563   "Major mode for listing and editing servers.
13564
13565 All normal editing commands are switched off.
13566 \\<gnus-server-mode-map>
13567
13568 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13569
13570 The following commands are available:
13571
13572 \\{gnus-server-mode-map}"
13573   (interactive)
13574   (if gnus-visual (gnus-server-make-menu-bar))
13575   (kill-all-local-variables)
13576   (setq mode-line-modified "-- ")
13577   (make-local-variable 'mode-line-format)
13578   (setq mode-line-format (copy-sequence mode-line-format))
13579   (and (equal (nth 3 mode-line-format) "   ")
13580        (setcar (nthcdr 3 mode-line-format) ""))
13581   (setq major-mode 'gnus-server-mode)
13582   (setq mode-name "Server")
13583                                         ;  (gnus-group-set-mode-line)
13584   (setq mode-line-process nil)
13585   (use-local-map gnus-server-mode-map)
13586   (buffer-disable-undo (current-buffer))
13587   (setq truncate-lines t)
13588   (setq buffer-read-only t)
13589   (run-hooks 'gnus-server-mode-hook))
13590
13591 (defun gnus-server-insert-server-line (sformat name method)
13592   (let* ((sformat (or sformat gnus-server-line-format-spec))
13593          (how (car method))
13594          (where (nth 1 method))
13595          b)
13596     (beginning-of-line)
13597     (setq b (point))
13598     ;; Insert the text.
13599     (insert (eval sformat))
13600     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13601
13602 (defun gnus-server-setup-buffer ()
13603   (if (get-buffer gnus-server-buffer)
13604       ()
13605     (save-excursion
13606       (set-buffer (get-buffer-create gnus-server-buffer))
13607       (gnus-server-mode)
13608       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13609
13610 (defun gnus-server-prepare ()
13611   (setq gnus-server-mode-line-format-spec 
13612         (gnus-parse-format gnus-server-mode-line-format 
13613                            gnus-server-mode-line-format-alist))
13614   (setq gnus-server-line-format-spec 
13615         (gnus-parse-format gnus-server-line-format 
13616                            gnus-server-line-format-alist))
13617   (let ((alist gnus-server-alist)
13618         (buffer-read-only nil))
13619     (erase-buffer)
13620     (while alist
13621       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13622       (setq alist (cdr alist))))
13623   (goto-char (point-min))
13624   (gnus-server-position-cursor))
13625
13626 (defun gnus-server-server-name ()
13627   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13628     (and server (symbol-name server))))
13629
13630 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13631
13632 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13633
13634 (defun gnus-server-update-server (server)
13635   (save-excursion
13636     (set-buffer gnus-server-buffer)
13637     (let ((buffer-read-only nil)
13638           (info (cdr (assoc server gnus-server-alist))))
13639       (gnus-dribble-enter 
13640        (concat "(gnus-server-set-info \"" server "\" '"
13641                (prin1-to-string info) ")"))
13642       ;; Buffer may be narrowed.
13643       (save-restriction
13644         (widen)
13645         (if (gnus-server-goto-server server)
13646             (delete-region (progn (beginning-of-line) (point))
13647                            (progn (forward-line 1) (point))))
13648         (let ((entry (assoc server gnus-server-alist)))
13649           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13650           (gnus-server-position-cursor))))))
13651
13652 (defun gnus-server-set-info (server info)
13653   ;; Enter a select method into the virtual server alist.
13654   (gnus-dribble-enter 
13655    (concat "(gnus-server-set-info \"" server "\" '"
13656            (prin1-to-string info) ")"))
13657   (let* ((server (nth 1 info))
13658          (entry (assoc server gnus-server-alist)))
13659     (if entry (setcdr entry info)
13660       (setq gnus-server-alist
13661             (nconc gnus-server-alist (list (cons server info)))))))
13662
13663 (defun gnus-server-to-method (server)
13664   ;; Map virtual server names to select methods.
13665   (or (and (equal server "native") gnus-select-method)
13666       (cdr (assoc server gnus-server-alist))))
13667
13668 (defun gnus-server-extend-method (group method)
13669   ;; This function "extends" a virtual server.  If the server is
13670   ;; "hello", and the select method is ("hello" (my-var "something")) 
13671   ;; in the group "alt.alt", this will result in a new virtual server
13672   ;; called "helly+alt.alt".
13673   (let ((entry
13674          (gnus-copy-sequence 
13675           (if (equal (car method) "native") gnus-select-method
13676             (cdr (assoc (car method) gnus-server-alist))))))
13677     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13678     (nconc entry (cdr method))))
13679
13680 (defun gnus-server-get-method (group method)
13681   ;; Input either a server name, and extended server name, or a
13682   ;; select method, and return a select method. 
13683   (cond ((stringp method)
13684          (gnus-server-to-method method))
13685         ((and (stringp (car method)) group)
13686          (gnus-server-extend-method group method))
13687         (t
13688          (gnus-server-add-address method))))
13689
13690 (defun gnus-server-add-address (method)
13691   (let ((method-name (symbol-name (car method))))
13692     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13693              (not (assq (intern (concat method-name "-address")) method)))
13694         (append method (list (list (intern (concat method-name "-address"))
13695                                    (nth 1 method))))
13696       method)))
13697
13698 (defun gnus-server-equal (s1 s2)
13699   (or (equal s1 s2)
13700       (and (= (length s1) (length s2))
13701            (progn
13702              (while (and s1 (member (car s1) s2))
13703                (setq s1 (cdr s1)))
13704              (null s1)))))
13705
13706 ;;; Interactive server functions.
13707
13708 (defun gnus-server-kill-server (server)
13709   "Kill the server on the current line."
13710   (interactive (list (gnus-server-server-name)))
13711   (or (gnus-server-goto-server server)
13712       (if server (error "No such server: %s" server)
13713         (error "No server on the current line")))
13714   (gnus-dribble-enter "")
13715   (let ((buffer-read-only nil))
13716     (delete-region (progn (beginning-of-line) (point))
13717                    (progn (forward-line 1) (point))))
13718   (setq gnus-server-killed-servers 
13719         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13720   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13721                                 gnus-server-alist))
13722   (gnus-server-position-cursor))
13723
13724 (defun gnus-server-yank-server ()
13725   "Yank the previously killed server."
13726   (interactive)
13727   (or gnus-server-killed-servers
13728       (error "No killed servers to be yanked"))
13729   (let ((alist gnus-server-alist)
13730         (server (gnus-server-server-name))
13731         (killed (car gnus-server-killed-servers)))
13732     (if (not server) 
13733         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13734       (if (string= server (car (car gnus-server-alist)))
13735           (setq gnus-server-alist (cons killed gnus-server-alist))
13736         (while (and (cdr alist)
13737                     (not (string= server (car (car (cdr alist))))))
13738           (setq alist (cdr alist)))
13739         (setcdr alist (cons killed (cdr alist)))))
13740     (gnus-server-update-server (car killed))
13741     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13742     (gnus-server-position-cursor)))
13743
13744 (defun gnus-server-exit ()
13745   "Return to the group buffer."
13746   (interactive)
13747   (kill-buffer (current-buffer))
13748   (switch-to-buffer gnus-group-buffer))
13749
13750 (defun gnus-server-list-servers ()
13751   "List all available servers."
13752   (interactive)
13753   (let ((cur (gnus-server-server-name)))
13754     (gnus-server-prepare)
13755     (if cur (gnus-server-goto-server cur)
13756       (goto-char (point-max))
13757       (forward-line -1))
13758     (gnus-server-position-cursor)))
13759
13760 (defun gnus-server-copy-server (from to)
13761   (interactive
13762    (list
13763     (or (gnus-server-server-name)
13764         (error "No server on the current line"))
13765     (read-string "Copy to: ")))
13766   (or from (error "No server on current line"))
13767   (or (and to (not (string= to ""))) (error "No name to copy to"))
13768   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13769   (or (assoc from gnus-server-alist) 
13770       (error "%s: no such server" from))
13771   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13772     (setcar to-entry to)
13773     (setcar (nthcdr 2 to-entry) to)
13774     (setq gnus-server-killed-servers 
13775           (cons to-entry gnus-server-killed-servers))
13776     (gnus-server-yank-server)))
13777
13778 (defun gnus-server-add-server (how where)
13779   (interactive 
13780    (list (intern (completing-read "Server method: "
13781                                   gnus-valid-select-methods nil t))
13782          (read-string "Server name: ")))
13783   (setq gnus-server-killed-servers 
13784         (cons (list where how where) gnus-server-killed-servers))
13785   (gnus-server-yank-server))
13786
13787 (defun gnus-server-goto-server (server)
13788   "Jump to a server line."
13789   (interactive
13790    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13791   (let ((to (text-property-any (point-min) (point-max) 
13792                                'gnus-server (intern server))))
13793     (and to
13794          (progn
13795            (goto-char to) 
13796            (gnus-server-position-cursor)))))
13797
13798 (defun gnus-server-edit-server (server)
13799   "Edit the server on the current line."
13800   (interactive (list (gnus-server-server-name)))
13801   (or server
13802       (error "No server on current line"))
13803   (let ((winconf (current-window-configuration)))
13804     (get-buffer-create gnus-server-edit-buffer)
13805     (gnus-configure-windows 'edit-server)
13806     (gnus-add-current-to-buffer-list)
13807     (emacs-lisp-mode)
13808     (make-local-variable 'gnus-prev-winconf)
13809     (setq gnus-prev-winconf winconf)
13810     (use-local-map (copy-keymap (current-local-map)))
13811     (let ((done-func '(lambda () 
13812                         "Exit editing mode and update the information."
13813                         (interactive)
13814                         (gnus-server-edit-server-done 'group))))
13815       (setcar (cdr (nth 4 done-func)) server)
13816       (local-set-key "\C-c\C-c" done-func))
13817     (erase-buffer)
13818     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13819     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13820
13821 (defun gnus-server-edit-server-done (server)
13822   (interactive)
13823   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13824   (goto-char (point-min))
13825   (let ((form (read (current-buffer)))
13826         (winconf gnus-prev-winconf))
13827     (gnus-server-set-info server form)
13828     (kill-buffer (current-buffer))
13829     (and winconf (set-window-configuration winconf))
13830     (set-buffer gnus-server-buffer)
13831     (gnus-server-update-server (gnus-server-server-name))
13832     (gnus-server-list-servers)
13833     (gnus-server-position-cursor)))
13834
13835 (defun gnus-server-read-server (server)
13836   "Browse a server."
13837   (interactive (list (gnus-server-server-name)))
13838   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13839
13840 (defun gnus-mouse-pick-server (e)
13841   (interactive "e")
13842   (mouse-set-point e)
13843   (gnus-server-read-server (gnus-server-server-name)))
13844
13845 ;;;
13846 ;;; entry points into gnus-score.el
13847 ;;;
13848
13849 ;;; Finding score files. 
13850
13851 (defvar gnus-global-score-files nil
13852   "*List of global score files and directories.
13853 Set this variable if you want to use people's score files.  One entry
13854 for each score file or each score file directory.  Gnus will decide
13855 by itself what score files are applicable to which group.
13856
13857 Say you want to use the single score file
13858 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13859 score files in the \"/ftp.some-where:/pub/score\" directory.
13860
13861  (setq gnus-global-score-files
13862        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13863          \"/ftp.some-where:/pub/score\"))")
13864
13865 (defun gnus-score-score-files (group)
13866   "Return a list of all possible score files."
13867   ;; Search and set any global score files.
13868   (and gnus-global-score-files 
13869        (or gnus-internal-global-score-files
13870            (gnus-score-search-global-directories gnus-global-score-files)))
13871   ;; Fix the kill-file dir variable.
13872   (setq gnus-kill-files-directory 
13873         (file-name-as-directory
13874          (or gnus-kill-files-directory "~/News/")))
13875   ;; If we can't read it, there are no score files.
13876   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13877       (setq gnus-score-file-list nil)
13878     (if (gnus-use-long-file-name 'not-score)
13879         ;; We want long file names.
13880         (if (or (not gnus-score-file-list)
13881                 (not (car gnus-score-file-list))
13882                 (gnus-file-newer-than gnus-kill-files-directory
13883                                       (car gnus-score-file-list)))
13884             (setq gnus-score-file-list 
13885                   (cons (nth 5 (file-attributes gnus-kill-files-directory))
13886                         (nreverse 
13887                          (directory-files 
13888                           gnus-kill-files-directory t 
13889                           (gnus-score-file-regexp))))))
13890       ;; We do not use long file names, so we have to do some
13891       ;; directory traversing.  
13892       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13893             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13894             dir files suffix)
13895         (while suffixes
13896           (setq dir (expand-file-name
13897                      (concat gnus-kill-files-directory
13898                              (gnus-replace-chars-in-string group ?. ?/))))
13899           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13900           (setq suffix (car suffixes)
13901                 suffixes (cdr suffixes))
13902           (if (file-exists-p (concat dir "/" suffix))
13903               (setq files (cons (concat dir "/" suffix) files)))
13904           (while (>= (1+ (length dir)) mdir)
13905             (and (file-exists-p (concat dir "/all/" suffix))
13906                  (setq files (cons (concat dir "/all/" suffix) files)))
13907             (string-match "/[^/]*$" dir)
13908             (setq dir (substring dir 0 (match-beginning 0)))))
13909         (setq gnus-score-file-list 
13910               (cons nil (nreverse files)))))
13911     (cdr gnus-score-file-list)))
13912
13913 (defun gnus-score-file-regexp ()
13914   (concat "\\(" gnus-score-file-suffix 
13915           "\\|" gnus-adaptive-file-suffix "\\)$"))
13916         
13917 (defun gnus-score-find-bnews (group)
13918   "Return a list of score files for GROUP.
13919 The score files are those files in the ~/News directory which matches
13920 GROUP using BNews sys file syntax."
13921   (let* ((sfiles (append (gnus-score-score-files group)
13922                          gnus-internal-global-score-files))
13923          (kill-dir (file-name-as-directory 
13924                     (expand-file-name gnus-kill-files-directory)))
13925          (klen (length kill-dir))
13926          ofiles not-match regexp)
13927     (save-excursion
13928       (set-buffer (get-buffer-create "*gnus score files*"))
13929       (buffer-disable-undo (current-buffer))
13930       ;; Go through all score file names and create regexp with them
13931       ;; as the source.  
13932       (while sfiles
13933         (erase-buffer)
13934         (insert (car sfiles))
13935         (goto-char (point-min))
13936         ;; First remove the suffix itself.
13937         (re-search-forward (concat "." (gnus-score-file-regexp)))
13938         (replace-match "" t t) 
13939         (goto-char (point-min))
13940         (if (looking-at (regexp-quote kill-dir))
13941             ;; If the file name was just "SCORE", `klen' is one character
13942             ;; too much.
13943             (delete-char (min (1- (point-max)) klen))
13944           (goto-char (point-max))
13945           (search-backward "/")
13946           (delete-region (1+ (point)) (point-min)))
13947         ;; If short file names were used, we have to translate slashes.
13948         (goto-char (point-min))
13949         (while (re-search-forward "[/:]" nil t)
13950           (replace-match "." t t))
13951         ;; Cludge to get rid of "nntp+" problems.
13952         (goto-char (point-min))
13953         (and (looking-at "nn[a-z]+\\+")
13954              (progn
13955                (search-forward "+")
13956                (forward-char -1)
13957                (insert "\\")))
13958         ;; Translate "all" to ".*".
13959         (while (search-forward "all" nil t)
13960           (replace-match ".*" t t))
13961         (goto-char (point-min))
13962         ;; Deal with "not."s.
13963         (if (looking-at "not.")
13964             (progn
13965               (setq not-match t)
13966               (setq regexp (buffer-substring 5 (point-max))))
13967           (setq regexp (buffer-substring 1 (point-max)))
13968           (setq not-match nil))
13969         ;; Finally - if this resulting regexp matches the group name,
13970         ;; we add this score file to the list of score files
13971         ;; applicable to this group.
13972         (if (or (and not-match
13973                      (not (string-match regexp group)))
13974                 (and (not not-match)
13975                      (string-match regexp group)))
13976             (setq ofiles (cons (car sfiles) ofiles)))
13977         (setq sfiles (cdr sfiles)))
13978       (kill-buffer (current-buffer))
13979       ;; Slight kludge here - the last score file returned should be
13980       ;; the local score file, whether it exists or not. This is so
13981       ;; that any score commands the user enters will go to the right
13982       ;; file, and not end up in some global score file.
13983       (let ((localscore
13984              (expand-file-name
13985               (if (gnus-use-long-file-name 'not-score)
13986                   (concat gnus-kill-files-directory group "." 
13987                           gnus-score-file-suffix)
13988                 (concat gnus-kill-files-directory
13989                         (gnus-replace-chars-in-string group ?. ?/ ?: ?/)
13990                         "/" gnus-score-file-suffix)))))
13991         (and (member localscore ofiles)
13992              (delete localscore ofiles))
13993         (setq ofiles (cons localscore ofiles)))
13994       (nreverse ofiles))))
13995
13996 (defun gnus-score-find-single (group)
13997   "Return list containing the score file for GROUP."
13998   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13999         (gnus-score-file-name group)))
14000
14001 (defun gnus-score-find-hierarchical (group)
14002   "Return list of score files for GROUP.
14003 This includes the score file for the group and all its parents."
14004   (let ((all (copy-sequence '(nil)))
14005         (start 0))
14006     (while (string-match "\\." group (1+ start))
14007       (setq start (match-beginning 0))
14008       (setq all (cons (substring group 0 start) all)))
14009     (setq all (cons group all))
14010     (nconc
14011      (mapcar (lambda (newsgroup)
14012                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
14013              (setq all (nreverse all)))
14014      (mapcar 'gnus-score-file-name all))))
14015
14016 (defvar gnus-score-file-alist-cache nil)
14017
14018 (defun gnus-score-find-alist (group)
14019   "Return list of score files for GROUP.
14020 The list is determined from the variable gnus-score-file-alist."
14021   (let ((alist gnus-score-file-multiple-match-alist)
14022         score-files)
14023     ;; if this group has been seen before, return the cached entry
14024     (if (setq score-files (assoc group gnus-score-file-alist-cache))
14025         (cdr score-files)               ;ensures caching groups with no matches
14026       ;; handle the multiple match alist
14027       (while alist
14028         (and (string-match (car (car alist)) group)
14029              (setq score-files
14030                    (nconc score-files (copy-sequence (cdr (car alist))))))
14031         (setq alist (cdr alist)))
14032       (setq alist gnus-score-file-single-match-alist)
14033       ;; handle the single match alist
14034       (while alist
14035         (and (string-match (car (car alist)) group)
14036              ;; progn used just in case ("regexp") has no files
14037              ;; and score-files is still nil. -sj
14038              ;; this can be construed as a "stop searching here" feature :>
14039              ;; and used to simplify regexps in the single-alist 
14040              (progn
14041                (setq score-files
14042                      (nconc score-files (copy-sequence (cdr (car alist)))))
14043                (setq alist nil)))
14044         (setq alist (cdr alist)))
14045       ;; cache the score files
14046       (setq gnus-score-file-alist-cache
14047             (cons (cons group score-files) gnus-score-file-alist-cache))
14048       score-files)))
14049
14050
14051 (defun gnus-possibly-score-headers (&optional trace)
14052   (let ((func gnus-score-find-score-files-function)
14053         score-files)
14054     (and func (not (listp func))
14055          (setq func (list func)))
14056     ;; Go through all the functions for finding score files (or actual
14057     ;; scores) and add them to a list.
14058     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
14059     (while func
14060       (and (symbolp (car func))
14061            (fboundp (car func))
14062            (setq score-files 
14063                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
14064       (setq func (cdr func)))
14065     (if score-files (gnus-score-headers score-files trace))))
14066
14067 (defun gnus-score-file-name (newsgroup &optional suffix)
14068   "Return the name of a score file for NEWSGROUP."
14069   (let ((suffix (or suffix gnus-score-file-suffix)))
14070     (cond 
14071      ((or (null newsgroup)
14072           (string-equal newsgroup ""))
14073       ;; The global score file is placed at top of the directory.
14074       (expand-file-name 
14075        suffix (or gnus-kill-files-directory "~/News")))
14076      ((gnus-use-long-file-name 'not-score)
14077       ;; Append ".SCORE" to newsgroup name.
14078       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14079                                 "." suffix)
14080                         (or gnus-kill-files-directory "~/News")))
14081      (t
14082       ;; Place "SCORE" under the hierarchical directory.
14083       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14084                                 "/" suffix)
14085                         (or gnus-kill-files-directory "~/News"))))))
14086
14087 (defun gnus-score-search-global-directories (files)
14088   "Scan all global score directories for score files."
14089   ;; Set the variable `gnus-internal-global-score-files' to all
14090   ;; available global score files.
14091   (interactive (list gnus-global-score-files))
14092   (let (out)
14093     (while files
14094       (if (string-match "/$" (car files))
14095           (setq out (nconc (directory-files 
14096                             (car files) t
14097                             (concat (gnus-score-file-regexp) "$"))))
14098         (setq out (cons (car files) out)))
14099       (setq files (cdr files)))
14100     (setq gnus-internal-global-score-files out)))
14101
14102 ;; Allow redefinition of Gnus functions.
14103
14104 (gnus-ems-redefine)
14105
14106 (provide 'gnus)
14107
14108 ;;; gnus.el ends here