*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-ni, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [article 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer))
731   "Mapping from short symbols to buffer names or buffer variables.")
732
733 (defvar gnus-carpal nil
734   "*If non-nil, display clickable icons.")
735
736 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
737   "*Function called with a group name when new group is detected.
738 A few pre-made functions are supplied: `gnus-subscribe-randomly'
739 inserts new groups at the beginning of the list of groups;
740 `gnus-subscribe-alphabetically' inserts new groups in strict
741 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
742 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
743 for your decision.")
744
745 ;; Suggested by a bug report by Hallvard B Furuseth.
746 ;; <h.b.furuseth@usit.uio.no>. 
747 (defvar gnus-subscribe-options-newsgroup-method
748   (function gnus-subscribe-alphabetically)
749   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
750 If, for instance, you want to subscribe to all newsgroups in the
751 \"no\" and \"alt\" hierarchies, you'd put the following in your
752 .newsrc file:
753
754 options -n no.all alt.all
755
756 Gnus will the subscribe all new newsgroups in these hierarchies with
757 the subscription method in this variable.")
758
759 (defvar gnus-subscribe-hierarchical-interactive nil
760   "*If non-nil, Gnus will offer to subscribe hierarchically.
761 When a new hierarchy appears, Gnus will ask the user:
762
763 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
764
765 If the user pressed `d', Gnus will descend the hierarchy, `y' will
766 subscribe to all newsgroups in the hierarchy and `s' will skip this
767 hierarchy in its entirety.")
768
769 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
770   "*Function used for sorting the group buffer.
771 This function will be called with group info entries as the arguments
772 for the groups to be sorted.  Pre-made functions include
773 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
774 `gnus-sort-by-level'")
775
776 ;; Mark variables suggested by Thomas Michanek
777 ;; <Thomas.Michanek@telelogic.se>. 
778 (defvar gnus-unread-mark ? 
779   "*Mark used for unread articles.")
780 (defvar gnus-ticked-mark ?!
781   "*Mark used for ticked articles.")
782 (defvar gnus-dormant-mark ??
783   "*Mark used for dormant articles.")
784 (defvar gnus-del-mark ?D
785   "*Mark used for del'd articles.")
786 (defvar gnus-read-mark ?d
787   "*Mark used for read articles.")
788 (defvar gnus-expirable-mark ?E
789   "*Mark used for expirable articles.")
790 (defvar gnus-killed-mark ?K
791   "*Mark used for killed articles.")
792 (defvar gnus-kill-file-mark ?X
793   "*Mark used for articles killed by kill files.")
794 (defvar gnus-low-score-mark ?Y
795   "*Mark used for articles with a low score.")
796 (defvar gnus-catchup-mark ?C
797   "*Mark used for articles that are caught up.")
798 (defvar gnus-replied-mark ?R
799   "*Mark used for articles that have been replied to.")
800 (defvar gnus-process-mark ?# 
801   "*Process mark.")
802 (defvar gnus-ancient-mark ?A
803   "*Mark used for ancient articles.")
804 (defvar gnus-canceled-mark ?G
805   "*Mark used for canceled articles.")
806 (defvar gnus-score-over-mark ?+
807   "*Score mark used for articles with high scores.")
808 (defvar gnus-score-below-mark ?-
809   "*Score mark used for articles with low scores.")
810 (defvar gnus-empty-thread-mark ? 
811   "*There is no thread under the article.")
812 (defvar gnus-not-empty-thread-mark ?=
813   "*There is a thread under the article.")
814 (defvar gnus-dummy-mark ?Z
815   "*This is a dummy article.")
816
817 (defvar gnus-view-pseudo-asynchronously nil
818   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
819
820 (defvar gnus-view-pseudos nil
821   "*If `automatic', pseudo-articles will be viewed automatically.
822 If `not-confirm', pseudos will be viewed automatically, and the user
823 will not be asked to confirm the command.")
824
825 (defvar gnus-view-pseudos-separately t
826   "*If non-nil, one pseudo-article will be created for each file to be viewed.
827 If nil, all files that use the same viewing command will be given as a
828 list of parameters to that command.")
829
830 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
831   "*Format of group lines.
832 It works along the same lines as a normal formatting string,
833 with some simple extensions.
834
835 %M    Only marked articles (character, \"*\" or \" \")
836 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
837 %L    Level of subscribedness (integer)
838 %N    Number of unread articles (integer)
839 %I    Number of dormant articles (integer)
840 %i    Number of ticked and dormant (integer)
841 %T    Number of ticked articles (integer)
842 %R    Number of read articles (integer)
843 %t    Total number of articles (integer)
844 %y    Number of unread, unticked articles (integer)
845 %G    Group name (string)
846 %g    Qualified group name (string)
847 %D    Group description (string)
848 %s    Select method (string)
849 %o    Moderated group (char, \"m\")
850 %p    Process mark (char)
851 %O    Moderated group (string, \"(m)\" or \"\")
852 %n    Select from where (string)
853 %z    A string that look like `<%s:%n>' if a foreign select method is used
854 %u    User defined specifier. The next character in the format string should
855       be a letter.  Gnus will call the function gnus-user-format-function-X,
856       where X is the letter following %u. The function will be passed the
857       current header as argument. The function should return a string, which
858       will be inserted into the buffer just like information from any other
859       group specifier.
860
861 Text between %( and %) will be highlighted with `gnus-mouse-face' when
862 the mouse point move inside the area.  There can only be one such area.
863
864 Note that this format specification is not always respected. For
865 reasons of efficiency, when listing killed groups, this specification
866 is ignored altogether. If the spec is changed considerably, your
867 output may end up looking strange when listing both alive and killed
868 groups.
869
870 If you use %o or %O, reading the active file will be slower and quite
871 a bit of extra memory will be used. %D will also worsen performance.
872 Also note that if you change the format specification to include any
873 of these specs, you must probably re-start Gnus to see them go into
874 effect.") 
875
876 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
877   "*The format specification of the lines in the summary buffer.
878
879 It works along the same lines as a normal formatting string,
880 with some simple extensions.
881
882 %N   Article number, left padded with spaces (string)
883 %S   Subject (string)
884 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
885 %n   Name of the poster (string)
886 %A   Address of the poster (string)
887 %F   Contents of the From: header (string)
888 %x   Contents of the Xref: header (string)
889 %D   Date of the article (string)
890 %d   Date of the article (string) in DD-MMM format
891 %M   Message-id of the article (string)
892 %r   References of the article (string)
893 %c   Number of characters in the article (integer)
894 %L   Number of lines in the article (integer)
895 %I   Indentation based on thread level (a string of spaces)
896 %T   A string with two possible values: 80 spaces if the article
897      is on thread level two or larger and 0 spaces on level one
898 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
899 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
900 %[   Opening bracket (character, \"[\" or \"<\")
901 %]   Closing bracket (character, \"]\" or \">\")
902 %>   Spaces of length thread-level (string)
903 %<   Spaces of length (- 20 thread-level) (string)
904 %i   Article score (number)
905 %z   Article zcore (character)
906 %t   Number of articles under the current thread (number).
907 %e   Whether the thread is empty or not (character).
908 %u   User defined specifier. The next character in the format string should
909      be a letter.  Gnus will call the function gnus-user-format-function-X,
910      where X is the letter following %u. The function will be passed the
911      current header as argument. The function should return a string, which
912      will be inserted into the summary just like information from any other
913      summary specifier.
914
915 Text between %( and %) will be highlighted with `gnus-mouse-face'
916 when the mouse point is placed inside the area.  There can only be one
917 such area.
918
919 The %U (status), %R (replied) and %z (zcore) specs have to be handled
920 with care. For reasons of efficiency, Gnus will compute what column
921 these characters will end up in, and \"hard-code\" that. This means that
922 it is illegal to have these specs after a variable-length spec. Well,
923 you might not be arrested, but your summary buffer will look strange,
924 which is bad enough.
925
926 The smart choice is to have these specs as for to the left as
927 possible. 
928
929 This restriction may disappear in later versions of Gnus.")
930
931 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
932   "*The format specification for the dummy roots in the summary buffer.
933 It works along the same lines as a normal formatting string,
934 with some simple extensions.
935
936 %S  The subject")
937
938 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
939   "*The format specification for the summary mode line.")
940
941 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
942   "*The format specification for the article mode line.")
943
944 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
945   "*The format specification for the group mode line.")
946
947 (defvar gnus-valid-select-methods
948   '(("nntp" post address prompt-address)
949     ("nnspool" post)
950     ("nnvirtual" none virtual prompt-address) 
951     ("nnmbox" mail respool) 
952     ("nnml" mail respool)
953     ("nnmh" mail respool) 
954     ("nndir" none prompt-address address)
955     ("nneething" none prompt-address)
956     ("nndigest" none) 
957     ("nndoc" none prompt-address) 
958     ("nnbabyl" mail respool) 
959     ("nnkiboze" post virtual) 
960     ("nnsoup" post)
961     ("nnfolder" mail respool))
962   "An alist of valid select methods.
963 The first element of each list lists should be a string with the name
964 of the select method. The other elements may be be the category of
965 this method (ie. `post', `mail', `none' or whatever) or other
966 properties that this method has (like being respoolable).
967 If you implement a new select method, all you should have to change is
968 this variable. I think.")
969
970 (defvar gnus-updated-mode-lines '(group article summary)
971   "*List of buffers that should update their mode lines.
972 The list may contain the symbols `group', `article' and `summary'. If
973 the corresponding symbol is present, Gnus will keep that mode line
974 updated with information that may be pertinent. 
975 If this variable is nil, screen refresh may be quicker.")
976
977 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
978 (defvar gnus-mode-non-string-length 21
979   "*Max length of mode-line non-string contents.
980 If this is nil, Gnus will take space as is needed, leaving the rest
981 of the modeline intact.")
982
983 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
984 (defvar gnus-mouse-face 'highlight
985   "*Face used for mouse highlighting in Gnus.
986 No mouse highlights will be done if `gnus-visual' is nil.")
987
988 (defvar gnus-summary-mark-below nil
989   "*Mark all articles with a score below this variable as read.
990 This variable is local to each summary buffer and usually set by the
991 score file.")  
992
993 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
994   "*List of functions used for sorting threads in the summary buffer.
995 By default, threads are sorted by article number.
996
997 Each function takes two threads and return non-nil if the first thread
998 should be sorted before the other.  If you use more than one function,
999 the primary sort function should be the last.
1000
1001 Ready-mady functions include `gnus-thread-sort-by-number',
1002 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1003 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1004 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1005
1006 (defvar gnus-thread-score-function '+
1007   "*Function used for calculating the total score of a thread.
1008
1009 The function is called with the scores of the article and each
1010 subthread and should then return the score of the thread.
1011
1012 Some functions you can use are `+', `max', or `min'.")
1013
1014 (defvar gnus-options-subscribe nil
1015   "*All new groups matching this regexp will be subscribed unconditionally.
1016 Note that this variable deals only with new newsgroups.  This variable
1017 does not affect old newsgroups.")
1018
1019 (defvar gnus-options-not-subscribe nil
1020   "*All new groups matching this regexp will be ignored.
1021 Note that this variable deals only with new newsgroups.  This variable
1022 does not affect old (already subscribed) newsgroups.")
1023
1024 (defvar gnus-auto-expirable-newsgroups nil
1025   "*Groups in which to automatically mark read articles as expirable.
1026 If non-nil, this should be a regexp that should match all groups in
1027 which to perform auto-expiry.  This only makes sense for mail groups.")
1028
1029 (defvar gnus-hidden-properties '(invisible t intangible t)
1030   "Property list to use for hiding text.")
1031
1032 ;; Hooks.
1033
1034 (defvar gnus-group-mode-hook nil
1035   "*A hook for Gnus group mode.")
1036
1037 (defvar gnus-summary-mode-hook nil
1038   "*A hook for Gnus summary mode.
1039 This hook is run before any variables are set in the summary buffer.")
1040
1041 (defvar gnus-article-mode-hook nil
1042   "*A hook for Gnus article mode.")
1043
1044 (defvar gnus-open-server-hook nil
1045   "*A hook called just before opening connection to the news server.")
1046
1047 (defvar gnus-startup-hook nil
1048   "*A hook called at startup.
1049 This hook is called after Gnus is connected to the NNTP server.")
1050
1051 (defvar gnus-get-new-news-hook nil
1052   "*A hook run just before Gnus checks for new news.")
1053
1054 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1055   "*A function that is called to generate the group buffer.
1056 The function is called with three arguments: The first is a number;
1057 all group with a level less or equal to that number should be listed,
1058 if the second is non-nil, empty groups should also be displayed. If
1059 the third is non-nil, it is a number. No groups with a level lower
1060 than this number should be displayed.
1061
1062 The only current function implemented is `gnus-group-prepare-flat'.")
1063
1064 (defvar gnus-group-prepare-hook nil
1065   "*A hook called after the group buffer has been generated.
1066 If you want to modify the group buffer, you can use this hook.")
1067
1068 (defvar gnus-summary-prepare-hook nil
1069   "*A hook called after the summary buffer has been generated.
1070 If you want to modify the summary buffer, you can use this hook.")
1071
1072 (defvar gnus-article-prepare-hook nil
1073   "*A hook called after an article has been prepared in the article buffer.
1074 If you want to run a special decoding program like nkf, use this hook.")
1075
1076 (defvar gnus-article-display-hook nil
1077   "*A hook called after the article is displayed in the article buffer.
1078 The hook is designed to change the contents of the article
1079 buffer. Typical functions that this hook may contain are
1080 `gnus-article-hide-headers' (hide selected headers),
1081 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1082 `gnus-article-hide-signature' (hide signature) and
1083 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1084 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1085 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1086
1087 (defvar gnus-article-x-face-command
1088   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1089   "String or function to be executed to display an X-Face header.
1090 If it is a string, the command will be executed in a sub-shell
1091 asynchronously. The compressed face will be piped to this command.") 
1092
1093 (defvar gnus-article-x-face-too-ugly nil
1094   "Regexp matching posters whose face shouldn't be shown automatically.")
1095
1096 (defvar gnus-select-group-hook nil
1097   "*A hook called when a newsgroup is selected.
1098
1099 If you'd like to simplify subjects like the
1100 `gnus-summary-next-same-subject' command does, you can use the
1101 following hook:
1102
1103  (setq gnus-select-group-hook
1104       (list
1105         (lambda ()
1106           (mapcar (lambda (header)
1107                      (header-set-subject
1108                       header
1109                       (gnus-simplify-subject
1110                        (header-subject header) 're-only)))
1111                   gnus-newsgroup-headers))))")
1112
1113 (defvar gnus-select-article-hook
1114   '(gnus-summary-show-thread)
1115   "*A hook called when an article is selected.
1116 The default hook shows conversation thread subtrees of the selected
1117 article automatically using `gnus-summary-show-thread'.")
1118
1119 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1120   "*A hook called to apply kill files to a group.
1121 This hook is intended to apply a kill file to the selected newsgroup.
1122 The function `gnus-apply-kill-file' is called by default.
1123
1124 Since a general kill file is too heavy to use only for a few
1125 newsgroups, I recommend you to use a lighter hook function. For
1126 example, if you'd like to apply a kill file to articles which contains
1127 a string `rmgroup' in subject in newsgroup `control', you can use the
1128 following hook:
1129
1130 \(setq gnus-apply-kill-hook
1131       (list
1132         (lambda ()
1133           (cond ((string-match \"control\" gnus-newsgroup-name)
1134                  (gnus-kill \"Subject\" \"rmgroup\")
1135                  (gnus-expunge \"X\"))))))")
1136
1137 (defvar gnus-visual-mark-article-hook 
1138   (list 'gnus-highlight-selected-summary)
1139   "*Hook run after selecting an article in the summary buffer.
1140 It is meant to be used for highlighting the article in some way.  It
1141 is not run if `gnus-visual' is nil.")
1142
1143 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1144   "*A hook called after preparing body, but before preparing header headers.
1145 The default hook (`gnus-inews-insert-signature') inserts a signature
1146 file specified by the variable `gnus-signature-file'.")
1147
1148 (defvar gnus-exit-group-hook nil
1149   "*A hook called when exiting (not quitting) summary mode.")
1150
1151 (defvar gnus-suspend-gnus-hook nil
1152   "*A hook called when suspending (not exiting) Gnus.")
1153
1154 (defvar gnus-exit-gnus-hook nil
1155   "*A hook called when exiting Gnus.")
1156
1157 (defvar gnus-save-newsrc-hook nil
1158   "*A hook called when saving the newsrc file.")
1159
1160 (defvar gnus-summary-update-hook 
1161   (list 'gnus-summary-highlight-line)
1162   "*A hook called when a summary line is changed.
1163 The hook will not be called if `gnus-visual' is nil.
1164
1165 The default function `gnus-summary-highlight-line' will
1166 highlight the line according to the `gnus-summary-highlight'
1167 variable.")
1168
1169 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1170   "*A hook called when an article is selected for the first time.
1171 The hook is intended to mark an article as read (or unread)
1172 automatically when it is selected.")
1173
1174 \f
1175 ;; Internal variables
1176
1177 ;; Avoid highlighting in kill files.
1178 (defvar gnus-summary-inhibit-highlight nil)
1179 (defvar gnus-newsgroup-selected-overlay nil)
1180
1181 (defvar gnus-article-mode-map nil)
1182 (defvar caesar-translate-table nil)
1183 (defvar gnus-dribble-buffer nil)
1184 (defvar gnus-headers-retrieved-by nil)
1185 (defvar gnus-article-reply nil)
1186 (defvar gnus-override-method nil)
1187 (defvar gnus-article-check-size nil)
1188
1189 (defvar gnus-current-score-file nil)
1190 (defvar gnus-internal-global-score-files nil)
1191 (defvar gnus-score-file-list nil)
1192
1193
1194 (defvar gnus-current-move-group nil)
1195
1196 (defvar gnus-newsgroup-dependencies nil)
1197 (defvar gnus-newsgroup-threads nil)
1198 (defvar gnus-newsgroup-async nil)
1199 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1200
1201 (defvar gnus-newsgroup-adaptive nil)
1202
1203 (defvar gnus-summary-display-table nil)
1204
1205 (defconst gnus-group-line-format-alist
1206   (list (list ?M 'marked ?c)
1207         (list ?S 'subscribed ?c)
1208         (list ?L 'level ?d)
1209         (list ?N 'number ?s)
1210         (list ?I 'number-of-dormant ?d)
1211         (list ?T 'number-of-ticked ?d)
1212         (list ?R 'number-of-read ?s)
1213         (list ?t 'number-total ?d)
1214         (list ?y 'number-of-unread-unticked ?s)
1215         (list ?i 'number-of-ticked-and-dormant ?d)
1216         (list ?g 'group ?s)
1217         (list ?G 'qualified-group ?s)
1218         (list ?D 'newsgroup-description ?s)
1219         (list ?o 'moderated ?c)
1220         (list ?O 'moderated-string ?s)
1221         (list ?p 'process-marked ?c)
1222         (list ?s 'news-server ?s)
1223         (list ?n 'news-method ?s)
1224         (list ?z 'news-method-string ?s)
1225         (list ?u 'user-defined ?s)))
1226
1227 (defconst gnus-summary-line-format-alist 
1228   (list (list ?N 'number ?d)
1229         (list ?S 'subject ?s)
1230         (list ?s 'subject-or-nil ?s)
1231         (list ?n 'name ?s)
1232         (list ?A 'address ?s)
1233         (list ?F 'from ?s)
1234         (list ?x (macroexpand '(header-xref header)) ?s)
1235         (list ?D (macroexpand '(header-date header)) ?s)
1236         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1237         (list ?M (macroexpand '(header-id header)) ?s)
1238         (list ?r (macroexpand '(header-references header)) ?s)
1239         (list ?c '(or (header-chars header) 0) ?d)
1240         (list ?L 'lines ?d)
1241         (list ?I 'indentation ?s)
1242         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1243         (list ?R 'replied ?c)
1244         (list ?\[ 'opening-bracket ?c)
1245         (list ?\] 'closing-bracket ?c)
1246         (list ?\> '(make-string level ? ) ?s)
1247         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1248         (list ?i 'score ?d)
1249         (list ?z 'score-char ?c)
1250         (list ?U 'unread ?c)
1251         (list ?t '(gnus-summary-number-of-articles-in-thread 
1252                    (or (prog1 gnus-tmp-adopt-thread 
1253                          (setq gnus-tmp-adopt-thread nil))
1254                        (if (boundp 'thread) (symbol-value 'thread)
1255                          thread nil)))
1256                    ?d)
1257         (list ?e '(gnus-summary-number-of-articles-in-thread 
1258                    (or gnus-tmp-adopt-thread 
1259                        (if (boundp 'thread) (symbol-value 'thread)
1260                          thread nil)) t)
1261                    ?c)
1262         (list ?u 'user-defined ?s))
1263   "An alist of format specifications that can appear in summary lines,
1264 and what variables they correspond with, along with the type of the
1265 variable (string, integer, character, etc).")
1266
1267 (defconst gnus-summary-dummy-line-format-alist
1268   (list (list ?S 'subject ?s)
1269         (list ?N 'number ?d)
1270         (list ?u 'user-defined ?s)))
1271
1272 (defconst gnus-summary-mode-line-format-alist 
1273   (list (list ?G 'group-name ?s)
1274         (list ?g '(gnus-short-group-name group-name) ?s)
1275         (list ?A 'article-number ?d)
1276         (list ?Z 'unread-and-unselected ?s)
1277         (list ?V 'gnus-version ?s)
1278         (list ?U 'unread ?d)
1279         (list ?S 'subject ?s)
1280         (list ?e 'unselected ?d)
1281         (list ?u 'user-defined ?s)
1282         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1283
1284 (defconst gnus-group-mode-line-format-alist 
1285   (list (list ?S 'news-server ?s)
1286         (list ?M 'news-method ?s)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defvar gnus-have-read-active-file nil)
1290
1291 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1292   "The mail address of the Gnus maintainers.")
1293
1294 (defconst gnus-version "(ding) Gnus v0.89"
1295   "Version number for this version of Gnus.")
1296
1297 (defvar gnus-info-nodes
1298   '((gnus-group-mode            "(gnus)The Group Buffer")
1299     (gnus-summary-mode          "(gnus)The Summary Buffer")
1300     (gnus-article-mode          "(gnus)The Article Buffer"))
1301   "Assoc list of major modes and related Info nodes.")
1302
1303 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1304   "The location of the (ding) Gnus documentation group.")
1305
1306 (defvar gnus-group-buffer "*Group*")
1307 (defvar gnus-summary-buffer "*Summary*")
1308 (defvar gnus-article-buffer "*Article*")
1309 (defvar gnus-server-buffer "*Server*")
1310
1311 (defvar gnus-work-buffer " *gnus work*")
1312
1313 (defvar gnus-buffer-list nil
1314   "Gnus buffers that should be killed on exit.")
1315
1316 (defvar gnus-server-alist nil
1317   "List of available servers.")
1318
1319 (defvar gnus-variable-list
1320   '(gnus-newsrc-options gnus-newsrc-options-n
1321     gnus-newsrc-last-checked-date 
1322     gnus-newsrc-alist gnus-server-alist
1323     gnus-killed-list gnus-zombie-list)
1324   "Gnus variables saved in the quick startup file.")
1325
1326 (defvar gnus-overload-functions
1327   '((news-inews gnus-inews-news "rnewspost")
1328     (caesar-region gnus-caesar-region "rnews"))
1329   "Functions overloaded by gnus.
1330 It is a list of `(original overload &optional file)'.")
1331
1332 (defvar gnus-newsrc-options nil
1333   "Options line in the .newsrc file.")
1334
1335 (defvar gnus-newsrc-options-n nil
1336   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1337
1338 (defvar gnus-newsrc-last-checked-date nil
1339   "Date Gnus last asked server for new newsgroups.")
1340
1341 (defvar gnus-newsrc-alist nil
1342   "Assoc list of read articles.
1343 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1344
1345 (defvar gnus-newsrc-hashtb nil
1346   "Hashtable of gnus-newsrc-alist.")
1347
1348 (defvar gnus-killed-list nil
1349   "List of killed newsgroups.")
1350
1351 (defvar gnus-killed-hashtb nil
1352   "Hash table equivalent of gnus-killed-list.")
1353
1354 (defvar gnus-zombie-list nil
1355   "List of almost dead newsgroups.")
1356
1357 (defvar gnus-description-hashtb nil
1358   "Descriptions of newsgroups.")
1359
1360 (defvar gnus-list-of-killed-groups nil
1361   "List of newsgroups that have recently been killed by the user.")
1362
1363 (defvar gnus-active-hashtb nil
1364   "Hashtable of active articles.")
1365
1366 (defvar gnus-moderated-list nil
1367   "List of moderated newsgroups.")
1368
1369 (defvar gnus-group-marked nil)
1370
1371 (defvar gnus-current-startup-file nil
1372   "Startup file for the current host.")
1373
1374 (defvar gnus-last-search-regexp nil
1375   "Default regexp for article search command.")
1376
1377 (defvar gnus-last-shell-command nil
1378   "Default shell command on article.")
1379
1380 (defvar gnus-current-select-method nil
1381   "The current method for selecting a newsgroup.")
1382
1383 (defvar gnus-have-all-newsgroups nil)
1384
1385 (defvar gnus-article-internal-prepare-hook nil)
1386
1387 (defvar gnus-newsgroup-name nil)
1388 (defvar gnus-newsgroup-begin nil)
1389 (defvar gnus-newsgroup-end nil)
1390 (defvar gnus-newsgroup-last-rmail nil)
1391 (defvar gnus-newsgroup-last-mail nil)
1392 (defvar gnus-newsgroup-last-folder nil)
1393 (defvar gnus-newsgroup-last-file nil)
1394 (defvar gnus-newsgroup-auto-expire nil)
1395 (defvar gnus-newsgroup-active nil)
1396
1397 (defvar gnus-newsgroup-unreads nil
1398   "List of unread articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-unselected nil
1401   "List of unselected unread articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-marked nil
1404   "List of ticked articles in the current newsgroup (a subset of unread art).")
1405
1406 (defvar gnus-newsgroup-killed nil
1407   "List of ranges of articles that have been through the scoring process.")
1408
1409 (defvar gnus-newsgroup-kill-headers nil)
1410
1411 (defvar gnus-newsgroup-replied nil
1412   "List of articles that have been replied to in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-expirable nil
1415   "List of articles in the current newsgroup that can be expired.")
1416
1417 (defvar gnus-newsgroup-processable nil
1418   "List of articles in the current newsgroup that can be processed.")
1419
1420 (defvar gnus-newsgroup-bookmarks nil
1421   "List of articles in the current newsgroup that have bookmarks.")
1422
1423 (defvar gnus-newsgroup-dormant nil
1424   "List of dormant articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-scored nil
1427   "List of scored articles in the current newsgroup.")
1428
1429 (defvar gnus-newsgroup-headers nil
1430   "List of article headers in the current newsgroup.")
1431 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1432
1433 (defvar gnus-newsgroup-ancient nil
1434   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1435
1436 (defvar gnus-current-article nil)
1437 (defvar gnus-article-current nil)
1438 (defvar gnus-current-headers nil)
1439 (defvar gnus-have-all-headers nil)
1440 (defvar gnus-last-article nil)
1441 (defvar gnus-newsgroup-history nil)
1442 (defvar gnus-current-kill-article nil)
1443
1444 ;; Save window configuration.
1445 (defvar gnus-prev-winconf nil)
1446
1447 ;; Format specs
1448 (defvar gnus-summary-line-format-spec nil)
1449 (defvar gnus-summary-dummy-line-format-spec nil)
1450 (defvar gnus-group-line-format-spec nil)
1451 (defvar gnus-summary-mode-line-format-spec nil)
1452 (defvar gnus-article-mode-line-format-spec nil)
1453 (defvar gnus-group-mode-line-format-spec nil)
1454 (defvar gnus-summary-mark-positions nil)
1455
1456 (defvar gnus-summary-expunge-below nil)
1457 (defvar gnus-reffed-article-number nil)
1458
1459 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1460 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1461
1462 (defconst gnus-summary-local-variables 
1463   '(gnus-newsgroup-name 
1464     gnus-newsgroup-begin gnus-newsgroup-end 
1465     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1466     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1467     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1468     gnus-newsgroup-unselected gnus-newsgroup-marked
1469     gnus-newsgroup-replied gnus-newsgroup-expirable
1470     gnus-newsgroup-processable gnus-newsgroup-killed
1471     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1472     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1473     gnus-current-article gnus-current-headers gnus-have-all-headers
1474     gnus-last-article gnus-article-internal-prepare-hook
1475     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1476     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1477     gnus-newsgroup-threads gnus-newsgroup-async
1478     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1479     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1480     gnus-newsgroup-history gnus-newsgroup-ancient
1481     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring))
1482   "Variables that are buffer-local to the summary buffers.")
1483
1484 (defconst gnus-bug-message
1485   "Sending a bug report to the Gnus Towers.
1486 ========================================
1487
1488 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1489 be sent to the Gnus Bug Exterminators. 
1490
1491 At the bottom of the buffer you'll see lots of variable settings.
1492 Please do not delete those.  They will tell the Bug People what your
1493 environment is, so that it will be easier to locate the bugs.
1494
1495 If you have found a bug that makes Emacs go \"beep\", set
1496 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1497 and include the backtrace in your bug report.
1498
1499 Please describe the bug in annoying, painstaking detail.
1500
1501 Thank you for your help in stamping out bugs.
1502 ")
1503
1504 ;;; End of variables.
1505
1506 ;; Define some autoload functions Gnus might use.
1507 (eval-and-compile
1508
1509   ;; Various 
1510   (autoload 'metamail-buffer "metamail")
1511   (autoload 'Info-goto-node "info")
1512   (autoload 'hexl-hex-string-to-integer "hexl")
1513   (autoload 'pp "pp")
1514   (autoload 'pp-to-string "pp")
1515   (autoload 'pp-eval-expression "pp")
1516   (autoload 'mail-extract-address-components "mail-extr")
1517
1518   (autoload 'nnmail-split-fancy "nnmail")
1519   (autoload 'nnvirtual-catchup-group "nnvirtual")
1520
1521   ;; timezone
1522   (autoload 'timezone-make-date-arpa-standard "timezone")
1523   (autoload 'timezone-fix-time "timezone")
1524   (autoload 'timezone-make-sortable-date "timezone")
1525   (autoload 'timezone-make-time-string "timezone")
1526
1527   ;; rmail & friends
1528   (autoload 'mail-position-on-field "sendmail")
1529   (autoload 'mail-setup "sendmail")
1530   (autoload 'rmail-output "rmailout")
1531   (autoload 'news-mail-other-window "rnewspost")
1532   (autoload 'news-reply-yank-original "rnewspost")
1533   (autoload 'news-caesar-buffer-body "rnewspost")
1534   (autoload 'rmail-insert-rmail-file-header "rmail")
1535   (autoload 'rmail-count-new-messages "rmail")
1536   (autoload 'rmail-show-message "rmail")
1537
1538   ;; gnus-soup
1539   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1540   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1541   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1542   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1543   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1544   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1545   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1546
1547   ;; gnus-mh
1548   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1549   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1550   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1551   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1552   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1553   (autoload 'gnus-Folder-save-name "gnus-mh")
1554   (autoload 'gnus-folder-save-name "gnus-mh")
1555
1556   ;; gnus-vis misc
1557   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1558   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1559   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1560   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1561   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1562   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1563   (autoload 'gnus-summary-highlight-line "gnus-vis")
1564   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1565
1566   ;; gnus-vis article
1567   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1568   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1569   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1570   (autoload 'gnus-article-hide "gnus-vis" nil t)
1571   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1572   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1573   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1574   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1575   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1576   (autoload 'gnus-article-add-button "gnus-vis")
1577
1578   ;; gnus-cite
1579   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1580   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1581   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1582
1583   ;; gnus-kill
1584   (autoload 'gnus-kill "gnus-kill")
1585   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1586   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1587   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1588   (autoload 'gnus-execute "gnus-kill")
1589   (autoload 'gnus-expunge "gnus-kill")
1590
1591   ;; gnus-cache
1592   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1593   (autoload 'gnus-cache-save-buffers "gnus-cache")
1594   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1595   (autoload 'gnus-cache-request-article "gnus-cache")
1596   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1597   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1598   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1599   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1600
1601   ;; gnus-score
1602   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1603   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1604   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1605   (autoload 'gnus-score-save "gnus-score")
1606   (autoload 'gnus-score-headers "gnus-score")
1607   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1608   (autoload 'gnus-score-adaptive "gnus-score")
1609   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1610   (autoload 'gnus-score-find-trace "gnus-score")
1611
1612   ;; gnus-edit
1613   (autoload 'gnus-score-customize "gnus-edit" nil t)
1614
1615   ;; gnus-uu
1616   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1617   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1618   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1619   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1620   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1621   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1622   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1623   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1624   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1625   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1626   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1627   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1628   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1629   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1630   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1631   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1632   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1633   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1634   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1635   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1636   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1637   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1638
1639   ;; gnus-msg
1640   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1641   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1642   (autoload 'gnus-group-mail "gnus-msg" nil t)
1643   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1644   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1645   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1646   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1647   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1648   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1649   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1650   (autoload 'gnus-post-news "gnus-msg" nil t)
1651   (autoload 'gnus-inews-news "gnus-msg" nil t)
1652   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1653   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1654   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1655   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1656   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1657   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1658   (autoload 'gnus-mail-yank-original "gnus-msg")
1659   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1660   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1661   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1662   (autoload 'gnus-article-mail-with-original "gnus-msg")
1663   (autoload 'gnus-article-mail "gnus-msg")
1664   (autoload 'gnus-bug "gnus-msg")
1665
1666   ;; gnus-vm
1667   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1668   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1669   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1670   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1671   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1672   (autoload 'gnus-yank-article "gnus-vm" nil t)
1673
1674   )
1675
1676 \f
1677
1678 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1679 ;; If you want the cursor to go somewhere else, set these two
1680 ;; functions in some startup hook to whatever you want.
1681 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1682 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1683
1684 ;;; Various macros and substs.
1685
1686 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1687   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1688   (` (let ((GnusStartBufferWindow (selected-window)))
1689        (unwind-protect
1690            (progn
1691              (pop-to-buffer (, buffer))
1692              (,@ forms))
1693          (select-window GnusStartBufferWindow)))))
1694
1695 (defmacro gnus-gethash (string hashtable)
1696   "Get hash value of STRING in HASHTABLE."
1697   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1698   ;;(` (abbrev-expansion (, string) (, hashtable)))
1699   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1700
1701 (defmacro gnus-sethash (string value hashtable)
1702   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1703   ;; We cannot use define-abbrev since it only accepts string as value.
1704   ;; (set (intern string hashtable) value))
1705   (` (set (intern (, string) (, hashtable)) (, value))))
1706
1707 (defsubst gnus-buffer-substring (beg end)
1708   (buffer-substring (match-beginning beg) (match-end end)))
1709
1710 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1711 ;;   function `substring' might cut on a middle of multi-octet
1712 ;;   character.
1713
1714 (defun gnus-truncate-string (str width)
1715   (substring str 0 width))
1716
1717 (defsubst gnus-simplify-subject-re (subject)
1718   "Remove \"Re:\" from subject lines."
1719   (let ((case-fold-search t))
1720     (if (string-match "^re: *" subject)
1721         (substring subject (match-end 0))
1722       subject)))
1723
1724 (defsubst gnus-goto-char (point)
1725   (and point (goto-char point)))
1726
1727 (defmacro gnus-buffer-exists-p (buffer)
1728   (` (and (, buffer)
1729           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1730                    (, buffer)))))
1731
1732 (defmacro gnus-kill-buffer (buffer)
1733   (` (if (gnus-buffer-exists-p (, buffer))
1734          (kill-buffer (, buffer)))))
1735
1736 (defsubst gnus-point-at-bol ()
1737   "Return point at the beginning of line."
1738   (let ((p (point)))
1739     (beginning-of-line)
1740     (prog1
1741         (point)
1742       (goto-char p))))
1743
1744 (defsubst gnus-point-at-eol ()
1745   "Return point at the beginning of line."
1746   (let ((p (point)))
1747     (end-of-line)
1748     (prog1
1749         (point)
1750       (goto-char p))))
1751
1752 ;; Delete the current line (and the next N lines.);
1753 (defmacro gnus-delete-line (&optional n)
1754   (` (delete-region (progn (beginning-of-line) (point))
1755                     (progn (forward-line (, (or n 1))) (point)))))
1756
1757 ;;; Load the compatability functions. 
1758
1759 (require 'gnus-ems)
1760
1761 \f
1762 ;;;
1763 ;;; Gnus Utility Functions
1764 ;;;
1765
1766 (defun gnus-extract-address-components (from)
1767   (let (name address)
1768     ;; First find the address - the thing with the @ in it.  This may
1769     ;; not be accurate in mail addresses, but does the trick most of
1770     ;; the time in news messages.
1771     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1772         (setq address (substring from (match-beginning 0) (match-end 0))))
1773     ;; Then we check whether the "name <address>" format is used.
1774     (and address
1775          (string-match (concat "<" (regexp-quote address) ">") from)
1776          (and (setq name (substring from 0 (1- (match-beginning 0))))
1777               ;; Strip any quotes from the name.
1778               (string-match "\".*\"" name)
1779               (setq name (substring name 1 (1- (match-end 0))))))
1780     ;; If not, then "address (name)" is used.
1781     (or name
1782         (and (string-match "(.+)" from)
1783              (setq name (substring from (1+ (match-beginning 0)) 
1784                                    (1- (match-end 0)))))
1785         (and (string-match "()" from)
1786              (setq name address))
1787         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1788         ;; XOVER might not support folded From headers.
1789         (and (string-match "(.*" from)
1790              (setq name (substring from (1+ (match-beginning 0)) 
1791                                    (match-end 0)))))
1792     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1793     (list (or name from) (or address from))))
1794
1795 (defun gnus-fetch-field (field)
1796   "Return the value of the header FIELD of current article."
1797   (save-excursion
1798     (save-restriction
1799       (let ((case-fold-search t))
1800         (gnus-narrow-to-headers)
1801         (mail-fetch-field field)))))
1802
1803 (defun gnus-goto-colon ()
1804   (beginning-of-line)
1805   (search-forward ":" (gnus-point-at-eol) t))
1806
1807 (defun gnus-narrow-to-headers ()
1808   (widen)
1809   (save-excursion
1810     (narrow-to-region
1811      (goto-char (point-min))
1812      (if (search-forward "\n\n" nil t)
1813          (1- (point))
1814        (point-max)))))
1815
1816 (defun gnus-update-format-specifications ()
1817   (gnus-make-thread-indent-array)
1818   (setq gnus-summary-line-format-spec 
1819         (gnus-parse-format
1820          gnus-summary-line-format gnus-summary-line-format-alist))
1821   (gnus-update-summary-mark-positions)
1822   (setq gnus-summary-dummy-line-format-spec 
1823         (gnus-parse-format gnus-summary-dummy-line-format 
1824                            gnus-summary-dummy-line-format-alist))
1825   (setq gnus-group-line-format-spec
1826         (gnus-parse-format 
1827          gnus-group-line-format 
1828          gnus-group-line-format-alist))
1829   (if (and (string-match "%D" gnus-group-line-format)
1830            (not gnus-description-hashtb)
1831            gnus-read-active-file)
1832       (gnus-read-all-descriptions-files))
1833   (setq gnus-summary-mode-line-format-spec 
1834         (gnus-parse-format gnus-summary-mode-line-format 
1835                            gnus-summary-mode-line-format-alist))
1836   (setq gnus-article-mode-line-format-spec 
1837         (gnus-parse-format gnus-article-mode-line-format 
1838                            gnus-summary-mode-line-format-alist))
1839   (setq gnus-group-mode-line-format-spec 
1840         (gnus-parse-format gnus-group-mode-line-format 
1841                            gnus-group-mode-line-format-alist)))
1842
1843 (defun gnus-update-summary-mark-positions ()
1844   (save-excursion
1845     (let ((gnus-replied-mark 129)
1846           (gnus-score-below-mark 130)
1847           (gnus-score-over-mark 130)
1848           (thread nil)
1849           pos)
1850       (gnus-set-work-buffer)
1851       (gnus-summary-insert-line 
1852        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1853       (goto-char (point-min))
1854       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1855                                          (- (point) 2)))))
1856       (goto-char (point-min))
1857       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1858                                           (- (point) 2))) pos))
1859       (goto-char (point-min))
1860       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1861                                         (- (point) 2))) pos))
1862       (setq gnus-summary-mark-positions pos))))
1863
1864 (defun gnus-format-max-width (form length)
1865   (let* ((val (eval form))
1866          (valstr (if (numberp val) (int-to-string val) val)))
1867     (if (> (length valstr) length)
1868         (substring valstr 0 length)
1869       valstr)))
1870
1871 (defun gnus-set-mouse-face (string)
1872   ;; Set mouse face property on STRING.
1873   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1874   string)
1875
1876 (defun gnus-parse-format (format spec-alist)
1877   ;; This function parses the FORMAT string with the help of the
1878   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1879   ;; string.  If the FORMAT string contains the specifiers %( and %)
1880   ;; the text between them will have the mouse-face text property.
1881   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1882       (if (and gnus-visual gnus-mouse-face)
1883           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1884                 (button (substring format (match-beginning 2) (match-end 2)))
1885                 (post (substring format (match-beginning 3) (match-end 3))))
1886             (list 'concat
1887                   (gnus-parse-simple-format pre spec-alist)
1888                   (list 'gnus-set-mouse-face
1889                         (gnus-parse-simple-format button spec-alist))
1890                   (gnus-parse-simple-format post spec-alist)))
1891         (gnus-parse-simple-format
1892          (concat (substring format (match-beginning 1) (match-end 1))
1893                  (substring format (match-beginning 2) (match-end 2))
1894                  (substring format (match-beginning 3) (match-end 3)))
1895          spec-alist))
1896     (gnus-parse-simple-format format spec-alist)))
1897
1898 (defun gnus-parse-simple-format (format spec-alist)
1899   ;; This function parses the FORMAT string with the help of the
1900   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1901   ;; string. The list will consist of the symbol `format', a format
1902   ;; specification string, and a list of forms depending on the
1903   ;; SPEC-ALIST.
1904   (let ((max-width 0)
1905         spec flist fstring b newspec max-width elem beg)
1906     (save-excursion
1907       (gnus-set-work-buffer)
1908       (insert format)
1909       (goto-char (point-min))
1910       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1911         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1912                                                      (match-end 2))))
1913         ;; First check if there are any specs that look anything like
1914         ;; "%12,12A", ie. with a "max width specification". These have
1915         ;; to be treated specially.
1916         (if (setq beg (match-beginning 1))
1917             (setq max-width 
1918                   (string-to-int 
1919                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1920           (setq max-width 0)
1921           (setq beg (match-beginning 2)))
1922         ;; Find the specification from `spec-alist'.
1923         (if (not (setq elem (cdr (assq spec spec-alist))))
1924             (setq elem '("*" ?s)))
1925         ;; Treat user defined format specifiers specially
1926         (and (eq (car elem) 'user-defined)
1927              (setq elem
1928                    (list 
1929                     (list (intern (concat "gnus-user-format-function-"
1930                                           (buffer-substring
1931                                            (match-beginning 3)
1932                                            (match-end 3))))
1933                           'header)
1934                     ?s))
1935              (delete-region (match-beginning 3) (match-end 3)))
1936         (if (not (zerop max-width))
1937             (let ((el (car elem)))
1938               (cond ((= (car (cdr elem)) ?c) 
1939                      (setq el (list 'char-to-string el)))
1940                     ((= (car (cdr elem)) ?d)
1941                      (numberp el) (setq el (list 'int-to-string el))))
1942               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1943                                 flist))
1944               (setq newspec ?s))
1945           (setq flist (cons (car elem) flist))
1946           (setq newspec (car (cdr elem))))
1947         ;; Remove the old specification (and possibly a ",12" string).
1948         (delete-region beg (match-end 2))
1949         ;; Insert the new specification.
1950         (goto-char beg)
1951         (insert newspec))
1952       (setq fstring (buffer-substring 1 (point-max))))
1953     (cons 'format (cons fstring (nreverse flist)))))
1954
1955 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1956 (defun gnus-read-init-file ()
1957   (and gnus-init-file
1958        (or (and (file-exists-p gnus-init-file) 
1959                 ;; Don't try to load a directory.
1960                 (not (file-directory-p gnus-init-file)))
1961            (file-exists-p (concat gnus-init-file ".el"))
1962            (file-exists-p (concat gnus-init-file ".elc")))
1963        (load gnus-init-file nil t)))
1964
1965 (defun gnus-set-work-buffer ()
1966   (if (get-buffer gnus-work-buffer)
1967       (progn
1968         (set-buffer gnus-work-buffer)
1969         (erase-buffer))
1970     (set-buffer (get-buffer-create gnus-work-buffer))
1971     (kill-all-local-variables)
1972     (buffer-disable-undo (current-buffer))
1973     (gnus-add-current-to-buffer-list)))
1974
1975 ;; Article file names when saving.
1976
1977 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1978   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1979 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1980 Otherwise, it is like ~/News/news/group/num."
1981   (let ((default
1982           (expand-file-name
1983            (concat (if (gnus-use-long-file-name 'not-save)
1984                        (gnus-capitalize-newsgroup newsgroup)
1985                      (gnus-newsgroup-directory-form newsgroup))
1986                    "/" (int-to-string (header-number headers)))
1987            (or gnus-article-save-directory "~/News"))))
1988     (if (and last-file
1989              (string-equal (file-name-directory default)
1990                            (file-name-directory last-file))
1991              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1992         default
1993       (or last-file default))))
1994
1995 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1996   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1997 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1998 Otherwise, it is like ~/News/news/group/num."
1999   (let ((default
2000           (expand-file-name
2001            (concat (if (gnus-use-long-file-name 'not-save)
2002                        newsgroup
2003                      (gnus-newsgroup-directory-form newsgroup))
2004                    "/" (int-to-string (header-number headers)))
2005            (or gnus-article-save-directory "~/News"))))
2006     (if (and last-file
2007              (string-equal (file-name-directory default)
2008                            (file-name-directory last-file))
2009              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2010         default
2011       (or last-file default))))
2012
2013 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2014   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2015 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2016 Otherwise, it is like ~/News/news/group/news."
2017   (or last-file
2018       (expand-file-name
2019        (if (gnus-use-long-file-name 'not-save)
2020            (gnus-capitalize-newsgroup newsgroup)
2021          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2022        (or gnus-article-save-directory "~/News"))))
2023
2024 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2025   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2026 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2027 Otherwise, it is like ~/News/news/group/news."
2028   (or last-file
2029       (expand-file-name
2030        (if (gnus-use-long-file-name 'not-save)
2031            newsgroup
2032          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2033        (or gnus-article-save-directory "~/News"))))
2034
2035 ;; For subscribing new newsgroup
2036
2037 (defun gnus-subscribe-hierarchical-interactive (groups)
2038   (let ((groups (sort groups 'string<))
2039         prefixes prefix start rest ans group starts)
2040     (while groups
2041       (setq prefixes (list "^"))
2042       (while (and groups prefixes)
2043         (while (not (string-match (car prefixes) (car groups)))
2044           (setq prefixes (cdr prefixes)))
2045         (setq prefix (car prefixes))
2046         (setq start (1- (length prefix)))
2047         (if (and (string-match "[^\\.]\\." (car groups) start)
2048                  (cdr groups)
2049                  (setq prefix 
2050                        (concat "^" (substring (car groups) 0 (match-end 0))))
2051                  (string-match prefix (car (cdr groups))))
2052             (progn
2053               (setq prefixes (cons prefix prefixes))
2054               (message "Descend hierarchy %s? ([y]nsq): " 
2055                        (substring prefix 1 (1- (length prefix))))
2056               (setq ans (read-char))
2057               (cond ((= ans ?n)
2058                      (while (and groups 
2059                                  (string-match prefix 
2060                                                (setq group (car groups))))
2061                        (setq gnus-killed-list 
2062                              (cons group gnus-killed-list))
2063                        (gnus-sethash group group gnus-killed-hashtb)
2064                        (setq groups (cdr groups)))
2065                      (setq starts (cdr starts)))
2066                     ((= ans ?s)
2067                      (while (and groups 
2068                                  (string-match prefix 
2069                                                (setq group (car groups))))
2070                        (gnus-sethash group group gnus-killed-hashtb)
2071                        (gnus-subscribe-alphabetically (car groups))
2072                        (setq groups (cdr groups)))
2073                      (setq starts (cdr starts)))
2074                     ((= ans ?q)
2075                      (while groups
2076                        (setq group (car groups))
2077                        (setq gnus-killed-list (cons group gnus-killed-list))
2078                        (gnus-sethash group group gnus-killed-hashtb)
2079                        (setq groups (cdr groups))))
2080                     (t nil)))
2081           (message "Subscribe %s? ([n]yq)" (car groups))
2082           (setq ans (read-char))
2083           (setq group (car groups))
2084           (cond ((= ans ?y)
2085                  (gnus-subscribe-alphabetically (car groups))
2086                  (gnus-sethash group group gnus-killed-hashtb))
2087                 ((= ans ?q)
2088                  (while groups
2089                    (setq group (car groups))
2090                    (setq gnus-killed-list (cons group gnus-killed-list))
2091                    (gnus-sethash group group gnus-killed-hashtb)
2092                    (setq groups (cdr groups))))
2093                 (t 
2094                  (setq gnus-killed-list (cons group gnus-killed-list))
2095                  (gnus-sethash group group gnus-killed-hashtb)))
2096           (setq groups (cdr groups)))))))
2097
2098 (defun gnus-subscribe-randomly (newsgroup)
2099   "Subscribe new NEWSGROUP by making it the first newsgroup."
2100   (gnus-subscribe-newsgroup newsgroup))
2101
2102 (defun gnus-subscribe-alphabetically (newgroup)
2103   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2104   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2105   (let ((groups (cdr gnus-newsrc-alist))
2106         before)
2107     (while (and (not before) groups)
2108       (if (string< newgroup (car (car groups)))
2109           (setq before (car (car groups)))
2110         (setq groups (cdr groups))))
2111     (gnus-subscribe-newsgroup newgroup before)))
2112
2113 (defun gnus-subscribe-hierarchically (newgroup)
2114   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2115   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2116   (save-excursion
2117     (set-buffer (find-file-noselect gnus-current-startup-file))
2118     (let ((groupkey newgroup)
2119           before)
2120       (while (and (not before) groupkey)
2121         (goto-char (point-min))
2122         (let ((groupkey-re
2123                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2124           (while (and (re-search-forward groupkey-re nil t)
2125                       (progn
2126                         (setq before (buffer-substring
2127                                       (match-beginning 1) (match-end 1)))
2128                         (string< before newgroup)))))
2129         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2130         (setq groupkey
2131               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2132                   (substring groupkey (match-beginning 1) (match-end 1)))))
2133       (gnus-subscribe-newsgroup newgroup before))))
2134
2135 (defun gnus-subscribe-interactively (newsgroup)
2136   "Subscribe new NEWSGROUP interactively.
2137 It is inserted in hierarchical newsgroup order if subscribed. If not,
2138 it is killed."
2139   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2140       (gnus-subscribe-hierarchically newsgroup)
2141     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2142
2143 (defun gnus-subscribe-zombies (newsgroup)
2144   "Make new NEWSGROUP a zombie group."
2145   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2146
2147 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2148   "Subscribe new NEWSGROUP.
2149 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2150 the first newsgroup."
2151   ;; We subscribe the group by changing its level to `subscribed'.
2152   (gnus-group-change-level 
2153    newsgroup gnus-level-default-subscribed
2154    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2155   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2156
2157 ;; For directories
2158
2159 (defun gnus-newsgroup-directory-form (newsgroup)
2160   "Make hierarchical directory name from NEWSGROUP name."
2161   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2162         (len (length newsgroup))
2163         idx)
2164     ;; If this is a foreign group, we don't want to translate the
2165     ;; entire name.  
2166     (if (setq idx (string-match ":" newsgroup))
2167         (aset newsgroup idx ?/)
2168       (setq idx 0))
2169     ;; Replace all occurrences of `.' with `/'.
2170     (while (< idx len)
2171       (if (= (aref newsgroup idx) ?.)
2172           (aset newsgroup idx ?/))
2173       (setq idx (1+ idx)))
2174     newsgroup))
2175
2176 (defun gnus-make-directory (dir)
2177   "Make DIRECTORY recursively."
2178   (let* ((dir (expand-file-name dir default-directory))
2179          dirs)
2180     (if (string-match "/$" dir)
2181         (setq dir (substring dir 0 (match-beginning 0))))
2182     (while (not (file-exists-p dir))
2183       (setq dirs (cons dir dirs))
2184       (string-match "/[^/]+$" dir)
2185       (setq dir (substring dir 0 (match-beginning 0))))
2186     (while dirs
2187       (make-directory (car dirs))
2188       (setq dirs (cdr dirs)))))
2189
2190 (defun gnus-capitalize-newsgroup (newsgroup)
2191   "Capitalize NEWSGROUP name."
2192   (and (not (zerop (length newsgroup)))
2193        (concat (char-to-string (upcase (aref newsgroup 0)))
2194                (substring newsgroup 1))))
2195
2196 ;; Var
2197
2198 (defun gnus-simplify-subject (subject &optional re-only)
2199   "Remove `Re:' and words in parentheses.
2200 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2201   (let ((case-fold-search t))           ;Ignore case.
2202     ;; Remove `Re:' and `Re^N:'.
2203     (if (string-match "^re:[ \t]*" subject)
2204         (setq subject (substring subject (match-end 0))))
2205     ;; Remove words in parentheses from end.
2206     (or re-only
2207         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2208           (setq subject (substring subject 0 (match-beginning 0)))))
2209     ;; Return subject string.
2210     subject))
2211
2212 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2213 ;; all whitespace.
2214 (defun gnus-simplify-subject-fuzzy (subject)
2215   (let ((case-fold-search t))
2216     (save-excursion
2217       (gnus-set-work-buffer)
2218       (insert subject)
2219       (inline (gnus-simplify-buffer-fuzzy))
2220       (buffer-string))))
2221
2222 (defun gnus-simplify-buffer-fuzzy ()
2223   (goto-char (point-min))
2224   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2225   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2226                             nil t)
2227     (replace-match "" t t))
2228   (goto-char (point-min))
2229   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2230     (replace-match "" t t))
2231   (goto-char (point-min))
2232   (while (re-search-forward "[ \t]+" nil t)
2233     (replace-match " " t t))
2234   (goto-char (point-min))
2235   (while (re-search-forward "[ \t]+$" nil t)
2236     (replace-match "" t t))
2237   (goto-char (point-min))
2238   (while (re-search-forward "^[ \t]+" nil t)
2239     (replace-match "" t t))
2240   (if gnus-simplify-subject-fuzzy-regexp
2241       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2242         (replace-match "" t t)))
2243     )
2244
2245 ;; Add the current buffer to the list of buffers to be killed on exit. 
2246 (defun gnus-add-current-to-buffer-list ()
2247   (or (memq (current-buffer) gnus-buffer-list)
2248       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2249
2250 (defun gnus-string> (s1 s2)
2251   (not (or (string< s1 s2)
2252            (string= s1 s2))))
2253
2254 ;; Functions accessing headers.
2255 ;; Functions are more convenient than macros in some cases.
2256
2257 (defun gnus-header-number (header)
2258   (header-number header))
2259
2260 (defun gnus-header-subject (header)
2261   (header-subject header))
2262
2263 (defun gnus-header-from (header)
2264   (header-from header))
2265
2266 (defun gnus-header-xref (header)
2267   (header-xref header))
2268
2269 (defun gnus-header-lines (header)
2270   (header-lines header))
2271
2272 (defun gnus-header-date (header)
2273   (header-date header))
2274
2275 (defun gnus-header-id (header)
2276   (header-id header))
2277
2278 (defun gnus-header-references (header)
2279   (header-references header))
2280
2281 ;;; General various misc type functions.
2282
2283 (defun gnus-clear-system ()
2284   "Clear all variables and buffers."
2285   ;; Clear Gnus variables.
2286   (let ((variables gnus-variable-list))
2287     (while variables
2288       (set (car variables) nil)
2289       (setq variables (cdr variables))))
2290   ;; Clear other internal variables.
2291   (setq gnus-list-of-killed-groups nil
2292         gnus-have-read-active-file nil
2293         gnus-newsrc-alist nil
2294         gnus-newsrc-hashtb nil
2295         gnus-killed-list nil
2296         gnus-zombie-list nil
2297         gnus-killed-hashtb nil
2298         gnus-active-hashtb nil
2299         gnus-moderated-list nil
2300         gnus-description-hashtb nil
2301         gnus-newsgroup-headers nil
2302         gnus-newsgroup-headers-hashtb-by-number nil
2303         gnus-newsgroup-name nil
2304         gnus-server-alist nil
2305         gnus-current-select-method nil)
2306   ;; Reset any score variables.
2307   (and (boundp 'gnus-score-cache)
2308        (set 'gnus-score-cache nil))
2309   (and (boundp 'gnus-internal-global-score-files)
2310        (set 'gnus-internal-global-score-files nil))
2311   ;; Kill the startup file.
2312   (and gnus-current-startup-file
2313        (get-file-buffer gnus-current-startup-file)
2314        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2315   ;; Save any cache buffers.
2316   (and gnus-use-cache (gnus-cache-save-buffers))
2317   ;; Clear the dribble buffer.
2318   (gnus-dribble-clear)
2319   ;; Kill global KILL file buffer.
2320   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2321       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2322   (gnus-kill-buffer nntp-server-buffer)
2323   ;; Kill Gnus buffers.
2324   (while gnus-buffer-list
2325     (gnus-kill-buffer (car gnus-buffer-list))
2326     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2327
2328 (defun gnus-windows-old-to-new (setting)
2329   (if (symbolp setting)
2330       (setq setting 
2331             (cond ((eq setting 'SelectArticle)
2332                    'article)
2333                   ((eq setting 'SelectSubject)
2334                    'summary)
2335                   ((eq setting 'SelectNewsgroup)
2336                    'group)
2337                   (t setting))))
2338   (if (or (listp setting)
2339           (not (and gnus-window-configuration
2340                     (memq setting '(group summary article)))))
2341       setting
2342     (let* ((setting (if (eq setting 'group) 
2343                         (if (assq 'newsgroup gnus-window-configuration)
2344                             'newsgroup
2345                           'newsgroups) setting))
2346            (elem (car (cdr (assq setting gnus-window-configuration))))
2347            (total (apply '+ elem))
2348            (types '(group summary article))
2349            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2350            (i 0)
2351            perc
2352            out)
2353       (while (< i 3)
2354         (or (zerop (nth i elem))
2355             (progn
2356               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2357               (setq out (cons (if (eq pbuf (nth i types))
2358                                   (vector (nth i types) perc 'point)
2359                                 (vector (nth i types) perc))
2360                               out))))
2361         (setq i (1+ i)))
2362       (list (nreverse out)))))
2363            
2364 (defun gnus-add-configuration (conf)
2365   (setq gnus-buffer-configuration 
2366         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2367                          gnus-buffer-configuration))))
2368
2369 (defun gnus-configure-windows (setting)
2370   (setq setting (gnus-windows-old-to-new setting))
2371   (let ((r (if (symbolp setting)
2372                   (cdr (assq setting gnus-buffer-configuration))
2373                 setting))
2374         (in-buf (current-buffer))
2375         rule val window w height hor ohor heights sub jump-buffer
2376         rel total to-buf)
2377     (or r (error "No such setting: %s" setting))
2378
2379     ;; Either remove all windows or just remove all Gnus windows.
2380     (if gnus-use-full-window
2381         (delete-other-windows)
2382       (gnus-remove-some-windows)
2383       (switch-to-buffer nntp-server-buffer))
2384
2385     (while r
2386       (setq hor (car r)
2387             ohor nil)
2388
2389       ;; We have to do the (possible) horizontal splitting before the
2390       ;; vertical. 
2391       (if (and (listp (car hor)) 
2392                (eq (car (car hor)) 'horizontal))
2393           (progn
2394             (split-window 
2395              nil
2396              (if (integerp (nth 1 (car hor)))
2397                  (nth 1 (car hor))
2398                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2399              t)
2400             (setq hor (cdr hor))))
2401
2402       ;; Go through the rules and eval the elements that are to be
2403       ;; evaled.  
2404       (while hor
2405         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2406             (progn
2407               ;; Expand short buffer name.
2408               (setq w (aref val 0))
2409               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2410                    (progn
2411                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2412                      (aset val 0 w)))
2413               (setq ohor (cons val ohor))))
2414         (setq hor (cdr hor)))
2415       (setq rule (cons (nreverse ohor) rule))
2416       (setq r (cdr r)))
2417     (setq rule (nreverse rule))
2418
2419     ;; We tally the window sizes.
2420     (setq total (window-height))
2421     (while rule
2422       (setq hor (car rule))
2423       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2424           (setq hor (cdr hor)))
2425       (setq sub 0)
2426       (while hor
2427         (setq rel (aref (car hor) 1)
2428               heights (cons
2429                        (cond ((and (floatp rel) (= 1.0 rel))
2430                               'x)
2431                              ((integerp rel)
2432                               rel)
2433                              (t
2434                               (max (floor (* total rel)) 4)))
2435                        heights)
2436               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2437               hor (cdr hor)))
2438       (setq heights (nreverse heights)
2439             hor (car rule))
2440
2441       ;; We then go through these heighs and create windows for them.
2442       (while heights
2443         (setq height (car heights)
2444               heights (cdr heights))
2445         (and (eq height 'x)
2446              (setq height (- total sub)))
2447         (and heights
2448              (split-window nil height))
2449         (setq to-buf (aref (car hor) 0))
2450         (switch-to-buffer 
2451          (cond ((not to-buf)
2452                 in-buf)
2453                ((symbolp to-buf)
2454                 (symbol-value (aref (car hor) 0)))
2455                (t
2456                 (aref (car hor) 0))))
2457         (and (> (length (car hor)) 2)
2458              (eq (aref (car hor) 2) 'point)
2459              (setq jump-buffer (current-buffer)))
2460         (other-window 1)
2461         (setq hor (cdr hor)))
2462       
2463       (setq rule (cdr rule)))
2464
2465     ;; Finally, we pop to the buffer that's supposed to have point. 
2466     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2467
2468     (select-window (get-buffer-window jump-buffer))
2469     (set-buffer jump-buffer)))
2470       
2471 (defun gnus-remove-some-windows ()
2472   (let ((buffers gnus-window-to-buffer)
2473         (first t)
2474         buf)
2475     (save-excursion
2476       ;; Remove windows on all known Gnus buffers.
2477       (while buffers
2478         (setq buf (cdr (car buffers)))
2479         (if (symbolp buf)
2480             (setq buf (and (boundp buf) (symbol-value buf))))
2481         (and buf 
2482              (get-buffer-window buf)
2483              (progn
2484                (if first
2485                    (progn
2486                      (pop-to-buffer buf)
2487                      (switch-to-buffer nntp-server-buffer)
2488                      (setq first nil))
2489                  (delete-window (get-buffer-window buf)))))
2490         (setq buffers (cdr buffers)))
2491       ;; Remove windows on *all* summary buffers.
2492       (let ((buffers (buffer-list)))
2493         (while buffers
2494           (if (and (string-match 
2495                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2496                    (get-buffer-window (car buffers)))
2497               (delete-window (get-buffer-window (car buffers))))
2498           (setq buffers (cdr buffers)))))))
2499                           
2500 (defun gnus-version ()
2501   "Version numbers of this version of Gnus."
2502   (interactive)
2503   (let ((methods gnus-valid-select-methods)
2504         (mess gnus-version)
2505         meth)
2506     ;; Go through all the legal select methods and add their version
2507     ;; numbers to the total version string. Only the backends that are
2508     ;; currently in use will have their message numbers taken into
2509     ;; consideration. 
2510     (while methods
2511       (setq meth (intern (concat (car (car methods)) "-version")))
2512       (and (boundp meth)
2513            (stringp (symbol-value meth))
2514            (setq mess (concat mess "; " (symbol-value meth))))
2515       (setq methods (cdr methods)))
2516     (gnus-message 2 mess)))
2517
2518 (defun gnus-info-find-node ()
2519   "Find Info documentation of Gnus."
2520   (interactive)
2521   ;; Enlarge info window if needed.
2522   (let ((mode major-mode))
2523     (gnus-configure-windows 'info)
2524     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2525
2526 (defun gnus-overload-functions (&optional overloads)
2527   "Overload functions specified by optional argument OVERLOADS.
2528 If nothing is specified, use the variable gnus-overload-functions."
2529   (let ((defs nil)
2530         (overloads (or overloads gnus-overload-functions)))
2531     (while overloads
2532       (setq defs (car overloads))
2533       (setq overloads (cdr overloads))
2534       ;; Load file before overloading function if necessary.  Make
2535       ;; sure we cannot use `require' always.
2536       (and (not (fboundp (car defs)))
2537            (car (cdr (cdr defs)))
2538            (load (car (cdr (cdr defs))) nil 'nomessage))
2539       (fset (car defs) (car (cdr defs))))))
2540
2541 (defun gnus-replace-chars-in-string (string from to)
2542   "Replace characters in STRING from FROM to TO."
2543   (let ((string (substring string 0))   ;Copy string.
2544         (len (length string))
2545         (idx 0))
2546     ;; Replace all occurrences of FROM with TO.
2547     (while (< idx len)
2548       (if (= (aref string idx) from)
2549           (aset string idx to))
2550       (setq idx (1+ idx)))
2551     string))
2552
2553 (defun gnus-days-between (date1 date2)
2554   ;; Return the number of days between date1 and date2.
2555   (- (gnus-day-number date1) (gnus-day-number date2)))
2556
2557 (defun gnus-day-number (date)
2558   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2559                      (timezone-parse-date date))))
2560     (timezone-absolute-from-gregorian 
2561      (nth 1 dat) (nth 2 dat) (car dat))))
2562
2563 ;; Returns a floating point number that says how many seconds have
2564 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2565 (defun gnus-seconds-since-epoch (date)
2566   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2567                         (timezone-parse-date date)))
2568          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2569                         (timezone-parse-time
2570                          (aref (timezone-parse-date date) 3))))
2571          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2572                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2573          (tday (- (timezone-absolute-from-gregorian 
2574                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2575                   (timezone-absolute-from-gregorian 
2576                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2577     (+ (nth 2 ttime)
2578        (* (nth 1 ttime) 60)
2579        (* 1.0 (nth 0 ttime) 60 60)
2580        (* 1.0 tday 60 60 24))))
2581
2582 (defun gnus-file-newer-than (file date)
2583   (let ((fdate (nth 5 (file-attributes file))))
2584     (or (> (car fdate) (car date))
2585         (and (= (car fdate) (car date))
2586              (> (nth 1 fdate) (nth 1 date))))))
2587
2588 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2589 ;; the echo area.
2590 (defun gnus-y-or-n-p (prompt)
2591   (prog1
2592       (y-or-n-p prompt)
2593     (message "")))
2594
2595 (defun gnus-yes-or-no-p (prompt)
2596   (prog1
2597       (yes-or-no-p prompt)
2598     (message "")))
2599
2600 ;; Check whether to use long file names.
2601 (defun gnus-use-long-file-name (symbol)
2602   ;; The variable has to be set...
2603   (and gnus-use-long-file-name
2604        ;; If it isn't a list, then we return t.
2605        (or (not (listp gnus-use-long-file-name))
2606            ;; If it is a list, and the list contains `symbol', we
2607            ;; return nil.  
2608            (not (memq symbol gnus-use-long-file-name)))))
2609
2610 ;; I suspect there's a better way, but I haven't taken the time to do
2611 ;; it yet. -erik selberg@cs.washington.edu
2612 (defun gnus-dd-mmm (messy-date)
2613   "Return a string like DD-MMM from a big messy string"
2614   (let ((datevec (timezone-parse-date messy-date)))
2615     (format "%2s-%s"
2616             (or (aref datevec 2) "??")
2617             (capitalize
2618              (or (car 
2619                   (nth (1- (string-to-number (aref datevec 1)))
2620                        timezone-months-assoc))
2621                  "???")))))
2622
2623 ;; Make a hash table (default and minimum size is 255).
2624 ;; Optional argument HASHSIZE specifies the table size.
2625 (defun gnus-make-hashtable (&optional hashsize)
2626   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2627
2628 ;; Make a number that is suitable for hashing; bigger than MIN and one
2629 ;; less than 2^x.
2630 (defun gnus-create-hash-size (min)
2631   (let ((i 1))
2632     (while (< i min)
2633       (setq i (* 2 i)))
2634     (1- i)))
2635
2636 ;; Show message if message has a lower level than `gnus-verbose'. 
2637 ;; Guide-line for numbers:
2638 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2639 ;; for things that take a long time, 7 - not very important messages
2640 ;; on stuff, 9 - messages inside loops.
2641 (defun gnus-message (level &rest args)
2642   (if (<= level gnus-verbose)
2643       (apply 'message args)
2644     ;; We have to do this format thingie here even if the result isn't
2645     ;; shown - the return value has to be the same as the return value
2646     ;; from `message'.
2647     (apply 'format args)))
2648
2649 ;; Generate a unique new group name.
2650 (defun gnus-generate-new-group-name (leaf)
2651   (let ((name leaf)
2652         (num 0))
2653     (while (gnus-gethash name gnus-newsrc-hashtb)
2654       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2655     name))
2656
2657 (defun gnus-find-file-noselect (file &optional force)
2658   "Does vaguely the same as find-file-noselect. No hooks are run."
2659   (let (buf insert)
2660     (if (setq buf (get-file-buffer file))
2661         (setq insert force)
2662       (setq buf (create-file-buffer file))
2663       (setq insert t))
2664     (if (not insert)
2665         buf
2666       (save-excursion
2667         (set-buffer buf)
2668         (erase-buffer)
2669         (and (file-readable-p file)
2670              (insert-file-contents file))
2671         (set-visited-file-name file)
2672         (set-buffer-modified-p nil)
2673         (current-buffer)))))
2674
2675 ;;; List and range functions
2676
2677 (defun gnus-last-element (list)
2678   "Return last element of LIST."
2679   (while (cdr list)
2680     (setq list (cdr list)))
2681   (car list))
2682
2683 (defun gnus-copy-sequence (list)
2684   "Do a complete, total copy of a list."
2685   (if (and (consp list) (not (consp (cdr list))))
2686       (cons (car list) (cdr list))
2687     (mapcar (lambda (elem) (if (consp elem) 
2688                                (if (consp (cdr elem))
2689                                    (gnus-copy-sequence elem)
2690                                  (cons (car elem) (cdr elem)))
2691                              elem))
2692             list)))
2693
2694 (defun gnus-set-difference (list1 list2)
2695   "Return a list of elements of LIST1 that do not appear in LIST2."
2696   (let ((list1 (copy-sequence list1)))
2697     (while list2
2698       (setq list1 (delq (car list2) list1))
2699       (setq list2 (cdr list2)))
2700     list1))
2701
2702 (defun gnus-sorted-complement (list1 list2)
2703   "Return a list of elements of LIST1 that do not appear in LIST2.
2704 Both lists have to be sorted over <."
2705   (let (out)
2706     (if (or (null list1) (null list2))
2707         (or list1 list2)
2708       (while (and list1 list2)
2709         (cond ((= (car list1) (car list2))
2710                (setq list1 (cdr list1)
2711                      list2 (cdr list2)))
2712               ((< (car list1) (car list2))
2713                (setq out (cons (car list1) out))
2714                (setq list1 (cdr list1)))
2715               (t
2716                (setq out (cons (car list2) out))
2717                (setq list2 (cdr list2)))))
2718       (nconc (nreverse out) (or list1 list2)))))
2719
2720 (defun gnus-intersection (list1 list2)      
2721   (let ((result nil))
2722     (while list2
2723       (if (memq (car list2) list1)
2724           (setq result (cons (car list2) result)))
2725       (setq list2 (cdr list2)))
2726     result))
2727
2728 (defun gnus-sorted-intersection (list1 list2)
2729   ;; LIST1 and LIST2 have to be sorted over <.
2730   (let (out)
2731     (while (and list1 list2)
2732       (cond ((= (car list1) (car list2))
2733              (setq out (cons (car list1) out)
2734                    list1 (cdr list1)
2735                    list2 (cdr list2)))
2736             ((< (car list1) (car list2))
2737              (setq list1 (cdr list1)))
2738             (t
2739              (setq list2 (cdr list2)))))
2740     (nreverse out)))
2741
2742 (defun gnus-set-sorted-intersection (list1 list2)
2743   ;; LIST1 and LIST2 have to be sorted over <.
2744   ;; This function modifies LIST1.
2745   (let* ((top (cons nil list1))
2746          (prev top))
2747   (while (and list1 list2)
2748     (cond ((= (car list1) (car list2))
2749            (setq prev list1
2750                  list1 (cdr list1)
2751                  list2 (cdr list2)))
2752           ((< (car list1) (car list2))
2753            (setcdr prev (cdr list1))
2754            (setq list1 (cdr list1)))
2755           (t
2756            (setq list2 (cdr list2)))))
2757   (setcdr prev nil)
2758   (cdr top)))
2759
2760 (defun gnus-compress-sequence (numbers &optional always-list)
2761   "Convert list of numbers to a list of ranges or a single range.
2762 If ALWAYS-LIST is non-nil, this function will always release a list of
2763 ranges."
2764   (let* ((first (car numbers))
2765          (last (car numbers))
2766          result)
2767     (if (null numbers)
2768         nil
2769       (if (not (listp (cdr numbers)))
2770           numbers
2771         (while numbers
2772           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2773                 ((= (1+ last) (car numbers)) ;Still in sequence
2774                  (setq last (car numbers)))
2775                 (t                      ;End of one sequence
2776                  (setq result 
2777                        (cons (if (= first last) first
2778                                (cons first last)) result))
2779                  (setq first (car numbers))
2780                  (setq last  (car numbers))))
2781           (setq numbers (cdr numbers)))
2782         (if (and (not always-list) (null result))
2783             (if (= first last) (list first) (cons first last))
2784           (nreverse (cons (if (= first last) first (cons first last))
2785                           result)))))))
2786
2787 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2788 (defun gnus-uncompress-range (ranges)
2789   "Expand a list of ranges into a list of numbers.
2790 RANGES is either a single range on the form `(num . num)' or a list of
2791 these ranges."
2792   (let (first last result)
2793     (cond 
2794      ((null ranges)
2795       nil)
2796      ((not (listp (cdr ranges)))
2797       (setq first (car ranges))
2798       (setq last (cdr ranges))
2799       (while (<= first last)
2800         (setq result (cons first result))
2801         (setq first (1+ first)))
2802       (nreverse result))
2803      (t
2804       (while ranges
2805         (if (atom (car ranges))
2806             (if (numberp (car ranges))
2807                 (setq result (cons (car ranges) result)))
2808           (setq first (car (car ranges)))
2809           (setq last  (cdr (car ranges)))
2810           (while (<= first last)
2811             (setq result (cons first result))
2812             (setq first (1+ first))))
2813         (setq ranges (cdr ranges)))
2814       (nreverse result)))))
2815
2816 (defun gnus-add-to-range (ranges list)
2817   "Return a list of ranges that has all articles from both RANGES and LIST.
2818 Note: LIST has to be sorted over `<'."
2819   (if (not ranges)
2820       (gnus-compress-sequence list t)
2821     (setq list (copy-sequence list))
2822     (or (listp (cdr ranges))
2823         (setq ranges (list ranges)))
2824     (let ((out ranges)
2825           ilist lowest highest temp)
2826       (while (and ranges list)
2827         (setq ilist list)
2828         (setq lowest (or (and (atom (car ranges)) (car ranges))
2829                          (car (car ranges))))
2830         (while (and list (cdr list) (< (car (cdr list)) lowest))
2831           (setq list (cdr list)))
2832         (if (< (car ilist) lowest)
2833             (progn
2834               (setq temp list)
2835               (setq list (cdr list))
2836               (setcdr temp nil)
2837               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2838         (setq highest (or (and (atom (car ranges)) (car ranges))
2839                           (cdr (car ranges))))
2840         (while (and list (<= (car list) highest))
2841           (setq list (cdr list)))
2842         (setq ranges (cdr ranges)))
2843       (if list
2844           (setq out (nconc (gnus-compress-sequence list t) out)))
2845       (setq out (sort out (lambda (r1 r2) 
2846                             (< (or (and (atom r1) r1) (car r1))
2847                                (or (and (atom r2) r2) (car r2))))))
2848       (setq ranges out)
2849       (while ranges
2850         (if (atom (car ranges))
2851             (if (cdr ranges)
2852                 (if (atom (car (cdr ranges)))
2853                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2854                         (progn
2855                           (setcar ranges (cons (car ranges) 
2856                                                (car (cdr ranges))))
2857                           (setcdr ranges (cdr (cdr ranges)))))
2858                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2859                       (progn
2860                         (setcar (car (cdr ranges)) (car ranges))
2861                         (setcar ranges (car (cdr ranges)))
2862                         (setcdr ranges (cdr (cdr ranges)))))))
2863           (if (cdr ranges)
2864               (if (atom (car (cdr ranges)))
2865                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2866                       (progn
2867                         (setcdr (car ranges) (car (cdr ranges)))
2868                         (setcdr ranges (cdr (cdr ranges)))))
2869                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2870                     (progn
2871                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2872                       (setcdr ranges (cdr (cdr ranges))))))))
2873         (setq ranges (cdr ranges)))
2874       out)))
2875
2876 (defun gnus-remove-from-range (ranges list)
2877   "Return a list of ranges that has all articles from LIST removed from RANGES.
2878 Note: LIST has to be sorted over `<'."
2879   ;; !!! This function shouldn't look like this, but I've got a headache.
2880   (gnus-compress-sequence 
2881    (gnus-sorted-complement
2882     (gnus-uncompress-range ranges) list)))
2883
2884 (defun gnus-member-of-range (number ranges)
2885   (if (not (listp (cdr ranges)))
2886       (and (>= number (car ranges)) 
2887            (<= number (cdr ranges)))
2888     (let ((not-stop t))
2889       (while (and ranges 
2890                   (if (numberp (car ranges))
2891                       (>= number (car ranges))
2892                     (>= number (car (car ranges))))
2893                   not-stop)
2894         (if (if (numberp (car ranges))
2895                 (= number (car ranges))
2896               (and (>= number (car (car ranges)))
2897                    (<= number (cdr (car ranges)))))
2898             (setq not-stop nil))
2899         (setq ranges (cdr ranges)))
2900       (not not-stop))))
2901
2902 \f
2903 ;;;
2904 ;;; Gnus group mode
2905 ;;;
2906
2907 (defvar gnus-group-mode-map nil)
2908 (defvar gnus-group-group-map nil)
2909 (defvar gnus-group-mark-map nil)
2910 (defvar gnus-group-list-map nil)
2911 (defvar gnus-group-sub-map nil)
2912 (put 'gnus-group-mode 'mode-class 'special)
2913
2914 (if gnus-group-mode-map
2915     nil
2916   (setq gnus-group-mode-map (make-keymap))
2917   (suppress-keymap gnus-group-mode-map)
2918   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2919   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2920   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2921   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2922   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2923   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2924   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2925   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2926   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2927   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2928   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2929   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2930   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2931   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2932   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2933   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2934   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2935   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2936   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2937   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2938   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2939   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2940   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2941   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2942   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2943   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2944   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2945   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2946   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2947   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2948   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2949   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2950   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2951   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2952   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2953   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2954   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2955   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2956   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2957   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2958   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2959   (define-key gnus-group-mode-map "V" 'gnus-version)
2960   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2961   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2962   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2963   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2964   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2965   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2966   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2967   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2968   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2969   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2970   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2971   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2972   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2973   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2974   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2975
2976   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2977   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2978   (define-prefix-command 'gnus-group-mark-map)
2979   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2980   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2981   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2982   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2983
2984   (define-prefix-command 'gnus-group-group-map)
2985   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2986   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2987   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2988   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2989   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2990   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2991   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2992   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2993   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2994   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2995   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2996   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2997   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2998   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
2999   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3000   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3001   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3002   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3003
3004   (define-prefix-command 'gnus-group-list-map)
3005   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3006   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3007   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3008   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3009   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3010   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3011   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3012   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3013   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3014
3015   (define-prefix-command 'gnus-group-sub-map)
3016   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3017   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3018   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3019   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3020   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3021   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3022   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3023   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3024
3025 (defun gnus-group-mode ()
3026   "Major mode for reading news.
3027
3028 All normal editing commands are switched off.
3029 \\<gnus-group-mode-map>
3030 The group buffer lists (some of) the groups available.  For instance,
3031 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3032 lists all zombie groups. 
3033
3034 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3035 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3036
3037 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3038
3039 The following commands are available:
3040
3041 \\{gnus-group-mode-map}"
3042   (interactive)
3043   (if gnus-visual (gnus-group-make-menu-bar))
3044   (kill-all-local-variables)
3045   (setq mode-line-modified "-- ")
3046   (make-local-variable 'mode-line-format)
3047   (setq mode-line-format (copy-sequence mode-line-format))
3048   (and (equal (nth 3 mode-line-format) "   ")
3049        (setcar (nthcdr 3 mode-line-format) ""))
3050   (setq major-mode 'gnus-group-mode)
3051   (setq mode-name "Group")
3052   (gnus-group-set-mode-line)
3053   (setq mode-line-process nil)
3054   (use-local-map gnus-group-mode-map)
3055   (buffer-disable-undo (current-buffer))
3056   (setq truncate-lines t)
3057   (setq buffer-read-only t)
3058   (run-hooks 'gnus-group-mode-hook))
3059
3060 (defun gnus-mouse-pick-group (e)
3061   (interactive "e")
3062   (mouse-set-point e)
3063   (gnus-group-read-group nil))
3064
3065 ;;;###autoload
3066 (defun gnus-no-server (&optional arg)
3067   "Read network news.
3068 If ARG is a positive number, Gnus will use that as the
3069 startup level. If ARG is nil, Gnus will be started at level 2. 
3070 If ARG is non-nil and not a positive number, Gnus will
3071 prompt the user for the name of an NNTP server to use.
3072 As opposed to `gnus', this command will not connect to the local server."
3073   (interactive "P")
3074   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3075
3076 (defalias '\(ding\) 'gnus)
3077
3078 ;;;###autoload
3079 (defun gnus (&optional arg dont-connect)
3080   "Read network news.
3081 If ARG is non-nil and a positive number, Gnus will use that as the
3082 startup level. If ARG is non-nil and not a positive number, Gnus will
3083 prompt the user for the name of an NNTP server to use."
3084   (interactive "P")
3085   (if (get-buffer gnus-group-buffer)
3086       (progn
3087         (switch-to-buffer gnus-group-buffer)
3088         (gnus-group-get-new-news))
3089     (gnus-clear-system)
3090     (nnheader-init-server-buffer)
3091     (gnus-read-init-file)
3092     (let ((level (and arg (numberp arg) (> arg 0) arg))
3093           did-connect)
3094       (unwind-protect
3095           (progn
3096             (gnus-group-setup-buffer)
3097             (or dont-connect 
3098                 (setq did-connect
3099                       (gnus-start-news-server (and arg (not level))))))
3100         (if (and (not dont-connect) 
3101                  (not did-connect))
3102             (gnus-group-quit)
3103           (run-hooks 'gnus-startup-hook)
3104           ;; NNTP server is successfully open. 
3105           (gnus-update-format-specifications)
3106           (gnus-summary-make-display-table)
3107           (let ((buffer-read-only nil))
3108             (erase-buffer)
3109             (if (not gnus-inhibit-startup-message)
3110                 (progn
3111                   (gnus-group-startup-message)
3112                   (sit-for 0))))
3113           (gnus-setup-news nil level)
3114           (and gnus-use-dribble-file (gnus-dribble-open))
3115           (gnus-group-list-groups level)
3116           (gnus-configure-windows 'group))))))
3117
3118 (defun gnus-group-startup-message (&optional x y)
3119   "Insert startup message in current buffer."
3120   ;; Insert the message.
3121   (erase-buffer)
3122   (insert
3123    (format "
3124     %s
3125            A newsreader 
3126       for GNU Emacs
3127
3128         Based on GNUS 
3129              written by 
3130      Masanobu UMEDA
3131
3132     Lars Magne 
3133          Ingebrigtsen 
3134       larsi@ifi.uio.no
3135
3136            gnus-version))
3137   ;; And then hack it.
3138   ;; 18 is the longest line.
3139   (indent-rigidly (point-min) (point-max) 
3140                   (/ (max (- (window-width) (or x 28)) 0) 2))
3141   (goto-char (point-min))
3142   ;; +4 is fuzzy factor.
3143   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3144
3145 (defun gnus-group-setup-buffer ()
3146   (or (get-buffer gnus-group-buffer)
3147       (progn
3148         (switch-to-buffer gnus-group-buffer)
3149         (gnus-add-current-to-buffer-list)
3150         (gnus-group-mode)
3151         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3152
3153 (defun gnus-group-list-groups (level &optional unread)
3154   "List newsgroups with level LEVEL or lower that have unread articles.
3155 Default is all subscribed groups.
3156 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3157   (interactive (list (and current-prefix-arg
3158                           (prefix-numeric-value current-prefix-arg))))
3159   (if gnus-group-use-permanent-levels
3160       (progn
3161         (setq gnus-group-default-list-level 
3162               (or level gnus-group-default-list-level))
3163         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3164     (setq level (or level gnus-group-default-list-level 
3165                     gnus-level-subscribed)))
3166   (gnus-group-setup-buffer)     ;May call from out of group buffer
3167   (let ((case-fold-search nil)
3168         (group (gnus-group-group-name)))
3169     (funcall gnus-group-prepare-function level unread nil)
3170     (if (zerop (buffer-size))
3171         (gnus-message 5 gnus-no-groups-message)
3172       (goto-char (point-min))
3173       (if (not group)
3174           ;; Go to the first group with unread articles.
3175           (gnus-group-search-forward nil nil nil t)
3176         ;; Find the right group to put point on. If the current group
3177         ;; has disapeared in the new listing, try to find the next
3178         ;; one. If no next one can be found, just leave point at the
3179         ;; first newsgroup in the buffer.
3180         (if (not (gnus-goto-char
3181                   (text-property-any (point-min) (point-max) 
3182                                      'gnus-group (intern group))))
3183             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3184               (while (and newsrc
3185                           (not (gnus-goto-char 
3186                                 (text-property-any 
3187                                  (point-min) (point-max) 'gnus-group 
3188                                  (intern (car (car newsrc)))))))
3189                 (setq newsrc (cdr newsrc)))
3190               (or newsrc (progn (goto-char (point-max))
3191                                 (forward-line -1))))))
3192       ;; Adjust cursor point.
3193       (gnus-group-position-cursor))))
3194
3195 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3196   "List all newsgroups with unread articles of level LEVEL or lower.
3197 If ALL is non-nil, list groups that have no unread articles.
3198 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3199 If REGEXP, only list groups matching REGEXP."
3200   (set-buffer gnus-group-buffer)
3201   (let ((buffer-read-only nil)
3202         (newsrc (cdr gnus-newsrc-alist))
3203         (lowest (or lowest 1))
3204         info clevel unread group)
3205     (erase-buffer)
3206     (if (< lowest gnus-level-zombie)
3207         ;; List living groups.
3208         (while newsrc
3209           (setq info (car newsrc)
3210                 group (car info)
3211                 newsrc (cdr newsrc)
3212                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3213           (and unread ; This group might be bogus
3214                (or (not regexp)
3215                    (string-match regexp group))
3216                (<= (setq clevel (car (cdr info))) level) 
3217                (>= clevel lowest)
3218                (or all            ; We list all groups?
3219                    (eq unread t)  ; We list unactivated groups
3220                    (> unread 0)   ; We list groups with unread articles
3221                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3222                (gnus-group-insert-group-line 
3223                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3224
3225     ;; List dead groups.
3226     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3227          (gnus-group-prepare-flat-list-dead 
3228           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3229           gnus-level-zombie ?Z
3230           regexp))
3231     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3232          (gnus-group-prepare-flat-list-dead 
3233           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3234           gnus-level-killed ?K regexp))
3235
3236     (gnus-group-set-mode-line)
3237     (setq gnus-have-all-newsgroups all)
3238     (run-hooks 'gnus-group-prepare-hook)))
3239
3240 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3241   ;; List zombies and killed lists somehwat faster, which was
3242   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3243   ;; this by ignoring the group format specification altogether.
3244   (let (group beg)
3245     (while groups
3246       (setq group (car groups)
3247             groups (cdr groups))
3248       (if (or (not regexp)
3249               (string-match regexp group))
3250           (progn
3251             (setq beg (point))
3252             (insert (format " %c     *: %s\n" mark group))
3253             (add-text-properties 
3254              beg (1+ beg) 
3255              (list 'gnus-group (intern group)
3256                    'gnus-unread t
3257                    'gnus-level level)))))))
3258
3259 (defun gnus-group-real-name (group)
3260   "Find the real name of a foreign newsgroup."
3261   (if (string-match ":[^:]+$" group)
3262       (substring group (1+ (match-beginning 0)))
3263     group))
3264
3265 (defun gnus-group-prefixed-name (group method)
3266   "Return the whole name from GROUP and METHOD."
3267   (and (stringp method) (setq method (gnus-server-to-method method)))
3268   (concat (format "%s" (car method))
3269           (if (and 
3270                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3271                (not (string= (nth 1 method) "")))
3272               (concat "+" (nth 1 method)))
3273           ":" group))
3274
3275 (defun gnus-group-real-prefix (group)
3276   "Return the prefix of the current group name."
3277   (if (string-match "^[^:]+:" group)
3278       (substring group 0 (match-end 0))
3279     ""))
3280
3281 (defun gnus-group-method-name (group)
3282   "Return the method used for selecting GROUP."
3283   (let ((prefix (gnus-group-real-prefix group)))
3284     (if (equal prefix "")
3285         gnus-select-method
3286       (if (string-match "^[^\\+]+\\+" prefix)
3287           (list (intern (substring prefix 0 (1- (match-end 0))))
3288                 (substring prefix (match-end 0) (1- (length prefix))))
3289         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3290
3291 (defun gnus-group-foreign-p (group)
3292   "Return nil if GROUP is native, non-nil if it is foreign."
3293   (string-match ":" group))
3294
3295 (defun gnus-group-set-info (info &optional method-only-group part)
3296   (let* ((entry (gnus-gethash
3297                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3298          (part-info info)
3299          (info (if method-only-group (nth 2 entry) info)))
3300     (if (not method-only-group)
3301         ()
3302       (or entry
3303           (error "Trying to change non-existent group %s" method-only-group))
3304       ;; We have recevied parts of the actual group info - either the
3305       ;; select method or the group parameters.  We first check
3306       ;; whether we have to extend the info, and if so, do that.
3307       (let ((len (length info))
3308             (total (if (eq part 'method) 5 6)))
3309         (and (< len total)
3310              (setcdr (nthcdr (1- len) info)
3311                      (make-list (- total len) nil)))
3312         ;; Then we enter the new info.
3313         (setcar (nthcdr (1- total) info) part-info)))
3314     ;; We uncompress some lists of marked articles.
3315     (let (marked)
3316       (if (not (setq marked (nth 3 info)))
3317           ()
3318         (while marked
3319           (or (eq 'score (car (car marked)))
3320               (eq 'bookmark (car (car marked)))
3321               (eq 'killed (car (car marked)))
3322               (setcdr (car marked) 
3323                       (gnus-uncompress-range (cdr (car marked)))))
3324           (setq marked (cdr marked)))))
3325     (if entry
3326         ()
3327       ;; This is a new group, so we just create it.
3328       (save-excursion
3329         (set-buffer gnus-group-buffer)
3330         (if (nth 4 info)
3331             ;; It's a foreign group...
3332             (gnus-group-make-group 
3333              (gnus-group-real-name (car info))
3334              (prin1-to-string (car (nth 4 info)))
3335              (nth 1 (nth 4 info)))
3336           ;; It's a native group.
3337           (gnus-group-make-group
3338            (car info)
3339            (prin1-to-string (car gnus-select-method))
3340            (nth 1 gnus-select-method)))
3341         (gnus-message 6 "Note: New group created")
3342         (setq entry 
3343               (gnus-gethash (gnus-group-prefixed-name 
3344                              (gnus-group-real-name (car info))
3345                              (or (nth 4 info) gnus-select-method))
3346                             gnus-newsrc-hashtb))))
3347     ;; Whether it was a new group or not, we now have the entry, so we
3348     ;; can do the update.
3349     (if entry
3350         (progn
3351           (setcar (nthcdr 2 entry) info)
3352           (if (and (not (eq (car entry) t)) 
3353                    (gnus-gethash (car info) gnus-active-hashtb))
3354               (let ((marked (nth 3 info)))
3355                 (setcar entry 
3356                         (max 0 (- (length (gnus-list-of-unread-articles 
3357                                            (car info)))
3358                                   (length (cdr (assq 'tick marked)))
3359                                   (length (cdr (assq 'dormant marked)))))))))
3360       (error "No such group: %s" (car info)))))
3361
3362 (defun gnus-group-set-method-info (group select-method)
3363   (gnus-group-set-info select-method group 'method))
3364
3365 (defun gnus-group-set-params-info (group params)
3366   (gnus-group-set-info params group 'params))
3367
3368 (defun gnus-group-update-group-line ()
3369   "This function updates the current line in the newsgroup buffer and
3370 moves the point to the colon."
3371   (let* ((buffer-read-only nil)
3372          (group (gnus-group-group-name))
3373          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3374     (if entry
3375         (gnus-dribble-enter 
3376          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3377                  ")")))
3378     (beginning-of-line)
3379     (delete-region (point) (progn (forward-line 1) (point)))
3380     (gnus-group-insert-group-line-info group)
3381     (forward-line -1)
3382     (gnus-group-position-cursor)))
3383
3384 (defun gnus-group-insert-group-line-info (group)
3385   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3386         active info)
3387     (if entry
3388         (progn
3389           (setq info (nth 2 entry))
3390           (gnus-group-insert-group-line 
3391            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3392       (setq active (gnus-gethash group gnus-active-hashtb))
3393       (gnus-group-insert-group-line 
3394        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3395                    gnus-level-killed)
3396        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3397
3398 (defun gnus-group-insert-group-line (gformat group level marked number method)
3399   (let* ((gformat (or gformat gnus-group-line-format-spec))
3400          (active (gnus-gethash group gnus-active-hashtb))
3401          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3402          (number-of-dormant (length (cdr (assq 'dormant marked))))
3403          (number-of-ticked (length (cdr (assq 'tick marked))))
3404          (number-of-ticked-and-dormant
3405           (+ number-of-ticked number-of-dormant))
3406          (number-of-unread-unticked 
3407           (if (numberp number) (int-to-string (max 0 number))
3408             "*"))
3409          (number-of-read
3410           (if (numberp number)
3411               (max 0 (- number-total number))
3412             "*"))
3413          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3414                            ((<= level gnus-level-unsubscribed) ?U)
3415                            ((= level gnus-level-zombie) ?Z)
3416                            (t ?K)))
3417          (qualified-group (gnus-group-real-name group))
3418          (newsgroup-description 
3419           (if gnus-description-hashtb
3420               (or (gnus-gethash group gnus-description-hashtb) "")
3421             ""))
3422          (moderated (if (member group gnus-moderated-list) ?m ? ))
3423          (moderated-string (if (eq moderated ?m) "(m)" ""))
3424          (method (gnus-server-get-method group method))
3425          (news-server (or (car (cdr method)) ""))
3426          (news-method (or (car method) ""))
3427          (news-method-string 
3428           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3429          (marked (if (and 
3430                       (numberp number) 
3431                       (zerop number)
3432                       (> number-of-ticked 0))
3433                      ?* ? ))
3434          (number (if (eq number t) "*" (+ number number-of-dormant 
3435                                           number-of-ticked)))
3436          (process-marked (if (member qualified-group gnus-group-marked)
3437                              gnus-process-mark ? ))
3438          (buffer-read-only nil)
3439          b)
3440     (beginning-of-line)
3441     (setq b (point))
3442     ;; Insert the text.
3443     (insert (eval gformat))
3444
3445     (add-text-properties 
3446      b (1+ b) (list 'gnus-group (intern group)
3447                     'gnus-unread (if (numberp number)
3448                                      (string-to-int number-of-unread-unticked)
3449                                    t)
3450                     'gnus-marked marked
3451                     'gnus-level level))))
3452
3453 (defun gnus-group-update-group (group &optional visible-only)
3454   "Update newsgroup info of GROUP.
3455 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3456   (save-excursion
3457     (set-buffer gnus-group-buffer)
3458     (let ((buffer-read-only nil)
3459           visible)
3460       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3461         (if entry
3462             (gnus-dribble-enter 
3463              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3464                      ")"))))
3465       ;; Buffer may be narrowed.
3466       (save-restriction
3467         (widen)
3468         ;; Search a line to modify.  If the buffer is large, the search
3469         ;; takes long time.  In most cases, current point is on the line
3470         ;; we are looking for.  So, first of all, check current line. 
3471         (if (or (progn
3472                   (beginning-of-line)
3473                   (eq (get-text-property (point) 'gnus-group)
3474                       (intern group)))
3475                 (progn
3476                   (gnus-goto-char 
3477                    (text-property-any 
3478                     (point-min) (point-max) 'gnus-group (intern group)))))
3479             ;; GROUP is listed in current buffer. So, delete old line.
3480             (progn
3481               (setq visible t)
3482               (beginning-of-line)
3483               (delete-region (point) (progn (forward-line 1) (point))))
3484           ;; No such line in the buffer, find out where it's supposed to
3485           ;; go, and insert it there (or at the end of the buffer).
3486           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3487           (or visible-only
3488               (let ((entry 
3489                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3490                 (while (and entry
3491                             (car entry)
3492                             (not
3493                              (gnus-goto-char
3494                               (text-property-any
3495                                (point-min) (point-max) 
3496                                'gnus-group (intern (car (car entry)))))))
3497                   (setq entry (cdr entry)))
3498                 (or entry (goto-char (point-max)))))))
3499       (if (or visible (not visible-only))
3500           (gnus-group-insert-group-line-info group))
3501       (gnus-group-set-mode-line))))
3502
3503 (defun gnus-group-set-mode-line ()
3504   (if (memq 'group gnus-updated-mode-lines)
3505       (let* ((gformat (or gnus-group-mode-line-format-spec
3506                           (setq gnus-group-mode-line-format-spec
3507                                 (gnus-parse-format 
3508                                  gnus-group-mode-line-format 
3509                                  gnus-group-mode-line-format-alist))))
3510              (news-server (car (cdr gnus-select-method)))
3511              (news-method (car gnus-select-method))
3512              (max-len 60)
3513              (mode-string (eval gformat)))
3514         (setq mode-string (eval gformat))
3515         (if (> (length mode-string) max-len) 
3516             (setq mode-string (substring mode-string 0 (- max-len 4))))
3517         (setq mode-line-buffer-identification mode-string)
3518         (set-buffer-modified-p t))))
3519
3520 (defun gnus-group-group-name ()
3521   "Get the name of the newsgroup on the current line."
3522   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3523     (and group (symbol-name group))))
3524
3525 (defun gnus-group-group-level ()
3526   "Get the level of the newsgroup on the current line."
3527   (get-text-property (gnus-point-at-bol) 'gnus-level))
3528
3529 (defun gnus-group-search-forward (&optional backward all level first-too)
3530   "Find the next newsgroup with unread articles.
3531 If BACKWARD is non-nil, find the previous newsgroup instead.
3532 If ALL is non-nil, just find any newsgroup.
3533 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3534 group exists.
3535 If FIRST-TOO, the current line is also eligible as a target."
3536   (let ((way (if backward -1 1))
3537         (low 10)
3538         (beg (point))
3539         pos found)
3540     (if (and backward (progn (beginning-of-line)) (bobp))
3541         nil
3542       (or first-too (forward-line way))
3543       (while (and 
3544               (not (eobp))
3545               (not (setq 
3546                     found 
3547                     (and (or all
3548                              (and
3549                               (let ((unread 
3550                                      (get-text-property (point) 'gnus-unread)))
3551                                 (or (eq unread t) (and unread (> unread 0))))
3552                               (let ((lev (get-text-property
3553                                           (point) 'gnus-level)))
3554                                 (and lev (<= (get-text-property 
3555                                               (point) 'gnus-level)
3556                                              gnus-level-subscribed)))))
3557                          (or (not level)
3558                              (let ((lev (get-text-property (point) 'gnus-level)))
3559                                (if (and lev (<= lev level))
3560                                    t
3561                                  (if (< lev low)
3562                                      (progn
3563                                        (setq low lev)
3564                                        (setq pos (point))))
3565                                  nil))))))
3566               (zerop (forward-line way)))))
3567     (if found 
3568         (progn (gnus-group-position-cursor) t)
3569       (if pos (goto-char pos) (goto-char beg))
3570       nil)))
3571
3572 ;;; Gnus group mode commands
3573
3574 ;; Group marking.
3575
3576 (defun gnus-group-mark-group (n &optional unmark)
3577   "Mark the current group."
3578   (interactive "p")
3579   (let ((buffer-read-only nil)
3580         group)
3581     (while 
3582         (and (> n 0) 
3583              (setq group (gnus-group-group-name))
3584              (progn
3585                (beginning-of-line)
3586                (forward-char 2)
3587                (delete-char 1)
3588                (if unmark
3589                    (progn
3590                      (insert " ")
3591                      (setq gnus-group-marked (delete group gnus-group-marked)))
3592                  (insert "#")
3593                  (setq gnus-group-marked
3594                        (cons group (delete group gnus-group-marked))))
3595                t)
3596              (zerop (gnus-group-next-group 1)))
3597       (setq n (1- n)))
3598     (gnus-summary-position-cursor)
3599     n))
3600
3601 (defun gnus-group-unmark-group (n)
3602   "Remove the mark from the current group."
3603   (interactive "p")
3604   (gnus-group-mark-group n 'unmark))
3605
3606 (defun gnus-group-mark-region (unmark beg end)
3607   "Mark all groups between point and mark.
3608 If UNMARK, remove the mark instead."
3609   (interactive "P\nr")
3610   (let ((num (count-lines beg end)))
3611     (save-excursion
3612       (goto-char beg)
3613       (- num (gnus-group-mark-group num unmark)))))
3614
3615 (defun gnus-group-remove-mark (group)
3616   (and (gnus-group-goto-group group)
3617        (save-excursion
3618          (gnus-group-mark-group 1 'unmark))))
3619
3620 ;; Return a list of groups to work on.  Take into consideration N (the
3621 ;; prefix) and the list of marked groups.
3622 (defun gnus-group-process-prefix (n)
3623   (cond (n
3624          (setq n (prefix-numeric-value n))
3625          ;; There is a prefix, so we return a list of the N next
3626          ;; groups. 
3627          (let ((way (if (< n 0) -1 1))
3628                (n (abs n))
3629                group groups)
3630            (save-excursion
3631              (while (and (> n 0)
3632                          (setq group (gnus-group-group-name)))
3633                (setq groups (cons group groups))
3634                (setq n (1- n))
3635                (forward-line way)))
3636            (nreverse groups)))
3637         (gnus-group-marked
3638          ;; No prefix, but a list of marked articles.
3639          (reverse gnus-group-marked))
3640         (t
3641          ;; Neither marked articles or a prefix, so we return the
3642          ;; current group.
3643          (let ((group (gnus-group-group-name)))
3644            (and group (list group))))))
3645
3646 ;; Selecting groups.
3647
3648 (defun gnus-group-read-group (all &optional no-article group)
3649   "Read news in this newsgroup.
3650 If argument ALL is non-nil, already read articles become readable.
3651 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3652   (interactive "P")
3653   (let ((group (or group (gnus-group-group-name)))
3654         number active marked entry)
3655     (or group (error "No group on current line"))
3656     (setq marked 
3657           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3658     ;; This group might be a dead group. In that case we have to get
3659     ;; the number of unread articles from `gnus-active-hashtb'.
3660     (if entry
3661         (setq number (car entry))
3662       (if (setq active (gnus-gethash group gnus-active-hashtb))
3663           (setq number (- (1+ (cdr active)) (car active)))))
3664     (gnus-summary-read-group 
3665      group (or all (and (numberp number) 
3666                         (zerop (+ number (length (cdr (assq 'tick marked)))
3667                                   (length (cdr (assq 'dormant marked)))))))
3668      no-article)))
3669
3670 (defun gnus-group-select-group (all)
3671   "Select this newsgroup.
3672 No article is selected automatically.
3673 If argument ALL is non-nil, already read articles become readable."
3674   (interactive "P")
3675   (gnus-group-read-group all t))
3676
3677 ;; Enter a group that is not in the group buffer. Non-nil is returned
3678 ;; if selection was successful.
3679 (defun gnus-group-read-ephemeral-group 
3680   (group method &optional activate quit-config)
3681   (let ((group (if (gnus-group-foreign-p group) group
3682                  (gnus-group-prefixed-name group method))))
3683     (gnus-sethash 
3684      group
3685      (list t nil (list group gnus-level-default-subscribed nil nil 
3686                        (append method
3687                                (list
3688                                 (list 'quit-config 
3689                                       (if quit-config quit-config
3690                                         (cons (current-buffer) 'summary)))))))
3691      gnus-newsrc-hashtb)
3692     (set-buffer gnus-group-buffer)
3693     (or (gnus-server-opened method)
3694         (gnus-open-server method)
3695         (error "Unable to contact server: %s" (gnus-status-message method)))
3696     (if activate (gnus-request-group group))
3697     (condition-case ()
3698         (gnus-group-read-group t t group)
3699       (error nil)
3700       (quit nil))
3701     (not (equal major-mode 'gnus-group-mode))))
3702   
3703 (defun gnus-group-jump-to-group (group)
3704   "Jump to newsgroup GROUP."
3705   (interactive 
3706    (list (completing-read 
3707           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3708
3709   (if (equal group "")
3710       (error "Empty group name"))
3711
3712   (let ((b (text-property-any 
3713             (point-min) (point-max) 'gnus-group (intern group))))
3714     (if b
3715         ;; Either go to the line in the group buffer...
3716         (goto-char b)
3717       ;; ... or insert the line.
3718       (or
3719        (gnus-gethash group gnus-active-hashtb)
3720        (gnus-activate-newsgroup group)
3721        (error "%s error: %s" group (gnus-status-message group)))
3722
3723       (gnus-group-update-group group)
3724       (goto-char (text-property-any 
3725                   (point-min) (point-max) 'gnus-group (intern group)))))
3726   ;; Adjust cursor point.
3727   (gnus-group-position-cursor))
3728
3729 (defun gnus-group-goto-group (group)
3730   "Goto to newsgroup GROUP."
3731   (let ((b (text-property-any (point-min) (point-max) 
3732                               'gnus-group (intern group))))
3733     (and b (goto-char b))))
3734
3735 (defun gnus-group-next-group (n)
3736   "Go to next N'th newsgroup.
3737 If N is negative, search backward instead.
3738 Returns the difference between N and the number of skips actually
3739 done."
3740   (interactive "p")
3741   (gnus-group-next-unread-group n t))
3742
3743 (defun gnus-group-next-unread-group (n &optional all level)
3744   "Go to next N'th unread newsgroup.
3745 If N is negative, search backward instead.
3746 If ALL is non-nil, choose any newsgroup, unread or not.
3747 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3748 such group can be found, the next group with a level higher than
3749 LEVEL.
3750 Returns the difference between N and the number of skips actually
3751 made."
3752   (interactive "p")
3753   (let ((backward (< n 0))
3754         (n (abs n)))
3755     (while (and (> n 0)
3756                 (gnus-group-search-forward 
3757                  backward (or (not gnus-group-goto-unread) all) level))
3758       (setq n (1- n)))
3759     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3760                                (if level " on this level or higher" "")))
3761     n))
3762
3763 (defun gnus-group-prev-group (n)
3764   "Go to previous N'th newsgroup.
3765 Returns the difference between N and the number of skips actually
3766 done."
3767   (interactive "p")
3768   (gnus-group-next-unread-group (- n) t))
3769
3770 (defun gnus-group-prev-unread-group (n)
3771   "Go to previous N'th unread newsgroup.
3772 Returns the difference between N and the number of skips actually
3773 done."  
3774   (interactive "p")
3775   (gnus-group-next-unread-group (- n)))
3776
3777 (defun gnus-group-next-unread-group-same-level (n)
3778   "Go to next N'th unread newsgroup on the same level.
3779 If N is negative, search backward instead.
3780 Returns the difference between N and the number of skips actually
3781 done."
3782   (interactive "p")
3783   (gnus-group-next-unread-group n t (gnus-group-group-level))
3784   (gnus-group-position-cursor))
3785
3786 (defun gnus-group-prev-unread-group-same-level (n)
3787   "Go to next N'th unread newsgroup on the same level.
3788 Returns the difference between N and the number of skips actually
3789 done."
3790   (interactive "p")
3791   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3792   (gnus-group-position-cursor))
3793
3794 (defun gnus-group-best-unread-group (&optional exclude-group)
3795   "Go to the group with the highest level.
3796 If EXCLUDE-GROUP, do not go to that group."
3797   (interactive)
3798   (goto-char (point-min))
3799   (let ((best 100000)
3800         unread best-point)
3801     (while (setq unread (get-text-property (point) 'gnus-unread))
3802       (if (and (numberp unread) (> unread 0))
3803           (progn
3804             (or best-point (setq best-point (point)))
3805             (if (and (< (get-text-property (point) 'gnus-level) best)
3806                      (or (not exclude-group)
3807                          (not (equal exclude-group (gnus-group-group-name)))))
3808                 (progn 
3809                   (setq best (get-text-property (point) 'gnus-level))
3810                   (setq best-point (point))))))
3811       (forward-line 1))
3812     (if best-point (goto-char best-point))
3813     (gnus-summary-position-cursor)
3814     (and best-point (gnus-group-group-name))))
3815
3816 (defun gnus-group-first-unread-group ()
3817   "Go to the first group with unread articles."
3818   (interactive)
3819   (goto-char (point-min))
3820   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3821       (gnus-group-next-unread-group 1))
3822   (gnus-group-position-cursor))
3823
3824 (defun gnus-group-enter-server-mode ()
3825   "Jump to the server buffer."
3826   (interactive)
3827   (gnus-server-setup-buffer)
3828   (gnus-configure-windows 'server)
3829   (gnus-server-prepare))
3830
3831 (defun gnus-group-make-group (name method &optional address)
3832   "Add a new newsgroup.
3833 The user will be prompted for a NAME, for a select METHOD, and an
3834 ADDRESS."
3835   (interactive
3836    (cons 
3837     (read-string "Group name: ")
3838     (let ((method
3839            (completing-read 
3840             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3841             nil t)))
3842       (if (assoc method gnus-valid-select-methods)
3843           (list method
3844                 (if (memq 'prompt-address
3845                           (assoc method gnus-valid-select-methods))
3846                     (read-string "Address: ")
3847                   ""))
3848         (list method nil)))))
3849   
3850   (let* ((meth (if address (list (intern method) address) method))
3851          (nname (gnus-group-prefixed-name name meth))
3852          info)
3853     (and (gnus-gethash nname gnus-newsrc-hashtb)
3854          (error "Group %s already exists" nname))
3855     (gnus-group-change-level 
3856      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3857      gnus-level-default-subscribed gnus-level-killed 
3858      (and (gnus-group-group-name)
3859           (gnus-gethash (gnus-group-group-name)
3860                         gnus-newsrc-hashtb))
3861      t)
3862     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3863     (gnus-dribble-enter 
3864      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3865     (gnus-group-insert-group-line-info nname)
3866
3867     (and (gnus-check-backend-function 'request-create-group nname)
3868          (gnus-request-create-group nname))))
3869
3870 (defun gnus-group-edit-group (group &optional part)
3871   "Edit the group on the current line."
3872   (interactive (list (gnus-group-group-name)))
3873   (let ((done-func '(lambda () 
3874                       "Exit editing mode and update the information."
3875                       (interactive)
3876                       (gnus-group-edit-group-done 'part 'group)))
3877         (part (or part 'info))
3878         (winconf (current-window-configuration))
3879         info)
3880     (or group (error "No group on current line"))
3881     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3882         (error "Killed group; can't be edited"))
3883     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3884     (gnus-configure-windows 'edit-group)
3885     (gnus-add-current-to-buffer-list)
3886     (emacs-lisp-mode)
3887     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3888     (use-local-map (copy-keymap emacs-lisp-mode-map))
3889     (local-set-key "\C-c\C-c" done-func)
3890     (make-local-variable 'gnus-prev-winconf)
3891     (setq gnus-prev-winconf winconf)
3892     ;; We modify the func to let it know what part it is editing.
3893     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3894     (setcar (cdr (cdr (nth 4 done-func))) group)
3895     (erase-buffer)
3896     (insert
3897      (cond 
3898       ((eq part 'method)
3899        ";; Type `C-c C-c' after editing the select method.\n\n")
3900       ((eq part 'params)
3901        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3902       ((eq part 'info)
3903        ";; Type `C-c C-c' after editing the group info.\n\n")))
3904     (let ((cinfo (gnus-copy-sequence info))
3905           marked)
3906       (if (not (setq marked (nth 3 cinfo)))
3907           ()
3908         (while marked
3909           (or (eq 'score (car (car marked)))
3910               (eq 'bookmark (car (car marked)))
3911               (eq 'killed (car (car marked)))
3912               (not (numberp (car (cdr (car marked)))))
3913               (setcdr (car marked) 
3914                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3915           (setq marked (cdr marked))))
3916       (insert 
3917        (pp-to-string
3918         (cond ((eq part 'method)
3919                (or (nth 4 info) "native"))
3920               ((eq part 'params)
3921                (nth 5 info))
3922               (t
3923                cinfo)))
3924        "\n"))))
3925
3926 (defun gnus-group-edit-group-method (group)
3927   "Edit the select method of GROUP."
3928   (interactive (list (gnus-group-group-name)))
3929   (gnus-group-edit-group group 'method))
3930
3931 (defun gnus-group-edit-group-parameters (group)
3932   "Edit the group parameters of GROUP."
3933   (interactive (list (gnus-group-group-name)))
3934   (gnus-group-edit-group group 'params))
3935
3936 (defun gnus-group-edit-group-done (part group)
3937   "Get info from buffer, update variables and jump to the group buffer."
3938   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3939   (goto-char (point-min))
3940   (let ((form (read (current-buffer)))
3941         (winconf gnus-prev-winconf))
3942     (if (eq part 'info) 
3943         (gnus-group-set-info form)
3944       (gnus-group-set-info form group part))
3945     (kill-buffer (current-buffer))
3946     (and winconf (set-window-configuration winconf))
3947     (set-buffer gnus-group-buffer)
3948     (gnus-group-update-group (gnus-group-group-name))
3949     (gnus-group-position-cursor)))
3950
3951 (defun gnus-group-make-help-group ()
3952   "Create the (ding) Gnus documentation group."
3953   (interactive)
3954   (let ((path load-path))
3955     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3956                                    "gnus-help" '(nndoc "gnus-help")))
3957                        gnus-newsrc-hashtb)
3958          (error "Documentation group already exists"))
3959     (while (and path
3960                 (not (file-exists-p (concat (file-name-as-directory (car path))
3961                                             "doc.txt"))))
3962       (setq path (cdr path)))
3963     (or path (error "Couldn't find doc group"))
3964     (gnus-group-make-group 
3965      (gnus-group-real-name name)
3966      (list 'nndoc name
3967            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3968            (list 'nndoc-article-type 'mbox))))
3969   (gnus-group-position-cursor))
3970
3971 (defun gnus-group-make-doc-group (file type)
3972   "Create a group that uses a single file as the source."
3973   (interactive 
3974    (list (read-file-name "File name: ") 
3975          (let ((err "")
3976                found char)
3977            (while (not found)
3978              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3979              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3980                                ((= char ?b) 'babyl)
3981                                ((= char ?d) 'digest)
3982                                (t (setq mess "%c unknown. " char)
3983                                   nil))))
3984            found)))
3985   (let* ((file (expand-file-name file))
3986          (name (gnus-generate-new-group-name
3987                 (gnus-group-prefixed-name
3988                  (file-name-nondirectory file) '(nndoc "")))))
3989     (gnus-group-make-group 
3990      (gnus-group-real-name name)
3991      (list 'nndoc name
3992            (list 'nndoc-address file)
3993            (list 'nndoc-article-type type)))))
3994
3995 (defun gnus-group-make-archive-group ()
3996   "Create the (ding) Gnus archive group."
3997   (interactive)
3998   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3999                      gnus-newsrc-hashtb)
4000        (error "Archive group already exists"))
4001   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4002   (gnus-group-position-cursor))
4003
4004 (defun gnus-group-make-directory-group (dir)
4005   "Create an nndir group.
4006 The user will be prompted for a directory. The contents of this
4007 directory will be used as a newsgroup. The directory should contain
4008 mail messages or news articles in files that have numeric names."
4009   (interactive
4010    (list (read-file-name "Create group from directory: ")))
4011   (or (file-exists-p dir) (error "No such directory"))
4012   (or (file-directory-p dir) (error "Not a directory"))
4013   (gnus-group-make-group dir "nndir" dir)
4014   (gnus-group-position-cursor))
4015
4016 (defun gnus-group-make-kiboze-group (group address scores)
4017   "Create an nnkiboze group.
4018 The user will be prompted for a name, a regexp to match groups, and
4019 score file entries for articles to include in the group."
4020   (interactive
4021    (list
4022     (read-string "nnkiboze group name: ")
4023     (read-string "Source groups (regexp): ")
4024     (let ((headers (mapcar (lambda (group) (list group))
4025                            '("subject" "from" "number" "date" "message-id"
4026                              "references" "chars" "lines" "xref")))
4027           scores header regexp regexps)
4028       (while (not (equal "" (setq header (completing-read 
4029                                           "Match on header: " headers nil t))))
4030         (setq regexps nil)
4031         (while (not (equal "" (setq regexp (read-string 
4032                                             (format "Match on %s (string): "
4033                                                     header)))))
4034           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4035         (setq scores (cons (cons header regexps) scores)))
4036       scores)))
4037   (gnus-group-make-group group "nnkiboze" address)
4038   (save-excursion
4039     (gnus-set-work-buffer)
4040     (let (emacs-lisp-mode-hook)
4041       (pp scores (current-buffer)))
4042     (write-region (point-min) (point-max) 
4043                   (concat (or gnus-kill-files-directory "~/News")
4044                           "nnkiboze:" group "." gnus-score-file-suffix)))
4045   (gnus-group-position-cursor))
4046
4047 (defun gnus-group-add-to-virtual (n vgroup)
4048   "Add the current group to a virtual group."
4049   (interactive
4050    (list current-prefix-arg
4051          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4052                           "nnvirtual:")))
4053   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4054       (error "%s is not an nnvirtual group" vgroup))
4055   (let* ((groups (gnus-group-process-prefix n))
4056          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4057     (setcar (cdr method)
4058             (concat 
4059              (nth 1 method) "\\|"
4060              (mapconcat 
4061               (lambda (s) 
4062                 (gnus-group-remove-mark s)
4063                 (concat "\\(^" (regexp-quote s) "$\\)"))
4064               groups "\\|"))))
4065   (gnus-group-position-cursor))
4066
4067 (defun gnus-group-make-empty-virtual (group)
4068   "Create a new, fresh, empty virtual group."
4069   (interactive "sCreate new, empty virtual group: ")
4070   (let* ((method (list 'nnvirtual "^$"))
4071          (pgroup (gnus-group-prefixed-name group method)))
4072     ;; Check whether it exists already.
4073     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4074          (error "Group %s already exists." pgroup))
4075     ;; Subscribe the new group after the group on the current line.
4076     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4077     (gnus-group-update-group pgroup)
4078     (forward-line -1)
4079     (gnus-group-position-cursor)))
4080
4081 (defun gnus-group-enter-directory (dir)
4082   "Enter an ephemeral nneething group."
4083   (interactive "DDirectory to read: ")
4084   (let* ((method (list 'nneething dir))
4085          (leaf (gnus-group-prefixed-name
4086                 (file-name-nondirectory (directory-file-name dir))
4087                 method))
4088          (name (gnus-generate-new-group-name leaf))
4089          (num 0))
4090     (let ((nneething-read-only t))
4091       (or (gnus-group-read-ephemeral-group 
4092            name method t
4093            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4094                                       'summary 'group)))
4095           (error "Couldn't enter %s" dir)))))
4096
4097 ;; Group sorting commands
4098 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4099
4100 (defun gnus-group-sort-groups ()
4101   "Sort the group buffer using `gnus-group-sort-function'."
4102   (interactive)
4103   (setq gnus-newsrc-alist 
4104         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4105   (gnus-make-hashtable-from-newsrc-alist)
4106   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4107
4108 (defun gnus-group-sort-by-alphabet (info1 info2)
4109   (string< (car info1) (car info2)))
4110
4111 (defun gnus-group-sort-by-unread (info1 info2)
4112   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4113         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4114     (< (or (and (numberp n1) n1) 0)
4115        (or (and (numberp n2) n2) 0))))
4116
4117 (defun gnus-group-sort-by-level (info1 info2)
4118   (< (nth 1 info1) (nth 1 info2)))
4119
4120 ;; Group catching up.
4121
4122 (defun gnus-group-catchup-current (n &optional all)
4123   "Mark all articles not marked as unread in current newsgroup as read.
4124 If prefix argument N is numeric, the ARG next newsgroups will be
4125 caught up. If ALL is non-nil, marked articles will also be marked as
4126 read. Cross references (Xref: header) of articles are ignored.
4127 The difference between N and actual number of newsgroups that were
4128 caught up is returned."
4129   (interactive "P")
4130   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4131                gnus-expert-user
4132                (gnus-y-or-n-p
4133                 (if all
4134                     "Do you really want to mark all articles as read? "
4135                   "Mark all unread articles as read? "))))
4136       n
4137     (let ((groups (gnus-group-process-prefix n))
4138           (ret 0))
4139       (while groups
4140         ;; Virtual groups have to be given special treatment. 
4141         (let ((method (gnus-find-method-for-group (car groups))))
4142           (if (eq 'nnvirtual (car method))
4143               (nnvirtual-catchup-group
4144                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4145         (gnus-group-remove-mark (car groups))
4146         (if (prog1
4147                 (gnus-group-goto-group (car groups))
4148               (gnus-group-catchup (car groups) all))
4149             (gnus-group-update-group-line)
4150           (setq ret (1+ ret)))
4151         (setq groups (cdr groups)))
4152       (gnus-group-next-unread-group 1)
4153       ret)))
4154
4155 (defun gnus-group-catchup-current-all (n)
4156   "Mark all articles in current newsgroup as read.
4157 Cross references (Xref: header) of articles are ignored."
4158   (interactive "P")
4159   (gnus-group-catchup-current n 'all))
4160
4161 (defun gnus-group-catchup (group &optional all)
4162   "Mark all articles in GROUP as read.
4163 If ALL is non-nil, all articles are marked as read.
4164 The return value is the number of articles that were marked as read,
4165 or nil if no action could be taken."
4166   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4167          (num (car entry))
4168          (marked (nth 3 (nth 2 entry)))
4169          ticked)
4170     (if (not (numberp (car entry)))
4171         (gnus-message 1 "Can't catch up; non-active group")
4172       ;; Do the updating only if the newsgroup isn't killed.
4173       (if (not entry)
4174           ()
4175         (gnus-update-read-articles 
4176          group (and (not all) (append (cdr (assq 'tick marked))
4177                                       (cdr (assq 'dormant marked))))
4178          nil (and (not all) (cdr (assq 'tick marked))))
4179         (and all marked
4180              (setcar (nthcdr 3 (nth 2 entry)) 
4181                      (delq (assq 'dormant marked) 
4182                            (nth 3 (nth 2 entry)))))))
4183     num))
4184
4185 (defun gnus-group-expire-articles (n)
4186   "Expire all expirable articles in the current newsgroup."
4187   (interactive "P")
4188   (let ((groups (gnus-group-process-prefix n))
4189         group)
4190     (or groups (error "No groups to expire"))
4191     (while groups
4192       (setq group (car groups)
4193             groups (cdr groups))
4194       (gnus-group-remove-mark group)
4195       (if (not (gnus-check-backend-function 'request-expire-articles group))
4196           ()
4197         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4198                (expirable (if (memq 'total-expire (nth 5 info))
4199                               (cons nil (gnus-list-of-read-articles group))
4200                             (assq 'expire (nth 3 info)))))
4201           (and expirable 
4202                (setcdr expirable
4203                        (gnus-request-expire-articles 
4204                         (cdr expirable) group))))))))
4205
4206 (defun gnus-group-expire-all-groups ()
4207   "Expire all expirable articles in all newsgroups."
4208   (interactive)
4209   (gnus-message 5 "Expiring...")
4210   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4211                                    (cdr gnus-newsrc-alist))))
4212     (gnus-group-expire-articles nil))
4213   (gnus-message 5 "Expiring...done"))
4214
4215 (defun gnus-group-set-current-level (n level)
4216   "Set the level of the next N groups to LEVEL."
4217   (interactive "P\nnLevel: ")
4218   (or (and (>= level 1) (<= level gnus-level-killed))
4219       (error "Illegal level: %d" level))
4220   (let ((groups (gnus-group-process-prefix n))
4221         group)
4222     (while groups
4223       (setq group (car groups)
4224             groups (cdr groups))
4225       (gnus-group-remove-mark group)
4226       (gnus-message 6 "Changed level of %s from %d to %d" 
4227                     group (gnus-group-group-level) level)
4228       (gnus-group-change-level group level
4229                                (gnus-group-group-level))
4230       (gnus-group-update-group-line)))
4231   (gnus-group-position-cursor))
4232
4233 (defun gnus-group-unsubscribe-current-group (n)
4234   "Toggle subscription of the current group.
4235 If given numerical prefix, toggle the N next groups."
4236   (interactive "P")
4237   (let ((groups (gnus-group-process-prefix n))
4238         group)
4239     (while groups
4240       (setq group (car groups)
4241             groups (cdr groups))
4242       (gnus-group-remove-mark group)
4243       (gnus-group-unsubscribe-group
4244        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4245                  gnus-level-default-unsubscribed
4246                gnus-level-default-subscribed))
4247       (gnus-group-update-group-line))
4248     (gnus-group-next-group 1)))
4249
4250 (defun gnus-group-unsubscribe-group (group &optional level)
4251   "Toggle subscribe from/to unsubscribe GROUP.
4252 New newsgroup is added to .newsrc automatically."
4253   (interactive
4254    (list (completing-read "Group: " gnus-active-hashtb nil 
4255                           gnus-have-read-active-file)))
4256   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4257     (cond (newsrc
4258            ;; Toggle subscription flag.
4259            (gnus-group-change-level 
4260             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4261                                            gnus-level-subscribed) 
4262                                        (1+ gnus-level-subscribed)
4263                                      gnus-level-default-subscribed)))
4264            (gnus-group-update-group group))
4265           ((and (stringp group)
4266                 (or (not gnus-have-read-active-file)
4267                     (gnus-gethash group gnus-active-hashtb)))
4268            ;; Add new newsgroup.
4269            (gnus-group-change-level 
4270             group 
4271             (if level level gnus-level-default-subscribed) 
4272             (or (and (member group gnus-zombie-list) 
4273                      gnus-level-zombie) 
4274                 gnus-level-killed)
4275             (and (gnus-group-group-name)
4276                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4277            (gnus-group-update-group group))
4278           (t (error "No such newsgroup: %s" group)))
4279     (gnus-group-position-cursor)))
4280
4281 (defun gnus-group-transpose-groups (n)
4282   "Move the current newsgroup up N places.
4283 If given a negative prefix, move down instead. The difference between
4284 N and the number of steps taken is returned." 
4285   (interactive "p")
4286   (or (gnus-group-group-name)
4287       (error "No group on current line"))
4288   (gnus-group-kill-group 1)
4289   (prog1
4290       (forward-line (- n))
4291     (gnus-group-yank-group)
4292     (gnus-group-position-cursor)))
4293
4294 (defun gnus-group-kill-all-zombies ()
4295   "Kill all zombie newsgroups."
4296   (interactive)
4297   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4298   (setq gnus-zombie-list nil)
4299   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4300   (goto-char (point-min))
4301   (gnus-group-position-cursor))
4302
4303 (defun gnus-group-kill-region (begin end)
4304   "Kill newsgroups in current region (excluding current point).
4305 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4306   (interactive "r")
4307   (let ((lines
4308          ;; Count lines.
4309          (save-excursion
4310            (count-lines
4311             (progn
4312               (goto-char begin)
4313               (beginning-of-line)
4314               (point))
4315             (progn
4316               (goto-char end)
4317               (beginning-of-line)
4318               (point))))))
4319     (goto-char begin)
4320     (beginning-of-line)                 ;Important when LINES < 1
4321     (gnus-group-kill-group lines)))
4322
4323 (defun gnus-group-kill-group (n)
4324   "The the next N groups.
4325 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4326 However, only groups that were alive can be yanked; already killed 
4327 groups or zombie groups can't be yanked.
4328 The return value is the name of the (last) group that was killed."
4329   (interactive "P")
4330   (let ((buffer-read-only nil)
4331         (groups (gnus-group-process-prefix n))
4332         group entry level)
4333     (while groups
4334       (setq group (car groups)
4335             groups (cdr groups))
4336       (gnus-group-remove-mark group)
4337       (setq level (gnus-group-group-level))
4338       (gnus-delete-line)
4339       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4340           (setq gnus-list-of-killed-groups 
4341                 (cons (cons (car entry) (nth 2 entry)) 
4342                       gnus-list-of-killed-groups)))
4343       (gnus-group-change-level 
4344        (if entry entry group) gnus-level-killed (if entry nil level)))
4345     (gnus-group-position-cursor)
4346     group))
4347
4348 (defun gnus-group-yank-group (&optional arg)
4349   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4350 inserting it before the current newsgroup.  The numeric ARG specifies
4351 how many newsgroups are to be yanked.  The name of the (last)
4352 newsgroup yanked is returned."
4353   (interactive "p")
4354   (if (not arg) (setq arg 1))
4355   (let (info group prev)
4356     (while (>= (setq arg (1- arg)) 0)
4357       (if (not (setq info (car gnus-list-of-killed-groups)))
4358           (error "No more newsgroups to yank"))
4359       (setq group (nth 2 info))
4360       ;; Find which newsgroup to insert this one before - search
4361       ;; backward until something suitable is found. If there are no
4362       ;; other newsgroups in this buffer, just make this newsgroup the
4363       ;; first newsgroup.
4364       (setq prev (gnus-group-group-name))
4365       (gnus-group-change-level 
4366        info (nth 2 info) gnus-level-killed 
4367        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4368        t)
4369       (gnus-group-insert-group-line-info (nth 1 info))
4370       (setq gnus-list-of-killed-groups 
4371             (cdr gnus-list-of-killed-groups)))
4372     (forward-line -1)
4373     (gnus-group-position-cursor)
4374     group))
4375       
4376 (defun gnus-group-list-all-groups (arg)
4377   "List all newsgroups with level ARG or lower.
4378 Default is gnus-level-unsubscribed, which lists all subscribed and most
4379 unsubscribed groups."
4380   (interactive "P")
4381   (setq arg (or arg gnus-level-unsubscribed))
4382   (gnus-group-list-groups arg t))
4383
4384 (defun gnus-group-list-killed ()
4385   "List all killed newsgroups in the group buffer."
4386   (interactive)
4387   (if (not gnus-killed-list)
4388       (gnus-message 6 "No killed groups")
4389     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4390     (goto-char (point-min)))
4391   (gnus-group-position-cursor))
4392
4393 (defun gnus-group-list-zombies ()
4394   "List all zombie newsgroups in the group buffer."
4395   (interactive)
4396   (if (not gnus-zombie-list)
4397       (gnus-message 6 "No zombie groups")
4398     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4399     (goto-char (point-min)))
4400   (gnus-group-position-cursor))
4401
4402 (defun gnus-group-get-new-news (&optional arg)
4403   "Get newly arrived articles.
4404 If ARG is non-nil, it should be a number between one and nine to
4405 specify which levels you are interested in re-scanning."
4406   (interactive "P")
4407   (run-hooks 'gnus-get-new-news-hook)
4408   (let ((level arg))
4409     (if gnus-group-use-permanent-levels
4410         (if level
4411             (setq gnus-group-default-list-level level)
4412           (setq level (or gnus-group-default-list-level 
4413                           gnus-level-subscribed))))
4414     (if (and gnus-read-active-file (not level))
4415         (progn
4416           (gnus-read-active-file)
4417           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4418       (let ((gnus-read-active-file nil)
4419             (gnus-have-read-active-file nil))
4420         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4421     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4422                                 gnus-group-default-list-level
4423                                 gnus-level-subscribed)
4424                             gnus-have-all-newsgroups)))
4425
4426 (defun gnus-group-get-new-news-this-group (n)
4427   "Check for newly arrived news in the current group (and the N-1 next groups).
4428 The difference between N and the number of newsgroup checked is returned.
4429 If N is negative, this group and the N-1 previous groups will be checked."
4430   (interactive "P")
4431   (let* ((groups (gnus-group-process-prefix n))
4432          (ret (if (numberp n) (- n (length groups)) 0))
4433          (w-p (window-start))
4434          group)
4435     (while groups
4436       (setq group (car groups)
4437             groups (cdr groups))
4438       (gnus-group-remove-mark group)
4439       (or (gnus-get-new-news-in-group group)
4440           (progn 
4441             (ding) 
4442             (message "%s error: %s" group (gnus-status-message group))
4443             (sit-for 2))))
4444     ;; !!! I don't know why the buffer scrolls forward when updating
4445     ;; the first line in the group buffer, but it does. So we set the
4446     ;; window start forcibly.
4447 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4448     (gnus-group-next-unread-group 1 t)
4449     (gnus-summary-position-cursor)
4450     ret))
4451
4452 (defun gnus-get-new-news-in-group (group)
4453   (and group 
4454        (gnus-activate-newsgroup group)
4455        (progn
4456          (gnus-get-unread-articles-in-group 
4457           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4458           (gnus-gethash group gnus-active-hashtb))
4459          (gnus-group-update-group-line)
4460          t)))
4461
4462 (defun gnus-group-fetch-faq (group)
4463   "Fetch the FAQ for the current group."
4464   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4465   (or group (error "No group name given"))
4466   (let ((file (concat gnus-group-faq-directory group))) 
4467     (if (not (file-exists-p file))
4468         (error "No such file: %s" file)
4469       (find-file file))))
4470   
4471 (defun gnus-group-describe-group (force &optional group)
4472   "Display a description of the current newsgroup."
4473   (interactive (list current-prefix-arg (gnus-group-group-name)))
4474   (and force (setq gnus-description-hashtb nil))
4475   (let ((method (gnus-find-method-for-group group))
4476         desc)
4477     (or group (error "No group name given"))
4478     (and (or (and gnus-description-hashtb
4479                   ;; We check whether this group's method has been
4480                   ;; queried for a description file.  
4481                   (gnus-gethash 
4482                    (gnus-group-prefixed-name "" method) 
4483                    gnus-description-hashtb))
4484              (setq desc (gnus-group-get-description group))
4485              (gnus-read-descriptions-file method))
4486          (message
4487           (or desc (gnus-gethash group gnus-description-hashtb)
4488               "No description available")))))
4489
4490 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4491 (defun gnus-group-describe-all-groups (force)
4492   "Pop up a buffer with descriptions of all newsgroups."
4493   (interactive "P")
4494   (and force (setq gnus-description-hashtb nil))
4495   (if (not (or gnus-description-hashtb
4496                (gnus-read-all-descriptions-files)))
4497       (error "Couldn't request descriptions file"))
4498   (let ((buffer-read-only nil)
4499         b)
4500     (erase-buffer)
4501     (mapatoms
4502      (lambda (group)
4503        (setq b (point))
4504        (insert (format "      *: %-20s %s\n" (symbol-name group)
4505                        (symbol-value group)))
4506        (add-text-properties 
4507         b (1+ b) (list 'gnus-group group
4508                        'gnus-unread t 'gnus-marked nil
4509                        'gnus-level (1+ gnus-level-subscribed))))
4510      gnus-description-hashtb)
4511     (goto-char (point-min))
4512     (gnus-group-position-cursor)))
4513
4514 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4515 (defun gnus-group-apropos (regexp &optional search-description)
4516   "List all newsgroups that have names that match a regexp."
4517   (interactive "sGnus apropos (regexp): ")
4518   (let ((prev "")
4519         (obuf (current-buffer))
4520         groups des prev)
4521     ;; Go through all newsgroups that are known to Gnus.
4522     (mapatoms 
4523      (lambda (group)
4524        (and (string-match regexp (symbol-name group))
4525             (setq groups (cons (symbol-name group) groups))))
4526      gnus-active-hashtb)
4527     ;; Go through all descriptions that are known to Gnus. 
4528     (if search-description
4529         (mapatoms 
4530          (lambda (group)
4531            (and (string-match regexp (symbol-value group))
4532                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4533                 (setq groups (cons (symbol-name group) groups))))
4534          gnus-description-hashtb))
4535     (if (not groups)
4536         (gnus-message 3 "No groups matched \"%s\"." regexp)
4537       ;; Print out all the groups.
4538       (save-excursion
4539         (pop-to-buffer "*Gnus Help*")
4540         (buffer-disable-undo (current-buffer))
4541         (erase-buffer)
4542         (setq groups (sort groups 'string<))
4543         (while groups
4544           ;; Groups may be entered twice into the list of groups.
4545           (if (not (string= (car groups) prev))
4546               (progn
4547                 (insert (setq prev (car groups)) "\n")
4548                 (if (and gnus-description-hashtb
4549                          (setq des (gnus-gethash (car groups) 
4550                                                  gnus-description-hashtb)))
4551                     (insert "  " des "\n"))))
4552           (setq groups (cdr groups)))
4553         (goto-char (point-min))))
4554     (pop-to-buffer obuf)))
4555
4556 (defun gnus-group-description-apropos (regexp)
4557   "List all newsgroups that have names or descriptions that match a regexp."
4558   (interactive "sGnus description apropos (regexp): ")
4559   (if (not (or gnus-description-hashtb
4560                (gnus-read-all-descriptions-files)))
4561       (error "Couldn't request descriptions file"))
4562   (gnus-group-apropos regexp t))
4563
4564 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4565 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4566   "List all groups with unread articles that match REGEXP.
4567 If the prefix LEVEL is non-nil, it should be a number that says which
4568 level to cut off listing groups. 
4569 If ALL, also list groups with no unread articles.
4570 If LOWEST, don't list groups with level lower than LOWEST."
4571   (interactive "P\nsList newsgroups matching: ")
4572   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4573                            all (or lowest 1) regexp)
4574   (goto-char (point-min))
4575   (gnus-group-position-cursor))
4576
4577 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4578   "List all groups that match REGEXP.
4579 If the prefix LEVEL is non-nil, it should be a number that says which
4580 level to cut off listing groups. 
4581 If LOWEST, don't list groups with level lower than LOWEST."
4582   (interactive "P\nsList newsgroups matching: ")
4583   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4584
4585 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4586 (defun gnus-group-save-newsrc ()
4587   "Save the Gnus startup files."
4588   (interactive)
4589   (gnus-save-newsrc-file))
4590
4591 (defun gnus-group-restart (&optional arg)
4592   "Force Gnus to read the .newsrc file."
4593   (interactive "P")
4594   (gnus-save-newsrc-file)
4595   (gnus-setup-news 'force)
4596   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4597
4598 (defun gnus-group-read-init-file ()
4599   "Read the Gnus elisp init file."
4600   (interactive)
4601   (gnus-read-init-file))
4602
4603 (defun gnus-group-check-bogus-groups (silent)
4604   "Check bogus newsgroups.
4605 If given a prefix, don't ask for confirmation before removing a bogus
4606 group."
4607   (interactive "P")
4608   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4609   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4610
4611 (defun gnus-group-edit-global-kill (article &optional group)
4612   "Edit the global kill file.
4613 If GROUP, edit that local kill file instead."
4614   (interactive "P")
4615   (setq gnus-current-kill-article article)
4616   (gnus-kill-file-edit-file group)
4617   (gnus-message 6
4618    (substitute-command-keys
4619     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4620
4621 (defun gnus-group-edit-local-kill (article group)
4622   "Edit a local kill file."
4623   (interactive (list nil (gnus-group-group-name)))
4624   (gnus-group-edit-global-kill article group))
4625
4626 (defun gnus-group-force-update ()
4627   "Update `.newsrc' file."
4628   (interactive)
4629   (gnus-save-newsrc-file))
4630
4631 (defun gnus-group-suspend ()
4632   "Suspend the current Gnus session.
4633 In fact, cleanup buffers except for group mode buffer.
4634 The hook gnus-suspend-gnus-hook is called before actually suspending."
4635   (interactive)
4636   (run-hooks 'gnus-suspend-gnus-hook)
4637   ;; Kill Gnus buffers except for group mode buffer.
4638   (let ((group-buf (get-buffer gnus-group-buffer)))
4639     ;; Do this on a separate list in case the user does a ^G before we finish
4640     (let ((gnus-buffer-list
4641            (delq group-buf (delq gnus-dribble-buffer
4642                                  (append gnus-buffer-list nil)))))
4643       (while gnus-buffer-list
4644         (gnus-kill-buffer (car gnus-buffer-list))
4645         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4646     (if group-buf
4647         (progn
4648           (setq gnus-buffer-list (list group-buf))
4649           (bury-buffer group-buf)
4650           (delete-windows-on group-buf t)))))
4651
4652 (defun gnus-group-clear-dribble ()
4653   "Clear all information from the dribble buffer."
4654   (interactive)
4655   (gnus-dribble-clear))
4656
4657 (defun gnus-group-exit ()
4658   "Quit reading news after updating .newsrc.eld and .newsrc.
4659 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4660   (interactive)
4661   (if (or noninteractive                ;For gnus-batch-kill
4662           (zerop (buffer-size))         ;No news is good news.
4663           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4664           (not gnus-interactive-exit)   ;Without confirmation
4665           gnus-expert-user
4666           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4667       (progn
4668         (if gnus-use-full-window
4669             (delete-other-windows)
4670           (gnus-remove-some-windows))
4671         (run-hooks 'gnus-exit-gnus-hook)
4672         (gnus-offer-save-summaries)
4673         (gnus-save-newsrc-file)
4674         (gnus-close-backends)
4675         (gnus-clear-system))))
4676
4677 (defun gnus-close-backends ()
4678   ;; Send a close request to all backends that support such a request. 
4679   (let ((methods gnus-valid-select-methods)
4680         func)
4681     (while methods
4682       (if (fboundp (setq func (intern (concat (car (car methods))
4683                                               "-request-close"))))
4684           (funcall func))
4685       (setq methods (cdr methods)))))
4686
4687 (defun gnus-group-quit ()
4688   "Quit reading news without updating .newsrc.eld or .newsrc.
4689 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4690   (interactive)
4691   (if (or noninteractive                ;For gnus-batch-kill
4692           (zerop (buffer-size))
4693           (not (gnus-server-opened gnus-select-method))
4694           gnus-expert-user
4695           (not gnus-current-startup-file)
4696           (gnus-yes-or-no-p
4697            (format "Quit reading news without saving %s? "
4698                    (file-name-nondirectory gnus-current-startup-file))))
4699       (progn
4700         (run-hooks 'gnus-exit-gnus-hook)
4701         (if gnus-use-full-window
4702             (delete-other-windows)
4703           (gnus-remove-some-windows))
4704         (gnus-dribble-save)
4705         (gnus-close-backends)
4706         (gnus-clear-system))))
4707
4708 (defun gnus-offer-save-summaries ()
4709   (let ((buffers (buffer-list)))
4710     (save-excursion
4711       (while buffers
4712         (and 
4713          ;; We look for buffers with "Summary" in the name.
4714          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4715          (progn
4716            (set-buffer (car buffers))
4717            ;; We check that this is, indeed, a summary buffer.
4718            (eq major-mode 'gnus-summary-mode)) 
4719          ;; We ask the user whether she wants to save the info.
4720          (gnus-y-or-n-p
4721                (format "Update summary buffer %s? " (buffer-name)))
4722          ;; We do it by simply exiting.
4723          (gnus-summary-exit))
4724         (setq buffers (cdr buffers))))))
4725
4726 (defun gnus-group-describe-briefly ()
4727   "Give a one line description of the group mode commands."
4728   (interactive)
4729   (gnus-message 6
4730    (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")))
4731
4732 (defun gnus-group-browse-foreign-server (method)
4733   "Browse a foreign news server.
4734 If called interactively, this function will ask for a select method
4735  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4736 If not, METHOD should be a list where the first element is the method
4737 and the second element is the address."
4738   (interactive
4739    (list (let ((how (completing-read 
4740                      "Which backend: "
4741                      (append gnus-valid-select-methods gnus-server-alist)
4742                      nil t "nntp")))
4743            ;; We either got a backend name or a virtual server name.
4744            ;; If the first, we also need an address.
4745            (if (assoc how gnus-valid-select-methods)
4746                (list (intern how)
4747                      ;; Suggested by mapjph@bath.ac.uk.
4748                      (completing-read 
4749                       "Address: " 
4750                       (mapcar (lambda (server) (list server))
4751                               gnus-secondary-servers)))
4752              ;; We got a server name, so we find the method.
4753              (gnus-server-to-method how)))))
4754   (gnus-browse-foreign-server method))
4755
4756 \f
4757 ;;;
4758 ;;; Browse Server Mode
4759 ;;;
4760
4761 (defvar gnus-browse-mode-hook nil)
4762 (defvar gnus-browse-mode-map nil)
4763 (put 'gnus-browse-mode 'mode-class 'special)
4764
4765 (if gnus-browse-mode-map
4766     nil
4767   (setq gnus-browse-mode-map (make-keymap))
4768   (suppress-keymap gnus-browse-mode-map)
4769   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4770   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4771   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4772   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4773   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4774   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4775   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4776   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4777   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4778   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4779   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4780   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4781   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4782   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4783   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4784   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4785   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4786   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4787   )
4788
4789 (defvar gnus-browse-current-method nil)
4790 (defvar gnus-browse-return-buffer nil)
4791
4792 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4793
4794 (defun gnus-browse-foreign-server (method &optional return-buffer)
4795   (setq gnus-browse-current-method method)
4796   (setq gnus-browse-return-buffer return-buffer)
4797   (let ((gnus-select-method method)
4798         groups group)
4799     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4800     (or (gnus-server-opened method)
4801         (gnus-open-server method)
4802         (error "Unable to contact server: %s" (gnus-status-message method)))
4803     (or (gnus-request-list method)
4804         (error "Couldn't request list: %s" (gnus-status-message method)))
4805     (get-buffer-create gnus-browse-buffer)
4806     (gnus-add-current-to-buffer-list)
4807     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4808     (gnus-configure-windows 'browse)
4809     (buffer-disable-undo (current-buffer))
4810     (let ((buffer-read-only nil))
4811       (erase-buffer))
4812     (gnus-browse-mode)
4813     (setq mode-line-buffer-identification
4814           (format
4815            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4816     (save-excursion
4817       (set-buffer nntp-server-buffer)
4818       (let ((cur (current-buffer)))
4819         (goto-char (point-min))
4820         (or (string= gnus-ignored-newsgroups "")
4821             (delete-matching-lines gnus-ignored-newsgroups))
4822         (while (re-search-forward 
4823                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4824           (goto-char (match-end 1))
4825           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4826                                                      (match-end 1))
4827                                    (max 0 (- (1+ (read cur)) (read cur))))
4828                              groups)))))
4829     (setq groups (sort groups 
4830                        (lambda (l1 l2)
4831                          (string< (car l1) (car l2)))))
4832     (let ((buffer-read-only nil))
4833       (while groups
4834         (setq group (car groups))
4835         (insert 
4836          (format "K%7d: %s\n" (cdr group) (car group)))
4837         (setq groups (cdr groups))))
4838     (switch-to-buffer (current-buffer))
4839     (goto-char (point-min))
4840     (gnus-group-position-cursor)))
4841
4842 (defun gnus-browse-mode ()
4843   "Major mode for browsing a foreign server.
4844
4845 All normal editing commands are switched off.
4846
4847 \\<gnus-browse-mode-map>
4848 The only things you can do in this buffer is
4849
4850 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4851 The group will be inserted into the group buffer upon exit from this
4852 buffer.  
4853
4854 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4855
4856 3) `\\[gnus-browse-exit]' to return to the group buffer."
4857   (interactive)
4858   (kill-all-local-variables)
4859   (if gnus-visual (gnus-browse-make-menu-bar))
4860   (setq mode-line-modified "-- ")
4861   (make-local-variable 'mode-line-format)
4862   (setq mode-line-format (copy-sequence mode-line-format))
4863   (and (equal (nth 3 mode-line-format) "   ")
4864        (setcar (nthcdr 3 mode-line-format) ""))
4865   (setq major-mode 'gnus-browse-mode)
4866   (setq mode-name "Browse Server")
4867   (setq mode-line-process nil)
4868   (use-local-map gnus-browse-mode-map)
4869   (buffer-disable-undo (current-buffer))
4870   (setq truncate-lines t)
4871   (setq buffer-read-only t)
4872   (run-hooks 'gnus-browse-mode-hook))
4873
4874 (defun gnus-browse-read-group (&optional no-article)
4875   "Enter the group at the current line."
4876   (interactive)
4877   (let ((group (gnus-browse-group-name)))
4878     (or (gnus-group-read-ephemeral-group 
4879          group gnus-browse-current-method nil
4880          (cons (current-buffer) 'browse))
4881         (error "Couldn't enter %s" group))))
4882
4883 (defun gnus-browse-select-group ()
4884   "Select the current group."
4885   (interactive)
4886   (gnus-browse-read-group 'no))
4887
4888 (defun gnus-browse-next-group (n)
4889   "Go to the next group."
4890   (interactive "p")
4891   (prog1
4892       (forward-line n)
4893     (gnus-group-position-cursor)))
4894
4895 (defun gnus-browse-prev-group (n)
4896   "Go to the next group."
4897   (interactive "p")
4898   (gnus-browse-next-group (- n)))
4899
4900 (defun gnus-browse-unsubscribe-current-group (arg)
4901   "(Un)subscribe to the next ARG groups."
4902   (interactive "p")
4903   (and (eobp)
4904        (error "No group at current line."))
4905   (let ((ward (if (< arg 0) -1 1))
4906         (arg (abs arg)))
4907     (while (and (> arg 0)
4908                 (not (eobp))
4909                 (gnus-browse-unsubscribe-group)
4910                 (zerop (gnus-browse-next-group ward)))
4911       (setq arg (1- arg)))
4912     (gnus-group-position-cursor)
4913     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4914     arg))
4915
4916 (defun gnus-browse-group-name ()
4917   (save-excursion
4918     (beginning-of-line)
4919     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4920         ()
4921       (gnus-group-prefixed-name 
4922        (buffer-substring (match-beginning 1) (match-end 1))
4923        gnus-browse-current-method))))
4924   
4925 (defun gnus-browse-unsubscribe-group ()
4926   (let ((sub nil)
4927         (buffer-read-only nil)
4928         group)
4929     (save-excursion
4930       (beginning-of-line)
4931       (if (= (following-char) ?K) (setq sub t))
4932       (setq group (gnus-browse-group-name))
4933       (beginning-of-line)
4934       (delete-char 1)
4935       (if sub
4936           (progn
4937             (gnus-group-change-level 
4938              (list t group gnus-level-default-subscribed
4939                    nil nil gnus-browse-current-method) 
4940              gnus-level-default-subscribed gnus-level-killed
4941              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4942              t)
4943             (insert ? ))
4944         (gnus-group-change-level 
4945          group gnus-level-killed gnus-level-default-subscribed)
4946         (insert ?K)))
4947     t))
4948
4949 (defun gnus-browse-exit ()
4950   "Quit browsing and return to the group buffer."
4951   (interactive)
4952   (if (eq major-mode 'gnus-browse-mode)
4953       (kill-buffer (current-buffer)))
4954   (if gnus-browse-return-buffer
4955       (gnus-configure-windows 'server)
4956     (gnus-configure-windows 'group)))
4957
4958 (defun gnus-browse-describe-briefly ()
4959   "Give a one line description of the group mode commands."
4960   (interactive)
4961   (gnus-message 6
4962    (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")))
4963       
4964 \f
4965 ;;;
4966 ;;; Gnus summary mode
4967 ;;;
4968
4969 (defvar gnus-summary-mode-map nil)
4970 (defvar gnus-summary-mark-map nil)
4971 (defvar gnus-summary-mscore-map nil)
4972 (defvar gnus-summary-article-map nil)
4973 (defvar gnus-summary-thread-map nil)
4974 (defvar gnus-summary-goto-map nil)
4975 (defvar gnus-summary-exit-map nil)
4976 (defvar gnus-summary-various-map nil)
4977 (defvar gnus-summary-interest-map nil)
4978 (defvar gnus-summary-sort-map nil)
4979 (defvar gnus-summary-backend-map nil)
4980 (defvar gnus-summary-save-map nil)
4981 (defvar gnus-summary-wash-map nil)
4982 (defvar gnus-summary-help-map nil)
4983
4984 (put 'gnus-summary-mode 'mode-class 'special)
4985
4986 (if gnus-summary-mode-map
4987     nil
4988   (setq gnus-summary-mode-map (make-keymap))
4989   (suppress-keymap gnus-summary-mode-map)
4990
4991   ;; Non-orthogonal keys
4992
4993   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4994   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4995   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4996   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4997   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4998   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4999   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5000   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5001   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5002   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5003   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5004   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5005   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5006   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5007   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5008   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5009   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5010   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5011   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5012   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5013   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5014   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5015   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5016   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5017   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5018   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5019   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5020   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5021   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5022   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5023   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5024   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5025   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5026   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5027   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5028   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5029   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5030   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5031   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5032   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5033   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5034   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5035   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5036   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5037   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5038   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5039   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5040   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5041   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5042   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5043   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5044   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5045   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5046   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5047   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5048   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5049   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5050   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5051   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5052   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5053   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5054   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5055   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5056   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5057   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5058   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5059   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5060   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5061   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5062   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5063   (define-key gnus-summary-mode-map "V" 'gnus-version)
5064   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5065   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5066   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5067   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5068   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5069   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5070   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5071   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5072 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5073   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5074   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5075   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5076 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5077   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5078   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5079   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5080   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5081   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5082
5083
5084   ;; Sort of orthogonal keymap
5085   (define-prefix-command 'gnus-summary-mark-map)
5086   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5087   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5088   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5089   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5090   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5091   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5092   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5093   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5094   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5095   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5096   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5097   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5098   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5099   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5100   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5101   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5102   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5103   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5104   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5105   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5106   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5107   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5108   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5109   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5110
5111   (define-prefix-command 'gnus-summary-mscore-map)
5112   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5113   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5114   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5115   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5116   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5117
5118   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5119   
5120   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5121   
5122   (define-prefix-command 'gnus-summary-goto-map)
5123   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5124   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5125   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5126   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5127   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5128   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5129   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5130   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5131   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5132   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5133   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5134   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5135   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5136   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5137
5138
5139   (define-prefix-command 'gnus-summary-thread-map)
5140   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5141   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5142   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5143   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5144   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5145   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5146   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5147   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5148   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5149   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5150   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5151   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5152   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5153   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5154
5155   
5156   (define-prefix-command 'gnus-summary-exit-map)
5157   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5158   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5159   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5160   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5161   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5162   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5163   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5164   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5165   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5166   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5167   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5168
5169
5170   (define-prefix-command 'gnus-summary-article-map)
5171   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5172   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5173   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5174   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5175   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5176   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5177   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5178   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5179   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5180   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5181   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5182   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5183   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5184   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5185   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5186   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5187   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5188   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5189
5190
5191   (define-prefix-command 'gnus-summary-wash-map)
5192   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5193   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5194   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5195   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5196   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5197   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5198   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5199   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5200   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5201   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5202   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5203   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5204
5205   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5206   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5207   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5208   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5209   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5210   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5211
5212
5213   (define-prefix-command 'gnus-summary-help-map)
5214   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5215   (define-key gnus-summary-help-map "v" 'gnus-version)
5216   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5217   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5218   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5219   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5220
5221
5222   (define-prefix-command 'gnus-summary-backend-map)
5223   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5224   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5225   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5226   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5227   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5228   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5229   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5230   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5231   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5232
5233
5234   (define-prefix-command 'gnus-summary-save-map)
5235   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5236   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5237   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5238   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5239   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5240   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5241   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5242   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5243 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5244
5245   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5246   
5247   (define-prefix-command 'gnus-summary-various-map)
5248   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5249   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5250   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5251   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5252   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5253   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5254   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5255   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5256   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5257   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5258   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5259
5260   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5261
5262   (define-prefix-command 'gnus-summary-sort-map)
5263   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5264   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5265   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5266   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5267   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5268   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5269
5270   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5271   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5272   )
5273
5274
5275 \f
5276
5277 (defun gnus-summary-mode ()
5278   "Major mode for reading articles.
5279
5280 All normal editing commands are switched off.
5281 \\<gnus-summary-mode-map>
5282 Each line in this buffer represents one article.  To read an
5283 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5284 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5285 respectively.
5286
5287 You can also post articles and send mail from this buffer.  To 
5288 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5289 of an article, type `\\[gnus-summary-reply]'.
5290
5291 There are approx. one gazillion commands you can execute in this 
5292 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5293
5294 The following commands are available:
5295
5296 \\{gnus-summary-mode-map}"
5297   (interactive)
5298   (if gnus-visual (gnus-summary-make-menu-bar))
5299   (kill-all-local-variables)
5300   (let ((locals gnus-summary-local-variables))
5301     (while locals
5302       (if (consp (car locals))
5303           (progn
5304             (make-local-variable (car (car locals)))
5305             (set (car (car locals)) (eval (cdr (car locals)))))
5306         (make-local-variable (car locals))
5307         (set (car locals) nil))
5308       (setq locals (cdr locals))))
5309   (gnus-make-thread-indent-array)
5310   (gnus-update-format-specifications)
5311   (setq mode-line-modified "-- ")
5312   (make-local-variable 'mode-line-format)
5313   (setq mode-line-format (copy-sequence mode-line-format))
5314   (and (equal (nth 3 mode-line-format) "   ")
5315        (setcar (nthcdr 3 mode-line-format) ""))
5316   (setq major-mode 'gnus-summary-mode)
5317   (setq mode-name "Summary")
5318   (make-local-variable 'minor-mode-alist)
5319   (use-local-map gnus-summary-mode-map)
5320   (buffer-disable-undo (current-buffer))
5321   (setq buffer-read-only t)             ;Disable modification
5322   (setq truncate-lines t)
5323   (setq selective-display t)
5324   (setq selective-display-ellipses t)   ;Display `...'
5325   (setq buffer-display-table gnus-summary-display-table)
5326   (run-hooks 'gnus-summary-mode-hook))
5327
5328 (defun gnus-summary-make-display-table ()
5329   ;; Change the display table.  Odd characters have a tendency to mess
5330   ;; up nicely formatted displays - we make all possible glyphs
5331   ;; display only a single character.
5332
5333   ;; We start from the standard display table, if any.
5334   (setq gnus-summary-display-table 
5335         (or (copy-sequence standard-display-table)
5336             (make-display-table)))
5337   ;; Nix out all the control chars...
5338   (let ((i 32))
5339     (while (>= (setq i (1- i)) 0)
5340       (aset gnus-summary-display-table i [??])))
5341   ;; ... but not newline and cr, of course. (cr is necessary for the
5342   ;; selective display).  
5343   (aset gnus-summary-display-table ?\n nil)
5344   (aset gnus-summary-display-table ?\r nil)
5345   ;; We nix out any glyphs over 126 that are not set already.  
5346   (let ((i 256))
5347     (while (>= (setq i (1- i)) 127)
5348       ;; Only modify if the entry is nil.
5349       (or (aref gnus-summary-display-table i) 
5350           (aset gnus-summary-display-table i [??])))))
5351
5352 (defun gnus-summary-clear-local-variables ()
5353   (let ((locals gnus-summary-local-variables))
5354     (while locals
5355       (if (consp (car locals))
5356           (and (vectorp (car (car locals)))
5357                (set (car (car locals)) nil))
5358         (and (vectorp (car locals))
5359              (set (car locals) nil)))
5360       (setq locals (cdr locals)))))
5361
5362 (defun gnus-mouse-pick-article (e)
5363   (interactive "e")
5364   (mouse-set-point e)
5365   (gnus-summary-next-page nil t))
5366
5367 (defun gnus-summary-setup-buffer (group)
5368   "Initialize summary buffer."
5369   (let ((buffer (concat "*Summary " group "*")))
5370     (if (get-buffer buffer)
5371         (progn
5372           (set-buffer buffer)
5373           (not gnus-newsgroup-begin))
5374       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5375       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5376       (gnus-add-current-to-buffer-list)
5377       (gnus-summary-mode)
5378       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5379       (setq gnus-newsgroup-name group)
5380       t)))
5381
5382 (defun gnus-set-global-variables ()
5383   ;; Set the global equivalents of the summary buffer-local variables
5384   ;; to the latest values they had. These reflect the summary buffer
5385   ;; that was in action when the last article was fetched.
5386   (if (eq major-mode 'gnus-summary-mode) 
5387       (progn
5388         (setq gnus-summary-buffer (current-buffer))
5389         (let ((name gnus-newsgroup-name)
5390               (marked gnus-newsgroup-marked)
5391               (unread gnus-newsgroup-unreads)
5392               (headers gnus-current-headers)
5393               (score-file gnus-current-score-file))
5394           (save-excursion
5395             (set-buffer gnus-group-buffer)
5396             (setq gnus-newsgroup-name name)
5397             (setq gnus-newsgroup-marked marked)
5398             (setq gnus-newsgroup-unreads unread)
5399             (setq gnus-current-headers headers)
5400             (setq gnus-current-score-file score-file))))))
5401
5402 (defun gnus-summary-insert-dummy-line (sformat subject number)
5403   (if (not sformat) 
5404       (setq sformat gnus-summary-dummy-line-format-spec))
5405   (let (b)
5406     (beginning-of-line)
5407     (setq b (point))
5408     (insert (eval sformat))
5409     (add-text-properties
5410      b (1+ b)
5411      (list 'gnus-number number 
5412            'gnus-mark gnus-dummy-mark
5413            'gnus-level 0))))
5414
5415 (defvar gnus-thread-indent-array nil)
5416 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5417 (defun gnus-make-thread-indent-array ()
5418   (let ((n 200))
5419     (if (and gnus-thread-indent-array
5420              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5421         nil
5422       (setq gnus-thread-indent-array (make-vector 201 "")
5423             gnus-thread-indent-array-level gnus-thread-indent-level)
5424       (while (>= n 0)
5425         (aset gnus-thread-indent-array n
5426               (make-string (* n gnus-thread-indent-level) ? ))
5427         (setq n (1- n))))))
5428
5429 (defun gnus-summary-insert-line 
5430   (sformat header level current unread replied expirable subject-or-nil
5431            &optional dummy score)
5432   (or sformat (setq sformat gnus-summary-line-format-spec))
5433   (let* ((indentation (aref gnus-thread-indent-array level))
5434          (lines (header-lines header))
5435          (score (or score gnus-summary-default-score 0))
5436          (score-char
5437           (if (or (null gnus-summary-default-score)
5438                   (<= (abs (- score gnus-summary-default-score))
5439                       gnus-summary-zcore-fuzz)) ? 
5440             (if (< score gnus-summary-default-score)
5441                 gnus-score-below-mark gnus-score-over-mark)))
5442          (replied (if replied gnus-replied-mark ? ))
5443          (from (header-from header))
5444          (name-address (funcall gnus-extract-address-components from))
5445          (address (car (cdr name-address)))
5446          (name (or (car name-address) (car (cdr name-address))))
5447          (subject (header-subject header))
5448          (number (header-number header))
5449          (opening-bracket (if dummy ?\< ?\[))
5450          (closing-bracket (if dummy ?\> ?\]))
5451          (buffer-read-only nil)
5452          (b (progn (beginning-of-line) (point))))
5453     (or (numberp lines) (setq lines 0))
5454     (insert (eval sformat))
5455     (add-text-properties
5456      b (1+ b) (list 'gnus-number number 
5457                     'gnus-mark (or unread gnus-unread-mark)
5458                     'gnus-level level))))
5459
5460 (defun gnus-summary-update-line (&optional dont-update)
5461   ;; Update summary line after change.
5462   (or (not gnus-summary-default-score)
5463       gnus-summary-inhibit-highlight
5464       (let ((gnus-summary-inhibit-highlight t)
5465             (article (gnus-summary-article-number)))
5466         (progn
5467           (or dont-update
5468               (if (and gnus-summary-mark-below
5469                        (< (gnus-summary-article-score)
5470                           gnus-summary-mark-below))
5471                   (and (not (memq article gnus-newsgroup-marked))
5472                        (not (memq article gnus-newsgroup-dormant))
5473                        (memq article gnus-newsgroup-unreads)
5474                        (gnus-summary-mark-article nil gnus-low-score-mark))
5475                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5476                      (gnus-summary-mark-article nil gnus-unread-mark))))
5477           (and gnus-visual
5478                (run-hooks 'gnus-summary-update-hook))))))
5479
5480 (defun gnus-summary-update-lines (&optional beg end)
5481   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5482   (let ((beg (or beg (point-min)))
5483         (end (or end (point-max))))
5484     (save-excursion
5485       (set-buffer gnus-summary-buffer)
5486       (goto-char beg)
5487       (while (and (not (eobp)) (< (point) end))
5488         (gnus-summary-update-line)
5489         (forward-line 1)))))
5490
5491 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5492   ;; Sum up all elements (and sub-elements) in a list.
5493   (let ((number 
5494          (if (listp thread) 
5495              (apply 
5496               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5497            1)))
5498     (if char 
5499         (if (> number 1) gnus-not-empty-thread-mark
5500           gnus-empty-thread-mark)
5501       number)))
5502
5503 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5504   "Start reading news in newsgroup GROUP.
5505 If SHOW-ALL is non-nil, already read articles are also listed.
5506 If NO-ARTICLE is non-nil, no article is selected initially."
5507   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5508   (let* ((new-group (gnus-summary-setup-buffer group))
5509          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5510          (method (car (gnus-find-method-for-group group))))
5511     (cond 
5512      ((not new-group)
5513       (gnus-set-global-variables)
5514       (gnus-kill-buffer kill-buffer)
5515       (gnus-configure-windows 'summary)
5516       (gnus-set-mode-line 'summary)
5517       (gnus-summary-position-cursor)
5518       (message "")
5519       t)
5520      ((null did-select) 
5521       (and (eq major-mode 'gnus-summary-mode)
5522            (not (equal (current-buffer) kill-buffer))
5523            (progn
5524              (kill-buffer (current-buffer))
5525              (set-buffer gnus-group-buffer)
5526              (gnus-group-next-unread-group 1)))
5527       (message "Can't select group")
5528       nil)
5529      ((eq did-select 'quit)
5530       (and (eq major-mode 'gnus-summary-mode)
5531            (not (equal (current-buffer) kill-buffer))
5532            (kill-buffer (current-buffer)))
5533       (gnus-kill-buffer kill-buffer)
5534       (gnus-configure-windows 'group)
5535       (gnus-group-next-unread-group 1)
5536       (signal 'quit nil))
5537      (t
5538       (gnus-set-global-variables)
5539       ;; Save the active value in effect when the group was entered.
5540       (setq gnus-newsgroup-active 
5541             (gnus-copy-sequence
5542              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5543       ;; You can change the subjects in this hook.
5544       (run-hooks 'gnus-select-group-hook)
5545       ;; Do score processing.
5546       (and gnus-use-scoring (gnus-possibly-score-headers))
5547       ;; Update the format specifiers.
5548       (gnus-update-format-specifications)
5549       ;; Generate the summary buffer.
5550       (gnus-summary-prepare)
5551       (if (zerop (buffer-size))
5552           (cond (gnus-newsgroup-dormant
5553                  (gnus-summary-show-all-dormant))
5554                 ((and gnus-newsgroup-scored show-all)
5555                  (gnus-summary-show-all-expunged))))
5556       ;; Function `gnus-apply-kill-file' must be called in this hook.
5557       (run-hooks 'gnus-apply-kill-hook)
5558       (if (zerop (buffer-size))
5559           (progn
5560             ;; This newsgroup is empty.
5561             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5562             (gnus-message 6 "No unread news")
5563             (gnus-kill-buffer kill-buffer)
5564             nil)
5565         ;;(save-excursion
5566         ;;  (if kill-buffer
5567         ;;      (let ((gnus-summary-buffer kill-buffer))
5568         ;;      (gnus-configure-windows 'group))))
5569         ;; Hide conversation thread subtrees.  We cannot do this in
5570         ;; gnus-summary-prepare-hook since kill processing may not
5571         ;; work with hidden articles.
5572         (and gnus-show-threads
5573              gnus-thread-hide-subtree
5574              (gnus-summary-hide-all-threads))
5575         ;; Show first unread article if requested.
5576         (goto-char (point-min))
5577         (if (and (not no-article)
5578                  gnus-auto-select-first
5579                  (gnus-summary-first-unread-article))
5580             ()
5581           (gnus-configure-windows 'summary))
5582         (gnus-set-mode-line 'summary)
5583         (gnus-summary-position-cursor)
5584         ;; If in async mode, we send some info to the backend.
5585         (and gnus-newsgroup-async
5586              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5587              (gnus-request-asynchronous 
5588               gnus-newsgroup-name
5589               (if (and gnus-asynchronous-article-function
5590                        (fboundp gnus-asynchronous-article-function))
5591                   (funcall gnus-asynchronous-article-function
5592                            gnus-newsgroup-threads)
5593                 gnus-newsgroup-threads)))
5594         (gnus-kill-buffer kill-buffer)
5595         (if (not (get-buffer-window gnus-group-buffer))
5596             ()
5597           ;; gotta use windows, because recenter does wierd stuff if
5598           ;; the current buffer ain't the displayed window.
5599           (let ((owin (selected-window))) 
5600             (select-window (get-buffer-window gnus-group-buffer))
5601             (and (gnus-group-goto-group group)
5602                  (recenter))
5603             (select-window owin))))
5604       t))))
5605
5606 (defun gnus-summary-prepare ()
5607   ;; Generate the summary buffer.
5608   (let ((buffer-read-only nil))
5609     (erase-buffer)
5610     (gnus-summary-prepare-threads 
5611      (if gnus-show-threads
5612          (gnus-gather-threads 
5613           (gnus-sort-threads 
5614            (if (and gnus-summary-expunge-below
5615                     (not gnus-fetch-old-headers))
5616                (gnus-make-threads-and-expunge)
5617              (gnus-make-threads))))
5618        gnus-newsgroup-headers)
5619      0 nil nil t)
5620     ;; Erase header retrieval message.
5621     (gnus-summary-update-lines)
5622     (message "")
5623     ;; Remove the final newline.
5624     ;;(goto-char (point-max))
5625     ;;(delete-char -1)
5626     ;; Call hooks for modifying summary buffer.
5627     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5628     (goto-char (point-min))
5629     (run-hooks 'gnus-summary-prepare-hook)))
5630
5631 (defun gnus-subject-equal (s1 s2)
5632   (cond 
5633    ((numberp gnus-summary-gather-subject-limit)
5634     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5635                  (substring s1 0 gnus-summary-gather-subject-limit)
5636                s1)
5637              (if (> (length s2) gnus-summary-gather-subject-limit)
5638                  (substring s2 0 gnus-summary-gather-subject-limit)
5639                s2)))
5640    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5641     (string= (gnus-simplify-subject-fuzzy s1)
5642              (gnus-simplify-subject-fuzzy s2)))
5643    (t
5644     (string= s1 s2))))
5645
5646 (defun gnus-gather-threads (threads)
5647   "Gather threads that have lost their roots."
5648   (if (not gnus-summary-make-false-root)
5649       threads 
5650     (let ((hashtb (gnus-make-hashtable 1023))
5651           (prev threads)
5652           (result threads)
5653           thread subject hthread whole-subject)
5654       (while threads
5655         (setq whole-subject 
5656               (setq subject (header-subject (car (car threads)))))
5657         (if gnus-summary-gather-subject-limit
5658             (or (and (numberp gnus-summary-gather-subject-limit)
5659                      (> (length subject) gnus-summary-gather-subject-limit)
5660                      (setq subject
5661                            (substring subject 0 
5662                                       gnus-summary-gather-subject-limit)))
5663                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5664                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5665           (setq subject (gnus-simplify-subject-re subject)))
5666         (if (setq hthread 
5667                   (gnus-gethash subject hashtb))
5668             (progn
5669               (or (stringp (car (car hthread)))
5670                   (setcar hthread (list whole-subject (car hthread))))
5671               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5672                                            (list (car threads))))
5673               (setcdr prev (cdr threads))
5674               (setq threads prev))
5675           (gnus-sethash subject threads hashtb))
5676         (setq prev threads)
5677         (setq threads (cdr threads)))
5678       result)))
5679
5680 (defun gnus-make-threads ()
5681   ;; This function takes the dependencies already made by 
5682   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5683   ;; through the dependecies in the hash table and finds all the
5684   ;; roots. Roots do not refer back to any valid articles.
5685   (let (roots)
5686     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5687          (gnus-build-old-threads))
5688     (mapatoms
5689      (lambda (refs)
5690        (if (not (car (symbol-value refs)))
5691            (setq roots (append (cdr (symbol-value refs)) roots))
5692          ;; Ok, these refer back to valid articles, but if
5693          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5694          ;; the root has the same subject as its children. The children
5695          ;; that do not are made into roots and removed from the list
5696          ;; of children. 
5697          (or gnus-thread-ignore-subject
5698              (let* ((prev (symbol-value refs))
5699                     (subject (gnus-simplify-subject-re 
5700                               (header-subject (car prev))))
5701                     (headers (cdr prev)))
5702                (while headers
5703                  (if (not (string= subject
5704                                    (gnus-simplify-subject-re 
5705                                     (header-subject (car headers)))))
5706                      (progn
5707                        (setq roots (cons (car headers) roots))
5708                        (setcdr prev (cdr headers)))
5709                    (setq prev headers))
5710                  (setq headers (cdr headers)))))))
5711      gnus-newsgroup-dependencies)
5712     
5713     (mapcar 'gnus-trim-thread
5714             (apply 'append
5715                    (mapcar 'gnus-cut-thread
5716                            (mapcar 'gnus-make-sub-thread roots))))))
5717   
5718 (defun gnus-make-threads-and-expunge ()
5719   ;; This function takes the dependencies already made by 
5720   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5721   ;; through the dependecies in the hash table and finds all the
5722   ;; roots. Roots do not refer back to any valid articles.
5723   (let ((default (or gnus-summary-default-score 0))
5724         (below gnus-summary-expunge-below)
5725         roots article)
5726     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5727          (gnus-build-old-threads))
5728     (mapatoms
5729      (lambda (refs)
5730        (if (not (car (symbol-value refs)))
5731            ;; These articles do not refer back to any other articles -
5732            ;; they are roots.
5733            (let ((headers (cdr (symbol-value refs))))
5734              ;; We weed out the low-scored articles.
5735              (while headers
5736                (if (not (< (or (cdr (assq (header-number (car headers))
5737                                           gnus-newsgroup-scored)) default)
5738                            below))
5739                    ;; It is over.
5740                    (setq roots (cons (car headers) roots))
5741                  ;; It is below, so we mark it as read.
5742                  (setq gnus-newsgroup-unreads
5743                        (delq (header-number (car headers))
5744                              gnus-newsgroup-unreads)))
5745                (setq headers (cdr headers))))
5746          ;; Ok, these refer back to valid articles, but if
5747          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5748          ;; the root has the same subject as its children. The children
5749          ;; that do not are made into roots and removed from the list
5750          ;; of children. 
5751          (or gnus-thread-ignore-subject
5752              (let* ((prev (symbol-value refs))
5753                     (subject (gnus-simplify-subject-re 
5754                               (header-subject (car prev))))
5755                     (headers (cdr prev)))
5756                (while headers
5757                  (if (not (string= subject
5758                                    (gnus-simplify-subject-re 
5759                                     (header-subject (car headers)))))
5760                      (progn
5761                        (if (not (< (or (cdr (assq (header-number (car headers))
5762                                                   gnus-newsgroup-scored))
5763                                        default) below))
5764                            (setq roots (cons (car headers) roots))
5765                          (setq gnus-newsgroup-unreads
5766                                (delq (header-number (car headers))
5767                                      gnus-newsgroup-unreads)))
5768                        (setcdr prev (cdr headers)))
5769                    (setq prev headers))
5770                  (setq headers (cdr headers)))))
5771          ;; If this article is expunged, some of the children might be
5772          ;; roots.  
5773          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5774                                gnus-newsgroup-scored)) default)
5775                 below)
5776              (let* ((prev (symbol-value refs))
5777                     (headers (cdr prev)))
5778                (while headers
5779                  (setq article (header-number (car headers)))
5780                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5781                                  default) below))
5782                      (progn (setq roots (cons (car headers) roots))
5783                             (setq prev headers))
5784                    (setq gnus-newsgroup-unreads 
5785                          (delq article gnus-newsgroup-unreads))
5786                    (setcdr prev (cdr headers)))
5787                  (setq headers (cdr headers))))
5788            ;; It was not expunged, but we look at expunged children.
5789            (let* ((prev (symbol-value refs))
5790                   (headers (cdr prev))
5791                   article id)
5792              (while headers
5793                (setq article (header-number (car headers)))
5794                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5795                                default) below))
5796                    (setq prev headers)
5797                  (setq gnus-newsgroup-unreads 
5798                        (delq article gnus-newsgroup-unreads))
5799                  (setcdr prev (cdr headers)))
5800                (setq headers (cdr headers)))))))
5801      gnus-newsgroup-dependencies)
5802
5803     (mapcar 'gnus-trim-thread
5804             (apply 'append
5805                    (mapcar 'gnus-cut-thread
5806                            (mapcar 'gnus-make-sub-thread roots))))))
5807   
5808 (defun gnus-cut-thread (thread)
5809   ;; Remove leaf dormant or ancient articles from THREAD.
5810   (let ((head (car thread))
5811         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5812     (if (and (null tail)
5813              (let ((number (header-number head)))
5814                (or (memq number gnus-newsgroup-ancient)
5815                    (memq number gnus-newsgroup-dormant)
5816                    (and gnus-summary-expunge-below
5817                         (eq gnus-fetch-old-headers 'some)
5818                         (< (or (cdr (assq number gnus-newsgroup-scored))
5819                                gnus-summary-default-score 0)
5820                            gnus-summary-expunge-below)
5821                         (progn
5822                           (setq gnus-newsgroup-unreads
5823                                 (delq number gnus-newsgroup-unreads))
5824                           t)))))
5825         nil
5826       (list (cons head tail)))))
5827
5828 (defun gnus-trim-thread (thread)
5829   ;; Remove root ancient articles with only one child from THREAD.
5830   (if (and (eq gnus-fetch-old-headers 'some)
5831            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5832            (= (length thread) 2))
5833       (gnus-trim-thread (nth 1 thread))
5834     thread))
5835
5836 (defun gnus-make-sub-thread (root)
5837   ;; This function makes a sub-tree for a node in the tree.
5838   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5839                                               gnus-newsgroup-dependencies)))))
5840     (cons root (mapcar 'gnus-make-sub-thread children))))
5841
5842 (defun gnus-build-old-threads ()
5843   ;; Look at all the articles that refer back to old articles, and
5844   ;; fetch the headers for the articles that aren't there. This will
5845   ;; build complete threads - if the roots haven't been expired by the
5846   ;; server, that is.
5847   (let (id heads)
5848     (mapatoms
5849      (lambda (refs)
5850        (if (not (car (symbol-value refs)))
5851            (progn
5852              (setq heads (cdr (symbol-value refs)))
5853              (while heads
5854                (if (not (memq (header-number (car heads))
5855                               gnus-newsgroup-dormant))
5856                    (progn
5857                      (setq id (symbol-name refs))
5858                      (while (and (setq id (gnus-build-get-header id))
5859                                  (not (car (gnus-gethash 
5860                                             id gnus-newsgroup-dependencies)))))
5861                      (setq heads nil))
5862                  (setq heads (cdr heads)))))))
5863      gnus-newsgroup-dependencies)))
5864
5865 (defun gnus-build-get-header (id)
5866   ;; Look through the buffer of NOV lines and find the header to
5867   ;; ID. Enter this line into the dependencies hash table, and return
5868   ;; the id of the parent article (if any).
5869   (let ((deps gnus-newsgroup-dependencies)
5870         found header)
5871     (prog1
5872         (save-excursion
5873           (set-buffer nntp-server-buffer)
5874           (goto-char (point-min))
5875           (while (and (not found) (search-forward id nil t))
5876             (beginning-of-line)
5877             (setq found (looking-at 
5878                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5879                                  (regexp-quote id))))
5880             (or found (beginning-of-line 2)))
5881           (if found
5882               (let (ref)
5883                 (beginning-of-line)
5884                 (and
5885                  (setq header (gnus-nov-parse-line 
5886                                (read (current-buffer)) deps))
5887                  (setq ref (header-references header))
5888                  (string-match "\\(<[^>]+>\\) *$" ref)
5889                  (substring ref (match-beginning 1) (match-end 1))))))
5890       (and header
5891            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5892                  gnus-newsgroup-ancient (cons (header-number header)
5893                                               gnus-newsgroup-ancient))))))
5894
5895 ;; Re-build the thread containing ID.
5896 (defun gnus-rebuild-thread (id)
5897   (let ((dep gnus-newsgroup-dependencies)
5898         (buffer-read-only nil)
5899         parent headers refs thread art)
5900     (while (and id (setq headers
5901                          (car (setq art (gnus-gethash (downcase id) dep)))))
5902       (setq parent art)
5903       (setq id (and (setq refs (header-references headers))
5904                     (string-match "\\(<[^>]+>\\) *$" refs)
5905                     (substring refs (match-beginning 1) (match-end 1)))))
5906     (setq thread (gnus-make-sub-thread (car parent)))
5907     (gnus-rebuild-remove-articles thread)
5908     (let ((beg (point)))
5909       (gnus-summary-prepare-threads (list thread) 0)
5910       (gnus-summary-update-lines beg (point)))))
5911
5912 ;; Delete all lines in the summary buffer that correspond to articles
5913 ;; in this thread.
5914 (defun gnus-rebuild-remove-articles (thread)
5915   (and (gnus-summary-goto-subject (header-number (car thread)))
5916        (gnus-delete-line))
5917   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5918
5919 (defun gnus-sort-threads (threads)
5920   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5921   (let ((fun gnus-thread-sort-functions))
5922     (while fun
5923       (setq threads (sort threads (car fun))
5924             fun (cdr fun))))
5925   threads)
5926
5927 (defun gnus-thread-header (thread)
5928   ;; Return header of first article in THREAD.
5929   (if (consp thread)
5930       (if (stringp (car thread))
5931           (car (car (cdr thread)))
5932         (car thread))
5933     thread))
5934
5935 (defun gnus-thread-sort-by-number (h1 h2)
5936   "Sort threads by root article number."
5937   (let ((h1 (gnus-thread-header h1))
5938         (h2 (gnus-thread-header h2)))
5939     (< (header-number h1) (header-number h2))))
5940
5941 (defun gnus-thread-sort-by-author (h1 h2)
5942   "Sort threads by root author."
5943   (let ((h1 (gnus-thread-header h1))
5944         (h2 (gnus-thread-header h2)))
5945     (string-lessp
5946      (let ((extract (funcall 
5947                      gnus-extract-address-components (header-from h1))))
5948        (or (car extract) (cdr extract)))
5949      (let ((extract (funcall
5950                      gnus-extract-address-components (header-from h2))))
5951        (or (car extract) (cdr extract))))))
5952
5953 (defun gnus-thread-sort-by-subject (h1 h2)
5954   "Sort threads by root subject."
5955   (let ((h1 (gnus-thread-header h1))
5956         (h2 (gnus-thread-header h2)))
5957     (string-lessp
5958      (downcase (gnus-simplify-subject (header-subject h1)))
5959      (downcase (gnus-simplify-subject (header-subject h2))))))
5960
5961 (defun gnus-thread-sort-by-date (h1 h2)
5962   "Sort threads by root article date."
5963   (let ((h1 (gnus-thread-header h1))
5964         (h2 (gnus-thread-header h2)))
5965     (string-lessp
5966      (gnus-sortable-date (header-date h1))
5967      (gnus-sortable-date (header-date h2)))))
5968
5969 (defun gnus-thread-sort-by-score (h1 h2)
5970   "Sort threads by root article score.
5971 Unscored articles will be counted as having a score of zero."
5972   (let ((h1 (gnus-thread-header h1))
5973         (h2 (gnus-thread-header h2)))
5974     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5975           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5976       (> (or (cdr s1) gnus-summary-default-score 0)
5977          (or (cdr s2) gnus-summary-default-score 0)))))
5978
5979 (defun gnus-thread-sort-by-total-score (h1 h2)
5980   "Sort threads by the sum of all scores in the thread.
5981 Unscored articles will be counted as having a score of zero."
5982   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5983
5984 (defun gnus-thread-total-score (thread)
5985   ;;  This function find the total score of THREAD.
5986   (if (consp thread)
5987       (if (stringp (car thread))
5988           (apply gnus-thread-score-function 0
5989                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5990         (gnus-thread-total-score-1 thread))
5991     (gnus-thread-total-score-1 (list thread))))
5992
5993 (defun gnus-thread-total-score-1 (root)
5994   ;; This function find the total score of the thread below ROOT.
5995   (setq root (car root))
5996   (apply gnus-thread-score-function
5997          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5998              gnus-summary-default-score 0)
5999          (mapcar 'gnus-thread-total-score
6000                  (cdr (gnus-gethash (downcase (header-id root))
6001                                     gnus-newsgroup-dependencies)))))
6002
6003 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6004 (defvar gnus-tmp-prev-subject "")
6005 (defvar gnus-tmp-adopt-thread nil)
6006
6007 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6008 (defun gnus-summary-prepare-threads 
6009   (threads level &optional not-child no-subject cull)
6010   "Prepare summary buffer from THREADS and indentation LEVEL.  
6011 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6012 or a straight list of headers."
6013   (let (thread header number subject clevel)
6014     (while threads
6015       (setq thread (car threads)
6016             threads (cdr threads))
6017       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6018       ;; `thread' is the header.
6019       (if (consp thread)
6020           (setq header (car thread))
6021         (setq header thread)
6022         (and cull
6023              (or (memq (setq number (header-number header))
6024                        gnus-newsgroup-dormant)
6025                  (and gnus-summary-expunge-below
6026                       (< (or (cdr (assq number gnus-newsgroup-scored))
6027                              gnus-summary-default-score 0)
6028                          gnus-summary-expunge-below)))
6029              (progn
6030                (setq header nil)
6031                (setq gnus-newsgroup-unreads 
6032                      (delq number gnus-newsgroup-unreads)))))
6033       (cond 
6034        ((stringp header)
6035         ;; The header is a dummy root.
6036         (cond ((eq gnus-summary-make-false-root 'adopt)
6037                ;; We let the first article adopt the rest.
6038                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6039                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6040                (setq thread (cdr (cdr thread)))
6041                (while thread
6042                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6043                  (setq thread (cdr thread))))
6044               ((eq gnus-summary-make-false-root 'dummy)
6045                ;; We output a dummy root.
6046                (gnus-summary-insert-dummy-line 
6047                 nil header (header-number (car (car (cdr thread)))))
6048                (setq clevel 1))
6049               ((eq gnus-summary-make-false-root 'empty)
6050                ;; We print the articles with empty subject fields. 
6051                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6052                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6053                (setq thread (cdr (cdr thread)))
6054                (while thread
6055                  (gnus-summary-prepare-threads 
6056                   (list (car thread)) 0 nil
6057                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6058                             (not (string=  
6059                                   (gnus-simplify-subject-re 
6060                                    (header-subject (car (car thread))))
6061                                   (gnus-simplify-subject-re header))))))
6062                  (setq thread (cdr thread))))
6063               (t
6064                ;; We do not make a root for the gathered
6065                ;; sub-threads at all.  
6066                (setq clevel 0)))
6067         ;; Print the sub-threads.
6068         (and (consp thread) (cdr thread)
6069              (gnus-summary-prepare-threads (cdr thread) clevel)))
6070        ;; The header is a real article.
6071        (header
6072         (setq number (header-number header)
6073               subject (header-subject header))
6074         (and gnus-newsgroup-async
6075              (setq gnus-newsgroup-threads
6076                    (cons (cons (header-number header)
6077                                (header-lines header)) gnus-newsgroup-threads)))
6078         (gnus-summary-insert-line
6079          nil header level nil 
6080          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6081                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6082                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6083                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6084                (t gnus-ancient-mark))
6085          (memq number gnus-newsgroup-replied)
6086          (memq number gnus-newsgroup-expirable)
6087          (if no-subject 
6088              gnus-summary-same-subject
6089            (if (or (zerop level)
6090                    (and gnus-thread-ignore-subject
6091                         (not (string= 
6092                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6093                               (gnus-simplify-subject-re subject)))))
6094                subject
6095              gnus-summary-same-subject))
6096          not-child
6097          (cdr (assq number gnus-newsgroup-scored)))
6098         (setq gnus-tmp-prev-subject subject)
6099         ;; Recursively print subthreads.
6100         (and (consp thread) (cdr thread)
6101              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6102
6103 (defun gnus-select-newsgroup (group &optional read-all)
6104   "Select newsgroup GROUP.
6105 If READ-ALL is non-nil, all articles in the group are selected."
6106   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6107          (info (nth 2 entry))
6108          articles header-marks)
6109     (gnus-check-news-server
6110      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6111
6112     (or (gnus-server-opened gnus-current-select-method)
6113         (gnus-open-server gnus-current-select-method)
6114         (error "Couldn't open server"))
6115     
6116     (or (and (eq (car entry) t)
6117              (gnus-activate-newsgroup (car info)))
6118         (gnus-request-group group t)
6119         (progn
6120           (kill-buffer (current-buffer))
6121           (error "Couldn't request group %s: %s" 
6122                  group (gnus-status-message group))))
6123
6124     (setq gnus-newsgroup-name group)
6125     (setq gnus-newsgroup-unselected nil)
6126     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6127
6128     (and gnus-asynchronous
6129          (gnus-check-backend-function 
6130           'request-asynchronous gnus-newsgroup-name)
6131          (setq gnus-newsgroup-async
6132                (gnus-request-asynchronous gnus-newsgroup-name)))
6133
6134     (setq articles (gnus-articles-to-read group read-all))
6135
6136     (cond 
6137      ((null articles) 
6138       (gnus-message 3 "Couldn't select newsgroup")
6139       'quit)
6140      ((eq articles 0) nil)
6141      (t
6142       ;; Init the dependencies hash table.
6143       (setq gnus-newsgroup-dependencies 
6144             (gnus-make-hashtable (length articles)))
6145       ;; Retrieve the headers and read them in.
6146       (setq gnus-newsgroup-headers 
6147             (if (eq 'nov (setq gnus-headers-retrieved-by
6148                                (gnus-retrieve-headers 
6149                                 (if (and gnus-fetch-old-headers 
6150                                          (not (eq 1 (car articles))))
6151                                     (cons 1 articles)
6152                                   articles)
6153                                 gnus-newsgroup-name)))
6154                 (progn
6155                   (gnus-get-newsgroup-headers-xover articles))
6156               ;; If we were to fetch old headers, but the backend didn't
6157               ;; support XOVER, then it is possible we fetched one article
6158               ;; that we shouldn't have. If that's the case, we pop it off the
6159               ;; list of headers.
6160               (if (not gnus-fetch-old-headers)
6161                   ()
6162                 (save-excursion
6163                   (set-buffer nntp-server-buffer)
6164                   (goto-char (point-min))
6165                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6166                        (delete-region 
6167                         (point) 
6168                         (search-forward "\n.\n" nil t)))))
6169               (gnus-get-newsgroup-headers)))
6170       ;; Remove canceled articles from the list of unread articles.
6171       (setq gnus-newsgroup-unreads
6172             (gnus-set-sorted-intersection 
6173              gnus-newsgroup-unreads
6174              (mapcar (lambda (headers) (header-number headers))
6175                      gnus-newsgroup-headers)))
6176       ;; Adjust and set lists of article marks.
6177       (and info
6178            (let (marked)
6179              (gnus-adjust-marked-articles info)
6180              (setq gnus-newsgroup-marked 
6181                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6182              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6183              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6184              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6185              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6186              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6187              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6188              (setq gnus-newsgroup-processable nil)))
6189       ;; Check whether auto-expire is to be done in this group.
6190       (setq gnus-newsgroup-auto-expire
6191             (or (and (stringp gnus-auto-expirable-newsgroups)
6192                      (string-match gnus-auto-expirable-newsgroups group))
6193                 (memq 'auto-expire (nth 5 info))))
6194       ;; First and last article in this newsgroup.
6195       (and gnus-newsgroup-headers
6196            (setq gnus-newsgroup-begin 
6197                  (header-number (car gnus-newsgroup-headers)))
6198            (setq gnus-newsgroup-end
6199                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6200       (setq gnus-reffed-article-number -1)
6201       ;; GROUP is successfully selected.
6202       (or gnus-newsgroup-headers t)))))
6203
6204 (defun gnus-articles-to-read (group read-all)
6205   ;; Find out what articles the user wants to read.
6206   (let* ((articles
6207           ;; Select all articles if `read-all' is non-nil, or if all the
6208           ;; unread articles are dormant articles.
6209           (if (or (and read-all (not (numberp read-all)))
6210                   (= (length gnus-newsgroup-unreads) 
6211                      (length gnus-newsgroup-dormant)))
6212               (gnus-uncompress-range 
6213                (gnus-gethash group gnus-active-hashtb))
6214             gnus-newsgroup-unreads))
6215          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6216          (scored (length scored-list))
6217          (number (length articles))
6218          (marked (+ (length gnus-newsgroup-marked)
6219                     (length gnus-newsgroup-dormant)))
6220          (select
6221           (cond 
6222            ((numberp read-all)
6223             read-all)
6224            (t
6225             (condition-case ()
6226                 (cond ((and (or (<= scored marked)
6227                                 (= scored number))
6228                             (numberp gnus-large-newsgroup)
6229                             (> number gnus-large-newsgroup))
6230                        (let ((input
6231                               (read-string
6232                                (format
6233                                 "How many articles from %s (default %d): "
6234                                 gnus-newsgroup-name number))))
6235                          (if (string-equal input "")
6236                              number input)))
6237                       ((and (> scored marked) (< scored number))
6238                        (let ((input
6239                               (read-string
6240                                (format 
6241                                 "%s %s (%d scored, %d total): "
6242                                 "How many articles from"
6243                                 group scored number))))
6244                          (if (string-match "^[ \t]*$" input)
6245                              number input)))
6246                       (t number))
6247               (quit nil)))))
6248          total-articles)
6249     (setq select (if (stringp select) (string-to-number select) select))
6250     (if (or (null select) (zerop select))
6251         select
6252       (if (and (not (zerop scored)) (<= (abs select) scored))
6253           (progn
6254             (setq articles (sort scored-list '<))
6255             (setq number (length articles)))
6256         (setq articles (copy-sequence articles)))
6257
6258       (setq total-articles articles)
6259       
6260       (if (< (abs select) number)
6261           (if (< select 0) 
6262               ;; Select the N oldest articles.
6263               (setcdr (nthcdr (1- (abs select)) articles) nil)
6264             ;; Select the N most recent articles.
6265             (setq articles (nthcdr (- number select) articles))))
6266       (setq gnus-newsgroup-unselected
6267             (gnus-sorted-intersection
6268              gnus-newsgroup-unreads
6269              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6270       articles)))
6271
6272 (defun gnus-killed-articles (killed articles)
6273   (let (out)
6274     (while articles
6275       (if (inline (gnus-member-of-range (car articles) killed))
6276           (setq out (cons (car articles) out)))
6277       (setq articles (cdr articles)))
6278     out))
6279
6280 (defun gnus-adjust-marked-articles (info &optional active)
6281   "Remove all marked articles that are no longer legal."
6282   (let ((marked-lists (nth 3 info))
6283         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6284         marked m prev)
6285     ;; There are many types of marked articles.
6286     (while marked-lists
6287       (setq m (cdr (setq prev (car marked-lists))))
6288       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6289              ;; Make sure that all ticked articles are a subset of the
6290              ;; unread/unselected articles.
6291              (while m
6292                (if (or (memq (car m) gnus-newsgroup-unreads)
6293                        (memq (car m) gnus-newsgroup-unselected))
6294                    (setq prev m)
6295                  (setcdr prev (cdr m)))
6296                (setq m (cdr m))))
6297             ((eq 'score (car prev))
6298              ;; Scored articles should be a subset of
6299              ;; unread/unselected articles. 
6300              (while m
6301                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6302                        (memq (car (car m)) gnus-newsgroup-unreads))
6303                    (setq prev m)
6304                  (setcdr prev (cdr m)))
6305                (setq m (cdr m))))
6306             ((eq 'bookmark (car prev))
6307              ;; Bookmarks should be a subset of active articles.
6308              (while m
6309                (if (< (car (car m)) (car active))
6310                    (setcdr prev (cdr m))
6311                  (setq prev m))
6312                (setq m (cdr m))))
6313             ((eq 'killed (car prev))
6314              ;; Articles that have been through the kill process are
6315              ;; to be a subset of active articles.
6316              (while (and m (< (or (and (numberp (car m)) (car m))
6317                                   (cdr (car m)))
6318                               (car active)))
6319                (setcdr prev (cdr m))
6320                (setq m (cdr m)))
6321              (if (and m (< (or (and (numberp (car m)) (car m))
6322                                (car (car m)))
6323                            (car active))) 
6324                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6325             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6326              ;; The replied and expirable articles have to be articles
6327              ;; that are active. 
6328              (while m
6329                (if (< (car m) (car active))
6330                    (setcdr prev (cdr m))
6331                  (setq prev m))
6332                (setq m (cdr m)))))
6333       (setq marked-lists (cdr marked-lists)))
6334     ;; Remove all lists that are empty.
6335     (setq marked-lists (nth 3 info))
6336     (if marked-lists
6337         (progn
6338           (while (= 1 (length (car marked-lists)))
6339             (setq marked-lists (cdr marked-lists)))
6340           (setq m (cdr (setq prev marked-lists)))
6341           (while m
6342             (if (= 1 (length (car m)))
6343                 (setcdr prev (cdr m))
6344               (setq prev m))
6345             (setq m (cdr m)))
6346           (setcar (nthcdr 3 info) marked-lists)))
6347     ;; Finally, if there are no marked lists at all left, and if there
6348     ;; are no elements after the lists in the info list, we just chop
6349     ;; the info list off before the marked lists.
6350     (and (null marked-lists) 
6351          (not (nthcdr 4 info))
6352          (setcdr (nthcdr 2 info) nil)))
6353   info)
6354
6355 (defun gnus-set-marked-articles 
6356   (info ticked replied expirable killed dormant bookmark score) 
6357   "Enter the various lists of marked articles into the newsgroup info list."
6358   (let (newmarked)
6359     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6360     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6361     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6362                                          newmarked)))
6363     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6364     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6365     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6366                                         newmarked)))
6367     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6368     (if (nthcdr 3 info)
6369         (progn
6370           (setcar (nthcdr 3 info) newmarked)
6371           (and (not newmarked)
6372                (not (nthcdr 4 info))
6373                (setcdr (nthcdr 2 info) nil)))
6374       (if newmarked
6375           (setcdr (nthcdr 2 info) (list newmarked))))))
6376
6377 (defun gnus-add-marked-articles (group type articles &optional info force)
6378   ;; Add ARTICLES of TYPE to the info of GROUP.
6379   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6380   ;; add, but replace marked articles of TYPE with ARTICLES.
6381   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6382         marked m)
6383     (or (not info)
6384         (and (not (setq marked (nthcdr 3 info)))
6385              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6386         (and (not (setq m (assq type (car marked))))
6387              (setcar marked (cons (cons type articles) (car marked))))
6388         (if force
6389             (setcdr m articles)
6390           (nconc m articles)))))
6391          
6392 (defun gnus-set-mode-line (where)
6393   "This function sets the mode line of the article or summary buffers.
6394 If WHERE is `summary', the summary mode line format will be used."
6395   (if (memq where gnus-updated-mode-lines)
6396       (let (mode-string)
6397         (save-excursion
6398           (set-buffer gnus-summary-buffer)
6399           (let* ((mformat (if (eq where 'article) 
6400                               gnus-article-mode-line-format-spec
6401                             gnus-summary-mode-line-format-spec))
6402                  (group-name gnus-newsgroup-name)
6403                  (article-number (or gnus-current-article 0))
6404                  (unread (- (length gnus-newsgroup-unreads)
6405                             (length gnus-newsgroup-dormant)))
6406                  (unread-and-unticked 
6407                   (- unread (length gnus-newsgroup-marked)))
6408                  (unselected (length gnus-newsgroup-unselected))
6409                  (unread-and-unselected
6410                   (cond ((and (zerop unread-and-unticked)
6411                               (zerop unselected)) "")
6412                         ((zerop unselected) 
6413                          (format "{%d more}" unread-and-unticked))
6414                         (t (format "{%d(+%d) more}"
6415                                    unread-and-unticked unselected))))
6416                  (subject
6417                   (if gnus-current-headers
6418                       (header-subject gnus-current-headers) ""))
6419                  (max-len (and gnus-mode-non-string-length
6420                                (- (frame-width) gnus-mode-non-string-length)))
6421                  header) ;; passed as argument to any user-format-funcs
6422             (setq mode-string (eval mformat))
6423             (or (numberp max-len)
6424                 (setq max-len (length mode-string)))
6425             (if (< max-len 4) (setq max-len 4))
6426             (if (> (length mode-string) max-len)
6427                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6428                 ;;  function `substring' might cut on a middle
6429                 ;;  of multi-octet character.
6430                 (setq mode-string 
6431                       (concat (gnus-truncate-string mode-string (- max-len 3))
6432                               "...")))
6433             (setq mode-string (format (format "%%-%ds" max-len)
6434                                       mode-string))))
6435         (setq mode-line-buffer-identification mode-string)
6436         (set-buffer-modified-p t))))
6437
6438 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6439   "Go through the HEADERS list and add all Xrefs to a hash table.
6440 The resulting hash table is returned, or nil if no Xrefs were found."
6441   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6442          (prefix (if (and 
6443                       (gnus-group-foreign-p from-newsgroup)
6444                       (not (memq 'virtual 
6445                                  (assoc (symbol-name (car from-method))
6446                                         gnus-valid-select-methods))))
6447                      (gnus-group-real-prefix from-newsgroup)))
6448          (xref-hashtb (make-vector 63 0))
6449          start group entry number xrefs header)
6450     (while headers
6451       (setq header (car headers))
6452       (if (and (setq xrefs (header-xref header))
6453                (not (memq (header-number header) unreads)))
6454           (progn
6455             (setq start 0)
6456             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6457               (setq start (match-end 0))
6458               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6459                                                     (match-end 1))))
6460               (setq number 
6461                     (string-to-int (substring xrefs (match-beginning 2) 
6462                                               (match-end 2))))
6463               (if (setq entry (gnus-gethash group xref-hashtb))
6464                   (setcdr entry (cons number (cdr entry)))
6465                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6466       (setq headers (cdr headers)))
6467     (if start xref-hashtb nil)))
6468
6469 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6470   "Look through all the headers and mark the Xrefs as read."
6471   (let ((virtual (memq 'virtual 
6472                        (assoc (symbol-name (car (gnus-find-method-for-group 
6473                                                  from-newsgroup)))
6474                               gnus-valid-select-methods)))
6475         name entry read info xref-hashtb idlist active num range exps method
6476         nth4)
6477     (save-excursion
6478       (set-buffer gnus-group-buffer)
6479       (if (setq xref-hashtb 
6480                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6481           (mapatoms 
6482            (lambda (group)
6483              (if (string= from-newsgroup (setq name (symbol-name group)))
6484                  ()
6485                (setq idlist (symbol-value group))
6486                ;; Dead groups are not updated.
6487                (if (and (prog1 
6488                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6489                                   info (nth 2 entry))
6490                           (if (stringp (setq nth4 (nth 4 info)))
6491                               (setq nth4 (gnus-server-to-method nth4))))
6492                         ;; Only do the xrefs if the group has the same
6493                         ;; select method as the group we have just read.
6494                         (or (gnus-methods-equal-p 
6495                              nth4 (gnus-find-method-for-group from-newsgroup))
6496                             virtual
6497                             (equal nth4 
6498                                    (setq method (gnus-find-method-for-group 
6499                                                  from-newsgroup)))
6500                             (and (equal (car nth4) (car method))
6501                                  (equal (nth 1 nth4) (nth 1 method))))
6502                         gnus-use-cross-reference
6503                         (or (not (eq gnus-use-cross-reference t))
6504                             virtual
6505                             ;; Only do cross-references on subscribed
6506                             ;; groups, if that is what is wanted.  
6507                             (<= (nth 1 info) gnus-level-subscribed)))
6508                    (progn
6509                      (setq num 0)
6510                      ;; Set the new list of read articles in this group.
6511                      (setq active (gnus-gethash name gnus-active-hashtb))
6512                      ;; First peel off all illegal article numbers.
6513                      (if active
6514                          (let ((ids idlist)
6515                                (ticked (cdr (assq 'tick (nth 3 info))))
6516                                (dormant (cdr (assq 'dormant (nth 3 info))))
6517                                id)
6518                            (setq exps nil)
6519                            (while ids
6520                              (setq id (car ids))
6521                              (if (or (> id (cdr active))
6522                                      (< id (car active))
6523                                      (memq id ticked)
6524                                      (memq id dormant))
6525                                  (setq idlist (delq id idlist)))
6526                              (and (memq id expirable)
6527                                   (setq exps (cons id exps)))
6528                              (setq ids (cdr ids)))))
6529                      ;; Update expirable articles.
6530                      (gnus-add-marked-articles nil 'expirable exps info)
6531                      (and active
6532                           (null (nth 2 info))
6533                           (> (car active) 1)
6534                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6535                      (setcar (nthcdr 2 info)
6536                              (setq range
6537                                    (gnus-add-to-range 
6538                                     (nth 2 info) 
6539                                     (setq idlist (sort idlist '<)))))
6540                      ;; Then we have to re-compute how many unread
6541                      ;; articles there are in this group.
6542                      (if active
6543                          (progn
6544                            (cond 
6545                             ((not range)
6546                              (setq num (- (1+ (cdr active)) (car active))))
6547                             ((not (listp (cdr range)))
6548                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6549                                                           (car range)))))
6550                             (t
6551                              (while range
6552                                (if (numberp (car range))
6553                                    (setq num (1+ num))
6554                                  (setq num (+ num (- (1+ (cdr (car range)))
6555                                                      (car (car range))))))
6556                                (setq range (cdr range)))
6557                              (setq num (- (cdr active) num))))
6558                            ;; Update the number of unread articles.
6559                            (setcar 
6560                             entry 
6561                             (max 0 (- num 
6562                                       (length (cdr (assq 'tick (nth 3 info))))
6563                                       (length 
6564                                        (cdr (assq 'dormant (nth 3 info)))))))
6565                            ;; Update the group buffer.
6566                            (gnus-group-update-group name t)))))))
6567            xref-hashtb)))))
6568
6569 (defun gnus-methods-equal-p (m1 m2)
6570   (let ((m1 (or m1 gnus-select-method))
6571         (m2 (or m2 gnus-select-method)))
6572     (or (equal m1 m2)
6573         (and (eq (car m1) (car m2))
6574              (or (not (memq 'address (assoc (symbol-name (car m1))
6575                                             gnus-valid-select-methods)))
6576                  (equal (nth 1 m1) (nth 1 m2)))))))
6577
6578 (defsubst gnus-header-value ()
6579   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6580
6581 (defvar gnus-newsgroup-none-id 0)
6582
6583 (defun gnus-get-newsgroup-headers ()
6584   (setq gnus-article-internal-prepare-hook nil)
6585   (let ((cur nntp-server-buffer)
6586         (dependencies gnus-newsgroup-dependencies)
6587         headers char article id dep end)
6588     (save-excursion
6589       (set-buffer nntp-server-buffer)
6590       (goto-char (point-min))
6591       ;; Search to the beginning of the next header. Error messages
6592       ;; do not begin with 2 or 3.
6593       (while (re-search-forward "^[23][0-9]+ " nil t)
6594         (let ((header (make-vector 9 nil))
6595               (c (following-char))
6596               (case-fold-search t)
6597               (p (point))
6598               from subject in-reply-to references ref)
6599           (setq id nil
6600                 ref nil
6601                 references nil
6602                 subject nil
6603                 from nil)
6604           (header-set-number header (setq article (read cur)))
6605           ;; This implementation of this function, with nine
6606           ;; search-forwards instead of the one re-search-forward and
6607           ;; a case (which basically was the old function) is actually
6608           ;; about twice as fast, even though it looks messier. You
6609           ;; can't have everything, I guess. Speed and elegance
6610           ;; doesn't always come hand in hand.
6611           (save-restriction
6612             (narrow-to-region (point) (or (save-excursion 
6613                                             (search-forward "\n.\n" nil t))
6614                                           (point)))
6615             (if (search-forward "\nfrom: " nil t)
6616                 (header-set-from header (gnus-header-value))
6617               (header-set-from header "(nobody)"))
6618             (goto-char p)
6619             (if (search-forward "\nsubject: " nil t)
6620                 (header-set-subject header (gnus-header-value))
6621               (header-set-subject header "(none)"))
6622             (goto-char p)
6623             (and (search-forward "\nxref: " nil t)
6624                  (header-set-xref header (gnus-header-value)))
6625             (goto-char p)
6626             (or (numberp (and (search-forward "\nlines: " nil t)
6627                               (header-set-lines header (read cur))))
6628                 (header-set-lines header 0))
6629             (goto-char p)
6630             (and (search-forward "\ndate: " nil t)
6631                  (header-set-date header (gnus-header-value)))
6632             (goto-char p)
6633             (if (search-forward "\nmessage-id: " nil t)
6634                 (header-set-id header (setq id (gnus-header-value)))
6635               ;; If there was no message-id, we just fake one to make
6636               ;; subsequent routines simpler.
6637               (header-set-id 
6638                header 
6639                (setq id (concat "none+" 
6640                                 (int-to-string 
6641                                  (setq gnus-newsgroup-none-id 
6642                                        (1+ gnus-newsgroup-none-id)))))))
6643             (goto-char p)
6644             (if (search-forward "\nreferences: " nil t)
6645                 (progn
6646                   (header-set-references header (gnus-header-value))
6647                   (setq end (match-end 0))
6648                   (save-excursion
6649                     (setq ref 
6650                           (downcase
6651                            (buffer-substring
6652                             (progn 
6653                               (end-of-line)
6654                               (search-backward ">" end t)
6655                               (1+ (point)))
6656                             (progn
6657                               (search-backward "<" end t)
6658                               (point)))))))
6659               ;; Get the references from the in-reply-to header if there
6660               ;; ware no references and the in-reply-to header looks
6661               ;; promising. 
6662               (if (and (search-forward "\nin-reply-to: " nil t)
6663                        (setq in-reply-to (gnus-header-value))
6664                        (string-match "<[^>]+>" in-reply-to))
6665                   (progn
6666                     (header-set-references 
6667                      header 
6668                      (setq ref (substring in-reply-to (match-beginning 0)
6669                                           (match-end 0))))
6670                     (setq ref (downcase ref)))
6671                 (setq ref "none")))
6672             ;; We do some threading while we read the headers. The
6673             ;; message-id and the last reference are both entered into
6674             ;; the same hash table. Some tippy-toeing around has to be
6675             ;; done in case an article has arrived before the article
6676             ;; which it refers to.
6677             (if (boundp (setq dep (intern (downcase id) dependencies)))
6678                 (if (car (symbol-value dep))
6679                     ;; An article with this Message-ID has already
6680                     ;; been seen, so we ignore this one, except we add
6681                     ;; any additional Xrefs (in case the two articles
6682                     ;; came from different servers.
6683                     (progn
6684                       (header-set-xref 
6685                        (car (symbol-value dep))
6686                        (concat (or (header-xref (car (symbol-value dep))) "")
6687                                (or (header-xref header) "")))
6688                       (setq header nil))
6689                   (setcar (symbol-value dep) header))
6690               (set dep (list header)))
6691             (if header
6692                 (progn
6693                   (if (boundp (setq dep (intern ref dependencies)))
6694                       (setcdr (symbol-value dep) 
6695                               (cons header (cdr (symbol-value dep))))
6696                     (set dep (list nil header)))
6697                   (setq headers (cons header headers))))
6698             (goto-char (point-max))))))
6699     (nreverse headers)))
6700
6701 ;; The following macros and functions were written by Felix Lee
6702 ;; <flee@cse.psu.edu>. 
6703
6704 (defmacro gnus-nov-read-integer ()
6705   '(prog1
6706        (if (= (following-char) ?\t)
6707            0
6708          (let ((num (condition-case nil (read buffer) (error nil))))
6709            (if (numberp num) num 0)))
6710      (or (eobp) (forward-char 1))))
6711
6712 (defmacro gnus-nov-skip-field ()
6713   '(search-forward "\t" eol 'move))
6714
6715 (defmacro gnus-nov-field ()
6716   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6717
6718 ;; Goes through the xover lines and returns a list of vectors
6719 (defun gnus-get-newsgroup-headers-xover (sequence)
6720   "Parse the news overview data in the server buffer, and return a
6721 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6722   ;; Get the Xref when the users reads the articles since most/some
6723   ;; NNTP servers do not include Xrefs when using XOVER.
6724   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6725   (let ((cur nntp-server-buffer)
6726         (dependencies gnus-newsgroup-dependencies)
6727         (none 0)
6728         number headers header)
6729     (save-excursion
6730       (set-buffer nntp-server-buffer)
6731       (goto-char (point-min))
6732       (while (and sequence (not (eobp)))
6733         (setq number (read cur))
6734         (while (and sequence (< (car sequence) number))
6735           (setq sequence (cdr sequence)))
6736         (and sequence 
6737              (eq number (car sequence))
6738              (progn
6739                (setq sequence (cdr sequence))
6740                (if (setq header 
6741                          (inline (gnus-nov-parse-line number dependencies)))
6742                    (setq headers (cons header headers)))))
6743         (forward-line 1))
6744       (setq headers (nreverse headers)))
6745     headers))
6746
6747 ;; This function has to be called with point after the article number
6748 ;; on the beginning of the line.
6749 (defun gnus-nov-parse-line (number dependencies)
6750   (let ((none 0)
6751         (eol (gnus-point-at-eol)) 
6752         (buffer (current-buffer))
6753         header ref id dep)
6754
6755     ;; overview: [num subject from date id refs chars lines misc]
6756     (narrow-to-region (point) eol)
6757     (forward-char)
6758
6759     (condition-case nil
6760         (setq header
6761               (vector 
6762                number                   ; number
6763                (gnus-nov-field)         ; subject
6764                (gnus-nov-field)         ; from
6765                (gnus-nov-field)         ; date
6766                (setq id (or (gnus-nov-field)
6767                             (concat "none+"
6768                                     (int-to-string 
6769                                      (setq none (1+ none)))))) ; id
6770                (progn
6771                  (save-excursion
6772                    (let ((beg (point)))
6773                      (search-forward "\t" eol)
6774                      (if (search-backward ">" beg t)
6775                          (setq ref 
6776                                (downcase 
6777                                 (buffer-substring 
6778                                  (1+ (point))
6779                                  (progn
6780                                    (search-backward "<" beg t)
6781                                    (point)))))
6782                        (setq ref nil))))
6783                  (gnus-nov-field))      ; refs
6784                (gnus-nov-read-integer)  ; chars
6785                (gnus-nov-read-integer)  ; lines
6786                (if (= (following-char) ?\n)
6787                    nil
6788                  (gnus-nov-field))      ; misc
6789                ))
6790       (error (progn 
6791                (ding)
6792                (message "Strange nov line.")
6793                (setq header nil)
6794                (goto-char eol))))
6795
6796     (widen)
6797
6798     ;; We build the thread tree.
6799     (and header
6800          (if (boundp (setq dep (intern (downcase id) dependencies)))
6801              (if (car (symbol-value dep))
6802                  ;; An article with this Message-ID has already been seen,
6803                  ;; so we ignore this one, except we add any additional
6804                  ;; Xrefs (in case the two articles came from different
6805                  ;; servers.
6806                  (progn
6807                    (header-set-xref 
6808                     (car (symbol-value dep))
6809                     (concat (or (header-xref (car (symbol-value dep))) "")
6810                             (or (header-xref header) "")))
6811                    (setq header nil))
6812                (setcar (symbol-value dep) header))
6813            (set dep (list header))))
6814     (if header
6815         (progn
6816           (if (boundp (setq dep (intern (or ref "none") 
6817                                         dependencies)))
6818               (setcdr (symbol-value dep) 
6819                       (cons header (cdr (symbol-value dep))))
6820             (set dep (list nil header)))))
6821     header))
6822
6823 (defun gnus-article-get-xrefs ()
6824   "Fill in the Xref value in `gnus-current-headers', if necessary.
6825 This is meant to be called in `gnus-article-internal-prepare-hook'."
6826   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6827                                  gnus-current-headers)))
6828     (or (not gnus-use-cross-reference)
6829         (not headers)
6830         (and (header-xref headers)
6831              (not (string= (header-xref headers) "")))
6832         (let ((case-fold-search t)
6833               xref)
6834           (save-restriction
6835             (gnus-narrow-to-headers)
6836             (goto-char (point-min))
6837             (if (or (and (eq (downcase (following-char)) ?x)
6838                          (looking-at "Xref:"))
6839                     (search-forward "\nXref:" nil t))
6840                 (progn
6841                   (goto-char (1+ (match-end 0)))
6842                   (setq xref (buffer-substring (point) 
6843                                                (progn (end-of-line) (point))))
6844                   (header-set-xref headers xref))))))))
6845
6846 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6847 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6848
6849 ;; Return a header specified by a NUMBER.
6850 (defun gnus-get-header-by-number (number)
6851   (save-excursion
6852     (set-buffer gnus-summary-buffer)
6853     (or gnus-newsgroup-headers-hashtb-by-number
6854         (gnus-make-headers-hashtable-by-number))
6855     (gnus-gethash (int-to-string number)
6856                   gnus-newsgroup-headers-hashtb-by-number)))
6857
6858 (defun gnus-make-headers-hashtable-by-number ()
6859   "Make hashtable for the variable gnus-newsgroup-headers by number."
6860   (save-excursion
6861     (set-buffer gnus-summary-buffer)
6862     (let ((headers gnus-newsgroup-headers)
6863           header)
6864       (setq gnus-newsgroup-headers-hashtb-by-number
6865             (gnus-make-hashtable (length headers)))
6866       (while headers
6867         (setq header (car headers))
6868         (gnus-sethash (int-to-string (header-number header))
6869                       header gnus-newsgroup-headers-hashtb-by-number)
6870         (setq headers (cdr headers))))))
6871
6872 (defun gnus-more-header-backward ()
6873   "Find new header backward."
6874   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6875         (artnum gnus-newsgroup-begin)
6876         (header nil))
6877     (while (and (not header)
6878                 (> artnum first))
6879       (setq artnum (1- artnum))
6880       (setq header (gnus-read-header artnum)))
6881     header))
6882
6883 (defun gnus-more-header-forward (&optional backward)
6884   "Find new header forward.
6885 If BACKWARD, find new header backward instead."
6886   (if backward
6887       (gnus-more-header-backward)
6888     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6889           (artnum gnus-newsgroup-end)
6890           (header nil))
6891       (while (and (not header)
6892                   (< artnum last))
6893         (setq artnum (1+ artnum))
6894         (setq header (gnus-read-header artnum)))
6895       header)))
6896
6897 (defun gnus-extend-newsgroup (header &optional backward)
6898   "Extend newsgroup selection with HEADER.
6899 Optional argument BACKWARD means extend toward backward."
6900   (if header
6901       (let ((artnum (header-number header)))
6902         (setq gnus-newsgroup-headers
6903               (if backward
6904                   (cons header gnus-newsgroup-headers)
6905                 (nconc gnus-newsgroup-headers (list header))))
6906         (setq gnus-newsgroup-unselected
6907               (delq artnum gnus-newsgroup-unselected))
6908         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6909         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6910
6911 (defun gnus-summary-work-articles (n)
6912   "Return a list of articles to be worked upon. The prefix argument,
6913 the list of process marked articles, and the current article will be
6914 taken into consideration."
6915   (let (articles)
6916     (if (and n (numberp n))
6917         (let ((backward (< n 0))
6918               (n (abs n)))
6919           (save-excursion
6920             (while (and (> n 0)
6921                         (setq articles (cons (gnus-summary-article-number) 
6922                                              articles))
6923                         (gnus-summary-search-forward nil nil backward))
6924               (setq n (1- n))))
6925           (sort articles (function <)))
6926       (or (reverse gnus-newsgroup-processable)
6927           (list (gnus-summary-article-number))))))
6928
6929 (defun gnus-summary-search-group (&optional backward use-level)
6930   "Search for next unread newsgroup.
6931 If optional argument BACKWARD is non-nil, search backward instead."
6932   (save-excursion
6933     (set-buffer gnus-group-buffer)
6934     (if (gnus-group-search-forward 
6935          backward nil (if use-level (gnus-group-group-level) nil))
6936         (gnus-group-group-name))))
6937
6938 (defun gnus-summary-best-group (&optional exclude-group)
6939   "Find the name of the best unread group.
6940 If EXCLUDE-GROUP, do not go to this group."
6941   (save-excursion
6942     (set-buffer gnus-group-buffer)
6943     (save-excursion
6944       (gnus-group-best-unread-group exclude-group))))
6945
6946 (defun gnus-summary-search-subject (&optional backward unread subject)
6947   "Search for article forward.
6948 If BACKWARD is non-nil, search backward.
6949 If UNREAD is non-nil, only unread articles are selected.
6950 If SUBJECT is non-nil, the article which has the same subject will be
6951 searched for." 
6952   (let ((func (if backward 'previous-single-property-change
6953                 'next-single-property-change))
6954         (beg (point))
6955         (did t)
6956         pos psubject)
6957     (beginning-of-line)
6958     (and gnus-summary-check-current unread
6959          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6960          (setq did nil))
6961     (if (not did)
6962         ()
6963       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6964       (while
6965           (and 
6966            (setq pos (funcall func (point) 'gnus-number))
6967            (goto-char (if backward (1- pos) pos))
6968            (setq did
6969                  (not (and
6970                        (or (not unread)
6971                            (eq (get-text-property (point) 'gnus-mark)
6972                                gnus-unread-mark))
6973                        (or (not subject)
6974                            (and (setq psubject (gnus-summary-subject-string))
6975                                 (gnus-subject-eq subject psubject))))))
6976            (if backward (if (bobp) nil (forward-char -1) t)
6977              (if (eobp) nil (forward-char 1) t)))))
6978     (if did
6979         (progn (goto-char beg) nil)
6980       (prog1
6981           (get-text-property (point) 'gnus-number)
6982         (gnus-summary-position-cursor)))))
6983
6984 (defun gnus-subject-eq (s1 s2)
6985   (cond
6986    ((null gnus-summary-gather-subject-limit)
6987     (equal (gnus-simplify-subject-re s1)
6988            (gnus-simplify-subject-re s2)))
6989    ((eq gnus-summary-gather-subject-limit 'fuzzy)
6990     (equal (gnus-simplify-subject-fuzzy s1)
6991            (gnus-simplify-subject-fuzzy s2)))
6992    ((numberp gnus-summary-gather-subject-limit)
6993     (equal (substring s1 gnus-summary-gather-subject-limit)
6994            (substring s2 gnus-summary-gather-subject-limit)))
6995    (t
6996     (equal s1 s2))))
6997     
6998 (defun gnus-summary-search-forward (&optional unread subject backward)
6999   "Search for article forward.
7000 If UNREAD is non-nil, only unread articles are selected.
7001 If SUBJECT is non-nil, the article which has the same subject will be
7002 searched for. 
7003 If BACKWARD is non-nil, the search will be performed backwards instead."
7004   (gnus-summary-search-subject backward unread subject))
7005
7006 (defun gnus-summary-search-backward (&optional unread subject)
7007   "Search for article backward.
7008 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7009 If 2nd optional argument SUBJECT is non-nil, the article which has
7010 the same subject will be searched for."
7011   (gnus-summary-search-forward unread subject t))
7012
7013 (defun gnus-summary-article-number (&optional number-or-nil)
7014   "The article number of the article on the current line.
7015 If there isn's an article number here, then we return the current
7016 article number."
7017   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7018     (if number-or-nil number (or number gnus-current-article))))
7019
7020 (defun gnus-summary-thread-level ()
7021   "The thread level of the article on the current line."
7022   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7023       0))
7024
7025 (defun gnus-summary-pseudo-article ()
7026   "The thread level of the article on the current line."
7027   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7028
7029 (defun gnus-summary-article-mark ()
7030   "The mark on the current line."
7031   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7032
7033 (defun gnus-summary-subject-string ()
7034   "Return current subject string or nil if nothing."
7035   (let ((article (gnus-summary-article-number))
7036         header)
7037     (and article 
7038          (setq header (gnus-get-header-by-number article))
7039          (vectorp header)
7040          (header-subject header))))
7041
7042 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7043 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7044 (defun gnus-summary-article-score ()
7045   "Return current article score."
7046   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7047       gnus-summary-default-score 0))
7048
7049 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
7050
7051 (defun gnus-summary-recenter ()
7052   "Center point in the summary window.
7053 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7054 displayed, no centering will be performed." 
7055   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7056   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7057   (let* ((top (cond ((< (window-height) 4) 0)
7058                     ((< (window-height) 7) 1)
7059                     (t 2)))
7060          (height (1- (window-height)))
7061          (bottom (save-excursion (goto-char (point-max))
7062                                  (forward-line (- height))
7063                                  (point)))
7064          (window (get-buffer-window (current-buffer))))
7065     (and 
7066      ;; The user has to want it,
7067      gnus-auto-center-summary 
7068      ;; the article buffer must be displayed,
7069      (get-buffer-window gnus-article-buffer)
7070      ;; Set the window start to either `bottom', which is the biggest
7071      ;; possible valid number, or the second line from the top,
7072      ;; whichever is the least.
7073      (set-window-start
7074       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7075
7076 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7077 (defun gnus-short-group-name (group &optional levels)
7078   "Collapse GROUP name LEVELS."
7079   (let* ((name "") (foreign "") (depth -1) (skip 1)
7080          (levels (or levels
7081                      (progn
7082                        (while (string-match "\\." group skip)
7083                          (setq skip (match-end 0)
7084                                depth (+ depth 1)))
7085                        depth))))
7086     (if (string-match ":" group)
7087         (setq foreign (substring group 0 (match-end 0))
7088               group (substring group (match-end 0))))
7089     (while group
7090       (if (and (string-match "\\." group) (> levels 0))
7091           (setq name (concat name (substring group 0 1))
7092                 group (substring group (match-end 0))
7093                 levels (- levels 1)
7094                 name (concat name "."))
7095         (setq name (concat foreign name group)
7096               group nil)))
7097     name))
7098
7099 (defun gnus-summary-jump-to-group (newsgroup)
7100   "Move point to NEWSGROUP in group mode buffer."
7101   ;; Keep update point of group mode buffer if visible.
7102   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7103       (save-window-excursion
7104         ;; Take care of tree window mode.
7105         (if (get-buffer-window gnus-group-buffer)
7106             (pop-to-buffer gnus-group-buffer))
7107         (gnus-group-jump-to-group newsgroup))
7108     (save-excursion
7109       ;; Take care of tree window mode.
7110       (if (get-buffer-window gnus-group-buffer)
7111           (pop-to-buffer gnus-group-buffer)
7112         (set-buffer gnus-group-buffer))
7113       (gnus-group-jump-to-group newsgroup))))
7114
7115 ;; This function returns a list of article numbers based on the
7116 ;; difference between the ranges of read articles in this group and
7117 ;; the range of active articles.
7118 (defun gnus-list-of-unread-articles (group)
7119   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7120          (active (gnus-gethash group gnus-active-hashtb))
7121          (last (cdr active))
7122          unread first nlast unread)
7123     ;; If none are read, then all are unread. 
7124     (if (not read)
7125         (setq first (car active))
7126       ;; If the range of read articles is a single range, then the
7127       ;; first unread article is the article after the last read
7128       ;; article. Sounds logical, doesn't it?
7129       (if (not (listp (cdr read)))
7130           (setq first (1+ (cdr read)))
7131         ;; `read' is a list of ranges.
7132         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7133                                 (car (car read)))) 1)
7134             (setq first 1))
7135         (while read
7136           (if first 
7137               (while (< first nlast)
7138                 (setq unread (cons first unread))
7139                 (setq first (1+ first))))
7140           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7141           (setq nlast (if (atom (car (cdr read))) 
7142                           (car (cdr read))
7143                         (car (car (cdr read)))))
7144           (setq read (cdr read)))))
7145     ;; And add the last unread articles.
7146     (while (<= first last)
7147       (setq unread (cons first unread))
7148       (setq first (1+ first)))
7149     ;; Return the list of unread articles.
7150     (nreverse unread)))
7151
7152 (defun gnus-list-of-read-articles (group)
7153   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7154         (active (gnus-gethash group gnus-active-hashtb)))
7155     (and info active
7156          (gnus-sorted-complement 
7157           (gnus-uncompress-range active) 
7158           (gnus-list-of-unread-articles group)))))
7159
7160 ;; Various summary commands
7161
7162 (defun gnus-summary-universal-argument ()
7163   "Perform any operation on all articles marked with the process mark."
7164   (interactive)
7165   (gnus-set-global-variables)
7166   (let ((articles (reverse gnus-newsgroup-processable))
7167         key func)
7168     (or articles (error "No articles marked"))
7169     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7170         (error "Undefined key"))
7171     (while articles
7172       (gnus-summary-goto-subject (car articles))
7173       (command-execute func)
7174       (gnus-summary-remove-process-mark (car articles))
7175       (setq articles (cdr articles)))))
7176
7177 (defun gnus-summary-toggle-truncation (arg)
7178   "Toggle truncation of summary lines.
7179 With arg, turn line truncation on iff arg is positive."
7180   (interactive "P")
7181   (setq truncate-lines
7182         (if (null arg) (not truncate-lines)
7183           (> (prefix-numeric-value arg) 0)))
7184   (redraw-display))
7185
7186 (defun gnus-summary-reselect-current-group (all)
7187   "Once exit and then reselect the current newsgroup.
7188 The prefix argument ALL means to select all articles."
7189   (interactive "P")
7190   (gnus-set-global-variables)
7191   (let ((current-subject (gnus-summary-article-number))
7192         (group gnus-newsgroup-name))
7193     (setq gnus-newsgroup-begin nil)
7194     (gnus-summary-exit t)
7195     ;; We have to adjust the point of group mode buffer because the
7196     ;; current point was moved to the next unread newsgroup by
7197     ;; exiting.
7198     (gnus-summary-jump-to-group group)
7199     (gnus-group-read-group all t)
7200     (gnus-summary-goto-subject current-subject)))
7201
7202 (defun gnus-summary-rescan-group (all)
7203   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7204   (interactive "P")
7205   (gnus-set-global-variables)
7206   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7207   (let ((group gnus-newsgroup-name))
7208     (gnus-summary-exit)
7209     (gnus-summary-jump-to-group group)
7210     (save-excursion
7211       (set-buffer gnus-group-buffer)
7212       (gnus-group-get-new-news-this-group 1))
7213     (gnus-summary-jump-to-group group)
7214     (gnus-group-read-group all)))
7215
7216 (defun gnus-summary-update-info ()
7217   (let* ((group gnus-newsgroup-name)
7218          (method (car (gnus-find-method-for-group group))))
7219     (if gnus-newsgroup-kill-headers
7220         (setq gnus-newsgroup-killed
7221               (gnus-compress-sequence
7222                (nconc
7223                 (gnus-set-sorted-intersection
7224                  (gnus-uncompress-range gnus-newsgroup-killed)
7225                  (setq gnus-newsgroup-unselected
7226                        (sort gnus-newsgroup-unselected '<)))
7227                 (setq gnus-newsgroup-unreads
7228                       (sort gnus-newsgroup-unreads '<))) t)))
7229     (or (listp (cdr gnus-newsgroup-killed))
7230         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7231     (let ((updated nil)
7232           (headers gnus-newsgroup-headers))
7233       (gnus-close-group group)
7234       (run-hooks 'gnus-exit-group-hook)
7235       (gnus-update-read-articles 
7236        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7237        gnus-newsgroup-marked
7238        t gnus-newsgroup-replied gnus-newsgroup-expirable
7239        gnus-newsgroup-killed gnus-newsgroup-dormant
7240        gnus-newsgroup-bookmarks 
7241        (and gnus-save-score gnus-newsgroup-scored))
7242       (and gnus-use-cross-reference
7243            (gnus-mark-xrefs-as-read 
7244             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7245       ;; Do adaptive scoring, and possibly save score files.
7246       (and gnus-newsgroup-adaptive
7247            (gnus-score-adaptive))
7248       (and gnus-use-scoring 
7249            (fboundp 'gnus-score-save)
7250            (funcall 'gnus-score-save))
7251       ;; Do not switch windows but change the buffer to work.
7252       (set-buffer gnus-group-buffer)
7253       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7254           (gnus-group-update-group group)))))
7255   
7256 (defun gnus-summary-exit (&optional temporary)
7257   "Exit reading current newsgroup, and then return to group selection mode.
7258 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7259   (interactive)
7260   (gnus-set-global-variables)
7261   (gnus-kill-save-kill-buffer)
7262   (let* ((group gnus-newsgroup-name)
7263          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7264                                                   gnus-newsgroup-name))))
7265          (mode major-mode)
7266          (method (car (gnus-find-method-for-group group)))
7267          (buf (current-buffer)))
7268     (gnus-summary-update-info) ; Make all changes in this group permanent.
7269     ;; Make sure where I was, and go to next newsgroup.
7270     (or quit-config
7271         (progn
7272           (gnus-group-jump-to-group group)
7273           (gnus-group-next-unread-group 1)))
7274     (if temporary
7275         nil                             ;Nothing to do.
7276       (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7277       ;; We set all buffer-local variables to nil. It is unclear why
7278       ;; this is needed, but if we don't, buffer-local variables are
7279       ;; not garbage-collected, it seems. This would the lead to en
7280       ;; ever-growing Emacs.
7281       (set-buffer buf)
7282       (gnus-summary-clear-local-variables)
7283       ;; We clear the global counterparts of the buffer-local
7284       ;; variables as well, just to be on the safe side.
7285       (gnus-configure-windows 'group)
7286       (gnus-summary-clear-local-variables)
7287       ;; Return to group mode buffer. 
7288       (if (eq mode 'gnus-summary-mode)
7289           (gnus-kill-buffer buf))
7290       (if (get-buffer gnus-article-buffer)
7291           (bury-buffer gnus-article-buffer))
7292       (setq gnus-current-select-method gnus-select-method)
7293       (pop-to-buffer gnus-group-buffer)
7294       (if (not quit-config)
7295           (progn
7296             (gnus-group-jump-to-group group)
7297             (gnus-group-next-unread-group 1))
7298         (if (not (buffer-name (car quit-config)))
7299             (gnus-configure-windows 'group)
7300           (set-buffer (car quit-config))
7301           (and (eq major-mode 'gnus-summary-mode)
7302                (gnus-set-global-variables))
7303           (gnus-configure-windows (cdr quit-config)))))))
7304
7305 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7306 (defun gnus-summary-exit-no-update (&optional no-questions)
7307   "Quit reading current newsgroup without updating read article info."
7308   (interactive)
7309   (let* ((group gnus-newsgroup-name)
7310          (quit-config (nth 1 (assoc 'quit-config 
7311                                     (gnus-find-method-for-group group)))))
7312     (if (or no-questions
7313             gnus-expert-user
7314             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7315         (progn
7316           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7317           (gnus-close-group group)
7318           (gnus-summary-clear-local-variables)
7319           (set-buffer gnus-group-buffer)
7320           (gnus-summary-clear-local-variables)
7321           ;; Return to group selection mode.
7322           (gnus-configure-windows 'group)
7323           (if (get-buffer gnus-summary-buffer)
7324               (kill-buffer gnus-summary-buffer))
7325           (if (get-buffer gnus-article-buffer)
7326               (bury-buffer gnus-article-buffer))
7327           (if (equal (gnus-group-group-name) group)
7328               (gnus-group-next-unread-group 1))
7329           (if quit-config
7330               (progn
7331                 (if (not (buffer-name (car quit-config)))
7332                     (gnus-configure-windows 'group)
7333                   (set-buffer (car quit-config))
7334                   (and (eq major-mode 'gnus-summary-mode)
7335                        (gnus-set-global-variables))
7336                   (gnus-configure-windows (cdr quit-config)))))))))
7337
7338 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7339 (defun gnus-summary-fetch-faq (group)
7340   "Fetch the FAQ for the current group."
7341   (interactive (list gnus-newsgroup-name))
7342   (gnus-configure-windows 'summary-faq)
7343   (find-file (concat gnus-group-faq-directory group)))
7344
7345 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7346 (defun gnus-summary-describe-group (force)
7347   "Describe the current newsgroup."
7348   (interactive "P")
7349   (gnus-group-describe-group force gnus-newsgroup-name))
7350
7351 (defun gnus-summary-describe-briefly ()
7352   "Describe summary mode commands briefly."
7353   (interactive)
7354   (gnus-message 6
7355     (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")))
7356
7357 ;; Walking around group mode buffer from summary mode.
7358
7359 (defun gnus-summary-next-group (&optional no-article group backward)
7360   "Exit current newsgroup and then select next unread newsgroup.
7361 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7362 If BACKWARD, go to previous group instead."
7363   (interactive "P")
7364   (gnus-set-global-variables)
7365   (let ((ingroup gnus-newsgroup-name)
7366         (sumbuf (current-buffer))
7367         num)
7368     (set-buffer gnus-group-buffer)
7369     (if (and group
7370              (or (and (numberp (setq num (car (gnus-gethash
7371                                                group gnus-newsrc-hashtb))))
7372                       (< num 1))
7373                  (null num)))
7374         (progn
7375           (gnus-group-jump-to-group group)
7376           (setq group nil))
7377       (gnus-group-jump-to-group ingroup))
7378     (gnus-summary-search-group backward)
7379     (let ((group (or group (gnus-summary-search-group backward))))
7380       (set-buffer sumbuf)
7381       (gnus-summary-exit t)             ;Update all information.
7382       (if (null group)
7383           (gnus-summary-exit-no-update t)
7384         (gnus-message 5 "Selecting %s..." group)
7385         (set-buffer gnus-group-buffer)
7386         ;; We are now in group mode buffer.
7387         ;; Make sure group mode buffer point is on GROUP.
7388         (gnus-group-jump-to-group group)
7389         (if (not (eq gnus-auto-select-next 'quietly))
7390             (progn
7391               (gnus-summary-read-group group nil no-article sumbuf)
7392               (and (string= gnus-newsgroup-name ingroup)
7393                    (bufferp sumbuf) (buffer-name sumbuf)
7394                    (progn
7395                      (set-buffer (setq gnus-summary-buffer sumbuf))
7396                      (gnus-summary-exit-no-update t))))
7397           (let ((prevgroup group))
7398             (gnus-summary-read-group group nil no-article sumbuf)
7399             (while (and (string= gnus-newsgroup-name ingroup)
7400                         (bufferp sumbuf) 
7401                         (buffer-name sumbuf)
7402                         (not (string= prevgroup (gnus-group-group-name))))
7403               (set-buffer gnus-group-buffer)
7404               (gnus-summary-read-group 
7405                (setq prevgroup (gnus-group-group-name)) 
7406                nil no-article sumbuf))
7407             (and (string= prevgroup (gnus-group-group-name))
7408                  ;; We have reached the final group in the group
7409                  ;; buffer.
7410                  (progn
7411                    (if (buffer-name sumbuf)
7412                        (progn
7413                          (set-buffer sumbuf)
7414                          (gnus-summary-exit)))))))))))
7415
7416 (defun gnus-summary-prev-group (no-article)
7417   "Exit current newsgroup and then select previous unread newsgroup.
7418 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7419   (interactive "P")
7420   (gnus-summary-next-group no-article nil t))
7421
7422 ;; Walking around summary lines.
7423
7424 (defun gnus-summary-first-subject (unread)
7425   "Go to the first unread subject.
7426 If UNREAD is non-nil, go to the first unread article.
7427 Returns nil if there are no unread articles."
7428   (interactive "P")
7429   (prog1
7430       (cond ((not unread)
7431              (goto-char (point-min)))
7432             ((gnus-goto-char 
7433               (text-property-any 
7434                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7435              t)
7436             (t 
7437              ;; There are no unread articles.
7438              (gnus-message 3 "No more unread articles")
7439              nil))
7440     (gnus-summary-position-cursor)))
7441
7442 (defun gnus-summary-next-subject (n &optional unread dont-display)
7443   "Go to next N'th summary line.
7444 If N is negative, go to the previous N'th subject line.
7445 If UNREAD is non-nil, only unread articles are selected.
7446 The difference between N and the actual number of steps taken is
7447 returned."
7448   (interactive "p")
7449   (let ((backward (< n 0))
7450         (n (abs n)))
7451     (while (and (> n 0)
7452                 (gnus-summary-search-forward unread nil backward))
7453       (setq n (1- n)))
7454     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7455                                (if unread " unread" "")))
7456     (or dont-display
7457         (progn
7458           (gnus-summary-recenter)
7459           (gnus-summary-position-cursor)))
7460   n))
7461
7462 (defun gnus-summary-next-unread-subject (n)
7463   "Go to next N'th unread summary line."
7464   (interactive "p")
7465   (gnus-summary-next-subject n t))
7466
7467 (defun gnus-summary-prev-subject (n &optional unread)
7468   "Go to previous N'th summary line.
7469 If optional argument UNREAD is non-nil, only unread article is selected."
7470   (interactive "p")
7471   (gnus-summary-next-subject (- n) unread))
7472
7473 (defun gnus-summary-prev-unread-subject (n)
7474   "Go to previous N'th unread summary line."
7475   (interactive "p")
7476   (gnus-summary-next-subject (- n) t))
7477
7478 (defun gnus-summary-goto-subject (article)
7479   "Go the subject line of ARTICLE."
7480   (interactive
7481    (list
7482     (string-to-int
7483      (completing-read "Article number: "
7484                       (mapcar
7485                        (lambda (headers)
7486                          (list
7487                           (int-to-string (header-number headers))))
7488                        gnus-newsgroup-headers)
7489                       nil 'require-match))))
7490   (or article (error "No article number"))
7491   (let ((b (point)))
7492     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7493                                                 'gnus-number article)))
7494         ()
7495       (gnus-summary-show-thread)
7496       ;; Skip dummy articles. 
7497       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7498           (forward-line 1))
7499       (prog1
7500           (if (not (eobp))
7501               article
7502             (goto-char b)
7503             nil)
7504         (gnus-summary-position-cursor)))))
7505
7506 ;; Walking around summary lines with displaying articles.
7507
7508 (defun gnus-summary-expand-window ()
7509   "Make the summary buffer take up the entire Emacs frame."
7510   (interactive)
7511   (gnus-set-global-variables)
7512   (gnus-configure-windows 'summary))
7513
7514 (defun gnus-summary-display-article (article &optional all-header)
7515   "Display ARTICLE in article buffer."
7516   (gnus-set-global-variables)
7517   (if (null article)
7518       nil
7519     (gnus-article-prepare article all-header)
7520     (gnus-summary-show-thread)
7521     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7522         (progn
7523           (forward-line 1)
7524           (gnus-summary-position-cursor)))
7525     (run-hooks 'gnus-select-article-hook)
7526     (gnus-summary-recenter)
7527 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7528 ;    (sit-for 0)
7529     (gnus-summary-goto-subject article)
7530     ;; Successfully display article.
7531     (gnus-summary-update-line)
7532     (gnus-article-set-window-start 
7533      (cdr (assq article gnus-newsgroup-bookmarks)))
7534     t))
7535
7536 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7537   "Select the current article.
7538 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7539 non-nil, the article will be re-fetched even if it already present in
7540 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7541 be displayed."
7542   (and (not pseudo) (gnus-summary-pseudo-article)
7543        (error "This is a pseudo-article."))
7544   (let ((article (or article (gnus-summary-article-number)))
7545         (all-headers (not (not all-headers))) ;Must be T or NIL.
7546         did) 
7547     (prog1
7548         (save-excursion
7549           (set-buffer gnus-summary-buffer)
7550           (if (or (null gnus-current-article)
7551                   (null gnus-article-current)
7552                   (not (eq article (cdr gnus-article-current)))
7553                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7554                   force)
7555               ;; The requested article is different from the current article.
7556               (progn
7557                 (gnus-summary-display-article article all-headers)
7558                 (setq did article))
7559             (if all-headers (gnus-article-show-all-headers))
7560             nil))
7561       (if did 
7562           (gnus-article-set-window-start 
7563            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7564
7565 (defun gnus-summary-set-current-mark (&optional current-mark)
7566   "Obsolete function."
7567   nil)
7568
7569 (defun gnus-summary-next-article (unread &optional subject backward)
7570   "Select the next article.
7571 If UNREAD, only unread articles are selected.
7572 If SUBJECT, only articles with SUBJECT are selected.
7573 If BACKWARD, the previous article is selected instead of the next."
7574   (interactive "P")
7575   (gnus-set-global-variables)
7576   (let ((opoint (point))
7577         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7578         header)
7579     (cond
7580      ;; Is there such an article?
7581      ((gnus-summary-display-article 
7582        (gnus-summary-search-forward unread subject backward))
7583       (gnus-summary-position-cursor))
7584      ;; If not, we try the first unread, if that is wanted.
7585      ((and subject
7586            gnus-auto-select-same
7587            (gnus-summary-first-unread-article))
7588       (gnus-message 6 "Wrapped"))
7589      ;; Try to get next/previous article not displayed in this group.
7590      ((and gnus-auto-extend-newsgroup
7591            (not unread) (not subject)
7592            (setq header (gnus-more-header-forward backward)))
7593       (gnus-extend-newsgroup header backward)
7594       (let ((buffer-read-only nil))
7595         (goto-char (if backward (point-min) (point-max)))
7596         (gnus-summary-prepare-threads (list header) 0))
7597       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7598                                    gnus-newsgroup-end)))
7599      ;; Go to next/previous group.
7600      (t
7601       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7602           (gnus-summary-jump-to-group gnus-newsgroup-name))
7603       (let ((cmd (aref (this-command-keys) 0))
7604             (group 
7605              (if (eq gnus-keep-same-level 'best) 
7606                  (gnus-summary-best-group gnus-newsgroup-name)
7607                (gnus-summary-search-group backward gnus-keep-same-level))))
7608         ;; For some reason, the group window gets selected. We change
7609         ;; it back.  
7610         (select-window (get-buffer-window (current-buffer)))
7611         ;; Keep just the event type of CMD.
7612         (and (listp cmd) (setq cmd (car cmd)))
7613         ;; Select next unread newsgroup automagically.
7614         (cond 
7615          ((not gnus-auto-select-next)
7616           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7617          ((eq gnus-auto-select-next 'quietly)
7618           ;; Select quietly.
7619           (if (assoc 'quit-config (gnus-find-method-for-group 
7620                                    gnus-newsgroup-name))
7621               (gnus-summary-exit)
7622             (gnus-message 7 "No more%s articles (%s)..."
7623                           (if unread " unread" "") 
7624                           (if group (concat "selecting " group)
7625                             "exiting"))
7626             (gnus-summary-next-group nil group backward)))
7627          (t
7628           (let ((keystrokes '(?\C-n ?\C-p))
7629                 key)
7630             (while (or (null key) (memq key keystrokes))
7631               (gnus-message 
7632                7 "No more%s articles%s" (if unread " unread" "")
7633                (if (and group (not (assoc 'quit-config
7634                                           (gnus-find-method-for-group 
7635                                            gnus-newsgroup-name))))
7636                    (format " (Type %s for %s [%s])"
7637                            (single-key-description cmd) group
7638                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7639                  (format " (Type %s to exit %s)"
7640                          (single-key-description cmd)
7641                          gnus-newsgroup-name)))
7642               ;; Confirm auto selection.
7643               (let* ((event (read-event)))
7644                 (setq key (if (listp event) (car event) event))
7645                 (if (memq key keystrokes)
7646                     (let ((obuf (current-buffer)))
7647                       (switch-to-buffer gnus-group-buffer)
7648                       (gnus-group-jump-to-group group)
7649                       (execute-kbd-macro (char-to-string key))
7650                       (setq group (gnus-group-group-name))
7651                       (switch-to-buffer obuf)))))
7652             (if (equal key cmd)
7653                 (if (or (not group) (assoc 'quit-config
7654                                            (gnus-find-method-for-group
7655                                             gnus-newsgroup-name)))
7656                     (gnus-summary-exit)
7657                   (gnus-summary-next-group nil group backward))
7658               (setq unread-command-events (list key)))))))))))
7659
7660 (defun gnus-summary-next-unread-article ()
7661   "Select unread article after current one."
7662   (interactive)
7663   (gnus-summary-next-article t (and gnus-auto-select-same
7664                                     (gnus-summary-subject-string))))
7665
7666 (defun gnus-summary-prev-article (unread &optional subject)
7667   "Select the article after the current one.
7668 If UNREAD is non-nil, only unread articles are selected."
7669   (interactive "P")
7670   (gnus-summary-next-article unread subject t))
7671
7672 (defun gnus-summary-prev-unread-article ()
7673   "Select unred article before current one."
7674   (interactive)
7675   (gnus-summary-prev-article t (and gnus-auto-select-same
7676                                     (gnus-summary-subject-string))))
7677
7678 (defun gnus-summary-next-page (lines &optional circular)
7679   "Show next page of selected article.
7680 If end of article, select next article.
7681 Argument LINES specifies lines to be scrolled up.
7682 If CIRCULAR is non-nil, go to the start of the article instead of 
7683 instead of selecting the next article when reaching the end of the
7684 current article." 
7685   (interactive "P")
7686   (setq gnus-summary-buffer (current-buffer))
7687   (gnus-set-global-variables)
7688   (let ((article (gnus-summary-article-number))
7689         (endp nil))
7690     (gnus-configure-windows 'article)
7691     (if (or (null gnus-current-article)
7692             (null gnus-article-current)
7693             (/= article (cdr gnus-article-current))
7694             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7695         ;; Selected subject is different from current article's.
7696         (gnus-summary-display-article article)
7697       (gnus-eval-in-buffer-window
7698        gnus-article-buffer
7699        (setq endp (gnus-article-next-page lines)))
7700       (if endp
7701           (cond (circular
7702                  (gnus-summary-beginning-of-article))
7703                 (lines
7704                  (gnus-message 3 "End of message"))
7705                 ((null lines)
7706                  (gnus-summary-next-unread-article)))))
7707     (gnus-summary-recenter)
7708     (gnus-summary-position-cursor)))
7709
7710 (defun gnus-summary-prev-page (lines)
7711   "Show previous page of selected article.
7712 Argument LINES specifies lines to be scrolled down."
7713   (interactive "P")
7714   (gnus-set-global-variables)
7715   (let ((article (gnus-summary-article-number)))
7716     (gnus-configure-windows 'article)
7717     (if (or (null gnus-current-article)
7718             (null gnus-article-current)
7719             (/= article (cdr gnus-article-current))
7720             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7721         ;; Selected subject is different from current article's.
7722         (gnus-summary-display-article article)
7723       (gnus-summary-recenter)
7724       (gnus-eval-in-buffer-window gnus-article-buffer
7725         (gnus-article-prev-page lines))))
7726   (gnus-summary-position-cursor))
7727
7728 (defun gnus-summary-scroll-up (lines)
7729   "Scroll up (or down) one line current article.
7730 Argument LINES specifies lines to be scrolled up (or down if negative)."
7731   (interactive "p")
7732   (gnus-set-global-variables)
7733   (gnus-configure-windows 'article)
7734   (or (gnus-summary-select-article nil nil 'pseudo)
7735       (gnus-eval-in-buffer-window 
7736        gnus-article-buffer
7737        (cond ((> lines 0)
7738               (if (gnus-article-next-page lines)
7739                   (gnus-message 3 "End of message")))
7740              ((< lines 0)
7741               (gnus-article-prev-page (- lines))))))
7742   (gnus-summary-recenter)
7743   (gnus-summary-position-cursor))
7744
7745 (defun gnus-summary-next-same-subject ()
7746   "Select next article which has the same subject as current one."
7747   (interactive)
7748   (gnus-set-global-variables)
7749   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7750
7751 (defun gnus-summary-prev-same-subject ()
7752   "Select previous article which has the same subject as current one."
7753   (interactive)
7754   (gnus-set-global-variables)
7755   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7756
7757 (defun gnus-summary-next-unread-same-subject ()
7758   "Select next unread article which has the same subject as current one."
7759   (interactive)
7760   (gnus-set-global-variables)
7761   (gnus-summary-next-article t (gnus-summary-subject-string)))
7762
7763 (defun gnus-summary-prev-unread-same-subject ()
7764   "Select previous unread article which has the same subject as current one."
7765   (interactive)
7766   (gnus-set-global-variables)
7767   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7768
7769 (defun gnus-summary-first-unread-article ()
7770   "Select the first unread article. 
7771 Return nil if there are no unread articles."
7772   (interactive)
7773   (gnus-set-global-variables)
7774   (prog1
7775       (if (gnus-summary-first-subject t)
7776           (gnus-summary-display-article (gnus-summary-article-number)))
7777     (gnus-summary-position-cursor)))
7778
7779 (defun gnus-summary-best-unread-article ()
7780   "Select the unread article with the highest score."
7781   (interactive)
7782   (gnus-set-global-variables)
7783   (let ((scored gnus-newsgroup-scored)
7784         (best -1000000)
7785         article art)
7786     (while scored
7787       (or (> best (cdr (car scored)))
7788           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7789                (not (memq art gnus-newsgroup-marked))
7790                (not (memq art gnus-newsgroup-dormant))
7791                (if (= best (cdr (car scored)))
7792                    (setq article (min art article))
7793                  (setq article art)
7794                  (setq best (cdr (car scored))))))
7795       (setq scored (cdr scored)))
7796     (if article 
7797         (gnus-summary-goto-article article)
7798       (gnus-summary-first-unread-article))
7799     (gnus-summary-position-cursor)))
7800
7801 (defun gnus-summary-goto-article (article &optional all-headers)
7802   "Fetch ARTICLE and display it if it exists.
7803 If ALL-HEADERS is non-nil, no header lines are hidden."
7804   (interactive
7805    (list
7806     (string-to-int
7807      (completing-read 
7808       "Article number: "
7809       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7810               gnus-newsgroup-headers) 
7811       nil 'require-match))))
7812   (prog1
7813       (and (gnus-summary-goto-subject article)
7814            (gnus-summary-display-article article all-headers))
7815     (gnus-summary-position-cursor)))
7816
7817 (defun gnus-summary-goto-last-article ()
7818   "Go to the last article."
7819   (interactive)
7820   (prog1
7821       (and gnus-last-article
7822            (gnus-summary-goto-article gnus-last-article))
7823     (gnus-summary-position-cursor)))
7824
7825 (defun gnus-summary-pop-article (number)
7826   "Pop one article off the history and go to the previous.
7827 NUMBER articles will be popped off."
7828   (interactive "p")
7829   (let (to)
7830     (setq gnus-newsgroup-history
7831           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7832     (if to
7833         (gnus-summary-goto-article (car to))
7834       (error "Article history empty")))
7835   (gnus-summary-position-cursor))
7836
7837 ;; Summary article oriented commands
7838
7839 (defun gnus-summary-refer-parent-article (n)
7840   "Refer parent article N times.
7841 The difference between N and the number of articles fetched is returned."
7842   (interactive "p")
7843   (gnus-set-global-variables)
7844   (while 
7845       (and 
7846        (> n 0)
7847        (let ((ref (header-references (gnus-get-header-by-number
7848                                       (gnus-summary-article-number)))))
7849          (if (and ref (not (equal ref ""))
7850                   (string-match "<[^<>]*>[ \t]*$" ref))
7851              (gnus-summary-refer-article 
7852               (substring ref (match-beginning 0) (match-end 0)))
7853            (gnus-message 1 "No references in article %d"
7854                          (gnus-summary-article-number))
7855            nil)))
7856     (setq n (1- n)))
7857   (gnus-summary-position-cursor)
7858   n)
7859     
7860 (defun gnus-summary-refer-article (message-id)
7861   "Refer article specified by MESSAGE-ID.
7862 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7863   (interactive "sMessage-ID: ")
7864   (if (or (not (stringp message-id))
7865           (zerop (length message-id)))
7866       ()
7867     ;; Construct the correct Message-ID if necessary.
7868     ;; Suggested by tale@pawl.rpi.edu.
7869     (or (string-match "^<" message-id)
7870         (setq message-id (concat "<" message-id)))
7871     (or (string-match ">$" message-id)
7872         (setq message-id (concat message-id ">")))
7873     (let ((header (car (gnus-gethash (downcase message-id)
7874                                      gnus-newsgroup-dependencies))))
7875       (if header
7876           (or (gnus-summary-goto-article (header-number header))
7877               ;; The header has been read, but the article had been
7878               ;; expunged, so we insert it again.
7879               (progn
7880                 (gnus-summary-insert-line
7881                  nil header 0 nil gnus-read-mark nil nil
7882                  (header-subject header))
7883                 (forward-line -1)
7884                 (header-number header)))
7885         (let ((gnus-override-method gnus-refer-article-method)
7886               (gnus-ancient-mark gnus-read-mark)
7887               (tmp-buf (get-buffer-create " *gnus refer"))
7888               (tmp-point (window-start
7889                           (get-buffer-window gnus-article-buffer)))
7890               number)
7891           (and gnus-refer-article-method
7892                (or (gnus-server-opened gnus-refer-article-method)
7893                    (gnus-open-server gnus-refer-article-method)))
7894           ;; Save the old article buffer.
7895           (save-excursion
7896             (set-buffer tmp-buf)
7897             (buffer-disable-undo (current-buffer))
7898             (insert-buffer-substring gnus-article-buffer))
7899           (prog1
7900               (if (gnus-article-prepare 
7901                    message-id nil (gnus-read-header message-id))
7902                   (progn
7903                     (setq number (header-number gnus-current-headers))
7904                     (gnus-rebuild-thread message-id)
7905                     (gnus-summary-goto-subject number)
7906                     (gnus-summary-recenter)
7907                     (gnus-article-set-window-start 
7908                      (cdr (assq number gnus-newsgroup-bookmarks)))
7909                     message-id)
7910                 ;; We restore the old article buffer.
7911                 (save-excursion
7912                   (set-buffer gnus-article-buffer)
7913                   (let ((buffer-read-only nil))
7914                     (insert-buffer-substring tmp-buf)
7915                     (and tmp-point
7916                          (set-window-start (get-buffer-window (current-buffer))
7917                                            tmp-point))))
7918                 nil)
7919             (kill-buffer tmp-buf)))))))
7920
7921 (defun gnus-summary-enter-digest-group ()
7922   "Enter a digest group based on the current article."
7923   (interactive)
7924   (gnus-set-global-variables)
7925   (gnus-summary-select-article)
7926   ;; We do not want a narrowed article.
7927   (gnus-summary-stop-page-breaking)
7928   (let ((name (format "%s-%d" 
7929                       (gnus-group-prefixed-name 
7930                        gnus-newsgroup-name (list 'nndoc "")) 
7931                       gnus-current-article))
7932         (ogroup gnus-newsgroup-name)
7933         (buf (current-buffer)))
7934     (if (gnus-group-read-ephemeral-group 
7935          name (list 'nndoc name
7936                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7937                     '(nndoc-article-type digest))
7938          t)
7939         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7940                 (list (list (cons 'to-group ogroup))))
7941       (switch-to-buffer buf)
7942       (gnus-set-global-variables)
7943       (gnus-configure-windows 'summary)
7944       (gnus-message 3 "Article not a digest?"))))
7945
7946 (defun gnus-summary-isearch-article ()
7947   "Do incremental search forward on current article."
7948   (interactive)
7949   (gnus-set-global-variables)
7950   (gnus-summary-select-article)
7951   (gnus-eval-in-buffer-window 
7952    gnus-article-buffer (isearch-forward)))
7953
7954 (defun gnus-summary-search-article-forward (regexp &optional backward)
7955   "Search for an article containing REGEXP forward.
7956 If BACKWARD, search backward instead."
7957   (interactive
7958    (list (read-string
7959           (format "Search article %s (regexp%s): "
7960                   (if current-prefix-arg "backward" "forward")
7961                   (if gnus-last-search-regexp
7962                       (concat ", default " gnus-last-search-regexp)
7963                     "")))
7964          current-prefix-arg))
7965   (gnus-set-global-variables)
7966   (if (string-equal regexp "")
7967       (setq regexp (or gnus-last-search-regexp ""))
7968     (setq gnus-last-search-regexp regexp))
7969   (if (gnus-summary-search-article regexp backward)
7970       (gnus-article-set-window-start 
7971        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
7972     (error "Search failed: \"%s\"" regexp)))
7973
7974 (defun gnus-summary-search-article-backward (regexp)
7975   "Search for an article containing REGEXP backward."
7976   (interactive
7977    (list (read-string
7978           (format "Search article backward (regexp%s): "
7979                   (if gnus-last-search-regexp
7980                       (concat ", default " gnus-last-search-regexp)
7981                     "")))))
7982   (gnus-summary-search-article-forward regexp 'backward))
7983
7984 (defun gnus-summary-search-article (regexp &optional backward)
7985   "Search for an article containing REGEXP.
7986 Optional argument BACKWARD means do search for backward.
7987 gnus-select-article-hook is not called during the search."
7988   (let ((gnus-select-article-hook nil)  ;Disable hook.
7989         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7990         (re-search
7991          (if backward
7992              (function re-search-backward) (function re-search-forward)))
7993         (found nil)
7994         (last nil))
7995     ;; Hidden thread subtrees must be searched for ,too.
7996     (gnus-summary-show-all-threads)
7997     (if (eobp) (forward-line -1))
7998     ;; First of all, search current article.
7999     ;; We don't want to read article again from NNTP server nor reset
8000     ;; current point.
8001     (gnus-summary-select-article)
8002     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8003     (setq last gnus-current-article)
8004     (gnus-eval-in-buffer-window gnus-article-buffer
8005       (save-restriction
8006         (widen)
8007         ;; Begin search from current point.
8008         (setq found (funcall re-search regexp nil t))))
8009     ;; Then search next articles.
8010     (while (and (not found)
8011                 (gnus-summary-display-article 
8012                  (gnus-summary-search-subject backward nil nil)))
8013       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8014       (gnus-eval-in-buffer-window gnus-article-buffer
8015         (save-restriction
8016           (widen)
8017           (goto-char (if backward (point-max) (point-min)))
8018           (setq found (funcall re-search regexp nil t)))))
8019     (message "")
8020     ;; Adjust article pointer.
8021     (or (eq last gnus-current-article)
8022         (setq gnus-last-article last))
8023     ;; Return T if found such article.
8024     found))
8025
8026 (defun gnus-summary-execute-command (header regexp command &optional backward)
8027   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8028 If HEADER is an empty string (or nil), the match is done on the entire
8029 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8030   (interactive
8031    (list (let ((completion-ignore-case t))
8032            (completing-read 
8033             "Header name: "
8034             (mapcar (lambda (string) (list string))
8035                     '("Number" "Subject" "From" "Lines" "Date"
8036                       "Message-ID" "Xref" "References"))
8037             nil 'require-match))
8038          (read-string "Regexp: ")
8039          (read-key-sequence "Command: ")
8040          current-prefix-arg))
8041   (gnus-set-global-variables)
8042   ;; Hidden thread subtrees must be searched as well.
8043   (gnus-summary-show-all-threads)
8044   ;; We don't want to change current point nor window configuration.
8045   (save-excursion
8046     (save-window-excursion
8047       (gnus-message 6 "Executing %s..." (key-description command))
8048       ;; We'd like to execute COMMAND interactively so as to give arguments.
8049       (gnus-execute header regexp
8050                     (` (lambda ()
8051                          (call-interactively '(, (key-binding command)))))
8052                     backward)
8053       (gnus-message 6 "Executing %s...done" (key-description command)))))
8054
8055 (defun gnus-summary-beginning-of-article ()
8056   "Scroll the article back to the beginning."
8057   (interactive)
8058   (gnus-set-global-variables)
8059   (gnus-summary-select-article)
8060   (gnus-configure-windows 'article)
8061   (gnus-eval-in-buffer-window
8062    gnus-article-buffer
8063    (widen)
8064    (goto-char (point-min))
8065    (and gnus-break-pages (gnus-narrow-to-page))))
8066
8067 (defun gnus-summary-end-of-article ()
8068   "Scroll to the end of the article."
8069   (interactive)
8070   (gnus-set-global-variables)
8071   (gnus-summary-select-article)
8072   (gnus-configure-windows 'article)
8073   (gnus-eval-in-buffer-window 
8074    gnus-article-buffer
8075    (widen)
8076    (goto-char (point-max))
8077    (and gnus-break-pages (gnus-narrow-to-page))))
8078
8079 (defun gnus-summary-show-article ()
8080   "Force re-fetching of the current article."
8081   (interactive)
8082   (gnus-set-global-variables)
8083   (gnus-summary-select-article gnus-have-all-headers t))
8084
8085 (defun gnus-summary-verbose-header (arg)
8086   "Toggle permanent full header display.
8087 If ARG is a positive number, turn header display on.
8088 If ARG is a negative number, turn header display off."
8089   (interactive "P")
8090   (gnus-set-global-variables)
8091   (gnus-summary-toggle-header arg)
8092   (setq gnus-have-all-headers
8093         (cond ((or (not (numberp arg))
8094                    (zerop arg))
8095                (not gnus-have-all-headers))
8096               ((natnump arg)
8097                t))))
8098
8099 (defun gnus-summary-toggle-header (arg)
8100   "Show the headers if they are hidden, or hide them if they are shown.
8101 If ARG is a positive number, show the entire header.
8102 If ARG is a negative number, hide the unwanted header lines."
8103   (interactive "P")
8104   (gnus-set-global-variables)
8105   (save-excursion
8106     (set-buffer gnus-article-buffer)
8107     (let ((buffer-read-only nil))
8108       (if (numberp arg) 
8109           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8110                                                 gnus-hidden-properties)
8111             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8112         (if (text-property-any (point-min) (point-max) 'invisible t)
8113             (remove-text-properties (point-min) (point-max)
8114                                     gnus-hidden-properties)
8115           (let ((gnus-have-all-headers nil))
8116             (run-hooks 'gnus-article-display-hook))))
8117       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8118
8119 (defun gnus-summary-show-all-headers ()
8120   "Make all header lines visible."
8121   (interactive)
8122   (gnus-set-global-variables)
8123   (gnus-article-show-all-headers))
8124
8125 (defun gnus-summary-toggle-mime (arg)
8126   "Toggle MIME processing.
8127 If ARG is a positive number, turn MIME processing on."
8128   (interactive "P")
8129   (gnus-set-global-variables)
8130   (setq gnus-show-mime
8131         (if (null arg) (not gnus-show-mime)
8132           (> (prefix-numeric-value arg) 0)))
8133   (gnus-summary-select-article t 'force))
8134
8135 (defun gnus-summary-caesar-message (rotnum)
8136   "Caesar rotates all letters of current message by 13/47 places.
8137 With prefix arg, specifies the number of places to rotate each letter forward.
8138 Caesar rotates Japanese letters by 47 places in any case."
8139   (interactive "P")
8140   (gnus-set-global-variables)
8141   (gnus-summary-select-article)
8142   (let ((mail-header-separator "")) ; !!! Is this necessary?
8143     (gnus-overload-functions)
8144     (gnus-eval-in-buffer-window 
8145      gnus-article-buffer
8146      (save-restriction
8147        (widen)
8148        ;; We don't want to jump to the beginning of the message.
8149        ;; `save-excursion' does not do its job.
8150        (move-to-window-line 0)
8151        (let ((last (point)))
8152          (news-caesar-buffer-body rotnum)
8153          (goto-char last)
8154          (recenter 0))))))
8155
8156 (defun gnus-summary-stop-page-breaking ()
8157   "Stop page breaking in the current article."
8158   (interactive)
8159   (gnus-set-global-variables)
8160   (gnus-summary-select-article)
8161   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8162
8163 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8164
8165 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8166   "Move the current article to a different newsgroup.
8167 If N is a positive number, move the N next articles.
8168 If N is a negative number, move the N previous articles.
8169 If N is nil and any articles have been marked with the process mark,
8170 move those articles instead.
8171 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8172 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8173 re-spool using this method.
8174 For this function to work, both the current newsgroup and the
8175 newsgroup that you want to move to have to support the `request-move'
8176 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8177   (interactive "P")
8178   (gnus-set-global-variables)
8179   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8180       (error "The current newsgroup does not support article moving"))
8181   (let ((articles (gnus-summary-work-articles n))
8182         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8183         art-group)
8184     (if (and (not to-newsgroup) (not select-method))
8185         (setq to-newsgroup
8186               (completing-read 
8187                (format "Where do you want to move %s? %s"
8188                        (if (> (length articles) 1)
8189                            (format "these %d articles" (length articles))
8190                          "this article")
8191                        (if gnus-current-move-group
8192                            (format "(%s default) " gnus-current-move-group)
8193                          ""))
8194                gnus-active-hashtb nil nil prefix)))
8195     (if to-newsgroup
8196         (progn
8197           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8198               (setq to-newsgroup (or gnus-current-move-group "")))
8199           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8200               (gnus-activate-newsgroup to-newsgroup)
8201               (error "No such group: %s" to-newsgroup))
8202           (setq gnus-current-move-group to-newsgroup)))
8203     (or (gnus-check-backend-function 'request-accept-article 
8204                                      (or select-method to-newsgroup))
8205         (error "%s does not support article moving" to-newsgroup))
8206     (gnus-message 6 "Moving to %s: %s..." 
8207                   (or select-method to-newsgroup) articles)
8208     (while articles
8209       (if (setq art-group
8210                 (gnus-request-move-article 
8211                  (car articles)                   ; Article to move
8212                  gnus-newsgroup-name              ; From newsgrouo
8213                  (nth 1 (gnus-find-method-for-group 
8214                          gnus-newsgroup-name))    ; Server
8215                  (list 'gnus-request-accept-article 
8216                        (if select-method
8217                            (list 'quote select-method)
8218                          to-newsgroup)
8219                        (not (cdr articles)))     ; Accept form
8220                  (not (cdr articles))))          ; Only save nov last time
8221           (let* ((buffer-read-only nil)
8222                  (entry 
8223                   (or
8224                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8225                    (gnus-gethash 
8226                     (gnus-group-prefixed-name 
8227                      (car art-group) 
8228                      (if select-method (list select-method "")
8229                        (gnus-find-method-for-group to-newsgroup)))
8230                     gnus-newsrc-hashtb)))
8231                  (info (nth 2 entry))
8232                  (article (car articles))
8233                  (marked (nth 3 info)))
8234             (gnus-summary-goto-subject article)
8235             (beginning-of-line)
8236             (delete-region (point)
8237                            (progn (forward-line 1) (point)))
8238             (if (not (memq article gnus-newsgroup-unreads))
8239                 (setcar (cdr (cdr info))
8240                         (gnus-add-to-range (nth 2 info) 
8241                                            (list (cdr art-group)))))
8242             ;; Copy any marks over to the new group.
8243             (let ((marks '((tick . gnus-newsgroup-marked)
8244                            (dormant . gnus-newsgroup-dormant)
8245                            (expire . gnus-newsgroup-expirable)
8246                            (bookmark . gnus-newsgroup-bookmarks)
8247                         ;   (score . gnus-newsgroup-scored)
8248                            (reply . gnus-newsgroup-replied)))
8249                   (to-article (cdr art-group)))
8250               (while marks
8251                 (if (memq article (symbol-value (cdr (car marks))))
8252                     (gnus-add-marked-articles 
8253                      (car info) (car (car marks)) (list to-article) info))
8254                 (setq marks (cdr marks))))
8255             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8256             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8257             (setq gnus-newsgroup-dormant
8258                   (delq article gnus-newsgroup-dormant)))
8259         (gnus-message 1 "Couldn't move article %s" (car articles)))
8260       (gnus-summary-remove-process-mark (car articles))
8261       (setq articles (cdr articles)))))
8262
8263 (defun gnus-summary-respool-article (n &optional respool-method)
8264   "Respool the current article.
8265 The article will be squeezed through the mail spooling process again,
8266 which means that it will be put in some mail newsgroup or other
8267 depending on `nnmail-split-methods'.
8268 If N is a positive number, respool the N next articles.
8269 If N is a negative number, respool the N previous articles.
8270 If N is nil and any articles have been marked with the process mark,
8271 respool those articles instead.
8272
8273 Respooling can be done both from mail groups and \"real\" newsgroups.
8274 In the former case, the articles in question will be moved from the
8275 current group into whatever groups they are destined to.  In the
8276 latter case, they will be copied into the relevant groups."
8277   (interactive "P")
8278   (gnus-set-global-variables)
8279   (let ((respool-methods (gnus-methods-using 'respool))
8280         (methname 
8281          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8282     (or respool-method
8283         (setq respool-method
8284               (completing-read
8285                "What method do you want to use when respooling? "
8286                respool-methods nil t methname)))
8287     (or (string= respool-method "")
8288         (if (assoc (symbol-name
8289                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8290                    respool-methods)
8291             (gnus-summary-move-article n nil (intern respool-method))
8292           (gnus-summary-copy-article n nil (intern respool-method))))))
8293
8294 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8295 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8296   "Move the current article to a different newsgroup.
8297 If N is a positive number, move the N next articles.
8298 If N is a negative number, move the N previous articles.
8299 If N is nil and any articles have been marked with the process mark,
8300 move those articles instead.
8301 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8302 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8303 re-spool using this method.
8304 For this function to work, the newsgroup that you want to move to have
8305 to support the `request-move' and `request-accept'
8306 functions. (Ie. mail newsgroups at present.)"
8307   (interactive "P")
8308   (gnus-set-global-variables)
8309   (let ((articles (gnus-summary-work-articles n))
8310         (copy-buf (get-buffer-create "*copy work*"))
8311         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8312         art-group)
8313     (buffer-disable-undo copy-buf)
8314     (if (and (not to-newsgroup) (not select-method))
8315         (setq to-newsgroup
8316               (completing-read 
8317                (format "Where do you want to copy %s? %s"
8318                        (if (> (length articles) 1)
8319                            (format "these %d articles" (length articles))
8320                          "this article")
8321                        (if gnus-current-move-group
8322                            (format "(%s default) " gnus-current-move-group)
8323                          ""))
8324                gnus-active-hashtb nil nil prefix)))
8325     (if to-newsgroup
8326         (progn
8327           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8328               (setq to-newsgroup (or gnus-current-move-group "")))
8329           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8330               (gnus-activate-newsgroup to-newsgroup)
8331               (error "No such group: %s" to-newsgroup))
8332           (setq gnus-current-move-group to-newsgroup)))
8333     (or (gnus-check-backend-function 'request-accept-article 
8334                                      (or select-method to-newsgroup))
8335         (error "%s does not support article copying" to-newsgroup))
8336     (gnus-message 6 "Copying to %s: %s..." 
8337                   (or select-method to-newsgroup) articles)
8338     (while articles
8339       (if (setq art-group
8340                 (save-excursion
8341                   (set-buffer copy-buf)
8342                   (gnus-request-article-this-buffer
8343                    (car articles) gnus-newsgroup-name)
8344                   (gnus-request-accept-article
8345                    (if select-method (quote select-method) to-newsgroup)
8346                    (not (cdr articles)))))
8347           (let* ((entry 
8348                   (or
8349                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8350                    (gnus-gethash 
8351                     (gnus-group-prefixed-name 
8352                      (car art-group) 
8353                      (if select-method (list select-method "")
8354                        (gnus-find-method-for-group to-newsgroup)))
8355                     gnus-newsrc-hashtb)))
8356                  (info (nth 2 entry))
8357                  (article (car articles))
8358                  (marked (nth 3 info)))
8359             (if (not (memq article gnus-newsgroup-unreads))
8360                 (setcar (cdr (cdr info))
8361                         (gnus-add-to-range (nth 2 info) 
8362                                            (list (cdr art-group)))))
8363             ;; Copy any marks over to the new group.
8364             (let ((marks '((tick . gnus-newsgroup-marked)
8365                            (dormant . gnus-newsgroup-dormant)
8366                            (expire . gnus-newsgroup-expirable)
8367                            (bookmark . gnus-newsgroup-bookmarks)
8368                         ;   (score . gnus-newsgroup-scored)
8369                            (reply . gnus-newsgroup-replied)))
8370                   (to-article (cdr art-group)))
8371               (while marks
8372                 (if (memq article (symbol-value (cdr (car marks))))
8373                     (gnus-add-marked-articles 
8374                      (car info) (car (car marks)) (list to-article) info))
8375                 (setq marks (cdr marks)))))
8376         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8377       (gnus-summary-remove-process-mark (car articles))
8378       (setq articles (cdr articles)))
8379     (kill-buffer copy-buf)))
8380
8381 (defun gnus-summary-import-article (file)
8382   "Import a random file into a mail newsgroup."
8383   (interactive "fImport file: ")
8384   (let ((group gnus-newsgroup-name)
8385         atts)
8386     (or (gnus-check-backend-function 'request-accept-article group)
8387         (error "%s does not support article importing" group))
8388     (or (file-readable-p file)
8389         (not (file-regular-p file))
8390         (error "Can't read %s" file))
8391     (save-excursion
8392       (set-buffer (get-buffer-create " *import file*"))
8393       (buffer-disable-undo (current-buffer))
8394       (erase-buffer)
8395       (insert-file-contents file)
8396       (goto-char (point-min))
8397       (if (nnheader-article-p)
8398           ()
8399         (setq atts (file-attributes file))
8400         (insert "From: " (read-string "From: ") "\n"
8401                 "Subject: " (read-string "Subject: ") "\n"
8402                 "Date: " (current-time-string (nth 5 atts)) "\n"
8403                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8404       (gnus-request-accept-article group t)
8405       (kill-buffer (current-buffer)))))
8406
8407 (defun gnus-summary-expire-articles ()
8408   "Expire all articles that are marked as expirable in the current group."
8409   (interactive)
8410   (if (and gnus-newsgroup-expirable
8411            (gnus-check-backend-function 
8412             'request-expire-articles gnus-newsgroup-name))
8413       (let ((expirable gnus-newsgroup-expirable))
8414         ;; The list of articles that weren't expired is returned.
8415         (setq gnus-newsgroup-expirable 
8416               (gnus-request-expire-articles gnus-newsgroup-expirable
8417                                             gnus-newsgroup-name))
8418         ;; We go through the old list of expirable, and mark all
8419         ;; really expired articles as non-existent.
8420         (while expirable
8421           (or (memq (car expirable) gnus-newsgroup-expirable)
8422               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8423           (setq expirable (cdr expirable))))))
8424
8425 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8426 (defun gnus-summary-delete-article (n)
8427   "Delete the N next (mail) articles.
8428 This command actually deletes articles. This is not a marking
8429 command. The article will disappear forever from you life, never to
8430 return. 
8431 If N is negative, delete backwards.
8432 If N is nil and articles have been marked with the process mark,
8433 delete these instead."
8434   (interactive "P")
8435   (or (gnus-check-backend-function 'request-expire-articles 
8436                                    gnus-newsgroup-name)
8437       (error "The current newsgroup does not support article deletion."))
8438   ;; Compute the list of articles to delete.
8439   (let ((articles (gnus-summary-work-articles n))
8440         not-deleted)
8441     (if (and gnus-novice-user
8442              (not (gnus-y-or-n-p 
8443                    (format "Do you really want to delete %s forever? "
8444                            (if (> (length articles) 1) "these articles"
8445                              "this article")))))
8446         ()
8447       ;; Delete the articles.
8448       (setq not-deleted (gnus-request-expire-articles 
8449                          articles gnus-newsgroup-name 'force))
8450       (while articles
8451         (gnus-summary-remove-process-mark (car articles))       
8452         ;; The backend might not have been able to delete the article
8453         ;; after all.  
8454         (or (memq (car articles) not-deleted)
8455             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8456         (setq articles (cdr articles))))
8457     (gnus-summary-position-cursor)
8458     not-deleted))
8459
8460 (defun gnus-summary-edit-article ()
8461   "Enter into a buffer and edit the current article.
8462 This will have permanent effect only in mail groups."
8463   (interactive)
8464   (or (gnus-check-backend-function 
8465        'request-replace-article gnus-newsgroup-name)
8466       (error "The current newsgroup does not support article editing."))
8467   (gnus-summary-select-article t)
8468   (other-window 1)
8469   (gnus-message 6 "C-c C-c to end edits")
8470   (setq buffer-read-only nil)
8471   (text-mode)
8472   (use-local-map (copy-keymap (current-local-map)))
8473   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8474   (buffer-enable-undo)
8475   (goto-char (point-min))
8476   (search-forward "\n\n" nil t))
8477
8478 (defun gnus-summary-edit-article-done ()
8479   "Make edits to the current article permanent."
8480   (interactive)
8481   (if (not (gnus-request-replace-article 
8482             (cdr gnus-article-current) (car gnus-article-current) 
8483             (current-buffer)))
8484       (error "Couldn't replace article.")
8485     (gnus-article-mode)
8486     (use-local-map gnus-article-mode-map)
8487     (setq buffer-read-only t)
8488     (buffer-disable-undo)
8489     (pop-to-buffer gnus-summary-buffer)))      
8490
8491 (defun gnus-summary-fancy-query ()
8492   "Query where the fancy respool algorithm would put this article."
8493   (interactive)
8494   (gnus-summary-select-article)
8495   (save-excursion
8496     (set-buffer gnus-article-buffer)
8497     (save-restriction
8498       (goto-char (point-min))
8499       (search-forward "\n\n")
8500       (narrow-to-region (point-min) (point))
8501       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8502
8503 ;; Summary score commands.
8504
8505 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8506
8507 (defun gnus-summary-raise-score (n)
8508   "Raise the score of the current article by N."
8509   (interactive "p")
8510   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8511
8512 (defun gnus-summary-lower-score (n)
8513   "Lower the score of the current article by N."
8514   (interactive "p")
8515   (gnus-summary-raise-score (- n)))
8516
8517 (defun gnus-summary-set-score (n)
8518   "Set the score of the current article to N."
8519   (interactive "p")
8520   ;; Skip dummy header line.
8521   (save-excursion
8522     (gnus-summary-show-thread)
8523     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8524         (forward-line 1))
8525     (let ((buffer-read-only nil))
8526       ;; Set score.
8527       (gnus-summary-update-mark
8528        (if (= n (or gnus-summary-default-score 0)) ? 
8529          (if (< n (or gnus-summary-default-score 0)) 
8530              gnus-score-below-mark gnus-score-over-mark)) 'score))
8531     (let* ((article (gnus-summary-article-number))
8532            (score (assq article gnus-newsgroup-scored)))
8533       (if score (setcdr score n)
8534         (setq gnus-newsgroup-scored 
8535               (cons (cons article n) gnus-newsgroup-scored))))
8536     (gnus-summary-update-line)))
8537
8538 (defun gnus-summary-current-score ()
8539   "Return the score of the current article."
8540   (interactive)
8541   (message "%s" (gnus-summary-article-score)))
8542
8543 ;; Summary marking commands.
8544
8545 (defun gnus-summary-raise-same-subject-and-select (score)
8546   "Raise articles which has the same subject with SCORE and select the next."
8547   (interactive "p")
8548   (let ((subject (gnus-summary-subject-string)))
8549     (gnus-summary-raise-score score)
8550     (while (gnus-summary-search-subject nil nil subject)
8551       (gnus-summary-raise-score score))
8552     (gnus-summary-next-article t)))
8553
8554 (defun gnus-summary-raise-same-subject (score)
8555   "Raise articles which has the same subject with SCORE."
8556   (interactive "p")
8557   (let ((subject (gnus-summary-subject-string)))
8558     (gnus-summary-raise-score score)
8559     (while (gnus-summary-search-subject nil nil subject)
8560       (gnus-summary-raise-score score))
8561     (gnus-summary-next-subject 1 t)))
8562
8563 (defun gnus-score-default (level)
8564   (if level (prefix-numeric-value level) 
8565     gnus-score-interactive-default-score))
8566
8567 (defun gnus-summary-raise-thread (score)
8568   "Raise the score of the articles in the current thread with SCORE."
8569   (interactive "P")
8570   (setq score (1- (gnus-score-default score)))
8571   (let (e)
8572     (save-excursion
8573       (let ((level (gnus-summary-thread-level)))
8574         (gnus-summary-raise-score score)
8575         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8576                     (> (gnus-summary-thread-level) level))
8577           (gnus-summary-raise-score score))
8578         (setq e (point))))
8579     (let ((gnus-summary-check-current t))
8580       (or (zerop (gnus-summary-next-subject 1 t))
8581           (goto-char e))))
8582   (gnus-summary-recenter)
8583   (gnus-summary-position-cursor)
8584   (gnus-set-mode-line 'summary))
8585
8586 (defun gnus-summary-lower-same-subject-and-select (score)
8587   "Raise articles which has the same subject with SCORE and select the next."
8588   (interactive "p")
8589   (gnus-summary-raise-same-subject-and-select (- score)))
8590
8591 (defun gnus-summary-lower-same-subject (score)
8592   "Raise articles which has the same subject with SCORE."
8593   (interactive "p")
8594   (gnus-summary-raise-same-subject (- score)))
8595
8596 (defun gnus-summary-lower-thread (score)
8597   "Lower score of articles in the current thread with SCORE."
8598   (interactive "P")
8599   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8600
8601 (defun gnus-summary-kill-same-subject-and-select (unmark)
8602   "Mark articles which has the same subject as read, and then select the next.
8603 If UNMARK is positive, remove any kind of mark.
8604 If UNMARK is negative, tick articles."
8605   (interactive "P")
8606   (if unmark
8607       (setq unmark (prefix-numeric-value unmark)))
8608   (let ((count
8609          (gnus-summary-mark-same-subject
8610           (gnus-summary-subject-string) unmark)))
8611     ;; Select next unread article. If auto-select-same mode, should
8612     ;; select the first unread article.
8613     (gnus-summary-next-article t (and gnus-auto-select-same
8614                                       (gnus-summary-subject-string)))
8615     (gnus-message 7 "%d articles are marked as %s"
8616                   count (if unmark "unread" "read"))))
8617
8618 (defun gnus-summary-kill-same-subject (unmark)
8619   "Mark articles which has the same subject as read. 
8620 If UNMARK is positive, remove any kind of mark.
8621 If UNMARK is negative, tick articles."
8622   (interactive "P")
8623   (if unmark
8624       (setq unmark (prefix-numeric-value unmark)))
8625   (let ((count
8626          (gnus-summary-mark-same-subject
8627           (gnus-summary-subject-string) unmark)))
8628     ;; If marked as read, go to next unread subject.
8629     (if (null unmark)
8630         ;; Go to next unread subject.
8631         (gnus-summary-next-subject 1 t))
8632     (gnus-message 7 "%d articles are marked as %s"
8633                   count (if unmark "unread" "read"))))
8634
8635 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8636   "Mark articles with same SUBJECT as read, and return marked number.
8637 If optional argument UNMARK is positive, remove any kinds of marks.
8638 If optional argument UNMARK is negative, mark articles as unread instead."
8639   (let ((count 1))
8640     (save-excursion
8641       (cond ((null unmark)
8642              (gnus-summary-mark-as-read nil gnus-killed-mark))
8643             ((> unmark 0)
8644              (gnus-summary-tick-article nil t))
8645             (t
8646              (gnus-summary-tick-article)))
8647       (while (and subject
8648                   (gnus-summary-search-forward nil subject))
8649         (cond ((null unmark)
8650                (gnus-summary-mark-as-read nil gnus-killed-mark))
8651               ((> unmark 0)
8652                (gnus-summary-tick-article nil t))
8653               (t
8654                (gnus-summary-tick-article)))
8655         (setq count (1+ count))))
8656     ;; Hide killed thread subtrees.  Does not work properly always.
8657     ;;(and (null unmark)
8658     ;;     gnus-thread-hide-killed
8659     ;;     (gnus-summary-hide-thread))
8660     ;; Return number of articles marked as read.
8661     count))
8662
8663 (defun gnus-summary-mark-as-processable (n &optional unmark)
8664   "Set the process mark on the next N articles.
8665 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8666 the process mark instead.  The difference between N and the actual
8667 number of articles marked is returned."
8668   (interactive "p")
8669   (let ((backward (< n 0))
8670         (n (abs n)))
8671   (while (and 
8672           (> n 0)
8673           (if unmark
8674               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8675             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8676           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8677     (setq n (1- n)))
8678   (if (/= 0 n) (gnus-message 7 "No more articles"))
8679   (gnus-summary-recenter)
8680   (gnus-summary-position-cursor)
8681   n))
8682
8683 (defun gnus-summary-unmark-as-processable (n)
8684   "Remove the process mark from the next N articles.
8685 If N is negative, mark backward instead.  The difference between N and
8686 the actual number of articles marked is returned."
8687   (interactive "p")
8688   (gnus-summary-mark-as-processable n t))
8689
8690 (defun gnus-summary-unmark-all-processable ()
8691   "Remove the process mark from all articles."
8692   (interactive)
8693   (save-excursion
8694     (while gnus-newsgroup-processable
8695       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8696   (gnus-summary-position-cursor))
8697
8698 (defun gnus-summary-mark-as-expirable (n)
8699   "Mark N articles forward as expirable.
8700 If N is negative, mark backward instead. The difference between N and
8701 the actual number of articles marked is returned."
8702   (interactive "p")
8703   (gnus-summary-mark-forward n gnus-expirable-mark))
8704
8705 (defun gnus-summary-mark-article-as-replied (article)
8706   "Mark ARTICLE replied and update the summary line."
8707   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8708   (let ((buffer-read-only nil))
8709     (if (gnus-summary-goto-subject article)
8710         (progn
8711           (gnus-summary-update-mark gnus-replied-mark 'replied)
8712           t))))
8713
8714 (defun gnus-summary-set-bookmark (article)
8715   "Set a bookmark in current article."
8716   (interactive (list (gnus-summary-article-number)))
8717   (if (or (not (get-buffer gnus-article-buffer))
8718           (not gnus-current-article)
8719           (not gnus-article-current)
8720           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8721       (error "No current article selected"))
8722   ;; Remove old bookmark, if one exists.
8723   (let ((old (assq article gnus-newsgroup-bookmarks)))
8724     (if old (setq gnus-newsgroup-bookmarks 
8725                   (delq old gnus-newsgroup-bookmarks))))
8726   ;; Set the new bookmark, which is on the form 
8727   ;; (article-number . line-number-in-body).
8728   (setq gnus-newsgroup-bookmarks 
8729         (cons 
8730          (cons article 
8731                (save-excursion
8732                  (set-buffer gnus-article-buffer)
8733                  (count-lines
8734                   (min (point)
8735                        (save-excursion
8736                          (goto-char (point-min))
8737                          (search-forward "\n\n" nil t)
8738                          (point)))
8739                   (point))))
8740          gnus-newsgroup-bookmarks))
8741   (gnus-message 6 "A bookmark has been added to the current article."))
8742
8743 (defun gnus-summary-remove-bookmark (article)
8744   "Remove the bookmark from the current article."
8745   (interactive (list (gnus-summary-article-number)))
8746   ;; Remove old bookmark, if one exists.
8747   (let ((old (assq article gnus-newsgroup-bookmarks)))
8748     (if old 
8749         (progn
8750           (setq gnus-newsgroup-bookmarks 
8751                 (delq old gnus-newsgroup-bookmarks))
8752           (gnus-message 6 "Removed bookmark."))
8753       (gnus-message 6 "No bookmark in current article."))))
8754
8755 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8756 (defun gnus-summary-mark-as-dormant (n)
8757   "Mark N articles forward as dormant.
8758 If N is negative, mark backward instead.  The difference between N and
8759 the actual number of articles marked is returned."
8760   (interactive "p")
8761   (gnus-summary-mark-forward n gnus-dormant-mark))
8762
8763 (defun gnus-summary-set-process-mark (article)
8764   "Set the process mark on ARTICLE and update the summary line."
8765   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8766   (let ((buffer-read-only nil))
8767     (if (gnus-summary-goto-subject article)
8768         (progn
8769           (gnus-summary-show-thread)
8770           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8771                (forward-line 1))
8772           (gnus-summary-update-mark gnus-process-mark 'replied)
8773           t))))
8774
8775 (defun gnus-summary-remove-process-mark (article)
8776   "Remove the process mark from ARTICLE and update the summary line."
8777   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8778   (let ((buffer-read-only nil))
8779     (if (gnus-summary-goto-subject article)
8780         (progn
8781           (gnus-summary-show-thread)
8782           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8783                (forward-line 1))
8784           (gnus-summary-update-mark ?  'replied)
8785           (if (memq article gnus-newsgroup-replied) 
8786               (gnus-summary-update-mark gnus-replied-mark 'replied))
8787           t))))
8788
8789 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8790   "Mark N articles as read forwards.
8791 If N is negative, mark backwards instead.
8792 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8793 marked as unread. 
8794 The difference between N and the actual number of articles marked is
8795 returned."
8796   (interactive "p")
8797   (gnus-set-global-variables)
8798   (let ((backward (< n 0))
8799         (n (abs n))
8800         (mark (or mark gnus-del-mark)))
8801   (while (and (> n 0)
8802               (gnus-summary-mark-article nil mark no-expire)
8803               (zerop (gnus-summary-next-subject 
8804                       (if backward -1 1) gnus-summary-goto-unread t)))
8805     (setq n (1- n)))
8806   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8807   (gnus-summary-recenter)
8808   (gnus-summary-position-cursor)
8809   (gnus-set-mode-line 'summary)
8810   n))
8811
8812 (defun gnus-summary-mark-article (&optional article mark no-expire)
8813   "Mark ARTICLE with MARK.
8814 MARK can be any character.
8815 Five MARK strings are reserved: ?  (unread), 
8816 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8817 If MARK is nil, then the default character ?D is used.
8818 If ARTICLE is nil, then the article on the current line will be
8819 marked." 
8820   ;; If no mark is given, then we check auto-expiring.
8821   (and (not no-expire)
8822        gnus-newsgroup-auto-expire 
8823        (or (not mark)
8824            (and (numberp mark) 
8825                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8826                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8827                     (= mark gnus-read-mark) (= mark gnus-ancient-mark))))
8828        (setq mark gnus-expirable-mark))
8829   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8830          (article (or article (gnus-summary-article-number))))
8831     (or article (error "No article on current line"))
8832     (if (or (= mark gnus-unread-mark) 
8833             (= mark gnus-ticked-mark) 
8834             (= mark gnus-dormant-mark))
8835         (gnus-mark-article-as-unread article mark)
8836       (gnus-mark-article-as-read article mark))
8837
8838     ;; See whether the article is to be put in the cache.
8839     (and gnus-use-cache
8840          (save-excursion
8841            (gnus-cache-possibly-enter-article 
8842             gnus-newsgroup-name article 
8843             (gnus-get-header-by-number article)
8844             (= mark gnus-ticked-mark)
8845             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8846
8847     (if (gnus-summary-goto-subject article)
8848         (let ((buffer-read-only nil))
8849           (gnus-summary-show-thread)
8850           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8851                (forward-line 1))
8852           ;; Fix the mark.
8853           (gnus-summary-update-mark mark 'unread)
8854           t))))
8855
8856 (defun gnus-summary-update-mark (mark type)
8857   (beginning-of-line)
8858   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8859         plist)
8860     (if (not forward)
8861         ()
8862       (forward-char forward)
8863       (setq plist (text-properties-at (point)))
8864       (delete-char 1)
8865       (insert mark)
8866       (and plist (add-text-properties (1- (point)) (point) plist))
8867       (and (eq type 'unread)
8868            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8869       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8870   
8871 (defun gnus-mark-article-as-read (article &optional mark)
8872   "Enter ARTICLE in the pertinent lists and remove it from others."
8873   ;; Make the article expirable.
8874   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8875     (if (= mark gnus-expirable-mark)
8876         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8877       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8878     ;; Remove from unread and marked lists.
8879     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8880     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8881     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8882     ;; Possibly remove from cache, if that is used. 
8883     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
8884
8885 (defun gnus-mark-article-as-unread (article &optional mark)
8886   "Enter ARTICLE in the pertinent lists and remove it from others."
8887   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8888     ;; Add to unread list.
8889     (or (memq article gnus-newsgroup-unreads)
8890         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8891     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8892     ;; lists.  Otherwise, it must be added to the list.
8893     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8894     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8895     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8896     (if (= mark gnus-ticked-mark)
8897         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8898     (if (= mark gnus-dormant-mark)
8899         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8900
8901 (defalias 'gnus-summary-mark-as-unread-forward 
8902   'gnus-summary-tick-article-forward)
8903 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8904                'gnus-summary-tick-article-forward)
8905 (defun gnus-summary-tick-article-forward (n)
8906   "Tick N articles forwards.
8907 If N is negative, tick backwards instead.
8908 The difference between N and the number of articles ticked is returned."
8909   (interactive "p")
8910   (gnus-summary-mark-forward n gnus-ticked-mark))
8911
8912 (defalias 'gnus-summary-mark-as-unread-backward 
8913   'gnus-summary-tick-article-backward)
8914 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8915                'gnus-summary-tick-article-backward)
8916 (defun gnus-summary-tick-article-backward (n)
8917   "Tick N articles backwards.
8918 The difference between N and the number of articles ticked is returned."
8919   (interactive "p")
8920   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8921
8922 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8923 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8924 (defun gnus-summary-tick-article (&optional article clear-mark)
8925   "Mark current article as unread.
8926 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8927 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8928   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8929                                        gnus-ticked-mark)))
8930
8931 (defun gnus-summary-mark-as-read-forward (n)
8932   "Mark N articles as read forwards.
8933 If N is negative, mark backwards instead.
8934 The difference between N and the actual number of articles marked is
8935 returned."
8936   (interactive "p")
8937   (gnus-summary-mark-forward n gnus-del-mark t))
8938
8939 (defun gnus-summary-mark-as-read-backward (n)
8940   "Mark the N articles as read backwards.
8941 The difference between N and the actual number of articles marked is
8942 returned."
8943   (interactive "p")
8944   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8945
8946 (defun gnus-summary-mark-as-read (&optional article mark)
8947   "Mark current article as read.
8948 ARTICLE specifies the article to be marked as read.
8949 MARK specifies a string to be inserted at the beginning of the line."
8950   (gnus-summary-mark-article article mark))
8951
8952 (defun gnus-summary-clear-mark-forward (n)
8953   "Clear marks from N articles forward.
8954 If N is negative, clear backward instead.
8955 The difference between N and the number of marks cleared is returned."
8956   (interactive "p")
8957   (gnus-summary-mark-forward n gnus-unread-mark))
8958
8959 (defun gnus-summary-clear-mark-backward (n)
8960   "Clear marks from N articles backward.
8961 The difference between N and the number of marks cleared is returned."
8962   (interactive "p")
8963   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8964
8965 (defun gnus-summary-mark-unread-as-read ()
8966   "Intended to be used by `gnus-summary-mark-article-hook'."
8967   (or (memq gnus-current-article gnus-newsgroup-marked)
8968       (memq gnus-current-article gnus-newsgroup-dormant)
8969       (memq gnus-current-article gnus-newsgroup-expirable)
8970       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8971
8972 (defun gnus-summary-mark-region-as-read (point mark all)
8973   "Mark all unread articles between point and mark as read.
8974 If given a prefix, mark all articles between point and mark as read,
8975 even ticked and dormant ones."
8976   (interactive "r\nP")
8977   (save-excursion
8978     (goto-char point)
8979     (beginning-of-line)
8980     (while (and 
8981             (< (point) mark)
8982             (progn
8983               (and
8984                (or all
8985                    (and
8986                     (not (memq (gnus-summary-article-number)
8987                                gnus-newsgroup-marked))
8988                     (not (memq (gnus-summary-article-number)
8989                                gnus-newsgroup-dormant))))
8990                (gnus-summary-mark-article
8991                 (gnus-summary-article-number) gnus-del-mark))
8992               t)
8993             (zerop (forward-line 1))))))
8994
8995 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8996 (defalias 'gnus-summary-delete-marked-as-read 
8997   'gnus-summary-remove-lines-marked-as-read)
8998 (make-obsolete 'gnus-summary-delete-marked-as-read 
8999                'gnus-summary-remove-lines-marked-as-read)
9000 (defun gnus-summary-remove-lines-marked-as-read ()
9001   "Remove lines that are marked as read."
9002   (interactive)
9003   (gnus-summary-remove-lines-marked-with 
9004    (concat (mapconcat
9005             (lambda (char) (char-to-string (symbol-value char)))
9006             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9007               gnus-killed-mark gnus-kill-file-mark
9008               gnus-low-score-mark gnus-expirable-mark)
9009             ""))))
9010
9011 (defalias 'gnus-summary-delete-marked-with 
9012   'gnus-summary-remove-lines-marked-with)
9013 (make-obsolete 'gnus-summary-delete-marked-with 
9014                'gnus-summary-remove-lines-marked-with)
9015 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9016 (defun gnus-summary-remove-lines-marked-with (marks)
9017   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9018   (interactive "sMarks: ")
9019   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9020   (gnus-set-global-variables)
9021   (let ((buffer-read-only nil)
9022         (marks (concat "^[" marks "]")))
9023     (goto-char (point-min))
9024     (if gnus-newsgroup-adaptive
9025         (gnus-score-remove-lines-adaptive marks)
9026       (while (re-search-forward marks nil t)
9027         (gnus-delete-line)))
9028     ;; If we use dummy roots, we have to do an additional sweep over
9029     ;; the buffer.
9030     (if (not (eq gnus-summary-make-false-root 'dummy))
9031         ()
9032       (goto-char (point-min))
9033       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9034       (while (re-search-forward marks nil t)
9035         (if (gnus-subject-equal
9036              (gnus-summary-subject-string)
9037              (progn
9038                (forward-line 1)
9039                (gnus-summary-subject-string)))
9040             ()
9041           (forward-line -1)
9042           (gnus-delete-line)))))
9043   (or (zerop (buffer-size))
9044       (if (eobp)
9045           (gnus-summary-prev-subject 1)
9046         (gnus-summary-position-cursor))))
9047
9048 (defun gnus-summary-expunge-below (score)
9049   "Remove articles with score less than SCORE."
9050   (interactive "P")
9051   (gnus-set-global-variables)
9052   (setq score (if score
9053                   (prefix-numeric-value score)
9054                 (or gnus-summary-default-score 0)))
9055   (save-excursion
9056     (set-buffer gnus-summary-buffer)
9057     (goto-char (point-min))
9058     (let ((buffer-read-only nil)
9059           beg)
9060       (while (not (eobp))
9061         (if (< (gnus-summary-article-score) score)
9062             (progn
9063               (setq beg (point))
9064               (forward-line 1)
9065               (delete-region beg (point)))
9066           (forward-line 1)))
9067       ;; Adjust point.
9068       (or (zerop (buffer-size))
9069           (if (eobp)
9070               (gnus-summary-prev-subject 1)
9071             (gnus-summary-position-cursor))))))
9072
9073 (defun gnus-summary-mark-below (score mark)
9074   "Mark articles with score less than SCORE with MARK."
9075   (interactive "P\ncMark: ")
9076   (gnus-set-global-variables)
9077   (setq score (if score
9078                   (prefix-numeric-value score)
9079                 (or gnus-summary-default-score 0)))
9080   (save-excursion
9081     (set-buffer gnus-summary-buffer)
9082     (goto-char (point-min))
9083     (while (not (eobp))
9084       (and (< (gnus-summary-article-score) score)
9085            (gnus-summary-mark-article nil mark))
9086       (forward-line 1))))
9087
9088 (defun gnus-summary-kill-below (score)
9089   "Mark articles with score below SCORE as read."
9090   (interactive "P")
9091   (gnus-summary-mark-below score gnus-killed-mark))
9092
9093 (defun gnus-summary-clear-above (score)
9094   "Clear all marks from articles with score above SCORE."
9095   (interactive "P")
9096   (gnus-summary-mark-above score gnus-unread-mark))
9097
9098 (defun gnus-summary-tick-above (score)
9099   "Tick all articles with score above SCORE."
9100   (interactive "P")
9101   (gnus-summary-mark-above score gnus-ticked-mark))
9102
9103 (defun gnus-summary-mark-above (score mark)
9104   "Mark articles with score over SCORE with MARK."
9105   (interactive "P\ncMark: ")
9106   (setq score (if score
9107                   (prefix-numeric-value score)
9108                 (or gnus-summary-default-score 0)))
9109   (save-excursion
9110     (set-buffer gnus-summary-buffer)
9111     (goto-char (point-min))
9112     (while (not (eobp))
9113       (if (> (gnus-summary-article-score) score)
9114           (progn
9115             (gnus-summary-mark-article nil mark)
9116             (forward-line 1))
9117         (forward-line 1)))))
9118
9119 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9120 (defun gnus-summary-show-all-expunged ()
9121   "Display all the hidden articles that were expunged for low scores."
9122   (interactive)
9123   (let ((buffer-read-only nil))
9124     (let ((scored gnus-newsgroup-scored)
9125           headers h)
9126       (while scored
9127         (or (gnus-summary-goto-subject (car (car scored)))
9128             (and (setq h (gnus-get-header-by-number (car (car scored))))
9129                  (< (cdr (car scored)) gnus-summary-expunge-below)
9130                  (setq headers (cons h headers))))
9131         (setq scored (cdr scored)))
9132       (or headers (error "No expunged articles hidden."))
9133       (goto-char (point-min))
9134       (save-excursion 
9135         (gnus-summary-update-lines 
9136          (point)
9137          (progn
9138            (gnus-summary-prepare-threads (nreverse headers) 0)
9139            (point)))))
9140     (goto-char (point-min))
9141     (gnus-summary-position-cursor)))
9142
9143 (defun gnus-summary-show-all-dormant ()
9144   "Display all the hidden articles that are marked as dormant."
9145   (interactive)
9146   (let ((buffer-read-only nil))
9147     (goto-char (point-min))
9148     (let ((dormant gnus-newsgroup-dormant)
9149           headers h)
9150       (while dormant
9151         (or (gnus-summary-goto-subject (car dormant))
9152             (and (setq h (gnus-get-header-by-number (car dormant)))
9153                  (setq headers (cons h headers))))
9154         (setq dormant (cdr dormant)))
9155       (or headers (error "No dormant articles hidden."))
9156       (save-excursion 
9157         (gnus-summary-update-lines 
9158          (point)
9159          (progn
9160            (gnus-summary-prepare-threads (nreverse headers) 0)
9161            (point)))))
9162     (goto-char (point-min))
9163     (gnus-summary-position-cursor)))
9164
9165 (defun gnus-summary-hide-all-dormant ()
9166   "Hide all dormant articles."
9167   (interactive)
9168   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9169   (gnus-summary-position-cursor))
9170
9171 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9172   "Mark all articles not marked as unread in this newsgroup as read.
9173 If prefix argument ALL is non-nil, all articles are marked as read.
9174 If QUIETLY is non-nil, no questions will be asked.
9175 If TO-HERE is non-nil, it should be a point in the buffer. All
9176 articles before this point will be marked as read.
9177 The number of articles marked as read is returned."
9178   (interactive "P")
9179   (prog1
9180       (if (or quietly
9181               (not gnus-interactive-catchup) ;Without confirmation?
9182               gnus-expert-user
9183               (gnus-y-or-n-p
9184                (if all
9185                    "Mark absolutely all articles as read? "
9186                  "Mark all unread articles as read? ")))
9187           (if (and not-mark 
9188                    (not gnus-newsgroup-adaptive)
9189                    (not gnus-newsgroup-auto-expire))
9190               (progn
9191                 (and all (setq gnus-newsgroup-marked nil
9192                                gnus-newsgroup-dormant nil))
9193                 (setq gnus-newsgroup-unreads 
9194                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9195             ;; We actually mark all articles as canceled, which we
9196             ;; have to do when using auto-expiry or adaptive scoring. 
9197             (let ((unreads (length gnus-newsgroup-unreads)))
9198               (if (gnus-summary-first-subject (not all))
9199                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9200                               (if to-here (< (point) to-here) t)
9201                               (gnus-summary-search-subject nil (not all)))))
9202               (- unreads (length gnus-newsgroup-unreads))
9203               (or to-here
9204                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9205     (gnus-summary-position-cursor)))
9206
9207 (defun gnus-summary-catchup-to-here (&optional all)
9208   "Mark all unticked articles before the current one as read.
9209 If ALL is non-nil, also mark ticked and dormant articles as read."
9210   (interactive)
9211   (beginning-of-line)
9212   (gnus-summary-catchup all t (point))
9213   (gnus-set-mode-line 'summary)
9214   (gnus-summary-position-cursor))
9215
9216 (defun gnus-summary-catchup-all (&optional quietly)
9217   "Mark all articles in this newsgroup as read."
9218   (interactive)
9219   (gnus-summary-catchup t quietly))
9220
9221 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9222   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9223 If prefix argument ALL is non-nil, all articles are marked as read."
9224   (interactive "P")
9225   (gnus-summary-catchup all quietly nil 'fast)
9226   ;; Select next newsgroup or exit.
9227   (if (eq gnus-auto-select-next 'quietly)
9228       (gnus-summary-next-group nil)
9229     (gnus-summary-exit)))
9230
9231 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9232   "Mark all articles in this newsgroup as read, and then exit."
9233   (interactive)
9234   (gnus-summary-catchup-and-exit t quietly))
9235
9236 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9237 (defun gnus-summary-catchup-and-goto-next-group (all)
9238   "Mark all articles in this group as read and select the next group.
9239 If given a prefix, mark all articles, unread as well as ticked, as
9240 read." 
9241   (interactive "P")
9242   (gnus-summary-catchup all)
9243   (gnus-summary-next-group))
9244
9245 ;; Thread-based commands.
9246
9247 (defun gnus-summary-toggle-threads (arg)
9248   "Toggle showing conversation threads.
9249 If ARG is positive number, turn showing conversation threads on."
9250   (interactive "P")
9251   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9252     (setq gnus-show-threads
9253           (if (null arg) (not gnus-show-threads)
9254             (> (prefix-numeric-value arg) 0)))
9255     (gnus-summary-prepare)
9256     (gnus-summary-goto-subject current)
9257     (gnus-summary-position-cursor)))
9258
9259 (defun gnus-summary-show-all-threads ()
9260   "Show all threads."
9261   (interactive)
9262   (save-excursion
9263     (let ((buffer-read-only nil))
9264       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9265   (gnus-summary-position-cursor))
9266
9267 (defun gnus-summary-show-thread ()
9268   "Show thread subtrees.
9269 Returns nil if no thread was there to be shown."
9270   (interactive)
9271   (prog1
9272       (save-excursion
9273         (let ((buffer-read-only nil)
9274               ;; first goto end then to beg, to have point at beg after let
9275               (end (progn (end-of-line) (point)))
9276               (beg (progn (beginning-of-line) (point))))
9277           (prog1
9278               ;; Any hidden lines here?
9279               (search-forward "\r" end t)
9280             (subst-char-in-region beg end ?\^M ?\n t))))
9281     (gnus-summary-position-cursor)))
9282
9283 (defun gnus-summary-hide-all-threads ()
9284   "Hide all thread subtrees."
9285   (interactive)
9286   (save-excursion
9287     (goto-char (point-min))
9288     (gnus-summary-hide-thread)
9289     (while (and (not (eobp)) (zerop (forward-line 1)))
9290       (gnus-summary-hide-thread)))
9291   (gnus-summary-position-cursor))
9292
9293 (defun gnus-summary-hide-thread ()
9294   "Hide thread subtrees.
9295 Returns nil if no threads were there to be hidden."
9296   (interactive)
9297   (let ((buffer-read-only nil)
9298         (start (point))
9299         (level (gnus-summary-thread-level))
9300         (end (point)))
9301     ;; Go forward until either the buffer ends or the subthread
9302     ;; ends. 
9303     (if (eobp)
9304         ()
9305       (while (and (zerop (forward-line 1))
9306                   (> (gnus-summary-thread-level) level))
9307         (setq end (point)))
9308       (prog1
9309           (save-excursion
9310             (goto-char end)
9311             (search-backward "\n" start t))
9312         (subst-char-in-region start end ?\n ?\^M t)
9313         (forward-line -1)))))
9314
9315 (defun gnus-summary-go-to-next-thread (&optional previous)
9316   "Go to the same level (or less) next thread.
9317 If PREVIOUS is non-nil, go to previous thread instead.
9318 Return the article number moved to, or nil if moving was impossible."
9319   (let ((level (gnus-summary-thread-level))
9320         (article (gnus-summary-article-number)))
9321     (if previous 
9322         (while (and (zerop (gnus-summary-prev-subject 1))
9323                     (> (gnus-summary-thread-level) level)))
9324       (while (and (zerop (gnus-summary-next-subject 1))
9325                   (> (gnus-summary-thread-level) level))))
9326     (gnus-summary-recenter)
9327     (gnus-summary-position-cursor)
9328     (let ((oart (gnus-summary-article-number)))
9329       (and (/= oart article) oart))))
9330
9331 (defun gnus-summary-next-thread (n)
9332   "Go to the same level next N'th thread.
9333 If N is negative, search backward instead.
9334 Returns the difference between N and the number of skips actually
9335 done."
9336   (interactive "p")
9337   (let ((backward (< n 0))
9338         (n (abs n)))
9339   (while (and (> n 0)
9340               (gnus-summary-go-to-next-thread backward))
9341     (setq n (1- n)))
9342   (gnus-summary-position-cursor)
9343   (if (/= 0 n) (gnus-message 7 "No more threads"))
9344   n))
9345
9346 (defun gnus-summary-prev-thread (n)
9347   "Go to the same level previous N'th thread.
9348 Returns the difference between N and the number of skips actually
9349 done."
9350   (interactive "p")
9351   (gnus-summary-next-thread (- n)))
9352
9353 (defun gnus-summary-go-down-thread (&optional same)
9354   "Go down one level in the current thread.
9355 If SAME is non-nil, also move to articles of the same level."
9356   (let ((level (gnus-summary-thread-level))
9357         (start (point)))
9358     (if (and (zerop (forward-line 1))
9359              (> (gnus-summary-thread-level) level))
9360         t
9361       (goto-char start)
9362       nil)))
9363
9364 (defun gnus-summary-go-up-thread ()
9365   "Go up one level in the current thread."
9366   (let ((level (gnus-summary-thread-level))
9367         (start (point)))
9368     (while (and (zerop (forward-line -1))
9369                 (>= (gnus-summary-thread-level) level)))
9370     (if (>= (gnus-summary-thread-level) level)
9371         (progn
9372           (goto-char start)
9373           nil)
9374       t)))
9375
9376 (defun gnus-summary-down-thread (n)
9377   "Go down thread N steps.
9378 If N is negative, go up instead.
9379 Returns the difference between N and how many steps down that were
9380 taken."
9381   (interactive "p")
9382   (let ((up (< n 0))
9383         (n (abs n)))
9384   (while (and (> n 0)
9385               (if up (gnus-summary-go-up-thread)
9386                 (gnus-summary-go-down-thread)))
9387     (setq n (1- n)))
9388   (gnus-summary-position-cursor)
9389   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9390   n))
9391
9392 (defun gnus-summary-up-thread (n)
9393   "Go up thread N steps.
9394 If N is negative, go up instead.
9395 Returns the difference between N and how many steps down that were
9396 taken."
9397   (interactive "p")
9398   (gnus-summary-down-thread (- n)))
9399
9400 (defun gnus-summary-kill-thread (unmark)
9401   "Mark articles under current thread as read.
9402 If the prefix argument is positive, remove any kinds of marks.
9403 If the prefix argument is negative, tick articles instead."
9404   (interactive "P")
9405   (if unmark
9406       (setq unmark (prefix-numeric-value unmark)))
9407   (let ((killing t)
9408         (level (gnus-summary-thread-level)))
9409     (save-excursion
9410       (while killing
9411         ;; Mark the article...
9412         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9413               ((> unmark 0) (gnus-summary-tick-article nil t))
9414               (t (gnus-summary-tick-article)))
9415         ;; ...and go forward until either the buffer ends or the subtree
9416         ;; ends. 
9417         (if (not (and (zerop (forward-line 1))
9418                       (> (gnus-summary-thread-level) level)))
9419             (setq killing nil))))
9420     ;; Hide killed subtrees.
9421     (and (null unmark)
9422          gnus-thread-hide-killed
9423          (gnus-summary-hide-thread))
9424     ;; If marked as read, go to next unread subject.
9425     (if (null unmark)
9426         ;; Go to next unread subject.
9427         (gnus-summary-next-subject 1 t)))
9428   (gnus-set-mode-line 'summary))
9429
9430 ;; Summary sorting commands
9431
9432 (defun gnus-summary-sort-by-number (&optional reverse)
9433   "Sort summary buffer by article number.
9434 Argument REVERSE means reverse order."
9435   (interactive "P")
9436   (gnus-summary-sort 
9437    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9438
9439 (defun gnus-summary-sort-by-author (&optional reverse)
9440   "Sort summary buffer by author name alphabetically.
9441 If case-fold-search is non-nil, case of letters is ignored.
9442 Argument REVERSE means reverse order."
9443   (interactive "P")
9444   (gnus-summary-sort
9445    (cons
9446     (lambda ()
9447       (let ((extract (funcall
9448                       gnus-extract-address-components
9449                       (header-from (gnus-get-header-by-number
9450                                     (gnus-summary-article-number))))))
9451         (or (car extract) (cdr extract))))
9452     'gnus-thread-sort-by-author)
9453    reverse))
9454
9455 (defun gnus-summary-sort-by-subject (&optional reverse)
9456   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9457 If case-fold-search is non-nil, case of letters is ignored.
9458 Argument REVERSE means reverse order."
9459   (interactive "P")
9460   (gnus-summary-sort
9461    (cons
9462     (lambda ()
9463       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9464     'gnus-thread-sort-by-subject)
9465    reverse))
9466
9467 (defun gnus-summary-sort-by-date (&optional reverse)
9468   "Sort summary buffer by date.
9469 Argument REVERSE means reverse order."
9470   (interactive "P")
9471   (gnus-summary-sort
9472    (cons
9473     (lambda ()
9474       (gnus-sortable-date
9475        (header-date 
9476         (gnus-get-header-by-number (gnus-summary-article-number)))))
9477     'gnus-thread-sort-by-date)
9478    reverse))
9479
9480 (defun gnus-summary-sort-by-score (&optional reverse)
9481   "Sort summary buffer by score.
9482 Argument REVERSE means reverse order."
9483   (interactive "P")
9484   (gnus-summary-sort 
9485    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9486    (not reverse)))
9487
9488 (defvar gnus-summary-already-sorted nil)
9489 (defun gnus-summary-sort (predicate reverse)
9490   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9491   (if gnus-summary-already-sorted
9492       ()
9493     (let (buffer-read-only)
9494       (if (not gnus-show-threads)
9495           (progn
9496             (goto-char (point-min))
9497             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9498         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9499               (gnus-summary-prepare-hook nil)
9500               (gnus-summary-already-sorted nil))
9501           (gnus-summary-prepare)
9502           (and gnus-show-threads
9503                gnus-thread-hide-subtree
9504                (gnus-summary-hide-all-threads))
9505           ;; If in async mode, we send some info to the backend.
9506           (and gnus-newsgroup-async
9507                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9508                (gnus-request-asynchronous 
9509                 gnus-newsgroup-name
9510                 (if (and gnus-asynchronous-article-function
9511                          (fboundp gnus-asynchronous-article-function))
9512                     (funcall gnus-asynchronous-article-function
9513                              gnus-newsgroup-threads)))))))))
9514
9515   
9516 (defun gnus-sortable-date (date)
9517   "Make sortable string by string-lessp from DATE.
9518 Timezone package is used."
9519   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9520          (year   (aref date 0))
9521          (month  (aref date 1))
9522          (day    (aref date 2)))
9523     (timezone-make-sortable-date 
9524      year month day 
9525      (timezone-make-time-string
9526       (aref date 3) (aref date 4) (aref date 5)))))
9527
9528
9529 ;; Summary saving commands.
9530
9531 (defun gnus-summary-save-article (n)
9532   "Save the current article using the default saver function.
9533 If N is a positive number, save the N next articles.
9534 If N is a negative number, save the N previous articles.
9535 If N is nil and any articles have been marked with the process mark,
9536 save those articles instead.
9537 The variable `gnus-default-article-saver' specifies the saver function."
9538   (interactive "P")
9539   (let ((articles (gnus-summary-work-articles n)))
9540     (while articles
9541       (let ((header (gnus-get-header-by-number (car articles))))
9542         (if (vectorp header)
9543             (progn
9544               (gnus-summary-select-article t nil nil (car articles))
9545               (or gnus-save-all-headers
9546                   (gnus-article-hide-headers t))
9547               ;; Remove any X-Gnus lines.
9548               (save-excursion
9549                 (save-restriction
9550                   (set-buffer gnus-article-buffer)
9551                   (let ((buffer-read-only nil))
9552                     (goto-char (point-min))
9553                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9554                                                   (point-max)))
9555                     (while (re-search-forward "^X-Gnus" nil t)
9556                       (beginning-of-line)
9557                       (delete-region (point)
9558                                      (progn (forward-line 1) (point))))
9559                     (widen))))
9560               (save-excursion
9561                 (if gnus-default-article-saver
9562                     (funcall gnus-default-article-saver)
9563                   (error "No default saver is defined."))))
9564           (if (assq 'name header)
9565               (gnus-copy-file (cdr (assq 'name header)))
9566             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9567       (gnus-summary-remove-process-mark (car articles))
9568       (setq articles (cdr articles)))
9569     (gnus-summary-position-cursor)
9570     n))
9571
9572 (defun gnus-summary-pipe-output (arg)
9573   "Pipe the current article to a subprocess.
9574 If N is a positive number, pipe the N next articles.
9575 If N is a negative number, pipe the N previous articles.
9576 If N is nil and any articles have been marked with the process mark,
9577 pipe those articles instead."
9578   (interactive "P")
9579   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9580     (gnus-summary-save-article arg)))
9581
9582 (defun gnus-summary-save-article-mail (arg)
9583   "Append the current article to an mail file.
9584 If N is a positive number, save the N next articles.
9585 If N is a negative number, save the N previous articles.
9586 If N is nil and any articles have been marked with the process mark,
9587 save those articles instead."
9588   (interactive "P")
9589   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9590     (gnus-summary-save-article arg)))
9591
9592 (defun gnus-summary-save-article-rmail (arg)
9593   "Append the current article to an rmail file.
9594 If N is a positive number, save the N next articles.
9595 If N is a negative number, save the N previous articles.
9596 If N is nil and any articles have been marked with the process mark,
9597 save those articles instead."
9598   (interactive "P")
9599   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9600     (gnus-summary-save-article arg)))
9601
9602 (defun gnus-summary-save-article-file (arg)
9603   "Append the current article to a file.
9604 If N is a positive number, save the N next articles.
9605 If N is a negative number, save the N previous articles.
9606 If N is nil and any articles have been marked with the process mark,
9607 save those articles instead."
9608   (interactive "P")
9609   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9610     (gnus-summary-save-article arg)))
9611
9612 (defun gnus-read-save-file-name (prompt default-name)
9613   (let ((methods gnus-split-methods)
9614         split-name)
9615     (if (not gnus-split-methods)
9616         ()
9617       (save-excursion
9618         (set-buffer gnus-article-buffer)
9619         (gnus-narrow-to-headers)
9620         (while methods
9621           (goto-char (point-min))
9622           (and (condition-case () 
9623                    (re-search-forward (car (car methods)) nil t)
9624                  (error nil))
9625                (setq split-name (cons (nth 1 (car methods)) split-name)))
9626           (setq methods (cdr methods)))
9627         (widen)))
9628     (cond ((null split-name)
9629            (read-file-name
9630             (concat prompt " (default "
9631                     (file-name-nondirectory default-name) ") ")
9632             (file-name-directory default-name)
9633             default-name))
9634           ((= 1 (length split-name))
9635            (read-file-name
9636             (concat prompt " (default " (car split-name) ") ")
9637             gnus-article-save-directory
9638             (concat gnus-article-save-directory (car split-name))))
9639           (t
9640            (setq split-name (mapcar (lambda (el) (list el))
9641                                     (nreverse split-name)))
9642            (let ((result (completing-read 
9643                           (concat prompt " ")
9644                           split-name nil nil)))
9645              (concat gnus-article-save-directory
9646                      (if (string= result "")
9647                          (car (car split-name))
9648                        result)))))))
9649
9650 (defun gnus-summary-save-in-rmail (&optional filename)
9651   "Append this article to Rmail file.
9652 Optional argument FILENAME specifies file name.
9653 Directory to save to is default to `gnus-article-save-directory' which
9654 is initialized from the SAVEDIR environment variable."
9655   (interactive)
9656   (let ((default-name
9657           (funcall gnus-rmail-save-name gnus-newsgroup-name
9658                    gnus-current-headers gnus-newsgroup-last-rmail)))
9659     (or filename
9660         (setq filename (gnus-read-save-file-name 
9661                         "Save in rmail file:" default-name)))
9662     (gnus-make-directory (file-name-directory filename))
9663     (gnus-eval-in-buffer-window 
9664      gnus-article-buffer
9665      (save-excursion
9666        (save-restriction
9667          (widen)
9668          (gnus-output-to-rmail filename))))
9669     ;; Remember the directory name to save articles.
9670     (setq gnus-newsgroup-last-rmail filename)))
9671
9672 (defun gnus-summary-save-in-mail (&optional filename)
9673   "Append this article to Unix mail file.
9674 Optional argument FILENAME specifies file name.
9675 Directory to save to is default to `gnus-article-save-directory' which
9676 is initialized from the SAVEDIR environment variable."
9677   (interactive)
9678   (let ((default-name
9679           (funcall gnus-mail-save-name gnus-newsgroup-name
9680                    gnus-current-headers gnus-newsgroup-last-mail)))
9681     (or filename
9682         (setq filename (gnus-read-save-file-name 
9683                         "Save in Unix mail file:" default-name)))
9684     (setq filename
9685           (expand-file-name filename
9686                             (and default-name
9687                                  (file-name-directory default-name))))
9688     (gnus-make-directory (file-name-directory filename))
9689     (gnus-eval-in-buffer-window 
9690      gnus-article-buffer
9691      (save-excursion
9692        (save-restriction
9693          (widen)
9694          (if (and (file-readable-p filename) (rmail-file-p filename))
9695              (gnus-output-to-rmail filename)
9696            (rmail-output filename 1 t t)))))
9697     ;; Remember the directory name to save articles.
9698     (setq gnus-newsgroup-last-mail filename)))
9699
9700 (defun gnus-summary-save-in-file (&optional filename)
9701   "Append this article to file.
9702 Optional argument FILENAME specifies file name.
9703 Directory to save to is default to `gnus-article-save-directory' which
9704 is initialized from the SAVEDIR environment variable."
9705   (interactive)
9706   (let ((default-name
9707           (funcall gnus-file-save-name gnus-newsgroup-name
9708                    gnus-current-headers gnus-newsgroup-last-file)))
9709     (or filename
9710         (setq filename (gnus-read-save-file-name 
9711                         "Save in file:" default-name)))
9712     (gnus-make-directory (file-name-directory filename))
9713     (gnus-eval-in-buffer-window 
9714      gnus-article-buffer
9715      (save-excursion
9716        (save-restriction
9717          (widen)
9718          (gnus-output-to-file filename))))
9719     ;; Remember the directory name to save articles.
9720     (setq gnus-newsgroup-last-file filename)))
9721
9722 (defun gnus-summary-save-in-pipe (&optional command)
9723   "Pipe this article to subprocess."
9724   (interactive)
9725   (let ((command (read-string "Shell command on article: "
9726                               gnus-last-shell-command)))
9727     (if (string-equal command "")
9728         (setq command gnus-last-shell-command))
9729     (gnus-eval-in-buffer-window 
9730      gnus-article-buffer
9731      (save-restriction
9732        (widen)
9733        (shell-command-on-region (point-min) (point-max) command nil)))
9734     (setq gnus-last-shell-command command)))
9735
9736 ;; Summary extract commands
9737
9738 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9739   (let ((buffer-read-only nil)
9740         (article (gnus-summary-article-number))
9741         b)
9742     (or (gnus-summary-goto-subject article)
9743         (error (format "No such article: %d" article)))
9744     (or gnus-newsgroup-headers-hashtb-by-number
9745         (gnus-make-headers-hashtable-by-number))
9746     (gnus-summary-position-cursor)
9747     ;; If all commands are to be bunched up on one line, we collect
9748     ;; them here.  
9749     (if gnus-view-pseudos-separately
9750         ()
9751       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9752             files action)
9753         (while ps
9754           (setq action (cdr (assq 'action (car ps))))
9755           (setq files (list (cdr (assq 'name (car ps)))))
9756           (while (and ps (cdr ps)
9757                       (string= (or action "1")
9758                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9759             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9760             (setcdr ps (cdr (cdr ps))))
9761           (if (not files)
9762               ()
9763             (if (not (string-match "%s" action))
9764                 (setq files (cons " " files)))
9765             (setq files (cons " " files))
9766             (and (assq 'execute (car ps))
9767                  (setcdr (assq 'execute (car ps))
9768                          (funcall (if (string-match "%s" action)
9769                                       'format 'concat)
9770                                   action 
9771                                   (mapconcat (lambda (f) f) files " ")))))
9772           (setq ps (cdr ps)))))
9773     (if (and gnus-view-pseudos (not not-view))
9774         (while pslist
9775           (and (assq 'execute (car pslist))
9776                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9777                                      (eq gnus-view-pseudos 'not-confirm)))
9778           (setq pslist (cdr pslist)))
9779       (save-excursion
9780         (while pslist
9781           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9782                                          (gnus-summary-article-number)))
9783           (forward-line 1)
9784           (setq b (point))
9785           (insert "          " (file-name-nondirectory 
9786                                 (cdr (assq 'name (car pslist))))
9787                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9788           (add-text-properties 
9789            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9790                           'gnus-mark gnus-unread-mark 
9791                           'gnus-level 0
9792                           'gnus-pseudo (car pslist)))
9793           (forward-line -1)
9794           (gnus-sethash (int-to-string gnus-reffed-article-number)
9795                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9796           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9797           (setq pslist (cdr pslist)))))))
9798
9799 (defun gnus-pseudos< (p1 p2)
9800   (let ((c1 (cdr (assq 'action p1)))
9801         (c2 (cdr (assq 'action p2))))
9802     (and c1 c2 (string< c1 c2))))
9803
9804 (defun gnus-request-pseudo-article (props)
9805   (cond ((assq 'execute props)
9806          (gnus-execute-command (cdr (assq 'execute props)))))
9807   (let ((gnus-current-article (gnus-summary-article-number)))
9808     (run-hooks 'gnus-mark-article-hook)))
9809
9810 (defun gnus-execute-command (command &optional automatic)
9811   (save-excursion
9812     (gnus-article-setup-buffer)
9813     (set-buffer gnus-article-buffer)
9814     (let ((command (if automatic command (read-string "Command: " command)))
9815           (buffer-read-only nil))
9816       (erase-buffer)
9817       (insert "$ " command "\n\n")
9818       (if gnus-view-pseudo-asynchronously
9819           (start-process "gnus-execute" nil "sh" "-c" command)
9820         (call-process "sh" nil t nil "-c" command)))))
9821
9822 (defun gnus-copy-file (file &optional to)
9823   "Copy FILE to TO."
9824   (interactive
9825    (list (read-file-name "Copy file: " default-directory)
9826          (read-file-name "Copy file to: " default-directory)))
9827   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9828   (and (file-directory-p to) 
9829        (setq to (concat (file-name-as-directory to)
9830                         (file-name-nondirectory file))))
9831   (copy-file file to))
9832
9833 ;; Summary kill commands.
9834
9835 (defun gnus-summary-edit-global-kill (article)
9836   "Edit the \"global\" kill file."
9837   (interactive (list (gnus-summary-article-number)))
9838   (gnus-group-edit-global-kill article))
9839
9840 (defun gnus-summary-edit-local-kill ()
9841   "Edit a local kill file applied to the current newsgroup."
9842   (interactive)
9843   (setq gnus-current-headers 
9844         (gnus-gethash 
9845          (int-to-string (gnus-summary-article-number))
9846          gnus-newsgroup-headers-hashtb-by-number))
9847   (gnus-set-global-variables)
9848   (gnus-group-edit-local-kill 
9849    (gnus-summary-article-number) gnus-newsgroup-name))
9850
9851 \f
9852 ;;;
9853 ;;; Gnus article mode
9854 ;;;
9855
9856 (put 'gnus-article-mode 'mode-class 'special)
9857
9858 (if gnus-article-mode-map
9859     nil
9860   (setq gnus-article-mode-map (make-keymap))
9861   (suppress-keymap gnus-article-mode-map)
9862   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9863   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9864   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9865   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9866   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9867   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9868   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9869   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9870   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9871   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9872   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9873   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
9874   
9875   ;; Duplicate almost all summary keystrokes in the article mode map.
9876   (let ((commands 
9877          (list 
9878           " " "\177" "\r" "n" "p" "N" "P" "\M-\C-n" "\M-\C-p"
9879           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
9880           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
9881           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
9882           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
9883           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
9884           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
9885           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
9886           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
9887           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
9888           "\C-c\C-v\C-v" "\C-d" "v" "\C-c\C-b" 
9889 ;;        "Mt" "M!" "Md" "Mr"
9890 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
9891 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
9892 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
9893 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
9894 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
9895 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
9896 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
9897 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
9898 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
9899 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
9900 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
9901           )))
9902     (while commands
9903       (define-key gnus-article-mode-map (car commands) 
9904         'gnus-article-summary-command)
9905       (setq commands (cdr commands))))
9906
9907   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
9908 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
9909                          "=")))
9910     (while commands
9911       (define-key gnus-article-mode-map (car commands) 
9912         'gnus-article-summary-command-nosave)
9913       (setq commands (cdr commands)))))
9914
9915
9916 (defun gnus-article-mode ()
9917   "Major mode for displaying an article.
9918
9919 All normal editing commands are switched off.
9920
9921 The following commands are available:
9922
9923 \\<gnus-article-mode-map>
9924 \\[gnus-article-next-page]\t Scroll the article one page forwards
9925 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9926 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9927 \\[gnus-article-show-summary]\t Display the summary buffer
9928 \\[gnus-article-mail]\t Send a reply to the address near point
9929 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9930 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9931 \\[gnus-info-find-node]\t Go to the Gnus info node"
9932   (interactive)
9933   (if gnus-visual (gnus-article-make-menu-bar))
9934   (kill-all-local-variables)
9935   (setq mode-line-modified "-- ")
9936   (make-local-variable 'mode-line-format)
9937   (setq mode-line-format (copy-sequence mode-line-format))
9938   (and (equal (nth 3 mode-line-format) "   ")
9939        (setcar (nthcdr 3 mode-line-format) ""))
9940   (setq mode-name "Article")
9941   (setq major-mode 'gnus-article-mode)
9942   (make-local-variable 'minor-mode-alist)
9943   (or (assq 'gnus-show-mime minor-mode-alist)
9944       (setq minor-mode-alist
9945             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9946   (use-local-map gnus-article-mode-map)
9947   (make-local-variable 'page-delimiter)
9948   (setq page-delimiter gnus-page-delimiter)
9949   (buffer-disable-undo (current-buffer))
9950   (setq buffer-read-only t)             ;Disable modification
9951   (run-hooks 'gnus-article-mode-hook))
9952
9953 (defun gnus-article-setup-buffer ()
9954   "Initialize article mode buffer."
9955   (if (get-buffer gnus-article-buffer)
9956       (save-excursion
9957         (set-buffer gnus-article-buffer)
9958         (buffer-disable-undo)
9959         (setq buffer-read-only t)
9960         (gnus-add-current-to-buffer-list)
9961         (or (eq major-mode 'gnus-article-mode)
9962             (gnus-article-mode)))
9963     (save-excursion
9964       (set-buffer (get-buffer-create gnus-article-buffer))
9965       (gnus-add-current-to-buffer-list)
9966       (gnus-article-mode))))
9967
9968 ;; Set article window start at LINE, where LINE is the number of lines
9969 ;; from the head of the article.
9970 (defun gnus-article-set-window-start (&optional line)
9971   (set-window-start 
9972    (get-buffer-window gnus-article-buffer)
9973    (save-excursion
9974      (set-buffer gnus-article-buffer)
9975      (goto-char (point-min))
9976      (if (not line)
9977          (point-min)
9978        (gnus-message 6 "Moved to bookmark")
9979        (search-forward "\n\n" nil t)
9980        (forward-line line)
9981        (point)))))
9982
9983 (defun gnus-request-article-this-buffer (article group)
9984   "Get an article and insert it into this buffer."
9985   (setq group (or group gnus-newsgroup-name))
9986
9987   ;; Open server if it has closed.
9988   (gnus-check-news-server (gnus-find-method-for-group group))
9989
9990   ;; Using `gnus-request-article' directly will insert the article into
9991   ;; `nntp-server-buffer' - so we'll save some time by not having to
9992   ;; copy it from the server buffer into the article buffer.
9993
9994   ;; We only request an article by message-id when we do not have the
9995   ;; headers for it, so we'll have to get those.
9996   (and (stringp article) 
9997        (let ((gnus-override-method gnus-refer-article-method))
9998          (gnus-read-header article)))
9999
10000   ;; If the article number is negative, that means that this article
10001   ;; doesn't belong in this newsgroup (possibly), so we find its
10002   ;; message-id and request it by id instead of number.
10003   (if (not (numberp article))
10004       ()
10005     (save-excursion
10006       (set-buffer gnus-summary-buffer)
10007       (let ((header (gnus-get-header-by-number article)))
10008         (if (< article 0)
10009             (if (vectorp header)
10010                 ;; It's a real article.
10011                 (setq article (header-id header))
10012               ;; It is an extracted pseudo-article.
10013               (setq article 'pseudo)
10014               (gnus-request-pseudo-article header)))
10015
10016         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10017           (if (not (eq (car method) 'nneething))
10018               ()
10019             (let ((dir (concat (file-name-as-directory (nth 1 method))
10020                                (header-subject header))))
10021               (if (file-directory-p dir)
10022                   (progn
10023                     (setq article 'nneething)
10024                     (gnus-group-enter-directory dir)))))))))
10025
10026   ;; Check the cache.
10027   (if (and gnus-use-cache
10028            (numberp article)
10029            (gnus-cache-request-article article group))
10030       'article
10031     ;; Get the article and into the article buffer.
10032     (if (or (stringp article) (numberp article))
10033         (progn
10034           (erase-buffer)
10035           (let ((gnus-override-method 
10036                  (and (stringp article) gnus-refer-article-method)))
10037             (and (gnus-request-article article group (current-buffer))
10038                  'article)))
10039       article)))
10040
10041 (defun gnus-read-header (id)
10042   "Read the headers of article ID and enter them into the Gnus system."
10043   (or gnus-newsgroup-headers-hashtb-by-number
10044       (gnus-make-headers-hashtable-by-number))
10045   (let (header)
10046     (if (not (setq header 
10047                    (car (if (let ((gnus-nov-is-evil t))
10048                               (gnus-retrieve-headers 
10049                                (list id) gnus-newsgroup-name))
10050                             (gnus-get-newsgroup-headers)))))
10051         nil
10052       (if (stringp id)
10053           (header-set-number header gnus-reffed-article-number))
10054       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10055       (gnus-sethash (int-to-string (header-number header)) header
10056                     gnus-newsgroup-headers-hashtb-by-number)
10057       (if (stringp id)
10058           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10059       (setq gnus-current-headers header)
10060       header)))
10061
10062 (defun gnus-article-prepare (article &optional all-headers header)
10063   "Prepare ARTICLE in article mode buffer.
10064 ARTICLE should either be an article number or a Message-ID.
10065 If ARTICLE is an id, HEADER should be the article headers.
10066 If ALL-HEADERS is non-nil, no headers are hidden."
10067   (save-excursion
10068     ;; Make sure we start in a summary buffer.
10069     (or (eq major-mode 'gnus-summary-mode)
10070         (set-buffer gnus-summary-buffer))
10071     (setq gnus-summary-buffer (current-buffer))
10072     ;; Make sure the connection to the server is alive.
10073     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10074         (progn
10075           (gnus-check-news-server 
10076            (gnus-find-method-for-group gnus-newsgroup-name))
10077           (gnus-request-group gnus-newsgroup-name t)))
10078     (or gnus-newsgroup-headers-hashtb-by-number
10079         (gnus-make-headers-hashtable-by-number))
10080     (let* ((article (if header (header-number header) article))
10081            (summary-buffer (current-buffer))
10082            (internal-hook gnus-article-internal-prepare-hook)
10083            (group gnus-newsgroup-name)
10084            result)
10085       (save-excursion
10086         (gnus-article-setup-buffer)
10087         (set-buffer gnus-article-buffer)
10088         (if (not (setq result (let ((buffer-read-only nil))
10089                                 (gnus-request-article-this-buffer 
10090                                  article group))))
10091             ;; There is no such article.
10092             (save-excursion
10093               (if (not (numberp article))
10094                   ()
10095                 (setq gnus-article-current 
10096                       (cons gnus-newsgroup-name article))
10097                 (set-buffer gnus-summary-buffer)
10098                 (setq gnus-current-article article)
10099                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10100               (gnus-message 1 "No such article (may be canceled)")
10101               (ding)
10102               nil)
10103           (if (or (eq result 'pseudo) (eq result 'nneething))
10104               (progn
10105                 (save-excursion
10106                   (set-buffer summary-buffer)
10107                   (setq gnus-last-article gnus-current-article
10108                         gnus-newsgroup-history (cons gnus-current-article
10109                                                      gnus-newsgroup-history)
10110                         gnus-current-article 0
10111                         gnus-current-headers nil
10112                         gnus-article-current nil)
10113                   (if (eq result 'nneething)
10114                       (gnus-configure-windows 'summary)
10115                     (gnus-configure-windows 'article))
10116                   (gnus-set-global-variables))
10117                 (gnus-set-mode-line 'article))
10118             ;; The result from the `request' was an actual article -
10119             ;; or at least some text that is now displayed in the
10120             ;; article buffer.
10121             (if (and (numberp article)
10122                      (not (eq article gnus-current-article)))
10123                 ;; Seems like a new article has been selected.
10124                 ;; `gnus-current-article' must be an article number.
10125                 (save-excursion
10126                   (set-buffer summary-buffer)
10127                   (setq gnus-last-article gnus-current-article
10128                         gnus-newsgroup-history (cons gnus-current-article
10129                                                      gnus-newsgroup-history)
10130                         gnus-current-article article
10131                         gnus-current-headers 
10132                         (gnus-get-header-by-number gnus-current-article)
10133                         gnus-article-current 
10134                         (cons gnus-newsgroup-name gnus-current-article))
10135                   (gnus-summary-show-thread)
10136                   (run-hooks 'gnus-mark-article-hook)
10137                   (gnus-set-mode-line 'summary)
10138                   (and gnus-visual 
10139                        (run-hooks 'gnus-visual-mark-article-hook))
10140                   ;; Set the global newsgroup variables here.
10141                   ;; Suggested by Jim Sisolak
10142                   ;; <sisolak@trans4.neep.wisc.edu>.
10143                   (gnus-set-global-variables)
10144                   (and gnus-use-cache 
10145                        (gnus-cache-possibly-enter-article
10146                         group article
10147                         (gnus-get-header-by-number article)
10148                         (memq article gnus-newsgroup-marked)
10149                         (memq article gnus-newsgroup-dormant)
10150                         (memq article gnus-newsgroup-unreads)))))
10151             ;; gnus-have-all-headers must be either T or NIL.
10152             (setq gnus-have-all-headers
10153                   (not (not (or all-headers gnus-show-all-headers))))
10154             ;; Hooks for getting information from the article.
10155             ;; This hook must be called before being narrowed.
10156             (let (buffer-read-only)
10157               (run-hooks 'internal-hook)
10158               (run-hooks 'gnus-article-prepare-hook)
10159               ;; Decode MIME message.
10160               (if (and gnus-show-mime
10161                        (or (not gnus-strict-mime)
10162                            (gnus-fetch-field "Mime-Version")))
10163                   (funcall gnus-show-mime-method))
10164               ;; Perform the article display hooks.
10165               (run-hooks 'gnus-article-display-hook))
10166             ;; Do page break.
10167             (goto-char (point-min))
10168             (and gnus-break-pages (gnus-narrow-to-page))
10169             (gnus-set-mode-line 'article)
10170             (gnus-configure-windows 'article)
10171             (goto-char (point-min))
10172             t))))))
10173
10174 (defun gnus-article-show-all-headers ()
10175   "Show all article headers in article mode buffer."
10176   (save-excursion 
10177     (gnus-article-setup-buffer)
10178     (set-buffer gnus-article-buffer)
10179     (let ((buffer-read-only nil))
10180       (remove-text-properties (point-min) (point-max) 
10181                               gnus-hidden-properties))))
10182
10183 (defun gnus-article-hide-headers-if-wanted ()
10184   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10185 Provided for backwards compatability."
10186   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10187       (gnus-article-hide-headers)))
10188
10189 (defun gnus-article-hide-headers (&optional delete)
10190   "Hide unwanted headers and possibly sort them as well."
10191   (interactive "P")
10192   (save-excursion
10193     (set-buffer gnus-article-buffer)
10194     (save-restriction
10195       (let ((sorted gnus-sorted-header-list)
10196             (buffer-read-only nil)
10197             want want-list beg want-l)
10198         ;; First we narrow to just the headers.
10199         (widen)
10200         (goto-char (point-min))
10201         ;; Hide any "From " lines at the beginning of (mail) articles. 
10202         (while (looking-at "From ")
10203           (forward-line 1))
10204         (if (bobp) 
10205             (add-text-properties (point-min) (point) gnus-hidden-properties))
10206         ;; Then treat the rest of the header lines.
10207         (narrow-to-region 
10208          (point) 
10209          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10210         ;; Then we use the two regular expressions
10211         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10212         ;; select which header lines is to remain visible in the
10213         ;; article buffer.
10214         (goto-char (point-min))
10215         (while (re-search-forward "^[^ \t]*:" nil t)
10216           (beginning-of-line)
10217           ;; We add the headers we want to keep to a list and delete
10218           ;; them from the buffer.
10219           (if (or (and (stringp gnus-visible-headers)
10220                        (looking-at gnus-visible-headers))
10221                   (and (not (stringp gnus-visible-headers))
10222                        (stringp gnus-ignored-headers)
10223                        (not (looking-at gnus-ignored-headers))))
10224               (progn
10225                 (setq beg (point))
10226                 (forward-line 1)
10227                 ;; Be sure to get multi-line headers...
10228                 (re-search-forward "^[^ \t]*:" nil t)
10229                 (beginning-of-line)
10230                 (setq want-list 
10231                       (cons (buffer-substring beg (point)) want-list))
10232                 (delete-region beg (point))
10233                 (goto-char beg))
10234             (forward-line 1)))
10235         ;; Next we perform the sorting by looking at
10236         ;; `gnus-sorted-header-list'. 
10237         (goto-char (point-min))
10238         (while (and sorted want-list)
10239           (setq want-l want-list)
10240           (while (and want-l
10241                       (not (string-match (car sorted) (car want-l))))
10242             (setq want-l (cdr want-l)))
10243           (if want-l 
10244               (progn
10245                 (insert (car want-l))
10246                 (setq want-list (delq (car want-l) want-list))))
10247           (setq sorted (cdr sorted)))
10248         ;; Any headers that were not matched by the sorted list we
10249         ;; just tack on the end of the visible header list.
10250         (while want-list
10251           (insert (car want-list))
10252           (setq want-list (cdr want-list)))
10253         ;; And finally we make the unwanted headers invisible.
10254         (if delete
10255             (delete-region (point) (point-max))
10256           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10257           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10258
10259 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10260 (defun gnus-article-treat-overstrike ()
10261   "Translate overstrikes into bold text."
10262   (interactive)
10263   (save-excursion
10264     (set-buffer gnus-article-buffer)
10265     (let ((buffer-read-only nil))
10266       (while (search-forward "\b" nil t)
10267         (let ((next (following-char))
10268               (previous (char-after (- (point) 2))))
10269           (cond ((eq next previous)
10270                  (delete-region (- (point) 2) (point))
10271                  (put-text-property (point) (1+ (point))
10272                                     'face 'bold))
10273                 ((eq next ?_)
10274                  (delete-region (1- (point)) (1+ (point)))
10275                  (put-text-property (1- (point)) (point)
10276                                     'face 'underline))
10277                 ((eq previous ?_)
10278                  (delete-region (- (point) 2) (point))
10279                  (put-text-property (point) (1+ (point))
10280                                     'face 'underline))))))))
10281
10282 (defun gnus-article-word-wrap ()
10283   "Format too long lines."
10284   (interactive)
10285   (save-excursion
10286     (set-buffer gnus-article-buffer)
10287     (let ((buffer-read-only nil))
10288       (goto-char (point-min))
10289       (search-forward "\n\n" nil t)
10290       (end-of-line 1)
10291       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10292             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10293             (adaptive-fill-mode t))
10294         (while (not (eobp))
10295           (and (>= (current-column) (min fill-column (window-width)))
10296                (/= (preceding-char) ?:)
10297                (fill-paragraph nil))
10298           (end-of-line 2))))))
10299
10300 (defun gnus-article-remove-cr ()
10301   "Remove carriage returns from an article."
10302   (interactive)
10303   (save-excursion
10304     (set-buffer gnus-article-buffer)
10305     (let ((buffer-read-only nil))
10306       (goto-char (point-min))
10307       (while (search-forward "\r" nil t)
10308         (replace-match "" t t)))))
10309
10310 (defun gnus-article-display-x-face (&optional force)
10311   "Look for an X-Face header and display it if present."
10312   (interactive (list 'force))
10313   (save-excursion
10314     (set-buffer gnus-article-buffer)
10315     (let ((inhibit-point-motion-hooks t)
10316           (case-fold-search nil))
10317       (save-restriction
10318         (goto-char (point-min))
10319         (search-forward "\n\n")
10320         (narrow-to-region (point-min) (point))
10321         (goto-char (point-min))
10322         (if (or (not gnus-article-x-face-command)
10323                 (and (not force)
10324                      (or (not gnus-article-x-face-too-ugly)
10325                          (string-match gnus-article-x-face-too-ugly
10326                                        (mail-fetch-field "from"))))
10327                 (progn
10328                   (goto-char (point-min))
10329                   (not (re-search-forward "^X-Face: " nil t))))
10330             nil
10331           (let ((beg (point))
10332                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10333             (if (symbolp gnus-article-x-face-command)
10334                 (and (or (fboundp gnus-article-x-face-command)
10335                          (error "%s is not a function"
10336                                 gnus-article-x-face-command))
10337                      (funcall gnus-article-x-face-command beg end))
10338               (call-process-region beg end "sh" nil 0 nil
10339                                    "-c" gnus-article-x-face-command))))))))
10340
10341 (defun gnus-article-de-quoted-unreadable (&optional force)
10342   "Do a naïve translation of a quoted-printable-encoded article.
10343 This is in no way, shape or form meant as a replacement for real MIME
10344 processing, but is simply a stop-gap measure until MIME support is
10345 written.
10346 If FORCE, decode the article whether it is marked as quoted-printable
10347 or not." 
10348   (interactive (list 'force))
10349   (save-excursion
10350     (set-buffer gnus-article-buffer)
10351     (let ((case-fold-search t)
10352           (buffer-read-only nil)
10353           (type (gnus-fetch-field "content-transfer-encoding")))
10354       (if (or force (and type (string-match "quoted-printable" type)))
10355           (progn
10356             (goto-char (point-min))
10357             (search-forward "\n\n" nil 'move)
10358             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10359
10360 (defun gnus-mime-decode-quoted-printable (from to)
10361   ;; Decode quoted-printable from region between FROM and TO.
10362   (save-excursion
10363     (goto-char from)
10364     (while (search-forward "=" to t)
10365       (cond ((eq (following-char) ?\n)
10366              (delete-char -1)
10367              (delete-char 1))
10368             ((looking-at "[0-9A-F][0-9A-F]")
10369              (delete-char -1)
10370              (insert (hexl-hex-string-to-integer
10371                       (buffer-substring (point) (+ 2 (point)))))
10372              (delete-char 2))
10373             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10374
10375 (defvar gnus-article-time-units
10376   (list (cons 'year (* 365.25 24 60 60))
10377         (cons 'week (* 7 24 60 60))
10378         (cons 'day (* 24 60 60))
10379         (cons 'hour (* 60 60))
10380         (cons 'minute 60)
10381         (cons 'second 1)))
10382
10383 (defun gnus-article-date-ut (&optional type)
10384   "Convert DATE date to universal time in the current article.
10385 If TYPE is `local', convert to local time; if it is `lapsed', output
10386 how much time has lapsed since DATE."
10387   (interactive (list 'ut))
10388   (let ((date (header-date (or gnus-current-headers 
10389                                (gnus-get-header-by-number
10390                                 (gnus-summary-article-number))"")))
10391         (date-regexp "^Date: \\|^X-Sent: "))
10392     (if (or (not date)
10393             (string= date ""))
10394         ()
10395       (save-excursion
10396         (set-buffer gnus-article-buffer)
10397         (let ((buffer-read-only nil))
10398           (goto-char (point-min))
10399           (if (and (re-search-forward date-regexp nil t)
10400                    (progn 
10401                      (beginning-of-line)
10402                      (looking-at date-regexp)))
10403               (delete-region (gnus-point-at-bol)
10404                              (progn (end-of-line) (1+ (point))))
10405             (goto-char (point-min))
10406             (goto-char (- (search-forward "\n\n") 2)))
10407           (insert
10408            (cond 
10409             ((eq type 'local)
10410              (concat "Date: " (condition-case ()
10411                                   (timezone-make-date-arpa-standard date)
10412                                 (error date))
10413                      "\n"))
10414             ((eq type 'ut)
10415              (concat "Date: "
10416                      (condition-case ()
10417                          (timezone-make-date-arpa-standard date nil "UT")
10418                        (error date))
10419                      "\n"))
10420             ((eq type 'lapsed)
10421              ;; If the date is seriously mangled, the timezone
10422              ;; functions are liable to bug out, so we condition-case
10423              ;; the entire thing.  
10424              (let* ((sec (condition-case ()
10425                              (max (- (gnus-seconds-since-epoch 
10426                                       (timezone-make-date-arpa-standard
10427                                        (current-time-string) 
10428                                        (current-time-zone) "UT"))
10429                                      (gnus-seconds-since-epoch 
10430                                       (timezone-make-date-arpa-standard 
10431                                        date nil "UT")))
10432                                   0)
10433                            (error 0)))
10434                     num prev)
10435                (concat
10436                 "X-Sent: "
10437                 (mapconcat 
10438                  (lambda (unit)
10439                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10440                        ""
10441                      (setq sec (- sec (* num (cdr unit))))
10442                      (prog1
10443                          (concat (if prev ", " "") (int-to-string (floor num))
10444                                  " " (symbol-name (car unit))
10445                                  (if (> num 1) "s" ""))
10446                        (setq prev t))))
10447                  gnus-article-time-units "")
10448                 " ago\n")))
10449             (t
10450              (error "Unknown conversion type: %s" type)))))))))
10451
10452 (defun gnus-article-date-local ()
10453   "Convert the current article date to the local timezone."
10454   (interactive)
10455   (gnus-article-date-ut 'local))
10456
10457 (defun gnus-article-date-lapsed ()
10458   "Convert the current article date to time lapsed since it was sent."
10459   (interactive)
10460   (gnus-article-date-ut 'lapsed))
10461
10462 (defun gnus-article-maybe-highlight ()
10463   (if gnus-visual (gnus-article-highlight)))
10464
10465 ;; Article savers.
10466
10467 (defun gnus-output-to-rmail (file-name)
10468   "Append the current article to an Rmail file named FILE-NAME."
10469   (require 'rmail)
10470   ;; Most of these codes are borrowed from rmailout.el.
10471   (setq file-name (expand-file-name file-name))
10472   (setq rmail-default-rmail-file file-name)
10473   (let ((artbuf (current-buffer))
10474         (tmpbuf (get-buffer-create " *Gnus-output*")))
10475     (save-excursion
10476       (or (get-file-buffer file-name)
10477           (file-exists-p file-name)
10478           (if (gnus-yes-or-no-p
10479                (concat "\"" file-name "\" does not exist, create it? "))
10480               (let ((file-buffer (create-file-buffer file-name)))
10481                 (save-excursion
10482                   (set-buffer file-buffer)
10483                   (rmail-insert-rmail-file-header)
10484                   (let ((require-final-newline nil))
10485                     (write-region (point-min) (point-max) file-name t 1)))
10486                 (kill-buffer file-buffer))
10487             (error "Output file does not exist")))
10488       (set-buffer tmpbuf)
10489       (buffer-disable-undo (current-buffer))
10490       (erase-buffer)
10491       (insert-buffer-substring artbuf)
10492       (gnus-convert-article-to-rmail)
10493       ;; Decide whether to append to a file or to an Emacs buffer.
10494       (let ((outbuf (get-file-buffer file-name)))
10495         (if (not outbuf)
10496             (append-to-file (point-min) (point-max) file-name)
10497           ;; File has been visited, in buffer OUTBUF.
10498           (set-buffer outbuf)
10499           (let ((buffer-read-only nil)
10500                 (msg (and (boundp 'rmail-current-message)
10501                           (symbol-value 'rmail-current-message))))
10502             ;; If MSG is non-nil, buffer is in RMAIL mode.
10503             (if msg
10504                 (progn (widen)
10505                        (narrow-to-region (point-max) (point-max))))
10506             (insert-buffer-substring tmpbuf)
10507             (if msg
10508                 (progn
10509                   (goto-char (point-min))
10510                   (widen)
10511                   (search-backward "\^_")
10512                   (narrow-to-region (point) (point-max))
10513                   (goto-char (1+ (point-min)))
10514                   (rmail-count-new-messages t)
10515                   (rmail-show-message msg)))))))
10516     (kill-buffer tmpbuf)))
10517
10518 (defun gnus-output-to-file (file-name)
10519   "Append the current article to a file named FILE-NAME."
10520   (setq file-name (expand-file-name file-name))
10521   (let ((artbuf (current-buffer))
10522         (tmpbuf (get-buffer-create " *Gnus-output*")))
10523     (save-excursion
10524       (set-buffer tmpbuf)
10525       (buffer-disable-undo (current-buffer))
10526       (erase-buffer)
10527       (insert-buffer-substring artbuf)
10528       ;; Append newline at end of the buffer as separator, and then
10529       ;; save it to file.
10530       (goto-char (point-max))
10531       (insert "\n")
10532       (append-to-file (point-min) (point-max) file-name))
10533     (kill-buffer tmpbuf)))
10534
10535 (defun gnus-convert-article-to-rmail ()
10536   "Convert article in current buffer to Rmail message format."
10537   (let ((buffer-read-only nil))
10538     ;; Convert article directly into Babyl format.
10539     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10540     (goto-char (point-min))
10541     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10542     (while (search-forward "\n\^_" nil t) ;single char
10543       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10544     (goto-char (point-max))
10545     (insert "\^_")))
10546
10547 (defun gnus-narrow-to-page (&optional arg)
10548   "Make text outside current page invisible except for page delimiter.
10549 A numeric arg specifies to move forward or backward by that many pages,
10550 thus showing a page other than the one point was originally in."
10551   (interactive "P")
10552   (setq arg (if arg (prefix-numeric-value arg) 0))
10553   (save-excursion
10554     (forward-page -1)                   ;Beginning of current page.
10555     (widen)
10556     (if (> arg 0)
10557         (forward-page arg)
10558       (if (< arg 0)
10559           (forward-page (1- arg))))
10560     ;; Find the end of the page.
10561     (forward-page)
10562     ;; If we stopped due to end of buffer, stay there.
10563     ;; If we stopped after a page delimiter, put end of restriction
10564     ;; at the beginning of that line.
10565     ;; These are commented out.
10566     ;;    (if (save-excursion (beginning-of-line)
10567     ;;                  (looking-at page-delimiter))
10568     ;;  (beginning-of-line))
10569     (narrow-to-region (point)
10570                       (progn
10571                         ;; Find the top of the page.
10572                         (forward-page -1)
10573                         ;; If we found beginning of buffer, stay there.
10574                         ;; If extra text follows page delimiter on same line,
10575                         ;; include it.
10576                         ;; Otherwise, show text starting with following line.
10577                         (if (and (eolp) (not (bobp)))
10578                             (forward-line 1))
10579                         (point)))))
10580
10581 (defun gnus-gmt-to-local ()
10582   "Rewrite Date header described in GMT to local in current buffer.
10583 Intended to be used with gnus-article-prepare-hook."
10584   (save-excursion
10585     (save-restriction
10586       (widen)
10587       (goto-char (point-min))
10588       (narrow-to-region (point-min)
10589                         (progn (search-forward "\n\n" nil 'move) (point)))
10590       (goto-char (point-min))
10591       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10592           (let ((buffer-read-only nil)
10593                 (date (buffer-substring-no-properties
10594                        (match-beginning 1) (match-end 1))))
10595             (delete-region (match-beginning 1) (match-end 1))
10596             (insert
10597              (timezone-make-date-arpa-standard 
10598               date nil (current-time-zone))))))))
10599
10600
10601 ;; Article mode commands
10602
10603 (defun gnus-article-next-page (lines)
10604   "Show next page of current article.
10605 If end of article, return non-nil. Otherwise return nil.
10606 Argument LINES specifies lines to be scrolled up."
10607   (interactive "P")
10608   (move-to-window-line -1)
10609   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10610   (if (save-excursion
10611         (end-of-line)
10612         (and (pos-visible-in-window-p)  ;Not continuation line.
10613              (eobp)))
10614       ;; Nothing in this page.
10615       (if (or (not gnus-break-pages)
10616               (save-excursion
10617                 (save-restriction
10618                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10619           t                             ;Nothing more.
10620         (gnus-narrow-to-page 1)         ;Go to next page.
10621         nil)
10622     ;; More in this page.
10623     (condition-case ()
10624         (scroll-up lines)
10625       (end-of-buffer
10626        ;; Long lines may cause an end-of-buffer error.
10627        (goto-char (point-max))))
10628     nil))
10629
10630 (defun gnus-article-prev-page (lines)
10631   "Show previous page of current article.
10632 Argument LINES specifies lines to be scrolled down."
10633   (interactive "P")
10634   (move-to-window-line 0)
10635   (if (and gnus-break-pages
10636            (bobp)
10637            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10638       (progn
10639         (gnus-narrow-to-page -1) ;Go to previous page.
10640         (goto-char (point-max))
10641         (recenter -1))
10642     (scroll-down lines)))
10643
10644 (defun gnus-article-refer-article ()
10645   "Read article specified by message-id around point."
10646   (interactive)
10647   (search-forward ">" nil t)    ;Move point to end of "<....>".
10648   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10649       (let ((message-id
10650              (buffer-substring (match-beginning 1) (match-end 1))))
10651         (set-buffer gnus-summary-buffer)
10652         (gnus-summary-refer-article message-id))
10653     (error "No references around point")))
10654
10655 (defun gnus-article-show-summary ()
10656   "Reconfigure windows to show summary buffer."
10657   (interactive)
10658   (gnus-configure-windows 'article)
10659   (gnus-summary-goto-subject gnus-current-article))
10660
10661 (defun gnus-article-describe-briefly ()
10662   "Describe article mode commands briefly."
10663   (interactive)
10664   (gnus-message 6
10665    (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")))
10666
10667 (defun gnus-article-summary-command ()
10668   "Execute the last keystroke in the summary buffer."
10669   (interactive)
10670   (let ((obuf (current-buffer))
10671         (owin (current-window-configuration))
10672         func)
10673     (switch-to-buffer gnus-summary-buffer 'norecord)
10674     (setq func (lookup-key (current-local-map) (this-command-keys)))
10675     (call-interactively func)
10676     (set-buffer obuf)
10677     (let ((npoint (point)))
10678       (set-window-configuration owin)
10679       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10680
10681 (defun gnus-article-summary-command-nosave ()
10682   "Execute the last keystroke in the summary buffer."
10683   (interactive)
10684   (let ((obuf (current-buffer))
10685         (owin (current-window-configuration))
10686         func)
10687     (switch-to-buffer gnus-summary-buffer 'norecord)
10688     (setq func (lookup-key (current-local-map) (this-command-keys)))
10689     (call-interactively func)))
10690
10691 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10692 ;; Modified by tower@prep Nov 86
10693 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10694
10695 (defun gnus-caesar-region (&optional n)
10696   "Caesar rotation of region by N, default 13, for decrypting netnews.
10697 ROT47 will be performed for Japanese text in any case."
10698   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10699                    (list (prefix-numeric-value current-prefix-arg))
10700                  (list nil)))
10701   (cond ((not (numberp n)) (setq n 13))
10702         (t (setq n (mod n 26))))        ;canonicalize N
10703   (if (not (zerop n))           ; no action needed for a rot of 0
10704       (progn
10705         (if (or (not (boundp 'caesar-translate-table))
10706                 (not caesar-translate-table)
10707                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10708             (let ((i 0) 
10709                   (lower "abcdefghijklmnopqrstuvwxyz")
10710                   upper)
10711               (gnus-message 9 "Building caesar-translate-table...")
10712               (setq caesar-translate-table (make-vector 256 0))
10713               (while (< i 256)
10714                 (aset caesar-translate-table i i)
10715                 (setq i (1+ i)))
10716               (setq lower (concat lower lower)
10717                     upper (upcase lower)
10718                     i 0)
10719               (while (< i 26)
10720                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10721                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10722                 (setq i (1+ i)))
10723               ;; ROT47 for Japanese text.
10724               ;; Thanks to ichikawa@flab.fujitsu.junet.
10725               (setq i 161)
10726               (let ((t1 (logior ?O 128))
10727                     (t2 (logior ?! 128))
10728                     (t3 (logior ?~ 128)))
10729                 (while (< i 256)
10730                   (aset caesar-translate-table i
10731                         (let ((v (aref caesar-translate-table i)))
10732                           (if (<= v t1) (if (< v t2) v (+ v 47))
10733                             (if (<= v t3) (- v 47) v))))
10734                   (setq i (1+ i))))
10735               (gnus-message 9 "Building caesar-translate-table...done")))
10736         (let ((from (region-beginning))
10737               (to (region-end))
10738               (i 0) str len)
10739           (setq str (buffer-substring from to))
10740           (setq len (length str))
10741           (while (< i len)
10742             (aset str i (aref caesar-translate-table (aref str i)))
10743             (setq i (1+ i)))
10744           (goto-char from)
10745           (delete-region from to)
10746           (insert str)))))
10747
10748 \f
10749 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10750
10751 ;;;###autoload
10752 (defalias 'gnus-batch-kill 'gnus-batch-score)
10753 ;;;###autoload
10754 (defun gnus-batch-score ()
10755   "Run batched scoring.
10756 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10757 Newsgroups is a list of strings in Bnews format.  If you want to score
10758 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10759 score the alt hierarchy, you'd say \"!alt.all\"."
10760   (interactive)
10761   (let* ((yes-and-no
10762           (gnus-newsrc-parse-options
10763            (apply (function concat)
10764                   (mapcar (lambda (g) (concat g " "))
10765                           command-line-args-left))))
10766          (gnus-expert-user t)
10767          (nnmail-spool-file nil)
10768          (gnus-use-dribble-file nil)
10769          (yes (car yes-and-no))
10770          (no (cdr yes-and-no))
10771          group subscribed newsrc entry
10772          ;; Disable verbose message.
10773          gnus-novice-user gnus-large-newsgroup)
10774     ;; Eat all arguments.
10775     (setq command-line-args-left nil)
10776     ;; Start Gnus.
10777     (gnus)
10778     ;; Apply kills to specified newsgroups in command line arguments.
10779     (setq newsrc (cdr gnus-newsrc-alist))
10780     (while newsrc
10781       (setq group (car (car newsrc)))
10782       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10783       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10784                (and (car entry)
10785                     (or (eq (car entry) t)
10786                         (not (zerop (car entry)))))
10787                (if yes (string-match yes group) t)
10788                (or (null no) (not (string-match no group))))
10789           (progn
10790             (gnus-summary-read-group group nil t)
10791             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10792                  (gnus-summary-exit))))
10793       (setq newsrc (cdr newsrc)))
10794     ;; Exit Emacs.
10795     (switch-to-buffer gnus-group-buffer)
10796     (gnus-group-save-newsrc)))
10797
10798 (defun gnus-apply-kill-file ()
10799   "Apply a kill file to the current newsgroup.
10800 Returns the number of articles marked as read."
10801   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10802           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10803       (gnus-apply-kill-file-internal)
10804     0))
10805
10806 (defun gnus-kill-save-kill-buffer ()
10807   (save-excursion
10808     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10809       (if (get-file-buffer file)
10810           (progn
10811             (set-buffer (get-file-buffer file))
10812             (and (buffer-modified-p) (save-buffer))
10813             (kill-buffer (current-buffer)))))))
10814
10815 (defvar gnus-kill-file-name "KILL"
10816   "Suffix of the kill files.")
10817
10818 (defun gnus-newsgroup-kill-file (newsgroup)
10819   "Return the name of a kill file name for NEWSGROUP.
10820 If NEWSGROUP is nil, return the global kill file name instead."
10821   (cond ((or (null newsgroup)
10822              (string-equal newsgroup ""))
10823          ;; The global KILL file is placed at top of the directory.
10824          (expand-file-name gnus-kill-file-name
10825                            (or gnus-kill-files-directory "~/News")))
10826         ((gnus-use-long-file-name 'not-kill)
10827          ;; Append ".KILL" to newsgroup name.
10828          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10829                            (or gnus-kill-files-directory "~/News")))
10830         (t
10831          ;; Place "KILL" under the hierarchical directory.
10832          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10833                                    "/" gnus-kill-file-name)
10834                            (or gnus-kill-files-directory "~/News")))))
10835
10836 \f
10837 ;;;
10838 ;;; Dribble file
10839 ;;;
10840
10841 (defvar gnus-dribble-ignore nil)
10842
10843 (defun gnus-dribble-file-name ()
10844   (concat gnus-startup-file "-dribble"))
10845
10846 (defun gnus-dribble-open ()
10847   (save-excursion 
10848     (set-buffer 
10849      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10850     (buffer-disable-undo (current-buffer))
10851     (bury-buffer gnus-dribble-buffer)
10852     (auto-save-mode t)
10853     (goto-char (point-max))))
10854
10855 (defun gnus-dribble-enter (string)
10856   (if (and (not gnus-dribble-ignore)
10857            gnus-dribble-buffer
10858            (buffer-name gnus-dribble-buffer))
10859       (let ((obuf (current-buffer)))
10860         (set-buffer gnus-dribble-buffer)
10861         (insert string "\n")
10862         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10863         (set-buffer obuf))))
10864
10865 (defun gnus-dribble-read-file ()
10866   (let ((dribble-file (gnus-dribble-file-name)))
10867     (save-excursion 
10868       (set-buffer (setq gnus-dribble-buffer 
10869                         (get-buffer-create 
10870                          (file-name-nondirectory dribble-file))))
10871       (gnus-add-current-to-buffer-list)
10872       (erase-buffer)
10873       (set-visited-file-name dribble-file)
10874       (buffer-disable-undo (current-buffer))
10875       (bury-buffer (current-buffer))
10876       (set-buffer-modified-p nil)
10877       (let ((auto (make-auto-save-file-name))
10878             (gnus-dribble-ignore t))
10879         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10880             (progn
10881               (if (file-newer-than-file-p auto dribble-file)
10882                   (setq dribble-file auto))
10883               (insert-file-contents dribble-file)
10884               (if (not (zerop (buffer-size)))
10885                   (set-buffer-modified-p t))
10886               (if (gnus-y-or-n-p 
10887                    "Auto-save file exists. Do you want to read it? ")
10888                   (progn
10889                     (gnus-message 5 "Reading %s..." dribble-file) 
10890                     (eval-current-buffer)
10891                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10892
10893 (defun gnus-dribble-delete-file ()
10894   (if (file-exists-p (gnus-dribble-file-name))
10895       (delete-file (gnus-dribble-file-name)))
10896   (if gnus-dribble-buffer
10897       (save-excursion
10898         (set-buffer gnus-dribble-buffer)
10899         (let ((auto (make-auto-save-file-name)))
10900           (if (file-exists-p auto)
10901               (delete-file auto))
10902           (erase-buffer)
10903           (set-buffer-modified-p nil)))))
10904
10905 (defun gnus-dribble-save ()
10906   (if (and gnus-dribble-buffer
10907            (buffer-name gnus-dribble-buffer))
10908       (save-excursion
10909         (set-buffer gnus-dribble-buffer)
10910         (save-buffer))))
10911
10912 (defun gnus-dribble-clear ()
10913   (save-excursion
10914     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10915         (progn
10916           (set-buffer gnus-dribble-buffer)
10917           (erase-buffer)
10918           (set-buffer-modified-p nil)
10919           (setq buffer-saved-size (buffer-size))))))
10920
10921 ;;;
10922 ;;; Server Communication
10923 ;;;
10924
10925 ;; All the Gnus backends have the same interface, and should return
10926 ;; data in a similar format. Below is an overview of what functions
10927 ;; these packages must supply and what results they should return.
10928 ;;
10929 ;; Variables:
10930 ;;
10931 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10932 ;; buffer. 
10933 ;;
10934 ;; Functions for the imaginary backend `choke':
10935 ;;
10936 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10937 ;; Should return all headers for all ARTICLES, or return NOV lines for
10938 ;; the same.
10939 ;;
10940 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10941 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10942 ;; must be returned.
10943 ;;
10944 ;; `choke-close-group GROUP &optional SERVER'
10945 ;; Close group. Most backends won't have to do anything with this
10946 ;; call, but it is an opportunity to clean up, if that is needed. It
10947 ;; is called when Gnus exits a group.
10948 ;;
10949 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10950 ;; Return ARTICLE, which is either an article number or
10951 ;; message-id. Note that not all backends can return articles based on
10952 ;; message-id. 
10953 ;;
10954 ;; `choke-request-list SERVER'
10955 ;; Return a list of all newsgroups on SERVER.
10956 ;;
10957 ;; `choke-request-list-newsgroups SERVER'
10958 ;; Return a list of descriptions of all newsgroups on SERVER.
10959 ;;
10960 ;; `choke-request-newgroups DATE &optional SERVER'
10961 ;; Return a list of all groups that have arrived after DATE on
10962 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10963 ;; always check whether the groups are old or not. Backends that do
10964 ;; not store date information may just return the entire list of
10965 ;; groups, although this might not be a good idea in general.
10966 ;;
10967 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10968 ;; Should return a buffer that is suitable for "posting". nnspool and
10969 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10970 ;; buffer. This function should fill out the appropriate headers. 
10971 ;;
10972 ;; `choke-request-post &optional SERVER'
10973 ;; Function that will be called from a buffer to be posted. 
10974 ;;
10975 ;; `choke-open-server SERVER &optional ARGUMENT'
10976 ;; Open a connection to SERVER.
10977 ;;
10978 ;; `choke-close-server &optional SERVER'
10979 ;; Close the connection to SERVER.
10980 ;;
10981 ;; `choke-server-opened &optional SERVER'
10982 ;; Whether the conenction to SERVER is opened or not.
10983 ;;
10984 ;; `choke-server-status &optional SERVER'
10985 ;; Should return a status string (not in the nntp buffer, but as the
10986 ;; result of the function).
10987 ;;
10988 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10989 ;; Optional function for retrieving active file info on all groups in
10990 ;; GROUPS.  Two return formats are supported: The normal active file
10991 ;; format, and a list of GROUP lines.  This function should return (as
10992 ;; a function value) either `active' or `group', depending on what
10993 ;; format it returns.
10994 ;;
10995 ;; The following functions are optional and apply only to backends
10996 ;; that are able to control the contents of their groups totally
10997 ;; (ie. mail backends.)  Backends that aren't able to do that
10998 ;; shouldn't define these functions at all. Gnus will check for their
10999 ;; presence before attempting to call them.
11000 ;;
11001 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11002 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11003 ;; backends will not be able to expire articles. Should return a list
11004 ;; of all articles that were not expired.
11005 ;;
11006 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11007 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11008 ;; Removes any information it has added to the article (extra headers,
11009 ;; whatever - make it as clean as possible), and then passes the
11010 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11011 ;; function described below. If the ACCEPT-FORM returns a non-nil
11012 ;; value, the article should then be deleted. If LAST is nil, that
11013 ;; means that there will be further calls to this function. This might
11014 ;; be taken as an advice not to save buffers/internal variables just
11015 ;; yet, but wait until the last call to speed things up.
11016 ;;
11017 ;; `choke-request-accept-article GROUP &optional LAST' 
11018 ;; The contents of the current buffer will be put into GROUP.  There
11019 ;; should, of course, be an article in the current buffer.  This
11020 ;; function is normally only called by the function described above,
11021 ;; and LAST works the same way as in that function.
11022 ;;
11023 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11024 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11025 ;; This provides an easy interface for allowing editing of
11026 ;; articles. Note that even headers may be edited, so the backend has
11027 ;; to update any tables (nov buffers, etc) that it maintains after
11028 ;; replacing the article.
11029 ;;
11030 ;; `choke-request-create-group GROUP &optional SERVER'
11031 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11032 ;; might be a group that already exists, but hasn't been registered
11033 ;; yet. 
11034 ;;
11035 ;; All these functions must return nil if they couldn't service the
11036 ;; request. If the optional arguments are not supplied, some "current"
11037 ;; or "default" values should be used. In short, one should emulate an
11038 ;; NNTP server, in a way.
11039 ;;
11040 ;; If you want to write a new backend, you just have to supply the
11041 ;; functions listed above. In addition, you must enter the new backend
11042 ;; into the list of valid select methods:
11043 ;; (setq gnus-valid-select-methods 
11044 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11045 ;; The first element in this list is the name of the backend. Other
11046 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11047 ;; groups), `none' (neither), `respool' (for groups that can control
11048 ;; their contents). 
11049
11050 (defun gnus-start-news-server (&optional confirm)
11051   "Open a method for getting news.
11052 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11053   (let (how where)
11054     (if gnus-current-select-method
11055         ;; Stream is already opened.
11056         nil
11057       ;; Open NNTP server.
11058       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11059       (if confirm
11060           (progn
11061             ;; Read server name with completion.
11062             (setq gnus-nntp-server
11063                   (completing-read "NNTP server: "
11064                                    (mapcar (lambda (server) (list server))
11065                                            (cons (list gnus-nntp-server)
11066                                                  gnus-secondary-servers))
11067                                    nil nil gnus-nntp-server))))
11068
11069       (if (and gnus-nntp-server 
11070                (stringp gnus-nntp-server)
11071                (not (string= gnus-nntp-server "")))
11072           (setq gnus-select-method
11073                 (cond ((or (string= gnus-nntp-server "")
11074                            (string= gnus-nntp-server "::"))
11075                        (list 'nnspool (system-name)))
11076                       ((string-match "^:" gnus-nntp-server)
11077                        (list 'nnmh gnus-nntp-server 
11078                              (list 'nnmh-directory 
11079                                    (file-name-as-directory
11080                                     (expand-file-name
11081                                      (concat "~/" (substring
11082                                                    gnus-nntp-server 1)))))
11083                              (list 'nnmh-get-new-mail nil)))
11084                       (t
11085                        (list 'nntp gnus-nntp-server)))))
11086
11087       (setq how (car gnus-select-method))
11088       (setq where (car (cdr gnus-select-method)))
11089       (cond ((eq how 'nnspool)
11090              (require 'nnspool)
11091              (gnus-message 5 "Looking up local news spool..."))
11092             ((eq how 'nnmh)
11093              (require 'nnmh)
11094              (gnus-message 5 "Looking up mh spool..."))
11095             (t
11096              (require 'nntp)))
11097       (setq gnus-current-select-method gnus-select-method)
11098       (run-hooks 'gnus-open-server-hook)
11099       (or 
11100        ;; gnus-open-server-hook might have opened it
11101        (gnus-server-opened gnus-select-method)  
11102        (gnus-open-server gnus-select-method)
11103        (gnus-y-or-n-p
11104         (format
11105          "%s server on %s can't be opened. Continue? "
11106          (car gnus-select-method) (nth 1 gnus-select-method)))
11107        (progn
11108          (gnus-message 1 "Couldn't open server on %s" 
11109                        (nth 1 gnus-select-method))
11110          (ding)
11111          nil)))))
11112
11113 (defun gnus-check-news-server (&optional method)
11114   "If the news server is down, start it up again."
11115   (let ((method (if method method gnus-select-method)))
11116     (and (stringp method)
11117          (setq method (gnus-server-to-method method)))
11118     (if (gnus-server-opened method)
11119         ;; Stream is already opened.
11120         t
11121       ;; Open server.
11122       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11123       (run-hooks 'gnus-open-server-hook)
11124       (or (gnus-server-opened method)
11125           (gnus-open-server method))
11126       (message ""))))
11127
11128 (defun gnus-nntp-message (&optional message)
11129   "Check the status of the NNTP server.
11130 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11131 is returned insted of the status string."
11132   (let ((status (gnus-status-message (gnus-find-method-for-group 
11133                                       gnus-newsgroup-name)))
11134         (message (or message "")))
11135     (if (and (stringp status) (> (length status) 0))
11136         status message)))
11137
11138 (defun gnus-get-function (method function)
11139   (and (stringp method)
11140        (setq method (gnus-server-to-method method)))
11141   (let ((func (intern (format "%s-%s" (car method) function))))
11142     (if (not (fboundp func)) 
11143         (progn
11144           (require (car method))
11145           (if (not (fboundp func)) 
11146               (error "No such function: %s" func))))
11147     func))
11148
11149 ;;; Interface functions to the backends.
11150
11151 (defun gnus-open-server (method)
11152   (funcall (gnus-get-function method 'open-server)
11153            (nth 1 method) (nthcdr 2 method)))
11154
11155 (defun gnus-close-server (method)
11156   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11157
11158 (defun gnus-request-list (method)
11159   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11160
11161 (defun gnus-request-list-newsgroups (method)
11162   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11163
11164 (defun gnus-request-newgroups (date method)
11165   (funcall (gnus-get-function method 'request-newgroups) 
11166            date (nth 1 method)))
11167
11168 (defun gnus-server-opened (method)
11169   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11170
11171 (defun gnus-status-message (method)
11172   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11173                   method)))
11174     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11175
11176 (defun gnus-request-group (group &optional dont-check)
11177   (let ((method (gnus-find-method-for-group group)))
11178 ;    (and t (message "%s GROUP %s" (car method) group))
11179     (funcall (gnus-get-function method 'request-group) 
11180              (gnus-group-real-name group) (nth 1 method) dont-check)))
11181
11182 (defun gnus-request-asynchronous (group &optional articles)
11183   (let ((method (gnus-find-method-for-group group)))
11184     (funcall (gnus-get-function method 'request-asynchronous) 
11185              (gnus-group-real-name group) (nth 1 method) articles)))
11186
11187 (defun gnus-list-active-group (group)
11188   (let ((method (gnus-find-method-for-group group))
11189         (func 'list-active-group))
11190     (and (gnus-check-backend-function func group)
11191          (funcall (gnus-get-function method func) 
11192                   (gnus-group-real-name group) (nth 1 method)))))
11193
11194 (defun gnus-request-group-description (group)
11195   (let ((method (gnus-find-method-for-group group))
11196         (func 'request-group-description))
11197     (and (gnus-check-backend-function func group)
11198          (funcall (gnus-get-function method func) 
11199                   (gnus-group-real-name group) (nth 1 method)))))
11200
11201 (defun gnus-close-group (group)
11202   (let ((method (gnus-find-method-for-group group)))
11203     (funcall (gnus-get-function method 'close-group) 
11204              (gnus-group-real-name group) (nth 1 method))))
11205
11206 (defun gnus-retrieve-headers (articles group)
11207   (let ((method (gnus-find-method-for-group group)))
11208     (if (and gnus-use-cache (numberp (car articles)))
11209         (gnus-cache-retrieve-headers articles group)
11210       (funcall (gnus-get-function method 'retrieve-headers) 
11211                articles (gnus-group-real-name group) (nth 1 method)))))
11212
11213 (defun gnus-retrieve-groups (groups method)
11214   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11215
11216 (defun gnus-request-article (article group &optional buffer)
11217   (let ((method (gnus-find-method-for-group group)))
11218     (funcall (gnus-get-function method 'request-article) 
11219              article (gnus-group-real-name group) (nth 1 method) buffer)))
11220
11221 (defun gnus-request-head (article group)
11222   (let ((method (gnus-find-method-for-group group)))
11223     (funcall (gnus-get-function method 'request-head) 
11224              article (gnus-group-real-name group) (nth 1 method))))
11225
11226 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11227 (defun gnus-request-post-buffer (post group subject header artbuf
11228                                       info follow-to respect-poster)
11229    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11230                                             group gnus-newsrc-hashtb)))))
11231           (method
11232            (if (and gnus-post-method
11233                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11234                     (memq 'post (assoc
11235                                  (format "%s" (car (gnus-find-method-for-group
11236                                                     gnus-newsgroup-name)))
11237                                         gnus-valid-select-methods)))
11238                gnus-post-method
11239              (gnus-find-method-for-group gnus-newsgroup-name))))
11240      (or (gnus-server-opened method)
11241          (gnus-open-server method)
11242          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11243      (let ((mail-self-blind nil)
11244            (mail-archive-file-name nil))
11245        (funcall (gnus-get-function method 'request-post-buffer) 
11246                 post group subject header artbuf info follow-to
11247                 respect-poster))))
11248
11249 (defun gnus-request-post (method &optional force)
11250   (and (stringp method)
11251        (setq method (gnus-server-to-method method)))
11252   (and (not force) gnus-post-method
11253        (memq 'post (assoc (format "%s" (car method))
11254                           gnus-valid-select-methods))
11255        (setq method gnus-post-method))
11256   (funcall (gnus-get-function method 'request-post) 
11257            (nth 1 method)))
11258
11259 (defun gnus-request-expire-articles (articles group &optional force)
11260   (let ((method (gnus-find-method-for-group group)))
11261     (funcall (gnus-get-function method 'request-expire-articles) 
11262              articles (gnus-group-real-name group) (nth 1 method)
11263              force)))
11264
11265 (defun gnus-request-move-article 
11266   (article group server accept-function &optional last)
11267   (let ((method (gnus-find-method-for-group group)))
11268     (funcall (gnus-get-function method 'request-move-article) 
11269              article (gnus-group-real-name group) 
11270              (nth 1 method) accept-function last)))
11271
11272 (defun gnus-request-accept-article (group &optional last)
11273   (let ((func (if (symbolp group) group
11274                 (car (gnus-find-method-for-group group)))))
11275     (funcall (intern (format "%s-request-accept-article" func))
11276              (if (stringp group) (gnus-group-real-name group) group)
11277              last)))
11278
11279 (defun gnus-request-replace-article (article group buffer)
11280   (let ((func (car (gnus-find-method-for-group group))))
11281     (funcall (intern (format "%s-request-replace-article" func))
11282              article (gnus-group-real-name group) buffer)))
11283
11284 (defun gnus-request-create-group (group)
11285   (let ((method (gnus-find-method-for-group group)))
11286     (funcall (gnus-get-function method 'request-create-group) 
11287              (gnus-group-real-name group) (nth 1 method))))
11288
11289 (defun gnus-member-of-valid (symbol group)
11290   (memq symbol (assoc
11291                 (format "%s" (car (gnus-find-method-for-group group)))
11292                 gnus-valid-select-methods)))
11293
11294 (defsubst gnus-secondary-method-p (method)
11295   (member method gnus-secondary-select-methods))
11296
11297 (defun gnus-find-method-for-group (group &optional info)
11298   (or gnus-override-method
11299       (and (not group)
11300            gnus-select-method)
11301       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11302             method)
11303         (if (or (not info)
11304                 (not (setq method (nth 4 info))))
11305             (setq method gnus-select-method)
11306           (setq method
11307                 (cond ((stringp method)
11308                        (gnus-server-to-method method))
11309                       ((stringp (car method))
11310                        (gnus-server-extend-method group method))
11311                       (t
11312                        method))))
11313         (gnus-server-add-address method))))
11314
11315 (defun gnus-check-backend-function (func group)
11316   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11317                  group)))
11318     (fboundp (intern (format "%s-%s" method func)))))
11319
11320 (defun gnus-methods-using (method)
11321   (let ((valids gnus-valid-select-methods)
11322         outs)
11323     (while valids
11324       (if (memq method (car valids)) 
11325           (setq outs (cons (car valids) outs)))
11326       (setq valids (cdr valids)))
11327     outs))
11328
11329 ;;; 
11330 ;;; Active & Newsrc File Handling
11331 ;;;
11332
11333 ;; Newsrc related functions.
11334 ;; Gnus internal format of gnus-newsrc-alist:
11335 ;; (("alt.general" 3 (1 . 1))
11336 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11337 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11338 ;; The first item is the group name; the second is the subscription
11339 ;; level; the third is either a range of a list of ranges of read
11340 ;; articles, the optional fourth element is a list of marked articles,
11341 ;; the optional fifth element is the select method.
11342 ;;
11343 ;; Gnus internal format of gnus-newsrc-hashtb:
11344 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11345 ;; This is the entry for "alt.misc". The first element is the number
11346 ;; of unread articles in "alt.misc". The cdr of this entry is the
11347 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11348 ;; trivial to remove or add new elements into gnus-newsrc-alist
11349 ;; without scanning the entire list. So, to get the actual information
11350 ;; of "alt.misc", you'd say something like 
11351 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11352 ;;
11353 ;; Gnus internal format of gnus-active-hashtb:
11354 ;; ((1 . 1))
11355 ;;  (5 . 10))
11356 ;;  (67 . 99)) ...)
11357 ;; The only element in each entry in this hash table is a range of
11358 ;; (possibly) available articles. (Articles in this range may have
11359 ;; been expired or canceled.)
11360 ;;
11361 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11362 ;; ("alt.misc" "alt.test" "alt.general" ...)
11363
11364 (defun gnus-setup-news (&optional rawfile level)
11365   "Setup news information.
11366 If RAWFILE is non-nil, the .newsrc file will also be read.
11367 If LEVEL is non-nil, the news will be set up at level LEVEL."
11368   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11369     ;; Clear some variables to re-initialize news information.
11370     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11371
11372     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11373     (if init (gnus-read-newsrc-file rawfile))
11374
11375     ;; Read the active file and create `gnus-active-hashtb'.
11376     ;; If `gnus-read-active-file' is nil, then we just create an empty
11377     ;; hash table. The partial filling out of the hash table will be
11378     ;; done in `gnus-get-unread-articles'.
11379     (if (and gnus-read-active-file 
11380              (not level))
11381         (gnus-read-active-file)
11382       (setq gnus-active-hashtb (make-vector 4095 0)))
11383
11384     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11385
11386     ;; Find the number of unread articles in each non-dead group.
11387     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11388     ;; Find new newsgroups and treat them.
11389     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11390              (gnus-server-opened gnus-select-method))
11391         (gnus-find-new-newsgroups))
11392     (if (and init gnus-check-bogus-newsgroups 
11393              gnus-read-active-file (not level)
11394              (gnus-server-opened gnus-select-method))
11395         (gnus-check-bogus-newsgroups))))
11396
11397 (defun gnus-find-new-newsgroups ()
11398   "Search for new newsgroups and add them.
11399 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11400 The `-n' option line from .newsrc is respected."
11401   (interactive)
11402   (or (gnus-check-first-time-used)
11403       (if (or (consp gnus-check-new-newsgroups)
11404               (eq gnus-check-new-newsgroups 'ask-server))
11405           (gnus-ask-server-for-new-groups)
11406         (let ((groups 0)
11407               group new-newsgroups)
11408           (or gnus-have-read-active-file (gnus-read-active-file))
11409           (setq gnus-newsrc-last-checked-date (current-time-string))
11410           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11411           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11412           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11413           (mapatoms
11414            (lambda (sym)
11415              (setq group (symbol-name sym))
11416              (if (or (gnus-gethash group gnus-killed-hashtb)
11417                      (gnus-gethash group gnus-newsrc-hashtb))
11418                  ()
11419                (let ((do-sub (gnus-matches-options-n group)))
11420                  (cond ((eq do-sub 'subscribe)
11421                         (setq groups (1+ groups))
11422                         (gnus-sethash group group gnus-killed-hashtb)
11423                         (funcall 
11424                          gnus-subscribe-options-newsgroup-method group))
11425                        ((eq do-sub 'ignore)
11426                         nil)
11427                        (t
11428                         (setq groups (1+ groups))
11429                         (gnus-sethash group group gnus-killed-hashtb)
11430                         (if gnus-subscribe-hierarchical-interactive
11431                             (setq new-newsgroups (cons group new-newsgroups))
11432                           (funcall gnus-subscribe-newsgroup-method group)))))))
11433            gnus-active-hashtb)
11434           (if new-newsgroups 
11435               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11436           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11437           (if (> groups 0)
11438               (gnus-message 6 "%d new newsgroup%s arrived." 
11439                             groups (if (> groups 1) "s have" " has")))))))
11440
11441 (defun gnus-matches-options-n (group)
11442   ;; Returns `subscribe' if the group is to be uncoditionally
11443   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11444   ;; no match for the group.
11445
11446   ;; First we check the two user variables.
11447   (cond
11448    ((and gnus-options-subscribe
11449          (string-match gnus-options-subscribe group))
11450     'subscribe)
11451    ((and gnus-options-not-subscribe
11452          (string-match gnus-options-not-subscribe group))
11453     'ignore)
11454    ;; Then we go through the list that was retrieved from the .newsrc
11455    ;; file.  This list has elements on the form 
11456    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11457    ;; is in the reverse order of the options line) is returned.
11458    (t
11459     (let ((regs gnus-newsrc-options-n))
11460       (while (and regs
11461                   (not (string-match (car (car regs)) group)))
11462         (setq regs (cdr regs)))
11463       (and regs (cdr (car regs)))))))
11464
11465 (defun gnus-ask-server-for-new-groups ()
11466   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11467          (methods (cons gnus-select-method 
11468                         (append
11469                          (and (consp gnus-check-new-newsgroups)
11470                               gnus-check-new-newsgroups)
11471                          gnus-secondary-select-methods)))
11472          (groups 0)
11473          (new-date (current-time-string))
11474          hashtb group new-newsgroups got-new)
11475     ;; Go thorugh both primary and secondary select methods and
11476     ;; request new newsgroups.  
11477     (while methods
11478       (and (or (gnus-server-opened (car methods))
11479                (gnus-open-server (car methods)))
11480            (gnus-request-newgroups date (car methods))
11481            (save-excursion
11482              (setq got-new t)
11483              (set-buffer nntp-server-buffer)
11484              (or hashtb (setq hashtb (gnus-make-hashtable 
11485                                       (count-lines (point-min) (point-max)))))
11486              ;; Enter all the new groups in a hashtable.
11487              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11488       (setq methods (cdr methods)))
11489     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11490     ;; Now all new groups from all select methods are in `hashtb'.
11491     (mapatoms
11492      (lambda (group-sym)
11493        (setq group (symbol-name group-sym))
11494        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11495                (member group gnus-zombie-list)
11496                (member group gnus-killed-list))
11497            ;; The group is already known.
11498            ()
11499          (and (symbol-value group-sym)
11500               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11501          (let ((do-sub (gnus-matches-options-n group)))
11502            (cond ((eq do-sub 'subscribe)
11503                   (setq groups (1+ groups))
11504                   (gnus-sethash group group gnus-killed-hashtb)
11505                   (funcall 
11506                    gnus-subscribe-options-newsgroup-method group))
11507                  ((eq do-sub 'ignore)
11508                   nil)
11509                  (t
11510                   (setq groups (1+ groups))
11511                   (gnus-sethash group group gnus-killed-hashtb)
11512                   (if gnus-subscribe-hierarchical-interactive
11513                       (setq new-newsgroups (cons group new-newsgroups))
11514                     (funcall gnus-subscribe-newsgroup-method group)))))))
11515      hashtb)
11516     (if new-newsgroups 
11517         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11518     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11519     (if (> groups 0)
11520         (gnus-message 6 "%d new newsgroup%s arrived." 
11521                       groups (if (> groups 1) "s have" " has")))
11522     got-new))
11523
11524 (defun gnus-check-first-time-used ()
11525   (if (or (> (length gnus-newsrc-alist) 1)
11526           (file-exists-p gnus-startup-file)
11527           (file-exists-p (concat gnus-startup-file ".el"))
11528           (file-exists-p (concat gnus-startup-file ".eld")))
11529       nil
11530     (gnus-message 6 "First time user; subscribing you to default groups")
11531     (or gnus-have-read-active-file (gnus-read-active-file))
11532     (setq gnus-newsrc-last-checked-date (current-time-string))
11533     (let ((groups gnus-default-subscribed-newsgroups)
11534           group)
11535       (if (eq groups t)
11536           nil
11537         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11538         (mapatoms
11539          (lambda (sym)
11540            (setq group (symbol-name sym))
11541            (let ((do-sub (gnus-matches-options-n group)))
11542              (cond ((eq do-sub 'subscribe)
11543                     (gnus-sethash group group gnus-killed-hashtb)
11544                     (funcall 
11545                      gnus-subscribe-options-newsgroup-method group))
11546                    ((eq do-sub 'ignore)
11547                     nil)
11548                    (t
11549                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11550          gnus-active-hashtb)
11551         (while groups
11552           (if (gnus-gethash (car groups) gnus-active-hashtb)
11553               (gnus-group-change-level 
11554                (car groups) gnus-level-default-subscribed gnus-level-killed))
11555           (setq groups (cdr groups)))
11556         (gnus-group-make-help-group)
11557         (and gnus-novice-user
11558              (gnus-message 7 "`A k' to list killed groups"))))))
11559
11560 (defun gnus-subscribe-group (group previous &optional method)
11561   (gnus-group-change-level 
11562    (if method
11563        (list t group gnus-level-default-subscribed nil nil method)
11564      group) 
11565    gnus-level-default-subscribed gnus-level-killed previous t))
11566
11567 ;; `gnus-group-change-level' is the fundamental function for changing
11568 ;; subscription levels of newsgroups. This might mean just changing
11569 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11570 ;; again, which subscribes/unsubscribes a group, which is equally
11571 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11572 ;; from 8-9 to 1-7 means that you remove the group from the list of
11573 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11574 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11575 ;; which is trivial.
11576 ;; ENTRY can either be a string (newsgroup name) or a list (if
11577 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11578 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11579 ;; entries. 
11580 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11581 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11582 ;; after. 
11583 (defun gnus-group-change-level (entry level &optional oldlevel
11584                                       previous fromkilled)
11585   (let ((pinfo entry)
11586         group info active num)
11587     ;; Glean what info we can from the arguments
11588     (if (consp entry)
11589         (if fromkilled (setq group (nth 1 entry))
11590           (setq group (car (nth 2 entry))))
11591       (setq group entry))
11592     (if (and (stringp entry)
11593              oldlevel 
11594              (< oldlevel gnus-level-zombie))
11595         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11596     (if (and (not oldlevel)
11597              (consp entry))
11598         (setq oldlevel (car (cdr (nth 2 entry)))))
11599     (if (stringp previous)
11600         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11601
11602     (gnus-dribble-enter
11603      (format "(gnus-group-change-level %S %S %S %S %S)" 
11604              group level oldlevel (car (nth 2 previous)) fromkilled))
11605     
11606     ;; Then we remove the newgroup from any old structures, if needed.
11607     ;; If the group was killed, we remove it from the killed or zombie
11608     ;; list. If not, and it is in fact going to be killed, we remove
11609     ;; it from the newsrc hash table and assoc.
11610     (cond ((>= oldlevel gnus-level-zombie)
11611            (if (= oldlevel gnus-level-zombie)
11612                (setq gnus-zombie-list (delete group gnus-zombie-list))
11613              (setq gnus-killed-list (delete group gnus-killed-list))))
11614           (t
11615            (if (>= level gnus-level-zombie)
11616                (progn
11617                  (gnus-sethash (car (nth 2 entry))
11618                                nil gnus-newsrc-hashtb)
11619                  (if (nth 3 entry)
11620                      (setcdr (gnus-gethash (car (nth 3 entry))
11621                                            gnus-newsrc-hashtb)
11622                              (cdr entry)))
11623                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11624
11625     ;; Finally we enter (if needed) the list where it is supposed to
11626     ;; go, and change the subscription level. If it is to be killed,
11627     ;; we enter it into the killed or zombie list.
11628     (cond ((>= level gnus-level-zombie)
11629            ;; Remove from the hash table.
11630            (gnus-sethash group nil gnus-newsrc-hashtb)
11631            (or (gnus-group-foreign-p group)
11632                ;; We do not enter foreign groups into the list of dead
11633                ;; groups.  
11634                (if (= level gnus-level-zombie)
11635                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11636                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11637           (t
11638            ;; If the list is to be entered into the newsrc assoc, and
11639            ;; it was killed, we have to create an entry in the newsrc
11640            ;; hashtb format and fix the pointers in the newsrc assoc.
11641            (if (>= oldlevel gnus-level-zombie)
11642                (progn
11643                  (if (listp entry)
11644                      (progn
11645                        (setq info (cdr entry))
11646                        (setq num (car entry)))
11647                    (setq active (gnus-gethash group gnus-active-hashtb))
11648                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11649                    ;; Check whether the group is foreign. If so, the
11650                    ;; foreign select method has to be entered into the
11651                    ;; info. 
11652                    (let ((method (gnus-group-method-name group)))
11653                      (if (eq method gnus-select-method)
11654                          (setq info (list group level nil))
11655                        (setq info (list group level nil nil method)))))
11656                  (or previous 
11657                      (setq previous 
11658                            (let ((p gnus-newsrc-alist))
11659                              (while (cdr (cdr p))
11660                                (setq p (cdr p)))
11661                              p)))
11662                  (setq entry (cons info (cdr (cdr previous))))
11663                  (if (cdr previous)
11664                      (progn
11665                        (setcdr (cdr previous) entry)
11666                        (gnus-sethash group (cons num (cdr previous)) 
11667                                      gnus-newsrc-hashtb))
11668                    (setcdr previous entry)
11669                    (gnus-sethash group (cons num previous)
11670                                  gnus-newsrc-hashtb))
11671                  (if (cdr entry)
11672                      (setcdr (gnus-gethash (car (car (cdr entry)))
11673                                            gnus-newsrc-hashtb)
11674                              entry)))
11675              ;; It was alive, and it is going to stay alive, so we
11676              ;; just change the level and don't change any pointers or
11677              ;; hash table entries.
11678              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11679
11680 (defun gnus-kill-newsgroup (newsgroup)
11681   "Obsolete function. Kills a newsgroup."
11682   (gnus-group-change-level
11683    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11684
11685 (defun gnus-check-bogus-newsgroups (&optional confirm)
11686   "Remove bogus newsgroups.
11687 If CONFIRM is non-nil, the user has to confirm the deletion of every
11688 newsgroup." 
11689   (let ((newsrc (cdr gnus-newsrc-alist))
11690         bogus group entry)
11691     (gnus-message 5 "Checking bogus newsgroups...")
11692     (or gnus-have-read-active-file (gnus-read-active-file))
11693     ;; Find all bogus newsgroup that are subscribed.
11694     (while newsrc
11695       (setq group (car (car newsrc)))
11696       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11697               (nth 4 (car newsrc))      ; Foreign
11698               (and confirm
11699                    (not (gnus-y-or-n-p
11700                          (format "Remove bogus newsgroup: %s " group)))))
11701           ;; Don't remove.
11702           ()
11703         ;; Found a bogus newsgroup.
11704         (setq bogus (cons group bogus)))
11705       (setq newsrc (cdr newsrc)))
11706     ;; Remove all bogus subscribed groups by first killing them, and
11707     ;; then removing them from the list of killed groups.
11708     (while bogus
11709       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11710            (progn
11711              (gnus-group-change-level entry gnus-level-killed)
11712              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11713       (setq bogus (cdr bogus)))
11714     ;; Then we remove all bogus groups from the list of killed and
11715     ;; zombie groups. They are are removed without confirmation.
11716     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11717           killed)
11718       (while dead-lists
11719         (setq killed (symbol-value (car dead-lists)))
11720         (while killed
11721           (setq group (car killed))
11722           (or (gnus-gethash group gnus-active-hashtb)
11723               ;; The group is bogus.
11724               (set (car dead-lists)
11725                    (delete group (symbol-value (car dead-lists)))))
11726           (setq killed (cdr killed)))
11727         (setq dead-lists (cdr dead-lists))))
11728     (gnus-message 5 "Checking bogus newsgroups...done")))
11729
11730 (defun gnus-check-duplicate-killed-groups ()
11731   "Remove duplicates from the list of killed groups."
11732   (interactive)
11733   (let ((killed gnus-killed-list))
11734     (while killed
11735       (gnus-message 9 "%d" (length killed))
11736       (setcdr killed (delete (car killed) (cdr killed)))
11737       (setq killed (cdr killed)))))
11738
11739 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11740 ;; and compute how many unread articles there are in each group.
11741 (defun gnus-get-unread-articles (&optional level) 
11742   (let* ((newsrc (cdr gnus-newsrc-alist))
11743          (conditional level)
11744          (level (or level (1+ gnus-level-subscribed)))
11745          (foreign-level
11746           (min 
11747            (cond ((and gnus-activate-foreign-newsgroups 
11748                        (not (numberp gnus-activate-foreign-newsgroups)))
11749                   (1+ gnus-level-subscribed))
11750                  ((numberp gnus-activate-foreign-newsgroups)
11751                   gnus-activate-foreign-newsgroups)
11752                  (t 0))
11753            level))
11754          info group active virtuals method)
11755     (gnus-message 5 "Checking new news...")
11756
11757     (while newsrc
11758       (setq info (car newsrc)
11759             group (car info)
11760             active (gnus-gethash group gnus-active-hashtb))
11761
11762       ;; Check newsgroups. If the user doesn't want to check them, or
11763       ;; they can't be checked (for instance, if the news server can't
11764       ;; be reached) we just set the number of unread articles in this
11765       ;; newsgroup to t. This means that Gnus thinks that there are
11766       ;; unread articles, but it has no idea how many.
11767       (if (and (setq method (nth 4 info))
11768                (not (gnus-server-equal gnus-select-method
11769                                        (gnus-server-get-method nil method)))
11770                (not (gnus-secondary-method-p method)))
11771           ;; These groups are foreign. Check the level.
11772           (if (<= (nth 1 info) foreign-level)
11773               (if (eq (car (if (stringp method) 
11774                                (gnus-server-to-method method)
11775                              (nth 4 info))) 'nnvirtual)
11776                   ;; We have to activate the virtual groups after all
11777                   ;; the others, so we just pop them on a list for
11778                   ;; now. 
11779                   (setq virtuals (cons info virtuals))
11780                 (and (setq active (gnus-activate-newsgroup (car info)))
11781                      ;; Close the groups as we look at them!
11782                      (gnus-close-group group))))
11783                 
11784         ;; These groups are native or secondary. 
11785         (if (and (not gnus-have-read-active-file)
11786                  (<= (nth 1 info) level))
11787             (setq active (gnus-activate-newsgroup (car info)))))
11788       
11789       (if active
11790           (gnus-get-unread-articles-in-group info active)
11791         ;; The group couldn't be reached, so we nix out the number of
11792         ;; unread articles and stuff.
11793         (gnus-sethash group nil gnus-active-hashtb)
11794         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11795
11796       (setq newsrc (cdr newsrc)))
11797
11798     ;; Activate the virtual groups. This has to be done after all the
11799     ;; other groups. 
11800     ;; !!! If one virtual group contains another virtual group, even
11801     ;; doing it this way might cause problems.
11802    (while virtuals
11803       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11804            (gnus-get-unread-articles-in-group (car virtuals) active))
11805       (setq virtuals (cdr virtuals)))
11806
11807     (gnus-message 5 "Checking new news...done")))
11808
11809 ;; Create a hash table out of the newsrc alist. The `car's of the
11810 ;; alist elements are used as keys.
11811 (defun gnus-make-hashtable-from-newsrc-alist ()
11812   (let ((alist gnus-newsrc-alist)
11813         (ohashtb gnus-newsrc-hashtb)
11814         prev)
11815     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11816     (setq alist 
11817           (setq prev (setq gnus-newsrc-alist 
11818                            (if (equal (car (car gnus-newsrc-alist))
11819                                       "dummy.group")
11820                                gnus-newsrc-alist
11821                              (cons (list "dummy.group" 0 nil) alist)))))
11822     (while alist
11823       (gnus-sethash (car (car alist)) 
11824                     (cons (and ohashtb (car (gnus-gethash 
11825                                              (car (car alist)) ohashtb))) 
11826                           prev) gnus-newsrc-hashtb)
11827       (setq prev alist
11828             alist (cdr alist)))))
11829
11830 (defun gnus-make-hashtable-from-killed ()
11831   "Create a hash table from the killed and zombie lists."
11832   (let ((lists '(gnus-killed-list gnus-zombie-list))
11833         list)
11834     (setq gnus-killed-hashtb 
11835           (gnus-make-hashtable 
11836            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11837     (while lists
11838       (setq list (symbol-value (car lists)))
11839       (setq lists (cdr lists))
11840       (while list
11841         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11842         (setq list (cdr list))))))
11843
11844 (defun gnus-get-unread-articles-in-group (info active)
11845   (let* ((range (nth 2 info))
11846          (num 0)
11847          (marked (nth 3 info))
11848          srange lowest group highest)
11849     ;; If a cache is present, we may have to alter the active info.
11850     (and gnus-use-cache
11851          (gnus-cache-possibly-alter-active (car info) active))
11852     ;; Modify the list of read articles according to what articles 
11853     ;; are available; then tally the unread articles and add the
11854     ;; number to the group hash table entry.
11855     (cond ((zerop (cdr active))
11856            (setq num 0))
11857           ((not range)
11858            (setq num (- (1+ (cdr active)) (car active))))
11859           ((not (listp (cdr range)))
11860            ;; Fix a single (num . num) range according to the
11861            ;; active hash table.
11862            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11863            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11864            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11865            ;; Compute number of unread articles.
11866            (setq num (max 0 (- (cdr active) 
11867                                (- (1+ (cdr range)) (car range))))))
11868           (t
11869            ;; The read list is a list of ranges. Fix them according to
11870            ;; the active hash table.
11871            ;; First peel off any elements that are below the lower
11872            ;; active limit. 
11873            (while (and (cdr range) 
11874                        (>= (car active) 
11875                            (or (and (atom (car (cdr range))) (car (cdr range)))
11876                                (car (car (cdr range))))))
11877              (if (numberp (car range))
11878                  (setcar range 
11879                          (cons (car range) 
11880                                (or (and (numberp (car (cdr range)))
11881                                         (car (cdr range))) 
11882                                    (cdr (car (cdr range))))))
11883                (setcdr (car range) 
11884                        (or (and (numberp (nth 1 range)) (nth 1 range))
11885                            (cdr (car (cdr range))))))
11886              (setcdr range (cdr (cdr range))))
11887            ;; Adjust the first element to be the same as the lower limit. 
11888            (if (and (not (atom (car range))) 
11889                     (< (cdr (car range)) (car active)))
11890                (setcdr (car range) (1- (car active))))
11891            ;; Then we want to peel off any elements that are higher
11892            ;; than the upper active limit.  
11893            (let ((srange range))
11894              ;; Go past all legal elements.
11895              (while (and (cdr srange) 
11896                          (<= (or (and (atom (car (cdr srange)))
11897                                       (car (cdr srange)))
11898                                  (car (car (cdr srange)))) (cdr active)))
11899                (setq srange (cdr srange)))
11900              (if (cdr srange)
11901                  ;; Nuke all remaining illegal elements.
11902                  (setcdr srange nil))
11903
11904              ;; Adjust the final element.
11905              (if (and (not (atom (car srange)))
11906                       (> (cdr (car srange)) (cdr active)))
11907                  (setcdr (car srange) (cdr active))))
11908            ;; Compute the number of unread articles.
11909            (while range
11910              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11911                                          (cdr (car range))))
11912                                  (or (and (atom (car range)) (car range))
11913                                      (car (car range))))))
11914              (setq range (cdr range)))
11915            (setq num (max 0 (- (cdr active) num)))))
11916     (and info
11917          (progn
11918            (and (assq 'tick marked)
11919                 (inline (gnus-remove-illegal-marked-articles
11920                          (assq 'tick marked) (nth 2 info))))
11921            (and (assq 'dormant marked)
11922                 (inline (gnus-remove-illegal-marked-articles
11923                          (assq 'dormant marked) (nth 2 info))))
11924            (setcar
11925             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11926             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11927                                 (length (cdr (assq 'dormant marked)))))))))
11928     num))
11929
11930 (defun gnus-remove-illegal-marked-articles (marked ranges)
11931   (let ((m (cdr marked)))
11932     ;; Make sure that all ticked articles are a subset of the unread
11933     ;; articles. 
11934     (while m
11935       (if (gnus-member-of-range (car m) ranges)
11936           (setcdr marked (cdr m))
11937         (setq marked m))
11938       (setq m (cdr m)))))
11939
11940 (defun gnus-activate-newsgroup (group)
11941   (let ((method (gnus-find-method-for-group group))
11942         active)
11943     (and (or (gnus-server-opened method) (gnus-open-server method))
11944          (gnus-request-group group)
11945          (save-excursion
11946            (set-buffer nntp-server-buffer)
11947            (goto-char (point-min))
11948            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11949                 (progn
11950                   (goto-char (match-beginning 1))
11951                   (gnus-sethash 
11952                    group (setq active (cons (read (current-buffer))
11953                                             (read (current-buffer))))
11954                    gnus-active-hashtb))
11955                 active)))))
11956
11957 (defun gnus-update-read-articles 
11958   (group unread unselected ticked &optional domarks replied expirable killed
11959          dormant bookmark score)
11960   "Update the list of read and ticked articles in GROUP using the
11961 UNREAD and TICKED lists.
11962 Note: UNSELECTED has to be sorted over `<'.
11963 Returns whether the updating was successful."
11964   (let* ((active (or gnus-newsgroup-active 
11965                      (gnus-gethash group gnus-active-hashtb)))
11966          (entry (gnus-gethash group gnus-newsrc-hashtb))
11967          (number (car entry))
11968          (info (nth 2 entry))
11969          (marked (nth 3 info))
11970          (prev 1)
11971          (unread (sort (copy-sequence unread) (function <)))
11972          last read)
11973     (if (or (not info) (not active))
11974         ;; There is no info on this group if it was, in fact,
11975         ;; killed. Gnus stores no information on killed groups, so
11976         ;; there's nothing to be done. 
11977         ;; One could store the information somewhere temporarily,
11978         ;; perhaps... Hmmm... 
11979         ()
11980       ;; Remove any negative articles numbers.
11981       (while (and unread (< (car unread) 0))
11982         (setq unread (cdr unread)))
11983       ;; Remove any expired article numbers
11984       (while (and unread (< (car unread) (car active)))
11985         (setq unread (cdr unread)))
11986       (while (and ticked (< (car ticked) (car active)))
11987         (setq ticked (cdr ticked)))
11988       (while (and dormant (< (car dormant) (car active)))
11989         (setq dormant (cdr dormant)))
11990       (setq unread (sort (append unselected unread) '<))
11991       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11992       (setcar entry (max 0 (- (length unread) (length ticked) 
11993                               (length dormant))))
11994       ;; Compute the ranges of read articles by looking at the list of
11995       ;; unread articles.  
11996       (while unread
11997         (if (/= (car unread) prev)
11998             (setq read (cons (if (= prev (1- (car unread))) prev
11999                                (cons prev (1- (car unread)))) read)))
12000         (setq prev (1+ (car unread)))
12001         (setq unread (cdr unread)))
12002       (if (<= prev (cdr active))
12003           (setq read (cons (cons prev (cdr active)) read)))
12004       ;; Enter this list into the group info.
12005       (setcar (cdr (cdr info)) 
12006               (if (> (length read) 1) (nreverse read) read))
12007       ;; Enter the list of ticked articles.
12008       (gnus-set-marked-articles 
12009        info ticked
12010        (if domarks replied (cdr (assq 'reply marked)))
12011        (if domarks expirable (cdr (assq 'expire marked)))
12012        (if domarks killed (cdr (assq 'killed marked)))
12013        (if domarks dormant (cdr (assq 'dormant marked)))
12014        (if domarks bookmark (cdr (assq 'bookmark marked)))
12015        (if domarks score (cdr (assq 'score marked))))
12016       t)))
12017
12018 (defun gnus-make-articles-unread (group articles)
12019   "Mark ARTICLES in GROUP as unread."
12020   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12021                           (gnus-gethash (gnus-group-real-name group)
12022                                         gnus-newsrc-hashtb))))
12023          (ranges (nth 2 info))
12024          news)
12025     (while articles
12026       (and (gnus-member-of-range (car articles) ranges)
12027            (setq news (cons (car articles) news)))
12028       (setq articles (cdr articles)))
12029     (if (not news)
12030         ()
12031       (setcar (nthcdr 2 info)
12032               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12033       (gnus-group-update-group group t))))
12034
12035 ;; Get the active file(s) from the backend(s).
12036 (defun gnus-read-active-file ()
12037   (gnus-group-set-mode-line)
12038   (let ((methods (if (gnus-server-opened gnus-select-method)
12039                      ;; The native server is available.
12040                      (cons gnus-select-method gnus-secondary-select-methods)
12041                    ;; The native server is down, so we just do the
12042                    ;; secondary ones.   
12043                    gnus-secondary-select-methods))
12044         list-type)
12045     (setq gnus-have-read-active-file nil)
12046     (save-excursion
12047       (set-buffer nntp-server-buffer)
12048       (while methods
12049         (let* ((where (nth 1 (car methods)))
12050                (mesg (format "Reading active file%s via %s..."
12051                              (if (and where (not (zerop (length where))))
12052                                  (concat " from " where) "")
12053                              (car (car methods)))))
12054           (gnus-message 5 mesg)
12055           (gnus-check-news-server (car methods))
12056           (cond 
12057            ((and (eq gnus-read-active-file 'some)
12058                  (gnus-check-backend-function
12059                   'retrieve-groups (car (car methods))))
12060             (let ((newsrc (cdr gnus-newsrc-alist))
12061                   groups)
12062               (while newsrc
12063                 (and (gnus-server-equal 
12064                       (gnus-find-method-for-group
12065                        (car (car newsrc)) (car newsrc))
12066                       (gnus-server-get-method nil (car methods)))
12067                      (setq groups (cons (car (car newsrc)) groups)))
12068                 (setq newsrc (cdr newsrc)))
12069               (setq list-type (gnus-retrieve-groups groups (car methods)))
12070               (cond ((not list-type)
12071                      (gnus-message 
12072                       1 "Cannot read partial active file from %s server." 
12073                       (car (car methods)))
12074                      (ding)
12075                      (sit-for 2))
12076                     ((eq list-type 'active)
12077                      (gnus-active-to-gnus-format (car methods)))
12078                     (t
12079                      (gnus-groups-to-gnus-format (car methods))))))
12080            (t
12081             (if (not (gnus-request-list (car methods)))
12082                 (progn
12083                   (gnus-message 1 "Cannot read active file from %s server." 
12084                                 (car (car methods)))
12085                   (ding))
12086               (gnus-active-to-gnus-format (car methods))
12087               (setq gnus-have-read-active-file t)
12088               (gnus-message 5 "%sdone" mesg)))))
12089         (setq methods (cdr methods))))))
12090
12091 ;; Read an active file and place the results in `gnus-active-hashtb'.
12092 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12093   (let ((cur (current-buffer))
12094         (hashtb (or hashtb 
12095                     (if (and gnus-active-hashtb 
12096                              (not (equal method gnus-select-method)))
12097                         gnus-active-hashtb
12098                       (setq gnus-active-hashtb
12099                             (if (equal method gnus-select-method)
12100                                 (gnus-make-hashtable 
12101                                  (count-lines (point-min) (point-max)))
12102                               (gnus-make-hashtable 4096)))))))
12103     ;; Delete unnecessary lines.
12104     (goto-char (point-min))
12105     (while (search-forward "\nto." nil t)
12106       (delete-region (1+ (match-beginning 0)) 
12107                      (progn (forward-line 1) (point))))
12108     (or (string= gnus-ignored-newsgroups "")
12109         (progn
12110           (goto-char (point-min))
12111           (delete-matching-lines gnus-ignored-newsgroups)))
12112     ;; If these are groups from a foreign select method, we insert the
12113     ;; group prefix in front of the group names. 
12114     (and method (not (eq method gnus-select-method))
12115          (let ((prefix (gnus-group-prefixed-name "" method)))
12116            (goto-char (point-min))
12117            (while (and (not (eobp))
12118                        (progn (insert prefix)
12119                               (zerop (forward-line 1)))))))
12120     (goto-char (point-min))
12121     ;; Store active file in hashtable.
12122     (goto-char (point-min))
12123     (if (string-match "%[oO]" gnus-group-line-format)
12124         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12125         ;; If we want information on moderated groups, we use this
12126         ;; loop...   
12127         (let* ((mod-hashtb (make-vector 7 0))
12128                (m (intern "m" mod-hashtb))
12129                group max mod min)
12130           (while (not (eobp))
12131             (condition-case nil
12132                 (progn
12133                   (narrow-to-region (point) (gnus-point-at-eol))
12134                   (setq group (let ((obarray hashtb)) (read cur)))
12135                   (if (and (numberp (setq max (read cur)))
12136                            (numberp (setq min (read cur))))
12137                       (set group (cons min max))
12138                     (set group nil))
12139                   ;; Enter moderated groups into a list.
12140                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12141                       (setq gnus-moderated-list 
12142                             (cons (symbol-name group) gnus-moderated-list))))
12143               (error nil))
12144             (widen)
12145             (forward-line 1)))
12146       ;; And if we do not care about moderation, we use this loop,
12147       ;; which is faster.
12148       (let (group max min)
12149         (while (not (eobp))
12150           (condition-case ()
12151               (progn
12152                 (narrow-to-region (point) (gnus-point-at-eol))
12153                 ;; group gets set to a symbol interned in the hash table
12154                 ;; (what a hack!!)
12155                 (setq group (let ((obarray hashtb)) (read cur)))
12156                 (if (and (numberp (setq max (read cur)))
12157                          (numberp (setq min (read cur))))
12158                     (set group (cons min max))
12159                   (set group nil)))
12160             (error 
12161              (progn 
12162                (if ignore-errors
12163                    (set group nil)
12164                  (ding) 
12165                  (gnus-message 3 "Warning - illegal active: %s"
12166                                (buffer-substring 
12167                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12168                  nil))))
12169           (widen)
12170           (forward-line 1))))))
12171
12172 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12173   ;; Parse a "groups" active file.
12174   (let ((cur (current-buffer))
12175         (hashtb (or hashtb 
12176                     (if method
12177                         gnus-active-hashtb
12178                       (setq gnus-active-hashtb
12179                             (gnus-make-hashtable 
12180                              (count-lines (point-min) (point-max)))))))
12181         (prefix (and method (not (eq method gnus-select-method))
12182                      (gnus-group-prefixed-name "" method))))
12183
12184     (goto-char (point-min))
12185     (condition-case ()
12186         ;; We split this into to separate loops, one with the prefix
12187         ;; and one without to speed the reading up somewhat.
12188         (if prefix
12189             (let (min max opoint)
12190               (while (not (eobp))
12191                 (read cur) (read cur)
12192                 (setq min (read cur)
12193                       max (read cur)
12194                       opoint (point))
12195                 (skip-chars-forward " \t")
12196                 (insert prefix)
12197                 (goto-char opoint)
12198                 (set (let ((obarray hashtb)) (read cur)) 
12199                      (cons min max))
12200                 (forward-line 1)))
12201           (let (min max opoint)
12202             (while (not (eobp))
12203               (if (= (following-char) ?2)
12204                   (progn
12205                     (read cur) (read cur)
12206                     (setq min (read cur)
12207                           max (read cur))
12208                     (set (let ((obarray hashtb)) (read cur)) 
12209                          (cons min max))))
12210               (forward-line 1))))
12211       (error 
12212        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12213
12214 (defun gnus-read-newsrc-file (&optional force)
12215   "Read startup file.
12216 If FORCE is non-nil, the .newsrc file is read."
12217   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12218   ;; Reset variables that might be defined in the .newsrc.eld file.
12219   (let ((variables gnus-variable-list))
12220     (while variables
12221       (set (car variables) nil)
12222       (setq variables (cdr variables))))
12223   (let* ((newsrc-file gnus-current-startup-file)
12224          (quick-file (concat newsrc-file ".el")))
12225     (save-excursion
12226       ;; We always load the .newsrc.eld file. If always contains
12227       ;; much information that can not be gotten from the .newsrc
12228       ;; file (ticked articles, killed groups, foreign methods, etc.)
12229       (gnus-read-newsrc-el-file quick-file)
12230  
12231       (if (or force
12232               (and (file-newer-than-file-p newsrc-file quick-file)
12233                    (file-newer-than-file-p newsrc-file 
12234                                            (concat quick-file "d")))
12235               (not gnus-newsrc-alist))
12236           ;; We read the .newsrc file. Note that if there if a
12237           ;; .newsrc.eld file exists, it has already been read, and
12238           ;; the `gnus-newsrc-hashtb' has been created. While reading
12239           ;; the .newsrc file, Gnus will only use the information it
12240           ;; can find there for changing the data already read -
12241           ;; ie. reading the .newsrc file will not trash the data
12242           ;; already read (except for read articles).
12243           (save-excursion
12244             (gnus-message 5 "Reading %s..." newsrc-file)
12245             (set-buffer (find-file-noselect newsrc-file))
12246             (buffer-disable-undo (current-buffer))
12247             (gnus-newsrc-to-gnus-format)
12248             (kill-buffer (current-buffer))
12249             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12250
12251 (defun gnus-read-newsrc-el-file (file)
12252   (let ((ding-file (concat file "d")))
12253     ;; We always, always read the .eld file.
12254     (gnus-message 5 "Reading %s..." ding-file)
12255     (let (gnus-newsrc-assoc)
12256       (condition-case nil
12257           (load ding-file t t t)
12258         (error nil))
12259       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12260     (let ((inhibit-quit t))
12261       (gnus-uncompress-newsrc-alist))
12262     (gnus-make-hashtable-from-newsrc-alist)
12263     (if (not (file-newer-than-file-p file ding-file))
12264         ()
12265       ;; Old format quick file
12266       (gnus-message 5 "Reading %s..." file)
12267       ;; The .el file is newer than the .eld file, so we read that one
12268       ;; as well. 
12269       (gnus-read-old-newsrc-el-file file))))
12270
12271 ;; Parse the old-style quick startup file
12272 (defun gnus-read-old-newsrc-el-file (file)
12273   (let (newsrc killed marked group g m len info)
12274     (prog1
12275         (let ((gnus-killed-assoc nil)
12276               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12277           (prog1
12278               (condition-case nil
12279                   (load file t t t)
12280                 (error nil))
12281             (setq newsrc gnus-newsrc-assoc
12282                   killed gnus-killed-assoc
12283                   marked gnus-marked-assoc)))
12284       (setq gnus-newsrc-alist nil)
12285       (while newsrc
12286         (setq group (car newsrc))
12287         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12288           (if info
12289               (progn
12290                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12291                 (setcar (cdr info)
12292                         (if (nth 1 group) gnus-level-default-subscribed 
12293                           gnus-level-default-unsubscribed))
12294                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12295             (setq gnus-newsrc-alist
12296                   (cons 
12297                    (setq info
12298                          (list (car group)
12299                                (if (nth 1 group) gnus-level-default-subscribed
12300                                  gnus-level-default-unsubscribed) 
12301                                (cdr (cdr group))))
12302                    gnus-newsrc-alist)))
12303           (if (setq m (assoc (car group) marked))
12304             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12305         (setq newsrc (cdr newsrc)))
12306       (setq newsrc killed)
12307       (while newsrc
12308         (setcar newsrc (car (car newsrc)))
12309         (setq newsrc (cdr newsrc)))
12310       (setq gnus-killed-list killed))
12311     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12312     (gnus-make-hashtable-from-newsrc-alist)))
12313       
12314 (defun gnus-make-newsrc-file (file)
12315   "Make server dependent file name by catenating FILE and server host name."
12316   (let* ((file (expand-file-name file nil))
12317          (real-file (concat file "-" (nth 1 gnus-select-method))))
12318     (if (file-exists-p real-file)
12319         real-file file)))
12320
12321 (defun gnus-uncompress-newsrc-alist ()
12322   ;; Uncompress all lists of marked articles in the newsrc assoc.
12323   (let ((newsrc gnus-newsrc-alist)
12324         marked)
12325     (while newsrc
12326       (if (not (setq marked (nth 3 (car newsrc))))
12327           ()
12328         (while marked
12329           (or (eq 'score (car (car marked)))
12330               (eq 'bookmark (car (car marked)))
12331               (eq 'killed (car (car marked)))
12332               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12333           (setq marked (cdr marked))))
12334       (setq newsrc (cdr newsrc)))))
12335
12336 (defun gnus-compress-newsrc-alist ()
12337   ;; Compress all lists of marked articles in the newsrc assoc.
12338   (let ((newsrc gnus-newsrc-alist)
12339         marked)
12340     (while newsrc
12341       (if (not (setq marked (nth 3 (car newsrc))))
12342           ()
12343         (while marked
12344           (or (eq 'score (car (car marked)))
12345               (eq 'bookmark (car (car marked)))
12346               (eq 'killed (car (car marked)))
12347               (setcdr (car marked) 
12348                       (condition-case ()
12349                           (gnus-compress-sequence 
12350                            (sort (cdr (car marked)) '<) t)
12351                         (error (cdr (car marked))))))
12352           (setq marked (cdr marked))))
12353       (setq newsrc (cdr newsrc)))))
12354
12355 (defun gnus-newsrc-to-gnus-format ()
12356   (setq gnus-newsrc-options "")
12357   (setq gnus-newsrc-options-n nil)
12358
12359   (or gnus-active-hashtb
12360       (setq gnus-active-hashtb (make-vector 4095 0)))
12361   (let ((buf (current-buffer))
12362         (already-read (> (length gnus-newsrc-alist) 1))
12363         group level subscribed info options-symbol newsrc
12364         symbol reads num1)
12365     (goto-char (point-min))
12366     ;; We intern the symbol `options' in the active hashtb so that we
12367     ;; can `eq' against it later.
12368     (setq options-symbol (intern "options" gnus-active-hashtb))
12369   
12370     (while (not (eobp))
12371       ;; We first read the first word on the line by narrowing and
12372       ;; then reading into `gnus-active-hashtb'.  Most groups will
12373       ;; already exist in that hashtb, so this will save some string
12374       ;; space.
12375       (narrow-to-region
12376        (point)
12377        (progn (skip-chars-forward "^ \t!:\n") (point)))
12378       (goto-char (point-min))
12379       (setq symbol 
12380             (and (/= (point-min) (point-max))
12381                  (let ((obarray gnus-active-hashtb)) (read buf))))
12382       (widen)
12383       ;; Now, the symbol we have read is either `options' or a group
12384       ;; name.  If it is an options line, we just add it to a string. 
12385       (cond 
12386        ((eq symbol options-symbol)
12387         (setq gnus-newsrc-options
12388               ;; This concatting is quite inefficient, but since our
12389               ;; thorough studies show that approx 99.37% of all
12390               ;; .newsrc files only contain a single options line, we
12391               ;; don't give a damn, frankly, my dear.
12392               (concat gnus-newsrc-options
12393                       (buffer-substring 
12394                        (gnus-point-at-bol)
12395                        ;; Options may continue on the next line.
12396                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12397                                 (progn (beginning-of-line) (point)))
12398                            (point))))))
12399        (symbol
12400         ;; It was a group name.
12401         (setq subscribed (= (following-char) ?:)
12402               group (symbol-name symbol)
12403               reads nil)
12404         (if (eolp)
12405             ;; If the line ends here, this is clearly a buggy line, so
12406             ;; we put point a the beginning of line and let the cond
12407             ;; below do the error handling.
12408             (beginning-of-line)
12409           ;; We skip to the beginning of the ranges.
12410           (skip-chars-forward "!: \t"))
12411         ;; We are now at the beginning of the list of read articles.
12412         ;; We read them range by range.
12413         (while
12414             (cond 
12415              ((looking-at "[0-9]+")
12416               ;; We narrow and read a number instead of buffer-substring/
12417               ;; string-to-int because it's faster. narrow/widen is
12418               ;; faster than save-restriction/narrow, and save-restriction
12419               ;; produces a garbage object.
12420               (setq num1 (progn
12421                            (narrow-to-region (match-beginning 0) (match-end 0))
12422                            (read buf)))
12423               (widen)
12424               ;; If the next character is a dash, then this is a range.
12425               (if (= (following-char) ?-)
12426                   (progn
12427                     ;; We read the upper bound of the range.
12428                     (forward-char 1)
12429                     (if (not (looking-at "[0-9]+"))
12430                         ;; This is a buggy line, by we pretend that
12431                         ;; it's kinda OK. Perhaps the user should be
12432                         ;; dinged? 
12433                         (setq reads (cons num1 reads))
12434                       (setq reads 
12435                             (cons 
12436                              (cons num1 (progn
12437                                           (narrow-to-region (match-beginning 0) 
12438                                                             (match-end 0))
12439                                           (read buf)))
12440                              reads))
12441                       (widen)))
12442                 ;; It was just a simple number, so we add it to the
12443                 ;; list of ranges.
12444                 (setq reads (cons num1 reads)))
12445               ;; If the next char in ?\n, then we have reached the end
12446               ;; of the line and return nil.
12447               (/= (following-char) ?\n))
12448              ((= (following-char) ?\n)
12449               ;; End of line, so we end.
12450               nil)
12451              (t
12452               ;; Not numbers and not eol, so this might be a buggy
12453               ;; line... 
12454               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12455                   (progn
12456                     ;; The line was buggy.
12457                     (setq group nil)
12458                     (gnus-message 3 "Mangled line: %s" 
12459                                   (buffer-substring (gnus-point-at-bol) 
12460                                                     (gnus-point-at-eol)))
12461                     (ding)
12462                     (sit-for 1)))
12463               nil))
12464           ;; Skip past ", ". Spaces are illegal in these ranges, but
12465           ;; we allow them, because it's a common mistake to put a
12466           ;; space after the comma.
12467           (skip-chars-forward ", "))
12468
12469         ;; We have already read .newsrc.eld, so we gently update the
12470         ;; data in the hash table with the information we have just
12471         ;; read. 
12472         (if (not group)
12473             ()
12474           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12475                 level)
12476             (if info
12477                 ;; There is an entry for this file in the alist.
12478                 (progn
12479                   (setcar (nthcdr 2 info) (nreverse reads))
12480                   ;; We update the level very gently.  In fact, we
12481                   ;; only change it if there's been a status change
12482                   ;; from subscribed to unsubscribed, or vice versa.
12483                   (setq level (nth 1 info))
12484                   (cond ((and (<= level gnus-level-subscribed)
12485                               (not subscribed))
12486                          (setq level (if reads
12487                                          gnus-level-default-unsubscribed 
12488                                        (1+ gnus-level-default-unsubscribed))))
12489                         ((and (> level gnus-level-subscribed) subscribed)
12490                          (setq level gnus-level-default-subscribed)))
12491                   (setcar (cdr info) level))
12492               ;; This is a new group.
12493               (setq info (list group 
12494                                (if subscribed
12495                                    gnus-level-default-subscribed 
12496                                  (if reads
12497                                      (1+ gnus-level-subscribed)
12498                                    gnus-level-default-unsubscribed))
12499                                (nreverse reads))))
12500             (setq newsrc (cons info newsrc))))))
12501       (forward-line 1))
12502     
12503     (setq newsrc (nreverse newsrc))
12504
12505     (if (not already-read)
12506         ()
12507       ;; We now have two newsrc lists - `newsrc', which is what we
12508       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12509       ;; what we've read from .newsrc.eld. We have to merge these
12510       ;; lists. We do this by "attaching" any (foreign) groups in the
12511       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12512       (let ((rc (cdr gnus-newsrc-alist))
12513             (prev gnus-newsrc-alist)
12514             entry mentry)
12515         (while rc
12516           (or (null (nth 4 (car rc))) ; It's a native group.
12517               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12518               (if (setq entry (assoc (car (car prev)) newsrc))
12519                   (setcdr (setq mentry (memq entry newsrc))
12520                           (cons (car rc) (cdr mentry)))
12521                 (setq newsrc (cons (car rc) newsrc))))
12522           (setq prev rc
12523                 rc (cdr rc)))))
12524
12525     (setq gnus-newsrc-alist newsrc)
12526     ;; We make the newsrc hashtb.
12527     (gnus-make-hashtable-from-newsrc-alist)
12528
12529     ;; Finally, if we read some options lines, we parse them.
12530     (or (string= gnus-newsrc-options "")
12531         (gnus-newsrc-parse-options gnus-newsrc-options))))
12532
12533 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12534 ;; The return value will be a list on the form
12535 ;; ((regexp1 . ignore)
12536 ;;  (regexp2 . subscribe)...)
12537 ;; When handling new newsgroups, groups that match a `ignore' regexp
12538 ;; will be ignored, and groups that match a `subscribe' regexp will be
12539 ;; subscribed. A line like
12540 ;; options -n !all rec.all
12541 ;; will lead to a list that looks like
12542 ;; (("^rec\\..+" . subscribe) 
12543 ;;  ("^.+" . ignore))
12544 ;; So all "rec.*" groups will be subscribed, while all the other
12545 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12546 ;; different from "options -n rec.all !all". 
12547 (defun gnus-newsrc-parse-options (options)
12548   (let (out eol)
12549     (save-excursion
12550       (gnus-set-work-buffer)
12551       (insert (regexp-quote options))
12552       ;; First we treat all continuation lines.
12553       (goto-char (point-min))
12554       (while (re-search-forward "\n[ \t]+" nil t)
12555         (replace-match " " t t))
12556       ;; Then we transform all "all"s into ".+"s.
12557       (goto-char (point-min))
12558       (while (re-search-forward "\\ball\\b" nil t)
12559         (replace-match ".+" t t))
12560       (goto-char (point-min))
12561       ;; We remove all other options than the "-n" ones.
12562       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12563         (replace-match " ")
12564         (forward-char -1))
12565       (goto-char (point-min))
12566
12567       ;; We are only interested in "options -n" lines - we
12568       ;; ignore the other option lines.
12569       (while (re-search-forward "[ \t]-n" nil t)
12570         (setq eol 
12571               (or (save-excursion
12572                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12573                          (- (point) 2)))
12574                   (gnus-point-at-eol)))
12575         ;; Search for all "words"...
12576         (while (re-search-forward "[^ \t,\n]+" eol t)
12577           (if (= (char-after (match-beginning 0)) ?!)
12578               ;; If the word begins with a bang (!), this is a "not"
12579               ;; spec. We put this spec (minus the bang) and the
12580               ;; symbol `ignore' into the list.
12581               (setq out (cons (cons (concat 
12582                                      "^" (buffer-substring 
12583                                           (1+ (match-beginning 0))
12584                                           (match-end 0)))
12585                                     'ignore) out))
12586             ;; There was no bang, so this is a "yes" spec.
12587             (setq out (cons (cons (concat 
12588                                    "^" (buffer-substring (match-beginning 0)
12589                                                          (match-end 0)))
12590                                   'subscribe) out)))))
12591     
12592       (setq gnus-newsrc-options-n out))))
12593                
12594
12595 (defun gnus-save-newsrc-file ()
12596   "Save .newsrc file."
12597   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12598   ;; from the variable gnus-newsrc-alist.
12599   (and (or gnus-newsrc-alist gnus-killed-list)
12600        gnus-current-startup-file
12601        (let ((make-backup-files t)
12602              (version-control nil)
12603              (require-final-newline t)) ;Don't ask even if requested.
12604          ;; You can stop or change version control of backup file.
12605          ;; Suggested by jason@violet.berkeley.edu.
12606          (run-hooks 'gnus-save-newsrc-hook)
12607          (save-excursion
12608            (if (or (not gnus-dribble-buffer)
12609                    (not (buffer-name gnus-dribble-buffer))
12610                    (zerop (save-excursion
12611                             (set-buffer gnus-dribble-buffer)
12612                             (buffer-size))))
12613                (gnus-message 4 "(No changes need to be saved)")
12614              (if gnus-save-newsrc-file
12615                  (progn
12616                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12617                    ;; Make backup file of master newsrc.
12618                    (gnus-gnus-to-newsrc-format)
12619                    (gnus-message 5 "Saving %s...done"
12620                                  gnus-current-startup-file)))
12621              ;; Quickly loadable .newsrc.
12622              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12623              (gnus-add-current-to-buffer-list)
12624              (buffer-disable-undo (current-buffer))
12625              (erase-buffer)
12626              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12627              (gnus-gnus-to-quick-newsrc-format)
12628              (write-region 1 (point-max) 
12629                            (concat gnus-current-startup-file ".eld") 
12630                            nil 'nomesg)
12631              (kill-buffer (current-buffer))
12632              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12633              (gnus-dribble-delete-file))))))
12634
12635 (defun gnus-gnus-to-quick-newsrc-format ()
12636   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12637   (insert ";; (ding) Gnus startup file.\n")
12638   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12639   (insert ";; to read .newsrc.\n")
12640   (let ((variables gnus-variable-list)
12641         (inhibit-quit t)
12642         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12643         variable)
12644     ;; insert lisp expressions.
12645     (gnus-compress-newsrc-alist)
12646     (while variables
12647       (setq variable (car variables))
12648       (and (boundp variable)
12649            (symbol-value variable)
12650            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12651            (insert "(setq " (symbol-name variable) " '"
12652                    (prin1-to-string (symbol-value variable))
12653                    ")\n"))
12654       (setq variables (cdr variables)))
12655     (gnus-uncompress-newsrc-alist)))
12656
12657
12658 (defun gnus-gnus-to-newsrc-format ()
12659   ;; Generate and save the .newsrc file.
12660   (let ((newsrc (cdr gnus-newsrc-alist))
12661         info ranges range)
12662     (save-excursion
12663       (set-buffer (create-file-buffer gnus-startup-file))
12664       (buffer-disable-undo (current-buffer))
12665       (erase-buffer)
12666       ;; Write options.
12667       (if gnus-newsrc-options (insert gnus-newsrc-options))
12668       ;; Write subscribed and unsubscribed.
12669       (while newsrc
12670         (setq info (car newsrc))
12671         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12672             (progn
12673               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12674                                      "!" ":"))
12675               (if (setq ranges (nth 2 info))
12676                   (progn
12677                     (insert " ")
12678                     (if (not (listp (cdr ranges)))
12679                         (if (= (car ranges) (cdr ranges))
12680                             (insert (int-to-string (car ranges)))
12681                           (insert (int-to-string (car ranges)) "-" 
12682                                   (int-to-string (cdr ranges))))
12683                       (while ranges
12684                         (setq range (car ranges)
12685                               ranges (cdr ranges))
12686                         (if (or (atom range) (= (car range) (cdr range)))
12687                             (insert (int-to-string 
12688                                      (or (and (atom range) range) 
12689                                          (car range))))
12690                           (insert (int-to-string (car range)) "-"
12691                                   (int-to-string (cdr range))))
12692                         (if ranges (insert ","))))))
12693               (insert "\n")))
12694         (setq newsrc (cdr newsrc)))
12695       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12696       (kill-buffer (current-buffer)))))
12697
12698 (defun gnus-read-all-descriptions-files ()
12699   (let ((methods (nconc (list gnus-select-method) 
12700                         gnus-secondary-select-methods)))
12701     (while methods
12702       (gnus-read-descriptions-file (car methods))
12703       (setq methods (cdr methods)))
12704     t))
12705
12706 (defun gnus-read-descriptions-file (&optional method)
12707   (let ((method (or method gnus-select-method)))
12708     ;; We create the hashtable whether we manage to read the desc file
12709     ;; to avoid trying to re-read after a failed read.
12710     (or gnus-description-hashtb
12711         (setq gnus-description-hashtb 
12712               (gnus-make-hashtable (length gnus-active-hashtb))))
12713     ;; Mark this method's desc file as read.
12714     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12715                   gnus-description-hashtb)
12716
12717     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12718     (cond 
12719      ((not (or (gnus-server-opened method)
12720                (gnus-open-server method)))
12721       (gnus-message 1 "Couldn't open server")
12722       nil)
12723      ((not (gnus-request-list-newsgroups method))
12724       (gnus-message 1 "Couldn't read newsgroups descriptions")
12725       nil)
12726      (t
12727       (let (group)
12728         (save-excursion
12729           (save-restriction
12730             (set-buffer nntp-server-buffer)
12731             (goto-char (point-min))
12732             (if (or (search-forward "\n.\n" nil t)
12733                     (goto-char (point-max)))
12734                 (progn
12735                   (beginning-of-line)
12736                   (narrow-to-region (point-min) (point))))
12737             (goto-char (point-min))
12738             (while (not (eobp))
12739               ;; If we get an error, we set group to 0, which is not a
12740               ;; symbol... 
12741               (setq group 
12742                     (condition-case ()
12743                         (let ((obarray gnus-description-hashtb))
12744                           ;; Group is set to a symbol interned in this
12745                           ;; hash table.
12746                           (read nntp-server-buffer))
12747                       (error 0)))
12748               (skip-chars-forward " \t")
12749               ;; ... which leads to this line being effectively ignored.
12750               (and (symbolp group)
12751                    (set group (buffer-substring 
12752                                (point) (progn (end-of-line) (point)))))
12753               (forward-line 1))))
12754         (gnus-message 5 "Reading descriptions file...done")
12755         t)))))
12756
12757 (defun gnus-group-get-description (group)
12758   ;; Get the description of a group by sending XGTITLE to the server.
12759   (and (gnus-request-group-description group)
12760        (save-excursion
12761          (set-buffer nntp-server-buffer)
12762          (goto-char (point-min))
12763          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12764               (buffer-substring (match-beginning 1) (match-end 1))))))
12765
12766 ;;;
12767 ;;; Server
12768 ;;;
12769
12770 (defvar gnus-server-mode-hook nil
12771   "Hook run in `gnus-server-mode' buffers.")
12772
12773 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12774   "Format of server lines.
12775 It works along the same lines as a normal formatting string,
12776 with some simple extensions.")
12777
12778 (defvar gnus-server-mode-line-format "(ding) List of servers"
12779   "The format specification for the server mode line.")
12780
12781 (defconst gnus-server-line-format-alist
12782   (list (list ?h 'how ?s)
12783         (list ?n 'name ?s)
12784         (list ?w 'where ?s)
12785         ))
12786
12787 (defconst gnus-server-mode-line-format-alist 
12788   (list (list ?S 'news-server ?s)
12789         (list ?M 'news-method ?s)
12790         (list ?u 'user-defined ?s)))
12791
12792 (defvar gnus-server-line-format-spec nil)
12793 (defvar gnus-server-mode-line-format-spec nil)
12794 (defvar gnus-server-killed-servers nil)
12795
12796 (defvar gnus-server-mode-map nil)
12797 (put 'gnus-server-mode 'mode-class 'special)
12798
12799 (if gnus-server-mode-map
12800     nil
12801   (setq gnus-server-mode-map (make-sparse-keymap))
12802   (suppress-keymap gnus-server-mode-map)
12803   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12804   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12805   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12806   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12807   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12808   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12809   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12810   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12811   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12812   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12813
12814 (defun gnus-server-mode ()
12815   "Major mode for listing and editing servers.
12816
12817 All normal editing commands are switched off.
12818 \\<gnus-server-mode-map>
12819
12820 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12821
12822 The following commands are available:
12823
12824 \\{gnus-server-mode-map}"
12825   (interactive)
12826   (if gnus-visual (gnus-server-make-menu-bar))
12827   (kill-all-local-variables)
12828   (setq mode-line-modified "-- ")
12829   (make-local-variable 'mode-line-format)
12830   (setq mode-line-format (copy-sequence mode-line-format))
12831   (and (equal (nth 3 mode-line-format) "   ")
12832        (setcar (nthcdr 3 mode-line-format) ""))
12833   (setq major-mode 'gnus-server-mode)
12834   (setq mode-name "Server")
12835 ;  (gnus-group-set-mode-line)
12836   (setq mode-line-process nil)
12837   (use-local-map gnus-server-mode-map)
12838   (buffer-disable-undo (current-buffer))
12839   (setq truncate-lines t)
12840   (setq buffer-read-only t)
12841   (run-hooks 'gnus-server-mode-hook))
12842
12843 (defun gnus-server-insert-server-line (sformat name method)
12844   (let* ((sformat (or sformat gnus-server-line-format-spec))
12845          (how (car method))
12846          (where (nth 1 method))
12847          b)
12848     (beginning-of-line)
12849     (setq b (point))
12850     ;; Insert the text.
12851     (insert (eval sformat))
12852     (add-text-properties 
12853      b (1+ b) (list 'gnus-server (intern name)))))
12854
12855 (defun gnus-server-setup-buffer ()
12856   (if (get-buffer gnus-server-buffer)
12857       ()
12858     (save-excursion
12859       (set-buffer (get-buffer-create gnus-server-buffer))
12860       (gnus-server-mode)
12861       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12862
12863 (defun gnus-server-prepare ()
12864   (setq gnus-server-mode-line-format-spec 
12865         (gnus-parse-format gnus-server-mode-line-format 
12866                            gnus-server-mode-line-format-alist))
12867   (setq gnus-server-line-format-spec 
12868         (gnus-parse-format gnus-server-line-format 
12869                            gnus-server-line-format-alist))
12870   (let ((alist gnus-server-alist)
12871         (buffer-read-only nil))
12872     (erase-buffer)
12873     (while alist
12874       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12875       (setq alist (cdr alist))))
12876   (goto-char (point-min))
12877   (gnus-server-position-cursor))
12878
12879 (defun gnus-server-server-name ()
12880   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12881     (and server (symbol-name server))))
12882
12883 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12884
12885 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12886
12887 (defun gnus-server-update-server (server)
12888   (save-excursion
12889     (set-buffer gnus-server-buffer)
12890     (let ((buffer-read-only nil)
12891           (info (cdr (assoc server gnus-server-alist))))
12892       (gnus-dribble-enter 
12893        (concat "(gnus-server-set-info \"" server "\" '"
12894                (prin1-to-string info) ")"))
12895       ;; Buffer may be narrowed.
12896       (save-restriction
12897         (widen)
12898         (if (gnus-server-goto-server server)
12899             (delete-region (progn (beginning-of-line) (point))
12900                            (progn (forward-line 1) (point))))
12901         (let ((entry (assoc server gnus-server-alist)))
12902           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12903           (gnus-server-position-cursor))))))
12904
12905 (defun gnus-server-set-info (server info)
12906   ;; Enter a select method into the virtual server alist.
12907   (gnus-dribble-enter 
12908    (concat "(gnus-server-set-info \"" server "\" '"
12909            (prin1-to-string info) ")"))
12910   (let* ((server (nth 1 info))
12911          (entry (assoc server gnus-server-alist)))
12912     (if entry (setcdr entry info)
12913       (setq gnus-server-alist
12914             (nconc gnus-server-alist (list (cons server info)))))))
12915
12916 (defun gnus-server-to-method (server)
12917   ;; Map virtual server names to select methods.
12918   (or (and (equal server "native") gnus-select-method)
12919       (cdr (assoc server gnus-server-alist))))
12920
12921 (defun gnus-server-extend-method (group method)
12922   ;; This function "extends" a virtual server.  If the server is
12923   ;; "hello", and the select method is ("hello" (my-var "something")) 
12924   ;; in the group "alt.alt", this will result in a new virtual server
12925   ;; called "helly+alt.alt".
12926   (let ((entry
12927          (gnus-copy-sequence 
12928           (if (equal (car method) "native") gnus-select-method
12929               (cdr (assoc (car method) gnus-server-alist))))))
12930     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12931     (nconc entry (cdr method))))
12932
12933 (defun gnus-server-get-method (group method)
12934   ;; Input either a server name, and extended server name, or a
12935   ;; select method, and return a select method. 
12936   (cond ((stringp method)
12937          (gnus-server-to-method method))
12938         ((stringp (car method))
12939          (gnus-server-extend-method group method))
12940         (t
12941          (gnus-server-add-address method))))
12942
12943 (defun gnus-server-add-address (method)
12944   (let ((method-name (symbol-name (car method))))
12945     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12946              (not (assq (intern (concat method-name "-address")) method)))
12947         (append method (list (list (intern (concat method-name "-address"))
12948                                    (nth 1 method))))
12949       method)))
12950
12951 (defun gnus-server-equal (s1 s2)
12952   (or (equal s1 s2)
12953       (and (= (length s1) (length s2))
12954            (progn
12955              (while (and s1 (member (car s1) s2))
12956                (setq s1 (cdr s1)))
12957              (null s1)))))
12958
12959 ;;; Interactive server functions.
12960
12961 (defun gnus-server-kill-server (server)
12962   "Kill the server on the current line."
12963   (interactive (list (gnus-server-server-name)))
12964   (or (gnus-server-goto-server server)
12965       (if server (error "No such server: %s" server)
12966         (error "No server on the current line")))
12967   (let ((buffer-read-only nil))
12968     (delete-region (progn (beginning-of-line) (point))
12969                    (progn (forward-line 1) (point))))
12970   (setq gnus-server-killed-servers 
12971         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12972   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12973                                 gnus-server-alist))
12974   (gnus-server-position-cursor))
12975
12976 (defun gnus-server-yank-server ()
12977   "Yank the previously killed server."
12978   (interactive)
12979   (or gnus-server-killed-servers
12980       (error "No killed servers to be yanked"))
12981   (let ((alist gnus-server-alist)
12982         (server (gnus-server-server-name))
12983         (killed (car gnus-server-killed-servers)))
12984     (if (not server) 
12985         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12986       (if (string= server (car (car gnus-server-alist)))
12987           (setq gnus-server-alist (cons killed gnus-server-alist))
12988         (while (and (cdr alist)
12989                     (not (string= server (car (car (cdr alist))))))
12990           (setq alist (cdr alist)))
12991         (setcdr alist (cons killed (cdr alist)))))
12992     (gnus-server-update-server (car killed))
12993     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12994     (gnus-server-position-cursor)))
12995
12996 (defun gnus-server-exit ()
12997   "Return to the group buffer."
12998   (interactive)
12999   (kill-buffer (current-buffer))
13000   (switch-to-buffer gnus-group-buffer))
13001
13002 (defun gnus-server-list-servers ()
13003   "List all available servers."
13004   (interactive)
13005   (let ((cur (gnus-server-server-name)))
13006     (gnus-server-prepare)
13007     (if cur (gnus-server-goto-server cur)
13008       (goto-char (point-max))
13009       (forward-line -1))
13010     (gnus-server-position-cursor)))
13011
13012 (defun gnus-server-copy-server (from to)
13013   (interactive
13014    (list
13015     (or (gnus-server-server-name)
13016         (error "No server on the current line"))
13017     (read-string "Copy to: ")))
13018   (or from (error "No server on current line"))
13019   (or (and to (not (string= to ""))) (error "No name to copy to"))
13020   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13021   (or (assoc from gnus-server-alist) 
13022       (error "%s: no such server" from))
13023   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13024     (setcar to-entry to)
13025     (setcar (nthcdr 2 to-entry) to)
13026     (setq gnus-server-killed-servers 
13027           (cons to-entry gnus-server-killed-servers))
13028     (gnus-server-yank-server)))
13029
13030 (defun gnus-server-add-server (how where)
13031   (interactive 
13032    (list (intern (completing-read "Server method: "
13033                                   gnus-valid-select-methods nil t))
13034          (read-string "Server name: ")))
13035   (setq gnus-server-killed-servers 
13036         (cons (list where how where) gnus-server-killed-servers))
13037   (gnus-server-yank-server))
13038
13039 (defun gnus-server-goto-server (server)
13040   "Jump to a server line."
13041   (interactive
13042    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13043   (let ((to (text-property-any (point-min) (point-max) 
13044                                'gnus-server (intern server))))
13045     (and to
13046          (progn
13047            (goto-char to) 
13048            (gnus-server-position-cursor)))))
13049
13050 (defun gnus-server-edit-server (server)
13051   "Edit the server on the current line."
13052   (interactive (list (gnus-server-server-name)))
13053   (or server
13054       (error "No server on current line"))
13055   (let ((winconf (current-window-configuration)))
13056     (get-buffer-create gnus-server-edit-buffer)
13057     (gnus-configure-windows 'edit-server)
13058     (gnus-add-current-to-buffer-list)
13059     (emacs-lisp-mode)
13060     (make-local-variable 'gnus-prev-winconf)
13061     (setq gnus-prev-winconf winconf)
13062     (use-local-map (copy-keymap (current-local-map)))
13063     (let ((done-func '(lambda () 
13064                         "Exit editing mode and update the information."
13065                         (interactive)
13066                         (gnus-server-edit-server-done 'group))))
13067       (setcar (cdr (nth 4 done-func)) server)
13068       (local-set-key "\C-c\C-c" done-func))
13069     (erase-buffer)
13070     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13071     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13072
13073 (defun gnus-server-edit-server-done (server)
13074   (interactive)
13075   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13076   (goto-char (point-min))
13077   (let ((form (read (current-buffer)))
13078         (winconf gnus-prev-winconf))
13079     (gnus-server-set-info server form)
13080     (kill-buffer (current-buffer))
13081     (and winconf (set-window-configuration winconf))
13082     (set-buffer gnus-server-buffer)
13083     (gnus-server-update-server (gnus-server-server-name))
13084     (gnus-server-position-cursor)))
13085
13086 (defun gnus-server-read-server (server)
13087   "Browse a server."
13088   (interactive (list (gnus-server-server-name)))
13089   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13090
13091 (defun gnus-mouse-pick-server (e)
13092   (interactive "e")
13093   (mouse-set-point e)
13094   (gnus-server-read-server (gnus-server-server-name)))
13095
13096 ;;;
13097 ;;; entry points into gnus-score.el
13098 ;;;
13099
13100 ;;; Finding score files. 
13101
13102 (defvar gnus-global-score-files nil
13103   "*List of global score files and directories.
13104 Set this variable if you want to use people's score files.  One entry
13105 for each score file or each score file directory.  Gnus will decide
13106 by itself what score files are applicable to which group.
13107
13108 Say you want to use the single score file
13109 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13110 score files in the \"/ftp.some-where:/pub/score\" directory.
13111
13112  (setq gnus-global-score-files
13113        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13114          \"/ftp.some-where:/pub/score\"))")
13115
13116 (defun gnus-score-score-files (group)
13117   "Return a list of all possible score files."
13118   ;; Search and set any global score files.
13119   (and gnus-global-score-files 
13120        (or gnus-internal-global-score-files
13121            (gnus-score-search-global-directories gnus-global-score-files)))
13122   ;; Fix the kill-file dir variable.
13123   (setq gnus-kill-files-directory 
13124         (file-name-as-directory
13125          (or gnus-kill-files-directory "~/News/")))
13126   ;; If we can't read it, there are no score files.
13127   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13128       (setq gnus-score-file-list nil)
13129     (if (gnus-use-long-file-name 'not-score)
13130         ;; We want long file names.
13131         (if (or (not gnus-score-file-list)
13132                 (not (car gnus-score-file-list))
13133                 (gnus-file-newer-than gnus-kill-files-directory
13134                                       (car gnus-score-file-list)))
13135               (setq gnus-score-file-list 
13136                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13137                           (nreverse 
13138                            (directory-files 
13139                             gnus-kill-files-directory t 
13140                             (gnus-score-file-regexp))))))
13141       ;; We do not use long file names, so we have to do some
13142       ;; directory traversing.  
13143       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13144             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13145             dir files suffix)
13146         (while suffixes
13147           (setq dir (expand-file-name
13148                      (concat gnus-kill-files-directory
13149                              (gnus-replace-chars-in-string group ?. ?/))))
13150           (setq suffix (car suffixes)
13151                 suffixes (cdr suffixes))
13152           (if (file-exists-p (concat dir "/" suffix))
13153               (setq files (cons (concat dir "/" suffix) files)))
13154           (while (>= (1+ (length dir)) mdir)
13155             (and (file-exists-p (concat dir "/all/" suffix))
13156                  (setq files (cons (concat dir "/all/" suffix) files)))
13157             (string-match "/[^/]*$" dir)
13158             (setq dir (substring dir 0 (match-beginning 0)))))
13159         (setq gnus-score-file-list 
13160               (cons nil (nreverse files)))))
13161     (cdr gnus-score-file-list)))
13162
13163 (defun gnus-score-file-regexp ()
13164   (concat "\\(" gnus-score-file-suffix 
13165           "\\|" gnus-adaptive-file-suffix "\\)$"))
13166         
13167 (defun gnus-score-find-bnews (group)
13168   "Return a list of score files for GROUP.
13169 The score files are those files in the ~/News directory which matches
13170 GROUP using BNews sys file syntax."
13171   (let* ((sfiles (append (gnus-score-score-files group)
13172                          gnus-internal-global-score-files))
13173          (kill-dir (file-name-as-directory 
13174                     (expand-file-name gnus-kill-files-directory)))
13175          (klen (length kill-dir))
13176          ofiles not-match regexp)
13177     (save-excursion
13178       (set-buffer (get-buffer-create "*gnus score files*"))
13179       (buffer-disable-undo (current-buffer))
13180       ;; Go through all score file names and create regexp with them
13181       ;; as the source.  
13182       (while sfiles
13183         (erase-buffer)
13184         (insert (car sfiles))
13185         (goto-char (point-min))
13186         ;; First remove the suffix itself.
13187         (re-search-forward (concat "." (gnus-score-file-regexp)))
13188         (replace-match "" t t) 
13189         (goto-char (point-min))
13190         (if (looking-at (regexp-quote kill-dir))
13191             ;; If the file name was just "SCORE", `klen' is one character
13192             ;; too much.
13193             (delete-char (min (1- (point-max)) klen))
13194           (goto-char (point-max))
13195           (search-backward "/")
13196           (delete-region (1+ (point)) (point-min)))
13197         ;; If short file names were used, we have to translate slashes.
13198         (goto-char (point-min))
13199         (while (search-forward "/" nil t)
13200           (replace-match "." t t))
13201         ;; Translate "all" to ".*".
13202         (while (search-forward "all" nil t)
13203           (replace-match ".*" t t))
13204         (goto-char (point-min))
13205         ;; Deal with "not."s.
13206         (if (looking-at "not.")
13207             (progn
13208               (setq not-match t)
13209               (setq regexp (buffer-substring 5 (point-max))))
13210           (setq regexp (buffer-substring 1 (point-max)))
13211           (setq not-match nil))
13212         ;; Finally - if this resulting regexp matches the group name,
13213         ;; we add this score file to the list of score files
13214         ;; applicable to this group.
13215         (if (or (and not-match
13216                      (not (string-match regexp group)))
13217                 (and (not not-match)
13218                      (string-match regexp group)))
13219             (setq ofiles (cons (car sfiles) ofiles)))
13220         (setq sfiles (cdr sfiles)))
13221       (kill-buffer (current-buffer))
13222       ;; Slight kludge here - the last score file returned should be
13223       ;; the local score file, whether it exists or not. This is so
13224       ;; that any score commands the user enters will go to the right
13225       ;; file, and not end up in some global score file.
13226       (let ((localscore
13227              (expand-file-name
13228               (if (gnus-use-long-file-name 'not-score)
13229                   (concat gnus-kill-files-directory group "." 
13230                           gnus-score-file-suffix)
13231                 (concat gnus-kill-files-directory
13232                         (gnus-replace-chars-in-string group ?. ?/)
13233                         "/" gnus-score-file-suffix)))))
13234         (and (member localscore ofiles)
13235              (delete localscore ofiles))
13236         (setq ofiles (cons localscore ofiles)))
13237       (nreverse ofiles))))
13238
13239 (defun gnus-score-find-single (group)
13240   "Return list containing the score file for GROUP."
13241   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13242         (gnus-score-file-name group)))
13243
13244 (defun gnus-score-find-hierarchical (group)
13245   "Return list of score files for GROUP.
13246 This includes the score file for the group and all its parents."
13247   (let ((all (copy-sequence '(nil)))
13248         (start 0))
13249     (while (string-match "\\." group (1+ start))
13250       (setq start (match-beginning 0))
13251       (setq all (cons (substring group 0 start) all)))
13252     (setq all (cons group all))
13253     (nconc
13254      (mapcar (lambda (newsgroup)
13255                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13256              (setq all (nreverse all)))
13257      (mapcar 'gnus-score-file-name all))))
13258
13259 (defvar gnus-score-file-alist-cache nil)
13260
13261 (defun gnus-score-find-alist (group)
13262   "Return list of score files for GROUP.
13263 The list is determined from the variable gnus-score-file-alist."
13264   (let ((alist gnus-score-file-multiple-match-alist)
13265         score-files)
13266     ;; if this group has been seen before, return the cached entry
13267     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13268         (cdr score-files)       ; ensures caching of groups with no matches
13269       ;; handle the multiple match alist
13270       (while alist
13271         (and (string-match (car (car alist)) group)
13272              (setq score-files
13273                    (nconc score-files (cdr (car alist)))))
13274         (setq alist (cdr alist)))
13275       (setq alist gnus-score-file-single-match-alist)
13276       ;; handle the single match alist
13277       (catch 'done
13278         (while alist
13279           (and (string-match (car (car alist)) group)
13280                ;; progn used just in case ("regexp") has no files
13281                ;; and score-files is still nil. -sj
13282                ;; this can be construed as a "stop searching here" feature :>
13283                ;; and used to simplify regexps in the single-alist 
13284                (progn
13285                  (setq score-files
13286                        (append score-files (cdr (car alist))))
13287                  (throw 'done nil)))
13288           (setq alist (cdr alist))))
13289       ;; cache the score files
13290       (setq gnus-score-file-alist-cache
13291             (cons (cons group score-files) gnus-score-file-alist-cache))
13292       score-files)))
13293
13294
13295 (defun gnus-possibly-score-headers (&optional trace)
13296   (let ((func gnus-score-find-score-files-function)
13297         score-files scores)
13298     (and func (not (listp func))
13299          (setq func (list func)))
13300     ;; Go through all the functions for finding score files (or actual
13301     ;; scores) and add them to a list.
13302     (setq score-files (copy-sequence
13303                        (gnus-score-find-alist gnus-newsgroup-name)))
13304     (while func
13305       (and (symbolp (car func))
13306            (fboundp (car func))
13307            (setq score-files 
13308                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13309       (setq func (cdr func)))
13310     (if score-files (gnus-score-headers score-files trace))))
13311
13312 (defun gnus-score-file-name (newsgroup &optional suffix)
13313   "Return the name of a score file for NEWSGROUP."
13314   (let ((suffix (or suffix gnus-score-file-suffix)))
13315     (cond  ((or (null newsgroup)
13316                 (string-equal newsgroup ""))
13317             ;; The global score file is placed at top of the directory.
13318             (expand-file-name 
13319              suffix (or gnus-kill-files-directory "~/News")))
13320            ((gnus-use-long-file-name 'not-score)
13321             ;; Append ".SCORE" to newsgroup name.
13322             (expand-file-name (concat newsgroup "." suffix)
13323                               (or gnus-kill-files-directory "~/News")))
13324            (t
13325             ;; Place "SCORE" under the hierarchical directory.
13326             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13327                                       "/" suffix)
13328                               (or gnus-kill-files-directory "~/News"))))))
13329
13330 (defun gnus-score-search-global-directories (files)
13331   "Scan all global score directories for score files."
13332   ;; Set the variable `gnus-internal-global-score-files' to all
13333   ;; available global score files.
13334   (interactive (list gnus-global-score-files))
13335   (let (out)
13336     (while files
13337       (if (string-match "/$" (car files))
13338           (setq out (nconc (directory-files 
13339                             (car files) t
13340                             (concat (gnus-score-file-regexp) "$"))))
13341         (setq out (cons (car files) out)))
13342       (setq files (cdr files)))
13343     (setq gnus-internal-global-score-files out)))
13344
13345 ;; Allow redefinition of Gnus functions.
13346
13347 (gnus-ems-redefine)
13348
13349 (provide 'gnus)
13350
13351 ;;; gnus.el ends here