*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find score files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own score file.
242 gnus-score-find-hierarchical: Also apply score files from parent groups.
243 gnus-score-find-bnews: Apply score files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread t
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups 4
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed t
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-nil, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups 
589   (purecopy (mapconcat 'identity
590                        '("^to\\."       ; not "real" groups
591                          "^[0-9. \t]+ " ; all digits in name
592                          "[][\"#'()]"   ; bogus characters
593                          )
594                        "\\|"))
595   "*A regexp to match uninteresting newsgroups in the active file.
596 Any lines in the active file matching this regular expression are
597 removed from the newsgroup list before anything else is done to it,
598 thus making them effectively non-existent.")
599
600 (defvar gnus-ignored-headers
601   "^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:"
602   "*All headers that match this regexp will be hidden.
603 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
604
605 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
606   "*All headers that do not match this regexp will be hidden.
607 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
608
609 (defvar gnus-sorted-header-list
610   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
611     "^Cc:" "^Date:" "^Organization:")
612   "*This variable is a list of regular expressions.
613 If it is non-nil, headers that match the regular expressions will
614 be placed first in the article buffer in the sequence specified by
615 this list.")
616
617 (defvar gnus-show-all-headers nil
618   "*If non-nil, don't hide any headers.")
619
620 (defvar gnus-save-all-headers t
621   "*If non-nil, don't remove any headers before saving.")
622
623 (defvar gnus-inhibit-startup-message nil
624   "*If non-nil, the startup message will not be displayed.")
625
626 (defvar gnus-signature-separator "^-- *$"
627   "Regexp matching signature separator.")
628
629 (defvar gnus-auto-extend-newsgroup t
630   "*If non-nil, extend newsgroup forward and backward when requested.")
631
632 (defvar gnus-auto-select-first t
633   "*If non-nil, select the first unread article when entering a group.
634 If you want to prevent automatic selection of the first unread article
635 in some newsgroups, set the variable to nil in
636 `gnus-select-group-hook'.") 
637
638 (defvar gnus-auto-select-next t
639   "*If non-nil, offer to go to the next group from the end of the previous.
640 If the value is t and the next newsgroup is empty, Gnus will exit
641 summary mode and go back to group mode.  If the value is neither nil
642 nor t, Gnus will select the following unread newsgroup.  In
643 particular, if the value is the symbol `quietly', the next unread
644 newsgroup will be selected without any confirmations.")
645
646 (defvar gnus-auto-select-same nil
647   "*If non-nil, select the next article with the same subject.")
648
649 (defvar gnus-summary-check-current nil
650   "*If non-nil, consider the current article when moving.
651 The \"unread\" movement commands will stay on the same line if the
652 current article is unread.")
653
654 (defvar gnus-auto-center-summary t
655   "*If non-nil, always center the current summary buffer.")
656
657 (defvar gnus-break-pages t
658   "*If non-nil, do page breaking on articles.
659 The page delimiter is specified by the `gnus-page-delimiter'
660 variable.")
661
662 (defvar gnus-page-delimiter "^\^L"
663   "*Regexp describing what to use as article page delimiters.
664 The default value is \"^\^L\", which is a form linefeed at the
665 beginning of a line.")
666
667 (defvar gnus-use-full-window t
668   "*If non-nil, use the entire Emacs screen.")
669
670 (defvar gnus-window-configuration nil
671   "Obsolete variable.  See `gnus-buffer-configuration'.")
672
673 (defvar gnus-buffer-configuration
674   '((group ([group 1.0 point] 
675             (if gnus-carpal [group-carpal 4])))
676     (summary ([summary 1.0 point]
677               (if gnus-carpal [summary-carpal 4])))
678     (article ([summary 0.25 point] 
679               (if gnus-carpal [summary-carpal 4]) 
680               [article 1.0]))
681     (server ([server 1.0 point]
682              (if gnus-carpal [server-carpal 2])))
683     (browse ([browse 1.0 point]
684              (if gnus-carpal [browse-carpal 2])))
685     (group-mail ([mail 1.0 point]))
686     (summary-mail ([mail 1.0 point]))
687     (summary-reply ([article 0.5]
688                     [mail 1.0 point]))
689     (info ([nil 1.0 point]))
690     (summary-faq ([summary 0.25]
691                   [faq 1.0 point]))
692     (edit-group ([group 0.5]
693                  [edit-group 1.0 point]))
694     (edit-server ([server 0.5]
695                   [edit-server 1.0 point]))
696     (edit-score ([summary 0.25]
697                  [edit-score 1.0 point]))
698     (post ([post 1.0 point]))
699     (reply ([article 0.5]
700             [mail 1.0 point]))
701     (mail-forward ([mail 1.0 point]))
702     (post-forward ([post 1.0 point]))
703     (reply-yank ([mail 1.0 point]))
704     (followup ([article 0.5]
705                [post 1.0 point]))
706     (followup-yank ([post 1.0 point])))
707   "Window configuration for all possible Gnus buffers.
708 This variable is a list of lists.  Each of these lists has a NAME and
709 a RULE.  The NAMEs are commonsense names like `group', which names a
710 rule used when displaying the group buffer; `summary', which names a
711 rule for what happens when you enter a group and do not display an
712 article buffer; and so on.  See the value of this variable for a
713 complete list of NAMEs.
714
715 Each RULE is a list of vectors.  The first element in this vector is
716 the name of the buffer to be displayed; the second element is the
717 percentage of the screen this buffer is to occupy (a number in the
718 0.0-0.99 range); the optional third element is `point', which should
719 be present to denote which buffer point is to go to after making this
720 buffer configuration.")
721
722 (defvar gnus-window-to-buffer
723   '((group . gnus-group-buffer)
724     (summary . gnus-summary-buffer)
725     (article . gnus-article-buffer)
726     (server . gnus-server-buffer)
727     (browse . "*Gnus Browse Server*")
728     (edit-group . gnus-group-edit-buffer)
729     (edit-server . gnus-server-edit-buffer)
730     (group-carpal . gnus-carpal-group-buffer)
731     (summary-carpal . gnus-carpal-summary-buffer)
732     (server-carpal . gnus-carpal-server-buffer)
733     (browse-carpal . gnus-carpal-browse-buffer)
734     (edit-score . gnus-score-edit-buffer)
735     (mail . gnus-mail-buffer)
736     (post . gnus-post-news-buffer)
737     (faq . gnus-faq-buffer))
738   "Mapping from short symbols to buffer names or buffer variables.")
739
740 (defvar gnus-carpal nil
741   "*If non-nil, display clickable icons.")
742
743 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
744   "*Function called with a group name when new group is detected.
745 A few pre-made functions are supplied: `gnus-subscribe-randomly'
746 inserts new groups at the beginning of the list of groups;
747 `gnus-subscribe-alphabetically' inserts new groups in strict
748 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
749 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
750 for your decision.")
751
752 ;; Suggested by a bug report by Hallvard B Furuseth.
753 ;; <h.b.furuseth@usit.uio.no>. 
754 (defvar gnus-subscribe-options-newsgroup-method
755   (function gnus-subscribe-alphabetically)
756   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
757 If, for instance, you want to subscribe to all newsgroups in the
758 \"no\" and \"alt\" hierarchies, you'd put the following in your
759 .newsrc file:
760
761 options -n no.all alt.all
762
763 Gnus will the subscribe all new newsgroups in these hierarchies with
764 the subscription method in this variable.")
765
766 (defvar gnus-subscribe-hierarchical-interactive nil
767   "*If non-nil, Gnus will offer to subscribe hierarchically.
768 When a new hierarchy appears, Gnus will ask the user:
769
770 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
771
772 If the user pressed `d', Gnus will descend the hierarchy, `y' will
773 subscribe to all newsgroups in the hierarchy and `s' will skip this
774 hierarchy in its entirety.")
775
776 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
777   "*Function used for sorting the group buffer.
778 This function will be called with group info entries as the arguments
779 for the groups to be sorted.  Pre-made functions include
780 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
781 `gnus-group-sort-by-level'")
782
783 ;; Mark variables suggested by Thomas Michanek
784 ;; <Thomas.Michanek@telelogic.se>. 
785 (defvar gnus-unread-mark ? 
786   "*Mark used for unread articles.")
787 (defvar gnus-ticked-mark ?!
788   "*Mark used for ticked articles.")
789 (defvar gnus-dormant-mark ??
790   "*Mark used for dormant articles.")
791 (defvar gnus-del-mark ?r
792   "*Mark used for del'd articles.")
793 (defvar gnus-read-mark ?R
794   "*Mark used for read articles.")
795 (defvar gnus-expirable-mark ?E
796   "*Mark used for expirable articles.")
797 (defvar gnus-killed-mark ?K
798   "*Mark used for killed articles.")
799 (defvar gnus-kill-file-mark ?X
800   "*Mark used for articles killed by kill files.")
801 (defvar gnus-low-score-mark ?Y
802   "*Mark used for articles with a low score.")
803 (defvar gnus-catchup-mark ?C
804   "*Mark used for articles that are caught up.")
805 (defvar gnus-replied-mark ?A
806   "*Mark used for articles that have been replied to.")
807 (defvar gnus-process-mark ?# 
808   "*Process mark.")
809 (defvar gnus-ancient-mark ?O
810   "*Mark used for ancient articles.")
811 (defvar gnus-canceled-mark ?G
812   "*Mark used for canceled articles.")
813 (defvar gnus-score-over-mark ?+
814   "*Score mark used for articles with high scores.")
815 (defvar gnus-score-below-mark ?-
816   "*Score mark used for articles with low scores.")
817 (defvar gnus-empty-thread-mark ? 
818   "*There is no thread under the article.")
819 (defvar gnus-not-empty-thread-mark ?=
820   "*There is a thread under the article.")
821 (defvar gnus-dummy-mark ?Z
822   "*This is a dummy article.")
823
824 (defvar gnus-view-pseudo-asynchronously nil
825   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
826
827 (defvar gnus-view-pseudos nil
828   "*If `automatic', pseudo-articles will be viewed automatically.
829 If `not-confirm', pseudos will be viewed automatically, and the user
830 will not be asked to confirm the command.")
831
832 (defvar gnus-view-pseudos-separately t
833   "*If non-nil, one pseudo-article will be created for each file to be viewed.
834 If nil, all files that use the same viewing command will be given as a
835 list of parameters to that command.")
836
837 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
838   "*Format of group lines.
839 It works along the same lines as a normal formatting string,
840 with some simple extensions.
841
842 %M    Only marked articles (character, \"*\" or \" \")
843 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
844 %L    Level of subscribedness (integer)
845 %N    Number of unread articles (integer)
846 %I    Number of dormant articles (integer)
847 %i    Number of ticked and dormant (integer)
848 %T    Number of ticked articles (integer)
849 %R    Number of read articles (integer)
850 %t    Total number of articles (integer)
851 %y    Number of unread, unticked articles (integer)
852 %G    Group name (string)
853 %g    Qualified group name (string)
854 %D    Group description (string)
855 %s    Select method (string)
856 %o    Moderated group (char, \"m\")
857 %p    Process mark (char)
858 %O    Moderated group (string, \"(m)\" or \"\")
859 %n    Select from where (string)
860 %z    A string that look like `<%s:%n>' if a foreign select method is used
861 %u    User defined specifier. The next character in the format string should
862       be a letter.  Gnus will call the function gnus-user-format-function-X,
863       where X is the letter following %u. The function will be passed the
864       current header as argument. The function should return a string, which
865       will be inserted into the buffer just like information from any other
866       group specifier.
867
868 Text between %( and %) will be highlighted with `gnus-mouse-face' when
869 the mouse point move inside the area.  There can only be one such area.
870
871 Note that this format specification is not always respected. For
872 reasons of efficiency, when listing killed groups, this specification
873 is ignored altogether. If the spec is changed considerably, your
874 output may end up looking strange when listing both alive and killed
875 groups.
876
877 If you use %o or %O, reading the active file will be slower and quite
878 a bit of extra memory will be used. %D will also worsen performance.
879 Also note that if you change the format specification to include any
880 of these specs, you must probably re-start Gnus to see them go into
881 effect.") 
882
883 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
884   "*The format specification of the lines in the summary buffer.
885
886 It works along the same lines as a normal formatting string,
887 with some simple extensions.
888
889 %N   Article number, left padded with spaces (string)
890 %S   Subject (string)
891 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
892 %n   Name of the poster (string)
893 %a   Extracted name of the poster (string)
894 %A   Extracted address of the poster (string)
895 %F   Contents of the From: header (string)
896 %x   Contents of the Xref: header (string)
897 %D   Date of the article (string)
898 %d   Date of the article (string) in DD-MMM format
899 %M   Message-id of the article (string)
900 %r   References of the article (string)
901 %c   Number of characters in the article (integer)
902 %L   Number of lines in the article (integer)
903 %I   Indentation based on thread level (a string of spaces)
904 %T   A string with two possible values: 80 spaces if the article
905      is on thread level two or larger and 0 spaces on level one
906 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
907 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
908 %[   Opening bracket (character, \"[\" or \"<\")
909 %]   Closing bracket (character, \"]\" or \">\")
910 %>   Spaces of length thread-level (string)
911 %<   Spaces of length (- 20 thread-level) (string)
912 %i   Article score (number)
913 %z   Article zcore (character)
914 %t   Number of articles under the current thread (number).
915 %e   Whether the thread is empty or not (character).
916 %u   User defined specifier. The next character in the format string should
917      be a letter.  Gnus will call the function gnus-user-format-function-X,
918      where X is the letter following %u. The function will be passed the
919      current header as argument. The function should return a string, which
920      will be inserted into the summary just like information from any other
921      summary specifier.
922
923 Text between %( and %) will be highlighted with `gnus-mouse-face'
924 when the mouse point is placed inside the area.  There can only be one
925 such area.
926
927 The %U (status), %R (replied) and %z (zcore) specs have to be handled
928 with care. For reasons of efficiency, Gnus will compute what column
929 these characters will end up in, and \"hard-code\" that. This means that
930 it is illegal to have these specs after a variable-length spec. Well,
931 you might not be arrested, but your summary buffer will look strange,
932 which is bad enough.
933
934 The smart choice is to have these specs as for to the left as
935 possible. 
936
937 This restriction may disappear in later versions of Gnus.")
938
939 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
940   "*The format specification for the dummy roots in the summary buffer.
941 It works along the same lines as a normal formatting string,
942 with some simple extensions.
943
944 %S  The subject")
945
946 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
947   "*The format specification for the summary mode line.")
948
949 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
950   "*The format specification for the article mode line.")
951
952 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
953   "*The format specification for the group mode line.")
954
955 (defvar gnus-valid-select-methods
956   '(("nntp" post address prompt-address)
957     ("nnspool" post)
958     ("nnvirtual" none virtual prompt-address) 
959     ("nnmbox" mail respool) 
960     ("nnml" mail respool)
961     ("nnmh" mail respool) 
962     ("nndir" none prompt-address address)
963     ("nneething" none prompt-address)
964     ("nndigest" none) 
965     ("nndoc" none prompt-address) 
966     ("nnbabyl" mail respool) 
967     ("nnkiboze" post virtual) 
968     ("nnsoup" post)
969     ("nnfolder" mail respool))
970   "An alist of valid select methods.
971 The first element of each list lists should be a string with the name
972 of the select method. The other elements may be be the category of
973 this method (ie. `post', `mail', `none' or whatever) or other
974 properties that this method has (like being respoolable).
975 If you implement a new select method, all you should have to change is
976 this variable. I think.")
977
978 (defvar gnus-updated-mode-lines '(group article summary)
979   "*List of buffers that should update their mode lines.
980 The list may contain the symbols `group', `article' and `summary'. If
981 the corresponding symbol is present, Gnus will keep that mode line
982 updated with information that may be pertinent. 
983 If this variable is nil, screen refresh may be quicker.")
984
985 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
986 (defvar gnus-mode-non-string-length 21
987   "*Max length of mode-line non-string contents.
988 If this is nil, Gnus will take space as is needed, leaving the rest
989 of the modeline intact.")
990
991 (defvar gnus-mouse-face 'highlight
992   "*Face used for mouse highlighting in Gnus.
993 No mouse highlights will be done if `gnus-visual' is nil.")
994
995 (defvar gnus-summary-mark-below nil
996   "*Mark all articles with a score below this variable as read.
997 This variable is local to each summary buffer and usually set by the
998 score file.")  
999
1000 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1001   "*List of functions used for sorting threads in the summary buffer.
1002 By default, threads are sorted by article number.
1003
1004 Each function takes two threads and return non-nil if the first thread
1005 should be sorted before the other.  If you use more than one function,
1006 the primary sort function should be the last.
1007
1008 Ready-mady functions include `gnus-thread-sort-by-number',
1009 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1010 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1011 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1012
1013 (defvar gnus-thread-score-function '+
1014   "*Function used for calculating the total score of a thread.
1015
1016 The function is called with the scores of the article and each
1017 subthread and should then return the score of the thread.
1018
1019 Some functions you can use are `+', `max', or `min'.")
1020
1021 (defvar gnus-options-subscribe nil
1022   "*All new groups matching this regexp will be subscribed unconditionally.
1023 Note that this variable deals only with new newsgroups.  This variable
1024 does not affect old newsgroups.")
1025
1026 (defvar gnus-options-not-subscribe nil
1027   "*All new groups matching this regexp will be ignored.
1028 Note that this variable deals only with new newsgroups.  This variable
1029 does not affect old (already subscribed) newsgroups.")
1030
1031 (defvar gnus-auto-expirable-newsgroups nil
1032   "*Groups in which to automatically mark read articles as expirable.
1033 If non-nil, this should be a regexp that should match all groups in
1034 which to perform auto-expiry.  This only makes sense for mail groups.")
1035
1036 (defvar gnus-hidden-properties '(invisible t intangible t)
1037   "Property list to use for hiding text.")
1038
1039 ;; Hooks.
1040
1041 (defvar gnus-group-mode-hook nil
1042   "*A hook for Gnus group mode.")
1043
1044 (defvar gnus-summary-mode-hook nil
1045   "*A hook for Gnus summary mode.
1046 This hook is run before any variables are set in the summary buffer.")
1047
1048 (defvar gnus-article-mode-hook nil
1049   "*A hook for Gnus article mode.")
1050
1051 (defun gnus-summary-prepare-exit-hook nil
1052   "*A hook called when preparing to exit from the summary buffer.
1053 It calls `gnus-summary-expire-articles' by default.")
1054 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1055
1056 (defun gnus-summary-exit-hook nil
1057   "*A hook called on exit from the summary buffer.")
1058
1059 (defvar gnus-open-server-hook nil
1060   "*A hook called just before opening connection to the news server.")
1061
1062 (defvar gnus-startup-hook nil
1063   "*A hook called at startup.
1064 This hook is called after Gnus is connected to the NNTP server.")
1065
1066 (defvar gnus-get-new-news-hook nil
1067   "*A hook run just before Gnus checks for new news.")
1068
1069 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1070   "*A function that is called to generate the group buffer.
1071 The function is called with three arguments: The first is a number;
1072 all group with a level less or equal to that number should be listed,
1073 if the second is non-nil, empty groups should also be displayed. If
1074 the third is non-nil, it is a number. No groups with a level lower
1075 than this number should be displayed.
1076
1077 The only current function implemented is `gnus-group-prepare-flat'.")
1078
1079 (defvar gnus-group-prepare-hook nil
1080   "*A hook called after the group buffer has been generated.
1081 If you want to modify the group buffer, you can use this hook.")
1082
1083 (defvar gnus-summary-prepare-hook nil
1084   "*A hook called after the summary buffer has been generated.
1085 If you want to modify the summary buffer, you can use this hook.")
1086
1087 (defvar gnus-article-prepare-hook nil
1088   "*A hook called after an article has been prepared in the article buffer.
1089 If you want to run a special decoding program like nkf, use this hook.")
1090
1091 (defvar gnus-article-display-hook nil
1092   "*A hook called after the article is displayed in the article buffer.
1093 The hook is designed to change the contents of the article
1094 buffer. Typical functions that this hook may contain are
1095 `gnus-article-hide-headers' (hide selected headers),
1096 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1097 `gnus-article-hide-signature' (hide signature) and
1098 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1099 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1100 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1101 (add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1102
1103 (defvar gnus-article-x-face-command
1104   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1105   "String or function to be executed to display an X-Face header.
1106 If it is a string, the command will be executed in a sub-shell
1107 asynchronously. The compressed face will be piped to this command.") 
1108
1109 (defvar gnus-article-x-face-too-ugly nil
1110   "Regexp matching posters whose face shouldn't be shown automatically.")
1111
1112 (defvar gnus-select-group-hook nil
1113   "*A hook called when a newsgroup is selected.
1114
1115 If you'd like to simplify subjects like the
1116 `gnus-summary-next-same-subject' command does, you can use the
1117 following hook:
1118
1119  (setq gnus-select-group-hook
1120       (list
1121         (lambda ()
1122           (mapcar (lambda (header)
1123                      (header-set-subject
1124                       header
1125                       (gnus-simplify-subject
1126                        (header-subject header) 're-only)))
1127                   gnus-newsgroup-headers))))")
1128
1129 (defvar gnus-select-article-hook
1130   '(gnus-summary-show-thread)
1131   "*A hook called when an article is selected.
1132 The default hook shows conversation thread subtrees of the selected
1133 article automatically using `gnus-summary-show-thread'.")
1134
1135 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1136   "*A hook called to apply kill files to a group.
1137 This hook is intended to apply a kill file to the selected newsgroup.
1138 The function `gnus-apply-kill-file' is called by default.
1139
1140 Since a general kill file is too heavy to use only for a few
1141 newsgroups, I recommend you to use a lighter hook function. For
1142 example, if you'd like to apply a kill file to articles which contains
1143 a string `rmgroup' in subject in newsgroup `control', you can use the
1144 following hook:
1145
1146 \(setq gnus-apply-kill-hook
1147       (list
1148         (lambda ()
1149           (cond ((string-match \"control\" gnus-newsgroup-name)
1150                  (gnus-kill \"Subject\" \"rmgroup\")
1151                  (gnus-expunge \"X\"))))))")
1152
1153 (defvar gnus-visual-mark-article-hook 
1154   (list 'gnus-highlight-selected-summary)
1155   "*Hook run after selecting an article in the summary buffer.
1156 It is meant to be used for highlighting the article in some way.  It
1157 is not run if `gnus-visual' is nil.")
1158
1159 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1160   "*A hook called after preparing body, but before preparing header headers.
1161 The default hook (`gnus-inews-insert-signature') inserts a signature
1162 file specified by the variable `gnus-signature-file'.")
1163
1164 (defvar gnus-exit-group-hook nil
1165   "*A hook called when exiting (not quitting) summary mode.")
1166
1167 (defvar gnus-suspend-gnus-hook nil
1168   "*A hook called when suspending (not exiting) Gnus.")
1169
1170 (defvar gnus-exit-gnus-hook nil
1171   "*A hook called when exiting Gnus.")
1172
1173 (defvar gnus-save-newsrc-hook nil
1174   "*A hook called when saving the newsrc file.")
1175
1176 (defvar gnus-summary-update-hook 
1177   (list 'gnus-summary-highlight-line)
1178   "*A hook called when a summary line is changed.
1179 The hook will not be called if `gnus-visual' is nil.
1180
1181 The default function `gnus-summary-highlight-line' will
1182 highlight the line according to the `gnus-summary-highlight'
1183 variable.")
1184
1185 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1186   "*A hook called when an article is selected for the first time.
1187 The hook is intended to mark an article as read (or unread)
1188 automatically when it is selected.")
1189
1190 ;; Remove any hilit infestation.
1191 (add-hook 'gnus-startup-hook
1192           (lambda ()
1193             (remove-hook 'gnus-summary-prepare-hook
1194                          'hilit-rehighlight-buffer-quietly)
1195             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1196             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1197             (remove-hook 'gnus-article-prepare-hook
1198                          'hilit-rehighlight-buffer-quietly)))
1199
1200
1201 \f
1202 ;; Internal variables
1203
1204 ;; Avoid highlighting in kill files.
1205 (defvar gnus-summary-inhibit-highlight nil)
1206 (defvar gnus-newsgroup-selected-overlay nil)
1207
1208 (defvar gnus-article-mode-map nil)
1209 (defvar gnus-dribble-buffer nil)
1210 (defvar gnus-headers-retrieved-by nil)
1211 (defvar gnus-article-reply nil)
1212 (defvar gnus-override-method nil)
1213 (defvar gnus-article-check-size nil)
1214
1215 (defvar gnus-current-score-file nil)
1216 (defvar gnus-internal-global-score-files nil)
1217 (defvar gnus-score-file-list nil)
1218
1219
1220 (defvar gnus-current-move-group nil)
1221
1222 (defvar gnus-newsgroup-dependencies nil)
1223 (defvar gnus-newsgroup-threads nil)
1224 (defvar gnus-newsgroup-async nil)
1225 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1226
1227 (defvar gnus-newsgroup-adaptive nil)
1228
1229 (defvar gnus-summary-display-table nil)
1230
1231 (defconst gnus-group-line-format-alist
1232   (list (list ?M 'marked ?c)
1233         (list ?S 'subscribed ?c)
1234         (list ?L 'level ?d)
1235         (list ?N 'number ?s)
1236         (list ?I 'number-of-dormant ?d)
1237         (list ?T 'number-of-ticked ?d)
1238         (list ?R 'number-of-read ?s)
1239         (list ?t 'number-total ?d)
1240         (list ?y 'number-of-unread-unticked ?s)
1241         (list ?i 'number-of-ticked-and-dormant ?d)
1242         (list ?g 'group ?s)
1243         (list ?G 'qualified-group ?s)
1244         (list ?D 'newsgroup-description ?s)
1245         (list ?o 'moderated ?c)
1246         (list ?O 'moderated-string ?s)
1247         (list ?p 'process-marked ?c)
1248         (list ?s 'news-server ?s)
1249         (list ?n 'news-method ?s)
1250         (list ?z 'news-method-string ?s)
1251         (list ?u 'user-defined ?s)))
1252
1253 (defconst gnus-summary-line-format-alist 
1254   (list (list ?N 'number ?d)
1255         (list ?S 'subject ?s)
1256         (list ?s 'subject-or-nil ?s)
1257         (list ?n 'name ?s)
1258         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1259               ?s)
1260         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1261                       from) ?s)
1262         (list ?F 'from ?s)
1263         (list ?x (macroexpand '(header-xref header)) ?s)
1264         (list ?D (macroexpand '(header-date header)) ?s)
1265         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1266         (list ?M (macroexpand '(header-id header)) ?s)
1267         (list ?r (macroexpand '(header-references header)) ?s)
1268         (list ?c '(or (header-chars header) 0) ?d)
1269         (list ?L 'lines ?d)
1270         (list ?I 'indentation ?s)
1271         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1272         (list ?R 'replied ?c)
1273         (list ?\[ 'opening-bracket ?c)
1274         (list ?\] 'closing-bracket ?c)
1275         (list ?\> '(make-string level ? ) ?s)
1276         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1277         (list ?i 'score ?d)
1278         (list ?z 'score-char ?c)
1279         (list ?U 'unread ?c)
1280         (list ?t '(gnus-summary-number-of-articles-in-thread 
1281                    (and (boundp 'thread) (car thread)))
1282               ?d)
1283         (list ?e '(gnus-summary-number-of-articles-in-thread 
1284                    (and (boundp 'thread) (car thread)) t)
1285               ?c)
1286         (list ?u 'user-defined ?s))
1287   "An alist of format specifications that can appear in summary lines,
1288 and what variables they correspond with, along with the type of the
1289 variable (string, integer, character, etc).")
1290
1291 (defconst gnus-summary-dummy-line-format-alist
1292   (list (list ?S 'subject ?s)
1293         (list ?N 'number ?d)
1294         (list ?u 'user-defined ?s)))
1295
1296 (defconst gnus-summary-mode-line-format-alist 
1297   (list (list ?G 'group-name ?s)
1298         (list ?g '(gnus-short-group-name group-name) ?s)
1299         (list ?A 'article-number ?d)
1300         (list ?Z 'unread-and-unselected ?s)
1301         (list ?V 'gnus-version ?s)
1302         (list ?U 'unread ?d)
1303         (list ?S 'subject ?s)
1304         (list ?e 'unselected ?d)
1305         (list ?u 'user-defined ?s)
1306         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1307
1308 (defconst gnus-group-mode-line-format-alist 
1309   (list (list ?S 'news-server ?s)
1310         (list ?M 'news-method ?s)
1311         (list ?u 'user-defined ?s)))
1312
1313 (defvar gnus-have-read-active-file nil)
1314
1315 (defconst gnus-maintainer
1316   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1317   "The mail address of the Gnus maintainers.")
1318
1319 (defconst gnus-version "(ding) Gnus v0.99.16"
1320   "Version number for this version of Gnus.")
1321
1322 (defvar gnus-info-nodes
1323   '((gnus-group-mode            "(gnus)The Group Buffer")
1324     (gnus-summary-mode          "(gnus)The Summary Buffer")
1325     (gnus-article-mode          "(gnus)The Article Buffer"))
1326   "Assoc list of major modes and related Info nodes.")
1327
1328 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1329   "The location of the (ding) Gnus documentation group.")
1330
1331 (defvar gnus-group-buffer "*Group*")
1332 (defvar gnus-summary-buffer "*Summary*")
1333 (defvar gnus-article-buffer "*Article*")
1334 (defvar gnus-server-buffer "*Server*")
1335
1336 (defvar gnus-work-buffer " *gnus work*")
1337
1338 (defvar gnus-buffer-list nil
1339   "Gnus buffers that should be killed on exit.")
1340
1341 (defvar gnus-server-alist nil
1342   "List of available servers.")
1343
1344 (defvar gnus-variable-list
1345   '(gnus-newsrc-options gnus-newsrc-options-n
1346     gnus-newsrc-last-checked-date 
1347     gnus-newsrc-alist gnus-server-alist
1348     gnus-killed-list gnus-zombie-list)
1349   "Gnus variables saved in the quick startup file.")
1350
1351 (defvar gnus-overload-functions
1352   '((news-inews gnus-inews-news "rnewspost"))
1353   "Functions overloaded by gnus.
1354 It is a list of `(original overload &optional file)'.")
1355
1356 (defvar gnus-newsrc-options nil
1357   "Options line in the .newsrc file.")
1358
1359 (defvar gnus-newsrc-options-n nil
1360   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1361
1362 (defvar gnus-newsrc-last-checked-date nil
1363   "Date Gnus last asked server for new newsgroups.")
1364
1365 (defvar gnus-newsrc-alist nil
1366   "Assoc list of read articles.
1367 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1368
1369 (defvar gnus-newsrc-hashtb nil
1370   "Hashtable of gnus-newsrc-alist.")
1371
1372 (defvar gnus-killed-list nil
1373   "List of killed newsgroups.")
1374
1375 (defvar gnus-killed-hashtb nil
1376   "Hash table equivalent of gnus-killed-list.")
1377
1378 (defvar gnus-zombie-list nil
1379   "List of almost dead newsgroups.")
1380
1381 (defvar gnus-description-hashtb nil
1382   "Descriptions of newsgroups.")
1383
1384 (defvar gnus-list-of-killed-groups nil
1385   "List of newsgroups that have recently been killed by the user.")
1386
1387 (defvar gnus-active-hashtb nil
1388   "Hashtable of active articles.")
1389
1390 (defvar gnus-moderated-list nil
1391   "List of moderated newsgroups.")
1392
1393 (defvar gnus-group-marked nil)
1394
1395 (defvar gnus-current-startup-file nil
1396   "Startup file for the current host.")
1397
1398 (defvar gnus-last-search-regexp nil
1399   "Default regexp for article search command.")
1400
1401 (defvar gnus-last-shell-command nil
1402   "Default shell command on article.")
1403
1404 (defvar gnus-current-select-method nil
1405   "The current method for selecting a newsgroup.")
1406
1407 (defvar gnus-group-list-mode nil)
1408
1409 (defvar gnus-article-internal-prepare-hook nil)
1410
1411 (defvar gnus-newsgroup-name nil)
1412 (defvar gnus-newsgroup-begin nil)
1413 (defvar gnus-newsgroup-end nil)
1414 (defvar gnus-newsgroup-last-rmail nil)
1415 (defvar gnus-newsgroup-last-mail nil)
1416 (defvar gnus-newsgroup-last-folder nil)
1417 (defvar gnus-newsgroup-last-file nil)
1418 (defvar gnus-newsgroup-auto-expire nil)
1419 (defvar gnus-newsgroup-active nil)
1420
1421 (defvar gnus-newsgroup-unreads nil
1422   "List of unread articles in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-unselected nil
1425   "List of unselected unread articles in the current newsgroup.")
1426
1427 (defvar gnus-newsgroup-reads nil
1428   "Alist of read articles and article marks in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-marked nil
1431   "List of ticked articles in the current newsgroup (a subset of unread art).")
1432
1433 (defvar gnus-newsgroup-killed nil
1434   "List of ranges of articles that have been through the scoring process.")
1435
1436 (defvar gnus-newsgroup-kill-headers nil)
1437
1438 (defvar gnus-newsgroup-replied nil
1439   "List of articles that have been replied to in the current newsgroup.")
1440
1441 (defvar gnus-newsgroup-expirable nil
1442   "List of articles in the current newsgroup that can be expired.")
1443
1444 (defvar gnus-newsgroup-processable nil
1445   "List of articles in the current newsgroup that can be processed.")
1446
1447 (defvar gnus-newsgroup-bookmarks nil
1448   "List of articles in the current newsgroup that have bookmarks.")
1449
1450 (defvar gnus-newsgroup-dormant nil
1451   "List of dormant articles in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-scored nil
1454   "List of scored articles in the current newsgroup.")
1455
1456 (defvar gnus-newsgroup-headers nil
1457   "List of article headers in the current newsgroup.")
1458 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1459
1460 (defvar gnus-newsgroup-ancient nil
1461   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1462
1463 (defvar gnus-current-article nil)
1464 (defvar gnus-article-current nil)
1465 (defvar gnus-current-headers nil)
1466 (defvar gnus-have-all-headers nil)
1467 (defvar gnus-last-article nil)
1468 (defvar gnus-newsgroup-history nil)
1469 (defvar gnus-current-kill-article nil)
1470
1471 ;; Save window configuration.
1472 (defvar gnus-prev-winconf nil)
1473
1474 ;; Format specs
1475 (defvar gnus-summary-line-format-spec nil)
1476 (defvar gnus-summary-dummy-line-format-spec nil)
1477 (defvar gnus-group-line-format-spec nil)
1478 (defvar gnus-summary-mode-line-format-spec nil)
1479 (defvar gnus-article-mode-line-format-spec nil)
1480 (defvar gnus-group-mode-line-format-spec nil)
1481 (defvar gnus-summary-mark-positions nil)
1482 (defvar gnus-group-mark-positions nil)
1483
1484 (defvar gnus-summary-expunge-below nil)
1485 (defvar gnus-reffed-article-number nil)
1486
1487 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1488 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1489
1490 (defvar gnus-cache-removeable-articles nil)
1491
1492 (defconst gnus-summary-local-variables 
1493   '(gnus-newsgroup-name 
1494     gnus-newsgroup-begin gnus-newsgroup-end 
1495     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1496     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1497     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1498     gnus-newsgroup-unselected gnus-newsgroup-marked
1499     gnus-newsgroup-reads
1500     gnus-newsgroup-replied gnus-newsgroup-expirable
1501     gnus-newsgroup-processable gnus-newsgroup-killed
1502     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1503     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1504     gnus-current-article gnus-current-headers gnus-have-all-headers
1505     gnus-last-article gnus-article-internal-prepare-hook
1506     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1507     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1508     gnus-newsgroup-threads gnus-newsgroup-async
1509     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1510     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1511     gnus-newsgroup-history gnus-newsgroup-ancient
1512     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1513     gnus-cache-removeable-articles)
1514   "Variables that are buffer-local to the summary buffers.")
1515
1516 (defconst gnus-bug-message
1517   "Sending a bug report to the Gnus Towers.
1518 ========================================
1519
1520 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1521 be sent to the Gnus Bug Exterminators. 
1522
1523 At the bottom of the buffer you'll see lots of variable settings.
1524 Please do not delete those.  They will tell the Bug People what your
1525 environment is, so that it will be easier to locate the bugs.
1526
1527 If you have found a bug that makes Emacs go \"beep\", set
1528 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1529 and include the backtrace in your bug report.
1530
1531 Please describe the bug in annoying, painstaking detail.
1532
1533 Thank you for your help in stamping out bugs.
1534 ")
1535
1536 ;;; End of variables.
1537
1538 ;; Define some autoload functions Gnus might use.
1539 (eval-and-compile
1540
1541   ;; Various 
1542   (autoload 'metamail-buffer "metamail")
1543   (autoload 'Info-goto-node "info")
1544   (autoload 'hexl-hex-string-to-integer "hexl")
1545   (autoload 'pp "pp")
1546   (autoload 'pp-to-string "pp")
1547   (autoload 'pp-eval-expression "pp")
1548   (autoload 'mail-extract-address-components "mail-extr")
1549
1550   (autoload 'nnmail-split-fancy "nnmail")
1551   (autoload 'nnvirtual-catchup-group "nnvirtual")
1552
1553   ;; timezone
1554   (autoload 'timezone-make-date-arpa-standard "timezone")
1555   (autoload 'timezone-fix-time "timezone")
1556   (autoload 'timezone-make-sortable-date "timezone")
1557   (autoload 'timezone-make-time-string "timezone")
1558
1559   ;; rmail & friends
1560   (autoload 'mail-position-on-field "sendmail")
1561   (autoload 'mail-setup "sendmail")
1562   (autoload 'rmail-output "rmailout")
1563   (autoload 'news-mail-other-window "rnewspost")
1564   (autoload 'news-reply-yank-original "rnewspost")
1565   (autoload 'news-caesar-buffer-body "rnewspost")
1566   (autoload 'rmail-insert-rmail-file-header "rmail")
1567   (autoload 'rmail-count-new-messages "rmail")
1568   (autoload 'rmail-show-message "rmail")
1569
1570   ;; gnus-soup
1571   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1572   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1573   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1574   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1575   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1576   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1577   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1578
1579   ;; gnus-mh
1580   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1581   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1582   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1583   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1584   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1585   (autoload 'gnus-Folder-save-name "gnus-mh")
1586   (autoload 'gnus-folder-save-name "gnus-mh")
1587
1588   ;; gnus-vis misc
1589   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1590   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1591   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1592   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1593   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1594   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1595   (autoload 'gnus-summary-highlight-line "gnus-vis")
1596   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1597
1598   ;; gnus-vis article
1599   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1600   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1601   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1602   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1603   (autoload 'gnus-article-hide "gnus-vis" nil t)
1604   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1605   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1606   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1607   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1608   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1609   (autoload 'gnus-article-add-button "gnus-vis")
1610
1611   ;; gnus-cite
1612   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1613   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1614   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1615
1616   ;; gnus-kill
1617   (autoload 'gnus-kill "gnus-kill")
1618   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1619   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1620   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1621   (autoload 'gnus-execute "gnus-kill")
1622   (autoload 'gnus-expunge "gnus-kill")
1623
1624   ;; gnus-cache
1625   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1626   (autoload 'gnus-cache-save-buffers "gnus-cache")
1627   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1628   (autoload 'gnus-cache-request-article "gnus-cache")
1629   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1630   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1631   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1632   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1633
1634   ;; gnus-score
1635   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1636   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1637   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1638   (autoload 'gnus-score-save "gnus-score")
1639   (autoload 'gnus-score-headers "gnus-score")
1640   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1641   (autoload 'gnus-score-adaptive "gnus-score")
1642   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1643   (autoload 'gnus-score-find-trace "gnus-score")
1644
1645   ;; gnus-edit
1646   (autoload 'gnus-score-customize "gnus-edit" nil t)
1647
1648   ;; gnus-uu
1649   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1650   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1651   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1652   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1653   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1654   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1655   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1656   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1657   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1658   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1662   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1663   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1664   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1665   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1666   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1667   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1668   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1669   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1670   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1671
1672   ;; gnus-msg
1673   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1674   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1675   (autoload 'gnus-group-mail "gnus-msg" nil t)
1676   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1677   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1678   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1679   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1680   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1681   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1682   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1683   (autoload 'gnus-post-news "gnus-msg" nil t)
1684   (autoload 'gnus-inews-news "gnus-msg" nil t)
1685   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1686   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1687   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1688   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1689   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1690   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1691   (autoload 'gnus-mail-yank-original "gnus-msg")
1692   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1693   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1694   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1695   (autoload 'gnus-article-mail-with-original "gnus-msg")
1696   (autoload 'gnus-article-mail "gnus-msg")
1697   (autoload 'gnus-bug "gnus-msg" nil t)
1698
1699   ;; gnus-vm
1700   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1701   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1702   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1703   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1704   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1705   (autoload 'gnus-yank-article "gnus-vm" nil t)
1706
1707   )
1708
1709 \f
1710
1711 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1712 ;; If you want the cursor to go somewhere else, set these two
1713 ;; functions in some startup hook to whatever you want.
1714 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1715 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1716
1717 ;;; Various macros and substs.
1718
1719 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1720   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1721   (` (let ((GnusStartBufferWindow (selected-window)))
1722        (unwind-protect
1723            (progn
1724              (pop-to-buffer (, buffer))
1725              (,@ forms))
1726          (select-window GnusStartBufferWindow)))))
1727
1728 (defmacro gnus-gethash (string hashtable)
1729   "Get hash value of STRING in HASHTABLE."
1730   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1731   ;;(` (abbrev-expansion (, string) (, hashtable)))
1732   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1733
1734 (defmacro gnus-sethash (string value hashtable)
1735   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1736   ;; We cannot use define-abbrev since it only accepts string as value.
1737   ;; (set (intern string hashtable) value))
1738   (` (set (intern (, string) (, hashtable)) (, value))))
1739
1740 (defsubst gnus-buffer-substring (beg end)
1741   (buffer-substring (match-beginning beg) (match-end end)))
1742
1743 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1744 ;;   function `substring' might cut on a middle of multi-octet
1745 ;;   character.
1746 (defun gnus-truncate-string (str width)
1747   (substring str 0 width))
1748
1749 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1750 ;; to limit the length of a string. This function is necessary since
1751 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1752 (defsubst gnus-limit-string (str width)
1753   (if (> (length str) width)
1754       (substring str 0 width)
1755     str))
1756
1757 (defsubst gnus-simplify-subject-re (subject)
1758   "Remove \"Re:\" from subject lines."
1759   (let ((case-fold-search t))
1760     (if (string-match "^re: *" subject)
1761         (substring subject (match-end 0))
1762       subject)))
1763
1764 (defsubst gnus-goto-char (point)
1765   (and point (goto-char point)))
1766
1767 (defmacro gnus-buffer-exists-p (buffer)
1768   (` (and (, buffer)
1769           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1770                    (, buffer)))))
1771
1772 (defmacro gnus-kill-buffer (buffer)
1773   (` (if (gnus-buffer-exists-p (, buffer))
1774          (kill-buffer (, buffer)))))
1775
1776 (defsubst gnus-point-at-bol ()
1777   "Return point at the beginning of line."
1778   (let ((p (point)))
1779     (beginning-of-line)
1780     (prog1
1781         (point)
1782       (goto-char p))))
1783
1784 (defsubst gnus-point-at-eol ()
1785   "Return point at the beginning of line."
1786   (let ((p (point)))
1787     (end-of-line)
1788     (prog1
1789         (point)
1790       (goto-char p))))
1791
1792 ;; Delete the current line (and the next N lines.);
1793 (defmacro gnus-delete-line (&optional n)
1794   (` (delete-region (progn (beginning-of-line) (point))
1795                     (progn (forward-line (, (or n 1))) (point)))))
1796
1797 ;;; Load the compatability functions. 
1798
1799 (require 'gnus-ems)
1800
1801 \f
1802 ;;;
1803 ;;; Gnus Utility Functions
1804 ;;;
1805
1806 (defun gnus-extract-address-components (from)
1807   (let (name address)
1808     ;; First find the address - the thing with the @ in it.  This may
1809     ;; not be accurate in mail addresses, but does the trick most of
1810     ;; the time in news messages.
1811     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1812         (setq address (substring from (match-beginning 0) (match-end 0))))
1813     ;; Then we check whether the "name <address>" format is used.
1814     (and address
1815          (string-match (concat "<" (regexp-quote address) ">") from)
1816          (and (setq name (substring from 0 (1- (match-beginning 0))))
1817               ;; Strip any quotes from the name.
1818               (string-match "\".*\"" name)
1819               (setq name (substring name 1 (1- (match-end 0))))))
1820     ;; If not, then "address (name)" is used.
1821     (or name
1822         (and (string-match "(.+)" from)
1823              (setq name (substring from (1+ (match-beginning 0)) 
1824                                    (1- (match-end 0)))))
1825         (and (string-match "()" from)
1826              (setq name address))
1827         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1828         ;; XOVER might not support folded From headers.
1829         (and (string-match "(.*" from)
1830              (setq name (substring from (1+ (match-beginning 0)) 
1831                                    (match-end 0)))))
1832     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1833     (list (or name from) (or address from))))
1834
1835 (defun gnus-fetch-field (field)
1836   "Return the value of the header FIELD of current article."
1837   (save-excursion
1838     (save-restriction
1839       (let ((case-fold-search t))
1840         (gnus-narrow-to-headers)
1841         (mail-fetch-field field)))))
1842
1843 (defun gnus-goto-colon ()
1844   (beginning-of-line)
1845   (search-forward ":" (gnus-point-at-eol) t))
1846
1847 (defun gnus-narrow-to-headers ()
1848   (widen)
1849   (save-excursion
1850     (narrow-to-region
1851      (goto-char (point-min))
1852      (if (search-forward "\n\n" nil t)
1853          (1- (point))
1854        (point-max)))))
1855
1856 (defvar gnus-old-specs nil)
1857
1858 (defun gnus-update-format-specifications ()
1859   (gnus-make-thread-indent-array)
1860
1861   (let ((formats '(summary summary-dummy group 
1862                            summary-mode group-mode article-mode))
1863         old-format new-format)
1864     (while formats
1865       (setq new-format (symbol-value
1866                         (intern (format "gnus-%s-line-format" (car formats)))))
1867       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1868                (equal old-format new-format))
1869           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1870                (gnus-parse-format
1871                 new-format
1872                 (symbol-value 
1873                  (intern (format "gnus-%s-line-format-alist"
1874                                  (if (eq (car formats) 'article-mode)
1875                                      'summary-mode (car formats))))))))
1876       (setq gnus-old-specs (cons (cons (car formats) new-format)
1877                                  (delq (car formats) gnus-old-specs)))
1878       (setq formats (cdr formats))))
1879       
1880   (gnus-update-group-mark-positions)
1881   (gnus-update-summary-mark-positions)
1882
1883   (if (and (string-match "%D" gnus-group-line-format)
1884            (not gnus-description-hashtb)
1885            gnus-read-active-file)
1886       (gnus-read-all-descriptions-files)))
1887
1888 (defun gnus-update-summary-mark-positions ()
1889   (save-excursion
1890     (let ((gnus-replied-mark 129)
1891           (gnus-score-below-mark 130)
1892           (gnus-score-over-mark 130)
1893           (thread nil)
1894           pos)
1895       (gnus-set-work-buffer)
1896       (gnus-summary-insert-line 
1897        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1898       (goto-char (point-min))
1899       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1900                                          (- (point) 2)))))
1901       (goto-char (point-min))
1902       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1903                                           (- (point) 2))) pos))
1904       (goto-char (point-min))
1905       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1906                                         (- (point) 2))) pos))
1907       (setq gnus-summary-mark-positions pos))))
1908
1909 (defun gnus-update-group-mark-positions ()
1910   (save-excursion
1911     (let ((gnus-process-mark 128)
1912           (gnus-group-marked '("dummy.group")))
1913       (gnus-sethash "dummy.group" '(0 . 0) gnus-active-hashtb)
1914       (gnus-set-work-buffer)
1915       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1916       (goto-char (point-min))
1917       (setq gnus-group-mark-positions
1918             (list (cons 'process (and (search-forward "\200" nil t)
1919                                       (- (point) 2))))))))
1920
1921 (defun gnus-mouse-face-function (form)
1922   (` (let ((string (, form)))
1923        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1924        string)))
1925
1926 (defun gnus-max-width-function (el max-width)
1927   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1928   (` (let* ((val (eval (, el)))
1929             (valstr (if (numberp val)
1930                         (int-to-string val) val)))
1931        (if (> (length valstr) (, max-width))
1932            (substring valstr 0 (, max-width))
1933          valstr))))
1934
1935 (defun gnus-parse-format (format spec-alist)
1936   ;; This function parses the FORMAT string with the help of the
1937   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1938   ;; string.  If the FORMAT string contains the specifiers %( and %)
1939   ;; the text between them will have the mouse-face text property.
1940   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1941       (if (and gnus-visual gnus-mouse-face)
1942           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1943                 (button (substring format (match-beginning 2) (match-end 2)))
1944                 (post (substring format (match-beginning 3) (match-end 3))))
1945             (list 'concat
1946                   (gnus-parse-simple-format pre spec-alist)
1947                   (gnus-mouse-face-function 
1948                    (gnus-parse-simple-format button spec-alist))
1949                   (gnus-parse-simple-format post spec-alist)))
1950         (gnus-parse-simple-format
1951          (concat (substring format (match-beginning 1) (match-end 1))
1952                  (substring format (match-beginning 2) (match-end 2))
1953                  (substring format (match-beginning 3) (match-end 3)))
1954          spec-alist))
1955     (gnus-parse-simple-format format spec-alist)))
1956
1957 (defun gnus-parse-simple-format (format spec-alist)
1958   ;; This function parses the FORMAT string with the help of the
1959   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1960   ;; string. The list will consist of the symbol `format', a format
1961   ;; specification string, and a list of forms depending on the
1962   ;; SPEC-ALIST.
1963   (let ((max-width 0)
1964         spec flist fstring newspec elem beg)
1965     (save-excursion
1966       (gnus-set-work-buffer)
1967       (insert format)
1968       (goto-char (point-min))
1969       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1970         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1971                                                      (match-end 2))))
1972         ;; First check if there are any specs that look anything like
1973         ;; "%12,12A", ie. with a "max width specification". These have
1974         ;; to be treated specially.
1975         (if (setq beg (match-beginning 1))
1976             (setq max-width 
1977                   (string-to-int 
1978                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1979           (setq max-width 0)
1980           (setq beg (match-beginning 2)))
1981         ;; Find the specification from `spec-alist'.
1982         (if (not (setq elem (cdr (assq spec spec-alist))))
1983             (setq elem '("*" ?s)))
1984         ;; Treat user defined format specifiers specially
1985         (and (eq (car elem) 'user-defined)
1986              (setq elem
1987                    (list 
1988                     (list (intern (concat "gnus-user-format-function-"
1989                                           (buffer-substring
1990                                            (match-beginning 3)
1991                                            (match-end 3))))
1992                           'header)
1993                     ?s))
1994              (delete-region (match-beginning 3) (match-end 3)))
1995         (if (not (zerop max-width))
1996             (let ((el (car elem)))
1997               (cond ((= (car (cdr elem)) ?c) 
1998                      (setq el (list 'char-to-string el)))
1999                     ((= (car (cdr elem)) ?d)
2000                      (numberp el) (setq el (list 'int-to-string el))))
2001               (setq flist (cons (gnus-max-width-function el max-width)
2002                                 flist))
2003               (setq newspec ?s))
2004           (setq flist (cons (car elem) flist))
2005           (setq newspec (car (cdr elem))))
2006         ;; Remove the old specification (and possibly a ",12" string).
2007         (delete-region beg (match-end 2))
2008         ;; Insert the new specification.
2009         (goto-char beg)
2010         (insert newspec))
2011       (setq fstring (buffer-substring 1 (point-max))))
2012     (cons 'format (cons fstring (nreverse flist)))))
2013
2014 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2015 (defun gnus-read-init-file ()
2016   (and gnus-init-file
2017        (or (and (file-exists-p gnus-init-file) 
2018                 ;; Don't try to load a directory.
2019                 (not (file-directory-p gnus-init-file)))
2020            (file-exists-p (concat gnus-init-file ".el"))
2021            (file-exists-p (concat gnus-init-file ".elc")))
2022        (load gnus-init-file nil t)))
2023
2024 (defun gnus-set-work-buffer ()
2025   (if (get-buffer gnus-work-buffer)
2026       (progn
2027         (set-buffer gnus-work-buffer)
2028         (erase-buffer))
2029     (set-buffer (get-buffer-create gnus-work-buffer))
2030     (kill-all-local-variables)
2031     (buffer-disable-undo (current-buffer))
2032     (gnus-add-current-to-buffer-list)))
2033
2034 ;; Article file names when saving.
2035
2036 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2037   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2038 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2039 Otherwise, it is like ~/News/news/group/num."
2040   (let ((default
2041           (expand-file-name
2042            (concat (if (gnus-use-long-file-name 'not-save)
2043                        (gnus-capitalize-newsgroup newsgroup)
2044                      (gnus-newsgroup-directory-form newsgroup))
2045                    "/" (int-to-string (header-number headers)))
2046            (or gnus-article-save-directory "~/News"))))
2047     (if (and last-file
2048              (string-equal (file-name-directory default)
2049                            (file-name-directory last-file))
2050              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2051         default
2052       (or last-file default))))
2053
2054 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2055   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2056 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2057 Otherwise, it is like ~/News/news/group/num."
2058   (let ((default
2059           (expand-file-name
2060            (concat (if (gnus-use-long-file-name 'not-save)
2061                        newsgroup
2062                      (gnus-newsgroup-directory-form newsgroup))
2063                    "/" (int-to-string (header-number headers)))
2064            (or gnus-article-save-directory "~/News"))))
2065     (if (and last-file
2066              (string-equal (file-name-directory default)
2067                            (file-name-directory last-file))
2068              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2069         default
2070       (or last-file default))))
2071
2072 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2073   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2074 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2075 Otherwise, it is like ~/News/news/group/news."
2076   (or last-file
2077       (expand-file-name
2078        (if (gnus-use-long-file-name 'not-save)
2079            (gnus-capitalize-newsgroup newsgroup)
2080          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2081        (or gnus-article-save-directory "~/News"))))
2082
2083 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2084   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2085 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2086 Otherwise, it is like ~/News/news/group/news."
2087   (or last-file
2088       (expand-file-name
2089        (if (gnus-use-long-file-name 'not-save)
2090            newsgroup
2091          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2092        (or gnus-article-save-directory "~/News"))))
2093
2094 ;; For subscribing new newsgroup
2095
2096 (defun gnus-subscribe-hierarchical-interactive (groups)
2097   (let ((groups (sort groups 'string<))
2098         prefixes prefix start ans group starts)
2099     (while groups
2100       (setq prefixes (list "^"))
2101       (while (and groups prefixes)
2102         (while (not (string-match (car prefixes) (car groups)))
2103           (setq prefixes (cdr prefixes)))
2104         (setq prefix (car prefixes))
2105         (setq start (1- (length prefix)))
2106         (if (and (string-match "[^\\.]\\." (car groups) start)
2107                  (cdr groups)
2108                  (setq prefix 
2109                        (concat "^" (substring (car groups) 0 (match-end 0))))
2110                  (string-match prefix (car (cdr groups))))
2111             (progn
2112               (setq prefixes (cons prefix prefixes))
2113               (message "Descend hierarchy %s? ([y]nsq): " 
2114                        (substring prefix 1 (1- (length prefix))))
2115               (setq ans (read-char))
2116               (cond ((= ans ?n)
2117                      (while (and groups 
2118                                  (string-match prefix 
2119                                                (setq group (car groups))))
2120                        (setq gnus-killed-list 
2121                              (cons group gnus-killed-list))
2122                        (gnus-sethash group group gnus-killed-hashtb)
2123                        (setq groups (cdr groups)))
2124                      (setq starts (cdr starts)))
2125                     ((= ans ?s)
2126                      (while (and groups 
2127                                  (string-match prefix 
2128                                                (setq group (car groups))))
2129                        (gnus-sethash group group gnus-killed-hashtb)
2130                        (gnus-subscribe-alphabetically (car groups))
2131                        (setq groups (cdr groups)))
2132                      (setq starts (cdr starts)))
2133                     ((= ans ?q)
2134                      (while groups
2135                        (setq group (car groups))
2136                        (setq gnus-killed-list (cons group gnus-killed-list))
2137                        (gnus-sethash group group gnus-killed-hashtb)
2138                        (setq groups (cdr groups))))
2139                     (t nil)))
2140           (message "Subscribe %s? ([n]yq)" (car groups))
2141           (setq ans (read-char))
2142           (setq group (car groups))
2143           (cond ((= ans ?y)
2144                  (gnus-subscribe-alphabetically (car groups))
2145                  (gnus-sethash group group gnus-killed-hashtb))
2146                 ((= ans ?q)
2147                  (while groups
2148                    (setq group (car groups))
2149                    (setq gnus-killed-list (cons group gnus-killed-list))
2150                    (gnus-sethash group group gnus-killed-hashtb)
2151                    (setq groups (cdr groups))))
2152                 (t 
2153                  (setq gnus-killed-list (cons group gnus-killed-list))
2154                  (gnus-sethash group group gnus-killed-hashtb)))
2155           (setq groups (cdr groups)))))))
2156
2157 (defun gnus-subscribe-randomly (newsgroup)
2158   "Subscribe new NEWSGROUP by making it the first newsgroup."
2159   (gnus-subscribe-newsgroup newsgroup))
2160
2161 (defun gnus-subscribe-alphabetically (newgroup)
2162   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2163   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2164   (let ((groups (cdr gnus-newsrc-alist))
2165         before)
2166     (while (and (not before) groups)
2167       (if (string< newgroup (car (car groups)))
2168           (setq before (car (car groups)))
2169         (setq groups (cdr groups))))
2170     (gnus-subscribe-newsgroup newgroup before)))
2171
2172 (defun gnus-subscribe-hierarchically (newgroup)
2173   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2174   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2175   (save-excursion
2176     (set-buffer (find-file-noselect gnus-current-startup-file))
2177     (let ((groupkey newgroup)
2178           before)
2179       (while (and (not before) groupkey)
2180         (goto-char (point-min))
2181         (let ((groupkey-re
2182                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2183           (while (and (re-search-forward groupkey-re nil t)
2184                       (progn
2185                         (setq before (buffer-substring
2186                                       (match-beginning 1) (match-end 1)))
2187                         (string< before newgroup)))))
2188         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2189         (setq groupkey
2190               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2191                   (substring groupkey (match-beginning 1) (match-end 1)))))
2192       (gnus-subscribe-newsgroup newgroup before))))
2193
2194 (defun gnus-subscribe-interactively (newsgroup)
2195   "Subscribe new NEWSGROUP interactively.
2196 It is inserted in hierarchical newsgroup order if subscribed. If not,
2197 it is killed."
2198   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2199       (gnus-subscribe-hierarchically newsgroup)
2200     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2201
2202 (defun gnus-subscribe-zombies (newsgroup)
2203   "Make new NEWSGROUP a zombie group."
2204   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2205
2206 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2207   "Subscribe new NEWSGROUP.
2208 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2209 the first newsgroup."
2210   ;; We subscribe the group by changing its level to `subscribed'.
2211   (gnus-group-change-level 
2212    newsgroup gnus-level-default-subscribed
2213    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2214   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2215
2216 ;; For directories
2217
2218 (defun gnus-newsgroup-directory-form (newsgroup)
2219   "Make hierarchical directory name from NEWSGROUP name."
2220   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2221         (len (length newsgroup))
2222         idx)
2223     ;; If this is a foreign group, we don't want to translate the
2224     ;; entire name.  
2225     (if (setq idx (string-match ":" newsgroup))
2226         (aset newsgroup idx ?/)
2227       (setq idx 0))
2228     ;; Replace all occurrences of `.' with `/'.
2229     (while (< idx len)
2230       (if (= (aref newsgroup idx) ?.)
2231           (aset newsgroup idx ?/))
2232       (setq idx (1+ idx)))
2233     newsgroup))
2234
2235 (defun gnus-newsgroup-saveable-name (group)
2236   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2237   ;; with dots.
2238   (gnus-replace-chars-in-string group ?/ ?.))
2239
2240 (defun gnus-make-directory (dir)
2241   "Make DIRECTORY recursively."
2242   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2243   ;; of the many mysteries of the universe.
2244  (let* ((dir (expand-file-name dir default-directory))
2245          dirs err)
2246     (if (string-match "/$" dir)
2247         (setq dir (substring dir 0 (match-beginning 0))))
2248     ;; First go down the path until we find a directory that exists.
2249     (while (not (file-exists-p dir))
2250       (setq dirs (cons dir dirs))
2251       (string-match "/[^/]+$" dir)
2252       (setq dir (substring dir 0 (match-beginning 0))))
2253     ;; Then create all the subdirs.
2254     (while (and dirs (not err))
2255       (condition-case ()
2256           (make-directory (car dirs))
2257         (error (setq err t)))
2258       (setq dirs (cdr dirs)))
2259     ;; We return whether we were successful or not. 
2260     (not dirs)))
2261
2262 (defun gnus-capitalize-newsgroup (newsgroup)
2263   "Capitalize NEWSGROUP name."
2264   (and (not (zerop (length newsgroup)))
2265        (concat (char-to-string (upcase (aref newsgroup 0)))
2266                (substring newsgroup 1))))
2267
2268 ;; Var
2269
2270 (defun gnus-simplify-subject (subject &optional re-only)
2271   "Remove `Re:' and words in parentheses.
2272 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2273   (let ((case-fold-search t))           ;Ignore case.
2274     ;; Remove `Re:' and `Re^N:'.
2275     (if (string-match "^re:[ \t]*" subject)
2276         (setq subject (substring subject (match-end 0))))
2277     ;; Remove words in parentheses from end.
2278     (or re-only
2279         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2280           (setq subject (substring subject 0 (match-beginning 0)))))
2281     ;; Return subject string.
2282     subject))
2283
2284 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2285 ;; all whitespace.
2286 (defun gnus-simplify-subject-fuzzy (subject)
2287   (let ((case-fold-search t))
2288     (save-excursion
2289       (gnus-set-work-buffer)
2290       (insert subject)
2291       (inline (gnus-simplify-buffer-fuzzy))
2292       (buffer-string))))
2293
2294 (defun gnus-simplify-buffer-fuzzy ()
2295   (goto-char (point-min))
2296   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2297   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2298                             nil t)
2299     (replace-match "" t t))
2300   (goto-char (point-min))
2301   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2302     (replace-match "" t t))
2303   (goto-char (point-min))
2304   (while (re-search-forward "[ \t]+" nil t)
2305     (replace-match " " t t))
2306   (goto-char (point-min))
2307   (while (re-search-forward "[ \t]+$" nil t)
2308     (replace-match "" t t))
2309   (goto-char (point-min))
2310   (while (re-search-forward "^[ \t]+" nil t)
2311     (replace-match "" t t))
2312   (if gnus-simplify-subject-fuzzy-regexp
2313       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2314         (replace-match "" t t))))
2315
2316 ;; Add the current buffer to the list of buffers to be killed on exit. 
2317 (defun gnus-add-current-to-buffer-list ()
2318   (or (memq (current-buffer) gnus-buffer-list)
2319       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2320
2321 (defun gnus-string> (s1 s2)
2322   (not (or (string< s1 s2)
2323            (string= s1 s2))))
2324
2325 ;; Functions accessing headers.
2326 ;; Functions are more convenient than macros in some cases.
2327
2328 (defun gnus-header-number (header)
2329   (header-number header))
2330
2331 (defun gnus-header-subject (header)
2332   (header-subject header))
2333
2334 (defun gnus-header-from (header)
2335   (header-from header))
2336
2337 (defun gnus-header-xref (header)
2338   (header-xref header))
2339
2340 (defun gnus-header-lines (header)
2341   (header-lines header))
2342
2343 (defun gnus-header-date (header)
2344   (header-date header))
2345
2346 (defun gnus-header-id (header)
2347   (header-id header))
2348
2349 (defun gnus-header-references (header)
2350   (header-references header))
2351
2352 ;;; General various misc type functions.
2353
2354 (defun gnus-clear-system ()
2355   "Clear all variables and buffers."
2356   ;; Clear Gnus variables.
2357   (let ((variables gnus-variable-list))
2358     (while variables
2359       (set (car variables) nil)
2360       (setq variables (cdr variables))))
2361   ;; Clear other internal variables.
2362   (setq gnus-list-of-killed-groups nil
2363         gnus-have-read-active-file nil
2364         gnus-newsrc-alist nil
2365         gnus-newsrc-hashtb nil
2366         gnus-killed-list nil
2367         gnus-zombie-list nil
2368         gnus-killed-hashtb nil
2369         gnus-active-hashtb nil
2370         gnus-moderated-list nil
2371         gnus-description-hashtb nil
2372         gnus-newsgroup-headers nil
2373         gnus-newsgroup-headers-hashtb-by-number nil
2374         gnus-newsgroup-name nil
2375         gnus-server-alist nil
2376         gnus-current-select-method nil)
2377   ;; Reset any score variables.
2378   (and (boundp 'gnus-score-cache)
2379        (set 'gnus-score-cache nil))
2380   (and (boundp 'gnus-internal-global-score-files)
2381        (set 'gnus-internal-global-score-files nil))
2382   ;; Kill the startup file.
2383   (and gnus-current-startup-file
2384        (get-file-buffer gnus-current-startup-file)
2385        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2386   ;; Save any cache buffers.
2387   (and gnus-use-cache (gnus-cache-save-buffers))
2388   ;; Clear the dribble buffer.
2389   (gnus-dribble-clear)
2390   ;; Kill global KILL file buffer.
2391   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2392       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2393   (gnus-kill-buffer nntp-server-buffer)
2394   ;; Kill Gnus buffers.
2395   (while gnus-buffer-list
2396     (gnus-kill-buffer (car gnus-buffer-list))
2397     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2398
2399 (defun gnus-windows-old-to-new (setting)
2400   (if (symbolp setting)
2401       (setq setting 
2402             (cond ((eq setting 'SelectArticle)
2403                    'article)
2404                   ((eq setting 'SelectSubject)
2405                    'summary)
2406                   ((eq setting 'SelectNewsgroup)
2407                    'group)
2408                   (t setting))))
2409   (if (or (listp setting)
2410           (not (and gnus-window-configuration
2411                     (memq setting '(group summary article)))))
2412       setting
2413     (let* ((setting (if (eq setting 'group) 
2414                         (if (assq 'newsgroup gnus-window-configuration)
2415                             'newsgroup
2416                           'newsgroups) setting))
2417            (elem (car (cdr (assq setting gnus-window-configuration))))
2418            (total (apply '+ elem))
2419            (types '(group summary article))
2420            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2421            (i 0)
2422            perc
2423            out)
2424       (while (< i 3)
2425         (or (zerop (nth i elem))
2426             (progn
2427               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2428               (setq out (cons (if (eq pbuf (nth i types))
2429                                   (vector (nth i types) perc 'point)
2430                                 (vector (nth i types) perc))
2431                               out))))
2432         (setq i (1+ i)))
2433       (list (nreverse out)))))
2434            
2435 (defun gnus-add-configuration (conf)
2436   (setq gnus-buffer-configuration 
2437         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2438                          gnus-buffer-configuration))))
2439
2440 (defun gnus-configure-windows (setting &optional force)
2441   (setq setting (gnus-windows-old-to-new setting))
2442   (let ((r (if (symbolp setting)
2443                (cdr (assq setting gnus-buffer-configuration))
2444              setting))
2445         (in-buf (current-buffer))
2446         rule val w height hor ohor heights sub jump-buffer
2447         rel total to-buf all-visible)
2448     (or r (error "No such setting: %s" setting))
2449
2450     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2451         ;; All the windows mentioned are already visibe, so we just
2452         ;; put point in the assigned buffer, and do not touch the
2453         ;; winconf. 
2454         (select-window (get-buffer-window all-visible))
2455
2456       ;; Either remove all windows or just remove all Gnus windows.
2457       (if gnus-use-full-window
2458           (delete-other-windows)
2459         (gnus-remove-some-windows)
2460         (switch-to-buffer nntp-server-buffer))
2461
2462       (while r
2463         (setq hor (car r)
2464               ohor nil)
2465
2466         ;; We have to do the (possible) horizontal splitting before the
2467         ;; vertical. 
2468         (if (and (listp (car hor)) 
2469                  (eq (car (car hor)) 'horizontal))
2470             (progn
2471               (split-window 
2472                nil
2473                (if (integerp (nth 1 (car hor)))
2474                    (nth 1 (car hor))
2475                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2476                t)
2477               (setq hor (cdr hor))))
2478
2479         ;; Go through the rules and eval the elements that are to be
2480         ;; evaled.  
2481         (while hor
2482           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2483               (progn
2484                 ;; Expand short buffer name.
2485                 (setq w (aref val 0))
2486                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2487                      (progn
2488                        (setq val (apply 'vector (mapcar 'identity val)))
2489                        (aset val 0 w)))
2490                 (setq ohor (cons val ohor))))
2491           (setq hor (cdr hor)))
2492         (setq rule (cons (nreverse ohor) rule))
2493         (setq r (cdr r)))
2494       (setq rule (nreverse rule))
2495
2496       ;; We tally the window sizes.
2497       (setq total (window-height))
2498       (while rule
2499         (setq hor (car rule))
2500         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2501             (setq hor (cdr hor)))
2502         (setq sub 0)
2503         (while hor
2504           (setq rel (aref (car hor) 1)
2505                 heights (cons
2506                          (cond ((and (floatp rel) (= 1.0 rel))
2507                                 'x)
2508                                ((integerp rel)
2509                                 rel)
2510                                (t
2511                                 (max (floor (* total rel)) 4)))
2512                          heights)
2513                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2514                 hor (cdr hor)))
2515         (setq heights (nreverse heights)
2516               hor (car rule))
2517
2518         ;; We then go through these heighs and create windows for them.
2519         (while heights
2520           (setq height (car heights)
2521                 heights (cdr heights))
2522           (and (eq height 'x)
2523                (setq height (- total sub)))
2524           (and heights
2525                (split-window nil height))
2526           (setq to-buf (aref (car hor) 0))
2527           (switch-to-buffer 
2528            (cond ((not to-buf)
2529                   in-buf)
2530                  ((symbolp to-buf)
2531                   (symbol-value (aref (car hor) 0)))
2532                  (t
2533                   (aref (car hor) 0))))
2534           (and (> (length (car hor)) 2)
2535                (eq (aref (car hor) 2) 'point)
2536                (setq jump-buffer (current-buffer)))
2537           (other-window 1)
2538           (setq hor (cdr hor)))
2539       
2540         (setq rule (cdr rule)))
2541
2542       ;; Finally, we pop to the buffer that's supposed to have point. 
2543       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2544
2545       (select-window (get-buffer-window jump-buffer))
2546       (set-buffer jump-buffer))))
2547
2548 (defun gnus-all-windows-visible-p (rule)
2549   (let (invisible hor jump-buffer val buffer)
2550     ;; Go through the rules and eval the elements that are to be
2551     ;; evaled.  
2552     (while (and rule (not invisible))
2553       (setq hor (car rule)
2554             rule (cdr rule))
2555       (while (and hor (not invisible))
2556         (if (setq val (if (vectorp (car hor)) 
2557                           (car hor)
2558                         (if (not (eq (car (car hor)) 'horizontal))
2559                             (eval (car hor)))))
2560             (progn
2561               ;; Expand short buffer name.
2562               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2563                                (aref val 0)))
2564               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2565                              buffer))
2566               (and (> (length val) 2) (eq 'point (aref val 2))
2567                    (setq jump-buffer buffer))
2568               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2569         (setq hor (cdr hor))))
2570     (and (not invisible) jump-buffer)))
2571
2572 (defun gnus-window-top-edge (&optional window)
2573   (nth 1 (window-edges window)))
2574
2575 (defun gnus-remove-some-windows ()
2576   (let ((buffers gnus-window-to-buffer)
2577         buf bufs lowest-buf lowest)
2578     (save-excursion
2579       ;; Remove windows on all known Gnus buffers.
2580       (while buffers
2581         (setq buf (cdr (car buffers)))
2582         (if (symbolp buf)
2583             (setq buf (and (boundp buf) (symbol-value buf))))
2584         (and buf 
2585              (get-buffer-window buf)
2586              (progn
2587                (setq bufs (cons buf bufs))
2588                (pop-to-buffer buf)
2589                (if (or (not lowest)
2590                        (< (gnus-window-top-edge) lowest))
2591                    (progn
2592                      (setq lowest (gnus-window-top-edge))
2593                      (setq lowest-buf buf)))))
2594         (setq buffers (cdr buffers)))
2595       ;; Remove windows on *all* summary buffers.
2596       (let (wins)
2597         (walk-windows
2598          (lambda (win)
2599            (let ((buf (window-buffer win)))
2600              (if (string-match  "^\\*Summary" (buffer-name buf))
2601              (progn
2602                (setq bufs (cons buf bufs))
2603                (pop-to-buffer buf)
2604                (if (or (not lowest)
2605                        (< (gnus-window-top-edge) lowest))
2606                    (progn
2607                      (setq lowest-buf buf)
2608                      (setq lowest (gnus-window-top-edge))))))))))
2609       (and lowest-buf 
2610            (progn
2611              (pop-to-buffer lowest-buf)
2612              (switch-to-buffer nntp-server-buffer)))
2613       (while bufs
2614         (and (not (eq (car bufs) lowest-buf))
2615              (delete-windows-on (car bufs)))
2616         (setq bufs (cdr bufs))))))
2617                           
2618 (defun gnus-version ()
2619   "Version numbers of this version of Gnus."
2620   (interactive)
2621   (let ((methods gnus-valid-select-methods)
2622         (mess gnus-version)
2623         meth)
2624     ;; Go through all the legal select methods and add their version
2625     ;; numbers to the total version string. Only the backends that are
2626     ;; currently in use will have their message numbers taken into
2627     ;; consideration. 
2628     (while methods
2629       (setq meth (intern (concat (car (car methods)) "-version")))
2630       (and (boundp meth)
2631            (stringp (symbol-value meth))
2632            (setq mess (concat mess "; " (symbol-value meth))))
2633       (setq methods (cdr methods)))
2634     (gnus-message 2 mess)))
2635
2636 (defun gnus-info-find-node ()
2637   "Find Info documentation of Gnus."
2638   (interactive)
2639   ;; Enlarge info window if needed.
2640   (let ((mode major-mode))
2641     (gnus-configure-windows 'info)
2642     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2643
2644 (defun gnus-overload-functions (&optional overloads)
2645   "Overload functions specified by optional argument OVERLOADS.
2646 If nothing is specified, use the variable gnus-overload-functions."
2647   (let ((defs nil)
2648         (overloads (or overloads gnus-overload-functions)))
2649     (while overloads
2650       (setq defs (car overloads))
2651       (setq overloads (cdr overloads))
2652       ;; Load file before overloading function if necessary.  Make
2653       ;; sure we cannot use `require' always.
2654       (and (not (fboundp (car defs)))
2655            (car (cdr (cdr defs)))
2656            (load (car (cdr (cdr defs))) nil 'nomessage))
2657       (fset (car defs) (car (cdr defs))))))
2658
2659 (defun gnus-replace-chars-in-string (string from to)
2660   "Replace characters in STRING from FROM to TO."
2661   (let ((string (substring string 0))   ;Copy string.
2662         (len (length string))
2663         (idx 0))
2664     ;; Replace all occurrences of FROM with TO.
2665     (while (< idx len)
2666       (if (= (aref string idx) from)
2667           (aset string idx to))
2668       (setq idx (1+ idx)))
2669     string))
2670
2671 (defun gnus-days-between (date1 date2)
2672   ;; Return the number of days between date1 and date2.
2673   (- (gnus-day-number date1) (gnus-day-number date2)))
2674
2675 (defun gnus-day-number (date)
2676   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2677                      (timezone-parse-date date))))
2678     (timezone-absolute-from-gregorian 
2679      (nth 1 dat) (nth 2 dat) (car dat))))
2680
2681 ;; Returns a floating point number that says how many seconds have
2682 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2683 (defun gnus-seconds-since-epoch (date)
2684   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2685                         (timezone-parse-date date)))
2686          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2687                         (timezone-parse-time
2688                          (aref (timezone-parse-date date) 3))))
2689          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2690                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2691          (tday (- (timezone-absolute-from-gregorian 
2692                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2693                   (timezone-absolute-from-gregorian 
2694                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2695     (+ (nth 2 ttime)
2696        (* (nth 1 ttime) 60)
2697        (* 1.0 (nth 0 ttime) 60 60)
2698        (* 1.0 tday 60 60 24))))
2699
2700 (defun gnus-file-newer-than (file date)
2701   (let ((fdate (nth 5 (file-attributes file))))
2702     (or (> (car fdate) (car date))
2703         (and (= (car fdate) (car date))
2704              (> (nth 1 fdate) (nth 1 date))))))
2705
2706 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2707 ;; the echo area.
2708 (defun gnus-y-or-n-p (prompt)
2709   (prog1
2710       (y-or-n-p prompt)
2711     (message "")))
2712
2713 (defun gnus-yes-or-no-p (prompt)
2714   (prog1
2715       (yes-or-no-p prompt)
2716     (message "")))
2717
2718 ;; Check whether to use long file names.
2719 (defun gnus-use-long-file-name (symbol)
2720   ;; The variable has to be set...
2721   (and gnus-use-long-file-name
2722        ;; If it isn't a list, then we return t.
2723        (or (not (listp gnus-use-long-file-name))
2724            ;; If it is a list, and the list contains `symbol', we
2725            ;; return nil.  
2726            (not (memq symbol gnus-use-long-file-name)))))
2727
2728 ;; I suspect there's a better way, but I haven't taken the time to do
2729 ;; it yet. -erik selberg@cs.washington.edu
2730 (defun gnus-dd-mmm (messy-date)
2731   "Return a string like DD-MMM from a big messy string"
2732   (let ((datevec (timezone-parse-date messy-date)))
2733     (format "%2s-%s"
2734             (or (aref datevec 2) "??")
2735             (capitalize
2736              (or (car 
2737                   (nth (1- (string-to-number (aref datevec 1)))
2738                        timezone-months-assoc))
2739                  "???")))))
2740
2741 ;; Make a hash table (default and minimum size is 255).
2742 ;; Optional argument HASHSIZE specifies the table size.
2743 (defun gnus-make-hashtable (&optional hashsize)
2744   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2745
2746 ;; Make a number that is suitable for hashing; bigger than MIN and one
2747 ;; less than 2^x.
2748 (defun gnus-create-hash-size (min)
2749   (let ((i 1))
2750     (while (< i min)
2751       (setq i (* 2 i)))
2752     (1- i)))
2753
2754 ;; Show message if message has a lower level than `gnus-verbose'. 
2755 ;; Guide-line for numbers:
2756 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2757 ;; for things that take a long time, 7 - not very important messages
2758 ;; on stuff, 9 - messages inside loops.
2759 (defun gnus-message (level &rest args)
2760   (if (<= level gnus-verbose)
2761       (apply 'message args)
2762     ;; We have to do this format thingie here even if the result isn't
2763     ;; shown - the return value has to be the same as the return value
2764     ;; from `message'.
2765     (apply 'format args)))
2766
2767 ;; Generate a unique new group name.
2768 (defun gnus-generate-new-group-name (leaf)
2769   (let ((name leaf)
2770         (num 0))
2771     (while (gnus-gethash name gnus-newsrc-hashtb)
2772       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2773     name))
2774
2775 ;;; List and range functions
2776
2777 (defun gnus-last-element (list)
2778   "Return last element of LIST."
2779   (while (cdr list)
2780     (setq list (cdr list)))
2781   (car list))
2782
2783 (defun gnus-copy-sequence (list)
2784   "Do a complete, total copy of a list."
2785   (if (and (consp list) (not (consp (cdr list))))
2786       (cons (car list) (cdr list))
2787     (mapcar (lambda (elem) (if (consp elem) 
2788                                (if (consp (cdr elem))
2789                                    (gnus-copy-sequence elem)
2790                                  (cons (car elem) (cdr elem)))
2791                              elem))
2792             list)))
2793
2794 (defun gnus-set-difference (list1 list2)
2795   "Return a list of elements of LIST1 that do not appear in LIST2."
2796   (let ((list1 (copy-sequence list1)))
2797     (while list2
2798       (setq list1 (delq (car list2) list1))
2799       (setq list2 (cdr list2)))
2800     list1))
2801
2802 (defun gnus-sorted-complement (list1 list2)
2803   "Return a list of elements of LIST1 that do not appear in LIST2.
2804 Both lists have to be sorted over <."
2805   (let (out)
2806     (if (or (null list1) (null list2))
2807         (or list1 list2)
2808       (while (and list1 list2)
2809         (cond ((= (car list1) (car list2))
2810                (setq list1 (cdr list1)
2811                      list2 (cdr list2)))
2812               ((< (car list1) (car list2))
2813                (setq out (cons (car list1) out))
2814                (setq list1 (cdr list1)))
2815               (t
2816                (setq out (cons (car list2) out))
2817                (setq list2 (cdr list2)))))
2818       (nconc (nreverse out) (or list1 list2)))))
2819
2820 (defun gnus-intersection (list1 list2)      
2821   (let ((result nil))
2822     (while list2
2823       (if (memq (car list2) list1)
2824           (setq result (cons (car list2) result)))
2825       (setq list2 (cdr list2)))
2826     result))
2827
2828 (defun gnus-sorted-intersection (list1 list2)
2829   ;; LIST1 and LIST2 have to be sorted over <.
2830   (let (out)
2831     (while (and list1 list2)
2832       (cond ((= (car list1) (car list2))
2833              (setq out (cons (car list1) out)
2834                    list1 (cdr list1)
2835                    list2 (cdr list2)))
2836             ((< (car list1) (car list2))
2837              (setq list1 (cdr list1)))
2838             (t
2839              (setq list2 (cdr list2)))))
2840     (nreverse out)))
2841
2842 (defun gnus-set-sorted-intersection (list1 list2)
2843   ;; LIST1 and LIST2 have to be sorted over <.
2844   ;; This function modifies LIST1.
2845   (let* ((top (cons nil list1))
2846          (prev top))
2847   (while (and list1 list2)
2848     (cond ((= (car list1) (car list2))
2849            (setq prev list1
2850                  list1 (cdr list1)
2851                  list2 (cdr list2)))
2852           ((< (car list1) (car list2))
2853            (setcdr prev (cdr list1))
2854            (setq list1 (cdr list1)))
2855           (t
2856            (setq list2 (cdr list2)))))
2857   (setcdr prev nil)
2858   (cdr top)))
2859
2860 (defun gnus-compress-sequence (numbers &optional always-list)
2861   "Convert list of numbers to a list of ranges or a single range.
2862 If ALWAYS-LIST is non-nil, this function will always release a list of
2863 ranges."
2864   (let* ((first (car numbers))
2865          (last (car numbers))
2866          result)
2867     (if (null numbers)
2868         nil
2869       (if (not (listp (cdr numbers)))
2870           numbers
2871         (while numbers
2872           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2873                 ((= (1+ last) (car numbers)) ;Still in sequence
2874                  (setq last (car numbers)))
2875                 (t                      ;End of one sequence
2876                  (setq result 
2877                        (cons (if (= first last) first
2878                                (cons first last)) result))
2879                  (setq first (car numbers))
2880                  (setq last  (car numbers))))
2881           (setq numbers (cdr numbers)))
2882         (if (and (not always-list) (null result))
2883             (if (= first last) (list first) (cons first last))
2884           (nreverse (cons (if (= first last) first (cons first last))
2885                           result)))))))
2886
2887 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2888 (defun gnus-uncompress-range (ranges)
2889   "Expand a list of ranges into a list of numbers.
2890 RANGES is either a single range on the form `(num . num)' or a list of
2891 these ranges."
2892   (let (first last result)
2893     (cond 
2894      ((null ranges)
2895       nil)
2896      ((not (listp (cdr ranges)))
2897       (setq first (car ranges))
2898       (setq last (cdr ranges))
2899       (while (<= first last)
2900         (setq result (cons first result))
2901         (setq first (1+ first)))
2902       (nreverse result))
2903      (t
2904       (while ranges
2905         (if (atom (car ranges))
2906             (if (numberp (car ranges))
2907                 (setq result (cons (car ranges) result)))
2908           (setq first (car (car ranges)))
2909           (setq last  (cdr (car ranges)))
2910           (while (<= first last)
2911             (setq result (cons first result))
2912             (setq first (1+ first))))
2913         (setq ranges (cdr ranges)))
2914       (nreverse result)))))
2915
2916 (defun gnus-add-to-range (ranges list)
2917   "Return a list of ranges that has all articles from both RANGES and LIST.
2918 Note: LIST has to be sorted over `<'."
2919   (if (not ranges)
2920       (gnus-compress-sequence list t)
2921     (setq list (copy-sequence list))
2922     (or (listp (cdr ranges))
2923         (setq ranges (list ranges)))
2924     (let ((out ranges)
2925           ilist lowest highest temp)
2926       (while (and ranges list)
2927         (setq ilist list)
2928         (setq lowest (or (and (atom (car ranges)) (car ranges))
2929                          (car (car ranges))))
2930         (while (and list (cdr list) (< (car (cdr list)) lowest))
2931           (setq list (cdr list)))
2932         (if (< (car ilist) lowest)
2933             (progn
2934               (setq temp list)
2935               (setq list (cdr list))
2936               (setcdr temp nil)
2937               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2938         (setq highest (or (and (atom (car ranges)) (car ranges))
2939                           (cdr (car ranges))))
2940         (while (and list (<= (car list) highest))
2941           (setq list (cdr list)))
2942         (setq ranges (cdr ranges)))
2943       (if list
2944           (setq out (nconc (gnus-compress-sequence list t) out)))
2945       (setq out (sort out (lambda (r1 r2) 
2946                             (< (or (and (atom r1) r1) (car r1))
2947                                (or (and (atom r2) r2) (car r2))))))
2948       (setq ranges out)
2949       (while ranges
2950         (if (atom (car ranges))
2951             (if (cdr ranges)
2952                 (if (atom (car (cdr ranges)))
2953                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2954                         (progn
2955                           (setcar ranges (cons (car ranges) 
2956                                                (car (cdr ranges))))
2957                           (setcdr ranges (cdr (cdr ranges)))))
2958                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2959                       (progn
2960                         (setcar (car (cdr ranges)) (car ranges))
2961                         (setcar ranges (car (cdr ranges)))
2962                         (setcdr ranges (cdr (cdr ranges)))))))
2963           (if (cdr ranges)
2964               (if (atom (car (cdr ranges)))
2965                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2966                       (progn
2967                         (setcdr (car ranges) (car (cdr ranges)))
2968                         (setcdr ranges (cdr (cdr ranges)))))
2969                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2970                     (progn
2971                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2972                       (setcdr ranges (cdr (cdr ranges))))))))
2973         (setq ranges (cdr ranges)))
2974       out)))
2975
2976 (defun gnus-remove-from-range (ranges list)
2977   "Return a list of ranges that has all articles from LIST removed from RANGES.
2978 Note: LIST has to be sorted over `<'."
2979   ;; !!! This function shouldn't look like this, but I've got a headache.
2980   (gnus-compress-sequence 
2981    (gnus-sorted-complement
2982     (gnus-uncompress-range ranges) list)))
2983
2984 (defun gnus-member-of-range (number ranges)
2985   (if (not (listp (cdr ranges)))
2986       (and (>= number (car ranges)) 
2987            (<= number (cdr ranges)))
2988     (let ((not-stop t))
2989       (while (and ranges 
2990                   (if (numberp (car ranges))
2991                       (>= number (car ranges))
2992                     (>= number (car (car ranges))))
2993                   not-stop)
2994         (if (if (numberp (car ranges))
2995                 (= number (car ranges))
2996               (and (>= number (car (car ranges)))
2997                    (<= number (cdr (car ranges)))))
2998             (setq not-stop nil))
2999         (setq ranges (cdr ranges)))
3000       (not not-stop))))
3001
3002 \f
3003 ;;;
3004 ;;; Gnus group mode
3005 ;;;
3006
3007 (defvar gnus-group-mode-map nil)
3008 (defvar gnus-group-group-map nil)
3009 (defvar gnus-group-mark-map nil)
3010 (defvar gnus-group-list-map nil)
3011 (defvar gnus-group-sub-map nil)
3012 (put 'gnus-group-mode 'mode-class 'special)
3013
3014 (if gnus-group-mode-map
3015     nil
3016   (setq gnus-group-mode-map (make-keymap))
3017   (suppress-keymap gnus-group-mode-map)
3018   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3019   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3020   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3021   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3022   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3023   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3024   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3025   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3026   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3027   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3028   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3029   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3030   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3031   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3032   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3033   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3034   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3035   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3036   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3037   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3038   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3039   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3040   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3041   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3042   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3043   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3044   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3045   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3046   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3047   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3048   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3049   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3050   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3051   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3052   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3053   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3054   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3055   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3056   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3057   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3058   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3059   (define-key gnus-group-mode-map "V" 'gnus-version)
3060   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3061   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3062   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3063   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3064   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3065   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3066   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3067   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3068   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3069   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3070   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3071   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3072   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3073   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3074   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3075
3076   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3077   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3078   (define-prefix-command 'gnus-group-mark-map)
3079   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3080   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3081   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3082   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3083
3084   (define-prefix-command 'gnus-group-group-map)
3085   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3086   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3087   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3088   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3089   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3090   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3091   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3092   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3093   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3094   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3095   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3096   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3097   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3098   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3099   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3100   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3101   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3102   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3103
3104   (define-prefix-command 'gnus-group-list-map)
3105   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3106   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3107   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3108   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3109   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3110   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3111   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3112   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3113   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3114
3115   (define-prefix-command 'gnus-group-sub-map)
3116   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3117   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3118   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3119   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3120   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3121   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3122   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3123   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3124
3125 (defun gnus-group-mode ()
3126   "Major mode for reading news.
3127
3128 All normal editing commands are switched off.
3129 \\<gnus-group-mode-map>
3130 The group buffer lists (some of) the groups available.  For instance,
3131 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3132 lists all zombie groups. 
3133
3134 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3135 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3136
3137 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3138
3139 The following commands are available:
3140
3141 \\{gnus-group-mode-map}"
3142   (interactive)
3143   (if gnus-visual (gnus-group-make-menu-bar))
3144   (kill-all-local-variables)
3145   (setq mode-line-modified "-- ")
3146   (make-local-variable 'mode-line-format)
3147   (setq mode-line-format (copy-sequence mode-line-format))
3148   (and (equal (nth 3 mode-line-format) "   ")
3149        (setcar (nthcdr 3 mode-line-format) ""))
3150   (setq major-mode 'gnus-group-mode)
3151   (setq mode-name "Group")
3152   (gnus-group-set-mode-line)
3153   (setq mode-line-process nil)
3154   (use-local-map gnus-group-mode-map)
3155   (buffer-disable-undo (current-buffer))
3156   (setq truncate-lines t)
3157   (setq buffer-read-only t)
3158   (run-hooks 'gnus-group-mode-hook))
3159
3160 (defun gnus-mouse-pick-group (e)
3161   (interactive "e")
3162   (mouse-set-point e)
3163   (gnus-group-read-group nil))
3164
3165 ;; Look at LEVEL and find out what the level is really supposed to be.
3166 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3167 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3168 (defun gnus-group-default-level (&optional level number-or-nil)
3169   (cond  
3170    (gnus-group-use-permanent-levels
3171     (setq gnus-group-default-list-level 
3172           (or level gnus-group-default-list-level))
3173     (or gnus-group-default-list-level gnus-level-subscribed))
3174    (number-or-nil
3175     level)
3176    (t
3177     (or level gnus-group-default-list-level gnus-level-subscribed))))
3178   
3179
3180 ;;;###autoload
3181 (defun gnus-no-server (&optional arg)
3182   "Read network news.
3183 If ARG is a positive number, Gnus will use that as the
3184 startup level. If ARG is nil, Gnus will be started at level 2. 
3185 If ARG is non-nil and not a positive number, Gnus will
3186 prompt the user for the name of an NNTP server to use.
3187 As opposed to `gnus', this command will not connect to the local server."
3188   (interactive "P")
3189   (setq gnus-group-use-permanent-levels t)
3190   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3191
3192 (defalias '\(ding\) 'gnus)
3193
3194 ;;;###autoload
3195 (defun gnus (&optional arg dont-connect)
3196   "Read network news.
3197 If ARG is non-nil and a positive number, Gnus will use that as the
3198 startup level. If ARG is non-nil and not a positive number, Gnus will
3199 prompt the user for the name of an NNTP server to use."
3200   (interactive "P")
3201   (if (get-buffer gnus-group-buffer)
3202       (progn
3203         (switch-to-buffer gnus-group-buffer)
3204         (gnus-group-get-new-news))
3205     (gnus-clear-system)
3206     (nnheader-init-server-buffer)
3207     (gnus-read-init-file)
3208
3209     (let ((level (and arg (numberp arg) (> arg 0) arg))
3210           did-connect)
3211       (unwind-protect
3212           (progn
3213             (gnus-group-setup-buffer)
3214             (or dont-connect 
3215                 (setq did-connect
3216                       (gnus-start-news-server (and arg (not level))))))
3217         (if (and (not dont-connect) 
3218                  (not did-connect))
3219             (gnus-group-quit)
3220           (run-hooks 'gnus-startup-hook)
3221           ;; NNTP server is successfully open. 
3222
3223           ;; Find the current startup file name.
3224           (setq gnus-current-startup-file 
3225                 (gnus-make-newsrc-file gnus-startup-file))
3226
3227           ;; Read the dribble file.
3228           (and gnus-use-dribble-file (gnus-dribble-read-file))
3229
3230           (gnus-summary-make-display-table)
3231           (let ((buffer-read-only nil))
3232             (erase-buffer)
3233             (if (not gnus-inhibit-startup-message)
3234                 (progn
3235                   (gnus-group-startup-message)
3236                   (sit-for 0))))
3237           (gnus-setup-news nil level)
3238           (gnus-group-list-groups level)
3239           (gnus-configure-windows 'group))))))
3240
3241 (defun gnus-unload ()
3242   "Unload all Gnus features."
3243   (interactive)
3244   (let ((history load-history)
3245         feature)
3246     (while history
3247       (and (string-match "^gnus" (car (car history)))
3248            (setq feature (cdr (assq 'provide (car history))))
3249            (unload-feature feature 'force))
3250       (setq history (cdr history)))))
3251
3252                                        
3253
3254 (defun gnus-group-startup-message (&optional x y)
3255   "Insert startup message in current buffer."
3256   ;; Insert the message.
3257   (erase-buffer)
3258   (insert
3259    (format "
3260           _    ___ _             _      
3261           _ ___ __ ___  __    _ ___     
3262           __   _     ___    __  ___     
3263               _           ___     _     
3264              _  _ __             _      
3265              ___   __            _      
3266                    __           _       
3267                     _      _   _        
3268                    _      _    _        
3269                       _  _    _         
3270                   __  ___               
3271                  _   _ _     _          
3272                 _   _                   
3273               _    _                    
3274              _    _                     
3275             _                         
3276           __                             
3277
3278
3279       Gnus * A newsreader for Emacsen
3280     A Praxis release * larsi@ifi.uio.no
3281
3282            gnus-version))
3283   ;; And then hack it.
3284   ;; 18 is the longest line.
3285   (indent-rigidly (point-min) (point-max) 
3286                   (/ (max (- (window-width) (or x 46)) 0) 2))
3287   (goto-char (point-min))
3288   (let* ((pheight (count-lines (point-min) (point-max)))
3289          (wheight (window-height))
3290          (rest (- wheight  pheight)))
3291     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
3292     
3293     
3294
3295   ;; Fontify some.
3296   (goto-char (point-min))
3297   (search-forward "Praxis")
3298   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3299   (goto-char (point-min)))
3300
3301 (defun gnus-group-startup-message-old (&optional x y)
3302   "Insert startup message in current buffer."
3303   ;; Insert the message.
3304   (erase-buffer)
3305   (insert
3306    (format "
3307      %s
3308            A newsreader 
3309       for GNU Emacs
3310
3311         Based on GNUS 
3312              written by 
3313      Masanobu UMEDA
3314
3315        A Praxis Release
3316       larsi@ifi.uio.no
3317
3318            gnus-version))
3319   ;; And then hack it.
3320   ;; 18 is the longest line.
3321   (indent-rigidly (point-min) (point-max) 
3322                   (/ (max (- (window-width) (or x 28)) 0) 2))
3323   (goto-char (point-min))
3324   ;; +4 is fuzzy factor.
3325   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3326
3327   ;; Fontify some.
3328   (goto-char (point-min))
3329   (search-forward "Praxis")
3330   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3331   (goto-char (point-min)))
3332
3333 (defun gnus-group-setup-buffer ()
3334   (or (get-buffer gnus-group-buffer)
3335       (progn
3336         (switch-to-buffer gnus-group-buffer)
3337         (gnus-add-current-to-buffer-list)
3338         (gnus-group-mode)
3339         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3340
3341 (defun gnus-group-list-groups (&optional level unread)
3342   "List newsgroups with level LEVEL or lower that have unread articles.
3343 Default is all subscribed groups.
3344 If argument UNREAD is non-nil, groups with no unread articles are also
3345 listed." 
3346   (interactive (list (if current-prefix-arg
3347                          (prefix-numeric-value current-prefix-arg)
3348                        (or
3349                         (gnus-group-default-level nil t)
3350                         gnus-group-default-list-level
3351                         level-subscribed))))
3352   (or level
3353       (setq level (car gnus-group-list-mode)
3354             unread (cdr gnus-group-list-mode)))
3355   (setq level (gnus-group-default-level level))
3356   (gnus-group-setup-buffer)     ;May call from out of group buffer
3357   (let ((case-fold-search nil)
3358         (group (gnus-group-group-name)))
3359     (funcall gnus-group-prepare-function level unread nil)
3360     (if (zerop (buffer-size))
3361         (gnus-message 5 gnus-no-groups-message)
3362       (goto-char (point-min))
3363       (if (not group)
3364           ;; Go to the first group with unread articles.
3365           (gnus-group-search-forward nil nil nil t)
3366         ;; Find the right group to put point on. If the current group
3367         ;; has disapeared in the new listing, try to find the next
3368         ;; one. If no next one can be found, just leave point at the
3369         ;; first newsgroup in the buffer.
3370         (if (not (gnus-goto-char
3371                   (text-property-any (point-min) (point-max) 
3372                                      'gnus-group (intern group))))
3373             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3374               (while (and newsrc
3375                           (not (gnus-goto-char 
3376                                 (text-property-any 
3377                                  (point-min) (point-max) 'gnus-group 
3378                                  (intern (car (car newsrc)))))))
3379                 (setq newsrc (cdr newsrc)))
3380               (or newsrc (progn (goto-char (point-max))
3381                                 (forward-line -1))))))
3382       ;; Adjust cursor point.
3383       (gnus-group-position-cursor))))
3384
3385 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3386   "List all newsgroups with unread articles of level LEVEL or lower.
3387 If ALL is non-nil, list groups that have no unread articles.
3388 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3389 If REGEXP, only list groups matching REGEXP."
3390   (set-buffer gnus-group-buffer)
3391   (let ((buffer-read-only nil)
3392         (newsrc (cdr gnus-newsrc-alist))
3393         (lowest (or lowest 1))
3394         info clevel unread group)
3395     (erase-buffer)
3396     (if (< lowest gnus-level-zombie)
3397         ;; List living groups.
3398         (while newsrc
3399           (setq info (car newsrc)
3400                 group (car info)
3401                 newsrc (cdr newsrc)
3402                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3403           (and unread ; This group might be bogus
3404                (or (not regexp)
3405                    (string-match regexp group))
3406                (<= (setq clevel (car (cdr info))) level) 
3407                (>= clevel lowest)
3408                (or all            ; We list all groups?
3409                    (eq unread t)  ; We list unactivated groups
3410                    (> unread 0)   ; We list groups with unread articles
3411                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3412                (gnus-group-insert-group-line 
3413                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3414
3415     ;; List dead groups.
3416     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3417          (gnus-group-prepare-flat-list-dead 
3418           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3419           gnus-level-zombie ?Z
3420           regexp))
3421     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3422          (gnus-group-prepare-flat-list-dead 
3423           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3424           gnus-level-killed ?K regexp))
3425
3426     (gnus-group-set-mode-line)
3427     (setq gnus-group-list-mode (cons level all))
3428     (run-hooks 'gnus-group-prepare-hook)))
3429
3430 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3431   ;; List zombies and killed lists somehwat faster, which was
3432   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3433   ;; this by ignoring the group format specification altogether.
3434   (let (group beg)
3435     (while groups
3436       (setq group (car groups)
3437             groups (cdr groups))
3438       (if (or (not regexp)
3439               (string-match regexp group))
3440           (progn
3441             (setq beg (point))
3442             (insert (format " %c     *: %s\n" mark group))
3443             (add-text-properties 
3444              beg (1+ beg) 
3445              (list 'gnus-group (intern group)
3446                    'gnus-unread t
3447                    'gnus-level level)))))))
3448
3449 (defun gnus-group-real-name (group)
3450   "Find the real name of a foreign newsgroup."
3451   (if (string-match ":[^:]+$" group)
3452       (substring group (1+ (match-beginning 0)))
3453     group))
3454
3455 (defun gnus-group-prefixed-name (group method)
3456   "Return the whole name from GROUP and METHOD."
3457   (and (stringp method) (setq method (gnus-server-to-method method)))
3458   (concat (format "%s" (car method))
3459           (if (and 
3460                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3461                (not (string= (nth 1 method) "")))
3462               (concat "+" (nth 1 method)))
3463           ":" group))
3464
3465 (defun gnus-group-real-prefix (group)
3466   "Return the prefix of the current group name."
3467   (if (string-match "^[^:]+:" group)
3468       (substring group 0 (match-end 0))
3469     ""))
3470
3471 (defun gnus-group-method-name (group)
3472   "Return the method used for selecting GROUP."
3473   (let ((prefix (gnus-group-real-prefix group)))
3474     (if (equal prefix "")
3475         gnus-select-method
3476       (if (string-match "^[^\\+]+\\+" prefix)
3477           (list (intern (substring prefix 0 (1- (match-end 0))))
3478                 (substring prefix (match-end 0) (1- (length prefix))))
3479         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3480
3481 (defun gnus-group-foreign-p (group)
3482   "Return nil if GROUP is native, non-nil if it is foreign."
3483   (string-match ":" group))
3484
3485 (defun gnus-group-set-info (info &optional method-only-group part)
3486   (let* ((entry (gnus-gethash
3487                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3488          (part-info info)
3489          (info (if method-only-group (nth 2 entry) info)))
3490     (if (not method-only-group)
3491         ()
3492       (or entry
3493           (error "Trying to change non-existent group %s" method-only-group))
3494       ;; We have recevied parts of the actual group info - either the
3495       ;; select method or the group parameters.  We first check
3496       ;; whether we have to extend the info, and if so, do that.
3497       (let ((len (length info))
3498             (total (if (eq part 'method) 5 6)))
3499         (and (< len total)
3500              (setcdr (nthcdr (1- len) info)
3501                      (make-list (- total len) nil)))
3502         ;; Then we enter the new info.
3503         (setcar (nthcdr (1- total) info) part-info)))
3504     ;; We uncompress some lists of marked articles.
3505     (let (marked)
3506       (if (not (setq marked (nth 3 info)))
3507           ()
3508         (while marked
3509           (or (eq 'score (car (car marked)))
3510               (eq 'bookmark (car (car marked)))
3511               (eq 'killed (car (car marked)))
3512               (setcdr (car marked) 
3513                       (gnus-uncompress-range (cdr (car marked)))))
3514           (setq marked (cdr marked)))))
3515     (if entry
3516         ()
3517       ;; This is a new group, so we just create it.
3518       (save-excursion
3519         (set-buffer gnus-group-buffer)
3520         (if (nth 4 info)
3521             ;; It's a foreign group...
3522             (gnus-group-make-group 
3523              (gnus-group-real-name (car info))
3524              (prin1-to-string (car (nth 4 info)))
3525              (nth 1 (nth 4 info)))
3526           ;; It's a native group.
3527           (gnus-group-make-group
3528            (car info)
3529            (prin1-to-string (car gnus-select-method))
3530            (nth 1 gnus-select-method)))
3531         (gnus-message 6 "Note: New group created")
3532         (setq entry 
3533               (gnus-gethash (gnus-group-prefixed-name 
3534                              (gnus-group-real-name (car info))
3535                              (or (nth 4 info) gnus-select-method))
3536                             gnus-newsrc-hashtb))))
3537     ;; Whether it was a new group or not, we now have the entry, so we
3538     ;; can do the update.
3539     (if entry
3540         (progn
3541           (setcar (nthcdr 2 entry) info)
3542           (if (and (not (eq (car entry) t)) 
3543                    (gnus-gethash (car info) gnus-active-hashtb))
3544               (let ((marked (nth 3 info)))
3545                 (setcar entry 
3546                         (max 0 (- (length (gnus-list-of-unread-articles 
3547                                            (car info)))
3548                                   (length (cdr (assq 'tick marked)))
3549                                   (length (cdr (assq 'dormant marked)))))))))
3550       (error "No such group: %s" (car info)))))
3551
3552 (defun gnus-group-set-method-info (group select-method)
3553   (gnus-group-set-info select-method group 'method))
3554
3555 (defun gnus-group-set-params-info (group params)
3556   (gnus-group-set-info params group 'params))
3557
3558 (defun gnus-group-update-group-line ()
3559   "This function updates the current line in the newsgroup buffer and
3560 moves the point to the colon."
3561   (let* ((buffer-read-only nil)
3562          (group (gnus-group-group-name))
3563          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3564     (if entry
3565         (gnus-dribble-enter 
3566          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3567                  ")")))
3568     (beginning-of-line)
3569     (delete-region (point) (progn (forward-line 1) (point)))
3570     (gnus-group-insert-group-line-info group)
3571     (forward-line -1)
3572     (gnus-group-position-cursor)))
3573
3574 (defun gnus-group-insert-group-line-info (group)
3575   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3576         active info)
3577     (if entry
3578         (progn
3579           (setq info (nth 2 entry))
3580           (gnus-group-insert-group-line 
3581            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3582       (setq active (gnus-gethash group gnus-active-hashtb))
3583       (gnus-group-insert-group-line 
3584        nil group 
3585        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3586        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3587
3588 (defun gnus-group-insert-group-line (gformat group level marked number method)
3589   (let* ((gformat (or gformat gnus-group-line-format-spec))
3590          (active (gnus-gethash group gnus-active-hashtb))
3591          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3592          (number-of-dormant (length (cdr (assq 'dormant marked))))
3593          (number-of-ticked (length (cdr (assq 'tick marked))))
3594          (number-of-ticked-and-dormant
3595           (+ number-of-ticked number-of-dormant))
3596          (number-of-unread-unticked 
3597           (if (numberp number) (int-to-string (max 0 number))
3598             "*"))
3599          (number-of-read
3600           (if (numberp number)
3601               (max 0 (- number-total number))
3602             "*"))
3603          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3604                            ((<= level gnus-level-unsubscribed) ?U)
3605                            ((= level gnus-level-zombie) ?Z)
3606                            (t ?K)))
3607          (qualified-group (gnus-group-real-name group))
3608          (newsgroup-description 
3609           (if gnus-description-hashtb
3610               (or (gnus-gethash group gnus-description-hashtb) "")
3611             ""))
3612          (moderated (if (member group gnus-moderated-list) ?m ? ))
3613          (moderated-string (if (eq moderated ?m) "(m)" ""))
3614          (method (gnus-server-get-method group method))
3615          (news-server (or (car (cdr method)) ""))
3616          (news-method (or (car method) ""))
3617          (news-method-string 
3618           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3619          (marked (if (and 
3620                       (numberp number) 
3621                       (zerop number)
3622                       (> number-of-ticked 0))
3623                      ?* ? ))
3624          (number (if (eq number t) "*" (+ number number-of-dormant 
3625                                           number-of-ticked)))
3626          (process-marked (if (member qualified-group gnus-group-marked)
3627                              gnus-process-mark ? ))
3628          (buffer-read-only nil)
3629          header ; passed as parameter to user-funcs.
3630          b)
3631     (beginning-of-line)
3632     (setq b (point))
3633     ;; Insert the text.
3634     (insert (eval gformat))
3635
3636     (add-text-properties 
3637      b (1+ b) (list 'gnus-group (intern group)
3638                     'gnus-unread (if (numberp number)
3639                                      (string-to-int number-of-unread-unticked)
3640                                    t)
3641                     'gnus-marked marked
3642                     'gnus-level level))))
3643
3644 (defun gnus-group-update-group (group &optional visible-only)
3645   "Update newsgroup info of GROUP.
3646 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3647   (save-excursion
3648     (set-buffer gnus-group-buffer)
3649     (let ((buffer-read-only nil)
3650           visible)
3651       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3652         (if entry
3653             (gnus-dribble-enter 
3654              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3655                      ")"))))
3656       ;; Buffer may be narrowed.
3657       (save-restriction
3658         (widen)
3659         ;; Search a line to modify.  If the buffer is large, the search
3660         ;; takes long time.  In most cases, current point is on the line
3661         ;; we are looking for.  So, first of all, check current line. 
3662         (if (or (progn
3663                   (beginning-of-line)
3664                   (eq (get-text-property (point) 'gnus-group)
3665                       (intern group)))
3666                 (progn
3667                   (gnus-goto-char 
3668                    (text-property-any 
3669                     (point-min) (point-max) 'gnus-group (intern group)))))
3670             ;; GROUP is listed in current buffer. So, delete old line.
3671             (progn
3672               (setq visible t)
3673               (beginning-of-line)
3674               (delete-region (point) (progn (forward-line 1) (point))))
3675           ;; No such line in the buffer, find out where it's supposed to
3676           ;; go, and insert it there (or at the end of the buffer).
3677           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3678           (or visible-only
3679               (let ((entry 
3680                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3681                 (while (and entry
3682                             (car entry)
3683                             (not
3684                              (gnus-goto-char
3685                               (text-property-any
3686                                (point-min) (point-max) 
3687                                'gnus-group (intern (car (car entry)))))))
3688                   (setq entry (cdr entry)))
3689                 (or entry (goto-char (point-max)))))))
3690       (if (or visible (not visible-only))
3691           (gnus-group-insert-group-line-info group))
3692       (gnus-group-set-mode-line))))
3693
3694 (defun gnus-group-set-mode-line ()
3695   (if (memq 'group gnus-updated-mode-lines)
3696       (let* ((gformat (or gnus-group-mode-line-format-spec
3697                           (setq gnus-group-mode-line-format-spec
3698                                 (gnus-parse-format 
3699                                  gnus-group-mode-line-format 
3700                                  gnus-group-mode-line-format-alist))))
3701              (news-server (car (cdr gnus-select-method)))
3702              (news-method (car gnus-select-method))
3703              (max-len 60)
3704              (mode-string (eval gformat)))
3705         (setq mode-string (eval gformat))
3706         (if (> (length mode-string) max-len) 
3707             (setq mode-string (substring mode-string 0 (- max-len 4))))
3708         (setq mode-line-buffer-identification mode-string)
3709         (set-buffer-modified-p t))))
3710
3711 (defun gnus-group-group-name ()
3712   "Get the name of the newsgroup on the current line."
3713   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3714     (and group (symbol-name group))))
3715
3716 (defun gnus-group-group-level ()
3717   "Get the level of the newsgroup on the current line."
3718   (get-text-property (gnus-point-at-bol) 'gnus-level))
3719
3720 (defun gnus-group-group-unread ()
3721   "Get the number of unread articles of the newsgroup on the current line."
3722   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3723
3724 (defun gnus-group-search-forward (&optional backward all level first-too)
3725   "Find the next newsgroup with unread articles.
3726 If BACKWARD is non-nil, find the previous newsgroup instead.
3727 If ALL is non-nil, just find any newsgroup.
3728 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3729 group exists.
3730 If FIRST-TOO, the current line is also eligible as a target."
3731   (let ((way (if backward -1 1))
3732         (low gnus-level-killed)
3733         (beg (point))
3734         pos found lev)
3735     (if (and backward (progn (beginning-of-line)) (bobp))
3736         nil
3737       (or first-too (forward-line way))
3738       (while (and 
3739               (not (eobp))
3740               (not (setq 
3741                     found 
3742                     (and (or all
3743                              (and
3744                               (let ((unread 
3745                                      (get-text-property (point) 'gnus-unread)))
3746                                 (or (eq unread t) (and unread (> unread 0))))
3747                               (setq lev (get-text-property (point)
3748                                                            'gnus-level))
3749                               (<= lev gnus-level-subscribed)))
3750                          (or (not level)
3751                              (and (setq lev (get-text-property (point)
3752                                                                'gnus-level))
3753                                   (or (= lev level)
3754                                       (and (< lev low)
3755                                            (< level lev)
3756                                            (progn
3757                                              (setq low lev)
3758                                              (setq pos (point))
3759                                              nil))))))))
3760               (zerop (forward-line way)))))
3761     (if found 
3762         (progn (gnus-group-position-cursor) t)
3763       (goto-char (or pos beg))
3764       (and pos t))))
3765
3766 ;;; Gnus group mode commands
3767
3768 ;; Group marking.
3769
3770 (defun gnus-group-mark-group (n &optional unmark no-advance)
3771   "Mark the current group."
3772   (interactive "p")
3773   (let ((buffer-read-only nil)
3774         group)
3775     (while 
3776         (and (> n 0) 
3777              (setq group (gnus-group-group-name))
3778              (progn
3779                (beginning-of-line)
3780                (forward-char 
3781                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3782                (delete-char 1)
3783                (if unmark
3784                    (progn
3785                      (insert " ")
3786                      (setq gnus-group-marked (delete group gnus-group-marked)))
3787                  (insert "#")
3788                  (setq gnus-group-marked
3789                        (cons group (delete group gnus-group-marked))))
3790                t)
3791              (or no-advance (zerop (gnus-group-next-group 1))))
3792       (setq n (1- n)))
3793     (gnus-summary-position-cursor)
3794     n))
3795
3796 (defun gnus-group-unmark-group (n)
3797   "Remove the mark from the current group."
3798   (interactive "p")
3799   (gnus-group-mark-group n 'unmark))
3800
3801 (defun gnus-group-mark-region (unmark beg end)
3802   "Mark all groups between point and mark.
3803 If UNMARK, remove the mark instead."
3804   (interactive "P\nr")
3805   (let ((num (count-lines beg end)))
3806     (save-excursion
3807       (goto-char beg)
3808       (- num (gnus-group-mark-group num unmark)))))
3809
3810 (defun gnus-group-remove-mark (group)
3811   (and (gnus-group-goto-group group)
3812        (save-excursion
3813          (gnus-group-mark-group 1 'unmark t))))
3814
3815 ;; Return a list of groups to work on.  Take into consideration N (the
3816 ;; prefix) and the list of marked groups.
3817 (defun gnus-group-process-prefix (n)
3818   (cond (n
3819          (setq n (prefix-numeric-value n))
3820          ;; There is a prefix, so we return a list of the N next
3821          ;; groups. 
3822          (let ((way (if (< n 0) -1 1))
3823                (n (abs n))
3824                group groups)
3825            (save-excursion
3826              (while (and (> n 0)
3827                          (setq group (gnus-group-group-name)))
3828                (setq groups (cons group groups))
3829                (setq n (1- n))
3830                (forward-line way)))
3831            (nreverse groups)))
3832         (gnus-group-marked
3833          ;; No prefix, but a list of marked articles.
3834          (reverse gnus-group-marked))
3835         (t
3836          ;; Neither marked articles or a prefix, so we return the
3837          ;; current group.
3838          (let ((group (gnus-group-group-name)))
3839            (and group (list group))))))
3840
3841 ;; Selecting groups.
3842
3843 (defun gnus-group-read-group (&optional all no-article group)
3844   "Read news in this newsgroup.
3845 If the prefix argument ALL is non-nil, already read articles become
3846 readable. If the optional argument NO-ARTICLE is non-nil, no article
3847 will be auto-selected upon group entry."
3848   (interactive "P")
3849   (let ((group (or group (gnus-group-group-name)))
3850         number active marked entry)
3851     (or group (error "No group on current line"))
3852     (setq marked 
3853           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3854     ;; This group might be a dead group. In that case we have to get
3855     ;; the number of unread articles from `gnus-active-hashtb'.
3856     (if entry
3857         (setq number (car entry))
3858       (if (setq active (gnus-gethash group gnus-active-hashtb))
3859           (setq number (- (1+ (cdr active)) (car active)))))
3860     (gnus-summary-read-group 
3861      group (or all (and (numberp number) 
3862                         (zerop (+ number (length (cdr (assq 'tick marked)))
3863                                   (length (cdr (assq 'dormant marked)))))))
3864      no-article)))
3865
3866 (defun gnus-group-select-group (&optional all)
3867   "Select this newsgroup.
3868 No article is selected automatically.
3869 If argument ALL is non-nil, already read articles become readable."
3870   (interactive "P")
3871   (gnus-group-read-group all t))
3872
3873 (defun gnus-group-select-group-all ()
3874   "Select the current group and display all articles in it."
3875   (interactive)
3876   (gnus-group-select-group 'all))
3877
3878 ;; Enter a group that is not in the group buffer. Non-nil is returned
3879 ;; if selection was successful.
3880 (defun gnus-group-read-ephemeral-group 
3881   (group method &optional activate quit-config)
3882   (let ((group (if (gnus-group-foreign-p group) group
3883                  (gnus-group-prefixed-name group method))))
3884     (gnus-sethash 
3885      group
3886      (list t nil (list group gnus-level-default-subscribed nil nil 
3887                        (append method
3888                                (list
3889                                 (list 'quit-config 
3890                                       (if quit-config quit-config
3891                                         (cons (current-buffer) 'summary)))))))
3892      gnus-newsrc-hashtb)
3893     (set-buffer gnus-group-buffer)
3894     (or (gnus-check-server method)
3895         (error "Unable to contact server: %s" (gnus-status-message method)))
3896     (if activate (or (gnus-request-group group)
3897                      (error "Couldn't request group")))
3898     (condition-case ()
3899         (gnus-group-read-group t t group)
3900       (error nil)
3901       (quit nil))
3902     (not (equal major-mode 'gnus-group-mode))))
3903   
3904 (defun gnus-group-jump-to-group (group)
3905   "Jump to newsgroup GROUP."
3906   (interactive 
3907    (list (completing-read 
3908           "Group: " gnus-active-hashtb nil 
3909           (memq gnus-select-method gnus-have-read-active-file))))
3910
3911   (if (equal group "")
3912       (error "Empty group name"))
3913
3914   (let ((b (text-property-any 
3915             (point-min) (point-max) 'gnus-group (intern group))))
3916     (if b
3917         ;; Either go to the line in the group buffer...
3918         (goto-char b)
3919       ;; ... or insert the line.
3920       (or
3921        (gnus-gethash group gnus-active-hashtb)
3922        (gnus-activate-group group)
3923        (error "%s error: %s" group (gnus-status-message group)))
3924
3925       (gnus-group-update-group group)
3926       (goto-char (text-property-any 
3927                   (point-min) (point-max) 'gnus-group (intern group)))))
3928   ;; Adjust cursor point.
3929   (gnus-group-position-cursor))
3930
3931 (defun gnus-group-goto-group (group)
3932   "Goto to newsgroup GROUP."
3933   (let ((b (text-property-any (point-min) (point-max) 
3934                               'gnus-group (intern group))))
3935     (and b (goto-char b))))
3936
3937 (defun gnus-group-next-group (n)
3938   "Go to next N'th newsgroup.
3939 If N is negative, search backward instead.
3940 Returns the difference between N and the number of skips actually
3941 done."
3942   (interactive "p")
3943   (gnus-group-next-unread-group n t))
3944
3945 (defun gnus-group-next-unread-group (n &optional all level)
3946   "Go to next N'th unread newsgroup.
3947 If N is negative, search backward instead.
3948 If ALL is non-nil, choose any newsgroup, unread or not.
3949 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3950 such group can be found, the next group with a level higher than
3951 LEVEL.
3952 Returns the difference between N and the number of skips actually
3953 made."
3954   (interactive "p")
3955   (let ((backward (< n 0))
3956         (n (abs n)))
3957     (while (and (> n 0)
3958                 (gnus-group-search-forward 
3959                  backward (or (not gnus-group-goto-unread) all) level))
3960       (setq n (1- n)))
3961     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3962                                (if level " on this level or higher" "")))
3963     n))
3964
3965 (defun gnus-group-prev-group (n)
3966   "Go to previous N'th newsgroup.
3967 Returns the difference between N and the number of skips actually
3968 done."
3969   (interactive "p")
3970   (gnus-group-next-unread-group (- n) t))
3971
3972 (defun gnus-group-prev-unread-group (n)
3973   "Go to previous N'th unread newsgroup.
3974 Returns the difference between N and the number of skips actually
3975 done."  
3976   (interactive "p")
3977   (gnus-group-next-unread-group (- n)))
3978
3979 (defun gnus-group-next-unread-group-same-level (n)
3980   "Go to next N'th unread newsgroup on the same level.
3981 If N is negative, search backward instead.
3982 Returns the difference between N and the number of skips actually
3983 done."
3984   (interactive "p")
3985   (gnus-group-next-unread-group n t (gnus-group-group-level))
3986   (gnus-group-position-cursor))
3987
3988 (defun gnus-group-prev-unread-group-same-level (n)
3989   "Go to next N'th unread newsgroup on the same level.
3990 Returns the difference between N and the number of skips actually
3991 done."
3992   (interactive "p")
3993   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3994   (gnus-group-position-cursor))
3995
3996 (defun gnus-group-best-unread-group (&optional exclude-group)
3997   "Go to the group with the highest level.
3998 If EXCLUDE-GROUP, do not go to that group."
3999   (interactive)
4000   (goto-char (point-min))
4001   (let ((best 100000)
4002         unread best-point)
4003     (while (setq unread (get-text-property (point) 'gnus-unread))
4004       (if (and (numberp unread) (> unread 0))
4005           (progn
4006             (if (and (< (get-text-property (point) 'gnus-level) best)
4007                      (or (not exclude-group)
4008                          (not (equal exclude-group (gnus-group-group-name)))))
4009                 (progn 
4010                   (setq best (get-text-property (point) 'gnus-level))
4011                   (setq best-point (point))))))
4012       (forward-line 1))
4013     (if best-point (goto-char best-point))
4014     (gnus-summary-position-cursor)
4015     (and best-point (gnus-group-group-name))))
4016
4017 (defun gnus-group-first-unread-group ()
4018   "Go to the first group with unread articles."
4019   (interactive)
4020   (prog1
4021       (let ((opoint (point))
4022             unread)
4023         (goto-char (point-min))
4024         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4025                 (not (zerop unread)) ; Has unread articles.
4026                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4027             (point) ; Success.
4028           (goto-char opoint)
4029           nil)) ; Not success.
4030     (gnus-group-position-cursor)))
4031
4032 (defun gnus-group-enter-server-mode ()
4033   "Jump to the server buffer."
4034   (interactive)
4035   (gnus-server-setup-buffer)
4036   (gnus-configure-windows 'server)
4037   (gnus-server-prepare))
4038
4039 (defun gnus-group-make-group (name method &optional address)
4040   "Add a new newsgroup.
4041 The user will be prompted for a NAME, for a select METHOD, and an
4042 ADDRESS."
4043   (interactive
4044    (cons 
4045     (read-string "Group name: ")
4046     (let ((method
4047            (completing-read 
4048             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4049             nil t)))
4050       (if (assoc method gnus-valid-select-methods)
4051           (list method
4052                 (if (memq 'prompt-address
4053                           (assoc method gnus-valid-select-methods))
4054                     (read-string "Address: ")
4055                   ""))
4056         (list method nil)))))
4057   
4058   (let* ((meth (if address (list (intern method) address) method))
4059          (nname (gnus-group-prefixed-name name meth))
4060          info)
4061     (and (gnus-gethash nname gnus-newsrc-hashtb)
4062          (error "Group %s already exists" nname))
4063     (gnus-group-change-level 
4064      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4065      gnus-level-default-subscribed gnus-level-killed 
4066      (and (gnus-group-group-name)
4067           (gnus-gethash (gnus-group-group-name)
4068                         gnus-newsrc-hashtb))
4069      t)
4070     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4071     (gnus-dribble-enter 
4072      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4073     (gnus-group-insert-group-line-info nname)
4074
4075     (if (assoc method gnus-valid-select-methods)
4076         (require (intern method)))
4077     (and (gnus-check-backend-function 'request-create-group nname)
4078          (gnus-request-create-group nname))))
4079
4080 (defun gnus-group-edit-group (group &optional part)
4081   "Edit the group on the current line."
4082   (interactive (list (gnus-group-group-name)))
4083   (let ((done-func '(lambda () 
4084                       "Exit editing mode and update the information."
4085                       (interactive)
4086                       (gnus-group-edit-group-done 'part 'group)))
4087         (part (or part 'info))
4088         (winconf (current-window-configuration))
4089         info)
4090     (or group (error "No group on current line"))
4091     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4092         (error "Killed group; can't be edited"))
4093     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4094     (gnus-configure-windows 'edit-group)
4095     (gnus-add-current-to-buffer-list)
4096     (emacs-lisp-mode)
4097     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4098     (use-local-map (copy-keymap emacs-lisp-mode-map))
4099     (local-set-key "\C-c\C-c" done-func)
4100     (make-local-variable 'gnus-prev-winconf)
4101     (setq gnus-prev-winconf winconf)
4102     ;; We modify the func to let it know what part it is editing.
4103     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4104     (setcar (cdr (cdr (nth 4 done-func))) group)
4105     (erase-buffer)
4106     (insert
4107      (cond 
4108       ((eq part 'method)
4109        ";; Type `C-c C-c' after editing the select method.\n\n")
4110       ((eq part 'params)
4111        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4112       ((eq part 'info)
4113        ";; Type `C-c C-c' after editing the group info.\n\n")))
4114     (let ((cinfo (gnus-copy-sequence info))
4115           marked)
4116       (if (not (setq marked (nth 3 cinfo)))
4117           ()
4118         (while marked
4119           (or (eq 'score (car (car marked)))
4120               (eq 'bookmark (car (car marked)))
4121               (eq 'killed (car (car marked)))
4122               (not (numberp (car (cdr (car marked)))))
4123               (setcdr (car marked) 
4124                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4125           (setq marked (cdr marked))))
4126       (insert 
4127        (pp-to-string
4128         (cond ((eq part 'method)
4129                (or (nth 4 info) "native"))
4130               ((eq part 'params)
4131                (nth 5 info))
4132               (t
4133                cinfo)))
4134        "\n"))))
4135
4136 (defun gnus-group-edit-group-method (group)
4137   "Edit the select method of GROUP."
4138   (interactive (list (gnus-group-group-name)))
4139   (gnus-group-edit-group group 'method))
4140
4141 (defun gnus-group-edit-group-parameters (group)
4142   "Edit the group parameters of GROUP."
4143   (interactive (list (gnus-group-group-name)))
4144   (gnus-group-edit-group group 'params))
4145
4146 (defun gnus-group-edit-group-done (part group)
4147   "Get info from buffer, update variables and jump to the group buffer."
4148   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4149   (goto-char (point-min))
4150   (let ((form (read (current-buffer)))
4151         (winconf gnus-prev-winconf))
4152     (if (eq part 'info) 
4153         (gnus-group-set-info form)
4154       (gnus-group-set-info form group part))
4155     (kill-buffer (current-buffer))
4156     (and winconf (set-window-configuration winconf))
4157     (set-buffer gnus-group-buffer)
4158     (gnus-group-update-group (gnus-group-group-name))
4159     (gnus-group-position-cursor)))
4160
4161 (defun gnus-group-make-help-group ()
4162   "Create the (ding) Gnus documentation group."
4163   (interactive)
4164   (let ((path load-path)
4165         name)
4166     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4167                                    "gnus-help" '(nndoc "gnus-help")))
4168                        gnus-newsrc-hashtb)
4169          (error "Documentation group already exists"))
4170     (while (and path
4171                 (not (file-exists-p (concat (file-name-as-directory (car path))
4172                                             "doc.txt"))))
4173       (setq path (cdr path)))
4174     (or path (error "Couldn't find doc group"))
4175     (gnus-group-make-group 
4176      (gnus-group-real-name name)
4177      (list 'nndoc name
4178            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4179            (list 'nndoc-article-type 'mbox))))
4180   (gnus-group-position-cursor))
4181
4182 (defun gnus-group-make-doc-group (file type)
4183   "Create a group that uses a single file as the source."
4184   (interactive 
4185    (list (read-file-name "File name: ") 
4186          (let ((err "")
4187                found char)
4188            (while (not found)
4189              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4190              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4191                                ((= char ?b) 'babyl)
4192                                ((= char ?d) 'digest)
4193                                (t (setq err (format "%c unknown. " char))
4194                                   nil))))
4195            found)))
4196   (let* ((file (expand-file-name file))
4197          (name (gnus-generate-new-group-name
4198                 (gnus-group-prefixed-name
4199                  (file-name-nondirectory file) '(nndoc "")))))
4200     (gnus-group-make-group 
4201      (gnus-group-real-name name)
4202      (list 'nndoc name
4203            (list 'nndoc-address file)
4204            (list 'nndoc-article-type type)))))
4205
4206 (defun gnus-group-make-archive-group ()
4207   "Create the (ding) Gnus archive group."
4208   (interactive)
4209   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4210                      gnus-newsrc-hashtb)
4211        (error "Archive group already exists"))
4212   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4213   (gnus-group-position-cursor))
4214
4215 (defun gnus-group-make-directory-group (dir)
4216   "Create an nndir group.
4217 The user will be prompted for a directory. The contents of this
4218 directory will be used as a newsgroup. The directory should contain
4219 mail messages or news articles in files that have numeric names."
4220   (interactive
4221    (list (read-file-name "Create group from directory: ")))
4222   (or (file-exists-p dir) (error "No such directory"))
4223   (or (file-directory-p dir) (error "Not a directory"))
4224   (gnus-group-make-group dir "nndir" dir)
4225   (gnus-group-position-cursor))
4226
4227 (defun gnus-group-make-kiboze-group (group address scores)
4228   "Create an nnkiboze group.
4229 The user will be prompted for a name, a regexp to match groups, and
4230 score file entries for articles to include in the group."
4231   (interactive
4232    (list
4233     (read-string "nnkiboze group name: ")
4234     (read-string "Source groups (regexp): ")
4235     (let ((headers (mapcar (lambda (group) (list group))
4236                            '("subject" "from" "number" "date" "message-id"
4237                              "references" "chars" "lines" "xref")))
4238           scores header regexp regexps)
4239       (while (not (equal "" (setq header (completing-read 
4240                                           "Match on header: " headers nil t))))
4241         (setq regexps nil)
4242         (while (not (equal "" (setq regexp (read-string 
4243                                             (format "Match on %s (string): "
4244                                                     header)))))
4245           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4246         (setq scores (cons (cons header regexps) scores)))
4247       scores)))
4248   (gnus-group-make-group group "nnkiboze" address)
4249   (save-excursion
4250     (gnus-set-work-buffer)
4251     (let (emacs-lisp-mode-hook)
4252       (pp scores (current-buffer)))
4253     (write-region (point-min) (point-max) 
4254                   (concat (or gnus-kill-files-directory "~/News")
4255                           "nnkiboze:" group "." gnus-score-file-suffix)))
4256   (gnus-group-position-cursor))
4257
4258 (defun gnus-group-add-to-virtual (n vgroup)
4259   "Add the current group to a virtual group."
4260   (interactive
4261    (list current-prefix-arg
4262          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4263                           "nnvirtual:")))
4264   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4265       (error "%s is not an nnvirtual group" vgroup))
4266   (let* ((groups (gnus-group-process-prefix n))
4267          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4268     (setcar (cdr method)
4269             (concat 
4270              (nth 1 method) "\\|"
4271              (mapconcat 
4272               (lambda (s) 
4273                 (gnus-group-remove-mark s)
4274                 (concat "\\(^" (regexp-quote s) "$\\)"))
4275               groups "\\|"))))
4276   (gnus-group-position-cursor))
4277
4278 (defun gnus-group-make-empty-virtual (group)
4279   "Create a new, fresh, empty virtual group."
4280   (interactive "sCreate new, empty virtual group: ")
4281   (let* ((method (list 'nnvirtual "^$"))
4282          (pgroup (gnus-group-prefixed-name group method)))
4283     ;; Check whether it exists already.
4284     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4285          (error "Group %s already exists." pgroup))
4286     ;; Subscribe the new group after the group on the current line.
4287     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4288     (gnus-group-update-group pgroup)
4289     (forward-line -1)
4290     (gnus-group-position-cursor)))
4291
4292 (defun gnus-group-enter-directory (dir)
4293   "Enter an ephemeral nneething group."
4294   (interactive "DDirectory to read: ")
4295   (let* ((method (list 'nneething dir))
4296          (leaf (gnus-group-prefixed-name
4297                 (file-name-nondirectory (directory-file-name dir))
4298                 method))
4299          (name (gnus-generate-new-group-name leaf)))
4300     (let ((nneething-read-only t))
4301       (or (gnus-group-read-ephemeral-group 
4302            name method t
4303            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4304                                       'summary 'group)))
4305           (error "Couldn't enter %s" dir)))))
4306
4307 ;; Group sorting commands
4308 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4309
4310 (defun gnus-group-sort-groups ()
4311   "Sort the group buffer using `gnus-group-sort-function'."
4312   (interactive)
4313   (setq gnus-newsrc-alist 
4314         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4315   (gnus-make-hashtable-from-newsrc-alist)
4316   (gnus-group-list-groups))
4317
4318 (defun gnus-group-sort-by-alphabet (info1 info2)
4319   (string< (car info1) (car info2)))
4320
4321 (defun gnus-group-sort-by-unread (info1 info2)
4322   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4323         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4324     (< (or (and (numberp n1) n1) 0)
4325        (or (and (numberp n2) n2) 0))))
4326
4327 (defun gnus-group-sort-by-level (info1 info2)
4328   (< (nth 1 info1) (nth 1 info2)))
4329
4330 ;; Group catching up.
4331
4332 (defun gnus-group-catchup-current (&optional n all)
4333   "Mark all articles not marked as unread in current newsgroup as read.
4334 If prefix argument N is numeric, the ARG next newsgroups will be
4335 caught up. If ALL is non-nil, marked articles will also be marked as
4336 read. Cross references (Xref: header) of articles are ignored.
4337 The difference between N and actual number of newsgroups that were
4338 caught up is returned."
4339   (interactive "P")
4340   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4341                gnus-expert-user
4342                (gnus-y-or-n-p
4343                 (if all
4344                     "Do you really want to mark all articles as read? "
4345                   "Mark all unread articles as read? "))))
4346       n
4347     (let ((groups (gnus-group-process-prefix n))
4348           (ret 0))
4349       (while groups
4350         ;; Virtual groups have to be given special treatment. 
4351         (let ((method (gnus-find-method-for-group (car groups))))
4352           (if (eq 'nnvirtual (car method))
4353               (nnvirtual-catchup-group
4354                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4355         (gnus-group-remove-mark (car groups))
4356         (if (prog1
4357                 (gnus-group-goto-group (car groups))
4358               (gnus-group-catchup (car groups) all))
4359             (gnus-group-update-group-line)
4360           (setq ret (1+ ret)))
4361         (setq groups (cdr groups)))
4362       (gnus-group-next-unread-group 1)
4363       ret)))
4364
4365 (defun gnus-group-catchup-current-all (&optional n)
4366   "Mark all articles in current newsgroup as read.
4367 Cross references (Xref: header) of articles are ignored."
4368   (interactive "P")
4369   (gnus-group-catchup-current n 'all))
4370
4371 (defun gnus-group-catchup (group &optional all)
4372   "Mark all articles in GROUP as read.
4373 If ALL is non-nil, all articles are marked as read.
4374 The return value is the number of articles that were marked as read,
4375 or nil if no action could be taken."
4376   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4377          (num (car entry))
4378          (marked (nth 3 (nth 2 entry))))
4379     (if (not (numberp (car entry)))
4380         (gnus-message 1 "Can't catch up; non-active group")
4381       ;; Do the updating only if the newsgroup isn't killed.
4382       (if (not entry)
4383           ()
4384         (gnus-update-read-articles 
4385          group (and (not all) (append (cdr (assq 'tick marked))
4386                                       (cdr (assq 'dormant marked))))
4387          nil (and (not all) (cdr (assq 'tick marked))))
4388         (and all 
4389              (setq marked (nth 3 (nth 2 entry)))
4390              (setcar (nthcdr 3 (nth 2 entry)) 
4391                      (delq (assq 'dormant marked) 
4392                            (nth 3 (nth 2 entry)))))))
4393     num))
4394
4395 (defun gnus-group-expire-articles (&optional n)
4396   "Expire all expirable articles in the current newsgroup."
4397   (interactive "P")
4398   (let ((groups (gnus-group-process-prefix n))
4399         group)
4400     (or groups (error "No groups to expire"))
4401     (while groups
4402       (setq group (car groups)
4403             groups (cdr groups))
4404       (gnus-group-remove-mark group)
4405       (if (not (gnus-check-backend-function 'request-expire-articles group))
4406           ()
4407         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4408                (expirable (if (memq 'total-expire (nth 5 info))
4409                               (cons nil (gnus-list-of-read-articles group))
4410                             (assq 'expire (nth 3 info)))))
4411           (and expirable 
4412                (setcdr expirable
4413                        (gnus-request-expire-articles 
4414                         (cdr expirable) group))))))))
4415
4416 (defun gnus-group-expire-all-groups ()
4417   "Expire all expirable articles in all newsgroups."
4418   (interactive)
4419   (save-excursion
4420     (gnus-message 5 "Expiring...")
4421     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4422                                      (cdr gnus-newsrc-alist))))
4423       (gnus-group-expire-articles nil)))
4424   (gnus-group-position-cursor)
4425   (gnus-message 5 "Expiring...done"))
4426
4427 (defun gnus-group-set-current-level (n level)
4428   "Set the level of the next N groups to LEVEL."
4429   (interactive "P\nnLevel: ")
4430   (or (and (>= level 1) (<= level gnus-level-killed))
4431       (error "Illegal level: %d" level))
4432   (let ((groups (gnus-group-process-prefix n))
4433         group)
4434     (while groups
4435       (setq group (car groups)
4436             groups (cdr groups))
4437       (gnus-group-remove-mark group)
4438       (gnus-message 6 "Changed level of %s from %d to %d" 
4439                     group (gnus-group-group-level) level)
4440       (gnus-group-change-level group level
4441                                (gnus-group-group-level))
4442       (gnus-group-update-group-line)))
4443   (gnus-group-position-cursor))
4444
4445 (defun gnus-group-unsubscribe-current-group (&optional n)
4446   "Toggle subscription of the current group.
4447 If given numerical prefix, toggle the N next groups."
4448   (interactive "P")
4449   (let ((groups (gnus-group-process-prefix n))
4450         group)
4451     (while groups
4452       (setq group (car groups)
4453             groups (cdr groups))
4454       (gnus-group-remove-mark group)
4455       (gnus-group-unsubscribe-group
4456        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4457                  gnus-level-default-unsubscribed
4458                gnus-level-default-subscribed))
4459       (gnus-group-update-group-line))
4460     (gnus-group-next-group 1)))
4461
4462 (defun gnus-group-unsubscribe-group (group &optional level)
4463   "Toggle subscribe from/to unsubscribe GROUP.
4464 New newsgroup is added to .newsrc automatically."
4465   (interactive
4466    (list (completing-read
4467           "Group: " gnus-active-hashtb nil 
4468           (memq gnus-select-method gnus-have-read-active-file))))
4469   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4470     (cond (newsrc
4471            ;; Toggle subscription flag.
4472            (gnus-group-change-level 
4473             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4474                                            gnus-level-subscribed) 
4475                                        (1+ gnus-level-subscribed)
4476                                      gnus-level-default-subscribed)))
4477            (gnus-group-update-group group))
4478           ((and (stringp group)
4479                 (or (not (memq gnus-select-method gnus-have-read-active-file))
4480                     (gnus-gethash group gnus-active-hashtb)))
4481            ;; Add new newsgroup.
4482            (gnus-group-change-level 
4483             group 
4484             (if level level gnus-level-default-subscribed) 
4485             (or (and (member group gnus-zombie-list) 
4486                      gnus-level-zombie) 
4487                 gnus-level-killed)
4488             (and (gnus-group-group-name)
4489                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4490            (gnus-group-update-group group))
4491           (t (error "No such newsgroup: %s" group)))
4492     (gnus-group-position-cursor)))
4493
4494 (defun gnus-group-transpose-groups (n)
4495   "Move the current newsgroup up N places.
4496 If given a negative prefix, move down instead. The difference between
4497 N and the number of steps taken is returned." 
4498   (interactive "p")
4499   (or (gnus-group-group-name)
4500       (error "No group on current line"))
4501   (gnus-group-kill-group 1)
4502   (prog1
4503       (forward-line (- n))
4504     (gnus-group-yank-group)
4505     (gnus-group-position-cursor)))
4506
4507 (defun gnus-group-kill-all-zombies ()
4508   "Kill all zombie newsgroups."
4509   (interactive)
4510   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4511   (setq gnus-zombie-list nil)
4512   (gnus-group-list-groups))
4513
4514 (defun gnus-group-kill-region (begin end)
4515   "Kill newsgroups in current region (excluding current point).
4516 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4517   (interactive "r")
4518   (let ((lines
4519          ;; Count lines.
4520          (save-excursion
4521            (count-lines
4522             (progn
4523               (goto-char begin)
4524               (beginning-of-line)
4525               (point))
4526             (progn
4527               (goto-char end)
4528               (beginning-of-line)
4529               (point))))))
4530     (goto-char begin)
4531     (beginning-of-line)                 ;Important when LINES < 1
4532     (gnus-group-kill-group lines)))
4533
4534 (defun gnus-group-kill-group (&optional n)
4535   "The the next N groups.
4536 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4537 However, only groups that were alive can be yanked; already killed 
4538 groups or zombie groups can't be yanked.
4539 The return value is the name of the (last) group that was killed."
4540   (interactive "P")
4541   (let ((buffer-read-only nil)
4542         (groups (gnus-group-process-prefix n))
4543         group entry level)
4544     (while groups
4545       (setq group (car groups)
4546             groups (cdr groups))
4547       (gnus-group-remove-mark group)
4548       (setq level (gnus-group-group-level))
4549       (gnus-delete-line)
4550       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4551           (setq gnus-list-of-killed-groups 
4552                 (cons (cons (car entry) (nth 2 entry)) 
4553                       gnus-list-of-killed-groups)))
4554       (gnus-group-change-level 
4555        (if entry entry group) gnus-level-killed (if entry nil level)))
4556     (gnus-group-position-cursor)
4557     group))
4558
4559 (defun gnus-group-yank-group (&optional arg)
4560   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4561 inserting it before the current newsgroup.  The numeric ARG specifies
4562 how many newsgroups are to be yanked.  The name of the (last)
4563 newsgroup yanked is returned."
4564   (interactive "p")
4565   (if (not arg) (setq arg 1))
4566   (let (info group prev)
4567     (while (>= (setq arg (1- arg)) 0)
4568       (if (not (setq info (car gnus-list-of-killed-groups)))
4569           (error "No more newsgroups to yank"))
4570       (setq group (nth 2 info))
4571       ;; Find which newsgroup to insert this one before - search
4572       ;; backward until something suitable is found. If there are no
4573       ;; other newsgroups in this buffer, just make this newsgroup the
4574       ;; first newsgroup.
4575       (setq prev (gnus-group-group-name))
4576       (gnus-group-change-level 
4577        info (nth 2 info) gnus-level-killed 
4578        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4579        t)
4580       (gnus-group-insert-group-line-info (nth 1 info))
4581       (setq gnus-list-of-killed-groups 
4582             (cdr gnus-list-of-killed-groups)))
4583     (forward-line -1)
4584     (gnus-group-position-cursor)
4585     group))
4586       
4587 (defun gnus-group-list-all-groups (&optional arg)
4588   "List all newsgroups with level ARG or lower.
4589 Default is gnus-level-unsubscribed, which lists all subscribed and most
4590 unsubscribed groups."
4591   (interactive "P")
4592   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4593
4594 (defun gnus-group-list-killed ()
4595   "List all killed newsgroups in the group buffer."
4596   (interactive)
4597   (if (not gnus-killed-list)
4598       (gnus-message 6 "No killed groups")
4599     (let (gnus-group-list-mode)
4600       (funcall gnus-group-prepare-function 
4601                gnus-level-killed t gnus-level-killed))
4602     (goto-char (point-min)))
4603   (gnus-group-position-cursor))
4604
4605 (defun gnus-group-list-zombies ()
4606   "List all zombie newsgroups in the group buffer."
4607   (interactive)
4608   (if (not gnus-zombie-list)
4609       (gnus-message 6 "No zombie groups")
4610     (let (gnus-group-list-mode)
4611       (funcall gnus-group-prepare-function
4612                gnus-level-zombie t gnus-level-zombie))
4613     (goto-char (point-min)))
4614   (gnus-group-position-cursor))
4615
4616 (defun gnus-group-get-new-news (&optional arg)
4617   "Get newly arrived articles.
4618 If ARG is non-nil, it should be a number between one and nine to
4619 specify which levels you are interested in re-scanning."
4620   (interactive "P")
4621   (run-hooks 'gnus-get-new-news-hook)
4622   (setq arg (gnus-group-default-level arg t))
4623   (if (and gnus-read-active-file (not arg))
4624       (progn
4625         (gnus-read-active-file)
4626         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4627     (let ((gnus-read-active-file (not arg))
4628           (gnus-have-read-active-file 
4629            (and (not arg) gnus-have-read-active-file)))
4630       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4631   (gnus-group-list-groups))
4632
4633 (defun gnus-group-get-new-news-this-group (&optional n)
4634   "Check for newly arrived news in the current group (and the N-1 next groups).
4635 The difference between N and the number of newsgroup checked is returned.
4636 If N is negative, this group and the N-1 previous groups will be checked."
4637   (interactive "P")
4638   (let* ((groups (gnus-group-process-prefix n))
4639          (ret (if (numberp n) (- n (length groups)) 0))
4640          group)
4641     (while groups
4642       (setq group (car groups)
4643             groups (cdr groups))
4644       (gnus-group-remove-mark group)
4645       (or (gnus-get-new-news-in-group group)
4646           (progn 
4647             (ding) 
4648             (message "%s error: %s" group (gnus-status-message group))
4649             (sit-for 2))))
4650     (gnus-group-next-unread-group 1 t)
4651     (gnus-summary-position-cursor)
4652     ret))
4653
4654 (defun gnus-get-new-news-in-group (group)
4655   (and group 
4656        (gnus-activate-group group)
4657        (progn
4658          (gnus-get-unread-articles-in-group 
4659           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4660           (gnus-gethash group gnus-active-hashtb))
4661          (gnus-group-update-group-line)
4662          t)))
4663
4664 (defun gnus-group-fetch-faq (group)
4665   "Fetch the FAQ for the current group."
4666   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4667   (or group (error "No group name given"))
4668   (let ((file (concat gnus-group-faq-directory group))) 
4669     (if (not (file-exists-p file))
4670         (error "No such file: %s" file)
4671       (find-file file))))
4672   
4673 (defun gnus-group-describe-group (force &optional group)
4674   "Display a description of the current newsgroup."
4675   (interactive (list current-prefix-arg (gnus-group-group-name)))
4676   (and force (setq gnus-description-hashtb nil))
4677   (let ((method (gnus-find-method-for-group group))
4678         desc)
4679     (or group (error "No group name given"))
4680     (and (or (and gnus-description-hashtb
4681                   ;; We check whether this group's method has been
4682                   ;; queried for a description file.  
4683                   (gnus-gethash 
4684                    (gnus-group-prefixed-name "" method) 
4685                    gnus-description-hashtb))
4686              (setq desc (gnus-group-get-description group))
4687              (gnus-read-descriptions-file method))
4688          (message
4689           (or desc (gnus-gethash group gnus-description-hashtb)
4690               "No description available")))))
4691
4692 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4693 (defun gnus-group-describe-all-groups (&optional force)
4694   "Pop up a buffer with descriptions of all newsgroups."
4695   (interactive "P")
4696   (and force (setq gnus-description-hashtb nil))
4697   (if (not (or gnus-description-hashtb
4698                (gnus-read-all-descriptions-files)))
4699       (error "Couldn't request descriptions file"))
4700   (let ((buffer-read-only nil)
4701         b)
4702     (erase-buffer)
4703     (mapatoms
4704      (lambda (group)
4705        (setq b (point))
4706        (insert (format "      *: %-20s %s\n" (symbol-name group)
4707                        (symbol-value group)))
4708        (add-text-properties 
4709         b (1+ b) (list 'gnus-group group
4710                        'gnus-unread t 'gnus-marked nil
4711                        'gnus-level (1+ gnus-level-subscribed))))
4712      gnus-description-hashtb)
4713     (goto-char (point-min))
4714     (gnus-group-position-cursor)))
4715
4716 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4717 (defun gnus-group-apropos (regexp &optional search-description)
4718   "List all newsgroups that have names that match a regexp."
4719   (interactive "sGnus apropos (regexp): ")
4720   (let ((prev "")
4721         (obuf (current-buffer))
4722         groups des)
4723     ;; Go through all newsgroups that are known to Gnus.
4724     (mapatoms 
4725      (lambda (group)
4726        (and (symbol-name group)
4727             (string-match regexp (symbol-name group))
4728             (setq groups (cons (symbol-name group) groups))))
4729      gnus-active-hashtb)
4730     ;; Go through all descriptions that are known to Gnus. 
4731     (if search-description
4732         (mapatoms 
4733          (lambda (group)
4734            (and (string-match regexp (symbol-value group))
4735                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4736                 (setq groups (cons (symbol-name group) groups))))
4737          gnus-description-hashtb))
4738     (if (not groups)
4739         (gnus-message 3 "No groups matched \"%s\"." regexp)
4740       ;; Print out all the groups.
4741       (save-excursion
4742         (pop-to-buffer "*Gnus Help*")
4743         (buffer-disable-undo (current-buffer))
4744         (erase-buffer)
4745         (setq groups (sort groups 'string<))
4746         (while groups
4747           ;; Groups may be entered twice into the list of groups.
4748           (if (not (string= (car groups) prev))
4749               (progn
4750                 (insert (setq prev (car groups)) "\n")
4751                 (if (and gnus-description-hashtb
4752                          (setq des (gnus-gethash (car groups) 
4753                                                  gnus-description-hashtb)))
4754                     (insert "  " des "\n"))))
4755           (setq groups (cdr groups)))
4756         (goto-char (point-min))))
4757     (pop-to-buffer obuf)))
4758
4759 (defun gnus-group-description-apropos (regexp)
4760   "List all newsgroups that have names or descriptions that match a regexp."
4761   (interactive "sGnus description apropos (regexp): ")
4762   (if (not (or gnus-description-hashtb
4763                (gnus-read-all-descriptions-files)))
4764       (error "Couldn't request descriptions file"))
4765   (gnus-group-apropos regexp t))
4766
4767 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4768 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4769   "List all groups with unread articles that match REGEXP.
4770 If the prefix LEVEL is non-nil, it should be a number that says which
4771 level to cut off listing groups. 
4772 If ALL, also list groups with no unread articles.
4773 If LOWEST, don't list groups with level lower than LOWEST."
4774   (interactive "P\nsList newsgroups matching: ")
4775   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4776                            all (or lowest 1) regexp)
4777   (goto-char (point-min))
4778   (gnus-group-position-cursor))
4779
4780 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4781   "List all groups that match REGEXP.
4782 If the prefix LEVEL is non-nil, it should be a number that says which
4783 level to cut off listing groups. 
4784 If LOWEST, don't list groups with level lower than LOWEST."
4785   (interactive "P\nsList newsgroups matching: ")
4786   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4787
4788 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4789 (defun gnus-group-save-newsrc ()
4790   "Save the Gnus startup files."
4791   (interactive)
4792   (gnus-save-newsrc-file))
4793
4794 (defun gnus-group-restart (&optional arg)
4795   "Force Gnus to read the .newsrc file."
4796   (interactive "P")
4797   (gnus-save-newsrc-file)
4798   (gnus-setup-news 'force)
4799   (gnus-group-list-groups arg))
4800
4801 (defun gnus-group-read-init-file ()
4802   "Read the Gnus elisp init file."
4803   (interactive)
4804   (gnus-read-init-file))
4805
4806 (defun gnus-group-check-bogus-groups (&optional silent)
4807   "Check bogus newsgroups.
4808 If given a prefix, don't ask for confirmation before removing a bogus
4809 group."
4810   (interactive "P")
4811   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4812   (gnus-group-list-groups))
4813
4814 (defun gnus-group-edit-global-kill (&optional article group)
4815   "Edit the global kill file.
4816 If GROUP, edit that local kill file instead."
4817   (interactive "P")
4818   (setq gnus-current-kill-article article)
4819   (gnus-kill-file-edit-file group)
4820   (gnus-message 6
4821    (substitute-command-keys
4822     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4823
4824 (defun gnus-group-edit-local-kill (article group)
4825   "Edit a local kill file."
4826   (interactive (list nil (gnus-group-group-name)))
4827   (gnus-group-edit-global-kill article group))
4828
4829 (defun gnus-group-force-update ()
4830   "Update `.newsrc' file."
4831   (interactive)
4832   (gnus-save-newsrc-file))
4833
4834 (defun gnus-group-suspend ()
4835   "Suspend the current Gnus session.
4836 In fact, cleanup buffers except for group mode buffer.
4837 The hook gnus-suspend-gnus-hook is called before actually suspending."
4838   (interactive)
4839   (run-hooks 'gnus-suspend-gnus-hook)
4840   ;; Kill Gnus buffers except for group mode buffer.
4841   (let ((group-buf (get-buffer gnus-group-buffer)))
4842     ;; Do this on a separate list in case the user does a ^G before we finish
4843     (let ((gnus-buffer-list
4844            (delq group-buf (delq gnus-dribble-buffer
4845                                  (append gnus-buffer-list nil)))))
4846       (while gnus-buffer-list
4847         (gnus-kill-buffer (car gnus-buffer-list))
4848         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4849     (if group-buf
4850         (progn
4851           (setq gnus-buffer-list (list group-buf))
4852           (bury-buffer group-buf)
4853           (delete-windows-on group-buf t)))))
4854
4855 (defun gnus-group-clear-dribble ()
4856   "Clear all information from the dribble buffer."
4857   (interactive)
4858   (gnus-dribble-clear))
4859
4860 (defun gnus-group-exit ()
4861   "Quit reading news after updating .newsrc.eld and .newsrc.
4862 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4863   (interactive)
4864   (if (or noninteractive                ;For gnus-batch-kill
4865           (zerop (buffer-size))         ;No news is good news.
4866           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4867           (not gnus-interactive-exit)   ;Without confirmation
4868           gnus-expert-user
4869           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4870       (progn
4871         (run-hooks 'gnus-exit-gnus-hook)
4872         ;; Offer to save data from non-quitted summary buffers.
4873         (gnus-offer-save-summaries)
4874         ;; Save the newsrc file(s).
4875         (gnus-save-newsrc-file)
4876         ;; Kill-em-all.
4877         (gnus-close-backends)
4878         ;; Reset everything.
4879         (gnus-clear-system))))
4880
4881 (defun gnus-close-backends ()
4882   ;; Send a close request to all backends that support such a request. 
4883   (let ((methods gnus-valid-select-methods)
4884         func)
4885     (while methods
4886       (if (fboundp (setq func (intern (concat (car (car methods))
4887                                               "-request-close"))))
4888           (funcall func))
4889       (setq methods (cdr methods)))))
4890
4891 (defun gnus-group-quit ()
4892   "Quit reading news without updating .newsrc.eld or .newsrc.
4893 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4894   (interactive)
4895   (if (or noninteractive                ;For gnus-batch-kill
4896           (zerop (buffer-size))
4897           (not (gnus-server-opened gnus-select-method))
4898           gnus-expert-user
4899           (not gnus-current-startup-file)
4900           (gnus-yes-or-no-p
4901            (format "Quit reading news without saving %s? "
4902                    (file-name-nondirectory gnus-current-startup-file))))
4903       (progn
4904         (run-hooks 'gnus-exit-gnus-hook)
4905         (if gnus-use-full-window
4906             (delete-other-windows)
4907           (gnus-remove-some-windows))
4908         (gnus-dribble-save)
4909         (gnus-close-backends)
4910         (gnus-clear-system))))
4911
4912 (defun gnus-offer-save-summaries ()
4913   (save-excursion
4914     (let ((buflist (buffer-list)) buffers bufname)
4915       (while buflist
4916         (and (setq bufname (buffer-name (car buflist)))
4917              (string-match "Summary" bufname)
4918              (setq buffers (cons bufname buffers)))
4919         (setq buflist (cdr buflist)))
4920       (map-y-or-n-p "Update summary buffer %s? "
4921                     (lambda (buf)
4922                       (set-buffer buf)
4923                       (and
4924                        ;; We check that this is, indeed, a summary buffer.
4925                        (eq major-mode 'gnus-summary-mode)
4926                        (gnus-summary-exit)))
4927                     buffers))))
4928
4929 (defun gnus-group-describe-briefly ()
4930   "Give a one line description of the group mode commands."
4931   (interactive)
4932   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
4933
4934 (defun gnus-group-browse-foreign-server (method)
4935   "Browse a foreign news server.
4936 If called interactively, this function will ask for a select method
4937  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4938 If not, METHOD should be a list where the first element is the method
4939 and the second element is the address."
4940   (interactive
4941    (list (let ((how (completing-read 
4942                      "Which backend: "
4943                      (append gnus-valid-select-methods gnus-server-alist)
4944                      nil t "nntp")))
4945            ;; We either got a backend name or a virtual server name.
4946            ;; If the first, we also need an address.
4947            (if (assoc how gnus-valid-select-methods)
4948                (list (intern how)
4949                      ;; Suggested by mapjph@bath.ac.uk.
4950                      (completing-read 
4951                       "Address: " 
4952                       (mapcar (lambda (server) (list server))
4953                               gnus-secondary-servers)))
4954              ;; We got a server name, so we find the method.
4955              (gnus-server-to-method how)))))
4956   (gnus-browse-foreign-server method))
4957
4958 \f
4959 ;;;
4960 ;;; Browse Server Mode
4961 ;;;
4962
4963 (defvar gnus-browse-mode-hook nil)
4964 (defvar gnus-browse-mode-map nil)
4965 (put 'gnus-browse-mode 'mode-class 'special)
4966
4967 (if gnus-browse-mode-map
4968     nil
4969   (setq gnus-browse-mode-map (make-keymap))
4970   (suppress-keymap gnus-browse-mode-map)
4971   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4972   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4973   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4974   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4975   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4976   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4977   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4978   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4979   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4980   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4981   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4982   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4983   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4984   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4985   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4986   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4987   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4988   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4989   )
4990
4991 (defvar gnus-browse-current-method nil)
4992 (defvar gnus-browse-return-buffer nil)
4993
4994 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4995
4996 (defun gnus-browse-foreign-server (method &optional return-buffer)
4997   (setq gnus-browse-current-method method)
4998   (setq gnus-browse-return-buffer return-buffer)
4999   (let ((gnus-select-method method)
5000         groups group)
5001     (gnus-message 5 "Connecting to %s..." (nth 1 method))
5002     (or (gnus-check-server method)
5003         (error "Unable to contact server: %s" (gnus-status-message method)))
5004     (or (gnus-request-list method)
5005         (error "Couldn't request list: %s" (gnus-status-message method)))
5006     (get-buffer-create gnus-browse-buffer)
5007     (gnus-add-current-to-buffer-list)
5008     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
5009     (gnus-configure-windows 'browse)
5010     (buffer-disable-undo (current-buffer))
5011     (let ((buffer-read-only nil))
5012       (erase-buffer))
5013     (gnus-browse-mode)
5014     (setq mode-line-buffer-identification
5015           (format
5016            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
5017     (save-excursion
5018       (set-buffer nntp-server-buffer)
5019       (let ((cur (current-buffer)))
5020         (goto-char (point-min))
5021         (or (string= gnus-ignored-newsgroups "")
5022             (delete-matching-lines gnus-ignored-newsgroups))
5023         (while (re-search-forward 
5024                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
5025           (goto-char (match-end 1))
5026           (setq groups (cons (cons (buffer-substring (match-beginning 1)
5027                                                      (match-end 1))
5028                                    (max 0 (- (1+ (read cur)) (read cur))))
5029                              groups)))))
5030     (setq groups (sort groups 
5031                        (lambda (l1 l2)
5032                          (string< (car l1) (car l2)))))
5033     (let ((buffer-read-only nil))
5034       (while groups
5035         (setq group (car groups))
5036         (insert 
5037          (format "K%7d: %s\n" (cdr group) (car group)))
5038         (setq groups (cdr groups))))
5039     (switch-to-buffer (current-buffer))
5040     (goto-char (point-min))
5041     (gnus-group-position-cursor)))
5042
5043 (defun gnus-browse-mode ()
5044   "Major mode for browsing a foreign server.
5045
5046 All normal editing commands are switched off.
5047
5048 \\<gnus-browse-mode-map>
5049 The only things you can do in this buffer is
5050
5051 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5052 The group will be inserted into the group buffer upon exit from this
5053 buffer.  
5054
5055 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5056
5057 3) `\\[gnus-browse-exit]' to return to the group buffer."
5058   (interactive)
5059   (kill-all-local-variables)
5060   (if gnus-visual (gnus-browse-make-menu-bar))
5061   (setq mode-line-modified "-- ")
5062   (make-local-variable 'mode-line-format)
5063   (setq mode-line-format (copy-sequence mode-line-format))
5064   (and (equal (nth 3 mode-line-format) "   ")
5065        (setcar (nthcdr 3 mode-line-format) ""))
5066   (setq major-mode 'gnus-browse-mode)
5067   (setq mode-name "Browse Server")
5068   (setq mode-line-process nil)
5069   (use-local-map gnus-browse-mode-map)
5070   (buffer-disable-undo (current-buffer))
5071   (setq truncate-lines t)
5072   (setq buffer-read-only t)
5073   (run-hooks 'gnus-browse-mode-hook))
5074
5075 (defun gnus-browse-read-group (&optional no-article)
5076   "Enter the group at the current line."
5077   (interactive)
5078   (let ((group (gnus-browse-group-name)))
5079     (or (gnus-group-read-ephemeral-group 
5080          group gnus-browse-current-method nil
5081          (cons (current-buffer) 'browse))
5082         (error "Couldn't enter %s" group))))
5083
5084 (defun gnus-browse-select-group ()
5085   "Select the current group."
5086   (interactive)
5087   (gnus-browse-read-group 'no))
5088
5089 (defun gnus-browse-next-group (n)
5090   "Go to the next group."
5091   (interactive "p")
5092   (prog1
5093       (forward-line n)
5094     (gnus-group-position-cursor)))
5095
5096 (defun gnus-browse-prev-group (n)
5097   "Go to the next group."
5098   (interactive "p")
5099   (gnus-browse-next-group (- n)))
5100
5101 (defun gnus-browse-unsubscribe-current-group (arg)
5102   "(Un)subscribe to the next ARG groups."
5103   (interactive "p")
5104   (and (eobp)
5105        (error "No group at current line."))
5106   (let ((ward (if (< arg 0) -1 1))
5107         (arg (abs arg)))
5108     (while (and (> arg 0)
5109                 (not (eobp))
5110                 (gnus-browse-unsubscribe-group)
5111                 (zerop (gnus-browse-next-group ward)))
5112       (setq arg (1- arg)))
5113     (gnus-group-position-cursor)
5114     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5115     arg))
5116
5117 (defun gnus-browse-group-name ()
5118   (save-excursion
5119     (beginning-of-line)
5120     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5121         ()
5122       (gnus-group-prefixed-name 
5123        (buffer-substring (match-beginning 1) (match-end 1))
5124        gnus-browse-current-method))))
5125   
5126 (defun gnus-browse-unsubscribe-group ()
5127   (let ((sub nil)
5128         (buffer-read-only nil)
5129         group)
5130     (save-excursion
5131       (beginning-of-line)
5132       (if (= (following-char) ?K) (setq sub t))
5133       (setq group (gnus-browse-group-name))
5134       (beginning-of-line)
5135       (delete-char 1)
5136       (if sub
5137           (progn
5138             (gnus-group-change-level 
5139              (list t group gnus-level-default-subscribed
5140                    nil nil gnus-browse-current-method) 
5141              gnus-level-default-subscribed gnus-level-killed
5142              (and (car (nth 1 gnus-newsrc-alist))
5143                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5144                                 gnus-newsrc-hashtb))
5145              t)
5146             (insert ? ))
5147         (gnus-group-change-level 
5148          group gnus-level-killed gnus-level-default-subscribed)
5149         (insert ?K)))
5150     t))
5151
5152 (defun gnus-browse-exit ()
5153   "Quit browsing and return to the group buffer."
5154   (interactive)
5155   (if (eq major-mode 'gnus-browse-mode)
5156       (kill-buffer (current-buffer)))
5157   (if gnus-browse-return-buffer
5158       (gnus-configure-windows 'server 'force)
5159     (gnus-configure-windows 'group 'force)
5160     (gnus-group-list-groups nil)))
5161
5162 (defun gnus-browse-describe-briefly ()
5163   "Give a one line description of the group mode commands."
5164   (interactive)
5165   (gnus-message 6
5166    (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")))
5167       
5168 \f
5169 ;;;
5170 ;;; Gnus summary mode
5171 ;;;
5172
5173 (defvar gnus-summary-mode-map nil)
5174 (defvar gnus-summary-mark-map nil)
5175 (defvar gnus-summary-mscore-map nil)
5176 (defvar gnus-summary-article-map nil)
5177 (defvar gnus-summary-thread-map nil)
5178 (defvar gnus-summary-goto-map nil)
5179 (defvar gnus-summary-exit-map nil)
5180 (defvar gnus-summary-interest-map nil)
5181 (defvar gnus-summary-sort-map nil)
5182 (defvar gnus-summary-backend-map nil)
5183 (defvar gnus-summary-save-map nil)
5184 (defvar gnus-summary-wash-map nil)
5185 (defvar gnus-summary-wash-hide-map nil)
5186 (defvar gnus-summary-wash-highlight-map nil)
5187 (defvar gnus-summary-wash-time-map nil)
5188 (defvar gnus-summary-help-map nil)
5189
5190 (put 'gnus-summary-mode 'mode-class 'special)
5191
5192 (if gnus-summary-mode-map
5193     nil
5194   (setq gnus-summary-mode-map (make-keymap))
5195   (suppress-keymap gnus-summary-mode-map)
5196
5197   ;; Non-orthogonal keys
5198
5199   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5200   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5201   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5202   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5203   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5204   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5205   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5206   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5207   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5208   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5209   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5210   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5211   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5212   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5213   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5214   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5215   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5216   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5217   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5218   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5219   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5220   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5221   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5222   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5223   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5224   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5225   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5226   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5227   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5228   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5229   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5230   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5231   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5232   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5233   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5234   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5235   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5236   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5237   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5238   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5239   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5240   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5241   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5242   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5243   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5244   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5245   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5246   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5247   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5248   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5249   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5250   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5251   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5252   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5253   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5254   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5255   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5256   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5257   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5258   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5259   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5260   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5261   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5262   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5263   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5264   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5265   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5266   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5267   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5268   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5269   (define-key gnus-summary-mode-map "V" 'gnus-version)
5270   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5271   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5272   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5273   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5274   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5275   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5276   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5277   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5278 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5279   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5280   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5281   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5282 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5283   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5284   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5285   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5286   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5287   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5288
5289
5290   ;; Sort of orthogonal keymap
5291   (define-prefix-command 'gnus-summary-mark-map)
5292   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5293   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5294   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5295   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5296   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5297   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5298   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5299   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5300   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5301   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5302   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5303   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5304   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5305   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5306   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5307   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5308   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5309   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5310   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5311   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5312   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5313   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5314   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5315   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5316
5317   (define-prefix-command 'gnus-summary-mscore-map)
5318   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5319   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5320   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5321   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5322   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5323
5324   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5325   
5326   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5327   
5328   (define-prefix-command 'gnus-summary-goto-map)
5329   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5330   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5331   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5332   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5333   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5334   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5335   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5336   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5337   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5338   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5339   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5340   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5341   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5342   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5343
5344
5345   (define-prefix-command 'gnus-summary-thread-map)
5346   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5347   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5348   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5349   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5350   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5351   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5352   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5353   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5354   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5355   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5356   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5357   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5358   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5359   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5360
5361   
5362   (define-prefix-command 'gnus-summary-exit-map)
5363   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5364   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5365   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5366   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5367   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5368   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5369   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5370   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5371   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5372   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5373   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5374
5375
5376   (define-prefix-command 'gnus-summary-article-map)
5377   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5378   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5379   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5380   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5381   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5382   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5383   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5384   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5385   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5386   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5387   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5388   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5389   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5390   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5391
5392
5393
5394   (define-prefix-command 'gnus-summary-wash-map)
5395   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5396
5397   (define-prefix-command 'gnus-summary-wash-hide-map)
5398   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5399   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5400   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5401   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5402   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5403   (define-key gnus-summary-wash-hide-map "\C-c" 'gnus-article-hide-citation-maybe)
5404
5405   (define-prefix-command 'gnus-summary-wash-highlight-map)
5406   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5407   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5408   (define-key gnus-summary-wash-highlight-map "h" 'gnus-article-highlight-headers)
5409   (define-key gnus-summary-wash-highlight-map "c" 'gnus-article-highlight-citation)
5410   (define-key gnus-summary-wash-highlight-map "s" 'gnus-article-highlight-signature)
5411
5412   (define-prefix-command 'gnus-summary-wash-time-map)
5413   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5414   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5415   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5416   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5417   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5418
5419   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5420   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5421   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5422   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5423   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5424   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5425   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5426   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5427   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5428   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5429
5430
5431   (define-prefix-command 'gnus-summary-help-map)
5432   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5433   (define-key gnus-summary-help-map "v" 'gnus-version)
5434   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5435   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5436   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5437   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5438
5439
5440   (define-prefix-command 'gnus-summary-backend-map)
5441   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5442   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5443   (define-key gnus-summary-backend-map "\M-\C-e" 
5444     'gnus-summary-expire-articles-now)
5445   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5446   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5447   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5448   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5449   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5450   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5451   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5452
5453
5454   (define-prefix-command 'gnus-summary-save-map)
5455   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5456   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5457   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5458   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5459   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5460   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5461   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5462   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5463 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5464
5465   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5466
5467   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5468 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5469 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5470 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5471 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5472 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5473 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5474   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5475 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5476 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5477
5478   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5479
5480 ;  (define-prefix-command 'gnus-summary-sort-map)
5481 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5482 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5483 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5484 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5485 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5486 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5487
5488   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5489   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5490   )
5491
5492
5493 \f
5494
5495 (defun gnus-summary-mode (&optional group)
5496   "Major mode for reading articles.
5497
5498 All normal editing commands are switched off.
5499 \\<gnus-summary-mode-map>
5500 Each line in this buffer represents one article.  To read an
5501 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5502 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5503 respectively.
5504
5505 You can also post articles and send mail from this buffer.  To 
5506 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5507 of an article, type `\\[gnus-summary-reply]'.
5508
5509 There are approx. one gazillion commands you can execute in this 
5510 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5511
5512 The following commands are available:
5513
5514 \\{gnus-summary-mode-map}"
5515   (interactive)
5516   (if gnus-visual (gnus-summary-make-menu-bar))
5517   (kill-all-local-variables)
5518   (let ((locals gnus-summary-local-variables))
5519     (while locals
5520       (if (consp (car locals))
5521           (progn
5522             (make-local-variable (car (car locals)))
5523             (set (car (car locals)) (eval (cdr (car locals)))))
5524         (make-local-variable (car locals))
5525         (set (car locals) nil))
5526       (setq locals (cdr locals))))
5527   (gnus-make-thread-indent-array)
5528   (setq mode-line-modified "-- ")
5529   (make-local-variable 'mode-line-format)
5530   (setq mode-line-format (copy-sequence mode-line-format))
5531   (and (equal (nth 3 mode-line-format) "   ")
5532        (setcar (nthcdr 3 mode-line-format) ""))
5533   (setq major-mode 'gnus-summary-mode)
5534   (setq mode-name "Summary")
5535   (make-local-variable 'minor-mode-alist)
5536   (use-local-map gnus-summary-mode-map)
5537   (buffer-disable-undo (current-buffer))
5538   (setq buffer-read-only t)             ;Disable modification
5539   (setq truncate-lines t)
5540   (setq selective-display t)
5541   (setq selective-display-ellipses t)   ;Display `...'
5542   (setq buffer-display-table gnus-summary-display-table)
5543   (setq gnus-newsgroup-name group)
5544   (run-hooks 'gnus-summary-mode-hook))
5545
5546 (defun gnus-summary-make-display-table ()
5547   ;; Change the display table.  Odd characters have a tendency to mess
5548   ;; up nicely formatted displays - we make all possible glyphs
5549   ;; display only a single character.
5550
5551   ;; We start from the standard display table, if any.
5552   (setq gnus-summary-display-table 
5553         (or (copy-sequence standard-display-table)
5554             (make-display-table)))
5555   ;; Nix out all the control chars...
5556   (let ((i 32))
5557     (while (>= (setq i (1- i)) 0)
5558       (aset gnus-summary-display-table i [??])))
5559   ;; ... but not newline and cr, of course. (cr is necessary for the
5560   ;; selective display).  
5561   (aset gnus-summary-display-table ?\n nil)
5562   (aset gnus-summary-display-table ?\r nil)
5563   ;; We nix out any glyphs over 126 that are not set already.  
5564   (let ((i 256))
5565     (while (>= (setq i (1- i)) 127)
5566       ;; Only modify if the entry is nil.
5567       (or (aref gnus-summary-display-table i) 
5568           (aset gnus-summary-display-table i [??])))))
5569
5570 (defun gnus-summary-clear-local-variables ()
5571   (let ((locals gnus-summary-local-variables))
5572     (while locals
5573       (if (consp (car locals))
5574           (and (vectorp (car (car locals)))
5575                (set (car (car locals)) nil))
5576         (and (vectorp (car locals))
5577              (set (car locals) nil)))
5578       (setq locals (cdr locals)))))
5579
5580 ;; Some summary mode macros.
5581
5582 ;; Return a header specified by a NUMBER.
5583 (defun gnus-get-header-by-number (number)
5584   (save-excursion
5585     (set-buffer gnus-summary-buffer)
5586     (or gnus-newsgroup-headers-hashtb-by-number
5587         (gnus-make-headers-hashtable-by-number))
5588     (gnus-gethash (int-to-string number)
5589                   gnus-newsgroup-headers-hashtb-by-number)))
5590
5591 ;; Fast version of the function above.
5592 (defmacro gnus-get-header-by-num (number)
5593   (` (gnus-gethash (int-to-string (, number)) 
5594                    gnus-newsgroup-headers-hashtb-by-number)))
5595
5596 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5597   "Search for article forward.
5598 If UNREAD is non-nil, only unread articles are selected.
5599 If SUBJECT is non-nil, the article which has the same subject will be
5600 searched for. 
5601 If BACKWARD is non-nil, the search will be performed backwards instead."
5602   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5603
5604 (defmacro gnus-summary-search-backward (&optional unread subject)
5605   "Search for article backward.
5606 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5607 If 2nd optional argument SUBJECT is non-nil, the article which has
5608 the same subject will be searched for."
5609   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5610
5611 (defmacro gnus-summary-article-number (&optional number-or-nil)
5612   "The article number of the article on the current line.
5613 If there isn's an article number here, then we return the current
5614 article number."
5615   (if number-or-nil
5616       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5617     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5618          gnus-current-article)))
5619
5620 (defmacro gnus-summary-thread-level ()
5621   "The thread level of the article on the current line."
5622   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5623        0))
5624
5625 (defmacro gnus-summary-article-mark ()
5626   "The mark on the current line."
5627   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5628
5629 (defun gnus-summary-subject-string ()
5630   "Return current subject string or nil if nothing."
5631   (let ((article (gnus-summary-article-number))
5632         header)
5633     (and article 
5634          (setq header (gnus-get-header-by-num article))
5635          (vectorp header)
5636          (header-subject header))))
5637
5638 ;; Various summary mode internalish functions.
5639
5640 (defun gnus-mouse-pick-article (e)
5641   (interactive "e")
5642   (mouse-set-point e)
5643   (gnus-summary-next-page nil t))
5644
5645 (defun gnus-summary-setup-buffer (group)
5646   "Initialize summary buffer."
5647   (let ((buffer (concat "*Summary " group "*")))
5648     (if (get-buffer buffer)
5649         (progn
5650           (set-buffer buffer)
5651           (not gnus-newsgroup-begin))
5652       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5653       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5654       (gnus-add-current-to-buffer-list)
5655       (gnus-summary-mode group)
5656       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5657       (setq gnus-newsgroup-name group)
5658       t)))
5659
5660 (defun gnus-set-global-variables ()
5661   ;; Set the global equivalents of the summary buffer-local variables
5662   ;; to the latest values they had. These reflect the summary buffer
5663   ;; that was in action when the last article was fetched.
5664   (if (eq major-mode 'gnus-summary-mode) 
5665       (progn
5666         (setq gnus-summary-buffer (current-buffer))
5667         (let ((name gnus-newsgroup-name)
5668               (marked gnus-newsgroup-marked)
5669               (unread gnus-newsgroup-unreads)
5670               (headers gnus-current-headers)
5671               (score-file gnus-current-score-file))
5672           (save-excursion
5673             (set-buffer gnus-group-buffer)
5674             (setq gnus-newsgroup-name name)
5675             (setq gnus-newsgroup-marked marked)
5676             (setq gnus-newsgroup-unreads unread)
5677             (setq gnus-current-headers headers)
5678             (setq gnus-current-score-file score-file))))))
5679
5680 (defun gnus-summary-insert-dummy-line (sformat subject number)
5681   (if (not sformat) 
5682       (setq sformat gnus-summary-dummy-line-format-spec))
5683   (let (b)
5684     (beginning-of-line)
5685     (setq b (point))
5686     (insert (eval sformat))
5687     (add-text-properties
5688      b (1+ b)
5689      (list 'gnus-number number 
5690            'gnus-mark gnus-dummy-mark
5691            'gnus-level 0))))
5692
5693 (defvar gnus-thread-indent-array nil)
5694 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5695 (defun gnus-make-thread-indent-array ()
5696   (let ((n 200))
5697     (if (and gnus-thread-indent-array
5698              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5699         nil
5700       (setq gnus-thread-indent-array (make-vector 201 "")
5701             gnus-thread-indent-array-level gnus-thread-indent-level)
5702       (while (>= n 0)
5703         (aset gnus-thread-indent-array n
5704               (make-string (* n gnus-thread-indent-level) ? ))
5705         (setq n (1- n))))))
5706
5707 (defun gnus-summary-insert-line 
5708   (sformat header level current unread replied expirable subject-or-nil
5709            &optional dummy score)
5710   (or sformat (setq sformat gnus-summary-line-format-spec))
5711   (let* ((indentation (aref gnus-thread-indent-array level))
5712          (lines (header-lines header))
5713          (score (or score gnus-summary-default-score 0))
5714          (score-char
5715           (if (or (null gnus-summary-default-score)
5716                   (<= (abs (- score gnus-summary-default-score))
5717                       gnus-summary-zcore-fuzz)) ? 
5718             (if (< score gnus-summary-default-score)
5719                 gnus-score-below-mark gnus-score-over-mark)))
5720          (replied (if replied gnus-replied-mark ? ))
5721          (from (header-from header))
5722          (name (cond 
5723                 ((string-match "(.+)" from)
5724                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5725                 ((string-match "<[^>]+> *$" from)
5726                  (let ((beg (match-beginning 0)))
5727                    (or (and (string-match "^\"[^\"]*\"" from)
5728                             (substring from (1+ (match-beginning 0))
5729                                        (1- (match-end 0))))
5730                        (substring from 0 beg))))
5731                 (t from)))
5732          (subject (header-subject header))
5733          (number (header-number header))
5734          (opening-bracket (if dummy ?\< ?\[))
5735          (closing-bracket (if dummy ?\> ?\]))
5736          (buffer-read-only nil)
5737          (b (progn (beginning-of-line) (point))))
5738     (or (numberp lines) (setq lines 0))
5739     (insert (eval sformat))
5740     (add-text-properties
5741      b (1+ b) (list 'gnus-number number 
5742                     'gnus-mark (or unread gnus-unread-mark)
5743                     'gnus-level level))))
5744
5745 (defun gnus-summary-update-line (&optional dont-update)
5746   ;; Update summary line after change.
5747   (or (not gnus-summary-default-score)
5748       gnus-summary-inhibit-highlight
5749       (let ((gnus-summary-inhibit-highlight t)
5750             (article (gnus-summary-article-number)))
5751         (progn
5752           (or dont-update
5753               (if (and gnus-summary-mark-below
5754                        (< (gnus-summary-article-score)
5755                           gnus-summary-mark-below))
5756                   (and (not (memq article gnus-newsgroup-marked))
5757                        (not (memq article gnus-newsgroup-dormant))
5758                        (memq article gnus-newsgroup-unreads)
5759                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5760                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5761                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5762           (and gnus-visual
5763                (run-hooks 'gnus-summary-update-hook))))))
5764
5765 (defun gnus-summary-update-lines (&optional beg end)
5766   ;; Mark article as read (or not) by taking into account scores.
5767   (let ((beg (or beg (point-min)))
5768         (end (or end (point-max))))
5769     (if (or (not gnus-summary-default-score)
5770             gnus-summary-inhibit-highlight)
5771         ()
5772       (let ((gnus-summary-inhibit-highlight t)
5773             article)
5774         (save-excursion
5775           (set-buffer gnus-summary-buffer)
5776           (goto-char beg)
5777           (beginning-of-line)
5778           (while (and (not (eobp)) (< (point) end))
5779             (if (and gnus-summary-mark-below
5780                      (< (or (cdr (assq 
5781                                   (setq article (get-text-property 
5782                                                  (point) 'gnus-number))
5783                                   gnus-newsgroup-scored))
5784                             gnus-summary-default-score 0)
5785                         gnus-summary-mark-below))
5786                 ;; We want to possibly mark it as read...
5787                 (and (not (memq article gnus-newsgroup-marked))
5788                      (not (memq article gnus-newsgroup-dormant))
5789                      (memq article gnus-newsgroup-unreads)
5790                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5791               ;; We want to possibly mark it as unread.
5792               (and (eq (get-text-property (point) 'gnus-mark)
5793                        gnus-low-score-mark)
5794                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5795             ;; Do the visual highlights at the same time.
5796             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5797             (forward-line 1)))))))
5798
5799 (defvar gnus-tmp-gathered nil)
5800
5801 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5802   ;; Sum up all elements (and sub-elements) in a list.
5803   (let* ((number
5804           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5805           (if (and (consp thread) (cdr thread))
5806               (apply
5807                '+ 1 (mapcar
5808                      'gnus-summary-number-of-articles-in-thread 
5809                      (cdr thread)))
5810             1)))
5811     (if char 
5812         (if (> number 1) gnus-not-empty-thread-mark
5813           gnus-empty-thread-mark)
5814       number)))
5815
5816 (defun gnus-summary-read-group 
5817   (group &optional show-all no-article kill-buffer)
5818   "Start reading news in newsgroup GROUP.
5819 If SHOW-ALL is non-nil, already read articles are also listed.
5820 If NO-ARTICLE is non-nil, no article is selected initially."
5821   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5822   (let* ((new-group (gnus-summary-setup-buffer group))
5823          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5824                                                   group))))
5825          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5826     (cond 
5827      ((not new-group)
5828       (gnus-set-global-variables)
5829       (gnus-kill-buffer kill-buffer)
5830       (gnus-configure-windows 'summary)
5831       (gnus-set-mode-line 'summary)
5832       (gnus-summary-position-cursor)
5833       (message "")
5834       t)
5835      ((null did-select) 
5836       (and (eq major-mode 'gnus-summary-mode)
5837            (not (equal (current-buffer) kill-buffer))
5838            (progn
5839              (kill-buffer (current-buffer))
5840              (if (not quit-config)
5841                  (progn
5842                    (set-buffer gnus-group-buffer)
5843                    (gnus-group-jump-to-group group)
5844                    (gnus-group-next-unread-group 1))
5845                (if (not (buffer-name (car quit-config)))
5846                    (gnus-configure-windows 'group 'force)
5847                  (set-buffer (car quit-config))
5848                  (and (eq major-mode 'gnus-summary-mode)
5849                       (gnus-set-global-variables))
5850                  (gnus-configure-windows (cdr quit-config))))))
5851       (message "Can't select group")
5852       nil)
5853      ((eq did-select 'quit)
5854       (and (eq major-mode 'gnus-summary-mode)
5855            (not (equal (current-buffer) kill-buffer))
5856            (kill-buffer (current-buffer)))
5857       (gnus-kill-buffer kill-buffer)
5858       (if (not quit-config)
5859           (progn
5860             (set-buffer gnus-group-buffer)
5861             (gnus-group-jump-to-group group)
5862             (gnus-group-next-unread-group 1)
5863             (gnus-configure-windows 'group 'force))
5864         (if (not (buffer-name (car quit-config)))
5865             (gnus-configure-windows 'group 'force)
5866           (set-buffer (car quit-config))
5867           (and (eq major-mode 'gnus-summary-mode)
5868                (gnus-set-global-variables))
5869           (gnus-configure-windows (cdr quit-config))))
5870       (signal 'quit nil))
5871      (t
5872       (gnus-set-global-variables)
5873       ;; Save the active value in effect when the group was entered.
5874       (setq gnus-newsgroup-active 
5875             (gnus-copy-sequence
5876              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5877       ;; You can change the subjects in this hook.
5878       (run-hooks 'gnus-select-group-hook)
5879       ;; Do score processing.
5880       (and gnus-use-scoring (gnus-possibly-score-headers))
5881       (gnus-update-format-specifications)
5882       ;; Generate the summary buffer.
5883       (gnus-summary-prepare)
5884       ;; Create the header hashtb.
5885       (gnus-make-headers-hashtable-by-number)
5886       (if (zerop (buffer-size))
5887           (cond (gnus-newsgroup-dormant
5888                  (gnus-summary-show-all-dormant))
5889                 ((and gnus-newsgroup-scored show-all)
5890                  (gnus-summary-show-all-expunged))))
5891       ;; Function `gnus-apply-kill-file' must be called in this hook.
5892       (run-hooks 'gnus-apply-kill-hook)
5893       (if (zerop (buffer-size))
5894           (progn
5895             ;; This newsgroup is empty.
5896             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5897             (gnus-message 6 "No unread news")
5898             (gnus-kill-buffer kill-buffer)
5899             nil)
5900         ;;(save-excursion
5901         ;;  (if kill-buffer
5902         ;;      (let ((gnus-summary-buffer kill-buffer))
5903         ;;      (gnus-configure-windows 'group))))
5904         ;; Hide conversation thread subtrees.  We cannot do this in
5905         ;; gnus-summary-prepare-hook since kill processing may not
5906         ;; work with hidden articles.
5907         (and gnus-show-threads
5908              gnus-thread-hide-subtree
5909              (gnus-summary-hide-all-threads))
5910         ;; Show first unread article if requested.
5911         (goto-char (point-min))
5912         (if (and (not no-article)
5913                  gnus-auto-select-first
5914                  (gnus-summary-first-unread-article))
5915             ()
5916           (gnus-configure-windows 'summary))
5917         (gnus-set-mode-line 'summary)
5918         (gnus-summary-position-cursor)
5919         ;; If in async mode, we send some info to the backend.
5920         (and gnus-newsgroup-async
5921              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5922              (gnus-request-asynchronous 
5923               gnus-newsgroup-name
5924               (if (and gnus-asynchronous-article-function
5925                        (fboundp gnus-asynchronous-article-function))
5926                   (funcall gnus-asynchronous-article-function
5927                            gnus-newsgroup-threads)
5928                 gnus-newsgroup-threads)))
5929         (gnus-kill-buffer kill-buffer)
5930         (if (not (get-buffer-window gnus-group-buffer))
5931             ()
5932           ;; gotta use windows, because recenter does wierd stuff if
5933           ;; the current buffer ain't the displayed window.
5934           (let ((owin (selected-window))) 
5935             (select-window (get-buffer-window gnus-group-buffer))
5936             (and (gnus-group-goto-group group)
5937                  (recenter))
5938             (select-window owin))))
5939       t))))
5940
5941 (defun gnus-summary-prepare ()
5942   ;; Generate the summary buffer.
5943   (let ((buffer-read-only nil))
5944     (erase-buffer)
5945     (gnus-summary-prepare-threads 
5946      (if gnus-show-threads
5947          (gnus-gather-threads 
5948           (gnus-sort-threads 
5949            (if (and gnus-summary-expunge-below
5950                     (not gnus-fetch-old-headers))
5951                (gnus-make-threads-and-expunge)
5952              (gnus-make-threads))))
5953        gnus-newsgroup-headers)
5954      'cull)
5955     (gnus-summary-update-lines)
5956     ;; Remove the final newline.
5957     ;;(goto-char (point-max))
5958     ;;(delete-char -1)
5959     ;; Call hooks for modifying summary buffer.
5960     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5961     (goto-char (point-min))
5962     (run-hooks 'gnus-summary-prepare-hook)))
5963
5964 (defun gnus-gather-threads (threads)
5965   "Gather threads that have lost their roots."
5966   (if (not gnus-summary-make-false-root)
5967       threads 
5968     (let ((hashtb (gnus-make-hashtable 1023))
5969           (prev threads)
5970           (result threads)
5971           subject hthread whole-subject)
5972       (while threads
5973         (setq whole-subject 
5974               (setq subject (header-subject (car (car threads)))))
5975         (if gnus-summary-gather-subject-limit
5976             (or (and (numberp gnus-summary-gather-subject-limit)
5977                      (> (length subject) gnus-summary-gather-subject-limit)
5978                      (setq subject
5979                            (substring subject 0 
5980                                       gnus-summary-gather-subject-limit)))
5981                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5982                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5983           (setq subject (gnus-simplify-subject-re subject)))
5984         (if (setq hthread 
5985                   (gnus-gethash subject hashtb))
5986             (progn
5987               (or (stringp (car (car hthread)))
5988                   (setcar hthread (list whole-subject (car hthread))))
5989               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5990                                            (list (car threads))))
5991               (setcdr prev (cdr threads))
5992               (setq threads prev))
5993           (gnus-sethash subject threads hashtb))
5994         (setq prev threads)
5995         (setq threads (cdr threads)))
5996       result)))
5997
5998 (defun gnus-make-threads ()
5999   ;; This function takes the dependencies already made by 
6000   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6001   ;; through the dependecies in the hash table and finds all the
6002   ;; roots. Roots do not refer back to any valid articles.
6003   (gnus-message 6 "Threading...")
6004   (let (roots new-roots)
6005     (and gnus-fetch-old-headers
6006          (eq gnus-headers-retrieved-by 'nov)
6007          (gnus-build-old-threads))
6008     (mapatoms
6009      (lambda (refs)
6010        (if (not (car (symbol-value refs)))
6011            (setq roots (append (cdr (symbol-value refs)) roots))
6012          ;; Ok, these refer back to valid articles, but if
6013          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6014          ;; the root has the same subject as its children. The children
6015          ;; that do not are made into roots and removed from the list
6016          ;; of children. 
6017          (or gnus-thread-ignore-subject
6018              (let* ((prev (symbol-value refs))
6019                     (subject (gnus-simplify-subject-re 
6020                               (header-subject (car prev))))
6021                     (headers (cdr prev)))
6022                (while headers
6023                  (if (not (string= subject
6024                                    (gnus-simplify-subject-re 
6025                                     (header-subject (car headers)))))
6026                      (progn
6027                        (setq new-roots (cons (car headers) new-roots))
6028                        (setcdr prev (cdr headers)))
6029                    (setq prev headers))
6030                  (setq headers (cdr headers)))))))
6031      gnus-newsgroup-dependencies)
6032
6033     ;; We enter the new roots into the dependencies structure to
6034     ;; ensure that any possible later thread-regeneration will be
6035     ;; possible. 
6036     (let ((r new-roots))
6037       (while r
6038         (gnus-sethash (concat (header-id (car r)) ".boo")
6039                       (list nil (car r)) gnus-newsgroup-dependencies)
6040         (setq r (cdr r))))
6041
6042     (setq roots (nconc new-roots roots))
6043
6044     (prog1
6045         (mapcar 'gnus-trim-thread
6046                 (apply 'append
6047                        (mapcar 'gnus-cut-thread
6048                                (mapcar 'gnus-make-sub-thread roots))))
6049       (gnus-message 6 "Threading...done"))))
6050
6051   
6052 (defun gnus-make-threads-and-expunge ()
6053   ;; This function takes the dependencies already made by 
6054   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6055   ;; through the dependecies in the hash table and finds all the
6056   ;; roots. Roots do not refer back to any valid articles.
6057   (gnus-message 6 "Threading...")
6058   (let ((default (or gnus-summary-default-score 0))
6059         (below gnus-summary-expunge-below)
6060         roots article new-roots)
6061     (and gnus-fetch-old-headers
6062          (eq gnus-headers-retrieved-by 'nov)
6063          (gnus-build-old-threads))
6064     (mapatoms
6065      (lambda (refs)
6066        (if (not (car (symbol-value refs)))
6067            ;; These articles do not refer back to any other articles -
6068            ;; they are roots.
6069            (let ((headers (cdr (symbol-value refs))))
6070              ;; We weed out the low-scored articles.
6071              (while headers
6072                (if (not (< (or (cdr (assq (header-number (car headers))
6073                                           gnus-newsgroup-scored)) default)
6074                            below))
6075                    ;; It is over.
6076                    (setq roots (cons (car headers) roots))
6077                  ;; It is below, so we mark it as read.
6078                  (setq gnus-newsgroup-unreads
6079                        (delq (header-number (car headers))
6080                              gnus-newsgroup-unreads))
6081                  (setq gnus-newsgroup-reads 
6082                        (cons (cons (header-number (car headers))
6083                                    gnus-low-score-mark) 
6084                              gnus-newsgroup-reads)))
6085                (setq headers (cdr headers))))
6086          ;; Ok, these refer back to valid articles, but if
6087          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6088          ;; the root has the same subject as its children. The children
6089          ;; that do not are made into roots and removed from the list
6090          ;; of children. 
6091          (or gnus-thread-ignore-subject
6092              (let* ((prev (symbol-value refs))
6093                     (subject (gnus-simplify-subject-re 
6094                               (header-subject (car prev))))
6095                     (headers (cdr prev)))
6096                (while headers
6097                  (if (not (string= subject
6098                                    (gnus-simplify-subject-re 
6099                                     (header-subject (car headers)))))
6100                      (progn
6101                        (if (not (< (or (cdr (assq (header-number (car headers))
6102                                                   gnus-newsgroup-scored))
6103                                        default) below))
6104                            (setq new-roots (cons (car headers) new-roots))
6105                          (setq gnus-newsgroup-unreads
6106                                (delq (header-number (car headers))
6107                                      gnus-newsgroup-unreads))
6108                          (setq gnus-newsgroup-reads
6109                                (cons (cons (header-number (car headers)) 
6110                                            gnus-low-score-mark) 
6111                                      gnus-newsgroup-reads)))
6112                        (setcdr prev (cdr headers)))
6113                    (setq prev headers))
6114                  (setq headers (cdr headers)))))
6115          ;; If this article is expunged, some of the children might be
6116          ;; roots.  
6117          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6118                                gnus-newsgroup-scored)) default)
6119                 below)
6120              (let* ((prev (symbol-value refs))
6121                     (headers (cdr prev)))
6122                (while headers
6123                  (setq article (header-number (car headers)))
6124                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6125                                  default) below))
6126                      (progn (setq new-roots (cons (car headers) new-roots))
6127                             (setq prev headers))
6128                    (setq gnus-newsgroup-unreads 
6129                          (delq article gnus-newsgroup-unreads))
6130                    (setq gnus-newsgroup-reads 
6131                          (cons (cons article gnus-low-score-mark) 
6132                                gnus-newsgroup-reads))
6133                    (setcdr prev (cdr headers)))
6134                  (setq headers (cdr headers))))
6135            ;; It was not expunged, but we look at expunged children.
6136            (let* ((prev (symbol-value refs))
6137                   (headers (cdr prev))
6138                   article)
6139              (while headers
6140                (setq article (header-number (car headers)))
6141                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6142                                default) below))
6143                    (setq prev headers)
6144                  (setq gnus-newsgroup-unreads 
6145                        (delq article gnus-newsgroup-unreads))
6146                  (setq gnus-newsgroup-reads 
6147                        (cons (cons article gnus-low-score-mark)
6148                              gnus-newsgroup-reads))
6149                  (setcdr prev (cdr headers)))
6150                (setq headers (cdr headers)))))))
6151      gnus-newsgroup-dependencies)
6152
6153     ;; We enter the new roots into the dependencies structure to
6154     ;; ensure that any possible later thread-regeneration will be
6155     ;; possible. 
6156     (let ((r new-roots))
6157       (while r
6158         (gnus-sethash (concat (header-id (car r)) ".boo")
6159                       (list nil (car r)) gnus-newsgroup-dependencies)
6160         (setq r (cdr r))))
6161
6162     (setq roots (nconc new-roots roots))
6163
6164     (prog1
6165         (mapcar 'gnus-trim-thread
6166                 (apply 'append
6167                        (mapcar 'gnus-cut-thread
6168                                (mapcar 'gnus-make-sub-thread roots))))
6169       (gnus-message 6 "Threading...done"))))
6170
6171   
6172 (defun gnus-cut-thread (thread)
6173   ;; Remove leaf dormant or ancient articles from THREAD.
6174   (let ((head (car thread))
6175         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6176     (if (and (null tail)
6177              (let ((number (header-number head)))
6178                (or (memq number gnus-newsgroup-ancient)
6179                    (memq number gnus-newsgroup-dormant)
6180                    (and gnus-summary-expunge-below
6181                         (eq gnus-fetch-old-headers 'some)
6182                         (< (or (cdr (assq number gnus-newsgroup-scored))
6183                                gnus-summary-default-score 0)
6184                            gnus-summary-expunge-below)
6185                         (progn
6186                           (setq gnus-newsgroup-unreads
6187                                 (delq number gnus-newsgroup-unreads))
6188                           (setq gnus-newsgroup-reads
6189                                 (cons (cons number gnus-low-score-mark)
6190                                       gnus-newsgroup-reads))
6191                           t)))))
6192         nil
6193       (list (cons head tail)))))
6194
6195 (defun gnus-trim-thread (thread)
6196   ;; Remove root ancient articles with only one child from THREAD.
6197   (if (and (eq gnus-fetch-old-headers 'some)
6198            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6199            (= (length thread) 2))
6200       (gnus-trim-thread (nth 1 thread))
6201     thread))
6202
6203 (defun gnus-make-sub-thread (root)
6204   ;; This function makes a sub-tree for a node in the tree.
6205   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6206                                               gnus-newsgroup-dependencies)))))
6207     (cons root (mapcar 'gnus-make-sub-thread children))))
6208
6209 (defun gnus-build-old-threads ()
6210   ;; Look at all the articles that refer back to old articles, and
6211   ;; fetch the headers for the articles that aren't there. This will
6212   ;; build complete threads - if the roots haven't been expired by the
6213   ;; server, that is.
6214   (let (id heads)
6215     (mapatoms
6216      (lambda (refs)
6217        (if (not (car (symbol-value refs)))
6218            (progn
6219              (setq heads (cdr (symbol-value refs)))
6220              (while heads
6221                (if (not (memq (header-number (car heads))
6222                               gnus-newsgroup-dormant))
6223                    (progn
6224                      (setq id (symbol-name refs))
6225                      (while (and (setq id (gnus-build-get-header id))
6226                                  (not (car (gnus-gethash 
6227                                             id gnus-newsgroup-dependencies)))))
6228                      (setq heads nil))
6229                  (setq heads (cdr heads)))))))
6230      gnus-newsgroup-dependencies)))
6231
6232 (defun gnus-build-get-header (id)
6233   ;; Look through the buffer of NOV lines and find the header to
6234   ;; ID. Enter this line into the dependencies hash table, and return
6235   ;; the id of the parent article (if any).
6236   (let ((deps gnus-newsgroup-dependencies)
6237         found header)
6238     (prog1
6239         (save-excursion
6240           (set-buffer nntp-server-buffer)
6241           (goto-char (point-min))
6242           (while (and (not found) (search-forward id nil t))
6243             (beginning-of-line)
6244             (setq found (looking-at 
6245                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6246                                  (regexp-quote id))))
6247             (or found (beginning-of-line 2)))
6248           (if found
6249               (let (ref)
6250                 (beginning-of-line)
6251                 (and
6252                  (setq header (gnus-nov-parse-line 
6253                                (read (current-buffer)) deps))
6254                  (setq ref (header-references header))
6255                  (string-match "\\(<[^>]+>\\) *$" ref)
6256                  (substring ref (match-beginning 1) (match-end 1))))))
6257       (and header
6258            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6259                  gnus-newsgroup-ancient (cons (header-number header)
6260                                               gnus-newsgroup-ancient))))))
6261
6262 ;; Re-build the thread containing ID.
6263 (defun gnus-rebuild-thread (id)
6264   (let ((dep gnus-newsgroup-dependencies)
6265         (buffer-read-only nil)
6266         parent headers refs thread art)
6267     (while (and id (setq headers
6268                          (car (setq art (gnus-gethash (downcase id) dep)))))
6269       (setq parent art)
6270       (setq id (and (setq refs (header-references headers))
6271                     (string-match "\\(<[^>]+>\\) *$" refs)
6272                     (substring refs (match-beginning 1) (match-end 1)))))
6273     (setq thread (gnus-make-sub-thread (car parent)))
6274     (gnus-rebuild-remove-articles thread)
6275     (let ((beg (point)))
6276       (gnus-summary-prepare-threads (list thread))
6277       (gnus-summary-update-lines beg (point)))))
6278
6279 ;; Delete all lines in the summary buffer that correspond to articles
6280 ;; in this thread.
6281 (defun gnus-rebuild-remove-articles (thread)
6282   (and (gnus-summary-goto-subject (header-number (car thread)))
6283        (gnus-delete-line))
6284   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6285
6286 (defun gnus-sort-threads (threads)
6287   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6288   (let ((fun gnus-thread-sort-functions))
6289     (while fun
6290       (setq threads (sort threads (car fun))
6291             fun (cdr fun))))
6292   threads)
6293
6294 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6295 (defmacro gnus-thread-header (thread)
6296   ;; Return header of first article in THREAD.
6297   ;; Note that THREAD must never, evr be anything else than a variable -
6298   ;; using some other form will lead to serious barfage.
6299   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6300   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6301   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6302         (vector thread) 2))
6303
6304 (defun gnus-thread-sort-by-number (h1 h2)
6305   "Sort threads by root article number."
6306   (< (header-number (gnus-thread-header h1))
6307      (header-number (gnus-thread-header h2))))
6308
6309 (defun gnus-thread-sort-by-author (h1 h2)
6310   "Sort threads by root author."
6311   (string-lessp
6312    (let ((extract (funcall 
6313                    gnus-extract-address-components
6314                    (header-from (gnus-thread-header h1)))))
6315      (or (car extract) (cdr extract)))
6316    (let ((extract (funcall
6317                    gnus-extract-address-components 
6318                    (header-from (gnus-thread-header h2)))))
6319      (or (car extract) (cdr extract)))))
6320
6321 (defun gnus-thread-sort-by-subject (h1 h2)
6322   "Sort threads by root subject."
6323   (string-lessp
6324    (downcase (gnus-simplify-subject 
6325               (header-subject (gnus-thread-header h1))))
6326    (downcase (gnus-simplify-subject 
6327               (header-subject (gnus-thread-header h2))))))
6328
6329 (defun gnus-thread-sort-by-date (h1 h2)
6330   "Sort threads by root article date."
6331   (string-lessp
6332    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6333    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6334
6335 (defun gnus-thread-sort-by-score (h1 h2)
6336   "Sort threads by root article score.
6337 Unscored articles will be counted as having a score of zero."
6338   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6339                     gnus-newsgroup-scored))
6340          gnus-summary-default-score 0)
6341      (or (cdr (assq (header-number (gnus-thread-header h2))
6342                     gnus-newsgroup-scored))
6343          gnus-summary-default-score 0)))
6344
6345 (defun gnus-thread-sort-by-total-score (h1 h2)
6346   "Sort threads by the sum of all scores in the thread.
6347 Unscored articles will be counted as having a score of zero."
6348   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6349
6350 (defun gnus-thread-total-score (thread)
6351   ;;  This function find the total score of THREAD.
6352   (if (consp thread)
6353       (if (stringp (car thread))
6354           (apply gnus-thread-score-function 0
6355                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6356         (gnus-thread-total-score-1 thread))
6357     (gnus-thread-total-score-1 (list thread))))
6358
6359 (defun gnus-thread-total-score-1 (root)
6360   ;; This function find the total score of the thread below ROOT.
6361   (setq root (car root))
6362   (apply gnus-thread-score-function
6363          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6364              gnus-summary-default-score 0)
6365          (mapcar 'gnus-thread-total-score
6366                  (cdr (gnus-gethash (downcase (header-id root))
6367                                     gnus-newsgroup-dependencies)))))
6368
6369 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6370 (defvar gnus-tmp-prev-subject "")
6371
6372 (defun gnus-summary-prepare-threads (threads &optional cull)
6373   "Prepare summary buffer from THREADS and indentation LEVEL.  
6374 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6375 or a straight list of headers."
6376   (message "Generating summary...")
6377   (let ((level 0)
6378         thread header number subject stack state gnus-tmp-gathered)
6379     (if (vectorp (car threads))
6380         ;; If this is a straight (sic) list of headers, then a
6381         ;; threaded summary display isn't required, so we just create
6382         ;; an unthreaded one.
6383         (gnus-summary-prepare-unthreaded threads cull)
6384
6385       ;; Do the threaded display.
6386
6387       (while (or threads stack)
6388         
6389         (if threads
6390             ;; If there are some threads, we do them before the
6391             ;; threads on the stack.
6392             (setq thread threads
6393                   header (car (car thread)))
6394           ;; There were no current threads, so we pop something off
6395           ;; the stack. 
6396           (setq state (car stack)
6397                 level (car state)
6398                 thread (cdr state)
6399                 stack (cdr stack)
6400                 header (car (car thread))))
6401
6402         (if (stringp header)
6403             (progn
6404               ;; The header is a dummy root.
6405               (cond 
6406                ((eq gnus-summary-make-false-root 'adopt)
6407                 ;; We let the first article adopt the rest.
6408                 (let ((th (car (cdr (car thread)))))
6409                   (while (cdr th)
6410                     (setq th (cdr th)))
6411                   (setcdr th (cdr (cdr (car thread))))
6412                   (setq gnus-tmp-gathered 
6413                         (nconc (mapcar
6414                                 (lambda (h) (header-number (car h)))
6415                                 (cdr (cdr (car thread))))
6416                                gnus-tmp-gathered))
6417                   (setcdr (cdr (car thread)) nil))
6418                 (setq level -1))
6419                ((eq gnus-summary-make-false-root 'empty)
6420                 ;; We print adopted articles with empty subject fields.
6421                 (setq gnus-tmp-gathered 
6422                       (nconc (mapcar
6423                               (lambda (h) (header-number (car h)))
6424                               (cdr (cdr (car thread))))
6425                              gnus-tmp-gathered))
6426                 (setq level -1))
6427                ((eq gnus-summary-make-false-root 'dummy)
6428                 ;; We output a dummy root.
6429                 (gnus-summary-insert-dummy-line 
6430                  nil header (header-number (car (car (cdr (car thread)))))))
6431                (t
6432                 ;; We do not make a root for the gathered
6433                 ;; sub-threads at all.  
6434                 (setq level -1))))
6435       
6436           (setq number (header-number header)
6437                 subject (header-subject header))
6438
6439           ;; Do the async thing.
6440           (and gnus-newsgroup-async
6441                (setq gnus-newsgroup-threads
6442                      (cons (cons number (header-lines header)) 
6443                            gnus-newsgroup-threads)))
6444
6445           ;; We may have to root out some bad articles...
6446           (and cull
6447                (= level 0)
6448                (cond ((and (memq (setq number (header-number header))
6449                                  gnus-newsgroup-dormant)
6450                            (null thread))
6451                       (setq header nil))
6452                      ((and gnus-summary-expunge-below
6453                            (< (or (cdr (assq number gnus-newsgroup-scored))
6454                                   gnus-summary-default-score 0)
6455                               gnus-summary-expunge-below))
6456                       (setq header nil)
6457                       (setq gnus-newsgroup-unreads 
6458                             (delq number gnus-newsgroup-unreads))
6459                       (setq gnus-newsgroup-reads
6460                             (cons (cons number gnus-low-score-mark)
6461                                   gnus-newsgroup-reads)))))
6462           
6463           (and
6464            header
6465            (progn
6466              (inline
6467                (gnus-summary-insert-line
6468                 nil header level nil 
6469                 (cond 
6470                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6471                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6472                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6473                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6474                  (t (or (cdr (assq number gnus-newsgroup-reads))
6475                         gnus-ancient-mark)))
6476                 (memq number gnus-newsgroup-replied)
6477                 (memq number gnus-newsgroup-expirable)
6478                 (if (and (eq gnus-summary-make-false-root 'empty)
6479                          (memq number gnus-tmp-gathered))
6480                     gnus-summary-same-subject
6481                   (if (or (zerop level)
6482                           (and gnus-thread-ignore-subject
6483                                (not (string= 
6484                                      (gnus-simplify-subject-re
6485                                       gnus-tmp-prev-subject)
6486                                      (gnus-simplify-subject-re
6487                                       subject)))))
6488                       subject
6489                     gnus-summary-same-subject))
6490                 (and (eq gnus-summary-make-false-root 'adopt)
6491                      (memq number gnus-tmp-gathered))
6492                 (cdr (assq number gnus-newsgroup-scored)))
6493
6494                (setq gnus-tmp-prev-subject subject)))))
6495
6496         (if (nth 1 thread) 
6497             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6498         (setq level (1+ level))
6499         (setq threads (cdr (car thread))))))
6500   (message "Generating summary...done"))
6501
6502
6503
6504 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6505   (let (header number)
6506
6507     ;; Do the async thing, if that is required.
6508     (if gnus-newsgroup-async
6509         (setq gnus-newsgroup-threads
6510               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6511                       headers)))
6512
6513     (while headers
6514       (setq header (car headers)
6515             headers (cdr headers)
6516             number (header-number header))
6517
6518       ;; We may have to root out some bad articles...
6519       (cond 
6520        ((and cull
6521              (memq (setq number (header-number header))
6522                    gnus-newsgroup-dormant)))
6523        ((and cull gnus-summary-expunge-below
6524              (< (or (cdr (assq number gnus-newsgroup-scored))
6525                     gnus-summary-default-score 0)
6526                 gnus-summary-expunge-below))
6527         (setq gnus-newsgroup-unreads 
6528               (delq number gnus-newsgroup-unreads))
6529         (setq gnus-newsgroup-reads
6530               (cons (cons number gnus-low-score-mark)
6531                     gnus-newsgroup-reads)))
6532        (t
6533         (gnus-summary-insert-line
6534          nil header 0 nil 
6535          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6536                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6537                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6538                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6539                (t (or (cdr (assq number gnus-newsgroup-reads))
6540                       gnus-ancient-mark)))
6541          (memq number gnus-newsgroup-replied)
6542          (memq number gnus-newsgroup-expirable)
6543          (header-subject header) nil
6544          (cdr (assq number gnus-newsgroup-scored))))))))
6545
6546 (defun gnus-select-newsgroup (group &optional read-all)
6547   "Select newsgroup GROUP.
6548 If READ-ALL is non-nil, all articles in the group are selected."
6549   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6550          (info (nth 2 entry))
6551          articles)
6552     (gnus-check-server
6553      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6554
6555     (or (gnus-check-server gnus-current-select-method)
6556         (error "Couldn't open server"))
6557     
6558     (or (and (null entry)
6559              (gnus-activate-group group))
6560         (and (eq (car entry) t)
6561              (gnus-activate-group (car info)))
6562         (gnus-request-group group t)
6563         (progn
6564           (kill-buffer (current-buffer))
6565           (error "Couldn't request group %s: %s" 
6566                  group (gnus-status-message group))))
6567
6568     (setq gnus-newsgroup-name group)
6569     (setq gnus-newsgroup-unselected nil)
6570     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6571
6572     (and gnus-asynchronous
6573          (gnus-check-backend-function 
6574           'request-asynchronous gnus-newsgroup-name)
6575          (setq gnus-newsgroup-async
6576                (gnus-request-asynchronous gnus-newsgroup-name)))
6577
6578     (setq articles (gnus-articles-to-read group read-all))
6579
6580     (cond 
6581      ((null articles) 
6582       (gnus-message 3 "Couldn't select newsgroup")
6583       'quit)
6584      ((eq articles 0) nil)
6585      (t
6586       ;; Init the dependencies hash table.
6587       (setq gnus-newsgroup-dependencies 
6588             (gnus-make-hashtable (length articles)))
6589       ;; Retrieve the headers and read them in.
6590       (gnus-message 5 "Fetching headers...")
6591       (setq gnus-newsgroup-headers 
6592             (if (eq 'nov (setq gnus-headers-retrieved-by
6593                                ;; This is a naughty hack. To get the
6594                                ;; retrieval of old headers to work, we
6595                                ;; set `nntp-nov-gap' to nil (locally),
6596                                ;; and then just retrieve the headers.
6597                                ;; Mucho magic.
6598                                (if gnus-fetch-old-headers
6599                                    (let (nntp-nov-gap)
6600                                      (gnus-retrieve-headers 
6601                                       (if (not (eq 1 (car articles)))
6602                                           (cons 1 articles)
6603                                         articles)
6604                                       gnus-newsgroup-name))
6605                                  (gnus-retrieve-headers 
6606                                   articles gnus-newsgroup-name))))
6607                 (progn
6608                   (gnus-get-newsgroup-headers-xover articles))
6609               ;; If we were to fetch old headers, but the backend didn't
6610               ;; support XOVER, then it is possible we fetched one article
6611               ;; that we shouldn't have. If that's the case, we remove it.
6612               (if (or (not gnus-fetch-old-headers)
6613                       (eq 1 (car articles)))
6614                   ()
6615                 (save-excursion
6616                   (set-buffer nntp-server-buffer)
6617                   (goto-char (point-min))
6618                   (and 
6619                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6620                    (delete-region       ; So we delete this head.
6621                     (point) 
6622                     (search-forward "\n.\n" nil t)))))
6623               (gnus-get-newsgroup-headers)))
6624       (gnus-message 5 "Fetching headers...done")      
6625       ;; Remove canceled articles from the list of unread articles.
6626       (setq gnus-newsgroup-unreads
6627             (gnus-set-sorted-intersection 
6628              gnus-newsgroup-unreads
6629              (mapcar (lambda (headers) (header-number headers))
6630                      gnus-newsgroup-headers)))
6631       ;; Adjust and set lists of article marks.
6632       (and info
6633            (let (marked)
6634              (gnus-adjust-marked-articles info)
6635              (setq gnus-newsgroup-marked 
6636                    (copy-sequence
6637                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6638              (setq gnus-newsgroup-replied 
6639                    (copy-sequence (cdr (assq 'reply marked))))
6640              (setq gnus-newsgroup-expirable
6641                    (copy-sequence (cdr (assq 'expire marked))))
6642              (setq gnus-newsgroup-killed
6643                    (copy-sequence (cdr (assq 'killed marked))))
6644              (setq gnus-newsgroup-bookmarks 
6645                    (copy-sequence (cdr (assq 'bookmark marked))))
6646              (setq gnus-newsgroup-dormant 
6647                    (copy-sequence (cdr (assq 'dormant marked))))
6648              (setq gnus-newsgroup-scored 
6649                    (copy-sequence (cdr (assq 'score marked))))
6650              (setq gnus-newsgroup-processable nil)))
6651       ;; Check whether auto-expire is to be done in this group.
6652       (setq gnus-newsgroup-auto-expire
6653             (or (and (stringp gnus-auto-expirable-newsgroups)
6654                      (string-match gnus-auto-expirable-newsgroups group))
6655                 (memq 'auto-expire (nth 5 info))))
6656       ;; First and last article in this newsgroup.
6657       (and gnus-newsgroup-headers
6658            (setq gnus-newsgroup-begin 
6659                  (header-number (car gnus-newsgroup-headers)))
6660            (setq gnus-newsgroup-end
6661                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6662       (setq gnus-reffed-article-number -1)
6663       ;; GROUP is successfully selected.
6664       (or gnus-newsgroup-headers t)))))
6665
6666 (defun gnus-articles-to-read (group read-all)
6667   ;; Find out what articles the user wants to read.
6668   (let* ((articles
6669           ;; Select all articles if `read-all' is non-nil, or if all the
6670           ;; unread articles are dormant articles.
6671           (if (or (and read-all (not (numberp read-all)))
6672                   (= (length gnus-newsgroup-unreads) 
6673                      (length gnus-newsgroup-dormant)))
6674               (gnus-uncompress-range 
6675                (gnus-gethash group gnus-active-hashtb))
6676             gnus-newsgroup-unreads))
6677          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6678          (scored (length scored-list))
6679          (number (length articles))
6680          (marked (+ (length gnus-newsgroup-marked)
6681                     (length gnus-newsgroup-dormant)))
6682          (select
6683           (cond 
6684            ((numberp read-all)
6685             read-all)
6686            (t
6687             (condition-case ()
6688                 (cond ((and (or (<= scored marked)
6689                                 (= scored number))
6690                             (numberp gnus-large-newsgroup)
6691                             (> number gnus-large-newsgroup))
6692                        (let ((input
6693                               (read-string
6694                                (format
6695                                 "How many articles from %s (default %d): "
6696                                 gnus-newsgroup-name number))))
6697                          (if (string-match "^[ \t]*$" input)
6698                              number input)))
6699                       ((and (> scored marked) (< scored number))
6700                        (let ((input
6701                               (read-string
6702                                (format 
6703                                 "%s %s (%d scored, %d total): "
6704                                 "How many articles from"
6705                                 group scored number))))
6706                          (if (string-match "^[ \t]*$" input)
6707                              number input)))
6708                       (t number))
6709               (quit nil))))))
6710     (setq select (if (stringp select) (string-to-number select) select))
6711     (if (or (null select) (zerop select))
6712         select
6713       (if (and (not (zerop scored)) (<= (abs select) scored))
6714           (progn
6715             (setq articles (sort scored-list '<))
6716             (setq number (length articles)))
6717         (setq articles (copy-sequence articles)))
6718
6719       (if (< (abs select) number)
6720           (if (< select 0) 
6721               ;; Select the N oldest articles.
6722               (setcdr (nthcdr (1- (abs select)) articles) nil)
6723             ;; Select the N most recent articles.
6724             (setq articles (nthcdr (- number select) articles))))
6725       (setq gnus-newsgroup-unselected
6726             (gnus-sorted-intersection
6727              gnus-newsgroup-unreads
6728              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6729       articles)))
6730
6731 (defun gnus-killed-articles (killed articles)
6732   (let (out)
6733     (while articles
6734       (if (inline (gnus-member-of-range (car articles) killed))
6735           (setq out (cons (car articles) out)))
6736       (setq articles (cdr articles)))
6737     out))
6738
6739 (defun gnus-adjust-marked-articles (info &optional active)
6740   "Remove all marked articles that are no longer legal."
6741   (let ((marked-lists (nth 3 info))
6742         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6743         m prev)
6744     ;; There are many types of marked articles.
6745     (while marked-lists
6746       (setq m (cdr (setq prev (car marked-lists))))
6747       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6748              ;; Make sure that all ticked articles are a subset of the
6749              ;; unread/unselected articles.
6750              (while m
6751                (if (or (memq (car m) gnus-newsgroup-unreads)
6752                        (memq (car m) gnus-newsgroup-unselected))
6753                    (setq prev m)
6754                  (setcdr prev (cdr m)))
6755                (setq m (cdr m))))
6756             ((eq 'score (car prev))
6757              ;; Scored articles should be a subset of
6758              ;; unread/unselected articles. 
6759              (while m
6760                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6761                        (memq (car (car m)) gnus-newsgroup-unreads))
6762                    (setq prev m)
6763                  (setcdr prev (cdr m)))
6764                (setq m (cdr m))))
6765             ((eq 'bookmark (car prev))
6766              ;; Bookmarks should be a subset of active articles.
6767              (while m
6768                (if (< (car (car m)) (car active))
6769                    (setcdr prev (cdr m))
6770                  (setq prev m))
6771                (setq m (cdr m))))
6772             ((eq 'killed (car prev))
6773              ;; Articles that have been through the kill process are
6774              ;; to be a subset of active articles.
6775              (while (and m (< (or (and (numberp (car m)) (car m))
6776                                   (cdr (car m)))
6777                               (car active)))
6778                (setcdr prev (cdr m))
6779                (setq m (cdr m)))
6780              (if (and m (< (or (and (numberp (car m)) (car m))
6781                                (car (car m)))
6782                            (car active))) 
6783                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6784             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6785              ;; The replied and expirable articles have to be articles
6786              ;; that are active. 
6787              (while m
6788                (if (< (car m) (car active))
6789                    (setcdr prev (cdr m))
6790                  (setq prev m))
6791                (setq m (cdr m)))))
6792       (setq marked-lists (cdr marked-lists)))
6793     ;; Remove all lists that are empty.
6794     (setq marked-lists (nth 3 info))
6795     (if marked-lists
6796         (progn
6797           (while (= 1 (length (car marked-lists)))
6798             (setq marked-lists (cdr marked-lists)))
6799           (setq m (cdr (setq prev marked-lists)))
6800           (while m
6801             (if (= 1 (length (car m)))
6802                 (setcdr prev (cdr m))
6803               (setq prev m))
6804             (setq m (cdr m)))
6805           (setcar (nthcdr 3 info) marked-lists)))
6806     ;; Finally, if there are no marked lists at all left, and if there
6807     ;; are no elements after the lists in the info list, we just chop
6808     ;; the info list off before the marked lists.
6809     (and (null marked-lists) 
6810          (not (nthcdr 4 info))
6811          (setcdr (nthcdr 2 info) nil)))
6812   info)
6813
6814 (defun gnus-set-marked-articles 
6815   (info ticked replied expirable killed dormant bookmark score) 
6816   "Enter the various lists of marked articles into the newsgroup info list."
6817   (let (newmarked)
6818     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6819     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6820     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6821                                          newmarked)))
6822     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6823     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6824     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6825                                         newmarked)))
6826     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6827     (if (nthcdr 3 info)
6828         (progn
6829           (setcar (nthcdr 3 info) newmarked)
6830           (and (not newmarked)
6831                (not (nthcdr 4 info))
6832                (setcdr (nthcdr 2 info) nil)))
6833       (if newmarked
6834           (setcdr (nthcdr 2 info) (list newmarked))))))
6835
6836 (defun gnus-add-marked-articles (group type articles &optional info force)
6837   ;; Add ARTICLES of TYPE to the info of GROUP.
6838   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6839   ;; add, but replace marked articles of TYPE with ARTICLES.
6840   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6841         marked m)
6842     (or (not info)
6843         (and (not (setq marked (nthcdr 3 info)))
6844              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6845         (and (not (setq m (assq type (car marked))))
6846              (setcar marked (cons (cons type articles) (car marked))))
6847         (if force
6848             (setcdr m articles)
6849           (nconc m articles)))))
6850          
6851 (defun gnus-set-mode-line (where)
6852   "This function sets the mode line of the article or summary buffers.
6853 If WHERE is `summary', the summary mode line format will be used."
6854   (if (memq where gnus-updated-mode-lines)
6855       (let (mode-string)
6856         (save-excursion
6857           (set-buffer gnus-summary-buffer)
6858           (let* ((mformat (if (eq where 'article) 
6859                               gnus-article-mode-line-format-spec
6860                             gnus-summary-mode-line-format-spec))
6861                  (group-name gnus-newsgroup-name)
6862                  (article-number (or gnus-current-article 0))
6863                  (unread (- (length gnus-newsgroup-unreads)
6864                             (length gnus-newsgroup-dormant)))
6865                  (unread-and-unticked 
6866                   (- unread (length gnus-newsgroup-marked)))
6867                  (unselected (length gnus-newsgroup-unselected))
6868                  (unread-and-unselected
6869                   (cond ((and (zerop unread-and-unticked)
6870                               (zerop unselected)) "")
6871                         ((zerop unselected) 
6872                          (format "{%d more}" unread-and-unticked))
6873                         (t (format "{%d(+%d) more}"
6874                                    unread-and-unticked unselected))))
6875                  (subject
6876                   (if gnus-current-headers
6877                       (header-subject gnus-current-headers) ""))
6878                  (max-len (and gnus-mode-non-string-length
6879                                (- (frame-width) gnus-mode-non-string-length)))
6880                  header) ;; passed as argument to any user-format-funcs
6881             (setq mode-string (eval mformat))
6882             (or (numberp max-len)
6883                 (setq max-len (length mode-string)))
6884             (if (< max-len 4) (setq max-len 4))
6885             (if (> (length mode-string) max-len)
6886                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6887                 ;;  function `substring' might cut on a middle
6888                 ;;  of multi-octet character.
6889                 (setq mode-string 
6890                       (concat (gnus-truncate-string mode-string (- max-len 3))
6891                               "...")))
6892             (setq mode-string (format (format "%%-%ds" max-len)
6893                                       mode-string))))
6894         (setq mode-line-buffer-identification mode-string)
6895         (set-buffer-modified-p t))))
6896
6897 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6898   "Go through the HEADERS list and add all Xrefs to a hash table.
6899 The resulting hash table is returned, or nil if no Xrefs were found."
6900   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6901          (prefix (if (and 
6902                       (gnus-group-foreign-p from-newsgroup)
6903                       (not (memq 'virtual 
6904                                  (assoc (symbol-name (car from-method))
6905                                         gnus-valid-select-methods))))
6906                      (gnus-group-real-prefix from-newsgroup)))
6907          (xref-hashtb (make-vector 63 0))
6908          start group entry number xrefs header)
6909     (while headers
6910       (setq header (car headers))
6911       (if (and (setq xrefs (header-xref header))
6912                (not (memq (header-number header) unreads)))
6913           (progn
6914             (setq start 0)
6915             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6916               (setq start (match-end 0))
6917               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6918                                                     (match-end 1))))
6919               (setq number 
6920                     (string-to-int (substring xrefs (match-beginning 2) 
6921                                               (match-end 2))))
6922               (if (setq entry (gnus-gethash group xref-hashtb))
6923                   (setcdr entry (cons number (cdr entry)))
6924                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6925       (setq headers (cdr headers)))
6926     (if start xref-hashtb nil)))
6927
6928 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6929   "Look through all the headers and mark the Xrefs as read."
6930   (let ((virtual (memq 'virtual 
6931                        (assoc (symbol-name (car (gnus-find-method-for-group 
6932                                                  from-newsgroup)))
6933                               gnus-valid-select-methods)))
6934         name entry info xref-hashtb idlist method
6935         nth4)
6936     (save-excursion
6937       (set-buffer gnus-group-buffer)
6938       (if (setq xref-hashtb 
6939                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6940           (mapatoms 
6941            (lambda (group)
6942              (if (string= from-newsgroup (setq name (symbol-name group)))
6943                  ()
6944                (setq idlist (symbol-value group))
6945                ;; Dead groups are not updated.
6946                (if (and (prog1 
6947                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6948                                   info (nth 2 entry))
6949                           (if (stringp (setq nth4 (nth 4 info)))
6950                               (setq nth4 (gnus-server-to-method nth4))))
6951                         ;; Only do the xrefs if the group has the same
6952                         ;; select method as the group we have just read.
6953                         (or (gnus-methods-equal-p 
6954                              nth4 (gnus-find-method-for-group from-newsgroup))
6955                             virtual
6956                             (equal nth4 
6957                                    (setq method (gnus-find-method-for-group 
6958                                                  from-newsgroup)))
6959                             (and (equal (car nth4) (car method))
6960                                  (equal (nth 1 nth4) (nth 1 method))))
6961                         gnus-use-cross-reference
6962                         (or (not (eq gnus-use-cross-reference t))
6963                             virtual
6964                             ;; Only do cross-references on subscribed
6965                             ;; groups, if that is what is wanted.  
6966                             (<= (nth 1 info) gnus-level-subscribed)))
6967                    (gnus-group-make-articles-read name idlist expirable))))
6968            xref-hashtb)))))
6969
6970 (defun gnus-group-make-articles-read (group articles expirable)
6971   (let* ((num 0)
6972          (entry (gnus-gethash group gnus-newsrc-hashtb))
6973          (info (nth 2 entry))
6974          (active (gnus-gethash group gnus-active-hashtb))
6975          exps expirable range)
6976     ;; First peel off all illegal article numbers.
6977     (if active
6978         (let ((ids articles)
6979               (ticked (cdr (assq 'tick (nth 3 info))))
6980               (dormant (cdr (assq 'dormant (nth 3 info))))
6981               id)
6982           (setq exps nil)
6983           (while ids
6984             (setq id (car ids))
6985             (if (or (> id (cdr active))
6986                     (< id (car active))
6987                     (memq id ticked)
6988                     (memq id dormant))
6989                 (setq articles (delq id articles)))
6990             (and (memq id expirable)
6991                  (setq exps (cons id exps)))
6992             (setq ids (cdr ids)))))
6993     ;; Update expirable articles.
6994     (gnus-add-marked-articles nil 'expirable exps info)
6995     (and active
6996          (null (nth 2 info))
6997          (> (car active) 1)
6998          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6999     (setcar (nthcdr 2 info)
7000             (setq range
7001                   (gnus-add-to-range 
7002                    (nth 2 info) 
7003                    (setq articles (sort articles '<)))))
7004     ;; Then we have to re-compute how many unread
7005     ;; articles there are in this group.
7006     (if active
7007         (progn
7008           (cond 
7009            ((not range)
7010             (setq num (- (1+ (cdr active)) (car active))))
7011            ((not (listp (cdr range)))
7012             (setq num (- (cdr active) (- (1+ (cdr range)) 
7013                                          (car range)))))
7014            (t
7015             (while range
7016               (if (numberp (car range))
7017                   (setq num (1+ num))
7018                 (setq num (+ num (- (1+ (cdr (car range)))
7019                                     (car (car range))))))
7020               (setq range (cdr range)))
7021             (setq num (- (cdr active) num))))
7022           ;; Update the number of unread articles.
7023           (setcar 
7024            entry 
7025            (max 0 (- num 
7026                      (length (cdr (assq 'tick (nth 3 info))))
7027                      (length 
7028                       (cdr (assq 'dormant (nth 3 info)))))))
7029           ;; Update the group buffer.
7030           (gnus-group-update-group group t)))))
7031
7032 (defun gnus-methods-equal-p (m1 m2)
7033   (let ((m1 (or m1 gnus-select-method))
7034         (m2 (or m2 gnus-select-method)))
7035     (or (equal m1 m2)
7036         (and (eq (car m1) (car m2))
7037              (or (not (memq 'address (assoc (symbol-name (car m1))
7038                                             gnus-valid-select-methods)))
7039                  (equal (nth 1 m1) (nth 1 m2)))))))
7040
7041 (defsubst gnus-header-value ()
7042   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7043
7044 (defvar gnus-newsgroup-none-id 0)
7045
7046 (defun gnus-get-newsgroup-headers ()
7047   (setq gnus-article-internal-prepare-hook nil)
7048   (let ((cur nntp-server-buffer)
7049         (dependencies gnus-newsgroup-dependencies)
7050         headers id dep end ref)
7051     (save-excursion
7052       (set-buffer nntp-server-buffer)
7053       (goto-char (point-min))
7054       ;; Search to the beginning of the next header. Error messages
7055       ;; do not begin with 2 or 3.
7056       (while (re-search-forward "^[23][0-9]+ " nil t)
7057         (let ((header (make-vector 9 nil))
7058               (case-fold-search t)
7059               (p (point))
7060               in-reply-to)
7061           (setq id nil
7062                 ref nil)
7063           (header-set-number header (read cur))
7064           ;; This implementation of this function, with nine
7065           ;; search-forwards instead of the one re-search-forward and
7066           ;; a case (which basically was the old function) is actually
7067           ;; about twice as fast, even though it looks messier. You
7068           ;; can't have everything, I guess. Speed and elegance
7069           ;; doesn't always come hand in hand.
7070           (save-restriction
7071             (narrow-to-region (point) (or (save-excursion 
7072                                             (search-forward "\n.\n" nil t))
7073                                           (point)))
7074             (if (search-forward "\nfrom: " nil t)
7075                 (header-set-from header (gnus-header-value))
7076               (header-set-from header "(nobody)"))
7077             (goto-char p)
7078             (if (search-forward "\nsubject: " nil t)
7079                 (header-set-subject header (gnus-header-value))
7080               (header-set-subject header "(none)"))
7081             (goto-char p)
7082             (and (search-forward "\nxref: " nil t)
7083                  (header-set-xref header (gnus-header-value)))
7084             (goto-char p)
7085             (or (numberp (and (search-forward "\nlines: " nil t)
7086                               (header-set-lines header (read cur))))
7087                 (header-set-lines header 0))
7088             (goto-char p)
7089             (and (search-forward "\ndate: " nil t)
7090                  (header-set-date header (gnus-header-value)))
7091             (goto-char p)
7092             (if (search-forward "\nmessage-id: " nil t)
7093                 (header-set-id header (setq id (gnus-header-value)))
7094               ;; If there was no message-id, we just fake one to make
7095               ;; subsequent routines simpler.
7096               (header-set-id 
7097                header 
7098                (setq id (concat "none+" 
7099                                 (int-to-string 
7100                                  (setq gnus-newsgroup-none-id 
7101                                        (1+ gnus-newsgroup-none-id)))))))
7102             (goto-char p)
7103             (if (search-forward "\nreferences: " nil t)
7104                 (progn
7105                   (header-set-references header (gnus-header-value))
7106                   (setq end (match-end 0))
7107                   (save-excursion
7108                     (setq ref 
7109                           (downcase
7110                            (buffer-substring
7111                             (progn 
7112                               (end-of-line)
7113                               (search-backward ">" end t)
7114                               (1+ (point)))
7115                             (progn
7116                               (search-backward "<" end t)
7117                               (point)))))))
7118               ;; Get the references from the in-reply-to header if there
7119               ;; ware no references and the in-reply-to header looks
7120               ;; promising. 
7121               (if (and (search-forward "\nin-reply-to: " nil t)
7122                        (setq in-reply-to (gnus-header-value))
7123                        (string-match "<[^>]+>" in-reply-to))
7124                   (progn
7125                     (header-set-references 
7126                      header 
7127                      (setq ref (substring in-reply-to (match-beginning 0)
7128                                           (match-end 0))))
7129                     (setq ref (downcase ref)))
7130                 (setq ref "none")))
7131             ;; We do some threading while we read the headers. The
7132             ;; message-id and the last reference are both entered into
7133             ;; the same hash table. Some tippy-toeing around has to be
7134             ;; done in case an article has arrived before the article
7135             ;; which it refers to.
7136             (if (boundp (setq dep (intern (downcase id) dependencies)))
7137                 (if (car (symbol-value dep))
7138                     ;; An article with this Message-ID has already
7139                     ;; been seen, so we ignore this one, except we add
7140                     ;; any additional Xrefs (in case the two articles
7141                     ;; came from different servers.
7142                     (progn
7143                       (header-set-xref 
7144                        (car (symbol-value dep))
7145                        (concat (or (header-xref (car (symbol-value dep))) "")
7146                                (or (header-xref header) "")))
7147                       (setq header nil))
7148                   (setcar (symbol-value dep) header))
7149               (set dep (list header)))
7150             (if header
7151                 (progn
7152                   (if (boundp (setq dep (intern ref dependencies)))
7153                       (setcdr (symbol-value dep) 
7154                               (cons header (cdr (symbol-value dep))))
7155                     (set dep (list nil header)))
7156                   (setq headers (cons header headers))))
7157             (goto-char (point-max))))))
7158     (nreverse headers)))
7159
7160 ;; The following macros and functions were written by Felix Lee
7161 ;; <flee@cse.psu.edu>. 
7162
7163 (defmacro gnus-nov-read-integer ()
7164   '(prog1
7165        (if (= (following-char) ?\t)
7166            0
7167          (let ((num (condition-case nil (read buffer) (error nil))))
7168            (if (numberp num) num 0)))
7169      (or (eobp) (forward-char 1))))
7170
7171 (defmacro gnus-nov-skip-field ()
7172   '(search-forward "\t" eol 'move))
7173
7174 (defmacro gnus-nov-field ()
7175   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7176
7177 ;; Goes through the xover lines and returns a list of vectors
7178 (defun gnus-get-newsgroup-headers-xover (sequence)
7179   "Parse the news overview data in the server buffer, and return a
7180 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7181   ;; Get the Xref when the users reads the articles since most/some
7182   ;; NNTP servers do not include Xrefs when using XOVER.
7183   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7184   (let ((cur nntp-server-buffer)
7185         (dependencies gnus-newsgroup-dependencies)
7186         number headers header)
7187     (save-excursion
7188       (set-buffer nntp-server-buffer)
7189       (goto-char (point-min))
7190       (while (and sequence (not (eobp)))
7191         (setq number (read cur))
7192         (while (and sequence (< (car sequence) number))
7193           (setq sequence (cdr sequence)))
7194         (and sequence 
7195              (eq number (car sequence))
7196              (progn
7197                (setq sequence (cdr sequence))
7198                (if (setq header 
7199                          (inline (gnus-nov-parse-line number dependencies)))
7200                    (setq headers (cons header headers)))))
7201         (forward-line 1))
7202       (setq headers (nreverse headers)))
7203     headers))
7204
7205 ;; This function has to be called with point after the article number
7206 ;; on the beginning of the line.
7207 (defun gnus-nov-parse-line (number dependencies)
7208   (let ((none 0)
7209         (eol (gnus-point-at-eol)) 
7210         (buffer (current-buffer))
7211         header ref id dep)
7212
7213     ;; overview: [num subject from date id refs chars lines misc]
7214     (narrow-to-region (point) eol)
7215     (forward-char)
7216
7217     (condition-case nil
7218         (setq header
7219               (vector 
7220                number                   ; number
7221                (gnus-nov-field)         ; subject
7222                (gnus-nov-field)         ; from
7223                (gnus-nov-field)         ; date
7224                (setq id (or (gnus-nov-field)
7225                             (concat "none+"
7226                                     (int-to-string 
7227                                      (setq none (1+ none)))))) ; id
7228                (progn
7229                  (save-excursion
7230                    (let ((beg (point)))
7231                      (search-forward "\t" eol)
7232                      (if (search-backward ">" beg t)
7233                          (setq ref 
7234                                (downcase 
7235                                 (buffer-substring 
7236                                  (1+ (point))
7237                                  (progn
7238                                    (search-backward "<" beg t)
7239                                    (point)))))
7240                        (setq ref nil))))
7241                  (gnus-nov-field))      ; refs
7242                (gnus-nov-read-integer)  ; chars
7243                (gnus-nov-read-integer)  ; lines
7244                (if (= (following-char) ?\n)
7245                    nil
7246                  (gnus-nov-field))      ; misc
7247                ))
7248       (error (progn 
7249                (ding)
7250                (message "Strange nov line.")
7251                (setq header nil)
7252                (goto-char eol))))
7253
7254     (widen)
7255
7256     ;; We build the thread tree.
7257     (and header
7258          (if (boundp (setq dep (intern (downcase id) dependencies)))
7259              (if (car (symbol-value dep))
7260                  ;; An article with this Message-ID has already been seen,
7261                  ;; so we ignore this one, except we add any additional
7262                  ;; Xrefs (in case the two articles came from different
7263                  ;; servers.
7264                  (progn
7265                    (header-set-xref 
7266                     (car (symbol-value dep))
7267                     (concat (or (header-xref (car (symbol-value dep))) "")
7268                             (or (header-xref header) "")))
7269                    (setq header nil))
7270                (setcar (symbol-value dep) header))
7271            (set dep (list header))))
7272     (if header
7273         (progn
7274           (if (boundp (setq dep (intern (or ref "none") 
7275                                         dependencies)))
7276               (setcdr (symbol-value dep) 
7277                       (cons header (cdr (symbol-value dep))))
7278             (set dep (list nil header)))))
7279     header))
7280
7281 (defun gnus-article-get-xrefs ()
7282   "Fill in the Xref value in `gnus-current-headers', if necessary.
7283 This is meant to be called in `gnus-article-internal-prepare-hook'."
7284   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7285                                  gnus-current-headers)))
7286     (or (not gnus-use-cross-reference)
7287         (not headers)
7288         (and (header-xref headers)
7289              (not (string= (header-xref headers) "")))
7290         (let ((case-fold-search t)
7291               xref)
7292           (save-restriction
7293             (gnus-narrow-to-headers)
7294             (goto-char (point-min))
7295             (if (or (and (eq (downcase (following-char)) ?x)
7296                          (looking-at "Xref:"))
7297                     (search-forward "\nXref:" nil t))
7298                 (progn
7299                   (goto-char (1+ (match-end 0)))
7300                   (setq xref (buffer-substring (point) 
7301                                                (progn (end-of-line) (point))))
7302                   (header-set-xref headers xref))))))))
7303
7304 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7305 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7306
7307 (defun gnus-make-headers-hashtable-by-number ()
7308   "Make hashtable for the variable gnus-newsgroup-headers by number."
7309   (save-excursion
7310     (set-buffer gnus-summary-buffer)
7311     (let ((headers gnus-newsgroup-headers)
7312           header)
7313       (setq gnus-newsgroup-headers-hashtb-by-number
7314             (gnus-make-hashtable (length headers)))
7315       (while headers
7316         (setq header (car headers))
7317         (gnus-sethash (int-to-string (header-number header))
7318                       header gnus-newsgroup-headers-hashtb-by-number)
7319         (setq headers (cdr headers))))))
7320
7321 (defun gnus-more-header-backward ()
7322   "Find new header backward."
7323   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7324         (artnum gnus-newsgroup-begin)
7325         (header nil))
7326     (while (and (not header)
7327                 (> artnum first))
7328       (setq artnum (1- artnum))
7329       (setq header (gnus-read-header artnum)))
7330     header))
7331
7332 (defun gnus-more-header-forward (&optional backward)
7333   "Find new header forward.
7334 If BACKWARD, find new header backward instead."
7335   (if backward
7336       (gnus-more-header-backward)
7337     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7338           (artnum gnus-newsgroup-end)
7339           (header nil))
7340       (while (and (not header)
7341                   (< artnum last))
7342         (setq artnum (1+ artnum))
7343         (setq header (gnus-read-header artnum)))
7344       header)))
7345
7346 (defun gnus-extend-newsgroup (header &optional backward)
7347   "Extend newsgroup selection with HEADER.
7348 Optional argument BACKWARD means extend toward backward."
7349   (if header
7350       (let ((artnum (header-number header)))
7351         (setq gnus-newsgroup-headers
7352               (if backward
7353                   (cons header gnus-newsgroup-headers)
7354                 (nconc gnus-newsgroup-headers (list header))))
7355         (setq gnus-newsgroup-unselected
7356               (delq artnum gnus-newsgroup-unselected))
7357         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7358         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7359
7360 (defun gnus-summary-work-articles (n)
7361   "Return a list of articles to be worked upon. The prefix argument,
7362 the list of process marked articles, and the current article will be
7363 taken into consideration."
7364   (let (articles)
7365     (if (and n (numberp n))
7366         (let ((backward (< n 0))
7367               (n (abs n)))
7368           (save-excursion
7369             (while (and (> n 0)
7370                         (setq articles (cons (gnus-summary-article-number) 
7371                                              articles))
7372                         (gnus-summary-search-forward nil nil backward))
7373               (setq n (1- n))))
7374           (sort articles (function <)))
7375       (or (reverse gnus-newsgroup-processable)
7376           (list (gnus-summary-article-number))))))
7377
7378 (defun gnus-summary-search-group (&optional backward use-level)
7379   "Search for next unread newsgroup.
7380 If optional argument BACKWARD is non-nil, search backward instead."
7381   (save-excursion
7382     (set-buffer gnus-group-buffer)
7383     (if (gnus-group-search-forward 
7384          backward nil (if use-level (gnus-group-group-level) nil))
7385         (gnus-group-group-name))))
7386
7387 (defun gnus-summary-best-group (&optional exclude-group)
7388   "Find the name of the best unread group.
7389 If EXCLUDE-GROUP, do not go to this group."
7390   (save-excursion
7391     (set-buffer gnus-group-buffer)
7392     (save-excursion
7393       (gnus-group-best-unread-group exclude-group))))
7394
7395 (defun gnus-subject-equal (s1 s2)
7396   (cond
7397    ((null gnus-summary-gather-subject-limit)
7398     (equal (gnus-simplify-subject-re s1)
7399            (gnus-simplify-subject-re s2)))
7400    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7401     (equal (gnus-simplify-subject-fuzzy s1)
7402            (gnus-simplify-subject-fuzzy s2)))
7403    ((numberp gnus-summary-gather-subject-limit)
7404     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7405            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7406    (t
7407     (equal s1 s2))))
7408     
7409 (defun gnus-summary-search-subject (&optional backward unread subject)
7410   "Search for article forward.
7411 If BACKWARD is non-nil, search backward.
7412 If UNREAD is non-nil, only unread articles are selected.
7413 If SUBJECT is non-nil, the article which has the same subject will be
7414 searched for." 
7415   (let ((func (if backward 'previous-single-property-change
7416                 'next-single-property-change))
7417         (beg (point))
7418         (did t)
7419         pos psubject)
7420     (beginning-of-line)
7421     (and gnus-summary-check-current unread
7422          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7423          (setq did nil))
7424     (if (not did)
7425         ()
7426       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7427       (while
7428           (and 
7429            (setq pos (funcall func (point) 'gnus-number))
7430            (goto-char (if backward (1- pos) pos))
7431            (setq did
7432                  (not (and
7433                        (or (not unread)
7434                            (eq (get-text-property (point) 'gnus-mark)
7435                                gnus-unread-mark))
7436                        (or (not subject)
7437                            (and (setq psubject (inline (gnus-summary-subject-string)))
7438                                 (inline (gnus-subject-equal subject psubject)))))))
7439            (if backward (if (bobp) nil (forward-char -1) t)
7440              (if (eobp) nil (forward-char 1) t)))))
7441     (if did
7442         (progn (goto-char beg) nil)
7443       (prog1
7444           (get-text-property (point) 'gnus-number)
7445         (gnus-summary-position-cursor)))))
7446
7447 (defun gnus-summary-pseudo-article ()
7448   "The thread level of the article on the current line."
7449   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7450
7451 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7452 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7453 (defun gnus-summary-article-score ()
7454   "Return current article score."
7455   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7456       gnus-summary-default-score 0))
7457
7458 (defun gnus-summary-recenter ()
7459   "Center point in the summary window.
7460 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7461 displayed, no centering will be performed." 
7462   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7463   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7464   (let* ((top (cond ((< (window-height) 4) 0)
7465                     ((< (window-height) 7) 1)
7466                     (t 2)))
7467          (height (1- (window-height)))
7468          (bottom (save-excursion (goto-char (point-max))
7469                                  (forward-line (- height))
7470                                  (point)))
7471          (window (get-buffer-window (current-buffer))))
7472     (and 
7473      ;; The user has to want it,
7474      gnus-auto-center-summary 
7475      ;; the article buffer must be displayed,
7476      (get-buffer-window gnus-article-buffer)
7477      ;; Set the window start to either `bottom', which is the biggest
7478      ;; possible valid number, or the second line from the top,
7479      ;; whichever is the least.
7480      (set-window-start
7481       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7482
7483 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7484 (defun gnus-short-group-name (group &optional levels)
7485   "Collapse GROUP name LEVELS."
7486   (let* ((name "") (foreign "") (depth -1) (skip 1)
7487          (levels (or levels
7488                      (progn
7489                        (while (string-match "\\." group skip)
7490                          (setq skip (match-end 0)
7491                                depth (+ depth 1)))
7492                        depth))))
7493     (if (string-match ":" group)
7494         (setq foreign (substring group 0 (match-end 0))
7495               group (substring group (match-end 0))))
7496     (while group
7497       (if (and (string-match "\\." group) (> levels 0))
7498           (setq name (concat name (substring group 0 1))
7499                 group (substring group (match-end 0))
7500                 levels (- levels 1)
7501                 name (concat name "."))
7502         (setq name (concat foreign name group)
7503               group nil)))
7504     name))
7505
7506 (defun gnus-summary-jump-to-group (newsgroup)
7507   "Move point to NEWSGROUP in group mode buffer."
7508   ;; Keep update point of group mode buffer if visible.
7509   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7510       (save-window-excursion
7511         ;; Take care of tree window mode.
7512         (if (get-buffer-window gnus-group-buffer)
7513             (pop-to-buffer gnus-group-buffer))
7514         (gnus-group-jump-to-group newsgroup))
7515     (save-excursion
7516       ;; Take care of tree window mode.
7517       (if (get-buffer-window gnus-group-buffer)
7518           (pop-to-buffer gnus-group-buffer)
7519         (set-buffer gnus-group-buffer))
7520       (gnus-group-jump-to-group newsgroup))))
7521
7522 ;; This function returns a list of article numbers based on the
7523 ;; difference between the ranges of read articles in this group and
7524 ;; the range of active articles.
7525 (defun gnus-list-of-unread-articles (group)
7526   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7527          (active (gnus-gethash group gnus-active-hashtb))
7528          (last (cdr active))
7529          first nlast unread)
7530     ;; If none are read, then all are unread. 
7531     (if (not read)
7532         (setq first (car active))
7533       ;; If the range of read articles is a single range, then the
7534       ;; first unread article is the article after the last read
7535       ;; article. Sounds logical, doesn't it?
7536       (if (not (listp (cdr read)))
7537           (setq first (1+ (cdr read)))
7538         ;; `read' is a list of ranges.
7539         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7540                                 (car (car read)))) 1)
7541             (setq first 1))
7542         (while read
7543           (if first 
7544               (while (< first nlast)
7545                 (setq unread (cons first unread))
7546                 (setq first (1+ first))))
7547           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7548           (setq nlast (if (atom (car (cdr read))) 
7549                           (car (cdr read))
7550                         (car (car (cdr read)))))
7551           (setq read (cdr read)))))
7552     ;; And add the last unread articles.
7553     (while (<= first last)
7554       (setq unread (cons first unread))
7555       (setq first (1+ first)))
7556     ;; Return the list of unread articles.
7557     (nreverse unread)))
7558
7559 (defun gnus-list-of-read-articles (group)
7560   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7561         (active (gnus-gethash group gnus-active-hashtb)))
7562     (and info active
7563          (gnus-sorted-complement 
7564           (gnus-uncompress-range active) 
7565           (gnus-list-of-unread-articles group)))))
7566
7567 ;; Various summary commands
7568
7569 (defun gnus-summary-universal-argument ()
7570   "Perform any operation on all articles marked with the process mark."
7571   (interactive)
7572   (gnus-set-global-variables)
7573   (let ((articles (reverse gnus-newsgroup-processable))
7574         func)
7575     (or articles (error "No articles marked"))
7576     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7577         (error "Undefined key"))
7578     (while articles
7579       (gnus-summary-goto-subject (car articles))
7580       (command-execute func)
7581       (gnus-summary-remove-process-mark (car articles))
7582       (setq articles (cdr articles)))))
7583
7584 (defun gnus-summary-toggle-truncation (&optional arg)
7585   "Toggle truncation of summary lines.
7586 With arg, turn line truncation on iff arg is positive."
7587   (interactive "P")
7588   (setq truncate-lines
7589         (if (null arg) (not truncate-lines)
7590           (> (prefix-numeric-value arg) 0)))
7591   (redraw-display))
7592
7593 (defun gnus-summary-reselect-current-group (&optional all)
7594   "Once exit and then reselect the current newsgroup.
7595 The prefix argument ALL means to select all articles."
7596   (interactive "P")
7597   (gnus-set-global-variables)
7598   (let ((current-subject (gnus-summary-article-number))
7599         (group gnus-newsgroup-name))
7600     (setq gnus-newsgroup-begin nil)
7601     (gnus-summary-exit t)
7602     ;; We have to adjust the point of group mode buffer because the
7603     ;; current point was moved to the next unread newsgroup by
7604     ;; exiting.
7605     (gnus-summary-jump-to-group group)
7606     (gnus-group-read-group all t)
7607     (gnus-summary-goto-subject current-subject)))
7608
7609 (defun gnus-summary-rescan-group (&optional all)
7610   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7611   (interactive "P")
7612   (gnus-set-global-variables)
7613   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7614   (let ((group gnus-newsgroup-name))
7615     (gnus-summary-exit)
7616     (gnus-summary-jump-to-group group)
7617     (save-excursion
7618       (set-buffer gnus-group-buffer)
7619       (gnus-group-get-new-news-this-group 1))
7620     (gnus-summary-jump-to-group group)
7621     (gnus-group-read-group all)))
7622
7623 (defun gnus-summary-update-info ()
7624   (let* ((group gnus-newsgroup-name))
7625     (if gnus-newsgroup-kill-headers
7626         (setq gnus-newsgroup-killed
7627               (gnus-compress-sequence
7628                (nconc
7629                 (gnus-set-sorted-intersection
7630                  (gnus-uncompress-range gnus-newsgroup-killed)
7631                  (setq gnus-newsgroup-unselected
7632                        (sort gnus-newsgroup-unselected '<)))
7633                 (setq gnus-newsgroup-unreads
7634                       (sort gnus-newsgroup-unreads '<))) t)))
7635     (or (listp (cdr gnus-newsgroup-killed))
7636         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7637     (let ((headers gnus-newsgroup-headers))
7638       (gnus-close-group group)
7639       (run-hooks 'gnus-exit-group-hook)
7640       (gnus-update-read-articles 
7641        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7642        gnus-newsgroup-marked
7643        t gnus-newsgroup-replied gnus-newsgroup-expirable
7644        gnus-newsgroup-killed gnus-newsgroup-dormant
7645        gnus-newsgroup-bookmarks 
7646        (and gnus-save-score gnus-newsgroup-scored))
7647       (and gnus-use-cross-reference
7648            (gnus-mark-xrefs-as-read 
7649             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7650       ;; Do adaptive scoring, and possibly save score files.
7651       (and gnus-newsgroup-adaptive
7652            (gnus-score-adaptive))
7653       (and gnus-use-scoring 
7654            (fboundp 'gnus-score-save)
7655            (funcall 'gnus-score-save))
7656       ;; Do not switch windows but change the buffer to work.
7657       (set-buffer gnus-group-buffer)
7658       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7659           (gnus-group-update-group group)))))
7660   
7661 (defun gnus-summary-exit (&optional temporary)
7662   "Exit reading current newsgroup, and then return to group selection mode.
7663 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7664   (interactive)
7665   (gnus-set-global-variables)
7666   (gnus-kill-save-kill-buffer)
7667   (let* ((group gnus-newsgroup-name)
7668          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7669                                                   gnus-newsgroup-name))))
7670          (mode major-mode)
7671          (buf (current-buffer)))
7672     (run-hooks 'gnus-summary-prepare-exit-hook)
7673     (gnus-summary-update-info) ; Make all changes in this group permanent.
7674     (set-buffer buf)
7675     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7676     ;; Make sure where I was, and go to next newsgroup.
7677     (set-buffer gnus-group-buffer)
7678     (or quit-config
7679         (progn
7680           (gnus-group-jump-to-group group)
7681           (gnus-group-next-unread-group 1)))
7682     (if temporary
7683         nil                             ;Nothing to do.
7684       ;; We set all buffer-local variables to nil. It is unclear why
7685       ;; this is needed, but if we don't, buffer-local variables are
7686       ;; not garbage-collected, it seems. This would the lead to en
7687       ;; ever-growing Emacs.
7688       (set-buffer buf)
7689       (gnus-summary-clear-local-variables)
7690       ;; We clear the global counterparts of the buffer-local
7691       ;; variables as well, just to be on the safe side.
7692       (gnus-configure-windows 'group 'force)
7693       (gnus-summary-clear-local-variables)
7694       ;; Return to group mode buffer. 
7695       (if (eq mode 'gnus-summary-mode)
7696           (gnus-kill-buffer buf))
7697       (if (get-buffer gnus-article-buffer)
7698           (bury-buffer gnus-article-buffer))
7699       (setq gnus-current-select-method gnus-select-method)
7700       (pop-to-buffer gnus-group-buffer)
7701       (if (not quit-config)
7702           (progn
7703             (gnus-group-jump-to-group group)
7704             (gnus-group-next-unread-group 1))
7705         (if (not (buffer-name (car quit-config)))
7706             (gnus-configure-windows 'group 'force)
7707           (set-buffer (car quit-config))
7708           (and (eq major-mode 'gnus-summary-mode)
7709                (gnus-set-global-variables))
7710           (gnus-configure-windows (cdr quit-config))))
7711       (run-hooks 'gnus-summary-exit-hook))))
7712
7713 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7714 (defun gnus-summary-exit-no-update (&optional no-questions)
7715   "Quit reading current newsgroup without updating read article info."
7716   (interactive)
7717   (gnus-set-global-variables)
7718   (let* ((group gnus-newsgroup-name)
7719          (quit-config (nth 1 (assoc 'quit-config 
7720                                     (gnus-find-method-for-group group)))))
7721     (if (or no-questions
7722             gnus-expert-user
7723             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7724         (progn
7725           (gnus-close-group group)
7726           (gnus-summary-clear-local-variables)
7727           (set-buffer gnus-group-buffer)
7728           (gnus-summary-clear-local-variables)
7729           ;; Return to group selection mode.
7730           (gnus-configure-windows 'group 'force)
7731           (if (get-buffer gnus-summary-buffer)
7732               (kill-buffer gnus-summary-buffer))
7733           (if (get-buffer gnus-article-buffer)
7734               (bury-buffer gnus-article-buffer))
7735           (if (equal (gnus-group-group-name) group)
7736               (gnus-group-next-unread-group 1))
7737           (if quit-config
7738               (progn
7739                 (if (not (buffer-name (car quit-config)))
7740                     (gnus-configure-windows 'group 'force)
7741                   (set-buffer (car quit-config))
7742                   (and (eq major-mode 'gnus-summary-mode)
7743                        (gnus-set-global-variables))
7744                   (gnus-configure-windows (cdr quit-config)))))))))
7745
7746 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7747 (defun gnus-summary-fetch-faq (group)
7748   "Fetch the FAQ for the current group."
7749   (interactive (list gnus-newsgroup-name))
7750   (let ((gnus-faq-buffer 
7751          (find-file (concat gnus-group-faq-directory 
7752                             (gnus-group-real-name group)))))
7753   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7754
7755 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7756 (defun gnus-summary-describe-group (&optional force)
7757   "Describe the current newsgroup."
7758   (interactive "P")
7759   (gnus-group-describe-group force gnus-newsgroup-name))
7760
7761 (defun gnus-summary-describe-briefly ()
7762   "Describe summary mode commands briefly."
7763   (interactive)
7764   (gnus-message 6
7765     (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")))
7766
7767 ;; Walking around group mode buffer from summary mode.
7768
7769 (defun gnus-summary-next-group (&optional no-article target-group backward)
7770   "Exit current newsgroup and then select next unread newsgroup.
7771 If prefix argument NO-ARTICLE is non-nil, no article is selected
7772 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7773 previous group instead."
7774   (interactive "P")
7775   (gnus-set-global-variables)
7776   (let ((current-group gnus-newsgroup-name)
7777         (current-buffer (current-buffer))
7778         entered)
7779     ;; First we semi-exit this group to update Xrefs and all variables.
7780     ;; We can't do a real exit, because the window conf must remain
7781     ;; the same in case the user is prompted for info, and we don't
7782     ;; want the window conf to change before that...
7783     (gnus-summary-exit t)
7784     (while (not entered)
7785       ;; Then we find what group we are supposed to enter.
7786       (set-buffer gnus-group-buffer)
7787       (gnus-group-jump-to-group current-group)
7788       (setq target-group 
7789             (or target-group        
7790                 (if (eq gnus-keep-same-level 'best) 
7791                     (gnus-summary-best-group gnus-newsgroup-name)
7792                   (gnus-summary-search-group backward gnus-keep-same-level))))
7793       (if (not target-group)
7794           ;; There are no further groups, so we return to the group
7795           ;; buffer.
7796           (progn
7797             (gnus-message 5 "Returning to the group buffer")
7798             (setq entered t)
7799             (set-buffer current-buffer)
7800             (gnus-summary-exit))
7801         ;; We try to enter the target group.
7802         (gnus-group-jump-to-group target-group)
7803         (let ((unreads (gnus-group-group-unread)))
7804           (if (and (or (eq t unreads)
7805                        (and unreads (not (zerop unreads))))
7806                    (gnus-summary-read-group
7807                     target-group nil no-article current-buffer))
7808               (setq entered t)
7809             (setq current-group target-group
7810                   target-group nil)))))))
7811
7812 (defun gnus-summary-next-group-old (&optional no-article group backward)
7813   "Exit current newsgroup and then select next unread newsgroup.
7814 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7815 If BACKWARD, go to previous group instead."
7816   (interactive "P")
7817   (gnus-set-global-variables)
7818   (let ((ingroup gnus-newsgroup-name)
7819         (sumbuf (current-buffer))
7820         num)
7821     (set-buffer gnus-group-buffer)
7822     (if (and group
7823              (or (and (numberp (setq num (car (gnus-gethash
7824                                                group gnus-newsrc-hashtb))))
7825                       (< num 1))
7826                  (null num)))
7827         (progn
7828           (gnus-group-jump-to-group group)
7829           (setq group nil))
7830       (gnus-group-jump-to-group ingroup))
7831     (gnus-summary-search-group backward)
7832     (let ((group (or group (gnus-summary-search-group backward))))
7833       (set-buffer sumbuf)
7834       (gnus-summary-exit t)             ;Update all information.
7835       (if (null group)
7836           (gnus-summary-exit-no-update t)
7837         (gnus-group-jump-to-group ingroup)
7838         (setq group (gnus-summary-search-group backward))
7839         (gnus-message 5 "Selecting %s..." group)
7840         (set-buffer gnus-group-buffer)
7841         ;; We are now in group mode buffer.
7842         ;; Make sure group mode buffer point is on GROUP.
7843         (gnus-group-jump-to-group group)
7844         (if (not (eq gnus-auto-select-next 'quietly))
7845             (progn
7846               (gnus-summary-read-group group nil no-article sumbuf)
7847               (and (string= gnus-newsgroup-name ingroup)
7848                    (bufferp sumbuf) (buffer-name sumbuf)
7849                    (progn
7850                      (set-buffer (setq gnus-summary-buffer sumbuf))
7851                      (gnus-summary-exit-no-update t))))
7852           (let ((prevgroup group))
7853             (gnus-group-jump-to-group ingroup)
7854             (setq group (gnus-summary-search-group backward))
7855             (gnus-summary-read-group group nil no-article sumbuf)
7856             (while (and (string= gnus-newsgroup-name ingroup)
7857                         (bufferp sumbuf) 
7858                         (buffer-name sumbuf)
7859                         (not (string= prevgroup (gnus-group-group-name))))
7860               (set-buffer gnus-group-buffer)
7861               (gnus-summary-read-group 
7862                (setq prevgroup (gnus-group-group-name)) 
7863                nil no-article sumbuf))
7864             (and (string= prevgroup (gnus-group-group-name))
7865                  ;; We have reached the final group in the group
7866                  ;; buffer.
7867                  (progn
7868                    (if (buffer-name sumbuf)
7869                        (progn
7870                          (set-buffer sumbuf)
7871                          (gnus-summary-exit)))))))))))
7872
7873 (defun gnus-summary-prev-group (&optional no-article)
7874   "Exit current newsgroup and then select previous unread newsgroup.
7875 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7876   (interactive "P")
7877   (gnus-summary-next-group no-article nil t))
7878
7879 ;; Walking around summary lines.
7880
7881 (defun gnus-summary-first-subject (&optional unread)
7882   "Go to the first unread subject.
7883 If UNREAD is non-nil, go to the first unread article.
7884 Returns nil if there are no unread articles."
7885   (interactive "P")
7886   (prog1
7887       (cond ((not unread)
7888              (goto-char (point-min)))
7889             ((gnus-goto-char 
7890               (text-property-any 
7891                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7892              t)
7893             (t 
7894              ;; There are no unread articles.
7895              (gnus-message 3 "No more unread articles")
7896              nil))
7897     (gnus-summary-position-cursor)))
7898
7899 (defun gnus-summary-next-subject (n &optional unread dont-display)
7900   "Go to next N'th summary line.
7901 If N is negative, go to the previous N'th subject line.
7902 If UNREAD is non-nil, only unread articles are selected.
7903 The difference between N and the actual number of steps taken is
7904 returned."
7905   (interactive "p")
7906   (let ((backward (< n 0))
7907         (n (abs n)))
7908     (while (and (> n 0)
7909                 (gnus-summary-search-forward unread nil backward))
7910       (setq n (1- n)))
7911     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7912                                (if unread " unread" "")))
7913     (or dont-display
7914         (progn
7915           (gnus-summary-recenter)
7916           (gnus-summary-position-cursor)))
7917   n))
7918
7919 (defun gnus-summary-next-unread-subject (n)
7920   "Go to next N'th unread summary line."
7921   (interactive "p")
7922   (gnus-summary-next-subject n t))
7923
7924 (defun gnus-summary-prev-subject (n &optional unread)
7925   "Go to previous N'th summary line.
7926 If optional argument UNREAD is non-nil, only unread article is selected."
7927   (interactive "p")
7928   (gnus-summary-next-subject (- n) unread))
7929
7930 (defun gnus-summary-prev-unread-subject (n)
7931   "Go to previous N'th unread summary line."
7932   (interactive "p")
7933   (gnus-summary-next-subject (- n) t))
7934
7935 (defun gnus-summary-goto-subject (article)
7936   "Go the subject line of ARTICLE."
7937   (interactive
7938    (list
7939     (string-to-int
7940      (completing-read "Article number: "
7941                       (mapcar
7942                        (lambda (headers)
7943                          (list
7944                           (int-to-string (header-number headers))))
7945                        gnus-newsgroup-headers)
7946                       nil 'require-match))))
7947   (or article (error "No article number"))
7948   (let ((b (point)))
7949     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7950                                                 'gnus-number article)))
7951         ()
7952       (gnus-summary-show-thread)
7953       ;; Skip dummy articles. 
7954       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7955           (forward-line 1))
7956       (prog1
7957           (if (not (eobp))
7958               article
7959             (goto-char b)
7960             nil)
7961         (gnus-summary-position-cursor)))))
7962
7963 ;; Walking around summary lines with displaying articles.
7964
7965 (defun gnus-summary-expand-window (&optional arg)
7966   "Make the summary buffer take up the entire Emacs frame.
7967 Given a prefix, will force an `article' buffer configuration."
7968   (interactive "P")
7969   (gnus-set-global-variables)
7970   (if arg
7971       (gnus-configure-windows 'article 'force)
7972     (gnus-configure-windows 'summary 'force)))
7973
7974 (defun gnus-summary-display-article (article &optional all-header)
7975   "Display ARTICLE in article buffer."
7976   (gnus-set-global-variables)
7977   (if (null article)
7978       nil
7979     (prog1
7980         (gnus-article-prepare article all-header)
7981       (gnus-summary-show-thread)
7982       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7983           (progn
7984             (forward-line 1)
7985             (gnus-summary-position-cursor)))
7986       (run-hooks 'gnus-select-article-hook)
7987       (gnus-summary-recenter)
7988       (gnus-summary-goto-subject article)
7989       ;; Successfully display article.
7990       (gnus-summary-update-line)
7991       (gnus-article-set-window-start 
7992        (cdr (assq article gnus-newsgroup-bookmarks)))
7993       t)))
7994
7995 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7996   "Select the current article.
7997 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7998 non-nil, the article will be re-fetched even if it already present in
7999 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
8000 be displayed."
8001   (and (not pseudo) (gnus-summary-pseudo-article)
8002        (error "This is a pseudo-article."))
8003   (let ((article (or article (gnus-summary-article-number)))
8004         (all-headers (not (not all-headers))) ;Must be T or NIL.
8005         did) 
8006     (prog1
8007         (save-excursion
8008           (set-buffer gnus-summary-buffer)
8009           (if (or (null gnus-current-article)
8010                   (null gnus-article-current)
8011                   (null (get-buffer gnus-article-buffer))
8012                   (not (eq article (cdr gnus-article-current)))
8013                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
8014                   force)
8015               ;; The requested article is different from the current article.
8016               (progn
8017                 (gnus-summary-display-article article all-headers)
8018                 (setq did article))
8019             (if (or all-headers gnus-show-all-headers) 
8020                 (gnus-article-show-all-headers))
8021             nil))
8022       (if did 
8023           (gnus-article-set-window-start 
8024            (cdr (assq article gnus-newsgroup-bookmarks)))))))
8025
8026 (defun gnus-summary-set-current-mark (&optional current-mark)
8027   "Obsolete function."
8028   nil)
8029
8030 (defun gnus-summary-next-article (&optional unread subject backward)
8031   "Select the next article.
8032 If UNREAD, only unread articles are selected.
8033 If SUBJECT, only articles with SUBJECT are selected.
8034 If BACKWARD, the previous article is selected instead of the next."
8035   (interactive "P")
8036   (gnus-set-global-variables)
8037   (let (header)
8038     (cond
8039      ;; Is there such an article?
8040      ((or (gnus-summary-display-article 
8041            (gnus-summary-search-forward unread subject backward))
8042           (eq (gnus-summary-article-mark) gnus-canceled-mark))
8043       (gnus-summary-position-cursor))
8044      ;; If not, we try the first unread, if that is wanted.
8045      ((and subject
8046            gnus-auto-select-same
8047            (gnus-summary-first-unread-article))
8048       (gnus-message 6 "Wrapped"))
8049      ;; Try to get next/previous article not displayed in this group.
8050      ((and gnus-auto-extend-newsgroup
8051            (not unread) (not subject)
8052            (setq header (gnus-more-header-forward backward)))
8053       (gnus-extend-newsgroup header backward)
8054       (let ((buffer-read-only nil))
8055         (goto-char (if backward (point-min) (point-max)))
8056         (gnus-summary-prepare-threads (list header)))
8057       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8058                                    gnus-newsgroup-end)))
8059      ;; Go to next/previous group.
8060      (t
8061       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8062           (gnus-summary-jump-to-group gnus-newsgroup-name))
8063       (let ((cmd (aref (this-command-keys) 0))
8064             (group 
8065              (if (eq gnus-keep-same-level 'best) 
8066                  (gnus-summary-best-group gnus-newsgroup-name)
8067                (gnus-summary-search-group backward gnus-keep-same-level))))
8068         ;; For some reason, the group window gets selected. We change
8069         ;; it back.  
8070         (select-window (get-buffer-window (current-buffer)))
8071         ;; Keep just the event type of CMD.
8072         (and (listp cmd) (setq cmd (car cmd)))
8073         ;; Select next unread newsgroup automagically.
8074         (cond 
8075          ((not gnus-auto-select-next)
8076           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8077          ((eq gnus-auto-select-next 'quietly)
8078           ;; Select quietly.
8079           (if (assoc 'quit-config (gnus-find-method-for-group 
8080                                    gnus-newsgroup-name))
8081               (gnus-summary-exit)
8082             (gnus-message 7 "No more%s articles (%s)..."
8083                           (if unread " unread" "") 
8084                           (if group (concat "selecting " group)
8085                             "exiting"))
8086             (gnus-summary-next-group nil group backward)))
8087          (t
8088           (let ((keystrokes '(?\C-n ?\C-p))
8089                 key)
8090             (while (or (null key) (memq key keystrokes))
8091               (gnus-message 
8092                7 "No more%s articles%s" (if unread " unread" "")
8093                (if (and group (not (assoc 'quit-config
8094                                           (gnus-find-method-for-group 
8095                                            gnus-newsgroup-name))))
8096                    (format " (Type %s for %s [%s])"
8097                            (single-key-description cmd) group
8098                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8099                  (format " (Type %s to exit %s)"
8100                          (single-key-description cmd)
8101                          gnus-newsgroup-name)))
8102               ;; Confirm auto selection.
8103               (let* ((event (read-event)))
8104                 (setq key (if (listp event) (car event) event))
8105                 (if (memq key keystrokes)
8106                     (let ((obuf (current-buffer)))
8107                       (switch-to-buffer gnus-group-buffer)
8108                       (and group
8109                            (gnus-group-jump-to-group group))
8110                       (condition-case ()
8111                           (execute-kbd-macro (char-to-string key))
8112                         (error (ding) nil))
8113                       (setq group (gnus-group-group-name))
8114                       (switch-to-buffer obuf)))))
8115             (if (equal key cmd)
8116                 (if (or (not group) (assoc 'quit-config
8117                                            (gnus-find-method-for-group
8118                                             gnus-newsgroup-name)))
8119                     (gnus-summary-exit)
8120                   (gnus-summary-next-group nil group backward))
8121               (setq unread-command-events (list key)))))))))))
8122
8123 (defun gnus-summary-next-unread-article ()
8124   "Select unread article after current one."
8125   (interactive)
8126   (gnus-summary-next-article t (and gnus-auto-select-same
8127                                     (gnus-summary-subject-string))))
8128
8129 (defun gnus-summary-prev-article (&optional unread subject)
8130   "Select the article after the current one.
8131 If UNREAD is non-nil, only unread articles are selected."
8132   (interactive "P")
8133   (gnus-summary-next-article unread subject t))
8134
8135 (defun gnus-summary-prev-unread-article ()
8136   "Select unred article before current one."
8137   (interactive)
8138   (gnus-summary-prev-article t (and gnus-auto-select-same
8139                                     (gnus-summary-subject-string))))
8140
8141 (defun gnus-summary-next-page (&optional lines circular)
8142   "Show next page of selected article.
8143 If end of article, select next article.
8144 Argument LINES specifies lines to be scrolled up.
8145 If CIRCULAR is non-nil, go to the start of the article instead of 
8146 instead of selecting the next article when reaching the end of the
8147 current article." 
8148   (interactive "P")
8149   (setq gnus-summary-buffer (current-buffer))
8150   (gnus-set-global-variables)
8151   (let ((article (gnus-summary-article-number))
8152         (endp nil))
8153     (gnus-configure-windows 'article)
8154     (if (or (null gnus-current-article)
8155             (null gnus-article-current)
8156             (/= article (cdr gnus-article-current))
8157             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8158         ;; Selected subject is different from current article's.
8159         (gnus-summary-display-article article)
8160       (gnus-eval-in-buffer-window
8161        gnus-article-buffer
8162        (setq endp (gnus-article-next-page lines)))
8163       (if endp
8164           (cond (circular
8165                  (gnus-summary-beginning-of-article))
8166                 (lines
8167                  (gnus-message 3 "End of message"))
8168                 ((null lines)
8169                  (gnus-summary-next-unread-article)))))
8170     (gnus-summary-recenter)
8171     (gnus-summary-position-cursor)))
8172
8173 (defun gnus-summary-prev-page (&optional lines)
8174   "Show previous page of selected article.
8175 Argument LINES specifies lines to be scrolled down."
8176   (interactive "P")
8177   (gnus-set-global-variables)
8178   (let ((article (gnus-summary-article-number)))
8179     (gnus-configure-windows 'article)
8180     (if (or (null gnus-current-article)
8181             (null gnus-article-current)
8182             (/= article (cdr gnus-article-current))
8183             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8184         ;; Selected subject is different from current article's.
8185         (gnus-summary-display-article article)
8186       (gnus-summary-recenter)
8187       (gnus-eval-in-buffer-window gnus-article-buffer
8188         (gnus-article-prev-page lines))))
8189   (gnus-summary-position-cursor))
8190
8191 (defun gnus-summary-scroll-up (lines)
8192   "Scroll up (or down) one line current article.
8193 Argument LINES specifies lines to be scrolled up (or down if negative)."
8194   (interactive "p")
8195   (gnus-set-global-variables)
8196   (gnus-configure-windows 'article)
8197   (or (gnus-summary-select-article nil nil 'pseudo)
8198       (gnus-eval-in-buffer-window 
8199        gnus-article-buffer
8200        (cond ((> lines 0)
8201               (if (gnus-article-next-page lines)
8202                   (gnus-message 3 "End of message")))
8203              ((< lines 0)
8204               (gnus-article-prev-page (- lines))))))
8205   (gnus-summary-recenter)
8206   (gnus-summary-position-cursor))
8207
8208 (defun gnus-summary-next-same-subject ()
8209   "Select next article which has the same subject as current one."
8210   (interactive)
8211   (gnus-set-global-variables)
8212   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8213
8214 (defun gnus-summary-prev-same-subject ()
8215   "Select previous article which has the same subject as current one."
8216   (interactive)
8217   (gnus-set-global-variables)
8218   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8219
8220 (defun gnus-summary-next-unread-same-subject ()
8221   "Select next unread article which has the same subject as current one."
8222   (interactive)
8223   (gnus-set-global-variables)
8224   (gnus-summary-next-article t (gnus-summary-subject-string)))
8225
8226 (defun gnus-summary-prev-unread-same-subject ()
8227   "Select previous unread article which has the same subject as current one."
8228   (interactive)
8229   (gnus-set-global-variables)
8230   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8231
8232 (defun gnus-summary-first-unread-article ()
8233   "Select the first unread article. 
8234 Return nil if there are no unread articles."
8235   (interactive)
8236   (gnus-set-global-variables)
8237   (prog1
8238       (if (gnus-summary-first-subject t)
8239           (progn
8240             (gnus-summary-show-thread)
8241             (gnus-summary-first-subject t)
8242             (gnus-summary-display-article (gnus-summary-article-number))))
8243     (gnus-summary-position-cursor)))
8244
8245 (defun gnus-summary-best-unread-article ()
8246   "Select the unread article with the highest score."
8247   (interactive)
8248   (gnus-set-global-variables)
8249   (let ((scored gnus-newsgroup-scored)
8250         (best -1000000)
8251         article art)
8252     (while scored
8253       (or (> best (cdr (car scored)))
8254           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8255                (not (memq art gnus-newsgroup-marked))
8256                (not (memq art gnus-newsgroup-dormant))
8257                (if (= best (cdr (car scored)))
8258                    (setq article (min art article))
8259                  (setq article art)
8260                  (setq best (cdr (car scored))))))
8261       (setq scored (cdr scored)))
8262     (cond
8263      ((or (not article) (null gnus-newsgroup-unreads))
8264       ;; We didn't find any scored articles, so we just jump to the
8265       ;; first article. 
8266       (gnus-summary-first-unread-article))
8267      ((> best gnus-summary-default-score)
8268       ;; We found one, and it's bigger than the default score, so we
8269       ;; select it.
8270       (gnus-summary-goto-article article))
8271      (t
8272       ;; We found an article, but it has a score lower than the
8273       ;; defaults, so we try to find an article with the default
8274       ;; score. 
8275       (goto-char (point-min))
8276       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8277                       (not (eq (cdr (assq (gnus-summary-article-number)
8278                                           gnus-newsgroup-scored))
8279                                gnus-summary-default-score)))
8280                   (zerop (forward-line 1))
8281                   (not (eobp))))
8282       (if (= (gnus-summary-article-mark) gnus-unread-mark)
8283           ;; We jump to the article we have finally found.
8284           (gnus-summary-goto-article (gnus-summary-article-number))
8285         ;; Or there were no default-scored articles.
8286         (gnus-summary-goto-article article))))
8287     (gnus-summary-position-cursor)))
8288
8289 (defun gnus-summary-goto-article (article &optional all-headers)
8290   "Fetch ARTICLE and display it if it exists.
8291 If ALL-HEADERS is non-nil, no header lines are hidden."
8292   (interactive
8293    (list
8294     (string-to-int
8295      (completing-read 
8296       "Article number: "
8297       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8298               gnus-newsgroup-headers) 
8299       nil 'require-match))))
8300   (prog1
8301       (and (gnus-summary-goto-subject article)
8302            (gnus-summary-display-article article all-headers))
8303     (gnus-summary-position-cursor)))
8304
8305 (defun gnus-summary-goto-last-article ()
8306   "Go to the previously read article."
8307   (interactive)
8308   (prog1
8309       (and gnus-last-article
8310            (gnus-summary-goto-article gnus-last-article))
8311     (gnus-summary-position-cursor)))
8312
8313 (defun gnus-summary-pop-article (number)
8314   "Pop one article off the history and go to the previous.
8315 NUMBER articles will be popped off."
8316   (interactive "p")
8317   (let (to)
8318     (setq gnus-newsgroup-history
8319           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8320     (if to
8321         (gnus-summary-goto-article (car to))
8322       (error "Article history empty")))
8323   (gnus-summary-position-cursor))
8324
8325 ;; Summary article oriented commands
8326
8327 (defun gnus-summary-refer-parent-article (n)
8328   "Refer parent article N times.
8329 The difference between N and the number of articles fetched is returned."
8330   (interactive "p")
8331   (gnus-set-global-variables)
8332   (while 
8333       (and 
8334        (> n 0)
8335        (let ((ref (header-references (gnus-get-header-by-num
8336                                       (gnus-summary-article-number)))))
8337          (if (and ref (not (equal ref ""))
8338                   (string-match "<[^<>]*>[ \t]*$" ref))
8339              (gnus-summary-refer-article 
8340               (substring ref (match-beginning 0) (match-end 0)))
8341            (gnus-message 1 "No references in article %d"
8342                          (gnus-summary-article-number))
8343            nil)))
8344     (setq n (1- n)))
8345   (gnus-summary-position-cursor)
8346   n)
8347     
8348 (defun gnus-summary-refer-article (message-id)
8349   "Refer article specified by MESSAGE-ID.
8350 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8351   (interactive "sMessage-ID: ")
8352   (if (or (not (stringp message-id))
8353           (zerop (length message-id)))
8354       ()
8355     ;; Construct the correct Message-ID if necessary.
8356     ;; Suggested by tale@pawl.rpi.edu.
8357     (or (string-match "^<" message-id)
8358         (setq message-id (concat "<" message-id)))
8359     (or (string-match ">$" message-id)
8360         (setq message-id (concat message-id ">")))
8361     (let ((header (car (gnus-gethash (downcase message-id)
8362                                      gnus-newsgroup-dependencies))))
8363       (if header
8364           (or (gnus-summary-goto-article (header-number header))
8365               ;; The header has been read, but the article had been
8366               ;; expunged, so we insert it again.
8367               (progn
8368                 (gnus-summary-insert-line
8369                  nil header 0 nil gnus-read-mark nil nil
8370                  (header-subject header))
8371                 (forward-line -1)
8372                 (header-number header)))
8373         (let ((gnus-override-method gnus-refer-article-method)
8374               (gnus-ancient-mark gnus-read-mark)
8375               (tmp-point (window-start
8376                           (get-buffer-window gnus-article-buffer)))
8377               number tmp-buf)
8378           (and gnus-refer-article-method
8379                (gnus-check-server gnus-refer-article-method))
8380           ;; Save the old article buffer.
8381           (save-excursion
8382             (set-buffer gnus-article-buffer)
8383             (gnus-kill-buffer " *temp Article*")
8384             (setq tmp-buf (rename-buffer " *temp Article*")))
8385           (prog1
8386               (if (gnus-article-prepare 
8387                    message-id nil (gnus-read-header message-id))
8388                   (progn
8389                     (setq number (header-number gnus-current-headers))
8390                     (gnus-rebuild-thread message-id)
8391                     (gnus-summary-goto-subject number)
8392                     (gnus-summary-recenter)
8393                     (gnus-article-set-window-start 
8394                      (cdr (assq number gnus-newsgroup-bookmarks)))
8395                     message-id)
8396                 ;; We restore the old article buffer.
8397                 (save-excursion
8398                   (kill-buffer gnus-article-buffer)
8399                   (set-buffer tmp-buf)
8400                   (rename-buffer gnus-article-buffer)
8401                   (let ((buffer-read-only nil))
8402                     (and tmp-point
8403                          (set-window-start (get-buffer-window (current-buffer))
8404                                            tmp-point)))))))))))
8405
8406 (defun gnus-summary-enter-digest-group ()
8407   "Enter a digest group based on the current article."
8408   (interactive)
8409   (gnus-set-global-variables)
8410   (gnus-summary-select-article)
8411   ;; We do not want a narrowed article.
8412   (gnus-summary-stop-page-breaking)
8413   (let ((name (format "%s-%d" 
8414                       (gnus-group-prefixed-name 
8415                        gnus-newsgroup-name (list 'nndoc "")) 
8416                       gnus-current-article))
8417         (ogroup gnus-newsgroup-name)
8418         (buf (current-buffer)))
8419     (if (gnus-group-read-ephemeral-group 
8420          name (list 'nndoc name
8421                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8422                     '(nndoc-article-type digest))
8423          t)
8424         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8425                 (list (list (cons 'to-group ogroup))))
8426       (switch-to-buffer buf)
8427       (gnus-set-global-variables)
8428       (gnus-configure-windows 'summary)
8429       (gnus-message 3 "Article not a digest?"))))
8430
8431 (defun gnus-summary-isearch-article ()
8432   "Do incremental search forward on current article."
8433   (interactive)
8434   (gnus-set-global-variables)
8435   (gnus-summary-select-article)
8436   (gnus-eval-in-buffer-window 
8437    gnus-article-buffer (isearch-forward)))
8438
8439 (defun gnus-summary-search-article-forward (regexp &optional backward)
8440   "Search for an article containing REGEXP forward.
8441 If BACKWARD, search backward instead."
8442   (interactive
8443    (list (read-string
8444           (format "Search article %s (regexp%s): "
8445                   (if current-prefix-arg "backward" "forward")
8446                   (if gnus-last-search-regexp
8447                       (concat ", default " gnus-last-search-regexp)
8448                     "")))
8449          current-prefix-arg))
8450   (gnus-set-global-variables)
8451   (if (string-equal regexp "")
8452       (setq regexp (or gnus-last-search-regexp ""))
8453     (setq gnus-last-search-regexp regexp))
8454   (if (gnus-summary-search-article regexp backward)
8455       (gnus-article-set-window-start 
8456        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8457     (error "Search failed: \"%s\"" regexp)))
8458
8459 (defun gnus-summary-search-article-backward (regexp)
8460   "Search for an article containing REGEXP backward."
8461   (interactive
8462    (list (read-string
8463           (format "Search article backward (regexp%s): "
8464                   (if gnus-last-search-regexp
8465                       (concat ", default " gnus-last-search-regexp)
8466                     "")))))
8467   (gnus-summary-search-article-forward regexp 'backward))
8468
8469 (defun gnus-summary-search-article (regexp &optional backward)
8470   "Search for an article containing REGEXP.
8471 Optional argument BACKWARD means do search for backward.
8472 gnus-select-article-hook is not called during the search."
8473   (let ((gnus-select-article-hook nil)  ;Disable hook.
8474         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8475         (re-search
8476          (if backward
8477              (function re-search-backward) (function re-search-forward)))
8478         (found nil)
8479         (last nil))
8480     ;; Hidden thread subtrees must be searched for ,too.
8481     (gnus-summary-show-all-threads)
8482     (if (eobp) (forward-line -1))
8483     ;; First of all, search current article.
8484     ;; We don't want to read article again from NNTP server nor reset
8485     ;; current point.
8486     (gnus-summary-select-article)
8487     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8488     (setq last gnus-current-article)
8489     (gnus-eval-in-buffer-window gnus-article-buffer
8490       (save-restriction
8491         (widen)
8492         ;; Begin search from current point.
8493         (setq found (funcall re-search regexp nil t))))
8494     ;; Then search next articles.
8495     (while (and (not found)
8496                 (gnus-summary-display-article 
8497                  (gnus-summary-search-subject backward nil nil)))
8498       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8499       (gnus-eval-in-buffer-window gnus-article-buffer
8500         (save-restriction
8501           (widen)
8502           (goto-char (if backward (point-max) (point-min)))
8503           (setq found (funcall re-search regexp nil t)))))
8504     (message "")
8505     ;; Adjust article pointer.
8506     (or (eq last gnus-current-article)
8507         (setq gnus-last-article last))
8508     ;; Return T if found such article.
8509     found))
8510
8511 (defun gnus-summary-execute-command (header regexp command &optional backward)
8512   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8513 If HEADER is an empty string (or nil), the match is done on the entire
8514 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8515   (interactive
8516    (list (let ((completion-ignore-case t))
8517            (completing-read 
8518             "Header name: "
8519             (mapcar (lambda (string) (list string))
8520                     '("Number" "Subject" "From" "Lines" "Date"
8521                       "Message-ID" "Xref" "References"))
8522             nil 'require-match))
8523          (read-string "Regexp: ")
8524          (read-key-sequence "Command: ")
8525          current-prefix-arg))
8526   (gnus-set-global-variables)
8527   ;; Hidden thread subtrees must be searched as well.
8528   (gnus-summary-show-all-threads)
8529   ;; We don't want to change current point nor window configuration.
8530   (save-excursion
8531     (save-window-excursion
8532       (gnus-message 6 "Executing %s..." (key-description command))
8533       ;; We'd like to execute COMMAND interactively so as to give arguments.
8534       (gnus-execute header regexp
8535                     (` (lambda ()
8536                          (call-interactively '(, (key-binding command)))))
8537                     backward)
8538       (gnus-message 6 "Executing %s...done" (key-description command)))))
8539
8540 (defun gnus-summary-beginning-of-article ()
8541   "Scroll the article back to the beginning."
8542   (interactive)
8543   (gnus-set-global-variables)
8544   (gnus-summary-select-article)
8545   (gnus-configure-windows 'article)
8546   (gnus-eval-in-buffer-window
8547    gnus-article-buffer
8548    (widen)
8549    (goto-char (point-min))
8550    (and gnus-break-pages (gnus-narrow-to-page))))
8551
8552 (defun gnus-summary-end-of-article ()
8553   "Scroll to the end of the article."
8554   (interactive)
8555   (gnus-set-global-variables)
8556   (gnus-summary-select-article)
8557   (gnus-configure-windows 'article)
8558   (gnus-eval-in-buffer-window 
8559    gnus-article-buffer
8560    (widen)
8561    (goto-char (point-max))
8562    (and gnus-break-pages (gnus-narrow-to-page))))
8563
8564 (defun gnus-summary-show-article ()
8565   "Force re-fetching of the current article."
8566   (interactive)
8567   (gnus-set-global-variables)
8568   (gnus-summary-select-article nil 'force)
8569   (gnus-configure-windows 'article)
8570   (gnus-summary-position-cursor))
8571
8572 (defun gnus-summary-verbose-headers (&optional arg)
8573   "Toggle permanent full header display.
8574 If ARG is a positive number, turn header display on.
8575 If ARG is a negative number, turn header display off."
8576   (interactive "P")
8577   (gnus-set-global-variables)
8578   (gnus-summary-toggle-header arg)
8579   (setq gnus-show-all-headers
8580         (cond ((or (not (numberp arg))
8581                    (zerop arg))
8582                (not gnus-show-all-headers))
8583               ((natnump arg)
8584                t))))
8585
8586 (defun gnus-summary-toggle-header (&optional arg)
8587   "Show the headers if they are hidden, or hide them if they are shown.
8588 If ARG is a positive number, show the entire header.
8589 If ARG is a negative number, hide the unwanted header lines."
8590   (interactive "P")
8591   (gnus-set-global-variables)
8592   (save-excursion
8593     (set-buffer gnus-article-buffer)
8594     (let ((buffer-read-only nil))
8595       (if (numberp arg) 
8596           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8597                                                 gnus-hidden-properties)
8598             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8599         (if (text-property-any (point-min) (point-max) 'invisible t)
8600             (remove-text-properties 
8601              (point-min) (point-max) gnus-hidden-properties)
8602           ;; We hide the headers. This song and dance act below is
8603           ;; done because `gnus-have-all-headers' is buffer-local to
8604           ;; the summary buffer, and we only want to temporarily
8605           ;; change it in that buffer. Ugh.
8606           (let ((have gnus-have-all-headers))
8607             (save-excursion
8608               (set-buffer gnus-summary-buffer)
8609               (setq gnus-have-all-headers nil)
8610               (save-excursion
8611                 (set-buffer gnus-article-buffer)
8612                 (run-hooks 'gnus-article-display-hook))
8613               (setq gnus-have-all-headers have)))))
8614       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8615
8616 (defun gnus-summary-show-all-headers ()
8617   "Make all header lines visible."
8618   (interactive)
8619   (gnus-set-global-variables)
8620   (gnus-article-show-all-headers))
8621
8622 (defun gnus-summary-toggle-mime (&optional arg)
8623   "Toggle MIME processing.
8624 If ARG is a positive number, turn MIME processing on."
8625   (interactive "P")
8626   (gnus-set-global-variables)
8627   (setq gnus-show-mime
8628         (if (null arg) (not gnus-show-mime)
8629           (> (prefix-numeric-value arg) 0)))
8630   (gnus-summary-select-article t 'force))
8631
8632 (defun gnus-summary-caesar-message (&optional arg)
8633   "Caesar rotate the current article by 13.
8634 The numerical prefix specifies how manu places to rotate each letter
8635 forward."
8636   (interactive "P")
8637   (gnus-set-global-variables)
8638   (gnus-summary-select-article)
8639   (let ((mail-header-separator ""))
8640     (gnus-eval-in-buffer-window 
8641      gnus-article-buffer
8642      (save-restriction
8643        (widen)
8644        (let ((start (window-start)))
8645          (news-caesar-buffer-body arg)
8646          (set-window-start (get-buffer-window (current-buffer)) start))))))
8647
8648 (defun gnus-summary-stop-page-breaking ()
8649   "Stop page breaking in the current article."
8650   (interactive)
8651   (gnus-set-global-variables)
8652   (gnus-summary-select-article)
8653   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8654
8655 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8656
8657 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8658   "Move the current article to a different newsgroup.
8659 If N is a positive number, move the N next articles.
8660 If N is a negative number, move the N previous articles.
8661 If N is nil and any articles have been marked with the process mark,
8662 move those articles instead.
8663 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8664 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8665 re-spool using this method.
8666 For this function to work, both the current newsgroup and the
8667 newsgroup that you want to move to have to support the `request-move'
8668 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8669   (interactive "P")
8670   (gnus-set-global-variables)
8671   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8672       (error "The current newsgroup does not support article moving"))
8673   (let ((articles (gnus-summary-work-articles n))
8674         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8675         art-group to-method sel-met)
8676     (if (and (not to-newsgroup) (not select-method))
8677         (setq to-newsgroup
8678               (completing-read 
8679                (format "Where do you want to move %s? %s"
8680                        (if (> (length articles) 1)
8681                            (format "these %d articles" (length articles))
8682                          "this article")
8683                        (if gnus-current-move-group
8684                            (format "(%s default) " gnus-current-move-group)
8685                          ""))
8686                gnus-active-hashtb nil nil prefix)))
8687     (if to-newsgroup
8688         (progn
8689           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8690               (setq to-newsgroup (or gnus-current-move-group "")))
8691           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8692               (gnus-activate-group to-newsgroup)
8693               (error "No such group: %s" to-newsgroup))
8694           (setq gnus-current-move-group to-newsgroup)))
8695     (setq to-method (if select-method (list select-method "")
8696                       (gnus-find-method-for-group to-newsgroup)))
8697     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8698         (error "%s does not support article copying" (car to-method)))
8699     (or (gnus-check-server to-method)
8700         (error "Can't open server %s" (car to-method)))
8701     (gnus-message 6 "Moving to %s: %s..." 
8702                   (or select-method to-newsgroup) articles)
8703     (while articles
8704       (if (setq art-group
8705                 (gnus-request-move-article 
8706                  (car articles)                   ; Article to move
8707                  gnus-newsgroup-name              ; From newsgrouo
8708                  (nth 1 (gnus-find-method-for-group 
8709                          gnus-newsgroup-name))    ; Server
8710                  (list 'gnus-request-accept-article 
8711                        (if select-method
8712                            (list 'quote select-method)
8713                          to-newsgroup)
8714                        (not (cdr articles)))     ; Accept form
8715                  (not (cdr articles))))          ; Only save nov last time
8716           (let* ((buffer-read-only nil)
8717                  (entry 
8718                   (or
8719                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8720                    (gnus-gethash 
8721                     (gnus-group-prefixed-name 
8722                      (car art-group) 
8723                      (if select-method (list select-method "")
8724                        (gnus-find-method-for-group to-newsgroup)))
8725                     gnus-newsrc-hashtb)))
8726                  (info (nth 2 entry))
8727                  (article (car articles)))
8728             (gnus-summary-goto-subject article)
8729             (beginning-of-line)
8730             (delete-region (point) (progn (forward-line 1) (point)))
8731             ;; Update the group that has been moved to.
8732             (if (not info)
8733                 ()                      ; This group does not exist yet.
8734               (if (not (memq article gnus-newsgroup-unreads))
8735                   (setcar (cdr (cdr info))
8736                           (gnus-add-to-range (nth 2 info) 
8737                                              (list (cdr art-group)))))
8738               ;; Copy any marks over to the new group.
8739               (let ((marks '((tick . gnus-newsgroup-marked)
8740                              (dormant . gnus-newsgroup-dormant)
8741                              (expire . gnus-newsgroup-expirable)
8742                              (bookmark . gnus-newsgroup-bookmarks)
8743                              (reply . gnus-newsgroup-replied)))
8744                     (to-article (cdr art-group)))
8745                 (while marks
8746                   (if (memq article (symbol-value (cdr (car marks))))
8747                       (gnus-add-marked-articles 
8748                        (car info) (car (car marks)) (list to-article) info))
8749                   (setq marks (cdr marks)))))
8750             ;; Update marks.
8751             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8752             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8753             (setq gnus-newsgroup-dormant
8754                   (delq article gnus-newsgroup-dormant))
8755             (setq gnus-newsgroup-reads
8756                   (cons (cons article gnus-canceled-mark)
8757                         gnus-newsgroup-reads)))
8758         (gnus-message 1 "Couldn't move article %s" (car articles)))
8759       (gnus-summary-remove-process-mark (car articles))
8760       (setq articles (cdr articles)))))
8761
8762 (defun gnus-summary-respool-article (&optional n respool-method)
8763   "Respool the current article.
8764 The article will be squeezed through the mail spooling process again,
8765 which means that it will be put in some mail newsgroup or other
8766 depending on `nnmail-split-methods'.
8767 If N is a positive number, respool the N next articles.
8768 If N is a negative number, respool the N previous articles.
8769 If N is nil and any articles have been marked with the process mark,
8770 respool those articles instead.
8771
8772 Respooling can be done both from mail groups and \"real\" newsgroups.
8773 In the former case, the articles in question will be moved from the
8774 current group into whatever groups they are destined to.  In the
8775 latter case, they will be copied into the relevant groups."
8776   (interactive "P")
8777   (gnus-set-global-variables)
8778   (let ((respool-methods (gnus-methods-using 'respool))
8779         (methname 
8780          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8781     (or respool-method
8782         (setq respool-method
8783               (completing-read
8784                "What method do you want to use when respooling? "
8785                respool-methods nil t methname)))
8786     (or (string= respool-method "")
8787         (if (assoc (symbol-name
8788                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8789                    respool-methods)
8790             (gnus-summary-move-article n nil (intern respool-method))
8791           (gnus-summary-copy-article n nil (intern respool-method))))))
8792
8793 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8794 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8795   "Move the current article to a different newsgroup.
8796 If N is a positive number, move the N next articles.
8797 If N is a negative number, move the N previous articles.
8798 If N is nil and any articles have been marked with the process mark,
8799 move those articles instead.
8800 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8801 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8802 re-spool using this method.
8803 For this function to work, the newsgroup that you want to move to have
8804 to support the `request-move' and `request-accept'
8805 functions. (Ie. mail newsgroups at present.)"
8806   (interactive "P")
8807   (gnus-set-global-variables)
8808   (let ((articles (gnus-summary-work-articles n))
8809         (copy-buf (get-buffer-create "*copy work*"))
8810         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8811         art-group to-method)
8812     (buffer-disable-undo copy-buf)
8813     (if (and (not to-newsgroup) (not select-method))
8814         (setq to-newsgroup
8815               (completing-read 
8816                (format "Where do you want to copy %s? %s"
8817                        (if (> (length articles) 1)
8818                            (format "these %d articles" (length articles))
8819                          "this article")
8820                        (if gnus-current-move-group
8821                            (format "(%s default) " gnus-current-move-group)
8822                          ""))
8823                gnus-active-hashtb nil nil prefix)))
8824     (if to-newsgroup
8825         (progn
8826           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8827               (setq to-newsgroup (or gnus-current-move-group "")))
8828           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8829               (gnus-activate-group to-newsgroup)
8830               (error "No such group: %s" to-newsgroup))
8831           (setq gnus-current-move-group to-newsgroup)))
8832     (setq to-method (if select-method (list select-method "")
8833                       (gnus-find-method-for-group to-newsgroup)))
8834     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8835         (error "%s does not support article copying" (car to-method)))
8836     (or (gnus-check-server to-method)
8837         (error "Can't open server %s" (car to-method)))
8838     (while articles
8839       (gnus-message 6 "Copying to %s: %s..." 
8840                     (or select-method to-newsgroup) articles)
8841       (if (setq art-group
8842                 (save-excursion
8843                   (set-buffer copy-buf)
8844                   (gnus-request-article-this-buffer
8845                    (car articles) gnus-newsgroup-name)
8846                   (gnus-request-accept-article
8847                    (if select-method (quote select-method) to-newsgroup)
8848                    (not (cdr articles)))))
8849           (let* ((entry 
8850                   (or
8851                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8852                    (gnus-gethash 
8853                     (gnus-group-prefixed-name 
8854                      (car art-group) 
8855                      (if select-method (list select-method "")
8856                        (gnus-find-method-for-group to-newsgroup)))
8857                     gnus-newsrc-hashtb)))
8858                  (info (nth 2 entry))
8859                  (article (car articles)))
8860             ;; We copy the info over to the new group.
8861             (if (not info)
8862                 ()                      ; This group does not exist (yet).
8863               (if (not (memq article gnus-newsgroup-unreads))
8864                   (setcar (cdr (cdr info))
8865                           (gnus-add-to-range (nth 2 info) 
8866                                              (list (cdr art-group)))))
8867               ;; Copy any marks over to the new group.
8868               (let ((marks '((tick . gnus-newsgroup-marked)
8869                              (dormant . gnus-newsgroup-dormant)
8870                              (expire . gnus-newsgroup-expirable)
8871                              (bookmark . gnus-newsgroup-bookmarks)
8872                              (reply . gnus-newsgroup-replied)))
8873                     (to-article (cdr art-group)))
8874                 (while marks
8875                   (if (memq article (symbol-value (cdr (car marks))))
8876                       (gnus-add-marked-articles 
8877                        (car info) (car (car marks)) (list to-article) info))
8878                   (setq marks (cdr marks))))))
8879         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8880       (gnus-summary-remove-process-mark (car articles))
8881       (setq articles (cdr articles)))
8882     (kill-buffer copy-buf)))
8883
8884 (defun gnus-summary-import-article (file)
8885   "Import a random file into a mail newsgroup."
8886   (interactive "fImport file: ")
8887   (let ((group gnus-newsgroup-name)
8888         atts)
8889     (or (gnus-check-backend-function 'request-accept-article group)
8890         (error "%s does not support article importing" group))
8891     (or (file-readable-p file)
8892         (not (file-regular-p file))
8893         (error "Can't read %s" file))
8894     (save-excursion
8895       (set-buffer (get-buffer-create " *import file*"))
8896       (buffer-disable-undo (current-buffer))
8897       (erase-buffer)
8898       (insert-file-contents file)
8899       (goto-char (point-min))
8900       (if (nnheader-article-p)
8901           ()
8902         (setq atts (file-attributes file))
8903         (insert "From: " (read-string "From: ") "\n"
8904                 "Subject: " (read-string "Subject: ") "\n"
8905                 "Date: " (current-time-string (nth 5 atts)) "\n"
8906                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8907       (gnus-request-accept-article group t)
8908       (kill-buffer (current-buffer)))))
8909
8910 (defun gnus-summary-expire-articles ()
8911   "Expire all articles that are marked as expirable in the current group."
8912   (interactive)
8913   (if (not (gnus-check-backend-function 
8914             'request-expire-articles gnus-newsgroup-name))
8915       ()
8916     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8917                                       gnus-newsrc-hashtb)))
8918            (total (memq 'total-expire (nth 5 info)))
8919            (expirable (if total
8920                           (gnus-list-of-read-articles gnus-newsgroup-name)
8921                         (setq gnus-newsgroup-expirable
8922                               (sort gnus-newsgroup-expirable '<))))
8923            es)
8924       (if (not expirable)
8925           ()
8926         (gnus-message 6 "Expiring articles...")
8927         ;; The list of articles that weren't expired is returned.
8928         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8929         (or total (setq gnus-newsgroup-expirable es))
8930         ;; We go through the old list of expirable, and mark all
8931         ;; really expired articles as non-existant.
8932         (or (eq es expirable) ; If nothing was expired, we don't mark.
8933             (let ((gnus-use-cache nil))
8934               (while expirable
8935                 (or (memq (car expirable) es)
8936                     (gnus-summary-mark-article
8937                      (car expirable) gnus-canceled-mark))
8938                 (setq expirable (cdr expirable)))))
8939         (gnus-message 6 "Expiring articles...done")))))
8940
8941 (defun gnus-summary-expire-articles-now ()
8942   "Expunge all expirable articles in the current group.
8943 This means that *all* articles that are marked as expirable will be
8944 deleted forever, right now."
8945   (interactive)
8946   (or gnus-expert-user
8947       (gnus-y-or-n-p
8948        "Are you really, really, really sure you want to expunge? ")
8949       (error "Phew!"))
8950   (let ((nnmail-expiry-wait -1)
8951         (nnmail-expiry-wait-function nil))
8952     (gnus-summary-expire-articles)))
8953
8954 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8955 (defun gnus-summary-delete-article (&optional n)
8956   "Delete the N next (mail) articles.
8957 This command actually deletes articles. This is not a marking
8958 command. The article will disappear forever from you life, never to
8959 return. 
8960 If N is negative, delete backwards.
8961 If N is nil and articles have been marked with the process mark,
8962 delete these instead."
8963   (interactive "P")
8964   (or (gnus-check-backend-function 'request-expire-articles 
8965                                    gnus-newsgroup-name)
8966       (error "The current newsgroup does not support article deletion."))
8967   ;; Compute the list of articles to delete.
8968   (let ((articles (gnus-summary-work-articles n))
8969         not-deleted)
8970     (if (and gnus-novice-user
8971              (not (gnus-y-or-n-p 
8972                    (format "Do you really want to delete %s forever? "
8973                            (if (> (length articles) 1) "these articles"
8974                              "this article")))))
8975         ()
8976       ;; Delete the articles.
8977       (setq not-deleted (gnus-request-expire-articles 
8978                          articles gnus-newsgroup-name 'force))
8979       (while articles
8980         (gnus-summary-remove-process-mark (car articles))       
8981         ;; The backend might not have been able to delete the article
8982         ;; after all.  
8983         (or (memq (car articles) not-deleted)
8984             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8985         (setq articles (cdr articles))))
8986     (gnus-summary-position-cursor)
8987     not-deleted))
8988
8989 (defun gnus-summary-edit-article ()
8990   "Enter into a buffer and edit the current article.
8991 This will have permanent effect only in mail groups."
8992   (interactive)
8993   (or (gnus-check-backend-function 
8994        'request-replace-article gnus-newsgroup-name)
8995       (error "The current newsgroup does not support article editing."))
8996   (gnus-summary-select-article t)
8997   (gnus-configure-windows 'article)
8998   (select-window (get-buffer-window gnus-article-buffer))
8999   (gnus-message 6 "C-c C-c to end edits")
9000   (setq buffer-read-only nil)
9001   (text-mode)
9002   (use-local-map (copy-keymap (current-local-map)))
9003   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
9004   (buffer-enable-undo)
9005   (widen)
9006   (goto-char (point-min))
9007   (search-forward "\n\n" nil t))
9008
9009 (defun gnus-summary-edit-article-done ()
9010   "Make edits to the current article permanent."
9011   (interactive)
9012   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
9013     (erase-buffer)
9014     (insert buf)
9015     (if (not (gnus-request-replace-article 
9016               (cdr gnus-article-current) (car gnus-article-current) 
9017               (current-buffer)))
9018         (error "Couldn't replace article.")
9019       (gnus-article-mode)
9020       (use-local-map gnus-article-mode-map)
9021       (setq buffer-read-only t)
9022       (buffer-disable-undo (current-buffer))
9023       (gnus-configure-windows 'summary))
9024       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))))
9025
9026 (defun gnus-summary-edit-article-postpone ()
9027   "Postpone changes to the current article."
9028   (interactive)
9029   (gnus-article-mode)
9030   (use-local-map gnus-article-mode-map)
9031   (setq buffer-read-only t)
9032   (buffer-disable-undo (current-buffer))
9033   (gnus-configure-windows 'summary)
9034   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9035
9036 (defun gnus-summary-fancy-query ()
9037   "Query where the fancy respool algorithm would put this article."
9038   (interactive)
9039   (gnus-summary-select-article)
9040   (save-excursion
9041     (set-buffer gnus-article-buffer)
9042     (save-restriction
9043       (goto-char (point-min))
9044       (search-forward "\n\n")
9045       (narrow-to-region (point-min) (point))
9046       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9047
9048 ;; Summary score commands.
9049
9050 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9051
9052 (defun gnus-summary-raise-score (n)
9053   "Raise the score of the current article by N."
9054   (interactive "p")
9055   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9056
9057 (defun gnus-summary-set-score (n)
9058   "Set the score of the current article to N."
9059   (interactive "p")
9060   ;; Skip dummy header line.
9061   (save-excursion
9062     (gnus-summary-show-thread)
9063     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9064         (forward-line 1))
9065     (let ((buffer-read-only nil))
9066       ;; Set score.
9067       (gnus-summary-update-mark
9068        (if (= n (or gnus-summary-default-score 0)) ? 
9069          (if (< n (or gnus-summary-default-score 0)) 
9070              gnus-score-below-mark gnus-score-over-mark)) 'score))
9071     (let* ((article (gnus-summary-article-number))
9072            (score (assq article gnus-newsgroup-scored)))
9073       (if score (setcdr score n)
9074         (setq gnus-newsgroup-scored 
9075               (cons (cons article n) gnus-newsgroup-scored))))
9076     (gnus-summary-update-line)))
9077
9078 (defun gnus-summary-current-score ()
9079   "Return the score of the current article."
9080   (interactive)
9081   (message "%s" (gnus-summary-article-score)))
9082
9083 ;; Summary marking commands.
9084
9085 (defun gnus-summary-raise-same-subject-and-select (score)
9086   "Raise articles which has the same subject with SCORE and select the next."
9087   (interactive "p")
9088   (let ((subject (gnus-summary-subject-string)))
9089     (gnus-summary-raise-score score)
9090     (while (gnus-summary-search-subject nil nil subject)
9091       (gnus-summary-raise-score score))
9092     (gnus-summary-next-article t)))
9093
9094 (defun gnus-summary-raise-same-subject (score)
9095   "Raise articles which has the same subject with SCORE."
9096   (interactive "p")
9097   (let ((subject (gnus-summary-subject-string)))
9098     (gnus-summary-raise-score score)
9099     (while (gnus-summary-search-subject nil nil subject)
9100       (gnus-summary-raise-score score))
9101     (gnus-summary-next-subject 1 t)))
9102
9103 (defun gnus-score-default (level)
9104   (if level (prefix-numeric-value level) 
9105     gnus-score-interactive-default-score))
9106
9107 (defun gnus-summary-raise-thread (&optional score)
9108   "Raise the score of the articles in the current thread with SCORE."
9109   (interactive "P")
9110   (setq score (1- (gnus-score-default score)))
9111   (let (e)
9112     (save-excursion
9113       (let ((level (gnus-summary-thread-level)))
9114         (gnus-summary-raise-score score)
9115         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9116                     (> (gnus-summary-thread-level) level))
9117           (gnus-summary-raise-score score))
9118         (setq e (point))))
9119     (let ((gnus-summary-check-current t))
9120       (or (zerop (gnus-summary-next-subject 1 t))
9121           (goto-char e))))
9122   (gnus-summary-recenter)
9123   (gnus-summary-position-cursor)
9124   (gnus-set-mode-line 'summary))
9125
9126 (defun gnus-summary-lower-same-subject-and-select (score)
9127   "Raise articles which has the same subject with SCORE and select the next."
9128   (interactive "p")
9129   (gnus-summary-raise-same-subject-and-select (- score)))
9130
9131 (defun gnus-summary-lower-same-subject (score)
9132   "Raise articles which has the same subject with SCORE."
9133   (interactive "p")
9134   (gnus-summary-raise-same-subject (- score)))
9135
9136 (defun gnus-summary-lower-thread (&optional score)
9137   "Lower score of articles in the current thread with SCORE."
9138   (interactive "P")
9139   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9140
9141 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9142   "Mark articles which has the same subject as read, and then select the next.
9143 If UNMARK is positive, remove any kind of mark.
9144 If UNMARK is negative, tick articles."
9145   (interactive "P")
9146   (if unmark
9147       (setq unmark (prefix-numeric-value unmark)))
9148   (let ((count
9149          (gnus-summary-mark-same-subject
9150           (gnus-summary-subject-string) unmark)))
9151     ;; Select next unread article. If auto-select-same mode, should
9152     ;; select the first unread article.
9153     (gnus-summary-next-article t (and gnus-auto-select-same
9154                                       (gnus-summary-subject-string)))
9155     (gnus-message 7 "%d article%s marked as %s"
9156                   count (if (= count 1) " is" "s are")
9157                   (if unmark "unread" "read"))))
9158
9159 (defun gnus-summary-kill-same-subject (&optional unmark)
9160   "Mark articles which has the same subject as read. 
9161 If UNMARK is positive, remove any kind of mark.
9162 If UNMARK is negative, tick articles."
9163   (interactive "P")
9164   (if unmark
9165       (setq unmark (prefix-numeric-value unmark)))
9166   (let ((count
9167          (gnus-summary-mark-same-subject
9168           (gnus-summary-subject-string) unmark)))
9169     ;; If marked as read, go to next unread subject.
9170     (if (null unmark)
9171         ;; Go to next unread subject.
9172         (gnus-summary-next-subject 1 t))
9173     (gnus-message 7 "%d articles are marked as %s"
9174                   count (if unmark "unread" "read"))))
9175
9176 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9177   "Mark articles with same SUBJECT as read, and return marked number.
9178 If optional argument UNMARK is positive, remove any kinds of marks.
9179 If optional argument UNMARK is negative, mark articles as unread instead."
9180   (let ((count 1))
9181     (save-excursion
9182       (cond 
9183        ((null unmark)                   ; Mark as read.
9184         (while (and 
9185                 (progn
9186                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9187                   (gnus-summary-show-thread) t)
9188                 (gnus-summary-search-forward nil subject))
9189           (setq count (1+ count))))
9190        ((> unmark 0)                    ; Tick.
9191         (while (and
9192                 (progn
9193                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9194                   (gnus-summary-show-thread) t)
9195                 (gnus-summary-search-forward nil subject))
9196           (setq count (1+ count))))
9197        (t                               ; Mark as unread.
9198         (while (and
9199                 (progn
9200                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9201                   (gnus-summary-show-thread) t)
9202                 (gnus-summary-search-forward nil subject))
9203           (setq count (1+ count)))))
9204       (gnus-set-mode-line 'summary)
9205       ;; Return the number of marked articles.
9206       count)))
9207
9208 (defun gnus-summary-mark-as-processable (n &optional unmark)
9209   "Set the process mark on the next N articles.
9210 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9211 the process mark instead.  The difference between N and the actual
9212 number of articles marked is returned."
9213   (interactive "p")
9214   (let ((backward (< n 0))
9215         (n (abs n)))
9216   (while (and 
9217           (> n 0)
9218           (if unmark
9219               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9220             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9221           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9222     (setq n (1- n)))
9223   (if (/= 0 n) (gnus-message 7 "No more articles"))
9224   (gnus-summary-recenter)
9225   (gnus-summary-position-cursor)
9226   n))
9227
9228 (defun gnus-summary-unmark-as-processable (n)
9229   "Remove the process mark from the next N articles.
9230 If N is negative, mark backward instead.  The difference between N and
9231 the actual number of articles marked is returned."
9232   (interactive "p")
9233   (gnus-summary-mark-as-processable n t))
9234
9235 (defun gnus-summary-unmark-all-processable ()
9236   "Remove the process mark from all articles."
9237   (interactive)
9238   (save-excursion
9239     (while gnus-newsgroup-processable
9240       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9241   (gnus-summary-position-cursor))
9242
9243 (defun gnus-summary-mark-as-expirable (n)
9244   "Mark N articles forward as expirable.
9245 If N is negative, mark backward instead. The difference between N and
9246 the actual number of articles marked is returned."
9247   (interactive "p")
9248   (gnus-summary-mark-forward n gnus-expirable-mark))
9249
9250 (defun gnus-summary-mark-article-as-replied (article)
9251   "Mark ARTICLE replied and update the summary line."
9252   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9253   (let ((buffer-read-only nil))
9254     (if (gnus-summary-goto-subject article)
9255         (progn
9256           (gnus-summary-update-mark gnus-replied-mark 'replied)
9257           t))))
9258
9259 (defun gnus-summary-set-bookmark (article)
9260   "Set a bookmark in current article."
9261   (interactive (list (gnus-summary-article-number)))
9262   (if (or (not (get-buffer gnus-article-buffer))
9263           (not gnus-current-article)
9264           (not gnus-article-current)
9265           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9266       (error "No current article selected"))
9267   ;; Remove old bookmark, if one exists.
9268   (let ((old (assq article gnus-newsgroup-bookmarks)))
9269     (if old (setq gnus-newsgroup-bookmarks 
9270                   (delq old gnus-newsgroup-bookmarks))))
9271   ;; Set the new bookmark, which is on the form 
9272   ;; (article-number . line-number-in-body).
9273   (setq gnus-newsgroup-bookmarks 
9274         (cons 
9275          (cons article 
9276                (save-excursion
9277                  (set-buffer gnus-article-buffer)
9278                  (count-lines
9279                   (min (point)
9280                        (save-excursion
9281                          (goto-char (point-min))
9282                          (search-forward "\n\n" nil t)
9283                          (point)))
9284                   (point))))
9285          gnus-newsgroup-bookmarks))
9286   (gnus-message 6 "A bookmark has been added to the current article."))
9287
9288 (defun gnus-summary-remove-bookmark (article)
9289   "Remove the bookmark from the current article."
9290   (interactive (list (gnus-summary-article-number)))
9291   ;; Remove old bookmark, if one exists.
9292   (let ((old (assq article gnus-newsgroup-bookmarks)))
9293     (if old 
9294         (progn
9295           (setq gnus-newsgroup-bookmarks 
9296                 (delq old gnus-newsgroup-bookmarks))
9297           (gnus-message 6 "Removed bookmark."))
9298       (gnus-message 6 "No bookmark in current article."))))
9299
9300 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9301 (defun gnus-summary-mark-as-dormant (n)
9302   "Mark N articles forward as dormant.
9303 If N is negative, mark backward instead.  The difference between N and
9304 the actual number of articles marked is returned."
9305   (interactive "p")
9306   (gnus-summary-mark-forward n gnus-dormant-mark))
9307
9308 (defun gnus-summary-set-process-mark (article)
9309   "Set the process mark on ARTICLE and update the summary line."
9310   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9311   (let ((buffer-read-only nil))
9312     (if (gnus-summary-goto-subject article)
9313         (progn
9314           (gnus-summary-show-thread)
9315           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9316                (forward-line 1))
9317           (gnus-summary-update-mark gnus-process-mark 'replied)
9318           t))))
9319
9320 (defun gnus-summary-remove-process-mark (article)
9321   "Remove the process mark from ARTICLE and update the summary line."
9322   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9323   (let ((buffer-read-only nil))
9324     (if (gnus-summary-goto-subject article)
9325         (progn
9326           (gnus-summary-show-thread)
9327           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9328                (forward-line 1))
9329           (gnus-summary-update-mark ?  'replied)
9330           (if (memq article gnus-newsgroup-replied) 
9331               (gnus-summary-update-mark gnus-replied-mark 'replied))
9332           t))))
9333
9334 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9335   "Mark N articles as read forwards.
9336 If N is negative, mark backwards instead.
9337 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9338 marked as unread. 
9339 The difference between N and the actual number of articles marked is
9340 returned."
9341   (interactive "p")
9342   (gnus-set-global-variables)
9343   (let ((backward (< n 0))
9344         (gnus-summary-goto-unread
9345          (and gnus-summary-goto-unread
9346               (not (memq mark (list gnus-unread-mark
9347                                     gnus-ticked-mark gnus-dormant-mark)))))
9348         (n (abs n))
9349         (mark (or mark gnus-del-mark)))
9350   (while (and (> n 0)
9351               (gnus-summary-mark-article nil mark no-expire)
9352               (zerop (gnus-summary-next-subject 
9353                       (if backward -1 1) gnus-summary-goto-unread t)))
9354     (setq n (1- n)))
9355   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9356   (gnus-summary-recenter)
9357   (gnus-summary-position-cursor)
9358   (gnus-set-mode-line 'summary)
9359   n))
9360
9361 (defun gnus-summary-mark-article-as-read (mark)
9362   "Mark the current article quickly as read with MARK."
9363   (let ((article (gnus-summary-article-number)))
9364     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9365     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9366     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9367     (setq gnus-newsgroup-reads
9368           (cons (cons article mark) gnus-newsgroup-reads))
9369     ;; Possibly remove from cache, if that is used. 
9370     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9371     (and gnus-newsgroup-auto-expire 
9372          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9373              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9374              (= mark gnus-read-mark))
9375          (progn
9376            (setq mark gnus-expirable-mark)
9377            (setq gnus-newsgroup-expirable 
9378                  (cons article gnus-newsgroup-expirable))))
9379     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9380       (forward-line 1))
9381     ;; Fix the mark.
9382     (gnus-summary-update-mark mark 'unread)
9383     t))
9384
9385 (defun gnus-summary-mark-article-as-unread (mark)
9386   "Mark the current article quickly as unread with MARK."
9387   (let ((article (gnus-summary-article-number)))
9388     (or (memq article gnus-newsgroup-unreads)
9389         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9390     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9391     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9392     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9393     (setq gnus-newsgroup-reads
9394           (delq (assq article gnus-newsgroup-reads)
9395                 gnus-newsgroup-reads))
9396     (if (= mark gnus-ticked-mark)
9397         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9398     (if (= mark gnus-dormant-mark)
9399         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9400
9401     ;; See whether the article is to be put in the cache.
9402     (and gnus-use-cache
9403          (vectorp (gnus-get-header-by-num article))
9404          (save-excursion
9405            (gnus-cache-possibly-enter-article 
9406             gnus-newsgroup-name article 
9407             (gnus-get-header-by-num article)
9408             (= mark gnus-ticked-mark)
9409             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9410
9411     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9412       (forward-line 1))
9413     ;; Fix the mark.
9414     (gnus-summary-update-mark mark 'unread)
9415     t))
9416
9417 (defun gnus-summary-mark-article (&optional article mark no-expire)
9418   "Mark ARTICLE with MARK.  MARK can be any character.
9419 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9420 (dormant) and `?E' (expirable).
9421 If MARK is nil, then the default character `?D' is used.
9422 If ARTICLE is nil, then the article on the current line will be
9423 marked." 
9424   (and (stringp mark)
9425        (setq mark (aref mark 0)))
9426   ;; If no mark is given, then we check auto-expiring.
9427   (and (not no-expire)
9428        gnus-newsgroup-auto-expire 
9429        (or (not mark)
9430            (and (numberp mark) 
9431                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9432                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9433                     (= mark gnus-read-mark))))
9434        (setq mark gnus-expirable-mark))
9435   (let* ((mark (or mark gnus-del-mark))
9436          (article (or article (gnus-summary-article-number))))
9437     (or article (error "No article on current line"))
9438     (if (or (= mark gnus-unread-mark) 
9439             (= mark gnus-ticked-mark) 
9440             (= mark gnus-dormant-mark))
9441         (gnus-mark-article-as-unread article mark)
9442       (gnus-mark-article-as-read article mark))
9443
9444     ;; See whether the article is to be put in the cache.
9445     (and gnus-use-cache
9446          (not (= mark gnus-canceled-mark))
9447          (vectorp (gnus-get-header-by-num article))
9448          (save-excursion
9449            (gnus-cache-possibly-enter-article 
9450             gnus-newsgroup-name article 
9451             (gnus-get-header-by-num article)
9452             (= mark gnus-ticked-mark)
9453             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9454
9455     (if (gnus-summary-goto-subject article)
9456         (let ((buffer-read-only nil))
9457           (gnus-summary-show-thread)
9458           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9459                (forward-line 1))
9460           ;; Fix the mark.
9461           (gnus-summary-update-mark mark 'unread)
9462           t))))
9463
9464 (defun gnus-summary-update-mark (mark type)
9465   (beginning-of-line)
9466   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9467         (buffer-read-only nil)
9468         plist)
9469     (if (not forward)
9470         ()
9471       (forward-char forward)
9472       (setq plist (text-properties-at (point)))
9473       (delete-char 1)
9474       (insert mark)
9475       (and plist (add-text-properties (1- (point)) (point) plist))
9476       (and (eq type 'unread)
9477            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9478       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9479   
9480 (defun gnus-mark-article-as-read (article &optional mark)
9481   "Enter ARTICLE in the pertinent lists and remove it from others."
9482   ;; Make the article expirable.
9483   (let ((mark (or mark gnus-del-mark)))
9484     (if (= mark gnus-expirable-mark)
9485         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9486       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9487     ;; Remove from unread and marked lists.
9488     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9489     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9490     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9491     (setq gnus-newsgroup-reads 
9492           (cons (cons article mark) gnus-newsgroup-reads))
9493     ;; Possibly remove from cache, if that is used. 
9494     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9495
9496 (defun gnus-mark-article-as-unread (article &optional mark)
9497   "Enter ARTICLE in the pertinent lists and remove it from others."
9498   (let ((mark (or mark gnus-ticked-mark)))
9499     ;; Add to unread list.
9500     (or (memq article gnus-newsgroup-unreads)
9501         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9502     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9503     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9504     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9505     (setq gnus-newsgroup-reads
9506           (delq (assq article gnus-newsgroup-reads)
9507                 gnus-newsgroup-reads))
9508     (if (= mark gnus-ticked-mark)
9509         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9510     (if (= mark gnus-dormant-mark)
9511         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9512
9513 (defalias 'gnus-summary-mark-as-unread-forward 
9514   'gnus-summary-tick-article-forward)
9515 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9516                'gnus-summary-tick-article-forward)
9517 (defun gnus-summary-tick-article-forward (n)
9518   "Tick N articles forwards.
9519 If N is negative, tick backwards instead.
9520 The difference between N and the number of articles ticked is returned."
9521   (interactive "p")
9522   (gnus-summary-mark-forward n gnus-ticked-mark))
9523
9524 (defalias 'gnus-summary-mark-as-unread-backward 
9525   'gnus-summary-tick-article-backward)
9526 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9527                'gnus-summary-tick-article-backward)
9528 (defun gnus-summary-tick-article-backward (n)
9529   "Tick N articles backwards.
9530 The difference between N and the number of articles ticked is returned."
9531   (interactive "p")
9532   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9533
9534 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9535 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9536 (defun gnus-summary-tick-article (&optional article clear-mark)
9537   "Mark current article as unread.
9538 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9539 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9540   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9541                                        gnus-ticked-mark)))
9542
9543 (defun gnus-summary-mark-as-read-forward (n)
9544   "Mark N articles as read forwards.
9545 If N is negative, mark backwards instead.
9546 The difference between N and the actual number of articles marked is
9547 returned."
9548   (interactive "p")
9549   (gnus-summary-mark-forward n gnus-del-mark t))
9550
9551 (defun gnus-summary-mark-as-read-backward (n)
9552   "Mark the N articles as read backwards.
9553 The difference between N and the actual number of articles marked is
9554 returned."
9555   (interactive "p")
9556   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9557
9558 (defun gnus-summary-mark-as-read (&optional article mark)
9559   "Mark current article as read.
9560 ARTICLE specifies the article to be marked as read.
9561 MARK specifies a string to be inserted at the beginning of the line."
9562   (gnus-summary-mark-article article mark))
9563
9564 (defun gnus-summary-clear-mark-forward (n)
9565   "Clear marks from N articles forward.
9566 If N is negative, clear backward instead.
9567 The difference between N and the number of marks cleared is returned."
9568   (interactive "p")
9569   (gnus-summary-mark-forward n gnus-unread-mark))
9570
9571 (defun gnus-summary-clear-mark-backward (n)
9572   "Clear marks from N articles backward.
9573 The difference between N and the number of marks cleared is returned."
9574   (interactive "p")
9575   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9576
9577 (defun gnus-summary-mark-unread-as-read ()
9578   "Intended to be used by `gnus-summary-mark-article-hook'."
9579   (or (memq gnus-current-article gnus-newsgroup-marked)
9580       (memq gnus-current-article gnus-newsgroup-dormant)
9581       (memq gnus-current-article gnus-newsgroup-expirable)
9582       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9583
9584 (defun gnus-summary-mark-region-as-read (point mark all)
9585   "Mark all unread articles between point and mark as read.
9586 If given a prefix, mark all articles between point and mark as read,
9587 even ticked and dormant ones."
9588   (interactive "r\nP")
9589   (save-excursion
9590     (goto-char point)
9591     (beginning-of-line)
9592     (while (and 
9593             (< (point) mark)
9594             (progn
9595               (and
9596                (or all
9597                    (and
9598                     (not (memq (gnus-summary-article-number)
9599                                gnus-newsgroup-marked))
9600                     (not (memq (gnus-summary-article-number)
9601                                gnus-newsgroup-dormant))))
9602                (gnus-summary-mark-article
9603                 (gnus-summary-article-number) gnus-del-mark))
9604               t)
9605             (zerop (forward-line 1))))))
9606
9607 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9608 (defalias 'gnus-summary-delete-marked-as-read 
9609   'gnus-summary-remove-lines-marked-as-read)
9610 (make-obsolete 'gnus-summary-delete-marked-as-read 
9611                'gnus-summary-remove-lines-marked-as-read)
9612 (defun gnus-summary-remove-lines-marked-as-read ()
9613   "Remove lines that are marked as read."
9614   (interactive)
9615   (gnus-summary-remove-lines-marked-with 
9616    (concat (mapconcat
9617             (lambda (char) (char-to-string (symbol-value char)))
9618             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9619               gnus-killed-mark gnus-kill-file-mark
9620               gnus-low-score-mark gnus-expirable-mark
9621               gnus-canceled-mark gnus-catchup-mark)
9622             ""))))
9623
9624 (defalias 'gnus-summary-delete-marked-with 
9625   'gnus-summary-remove-lines-marked-with)
9626 (make-obsolete 'gnus-summary-delete-marked-with 
9627                'gnus-summary-remove-lines-marked-with)
9628 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9629 (defun gnus-summary-remove-lines-marked-with (marks)
9630   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9631   (interactive "sMarks: ")
9632   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9633   (gnus-set-global-variables)
9634   (let ((buffer-read-only nil)
9635         (marks (concat "^[" marks "]")))
9636     (goto-char (point-min))
9637     (if gnus-newsgroup-adaptive
9638         (gnus-score-remove-lines-adaptive marks)
9639       (while (re-search-forward marks nil t)
9640         (gnus-delete-line)))
9641     ;; If we use dummy roots, we have to do an additional sweep over
9642     ;; the buffer.
9643     (if (not (eq gnus-summary-make-false-root 'dummy))
9644         ()
9645       (goto-char (point-min))
9646       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9647       (while (re-search-forward marks nil t)
9648         (if (gnus-subject-equal
9649              (gnus-summary-subject-string)
9650              (progn
9651                (forward-line 1)
9652                (gnus-summary-subject-string)))
9653             ()
9654           (forward-line -1)
9655           (gnus-delete-line)))))
9656   (or (zerop (buffer-size))
9657       (if (eobp)
9658           (gnus-summary-prev-subject 1)
9659         (gnus-summary-position-cursor))))
9660
9661 (defun gnus-summary-expunge-below (&optional score)
9662   "Remove articles with score less than SCORE."
9663   (interactive "P")
9664   (gnus-set-global-variables)
9665   (setq score (if score
9666                   (prefix-numeric-value score)
9667                 (or gnus-summary-default-score 0)))
9668   (save-excursion
9669     (set-buffer gnus-summary-buffer)
9670     (goto-char (point-min))
9671     (let ((buffer-read-only nil)
9672           beg)
9673       (while (not (eobp))
9674         (if (< (gnus-summary-article-score) score)
9675             (progn
9676               (setq beg (point))
9677               (forward-line 1)
9678               (delete-region beg (point)))
9679           (forward-line 1)))
9680       ;; Adjust point.
9681       (or (zerop (buffer-size))
9682           (if (eobp)
9683               (gnus-summary-prev-subject 1)
9684             (gnus-summary-position-cursor))))))
9685
9686 (defun gnus-summary-mark-below (score mark)
9687   "Mark articles with score less than SCORE with MARK."
9688   (interactive "P\ncMark: ")
9689   (gnus-set-global-variables)
9690   (setq score (if score
9691                   (prefix-numeric-value score)
9692                 (or gnus-summary-default-score 0)))
9693   (save-excursion
9694     (set-buffer gnus-summary-buffer)
9695     (goto-char (point-min))
9696     (while (not (eobp))
9697       (and (< (gnus-summary-article-score) score)
9698            (gnus-summary-mark-article nil mark))
9699       (forward-line 1))))
9700
9701 (defun gnus-summary-kill-below (&optional score)
9702   "Mark articles with score below SCORE as read."
9703   (interactive "P")
9704   (gnus-set-global-variables)
9705   (gnus-summary-mark-below score gnus-killed-mark))
9706
9707 (defun gnus-summary-clear-above (&optional score)
9708   "Clear all marks from articles with score above SCORE."
9709   (interactive "P")
9710   (gnus-set-global-variables)
9711   (gnus-summary-mark-above score gnus-unread-mark))
9712
9713 (defun gnus-summary-tick-above (&optional score)
9714   "Tick all articles with score above SCORE."
9715   (interactive "P")
9716   (gnus-set-global-variables)
9717   (gnus-summary-mark-above score gnus-ticked-mark))
9718
9719 (defun gnus-summary-mark-above (score mark)
9720   "Mark articles with score over SCORE with MARK."
9721   (interactive "P\ncMark: ")
9722   (gnus-set-global-variables)
9723   (setq score (if score
9724                   (prefix-numeric-value score)
9725                 (or gnus-summary-default-score 0)))
9726   (save-excursion
9727     (set-buffer gnus-summary-buffer)
9728     (goto-char (point-min))
9729     (while (not (eobp))
9730       (if (> (gnus-summary-article-score) score)
9731           (progn
9732             (gnus-summary-mark-article nil mark)
9733             (forward-line 1))
9734         (forward-line 1)))))
9735
9736 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9737 (defun gnus-summary-show-all-expunged ()
9738   "Display all the hidden articles that were expunged for low scores."
9739   (interactive)
9740   (gnus-set-global-variables)
9741   (let ((buffer-read-only nil))
9742     (let ((scored gnus-newsgroup-scored)
9743           headers h)
9744       (while scored
9745         (or (gnus-summary-goto-subject (car (car scored)))
9746             (and (setq h (gnus-get-header-by-num (car (car scored))))
9747                  (< (cdr (car scored)) gnus-summary-expunge-below)
9748                  (setq headers (cons h headers))))
9749         (setq scored (cdr scored)))
9750       (or headers (error "No expunged articles hidden."))
9751       (goto-char (point-min))
9752       (save-excursion 
9753         (gnus-summary-update-lines 
9754          (point)
9755          (progn
9756            (gnus-summary-prepare-unthreaded (nreverse headers))
9757            (point)))))
9758     (goto-char (point-min))
9759     (gnus-summary-position-cursor)))
9760
9761 (defun gnus-summary-show-all-dormant ()
9762   "Display all the hidden articles that are marked as dormant."
9763   (interactive)
9764   (gnus-set-global-variables)
9765   (let ((buffer-read-only nil))
9766     (let ((dormant gnus-newsgroup-dormant)
9767           headers h)
9768       (while dormant
9769         (or (gnus-summary-goto-subject (car dormant))
9770             (and (setq h (gnus-get-header-by-num (car dormant)))
9771                  (setq headers (cons h headers))))
9772         (setq dormant (cdr dormant)))
9773       (or headers (error "No dormant articles hidden."))
9774       (goto-char (point-min))
9775       (save-excursion 
9776         (gnus-summary-update-lines 
9777          (point)
9778          (progn
9779            (gnus-summary-prepare-unthreaded (nreverse headers))
9780            (point)))))
9781     (goto-char (point-min))
9782     (gnus-summary-position-cursor)))
9783
9784 (defun gnus-summary-hide-all-dormant ()
9785   "Hide all dormant articles."
9786   (interactive)
9787   (gnus-set-global-variables)
9788   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9789   (gnus-summary-position-cursor))
9790
9791 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9792   "Mark all articles not marked as unread in this newsgroup as read.
9793 If prefix argument ALL is non-nil, all articles are marked as read.
9794 If QUIETLY is non-nil, no questions will be asked.
9795 If TO-HERE is non-nil, it should be a point in the buffer. All
9796 articles before this point will be marked as read.
9797 The number of articles marked as read is returned."
9798   (interactive "P")
9799   (gnus-set-global-variables)
9800   (prog1
9801       (if (or quietly
9802               (not gnus-interactive-catchup) ;Without confirmation?
9803               gnus-expert-user
9804               (gnus-y-or-n-p
9805                (if all
9806                    "Mark absolutely all articles as read? "
9807                  "Mark all unread articles as read? ")))
9808           (if (and not-mark 
9809                    (not gnus-newsgroup-adaptive)
9810                    (not gnus-newsgroup-auto-expire))
9811               (progn
9812                 (and all (setq gnus-newsgroup-marked nil
9813                                gnus-newsgroup-dormant nil))
9814                 (setq gnus-newsgroup-unreads 
9815                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9816             ;; We actually mark all articles as canceled, which we
9817             ;; have to do when using auto-expiry or adaptive scoring. 
9818             (let ((unreads (length gnus-newsgroup-unreads)))
9819               (gnus-summary-show-all-threads)
9820               (if (gnus-summary-first-subject (not all))
9821                   (while (and 
9822                           (if to-here (< (point) to-here) t)
9823                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9824                           (gnus-summary-search-subject nil (not all)))))
9825               (- unreads (length gnus-newsgroup-unreads))
9826               (or to-here
9827                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9828     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9829       (if (and (not to-here) (eq 'nnvirtual (car method)))
9830           (nnvirtual-catchup-group
9831            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9832     (gnus-summary-position-cursor)))
9833
9834 (defun gnus-summary-catchup-to-here (&optional all)
9835   "Mark all unticked articles before the current one as read.
9836 If ALL is non-nil, also mark ticked and dormant articles as read."
9837   (interactive)
9838   (gnus-set-global-variables)
9839   (save-excursion
9840     (and (zerop (forward-line -1))
9841          (progn
9842            (end-of-line)
9843            (gnus-summary-catchup all t (point))
9844            (gnus-set-mode-line 'summary))))
9845   (gnus-summary-position-cursor))
9846
9847 (defun gnus-summary-catchup-all (&optional quietly)
9848   "Mark all articles in this newsgroup as read."
9849   (interactive)
9850   (gnus-set-global-variables)
9851   (gnus-summary-catchup t quietly))
9852
9853 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9854   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9855 If prefix argument ALL is non-nil, all articles are marked as read."
9856   (interactive "P")
9857   (gnus-set-global-variables)
9858   (gnus-summary-catchup all quietly nil 'fast)
9859   ;; Select next newsgroup or exit.
9860   (if (eq gnus-auto-select-next 'quietly)
9861       (gnus-summary-next-group nil)
9862     (gnus-summary-exit)))
9863
9864 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9865   "Mark all articles in this newsgroup as read, and then exit."
9866   (interactive)
9867   (gnus-set-global-variables)
9868   (gnus-summary-catchup-and-exit t quietly))
9869
9870 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9871 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9872   "Mark all articles in this group as read and select the next group.
9873 If given a prefix, mark all articles, unread as well as ticked, as
9874 read." 
9875   (interactive "P")
9876   (gnus-set-global-variables)
9877   (gnus-summary-catchup all)
9878   (gnus-summary-next-group))
9879
9880 ;; Thread-based commands.
9881
9882 (defun gnus-summary-toggle-threads (&optional arg)
9883   "Toggle showing conversation threads.
9884 If ARG is positive number, turn showing conversation threads on."
9885   (interactive "P")
9886   (gnus-set-global-variables)
9887   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9888     (setq gnus-show-threads
9889           (if (null arg) (not gnus-show-threads)
9890             (> (prefix-numeric-value arg) 0)))
9891     (gnus-summary-prepare)
9892     (gnus-summary-goto-subject current)
9893     (gnus-summary-position-cursor)))
9894
9895 (defun gnus-summary-show-all-threads ()
9896   "Show all threads."
9897   (interactive)
9898   (gnus-set-global-variables)
9899   (save-excursion
9900     (let ((buffer-read-only nil))
9901       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9902   (gnus-summary-position-cursor))
9903
9904 (defun gnus-summary-show-thread ()
9905   "Show thread subtrees.
9906 Returns nil if no thread was there to be shown."
9907   (interactive)
9908   (gnus-set-global-variables)
9909   (let ((buffer-read-only nil)
9910         (orig (point))
9911         ;; first goto end then to beg, to have point at beg after let
9912         (end (progn (end-of-line) (point)))
9913         (beg (progn (beginning-of-line) (point))))
9914     (prog1
9915         ;; Any hidden lines here?
9916         (search-forward "\r" end t)
9917       (subst-char-in-region beg end ?\^M ?\n t)
9918       (goto-char orig)
9919       (gnus-summary-position-cursor))))
9920
9921 (defun gnus-summary-hide-all-threads ()
9922   "Hide all thread subtrees."
9923   (interactive)
9924   (gnus-set-global-variables)
9925   (save-excursion
9926     (goto-char (point-min))
9927     (gnus-summary-hide-thread)
9928     (while (and (not (eobp)) (zerop (forward-line 1)))
9929       (gnus-summary-hide-thread)))
9930   (gnus-summary-position-cursor))
9931
9932 (defun gnus-summary-hide-thread ()
9933   "Hide thread subtrees.
9934 Returns nil if no threads were there to be hidden."
9935   (interactive)
9936   (gnus-set-global-variables)
9937   (let ((buffer-read-only nil)
9938         (start (point))
9939         (level (gnus-summary-thread-level))
9940         (end (point)))
9941     ;; Go forward until either the buffer ends or the subthread
9942     ;; ends. 
9943     (if (eobp)
9944         ()
9945       (while (and (zerop (forward-line 1))
9946                   (> (gnus-summary-thread-level) level))
9947         (setq end (point)))
9948       (prog1
9949           (save-excursion
9950             (goto-char end)
9951             (search-backward "\n" start t))
9952         (subst-char-in-region start end ?\n ?\^M t)
9953         (forward-line -1)))))
9954
9955 (defun gnus-summary-go-to-next-thread (&optional previous)
9956   "Go to the same level (or less) next thread.
9957 If PREVIOUS is non-nil, go to previous thread instead.
9958 Return the article number moved to, or nil if moving was impossible."
9959   (let ((level (gnus-summary-thread-level))
9960         (article (gnus-summary-article-number)))
9961     (if previous 
9962         (while (and (zerop (forward-line -1))
9963                     (> (gnus-summary-thread-level) level)))
9964       (while (and (save-excursion
9965                     (forward-line 1)
9966                     (not (eobp)))
9967                   (zerop (forward-line 1))
9968                   (> (gnus-summary-thread-level) level))))
9969     (gnus-summary-recenter)
9970     (gnus-summary-position-cursor)
9971     (let ((oart (gnus-summary-article-number)))
9972       (and (/= oart article) oart))))
9973
9974 (defun gnus-summary-next-thread (n)
9975   "Go to the same level next N'th thread.
9976 If N is negative, search backward instead.
9977 Returns the difference between N and the number of skips actually
9978 done."
9979   (interactive "p")
9980   (gnus-set-global-variables)
9981   (let ((backward (< n 0))
9982         (n (abs n)))
9983   (while (and (> n 0)
9984               (gnus-summary-go-to-next-thread backward))
9985     (setq n (1- n)))
9986   (gnus-summary-position-cursor)
9987   (if (/= 0 n) (gnus-message 7 "No more threads"))
9988   n))
9989
9990 (defun gnus-summary-prev-thread (n)
9991   "Go to the same level previous N'th thread.
9992 Returns the difference between N and the number of skips actually
9993 done."
9994   (interactive "p")
9995   (gnus-set-global-variables)
9996   (gnus-summary-next-thread (- n)))
9997
9998 (defun gnus-summary-go-down-thread (&optional same)
9999   "Go down one level in the current thread.
10000 If SAME is non-nil, also move to articles of the same level."
10001   (let ((level (gnus-summary-thread-level))
10002         (start (point)))
10003     (if (and (zerop (forward-line 1))
10004              (> (gnus-summary-thread-level) level))
10005         t
10006       (goto-char start)
10007       nil)))
10008
10009 (defun gnus-summary-go-up-thread ()
10010   "Go up one level in the current thread."
10011   (let ((level (gnus-summary-thread-level))
10012         (start (point)))
10013     (while (and (zerop (forward-line -1))
10014                 (>= (gnus-summary-thread-level) level)))
10015     (if (>= (gnus-summary-thread-level) level)
10016         (progn
10017           (goto-char start)
10018           nil)
10019       t)))
10020
10021 (defun gnus-summary-down-thread (n)
10022   "Go down thread N steps.
10023 If N is negative, go up instead.
10024 Returns the difference between N and how many steps down that were
10025 taken."
10026   (interactive "p")
10027   (gnus-set-global-variables)
10028   (let ((up (< n 0))
10029         (n (abs n)))
10030   (while (and (> n 0)
10031               (if up (gnus-summary-go-up-thread)
10032                 (gnus-summary-go-down-thread)))
10033     (setq n (1- n)))
10034   (gnus-summary-position-cursor)
10035   (if (/= 0 n) (gnus-message 7 "Can't go further"))
10036   n))
10037
10038 (defun gnus-summary-up-thread (n)
10039   "Go up thread N steps.
10040 If N is negative, go up instead.
10041 Returns the difference between N and how many steps down that were
10042 taken."
10043   (interactive "p")
10044   (gnus-set-global-variables)
10045   (gnus-summary-down-thread (- n)))
10046
10047 (defun gnus-summary-kill-thread (&optional unmark)
10048   "Mark articles under current thread as read.
10049 If the prefix argument is positive, remove any kinds of marks.
10050 If the prefix argument is negative, tick articles instead."
10051   (interactive "P")
10052   (gnus-set-global-variables)
10053   (if unmark
10054       (setq unmark (prefix-numeric-value unmark)))
10055   (let ((killing t)
10056         (level (gnus-summary-thread-level)))
10057     (save-excursion
10058       (while killing
10059         ;; Mark the article...
10060         (cond ((null unmark) (gnus-summary-mark-article-as-read
10061                                gnus-killed-mark))
10062               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10063                              gnus-unread-mark))
10064               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10065         ;; ...and go forward until either the buffer ends or the subtree
10066         ;; ends. 
10067         (if (not (and (zerop (forward-line 1))
10068                       (> (gnus-summary-thread-level) level)))
10069             (setq killing nil))))
10070     ;; Hide killed subtrees.
10071     (and (null unmark)
10072          gnus-thread-hide-killed
10073          (gnus-summary-hide-thread))
10074     ;; If marked as read, go to next unread subject.
10075     (if (null unmark)
10076         ;; Go to next unread subject.
10077         (gnus-summary-next-subject 1 t)))
10078   (gnus-set-mode-line 'summary))
10079
10080 ;; Summary sorting commands
10081
10082 (defun gnus-summary-sort-by-number (&optional reverse)
10083   "Sort summary buffer by article number.
10084 Argument REVERSE means reverse order."
10085   (interactive "P")
10086   (gnus-set-global-variables)
10087   (gnus-summary-sort 
10088    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10089    ;; a function, so we wrap it.
10090    (cons (lambda () (gnus-summary-article-number))
10091          'gnus-thread-sort-by-number) reverse))
10092
10093 (defun gnus-summary-sort-by-author (&optional reverse)
10094   "Sort summary buffer by author name alphabetically.
10095 If case-fold-search is non-nil, case of letters is ignored.
10096 Argument REVERSE means reverse order."
10097   (interactive "P")
10098   (gnus-set-global-variables)
10099   (gnus-summary-sort
10100    (cons
10101     (lambda ()
10102       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10103              (extract (funcall
10104                        gnus-extract-address-components
10105                        (header-from header))))
10106         (concat (or (car extract) (cdr extract))
10107                 "\r" (header-subject header))))
10108     'gnus-thread-sort-by-author)
10109    reverse))
10110
10111 (defun gnus-summary-sort-by-subject (&optional reverse)
10112   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10113 If case-fold-search is non-nil, case of letters is ignored.
10114 Argument REVERSE means reverse order."
10115   (interactive "P")
10116   (gnus-set-global-variables)
10117   (gnus-summary-sort
10118    (cons
10119     (lambda ()
10120       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10121              (extract (funcall
10122                        gnus-extract-address-components
10123                        (header-from header))))
10124         (concat 
10125          (downcase (gnus-simplify-subject (gnus-summary-subject-string)))
10126          "\r" (or (car extract) (cdr extract)))))
10127     'gnus-thread-sort-by-subject)
10128    reverse))
10129
10130 (defun gnus-summary-sort-by-date (&optional reverse)
10131   "Sort summary buffer by date.
10132 Argument REVERSE means reverse order."
10133   (interactive "P")
10134   (gnus-set-global-variables)
10135   (gnus-summary-sort
10136    (cons
10137     (lambda ()
10138       (gnus-sortable-date
10139        (header-date 
10140         (gnus-get-header-by-num (gnus-summary-article-number)))))
10141     'gnus-thread-sort-by-date)
10142    reverse))
10143
10144 (defun gnus-summary-sort-by-score (&optional reverse)
10145   "Sort summary buffer by score.
10146 Argument REVERSE means reverse order."
10147   (interactive "P")
10148   (gnus-set-global-variables)
10149   (gnus-summary-sort 
10150    (cons (lambda () (gnus-summary-article-score))
10151          'gnus-thread-sort-by-score)
10152    (not reverse)))
10153
10154 (defvar gnus-summary-already-sorted nil)
10155 (defun gnus-summary-sort (predicate reverse)
10156   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10157   (if gnus-summary-already-sorted
10158       ()
10159     (let (buffer-read-only)
10160       (if (not gnus-show-threads)
10161           ;; We do untreaded sorting...
10162           (progn
10163             (goto-char (point-min))
10164             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10165         ;; ... or we do threaded sorting.
10166         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10167               (gnus-summary-prepare-hook nil)
10168               (gnus-summary-already-sorted nil))
10169           ;; We do that by simply regenerating the threads.
10170           (gnus-summary-prepare)
10171           (and gnus-show-threads
10172                gnus-thread-hide-subtree
10173                (gnus-summary-hide-all-threads))
10174           ;; If in async mode, we send some info to the backend.
10175           (and gnus-newsgroup-async
10176                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10177                (gnus-request-asynchronous 
10178                 gnus-newsgroup-name
10179                 (if (and gnus-asynchronous-article-function
10180                          (fboundp gnus-asynchronous-article-function))
10181                     (funcall gnus-asynchronous-article-function
10182                              gnus-newsgroup-threads)))))))))
10183
10184   
10185 (defun gnus-sortable-date (date)
10186   "Make sortable string by string-lessp from DATE.
10187 Timezone package is used."
10188   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10189          (year (aref date 0))
10190          (month (aref date 1))
10191          (day (aref date 2)))
10192     (timezone-make-sortable-date 
10193      year month day 
10194      (timezone-make-time-string
10195       (aref date 3) (aref date 4) (aref date 5)))))
10196
10197
10198 ;; Summary saving commands.
10199
10200 (defun gnus-summary-save-article (&optional n)
10201   "Save the current article using the default saver function.
10202 If N is a positive number, save the N next articles.
10203 If N is a negative number, save the N previous articles.
10204 If N is nil and any articles have been marked with the process mark,
10205 save those articles instead.
10206 The variable `gnus-default-article-saver' specifies the saver function."
10207   (interactive "P")
10208   (gnus-set-global-variables)
10209   (let ((articles (gnus-summary-work-articles n)))
10210     (while articles
10211       (let ((header (gnus-get-header-by-num (car articles))))
10212         (if (vectorp header)
10213             (progn
10214               (save-window-excursion
10215                 (gnus-summary-select-article t nil nil (car articles)))
10216               (or gnus-save-all-headers
10217                   (gnus-article-hide-headers t))
10218               ;; Remove any X-Gnus lines.
10219               (save-excursion
10220                 (save-restriction
10221                   (set-buffer gnus-article-buffer)
10222                   (let ((buffer-read-only nil))
10223                     (goto-char (point-min))
10224                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10225                                                   (point-max)))
10226                     (while (re-search-forward "^X-Gnus" nil t)
10227                       (beginning-of-line)
10228                       (delete-region (point)
10229                                      (progn (forward-line 1) (point))))
10230                     (widen))))
10231               (save-window-excursion
10232                 (if gnus-default-article-saver
10233                     (funcall gnus-default-article-saver)
10234                   (error "No default saver is defined."))))
10235           (if (assq 'name header)
10236               (gnus-copy-file (cdr (assq 'name header)))
10237             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10238       (gnus-summary-remove-process-mark (car articles))
10239       (setq articles (cdr articles)))
10240     (gnus-summary-position-cursor)
10241     n))
10242
10243 (defun gnus-summary-pipe-output (&optional arg)
10244   "Pipe the current article to a subprocess.
10245 If N is a positive number, pipe the N next articles.
10246 If N is a negative number, pipe the N previous articles.
10247 If N is nil and any articles have been marked with the process mark,
10248 pipe those articles instead."
10249   (interactive "P")
10250   (gnus-set-global-variables)
10251   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10252     (gnus-summary-save-article arg)))
10253
10254 (defun gnus-summary-save-article-mail (&optional arg)
10255   "Append the current article to an mail file.
10256 If N is a positive number, save the N next articles.
10257 If N is a negative number, save the N previous articles.
10258 If N is nil and any articles have been marked with the process mark,
10259 save those articles instead."
10260   (interactive "P")
10261   (gnus-set-global-variables)
10262   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10263     (gnus-summary-save-article arg)))
10264
10265 (defun gnus-summary-save-article-rmail (&optional arg)
10266   "Append the current article to an rmail file.
10267 If N is a positive number, save the N next articles.
10268 If N is a negative number, save the N previous articles.
10269 If N is nil and any articles have been marked with the process mark,
10270 save those articles instead."
10271   (interactive "P")
10272   (gnus-set-global-variables)
10273   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10274     (gnus-summary-save-article arg)))
10275
10276 (defun gnus-summary-save-article-file (&optional arg)
10277   "Append the current article to a file.
10278 If N is a positive number, save the N next articles.
10279 If N is a negative number, save the N previous articles.
10280 If N is nil and any articles have been marked with the process mark,
10281 save those articles instead."
10282   (interactive "P")
10283   (gnus-set-global-variables)
10284   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10285     (gnus-summary-save-article arg)))
10286
10287 (defun gnus-read-save-file-name (prompt default-name)
10288   (let ((methods gnus-split-methods)
10289         split-name)
10290     (if (not gnus-split-methods)
10291         ()
10292       (save-excursion
10293         (set-buffer gnus-article-buffer)
10294         (gnus-narrow-to-headers)
10295         (while methods
10296           (goto-char (point-min))
10297           (and (condition-case () 
10298                    (re-search-forward (car (car methods)) nil t)
10299                  (error nil))
10300                (setq split-name (cons (nth 1 (car methods)) split-name)))
10301           (setq methods (cdr methods)))
10302         (widen)))
10303     (cond ((null split-name)
10304            (read-file-name
10305             (concat prompt " (default "
10306                     (file-name-nondirectory default-name) ") ")
10307             (file-name-directory default-name)
10308             default-name))
10309           ((= 1 (length split-name))
10310            (read-file-name
10311             (concat prompt " (default " (car split-name) ") ")
10312             gnus-article-save-directory
10313             (concat gnus-article-save-directory (car split-name))))
10314           (t
10315            (setq split-name (mapcar (lambda (el) (list el))
10316                                     (nreverse split-name)))
10317            (let ((result (completing-read 
10318                           (concat prompt " ")
10319                           split-name nil nil)))
10320              (concat gnus-article-save-directory
10321                      (if (string= result "")
10322                          (car (car split-name))
10323                        result)))))))
10324
10325 (defun gnus-summary-save-in-rmail (&optional filename)
10326   "Append this article to Rmail file.
10327 Optional argument FILENAME specifies file name.
10328 Directory to save to is default to `gnus-article-save-directory' which
10329 is initialized from the SAVEDIR environment variable."
10330   (interactive)
10331   (gnus-set-global-variables)
10332   (let ((default-name
10333           (funcall gnus-rmail-save-name gnus-newsgroup-name
10334                    gnus-current-headers gnus-newsgroup-last-rmail)))
10335     (or filename
10336         (setq filename (gnus-read-save-file-name 
10337                         "Save in rmail file:" default-name)))
10338     (gnus-make-directory (file-name-directory filename))
10339     (gnus-eval-in-buffer-window 
10340      gnus-article-buffer
10341      (save-excursion
10342        (save-restriction
10343          (widen)
10344          (gnus-output-to-rmail filename))))
10345     ;; Remember the directory name to save articles
10346     (setq gnus-newsgroup-last-rmail filename)))
10347
10348 (defun gnus-summary-save-in-mail (&optional filename)
10349   "Append this article to Unix mail file.
10350 Optional argument FILENAME specifies file name.
10351 Directory to save to is default to `gnus-article-save-directory' which
10352 is initialized from the SAVEDIR environment variable."
10353   (interactive)
10354   (gnus-set-global-variables)
10355   (let ((default-name
10356           (funcall gnus-mail-save-name gnus-newsgroup-name
10357                    gnus-current-headers gnus-newsgroup-last-mail)))
10358     (or filename
10359         (setq filename (gnus-read-save-file-name 
10360                         "Save in Unix mail file:" default-name)))
10361     (setq filename
10362           (expand-file-name filename
10363                             (and default-name
10364                                  (file-name-directory default-name))))
10365     (gnus-make-directory (file-name-directory filename))
10366     (gnus-eval-in-buffer-window 
10367      gnus-article-buffer
10368      (save-excursion
10369        (save-restriction
10370          (widen)
10371          (if (and (file-readable-p filename) (rmail-file-p filename))
10372              (gnus-output-to-rmail filename)
10373            (rmail-output filename 1 t t)))))
10374     ;; Remember the directory name to save articles.
10375     (setq gnus-newsgroup-last-mail filename)))
10376
10377 (defun gnus-summary-save-in-file (&optional filename)
10378   "Append this article to file.
10379 Optional argument FILENAME specifies file name.
10380 Directory to save to is default to `gnus-article-save-directory' which
10381 is initialized from the SAVEDIR environment variable."
10382   (interactive)
10383   (gnus-set-global-variables)
10384   (let ((default-name
10385           (funcall gnus-file-save-name gnus-newsgroup-name
10386                    gnus-current-headers gnus-newsgroup-last-file)))
10387     (or filename
10388         (setq filename (gnus-read-save-file-name 
10389                         "Save in file:" default-name)))
10390     (gnus-make-directory (file-name-directory filename))
10391     (gnus-eval-in-buffer-window 
10392      gnus-article-buffer
10393      (save-excursion
10394        (save-restriction
10395          (widen)
10396          (gnus-output-to-file filename))))
10397     ;; Remember the directory name to save articles.
10398     (setq gnus-newsgroup-last-file filename)))
10399
10400 (defun gnus-summary-save-in-pipe (&optional command)
10401   "Pipe this article to subprocess."
10402   (interactive)
10403   (gnus-set-global-variables)
10404   (let ((command (read-string "Shell command on article: "
10405                               gnus-last-shell-command)))
10406     (if (string-equal command "")
10407         (setq command gnus-last-shell-command))
10408     (gnus-eval-in-buffer-window 
10409      gnus-article-buffer
10410      (save-restriction
10411        (widen)
10412        (shell-command-on-region (point-min) (point-max) command nil)))
10413     (setq gnus-last-shell-command command)))
10414
10415 ;; Summary extract commands
10416
10417 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10418   (let ((buffer-read-only nil)
10419         (article (gnus-summary-article-number))
10420         b)
10421     (or (gnus-summary-goto-subject article)
10422         (error (format "No such article: %d" article)))
10423     (gnus-summary-position-cursor)
10424     ;; If all commands are to be bunched up on one line, we collect
10425     ;; them here.  
10426     (if gnus-view-pseudos-separately
10427         ()
10428       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10429             files action)
10430         (while ps
10431           (setq action (cdr (assq 'action (car ps))))
10432           (setq files (list (cdr (assq 'name (car ps)))))
10433           (while (and ps (cdr ps)
10434                       (string= (or action "1")
10435                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10436             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10437             (setcdr ps (cdr (cdr ps))))
10438           (if (not files)
10439               ()
10440             (if (not (string-match "%s" action))
10441                 (setq files (cons " " files)))
10442             (setq files (cons " " files))
10443             (and (assq 'execute (car ps))
10444                  (setcdr (assq 'execute (car ps))
10445                          (funcall (if (string-match "%s" action)
10446                                       'format 'concat)
10447                                   action 
10448                                   (mapconcat (lambda (f) f) files " ")))))
10449           (setq ps (cdr ps)))))
10450     (if (and gnus-view-pseudos (not not-view))
10451         (while pslist
10452           (and (assq 'execute (car pslist))
10453                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10454                                      (eq gnus-view-pseudos 'not-confirm)))
10455           (setq pslist (cdr pslist)))
10456       (save-excursion
10457         (while pslist
10458           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10459                                          (gnus-summary-article-number)))
10460           (forward-line 1)
10461           (setq b (point))
10462           (insert "          " (file-name-nondirectory 
10463                                 (cdr (assq 'name (car pslist))))
10464                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10465           (add-text-properties 
10466            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10467                           'gnus-mark gnus-unread-mark 
10468                           'gnus-level 0
10469                           'gnus-pseudo (car pslist)))
10470           (forward-line -1)
10471           (gnus-sethash (int-to-string gnus-reffed-article-number)
10472                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10473           (setq gnus-newsgroup-unreads
10474                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10475           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10476           (setq pslist (cdr pslist)))))))
10477
10478 (defun gnus-pseudos< (p1 p2)
10479   (let ((c1 (cdr (assq 'action p1)))
10480         (c2 (cdr (assq 'action p2))))
10481     (and c1 c2 (string< c1 c2))))
10482
10483 (defun gnus-request-pseudo-article (props)
10484   (cond ((assq 'execute props)
10485          (gnus-execute-command (cdr (assq 'execute props)))))
10486   (let ((gnus-current-article (gnus-summary-article-number)))
10487     (run-hooks 'gnus-mark-article-hook)))
10488
10489 (defun gnus-execute-command (command &optional automatic)
10490   (save-excursion
10491     (gnus-article-setup-buffer)
10492     (set-buffer gnus-article-buffer)
10493     (let ((command (if automatic command (read-string "Command: " command)))
10494           (buffer-read-only nil))
10495       (erase-buffer)
10496       (insert "$ " command "\n\n")
10497       (if gnus-view-pseudo-asynchronously
10498           (start-process "gnus-execute" nil "sh" "-c" command)
10499         (call-process "sh" nil t nil "-c" command)))))
10500
10501 (defun gnus-copy-file (file &optional to)
10502   "Copy FILE to TO."
10503   (interactive
10504    (list (read-file-name "Copy file: " default-directory)
10505          (read-file-name "Copy file to: " default-directory)))
10506   (gnus-set-global-variables)
10507   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10508   (and (file-directory-p to) 
10509        (setq to (concat (file-name-as-directory to)
10510                         (file-name-nondirectory file))))
10511   (copy-file file to))
10512
10513 ;; Summary kill commands.
10514
10515 (defun gnus-summary-edit-global-kill (article)
10516   "Edit the \"global\" kill file."
10517   (interactive (list (gnus-summary-article-number)))
10518   (gnus-set-global-variables)
10519   (gnus-group-edit-global-kill article))
10520
10521 (defun gnus-summary-edit-local-kill ()
10522   "Edit a local kill file applied to the current newsgroup."
10523   (interactive)
10524   (gnus-set-global-variables)
10525   (setq gnus-current-headers 
10526         (gnus-gethash 
10527          (int-to-string (gnus-summary-article-number))
10528          gnus-newsgroup-headers-hashtb-by-number))
10529   (gnus-set-global-variables)
10530   (gnus-group-edit-local-kill 
10531    (gnus-summary-article-number) gnus-newsgroup-name))
10532
10533 \f
10534 ;;;
10535 ;;; Gnus article mode
10536 ;;;
10537
10538 (put 'gnus-article-mode 'mode-class 'special)
10539
10540 (if gnus-article-mode-map
10541     nil
10542   (setq gnus-article-mode-map (make-keymap))
10543   (suppress-keymap gnus-article-mode-map)
10544   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10545   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10546   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10547   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10548   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10549   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10550   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10551   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10552   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10553   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10554   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10555   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10556   
10557   ;; Duplicate almost all summary keystrokes in the article mode map.
10558   (let ((commands 
10559          (list 
10560           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10561           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10562           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10563           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10564           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10565           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10566           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10567           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10568           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10569           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10570           "\C-c\C-v\C-v" "\C-d" "v" 
10571 ;;        "Mt" "M!" "Md" "Mr"
10572 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10573 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10574 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10575 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10576 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10577 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10578 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10579 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10580 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10581 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10582 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10583           )))
10584     (while (and nil commands) ; disabled
10585       (define-key gnus-article-mode-map (car commands) 
10586         'gnus-article-summary-command)
10587       (setq commands (cdr commands))))
10588
10589   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10590 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10591                          "=" "n"  "^" "\M-^")))
10592     (while (and nil commands) ; disabled
10593       (define-key gnus-article-mode-map (car commands) 
10594         'gnus-article-summary-command-nosave)
10595       (setq commands (cdr commands)))))
10596
10597
10598 (defun gnus-article-mode ()
10599   "Major mode for displaying an article.
10600
10601 All normal editing commands are switched off.
10602
10603 The following commands are available:
10604
10605 \\<gnus-article-mode-map>
10606 \\[gnus-article-next-page]\t Scroll the article one page forwards
10607 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10608 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10609 \\[gnus-article-show-summary]\t Display the summary buffer
10610 \\[gnus-article-mail]\t Send a reply to the address near point
10611 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10612 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10613 \\[gnus-info-find-node]\t Go to the Gnus info node"
10614   (interactive)
10615   (if gnus-visual (gnus-article-make-menu-bar))
10616   (kill-all-local-variables)
10617   (setq mode-line-modified "-- ")
10618   (make-local-variable 'mode-line-format)
10619   (setq mode-line-format (copy-sequence mode-line-format))
10620   (and (equal (nth 3 mode-line-format) "   ")
10621        (setcar (nthcdr 3 mode-line-format) ""))
10622   (setq mode-name "Article")
10623   (setq major-mode 'gnus-article-mode)
10624   (make-local-variable 'minor-mode-alist)
10625   (or (assq 'gnus-show-mime minor-mode-alist)
10626       (setq minor-mode-alist
10627             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10628   (use-local-map gnus-article-mode-map)
10629   (make-local-variable 'page-delimiter)
10630   (setq page-delimiter gnus-page-delimiter)
10631   (buffer-disable-undo (current-buffer))
10632   (setq buffer-read-only t)             ;Disable modification
10633   (run-hooks 'gnus-article-mode-hook))
10634
10635 (defun gnus-article-setup-buffer ()
10636   "Initialize article mode buffer."
10637   (if (get-buffer gnus-article-buffer)
10638       (save-excursion
10639         (set-buffer gnus-article-buffer)
10640         (buffer-disable-undo (current-buffer))
10641         (setq buffer-read-only t)
10642         (gnus-add-current-to-buffer-list)
10643         (or (eq major-mode 'gnus-article-mode)
10644             (gnus-article-mode)))
10645     (save-excursion
10646       (set-buffer (get-buffer-create gnus-article-buffer))
10647       (gnus-add-current-to-buffer-list)
10648       (gnus-article-mode))))
10649
10650 ;; Set article window start at LINE, where LINE is the number of lines
10651 ;; from the head of the article.
10652 (defun gnus-article-set-window-start (&optional line)
10653   (set-window-start 
10654    (get-buffer-window gnus-article-buffer)
10655    (save-excursion
10656      (set-buffer gnus-article-buffer)
10657      (goto-char (point-min))
10658      (if (not line)
10659          (point-min)
10660        (gnus-message 6 "Moved to bookmark")
10661        (search-forward "\n\n" nil t)
10662        (forward-line line)
10663        (point)))))
10664
10665 (defun gnus-request-article-this-buffer (article group)
10666   "Get an article and insert it into this buffer."
10667   (setq group (or group gnus-newsgroup-name))
10668
10669   ;; Open server if it has closed.
10670   (gnus-check-server (gnus-find-method-for-group group))
10671
10672   ;; Using `gnus-request-article' directly will insert the article into
10673   ;; `nntp-server-buffer' - so we'll save some time by not having to
10674   ;; copy it from the server buffer into the article buffer.
10675
10676   ;; We only request an article by message-id when we do not have the
10677   ;; headers for it, so we'll have to get those.
10678   (and (stringp article) 
10679        (let ((gnus-override-method gnus-refer-article-method))
10680          (gnus-read-header article)))
10681
10682   ;; If the article number is negative, that means that this article
10683   ;; doesn't belong in this newsgroup (possibly), so we find its
10684   ;; message-id and request it by id instead of number.
10685   (if (not (numberp article))
10686       ()
10687     (save-excursion
10688       (set-buffer gnus-summary-buffer)
10689       (let ((header (gnus-get-header-by-num article)))
10690         (if (< article 0)
10691             (if (vectorp header)
10692                 ;; It's a real article.
10693                 (setq article (header-id header))
10694               ;; It is an extracted pseudo-article.
10695               (setq article 'pseudo)
10696               (gnus-request-pseudo-article header)))
10697
10698         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10699           (if (not (eq (car method) 'nneething))
10700               ()
10701             (let ((dir (concat (file-name-as-directory (nth 1 method))
10702                                (header-subject header))))
10703               (if (file-directory-p dir)
10704                   (progn
10705                     (setq article 'nneething)
10706                     (gnus-group-enter-directory dir)))))))))
10707
10708   ;; Check the cache.
10709   (if (and gnus-use-cache
10710            (numberp article)
10711            (gnus-cache-request-article article group))
10712       'article
10713     ;; Get the article and into the article buffer.
10714     (if (or (stringp article) (numberp article))
10715         (progn
10716           (erase-buffer)
10717           (let ((gnus-override-method 
10718                  (and (stringp article) gnus-refer-article-method)))
10719             (and (gnus-request-article article group (current-buffer))
10720                  'article)))
10721       article)))
10722
10723 (defun gnus-read-header (id)
10724   "Read the headers of article ID and enter them into the Gnus system."
10725   (let (header)
10726     (if (not (setq header 
10727                    (car (if (let ((gnus-nov-is-evil t))
10728                               (gnus-retrieve-headers 
10729                                (list id) gnus-newsgroup-name))
10730                             (gnus-get-newsgroup-headers)))))
10731         nil
10732       (if (stringp id)
10733           (header-set-number header gnus-reffed-article-number))
10734       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10735       (gnus-sethash (int-to-string (header-number header)) header
10736                     gnus-newsgroup-headers-hashtb-by-number)
10737       (if (stringp id)
10738           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10739       (setq gnus-current-headers header)
10740       header)))
10741
10742 (defun gnus-article-prepare (article &optional all-headers header)
10743   "Prepare ARTICLE in article mode buffer.
10744 ARTICLE should either be an article number or a Message-ID.
10745 If ARTICLE is an id, HEADER should be the article headers.
10746 If ALL-HEADERS is non-nil, no headers are hidden."
10747   (save-excursion
10748     ;; Make sure we start in a summary buffer.
10749     (or (eq major-mode 'gnus-summary-mode)
10750         (set-buffer gnus-summary-buffer))
10751     (setq gnus-summary-buffer (current-buffer))
10752     ;; Make sure the connection to the server is alive.
10753     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10754         (progn
10755           (gnus-check-server 
10756            (gnus-find-method-for-group gnus-newsgroup-name))
10757           (gnus-request-group gnus-newsgroup-name t)))
10758     (let* ((article (if header (header-number header) article))
10759            (summary-buffer (current-buffer))
10760            (internal-hook gnus-article-internal-prepare-hook)
10761            (group gnus-newsgroup-name)
10762            result)
10763       (save-excursion
10764         (gnus-article-setup-buffer)
10765         (set-buffer gnus-article-buffer)
10766         (if (not (setq result (let ((buffer-read-only nil))
10767                                 (gnus-request-article-this-buffer 
10768                                  article group))))
10769             ;; There is no such article.
10770             (save-excursion
10771               (if (not (numberp article))
10772                   ()
10773                 (setq gnus-article-current 
10774                       (cons gnus-newsgroup-name article))
10775                 (set-buffer gnus-summary-buffer)
10776                 (setq gnus-current-article article)
10777                 (gnus-summary-mark-article article gnus-canceled-mark))
10778               (gnus-message 1 "No such article (may be canceled)")
10779               (ding)
10780               nil)
10781           (if (or (eq result 'pseudo) (eq result 'nneething))
10782               (progn
10783                 (save-excursion
10784                   (set-buffer summary-buffer)
10785                   (setq gnus-last-article gnus-current-article
10786                         gnus-newsgroup-history (cons gnus-current-article
10787                                                      gnus-newsgroup-history)
10788                         gnus-current-article 0
10789                         gnus-current-headers nil
10790                         gnus-article-current nil)
10791                   (if (eq result 'nneething)
10792                       (gnus-configure-windows 'summary)
10793                     (gnus-configure-windows 'article))
10794                   (gnus-set-global-variables))
10795                 (gnus-set-mode-line 'article))
10796             ;; The result from the `request' was an actual article -
10797             ;; or at least some text that is now displayed in the
10798             ;; article buffer.
10799             (if (and (numberp article)
10800                      (not (eq article gnus-current-article)))
10801                 ;; Seems like a new article has been selected.
10802                 ;; `gnus-current-article' must be an article number.
10803                 (save-excursion
10804                   (set-buffer summary-buffer)
10805                   (setq gnus-last-article gnus-current-article
10806                         gnus-newsgroup-history (cons gnus-current-article
10807                                                      gnus-newsgroup-history)
10808                         gnus-current-article article
10809                         gnus-current-headers 
10810                         (gnus-get-header-by-num gnus-current-article)
10811                         gnus-article-current 
10812                         (cons gnus-newsgroup-name gnus-current-article))
10813                   (gnus-summary-show-thread)
10814                   (run-hooks 'gnus-mark-article-hook)
10815                   (gnus-set-mode-line 'summary)
10816                   (and gnus-visual 
10817                        (run-hooks 'gnus-visual-mark-article-hook))
10818                   ;; Set the global newsgroup variables here.
10819                   ;; Suggested by Jim Sisolak
10820                   ;; <sisolak@trans4.neep.wisc.edu>.
10821                   (gnus-set-global-variables)
10822                   (setq gnus-have-all-headers 
10823                         (or all-headers gnus-show-all-headers))
10824                   (and gnus-use-cache 
10825                        (vectorp (gnus-get-header-by-number article))
10826                        (gnus-cache-possibly-enter-article
10827                         group article
10828                         (gnus-get-header-by-number article)
10829                         (memq article gnus-newsgroup-marked)
10830                         (memq article gnus-newsgroup-dormant)
10831                         (memq article gnus-newsgroup-unreads)))))
10832             ;; Hooks for getting information from the article.
10833             ;; This hook must be called before being narrowed.
10834             (let (buffer-read-only)
10835               (run-hooks 'internal-hook)
10836               (run-hooks 'gnus-article-prepare-hook)
10837               ;; Decode MIME message.
10838               (if (and gnus-show-mime
10839                        (or (not gnus-strict-mime)
10840                            (gnus-fetch-field "Mime-Version")))
10841                   (funcall gnus-show-mime-method))
10842               ;; Perform the article display hooks.
10843               (run-hooks 'gnus-article-display-hook))
10844             ;; Do page break.
10845             (goto-char (point-min))
10846             (and gnus-break-pages (gnus-narrow-to-page))
10847             (gnus-set-mode-line 'article)
10848             (gnus-configure-windows 'article)
10849             (goto-char (point-min))
10850             t))))))
10851
10852 (defun gnus-article-show-all-headers ()
10853   "Show all article headers in article mode buffer."
10854   (save-excursion 
10855     (gnus-article-setup-buffer)
10856     (set-buffer gnus-article-buffer)
10857     (let ((buffer-read-only nil))
10858       (remove-text-properties (point-min) (point-max) 
10859                               gnus-hidden-properties))))
10860
10861 (defun gnus-article-hide-headers-if-wanted ()
10862   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10863 Provided for backwards compatability."
10864   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10865       (gnus-article-hide-headers)))
10866
10867 (defun gnus-article-hide-headers (&optional delete)
10868   "Hide unwanted headers and possibly sort them as well."
10869   (interactive "P")
10870   (save-excursion
10871     (set-buffer gnus-article-buffer)
10872     (save-restriction
10873       (let ((sorted gnus-sorted-header-list)
10874             (buffer-read-only nil)
10875             want-list beg want-l)
10876         ;; First we narrow to just the headers.
10877         (widen)
10878         (goto-char (point-min))
10879         ;; Hide any "From " lines at the beginning of (mail) articles. 
10880         (while (looking-at "From ")
10881           (forward-line 1))
10882         (if (bobp) 
10883             (add-text-properties (point-min) (point) gnus-hidden-properties))
10884         ;; Then treat the rest of the header lines.
10885         (narrow-to-region 
10886          (point) 
10887          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10888         ;; Then we use the two regular expressions
10889         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10890         ;; select which header lines is to remain visible in the
10891         ;; article buffer.
10892         (goto-char (point-min))
10893         (while (re-search-forward "^[^ \t]*:" nil t)
10894           (beginning-of-line)
10895           ;; We add the headers we want to keep to a list and delete
10896           ;; them from the buffer.
10897           (if (or (and (stringp gnus-visible-headers)
10898                        (looking-at gnus-visible-headers))
10899                   (and (not (stringp gnus-visible-headers))
10900                        (stringp gnus-ignored-headers)
10901                        (not (looking-at gnus-ignored-headers))))
10902               (progn
10903                 (setq beg (point))
10904                 (forward-line 1)
10905                 ;; Be sure to get multi-line headers...
10906                 (re-search-forward "^[^ \t]*:" nil t)
10907                 (beginning-of-line)
10908                 (setq want-list 
10909                       (cons (buffer-substring beg (point)) want-list))
10910                 (delete-region beg (point))
10911                 (goto-char beg))
10912             (forward-line 1)))
10913         ;; Next we perform the sorting by looking at
10914         ;; `gnus-sorted-header-list'. 
10915         (goto-char (point-min))
10916         (while (and sorted want-list)
10917           (setq want-l want-list)
10918           (while (and want-l
10919                       (not (string-match (car sorted) (car want-l))))
10920             (setq want-l (cdr want-l)))
10921           (if want-l 
10922               (progn
10923                 (insert (car want-l))
10924                 (setq want-list (delq (car want-l) want-list))))
10925           (setq sorted (cdr sorted)))
10926         ;; Any headers that were not matched by the sorted list we
10927         ;; just tack on the end of the visible header list.
10928         (while want-list
10929           (insert (car want-list))
10930           (setq want-list (cdr want-list)))
10931         ;; And finally we make the unwanted headers invisible.
10932         (if delete
10933             (delete-region (point) (point-max))
10934           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10935           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10936
10937 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10938 (defun gnus-article-treat-overstrike ()
10939   "Translate overstrikes into bold text."
10940   (interactive)
10941   (save-excursion
10942     (set-buffer gnus-article-buffer)
10943     (let ((buffer-read-only nil))
10944       (while (search-forward "\b" nil t)
10945         (let ((next (following-char))
10946               (previous (char-after (- (point) 2))))
10947           (cond ((eq next previous)
10948                  (put-text-property (- (point) 2) (point)
10949                                     'invisible t)
10950                  (put-text-property (point) (1+ (point))
10951                                     'face 'bold))
10952                 ((eq next ?_)
10953                  (put-text-property (1- (point)) (1+ (point))
10954                                     'invisible t)
10955                  (put-text-property (1- (point)) (point)
10956                                     'face 'underline))
10957                 ((eq previous ?_)
10958                  (put-text-property (- (point) 2) (point)
10959                                     'invisible t)
10960                  (put-text-property (point) (1+ (point))
10961                                     'face 'underline))))))))
10962
10963 (defun gnus-article-word-wrap ()
10964   "Format too long lines."
10965   (interactive)
10966   (save-excursion
10967     (set-buffer gnus-article-buffer)
10968     (let ((buffer-read-only nil))
10969       (goto-char (point-min))
10970       (search-forward "\n\n" nil t)
10971       (end-of-line 1)
10972       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10973             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10974             (adaptive-fill-mode t))
10975         (while (not (eobp))
10976           (and (>= (current-column) (min fill-column (window-width)))
10977                (/= (preceding-char) ?:)
10978                (fill-paragraph nil))
10979           (end-of-line 2))))))
10980
10981 (defun gnus-article-remove-cr ()
10982   "Remove carriage returns from an article."
10983   (interactive)
10984   (save-excursion
10985     (set-buffer gnus-article-buffer)
10986     (let ((buffer-read-only nil))
10987       (goto-char (point-min))
10988       (while (search-forward "\r" nil t)
10989         (replace-match "" t t)))))
10990
10991 (defun gnus-article-display-x-face (&optional force)
10992   "Look for an X-Face header and display it if present."
10993   (interactive (list 'force))
10994   (save-excursion
10995     (set-buffer gnus-article-buffer)
10996     (let ((inhibit-point-motion-hooks t)
10997           (case-fold-search nil))
10998       (save-restriction
10999         (goto-char (point-min))
11000         (search-forward "\n\n")
11001         (narrow-to-region (point-min) (point))
11002         (goto-char (point-min))
11003         (if (or (not gnus-article-x-face-command)
11004                 (and (not force)
11005                      (or (not gnus-article-x-face-too-ugly)
11006                          (string-match gnus-article-x-face-too-ugly
11007                                        (mail-fetch-field "from"))))
11008                 (progn
11009                   (goto-char (point-min))
11010                   (not (re-search-forward "^X-Face: " nil t))))
11011             nil
11012           (let ((beg (point))
11013                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
11014             (if (symbolp gnus-article-x-face-command)
11015                 (and (or (fboundp gnus-article-x-face-command)
11016                          (error "%s is not a function"
11017                                 gnus-article-x-face-command))
11018                      (funcall gnus-article-x-face-command beg end))
11019               (call-process-region beg end "sh" nil 0 nil
11020                                    "-c" gnus-article-x-face-command))))))))
11021
11022 (defun gnus-article-de-quoted-unreadable (&optional force)
11023   "Do a naïve translation of a quoted-printable-encoded article.
11024 This is in no way, shape or form meant as a replacement for real MIME
11025 processing, but is simply a stop-gap measure until MIME support is
11026 written.
11027 If FORCE, decode the article whether it is marked as quoted-printable
11028 or not." 
11029   (interactive (list 'force))
11030   (save-excursion
11031     (set-buffer gnus-article-buffer)
11032     (let ((case-fold-search t)
11033           (buffer-read-only nil)
11034           (type (gnus-fetch-field "content-transfer-encoding")))
11035       (if (or force (and type (string-match "quoted-printable" type)))
11036           (progn
11037             (goto-char (point-min))
11038             (search-forward "\n\n" nil 'move)
11039             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11040
11041 (defun gnus-mime-decode-quoted-printable (from to)
11042   ;; Decode quoted-printable from region between FROM and TO.
11043   (save-excursion
11044     (goto-char from)
11045     (while (search-forward "=" to t)
11046       (cond ((eq (following-char) ?\n)
11047              (delete-char -1)
11048              (delete-char 1))
11049             ((looking-at "[0-9A-F][0-9A-F]")
11050              (delete-char -1)
11051              (insert (hexl-hex-string-to-integer
11052                       (buffer-substring (point) (+ 2 (point)))))
11053              (delete-char 2))
11054             ((looking-at "=")
11055              (delete-char 1))
11056             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11057
11058 (defvar gnus-article-time-units
11059   (list (cons 'year (* 365.25 24 60 60))
11060         (cons 'week (* 7 24 60 60))
11061         (cons 'day (* 24 60 60))
11062         (cons 'hour (* 60 60))
11063         (cons 'minute 60)
11064         (cons 'second 1)))
11065
11066 (defun gnus-article-date-ut (&optional type)
11067   "Convert DATE date to universal time in the current article.
11068 If TYPE is `local', convert to local time; if it is `lapsed', output
11069 how much time has lapsed since DATE."
11070   (interactive (list 'ut))
11071   (let ((date (header-date (or gnus-current-headers 
11072                                (gnus-get-header-by-number
11073                                 (gnus-summary-article-number))"")))
11074         (date-regexp "^Date: \\|^X-Sent: "))
11075     (if (or (not date)
11076             (string= date ""))
11077         ()
11078       (save-excursion
11079         (set-buffer gnus-article-buffer)
11080         (let ((buffer-read-only nil))
11081           (goto-char (point-min))
11082           (if (and (re-search-forward date-regexp nil t)
11083                    (progn 
11084                      (beginning-of-line)
11085                      (looking-at date-regexp)))
11086               (delete-region (gnus-point-at-bol)
11087                              (progn (end-of-line) (1+ (point))))
11088             (goto-char (point-min))
11089             (goto-char (- (search-forward "\n\n") 2)))
11090           (insert
11091            (cond 
11092             ((eq type 'local)
11093              (concat "Date: " (condition-case ()
11094                                   (timezone-make-date-arpa-standard date)
11095                                 (error date))
11096                      "\n"))
11097             ((eq type 'ut)
11098              (concat "Date: "
11099                      (condition-case ()
11100                          (timezone-make-date-arpa-standard date nil "UT")
11101                        (error date))
11102                      "\n"))
11103             ((eq type 'lapsed)
11104              ;; If the date is seriously mangled, the timezone
11105              ;; functions are liable to bug out, so we condition-case
11106              ;; the entire thing.  
11107              (let* ((real-sec (condition-case ()
11108                                   (- (gnus-seconds-since-epoch 
11109                                       (timezone-make-date-arpa-standard
11110                                        (current-time-string) 
11111                                        (current-time-zone) "UT"))
11112                                      (gnus-seconds-since-epoch 
11113                                       (timezone-make-date-arpa-standard 
11114                                        date nil "UT")))
11115                                 (error 0)))
11116                     (sec (abs real-sec))
11117                     num prev)
11118                (if (zerop sec)
11119                    "X-Sent: Now\n"
11120                  (concat
11121                   "X-Sent: "
11122                   (mapconcat 
11123                    (lambda (unit)
11124                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11125                          ""
11126                        (setq sec (- sec (* num (cdr unit))))
11127                        (prog1
11128                            (concat (if prev ", " "") (int-to-string 
11129                                                       (floor num))
11130                                    " " (symbol-name (car unit))
11131                                    (if (> num 1) "s" ""))
11132                          (setq prev t))))
11133                    gnus-article-time-units "")
11134                   (if (> real-sec 0)
11135                       " ago\n"
11136                     " in the future\n")))))
11137             (t
11138              (error "Unknown conversion type: %s" type)))))))))
11139
11140 (defun gnus-article-date-local ()
11141   "Convert the current article date to the local timezone."
11142   (interactive)
11143   (gnus-article-date-ut 'local))
11144
11145 (defun gnus-article-date-lapsed ()
11146   "Convert the current article date to time lapsed since it was sent."
11147   (interactive)
11148   (gnus-article-date-ut 'lapsed))
11149
11150 (defun gnus-article-maybe-highlight ()
11151   "Do some article highlighting if `gnus-visual' is non-nil."
11152   (if gnus-visual (gnus-article-highlight-some)))
11153
11154 ;; Article savers.
11155
11156 (defun gnus-output-to-rmail (file-name)
11157   "Append the current article to an Rmail file named FILE-NAME."
11158   (require 'rmail)
11159   ;; Most of these codes are borrowed from rmailout.el.
11160   (setq file-name (expand-file-name file-name))
11161   (setq rmail-default-rmail-file file-name)
11162   (let ((artbuf (current-buffer))
11163         (tmpbuf (get-buffer-create " *Gnus-output*")))
11164     (save-excursion
11165       (or (get-file-buffer file-name)
11166           (file-exists-p file-name)
11167           (if (gnus-yes-or-no-p
11168                (concat "\"" file-name "\" does not exist, create it? "))
11169               (let ((file-buffer (create-file-buffer file-name)))
11170                 (save-excursion
11171                   (set-buffer file-buffer)
11172                   (rmail-insert-rmail-file-header)
11173                   (let ((require-final-newline nil))
11174                     (write-region (point-min) (point-max) file-name t 1)))
11175                 (kill-buffer file-buffer))
11176             (error "Output file does not exist")))
11177       (set-buffer tmpbuf)
11178       (buffer-disable-undo (current-buffer))
11179       (erase-buffer)
11180       (insert-buffer-substring artbuf)
11181       (gnus-convert-article-to-rmail)
11182       ;; Decide whether to append to a file or to an Emacs buffer.
11183       (let ((outbuf (get-file-buffer file-name)))
11184         (if (not outbuf)
11185             (append-to-file (point-min) (point-max) file-name)
11186           ;; File has been visited, in buffer OUTBUF.
11187           (set-buffer outbuf)
11188           (let ((buffer-read-only nil)
11189                 (msg (and (boundp 'rmail-current-message)
11190                           (symbol-value 'rmail-current-message))))
11191             ;; If MSG is non-nil, buffer is in RMAIL mode.
11192             (if msg
11193                 (progn (widen)
11194                        (narrow-to-region (point-max) (point-max))))
11195             (insert-buffer-substring tmpbuf)
11196             (if msg
11197                 (progn
11198                   (goto-char (point-min))
11199                   (widen)
11200                   (search-backward "\^_")
11201                   (narrow-to-region (point) (point-max))
11202                   (goto-char (1+ (point-min)))
11203                   (rmail-count-new-messages t)
11204                   (rmail-show-message msg)))))))
11205     (kill-buffer tmpbuf)))
11206
11207 (defun gnus-output-to-file (file-name)
11208   "Append the current article to a file named FILE-NAME."
11209   (setq file-name (expand-file-name file-name))
11210   (let ((artbuf (current-buffer))
11211         (tmpbuf (get-buffer-create " *Gnus-output*")))
11212     (save-excursion
11213       (set-buffer tmpbuf)
11214       (buffer-disable-undo (current-buffer))
11215       (erase-buffer)
11216       (insert-buffer-substring artbuf)
11217       ;; Append newline at end of the buffer as separator, and then
11218       ;; save it to file.
11219       (goto-char (point-max))
11220       (insert "\n")
11221       (append-to-file (point-min) (point-max) file-name))
11222     (kill-buffer tmpbuf)))
11223
11224 (defun gnus-convert-article-to-rmail ()
11225   "Convert article in current buffer to Rmail message format."
11226   (let ((buffer-read-only nil))
11227     ;; Convert article directly into Babyl format.
11228     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11229     (goto-char (point-min))
11230     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11231     (while (search-forward "\n\^_" nil t) ;single char
11232       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11233     (goto-char (point-max))
11234     (insert "\^_")))
11235
11236 (defun gnus-narrow-to-page (&optional arg)
11237   "Make text outside current page invisible except for page delimiter.
11238 A numeric arg specifies to move forward or backward by that many pages,
11239 thus showing a page other than the one point was originally in."
11240   (interactive "P")
11241   (setq arg (if arg (prefix-numeric-value arg) 0))
11242   (save-excursion
11243     (forward-page -1)                   ;Beginning of current page.
11244     (widen)
11245     (if (> arg 0)
11246         (forward-page arg)
11247       (if (< arg 0)
11248           (forward-page (1- arg))))
11249     ;; Find the end of the page.
11250     (forward-page)
11251     ;; If we stopped due to end of buffer, stay there.
11252     ;; If we stopped after a page delimiter, put end of restriction
11253     ;; at the beginning of that line.
11254     ;; These are commented out.
11255     ;;    (if (save-excursion (beginning-of-line)
11256     ;;                  (looking-at page-delimiter))
11257     ;;  (beginning-of-line))
11258     (narrow-to-region (point)
11259                       (progn
11260                         ;; Find the top of the page.
11261                         (forward-page -1)
11262                         ;; If we found beginning of buffer, stay there.
11263                         ;; If extra text follows page delimiter on same line,
11264                         ;; include it.
11265                         ;; Otherwise, show text starting with following line.
11266                         (if (and (eolp) (not (bobp)))
11267                             (forward-line 1))
11268                         (point)))))
11269
11270 (defun gnus-gmt-to-local ()
11271   "Rewrite Date header described in GMT to local in current buffer.
11272 Intended to be used with gnus-article-prepare-hook."
11273   (save-excursion
11274     (save-restriction
11275       (widen)
11276       (goto-char (point-min))
11277       (narrow-to-region (point-min)
11278                         (progn (search-forward "\n\n" nil 'move) (point)))
11279       (goto-char (point-min))
11280       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11281           (let ((buffer-read-only nil)
11282                 (date (buffer-substring-no-properties
11283                        (match-beginning 1) (match-end 1))))
11284             (delete-region (match-beginning 1) (match-end 1))
11285             (insert
11286              (timezone-make-date-arpa-standard 
11287               date nil (current-time-zone))))))))
11288
11289
11290 ;; Article mode commands
11291
11292 (defun gnus-article-next-page (&optional lines)
11293   "Show next page of current article.
11294 If end of article, return non-nil. Otherwise return nil.
11295 Argument LINES specifies lines to be scrolled up."
11296   (interactive "P")
11297   (move-to-window-line -1)
11298   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11299   (if (save-excursion
11300         (end-of-line)
11301         (and (pos-visible-in-window-p)  ;Not continuation line.
11302              (eobp)))
11303       ;; Nothing in this page.
11304       (if (or (not gnus-break-pages)
11305               (save-excursion
11306                 (save-restriction
11307                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11308           t                             ;Nothing more.
11309         (gnus-narrow-to-page 1)         ;Go to next page.
11310         nil)
11311     ;; More in this page.
11312     (condition-case ()
11313         (scroll-up lines)
11314       (end-of-buffer
11315        ;; Long lines may cause an end-of-buffer error.
11316        (goto-char (point-max))))
11317     nil))
11318
11319 (defun gnus-article-prev-page (&optional lines)
11320   "Show previous page of current article.
11321 Argument LINES specifies lines to be scrolled down."
11322   (interactive "P")
11323   (move-to-window-line 0)
11324   (if (and gnus-break-pages
11325            (bobp)
11326            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11327       (progn
11328         (gnus-narrow-to-page -1) ;Go to previous page.
11329         (goto-char (point-max))
11330         (recenter -1))
11331     (scroll-down lines)))
11332
11333 (defun gnus-article-refer-article ()
11334   "Read article specified by message-id around point."
11335   (interactive)
11336   (search-forward ">" nil t)    ;Move point to end of "<....>".
11337   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11338       (let ((message-id
11339              (buffer-substring (match-beginning 1) (match-end 1))))
11340         (set-buffer gnus-summary-buffer)
11341         (gnus-summary-refer-article message-id))
11342     (error "No references around point")))
11343
11344 (defun gnus-article-show-summary ()
11345   "Reconfigure windows to show summary buffer."
11346   (interactive)
11347   (gnus-configure-windows 'article)
11348   (gnus-summary-goto-subject gnus-current-article))
11349
11350 (defun gnus-article-describe-briefly ()
11351   "Describe article mode commands briefly."
11352   (interactive)
11353   (gnus-message 6
11354    (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")))
11355
11356 (defun gnus-article-summary-command ()
11357   "Execute the last keystroke in the summary buffer."
11358   (interactive)
11359   (let ((obuf (current-buffer))
11360         (owin (current-window-configuration))
11361         func)
11362     (switch-to-buffer gnus-summary-buffer 'norecord)
11363     (setq func (lookup-key (current-local-map) (this-command-keys)))
11364     (call-interactively func)
11365     (set-buffer obuf)
11366     (set-window-configuration owin)
11367     (set-window-point (get-buffer-window (current-buffer)) (point))))
11368
11369 (defun gnus-article-summary-command-nosave ()
11370   "Execute the last keystroke in the summary buffer."
11371   (interactive)
11372   (let (func)
11373     (pop-to-buffer gnus-summary-buffer 'norecord)
11374     (setq func (lookup-key (current-local-map) (this-command-keys)))
11375     (call-interactively func)))
11376
11377 \f
11378 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11379
11380 ;;;###autoload
11381 (defalias 'gnus-batch-kill 'gnus-batch-score)
11382 ;;;###autoload
11383 (defun gnus-batch-score ()
11384   "Run batched scoring.
11385 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11386 Newsgroups is a list of strings in Bnews format.  If you want to score
11387 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11388 score the alt hierarchy, you'd say \"!alt.all\"."
11389   (interactive)
11390   (let* ((yes-and-no
11391           (gnus-newsrc-parse-options
11392            (apply (function concat)
11393                   (mapcar (lambda (g) (concat g " "))
11394                           command-line-args-left))))
11395          (gnus-expert-user t)
11396          (nnmail-spool-file nil)
11397          (gnus-use-dribble-file nil)
11398          (yes (car yes-and-no))
11399          (no (cdr yes-and-no))
11400          group newsrc entry
11401          ;; Disable verbose message.
11402          gnus-novice-user gnus-large-newsgroup)
11403     ;; Eat all arguments.
11404     (setq command-line-args-left nil)
11405     ;; Start Gnus.
11406     (gnus)
11407     ;; Apply kills to specified newsgroups in command line arguments.
11408     (setq newsrc (cdr gnus-newsrc-alist))
11409     (while newsrc
11410       (setq group (car (car newsrc)))
11411       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11412       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11413                (and (car entry)
11414                     (or (eq (car entry) t)
11415                         (not (zerop (car entry)))))
11416                (if yes (string-match yes group) t)
11417                (or (null no) (not (string-match no group))))
11418           (progn
11419             (gnus-summary-read-group group nil t)
11420             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11421                  (gnus-summary-exit))))
11422       (setq newsrc (cdr newsrc)))
11423     ;; Exit Emacs.
11424     (switch-to-buffer gnus-group-buffer)
11425     (gnus-group-save-newsrc)))
11426
11427 (defun gnus-apply-kill-file ()
11428   "Apply a kill file to the current newsgroup.
11429 Returns the number of articles marked as read."
11430   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11431           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11432       (gnus-apply-kill-file-internal)
11433     0))
11434
11435 (defun gnus-kill-save-kill-buffer ()
11436   (save-excursion
11437     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11438       (if (get-file-buffer file)
11439           (progn
11440             (set-buffer (get-file-buffer file))
11441             (and (buffer-modified-p) (save-buffer))
11442             (kill-buffer (current-buffer)))))))
11443
11444 (defvar gnus-kill-file-name "KILL"
11445   "Suffix of the kill files.")
11446
11447 (defun gnus-newsgroup-kill-file (newsgroup)
11448   "Return the name of a kill file name for NEWSGROUP.
11449 If NEWSGROUP is nil, return the global kill file name instead."
11450   (cond ((or (null newsgroup)
11451              (string-equal newsgroup ""))
11452          ;; The global KILL file is placed at top of the directory.
11453          (expand-file-name gnus-kill-file-name
11454                            (or gnus-kill-files-directory "~/News")))
11455         ((gnus-use-long-file-name 'not-kill)
11456          ;; Append ".KILL" to newsgroup name.
11457          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11458                                    "." gnus-kill-file-name)
11459                            (or gnus-kill-files-directory "~/News")))
11460         (t
11461          ;; Place "KILL" under the hierarchical directory.
11462          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11463                                    "/" gnus-kill-file-name)
11464                            (or gnus-kill-files-directory "~/News")))))
11465
11466 \f
11467 ;;;
11468 ;;; Dribble file
11469 ;;;
11470
11471 (defvar gnus-dribble-ignore nil)
11472 (defvar gnus-dribble-eval-file nil)
11473
11474 (defun gnus-dribble-file-name ()
11475   (concat gnus-current-startup-file "-dribble"))
11476
11477 (defun gnus-dribble-enter (string)
11478   (if (and (not gnus-dribble-ignore)
11479            gnus-dribble-buffer
11480            (buffer-name gnus-dribble-buffer))
11481       (let ((obuf (current-buffer)))
11482         (set-buffer gnus-dribble-buffer)
11483         (insert string "\n")
11484         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11485         (set-buffer obuf))))
11486
11487 (defun gnus-dribble-read-file ()
11488   (let ((dribble-file (gnus-dribble-file-name)))
11489     (save-excursion 
11490       (set-buffer (setq gnus-dribble-buffer 
11491                         (get-buffer-create 
11492                          (file-name-nondirectory dribble-file))))
11493       (gnus-add-current-to-buffer-list)
11494       (erase-buffer)
11495       (set-visited-file-name dribble-file)
11496       (buffer-disable-undo (current-buffer))
11497       (bury-buffer (current-buffer))
11498       (set-buffer-modified-p nil)
11499       (let ((auto (make-auto-save-file-name))
11500             (gnus-dribble-ignore t))
11501         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11502             (progn
11503               (if (file-newer-than-file-p auto dribble-file)
11504                   (setq dribble-file auto))
11505               (insert-file-contents dribble-file)
11506               (if (not (zerop (buffer-size)))
11507                   (set-buffer-modified-p t))
11508               (if (gnus-y-or-n-p 
11509                    "Auto-save file exists. Do you want to read it? ")
11510                   (setq gnus-dribble-eval-file t))))))))
11511
11512 (defun gnus-dribble-eval-file ()
11513   (if (not gnus-dribble-eval-file)
11514       ()
11515     (setq gnus-dribble-eval-file nil)
11516     (save-excursion
11517       (let ((gnus-dribble-ignore t))
11518         (set-buffer gnus-dribble-buffer)
11519         (eval-buffer (current-buffer))))))
11520
11521 (defun gnus-dribble-delete-file ()
11522   (if (file-exists-p (gnus-dribble-file-name))
11523       (delete-file (gnus-dribble-file-name)))
11524   (if gnus-dribble-buffer
11525       (save-excursion
11526         (set-buffer gnus-dribble-buffer)
11527         (let ((auto (make-auto-save-file-name)))
11528           (if (file-exists-p auto)
11529               (delete-file auto))
11530           (erase-buffer)
11531           (set-buffer-modified-p nil)))))
11532
11533 (defun gnus-dribble-save ()
11534   (if (and gnus-dribble-buffer
11535            (buffer-name gnus-dribble-buffer))
11536       (save-excursion
11537         (set-buffer gnus-dribble-buffer)
11538         (save-buffer))))
11539
11540 (defun gnus-dribble-clear ()
11541   (save-excursion
11542     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11543         (progn
11544           (set-buffer gnus-dribble-buffer)
11545           (erase-buffer)
11546           (set-buffer-modified-p nil)
11547           (setq buffer-saved-size (buffer-size))))))
11548
11549 ;;;
11550 ;;; Server Communication
11551 ;;;
11552
11553 ;; All the Gnus backends have the same interface, and should return
11554 ;; data in a similar format. Below is an overview of what functions
11555 ;; these packages must supply and what results they should return.
11556 ;;
11557 ;; Variables:
11558 ;;
11559 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11560 ;; buffer. 
11561 ;;
11562 ;; Functions for the imaginary backend `choke':
11563 ;;
11564 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11565 ;; Should return all headers for all ARTICLES, or return NOV lines for
11566 ;; the same.
11567 ;;
11568 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11569 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11570 ;; must be returned.
11571 ;;
11572 ;; `choke-close-group GROUP &optional SERVER'
11573 ;; Close group. Most backends won't have to do anything with this
11574 ;; call, but it is an opportunity to clean up, if that is needed. It
11575 ;; is called when Gnus exits a group.
11576 ;;
11577 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11578 ;; Return ARTICLE, which is either an article number or
11579 ;; message-id. Note that not all backends can return articles based on
11580 ;; message-id. 
11581 ;;
11582 ;; `choke-request-list SERVER'
11583 ;; Return a list of all newsgroups on SERVER.
11584 ;;
11585 ;; `choke-request-list-newsgroups SERVER'
11586 ;; Return a list of descriptions of all newsgroups on SERVER.
11587 ;;
11588 ;; `choke-request-newgroups DATE &optional SERVER'
11589 ;; Return a list of all groups that have arrived after DATE on
11590 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11591 ;; always check whether the groups are old or not. Backends that do
11592 ;; not store date information may just return the entire list of
11593 ;; groups, although this might not be a good idea in general.
11594 ;;
11595 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11596 ;; Should return a buffer that is suitable for "posting". nnspool and
11597 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11598 ;; buffer. This function should fill out the appropriate headers. 
11599 ;;
11600 ;; `choke-request-post &optional SERVER'
11601 ;; Function that will be called from a buffer to be posted. 
11602 ;;
11603 ;; `choke-open-server SERVER &optional ARGUMENT'
11604 ;; Open a connection to SERVER.
11605 ;;
11606 ;; `choke-close-server &optional SERVER'
11607 ;; Close the connection to SERVER.
11608 ;;
11609 ;; `choke-server-opened &optional SERVER'
11610 ;; Whether the conenction to SERVER is opened or not.
11611 ;;
11612 ;; `choke-server-status &optional SERVER'
11613 ;; Should return a status string (not in the nntp buffer, but as the
11614 ;; result of the function).
11615 ;;
11616 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11617 ;; Optional function for retrieving active file info on all groups in
11618 ;; GROUPS.  Two return formats are supported: The normal active file
11619 ;; format, and a list of GROUP lines.  This function should return (as
11620 ;; a function value) either `active' or `group', depending on what
11621 ;; format it returns.
11622 ;;
11623 ;; The following functions are optional and apply only to backends
11624 ;; that are able to control the contents of their groups totally
11625 ;; (ie. mail backends.)  Backends that aren't able to do that
11626 ;; shouldn't define these functions at all. Gnus will check for their
11627 ;; presence before attempting to call them.
11628 ;;
11629 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11630 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11631 ;; backends will not be able to expire articles. Should return a list
11632 ;; of all articles that were not expired.
11633 ;;
11634 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11635 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11636 ;; Removes any information it has added to the article (extra headers,
11637 ;; whatever - make it as clean as possible), and then passes the
11638 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11639 ;; function described below. If the ACCEPT-FORM returns a non-nil
11640 ;; value, the article should then be deleted. If LAST is nil, that
11641 ;; means that there will be further calls to this function. This might
11642 ;; be taken as an advice not to save buffers/internal variables just
11643 ;; yet, but wait until the last call to speed things up.
11644 ;;
11645 ;; `choke-request-accept-article GROUP &optional LAST' 
11646 ;; The contents of the current buffer will be put into GROUP.  There
11647 ;; should, of course, be an article in the current buffer.  This
11648 ;; function is normally only called by the function described above,
11649 ;; and LAST works the same way as in that function.
11650 ;;
11651 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11652 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11653 ;; This provides an easy interface for allowing editing of
11654 ;; articles. Note that even headers may be edited, so the backend has
11655 ;; to update any tables (nov buffers, etc) that it maintains after
11656 ;; replacing the article.
11657 ;;
11658 ;; `choke-request-create-group GROUP &optional SERVER'
11659 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11660 ;; might be a group that already exists, but hasn't been registered
11661 ;; yet. 
11662 ;;
11663 ;; All these functions must return nil if they couldn't service the
11664 ;; request. If the optional arguments are not supplied, some "current"
11665 ;; or "default" values should be used. In short, one should emulate an
11666 ;; NNTP server, in a way.
11667 ;;
11668 ;; If you want to write a new backend, you just have to supply the
11669 ;; functions listed above. In addition, you must enter the new backend
11670 ;; into the list of valid select methods:
11671 ;; (setq gnus-valid-select-methods 
11672 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11673 ;; The first element in this list is the name of the backend. Other
11674 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11675 ;; groups), `none' (neither), `respool' (for groups that can control
11676 ;; their contents). 
11677
11678 (defun gnus-start-news-server (&optional confirm)
11679   "Open a method for getting news.
11680 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11681   (let (how)
11682     (if gnus-current-select-method
11683         ;; Stream is already opened.
11684         nil
11685       ;; Open NNTP server.
11686       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11687       (if confirm
11688           (progn
11689             ;; Read server name with completion.
11690             (setq gnus-nntp-server
11691                   (completing-read "NNTP server: "
11692                                    (mapcar (lambda (server) (list server))
11693                                            (cons (list gnus-nntp-server)
11694                                                  gnus-secondary-servers))
11695                                    nil nil gnus-nntp-server))))
11696
11697       (if (and gnus-nntp-server 
11698                (stringp gnus-nntp-server)
11699                (not (string= gnus-nntp-server "")))
11700           (setq gnus-select-method
11701                 (cond ((or (string= gnus-nntp-server "")
11702                            (string= gnus-nntp-server "::"))
11703                        (list 'nnspool (system-name)))
11704                       ((string-match "^:" gnus-nntp-server)
11705                        (list 'nnmh gnus-nntp-server 
11706                              (list 'nnmh-directory 
11707                                    (file-name-as-directory
11708                                     (expand-file-name
11709                                      (concat "~/" (substring
11710                                                    gnus-nntp-server 1)))))
11711                              (list 'nnmh-get-new-mail nil)))
11712                       (t
11713                        (list 'nntp gnus-nntp-server)))))
11714
11715       (setq how (car gnus-select-method))
11716       (cond ((eq how 'nnspool)
11717              (require 'nnspool)
11718              (gnus-message 5 "Looking up local news spool..."))
11719             ((eq how 'nnmh)
11720              (require 'nnmh)
11721              (gnus-message 5 "Looking up mh spool..."))
11722             (t
11723              (require 'nntp)))
11724       (setq gnus-current-select-method gnus-select-method)
11725       (run-hooks 'gnus-open-server-hook)
11726       (or 
11727        ;; gnus-open-server-hook might have opened it
11728        (gnus-server-opened gnus-select-method)  
11729        (gnus-open-server gnus-select-method)
11730        (gnus-y-or-n-p
11731         (format
11732          "%s server on %s can't be opened. Continue? "
11733          (car gnus-select-method) (nth 1 gnus-select-method)))
11734        (progn
11735          (gnus-message 1 "Couldn't open server on %s" 
11736                        (nth 1 gnus-select-method))
11737          (ding)
11738          nil)))))
11739
11740 (defun gnus-check-server (&optional method)
11741   "If the news server is down, start it up again."
11742   (let ((method (if method method gnus-select-method)))
11743     (and (stringp method)
11744          (setq method (gnus-server-to-method method)))
11745     (if (gnus-server-opened method)
11746         ;; Stream is already opened.
11747         t
11748       ;; Open server.
11749       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11750       (run-hooks 'gnus-open-server-hook)
11751       (prog1
11752           (gnus-open-server method)
11753         (message "")))))
11754
11755 (defun gnus-nntp-message (&optional message)
11756   "Check the status of the NNTP server.
11757 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11758 is returned insted of the status string."
11759   (let ((status (gnus-status-message (gnus-find-method-for-group 
11760                                       gnus-newsgroup-name)))
11761         (message (or message "")))
11762     (if (and (stringp status) (> (length status) 0))
11763         status message)))
11764
11765 (defun gnus-get-function (method function)
11766   (and (stringp method)
11767        (setq method (gnus-server-to-method method)))
11768   (let ((func (intern (format "%s-%s" (car method) function))))
11769     (if (not (fboundp func)) 
11770         (progn
11771           (require (car method))
11772           (if (not (fboundp func)) 
11773               (error "No such function: %s" func))))
11774     func))
11775
11776 ;;; Interface functions to the backends.
11777
11778 (defun gnus-open-server (method)
11779   (funcall (gnus-get-function method 'open-server)
11780            (nth 1 method) (nthcdr 2 method)))
11781
11782 (defun gnus-close-server (method)
11783   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11784
11785 (defun gnus-request-list (method)
11786   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11787
11788 (defun gnus-request-list-newsgroups (method)
11789   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11790
11791 (defun gnus-request-newgroups (date method)
11792   (funcall (gnus-get-function method 'request-newgroups) 
11793            date (nth 1 method)))
11794
11795 (defun gnus-server-opened (method)
11796   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11797
11798 (defun gnus-status-message (method)
11799   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11800                   method)))
11801     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11802
11803 (defun gnus-request-group (group &optional dont-check)
11804   (let ((method (gnus-find-method-for-group group)))
11805 ;    (and t (message "%s GROUP %s" (car method) group))
11806     (funcall (gnus-get-function method 'request-group) 
11807              (gnus-group-real-name group) (nth 1 method) dont-check)))
11808
11809 (defun gnus-request-asynchronous (group &optional articles)
11810   (let ((method (gnus-find-method-for-group group)))
11811     (funcall (gnus-get-function method 'request-asynchronous) 
11812              (gnus-group-real-name group) (nth 1 method) articles)))
11813
11814 (defun gnus-list-active-group (group)
11815   (let ((method (gnus-find-method-for-group group))
11816         (func 'list-active-group))
11817     (and (gnus-check-backend-function func group)
11818          (funcall (gnus-get-function method func) 
11819                   (gnus-group-real-name group) (nth 1 method)))))
11820
11821 (defun gnus-request-group-description (group)
11822   (let ((method (gnus-find-method-for-group group))
11823         (func 'request-group-description))
11824     (and (gnus-check-backend-function func group)
11825          (funcall (gnus-get-function method func) 
11826                   (gnus-group-real-name group) (nth 1 method)))))
11827
11828 (defun gnus-close-group (group)
11829   (let ((method (gnus-find-method-for-group group)))
11830     (funcall (gnus-get-function method 'close-group) 
11831              (gnus-group-real-name group) (nth 1 method))))
11832
11833 (defun gnus-retrieve-headers (articles group)
11834   (let ((method (gnus-find-method-for-group group)))
11835     (if (and gnus-use-cache (numberp (car articles)))
11836         (gnus-cache-retrieve-headers articles group)
11837       (funcall (gnus-get-function method 'retrieve-headers) 
11838                articles (gnus-group-real-name group) (nth 1 method)))))
11839
11840 (defun gnus-retrieve-groups (groups method)
11841   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11842
11843 (defun gnus-request-article (article group &optional buffer)
11844   (let ((method (gnus-find-method-for-group group)))
11845     (funcall (gnus-get-function method 'request-article) 
11846              article (gnus-group-real-name group) (nth 1 method) buffer)))
11847
11848 (defun gnus-request-head (article group)
11849   (let ((method (gnus-find-method-for-group group)))
11850     (funcall (gnus-get-function method 'request-head) 
11851              article (gnus-group-real-name group) (nth 1 method))))
11852
11853 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11854 (defun gnus-request-post-buffer (post group subject header artbuf
11855                                       info follow-to respect-poster)
11856    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11857                                             group gnus-newsrc-hashtb)))))
11858           (method
11859            (if (and gnus-post-method
11860                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11861                     (memq 'post (assoc
11862                                  (format "%s" (car (gnus-find-method-for-group
11863                                                     gnus-newsgroup-name)))
11864                                         gnus-valid-select-methods)))
11865                gnus-post-method
11866              (gnus-find-method-for-group gnus-newsgroup-name))))
11867      (or (gnus-check-server method)
11868          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11869      (let ((mail-self-blind nil)
11870            (mail-archive-file-name nil))
11871        (funcall (gnus-get-function method 'request-post-buffer) 
11872                 post group subject header artbuf info follow-to
11873                 respect-poster))))
11874
11875 (defun gnus-request-post (method &optional force)
11876   (and (stringp method)
11877        (setq method (gnus-server-to-method method)))
11878   (and (not force) gnus-post-method
11879        (memq 'post (assoc (format "%s" (car method))
11880                           gnus-valid-select-methods))
11881        (setq method gnus-post-method))
11882   (funcall (gnus-get-function method 'request-post) 
11883            (nth 1 method)))
11884
11885 (defun gnus-request-expire-articles (articles group &optional force)
11886   (let ((method (gnus-find-method-for-group group)))
11887     (funcall (gnus-get-function method 'request-expire-articles) 
11888              articles (gnus-group-real-name group) (nth 1 method)
11889              force)))
11890
11891 (defun gnus-request-move-article 
11892   (article group server accept-function &optional last)
11893   (let ((method (gnus-find-method-for-group group)))
11894     (funcall (gnus-get-function method 'request-move-article) 
11895              article (gnus-group-real-name group) 
11896              (nth 1 method) accept-function last)))
11897
11898 (defun gnus-request-accept-article (group &optional last)
11899   (let ((func (if (symbolp group) group
11900                 (car (gnus-find-method-for-group group)))))
11901     (funcall (intern (format "%s-request-accept-article" func))
11902              (if (stringp group) (gnus-group-real-name group) group)
11903              last)))
11904
11905 (defun gnus-request-replace-article (article group buffer)
11906   (let ((func (car (gnus-find-method-for-group group))))
11907     (funcall (intern (format "%s-request-replace-article" func))
11908              article (gnus-group-real-name group) buffer)))
11909
11910 (defun gnus-request-create-group (group)
11911   (let ((method (gnus-find-method-for-group group)))
11912     (funcall (gnus-get-function method 'request-create-group) 
11913              (gnus-group-real-name group) (nth 1 method))))
11914
11915 (defun gnus-member-of-valid (symbol group)
11916   (memq symbol (assoc
11917                 (format "%s" (car (gnus-find-method-for-group group)))
11918                 gnus-valid-select-methods)))
11919
11920 (defun gnus-secondary-method-p (method)
11921   (let ((methods gnus-secondary-select-methods)
11922         (gmethod (gnus-server-get-method nil method)))
11923     (while (and methods
11924                 (not (equal (gnus-server-get-method nil (car methods)) 
11925                             gmethod)))
11926       (setq methods (cdr methods)))
11927     methods))
11928
11929 (defun gnus-find-method-for-group (group &optional info)
11930   (or gnus-override-method
11931       (and (not group)
11932            gnus-select-method)
11933       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11934             method)
11935         (if (or (not info)
11936                 (not (setq method (nth 4 info))))
11937             (setq method gnus-select-method)
11938           (setq method
11939                 (cond ((stringp method)
11940                        (gnus-server-to-method method))
11941                       ((stringp (car method))
11942                        (gnus-server-extend-method group method))
11943                       (t
11944                        method))))
11945         (gnus-server-add-address method))))
11946
11947 (defun gnus-check-backend-function (func group)
11948   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11949                  group)))
11950     (fboundp (intern (format "%s-%s" method func)))))
11951
11952 (defun gnus-methods-using (method)
11953   (let ((valids gnus-valid-select-methods)
11954         outs)
11955     (while valids
11956       (if (memq method (car valids)) 
11957           (setq outs (cons (car valids) outs)))
11958       (setq valids (cdr valids)))
11959     outs))
11960
11961 ;;; 
11962 ;;; Active & Newsrc File Handling
11963 ;;;
11964
11965 ;; Newsrc related functions.
11966 ;; Gnus internal format of gnus-newsrc-alist:
11967 ;; (("alt.general" 3 (1 . 1))
11968 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11969 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11970 ;; The first item is the group name; the second is the subscription
11971 ;; level; the third is either a range of a list of ranges of read
11972 ;; articles, the optional fourth element is a list of marked articles,
11973 ;; the optional fifth element is the select method.
11974 ;;
11975 ;; Gnus internal format of gnus-newsrc-hashtb:
11976 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11977 ;; This is the entry for "alt.misc". The first element is the number
11978 ;; of unread articles in "alt.misc". The cdr of this entry is the
11979 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11980 ;; trivial to remove or add new elements into gnus-newsrc-alist
11981 ;; without scanning the entire list. So, to get the actual information
11982 ;; of "alt.misc", you'd say something like 
11983 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11984 ;;
11985 ;; Gnus internal format of gnus-active-hashtb:
11986 ;; ((1 . 1))
11987 ;;  (5 . 10))
11988 ;;  (67 . 99)) ...)
11989 ;; The only element in each entry in this hash table is a range of
11990 ;; (possibly) available articles. (Articles in this range may have
11991 ;; been expired or canceled.)
11992 ;;
11993 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11994 ;; ("alt.misc" "alt.test" "alt.general" ...)
11995
11996 (defun gnus-setup-news (&optional rawfile level)
11997   "Setup news information.
11998 If RAWFILE is non-nil, the .newsrc file will also be read.
11999 If LEVEL is non-nil, the news will be set up at level LEVEL."
12000   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
12001     ;; Clear some variables to re-initialize news information.
12002     (if init (setq gnus-newsrc-alist nil 
12003                    gnus-active-hashtb nil))
12004
12005     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12006     (if init (gnus-read-newsrc-file rawfile))
12007
12008     ;; If we don't read the complete active file, we fill in the
12009     ;; hashtb here. 
12010     (if (or (null gnus-read-active-file)
12011             (eq gnus-read-active-file 'some))
12012         (gnus-update-active-hashtb-from-killed))
12013
12014     ;; Read the active file and create `gnus-active-hashtb'.
12015     ;; If `gnus-read-active-file' is nil, then we just create an empty
12016     ;; hash table. The partial filling out of the hash table will be
12017     ;; done in `gnus-get-unread-articles'.
12018     (and gnus-read-active-file 
12019          (not level)
12020          (gnus-read-active-file))
12021
12022     ;; Possibly eval the dribble file.
12023     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
12024
12025     (gnus-update-format-specifications)
12026
12027     ;; Find the number of unread articles in each non-dead group.
12028     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12029       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12030     ;; Find new newsgroups and treat them.
12031     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12032              (gnus-server-opened gnus-select-method))
12033         (gnus-find-new-newsgroups))
12034     (if (and init gnus-check-bogus-newsgroups 
12035              gnus-read-active-file (not level)
12036              (gnus-server-opened gnus-select-method))
12037         (gnus-check-bogus-newsgroups))))
12038
12039 (defun gnus-find-new-newsgroups ()
12040   "Search for new newsgroups and add them.
12041 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12042 The `-n' option line from .newsrc is respected."
12043   (interactive)
12044   (or (gnus-check-first-time-used)
12045       (if (or (consp gnus-check-new-newsgroups)
12046               (eq gnus-check-new-newsgroups 'ask-server))
12047           (gnus-ask-server-for-new-groups)
12048         (let ((groups 0)
12049               group new-newsgroups)
12050           (gnus-message 5 "Checking for new newsgroups...")
12051           (or gnus-have-read-active-file (gnus-read-active-file))
12052           (setq gnus-newsrc-last-checked-date (current-time-string))
12053           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12054           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12055           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12056           (mapatoms
12057            (lambda (sym)
12058              (if (or (null (setq group (symbol-name sym)))
12059                      (null (symbol-value sym))
12060                      (gnus-gethash group gnus-killed-hashtb)
12061                      (gnus-gethash group gnus-newsrc-hashtb))
12062                  ()
12063                (let ((do-sub (gnus-matches-options-n group)))
12064                  (cond 
12065                   ((eq do-sub 'subscribe)
12066                    (setq groups (1+ groups))
12067                    (gnus-sethash group group gnus-killed-hashtb)
12068                    (funcall gnus-subscribe-options-newsgroup-method group))
12069                   ((eq do-sub 'ignore)
12070                    nil)
12071                   (t
12072                    (setq groups (1+ groups))
12073                    (gnus-sethash group group gnus-killed-hashtb)
12074                    (if gnus-subscribe-hierarchical-interactive
12075                        (setq new-newsgroups (cons group new-newsgroups))
12076                      (funcall gnus-subscribe-newsgroup-method group)))))))
12077            gnus-active-hashtb)
12078           (if new-newsgroups 
12079               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12080           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12081           (if (> groups 0)
12082               (gnus-message 6 "%d new newsgroup%s arrived." 
12083                             groups (if (> groups 1) "s have" " has"))
12084             (gnus-message 6 "No new newsgroups."))))))
12085
12086 (defun gnus-matches-options-n (group)
12087   ;; Returns `subscribe' if the group is to be uncoditionally
12088   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12089   ;; no match for the group.
12090
12091   ;; First we check the two user variables.
12092   (cond
12093    ((and gnus-options-subscribe
12094          (string-match gnus-options-subscribe group))
12095     'subscribe)
12096    ((and gnus-options-not-subscribe
12097          (string-match gnus-options-not-subscribe group))
12098     'ignore)
12099    ;; Then we go through the list that was retrieved from the .newsrc
12100    ;; file.  This list has elements on the form 
12101    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12102    ;; is in the reverse order of the options line) is returned.
12103    (t
12104     (let ((regs gnus-newsrc-options-n))
12105       (while (and regs
12106                   (not (string-match (car (car regs)) group)))
12107         (setq regs (cdr regs)))
12108       (and regs (cdr (car regs)))))))
12109
12110 (defun gnus-ask-server-for-new-groups ()
12111   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12112          (methods (cons gnus-select-method 
12113                         (append
12114                          (and (consp gnus-check-new-newsgroups)
12115                               gnus-check-new-newsgroups)
12116                          gnus-secondary-select-methods)))
12117          (groups 0)
12118          (new-date (current-time-string))
12119          hashtb group new-newsgroups got-new method)
12120     ;; Go thorugh both primary and secondary select methods and
12121     ;; request new newsgroups.  
12122     (while methods
12123       (setq method (gnus-server-get-method nil (car methods)))
12124       (and (gnus-check-server method)
12125            (gnus-request-newgroups date method)
12126            (save-excursion
12127              (setq got-new t)
12128              (set-buffer nntp-server-buffer)
12129              (or hashtb (setq hashtb (gnus-make-hashtable 
12130                                       (count-lines (point-min) (point-max)))))
12131              ;; Enter all the new groups in a hashtable.
12132              (gnus-active-to-gnus-format method hashtb 'ignore)))
12133       (setq methods (cdr methods)))
12134     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12135     ;; Now all new groups from all select methods are in `hashtb'.
12136     (mapatoms
12137      (lambda (group-sym)
12138        (setq group (symbol-name group-sym))
12139        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12140                (member group gnus-zombie-list)
12141                (member group gnus-killed-list))
12142            ;; The group is already known.
12143            ()
12144          (and (symbol-value group-sym)
12145               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12146          (let ((do-sub (gnus-matches-options-n group)))
12147            (cond ((eq do-sub 'subscribe)
12148                   (setq groups (1+ groups))
12149                   (gnus-sethash group group gnus-killed-hashtb)
12150                   (funcall 
12151                    gnus-subscribe-options-newsgroup-method group))
12152                  ((eq do-sub 'ignore)
12153                   nil)
12154                  (t
12155                   (setq groups (1+ groups))
12156                   (gnus-sethash group group gnus-killed-hashtb)
12157                   (if gnus-subscribe-hierarchical-interactive
12158                       (setq new-newsgroups (cons group new-newsgroups))
12159                     (funcall gnus-subscribe-newsgroup-method group)))))))
12160      hashtb)
12161     (if new-newsgroups 
12162         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12163     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12164     (if (> groups 0)
12165         (gnus-message 6 "%d new newsgroup%s arrived." 
12166                       groups (if (> groups 1) "s have" " has")))
12167     got-new))
12168
12169 (defun gnus-check-first-time-used ()
12170   (if (or (> (length gnus-newsrc-alist) 1)
12171           (file-exists-p gnus-startup-file)
12172           (file-exists-p (concat gnus-startup-file ".el"))
12173           (file-exists-p (concat gnus-startup-file ".eld")))
12174       nil
12175     (gnus-message 6 "First time user; subscribing you to default groups")
12176     (or gnus-have-read-active-file (gnus-read-active-file))
12177     (setq gnus-newsrc-last-checked-date (current-time-string))
12178     (let ((groups gnus-default-subscribed-newsgroups)
12179           group)
12180       (if (eq groups t)
12181           nil
12182         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12183         (mapatoms
12184          (lambda (sym)
12185            (if (null (setq group (symbol-name sym)))
12186                ()
12187              (let ((do-sub (gnus-matches-options-n group)))
12188                (cond 
12189                 ((eq do-sub 'subscribe)
12190                  (gnus-sethash group group gnus-killed-hashtb)
12191                  (funcall gnus-subscribe-options-newsgroup-method group))
12192                 ((eq do-sub 'ignore)
12193                  nil)
12194                 (t
12195                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12196          gnus-active-hashtb)
12197         (while groups
12198           (if (gnus-gethash (car groups) gnus-active-hashtb)
12199               (gnus-group-change-level 
12200                (car groups) gnus-level-default-subscribed gnus-level-killed))
12201           (setq groups (cdr groups)))
12202         (gnus-group-make-help-group)
12203         (and gnus-novice-user
12204              (gnus-message 7 "`A k' to list killed groups"))))))
12205
12206 (defun gnus-subscribe-group (group previous &optional method)
12207   (gnus-group-change-level 
12208    (if method
12209        (list t group gnus-level-default-subscribed nil nil method)
12210      group) 
12211    gnus-level-default-subscribed gnus-level-killed previous t))
12212
12213 ;; `gnus-group-change-level' is the fundamental function for changing
12214 ;; subscription levels of newsgroups. This might mean just changing
12215 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12216 ;; again, which subscribes/unsubscribes a group, which is equally
12217 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12218 ;; from 8-9 to 1-7 means that you remove the group from the list of
12219 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12220 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12221 ;; which is trivial.
12222 ;; ENTRY can either be a string (newsgroup name) or a list (if
12223 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12224 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12225 ;; entries. 
12226 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12227 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12228 ;; after. 
12229 (defun gnus-group-change-level (entry level &optional oldlevel
12230                                       previous fromkilled)
12231   (let (group info active num)
12232     ;; Glean what info we can from the arguments
12233     (if (consp entry)
12234         (if fromkilled (setq group (nth 1 entry))
12235           (setq group (car (nth 2 entry))))
12236       (setq group entry))
12237     (if (and (stringp entry)
12238              oldlevel 
12239              (< oldlevel gnus-level-zombie))
12240         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12241     (if (and (not oldlevel)
12242              (consp entry))
12243         (setq oldlevel (car (cdr (nth 2 entry)))))
12244     (if (stringp previous)
12245         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12246
12247     (if (and (>= oldlevel gnus-level-zombie)
12248              (gnus-gethash group gnus-newsrc-hashtb))
12249         ;; We are trying to subscribe a group that is already
12250         ;; subscribed. 
12251         () ; Do nothing. 
12252
12253       (gnus-dribble-enter
12254        (format "(gnus-group-change-level %S %S %S %S %S)" 
12255                group level oldlevel (car (nth 2 previous)) fromkilled))
12256     
12257       ;; Then we remove the newgroup from any old structures, if needed.
12258       ;; If the group was killed, we remove it from the killed or zombie
12259       ;; list. If not, and it is in fact going to be killed, we remove
12260       ;; it from the newsrc hash table and assoc.
12261       (cond ((>= oldlevel gnus-level-zombie)
12262              (if (= oldlevel gnus-level-zombie)
12263                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12264                (setq gnus-killed-list (delete group gnus-killed-list))))
12265             (t
12266              (if (and (>= level gnus-level-zombie)
12267                       entry)
12268                  (progn
12269                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12270                    (if (nth 3 entry)
12271                        (setcdr (gnus-gethash (car (nth 3 entry))
12272                                              gnus-newsrc-hashtb)
12273                                (cdr entry)))
12274                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12275
12276       ;; Finally we enter (if needed) the list where it is supposed to
12277       ;; go, and change the subscription level. If it is to be killed,
12278       ;; we enter it into the killed or zombie list.
12279       (cond ((>= level gnus-level-zombie)
12280              ;; Remove from the hash table.
12281              (gnus-sethash group nil gnus-newsrc-hashtb)
12282              (or (gnus-group-foreign-p group)
12283                  ;; We do not enter foreign groups into the list of dead
12284                  ;; groups.  
12285                  (if (= level gnus-level-zombie)
12286                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12287                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12288             (t
12289              ;; If the list is to be entered into the newsrc assoc, and
12290              ;; it was killed, we have to create an entry in the newsrc
12291              ;; hashtb format and fix the pointers in the newsrc assoc.
12292              (if (>= oldlevel gnus-level-zombie)
12293                  (progn
12294                    (if (listp entry)
12295                        (progn
12296                          (setq info (cdr entry))
12297                          (setq num (car entry)))
12298                      (setq active (gnus-gethash group gnus-active-hashtb))
12299                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12300                      ;; Check whether the group is foreign. If so, the
12301                      ;; foreign select method has to be entered into the
12302                      ;; info. 
12303                      (let ((method (gnus-group-method-name group)))
12304                        (if (eq method gnus-select-method)
12305                            (setq info (list group level nil))
12306                          (setq info (list group level nil nil method)))))
12307                    (or previous 
12308                        (setq previous 
12309                              (let ((p gnus-newsrc-alist))
12310                                (while (cdr (cdr p))
12311                                  (setq p (cdr p)))
12312                                p)))
12313                    (setq entry (cons info (cdr (cdr previous))))
12314                    (if (cdr previous)
12315                        (progn
12316                          (setcdr (cdr previous) entry)
12317                          (gnus-sethash group (cons num (cdr previous)) 
12318                                        gnus-newsrc-hashtb))
12319                      (setcdr previous entry)
12320                      (gnus-sethash group (cons num previous)
12321                                    gnus-newsrc-hashtb))
12322                    (if (cdr entry)
12323                        (setcdr (gnus-gethash (car (car (cdr entry)))
12324                                              gnus-newsrc-hashtb)
12325                                entry)))
12326                ;; It was alive, and it is going to stay alive, so we
12327                ;; just change the level and don't change any pointers or
12328                ;; hash table entries.
12329                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12330
12331 (defun gnus-kill-newsgroup (newsgroup)
12332   "Obsolete function. Kills a newsgroup."
12333   (gnus-group-change-level
12334    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12335
12336 (defun gnus-check-bogus-newsgroups (&optional confirm)
12337   "Remove bogus newsgroups.
12338 If CONFIRM is non-nil, the user has to confirm the deletion of every
12339 newsgroup." 
12340   (let ((newsrc (cdr gnus-newsrc-alist))
12341         bogus group entry)
12342     (gnus-message 5 "Checking bogus newsgroups...")
12343     (or gnus-have-read-active-file (gnus-read-active-file))
12344     ;; Find all bogus newsgroup that are subscribed.
12345     (while newsrc
12346       (setq group (car (car newsrc)))
12347       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12348               (nth 4 (car newsrc))      ; Foreign
12349               (and confirm
12350                    (not (gnus-y-or-n-p
12351                          (format "Remove bogus newsgroup: %s " group)))))
12352           ;; Don't remove.
12353           ()
12354         ;; Found a bogus newsgroup.
12355         (setq bogus (cons group bogus)))
12356       (setq newsrc (cdr newsrc)))
12357     ;; Remove all bogus subscribed groups by first killing them, and
12358     ;; then removing them from the list of killed groups.
12359     (while bogus
12360       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12361            (progn
12362              (gnus-group-change-level entry gnus-level-killed)
12363              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12364       (setq bogus (cdr bogus)))
12365     ;; Then we remove all bogus groups from the list of killed and
12366     ;; zombie groups. They are are removed without confirmation.
12367     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12368           killed)
12369       (while dead-lists
12370         (setq killed (symbol-value (car dead-lists)))
12371         (while killed
12372           (setq group (car killed))
12373           (or (gnus-gethash group gnus-active-hashtb)
12374               ;; The group is bogus.
12375               (set (car dead-lists)
12376                    (delete group (symbol-value (car dead-lists)))))
12377           (setq killed (cdr killed)))
12378         (setq dead-lists (cdr dead-lists))))
12379     (gnus-message 5 "Checking bogus newsgroups...done")))
12380
12381 (defun gnus-check-duplicate-killed-groups ()
12382   "Remove duplicates from the list of killed groups."
12383   (interactive)
12384   (let ((killed gnus-killed-list))
12385     (while killed
12386       (gnus-message 9 "%d" (length killed))
12387       (setcdr killed (delete (car killed) (cdr killed)))
12388       (setq killed (cdr killed)))))
12389
12390 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12391 ;; and compute how many unread articles there are in each group.
12392 (defun gnus-get-unread-articles (&optional level) 
12393   (let* ((newsrc (cdr gnus-newsrc-alist))
12394          (level (or level (1+ gnus-level-subscribed)))
12395          (foreign-level
12396           (min 
12397            (cond ((and gnus-activate-foreign-newsgroups 
12398                        (not (numberp gnus-activate-foreign-newsgroups)))
12399                   (1+ gnus-level-subscribed))
12400                  ((numberp gnus-activate-foreign-newsgroups)
12401                   gnus-activate-foreign-newsgroups)
12402                  (t 0))
12403            level))
12404          info group active virtuals method)
12405     (gnus-message 5 "Checking new news...")
12406
12407     (while newsrc
12408       (setq info (car newsrc)
12409             group (car info)
12410             active (gnus-gethash group gnus-active-hashtb))
12411
12412       ;; Check newsgroups. If the user doesn't want to check them, or
12413       ;; they can't be checked (for instance, if the news server can't
12414       ;; be reached) we just set the number of unread articles in this
12415       ;; newsgroup to t. This means that Gnus thinks that there are
12416       ;; unread articles, but it has no idea how many.
12417       (if (and (setq method (nth 4 info))
12418                (not (gnus-server-equal gnus-select-method
12419                                        (gnus-server-get-method nil method)))
12420                (not (gnus-secondary-method-p method)))
12421           ;; These groups are foreign. Check the level.
12422           (if (<= (nth 1 info) foreign-level)
12423               (if (eq (car (if (stringp method) 
12424                                (gnus-server-to-method method)
12425                              (nth 4 info))) 'nnvirtual)
12426                   ;; We have to activate the virtual groups after all
12427                   ;; the others, so we just pop them on a list for
12428                   ;; now. 
12429                   (setq virtuals (cons info virtuals))
12430                 (and (setq active (gnus-activate-group (car info)))
12431                      ;; Close the groups as we look at them!
12432                      (gnus-close-group group))))
12433
12434         (or gnus-read-active-file (gnus-check-server method))
12435         ;; These groups are native or secondary. 
12436         (if (and (not gnus-read-active-file)
12437                  (<= (nth 1 info) level))
12438             (setq active (gnus-activate-group (car info)))))
12439       
12440       (if active
12441           (gnus-get-unread-articles-in-group info active)
12442         ;; The group couldn't be reached, so we nix out the number of
12443         ;; unread articles and stuff.
12444         (gnus-sethash group nil gnus-active-hashtb)
12445         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12446
12447       (setq newsrc (cdr newsrc)))
12448
12449     ;; Activate the virtual groups. This has to be done after all the
12450     ;; other groups. 
12451     ;; !!! If one virtual group contains another virtual group, even
12452     ;; doing it this way might cause problems.
12453     (while virtuals
12454       (and (setq active (gnus-activate-group (car (car virtuals))))
12455            (gnus-get-unread-articles-in-group (car virtuals) active))
12456       (setq virtuals (cdr virtuals)))
12457
12458     (gnus-message 5 "Checking new news...done")))
12459
12460 ;; Create a hash table out of the newsrc alist. The `car's of the
12461 ;; alist elements are used as keys.
12462 (defun gnus-make-hashtable-from-newsrc-alist ()
12463   (let ((alist gnus-newsrc-alist)
12464         (ohashtb gnus-newsrc-hashtb)
12465         prev)
12466     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12467     (setq alist 
12468           (setq prev (setq gnus-newsrc-alist 
12469                            (if (equal (car (car gnus-newsrc-alist))
12470                                       "dummy.group")
12471                                gnus-newsrc-alist
12472                              (cons (list "dummy.group" 0 nil) alist)))))
12473     (while alist
12474       (gnus-sethash (car (car alist)) 
12475                     (cons (and ohashtb (car (gnus-gethash 
12476                                              (car (car alist)) ohashtb))) 
12477                           prev) gnus-newsrc-hashtb)
12478       (setq prev alist
12479             alist (cdr alist)))))
12480
12481 (defun gnus-make-hashtable-from-killed ()
12482   "Create a hash table from the killed and zombie lists."
12483   (let ((lists '(gnus-killed-list gnus-zombie-list))
12484         list)
12485     (setq gnus-killed-hashtb 
12486           (gnus-make-hashtable 
12487            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12488     (while lists
12489       (setq list (symbol-value (car lists)))
12490       (setq lists (cdr lists))
12491       (while list
12492         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12493         (setq list (cdr list))))))
12494
12495 (defun gnus-get-unread-articles-in-group (info active)
12496   (let* ((range (nth 2 info))
12497          (num 0)
12498          (marked (nth 3 info)))
12499     ;; If a cache is present, we may have to alter the active info.
12500     (and gnus-use-cache
12501          (gnus-cache-possibly-alter-active (car info) active))
12502     ;; Modify the list of read articles according to what articles 
12503     ;; are available; then tally the unread articles and add the
12504     ;; number to the group hash table entry.
12505     (cond 
12506      ((zerop (cdr active))
12507       (setq num 0))
12508      ((not range)
12509       (setq num (- (1+ (cdr active)) (car active))))
12510      ((not (listp (cdr range)))
12511       ;; Fix a single (num . num) range according to the
12512       ;; active hash table.
12513       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12514       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12515       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12516       ;; Compute number of unread articles.
12517       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12518      (t
12519       ;; The read list is a list of ranges. Fix them according to
12520       ;; the active hash table.
12521       ;; First peel off any elements that are below the lower
12522       ;; active limit. 
12523       (while (and (cdr range) 
12524                   (>= (car active) 
12525                       (or (and (atom (car (cdr range))) (car (cdr range)))
12526                           (car (car (cdr range))))))
12527         (if (numberp (car range))
12528             (setcar range 
12529                     (cons (car range) 
12530                           (or (and (numberp (car (cdr range)))
12531                                    (car (cdr range))) 
12532                               (cdr (car (cdr range))))))
12533           (setcdr (car range) 
12534                   (or (and (numberp (nth 1 range)) (nth 1 range))
12535                       (cdr (car (cdr range))))))
12536         (setcdr range (cdr (cdr range))))
12537       ;; Adjust the first element to be the same as the lower limit. 
12538       (if (and (not (atom (car range))) 
12539                (< (cdr (car range)) (car active)))
12540           (setcdr (car range) (1- (car active))))
12541       ;; Then we want to peel off any elements that are higher
12542       ;; than the upper active limit.  
12543       (let ((srange range))
12544         ;; Go past all legal elements.
12545         (while (and (cdr srange) 
12546                     (<= (or (and (atom (car (cdr srange)))
12547                                  (car (cdr srange)))
12548                             (car (car (cdr srange)))) (cdr active)))
12549           (setq srange (cdr srange)))
12550         (if (cdr srange)
12551             ;; Nuke all remaining illegal elements.
12552             (setcdr srange nil))
12553
12554         ;; Adjust the final element.
12555         (if (and (not (atom (car srange)))
12556                  (> (cdr (car srange)) (cdr active)))
12557             (setcdr (car srange) (cdr active))))
12558       ;; Compute the number of unread articles.
12559       (while range
12560         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12561                                     (cdr (car range))))
12562                             (or (and (atom (car range)) (car range))
12563                                 (car (car range))))))
12564         (setq range (cdr range)))
12565       (setq num (max 0 (- (cdr active) num)))))
12566     (and info
12567          (progn
12568            (and (assq 'tick marked)
12569                 (inline (gnus-remove-illegal-marked-articles
12570                          (assq 'tick marked) (nth 2 info))))
12571            (and (assq 'dormant marked)
12572                 (inline (gnus-remove-illegal-marked-articles
12573                          (assq 'dormant marked) (nth 2 info))))
12574            (setcar
12575             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12576             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12577                                 (length (cdr (assq 'dormant marked)))))))))
12578     num))
12579
12580 (defun gnus-remove-illegal-marked-articles (marked ranges)
12581   (let ((m (cdr marked)))
12582     ;; Make sure that all ticked articles are a subset of the unread
12583     ;; articles. 
12584     (while m
12585       (if (gnus-member-of-range (car m) ranges)
12586           (setcdr marked (cdr m))
12587         (setq marked m))
12588       (setq m (cdr m)))))
12589
12590 (defun gnus-activate-group (group)
12591   ;; Check whether a group has been activated or not.
12592   (let ((method (gnus-find-method-for-group group))
12593         active)
12594     (and (gnus-check-server method)
12595          ;; We escape all bugs and quits here to make it possible to
12596          ;; continue if a group is so out-there that it reports bugs
12597          ;; and stuff.
12598          (condition-case ()
12599              (gnus-request-group group)
12600            (error nil)
12601            (quit nil))
12602          (save-excursion
12603            (set-buffer nntp-server-buffer)
12604            (goto-char (point-min))
12605            ;; Parse the result we got from `gnus-request-group'.
12606            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12607                 (progn
12608                   (goto-char (match-beginning 1))
12609                   (gnus-sethash 
12610                    group (setq active (cons (read (current-buffer))
12611                                             (read (current-buffer))))
12612                    gnus-active-hashtb))
12613                 ;; Return the new active info.
12614                 active)))))
12615
12616 (defun gnus-update-read-articles 
12617   (group unread unselected ticked &optional domarks replied expirable killed
12618          dormant bookmark score)
12619   "Update the list of read and ticked articles in GROUP using the
12620 UNREAD and TICKED lists.
12621 Note: UNSELECTED has to be sorted over `<'.
12622 Returns whether the updating was successful."
12623   (let* ((active (or gnus-newsgroup-active 
12624                      (gnus-gethash group gnus-active-hashtb)))
12625          (entry (gnus-gethash group gnus-newsrc-hashtb))
12626          (info (nth 2 entry))
12627          (marked (nth 3 info))
12628          (prev 1)
12629          (unread (sort (copy-sequence unread) (function <)))
12630          read)
12631     (if (or (not info) (not active))
12632         ;; There is no info on this group if it was, in fact,
12633         ;; killed. Gnus stores no information on killed groups, so
12634         ;; there's nothing to be done. 
12635         ;; One could store the information somewhere temporarily,
12636         ;; perhaps... Hmmm... 
12637         ()
12638       ;; Remove any negative articles numbers.
12639       (while (and unread (< (car unread) 0))
12640         (setq unread (cdr unread)))
12641       ;; Remove any expired article numbers
12642       (while (and unread (< (car unread) (car active)))
12643         (setq unread (cdr unread)))
12644       (while (and ticked (< (car ticked) (car active)))
12645         (setq ticked (cdr ticked)))
12646       (while (and dormant (< (car dormant) (car active)))
12647         (setq dormant (cdr dormant)))
12648       (setq unread (sort (append unselected unread) '<))
12649       ;; Compute the ranges of read articles by looking at the list of
12650       ;; unread articles.  
12651       (while unread
12652         (if (/= (car unread) prev)
12653             (setq read (cons (if (= prev (1- (car unread))) prev
12654                                (cons prev (1- (car unread)))) read)))
12655         (setq prev (1+ (car unread)))
12656         (setq unread (cdr unread)))
12657       (if (<= prev (cdr active))
12658           (setq read (cons (cons prev (cdr active)) read)))
12659       ;; Enter this list into the group info.
12660       (setcar (cdr (cdr info)) 
12661               (if (> (length read) 1) (nreverse read) read))
12662       ;; Enter the list of ticked articles.
12663       (gnus-set-marked-articles 
12664        info ticked
12665        (if domarks replied (cdr (assq 'reply marked)))
12666        (if domarks expirable (cdr (assq 'expire marked)))
12667        (if domarks killed (cdr (assq 'killed marked)))
12668        (if domarks dormant (cdr (assq 'dormant marked)))
12669        (if domarks bookmark (cdr (assq 'bookmark marked)))
12670        (if domarks score (cdr (assq 'score marked))))
12671       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12672       (gnus-get-unread-articles-in-group 
12673        info (gnus-gethash group gnus-active-hashtb))
12674       t)))
12675
12676 (defun gnus-make-articles-unread (group articles)
12677   "Mark ARTICLES in GROUP as unread."
12678   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12679                           (gnus-gethash (gnus-group-real-name group)
12680                                         gnus-newsrc-hashtb))))
12681          (ranges (nth 2 info))
12682          news)
12683     (while articles
12684       (and (gnus-member-of-range (car articles) ranges)
12685            (setq news (cons (car articles) news)))
12686       (setq articles (cdr articles)))
12687     (if (not news)
12688         ()
12689       (setcar (nthcdr 2 info)
12690               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12691       (gnus-group-update-group group t))))
12692
12693 ;; Enter all dead groups into the hashtb.
12694 (defun gnus-update-active-hashtb-from-killed ()
12695   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12696         (lists (list gnus-killed-list gnus-zombie-list))
12697         killed)
12698     (while lists
12699       (setq killed (car lists))
12700       (while killed
12701         (gnus-sethash (car killed) nil hashtb)
12702         (setq killed (cdr killed)))
12703       (setq lists (cdr lists)))))
12704
12705 ;; Get the active file(s) from the backend(s).
12706 (defun gnus-read-active-file ()
12707   (gnus-group-set-mode-line)
12708   (let ((methods (if (gnus-check-server gnus-select-method)
12709                      ;; The native server is available.
12710                      (cons gnus-select-method gnus-secondary-select-methods)
12711                    ;; The native server is down, so we just do the
12712                    ;; secondary ones.   
12713                    gnus-secondary-select-methods))
12714         list-type)
12715     (setq gnus-have-read-active-file nil)
12716     (save-excursion
12717       (set-buffer nntp-server-buffer)
12718       (while methods
12719         (let* ((method (gnus-server-get-method nil (car methods)))
12720                (where (nth 1 method))
12721                (mesg (format "Reading active file%s via %s..."
12722                              (if (and where (not (zerop (length where))))
12723                                  (concat " from " where) "")
12724                              (car method))))
12725           (gnus-message 5 mesg)
12726           (gnus-check-server method)
12727           (cond 
12728            ((and (eq gnus-read-active-file 'some)
12729                  (gnus-check-backend-function 'retrieve-groups (car method)))
12730             (let ((newsrc (cdr gnus-newsrc-alist))
12731                   (gmethod (gnus-server-get-method nil method))
12732                   groups)
12733               (while newsrc
12734                 (and (gnus-server-equal 
12735                       (gnus-find-method-for-group 
12736                        (car (car newsrc)) (car newsrc))
12737                       gmethod)
12738                      (setq groups (cons (gnus-group-real-name 
12739                                          (car (car newsrc))) groups)))
12740                 (setq newsrc (cdr newsrc)))
12741               (gnus-check-server method)
12742               (setq list-type (gnus-retrieve-groups groups method))
12743               (cond ((not list-type)
12744                      (gnus-message 
12745                       1 "Cannot read partial active file from %s server." 
12746                       (car method))
12747                      (ding)
12748                      (sit-for 2))
12749                     ((eq list-type 'active)
12750                      (gnus-active-to-gnus-format method gnus-active-hashtb))
12751                     (t
12752                      (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12753            (t
12754             (if (not (gnus-request-list method))
12755                 (progn
12756                   (gnus-message 1 "Cannot read active file from %s server." 
12757                                 (car method))
12758                   (ding))
12759               (gnus-active-to-gnus-format method)
12760               ;; We mark this active file as read.
12761               (setq gnus-have-read-active-file
12762                     (cons method gnus-have-read-active-file))
12763               (gnus-message 5 "%sdone" mesg)))))
12764         (setq methods (cdr methods))))))
12765
12766 ;; Read an active file and place the results in `gnus-active-hashtb'.
12767 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12768   (let ((cur (current-buffer))
12769         (hashtb (or hashtb 
12770                     (if (and gnus-active-hashtb 
12771                              (not (equal method gnus-select-method)))
12772                         gnus-active-hashtb
12773                       (setq gnus-active-hashtb
12774                             (if (equal method gnus-select-method)
12775                                 (gnus-make-hashtable 
12776                                  (count-lines (point-min) (point-max)))
12777                               (gnus-make-hashtable 4096))))))
12778         (flag-hashtb (gnus-make-hashtable 60)))
12779     ;; Delete unnecessary lines.
12780     (goto-char (point-min))
12781     (while (search-forward "\nto." nil t)
12782       (delete-region (1+ (match-beginning 0)) 
12783                      (progn (forward-line 1) (point))))
12784     (or (string= gnus-ignored-newsgroups "")
12785         (progn
12786           (goto-char (point-min))
12787           (delete-matching-lines gnus-ignored-newsgroups)))
12788     ;; Make the group names readable as a lisp expression even if they
12789     ;; contain special characters.
12790     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12791     (goto-char (point-max))
12792     (while (re-search-backward "[][';?()#]" nil t)
12793       (insert ?\\))
12794     ;; If these are groups from a foreign select method, we insert the
12795     ;; group prefix in front of the group names. 
12796     (and method (not (gnus-server-equal
12797                       (gnus-server-get-method nil method)
12798                       (gnus-server-get-method nil gnus-select-method)))
12799          (let ((prefix (gnus-group-prefixed-name "" method)))
12800            (goto-char (point-min))
12801            (while (and (not (eobp))
12802                        (progn (insert prefix)
12803                               (zerop (forward-line 1)))))))
12804     ;; Store the active file in a hash table.
12805     (goto-char (point-min))
12806     (if (string-match "%[oO]" gnus-group-line-format)
12807         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12808         ;; If we want information on moderated groups, we use this
12809         ;; loop...   
12810         (let* ((mod-hashtb (make-vector 7 0))
12811                (m (intern "m" mod-hashtb))
12812                group max min)
12813           (while (not (eobp))
12814             (condition-case nil
12815                 (progn
12816                   (narrow-to-region (point) (gnus-point-at-eol))
12817                   (setq group (let ((obarray hashtb)) (read cur)))
12818                   (if (and (numberp (setq max (read cur)))
12819                            (numberp (setq min (read cur)))
12820                            (progn 
12821                              (skip-chars-forward " \t")
12822                              (not
12823                               (or (= (following-char) ?=)
12824                                   (= (following-char) ?x)
12825                                   (= (following-char) ?j)))))
12826                       (set group (cons min max))
12827                     (set group nil))
12828                   ;; Enter moderated groups into a list.
12829                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12830                       (setq gnus-moderated-list 
12831                             (cons (symbol-name group) gnus-moderated-list))))
12832               (error 
12833                (and (symbolp group)
12834                     (set group nil))))
12835             (widen)
12836             (forward-line 1)))
12837       ;; And if we do not care about moderation, we use this loop,
12838       ;; which is faster.
12839       (let (group max min)
12840         (while (not (eobp))
12841           (condition-case ()
12842               (progn
12843                 (narrow-to-region (point) (gnus-point-at-eol))
12844                 ;; group gets set to a symbol interned in the hash table
12845                 ;; (what a hack!!) - jwz
12846                 (setq group (let ((obarray hashtb)) (read cur)))
12847                 (if (and (numberp (setq max (read cur)))
12848                          (numberp (setq min (read cur)))
12849                          (progn 
12850                            (skip-chars-forward " \t")
12851                            (not
12852                             (or (= (following-char) ?=)
12853                                 (= (following-char) ?x)
12854                                 (= (following-char) ?j)))))
12855                     (set group (cons min max))
12856                   (set group nil)))
12857             (error 
12858              (progn 
12859                (and (symbolp group)
12860                     (set group nil))
12861                (if ignore-errors
12862                    ()
12863                  (gnus-message 3 "Warning - illegal active: %s"
12864                                (buffer-substring 
12865                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12866                  nil))))
12867           (widen)
12868           (forward-line 1))))))
12869
12870 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12871   ;; Parse a "groups" active file.
12872   (let ((cur (current-buffer))
12873         (hashtb (or hashtb 
12874                     (if (and method gnus-active-hashtb)
12875                         gnus-active-hashtb
12876                       (setq gnus-active-hashtb
12877                             (gnus-make-hashtable 
12878                              (count-lines (point-min) (point-max)))))))
12879         (prefix (and method (not (eq method gnus-select-method))
12880                      (gnus-group-prefixed-name "" method))))
12881
12882     (goto-char (point-min))
12883     ;; We split this into to separate loops, one with the prefix
12884     ;; and one without to speed the reading up somewhat.
12885     (if prefix
12886         (let (min max opoint group)
12887           (while (not (eobp))
12888             (condition-case ()
12889                 (progn
12890                   (read cur) (read cur)
12891                   (setq min (read cur)
12892                         max (read cur)
12893                         opoint (point))
12894                   (skip-chars-forward " \t")
12895                   (insert prefix)
12896                   (goto-char opoint)
12897                   (set (let ((obarray hashtb)) (read cur)) 
12898                        (cons min max)))
12899               (error (if group (set group nil))))
12900             (forward-line 1)))
12901       (let (min max group)
12902         (while (not (eobp))
12903           (condition-case ()
12904               (if (= (following-char) ?2)
12905                   (progn
12906                     (read cur) (read cur)
12907                     (setq min (read cur)
12908                           max (read cur))
12909                     (set (setq group (let ((obarray hashtb)) (read cur)))
12910                          (cons min max))))
12911             (error (if group (set group nil))))
12912           (forward-line 1))))))
12913
12914 (defun gnus-read-newsrc-file (&optional force)
12915   "Read startup file.
12916 If FORCE is non-nil, the .newsrc file is read."
12917   ;; Reset variables that might be defined in the .newsrc.eld file.
12918   (let ((variables gnus-variable-list))
12919     (while variables
12920       (set (car variables) nil)
12921       (setq variables (cdr variables))))
12922   (let* ((newsrc-file gnus-current-startup-file)
12923          (quick-file (concat newsrc-file ".el")))
12924     (save-excursion
12925       ;; We always load the .newsrc.eld file. If always contains
12926       ;; much information that can not be gotten from the .newsrc
12927       ;; file (ticked articles, killed groups, foreign methods, etc.)
12928       (gnus-read-newsrc-el-file quick-file)
12929  
12930       (if (or force
12931               (and (file-newer-than-file-p newsrc-file quick-file)
12932                    (file-newer-than-file-p newsrc-file 
12933                                            (concat quick-file "d")))
12934               (not gnus-newsrc-alist))
12935           ;; We read the .newsrc file. Note that if there if a
12936           ;; .newsrc.eld file exists, it has already been read, and
12937           ;; the `gnus-newsrc-hashtb' has been created. While reading
12938           ;; the .newsrc file, Gnus will only use the information it
12939           ;; can find there for changing the data already read -
12940           ;; ie. reading the .newsrc file will not trash the data
12941           ;; already read (except for read articles).
12942           (save-excursion
12943             (gnus-message 5 "Reading %s..." newsrc-file)
12944             (set-buffer (find-file-noselect newsrc-file))
12945             (buffer-disable-undo (current-buffer))
12946             (gnus-newsrc-to-gnus-format)
12947             (kill-buffer (current-buffer))
12948             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12949
12950 (defun gnus-read-newsrc-el-file (file)
12951   (let ((ding-file (concat file "d")))
12952     ;; We always, always read the .eld file.
12953     (gnus-message 5 "Reading %s..." ding-file)
12954     (let (gnus-newsrc-assoc)
12955       (condition-case nil
12956           (load ding-file t t t)
12957         (error nil))
12958       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12959     (let ((inhibit-quit t))
12960       (gnus-uncompress-newsrc-alist))
12961     (gnus-make-hashtable-from-newsrc-alist)
12962     (if (not (file-newer-than-file-p file ding-file))
12963         ()
12964       ;; Old format quick file
12965       (gnus-message 5 "Reading %s..." file)
12966       ;; The .el file is newer than the .eld file, so we read that one
12967       ;; as well. 
12968       (gnus-read-old-newsrc-el-file file))))
12969
12970 ;; Parse the old-style quick startup file
12971 (defun gnus-read-old-newsrc-el-file (file)
12972   (let (newsrc killed marked group m)
12973     (prog1
12974         (let ((gnus-killed-assoc nil)
12975               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12976           (prog1
12977               (condition-case nil
12978                   (load file t t t)
12979                 (error nil))
12980             (setq newsrc gnus-newsrc-assoc
12981                   killed gnus-killed-assoc
12982                   marked gnus-marked-assoc)))
12983       (setq gnus-newsrc-alist nil)
12984       (while newsrc
12985         (setq group (car newsrc))
12986         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12987           (if info
12988               (progn
12989                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12990                 (setcar (cdr info)
12991                         (if (nth 1 group) gnus-level-default-subscribed 
12992                           gnus-level-default-unsubscribed))
12993                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12994             (setq gnus-newsrc-alist
12995                   (cons 
12996                    (setq info
12997                          (list (car group)
12998                                (if (nth 1 group) gnus-level-default-subscribed
12999                                  gnus-level-default-unsubscribed) 
13000                                (cdr (cdr group))))
13001                    gnus-newsrc-alist)))
13002           (if (setq m (assoc (car group) marked))
13003             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
13004         (setq newsrc (cdr newsrc)))
13005       (setq newsrc killed)
13006       (while newsrc
13007         (setcar newsrc (car (car newsrc)))
13008         (setq newsrc (cdr newsrc)))
13009       (setq gnus-killed-list killed))
13010     ;; The .el file version of this variable does not begin with
13011     ;; "options", while the .eld version does, so we just add it if it
13012     ;; isn't there.
13013     (and
13014      gnus-newsrc-options 
13015      (progn
13016        (and (not (string-match "^ *options" gnus-newsrc-options))
13017             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
13018        (and (not (string-match "\n$" gnus-newsrc-options))
13019             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
13020     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
13021     (gnus-make-hashtable-from-newsrc-alist)))
13022       
13023 (defun gnus-make-newsrc-file (file)
13024   "Make server dependent file name by catenating FILE and server host name."
13025   (let* ((file (expand-file-name file nil))
13026          (real-file (concat file "-" (nth 1 gnus-select-method))))
13027     (if (or (file-exists-p real-file)
13028             (file-exists-p (concat real-file ".el"))
13029             (file-exists-p (concat real-file ".eld")))
13030         real-file file)))
13031
13032 (defun gnus-uncompress-newsrc-alist ()
13033   ;; Uncompress all lists of marked articles in the newsrc assoc.
13034   (let ((newsrc gnus-newsrc-alist)
13035         marked)
13036     (while newsrc
13037       (if (not (setq marked (nth 3 (car newsrc))))
13038           ()
13039         (while marked
13040           (or (eq 'score (car (car marked)))
13041               (eq 'bookmark (car (car marked)))
13042               (eq 'killed (car (car marked)))
13043               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13044           (setq marked (cdr marked))))
13045       (setq newsrc (cdr newsrc)))))
13046
13047 (defun gnus-compress-newsrc-alist ()
13048   ;; Compress all lists of marked articles in the newsrc assoc.
13049   (let ((newsrc gnus-newsrc-alist)
13050         marked)
13051     (while newsrc
13052       (if (not (setq marked (nth 3 (car newsrc))))
13053           ()
13054         (while marked
13055           (or (eq 'score (car (car marked)))
13056               (eq 'bookmark (car (car marked)))
13057               (eq 'killed (car (car marked)))
13058               (setcdr (car marked) 
13059                       (condition-case ()
13060                           (gnus-compress-sequence 
13061                            (sort (cdr (car marked)) '<) t)
13062                         (error (cdr (car marked))))))
13063           (setq marked (cdr marked))))
13064       (setq newsrc (cdr newsrc)))))
13065
13066 (defun gnus-newsrc-to-gnus-format ()
13067   (setq gnus-newsrc-options "")
13068   (setq gnus-newsrc-options-n nil)
13069
13070   (or gnus-active-hashtb
13071       (setq gnus-active-hashtb (make-vector 4095 0)))
13072   (let ((buf (current-buffer))
13073         (already-read (> (length gnus-newsrc-alist) 1))
13074         group subscribed options-symbol newsrc Options-symbol
13075         symbol reads num1)
13076     (goto-char (point-min))
13077     ;; We intern the symbol `options' in the active hashtb so that we
13078     ;; can `eq' against it later.
13079     (setq options-symbol (intern "options" gnus-active-hashtb))
13080     (setq Options-symbol (intern "Options" gnus-active-hashtb))
13081   
13082     (while (not (eobp))
13083       ;; We first read the first word on the line by narrowing and
13084       ;; then reading into `gnus-active-hashtb'.  Most groups will
13085       ;; already exist in that hashtb, so this will save some string
13086       ;; space.
13087       (narrow-to-region
13088        (point)
13089        (progn (skip-chars-forward "^ \t!:\n") (point)))
13090       (goto-char (point-min))
13091       (setq symbol 
13092             (and (/= (point-min) (point-max))
13093                  (let ((obarray gnus-active-hashtb)) (read buf))))
13094       (widen)
13095       ;; Now, the symbol we have read is either `options' or a group
13096       ;; name.  If it is an options line, we just add it to a string. 
13097       (cond 
13098        ((or (eq symbol options-symbol)
13099             (eq symbol Options-symbol))
13100         (setq gnus-newsrc-options
13101               ;; This concatting is quite inefficient, but since our
13102               ;; thorough studies show that approx 99.37% of all
13103               ;; .newsrc files only contain a single options line, we
13104               ;; don't give a damn, frankly, my dear.
13105               (concat gnus-newsrc-options
13106                       (buffer-substring 
13107                        (gnus-point-at-bol)
13108                        ;; Options may continue on the next line.
13109                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13110                                 (progn (beginning-of-line) (point)))
13111                            (point))))))
13112        (symbol
13113         ;; It was a group name.
13114         (setq subscribed (= (following-char) ?:)
13115               group (symbol-name symbol)
13116               reads nil)
13117         (if (eolp)
13118             ;; If the line ends here, this is clearly a buggy line, so
13119             ;; we put point a the beginning of line and let the cond
13120             ;; below do the error handling.
13121             (beginning-of-line)
13122           ;; We skip to the beginning of the ranges.
13123           (skip-chars-forward "!: \t"))
13124         ;; We are now at the beginning of the list of read articles.
13125         ;; We read them range by range.
13126         (while
13127             (cond 
13128              ((looking-at "[0-9]+")
13129               ;; We narrow and read a number instead of buffer-substring/
13130               ;; string-to-int because it's faster. narrow/widen is
13131               ;; faster than save-restriction/narrow, and save-restriction
13132               ;; produces a garbage object.
13133               (setq num1 (progn
13134                            (narrow-to-region (match-beginning 0) (match-end 0))
13135                            (read buf)))
13136               (widen)
13137               ;; If the next character is a dash, then this is a range.
13138               (if (= (following-char) ?-)
13139                   (progn
13140                     ;; We read the upper bound of the range.
13141                     (forward-char 1)
13142                     (if (not (looking-at "[0-9]+"))
13143                         ;; This is a buggy line, by we pretend that
13144                         ;; it's kinda OK. Perhaps the user should be
13145                         ;; dinged? 
13146                         (setq reads (cons num1 reads))
13147                       (setq reads 
13148                             (cons 
13149                              (cons num1 (progn
13150                                           (narrow-to-region (match-beginning 0) 
13151                                                             (match-end 0))
13152                                           (read buf)))
13153                              reads))
13154                       (widen)))
13155                 ;; It was just a simple number, so we add it to the
13156                 ;; list of ranges.
13157                 (setq reads (cons num1 reads)))
13158               ;; If the next char in ?\n, then we have reached the end
13159               ;; of the line and return nil.
13160               (/= (following-char) ?\n))
13161              ((= (following-char) ?\n)
13162               ;; End of line, so we end.
13163               nil)
13164              (t
13165               ;; Not numbers and not eol, so this might be a buggy
13166               ;; line... 
13167               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13168                   (progn
13169                     ;; The line was buggy.
13170                     (setq group nil)
13171                     (gnus-message 3 "Mangled line: %s" 
13172                                   (buffer-substring (gnus-point-at-bol) 
13173                                                     (gnus-point-at-eol)))
13174                     (ding)
13175                     (sit-for 1)))
13176               nil))
13177           ;; Skip past ", ". Spaces are illegal in these ranges, but
13178           ;; we allow them, because it's a common mistake to put a
13179           ;; space after the comma.
13180           (skip-chars-forward ", "))
13181
13182         ;; We have already read .newsrc.eld, so we gently update the
13183         ;; data in the hash table with the information we have just
13184         ;; read. 
13185         (if (not group)
13186             ()
13187           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13188                 level)
13189             (if info
13190                 ;; There is an entry for this file in the alist.
13191                 (progn
13192                   (setcar (nthcdr 2 info) (nreverse reads))
13193                   ;; We update the level very gently.  In fact, we
13194                   ;; only change it if there's been a status change
13195                   ;; from subscribed to unsubscribed, or vice versa.
13196                   (setq level (nth 1 info))
13197                   (cond ((and (<= level gnus-level-subscribed)
13198                               (not subscribed))
13199                          (setq level (if reads
13200                                          gnus-level-default-unsubscribed 
13201                                        (1+ gnus-level-default-unsubscribed))))
13202                         ((and (> level gnus-level-subscribed) subscribed)
13203                          (setq level gnus-level-default-subscribed)))
13204                   (setcar (cdr info) level))
13205               ;; This is a new group.
13206               (setq info (list group 
13207                                (if subscribed
13208                                    gnus-level-default-subscribed 
13209                                  (if reads
13210                                      (1+ gnus-level-subscribed)
13211                                    gnus-level-default-unsubscribed))
13212                                (nreverse reads))))
13213             (setq newsrc (cons info newsrc))))))
13214       (forward-line 1))
13215     
13216     (setq newsrc (nreverse newsrc))
13217
13218     (if (not already-read)
13219         ()
13220       ;; We now have two newsrc lists - `newsrc', which is what we
13221       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13222       ;; what we've read from .newsrc.eld. We have to merge these
13223       ;; lists. We do this by "attaching" any (foreign) groups in the
13224       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13225       (let ((rc (cdr gnus-newsrc-alist))
13226             (prev gnus-newsrc-alist)
13227             entry mentry)
13228         (while rc
13229           (or (null (nth 4 (car rc))) ; It's a native group.
13230               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13231               (if (setq entry (assoc (car (car prev)) newsrc))
13232                   (setcdr (setq mentry (memq entry newsrc))
13233                           (cons (car rc) (cdr mentry)))
13234                 (setq newsrc (cons (car rc) newsrc))))
13235           (setq prev rc
13236                 rc (cdr rc)))))
13237
13238     (setq gnus-newsrc-alist newsrc)
13239     ;; We make the newsrc hashtb.
13240     (gnus-make-hashtable-from-newsrc-alist)
13241
13242     ;; Finally, if we read some options lines, we parse them.
13243     (or (string= gnus-newsrc-options "")
13244         (gnus-newsrc-parse-options gnus-newsrc-options))))
13245
13246 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13247 ;; The return value will be a list on the form
13248 ;; ((regexp1 . ignore)
13249 ;;  (regexp2 . subscribe)...)
13250 ;; When handling new newsgroups, groups that match a `ignore' regexp
13251 ;; will be ignored, and groups that match a `subscribe' regexp will be
13252 ;; subscribed. A line like
13253 ;; options -n !all rec.all
13254 ;; will lead to a list that looks like
13255 ;; (("^rec\\..+" . subscribe) 
13256 ;;  ("^.+" . ignore))
13257 ;; So all "rec.*" groups will be subscribed, while all the other
13258 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13259 ;; different from "options -n rec.all !all". 
13260 (defun gnus-newsrc-parse-options (options)
13261   (let (out eol)
13262     (save-excursion
13263       (gnus-set-work-buffer)
13264       (insert (regexp-quote options))
13265       ;; First we treat all continuation lines.
13266       (goto-char (point-min))
13267       (while (re-search-forward "\n[ \t]+" nil t)
13268         (replace-match " " t t))
13269       ;; Then we transform all "all"s into ".+"s.
13270       (goto-char (point-min))
13271       (while (re-search-forward "\\ball\\b" nil t)
13272         (replace-match ".+" t t))
13273       (goto-char (point-min))
13274       ;; We remove all other options than the "-n" ones.
13275       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13276         (replace-match " ")
13277         (forward-char -1))
13278       (goto-char (point-min))
13279
13280       ;; We are only interested in "options -n" lines - we
13281       ;; ignore the other option lines.
13282       (while (re-search-forward "[ \t]-n" nil t)
13283         (setq eol 
13284               (or (save-excursion
13285                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13286                          (- (point) 2)))
13287                   (gnus-point-at-eol)))
13288         ;; Search for all "words"...
13289         (while (re-search-forward "[^ \t,\n]+" eol t)
13290           (if (= (char-after (match-beginning 0)) ?!)
13291               ;; If the word begins with a bang (!), this is a "not"
13292               ;; spec. We put this spec (minus the bang) and the
13293               ;; symbol `ignore' into the list.
13294               (setq out (cons (cons (concat 
13295                                      "^" (buffer-substring 
13296                                           (1+ (match-beginning 0))
13297                                           (match-end 0)))
13298                                     'ignore) out))
13299             ;; There was no bang, so this is a "yes" spec.
13300             (setq out (cons (cons (concat 
13301                                    "^" (buffer-substring (match-beginning 0)
13302                                                          (match-end 0)))
13303                                   'subscribe) out)))))
13304     
13305       (setq gnus-newsrc-options-n out))))
13306                
13307
13308 (defun gnus-save-newsrc-file ()
13309   "Save .newsrc file."
13310   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13311   ;; from the variable gnus-newsrc-alist.
13312   (and (or gnus-newsrc-alist gnus-killed-list)
13313        gnus-current-startup-file
13314        (progn
13315          (run-hooks 'gnus-save-newsrc-hook)
13316          (save-excursion
13317            (if (and gnus-use-dribble-file
13318                     (or (not gnus-dribble-buffer)
13319                         (not (buffer-name gnus-dribble-buffer))
13320                         (zerop (save-excursion
13321                                  (set-buffer gnus-dribble-buffer)
13322                                  (buffer-size)))))
13323                (gnus-message 4 "(No changes need to be saved)")
13324              (if gnus-save-newsrc-file
13325                  (progn
13326                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13327                    ;; Make backup file of master newsrc.
13328                    (gnus-gnus-to-newsrc-format)
13329                    (gnus-message 5 "Saving %s...done"
13330                                  gnus-current-startup-file)))
13331              ;; Quickly loadable .newsrc.
13332              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13333              (setq buffer-file-name (concat gnus-current-startup-file ".eld"))
13334              (gnus-add-current-to-buffer-list)
13335              (buffer-disable-undo (current-buffer))
13336              (erase-buffer)
13337              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13338              (gnus-gnus-to-quick-newsrc-format)
13339              (save-buffer)
13340              (kill-buffer (current-buffer))
13341              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13342              (gnus-dribble-delete-file))))))
13343
13344 (defun gnus-gnus-to-quick-newsrc-format ()
13345   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13346   (insert ";; (ding) Gnus startup file.\n")
13347   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13348   (insert ";; to read .newsrc.\n")
13349   (insert "(setq gnus-newsrc-file-version "
13350           (prin1-to-string gnus-version) ")\n")
13351   (let ((variables gnus-variable-list)
13352         (inhibit-quit t)
13353         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13354         variable)
13355     ;; insert lisp expressions.
13356     (gnus-compress-newsrc-alist)
13357     (while variables
13358       (setq variable (car variables))
13359       (and (boundp variable)
13360            (symbol-value variable)
13361            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13362            (insert "(setq " (symbol-name variable) " '"
13363                    (prin1-to-string (symbol-value variable))
13364                    ")\n"))
13365       (setq variables (cdr variables)))
13366     (gnus-uncompress-newsrc-alist)))
13367
13368
13369 (defun gnus-gnus-to-newsrc-format ()
13370   ;; Generate and save the .newsrc file.
13371   (let ((newsrc (cdr gnus-newsrc-alist))
13372         info ranges range)
13373     (save-excursion
13374       (set-buffer (create-file-buffer gnus-current-startup-file))
13375       (setq buffer-file-name gnus-current-startup-file)
13376       (buffer-disable-undo (current-buffer))
13377       (erase-buffer)
13378       ;; Write options.
13379       (if gnus-newsrc-options (insert gnus-newsrc-options))
13380       ;; Write subscribed and unsubscribed.
13381       (while newsrc
13382         (setq info (car newsrc))
13383         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13384             (progn
13385               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13386                                      "!" ":"))
13387               (if (setq ranges (nth 2 info))
13388                   (progn
13389                     (insert " ")
13390                     (if (not (listp (cdr ranges)))
13391                         (if (= (car ranges) (cdr ranges))
13392                             (insert (int-to-string (car ranges)))
13393                           (insert (int-to-string (car ranges)) "-" 
13394                                   (int-to-string (cdr ranges))))
13395                       (while ranges
13396                         (setq range (car ranges)
13397                               ranges (cdr ranges))
13398                         (if (or (atom range) (= (car range) (cdr range)))
13399                             (insert (int-to-string 
13400                                      (or (and (atom range) range) 
13401                                          (car range))))
13402                           (insert (int-to-string (car range)) "-"
13403                                   (int-to-string (cdr range))))
13404                         (if ranges (insert ","))))))
13405               (insert "\n")))
13406         (setq newsrc (cdr newsrc)))
13407       (save-buffer)
13408       (kill-buffer (current-buffer)))))
13409
13410 (defun gnus-read-all-descriptions-files ()
13411   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13412     (while methods
13413       (gnus-read-descriptions-file (car methods))
13414       (setq methods (cdr methods)))
13415     t))
13416
13417 (defun gnus-read-descriptions-file (&optional method)
13418   (let ((method (or method gnus-select-method)))
13419     ;; We create the hashtable whether we manage to read the desc file
13420     ;; to avoid trying to re-read after a failed read.
13421     (or gnus-description-hashtb
13422         (setq gnus-description-hashtb 
13423               (gnus-make-hashtable (length gnus-active-hashtb))))
13424     ;; Mark this method's desc file as read.
13425     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13426                   gnus-description-hashtb)
13427
13428     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13429     (cond 
13430      ((not (gnus-check-server method))
13431       (gnus-message 1 "Couldn't open server")
13432       nil)
13433      ((not (gnus-request-list-newsgroups method))
13434       (gnus-message 1 "Couldn't read newsgroups descriptions")
13435       nil)
13436      (t
13437       (let (group)
13438         (save-excursion
13439           (save-restriction
13440             (set-buffer nntp-server-buffer)
13441             (goto-char (point-min))
13442             (if (or (search-forward "\n.\n" nil t)
13443                     (goto-char (point-max)))
13444                 (progn
13445                   (beginning-of-line)
13446                   (narrow-to-region (point-min) (point))))
13447             (goto-char (point-min))
13448             (while (not (eobp))
13449               ;; If we get an error, we set group to 0, which is not a
13450               ;; symbol... 
13451               (setq group 
13452                     (condition-case ()
13453                         (let ((obarray gnus-description-hashtb))
13454                           ;; Group is set to a symbol interned in this
13455                           ;; hash table.
13456                           (read nntp-server-buffer))
13457                       (error 0)))
13458               (skip-chars-forward " \t")
13459               ;; ... which leads to this line being effectively ignored.
13460               (and (symbolp group)
13461                    (set group (buffer-substring 
13462                                (point) (progn (end-of-line) (point)))))
13463               (forward-line 1))))
13464         (gnus-message 5 "Reading descriptions file...done")
13465         t)))))
13466
13467 (defun gnus-group-get-description (group)
13468   ;; Get the description of a group by sending XGTITLE to the server.
13469   (and (gnus-request-group-description group)
13470        (save-excursion
13471          (set-buffer nntp-server-buffer)
13472          (goto-char (point-min))
13473          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13474               (buffer-substring (match-beginning 1) (match-end 1))))))
13475
13476 ;;;
13477 ;;; Server
13478 ;;;
13479
13480 (defvar gnus-server-mode-hook nil
13481   "Hook run in `gnus-server-mode' buffers.")
13482
13483 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13484   "Format of server lines.
13485 It works along the same lines as a normal formatting string,
13486 with some simple extensions.")
13487
13488 (defvar gnus-server-mode-line-format "(ding) List of servers"
13489   "The format specification for the server mode line.")
13490
13491 (defconst gnus-server-line-format-alist
13492   (list (list ?h 'how ?s)
13493         (list ?n 'name ?s)
13494         (list ?w 'where ?s)
13495         ))
13496
13497 (defconst gnus-server-mode-line-format-alist 
13498   (list (list ?S 'news-server ?s)
13499         (list ?M 'news-method ?s)
13500         (list ?u 'user-defined ?s)))
13501
13502 (defvar gnus-server-line-format-spec nil)
13503 (defvar gnus-server-mode-line-format-spec nil)
13504 (defvar gnus-server-killed-servers nil)
13505
13506 (defvar gnus-server-mode-map nil)
13507 (put 'gnus-server-mode 'mode-class 'special)
13508
13509 (if gnus-server-mode-map
13510     nil
13511   (setq gnus-server-mode-map (make-sparse-keymap))
13512   (suppress-keymap gnus-server-mode-map)
13513   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13514   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13515   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13516   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13517   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13518   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13519   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13520   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13521   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13522   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13523
13524 (defun gnus-server-mode ()
13525   "Major mode for listing and editing servers.
13526
13527 All normal editing commands are switched off.
13528 \\<gnus-server-mode-map>
13529
13530 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13531
13532 The following commands are available:
13533
13534 \\{gnus-server-mode-map}"
13535   (interactive)
13536   (if gnus-visual (gnus-server-make-menu-bar))
13537   (kill-all-local-variables)
13538   (setq mode-line-modified "-- ")
13539   (make-local-variable 'mode-line-format)
13540   (setq mode-line-format (copy-sequence mode-line-format))
13541   (and (equal (nth 3 mode-line-format) "   ")
13542        (setcar (nthcdr 3 mode-line-format) ""))
13543   (setq major-mode 'gnus-server-mode)
13544   (setq mode-name "Server")
13545 ;  (gnus-group-set-mode-line)
13546   (setq mode-line-process nil)
13547   (use-local-map gnus-server-mode-map)
13548   (buffer-disable-undo (current-buffer))
13549   (setq truncate-lines t)
13550   (setq buffer-read-only t)
13551   (run-hooks 'gnus-server-mode-hook))
13552
13553 (defun gnus-server-insert-server-line (sformat name method)
13554   (let* ((sformat (or sformat gnus-server-line-format-spec))
13555          (how (car method))
13556          (where (nth 1 method))
13557          b)
13558     (beginning-of-line)
13559     (setq b (point))
13560     ;; Insert the text.
13561     (insert (eval sformat))
13562     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13563
13564 (defun gnus-server-setup-buffer ()
13565   (if (get-buffer gnus-server-buffer)
13566       ()
13567     (save-excursion
13568       (set-buffer (get-buffer-create gnus-server-buffer))
13569       (gnus-server-mode)
13570       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13571
13572 (defun gnus-server-prepare ()
13573   (setq gnus-server-mode-line-format-spec 
13574         (gnus-parse-format gnus-server-mode-line-format 
13575                            gnus-server-mode-line-format-alist))
13576   (setq gnus-server-line-format-spec 
13577         (gnus-parse-format gnus-server-line-format 
13578                            gnus-server-line-format-alist))
13579   (let ((alist gnus-server-alist)
13580         (buffer-read-only nil))
13581     (erase-buffer)
13582     (while alist
13583       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13584       (setq alist (cdr alist))))
13585   (goto-char (point-min))
13586   (gnus-server-position-cursor))
13587
13588 (defun gnus-server-server-name ()
13589   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13590     (and server (symbol-name server))))
13591
13592 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13593
13594 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13595
13596 (defun gnus-server-update-server (server)
13597   (save-excursion
13598     (set-buffer gnus-server-buffer)
13599     (let ((buffer-read-only nil)
13600           (info (cdr (assoc server gnus-server-alist))))
13601       (gnus-dribble-enter 
13602        (concat "(gnus-server-set-info \"" server "\" '"
13603                (prin1-to-string info) ")"))
13604       ;; Buffer may be narrowed.
13605       (save-restriction
13606         (widen)
13607         (if (gnus-server-goto-server server)
13608             (delete-region (progn (beginning-of-line) (point))
13609                            (progn (forward-line 1) (point))))
13610         (let ((entry (assoc server gnus-server-alist)))
13611           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13612           (gnus-server-position-cursor))))))
13613
13614 (defun gnus-server-set-info (server info)
13615   ;; Enter a select method into the virtual server alist.
13616   (gnus-dribble-enter 
13617    (concat "(gnus-server-set-info \"" server "\" '"
13618            (prin1-to-string info) ")"))
13619   (let* ((server (nth 1 info))
13620          (entry (assoc server gnus-server-alist)))
13621     (if entry (setcdr entry info)
13622       (setq gnus-server-alist
13623             (nconc gnus-server-alist (list (cons server info)))))))
13624
13625 (defun gnus-server-to-method (server)
13626   ;; Map virtual server names to select methods.
13627   (or (and (equal server "native") gnus-select-method)
13628       (cdr (assoc server gnus-server-alist))))
13629
13630 (defun gnus-server-extend-method (group method)
13631   ;; This function "extends" a virtual server.  If the server is
13632   ;; "hello", and the select method is ("hello" (my-var "something")) 
13633   ;; in the group "alt.alt", this will result in a new virtual server
13634   ;; called "helly+alt.alt".
13635   (let ((entry
13636          (gnus-copy-sequence 
13637           (if (equal (car method) "native") gnus-select-method
13638               (cdr (assoc (car method) gnus-server-alist))))))
13639     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13640     (nconc entry (cdr method))))
13641
13642 (defun gnus-server-get-method (group method)
13643   ;; Input either a server name, and extended server name, or a
13644   ;; select method, and return a select method. 
13645   (cond ((stringp method)
13646          (gnus-server-to-method method))
13647         ((and (stringp (car method)) group)
13648          (gnus-server-extend-method group method))
13649         (t
13650          (gnus-server-add-address method))))
13651
13652 (defun gnus-server-add-address (method)
13653   (let ((method-name (symbol-name (car method))))
13654     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13655              (not (assq (intern (concat method-name "-address")) method)))
13656         (append method (list (list (intern (concat method-name "-address"))
13657                                    (nth 1 method))))
13658       method)))
13659
13660 (defun gnus-server-equal (s1 s2)
13661   (or (equal s1 s2)
13662       (and (= (length s1) (length s2))
13663            (progn
13664              (while (and s1 (member (car s1) s2))
13665                (setq s1 (cdr s1)))
13666              (null s1)))))
13667
13668 ;;; Interactive server functions.
13669
13670 (defun gnus-server-kill-server (server)
13671   "Kill the server on the current line."
13672   (interactive (list (gnus-server-server-name)))
13673   (or (gnus-server-goto-server server)
13674       (if server (error "No such server: %s" server)
13675         (error "No server on the current line")))
13676   (let ((buffer-read-only nil))
13677     (delete-region (progn (beginning-of-line) (point))
13678                    (progn (forward-line 1) (point))))
13679   (setq gnus-server-killed-servers 
13680         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13681   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13682                                 gnus-server-alist))
13683   (gnus-server-position-cursor))
13684
13685 (defun gnus-server-yank-server ()
13686   "Yank the previously killed server."
13687   (interactive)
13688   (or gnus-server-killed-servers
13689       (error "No killed servers to be yanked"))
13690   (let ((alist gnus-server-alist)
13691         (server (gnus-server-server-name))
13692         (killed (car gnus-server-killed-servers)))
13693     (if (not server) 
13694         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13695       (if (string= server (car (car gnus-server-alist)))
13696           (setq gnus-server-alist (cons killed gnus-server-alist))
13697         (while (and (cdr alist)
13698                     (not (string= server (car (car (cdr alist))))))
13699           (setq alist (cdr alist)))
13700         (setcdr alist (cons killed (cdr alist)))))
13701     (gnus-server-update-server (car killed))
13702     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13703     (gnus-server-position-cursor)))
13704
13705 (defun gnus-server-exit ()
13706   "Return to the group buffer."
13707   (interactive)
13708   (kill-buffer (current-buffer))
13709   (switch-to-buffer gnus-group-buffer))
13710
13711 (defun gnus-server-list-servers ()
13712   "List all available servers."
13713   (interactive)
13714   (let ((cur (gnus-server-server-name)))
13715     (gnus-server-prepare)
13716     (if cur (gnus-server-goto-server cur)
13717       (goto-char (point-max))
13718       (forward-line -1))
13719     (gnus-server-position-cursor)))
13720
13721 (defun gnus-server-copy-server (from to)
13722   (interactive
13723    (list
13724     (or (gnus-server-server-name)
13725         (error "No server on the current line"))
13726     (read-string "Copy to: ")))
13727   (or from (error "No server on current line"))
13728   (or (and to (not (string= to ""))) (error "No name to copy to"))
13729   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13730   (or (assoc from gnus-server-alist) 
13731       (error "%s: no such server" from))
13732   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13733     (setcar to-entry to)
13734     (setcar (nthcdr 2 to-entry) to)
13735     (setq gnus-server-killed-servers 
13736           (cons to-entry gnus-server-killed-servers))
13737     (gnus-server-yank-server)))
13738
13739 (defun gnus-server-add-server (how where)
13740   (interactive 
13741    (list (intern (completing-read "Server method: "
13742                                   gnus-valid-select-methods nil t))
13743          (read-string "Server name: ")))
13744   (setq gnus-server-killed-servers 
13745         (cons (list where how where) gnus-server-killed-servers))
13746   (gnus-server-yank-server))
13747
13748 (defun gnus-server-goto-server (server)
13749   "Jump to a server line."
13750   (interactive
13751    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13752   (let ((to (text-property-any (point-min) (point-max) 
13753                                'gnus-server (intern server))))
13754     (and to
13755          (progn
13756            (goto-char to) 
13757            (gnus-server-position-cursor)))))
13758
13759 (defun gnus-server-edit-server (server)
13760   "Edit the server on the current line."
13761   (interactive (list (gnus-server-server-name)))
13762   (or server
13763       (error "No server on current line"))
13764   (let ((winconf (current-window-configuration)))
13765     (get-buffer-create gnus-server-edit-buffer)
13766     (gnus-configure-windows 'edit-server)
13767     (gnus-add-current-to-buffer-list)
13768     (emacs-lisp-mode)
13769     (make-local-variable 'gnus-prev-winconf)
13770     (setq gnus-prev-winconf winconf)
13771     (use-local-map (copy-keymap (current-local-map)))
13772     (let ((done-func '(lambda () 
13773                         "Exit editing mode and update the information."
13774                         (interactive)
13775                         (gnus-server-edit-server-done 'group))))
13776       (setcar (cdr (nth 4 done-func)) server)
13777       (local-set-key "\C-c\C-c" done-func))
13778     (erase-buffer)
13779     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13780     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13781
13782 (defun gnus-server-edit-server-done (server)
13783   (interactive)
13784   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13785   (goto-char (point-min))
13786   (let ((form (read (current-buffer)))
13787         (winconf gnus-prev-winconf))
13788     (gnus-server-set-info server form)
13789     (kill-buffer (current-buffer))
13790     (and winconf (set-window-configuration winconf))
13791     (set-buffer gnus-server-buffer)
13792     (gnus-server-update-server (gnus-server-server-name))
13793     (gnus-server-position-cursor)))
13794
13795 (defun gnus-server-read-server (server)
13796   "Browse a server."
13797   (interactive (list (gnus-server-server-name)))
13798   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13799
13800 (defun gnus-mouse-pick-server (e)
13801   (interactive "e")
13802   (mouse-set-point e)
13803   (gnus-server-read-server (gnus-server-server-name)))
13804
13805 ;;;
13806 ;;; entry points into gnus-score.el
13807 ;;;
13808
13809 ;;; Finding score files. 
13810
13811 (defvar gnus-global-score-files nil
13812   "*List of global score files and directories.
13813 Set this variable if you want to use people's score files.  One entry
13814 for each score file or each score file directory.  Gnus will decide
13815 by itself what score files are applicable to which group.
13816
13817 Say you want to use the single score file
13818 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13819 score files in the \"/ftp.some-where:/pub/score\" directory.
13820
13821  (setq gnus-global-score-files
13822        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13823          \"/ftp.some-where:/pub/score\"))")
13824
13825 (defun gnus-score-score-files (group)
13826   "Return a list of all possible score files."
13827   ;; Search and set any global score files.
13828   (and gnus-global-score-files 
13829        (or gnus-internal-global-score-files
13830            (gnus-score-search-global-directories gnus-global-score-files)))
13831   ;; Fix the kill-file dir variable.
13832   (setq gnus-kill-files-directory 
13833         (file-name-as-directory
13834          (or gnus-kill-files-directory "~/News/")))
13835   ;; If we can't read it, there are no score files.
13836   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13837       (setq gnus-score-file-list nil)
13838     (if (gnus-use-long-file-name 'not-score)
13839         ;; We want long file names.
13840         (if (or (not gnus-score-file-list)
13841                 (not (car gnus-score-file-list))
13842                 (gnus-file-newer-than gnus-kill-files-directory
13843                                       (car gnus-score-file-list)))
13844               (setq gnus-score-file-list 
13845                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13846                           (nreverse 
13847                            (directory-files 
13848                             gnus-kill-files-directory t 
13849                             (gnus-score-file-regexp))))))
13850       ;; We do not use long file names, so we have to do some
13851       ;; directory traversing.  
13852       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13853             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13854             dir files suffix)
13855         (while suffixes
13856           (setq dir (expand-file-name
13857                      (concat gnus-kill-files-directory
13858                              (gnus-replace-chars-in-string group ?. ?/))))
13859           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13860           (setq suffix (car suffixes)
13861                 suffixes (cdr suffixes))
13862           (if (file-exists-p (concat dir "/" suffix))
13863               (setq files (cons (concat dir "/" suffix) files)))
13864           (while (>= (1+ (length dir)) mdir)
13865             (and (file-exists-p (concat dir "/all/" suffix))
13866                  (setq files (cons (concat dir "/all/" suffix) files)))
13867             (string-match "/[^/]*$" dir)
13868             (setq dir (substring dir 0 (match-beginning 0)))))
13869         (setq gnus-score-file-list 
13870               (cons nil (nreverse files)))))
13871     (cdr gnus-score-file-list)))
13872
13873 (defun gnus-score-file-regexp ()
13874   (concat "\\(" gnus-score-file-suffix 
13875           "\\|" gnus-adaptive-file-suffix "\\)$"))
13876         
13877 (defun gnus-score-find-bnews (group)
13878   "Return a list of score files for GROUP.
13879 The score files are those files in the ~/News directory which matches
13880 GROUP using BNews sys file syntax."
13881   (let* ((sfiles (append (gnus-score-score-files group)
13882                          gnus-internal-global-score-files))
13883          (kill-dir (file-name-as-directory 
13884                     (expand-file-name gnus-kill-files-directory)))
13885          (klen (length kill-dir))
13886          ofiles not-match regexp)
13887     (save-excursion
13888       (set-buffer (get-buffer-create "*gnus score files*"))
13889       (buffer-disable-undo (current-buffer))
13890       ;; Go through all score file names and create regexp with them
13891       ;; as the source.  
13892       (while sfiles
13893         (erase-buffer)
13894         (insert (car sfiles))
13895         (goto-char (point-min))
13896         ;; First remove the suffix itself.
13897         (re-search-forward (concat "." (gnus-score-file-regexp)))
13898         (replace-match "" t t) 
13899         (goto-char (point-min))
13900         (if (looking-at (regexp-quote kill-dir))
13901             ;; If the file name was just "SCORE", `klen' is one character
13902             ;; too much.
13903             (delete-char (min (1- (point-max)) klen))
13904           (goto-char (point-max))
13905           (search-backward "/")
13906           (delete-region (1+ (point)) (point-min)))
13907         ;; If short file names were used, we have to translate slashes.
13908         (goto-char (point-min))
13909         (while (re-search-forward "[/:]" nil t)
13910           (replace-match "." t t))
13911         ;; Translate "all" to ".*".
13912         (while (search-forward "all" nil t)
13913           (replace-match ".*" t t))
13914         (goto-char (point-min))
13915         ;; Deal with "not."s.
13916         (if (looking-at "not.")
13917             (progn
13918               (setq not-match t)
13919               (setq regexp (buffer-substring 5 (point-max))))
13920           (setq regexp (buffer-substring 1 (point-max)))
13921           (setq not-match nil))
13922         ;; Finally - if this resulting regexp matches the group name,
13923         ;; we add this score file to the list of score files
13924         ;; applicable to this group.
13925         (if (or (and not-match
13926                      (not (string-match regexp group)))
13927                 (and (not not-match)
13928                      (string-match regexp group)))
13929             (setq ofiles (cons (car sfiles) ofiles)))
13930         (setq sfiles (cdr sfiles)))
13931       (kill-buffer (current-buffer))
13932       ;; Slight kludge here - the last score file returned should be
13933       ;; the local score file, whether it exists or not. This is so
13934       ;; that any score commands the user enters will go to the right
13935       ;; file, and not end up in some global score file.
13936       (let ((localscore
13937              (expand-file-name
13938               (if (gnus-use-long-file-name 'not-score)
13939                   (concat gnus-kill-files-directory group "." 
13940                           gnus-score-file-suffix)
13941                 (concat gnus-kill-files-directory
13942                         (gnus-replace-chars-in-string group ?. ?/)
13943                         "/" gnus-score-file-suffix)))))
13944         (and (member localscore ofiles)
13945              (delete localscore ofiles))
13946         (setq ofiles (cons localscore ofiles)))
13947       (nreverse ofiles))))
13948
13949 (defun gnus-score-find-single (group)
13950   "Return list containing the score file for GROUP."
13951   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13952         (gnus-score-file-name group)))
13953
13954 (defun gnus-score-find-hierarchical (group)
13955   "Return list of score files for GROUP.
13956 This includes the score file for the group and all its parents."
13957   (let ((all (copy-sequence '(nil)))
13958         (start 0))
13959     (while (string-match "\\." group (1+ start))
13960       (setq start (match-beginning 0))
13961       (setq all (cons (substring group 0 start) all)))
13962     (setq all (cons group all))
13963     (nconc
13964      (mapcar (lambda (newsgroup)
13965                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13966              (setq all (nreverse all)))
13967      (mapcar 'gnus-score-file-name all))))
13968
13969 (defvar gnus-score-file-alist-cache nil)
13970
13971 (defun gnus-score-find-alist (group)
13972   "Return list of score files for GROUP.
13973 The list is determined from the variable gnus-score-file-alist."
13974   (let ((alist gnus-score-file-multiple-match-alist)
13975         score-files)
13976     ;; if this group has been seen before, return the cached entry
13977     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13978         (cdr score-files)      ; ensures caching of groups with no matches
13979       ;; handle the multiple match alist
13980       (while alist
13981         (and (string-match (car (car alist)) group)
13982              (setq score-files
13983                    (nconc score-files (copy-sequence (cdr (car alist))))))
13984         (setq alist (cdr alist)))
13985       (setq alist gnus-score-file-single-match-alist)
13986       ;; handle the single match alist
13987       (while alist
13988         (and (string-match (car (car alist)) group)
13989              ;; progn used just in case ("regexp") has no files
13990              ;; and score-files is still nil. -sj
13991              ;; this can be construed as a "stop searching here" feature :>
13992              ;; and used to simplify regexps in the single-alist 
13993              (progn
13994                (setq score-files
13995                      (nconc score-files (copy-sequence (cdr (car alist)))))
13996                (setq alist nil)))
13997         (setq alist (cdr alist)))
13998       ;; cache the score files
13999       (setq gnus-score-file-alist-cache
14000             (cons (cons group score-files) gnus-score-file-alist-cache))
14001       score-files)))
14002
14003
14004 (defun gnus-possibly-score-headers (&optional trace)
14005   (let ((func gnus-score-find-score-files-function)
14006         score-files)
14007     (and func (not (listp func))
14008          (setq func (list func)))
14009     ;; Go through all the functions for finding score files (or actual
14010     ;; scores) and add them to a list.
14011     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
14012     (while func
14013       (and (symbolp (car func))
14014            (fboundp (car func))
14015            (setq score-files 
14016                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
14017       (setq func (cdr func)))
14018     (if score-files (gnus-score-headers score-files trace))))
14019
14020 (defun gnus-score-file-name (newsgroup &optional suffix)
14021   "Return the name of a score file for NEWSGROUP."
14022   (let ((suffix (or suffix gnus-score-file-suffix)))
14023     (cond 
14024      ((or (null newsgroup)
14025           (string-equal newsgroup ""))
14026       ;; The global score file is placed at top of the directory.
14027       (expand-file-name 
14028        suffix (or gnus-kill-files-directory "~/News")))
14029      ((gnus-use-long-file-name 'not-score)
14030       ;; Append ".SCORE" to newsgroup name.
14031       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14032                                 "." suffix)
14033                         (or gnus-kill-files-directory "~/News")))
14034      (t
14035       ;; Place "SCORE" under the hierarchical directory.
14036       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14037                                 "/" suffix)
14038                         (or gnus-kill-files-directory "~/News"))))))
14039
14040 (defun gnus-score-search-global-directories (files)
14041   "Scan all global score directories for score files."
14042   ;; Set the variable `gnus-internal-global-score-files' to all
14043   ;; available global score files.
14044   (interactive (list gnus-global-score-files))
14045   (let (out)
14046     (while files
14047       (if (string-match "/$" (car files))
14048           (setq out (nconc (directory-files 
14049                             (car files) t
14050                             (concat (gnus-score-file-regexp) "$"))))
14051         (setq out (cons (car files) out)))
14052       (setq files (cdr files)))
14053     (setq gnus-internal-global-score-files out)))
14054
14055 ;; Allow redefinition of Gnus functions.
14056
14057 (gnus-ems-redefine)
14058
14059 (provide 'gnus)
14060
14061 ;;; gnus.el ends here