6d7a816f5a01523838d975e0584c4512a70e47e8
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is 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   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [faq 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer)
731     (faq . gnus-faq-buffer))
732   "Mapping from short symbols to buffer names or buffer variables.")
733
734 (defvar gnus-carpal nil
735   "*If non-nil, display clickable icons.")
736
737 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
738   "*Function called with a group name when new group is detected.
739 A few pre-made functions are supplied: `gnus-subscribe-randomly'
740 inserts new groups at the beginning of the list of groups;
741 `gnus-subscribe-alphabetically' inserts new groups in strict
742 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
743 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
744 for your decision.")
745
746 ;; Suggested by a bug report by Hallvard B Furuseth.
747 ;; <h.b.furuseth@usit.uio.no>. 
748 (defvar gnus-subscribe-options-newsgroup-method
749   (function gnus-subscribe-alphabetically)
750   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
751 If, for instance, you want to subscribe to all newsgroups in the
752 \"no\" and \"alt\" hierarchies, you'd put the following in your
753 .newsrc file:
754
755 options -n no.all alt.all
756
757 Gnus will the subscribe all new newsgroups in these hierarchies with
758 the subscription method in this variable.")
759
760 (defvar gnus-subscribe-hierarchical-interactive nil
761   "*If non-nil, Gnus will offer to subscribe hierarchically.
762 When a new hierarchy appears, Gnus will ask the user:
763
764 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
765
766 If the user pressed `d', Gnus will descend the hierarchy, `y' will
767 subscribe to all newsgroups in the hierarchy and `s' will skip this
768 hierarchy in its entirety.")
769
770 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
771   "*Function used for sorting the group buffer.
772 This function will be called with group info entries as the arguments
773 for the groups to be sorted.  Pre-made functions include
774 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
775 `gnus-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %A   Address of the poster (string)
888 %F   Contents of the From: header (string)
889 %x   Contents of the Xref: header (string)
890 %D   Date of the article (string)
891 %d   Date of the article (string) in DD-MMM format
892 %M   Message-id of the article (string)
893 %r   References of the article (string)
894 %c   Number of characters in the article (integer)
895 %L   Number of lines in the article (integer)
896 %I   Indentation based on thread level (a string of spaces)
897 %T   A string with two possible values: 80 spaces if the article
898      is on thread level two or larger and 0 spaces on level one
899 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
900 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
901 %[   Opening bracket (character, \"[\" or \"<\")
902 %]   Closing bracket (character, \"]\" or \">\")
903 %>   Spaces of length thread-level (string)
904 %<   Spaces of length (- 20 thread-level) (string)
905 %i   Article score (number)
906 %z   Article zcore (character)
907 %t   Number of articles under the current thread (number).
908 %e   Whether the thread is empty or not (character).
909 %u   User defined specifier. The next character in the format string should
910      be a letter.  Gnus will call the function gnus-user-format-function-X,
911      where X is the letter following %u. The function will be passed the
912      current header as argument. The function should return a string, which
913      will be inserted into the summary just like information from any other
914      summary specifier.
915
916 Text between %( and %) will be highlighted with `gnus-mouse-face'
917 when the mouse point is placed inside the area.  There can only be one
918 such area.
919
920 The %U (status), %R (replied) and %z (zcore) specs have to be handled
921 with care. For reasons of efficiency, Gnus will compute what column
922 these characters will end up in, and \"hard-code\" that. This means that
923 it is illegal to have these specs after a variable-length spec. Well,
924 you might not be arrested, but your summary buffer will look strange,
925 which is bad enough.
926
927 The smart choice is to have these specs as for to the left as
928 possible. 
929
930 This restriction may disappear in later versions of Gnus.")
931
932 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
933   "*The format specification for the dummy roots in the summary buffer.
934 It works along the same lines as a normal formatting string,
935 with some simple extensions.
936
937 %S  The subject")
938
939 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
940   "*The format specification for the summary mode line.")
941
942 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
943   "*The format specification for the article mode line.")
944
945 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
946   "*The format specification for the group mode line.")
947
948 (defvar gnus-valid-select-methods
949   '(("nntp" post address prompt-address)
950     ("nnspool" post)
951     ("nnvirtual" none virtual prompt-address) 
952     ("nnmbox" mail respool) 
953     ("nnml" mail respool)
954     ("nnmh" mail respool) 
955     ("nndir" none prompt-address address)
956     ("nneething" none prompt-address)
957     ("nndigest" none) 
958     ("nndoc" none prompt-address) 
959     ("nnbabyl" mail respool) 
960     ("nnkiboze" post virtual) 
961     ("nnsoup" post)
962     ("nnfolder" mail respool))
963   "An alist of valid select methods.
964 The first element of each list lists should be a string with the name
965 of the select method. The other elements may be be the category of
966 this method (ie. `post', `mail', `none' or whatever) or other
967 properties that this method has (like being respoolable).
968 If you implement a new select method, all you should have to change is
969 this variable. I think.")
970
971 (defvar gnus-updated-mode-lines '(group article summary)
972   "*List of buffers that should update their mode lines.
973 The list may contain the symbols `group', `article' and `summary'. If
974 the corresponding symbol is present, Gnus will keep that mode line
975 updated with information that may be pertinent. 
976 If this variable is nil, screen refresh may be quicker.")
977
978 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
979 (defvar gnus-mode-non-string-length 21
980   "*Max length of mode-line non-string contents.
981 If this is nil, Gnus will take space as is needed, leaving the rest
982 of the modeline intact.")
983
984 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
985 (defvar gnus-display-type 
986   (condition-case nil
987       (let ((display-resource (x-get-resource ".displayType" "DisplayType")))
988         (cond (display-resource (intern (downcase display-resource)))
989               ((x-display-color-p) 'color)
990               ((x-display-grayscale-p) 'grayscale)
991               (t 'mono)))
992     (error 'mono))
993   "A symbol indicating the display Emacs is running under.
994 The symbol should be one of `color', `grayscale' or `mono'. If Emacs
995 guesses this display attribute wrongly, either set this variable in
996 your `~/.emacs' or set the resource `Emacs.displayType' in your
997 `~/.Xdefaults'. See also `gnus-background-mode'.")
998
999 (defvar gnus-background-mode 
1000   (condition-case nil
1001       (let ((bg-resource (x-get-resource ".backgroundMode"
1002                                          "BackgroundMode"))
1003             (params (frame-parameters)))
1004         (cond (bg-resource (intern (downcase bg-resource)))
1005               ((and (cdr (assq 'background-color params))
1006                     (< (apply '+ (x-color-values
1007                                   (cdr (assq 'background-color params))))
1008                        (/ (apply '+ (x-color-values "white")) 3)))
1009                'dark)
1010               (t 'light)))
1011     (error 'light))
1012   "A symbol indicating the Emacs background brightness.
1013 The symbol should be one of `light' or `dark'.
1014 If Emacs guesses this frame attribute wrongly, either set this variable in
1015 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1016 `~/.Xdefaults'.
1017 See also `gnus-display-type'.")
1018
1019 (defvar gnus-mouse-face 'highlight
1020   "*Face used for mouse highlighting in Gnus.
1021 No mouse highlights will be done if `gnus-visual' is nil.")
1022
1023 (defvar gnus-summary-mark-below nil
1024   "*Mark all articles with a score below this variable as read.
1025 This variable is local to each summary buffer and usually set by the
1026 score file.")  
1027
1028 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1029   "*List of functions used for sorting threads in the summary buffer.
1030 By default, threads are sorted by article number.
1031
1032 Each function takes two threads and return non-nil if the first thread
1033 should be sorted before the other.  If you use more than one function,
1034 the primary sort function should be the last.
1035
1036 Ready-mady functions include `gnus-thread-sort-by-number',
1037 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1038 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1039 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1040
1041 (defvar gnus-thread-score-function '+
1042   "*Function used for calculating the total score of a thread.
1043
1044 The function is called with the scores of the article and each
1045 subthread and should then return the score of the thread.
1046
1047 Some functions you can use are `+', `max', or `min'.")
1048
1049 (defvar gnus-options-subscribe nil
1050   "*All new groups matching this regexp will be subscribed unconditionally.
1051 Note that this variable deals only with new newsgroups.  This variable
1052 does not affect old newsgroups.")
1053
1054 (defvar gnus-options-not-subscribe nil
1055   "*All new groups matching this regexp will be ignored.
1056 Note that this variable deals only with new newsgroups.  This variable
1057 does not affect old (already subscribed) newsgroups.")
1058
1059 (defvar gnus-auto-expirable-newsgroups nil
1060   "*Groups in which to automatically mark read articles as expirable.
1061 If non-nil, this should be a regexp that should match all groups in
1062 which to perform auto-expiry.  This only makes sense for mail groups.")
1063
1064 (defvar gnus-hidden-properties '(invisible t intangible t)
1065   "Property list to use for hiding text.")
1066
1067 ;; Hooks.
1068
1069 (defvar gnus-group-mode-hook nil
1070   "*A hook for Gnus group mode.")
1071
1072 (defvar gnus-summary-mode-hook nil
1073   "*A hook for Gnus summary mode.
1074 This hook is run before any variables are set in the summary buffer.")
1075
1076 (defvar gnus-article-mode-hook nil
1077   "*A hook for Gnus article mode.")
1078
1079 (defun gnus-summary-exit-hook nil
1080   "*A hook called on exit from the summary buffer.
1081 It calls `gnus-summary-expire-articles' by default.")
1082 (add-hook 'gnus-summary-exit-hook 'gnus-summary-expire-articles)
1083
1084 (defvar gnus-open-server-hook nil
1085   "*A hook called just before opening connection to the news server.")
1086
1087 (defvar gnus-startup-hook nil
1088   "*A hook called at startup.
1089 This hook is called after Gnus is connected to the NNTP server.")
1090
1091 (defvar gnus-get-new-news-hook nil
1092   "*A hook run just before Gnus checks for new news.")
1093
1094 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1095   "*A function that is called to generate the group buffer.
1096 The function is called with three arguments: The first is a number;
1097 all group with a level less or equal to that number should be listed,
1098 if the second is non-nil, empty groups should also be displayed. If
1099 the third is non-nil, it is a number. No groups with a level lower
1100 than this number should be displayed.
1101
1102 The only current function implemented is `gnus-group-prepare-flat'.")
1103
1104 (defvar gnus-group-prepare-hook nil
1105   "*A hook called after the group buffer has been generated.
1106 If you want to modify the group buffer, you can use this hook.")
1107
1108 (defvar gnus-summary-prepare-hook nil
1109   "*A hook called after the summary buffer has been generated.
1110 If you want to modify the summary buffer, you can use this hook.")
1111
1112 (defvar gnus-article-prepare-hook nil
1113   "*A hook called after an article has been prepared in the article buffer.
1114 If you want to run a special decoding program like nkf, use this hook.")
1115
1116 (defvar gnus-article-display-hook nil
1117   "*A hook called after the article is displayed in the article buffer.
1118 The hook is designed to change the contents of the article
1119 buffer. Typical functions that this hook may contain are
1120 `gnus-article-hide-headers' (hide selected headers),
1121 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1122 `gnus-article-hide-signature' (hide signature) and
1123 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1124 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1125 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1126
1127 (defvar gnus-article-x-face-command
1128   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1129   "String or function to be executed to display an X-Face header.
1130 If it is a string, the command will be executed in a sub-shell
1131 asynchronously. The compressed face will be piped to this command.") 
1132
1133 (defvar gnus-article-x-face-too-ugly nil
1134   "Regexp matching posters whose face shouldn't be shown automatically.")
1135
1136 (defvar gnus-select-group-hook nil
1137   "*A hook called when a newsgroup is selected.
1138
1139 If you'd like to simplify subjects like the
1140 `gnus-summary-next-same-subject' command does, you can use the
1141 following hook:
1142
1143  (setq gnus-select-group-hook
1144       (list
1145         (lambda ()
1146           (mapcar (lambda (header)
1147                      (header-set-subject
1148                       header
1149                       (gnus-simplify-subject
1150                        (header-subject header) 're-only)))
1151                   gnus-newsgroup-headers))))")
1152
1153 (defvar gnus-select-article-hook
1154   '(gnus-summary-show-thread)
1155   "*A hook called when an article is selected.
1156 The default hook shows conversation thread subtrees of the selected
1157 article automatically using `gnus-summary-show-thread'.")
1158
1159 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1160   "*A hook called to apply kill files to a group.
1161 This hook is intended to apply a kill file to the selected newsgroup.
1162 The function `gnus-apply-kill-file' is called by default.
1163
1164 Since a general kill file is too heavy to use only for a few
1165 newsgroups, I recommend you to use a lighter hook function. For
1166 example, if you'd like to apply a kill file to articles which contains
1167 a string `rmgroup' in subject in newsgroup `control', you can use the
1168 following hook:
1169
1170 \(setq gnus-apply-kill-hook
1171       (list
1172         (lambda ()
1173           (cond ((string-match \"control\" gnus-newsgroup-name)
1174                  (gnus-kill \"Subject\" \"rmgroup\")
1175                  (gnus-expunge \"X\"))))))")
1176
1177 (defvar gnus-visual-mark-article-hook 
1178   (list 'gnus-highlight-selected-summary)
1179   "*Hook run after selecting an article in the summary buffer.
1180 It is meant to be used for highlighting the article in some way.  It
1181 is not run if `gnus-visual' is nil.")
1182
1183 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1184   "*A hook called after preparing body, but before preparing header headers.
1185 The default hook (`gnus-inews-insert-signature') inserts a signature
1186 file specified by the variable `gnus-signature-file'.")
1187
1188 (defvar gnus-exit-group-hook nil
1189   "*A hook called when exiting (not quitting) summary mode.")
1190
1191 (defvar gnus-suspend-gnus-hook nil
1192   "*A hook called when suspending (not exiting) Gnus.")
1193
1194 (defvar gnus-exit-gnus-hook nil
1195   "*A hook called when exiting Gnus.")
1196
1197 (defvar gnus-save-newsrc-hook nil
1198   "*A hook called when saving the newsrc file.")
1199
1200 (defvar gnus-summary-update-hook 
1201   (list 'gnus-summary-highlight-line)
1202   "*A hook called when a summary line is changed.
1203 The hook will not be called if `gnus-visual' is nil.
1204
1205 The default function `gnus-summary-highlight-line' will
1206 highlight the line according to the `gnus-summary-highlight'
1207 variable.")
1208
1209 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1210   "*A hook called when an article is selected for the first time.
1211 The hook is intended to mark an article as read (or unread)
1212 automatically when it is selected.")
1213
1214 ;; Remove any hilit infestation.
1215 (add-hook 'gnus-startup-hook
1216           (lambda ()
1217             (remove-hook 'gnus-summary-prepare-hook
1218                          'hilit-rehighlight-buffer-quietly)
1219             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1220             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1221             (remove-hook 'gnus-article-prepare-hook
1222                          'hilit-rehighlight-buffer-quietly)))
1223
1224
1225 \f
1226 ;; Internal variables
1227
1228 ;; Avoid highlighting in kill files.
1229 (defvar gnus-summary-inhibit-highlight nil)
1230 (defvar gnus-newsgroup-selected-overlay nil)
1231
1232 (defvar gnus-article-mode-map nil)
1233 (defvar gnus-dribble-buffer nil)
1234 (defvar gnus-headers-retrieved-by nil)
1235 (defvar gnus-article-reply nil)
1236 (defvar gnus-override-method nil)
1237 (defvar gnus-article-check-size nil)
1238
1239 (defvar gnus-current-score-file nil)
1240 (defvar gnus-internal-global-score-files nil)
1241 (defvar gnus-score-file-list nil)
1242
1243
1244 (defvar gnus-current-move-group nil)
1245
1246 (defvar gnus-newsgroup-dependencies nil)
1247 (defvar gnus-newsgroup-threads nil)
1248 (defvar gnus-newsgroup-async nil)
1249 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1250
1251 (defvar gnus-newsgroup-adaptive nil)
1252
1253 (defvar gnus-summary-display-table nil)
1254
1255 (defconst gnus-group-line-format-alist
1256   (list (list ?M 'marked ?c)
1257         (list ?S 'subscribed ?c)
1258         (list ?L 'level ?d)
1259         (list ?N 'number ?s)
1260         (list ?I 'number-of-dormant ?d)
1261         (list ?T 'number-of-ticked ?d)
1262         (list ?R 'number-of-read ?s)
1263         (list ?t 'number-total ?d)
1264         (list ?y 'number-of-unread-unticked ?s)
1265         (list ?i 'number-of-ticked-and-dormant ?d)
1266         (list ?g 'group ?s)
1267         (list ?G 'qualified-group ?s)
1268         (list ?D 'newsgroup-description ?s)
1269         (list ?o 'moderated ?c)
1270         (list ?O 'moderated-string ?s)
1271         (list ?p 'process-marked ?c)
1272         (list ?s 'news-server ?s)
1273         (list ?n 'news-method ?s)
1274         (list ?z 'news-method-string ?s)
1275         (list ?u 'user-defined ?s)))
1276
1277 (defconst gnus-summary-line-format-alist 
1278   (list (list ?N 'number ?d)
1279         (list ?S 'subject ?s)
1280         (list ?s 'subject-or-nil ?s)
1281         (list ?n 'name ?s)
1282         (list ?A 'address ?s)
1283         (list ?F 'from ?s)
1284         (list ?x (macroexpand '(header-xref header)) ?s)
1285         (list ?D (macroexpand '(header-date header)) ?s)
1286         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1287         (list ?M (macroexpand '(header-id header)) ?s)
1288         (list ?r (macroexpand '(header-references header)) ?s)
1289         (list ?c '(or (header-chars header) 0) ?d)
1290         (list ?L 'lines ?d)
1291         (list ?I 'indentation ?s)
1292         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1293         (list ?R 'replied ?c)
1294         (list ?\[ 'opening-bracket ?c)
1295         (list ?\] 'closing-bracket ?c)
1296         (list ?\> '(make-string level ? ) ?s)
1297         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1298         (list ?i 'score ?d)
1299         (list ?z 'score-char ?c)
1300         (list ?U 'unread ?c)
1301         (list ?t '(gnus-summary-number-of-articles-in-thread 
1302                    (or (prog1 gnus-tmp-adopt-thread 
1303                          (setq gnus-tmp-adopt-thread nil))
1304                        (if (boundp 'thread) (symbol-value 'thread)
1305                          thread nil)))
1306                    ?d)
1307         (list ?e '(gnus-summary-number-of-articles-in-thread 
1308                    (or gnus-tmp-adopt-thread 
1309                        (if (boundp 'thread) (symbol-value 'thread)
1310                          thread nil)) t)
1311                    ?c)
1312         (list ?u 'user-defined ?s))
1313   "An alist of format specifications that can appear in summary lines,
1314 and what variables they correspond with, along with the type of the
1315 variable (string, integer, character, etc).")
1316
1317 (defconst gnus-summary-dummy-line-format-alist
1318   (list (list ?S 'subject ?s)
1319         (list ?N 'number ?d)
1320         (list ?u 'user-defined ?s)))
1321
1322 (defconst gnus-summary-mode-line-format-alist 
1323   (list (list ?G 'group-name ?s)
1324         (list ?g '(gnus-short-group-name group-name) ?s)
1325         (list ?A 'article-number ?d)
1326         (list ?Z 'unread-and-unselected ?s)
1327         (list ?V 'gnus-version ?s)
1328         (list ?U 'unread ?d)
1329         (list ?S 'subject ?s)
1330         (list ?e 'unselected ?d)
1331         (list ?u 'user-defined ?s)
1332         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1333
1334 (defconst gnus-group-mode-line-format-alist 
1335   (list (list ?S 'news-server ?s)
1336         (list ?M 'news-method ?s)
1337         (list ?u 'user-defined ?s)))
1338
1339 (defvar gnus-have-read-active-file nil)
1340
1341 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1342   "The mail address of the Gnus maintainers.")
1343
1344 (defconst gnus-version "(ding) Gnus v0.97"
1345   "Version number for this version of Gnus.")
1346
1347 (defvar gnus-info-nodes
1348   '((gnus-group-mode            "(gnus)The Group Buffer")
1349     (gnus-summary-mode          "(gnus)The Summary Buffer")
1350     (gnus-article-mode          "(gnus)The Article Buffer"))
1351   "Assoc list of major modes and related Info nodes.")
1352
1353 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1354   "The location of the (ding) Gnus documentation group.")
1355
1356 (defvar gnus-group-buffer "*Group*")
1357 (defvar gnus-summary-buffer "*Summary*")
1358 (defvar gnus-article-buffer "*Article*")
1359 (defvar gnus-server-buffer "*Server*")
1360
1361 (defvar gnus-work-buffer " *gnus work*")
1362
1363 (defvar gnus-buffer-list nil
1364   "Gnus buffers that should be killed on exit.")
1365
1366 (defvar gnus-server-alist nil
1367   "List of available servers.")
1368
1369 (defvar gnus-variable-list
1370   '(gnus-newsrc-options gnus-newsrc-options-n
1371     gnus-newsrc-last-checked-date 
1372     gnus-newsrc-alist gnus-server-alist
1373     gnus-killed-list gnus-zombie-list)
1374   "Gnus variables saved in the quick startup file.")
1375
1376 (defvar gnus-overload-functions
1377   '((news-inews gnus-inews-news "rnewspost"))
1378   "Functions overloaded by gnus.
1379 It is a list of `(original overload &optional file)'.")
1380
1381 (defvar gnus-newsrc-options nil
1382   "Options line in the .newsrc file.")
1383
1384 (defvar gnus-newsrc-options-n nil
1385   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1386
1387 (defvar gnus-newsrc-last-checked-date nil
1388   "Date Gnus last asked server for new newsgroups.")
1389
1390 (defvar gnus-newsrc-alist nil
1391   "Assoc list of read articles.
1392 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1393
1394 (defvar gnus-newsrc-hashtb nil
1395   "Hashtable of gnus-newsrc-alist.")
1396
1397 (defvar gnus-killed-list nil
1398   "List of killed newsgroups.")
1399
1400 (defvar gnus-killed-hashtb nil
1401   "Hash table equivalent of gnus-killed-list.")
1402
1403 (defvar gnus-zombie-list nil
1404   "List of almost dead newsgroups.")
1405
1406 (defvar gnus-description-hashtb nil
1407   "Descriptions of newsgroups.")
1408
1409 (defvar gnus-list-of-killed-groups nil
1410   "List of newsgroups that have recently been killed by the user.")
1411
1412 (defvar gnus-active-hashtb nil
1413   "Hashtable of active articles.")
1414
1415 (defvar gnus-moderated-list nil
1416   "List of moderated newsgroups.")
1417
1418 (defvar gnus-group-marked nil)
1419
1420 (defvar gnus-current-startup-file nil
1421   "Startup file for the current host.")
1422
1423 (defvar gnus-last-search-regexp nil
1424   "Default regexp for article search command.")
1425
1426 (defvar gnus-last-shell-command nil
1427   "Default shell command on article.")
1428
1429 (defvar gnus-current-select-method nil
1430   "The current method for selecting a newsgroup.")
1431
1432 (defvar gnus-have-all-newsgroups nil)
1433
1434 (defvar gnus-article-internal-prepare-hook nil)
1435
1436 (defvar gnus-newsgroup-name nil)
1437 (defvar gnus-newsgroup-begin nil)
1438 (defvar gnus-newsgroup-end nil)
1439 (defvar gnus-newsgroup-last-rmail nil)
1440 (defvar gnus-newsgroup-last-mail nil)
1441 (defvar gnus-newsgroup-last-folder nil)
1442 (defvar gnus-newsgroup-last-file nil)
1443 (defvar gnus-newsgroup-auto-expire nil)
1444 (defvar gnus-newsgroup-active nil)
1445
1446 (defvar gnus-newsgroup-unreads nil
1447   "List of unread articles in the current newsgroup.")
1448
1449 (defvar gnus-newsgroup-unselected nil
1450   "List of unselected unread articles in the current newsgroup.")
1451
1452 (defvar gnus-newsgroup-marked nil
1453   "List of ticked articles in the current newsgroup (a subset of unread art).")
1454
1455 (defvar gnus-newsgroup-killed nil
1456   "List of ranges of articles that have been through the scoring process.")
1457
1458 (defvar gnus-newsgroup-kill-headers nil)
1459
1460 (defvar gnus-newsgroup-replied nil
1461   "List of articles that have been replied to in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-expirable nil
1464   "List of articles in the current newsgroup that can be expired.")
1465
1466 (defvar gnus-newsgroup-processable nil
1467   "List of articles in the current newsgroup that can be processed.")
1468
1469 (defvar gnus-newsgroup-bookmarks nil
1470   "List of articles in the current newsgroup that have bookmarks.")
1471
1472 (defvar gnus-newsgroup-dormant nil
1473   "List of dormant articles in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-scored nil
1476   "List of scored articles in the current newsgroup.")
1477
1478 (defvar gnus-newsgroup-headers nil
1479   "List of article headers in the current newsgroup.")
1480 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1481
1482 (defvar gnus-newsgroup-ancient nil
1483   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1484
1485 (defvar gnus-current-article nil)
1486 (defvar gnus-article-current nil)
1487 (defvar gnus-current-headers nil)
1488 (defvar gnus-have-all-headers nil)
1489 (defvar gnus-last-article nil)
1490 (defvar gnus-newsgroup-history nil)
1491 (defvar gnus-current-kill-article nil)
1492
1493 ;; Save window configuration.
1494 (defvar gnus-prev-winconf nil)
1495
1496 ;; Format specs
1497 (defvar gnus-summary-line-format-spec nil)
1498 (defvar gnus-summary-dummy-line-format-spec nil)
1499 (defvar gnus-group-line-format-spec nil)
1500 (defvar gnus-summary-mode-line-format-spec nil)
1501 (defvar gnus-article-mode-line-format-spec nil)
1502 (defvar gnus-group-mode-line-format-spec nil)
1503 (defvar gnus-summary-mark-positions nil)
1504
1505 (defvar gnus-summary-expunge-below nil)
1506 (defvar gnus-reffed-article-number nil)
1507
1508 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1509 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1510
1511 (defvar gnus-cache-removeable-articles nil)
1512
1513 (defconst gnus-summary-local-variables 
1514   '(gnus-newsgroup-name 
1515     gnus-newsgroup-begin gnus-newsgroup-end 
1516     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1517     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1518     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1519     gnus-newsgroup-unselected gnus-newsgroup-marked
1520     gnus-newsgroup-replied gnus-newsgroup-expirable
1521     gnus-newsgroup-processable gnus-newsgroup-killed
1522     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1523     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1524     gnus-current-article gnus-current-headers gnus-have-all-headers
1525     gnus-last-article gnus-article-internal-prepare-hook
1526     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1527     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1528     gnus-newsgroup-threads gnus-newsgroup-async
1529     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1530     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1531     gnus-newsgroup-history gnus-newsgroup-ancient
1532     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1533     gnus-cache-removeable-articles)
1534   "Variables that are buffer-local to the summary buffers.")
1535
1536 (defconst gnus-bug-message
1537   "Sending a bug report to the Gnus Towers.
1538 ========================================
1539
1540 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1541 be sent to the Gnus Bug Exterminators. 
1542
1543 At the bottom of the buffer you'll see lots of variable settings.
1544 Please do not delete those.  They will tell the Bug People what your
1545 environment is, so that it will be easier to locate the bugs.
1546
1547 If you have found a bug that makes Emacs go \"beep\", set
1548 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1549 and include the backtrace in your bug report.
1550
1551 Please describe the bug in annoying, painstaking detail.
1552
1553 Thank you for your help in stamping out bugs.
1554 ")
1555
1556 ;;; End of variables.
1557
1558 ;; Define some autoload functions Gnus might use.
1559 (eval-and-compile
1560
1561   ;; Various 
1562   (autoload 'metamail-buffer "metamail")
1563   (autoload 'Info-goto-node "info")
1564   (autoload 'hexl-hex-string-to-integer "hexl")
1565   (autoload 'pp "pp")
1566   (autoload 'pp-to-string "pp")
1567   (autoload 'pp-eval-expression "pp")
1568   (autoload 'mail-extract-address-components "mail-extr")
1569
1570   (autoload 'nnmail-split-fancy "nnmail")
1571   (autoload 'nnvirtual-catchup-group "nnvirtual")
1572
1573   ;; timezone
1574   (autoload 'timezone-make-date-arpa-standard "timezone")
1575   (autoload 'timezone-fix-time "timezone")
1576   (autoload 'timezone-make-sortable-date "timezone")
1577   (autoload 'timezone-make-time-string "timezone")
1578
1579   ;; rmail & friends
1580   (autoload 'mail-position-on-field "sendmail")
1581   (autoload 'mail-setup "sendmail")
1582   (autoload 'rmail-output "rmailout")
1583   (autoload 'news-mail-other-window "rnewspost")
1584   (autoload 'news-reply-yank-original "rnewspost")
1585   (autoload 'news-caesar-buffer-body "rnewspost")
1586   (autoload 'rmail-insert-rmail-file-header "rmail")
1587   (autoload 'rmail-count-new-messages "rmail")
1588   (autoload 'rmail-show-message "rmail")
1589
1590   ;; gnus-soup
1591   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1592   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1593   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1594   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1595   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1596   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1597   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1598
1599   ;; gnus-mh
1600   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1601   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1602   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1603   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1604   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1605   (autoload 'gnus-Folder-save-name "gnus-mh")
1606   (autoload 'gnus-folder-save-name "gnus-mh")
1607
1608   ;; gnus-vis misc
1609   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1610   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1611   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1612   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1613   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1614   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1615   (autoload 'gnus-summary-highlight-line "gnus-vis")
1616   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1617
1618   ;; gnus-vis article
1619   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1620   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1621   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1622   (autoload 'gnus-article-hide "gnus-vis" nil t)
1623   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1624   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1625   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1626   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1627   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1628   (autoload 'gnus-article-add-button "gnus-vis")
1629
1630   ;; gnus-cite
1631   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1632   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1633   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1634
1635   ;; gnus-kill
1636   (autoload 'gnus-kill "gnus-kill")
1637   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1638   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1639   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1640   (autoload 'gnus-execute "gnus-kill")
1641   (autoload 'gnus-expunge "gnus-kill")
1642
1643   ;; gnus-cache
1644   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1645   (autoload 'gnus-cache-save-buffers "gnus-cache")
1646   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1647   (autoload 'gnus-cache-request-article "gnus-cache")
1648   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1649   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1650   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1651   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1652
1653   ;; gnus-score
1654   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1655   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1656   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1657   (autoload 'gnus-score-save "gnus-score")
1658   (autoload 'gnus-score-headers "gnus-score")
1659   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1660   (autoload 'gnus-score-adaptive "gnus-score")
1661   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1662   (autoload 'gnus-score-find-trace "gnus-score")
1663
1664   ;; gnus-edit
1665   (autoload 'gnus-score-customize "gnus-edit" nil t)
1666
1667   ;; gnus-uu
1668   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1669   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1670   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1671   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1672   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1673   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1674   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1675   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1676   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1677   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1678   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1679   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1680   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1681   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1682   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1683   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1684   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1685   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1686   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1687   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1688   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1689   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1690
1691   ;; gnus-msg
1692   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1693   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1694   (autoload 'gnus-group-mail "gnus-msg" nil t)
1695   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1696   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1697   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1698   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1699   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1700   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1701   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1702   (autoload 'gnus-post-news "gnus-msg" nil t)
1703   (autoload 'gnus-inews-news "gnus-msg" nil t)
1704   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1705   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1706   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1707   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1708   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1709   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1710   (autoload 'gnus-mail-yank-original "gnus-msg")
1711   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1712   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1713   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1714   (autoload 'gnus-article-mail-with-original "gnus-msg")
1715   (autoload 'gnus-article-mail "gnus-msg")
1716   (autoload 'gnus-bug "gnus-msg" nil t)
1717
1718   ;; gnus-vm
1719   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1720   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1721   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1722   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1723   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1724   (autoload 'gnus-yank-article "gnus-vm" nil t)
1725
1726   )
1727
1728 \f
1729
1730 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1731 ;; If you want the cursor to go somewhere else, set these two
1732 ;; functions in some startup hook to whatever you want.
1733 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1734 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1735
1736 ;;; Various macros and substs.
1737
1738 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1739   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1740   (` (let ((GnusStartBufferWindow (selected-window)))
1741        (unwind-protect
1742            (progn
1743              (pop-to-buffer (, buffer))
1744              (,@ forms))
1745          (select-window GnusStartBufferWindow)))))
1746
1747 (defmacro gnus-gethash (string hashtable)
1748   "Get hash value of STRING in HASHTABLE."
1749   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1750   ;;(` (abbrev-expansion (, string) (, hashtable)))
1751   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1752
1753 (defmacro gnus-sethash (string value hashtable)
1754   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1755   ;; We cannot use define-abbrev since it only accepts string as value.
1756   ;; (set (intern string hashtable) value))
1757   (` (set (intern (, string) (, hashtable)) (, value))))
1758
1759 (defsubst gnus-buffer-substring (beg end)
1760   (buffer-substring (match-beginning beg) (match-end end)))
1761
1762 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1763 ;;   function `substring' might cut on a middle of multi-octet
1764 ;;   character.
1765
1766 (defun gnus-truncate-string (str width)
1767   (substring str 0 width))
1768
1769 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1770 ;; to limit the length of a string. This function is necessary since
1771 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1772 (defsubst gnus-limit-string (str width)
1773   (if (> (length str) width)
1774       (substring str 0 width)
1775     str))
1776
1777 (defsubst gnus-simplify-subject-re (subject)
1778   "Remove \"Re:\" from subject lines."
1779   (let ((case-fold-search t))
1780     (if (string-match "^re: *" subject)
1781         (substring subject (match-end 0))
1782       subject)))
1783
1784 (defsubst gnus-goto-char (point)
1785   (and point (goto-char point)))
1786
1787 (defmacro gnus-buffer-exists-p (buffer)
1788   (` (and (, buffer)
1789           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1790                    (, buffer)))))
1791
1792 (defmacro gnus-kill-buffer (buffer)
1793   (` (if (gnus-buffer-exists-p (, buffer))
1794          (kill-buffer (, buffer)))))
1795
1796 (defsubst gnus-point-at-bol ()
1797   "Return point at the beginning of line."
1798   (let ((p (point)))
1799     (beginning-of-line)
1800     (prog1
1801         (point)
1802       (goto-char p))))
1803
1804 (defsubst gnus-point-at-eol ()
1805   "Return point at the beginning of line."
1806   (let ((p (point)))
1807     (end-of-line)
1808     (prog1
1809         (point)
1810       (goto-char p))))
1811
1812 ;; Delete the current line (and the next N lines.);
1813 (defmacro gnus-delete-line (&optional n)
1814   (` (delete-region (progn (beginning-of-line) (point))
1815                     (progn (forward-line (, (or n 1))) (point)))))
1816
1817 ;;; Load the compatability functions. 
1818
1819 (require 'gnus-ems)
1820
1821 \f
1822 ;;;
1823 ;;; Gnus Utility Functions
1824 ;;;
1825
1826 (defun gnus-extract-address-components (from)
1827   (let (name address)
1828     ;; First find the address - the thing with the @ in it.  This may
1829     ;; not be accurate in mail addresses, but does the trick most of
1830     ;; the time in news messages.
1831     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1832         (setq address (substring from (match-beginning 0) (match-end 0))))
1833     ;; Then we check whether the "name <address>" format is used.
1834     (and address
1835          (string-match (concat "<" (regexp-quote address) ">") from)
1836          (and (setq name (substring from 0 (1- (match-beginning 0))))
1837               ;; Strip any quotes from the name.
1838               (string-match "\".*\"" name)
1839               (setq name (substring name 1 (1- (match-end 0))))))
1840     ;; If not, then "address (name)" is used.
1841     (or name
1842         (and (string-match "(.+)" from)
1843              (setq name (substring from (1+ (match-beginning 0)) 
1844                                    (1- (match-end 0)))))
1845         (and (string-match "()" from)
1846              (setq name address))
1847         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1848         ;; XOVER might not support folded From headers.
1849         (and (string-match "(.*" from)
1850              (setq name (substring from (1+ (match-beginning 0)) 
1851                                    (match-end 0)))))
1852     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1853     (list (or name from) (or address from))))
1854
1855 (defun gnus-fetch-field (field)
1856   "Return the value of the header FIELD of current article."
1857   (save-excursion
1858     (save-restriction
1859       (let ((case-fold-search t))
1860         (gnus-narrow-to-headers)
1861         (mail-fetch-field field)))))
1862
1863 (defun gnus-goto-colon ()
1864   (beginning-of-line)
1865   (search-forward ":" (gnus-point-at-eol) t))
1866
1867 (defun gnus-narrow-to-headers ()
1868   (widen)
1869   (save-excursion
1870     (narrow-to-region
1871      (goto-char (point-min))
1872      (if (search-forward "\n\n" nil t)
1873          (1- (point))
1874        (point-max)))))
1875
1876 (defun gnus-update-format-specifications ()
1877   (gnus-make-thread-indent-array)
1878   (setq gnus-summary-line-format-spec 
1879         (gnus-parse-format
1880          gnus-summary-line-format gnus-summary-line-format-alist))
1881   (gnus-update-summary-mark-positions)
1882   (setq gnus-summary-dummy-line-format-spec 
1883         (gnus-parse-format gnus-summary-dummy-line-format 
1884                            gnus-summary-dummy-line-format-alist))
1885   (setq gnus-group-line-format-spec
1886         (gnus-parse-format 
1887          gnus-group-line-format 
1888          gnus-group-line-format-alist))
1889   (if (and (string-match "%D" gnus-group-line-format)
1890            (not gnus-description-hashtb)
1891            gnus-read-active-file)
1892       (gnus-read-all-descriptions-files))
1893   (setq gnus-summary-mode-line-format-spec 
1894         (gnus-parse-format gnus-summary-mode-line-format 
1895                            gnus-summary-mode-line-format-alist))
1896   (setq gnus-article-mode-line-format-spec 
1897         (gnus-parse-format gnus-article-mode-line-format 
1898                            gnus-summary-mode-line-format-alist))
1899   (setq gnus-group-mode-line-format-spec 
1900         (gnus-parse-format gnus-group-mode-line-format 
1901                            gnus-group-mode-line-format-alist)))
1902
1903 (defun gnus-update-summary-mark-positions ()
1904   (save-excursion
1905     (let ((gnus-replied-mark 129)
1906           (gnus-score-below-mark 130)
1907           (gnus-score-over-mark 130)
1908           (thread nil)
1909           pos)
1910       (gnus-set-work-buffer)
1911       (gnus-summary-insert-line 
1912        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1913       (goto-char (point-min))
1914       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1915                                          (- (point) 2)))))
1916       (goto-char (point-min))
1917       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1918                                           (- (point) 2))) pos))
1919       (goto-char (point-min))
1920       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1921                                         (- (point) 2))) pos))
1922       (setq gnus-summary-mark-positions pos))))
1923
1924 (defun gnus-format-max-width (form length)
1925   (let* ((val (eval form))
1926          (valstr (if (numberp val) (int-to-string val) val)))
1927     (gnus-limit-string valstr length)))
1928
1929 (defun gnus-set-mouse-face (string)
1930   ;; Set mouse face property on STRING.
1931   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1932   string)
1933
1934 (defun gnus-parse-format (format spec-alist)
1935   ;; This function parses the FORMAT string with the help of the
1936   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1937   ;; string.  If the FORMAT string contains the specifiers %( and %)
1938   ;; the text between them will have the mouse-face text property.
1939   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1940       (if (and gnus-visual gnus-mouse-face)
1941           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1942                 (button (substring format (match-beginning 2) (match-end 2)))
1943                 (post (substring format (match-beginning 3) (match-end 3))))
1944             (list 'concat
1945                   (gnus-parse-simple-format pre spec-alist)
1946                   (list 'gnus-set-mouse-face
1947                         (gnus-parse-simple-format button spec-alist))
1948                   (gnus-parse-simple-format post spec-alist)))
1949         (gnus-parse-simple-format
1950          (concat (substring format (match-beginning 1) (match-end 1))
1951                  (substring format (match-beginning 2) (match-end 2))
1952                  (substring format (match-beginning 3) (match-end 3)))
1953          spec-alist))
1954     (gnus-parse-simple-format format spec-alist)))
1955
1956 (defun gnus-parse-simple-format (format spec-alist)
1957   ;; This function parses the FORMAT string with the help of the
1958   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1959   ;; string. The list will consist of the symbol `format', a format
1960   ;; specification string, and a list of forms depending on the
1961   ;; SPEC-ALIST.
1962   (let ((max-width 0)
1963         spec flist fstring newspec elem beg)
1964     (save-excursion
1965       (gnus-set-work-buffer)
1966       (insert format)
1967       (goto-char (point-min))
1968       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1969         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1970                                                      (match-end 2))))
1971         ;; First check if there are any specs that look anything like
1972         ;; "%12,12A", ie. with a "max width specification". These have
1973         ;; to be treated specially.
1974         (if (setq beg (match-beginning 1))
1975             (setq max-width 
1976                   (string-to-int 
1977                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1978           (setq max-width 0)
1979           (setq beg (match-beginning 2)))
1980         ;; Find the specification from `spec-alist'.
1981         (if (not (setq elem (cdr (assq spec spec-alist))))
1982             (setq elem '("*" ?s)))
1983         ;; Treat user defined format specifiers specially
1984         (and (eq (car elem) 'user-defined)
1985              (setq elem
1986                    (list 
1987                     (list (intern (concat "gnus-user-format-function-"
1988                                           (buffer-substring
1989                                            (match-beginning 3)
1990                                            (match-end 3))))
1991                           'header)
1992                     ?s))
1993              (delete-region (match-beginning 3) (match-end 3)))
1994         (if (not (zerop max-width))
1995             (let ((el (car elem)))
1996               (cond ((= (car (cdr elem)) ?c) 
1997                      (setq el (list 'char-to-string el)))
1998                     ((= (car (cdr elem)) ?d)
1999                      (numberp el) (setq el (list 'int-to-string el))))
2000               (setq flist (cons (list 'gnus-format-max-width el max-width) 
2001                                 flist))
2002               (setq newspec ?s))
2003           (setq flist (cons (car elem) flist))
2004           (setq newspec (car (cdr elem))))
2005         ;; Remove the old specification (and possibly a ",12" string).
2006         (delete-region beg (match-end 2))
2007         ;; Insert the new specification.
2008         (goto-char beg)
2009         (insert newspec))
2010       (setq fstring (buffer-substring 1 (point-max))))
2011     (cons 'format (cons fstring (nreverse flist)))))
2012
2013 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2014 (defun gnus-read-init-file ()
2015   (and gnus-init-file
2016        (or (and (file-exists-p gnus-init-file) 
2017                 ;; Don't try to load a directory.
2018                 (not (file-directory-p gnus-init-file)))
2019            (file-exists-p (concat gnus-init-file ".el"))
2020            (file-exists-p (concat gnus-init-file ".elc")))
2021        (load gnus-init-file nil t)))
2022
2023 (defun gnus-set-work-buffer ()
2024   (if (get-buffer gnus-work-buffer)
2025       (progn
2026         (set-buffer gnus-work-buffer)
2027         (erase-buffer))
2028     (set-buffer (get-buffer-create gnus-work-buffer))
2029     (kill-all-local-variables)
2030     (buffer-disable-undo (current-buffer))
2031     (gnus-add-current-to-buffer-list)))
2032
2033 ;; Article file names when saving.
2034
2035 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2036   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2037 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2038 Otherwise, it is like ~/News/news/group/num."
2039   (let ((default
2040           (expand-file-name
2041            (concat (if (gnus-use-long-file-name 'not-save)
2042                        (gnus-capitalize-newsgroup newsgroup)
2043                      (gnus-newsgroup-directory-form newsgroup))
2044                    "/" (int-to-string (header-number headers)))
2045            (or gnus-article-save-directory "~/News"))))
2046     (if (and last-file
2047              (string-equal (file-name-directory default)
2048                            (file-name-directory last-file))
2049              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2050         default
2051       (or last-file default))))
2052
2053 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2054   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2055 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2056 Otherwise, it is like ~/News/news/group/num."
2057   (let ((default
2058           (expand-file-name
2059            (concat (if (gnus-use-long-file-name 'not-save)
2060                        newsgroup
2061                      (gnus-newsgroup-directory-form newsgroup))
2062                    "/" (int-to-string (header-number headers)))
2063            (or gnus-article-save-directory "~/News"))))
2064     (if (and last-file
2065              (string-equal (file-name-directory default)
2066                            (file-name-directory last-file))
2067              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2068         default
2069       (or last-file default))))
2070
2071 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2072   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2073 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2074 Otherwise, it is like ~/News/news/group/news."
2075   (or last-file
2076       (expand-file-name
2077        (if (gnus-use-long-file-name 'not-save)
2078            (gnus-capitalize-newsgroup newsgroup)
2079          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2080        (or gnus-article-save-directory "~/News"))))
2081
2082 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2083   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2084 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2085 Otherwise, it is like ~/News/news/group/news."
2086   (or last-file
2087       (expand-file-name
2088        (if (gnus-use-long-file-name 'not-save)
2089            newsgroup
2090          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2091        (or gnus-article-save-directory "~/News"))))
2092
2093 ;; For subscribing new newsgroup
2094
2095 (defun gnus-subscribe-hierarchical-interactive (groups)
2096   (let ((groups (sort groups 'string<))
2097         prefixes prefix start ans group starts)
2098     (while groups
2099       (setq prefixes (list "^"))
2100       (while (and groups prefixes)
2101         (while (not (string-match (car prefixes) (car groups)))
2102           (setq prefixes (cdr prefixes)))
2103         (setq prefix (car prefixes))
2104         (setq start (1- (length prefix)))
2105         (if (and (string-match "[^\\.]\\." (car groups) start)
2106                  (cdr groups)
2107                  (setq prefix 
2108                        (concat "^" (substring (car groups) 0 (match-end 0))))
2109                  (string-match prefix (car (cdr groups))))
2110             (progn
2111               (setq prefixes (cons prefix prefixes))
2112               (message "Descend hierarchy %s? ([y]nsq): " 
2113                        (substring prefix 1 (1- (length prefix))))
2114               (setq ans (read-char))
2115               (cond ((= ans ?n)
2116                      (while (and groups 
2117                                  (string-match prefix 
2118                                                (setq group (car groups))))
2119                        (setq gnus-killed-list 
2120                              (cons group gnus-killed-list))
2121                        (gnus-sethash group group gnus-killed-hashtb)
2122                        (setq groups (cdr groups)))
2123                      (setq starts (cdr starts)))
2124                     ((= ans ?s)
2125                      (while (and groups 
2126                                  (string-match prefix 
2127                                                (setq group (car groups))))
2128                        (gnus-sethash group group gnus-killed-hashtb)
2129                        (gnus-subscribe-alphabetically (car groups))
2130                        (setq groups (cdr groups)))
2131                      (setq starts (cdr starts)))
2132                     ((= ans ?q)
2133                      (while groups
2134                        (setq group (car groups))
2135                        (setq gnus-killed-list (cons group gnus-killed-list))
2136                        (gnus-sethash group group gnus-killed-hashtb)
2137                        (setq groups (cdr groups))))
2138                     (t nil)))
2139           (message "Subscribe %s? ([n]yq)" (car groups))
2140           (setq ans (read-char))
2141           (setq group (car groups))
2142           (cond ((= ans ?y)
2143                  (gnus-subscribe-alphabetically (car groups))
2144                  (gnus-sethash group group gnus-killed-hashtb))
2145                 ((= ans ?q)
2146                  (while groups
2147                    (setq group (car groups))
2148                    (setq gnus-killed-list (cons group gnus-killed-list))
2149                    (gnus-sethash group group gnus-killed-hashtb)
2150                    (setq groups (cdr groups))))
2151                 (t 
2152                  (setq gnus-killed-list (cons group gnus-killed-list))
2153                  (gnus-sethash group group gnus-killed-hashtb)))
2154           (setq groups (cdr groups)))))))
2155
2156 (defun gnus-subscribe-randomly (newsgroup)
2157   "Subscribe new NEWSGROUP by making it the first newsgroup."
2158   (gnus-subscribe-newsgroup newsgroup))
2159
2160 (defun gnus-subscribe-alphabetically (newgroup)
2161   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2162   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2163   (let ((groups (cdr gnus-newsrc-alist))
2164         before)
2165     (while (and (not before) groups)
2166       (if (string< newgroup (car (car groups)))
2167           (setq before (car (car groups)))
2168         (setq groups (cdr groups))))
2169     (gnus-subscribe-newsgroup newgroup before)))
2170
2171 (defun gnus-subscribe-hierarchically (newgroup)
2172   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2173   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2174   (save-excursion
2175     (set-buffer (find-file-noselect gnus-current-startup-file))
2176     (let ((groupkey newgroup)
2177           before)
2178       (while (and (not before) groupkey)
2179         (goto-char (point-min))
2180         (let ((groupkey-re
2181                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2182           (while (and (re-search-forward groupkey-re nil t)
2183                       (progn
2184                         (setq before (buffer-substring
2185                                       (match-beginning 1) (match-end 1)))
2186                         (string< before newgroup)))))
2187         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2188         (setq groupkey
2189               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2190                   (substring groupkey (match-beginning 1) (match-end 1)))))
2191       (gnus-subscribe-newsgroup newgroup before))))
2192
2193 (defun gnus-subscribe-interactively (newsgroup)
2194   "Subscribe new NEWSGROUP interactively.
2195 It is inserted in hierarchical newsgroup order if subscribed. If not,
2196 it is killed."
2197   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2198       (gnus-subscribe-hierarchically newsgroup)
2199     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2200
2201 (defun gnus-subscribe-zombies (newsgroup)
2202   "Make new NEWSGROUP a zombie group."
2203   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2204
2205 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2206   "Subscribe new NEWSGROUP.
2207 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2208 the first newsgroup."
2209   ;; We subscribe the group by changing its level to `subscribed'.
2210   (gnus-group-change-level 
2211    newsgroup gnus-level-default-subscribed
2212    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2213   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2214
2215 ;; For directories
2216
2217 (defun gnus-newsgroup-directory-form (newsgroup)
2218   "Make hierarchical directory name from NEWSGROUP name."
2219   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2220         (len (length newsgroup))
2221         idx)
2222     ;; If this is a foreign group, we don't want to translate the
2223     ;; entire name.  
2224     (if (setq idx (string-match ":" newsgroup))
2225         (aset newsgroup idx ?/)
2226       (setq idx 0))
2227     ;; Replace all occurrences of `.' with `/'.
2228     (while (< idx len)
2229       (if (= (aref newsgroup idx) ?.)
2230           (aset newsgroup idx ?/))
2231       (setq idx (1+ idx)))
2232     newsgroup))
2233
2234 (defun gnus-make-directory (dir)
2235   "Make DIRECTORY recursively."
2236   (let* ((dir (expand-file-name dir default-directory))
2237          dirs)
2238     (if (string-match "/$" dir)
2239         (setq dir (substring dir 0 (match-beginning 0))))
2240     (while (not (file-exists-p dir))
2241       (setq dirs (cons dir dirs))
2242       (string-match "/[^/]+$" dir)
2243       (setq dir (substring dir 0 (match-beginning 0))))
2244     (while dirs
2245       (make-directory (car dirs))
2246       (setq dirs (cdr dirs)))))
2247
2248 (defun gnus-capitalize-newsgroup (newsgroup)
2249   "Capitalize NEWSGROUP name."
2250   (and (not (zerop (length newsgroup)))
2251        (concat (char-to-string (upcase (aref newsgroup 0)))
2252                (substring newsgroup 1))))
2253
2254 ;; Var
2255
2256 (defun gnus-simplify-subject (subject &optional re-only)
2257   "Remove `Re:' and words in parentheses.
2258 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2259   (let ((case-fold-search t))           ;Ignore case.
2260     ;; Remove `Re:' and `Re^N:'.
2261     (if (string-match "^re:[ \t]*" subject)
2262         (setq subject (substring subject (match-end 0))))
2263     ;; Remove words in parentheses from end.
2264     (or re-only
2265         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2266           (setq subject (substring subject 0 (match-beginning 0)))))
2267     ;; Return subject string.
2268     subject))
2269
2270 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2271 ;; all whitespace.
2272 (defun gnus-simplify-subject-fuzzy (subject)
2273   (let ((case-fold-search t))
2274     (save-excursion
2275       (gnus-set-work-buffer)
2276       (insert subject)
2277       (inline (gnus-simplify-buffer-fuzzy))
2278       (buffer-string))))
2279
2280 (defun gnus-simplify-buffer-fuzzy ()
2281   (goto-char (point-min))
2282   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2283   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2284                             nil t)
2285     (replace-match "" t t))
2286   (goto-char (point-min))
2287   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2288     (replace-match "" t t))
2289   (goto-char (point-min))
2290   (while (re-search-forward "[ \t]+" nil t)
2291     (replace-match " " t t))
2292   (goto-char (point-min))
2293   (while (re-search-forward "[ \t]+$" nil t)
2294     (replace-match "" t t))
2295   (goto-char (point-min))
2296   (while (re-search-forward "^[ \t]+" nil t)
2297     (replace-match "" t t))
2298   (if gnus-simplify-subject-fuzzy-regexp
2299       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2300         (replace-match "" t t)))
2301     )
2302
2303 ;; Add the current buffer to the list of buffers to be killed on exit. 
2304 (defun gnus-add-current-to-buffer-list ()
2305   (or (memq (current-buffer) gnus-buffer-list)
2306       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2307
2308 (defun gnus-string> (s1 s2)
2309   (not (or (string< s1 s2)
2310            (string= s1 s2))))
2311
2312 ;; Functions accessing headers.
2313 ;; Functions are more convenient than macros in some cases.
2314
2315 (defun gnus-header-number (header)
2316   (header-number header))
2317
2318 (defun gnus-header-subject (header)
2319   (header-subject header))
2320
2321 (defun gnus-header-from (header)
2322   (header-from header))
2323
2324 (defun gnus-header-xref (header)
2325   (header-xref header))
2326
2327 (defun gnus-header-lines (header)
2328   (header-lines header))
2329
2330 (defun gnus-header-date (header)
2331   (header-date header))
2332
2333 (defun gnus-header-id (header)
2334   (header-id header))
2335
2336 (defun gnus-header-references (header)
2337   (header-references header))
2338
2339 ;;; General various misc type functions.
2340
2341 (defun gnus-clear-system ()
2342   "Clear all variables and buffers."
2343   ;; Clear Gnus variables.
2344   (let ((variables gnus-variable-list))
2345     (while variables
2346       (set (car variables) nil)
2347       (setq variables (cdr variables))))
2348   ;; Clear other internal variables.
2349   (setq gnus-list-of-killed-groups nil
2350         gnus-have-read-active-file nil
2351         gnus-newsrc-alist nil
2352         gnus-newsrc-hashtb nil
2353         gnus-killed-list nil
2354         gnus-zombie-list nil
2355         gnus-killed-hashtb nil
2356         gnus-active-hashtb nil
2357         gnus-moderated-list nil
2358         gnus-description-hashtb nil
2359         gnus-newsgroup-headers nil
2360         gnus-newsgroup-headers-hashtb-by-number nil
2361         gnus-newsgroup-name nil
2362         gnus-server-alist nil
2363         gnus-current-select-method nil)
2364   ;; Reset any score variables.
2365   (and (boundp 'gnus-score-cache)
2366        (set 'gnus-score-cache nil))
2367   (and (boundp 'gnus-internal-global-score-files)
2368        (set 'gnus-internal-global-score-files nil))
2369   ;; Kill the startup file.
2370   (and gnus-current-startup-file
2371        (get-file-buffer gnus-current-startup-file)
2372        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2373   ;; Save any cache buffers.
2374   (and gnus-use-cache (gnus-cache-save-buffers))
2375   ;; Clear the dribble buffer.
2376   (gnus-dribble-clear)
2377   ;; Kill global KILL file buffer.
2378   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2379       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2380   (gnus-kill-buffer nntp-server-buffer)
2381   ;; Kill Gnus buffers.
2382   (while gnus-buffer-list
2383     (gnus-kill-buffer (car gnus-buffer-list))
2384     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2385
2386 (defun gnus-windows-old-to-new (setting)
2387   (if (symbolp setting)
2388       (setq setting 
2389             (cond ((eq setting 'SelectArticle)
2390                    'article)
2391                   ((eq setting 'SelectSubject)
2392                    'summary)
2393                   ((eq setting 'SelectNewsgroup)
2394                    'group)
2395                   (t setting))))
2396   (if (or (listp setting)
2397           (not (and gnus-window-configuration
2398                     (memq setting '(group summary article)))))
2399       setting
2400     (let* ((setting (if (eq setting 'group) 
2401                         (if (assq 'newsgroup gnus-window-configuration)
2402                             'newsgroup
2403                           'newsgroups) setting))
2404            (elem (car (cdr (assq setting gnus-window-configuration))))
2405            (total (apply '+ elem))
2406            (types '(group summary article))
2407            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2408            (i 0)
2409            perc
2410            out)
2411       (while (< i 3)
2412         (or (zerop (nth i elem))
2413             (progn
2414               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2415               (setq out (cons (if (eq pbuf (nth i types))
2416                                   (vector (nth i types) perc 'point)
2417                                 (vector (nth i types) perc))
2418                               out))))
2419         (setq i (1+ i)))
2420       (list (nreverse out)))))
2421            
2422 (defun gnus-add-configuration (conf)
2423   (setq gnus-buffer-configuration 
2424         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2425                          gnus-buffer-configuration))))
2426
2427 (defun gnus-configure-windows (setting &optional force)
2428   (setq setting (gnus-windows-old-to-new setting))
2429   (let ((r (if (symbolp setting)
2430                (cdr (assq setting gnus-buffer-configuration))
2431              setting))
2432         (in-buf (current-buffer))
2433         rule val w height hor ohor heights sub jump-buffer
2434         rel total to-buf all-visible)
2435     (or r (error "No such setting: %s" setting))
2436
2437     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2438         ;; All the windows mentioned are already visibe, so we just
2439         ;; put point in the assigned buffer, and do not touch the
2440         ;; winconf. 
2441         (select-window (get-buffer-window all-visible))
2442
2443       ;; Either remove all windows or just remove all Gnus windows.
2444       (if gnus-use-full-window
2445           (delete-other-windows)
2446         (gnus-remove-some-windows)
2447         (switch-to-buffer nntp-server-buffer))
2448
2449       (while r
2450         (setq hor (car r)
2451               ohor nil)
2452
2453         ;; We have to do the (possible) horizontal splitting before the
2454         ;; vertical. 
2455         (if (and (listp (car hor)) 
2456                  (eq (car (car hor)) 'horizontal))
2457             (progn
2458               (split-window 
2459                nil
2460                (if (integerp (nth 1 (car hor)))
2461                    (nth 1 (car hor))
2462                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2463                t)
2464               (setq hor (cdr hor))))
2465
2466         ;; Go through the rules and eval the elements that are to be
2467         ;; evaled.  
2468         (while hor
2469           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2470               (progn
2471                 ;; Expand short buffer name.
2472                 (setq w (aref val 0))
2473                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2474                      (progn
2475                        (setq val (apply 'vector (mapcar 'identity val)))
2476                        (aset val 0 w)))
2477                 (setq ohor (cons val ohor))))
2478           (setq hor (cdr hor)))
2479         (setq rule (cons (nreverse ohor) rule))
2480         (setq r (cdr r)))
2481       (setq rule (nreverse rule))
2482
2483       ;; We tally the window sizes.
2484       (setq total (window-height))
2485       (while rule
2486         (setq hor (car rule))
2487         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2488             (setq hor (cdr hor)))
2489         (setq sub 0)
2490         (while hor
2491           (setq rel (aref (car hor) 1)
2492                 heights (cons
2493                          (cond ((and (floatp rel) (= 1.0 rel))
2494                                 'x)
2495                                ((integerp rel)
2496                                 rel)
2497                                (t
2498                                 (max (floor (* total rel)) 4)))
2499                          heights)
2500                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2501                 hor (cdr hor)))
2502         (setq heights (nreverse heights)
2503               hor (car rule))
2504
2505         ;; We then go through these heighs and create windows for them.
2506         (while heights
2507           (setq height (car heights)
2508                 heights (cdr heights))
2509           (and (eq height 'x)
2510                (setq height (- total sub)))
2511           (and heights
2512                (split-window nil height))
2513           (setq to-buf (aref (car hor) 0))
2514           (switch-to-buffer 
2515            (cond ((not to-buf)
2516                   in-buf)
2517                  ((symbolp to-buf)
2518                   (symbol-value (aref (car hor) 0)))
2519                  (t
2520                   (aref (car hor) 0))))
2521           (and (> (length (car hor)) 2)
2522                (eq (aref (car hor) 2) 'point)
2523                (setq jump-buffer (current-buffer)))
2524           (other-window 1)
2525           (setq hor (cdr hor)))
2526       
2527         (setq rule (cdr rule)))
2528
2529       ;; Finally, we pop to the buffer that's supposed to have point. 
2530       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2531
2532       (select-window (get-buffer-window jump-buffer))
2533       (set-buffer jump-buffer))))
2534
2535 (defun gnus-all-windows-visible-p (rule)
2536   (let (invisible hor jump-buffer val buffer)
2537     ;; Go through the rules and eval the elements that are to be
2538     ;; evaled.  
2539     (while (and rule (not invisible))
2540       (setq hor (car rule)
2541             rule (cdr rule))
2542       (while (and hor (not invisible))
2543         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2544             (progn
2545               ;; Expand short buffer name.
2546               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2547                                (aref val 0)))
2548               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2549                              buffer))
2550               (setq jump-buffer (and (> (length val) 2)
2551                                      (eq 'point (aref val 2))
2552                                      buffer))
2553               (setq invisible (not (get-buffer-window buffer)))))
2554         (setq hor (cdr hor))))
2555     (and (not invisible) jump-buffer)))
2556
2557       
2558 (defun gnus-remove-some-windows ()
2559   (let ((buffers gnus-window-to-buffer)
2560         (first t)
2561         buf)
2562     (save-excursion
2563       ;; Remove windows on all known Gnus buffers.
2564       (while buffers
2565         (setq buf (cdr (car buffers)))
2566         (if (symbolp buf)
2567             (setq buf (and (boundp buf) (symbol-value buf))))
2568         (and buf 
2569              (get-buffer-window buf)
2570              (progn
2571                (if first
2572                    (progn
2573                      (pop-to-buffer buf)
2574                      (switch-to-buffer nntp-server-buffer)
2575                      (setq first nil))
2576                  (delete-window (get-buffer-window buf)))))
2577         (setq buffers (cdr buffers)))
2578       ;; Remove windows on *all* summary buffers.
2579       (let ((buffers (buffer-list)))
2580         (while buffers
2581           (if (and (string-match 
2582                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2583                    (get-buffer-window (car buffers)))
2584               (delete-window (get-buffer-window (car buffers))))
2585           (setq buffers (cdr buffers)))))))
2586                           
2587 (defun gnus-version ()
2588   "Version numbers of this version of Gnus."
2589   (interactive)
2590   (let ((methods gnus-valid-select-methods)
2591         (mess gnus-version)
2592         meth)
2593     ;; Go through all the legal select methods and add their version
2594     ;; numbers to the total version string. Only the backends that are
2595     ;; currently in use will have their message numbers taken into
2596     ;; consideration. 
2597     (while methods
2598       (setq meth (intern (concat (car (car methods)) "-version")))
2599       (and (boundp meth)
2600            (stringp (symbol-value meth))
2601            (setq mess (concat mess "; " (symbol-value meth))))
2602       (setq methods (cdr methods)))
2603     (gnus-message 2 mess)))
2604
2605 (defun gnus-info-find-node ()
2606   "Find Info documentation of Gnus."
2607   (interactive)
2608   ;; Enlarge info window if needed.
2609   (let ((mode major-mode))
2610     (gnus-configure-windows 'info)
2611     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2612
2613 (defun gnus-overload-functions (&optional overloads)
2614   "Overload functions specified by optional argument OVERLOADS.
2615 If nothing is specified, use the variable gnus-overload-functions."
2616   (let ((defs nil)
2617         (overloads (or overloads gnus-overload-functions)))
2618     (while overloads
2619       (setq defs (car overloads))
2620       (setq overloads (cdr overloads))
2621       ;; Load file before overloading function if necessary.  Make
2622       ;; sure we cannot use `require' always.
2623       (and (not (fboundp (car defs)))
2624            (car (cdr (cdr defs)))
2625            (load (car (cdr (cdr defs))) nil 'nomessage))
2626       (fset (car defs) (car (cdr defs))))))
2627
2628 (defun gnus-replace-chars-in-string (string from to)
2629   "Replace characters in STRING from FROM to TO."
2630   (let ((string (substring string 0))   ;Copy string.
2631         (len (length string))
2632         (idx 0))
2633     ;; Replace all occurrences of FROM with TO.
2634     (while (< idx len)
2635       (if (= (aref string idx) from)
2636           (aset string idx to))
2637       (setq idx (1+ idx)))
2638     string))
2639
2640 (defun gnus-days-between (date1 date2)
2641   ;; Return the number of days between date1 and date2.
2642   (- (gnus-day-number date1) (gnus-day-number date2)))
2643
2644 (defun gnus-day-number (date)
2645   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2646                      (timezone-parse-date date))))
2647     (timezone-absolute-from-gregorian 
2648      (nth 1 dat) (nth 2 dat) (car dat))))
2649
2650 ;; Returns a floating point number that says how many seconds have
2651 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2652 (defun gnus-seconds-since-epoch (date)
2653   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2654                         (timezone-parse-date date)))
2655          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2656                         (timezone-parse-time
2657                          (aref (timezone-parse-date date) 3))))
2658          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2659                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2660          (tday (- (timezone-absolute-from-gregorian 
2661                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2662                   (timezone-absolute-from-gregorian 
2663                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2664     (+ (nth 2 ttime)
2665        (* (nth 1 ttime) 60)
2666        (* 1.0 (nth 0 ttime) 60 60)
2667        (* 1.0 tday 60 60 24))))
2668
2669 (defun gnus-file-newer-than (file date)
2670   (let ((fdate (nth 5 (file-attributes file))))
2671     (or (> (car fdate) (car date))
2672         (and (= (car fdate) (car date))
2673              (> (nth 1 fdate) (nth 1 date))))))
2674
2675 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2676 ;; the echo area.
2677 (defun gnus-y-or-n-p (prompt)
2678   (prog1
2679       (y-or-n-p prompt)
2680     (message "")))
2681
2682 (defun gnus-yes-or-no-p (prompt)
2683   (prog1
2684       (yes-or-no-p prompt)
2685     (message "")))
2686
2687 ;; Check whether to use long file names.
2688 (defun gnus-use-long-file-name (symbol)
2689   ;; The variable has to be set...
2690   (and gnus-use-long-file-name
2691        ;; If it isn't a list, then we return t.
2692        (or (not (listp gnus-use-long-file-name))
2693            ;; If it is a list, and the list contains `symbol', we
2694            ;; return nil.  
2695            (not (memq symbol gnus-use-long-file-name)))))
2696
2697 ;; I suspect there's a better way, but I haven't taken the time to do
2698 ;; it yet. -erik selberg@cs.washington.edu
2699 (defun gnus-dd-mmm (messy-date)
2700   "Return a string like DD-MMM from a big messy string"
2701   (let ((datevec (timezone-parse-date messy-date)))
2702     (format "%2s-%s"
2703             (or (aref datevec 2) "??")
2704             (capitalize
2705              (or (car 
2706                   (nth (1- (string-to-number (aref datevec 1)))
2707                        timezone-months-assoc))
2708                  "???")))))
2709
2710 ;; Make a hash table (default and minimum size is 255).
2711 ;; Optional argument HASHSIZE specifies the table size.
2712 (defun gnus-make-hashtable (&optional hashsize)
2713   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2714
2715 ;; Make a number that is suitable for hashing; bigger than MIN and one
2716 ;; less than 2^x.
2717 (defun gnus-create-hash-size (min)
2718   (let ((i 1))
2719     (while (< i min)
2720       (setq i (* 2 i)))
2721     (1- i)))
2722
2723 ;; Show message if message has a lower level than `gnus-verbose'. 
2724 ;; Guide-line for numbers:
2725 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2726 ;; for things that take a long time, 7 - not very important messages
2727 ;; on stuff, 9 - messages inside loops.
2728 (defun gnus-message (level &rest args)
2729   (if (<= level gnus-verbose)
2730       (apply 'message args)
2731     ;; We have to do this format thingie here even if the result isn't
2732     ;; shown - the return value has to be the same as the return value
2733     ;; from `message'.
2734     (apply 'format args)))
2735
2736 ;; Generate a unique new group name.
2737 (defun gnus-generate-new-group-name (leaf)
2738   (let ((name leaf)
2739         (num 0))
2740     (while (gnus-gethash name gnus-newsrc-hashtb)
2741       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2742     name))
2743
2744 (defun gnus-find-file-noselect (file &optional force)
2745   "Does vaguely the same as find-file-noselect. No hooks are run."
2746   (let (buf insert)
2747     (if (setq buf (get-file-buffer file))
2748         (setq insert force)
2749       (setq buf (create-file-buffer file))
2750       (setq insert t))
2751     (if (not insert)
2752         buf
2753       (save-excursion
2754         (set-buffer buf)
2755         (erase-buffer)
2756         (and (file-readable-p file)
2757              (insert-file-contents file))
2758         (set-visited-file-name file)
2759         (set-buffer-modified-p nil)
2760         (current-buffer)))))
2761
2762 ;;; List and range functions
2763
2764 (defun gnus-last-element (list)
2765   "Return last element of LIST."
2766   (while (cdr list)
2767     (setq list (cdr list)))
2768   (car list))
2769
2770 (defun gnus-copy-sequence (list)
2771   "Do a complete, total copy of a list."
2772   (if (and (consp list) (not (consp (cdr list))))
2773       (cons (car list) (cdr list))
2774     (mapcar (lambda (elem) (if (consp elem) 
2775                                (if (consp (cdr elem))
2776                                    (gnus-copy-sequence elem)
2777                                  (cons (car elem) (cdr elem)))
2778                              elem))
2779             list)))
2780
2781 (defun gnus-set-difference (list1 list2)
2782   "Return a list of elements of LIST1 that do not appear in LIST2."
2783   (let ((list1 (copy-sequence list1)))
2784     (while list2
2785       (setq list1 (delq (car list2) list1))
2786       (setq list2 (cdr list2)))
2787     list1))
2788
2789 (defun gnus-sorted-complement (list1 list2)
2790   "Return a list of elements of LIST1 that do not appear in LIST2.
2791 Both lists have to be sorted over <."
2792   (let (out)
2793     (if (or (null list1) (null list2))
2794         (or list1 list2)
2795       (while (and list1 list2)
2796         (cond ((= (car list1) (car list2))
2797                (setq list1 (cdr list1)
2798                      list2 (cdr list2)))
2799               ((< (car list1) (car list2))
2800                (setq out (cons (car list1) out))
2801                (setq list1 (cdr list1)))
2802               (t
2803                (setq out (cons (car list2) out))
2804                (setq list2 (cdr list2)))))
2805       (nconc (nreverse out) (or list1 list2)))))
2806
2807 (defun gnus-intersection (list1 list2)      
2808   (let ((result nil))
2809     (while list2
2810       (if (memq (car list2) list1)
2811           (setq result (cons (car list2) result)))
2812       (setq list2 (cdr list2)))
2813     result))
2814
2815 (defun gnus-sorted-intersection (list1 list2)
2816   ;; LIST1 and LIST2 have to be sorted over <.
2817   (let (out)
2818     (while (and list1 list2)
2819       (cond ((= (car list1) (car list2))
2820              (setq out (cons (car list1) out)
2821                    list1 (cdr list1)
2822                    list2 (cdr list2)))
2823             ((< (car list1) (car list2))
2824              (setq list1 (cdr list1)))
2825             (t
2826              (setq list2 (cdr list2)))))
2827     (nreverse out)))
2828
2829 (defun gnus-set-sorted-intersection (list1 list2)
2830   ;; LIST1 and LIST2 have to be sorted over <.
2831   ;; This function modifies LIST1.
2832   (let* ((top (cons nil list1))
2833          (prev top))
2834   (while (and list1 list2)
2835     (cond ((= (car list1) (car list2))
2836            (setq prev list1
2837                  list1 (cdr list1)
2838                  list2 (cdr list2)))
2839           ((< (car list1) (car list2))
2840            (setcdr prev (cdr list1))
2841            (setq list1 (cdr list1)))
2842           (t
2843            (setq list2 (cdr list2)))))
2844   (setcdr prev nil)
2845   (cdr top)))
2846
2847 (defun gnus-compress-sequence (numbers &optional always-list)
2848   "Convert list of numbers to a list of ranges or a single range.
2849 If ALWAYS-LIST is non-nil, this function will always release a list of
2850 ranges."
2851   (let* ((first (car numbers))
2852          (last (car numbers))
2853          result)
2854     (if (null numbers)
2855         nil
2856       (if (not (listp (cdr numbers)))
2857           numbers
2858         (while numbers
2859           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2860                 ((= (1+ last) (car numbers)) ;Still in sequence
2861                  (setq last (car numbers)))
2862                 (t                      ;End of one sequence
2863                  (setq result 
2864                        (cons (if (= first last) first
2865                                (cons first last)) result))
2866                  (setq first (car numbers))
2867                  (setq last  (car numbers))))
2868           (setq numbers (cdr numbers)))
2869         (if (and (not always-list) (null result))
2870             (if (= first last) (list first) (cons first last))
2871           (nreverse (cons (if (= first last) first (cons first last))
2872                           result)))))))
2873
2874 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2875 (defun gnus-uncompress-range (ranges)
2876   "Expand a list of ranges into a list of numbers.
2877 RANGES is either a single range on the form `(num . num)' or a list of
2878 these ranges."
2879   (let (first last result)
2880     (cond 
2881      ((null ranges)
2882       nil)
2883      ((not (listp (cdr ranges)))
2884       (setq first (car ranges))
2885       (setq last (cdr ranges))
2886       (while (<= first last)
2887         (setq result (cons first result))
2888         (setq first (1+ first)))
2889       (nreverse result))
2890      (t
2891       (while ranges
2892         (if (atom (car ranges))
2893             (if (numberp (car ranges))
2894                 (setq result (cons (car ranges) result)))
2895           (setq first (car (car ranges)))
2896           (setq last  (cdr (car ranges)))
2897           (while (<= first last)
2898             (setq result (cons first result))
2899             (setq first (1+ first))))
2900         (setq ranges (cdr ranges)))
2901       (nreverse result)))))
2902
2903 (defun gnus-add-to-range (ranges list)
2904   "Return a list of ranges that has all articles from both RANGES and LIST.
2905 Note: LIST has to be sorted over `<'."
2906   (if (not ranges)
2907       (gnus-compress-sequence list t)
2908     (setq list (copy-sequence list))
2909     (or (listp (cdr ranges))
2910         (setq ranges (list ranges)))
2911     (let ((out ranges)
2912           ilist lowest highest temp)
2913       (while (and ranges list)
2914         (setq ilist list)
2915         (setq lowest (or (and (atom (car ranges)) (car ranges))
2916                          (car (car ranges))))
2917         (while (and list (cdr list) (< (car (cdr list)) lowest))
2918           (setq list (cdr list)))
2919         (if (< (car ilist) lowest)
2920             (progn
2921               (setq temp list)
2922               (setq list (cdr list))
2923               (setcdr temp nil)
2924               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2925         (setq highest (or (and (atom (car ranges)) (car ranges))
2926                           (cdr (car ranges))))
2927         (while (and list (<= (car list) highest))
2928           (setq list (cdr list)))
2929         (setq ranges (cdr ranges)))
2930       (if list
2931           (setq out (nconc (gnus-compress-sequence list t) out)))
2932       (setq out (sort out (lambda (r1 r2) 
2933                             (< (or (and (atom r1) r1) (car r1))
2934                                (or (and (atom r2) r2) (car r2))))))
2935       (setq ranges out)
2936       (while ranges
2937         (if (atom (car ranges))
2938             (if (cdr ranges)
2939                 (if (atom (car (cdr ranges)))
2940                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2941                         (progn
2942                           (setcar ranges (cons (car ranges) 
2943                                                (car (cdr ranges))))
2944                           (setcdr ranges (cdr (cdr ranges)))))
2945                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2946                       (progn
2947                         (setcar (car (cdr ranges)) (car ranges))
2948                         (setcar ranges (car (cdr ranges)))
2949                         (setcdr ranges (cdr (cdr ranges)))))))
2950           (if (cdr ranges)
2951               (if (atom (car (cdr ranges)))
2952                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2953                       (progn
2954                         (setcdr (car ranges) (car (cdr ranges)))
2955                         (setcdr ranges (cdr (cdr ranges)))))
2956                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2957                     (progn
2958                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2959                       (setcdr ranges (cdr (cdr ranges))))))))
2960         (setq ranges (cdr ranges)))
2961       out)))
2962
2963 (defun gnus-remove-from-range (ranges list)
2964   "Return a list of ranges that has all articles from LIST removed from RANGES.
2965 Note: LIST has to be sorted over `<'."
2966   ;; !!! This function shouldn't look like this, but I've got a headache.
2967   (gnus-compress-sequence 
2968    (gnus-sorted-complement
2969     (gnus-uncompress-range ranges) list)))
2970
2971 (defun gnus-member-of-range (number ranges)
2972   (if (not (listp (cdr ranges)))
2973       (and (>= number (car ranges)) 
2974            (<= number (cdr ranges)))
2975     (let ((not-stop t))
2976       (while (and ranges 
2977                   (if (numberp (car ranges))
2978                       (>= number (car ranges))
2979                     (>= number (car (car ranges))))
2980                   not-stop)
2981         (if (if (numberp (car ranges))
2982                 (= number (car ranges))
2983               (and (>= number (car (car ranges)))
2984                    (<= number (cdr (car ranges)))))
2985             (setq not-stop nil))
2986         (setq ranges (cdr ranges)))
2987       (not not-stop))))
2988
2989 \f
2990 ;;;
2991 ;;; Gnus group mode
2992 ;;;
2993
2994 (defvar gnus-group-mode-map nil)
2995 (defvar gnus-group-group-map nil)
2996 (defvar gnus-group-mark-map nil)
2997 (defvar gnus-group-list-map nil)
2998 (defvar gnus-group-sub-map nil)
2999 (put 'gnus-group-mode 'mode-class 'special)
3000
3001 (if gnus-group-mode-map
3002     nil
3003   (setq gnus-group-mode-map (make-keymap))
3004   (suppress-keymap gnus-group-mode-map)
3005   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3006   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3007   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3008   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3009   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3010   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3011   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3012   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3013   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3014   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3015   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3016   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3017   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3018   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3019   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3020   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3021   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3022   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3023   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3024   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3025   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3026   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3027   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3028   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3029   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3030   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3031   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3032   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3033   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3034   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3035   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3036   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3037   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3038   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3039   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3040   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3041   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3042   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3043   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3044   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3045   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3046   (define-key gnus-group-mode-map "V" 'gnus-version)
3047   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3048   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3049   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3050   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3051   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3052   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3053   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3054   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3055   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3056   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3057   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3058   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3059   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3060   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3061   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3062
3063   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3064   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3065   (define-prefix-command 'gnus-group-mark-map)
3066   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3067   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3068   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3069   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3070
3071   (define-prefix-command 'gnus-group-group-map)
3072   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3073   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3074   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3075   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3076   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3077   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3078   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3079   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3080   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3081   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3082   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3083   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3084   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3085   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3086   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3087   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3088   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3089   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3090
3091   (define-prefix-command 'gnus-group-list-map)
3092   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3093   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3094   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3095   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3096   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3097   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3098   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3099   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3100   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3101
3102   (define-prefix-command 'gnus-group-sub-map)
3103   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3104   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3105   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3106   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3107   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3108   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3109   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3110   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3111
3112 (defun gnus-group-mode ()
3113   "Major mode for reading news.
3114
3115 All normal editing commands are switched off.
3116 \\<gnus-group-mode-map>
3117 The group buffer lists (some of) the groups available.  For instance,
3118 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3119 lists all zombie groups. 
3120
3121 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3122 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3123
3124 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3125
3126 The following commands are available:
3127
3128 \\{gnus-group-mode-map}"
3129   (interactive)
3130   (if gnus-visual (gnus-group-make-menu-bar))
3131   (kill-all-local-variables)
3132   (setq mode-line-modified "-- ")
3133   (make-local-variable 'mode-line-format)
3134   (setq mode-line-format (copy-sequence mode-line-format))
3135   (and (equal (nth 3 mode-line-format) "   ")
3136        (setcar (nthcdr 3 mode-line-format) ""))
3137   (setq major-mode 'gnus-group-mode)
3138   (setq mode-name "Group")
3139   (gnus-group-set-mode-line)
3140   (setq mode-line-process nil)
3141   (use-local-map gnus-group-mode-map)
3142   (buffer-disable-undo (current-buffer))
3143   (setq truncate-lines t)
3144   (setq buffer-read-only t)
3145   (run-hooks 'gnus-group-mode-hook))
3146
3147 (defun gnus-mouse-pick-group (e)
3148   (interactive "e")
3149   (mouse-set-point e)
3150   (gnus-group-read-group nil))
3151
3152 ;;;###autoload
3153 (defun gnus-no-server (&optional arg)
3154   "Read network news.
3155 If ARG is a positive number, Gnus will use that as the
3156 startup level. If ARG is nil, Gnus will be started at level 2. 
3157 If ARG is non-nil and not a positive number, Gnus will
3158 prompt the user for the name of an NNTP server to use.
3159 As opposed to `gnus', this command will not connect to the local server."
3160   (interactive "P")
3161   (setq gnus-group-use-permanent-levels t)
3162   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3163
3164 (defalias '\(ding\) 'gnus)
3165
3166 ;;;###autoload
3167 (defun gnus (&optional arg dont-connect)
3168   "Read network news.
3169 If ARG is non-nil and a positive number, Gnus will use that as the
3170 startup level. If ARG is non-nil and not a positive number, Gnus will
3171 prompt the user for the name of an NNTP server to use."
3172   (interactive "P")
3173   (if (get-buffer gnus-group-buffer)
3174       (progn
3175         (switch-to-buffer gnus-group-buffer)
3176         (gnus-group-get-new-news))
3177     (gnus-clear-system)
3178     (nnheader-init-server-buffer)
3179     (gnus-read-init-file)
3180     (let ((level (and arg (numberp arg) (> arg 0) arg))
3181           did-connect)
3182       (unwind-protect
3183           (progn
3184             (gnus-group-setup-buffer)
3185             (or dont-connect 
3186                 (setq did-connect
3187                       (gnus-start-news-server (and arg (not level))))))
3188         (if (and (not dont-connect) 
3189                  (not did-connect))
3190             (gnus-group-quit)
3191           (run-hooks 'gnus-startup-hook)
3192           ;; NNTP server is successfully open. 
3193           (gnus-update-format-specifications)
3194           (gnus-summary-make-display-table)
3195           (let ((buffer-read-only nil))
3196             (erase-buffer)
3197             (if (not gnus-inhibit-startup-message)
3198                 (progn
3199                   (gnus-group-startup-message)
3200                   (sit-for 0))))
3201           (gnus-setup-news nil level)
3202           (and gnus-use-dribble-file (gnus-dribble-open))
3203           (gnus-group-list-groups level)
3204           (gnus-configure-windows 'group))))))
3205
3206 (defun gnus-group-startup-message (&optional x y)
3207   "Insert startup message in current buffer."
3208   ;; Insert the message.
3209   (erase-buffer)
3210   (insert
3211    (format "
3212      %s
3213            A newsreader 
3214       for GNU Emacs
3215
3216         Based on GNUS 
3217              written by 
3218      Masanobu UMEDA
3219
3220        A Praxis Release
3221       larsi@ifi.uio.no
3222
3223            gnus-version))
3224   ;; And then hack it.
3225   ;; 18 is the longest line.
3226   (indent-rigidly (point-min) (point-max) 
3227                   (/ (max (- (window-width) (or x 28)) 0) 2))
3228   (goto-char (point-min))
3229   ;; +4 is fuzzy factor.
3230   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3231
3232   ;; Fontify some.
3233   (goto-char (point-min))
3234   (search-forward "Praxis")
3235   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3236   (goto-char (point-min)))
3237
3238 (defun gnus-group-setup-buffer ()
3239   (or (get-buffer gnus-group-buffer)
3240       (progn
3241         (switch-to-buffer gnus-group-buffer)
3242         (gnus-add-current-to-buffer-list)
3243         (gnus-group-mode)
3244         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3245
3246 (defun gnus-group-list-groups (level &optional unread)
3247   "List newsgroups with level LEVEL or lower that have unread articles.
3248 Default is all subscribed groups.
3249 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3250   (interactive (list (and current-prefix-arg
3251                           (prefix-numeric-value current-prefix-arg))))
3252   (if gnus-group-use-permanent-levels
3253       (progn
3254         (setq gnus-group-default-list-level 
3255               (or level gnus-group-default-list-level))
3256         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3257     (setq level (or level gnus-group-default-list-level 
3258                     gnus-level-subscribed)))
3259   (gnus-group-setup-buffer)     ;May call from out of group buffer
3260   (let ((case-fold-search nil)
3261         (group (gnus-group-group-name)))
3262     (funcall gnus-group-prepare-function level unread nil)
3263     (if (zerop (buffer-size))
3264         (gnus-message 5 gnus-no-groups-message)
3265       (goto-char (point-min))
3266       (if (not group)
3267           ;; Go to the first group with unread articles.
3268           (gnus-group-search-forward nil nil nil t)
3269         ;; Find the right group to put point on. If the current group
3270         ;; has disapeared in the new listing, try to find the next
3271         ;; one. If no next one can be found, just leave point at the
3272         ;; first newsgroup in the buffer.
3273         (if (not (gnus-goto-char
3274                   (text-property-any (point-min) (point-max) 
3275                                      'gnus-group (intern group))))
3276             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3277               (while (and newsrc
3278                           (not (gnus-goto-char 
3279                                 (text-property-any 
3280                                  (point-min) (point-max) 'gnus-group 
3281                                  (intern (car (car newsrc)))))))
3282                 (setq newsrc (cdr newsrc)))
3283               (or newsrc (progn (goto-char (point-max))
3284                                 (forward-line -1))))))
3285       ;; Adjust cursor point.
3286       (gnus-group-position-cursor))))
3287
3288 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3289   "List all newsgroups with unread articles of level LEVEL or lower.
3290 If ALL is non-nil, list groups that have no unread articles.
3291 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3292 If REGEXP, only list groups matching REGEXP."
3293   (set-buffer gnus-group-buffer)
3294   (let ((buffer-read-only nil)
3295         (newsrc (cdr gnus-newsrc-alist))
3296         (lowest (or lowest 1))
3297         info clevel unread group)
3298     (erase-buffer)
3299     (if (< lowest gnus-level-zombie)
3300         ;; List living groups.
3301         (while newsrc
3302           (setq info (car newsrc)
3303                 group (car info)
3304                 newsrc (cdr newsrc)
3305                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3306           (and unread ; This group might be bogus
3307                (or (not regexp)
3308                    (string-match regexp group))
3309                (<= (setq clevel (car (cdr info))) level) 
3310                (>= clevel lowest)
3311                (or all            ; We list all groups?
3312                    (eq unread t)  ; We list unactivated groups
3313                    (> unread 0)   ; We list groups with unread articles
3314                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3315                (gnus-group-insert-group-line 
3316                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3317
3318     ;; List dead groups.
3319     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3320          (gnus-group-prepare-flat-list-dead 
3321           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3322           gnus-level-zombie ?Z
3323           regexp))
3324     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3325          (gnus-group-prepare-flat-list-dead 
3326           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3327           gnus-level-killed ?K regexp))
3328
3329     (gnus-group-set-mode-line)
3330     (setq gnus-have-all-newsgroups all)
3331     (run-hooks 'gnus-group-prepare-hook)))
3332
3333 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3334   ;; List zombies and killed lists somehwat faster, which was
3335   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3336   ;; this by ignoring the group format specification altogether.
3337   (let (group beg)
3338     (while groups
3339       (setq group (car groups)
3340             groups (cdr groups))
3341       (if (or (not regexp)
3342               (string-match regexp group))
3343           (progn
3344             (setq beg (point))
3345             (insert (format " %c     *: %s\n" mark group))
3346             (add-text-properties 
3347              beg (1+ beg) 
3348              (list 'gnus-group (intern group)
3349                    'gnus-unread t
3350                    'gnus-level level)))))))
3351
3352 (defun gnus-group-real-name (group)
3353   "Find the real name of a foreign newsgroup."
3354   (if (string-match ":[^:]+$" group)
3355       (substring group (1+ (match-beginning 0)))
3356     group))
3357
3358 (defun gnus-group-prefixed-name (group method)
3359   "Return the whole name from GROUP and METHOD."
3360   (and (stringp method) (setq method (gnus-server-to-method method)))
3361   (concat (format "%s" (car method))
3362           (if (and 
3363                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3364                (not (string= (nth 1 method) "")))
3365               (concat "+" (nth 1 method)))
3366           ":" group))
3367
3368 (defun gnus-group-real-prefix (group)
3369   "Return the prefix of the current group name."
3370   (if (string-match "^[^:]+:" group)
3371       (substring group 0 (match-end 0))
3372     ""))
3373
3374 (defun gnus-group-method-name (group)
3375   "Return the method used for selecting GROUP."
3376   (let ((prefix (gnus-group-real-prefix group)))
3377     (if (equal prefix "")
3378         gnus-select-method
3379       (if (string-match "^[^\\+]+\\+" prefix)
3380           (list (intern (substring prefix 0 (1- (match-end 0))))
3381                 (substring prefix (match-end 0) (1- (length prefix))))
3382         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3383
3384 (defun gnus-group-foreign-p (group)
3385   "Return nil if GROUP is native, non-nil if it is foreign."
3386   (string-match ":" group))
3387
3388 (defun gnus-group-set-info (info &optional method-only-group part)
3389   (let* ((entry (gnus-gethash
3390                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3391          (part-info info)
3392          (info (if method-only-group (nth 2 entry) info)))
3393     (if (not method-only-group)
3394         ()
3395       (or entry
3396           (error "Trying to change non-existent group %s" method-only-group))
3397       ;; We have recevied parts of the actual group info - either the
3398       ;; select method or the group parameters.  We first check
3399       ;; whether we have to extend the info, and if so, do that.
3400       (let ((len (length info))
3401             (total (if (eq part 'method) 5 6)))
3402         (and (< len total)
3403              (setcdr (nthcdr (1- len) info)
3404                      (make-list (- total len) nil)))
3405         ;; Then we enter the new info.
3406         (setcar (nthcdr (1- total) info) part-info)))
3407     ;; We uncompress some lists of marked articles.
3408     (let (marked)
3409       (if (not (setq marked (nth 3 info)))
3410           ()
3411         (while marked
3412           (or (eq 'score (car (car marked)))
3413               (eq 'bookmark (car (car marked)))
3414               (eq 'killed (car (car marked)))
3415               (setcdr (car marked) 
3416                       (gnus-uncompress-range (cdr (car marked)))))
3417           (setq marked (cdr marked)))))
3418     (if entry
3419         ()
3420       ;; This is a new group, so we just create it.
3421       (save-excursion
3422         (set-buffer gnus-group-buffer)
3423         (if (nth 4 info)
3424             ;; It's a foreign group...
3425             (gnus-group-make-group 
3426              (gnus-group-real-name (car info))
3427              (prin1-to-string (car (nth 4 info)))
3428              (nth 1 (nth 4 info)))
3429           ;; It's a native group.
3430           (gnus-group-make-group
3431            (car info)
3432            (prin1-to-string (car gnus-select-method))
3433            (nth 1 gnus-select-method)))
3434         (gnus-message 6 "Note: New group created")
3435         (setq entry 
3436               (gnus-gethash (gnus-group-prefixed-name 
3437                              (gnus-group-real-name (car info))
3438                              (or (nth 4 info) gnus-select-method))
3439                             gnus-newsrc-hashtb))))
3440     ;; Whether it was a new group or not, we now have the entry, so we
3441     ;; can do the update.
3442     (if entry
3443         (progn
3444           (setcar (nthcdr 2 entry) info)
3445           (if (and (not (eq (car entry) t)) 
3446                    (gnus-gethash (car info) gnus-active-hashtb))
3447               (let ((marked (nth 3 info)))
3448                 (setcar entry 
3449                         (max 0 (- (length (gnus-list-of-unread-articles 
3450                                            (car info)))
3451                                   (length (cdr (assq 'tick marked)))
3452                                   (length (cdr (assq 'dormant marked)))))))))
3453       (error "No such group: %s" (car info)))))
3454
3455 (defun gnus-group-set-method-info (group select-method)
3456   (gnus-group-set-info select-method group 'method))
3457
3458 (defun gnus-group-set-params-info (group params)
3459   (gnus-group-set-info params group 'params))
3460
3461 (defun gnus-group-update-group-line ()
3462   "This function updates the current line in the newsgroup buffer and
3463 moves the point to the colon."
3464   (let* ((buffer-read-only nil)
3465          (group (gnus-group-group-name))
3466          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3467     (if entry
3468         (gnus-dribble-enter 
3469          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3470                  ")")))
3471     (beginning-of-line)
3472     (delete-region (point) (progn (forward-line 1) (point)))
3473     (gnus-group-insert-group-line-info group)
3474     (forward-line -1)
3475     (gnus-group-position-cursor)))
3476
3477 (defun gnus-group-insert-group-line-info (group)
3478   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3479         active info)
3480     (if entry
3481         (progn
3482           (setq info (nth 2 entry))
3483           (gnus-group-insert-group-line 
3484            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3485       (setq active (gnus-gethash group gnus-active-hashtb))
3486       (gnus-group-insert-group-line 
3487        nil group 
3488        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3489        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3490
3491 (defun gnus-group-insert-group-line (gformat group level marked number method)
3492   (let* ((gformat (or gformat gnus-group-line-format-spec))
3493          (active (gnus-gethash group gnus-active-hashtb))
3494          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3495          (number-of-dormant (length (cdr (assq 'dormant marked))))
3496          (number-of-ticked (length (cdr (assq 'tick marked))))
3497          (number-of-ticked-and-dormant
3498           (+ number-of-ticked number-of-dormant))
3499          (number-of-unread-unticked 
3500           (if (numberp number) (int-to-string (max 0 number))
3501             "*"))
3502          (number-of-read
3503           (if (numberp number)
3504               (max 0 (- number-total number))
3505             "*"))
3506          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3507                            ((<= level gnus-level-unsubscribed) ?U)
3508                            ((= level gnus-level-zombie) ?Z)
3509                            (t ?K)))
3510          (qualified-group (gnus-group-real-name group))
3511          (newsgroup-description 
3512           (if gnus-description-hashtb
3513               (or (gnus-gethash group gnus-description-hashtb) "")
3514             ""))
3515          (moderated (if (member group gnus-moderated-list) ?m ? ))
3516          (moderated-string (if (eq moderated ?m) "(m)" ""))
3517          (method (gnus-server-get-method group method))
3518          (news-server (or (car (cdr method)) ""))
3519          (news-method (or (car method) ""))
3520          (news-method-string 
3521           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3522          (marked (if (and 
3523                       (numberp number) 
3524                       (zerop number)
3525                       (> number-of-ticked 0))
3526                      ?* ? ))
3527          (number (if (eq number t) "*" (+ number number-of-dormant 
3528                                           number-of-ticked)))
3529          (process-marked (if (member qualified-group gnus-group-marked)
3530                              gnus-process-mark ? ))
3531          (buffer-read-only nil)
3532          header ; passed as parameter to user-funcs.
3533          b)
3534     (beginning-of-line)
3535     (setq b (point))
3536     ;; Insert the text.
3537     (insert (eval gformat))
3538
3539     (add-text-properties 
3540      b (1+ b) (list 'gnus-group (intern group)
3541                     'gnus-unread (if (numberp number)
3542                                      (string-to-int number-of-unread-unticked)
3543                                    t)
3544                     'gnus-marked marked
3545                     'gnus-level level))))
3546
3547 (defun gnus-group-update-group (group &optional visible-only)
3548   "Update newsgroup info of GROUP.
3549 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3550   (save-excursion
3551     (set-buffer gnus-group-buffer)
3552     (let ((buffer-read-only nil)
3553           visible)
3554       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3555         (if entry
3556             (gnus-dribble-enter 
3557              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3558                      ")"))))
3559       ;; Buffer may be narrowed.
3560       (save-restriction
3561         (widen)
3562         ;; Search a line to modify.  If the buffer is large, the search
3563         ;; takes long time.  In most cases, current point is on the line
3564         ;; we are looking for.  So, first of all, check current line. 
3565         (if (or (progn
3566                   (beginning-of-line)
3567                   (eq (get-text-property (point) 'gnus-group)
3568                       (intern group)))
3569                 (progn
3570                   (gnus-goto-char 
3571                    (text-property-any 
3572                     (point-min) (point-max) 'gnus-group (intern group)))))
3573             ;; GROUP is listed in current buffer. So, delete old line.
3574             (progn
3575               (setq visible t)
3576               (beginning-of-line)
3577               (delete-region (point) (progn (forward-line 1) (point))))
3578           ;; No such line in the buffer, find out where it's supposed to
3579           ;; go, and insert it there (or at the end of the buffer).
3580           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3581           (or visible-only
3582               (let ((entry 
3583                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3584                 (while (and entry
3585                             (car entry)
3586                             (not
3587                              (gnus-goto-char
3588                               (text-property-any
3589                                (point-min) (point-max) 
3590                                'gnus-group (intern (car (car entry)))))))
3591                   (setq entry (cdr entry)))
3592                 (or entry (goto-char (point-max)))))))
3593       (if (or visible (not visible-only))
3594           (gnus-group-insert-group-line-info group))
3595       (gnus-group-set-mode-line))))
3596
3597 (defun gnus-group-set-mode-line ()
3598   (if (memq 'group gnus-updated-mode-lines)
3599       (let* ((gformat (or gnus-group-mode-line-format-spec
3600                           (setq gnus-group-mode-line-format-spec
3601                                 (gnus-parse-format 
3602                                  gnus-group-mode-line-format 
3603                                  gnus-group-mode-line-format-alist))))
3604              (news-server (car (cdr gnus-select-method)))
3605              (news-method (car gnus-select-method))
3606              (max-len 60)
3607              (mode-string (eval gformat)))
3608         (setq mode-string (eval gformat))
3609         (if (> (length mode-string) max-len) 
3610             (setq mode-string (substring mode-string 0 (- max-len 4))))
3611         (setq mode-line-buffer-identification mode-string)
3612         (set-buffer-modified-p t))))
3613
3614 (defun gnus-group-group-name ()
3615   "Get the name of the newsgroup on the current line."
3616   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3617     (and group (symbol-name group))))
3618
3619 (defun gnus-group-group-level ()
3620   "Get the level of the newsgroup on the current line."
3621   (get-text-property (gnus-point-at-bol) 'gnus-level))
3622
3623 (defun gnus-group-group-unread ()
3624   "Get the number of unread articles of the newsgroup on the current line."
3625   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3626
3627 (defun gnus-group-search-forward (&optional backward all level first-too)
3628   "Find the next newsgroup with unread articles.
3629 If BACKWARD is non-nil, find the previous newsgroup instead.
3630 If ALL is non-nil, just find any newsgroup.
3631 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3632 group exists.
3633 If FIRST-TOO, the current line is also eligible as a target."
3634   (let ((way (if backward -1 1))
3635         (low gnus-level-killed)
3636         (beg (point))
3637         pos found lev)
3638     (if (and backward (progn (beginning-of-line)) (bobp))
3639         nil
3640       (or first-too (forward-line way))
3641       (while (and 
3642               (not (eobp))
3643               (not (setq 
3644                     found 
3645                     (and (or all
3646                              (and
3647                               (let ((unread 
3648                                      (get-text-property (point) 'gnus-unread)))
3649                                 (or (eq unread t) (and unread (> unread 0))))
3650                               (setq lev (get-text-property (point)
3651                                                            'gnus-level))
3652                               (<= lev gnus-level-subscribed)))
3653                          (or (not level)
3654                              (and (setq lev (get-text-property (point)
3655                                                                'gnus-level))
3656                                   (or (= lev level)
3657                                       (and (< lev low)
3658                                            (< level lev)
3659                                            (progn
3660                                              (setq low lev)
3661                                              (setq pos (point))
3662                                              nil))))))))
3663               (zerop (forward-line way)))))
3664     (if found 
3665         (progn (gnus-group-position-cursor) t)
3666       (goto-char (or pos beg))
3667       (and pos t))))
3668
3669 ;;; Gnus group mode commands
3670
3671 ;; Group marking.
3672
3673 (defun gnus-group-mark-group (n &optional unmark no-advance)
3674   "Mark the current group."
3675   (interactive "p")
3676   (let ((buffer-read-only nil)
3677         group)
3678     (while 
3679         (and (> n 0) 
3680              (setq group (gnus-group-group-name))
3681              (progn
3682                (beginning-of-line)
3683                (forward-char 2)
3684                (delete-char 1)
3685                (if unmark
3686                    (progn
3687                      (insert " ")
3688                      (setq gnus-group-marked (delete group gnus-group-marked)))
3689                  (insert "#")
3690                  (setq gnus-group-marked
3691                        (cons group (delete group gnus-group-marked))))
3692                t)
3693              (or no-advance (zerop (gnus-group-next-group 1))))
3694       (setq n (1- n)))
3695     (gnus-summary-position-cursor)
3696     n))
3697
3698 (defun gnus-group-unmark-group (n)
3699   "Remove the mark from the current group."
3700   (interactive "p")
3701   (gnus-group-mark-group n 'unmark))
3702
3703 (defun gnus-group-mark-region (unmark beg end)
3704   "Mark all groups between point and mark.
3705 If UNMARK, remove the mark instead."
3706   (interactive "P\nr")
3707   (let ((num (count-lines beg end)))
3708     (save-excursion
3709       (goto-char beg)
3710       (- num (gnus-group-mark-group num unmark)))))
3711
3712 (defun gnus-group-remove-mark (group)
3713   (and (gnus-group-goto-group group)
3714        (save-excursion
3715          (gnus-group-mark-group 1 'unmark t))))
3716
3717 ;; Return a list of groups to work on.  Take into consideration N (the
3718 ;; prefix) and the list of marked groups.
3719 (defun gnus-group-process-prefix (n)
3720   (cond (n
3721          (setq n (prefix-numeric-value n))
3722          ;; There is a prefix, so we return a list of the N next
3723          ;; groups. 
3724          (let ((way (if (< n 0) -1 1))
3725                (n (abs n))
3726                group groups)
3727            (save-excursion
3728              (while (and (> n 0)
3729                          (setq group (gnus-group-group-name)))
3730                (setq groups (cons group groups))
3731                (setq n (1- n))
3732                (forward-line way)))
3733            (nreverse groups)))
3734         (gnus-group-marked
3735          ;; No prefix, but a list of marked articles.
3736          (reverse gnus-group-marked))
3737         (t
3738          ;; Neither marked articles or a prefix, so we return the
3739          ;; current group.
3740          (let ((group (gnus-group-group-name)))
3741            (and group (list group))))))
3742
3743 ;; Selecting groups.
3744
3745 (defun gnus-group-read-group (all &optional no-article group)
3746   "Read news in this newsgroup.
3747 If argument ALL is non-nil, already read articles become readable.
3748 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3749   (interactive "P")
3750   (let ((group (or group (gnus-group-group-name)))
3751         number active marked entry)
3752     (or group (error "No group on current line"))
3753     (setq marked 
3754           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3755     ;; This group might be a dead group. In that case we have to get
3756     ;; the number of unread articles from `gnus-active-hashtb'.
3757     (if entry
3758         (setq number (car entry))
3759       (if (setq active (gnus-gethash group gnus-active-hashtb))
3760           (setq number (- (1+ (cdr active)) (car active)))))
3761     (gnus-summary-read-group 
3762      group (or all (and (numberp number) 
3763                         (zerop (+ number (length (cdr (assq 'tick marked)))
3764                                   (length (cdr (assq 'dormant marked)))))))
3765      no-article)))
3766
3767 (defun gnus-group-select-group (all)
3768   "Select this newsgroup.
3769 No article is selected automatically.
3770 If argument ALL is non-nil, already read articles become readable."
3771   (interactive "P")
3772   (gnus-group-read-group all t))
3773
3774 ;; Enter a group that is not in the group buffer. Non-nil is returned
3775 ;; if selection was successful.
3776 (defun gnus-group-read-ephemeral-group 
3777   (group method &optional activate quit-config)
3778   (let ((group (if (gnus-group-foreign-p group) group
3779                  (gnus-group-prefixed-name group method))))
3780     (gnus-sethash 
3781      group
3782      (list t nil (list group gnus-level-default-subscribed nil nil 
3783                        (append method
3784                                (list
3785                                 (list 'quit-config 
3786                                       (if quit-config quit-config
3787                                         (cons (current-buffer) 'summary)))))))
3788      gnus-newsrc-hashtb)
3789     (set-buffer gnus-group-buffer)
3790     (or (gnus-server-opened method)
3791         (gnus-open-server method)
3792         (error "Unable to contact server: %s" (gnus-status-message method)))
3793     (if activate (or (gnus-request-group group)
3794                      (error "Couldn't request group")))
3795     (condition-case ()
3796         (gnus-group-read-group t t group)
3797       (error nil)
3798       (quit nil))
3799     (not (equal major-mode 'gnus-group-mode))))
3800   
3801 (defun gnus-group-jump-to-group (group)
3802   "Jump to newsgroup GROUP."
3803   (interactive 
3804    (list (completing-read 
3805           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3806
3807   (if (equal group "")
3808       (error "Empty group name"))
3809
3810   (let ((b (text-property-any 
3811             (point-min) (point-max) 'gnus-group (intern group))))
3812     (if b
3813         ;; Either go to the line in the group buffer...
3814         (goto-char b)
3815       ;; ... or insert the line.
3816       (or
3817        (gnus-gethash group gnus-active-hashtb)
3818        (gnus-activate-newsgroup group)
3819        (error "%s error: %s" group (gnus-status-message group)))
3820
3821       (gnus-group-update-group group)
3822       (goto-char (text-property-any 
3823                   (point-min) (point-max) 'gnus-group (intern group)))))
3824   ;; Adjust cursor point.
3825   (gnus-group-position-cursor))
3826
3827 (defun gnus-group-goto-group (group)
3828   "Goto to newsgroup GROUP."
3829   (let ((b (text-property-any (point-min) (point-max) 
3830                               'gnus-group (intern group))))
3831     (and b (goto-char b))))
3832
3833 (defun gnus-group-next-group (n)
3834   "Go to next N'th newsgroup.
3835 If N is negative, search backward instead.
3836 Returns the difference between N and the number of skips actually
3837 done."
3838   (interactive "p")
3839   (gnus-group-next-unread-group n t))
3840
3841 (defun gnus-group-next-unread-group (n &optional all level)
3842   "Go to next N'th unread newsgroup.
3843 If N is negative, search backward instead.
3844 If ALL is non-nil, choose any newsgroup, unread or not.
3845 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3846 such group can be found, the next group with a level higher than
3847 LEVEL.
3848 Returns the difference between N and the number of skips actually
3849 made."
3850   (interactive "p")
3851   (let ((backward (< n 0))
3852         (n (abs n)))
3853     (while (and (> n 0)
3854                 (gnus-group-search-forward 
3855                  backward (or (not gnus-group-goto-unread) all) level))
3856       (setq n (1- n)))
3857     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3858                                (if level " on this level or higher" "")))
3859     n))
3860
3861 (defun gnus-group-prev-group (n)
3862   "Go to previous N'th newsgroup.
3863 Returns the difference between N and the number of skips actually
3864 done."
3865   (interactive "p")
3866   (gnus-group-next-unread-group (- n) t))
3867
3868 (defun gnus-group-prev-unread-group (n)
3869   "Go to previous N'th unread newsgroup.
3870 Returns the difference between N and the number of skips actually
3871 done."  
3872   (interactive "p")
3873   (gnus-group-next-unread-group (- n)))
3874
3875 (defun gnus-group-next-unread-group-same-level (n)
3876   "Go to next N'th unread newsgroup on the same level.
3877 If N is negative, search backward instead.
3878 Returns the difference between N and the number of skips actually
3879 done."
3880   (interactive "p")
3881   (gnus-group-next-unread-group n t (gnus-group-group-level))
3882   (gnus-group-position-cursor))
3883
3884 (defun gnus-group-prev-unread-group-same-level (n)
3885   "Go to next N'th unread newsgroup on the same level.
3886 Returns the difference between N and the number of skips actually
3887 done."
3888   (interactive "p")
3889   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3890   (gnus-group-position-cursor))
3891
3892 (defun gnus-group-best-unread-group (&optional exclude-group)
3893   "Go to the group with the highest level.
3894 If EXCLUDE-GROUP, do not go to that group."
3895   (interactive)
3896   (goto-char (point-min))
3897   (let ((best 100000)
3898         unread best-point)
3899     (while (setq unread (get-text-property (point) 'gnus-unread))
3900       (if (and (numberp unread) (> unread 0))
3901           (progn
3902             (if (and (< (get-text-property (point) 'gnus-level) best)
3903                      (or (not exclude-group)
3904                          (not (equal exclude-group (gnus-group-group-name)))))
3905                 (progn 
3906                   (setq best (get-text-property (point) 'gnus-level))
3907                   (setq best-point (point))))))
3908       (forward-line 1))
3909     (if best-point (goto-char best-point))
3910     (gnus-summary-position-cursor)
3911     (and best-point (gnus-group-group-name))))
3912
3913 (defun gnus-group-first-unread-group ()
3914   "Go to the first group with unread articles."
3915   (interactive)
3916   (goto-char (point-min))
3917   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3918       (gnus-group-next-unread-group 1))
3919   (gnus-group-position-cursor))
3920
3921 (defun gnus-group-enter-server-mode ()
3922   "Jump to the server buffer."
3923   (interactive)
3924   (gnus-server-setup-buffer)
3925   (gnus-configure-windows 'server)
3926   (gnus-server-prepare))
3927
3928 (defun gnus-group-make-group (name method &optional address)
3929   "Add a new newsgroup.
3930 The user will be prompted for a NAME, for a select METHOD, and an
3931 ADDRESS."
3932   (interactive
3933    (cons 
3934     (read-string "Group name: ")
3935     (let ((method
3936            (completing-read 
3937             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3938             nil t)))
3939       (if (assoc method gnus-valid-select-methods)
3940           (list method
3941                 (if (memq 'prompt-address
3942                           (assoc method gnus-valid-select-methods))
3943                     (read-string "Address: ")
3944                   ""))
3945         (list method nil)))))
3946   
3947   (let* ((meth (if address (list (intern method) address) method))
3948          (nname (gnus-group-prefixed-name name meth))
3949          info)
3950     (and (gnus-gethash nname gnus-newsrc-hashtb)
3951          (error "Group %s already exists" nname))
3952     (gnus-group-change-level 
3953      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3954      gnus-level-default-subscribed gnus-level-killed 
3955      (and (gnus-group-group-name)
3956           (gnus-gethash (gnus-group-group-name)
3957                         gnus-newsrc-hashtb))
3958      t)
3959     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3960     (gnus-dribble-enter 
3961      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3962     (gnus-group-insert-group-line-info nname)
3963
3964     (if (assoc method gnus-valid-select-methods)
3965         (require (intern method)))
3966     (and (gnus-check-backend-function 'request-create-group nname)
3967          (gnus-request-create-group nname))))
3968
3969 (defun gnus-group-edit-group (group &optional part)
3970   "Edit the group on the current line."
3971   (interactive (list (gnus-group-group-name)))
3972   (let ((done-func '(lambda () 
3973                       "Exit editing mode and update the information."
3974                       (interactive)
3975                       (gnus-group-edit-group-done 'part 'group)))
3976         (part (or part 'info))
3977         (winconf (current-window-configuration))
3978         info)
3979     (or group (error "No group on current line"))
3980     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3981         (error "Killed group; can't be edited"))
3982     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3983     (gnus-configure-windows 'edit-group)
3984     (gnus-add-current-to-buffer-list)
3985     (emacs-lisp-mode)
3986     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3987     (use-local-map (copy-keymap emacs-lisp-mode-map))
3988     (local-set-key "\C-c\C-c" done-func)
3989     (make-local-variable 'gnus-prev-winconf)
3990     (setq gnus-prev-winconf winconf)
3991     ;; We modify the func to let it know what part it is editing.
3992     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3993     (setcar (cdr (cdr (nth 4 done-func))) group)
3994     (erase-buffer)
3995     (insert
3996      (cond 
3997       ((eq part 'method)
3998        ";; Type `C-c C-c' after editing the select method.\n\n")
3999       ((eq part 'params)
4000        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4001       ((eq part 'info)
4002        ";; Type `C-c C-c' after editing the group info.\n\n")))
4003     (let ((cinfo (gnus-copy-sequence info))
4004           marked)
4005       (if (not (setq marked (nth 3 cinfo)))
4006           ()
4007         (while marked
4008           (or (eq 'score (car (car marked)))
4009               (eq 'bookmark (car (car marked)))
4010               (eq 'killed (car (car marked)))
4011               (not (numberp (car (cdr (car marked)))))
4012               (setcdr (car marked) 
4013                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4014           (setq marked (cdr marked))))
4015       (insert 
4016        (pp-to-string
4017         (cond ((eq part 'method)
4018                (or (nth 4 info) "native"))
4019               ((eq part 'params)
4020                (nth 5 info))
4021               (t
4022                cinfo)))
4023        "\n"))))
4024
4025 (defun gnus-group-edit-group-method (group)
4026   "Edit the select method of GROUP."
4027   (interactive (list (gnus-group-group-name)))
4028   (gnus-group-edit-group group 'method))
4029
4030 (defun gnus-group-edit-group-parameters (group)
4031   "Edit the group parameters of GROUP."
4032   (interactive (list (gnus-group-group-name)))
4033   (gnus-group-edit-group group 'params))
4034
4035 (defun gnus-group-edit-group-done (part group)
4036   "Get info from buffer, update variables and jump to the group buffer."
4037   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4038   (goto-char (point-min))
4039   (let ((form (read (current-buffer)))
4040         (winconf gnus-prev-winconf))
4041     (if (eq part 'info) 
4042         (gnus-group-set-info form)
4043       (gnus-group-set-info form group part))
4044     (kill-buffer (current-buffer))
4045     (and winconf (set-window-configuration winconf))
4046     (set-buffer gnus-group-buffer)
4047     (gnus-group-update-group (gnus-group-group-name))
4048     (gnus-group-position-cursor)))
4049
4050 (defun gnus-group-make-help-group ()
4051   "Create the (ding) Gnus documentation group."
4052   (interactive)
4053   (let ((path load-path)
4054         name)
4055     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4056                                    "gnus-help" '(nndoc "gnus-help")))
4057                        gnus-newsrc-hashtb)
4058          (error "Documentation group already exists"))
4059     (while (and path
4060                 (not (file-exists-p (concat (file-name-as-directory (car path))
4061                                             "doc.txt"))))
4062       (setq path (cdr path)))
4063     (or path (error "Couldn't find doc group"))
4064     (gnus-group-make-group 
4065      (gnus-group-real-name name)
4066      (list 'nndoc name
4067            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4068            (list 'nndoc-article-type 'mbox))))
4069   (gnus-group-position-cursor))
4070
4071 (defun gnus-group-make-doc-group (file type)
4072   "Create a group that uses a single file as the source."
4073   (interactive 
4074    (list (read-file-name "File name: ") 
4075          (let ((err "")
4076                found char)
4077            (while (not found)
4078              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4079              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4080                                ((= char ?b) 'babyl)
4081                                ((= char ?d) 'digest)
4082                                (t (setq err "%c unknown. " char)
4083                                   nil))))
4084            found)))
4085   (let* ((file (expand-file-name file))
4086          (name (gnus-generate-new-group-name
4087                 (gnus-group-prefixed-name
4088                  (file-name-nondirectory file) '(nndoc "")))))
4089     (gnus-group-make-group 
4090      (gnus-group-real-name name)
4091      (list 'nndoc name
4092            (list 'nndoc-address file)
4093            (list 'nndoc-article-type type)))))
4094
4095 (defun gnus-group-make-archive-group ()
4096   "Create the (ding) Gnus archive group."
4097   (interactive)
4098   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4099                      gnus-newsrc-hashtb)
4100        (error "Archive group already exists"))
4101   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4102   (gnus-group-position-cursor))
4103
4104 (defun gnus-group-make-directory-group (dir)
4105   "Create an nndir group.
4106 The user will be prompted for a directory. The contents of this
4107 directory will be used as a newsgroup. The directory should contain
4108 mail messages or news articles in files that have numeric names."
4109   (interactive
4110    (list (read-file-name "Create group from directory: ")))
4111   (or (file-exists-p dir) (error "No such directory"))
4112   (or (file-directory-p dir) (error "Not a directory"))
4113   (gnus-group-make-group dir "nndir" dir)
4114   (gnus-group-position-cursor))
4115
4116 (defun gnus-group-make-kiboze-group (group address scores)
4117   "Create an nnkiboze group.
4118 The user will be prompted for a name, a regexp to match groups, and
4119 score file entries for articles to include in the group."
4120   (interactive
4121    (list
4122     (read-string "nnkiboze group name: ")
4123     (read-string "Source groups (regexp): ")
4124     (let ((headers (mapcar (lambda (group) (list group))
4125                            '("subject" "from" "number" "date" "message-id"
4126                              "references" "chars" "lines" "xref")))
4127           scores header regexp regexps)
4128       (while (not (equal "" (setq header (completing-read 
4129                                           "Match on header: " headers nil t))))
4130         (setq regexps nil)
4131         (while (not (equal "" (setq regexp (read-string 
4132                                             (format "Match on %s (string): "
4133                                                     header)))))
4134           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4135         (setq scores (cons (cons header regexps) scores)))
4136       scores)))
4137   (gnus-group-make-group group "nnkiboze" address)
4138   (save-excursion
4139     (gnus-set-work-buffer)
4140     (let (emacs-lisp-mode-hook)
4141       (pp scores (current-buffer)))
4142     (write-region (point-min) (point-max) 
4143                   (concat (or gnus-kill-files-directory "~/News")
4144                           "nnkiboze:" group "." gnus-score-file-suffix)))
4145   (gnus-group-position-cursor))
4146
4147 (defun gnus-group-add-to-virtual (n vgroup)
4148   "Add the current group to a virtual group."
4149   (interactive
4150    (list current-prefix-arg
4151          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4152                           "nnvirtual:")))
4153   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4154       (error "%s is not an nnvirtual group" vgroup))
4155   (let* ((groups (gnus-group-process-prefix n))
4156          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4157     (setcar (cdr method)
4158             (concat 
4159              (nth 1 method) "\\|"
4160              (mapconcat 
4161               (lambda (s) 
4162                 (gnus-group-remove-mark s)
4163                 (concat "\\(^" (regexp-quote s) "$\\)"))
4164               groups "\\|"))))
4165   (gnus-group-position-cursor))
4166
4167 (defun gnus-group-make-empty-virtual (group)
4168   "Create a new, fresh, empty virtual group."
4169   (interactive "sCreate new, empty virtual group: ")
4170   (let* ((method (list 'nnvirtual "^$"))
4171          (pgroup (gnus-group-prefixed-name group method)))
4172     ;; Check whether it exists already.
4173     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4174          (error "Group %s already exists." pgroup))
4175     ;; Subscribe the new group after the group on the current line.
4176     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4177     (gnus-group-update-group pgroup)
4178     (forward-line -1)
4179     (gnus-group-position-cursor)))
4180
4181 (defun gnus-group-enter-directory (dir)
4182   "Enter an ephemeral nneething group."
4183   (interactive "DDirectory to read: ")
4184   (let* ((method (list 'nneething dir))
4185          (leaf (gnus-group-prefixed-name
4186                 (file-name-nondirectory (directory-file-name dir))
4187                 method))
4188          (name (gnus-generate-new-group-name leaf)))
4189     (let ((nneething-read-only t))
4190       (or (gnus-group-read-ephemeral-group 
4191            name method t
4192            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4193                                       'summary 'group)))
4194           (error "Couldn't enter %s" dir)))))
4195
4196 ;; Group sorting commands
4197 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4198
4199 (defun gnus-group-sort-groups ()
4200   "Sort the group buffer using `gnus-group-sort-function'."
4201   (interactive)
4202   (setq gnus-newsrc-alist 
4203         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4204   (gnus-make-hashtable-from-newsrc-alist)
4205   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4206
4207 (defun gnus-group-sort-by-alphabet (info1 info2)
4208   (string< (car info1) (car info2)))
4209
4210 (defun gnus-group-sort-by-unread (info1 info2)
4211   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4212         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4213     (< (or (and (numberp n1) n1) 0)
4214        (or (and (numberp n2) n2) 0))))
4215
4216 (defun gnus-group-sort-by-level (info1 info2)
4217   (< (nth 1 info1) (nth 1 info2)))
4218
4219 ;; Group catching up.
4220
4221 (defun gnus-group-catchup-current (n &optional all)
4222   "Mark all articles not marked as unread in current newsgroup as read.
4223 If prefix argument N is numeric, the ARG next newsgroups will be
4224 caught up. If ALL is non-nil, marked articles will also be marked as
4225 read. Cross references (Xref: header) of articles are ignored.
4226 The difference between N and actual number of newsgroups that were
4227 caught up is returned."
4228   (interactive "P")
4229   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4230                gnus-expert-user
4231                (gnus-y-or-n-p
4232                 (if all
4233                     "Do you really want to mark all articles as read? "
4234                   "Mark all unread articles as read? "))))
4235       n
4236     (let ((groups (gnus-group-process-prefix n))
4237           (ret 0))
4238       (while groups
4239         ;; Virtual groups have to be given special treatment. 
4240         (let ((method (gnus-find-method-for-group (car groups))))
4241           (if (eq 'nnvirtual (car method))
4242               (nnvirtual-catchup-group
4243                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4244         (gnus-group-remove-mark (car groups))
4245         (if (prog1
4246                 (gnus-group-goto-group (car groups))
4247               (gnus-group-catchup (car groups) all))
4248             (gnus-group-update-group-line)
4249           (setq ret (1+ ret)))
4250         (setq groups (cdr groups)))
4251       (gnus-group-next-unread-group 1)
4252       ret)))
4253
4254 (defun gnus-group-catchup-current-all (n)
4255   "Mark all articles in current newsgroup as read.
4256 Cross references (Xref: header) of articles are ignored."
4257   (interactive "P")
4258   (gnus-group-catchup-current n 'all))
4259
4260 (defun gnus-group-catchup (group &optional all)
4261   "Mark all articles in GROUP as read.
4262 If ALL is non-nil, all articles are marked as read.
4263 The return value is the number of articles that were marked as read,
4264 or nil if no action could be taken."
4265   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4266          (num (car entry))
4267          (marked (nth 3 (nth 2 entry))))
4268     (if (not (numberp (car entry)))
4269         (gnus-message 1 "Can't catch up; non-active group")
4270       ;; Do the updating only if the newsgroup isn't killed.
4271       (if (not entry)
4272           ()
4273         (gnus-update-read-articles 
4274          group (and (not all) (append (cdr (assq 'tick marked))
4275                                       (cdr (assq 'dormant marked))))
4276          nil (and (not all) (cdr (assq 'tick marked))))
4277         (and all marked
4278              (setcar (nthcdr 3 (nth 2 entry)) 
4279                      (delq (assq 'dormant marked) 
4280                            (nth 3 (nth 2 entry)))))))
4281     num))
4282
4283 (defun gnus-group-expire-articles (n)
4284   "Expire all expirable articles in the current newsgroup."
4285   (interactive "P")
4286   (let ((groups (gnus-group-process-prefix n))
4287         group)
4288     (or groups (error "No groups to expire"))
4289     (while groups
4290       (setq group (car groups)
4291             groups (cdr groups))
4292       (gnus-group-remove-mark group)
4293       (if (not (gnus-check-backend-function 'request-expire-articles group))
4294           ()
4295         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4296                (expirable (if (memq 'total-expire (nth 5 info))
4297                               (cons nil (gnus-list-of-read-articles group))
4298                             (assq 'expire (nth 3 info)))))
4299           (and expirable 
4300                (setcdr expirable
4301                        (gnus-request-expire-articles 
4302                         (cdr expirable) group))))))))
4303
4304 (defun gnus-group-expire-all-groups ()
4305   "Expire all expirable articles in all newsgroups."
4306   (interactive)
4307   (save-excursion
4308     (gnus-message 5 "Expiring...")
4309     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4310                                      (cdr gnus-newsrc-alist))))
4311       (gnus-group-expire-articles nil)))
4312   (gnus-group-position-cursor)
4313   (gnus-message 5 "Expiring...done"))
4314
4315 (defun gnus-group-set-current-level (n level)
4316   "Set the level of the next N groups to LEVEL."
4317   (interactive "P\nnLevel: ")
4318   (or (and (>= level 1) (<= level gnus-level-killed))
4319       (error "Illegal level: %d" level))
4320   (let ((groups (gnus-group-process-prefix n))
4321         group)
4322     (while groups
4323       (setq group (car groups)
4324             groups (cdr groups))
4325       (gnus-group-remove-mark group)
4326       (gnus-message 6 "Changed level of %s from %d to %d" 
4327                     group (gnus-group-group-level) level)
4328       (gnus-group-change-level group level
4329                                (gnus-group-group-level))
4330       (gnus-group-update-group-line)))
4331   (gnus-group-position-cursor))
4332
4333 (defun gnus-group-unsubscribe-current-group (n)
4334   "Toggle subscription of the current group.
4335 If given numerical prefix, toggle the N next groups."
4336   (interactive "P")
4337   (let ((groups (gnus-group-process-prefix n))
4338         group)
4339     (while groups
4340       (setq group (car groups)
4341             groups (cdr groups))
4342       (gnus-group-remove-mark group)
4343       (gnus-group-unsubscribe-group
4344        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4345                  gnus-level-default-unsubscribed
4346                gnus-level-default-subscribed))
4347       (gnus-group-update-group-line))
4348     (gnus-group-next-group 1)))
4349
4350 (defun gnus-group-unsubscribe-group (group &optional level)
4351   "Toggle subscribe from/to unsubscribe GROUP.
4352 New newsgroup is added to .newsrc automatically."
4353   (interactive
4354    (list (completing-read "Group: " gnus-active-hashtb nil 
4355                           gnus-have-read-active-file)))
4356   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4357     (cond (newsrc
4358            ;; Toggle subscription flag.
4359            (gnus-group-change-level 
4360             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4361                                            gnus-level-subscribed) 
4362                                        (1+ gnus-level-subscribed)
4363                                      gnus-level-default-subscribed)))
4364            (gnus-group-update-group group))
4365           ((and (stringp group)
4366                 (or (not gnus-have-read-active-file)
4367                     (gnus-gethash group gnus-active-hashtb)))
4368            ;; Add new newsgroup.
4369            (gnus-group-change-level 
4370             group 
4371             (if level level gnus-level-default-subscribed) 
4372             (or (and (member group gnus-zombie-list) 
4373                      gnus-level-zombie) 
4374                 gnus-level-killed)
4375             (and (gnus-group-group-name)
4376                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4377            (gnus-group-update-group group))
4378           (t (error "No such newsgroup: %s" group)))
4379     (gnus-group-position-cursor)))
4380
4381 (defun gnus-group-transpose-groups (n)
4382   "Move the current newsgroup up N places.
4383 If given a negative prefix, move down instead. The difference between
4384 N and the number of steps taken is returned." 
4385   (interactive "p")
4386   (or (gnus-group-group-name)
4387       (error "No group on current line"))
4388   (gnus-group-kill-group 1)
4389   (prog1
4390       (forward-line (- n))
4391     (gnus-group-yank-group)
4392     (gnus-group-position-cursor)))
4393
4394 (defun gnus-group-kill-all-zombies ()
4395   "Kill all zombie newsgroups."
4396   (interactive)
4397   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4398   (setq gnus-zombie-list nil)
4399   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4400   (goto-char (point-min))
4401   (gnus-group-position-cursor))
4402
4403 (defun gnus-group-kill-region (begin end)
4404   "Kill newsgroups in current region (excluding current point).
4405 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4406   (interactive "r")
4407   (let ((lines
4408          ;; Count lines.
4409          (save-excursion
4410            (count-lines
4411             (progn
4412               (goto-char begin)
4413               (beginning-of-line)
4414               (point))
4415             (progn
4416               (goto-char end)
4417               (beginning-of-line)
4418               (point))))))
4419     (goto-char begin)
4420     (beginning-of-line)                 ;Important when LINES < 1
4421     (gnus-group-kill-group lines)))
4422
4423 (defun gnus-group-kill-group (n)
4424   "The the next N groups.
4425 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4426 However, only groups that were alive can be yanked; already killed 
4427 groups or zombie groups can't be yanked.
4428 The return value is the name of the (last) group that was killed."
4429   (interactive "P")
4430   (let ((buffer-read-only nil)
4431         (groups (gnus-group-process-prefix n))
4432         group entry level)
4433     (while groups
4434       (setq group (car groups)
4435             groups (cdr groups))
4436       (gnus-group-remove-mark group)
4437       (setq level (gnus-group-group-level))
4438       (gnus-delete-line)
4439       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4440           (setq gnus-list-of-killed-groups 
4441                 (cons (cons (car entry) (nth 2 entry)) 
4442                       gnus-list-of-killed-groups)))
4443       (gnus-group-change-level 
4444        (if entry entry group) gnus-level-killed (if entry nil level)))
4445     (gnus-group-position-cursor)
4446     group))
4447
4448 (defun gnus-group-yank-group (&optional arg)
4449   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4450 inserting it before the current newsgroup.  The numeric ARG specifies
4451 how many newsgroups are to be yanked.  The name of the (last)
4452 newsgroup yanked is returned."
4453   (interactive "p")
4454   (if (not arg) (setq arg 1))
4455   (let (info group prev)
4456     (while (>= (setq arg (1- arg)) 0)
4457       (if (not (setq info (car gnus-list-of-killed-groups)))
4458           (error "No more newsgroups to yank"))
4459       (setq group (nth 2 info))
4460       ;; Find which newsgroup to insert this one before - search
4461       ;; backward until something suitable is found. If there are no
4462       ;; other newsgroups in this buffer, just make this newsgroup the
4463       ;; first newsgroup.
4464       (setq prev (gnus-group-group-name))
4465       (gnus-group-change-level 
4466        info (nth 2 info) gnus-level-killed 
4467        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4468        t)
4469       (gnus-group-insert-group-line-info (nth 1 info))
4470       (setq gnus-list-of-killed-groups 
4471             (cdr gnus-list-of-killed-groups)))
4472     (forward-line -1)
4473     (gnus-group-position-cursor)
4474     group))
4475       
4476 (defun gnus-group-list-all-groups (arg)
4477   "List all newsgroups with level ARG or lower.
4478 Default is gnus-level-unsubscribed, which lists all subscribed and most
4479 unsubscribed groups."
4480   (interactive "P")
4481   (setq arg (or arg gnus-level-unsubscribed))
4482   (gnus-group-list-groups arg t))
4483
4484 (defun gnus-group-list-killed ()
4485   "List all killed newsgroups in the group buffer."
4486   (interactive)
4487   (if (not gnus-killed-list)
4488       (gnus-message 6 "No killed groups")
4489     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4490     (goto-char (point-min)))
4491   (gnus-group-position-cursor))
4492
4493 (defun gnus-group-list-zombies ()
4494   "List all zombie newsgroups in the group buffer."
4495   (interactive)
4496   (if (not gnus-zombie-list)
4497       (gnus-message 6 "No zombie groups")
4498     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4499     (goto-char (point-min)))
4500   (gnus-group-position-cursor))
4501
4502 (defun gnus-group-get-new-news (&optional arg)
4503   "Get newly arrived articles.
4504 If ARG is non-nil, it should be a number between one and nine to
4505 specify which levels you are interested in re-scanning."
4506   (interactive "P")
4507   (run-hooks 'gnus-get-new-news-hook)
4508   (if gnus-group-use-permanent-levels
4509       (setq arg
4510             (setq gnus-group-default-list-level 
4511                   (or arg gnus-group-default-list-level
4512                       gnus-level-subscribed))))
4513   (if (and gnus-read-active-file (not arg))
4514       (progn
4515         (gnus-read-active-file)
4516         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4517     (let ((gnus-read-active-file nil)
4518           (gnus-have-read-active-file (not arg)))
4519       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4520   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4521                               gnus-group-default-list-level
4522                               gnus-level-subscribed)
4523                           gnus-have-all-newsgroups))
4524
4525 (defun gnus-group-get-new-news-this-group (n)
4526   "Check for newly arrived news in the current group (and the N-1 next groups).
4527 The difference between N and the number of newsgroup checked is returned.
4528 If N is negative, this group and the N-1 previous groups will be checked."
4529   (interactive "P")
4530   (let* ((groups (gnus-group-process-prefix n))
4531          (ret (if (numberp n) (- n (length groups)) 0))
4532          group)
4533     (while groups
4534       (setq group (car groups)
4535             groups (cdr groups))
4536       (gnus-group-remove-mark group)
4537       (or (gnus-get-new-news-in-group group)
4538           (progn 
4539             (ding) 
4540             (message "%s error: %s" group (gnus-status-message group))
4541             (sit-for 2))))
4542     ;; !!! I don't know why the buffer scrolls forward when updating
4543     ;; the first line in the group buffer, but it does. So we set the
4544     ;; window start forcibly.
4545 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4546     (gnus-group-next-unread-group 1 t)
4547     (gnus-summary-position-cursor)
4548     ret))
4549
4550 (defun gnus-get-new-news-in-group (group)
4551   (and group 
4552        (gnus-activate-newsgroup group)
4553        (progn
4554          (gnus-get-unread-articles-in-group 
4555           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4556           (gnus-gethash group gnus-active-hashtb))
4557          (gnus-group-update-group-line)
4558          t)))
4559
4560 (defun gnus-group-fetch-faq (group)
4561   "Fetch the FAQ for the current group."
4562   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4563   (or group (error "No group name given"))
4564   (let ((file (concat gnus-group-faq-directory group))) 
4565     (if (not (file-exists-p file))
4566         (error "No such file: %s" file)
4567       (find-file file))))
4568   
4569 (defun gnus-group-describe-group (force &optional group)
4570   "Display a description of the current newsgroup."
4571   (interactive (list current-prefix-arg (gnus-group-group-name)))
4572   (and force (setq gnus-description-hashtb nil))
4573   (let ((method (gnus-find-method-for-group group))
4574         desc)
4575     (or group (error "No group name given"))
4576     (and (or (and gnus-description-hashtb
4577                   ;; We check whether this group's method has been
4578                   ;; queried for a description file.  
4579                   (gnus-gethash 
4580                    (gnus-group-prefixed-name "" method) 
4581                    gnus-description-hashtb))
4582              (setq desc (gnus-group-get-description group))
4583              (gnus-read-descriptions-file method))
4584          (message
4585           (or desc (gnus-gethash group gnus-description-hashtb)
4586               "No description available")))))
4587
4588 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4589 (defun gnus-group-describe-all-groups (force)
4590   "Pop up a buffer with descriptions of all newsgroups."
4591   (interactive "P")
4592   (and force (setq gnus-description-hashtb nil))
4593   (if (not (or gnus-description-hashtb
4594                (gnus-read-all-descriptions-files)))
4595       (error "Couldn't request descriptions file"))
4596   (let ((buffer-read-only nil)
4597         b)
4598     (erase-buffer)
4599     (mapatoms
4600      (lambda (group)
4601        (setq b (point))
4602        (insert (format "      *: %-20s %s\n" (symbol-name group)
4603                        (symbol-value group)))
4604        (add-text-properties 
4605         b (1+ b) (list 'gnus-group group
4606                        'gnus-unread t 'gnus-marked nil
4607                        'gnus-level (1+ gnus-level-subscribed))))
4608      gnus-description-hashtb)
4609     (goto-char (point-min))
4610     (gnus-group-position-cursor)))
4611
4612 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4613 (defun gnus-group-apropos (regexp &optional search-description)
4614   "List all newsgroups that have names that match a regexp."
4615   (interactive "sGnus apropos (regexp): ")
4616   (let ((prev "")
4617         (obuf (current-buffer))
4618         groups des)
4619     ;; Go through all newsgroups that are known to Gnus.
4620     (mapatoms 
4621      (lambda (group)
4622        (and (string-match regexp (symbol-name group))
4623             (setq groups (cons (symbol-name group) groups))))
4624      gnus-active-hashtb)
4625     ;; Go through all descriptions that are known to Gnus. 
4626     (if search-description
4627         (mapatoms 
4628          (lambda (group)
4629            (and (string-match regexp (symbol-value group))
4630                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4631                 (setq groups (cons (symbol-name group) groups))))
4632          gnus-description-hashtb))
4633     (if (not groups)
4634         (gnus-message 3 "No groups matched \"%s\"." regexp)
4635       ;; Print out all the groups.
4636       (save-excursion
4637         (pop-to-buffer "*Gnus Help*")
4638         (buffer-disable-undo (current-buffer))
4639         (erase-buffer)
4640         (setq groups (sort groups 'string<))
4641         (while groups
4642           ;; Groups may be entered twice into the list of groups.
4643           (if (not (string= (car groups) prev))
4644               (progn
4645                 (insert (setq prev (car groups)) "\n")
4646                 (if (and gnus-description-hashtb
4647                          (setq des (gnus-gethash (car groups) 
4648                                                  gnus-description-hashtb)))
4649                     (insert "  " des "\n"))))
4650           (setq groups (cdr groups)))
4651         (goto-char (point-min))))
4652     (pop-to-buffer obuf)))
4653
4654 (defun gnus-group-description-apropos (regexp)
4655   "List all newsgroups that have names or descriptions that match a regexp."
4656   (interactive "sGnus description apropos (regexp): ")
4657   (if (not (or gnus-description-hashtb
4658                (gnus-read-all-descriptions-files)))
4659       (error "Couldn't request descriptions file"))
4660   (gnus-group-apropos regexp t))
4661
4662 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4663 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4664   "List all groups with unread articles that match REGEXP.
4665 If the prefix LEVEL is non-nil, it should be a number that says which
4666 level to cut off listing groups. 
4667 If ALL, also list groups with no unread articles.
4668 If LOWEST, don't list groups with level lower than LOWEST."
4669   (interactive "P\nsList newsgroups matching: ")
4670   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4671                            all (or lowest 1) regexp)
4672   (goto-char (point-min))
4673   (gnus-group-position-cursor))
4674
4675 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4676   "List all groups that match REGEXP.
4677 If the prefix LEVEL is non-nil, it should be a number that says which
4678 level to cut off listing groups. 
4679 If LOWEST, don't list groups with level lower than LOWEST."
4680   (interactive "P\nsList newsgroups matching: ")
4681   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4682
4683 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4684 (defun gnus-group-save-newsrc ()
4685   "Save the Gnus startup files."
4686   (interactive)
4687   (gnus-save-newsrc-file))
4688
4689 (defun gnus-group-restart (&optional arg)
4690   "Force Gnus to read the .newsrc file."
4691   (interactive "P")
4692   (gnus-save-newsrc-file)
4693   (gnus-setup-news 'force)
4694   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4695
4696 (defun gnus-group-read-init-file ()
4697   "Read the Gnus elisp init file."
4698   (interactive)
4699   (gnus-read-init-file))
4700
4701 (defun gnus-group-check-bogus-groups (silent)
4702   "Check bogus newsgroups.
4703 If given a prefix, don't ask for confirmation before removing a bogus
4704 group."
4705   (interactive "P")
4706   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4707   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4708
4709 (defun gnus-group-edit-global-kill (article &optional group)
4710   "Edit the global kill file.
4711 If GROUP, edit that local kill file instead."
4712   (interactive "P")
4713   (setq gnus-current-kill-article article)
4714   (gnus-kill-file-edit-file group)
4715   (gnus-message 6
4716    (substitute-command-keys
4717     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4718
4719 (defun gnus-group-edit-local-kill (article group)
4720   "Edit a local kill file."
4721   (interactive (list nil (gnus-group-group-name)))
4722   (gnus-group-edit-global-kill article group))
4723
4724 (defun gnus-group-force-update ()
4725   "Update `.newsrc' file."
4726   (interactive)
4727   (gnus-save-newsrc-file))
4728
4729 (defun gnus-group-suspend ()
4730   "Suspend the current Gnus session.
4731 In fact, cleanup buffers except for group mode buffer.
4732 The hook gnus-suspend-gnus-hook is called before actually suspending."
4733   (interactive)
4734   (run-hooks 'gnus-suspend-gnus-hook)
4735   ;; Kill Gnus buffers except for group mode buffer.
4736   (let ((group-buf (get-buffer gnus-group-buffer)))
4737     ;; Do this on a separate list in case the user does a ^G before we finish
4738     (let ((gnus-buffer-list
4739            (delq group-buf (delq gnus-dribble-buffer
4740                                  (append gnus-buffer-list nil)))))
4741       (while gnus-buffer-list
4742         (gnus-kill-buffer (car gnus-buffer-list))
4743         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4744     (if group-buf
4745         (progn
4746           (setq gnus-buffer-list (list group-buf))
4747           (bury-buffer group-buf)
4748           (delete-windows-on group-buf t)))))
4749
4750 (defun gnus-group-clear-dribble ()
4751   "Clear all information from the dribble buffer."
4752   (interactive)
4753   (gnus-dribble-clear))
4754
4755 (defun gnus-group-exit ()
4756   "Quit reading news after updating .newsrc.eld and .newsrc.
4757 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4758   (interactive)
4759   (if (or noninteractive                ;For gnus-batch-kill
4760           (zerop (buffer-size))         ;No news is good news.
4761           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4762           (not gnus-interactive-exit)   ;Without confirmation
4763           gnus-expert-user
4764           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4765       (progn
4766         (if gnus-use-full-window
4767             (delete-other-windows)
4768           (gnus-remove-some-windows))
4769         (run-hooks 'gnus-exit-gnus-hook)
4770         (gnus-offer-save-summaries)
4771         (gnus-save-newsrc-file)
4772         (gnus-close-backends)
4773         (gnus-clear-system))))
4774
4775 (defun gnus-close-backends ()
4776   ;; Send a close request to all backends that support such a request. 
4777   (let ((methods gnus-valid-select-methods)
4778         func)
4779     (while methods
4780       (if (fboundp (setq func (intern (concat (car (car methods))
4781                                               "-request-close"))))
4782           (funcall func))
4783       (setq methods (cdr methods)))))
4784
4785 (defun gnus-group-quit ()
4786   "Quit reading news without updating .newsrc.eld or .newsrc.
4787 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4788   (interactive)
4789   (if (or noninteractive                ;For gnus-batch-kill
4790           (zerop (buffer-size))
4791           (not (gnus-server-opened gnus-select-method))
4792           gnus-expert-user
4793           (not gnus-current-startup-file)
4794           (gnus-yes-or-no-p
4795            (format "Quit reading news without saving %s? "
4796                    (file-name-nondirectory gnus-current-startup-file))))
4797       (progn
4798         (run-hooks 'gnus-exit-gnus-hook)
4799         (if gnus-use-full-window
4800             (delete-other-windows)
4801           (gnus-remove-some-windows))
4802         (gnus-dribble-save)
4803         (gnus-close-backends)
4804         (gnus-clear-system))))
4805
4806 (defun gnus-offer-save-summaries ()
4807   (let ((buffers (buffer-list)))
4808     (save-excursion
4809       (while buffers
4810         (and 
4811          ;; We look for buffers with "Summary" in the name.
4812          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4813          (progn
4814            (set-buffer (car buffers))
4815            ;; We check that this is, indeed, a summary buffer.
4816            (eq major-mode 'gnus-summary-mode)) 
4817          ;; We ask the user whether she wants to save the info.
4818          (gnus-y-or-n-p
4819                (format "Update summary buffer %s? " (buffer-name)))
4820          ;; We do it by simply exiting.
4821          (gnus-summary-exit))
4822         (setq buffers (cdr buffers))))))
4823
4824 (defun gnus-group-describe-briefly ()
4825   "Give a one line description of the group mode commands."
4826   (interactive)
4827   (gnus-message 6
4828    (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")))
4829
4830 (defun gnus-group-browse-foreign-server (method)
4831   "Browse a foreign news server.
4832 If called interactively, this function will ask for a select method
4833  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4834 If not, METHOD should be a list where the first element is the method
4835 and the second element is the address."
4836   (interactive
4837    (list (let ((how (completing-read 
4838                      "Which backend: "
4839                      (append gnus-valid-select-methods gnus-server-alist)
4840                      nil t "nntp")))
4841            ;; We either got a backend name or a virtual server name.
4842            ;; If the first, we also need an address.
4843            (if (assoc how gnus-valid-select-methods)
4844                (list (intern how)
4845                      ;; Suggested by mapjph@bath.ac.uk.
4846                      (completing-read 
4847                       "Address: " 
4848                       (mapcar (lambda (server) (list server))
4849                               gnus-secondary-servers)))
4850              ;; We got a server name, so we find the method.
4851              (gnus-server-to-method how)))))
4852   (gnus-browse-foreign-server method))
4853
4854 \f
4855 ;;;
4856 ;;; Browse Server Mode
4857 ;;;
4858
4859 (defvar gnus-browse-mode-hook nil)
4860 (defvar gnus-browse-mode-map nil)
4861 (put 'gnus-browse-mode 'mode-class 'special)
4862
4863 (if gnus-browse-mode-map
4864     nil
4865   (setq gnus-browse-mode-map (make-keymap))
4866   (suppress-keymap gnus-browse-mode-map)
4867   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4868   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4869   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4870   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4871   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4872   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4873   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4874   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4875   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4876   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4877   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4878   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4879   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4880   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4881   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4882   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4883   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4884   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4885   )
4886
4887 (defvar gnus-browse-current-method nil)
4888 (defvar gnus-browse-return-buffer nil)
4889
4890 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4891
4892 (defun gnus-browse-foreign-server (method &optional return-buffer)
4893   (setq gnus-browse-current-method method)
4894   (setq gnus-browse-return-buffer return-buffer)
4895   (let ((gnus-select-method method)
4896         groups group)
4897     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4898     (or (gnus-server-opened method)
4899         (gnus-open-server method)
4900         (error "Unable to contact server: %s" (gnus-status-message method)))
4901     (or (gnus-request-list method)
4902         (error "Couldn't request list: %s" (gnus-status-message method)))
4903     (get-buffer-create gnus-browse-buffer)
4904     (gnus-add-current-to-buffer-list)
4905     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4906     (gnus-configure-windows 'browse)
4907     (buffer-disable-undo (current-buffer))
4908     (let ((buffer-read-only nil))
4909       (erase-buffer))
4910     (gnus-browse-mode)
4911     (setq mode-line-buffer-identification
4912           (format
4913            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4914     (save-excursion
4915       (set-buffer nntp-server-buffer)
4916       (let ((cur (current-buffer)))
4917         (goto-char (point-min))
4918         (or (string= gnus-ignored-newsgroups "")
4919             (delete-matching-lines gnus-ignored-newsgroups))
4920         (while (re-search-forward 
4921                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4922           (goto-char (match-end 1))
4923           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4924                                                      (match-end 1))
4925                                    (max 0 (- (1+ (read cur)) (read cur))))
4926                              groups)))))
4927     (setq groups (sort groups 
4928                        (lambda (l1 l2)
4929                          (string< (car l1) (car l2)))))
4930     (let ((buffer-read-only nil))
4931       (while groups
4932         (setq group (car groups))
4933         (insert 
4934          (format "K%7d: %s\n" (cdr group) (car group)))
4935         (setq groups (cdr groups))))
4936     (switch-to-buffer (current-buffer))
4937     (goto-char (point-min))
4938     (gnus-group-position-cursor)))
4939
4940 (defun gnus-browse-mode ()
4941   "Major mode for browsing a foreign server.
4942
4943 All normal editing commands are switched off.
4944
4945 \\<gnus-browse-mode-map>
4946 The only things you can do in this buffer is
4947
4948 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4949 The group will be inserted into the group buffer upon exit from this
4950 buffer.  
4951
4952 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4953
4954 3) `\\[gnus-browse-exit]' to return to the group buffer."
4955   (interactive)
4956   (kill-all-local-variables)
4957   (if gnus-visual (gnus-browse-make-menu-bar))
4958   (setq mode-line-modified "-- ")
4959   (make-local-variable 'mode-line-format)
4960   (setq mode-line-format (copy-sequence mode-line-format))
4961   (and (equal (nth 3 mode-line-format) "   ")
4962        (setcar (nthcdr 3 mode-line-format) ""))
4963   (setq major-mode 'gnus-browse-mode)
4964   (setq mode-name "Browse Server")
4965   (setq mode-line-process nil)
4966   (use-local-map gnus-browse-mode-map)
4967   (buffer-disable-undo (current-buffer))
4968   (setq truncate-lines t)
4969   (setq buffer-read-only t)
4970   (run-hooks 'gnus-browse-mode-hook))
4971
4972 (defun gnus-browse-read-group (&optional no-article)
4973   "Enter the group at the current line."
4974   (interactive)
4975   (let ((group (gnus-browse-group-name)))
4976     (or (gnus-group-read-ephemeral-group 
4977          group gnus-browse-current-method nil
4978          (cons (current-buffer) 'browse))
4979         (error "Couldn't enter %s" group))))
4980
4981 (defun gnus-browse-select-group ()
4982   "Select the current group."
4983   (interactive)
4984   (gnus-browse-read-group 'no))
4985
4986 (defun gnus-browse-next-group (n)
4987   "Go to the next group."
4988   (interactive "p")
4989   (prog1
4990       (forward-line n)
4991     (gnus-group-position-cursor)))
4992
4993 (defun gnus-browse-prev-group (n)
4994   "Go to the next group."
4995   (interactive "p")
4996   (gnus-browse-next-group (- n)))
4997
4998 (defun gnus-browse-unsubscribe-current-group (arg)
4999   "(Un)subscribe to the next ARG groups."
5000   (interactive "p")
5001   (and (eobp)
5002        (error "No group at current line."))
5003   (let ((ward (if (< arg 0) -1 1))
5004         (arg (abs arg)))
5005     (while (and (> arg 0)
5006                 (not (eobp))
5007                 (gnus-browse-unsubscribe-group)
5008                 (zerop (gnus-browse-next-group ward)))
5009       (setq arg (1- arg)))
5010     (gnus-group-position-cursor)
5011     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5012     arg))
5013
5014 (defun gnus-browse-group-name ()
5015   (save-excursion
5016     (beginning-of-line)
5017     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5018         ()
5019       (gnus-group-prefixed-name 
5020        (buffer-substring (match-beginning 1) (match-end 1))
5021        gnus-browse-current-method))))
5022   
5023 (defun gnus-browse-unsubscribe-group ()
5024   (let ((sub nil)
5025         (buffer-read-only nil)
5026         group)
5027     (save-excursion
5028       (beginning-of-line)
5029       (if (= (following-char) ?K) (setq sub t))
5030       (setq group (gnus-browse-group-name))
5031       (beginning-of-line)
5032       (delete-char 1)
5033       (if sub
5034           (progn
5035             (gnus-group-change-level 
5036              (list t group gnus-level-default-subscribed
5037                    nil nil gnus-browse-current-method) 
5038              gnus-level-default-subscribed gnus-level-killed
5039              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
5040              t)
5041             (insert ? ))
5042         (gnus-group-change-level 
5043          group gnus-level-killed gnus-level-default-subscribed)
5044         (insert ?K)))
5045     t))
5046
5047 (defun gnus-browse-exit ()
5048   "Quit browsing and return to the group buffer."
5049   (interactive)
5050   (if (eq major-mode 'gnus-browse-mode)
5051       (kill-buffer (current-buffer)))
5052   (if gnus-browse-return-buffer
5053       (gnus-configure-windows 'server)
5054     (gnus-configure-windows 'group)
5055     (gnus-group-list-groups nil)))
5056
5057 (defun gnus-browse-describe-briefly ()
5058   "Give a one line description of the group mode commands."
5059   (interactive)
5060   (gnus-message 6
5061    (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")))
5062       
5063 \f
5064 ;;;
5065 ;;; Gnus summary mode
5066 ;;;
5067
5068 (defvar gnus-summary-mode-map nil)
5069 (defvar gnus-summary-mark-map nil)
5070 (defvar gnus-summary-mscore-map nil)
5071 (defvar gnus-summary-article-map nil)
5072 (defvar gnus-summary-thread-map nil)
5073 (defvar gnus-summary-goto-map nil)
5074 (defvar gnus-summary-exit-map nil)
5075 (defvar gnus-summary-various-map nil)
5076 (defvar gnus-summary-interest-map nil)
5077 (defvar gnus-summary-sort-map nil)
5078 (defvar gnus-summary-backend-map nil)
5079 (defvar gnus-summary-save-map nil)
5080 (defvar gnus-summary-wash-map nil)
5081 (defvar gnus-summary-help-map nil)
5082
5083 (put 'gnus-summary-mode 'mode-class 'special)
5084
5085 (if gnus-summary-mode-map
5086     nil
5087   (setq gnus-summary-mode-map (make-keymap))
5088   (suppress-keymap gnus-summary-mode-map)
5089
5090   ;; Non-orthogonal keys
5091
5092   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5093   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5094   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5095   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5096   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5097   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5098   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5099   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5100   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5101   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5102   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5103   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5104   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5105   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5106   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5107   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5108   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5109   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5110   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5111   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5112   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5113   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5114   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5115   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5116   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5117   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5118   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5119   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5120   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5121   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5122   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5123   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5124   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5125   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5126   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5127   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5128   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5129   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5130   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5131   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5132   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5133   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5134   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5135   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5136   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5137   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5138   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5139   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5140   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5141   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5142   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5143   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5144   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5145   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5146   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5147   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5148   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5149   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5150   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5151   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5152   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5153   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5154   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5155   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5156   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5157   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5158   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5159   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5160   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5161   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5162   (define-key gnus-summary-mode-map "V" 'gnus-version)
5163   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5164   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5165   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5166   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5167   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5168   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5169   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5170   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5171 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5172   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5173   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5174   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5175 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5176   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5177   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5178   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5179   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5180   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5181
5182
5183   ;; Sort of orthogonal keymap
5184   (define-prefix-command 'gnus-summary-mark-map)
5185   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5186   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5187   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5188   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5189   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5190   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5191   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5192   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5193   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5194   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5195   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5196   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5197   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5198   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5199   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5200   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5201   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5202   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5203   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5204   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5205   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5206   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5207   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5208   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5209
5210   (define-prefix-command 'gnus-summary-mscore-map)
5211   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5212   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5213   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5214   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5215   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5216
5217   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5218   
5219   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5220   
5221   (define-prefix-command 'gnus-summary-goto-map)
5222   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5223   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5224   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5225   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5226   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5227   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5228   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5229   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5230   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5231   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5232   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5233   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5234   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5235   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5236
5237
5238   (define-prefix-command 'gnus-summary-thread-map)
5239   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5240   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5241   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5242   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5243   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5244   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5245   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5246   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5247   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5248   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5249   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5250   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5251   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5252   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5253
5254   
5255   (define-prefix-command 'gnus-summary-exit-map)
5256   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5257   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5258   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5259   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5260   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5261   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5262   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5263   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5264   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5265   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5266   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5267
5268
5269   (define-prefix-command 'gnus-summary-article-map)
5270   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5271   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5272   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5273   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5274   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5275   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5276   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5277   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5278   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5279   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5280   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5281   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5282   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5283   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5284   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5285   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5286   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5287   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5288
5289
5290   (define-prefix-command 'gnus-summary-wash-map)
5291   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5292   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5293   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5294   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5295   (define-key gnus-summary-wash-map "C" 'gnus-article-hide-citation-maybe)
5296   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5297   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5298   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5299   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5300   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5301   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5302   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5303   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5304
5305   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5306   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5307   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5308   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5309   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5310   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5311
5312
5313   (define-prefix-command 'gnus-summary-help-map)
5314   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5315   (define-key gnus-summary-help-map "v" 'gnus-version)
5316   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5317   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5318   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5319   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5320
5321
5322   (define-prefix-command 'gnus-summary-backend-map)
5323   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5324   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5325   (define-key gnus-summary-backend-map "\M-\C-e" 
5326     'gnus-summary-expire-articles-now)
5327   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5328   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5329   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5330   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5331   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5332   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5333   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5334
5335
5336   (define-prefix-command 'gnus-summary-save-map)
5337   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5338   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5339   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5340   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5341   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5342   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5343   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5344   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5345 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5346
5347   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5348   
5349   (define-prefix-command 'gnus-summary-various-map)
5350   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5351   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5352   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5353   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5354   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5355   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5356   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5357   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5358   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5359   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5360   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5361
5362   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5363
5364   (define-prefix-command 'gnus-summary-sort-map)
5365   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5366   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5367   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5368   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5369   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5370   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5371
5372   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5373   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5374   )
5375
5376
5377 \f
5378
5379 (defun gnus-summary-mode (&optional group)
5380   "Major mode for reading articles.
5381
5382 All normal editing commands are switched off.
5383 \\<gnus-summary-mode-map>
5384 Each line in this buffer represents one article.  To read an
5385 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5386 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5387 respectively.
5388
5389 You can also post articles and send mail from this buffer.  To 
5390 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5391 of an article, type `\\[gnus-summary-reply]'.
5392
5393 There are approx. one gazillion commands you can execute in this 
5394 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5395
5396 The following commands are available:
5397
5398 \\{gnus-summary-mode-map}"
5399   (interactive)
5400   (if gnus-visual (gnus-summary-make-menu-bar))
5401   (kill-all-local-variables)
5402   (let ((locals gnus-summary-local-variables))
5403     (while locals
5404       (if (consp (car locals))
5405           (progn
5406             (make-local-variable (car (car locals)))
5407             (set (car (car locals)) (eval (cdr (car locals)))))
5408         (make-local-variable (car locals))
5409         (set (car locals) nil))
5410       (setq locals (cdr locals))))
5411   (gnus-make-thread-indent-array)
5412   (gnus-update-format-specifications)
5413   (setq mode-line-modified "-- ")
5414   (make-local-variable 'mode-line-format)
5415   (setq mode-line-format (copy-sequence mode-line-format))
5416   (and (equal (nth 3 mode-line-format) "   ")
5417        (setcar (nthcdr 3 mode-line-format) ""))
5418   (setq major-mode 'gnus-summary-mode)
5419   (setq mode-name "Summary")
5420   (make-local-variable 'minor-mode-alist)
5421   (use-local-map gnus-summary-mode-map)
5422   (buffer-disable-undo (current-buffer))
5423   (setq buffer-read-only t)             ;Disable modification
5424   (setq truncate-lines t)
5425   (setq selective-display t)
5426   (setq selective-display-ellipses t)   ;Display `...'
5427   (setq buffer-display-table gnus-summary-display-table)
5428   (setq gnus-newsgroup-name group)
5429   (run-hooks 'gnus-summary-mode-hook))
5430
5431 (defun gnus-summary-make-display-table ()
5432   ;; Change the display table.  Odd characters have a tendency to mess
5433   ;; up nicely formatted displays - we make all possible glyphs
5434   ;; display only a single character.
5435
5436   ;; We start from the standard display table, if any.
5437   (setq gnus-summary-display-table 
5438         (or (copy-sequence standard-display-table)
5439             (make-display-table)))
5440   ;; Nix out all the control chars...
5441   (let ((i 32))
5442     (while (>= (setq i (1- i)) 0)
5443       (aset gnus-summary-display-table i [??])))
5444   ;; ... but not newline and cr, of course. (cr is necessary for the
5445   ;; selective display).  
5446   (aset gnus-summary-display-table ?\n nil)
5447   (aset gnus-summary-display-table ?\r nil)
5448   ;; We nix out any glyphs over 126 that are not set already.  
5449   (let ((i 256))
5450     (while (>= (setq i (1- i)) 127)
5451       ;; Only modify if the entry is nil.
5452       (or (aref gnus-summary-display-table i) 
5453           (aset gnus-summary-display-table i [??])))))
5454
5455 (defun gnus-summary-clear-local-variables ()
5456   (let ((locals gnus-summary-local-variables))
5457     (while locals
5458       (if (consp (car locals))
5459           (and (vectorp (car (car locals)))
5460                (set (car (car locals)) nil))
5461         (and (vectorp (car locals))
5462              (set (car locals) nil)))
5463       (setq locals (cdr locals)))))
5464
5465 (defun gnus-mouse-pick-article (e)
5466   (interactive "e")
5467   (mouse-set-point e)
5468   (gnus-summary-next-page nil t))
5469
5470 (defun gnus-summary-setup-buffer (group)
5471   "Initialize summary buffer."
5472   (let ((buffer (concat "*Summary " group "*")))
5473     (if (get-buffer buffer)
5474         (progn
5475           (set-buffer buffer)
5476           (not gnus-newsgroup-begin))
5477       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5478       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5479       (gnus-add-current-to-buffer-list)
5480       (gnus-summary-mode group)
5481       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5482       (setq gnus-newsgroup-name group)
5483       t)))
5484
5485 (defun gnus-set-global-variables ()
5486   ;; Set the global equivalents of the summary buffer-local variables
5487   ;; to the latest values they had. These reflect the summary buffer
5488   ;; that was in action when the last article was fetched.
5489   (if (eq major-mode 'gnus-summary-mode) 
5490       (progn
5491         (setq gnus-summary-buffer (current-buffer))
5492         (let ((name gnus-newsgroup-name)
5493               (marked gnus-newsgroup-marked)
5494               (unread gnus-newsgroup-unreads)
5495               (headers gnus-current-headers)
5496               (score-file gnus-current-score-file))
5497           (save-excursion
5498             (set-buffer gnus-group-buffer)
5499             (setq gnus-newsgroup-name name)
5500             (setq gnus-newsgroup-marked marked)
5501             (setq gnus-newsgroup-unreads unread)
5502             (setq gnus-current-headers headers)
5503             (setq gnus-current-score-file score-file))))))
5504
5505 (defun gnus-summary-insert-dummy-line (sformat subject number)
5506   (if (not sformat) 
5507       (setq sformat gnus-summary-dummy-line-format-spec))
5508   (let (b)
5509     (beginning-of-line)
5510     (setq b (point))
5511     (insert (eval sformat))
5512     (add-text-properties
5513      b (1+ b)
5514      (list 'gnus-number number 
5515            'gnus-mark gnus-dummy-mark
5516            'gnus-level 0))))
5517
5518 (defvar gnus-thread-indent-array nil)
5519 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5520 (defun gnus-make-thread-indent-array ()
5521   (let ((n 200))
5522     (if (and gnus-thread-indent-array
5523              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5524         nil
5525       (setq gnus-thread-indent-array (make-vector 201 "")
5526             gnus-thread-indent-array-level gnus-thread-indent-level)
5527       (while (>= n 0)
5528         (aset gnus-thread-indent-array n
5529               (make-string (* n gnus-thread-indent-level) ? ))
5530         (setq n (1- n))))))
5531
5532 (defun gnus-summary-insert-line 
5533   (sformat header level current unread replied expirable subject-or-nil
5534            &optional dummy score)
5535   (or sformat (setq sformat gnus-summary-line-format-spec))
5536   (let* ((indentation (aref gnus-thread-indent-array level))
5537          (lines (header-lines header))
5538          (score (or score gnus-summary-default-score 0))
5539          (score-char
5540           (if (or (null gnus-summary-default-score)
5541                   (<= (abs (- score gnus-summary-default-score))
5542                       gnus-summary-zcore-fuzz)) ? 
5543             (if (< score gnus-summary-default-score)
5544                 gnus-score-below-mark gnus-score-over-mark)))
5545          (replied (if replied gnus-replied-mark ? ))
5546          (from (header-from header))
5547          (name-address (funcall gnus-extract-address-components from))
5548          (address (car (cdr name-address)))
5549          (name (or (car name-address) (car (cdr name-address))))
5550          (subject (header-subject header))
5551          (number (header-number header))
5552          (opening-bracket (if dummy ?\< ?\[))
5553          (closing-bracket (if dummy ?\> ?\]))
5554          (buffer-read-only nil)
5555          (b (progn (beginning-of-line) (point))))
5556     (or (numberp lines) (setq lines 0))
5557     (insert (eval sformat))
5558     (add-text-properties
5559      b (1+ b) (list 'gnus-number number 
5560                     'gnus-mark (or unread gnus-unread-mark)
5561                     'gnus-level level))))
5562
5563 (defun gnus-summary-update-line (&optional dont-update)
5564   ;; Update summary line after change.
5565   (or (not gnus-summary-default-score)
5566       gnus-summary-inhibit-highlight
5567       (let ((gnus-summary-inhibit-highlight t)
5568             (article (gnus-summary-article-number)))
5569         (progn
5570           (or dont-update
5571               (if (and gnus-summary-mark-below
5572                        (< (gnus-summary-article-score)
5573                           gnus-summary-mark-below))
5574                   (and (not (memq article gnus-newsgroup-marked))
5575                        (not (memq article gnus-newsgroup-dormant))
5576                        (memq article gnus-newsgroup-unreads)
5577                        (gnus-summary-mark-article nil gnus-low-score-mark))
5578                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5579                      (gnus-summary-mark-article nil gnus-unread-mark))))
5580           (and gnus-visual
5581                (run-hooks 'gnus-summary-update-hook))))))
5582
5583 (defun gnus-summary-update-lines (&optional beg end)
5584   ;; Mark article as read (or not) by taking into account scores.
5585   (let ((beg (or beg (point-min)))
5586         (end (or end (point-max))))
5587     (if (or (not gnus-summary-default-score)
5588             gnus-summary-inhibit-highlight)
5589         ()
5590       (let ((gnus-summary-inhibit-highlight t)
5591             article)
5592         (save-excursion
5593           (set-buffer gnus-summary-buffer)
5594           (goto-char beg)
5595           (beginning-of-line)
5596           (while (and (not (eobp)) (< (point) end))
5597             (if (and gnus-summary-mark-below
5598                      (< (or (cdr (assq (get-text-property (point) 'gnus-number)
5599                                        gnus-newsgroup-scored))
5600                             gnus-summary-default-score 0)
5601                         gnus-summary-mark-below))
5602                 ;; We want to possibly mark it as read...
5603                 (and (not (memq article gnus-newsgroup-marked))
5604                      (not (memq article gnus-newsgroup-dormant))
5605                      (memq article gnus-newsgroup-unreads)
5606                      (gnus-summary-mark-article nil gnus-low-score-mark))
5607               ;; We want to possibly mark it as unread.
5608               (and (eq (get-text-property (point) 'gnus-mark) gnus-low-score-mark)
5609                    (gnus-summary-mark-article nil gnus-unread-mark)))
5610             ;; Do the visual highlights at the same time.
5611             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5612             (forward-line 1)))))))
5613
5614 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5615   ;; Sum up all elements (and sub-elements) in a list.
5616   (let ((number 
5617          (if (listp thread) 
5618              (apply 
5619               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5620            1)))
5621     (if char 
5622         (if (> number 1) gnus-not-empty-thread-mark
5623           gnus-empty-thread-mark)
5624       number)))
5625
5626 (defun gnus-summary-read-group 
5627   (group &optional show-all no-article kill-buffer)
5628   "Start reading news in newsgroup GROUP.
5629 If SHOW-ALL is non-nil, already read articles are also listed.
5630 If NO-ARTICLE is non-nil, no article is selected initially."
5631   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5632   (let* ((new-group (gnus-summary-setup-buffer group))
5633          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5634                                                   group))))
5635          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5636     (cond 
5637      ((not new-group)
5638       (gnus-set-global-variables)
5639       (gnus-kill-buffer kill-buffer)
5640       (gnus-configure-windows 'summary)
5641       (gnus-set-mode-line 'summary)
5642       (gnus-summary-position-cursor)
5643       (message "")
5644       t)
5645      ((null did-select) 
5646       (and (eq major-mode 'gnus-summary-mode)
5647            (not (equal (current-buffer) kill-buffer))
5648            (progn
5649              (kill-buffer (current-buffer))
5650              (if (not quit-config)
5651                  (progn
5652                    (set-buffer gnus-group-buffer)
5653                    (gnus-group-jump-to-group group)
5654                    (gnus-group-next-unread-group 1))
5655                (if (not (buffer-name (car quit-config)))
5656                    (gnus-configure-windows 'group)
5657                  (set-buffer (car quit-config))
5658                  (and (eq major-mode 'gnus-summary-mode)
5659                       (gnus-set-global-variables))
5660                  (gnus-configure-windows (cdr quit-config))))))
5661       (message "Can't select group")
5662       nil)
5663      ((eq did-select 'quit)
5664       (and (eq major-mode 'gnus-summary-mode)
5665            (not (equal (current-buffer) kill-buffer))
5666            (kill-buffer (current-buffer)))
5667       (gnus-kill-buffer kill-buffer)
5668       (if (not quit-config)
5669           (progn
5670             (set-buffer gnus-group-buffer)
5671             (gnus-group-jump-to-group group)
5672             (gnus-group-next-unread-group 1)
5673             (gnus-configure-windows 'group))
5674         (if (not (buffer-name (car quit-config)))
5675             (gnus-configure-windows 'group)
5676           (set-buffer (car quit-config))
5677           (and (eq major-mode 'gnus-summary-mode)
5678                (gnus-set-global-variables))
5679           (gnus-configure-windows (cdr quit-config))))
5680       (signal 'quit nil))
5681      (t
5682       (gnus-set-global-variables)
5683       ;; Save the active value in effect when the group was entered.
5684       (setq gnus-newsgroup-active 
5685             (gnus-copy-sequence
5686              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5687       ;; You can change the subjects in this hook.
5688       (run-hooks 'gnus-select-group-hook)
5689       ;; Do score processing.
5690       (and gnus-use-scoring (gnus-possibly-score-headers))
5691       ;; Update the format specifiers.
5692       (gnus-update-format-specifications)
5693       ;; Generate the summary buffer.
5694       (gnus-summary-prepare)
5695       (if (zerop (buffer-size))
5696           (cond (gnus-newsgroup-dormant
5697                  (gnus-summary-show-all-dormant))
5698                 ((and gnus-newsgroup-scored show-all)
5699                  (gnus-summary-show-all-expunged))))
5700       ;; Function `gnus-apply-kill-file' must be called in this hook.
5701       (run-hooks 'gnus-apply-kill-hook)
5702       (if (zerop (buffer-size))
5703           (progn
5704             ;; This newsgroup is empty.
5705             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5706             (gnus-message 6 "No unread news")
5707             (gnus-kill-buffer kill-buffer)
5708             nil)
5709         ;;(save-excursion
5710         ;;  (if kill-buffer
5711         ;;      (let ((gnus-summary-buffer kill-buffer))
5712         ;;      (gnus-configure-windows 'group))))
5713         ;; Hide conversation thread subtrees.  We cannot do this in
5714         ;; gnus-summary-prepare-hook since kill processing may not
5715         ;; work with hidden articles.
5716         (and gnus-show-threads
5717              gnus-thread-hide-subtree
5718              (gnus-summary-hide-all-threads))
5719         ;; Show first unread article if requested.
5720         (goto-char (point-min))
5721         (if (and (not no-article)
5722                  gnus-auto-select-first
5723                  (gnus-summary-first-unread-article))
5724             ()
5725           (gnus-configure-windows 'summary))
5726         (gnus-set-mode-line 'summary)
5727         (gnus-summary-position-cursor)
5728         ;; If in async mode, we send some info to the backend.
5729         (and gnus-newsgroup-async
5730              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5731              (gnus-request-asynchronous 
5732               gnus-newsgroup-name
5733               (if (and gnus-asynchronous-article-function
5734                        (fboundp gnus-asynchronous-article-function))
5735                   (funcall gnus-asynchronous-article-function
5736                            gnus-newsgroup-threads)
5737                 gnus-newsgroup-threads)))
5738         (gnus-kill-buffer kill-buffer)
5739         (if (not (get-buffer-window gnus-group-buffer))
5740             ()
5741           ;; gotta use windows, because recenter does wierd stuff if
5742           ;; the current buffer ain't the displayed window.
5743           (let ((owin (selected-window))) 
5744             (select-window (get-buffer-window gnus-group-buffer))
5745             (and (gnus-group-goto-group group)
5746                  (recenter))
5747             (select-window owin))))
5748       t))))
5749
5750 (defun gnus-summary-prepare ()
5751   ;; Generate the summary buffer.
5752   (let ((buffer-read-only nil))
5753     (erase-buffer)
5754     (gnus-summary-prepare-threads 
5755      (if gnus-show-threads
5756          (gnus-gather-threads 
5757           (gnus-sort-threads 
5758            (if (and gnus-summary-expunge-below
5759                     (not gnus-fetch-old-headers))
5760                (gnus-make-threads-and-expunge)
5761              (gnus-make-threads))))
5762        gnus-newsgroup-headers)
5763      'cull)
5764     ;; Erase header retrieval message.
5765     (gnus-summary-update-lines)
5766     (message "")
5767     ;; Remove the final newline.
5768     ;;(goto-char (point-max))
5769     ;;(delete-char -1)
5770     ;; Call hooks for modifying summary buffer.
5771     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5772     (goto-char (point-min))
5773     (run-hooks 'gnus-summary-prepare-hook)))
5774
5775 (defun gnus-gather-threads (threads)
5776   "Gather threads that have lost their roots."
5777   (if (not gnus-summary-make-false-root)
5778       threads 
5779     (let ((hashtb (gnus-make-hashtable 1023))
5780           (prev threads)
5781           (result threads)
5782           subject hthread whole-subject)
5783       (while threads
5784         (setq whole-subject 
5785               (setq subject (header-subject (car (car threads)))))
5786         (if gnus-summary-gather-subject-limit
5787             (or (and (numberp gnus-summary-gather-subject-limit)
5788                      (> (length subject) gnus-summary-gather-subject-limit)
5789                      (setq subject
5790                            (substring subject 0 
5791                                       gnus-summary-gather-subject-limit)))
5792                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5793                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5794           (setq subject (gnus-simplify-subject-re subject)))
5795         (if (setq hthread 
5796                   (gnus-gethash subject hashtb))
5797             (progn
5798               (or (stringp (car (car hthread)))
5799                   (setcar hthread (list whole-subject (car hthread))))
5800               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5801                                            (list (car threads))))
5802               (setcdr prev (cdr threads))
5803               (setq threads prev))
5804           (gnus-sethash subject threads hashtb))
5805         (setq prev threads)
5806         (setq threads (cdr threads)))
5807       result)))
5808
5809 (defun gnus-make-threads ()
5810   ;; This function takes the dependencies already made by 
5811   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5812   ;; through the dependecies in the hash table and finds all the
5813   ;; roots. Roots do not refer back to any valid articles.
5814   (let (roots)
5815     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5816          (gnus-build-old-threads))
5817     (mapatoms
5818      (lambda (refs)
5819        (if (not (car (symbol-value refs)))
5820            (setq roots (append (cdr (symbol-value refs)) roots))
5821          ;; Ok, these refer back to valid articles, but if
5822          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5823          ;; the root has the same subject as its children. The children
5824          ;; that do not are made into roots and removed from the list
5825          ;; of children. 
5826          (or gnus-thread-ignore-subject
5827              (let* ((prev (symbol-value refs))
5828                     (subject (gnus-simplify-subject-re 
5829                               (header-subject (car prev))))
5830                     (headers (cdr prev)))
5831                (while headers
5832                  (if (not (string= subject
5833                                    (gnus-simplify-subject-re 
5834                                     (header-subject (car headers)))))
5835                      (progn
5836                        (setq roots (cons (car headers) roots))
5837                        (setcdr prev (cdr headers)))
5838                    (setq prev headers))
5839                  (setq headers (cdr headers)))))))
5840      gnus-newsgroup-dependencies)
5841     
5842     (mapcar 'gnus-trim-thread
5843             (apply 'append
5844                    (mapcar 'gnus-cut-thread
5845                            (mapcar 'gnus-make-sub-thread roots))))))
5846   
5847 (defun gnus-make-threads-and-expunge ()
5848   ;; This function takes the dependencies already made by 
5849   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5850   ;; through the dependecies in the hash table and finds all the
5851   ;; roots. Roots do not refer back to any valid articles.
5852   (let ((default (or gnus-summary-default-score 0))
5853         (below gnus-summary-expunge-below)
5854         roots article)
5855     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5856          (gnus-build-old-threads))
5857     (mapatoms
5858      (lambda (refs)
5859        (if (not (car (symbol-value refs)))
5860            ;; These articles do not refer back to any other articles -
5861            ;; they are roots.
5862            (let ((headers (cdr (symbol-value refs))))
5863              ;; We weed out the low-scored articles.
5864              (while headers
5865                (if (not (< (or (cdr (assq (header-number (car headers))
5866                                           gnus-newsgroup-scored)) default)
5867                            below))
5868                    ;; It is over.
5869                    (setq roots (cons (car headers) roots))
5870                  ;; It is below, so we mark it as read.
5871                  (setq gnus-newsgroup-unreads
5872                        (delq (header-number (car headers))
5873                              gnus-newsgroup-unreads)))
5874                (setq headers (cdr headers))))
5875          ;; Ok, these refer back to valid articles, but if
5876          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5877          ;; the root has the same subject as its children. The children
5878          ;; that do not are made into roots and removed from the list
5879          ;; of children. 
5880          (or gnus-thread-ignore-subject
5881              (let* ((prev (symbol-value refs))
5882                     (subject (gnus-simplify-subject-re 
5883                               (header-subject (car prev))))
5884                     (headers (cdr prev)))
5885                (while headers
5886                  (if (not (string= subject
5887                                    (gnus-simplify-subject-re 
5888                                     (header-subject (car headers)))))
5889                      (progn
5890                        (if (not (< (or (cdr (assq (header-number (car headers))
5891                                                   gnus-newsgroup-scored))
5892                                        default) below))
5893                            (setq roots (cons (car headers) roots))
5894                          (setq gnus-newsgroup-unreads
5895                                (delq (header-number (car headers))
5896                                      gnus-newsgroup-unreads)))
5897                        (setcdr prev (cdr headers)))
5898                    (setq prev headers))
5899                  (setq headers (cdr headers)))))
5900          ;; If this article is expunged, some of the children might be
5901          ;; roots.  
5902          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5903                                gnus-newsgroup-scored)) default)
5904                 below)
5905              (let* ((prev (symbol-value refs))
5906                     (headers (cdr prev)))
5907                (while headers
5908                  (setq article (header-number (car headers)))
5909                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5910                                  default) below))
5911                      (progn (setq roots (cons (car headers) roots))
5912                             (setq prev headers))
5913                    (setq gnus-newsgroup-unreads 
5914                          (delq article gnus-newsgroup-unreads))
5915                    (setcdr prev (cdr headers)))
5916                  (setq headers (cdr headers))))
5917            ;; It was not expunged, but we look at expunged children.
5918            (let* ((prev (symbol-value refs))
5919                   (headers (cdr prev))
5920                   article)
5921              (while headers
5922                (setq article (header-number (car headers)))
5923                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5924                                default) below))
5925                    (setq prev headers)
5926                  (setq gnus-newsgroup-unreads 
5927                        (delq article gnus-newsgroup-unreads))
5928                  (setcdr prev (cdr headers)))
5929                (setq headers (cdr headers)))))))
5930      gnus-newsgroup-dependencies)
5931
5932     (mapcar 'gnus-trim-thread
5933             (apply 'append
5934                    (mapcar 'gnus-cut-thread
5935                            (mapcar 'gnus-make-sub-thread roots))))))
5936   
5937 (defun gnus-cut-thread (thread)
5938   ;; Remove leaf dormant or ancient articles from THREAD.
5939   (let ((head (car thread))
5940         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5941     (if (and (null tail)
5942              (let ((number (header-number head)))
5943                (or (memq number gnus-newsgroup-ancient)
5944                    (memq number gnus-newsgroup-dormant)
5945                    (and gnus-summary-expunge-below
5946                         (eq gnus-fetch-old-headers 'some)
5947                         (< (or (cdr (assq number gnus-newsgroup-scored))
5948                                gnus-summary-default-score 0)
5949                            gnus-summary-expunge-below)
5950                         (progn
5951                           (setq gnus-newsgroup-unreads
5952                                 (delq number gnus-newsgroup-unreads))
5953                           t)))))
5954         nil
5955       (list (cons head tail)))))
5956
5957 (defun gnus-trim-thread (thread)
5958   ;; Remove root ancient articles with only one child from THREAD.
5959   (if (and (eq gnus-fetch-old-headers 'some)
5960            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5961            (= (length thread) 2))
5962       (gnus-trim-thread (nth 1 thread))
5963     thread))
5964
5965 (defun gnus-make-sub-thread (root)
5966   ;; This function makes a sub-tree for a node in the tree.
5967   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5968                                               gnus-newsgroup-dependencies)))))
5969     (cons root (mapcar 'gnus-make-sub-thread children))))
5970
5971 (defun gnus-build-old-threads ()
5972   ;; Look at all the articles that refer back to old articles, and
5973   ;; fetch the headers for the articles that aren't there. This will
5974   ;; build complete threads - if the roots haven't been expired by the
5975   ;; server, that is.
5976   (let (id heads)
5977     (mapatoms
5978      (lambda (refs)
5979        (if (not (car (symbol-value refs)))
5980            (progn
5981              (setq heads (cdr (symbol-value refs)))
5982              (while heads
5983                (if (not (memq (header-number (car heads))
5984                               gnus-newsgroup-dormant))
5985                    (progn
5986                      (setq id (symbol-name refs))
5987                      (while (and (setq id (gnus-build-get-header id))
5988                                  (not (car (gnus-gethash 
5989                                             id gnus-newsgroup-dependencies)))))
5990                      (setq heads nil))
5991                  (setq heads (cdr heads)))))))
5992      gnus-newsgroup-dependencies)))
5993
5994 (defun gnus-build-get-header (id)
5995   ;; Look through the buffer of NOV lines and find the header to
5996   ;; ID. Enter this line into the dependencies hash table, and return
5997   ;; the id of the parent article (if any).
5998   (let ((deps gnus-newsgroup-dependencies)
5999         found header)
6000     (prog1
6001         (save-excursion
6002           (set-buffer nntp-server-buffer)
6003           (goto-char (point-min))
6004           (while (and (not found) (search-forward id nil t))
6005             (beginning-of-line)
6006             (setq found (looking-at 
6007                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6008                                  (regexp-quote id))))
6009             (or found (beginning-of-line 2)))
6010           (if found
6011               (let (ref)
6012                 (beginning-of-line)
6013                 (and
6014                  (setq header (gnus-nov-parse-line 
6015                                (read (current-buffer)) deps))
6016                  (setq ref (header-references header))
6017                  (string-match "\\(<[^>]+>\\) *$" ref)
6018                  (substring ref (match-beginning 1) (match-end 1))))))
6019       (and header
6020            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6021                  gnus-newsgroup-ancient (cons (header-number header)
6022                                               gnus-newsgroup-ancient))))))
6023
6024 ;; Re-build the thread containing ID.
6025 (defun gnus-rebuild-thread (id)
6026   (let ((dep gnus-newsgroup-dependencies)
6027         (buffer-read-only nil)
6028         parent headers refs thread art)
6029     (while (and id (setq headers
6030                          (car (setq art (gnus-gethash (downcase id) dep)))))
6031       (setq parent art)
6032       (setq id (and (setq refs (header-references headers))
6033                     (string-match "\\(<[^>]+>\\) *$" refs)
6034                     (substring refs (match-beginning 1) (match-end 1)))))
6035     (setq thread (gnus-make-sub-thread (car parent)))
6036     (gnus-rebuild-remove-articles thread)
6037     (let ((beg (point)))
6038       (gnus-summary-prepare-threads (list thread))
6039       (gnus-summary-update-lines beg (point)))))
6040
6041 ;; Delete all lines in the summary buffer that correspond to articles
6042 ;; in this thread.
6043 (defun gnus-rebuild-remove-articles (thread)
6044   (and (gnus-summary-goto-subject (header-number (car thread)))
6045        (gnus-delete-line))
6046   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6047
6048 (defun gnus-sort-threads (threads)
6049   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6050   (let ((fun gnus-thread-sort-functions))
6051     (while fun
6052       (setq threads (sort threads (car fun))
6053             fun (cdr fun))))
6054   threads)
6055
6056 (defsubst gnus-thread-header (thread)
6057   ;; Return header of first article in THREAD.
6058   (if (consp thread)
6059       (if (stringp (car thread))
6060           (car (car (cdr thread)))
6061         (car thread))
6062     thread))
6063
6064 (defun gnus-thread-sort-by-number (h1 h2)
6065   "Sort threads by root article number."
6066   (let ((h1 (gnus-thread-header h1))
6067         (h2 (gnus-thread-header h2)))
6068     (< (header-number h1) (header-number h2))))
6069
6070 (defun gnus-thread-sort-by-author (h1 h2)
6071   "Sort threads by root author."
6072   (let ((h1 (gnus-thread-header h1))
6073         (h2 (gnus-thread-header h2)))
6074     (string-lessp
6075      (let ((extract (funcall 
6076                      gnus-extract-address-components (header-from h1))))
6077        (or (car extract) (cdr extract)))
6078      (let ((extract (funcall
6079                      gnus-extract-address-components (header-from h2))))
6080        (or (car extract) (cdr extract))))))
6081
6082 (defun gnus-thread-sort-by-subject (h1 h2)
6083   "Sort threads by root subject."
6084   (let ((h1 (gnus-thread-header h1))
6085         (h2 (gnus-thread-header h2)))
6086     (string-lessp
6087      (downcase (gnus-simplify-subject (header-subject h1)))
6088      (downcase (gnus-simplify-subject (header-subject h2))))))
6089
6090 (defun gnus-thread-sort-by-date (h1 h2)
6091   "Sort threads by root article date."
6092   (let ((h1 (gnus-thread-header h1))
6093         (h2 (gnus-thread-header h2)))
6094     (string-lessp
6095      (gnus-sortable-date (header-date h1))
6096      (gnus-sortable-date (header-date h2)))))
6097
6098 (defun gnus-thread-sort-by-score (h1 h2)
6099   "Sort threads by root article score.
6100 Unscored articles will be counted as having a score of zero."
6101   (let ((h1 (gnus-thread-header h1))
6102         (h2 (gnus-thread-header h2)))
6103     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
6104           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
6105       (> (or (cdr s1) gnus-summary-default-score 0)
6106          (or (cdr s2) gnus-summary-default-score 0)))))
6107
6108 (defun gnus-thread-sort-by-total-score (h1 h2)
6109   "Sort threads by the sum of all scores in the thread.
6110 Unscored articles will be counted as having a score of zero."
6111   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6112
6113 (defun gnus-thread-total-score (thread)
6114   ;;  This function find the total score of THREAD.
6115   (if (consp thread)
6116       (if (stringp (car thread))
6117           (apply gnus-thread-score-function 0
6118                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6119         (gnus-thread-total-score-1 thread))
6120     (gnus-thread-total-score-1 (list thread))))
6121
6122 (defun gnus-thread-total-score-1 (root)
6123   ;; This function find the total score of the thread below ROOT.
6124   (setq root (car root))
6125   (apply gnus-thread-score-function
6126          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6127              gnus-summary-default-score 0)
6128          (mapcar 'gnus-thread-total-score
6129                  (cdr (gnus-gethash (downcase (header-id root))
6130                                     gnus-newsgroup-dependencies)))))
6131
6132 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6133 (defvar gnus-tmp-prev-subject "")
6134 (defvar gnus-tmp-adopt-thread nil)
6135
6136 (defun gnus-summary-prepare-threads (threads &optional cull)
6137   "Prepare summary buffer from THREADS and indentation LEVEL.  
6138 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6139 or a straight list of headers."
6140   (let (thread header number subject stack state gathered level)
6141     (if (vectorp (car threads))
6142         ;; If this is a straight (sic) list of headers, then a
6143         ;; threaded summary display isn't required, so we just create
6144         ;; an unthreaded one.
6145         (gnus-summary-prepare-unthreaded threads)
6146
6147       ;; Do the threaded display.
6148       (setq stack (cons (cons 0 threads) stack))
6149
6150       (while stack
6151         (setq state (car stack)
6152               level (car state)
6153               thread (cdr state)
6154               stack (cdr stack)
6155               header (car (car thread)))
6156
6157         (if (stringp header)
6158             (progn
6159               ;; The header is a dummy root.
6160               (cond 
6161                ((eq gnus-summary-make-false-root 'adopt)
6162                 ;; We let the first article adopt the rest.
6163                 (let ((th (car (cdr (car thread)))))
6164                   (while (cdr th)
6165                     (setq th (cdr th)))
6166                   (setcdr th (cdr (cdr (car thread))))
6167                   (setq gathered 
6168                         (nconc (mapcar
6169                                 (lambda (h) (header-number (car h)))
6170                                 (cdr (cdr (car thread))))
6171                                gathered))
6172                   (setcdr (cdr (car thread)) nil))
6173                 (setq level -1))
6174                ((eq gnus-summary-make-false-root 'empty)
6175                 ;; We print adopted articles with empty subject fields.
6176                 (setq gathered 
6177                       (nconc (mapcar
6178                               (lambda (h) (header-number (car h)))
6179                               (cdr (cdr (car thread))))
6180                              gathered))
6181                 (setq level -1))
6182                ((eq gnus-summary-make-false-root 'dummy)
6183                 ;; We output a dummy root.
6184                 (gnus-summary-insert-dummy-line 
6185                  nil header (header-number (car (car (cdr thread))))))
6186                (t
6187                 ;; We do not make a root for the gathered
6188                 ;; sub-threads at all.  
6189                 (setq level -1))))
6190       
6191           (setq number (header-number header)
6192                 subject (header-subject header))
6193
6194           ;; Do the async thing.
6195           (and gnus-newsgroup-async
6196                (setq gnus-newsgroup-threads
6197                      (cons (cons number (header-lines header)) 
6198                            gnus-newsgroup-threads)))
6199
6200           ;; We may have to root out some bad articles...
6201           (and cull
6202                (= level 0)
6203                (cond ((memq (setq number (header-number header))
6204                             gnus-newsgroup-dormant)
6205                       (setq header nil))
6206                      ((and gnus-summary-expunge-below
6207                            (< (or (cdr (assq number gnus-newsgroup-scored))
6208                                   gnus-summary-default-score 0)
6209                               gnus-summary-expunge-below))
6210                       (setq header nil)
6211                       (setq gnus-newsgroup-unreads 
6212                             (delq number gnus-newsgroup-unreads)))))
6213
6214           (gnus-summary-insert-line
6215            nil header level nil 
6216            (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6217                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6218                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6219                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6220                  (t gnus-ancient-mark))
6221            (memq number gnus-newsgroup-replied)
6222            (memq number gnus-newsgroup-expirable)
6223            (if (and (eq gnus-summary-make-false-root 'empty)
6224                     (memq number gathered))
6225                gnus-summary-same-subject
6226              (if (or (zerop level)
6227                      (and gnus-thread-ignore-subject
6228                           (not (string= 
6229                                 (gnus-simplify-subject-re
6230                                  gnus-tmp-prev-subject)
6231                                 (gnus-simplify-subject-re
6232                                  subject)))))
6233                  subject
6234                gnus-summary-same-subject))
6235            (and (eq gnus-summary-make-false-root 'adopt)
6236                 (memq number gathered))
6237            (cdr (assq number gnus-newsgroup-scored))))
6238
6239         (setq gnus-tmp-prev-subject subject)
6240
6241         (if (nth 1 thread) 
6242             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6243         (if (cdr (car thread))
6244             (setq stack (cons (cons (1+ level) (cdr (car thread))) stack)))))))
6245
6246
6247 (defun gnus-summary-prepare-unthreaded (headers)
6248   (let (header number)
6249
6250     ;; Do the async thing, if that is required.
6251     (if gnus-newsgroup-async
6252         (setq gnus-newsgroup-threads
6253               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6254                       headers)))
6255
6256     (while headers
6257       (setq header (car headers)
6258             headers (cdr headers)
6259             number (header-number header))
6260
6261       (gnus-summary-insert-line
6262        nil header 0 nil 
6263        (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6264              ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6265              ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6266              ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6267              (t gnus-ancient-mark))
6268        (memq number gnus-newsgroup-replied)
6269        (memq number gnus-newsgroup-expirable)
6270        (header-subject header) nil
6271        (cdr (assq number gnus-newsgroup-scored))))))
6272
6273 (defun gnus-select-newsgroup (group &optional read-all)
6274   "Select newsgroup GROUP.
6275 If READ-ALL is non-nil, all articles in the group are selected."
6276   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6277          (info (nth 2 entry))
6278          articles)
6279     (gnus-check-news-server
6280      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6281
6282     (or (gnus-server-opened gnus-current-select-method)
6283         (gnus-open-server gnus-current-select-method)
6284         (error "Couldn't open server"))
6285     
6286     (or (and (eq (car entry) t)
6287              (gnus-activate-newsgroup (car info)))
6288         (gnus-request-group group t)
6289         (progn
6290           (kill-buffer (current-buffer))
6291           (error "Couldn't request group %s: %s" 
6292                  group (gnus-status-message group))))
6293
6294     (setq gnus-newsgroup-name group)
6295     (setq gnus-newsgroup-unselected nil)
6296     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6297
6298     (and gnus-asynchronous
6299          (gnus-check-backend-function 
6300           'request-asynchronous gnus-newsgroup-name)
6301          (setq gnus-newsgroup-async
6302                (gnus-request-asynchronous gnus-newsgroup-name)))
6303
6304     (setq articles (gnus-articles-to-read group read-all))
6305
6306     (cond 
6307      ((null articles) 
6308       (gnus-message 3 "Couldn't select newsgroup")
6309       'quit)
6310      ((eq articles 0) nil)
6311      (t
6312       ;; Init the dependencies hash table.
6313       (setq gnus-newsgroup-dependencies 
6314             (gnus-make-hashtable (length articles)))
6315       ;; Retrieve the headers and read them in.
6316       (setq gnus-newsgroup-headers 
6317             (if (eq 'nov (setq gnus-headers-retrieved-by
6318                                (gnus-retrieve-headers 
6319                                 (if (and gnus-fetch-old-headers 
6320                                          (not (eq 1 (car articles))))
6321                                     (cons 1 articles)
6322                                   articles)
6323                                 gnus-newsgroup-name)))
6324                 (progn
6325                   (gnus-get-newsgroup-headers-xover articles))
6326               ;; If we were to fetch old headers, but the backend didn't
6327               ;; support XOVER, then it is possible we fetched one article
6328               ;; that we shouldn't have. If that's the case, we pop it off the
6329               ;; list of headers.
6330               (if (not gnus-fetch-old-headers)
6331                   ()
6332                 (save-excursion
6333                   (set-buffer nntp-server-buffer)
6334                   (goto-char (point-min))
6335                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6336                        (delete-region 
6337                         (point) 
6338                         (search-forward "\n.\n" nil t)))))
6339               (gnus-get-newsgroup-headers)))
6340       ;; Remove canceled articles from the list of unread articles.
6341       (setq gnus-newsgroup-unreads
6342             (gnus-set-sorted-intersection 
6343              gnus-newsgroup-unreads
6344              (mapcar (lambda (headers) (header-number headers))
6345                      gnus-newsgroup-headers)))
6346       ;; Adjust and set lists of article marks.
6347       (and info
6348            (let (marked)
6349              (gnus-adjust-marked-articles info)
6350              (setq gnus-newsgroup-marked 
6351                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6352              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6353              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6354              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6355              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6356              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6357              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6358              (setq gnus-newsgroup-processable nil)))
6359       ;; Check whether auto-expire is to be done in this group.
6360       (setq gnus-newsgroup-auto-expire
6361             (or (and (stringp gnus-auto-expirable-newsgroups)
6362                      (string-match gnus-auto-expirable-newsgroups group))
6363                 (memq 'auto-expire (nth 5 info))))
6364       ;; First and last article in this newsgroup.
6365       (and gnus-newsgroup-headers
6366            (setq gnus-newsgroup-begin 
6367                  (header-number (car gnus-newsgroup-headers)))
6368            (setq gnus-newsgroup-end
6369                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6370       (setq gnus-reffed-article-number -1)
6371       ;; GROUP is successfully selected.
6372       (or gnus-newsgroup-headers t)))))
6373
6374 (defun gnus-articles-to-read (group read-all)
6375   ;; Find out what articles the user wants to read.
6376   (let* ((articles
6377           ;; Select all articles if `read-all' is non-nil, or if all the
6378           ;; unread articles are dormant articles.
6379           (if (or (and read-all (not (numberp read-all)))
6380                   (= (length gnus-newsgroup-unreads) 
6381                      (length gnus-newsgroup-dormant)))
6382               (gnus-uncompress-range 
6383                (gnus-gethash group gnus-active-hashtb))
6384             gnus-newsgroup-unreads))
6385          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6386          (scored (length scored-list))
6387          (number (length articles))
6388          (marked (+ (length gnus-newsgroup-marked)
6389                     (length gnus-newsgroup-dormant)))
6390          (select
6391           (cond 
6392            ((numberp read-all)
6393             read-all)
6394            (t
6395             (condition-case ()
6396                 (cond ((and (or (<= scored marked)
6397                                 (= scored number))
6398                             (numberp gnus-large-newsgroup)
6399                             (> number gnus-large-newsgroup))
6400                        (let ((input
6401                               (read-string
6402                                (format
6403                                 "How many articles from %s (default %d): "
6404                                 gnus-newsgroup-name number))))
6405                          (if (string-match "^[ \t]*$" input)
6406                              number input)))
6407                       ((and (> scored marked) (< scored number))
6408                        (let ((input
6409                               (read-string
6410                                (format 
6411                                 "%s %s (%d scored, %d total): "
6412                                 "How many articles from"
6413                                 group scored number))))
6414                          (if (string-match "^[ \t]*$" input)
6415                              number input)))
6416                       (t number))
6417               (quit nil))))))
6418     (setq select (if (stringp select) (string-to-number select) select))
6419     (if (or (null select) (zerop select))
6420         select
6421       (if (and (not (zerop scored)) (<= (abs select) scored))
6422           (progn
6423             (setq articles (sort scored-list '<))
6424             (setq number (length articles)))
6425         (setq articles (copy-sequence articles)))
6426
6427       (if (< (abs select) number)
6428           (if (< select 0) 
6429               ;; Select the N oldest articles.
6430               (setcdr (nthcdr (1- (abs select)) articles) nil)
6431             ;; Select the N most recent articles.
6432             (setq articles (nthcdr (- number select) articles))))
6433       (setq gnus-newsgroup-unselected
6434             (gnus-sorted-intersection
6435              gnus-newsgroup-unreads
6436              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6437       articles)))
6438
6439 (defun gnus-killed-articles (killed articles)
6440   (let (out)
6441     (while articles
6442       (if (inline (gnus-member-of-range (car articles) killed))
6443           (setq out (cons (car articles) out)))
6444       (setq articles (cdr articles)))
6445     out))
6446
6447 (defun gnus-adjust-marked-articles (info &optional active)
6448   "Remove all marked articles that are no longer legal."
6449   (let ((marked-lists (nth 3 info))
6450         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6451         m prev)
6452     ;; There are many types of marked articles.
6453     (while marked-lists
6454       (setq m (cdr (setq prev (car marked-lists))))
6455       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6456              ;; Make sure that all ticked articles are a subset of the
6457              ;; unread/unselected articles.
6458              (while m
6459                (if (or (memq (car m) gnus-newsgroup-unreads)
6460                        (memq (car m) gnus-newsgroup-unselected))
6461                    (setq prev m)
6462                  (setcdr prev (cdr m)))
6463                (setq m (cdr m))))
6464             ((eq 'score (car prev))
6465              ;; Scored articles should be a subset of
6466              ;; unread/unselected articles. 
6467              (while m
6468                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6469                        (memq (car (car m)) gnus-newsgroup-unreads))
6470                    (setq prev m)
6471                  (setcdr prev (cdr m)))
6472                (setq m (cdr m))))
6473             ((eq 'bookmark (car prev))
6474              ;; Bookmarks should be a subset of active articles.
6475              (while m
6476                (if (< (car (car m)) (car active))
6477                    (setcdr prev (cdr m))
6478                  (setq prev m))
6479                (setq m (cdr m))))
6480             ((eq 'killed (car prev))
6481              ;; Articles that have been through the kill process are
6482              ;; to be a subset of active articles.
6483              (while (and m (< (or (and (numberp (car m)) (car m))
6484                                   (cdr (car m)))
6485                               (car active)))
6486                (setcdr prev (cdr m))
6487                (setq m (cdr m)))
6488              (if (and m (< (or (and (numberp (car m)) (car m))
6489                                (car (car m)))
6490                            (car active))) 
6491                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6492             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6493              ;; The replied and expirable articles have to be articles
6494              ;; that are active. 
6495              (while m
6496                (if (< (car m) (car active))
6497                    (setcdr prev (cdr m))
6498                  (setq prev m))
6499                (setq m (cdr m)))))
6500       (setq marked-lists (cdr marked-lists)))
6501     ;; Remove all lists that are empty.
6502     (setq marked-lists (nth 3 info))
6503     (if marked-lists
6504         (progn
6505           (while (= 1 (length (car marked-lists)))
6506             (setq marked-lists (cdr marked-lists)))
6507           (setq m (cdr (setq prev marked-lists)))
6508           (while m
6509             (if (= 1 (length (car m)))
6510                 (setcdr prev (cdr m))
6511               (setq prev m))
6512             (setq m (cdr m)))
6513           (setcar (nthcdr 3 info) marked-lists)))
6514     ;; Finally, if there are no marked lists at all left, and if there
6515     ;; are no elements after the lists in the info list, we just chop
6516     ;; the info list off before the marked lists.
6517     (and (null marked-lists) 
6518          (not (nthcdr 4 info))
6519          (setcdr (nthcdr 2 info) nil)))
6520   info)
6521
6522 (defun gnus-set-marked-articles 
6523   (info ticked replied expirable killed dormant bookmark score) 
6524   "Enter the various lists of marked articles into the newsgroup info list."
6525   (let (newmarked)
6526     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6527     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6528     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6529                                          newmarked)))
6530     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6531     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6532     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6533                                         newmarked)))
6534     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6535     (if (nthcdr 3 info)
6536         (progn
6537           (setcar (nthcdr 3 info) newmarked)
6538           (and (not newmarked)
6539                (not (nthcdr 4 info))
6540                (setcdr (nthcdr 2 info) nil)))
6541       (if newmarked
6542           (setcdr (nthcdr 2 info) (list newmarked))))))
6543
6544 (defun gnus-add-marked-articles (group type articles &optional info force)
6545   ;; Add ARTICLES of TYPE to the info of GROUP.
6546   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6547   ;; add, but replace marked articles of TYPE with ARTICLES.
6548   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6549         marked m)
6550     (or (not info)
6551         (and (not (setq marked (nthcdr 3 info)))
6552              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6553         (and (not (setq m (assq type (car marked))))
6554              (setcar marked (cons (cons type articles) (car marked))))
6555         (if force
6556             (setcdr m articles)
6557           (nconc m articles)))))
6558          
6559 (defun gnus-set-mode-line (where)
6560   "This function sets the mode line of the article or summary buffers.
6561 If WHERE is `summary', the summary mode line format will be used."
6562   (if (memq where gnus-updated-mode-lines)
6563       (let (mode-string)
6564         (save-excursion
6565           (set-buffer gnus-summary-buffer)
6566           (let* ((mformat (if (eq where 'article) 
6567                               gnus-article-mode-line-format-spec
6568                             gnus-summary-mode-line-format-spec))
6569                  (group-name gnus-newsgroup-name)
6570                  (article-number (or gnus-current-article 0))
6571                  (unread (- (length gnus-newsgroup-unreads)
6572                             (length gnus-newsgroup-dormant)))
6573                  (unread-and-unticked 
6574                   (- unread (length gnus-newsgroup-marked)))
6575                  (unselected (length gnus-newsgroup-unselected))
6576                  (unread-and-unselected
6577                   (cond ((and (zerop unread-and-unticked)
6578                               (zerop unselected)) "")
6579                         ((zerop unselected) 
6580                          (format "{%d more}" unread-and-unticked))
6581                         (t (format "{%d(+%d) more}"
6582                                    unread-and-unticked unselected))))
6583                  (subject
6584                   (if gnus-current-headers
6585                       (header-subject gnus-current-headers) ""))
6586                  (max-len (and gnus-mode-non-string-length
6587                                (- (frame-width) gnus-mode-non-string-length)))
6588                  header) ;; passed as argument to any user-format-funcs
6589             (setq mode-string (eval mformat))
6590             (or (numberp max-len)
6591                 (setq max-len (length mode-string)))
6592             (if (< max-len 4) (setq max-len 4))
6593             (if (> (length mode-string) max-len)
6594                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6595                 ;;  function `substring' might cut on a middle
6596                 ;;  of multi-octet character.
6597                 (setq mode-string 
6598                       (concat (gnus-truncate-string mode-string (- max-len 3))
6599                               "...")))
6600             (setq mode-string (format (format "%%-%ds" max-len)
6601                                       mode-string))))
6602         (setq mode-line-buffer-identification mode-string)
6603         (set-buffer-modified-p t))))
6604
6605 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6606   "Go through the HEADERS list and add all Xrefs to a hash table.
6607 The resulting hash table is returned, or nil if no Xrefs were found."
6608   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6609          (prefix (if (and 
6610                       (gnus-group-foreign-p from-newsgroup)
6611                       (not (memq 'virtual 
6612                                  (assoc (symbol-name (car from-method))
6613                                         gnus-valid-select-methods))))
6614                      (gnus-group-real-prefix from-newsgroup)))
6615          (xref-hashtb (make-vector 63 0))
6616          start group entry number xrefs header)
6617     (while headers
6618       (setq header (car headers))
6619       (if (and (setq xrefs (header-xref header))
6620                (not (memq (header-number header) unreads)))
6621           (progn
6622             (setq start 0)
6623             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6624               (setq start (match-end 0))
6625               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6626                                                     (match-end 1))))
6627               (setq number 
6628                     (string-to-int (substring xrefs (match-beginning 2) 
6629                                               (match-end 2))))
6630               (if (setq entry (gnus-gethash group xref-hashtb))
6631                   (setcdr entry (cons number (cdr entry)))
6632                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6633       (setq headers (cdr headers)))
6634     (if start xref-hashtb nil)))
6635
6636 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6637   "Look through all the headers and mark the Xrefs as read."
6638   (let ((virtual (memq 'virtual 
6639                        (assoc (symbol-name (car (gnus-find-method-for-group 
6640                                                  from-newsgroup)))
6641                               gnus-valid-select-methods)))
6642         name entry info xref-hashtb idlist method
6643         nth4)
6644     (save-excursion
6645       (set-buffer gnus-group-buffer)
6646       (if (setq xref-hashtb 
6647                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6648           (mapatoms 
6649            (lambda (group)
6650              (if (string= from-newsgroup (setq name (symbol-name group)))
6651                  ()
6652                (setq idlist (symbol-value group))
6653                ;; Dead groups are not updated.
6654                (if (and (prog1 
6655                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6656                                   info (nth 2 entry))
6657                           (if (stringp (setq nth4 (nth 4 info)))
6658                               (setq nth4 (gnus-server-to-method nth4))))
6659                         ;; Only do the xrefs if the group has the same
6660                         ;; select method as the group we have just read.
6661                         (or (gnus-methods-equal-p 
6662                              nth4 (gnus-find-method-for-group from-newsgroup))
6663                             virtual
6664                             (equal nth4 
6665                                    (setq method (gnus-find-method-for-group 
6666                                                  from-newsgroup)))
6667                             (and (equal (car nth4) (car method))
6668                                  (equal (nth 1 nth4) (nth 1 method))))
6669                         gnus-use-cross-reference
6670                         (or (not (eq gnus-use-cross-reference t))
6671                             virtual
6672                             ;; Only do cross-references on subscribed
6673                             ;; groups, if that is what is wanted.  
6674                             (<= (nth 1 info) gnus-level-subscribed)))
6675                    (gnus-group-make-articles-read name idlist expirable))))
6676            xref-hashtb)))))
6677
6678 (defun gnus-group-make-articles-read (group articles expirable)
6679   (let* ((num 0)
6680          (entry (gnus-gethash group gnus-newsrc-hashtb))
6681          (info (nth 2 entry))
6682          (active (gnus-gethash group gnus-active-hashtb))
6683          exps expirable range)
6684     ;; First peel off all illegal article numbers.
6685     (if active
6686         (let ((ids articles)
6687               (ticked (cdr (assq 'tick (nth 3 info))))
6688               (dormant (cdr (assq 'dormant (nth 3 info))))
6689               id)
6690           (setq exps nil)
6691           (while ids
6692             (setq id (car ids))
6693             (if (or (> id (cdr active))
6694                     (< id (car active))
6695                     (memq id ticked)
6696                     (memq id dormant))
6697                 (setq articles (delq id articles)))
6698             (and (memq id expirable)
6699                  (setq exps (cons id exps)))
6700             (setq ids (cdr ids)))))
6701     ;; Update expirable articles.
6702     (gnus-add-marked-articles nil 'expirable exps info)
6703     (and active
6704          (null (nth 2 info))
6705          (> (car active) 1)
6706          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6707     (setcar (nthcdr 2 info)
6708             (setq range
6709                   (gnus-add-to-range 
6710                    (nth 2 info) 
6711                    (setq articles (sort articles '<)))))
6712     ;; Then we have to re-compute how many unread
6713     ;; articles there are in this group.
6714     (if active
6715         (progn
6716           (cond 
6717            ((not range)
6718             (setq num (- (1+ (cdr active)) (car active))))
6719            ((not (listp (cdr range)))
6720             (setq num (- (cdr active) (- (1+ (cdr range)) 
6721                                          (car range)))))
6722            (t
6723             (while range
6724               (if (numberp (car range))
6725                   (setq num (1+ num))
6726                 (setq num (+ num (- (1+ (cdr (car range)))
6727                                     (car (car range))))))
6728               (setq range (cdr range)))
6729             (setq num (- (cdr active) num))))
6730           ;; Update the number of unread articles.
6731           (setcar 
6732            entry 
6733            (max 0 (- num 
6734                      (length (cdr (assq 'tick (nth 3 info))))
6735                      (length 
6736                       (cdr (assq 'dormant (nth 3 info)))))))
6737           ;; Update the group buffer.
6738           (gnus-group-update-group group t)))))
6739
6740 (defun gnus-methods-equal-p (m1 m2)
6741   (let ((m1 (or m1 gnus-select-method))
6742         (m2 (or m2 gnus-select-method)))
6743     (or (equal m1 m2)
6744         (and (eq (car m1) (car m2))
6745              (or (not (memq 'address (assoc (symbol-name (car m1))
6746                                             gnus-valid-select-methods)))
6747                  (equal (nth 1 m1) (nth 1 m2)))))))
6748
6749 (defsubst gnus-header-value ()
6750   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6751
6752 (defvar gnus-newsgroup-none-id 0)
6753
6754 (defun gnus-get-newsgroup-headers ()
6755   (setq gnus-article-internal-prepare-hook nil)
6756   (let ((cur nntp-server-buffer)
6757         (dependencies gnus-newsgroup-dependencies)
6758         headers id dep end ref)
6759     (save-excursion
6760       (set-buffer nntp-server-buffer)
6761       (goto-char (point-min))
6762       ;; Search to the beginning of the next header. Error messages
6763       ;; do not begin with 2 or 3.
6764       (while (re-search-forward "^[23][0-9]+ " nil t)
6765         (let ((header (make-vector 9 nil))
6766               (case-fold-search t)
6767               (p (point))
6768               in-reply-to)
6769           (setq id nil
6770                 ref nil)
6771           (header-set-number header (read cur))
6772           ;; This implementation of this function, with nine
6773           ;; search-forwards instead of the one re-search-forward and
6774           ;; a case (which basically was the old function) is actually
6775           ;; about twice as fast, even though it looks messier. You
6776           ;; can't have everything, I guess. Speed and elegance
6777           ;; doesn't always come hand in hand.
6778           (save-restriction
6779             (narrow-to-region (point) (or (save-excursion 
6780                                             (search-forward "\n.\n" nil t))
6781                                           (point)))
6782             (if (search-forward "\nfrom: " nil t)
6783                 (header-set-from header (gnus-header-value))
6784               (header-set-from header "(nobody)"))
6785             (goto-char p)
6786             (if (search-forward "\nsubject: " nil t)
6787                 (header-set-subject header (gnus-header-value))
6788               (header-set-subject header "(none)"))
6789             (goto-char p)
6790             (and (search-forward "\nxref: " nil t)
6791                  (header-set-xref header (gnus-header-value)))
6792             (goto-char p)
6793             (or (numberp (and (search-forward "\nlines: " nil t)
6794                               (header-set-lines header (read cur))))
6795                 (header-set-lines header 0))
6796             (goto-char p)
6797             (and (search-forward "\ndate: " nil t)
6798                  (header-set-date header (gnus-header-value)))
6799             (goto-char p)
6800             (if (search-forward "\nmessage-id: " nil t)
6801                 (header-set-id header (setq id (gnus-header-value)))
6802               ;; If there was no message-id, we just fake one to make
6803               ;; subsequent routines simpler.
6804               (header-set-id 
6805                header 
6806                (setq id (concat "none+" 
6807                                 (int-to-string 
6808                                  (setq gnus-newsgroup-none-id 
6809                                        (1+ gnus-newsgroup-none-id)))))))
6810             (goto-char p)
6811             (if (search-forward "\nreferences: " nil t)
6812                 (progn
6813                   (header-set-references header (gnus-header-value))
6814                   (setq end (match-end 0))
6815                   (save-excursion
6816                     (setq ref 
6817                           (downcase
6818                            (buffer-substring
6819                             (progn 
6820                               (end-of-line)
6821                               (search-backward ">" end t)
6822                               (1+ (point)))
6823                             (progn
6824                               (search-backward "<" end t)
6825                               (point)))))))
6826               ;; Get the references from the in-reply-to header if there
6827               ;; ware no references and the in-reply-to header looks
6828               ;; promising. 
6829               (if (and (search-forward "\nin-reply-to: " nil t)
6830                        (setq in-reply-to (gnus-header-value))
6831                        (string-match "<[^>]+>" in-reply-to))
6832                   (progn
6833                     (header-set-references 
6834                      header 
6835                      (setq ref (substring in-reply-to (match-beginning 0)
6836                                           (match-end 0))))
6837                     (setq ref (downcase ref)))
6838                 (setq ref "none")))
6839             ;; We do some threading while we read the headers. The
6840             ;; message-id and the last reference are both entered into
6841             ;; the same hash table. Some tippy-toeing around has to be
6842             ;; done in case an article has arrived before the article
6843             ;; which it refers to.
6844             (if (boundp (setq dep (intern (downcase id) dependencies)))
6845                 (if (car (symbol-value dep))
6846                     ;; An article with this Message-ID has already
6847                     ;; been seen, so we ignore this one, except we add
6848                     ;; any additional Xrefs (in case the two articles
6849                     ;; came from different servers.
6850                     (progn
6851                       (header-set-xref 
6852                        (car (symbol-value dep))
6853                        (concat (or (header-xref (car (symbol-value dep))) "")
6854                                (or (header-xref header) "")))
6855                       (setq header nil))
6856                   (setcar (symbol-value dep) header))
6857               (set dep (list header)))
6858             (if header
6859                 (progn
6860                   (if (boundp (setq dep (intern ref dependencies)))
6861                       (setcdr (symbol-value dep) 
6862                               (cons header (cdr (symbol-value dep))))
6863                     (set dep (list nil header)))
6864                   (setq headers (cons header headers))))
6865             (goto-char (point-max))))))
6866     (nreverse headers)))
6867
6868 ;; The following macros and functions were written by Felix Lee
6869 ;; <flee@cse.psu.edu>. 
6870
6871 (defmacro gnus-nov-read-integer ()
6872   '(prog1
6873        (if (= (following-char) ?\t)
6874            0
6875          (let ((num (condition-case nil (read buffer) (error nil))))
6876            (if (numberp num) num 0)))
6877      (or (eobp) (forward-char 1))))
6878
6879 (defmacro gnus-nov-skip-field ()
6880   '(search-forward "\t" eol 'move))
6881
6882 (defmacro gnus-nov-field ()
6883   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6884
6885 ;; Goes through the xover lines and returns a list of vectors
6886 (defun gnus-get-newsgroup-headers-xover (sequence)
6887   "Parse the news overview data in the server buffer, and return a
6888 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6889   ;; Get the Xref when the users reads the articles since most/some
6890   ;; NNTP servers do not include Xrefs when using XOVER.
6891   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6892   (let ((cur nntp-server-buffer)
6893         (dependencies gnus-newsgroup-dependencies)
6894         number headers header)
6895     (save-excursion
6896       (set-buffer nntp-server-buffer)
6897       (goto-char (point-min))
6898       (while (and sequence (not (eobp)))
6899         (setq number (read cur))
6900         (while (and sequence (< (car sequence) number))
6901           (setq sequence (cdr sequence)))
6902         (and sequence 
6903              (eq number (car sequence))
6904              (progn
6905                (setq sequence (cdr sequence))
6906                (if (setq header 
6907                          (inline (gnus-nov-parse-line number dependencies)))
6908                    (setq headers (cons header headers)))))
6909         (forward-line 1))
6910       (setq headers (nreverse headers)))
6911     headers))
6912
6913 ;; This function has to be called with point after the article number
6914 ;; on the beginning of the line.
6915 (defun gnus-nov-parse-line (number dependencies)
6916   (let ((none 0)
6917         (eol (gnus-point-at-eol)) 
6918         (buffer (current-buffer))
6919         header ref id dep)
6920
6921     ;; overview: [num subject from date id refs chars lines misc]
6922     (narrow-to-region (point) eol)
6923     (forward-char)
6924
6925     (condition-case nil
6926         (setq header
6927               (vector 
6928                number                   ; number
6929                (gnus-nov-field)         ; subject
6930                (gnus-nov-field)         ; from
6931                (gnus-nov-field)         ; date
6932                (setq id (or (gnus-nov-field)
6933                             (concat "none+"
6934                                     (int-to-string 
6935                                      (setq none (1+ none)))))) ; id
6936                (progn
6937                  (save-excursion
6938                    (let ((beg (point)))
6939                      (search-forward "\t" eol)
6940                      (if (search-backward ">" beg t)
6941                          (setq ref 
6942                                (downcase 
6943                                 (buffer-substring 
6944                                  (1+ (point))
6945                                  (progn
6946                                    (search-backward "<" beg t)
6947                                    (point)))))
6948                        (setq ref nil))))
6949                  (gnus-nov-field))      ; refs
6950                (gnus-nov-read-integer)  ; chars
6951                (gnus-nov-read-integer)  ; lines
6952                (if (= (following-char) ?\n)
6953                    nil
6954                  (gnus-nov-field))      ; misc
6955                ))
6956       (error (progn 
6957                (ding)
6958                (message "Strange nov line.")
6959                (setq header nil)
6960                (goto-char eol))))
6961
6962     (widen)
6963
6964     ;; We build the thread tree.
6965     (and header
6966          (if (boundp (setq dep (intern (downcase id) dependencies)))
6967              (if (car (symbol-value dep))
6968                  ;; An article with this Message-ID has already been seen,
6969                  ;; so we ignore this one, except we add any additional
6970                  ;; Xrefs (in case the two articles came from different
6971                  ;; servers.
6972                  (progn
6973                    (header-set-xref 
6974                     (car (symbol-value dep))
6975                     (concat (or (header-xref (car (symbol-value dep))) "")
6976                             (or (header-xref header) "")))
6977                    (setq header nil))
6978                (setcar (symbol-value dep) header))
6979            (set dep (list header))))
6980     (if header
6981         (progn
6982           (if (boundp (setq dep (intern (or ref "none") 
6983                                         dependencies)))
6984               (setcdr (symbol-value dep) 
6985                       (cons header (cdr (symbol-value dep))))
6986             (set dep (list nil header)))))
6987     header))
6988
6989 (defun gnus-article-get-xrefs ()
6990   "Fill in the Xref value in `gnus-current-headers', if necessary.
6991 This is meant to be called in `gnus-article-internal-prepare-hook'."
6992   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6993                                  gnus-current-headers)))
6994     (or (not gnus-use-cross-reference)
6995         (not headers)
6996         (and (header-xref headers)
6997              (not (string= (header-xref headers) "")))
6998         (let ((case-fold-search t)
6999               xref)
7000           (save-restriction
7001             (gnus-narrow-to-headers)
7002             (goto-char (point-min))
7003             (if (or (and (eq (downcase (following-char)) ?x)
7004                          (looking-at "Xref:"))
7005                     (search-forward "\nXref:" nil t))
7006                 (progn
7007                   (goto-char (1+ (match-end 0)))
7008                   (setq xref (buffer-substring (point) 
7009                                                (progn (end-of-line) (point))))
7010                   (header-set-xref headers xref))))))))
7011
7012 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7013 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7014
7015 ;; Return a header specified by a NUMBER.
7016 (defun gnus-get-header-by-number (number)
7017   (save-excursion
7018     (set-buffer gnus-summary-buffer)
7019     (or gnus-newsgroup-headers-hashtb-by-number
7020         (gnus-make-headers-hashtable-by-number))
7021     (gnus-gethash (int-to-string number)
7022                   gnus-newsgroup-headers-hashtb-by-number)))
7023
7024 (defun gnus-make-headers-hashtable-by-number ()
7025   "Make hashtable for the variable gnus-newsgroup-headers by number."
7026   (save-excursion
7027     (set-buffer gnus-summary-buffer)
7028     (let ((headers gnus-newsgroup-headers)
7029           header)
7030       (setq gnus-newsgroup-headers-hashtb-by-number
7031             (gnus-make-hashtable (length headers)))
7032       (while headers
7033         (setq header (car headers))
7034         (gnus-sethash (int-to-string (header-number header))
7035                       header gnus-newsgroup-headers-hashtb-by-number)
7036         (setq headers (cdr headers))))))
7037
7038 (defun gnus-more-header-backward ()
7039   "Find new header backward."
7040   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7041         (artnum gnus-newsgroup-begin)
7042         (header nil))
7043     (while (and (not header)
7044                 (> artnum first))
7045       (setq artnum (1- artnum))
7046       (setq header (gnus-read-header artnum)))
7047     header))
7048
7049 (defun gnus-more-header-forward (&optional backward)
7050   "Find new header forward.
7051 If BACKWARD, find new header backward instead."
7052   (if backward
7053       (gnus-more-header-backward)
7054     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7055           (artnum gnus-newsgroup-end)
7056           (header nil))
7057       (while (and (not header)
7058                   (< artnum last))
7059         (setq artnum (1+ artnum))
7060         (setq header (gnus-read-header artnum)))
7061       header)))
7062
7063 (defun gnus-extend-newsgroup (header &optional backward)
7064   "Extend newsgroup selection with HEADER.
7065 Optional argument BACKWARD means extend toward backward."
7066   (if header
7067       (let ((artnum (header-number header)))
7068         (setq gnus-newsgroup-headers
7069               (if backward
7070                   (cons header gnus-newsgroup-headers)
7071                 (nconc gnus-newsgroup-headers (list header))))
7072         (setq gnus-newsgroup-unselected
7073               (delq artnum gnus-newsgroup-unselected))
7074         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7075         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7076
7077 (defun gnus-summary-work-articles (n)
7078   "Return a list of articles to be worked upon. The prefix argument,
7079 the list of process marked articles, and the current article will be
7080 taken into consideration."
7081   (let (articles)
7082     (if (and n (numberp n))
7083         (let ((backward (< n 0))
7084               (n (abs n)))
7085           (save-excursion
7086             (while (and (> n 0)
7087                         (setq articles (cons (gnus-summary-article-number) 
7088                                              articles))
7089                         (gnus-summary-search-forward nil nil backward))
7090               (setq n (1- n))))
7091           (sort articles (function <)))
7092       (or (reverse gnus-newsgroup-processable)
7093           (list (gnus-summary-article-number))))))
7094
7095 (defun gnus-summary-search-group (&optional backward use-level)
7096   "Search for next unread newsgroup.
7097 If optional argument BACKWARD is non-nil, search backward instead."
7098   (save-excursion
7099     (set-buffer gnus-group-buffer)
7100     (if (gnus-group-search-forward 
7101          backward nil (if use-level (gnus-group-group-level) nil))
7102         (gnus-group-group-name))))
7103
7104 (defun gnus-summary-best-group (&optional exclude-group)
7105   "Find the name of the best unread group.
7106 If EXCLUDE-GROUP, do not go to this group."
7107   (save-excursion
7108     (set-buffer gnus-group-buffer)
7109     (save-excursion
7110       (gnus-group-best-unread-group exclude-group))))
7111
7112 (defun gnus-summary-search-subject (&optional backward unread subject)
7113   "Search for article forward.
7114 If BACKWARD is non-nil, search backward.
7115 If UNREAD is non-nil, only unread articles are selected.
7116 If SUBJECT is non-nil, the article which has the same subject will be
7117 searched for." 
7118   (let ((func (if backward 'previous-single-property-change
7119                 'next-single-property-change))
7120         (beg (point))
7121         (did t)
7122         pos psubject)
7123     (beginning-of-line)
7124     (and gnus-summary-check-current unread
7125          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7126          (setq did nil))
7127     (if (not did)
7128         ()
7129       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7130       (while
7131           (and 
7132            (setq pos (funcall func (point) 'gnus-number))
7133            (goto-char (if backward (1- pos) pos))
7134            (setq did
7135                  (not (and
7136                        (or (not unread)
7137                            (eq (get-text-property (point) 'gnus-mark)
7138                                gnus-unread-mark))
7139                        (or (not subject)
7140                            (and (setq psubject (gnus-summary-subject-string))
7141                                 (gnus-subject-equal subject psubject))))))
7142            (if backward (if (bobp) nil (forward-char -1) t)
7143              (if (eobp) nil (forward-char 1) t)))))
7144     (if did
7145         (progn (goto-char beg) nil)
7146       (prog1
7147           (get-text-property (point) 'gnus-number)
7148         (gnus-summary-position-cursor)))))
7149
7150 (defun gnus-subject-equal (s1 s2)
7151   (cond
7152    ((null gnus-summary-gather-subject-limit)
7153     (equal (gnus-simplify-subject-re s1)
7154            (gnus-simplify-subject-re s2)))
7155    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7156     (equal (gnus-simplify-subject-fuzzy s1)
7157            (gnus-simplify-subject-fuzzy s2)))
7158    ((numberp gnus-summary-gather-subject-limit)
7159     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7160            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7161    (t
7162     (equal s1 s2))))
7163     
7164 (defun gnus-summary-search-forward (&optional unread subject backward)
7165   "Search for article forward.
7166 If UNREAD is non-nil, only unread articles are selected.
7167 If SUBJECT is non-nil, the article which has the same subject will be
7168 searched for. 
7169 If BACKWARD is non-nil, the search will be performed backwards instead."
7170   (gnus-summary-search-subject backward unread subject))
7171
7172 (defun gnus-summary-search-backward (&optional unread subject)
7173   "Search for article backward.
7174 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7175 If 2nd optional argument SUBJECT is non-nil, the article which has
7176 the same subject will be searched for."
7177   (gnus-summary-search-forward unread subject t))
7178
7179 (defun gnus-summary-article-number (&optional number-or-nil)
7180   "The article number of the article on the current line.
7181 If there isn's an article number here, then we return the current
7182 article number."
7183   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7184     (if number-or-nil number (or number gnus-current-article))))
7185
7186 (defun gnus-summary-thread-level ()
7187   "The thread level of the article on the current line."
7188   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7189       0))
7190
7191 (defun gnus-summary-pseudo-article ()
7192   "The thread level of the article on the current line."
7193   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7194
7195 (defun gnus-summary-article-mark ()
7196   "The mark on the current line."
7197   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7198
7199 (defun gnus-summary-subject-string ()
7200   "Return current subject string or nil if nothing."
7201   (let ((article (gnus-summary-article-number))
7202         header)
7203     (and article 
7204          (setq header (gnus-get-header-by-number article))
7205          (vectorp header)
7206          (header-subject header))))
7207
7208 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7209 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7210 (defun gnus-summary-article-score ()
7211   "Return current article score."
7212   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7213       gnus-summary-default-score 0))
7214
7215 (defun gnus-summary-recenter ()
7216   "Center point in the summary window.
7217 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7218 displayed, no centering will be performed." 
7219   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7220   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7221   (let* ((top (cond ((< (window-height) 4) 0)
7222                     ((< (window-height) 7) 1)
7223                     (t 2)))
7224          (height (1- (window-height)))
7225          (bottom (save-excursion (goto-char (point-max))
7226                                  (forward-line (- height))
7227                                  (point)))
7228          (window (get-buffer-window (current-buffer))))
7229     (and 
7230      ;; The user has to want it,
7231      gnus-auto-center-summary 
7232      ;; the article buffer must be displayed,
7233      (get-buffer-window gnus-article-buffer)
7234      ;; Set the window start to either `bottom', which is the biggest
7235      ;; possible valid number, or the second line from the top,
7236      ;; whichever is the least.
7237      (set-window-start
7238       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7239
7240 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7241 (defun gnus-short-group-name (group &optional levels)
7242   "Collapse GROUP name LEVELS."
7243   (let* ((name "") (foreign "") (depth -1) (skip 1)
7244          (levels (or levels
7245                      (progn
7246                        (while (string-match "\\." group skip)
7247                          (setq skip (match-end 0)
7248                                depth (+ depth 1)))
7249                        depth))))
7250     (if (string-match ":" group)
7251         (setq foreign (substring group 0 (match-end 0))
7252               group (substring group (match-end 0))))
7253     (while group
7254       (if (and (string-match "\\." group) (> levels 0))
7255           (setq name (concat name (substring group 0 1))
7256                 group (substring group (match-end 0))
7257                 levels (- levels 1)
7258                 name (concat name "."))
7259         (setq name (concat foreign name group)
7260               group nil)))
7261     name))
7262
7263 (defun gnus-summary-jump-to-group (newsgroup)
7264   "Move point to NEWSGROUP in group mode buffer."
7265   ;; Keep update point of group mode buffer if visible.
7266   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7267       (save-window-excursion
7268         ;; Take care of tree window mode.
7269         (if (get-buffer-window gnus-group-buffer)
7270             (pop-to-buffer gnus-group-buffer))
7271         (gnus-group-jump-to-group newsgroup))
7272     (save-excursion
7273       ;; Take care of tree window mode.
7274       (if (get-buffer-window gnus-group-buffer)
7275           (pop-to-buffer gnus-group-buffer)
7276         (set-buffer gnus-group-buffer))
7277       (gnus-group-jump-to-group newsgroup))))
7278
7279 ;; This function returns a list of article numbers based on the
7280 ;; difference between the ranges of read articles in this group and
7281 ;; the range of active articles.
7282 (defun gnus-list-of-unread-articles (group)
7283   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7284          (active (gnus-gethash group gnus-active-hashtb))
7285          (last (cdr active))
7286          first nlast unread)
7287     ;; If none are read, then all are unread. 
7288     (if (not read)
7289         (setq first (car active))
7290       ;; If the range of read articles is a single range, then the
7291       ;; first unread article is the article after the last read
7292       ;; article. Sounds logical, doesn't it?
7293       (if (not (listp (cdr read)))
7294           (setq first (1+ (cdr read)))
7295         ;; `read' is a list of ranges.
7296         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7297                                 (car (car read)))) 1)
7298             (setq first 1))
7299         (while read
7300           (if first 
7301               (while (< first nlast)
7302                 (setq unread (cons first unread))
7303                 (setq first (1+ first))))
7304           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7305           (setq nlast (if (atom (car (cdr read))) 
7306                           (car (cdr read))
7307                         (car (car (cdr read)))))
7308           (setq read (cdr read)))))
7309     ;; And add the last unread articles.
7310     (while (<= first last)
7311       (setq unread (cons first unread))
7312       (setq first (1+ first)))
7313     ;; Return the list of unread articles.
7314     (nreverse unread)))
7315
7316 (defun gnus-list-of-read-articles (group)
7317   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7318         (active (gnus-gethash group gnus-active-hashtb)))
7319     (and info active
7320          (gnus-sorted-complement 
7321           (gnus-uncompress-range active) 
7322           (gnus-list-of-unread-articles group)))))
7323
7324 ;; Various summary commands
7325
7326 (defun gnus-summary-universal-argument ()
7327   "Perform any operation on all articles marked with the process mark."
7328   (interactive)
7329   (gnus-set-global-variables)
7330   (let ((articles (reverse gnus-newsgroup-processable))
7331         func)
7332     (or articles (error "No articles marked"))
7333     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7334         (error "Undefined key"))
7335     (while articles
7336       (gnus-summary-goto-subject (car articles))
7337       (command-execute func)
7338       (gnus-summary-remove-process-mark (car articles))
7339       (setq articles (cdr articles)))))
7340
7341 (defun gnus-summary-toggle-truncation (arg)
7342   "Toggle truncation of summary lines.
7343 With arg, turn line truncation on iff arg is positive."
7344   (interactive "P")
7345   (setq truncate-lines
7346         (if (null arg) (not truncate-lines)
7347           (> (prefix-numeric-value arg) 0)))
7348   (redraw-display))
7349
7350 (defun gnus-summary-reselect-current-group (all)
7351   "Once exit and then reselect the current newsgroup.
7352 The prefix argument ALL means to select all articles."
7353   (interactive "P")
7354   (gnus-set-global-variables)
7355   (let ((current-subject (gnus-summary-article-number))
7356         (group gnus-newsgroup-name))
7357     (setq gnus-newsgroup-begin nil)
7358     (gnus-summary-exit t)
7359     ;; We have to adjust the point of group mode buffer because the
7360     ;; current point was moved to the next unread newsgroup by
7361     ;; exiting.
7362     (gnus-summary-jump-to-group group)
7363     (gnus-group-read-group all t)
7364     (gnus-summary-goto-subject current-subject)))
7365
7366 (defun gnus-summary-rescan-group (all)
7367   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7368   (interactive "P")
7369   (gnus-set-global-variables)
7370   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7371   (let ((group gnus-newsgroup-name))
7372     (gnus-summary-exit)
7373     (gnus-summary-jump-to-group group)
7374     (save-excursion
7375       (set-buffer gnus-group-buffer)
7376       (gnus-group-get-new-news-this-group 1))
7377     (gnus-summary-jump-to-group group)
7378     (gnus-group-read-group all)))
7379
7380 (defun gnus-summary-update-info ()
7381   (let* ((group gnus-newsgroup-name))
7382     (if gnus-newsgroup-kill-headers
7383         (setq gnus-newsgroup-killed
7384               (gnus-compress-sequence
7385                (nconc
7386                 (gnus-set-sorted-intersection
7387                  (gnus-uncompress-range gnus-newsgroup-killed)
7388                  (setq gnus-newsgroup-unselected
7389                        (sort gnus-newsgroup-unselected '<)))
7390                 (setq gnus-newsgroup-unreads
7391                       (sort gnus-newsgroup-unreads '<))) t)))
7392     (or (listp (cdr gnus-newsgroup-killed))
7393         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7394     (let ((headers gnus-newsgroup-headers))
7395       (gnus-close-group group)
7396       (run-hooks 'gnus-exit-group-hook)
7397       (gnus-update-read-articles 
7398        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7399        gnus-newsgroup-marked
7400        t gnus-newsgroup-replied gnus-newsgroup-expirable
7401        gnus-newsgroup-killed gnus-newsgroup-dormant
7402        gnus-newsgroup-bookmarks 
7403        (and gnus-save-score gnus-newsgroup-scored))
7404       (and gnus-use-cross-reference
7405            (gnus-mark-xrefs-as-read 
7406             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7407       ;; Do adaptive scoring, and possibly save score files.
7408       (and gnus-newsgroup-adaptive
7409            (gnus-score-adaptive))
7410       (and gnus-use-scoring 
7411            (fboundp 'gnus-score-save)
7412            (funcall 'gnus-score-save))
7413       ;; Do not switch windows but change the buffer to work.
7414       (set-buffer gnus-group-buffer)
7415       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7416           (gnus-group-update-group group)))))
7417   
7418 (defun gnus-summary-exit (&optional temporary)
7419   "Exit reading current newsgroup, and then return to group selection mode.
7420 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7421   (interactive)
7422   (gnus-set-global-variables)
7423   (gnus-kill-save-kill-buffer)
7424   (let* ((group gnus-newsgroup-name)
7425          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7426                                                   gnus-newsgroup-name))))
7427          (mode major-mode)
7428          (buf (current-buffer)))
7429     (run-hooks 'gnus-summary-exit-hook)
7430     (gnus-summary-update-info) ; Make all changes in this group permanent.
7431     (set-buffer buf)
7432     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7433     ;; Make sure where I was, and go to next newsgroup.
7434     (set-buffer gnus-group-buffer)
7435     (or quit-config
7436         (progn
7437           (gnus-group-jump-to-group group)
7438           (gnus-group-next-unread-group 1)))
7439     (if temporary
7440         nil                             ;Nothing to do.
7441       ;; We set all buffer-local variables to nil. It is unclear why
7442       ;; this is needed, but if we don't, buffer-local variables are
7443       ;; not garbage-collected, it seems. This would the lead to en
7444       ;; ever-growing Emacs.
7445       (set-buffer buf)
7446       (gnus-summary-clear-local-variables)
7447       ;; We clear the global counterparts of the buffer-local
7448       ;; variables as well, just to be on the safe side.
7449       (gnus-configure-windows 'group)
7450       (gnus-summary-clear-local-variables)
7451       ;; Return to group mode buffer. 
7452       (if (eq mode 'gnus-summary-mode)
7453           (gnus-kill-buffer buf))
7454       (if (get-buffer gnus-article-buffer)
7455           (bury-buffer gnus-article-buffer))
7456       (setq gnus-current-select-method gnus-select-method)
7457       (pop-to-buffer gnus-group-buffer)
7458       (if (not quit-config)
7459           (progn
7460             (gnus-group-jump-to-group group)
7461             (gnus-group-next-unread-group 1))
7462         (if (not (buffer-name (car quit-config)))
7463             (gnus-configure-windows 'group)
7464           (set-buffer (car quit-config))
7465           (and (eq major-mode 'gnus-summary-mode)
7466                (gnus-set-global-variables))
7467           (gnus-configure-windows (cdr quit-config)))))))
7468
7469 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7470 (defun gnus-summary-exit-no-update (&optional no-questions)
7471   "Quit reading current newsgroup without updating read article info."
7472   (interactive)
7473   (gnus-set-global-variables)
7474   (let* ((group gnus-newsgroup-name)
7475          (quit-config (nth 1 (assoc 'quit-config 
7476                                     (gnus-find-method-for-group group)))))
7477     (if (or no-questions
7478             gnus-expert-user
7479             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7480         (progn
7481           (gnus-close-group group)
7482           (gnus-summary-clear-local-variables)
7483           (set-buffer gnus-group-buffer)
7484           (gnus-summary-clear-local-variables)
7485           ;; Return to group selection mode.
7486           (gnus-configure-windows 'group)
7487           (if (get-buffer gnus-summary-buffer)
7488               (kill-buffer gnus-summary-buffer))
7489           (if (get-buffer gnus-article-buffer)
7490               (bury-buffer gnus-article-buffer))
7491           (if (equal (gnus-group-group-name) group)
7492               (gnus-group-next-unread-group 1))
7493           (if quit-config
7494               (progn
7495                 (if (not (buffer-name (car quit-config)))
7496                     (gnus-configure-windows 'group)
7497                   (set-buffer (car quit-config))
7498                   (and (eq major-mode 'gnus-summary-mode)
7499                        (gnus-set-global-variables))
7500                   (gnus-configure-windows (cdr quit-config)))))))))
7501
7502 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7503 (defun gnus-summary-fetch-faq (group)
7504   "Fetch the FAQ for the current group."
7505   (interactive (list gnus-newsgroup-name))
7506   (let ((gnus-faq-buffer 
7507          (find-file (concat gnus-group-faq-directory 
7508                             (gnus-group-real-name group)))))
7509   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7510
7511 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7512 (defun gnus-summary-describe-group (force)
7513   "Describe the current newsgroup."
7514   (interactive "P")
7515   (gnus-group-describe-group force gnus-newsgroup-name))
7516
7517 (defun gnus-summary-describe-briefly ()
7518   "Describe summary mode commands briefly."
7519   (interactive)
7520   (gnus-message 6
7521     (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")))
7522
7523 ;; Walking around group mode buffer from summary mode.
7524
7525 (defun gnus-summary-next-group (&optional no-article target-group backward)
7526   "Exit current newsgroup and then select next unread newsgroup.
7527 If prefix argument NO-ARTICLE is non-nil, no article is selected
7528 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7529 previous group instead."
7530   (interactive "P")
7531   (gnus-set-global-variables)
7532   (let ((current-group gnus-newsgroup-name)
7533         (current-buffer (current-buffer))
7534         entered)
7535     ;; First we semi-exit this group to update Xrefs and all variables.
7536     ;; We can't do a real exit, because the window conf must remain
7537     ;; the same in case the user is prompted for info, and we don't
7538     ;; want the window conf to change before that...
7539     (gnus-summary-exit t)
7540     (while (not entered)
7541       ;; Then we find what group we are supposed to enter.
7542       (set-buffer gnus-group-buffer)
7543       (gnus-group-jump-to-group current-group)
7544       (setq target-group 
7545             (or target-group        
7546                 (if (eq gnus-keep-same-level 'best) 
7547                     (gnus-summary-best-group gnus-newsgroup-name)
7548                   (gnus-summary-search-group backward gnus-keep-same-level))))
7549       (if (not target-group)
7550           ;; There are no further groups, so we return to the group
7551           ;; buffer.
7552           (progn
7553             (gnus-message 5 "Returning to the group buffer")
7554             (setq entered t)
7555             (set-buffer current-buffer)
7556             (gnus-summary-exit))
7557         ;; We try to enter the target group.
7558         (gnus-group-jump-to-group target-group)
7559         (let ((unreads (gnus-group-group-unread)))
7560           (if (and (or (eq t unreads)
7561                        (and unreads (not (zerop unreads))))
7562                    (gnus-summary-read-group
7563                     target-group nil no-article current-buffer))
7564               (setq entered t)
7565             (setq current-group target-group
7566                   target-group nil)))))))
7567
7568 (defun gnus-summary-next-group-old (&optional no-article group backward)
7569   "Exit current newsgroup and then select next unread newsgroup.
7570 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7571 If BACKWARD, go to previous group instead."
7572   (interactive "P")
7573   (gnus-set-global-variables)
7574   (let ((ingroup gnus-newsgroup-name)
7575         (sumbuf (current-buffer))
7576         num)
7577     (set-buffer gnus-group-buffer)
7578     (if (and group
7579              (or (and (numberp (setq num (car (gnus-gethash
7580                                                group gnus-newsrc-hashtb))))
7581                       (< num 1))
7582                  (null num)))
7583         (progn
7584           (gnus-group-jump-to-group group)
7585           (setq group nil))
7586       (gnus-group-jump-to-group ingroup))
7587     (gnus-summary-search-group backward)
7588     (let ((group (or group (gnus-summary-search-group backward))))
7589       (set-buffer sumbuf)
7590       (gnus-summary-exit t)             ;Update all information.
7591       (if (null group)
7592           (gnus-summary-exit-no-update t)
7593         (gnus-group-jump-to-group ingroup)
7594         (setq group (gnus-summary-search-group backward))
7595         (gnus-message 5 "Selecting %s..." group)
7596         (set-buffer gnus-group-buffer)
7597         ;; We are now in group mode buffer.
7598         ;; Make sure group mode buffer point is on GROUP.
7599         (gnus-group-jump-to-group group)
7600         (if (not (eq gnus-auto-select-next 'quietly))
7601             (progn
7602               (gnus-summary-read-group group nil no-article sumbuf)
7603               (and (string= gnus-newsgroup-name ingroup)
7604                    (bufferp sumbuf) (buffer-name sumbuf)
7605                    (progn
7606                      (set-buffer (setq gnus-summary-buffer sumbuf))
7607                      (gnus-summary-exit-no-update t))))
7608           (let ((prevgroup group))
7609             (gnus-group-jump-to-group ingroup)
7610             (setq group (gnus-summary-search-group backward))
7611             (gnus-summary-read-group group nil no-article sumbuf)
7612             (while (and (string= gnus-newsgroup-name ingroup)
7613                         (bufferp sumbuf) 
7614                         (buffer-name sumbuf)
7615                         (not (string= prevgroup (gnus-group-group-name))))
7616               (set-buffer gnus-group-buffer)
7617               (gnus-summary-read-group 
7618                (setq prevgroup (gnus-group-group-name)) 
7619                nil no-article sumbuf))
7620             (and (string= prevgroup (gnus-group-group-name))
7621                  ;; We have reached the final group in the group
7622                  ;; buffer.
7623                  (progn
7624                    (if (buffer-name sumbuf)
7625                        (progn
7626                          (set-buffer sumbuf)
7627                          (gnus-summary-exit)))))))))))
7628
7629 (defun gnus-summary-prev-group (no-article)
7630   "Exit current newsgroup and then select previous unread newsgroup.
7631 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7632   (interactive "P")
7633   (gnus-summary-next-group no-article nil t))
7634
7635 ;; Walking around summary lines.
7636
7637 (defun gnus-summary-first-subject (unread)
7638   "Go to the first unread subject.
7639 If UNREAD is non-nil, go to the first unread article.
7640 Returns nil if there are no unread articles."
7641   (interactive "P")
7642   (prog1
7643       (cond ((not unread)
7644              (goto-char (point-min)))
7645             ((gnus-goto-char 
7646               (text-property-any 
7647                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7648              t)
7649             (t 
7650              ;; There are no unread articles.
7651              (gnus-message 3 "No more unread articles")
7652              nil))
7653     (gnus-summary-position-cursor)))
7654
7655 (defun gnus-summary-next-subject (n &optional unread dont-display)
7656   "Go to next N'th summary line.
7657 If N is negative, go to the previous N'th subject line.
7658 If UNREAD is non-nil, only unread articles are selected.
7659 The difference between N and the actual number of steps taken is
7660 returned."
7661   (interactive "p")
7662   (let ((backward (< n 0))
7663         (n (abs n)))
7664     (while (and (> n 0)
7665                 (gnus-summary-search-forward unread nil backward))
7666       (setq n (1- n)))
7667     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7668                                (if unread " unread" "")))
7669     (or dont-display
7670         (progn
7671           (gnus-summary-recenter)
7672           (gnus-summary-position-cursor)))
7673   n))
7674
7675 (defun gnus-summary-next-unread-subject (n)
7676   "Go to next N'th unread summary line."
7677   (interactive "p")
7678   (gnus-summary-next-subject n t))
7679
7680 (defun gnus-summary-prev-subject (n &optional unread)
7681   "Go to previous N'th summary line.
7682 If optional argument UNREAD is non-nil, only unread article is selected."
7683   (interactive "p")
7684   (gnus-summary-next-subject (- n) unread))
7685
7686 (defun gnus-summary-prev-unread-subject (n)
7687   "Go to previous N'th unread summary line."
7688   (interactive "p")
7689   (gnus-summary-next-subject (- n) t))
7690
7691 (defun gnus-summary-goto-subject (article)
7692   "Go the subject line of ARTICLE."
7693   (interactive
7694    (list
7695     (string-to-int
7696      (completing-read "Article number: "
7697                       (mapcar
7698                        (lambda (headers)
7699                          (list
7700                           (int-to-string (header-number headers))))
7701                        gnus-newsgroup-headers)
7702                       nil 'require-match))))
7703   (or article (error "No article number"))
7704   (let ((b (point)))
7705     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7706                                                 'gnus-number article)))
7707         ()
7708       (gnus-summary-show-thread)
7709       ;; Skip dummy articles. 
7710       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7711           (forward-line 1))
7712       (prog1
7713           (if (not (eobp))
7714               article
7715             (goto-char b)
7716             nil)
7717         (gnus-summary-position-cursor)))))
7718
7719 ;; Walking around summary lines with displaying articles.
7720
7721 (defun gnus-summary-expand-window (arg)
7722   "Make the summary buffer take up the entire Emacs frame.
7723 Given a prefix, will force an `article' buffer configuration."
7724   (interactive "P")
7725   (gnus-set-global-variables)
7726   (if arg
7727       (gnus-configure-windows 'article 'force)
7728     (gnus-configure-windows 'summary)))
7729
7730 (defun gnus-summary-display-article (article &optional all-header)
7731   "Display ARTICLE in article buffer."
7732   (gnus-set-global-variables)
7733   (if (null article)
7734       nil
7735     (prog1
7736         (gnus-article-prepare article all-header)
7737       (gnus-summary-show-thread)
7738       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7739           (progn
7740             (forward-line 1)
7741             (gnus-summary-position-cursor)))
7742       (run-hooks 'gnus-select-article-hook)
7743       (gnus-summary-recenter)
7744       (gnus-summary-goto-subject article)
7745       ;; Successfully display article.
7746       (gnus-summary-update-line)
7747       (gnus-article-set-window-start 
7748        (cdr (assq article gnus-newsgroup-bookmarks)))
7749       t)))
7750
7751 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7752   "Select the current article.
7753 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7754 non-nil, the article will be re-fetched even if it already present in
7755 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7756 be displayed."
7757   (and (not pseudo) (gnus-summary-pseudo-article)
7758        (error "This is a pseudo-article."))
7759   (let ((article (or article (gnus-summary-article-number)))
7760         (all-headers (not (not all-headers))) ;Must be T or NIL.
7761         did) 
7762     (prog1
7763         (save-excursion
7764           (set-buffer gnus-summary-buffer)
7765           (if (or (null gnus-current-article)
7766                   (null gnus-article-current)
7767                   (null (get-buffer gnus-article-buffer))
7768                   (not (eq article (cdr gnus-article-current)))
7769                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7770                   force)
7771               ;; The requested article is different from the current article.
7772               (progn
7773                 (gnus-summary-display-article article all-headers)
7774                 (setq did article))
7775             (if (or all-headers gnus-show-all-headers) 
7776                 (gnus-article-show-all-headers))
7777             nil))
7778       (if did 
7779           (gnus-article-set-window-start 
7780            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7781
7782 (defun gnus-summary-set-current-mark (&optional current-mark)
7783   "Obsolete function."
7784   nil)
7785
7786 (defun gnus-summary-next-article (unread &optional subject backward)
7787   "Select the next article.
7788 If UNREAD, only unread articles are selected.
7789 If SUBJECT, only articles with SUBJECT are selected.
7790 If BACKWARD, the previous article is selected instead of the next."
7791   (interactive "P")
7792   (gnus-set-global-variables)
7793   (let (header)
7794     (cond
7795      ;; Is there such an article?
7796      ((gnus-summary-display-article 
7797        (gnus-summary-search-forward unread subject backward))
7798       (gnus-summary-position-cursor))
7799      ;; If not, we try the first unread, if that is wanted.
7800      ((and subject
7801            gnus-auto-select-same
7802            (gnus-summary-first-unread-article))
7803       (gnus-message 6 "Wrapped"))
7804      ;; Try to get next/previous article not displayed in this group.
7805      ((and gnus-auto-extend-newsgroup
7806            (not unread) (not subject)
7807            (setq header (gnus-more-header-forward backward)))
7808       (gnus-extend-newsgroup header backward)
7809       (let ((buffer-read-only nil))
7810         (goto-char (if backward (point-min) (point-max)))
7811         (gnus-summary-prepare-threads (list header)))
7812       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7813                                    gnus-newsgroup-end)))
7814      ;; Go to next/previous group.
7815      (t
7816       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7817           (gnus-summary-jump-to-group gnus-newsgroup-name))
7818       (let ((cmd (aref (this-command-keys) 0))
7819             (group 
7820              (if (eq gnus-keep-same-level 'best) 
7821                  (gnus-summary-best-group gnus-newsgroup-name)
7822                (gnus-summary-search-group backward gnus-keep-same-level))))
7823         ;; For some reason, the group window gets selected. We change
7824         ;; it back.  
7825         (select-window (get-buffer-window (current-buffer)))
7826         ;; Keep just the event type of CMD.
7827         (and (listp cmd) (setq cmd (car cmd)))
7828         ;; Select next unread newsgroup automagically.
7829         (cond 
7830          ((not gnus-auto-select-next)
7831           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7832          ((eq gnus-auto-select-next 'quietly)
7833           ;; Select quietly.
7834           (if (assoc 'quit-config (gnus-find-method-for-group 
7835                                    gnus-newsgroup-name))
7836               (gnus-summary-exit)
7837             (gnus-message 7 "No more%s articles (%s)..."
7838                           (if unread " unread" "") 
7839                           (if group (concat "selecting " group)
7840                             "exiting"))
7841             (gnus-summary-next-group nil group backward)))
7842          (t
7843           (let ((keystrokes '(?\C-n ?\C-p))
7844                 key)
7845             (while (or (null key) (memq key keystrokes))
7846               (gnus-message 
7847                7 "No more%s articles%s" (if unread " unread" "")
7848                (if (and group (not (assoc 'quit-config
7849                                           (gnus-find-method-for-group 
7850                                            gnus-newsgroup-name))))
7851                    (format " (Type %s for %s [%s])"
7852                            (single-key-description cmd) group
7853                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7854                  (format " (Type %s to exit %s)"
7855                          (single-key-description cmd)
7856                          gnus-newsgroup-name)))
7857               ;; Confirm auto selection.
7858               (let* ((event (read-event)))
7859                 (setq key (if (listp event) (car event) event))
7860                 (if (memq key keystrokes)
7861                     (let ((obuf (current-buffer)))
7862                       (switch-to-buffer gnus-group-buffer)
7863                       (gnus-group-jump-to-group group)
7864                       (execute-kbd-macro (char-to-string key))
7865                       (setq group (gnus-group-group-name))
7866                       (switch-to-buffer obuf)))))
7867             (if (equal key cmd)
7868                 (if (or (not group) (assoc 'quit-config
7869                                            (gnus-find-method-for-group
7870                                             gnus-newsgroup-name)))
7871                     (gnus-summary-exit)
7872                   (gnus-summary-next-group nil group backward))
7873               (setq unread-command-events (list key)))))))))))
7874
7875 (defun gnus-summary-next-unread-article ()
7876   "Select unread article after current one."
7877   (interactive)
7878   (gnus-summary-next-article t (and gnus-auto-select-same
7879                                     (gnus-summary-subject-string))))
7880
7881 (defun gnus-summary-prev-article (unread &optional subject)
7882   "Select the article after the current one.
7883 If UNREAD is non-nil, only unread articles are selected."
7884   (interactive "P")
7885   (gnus-summary-next-article unread subject t))
7886
7887 (defun gnus-summary-prev-unread-article ()
7888   "Select unred article before current one."
7889   (interactive)
7890   (gnus-summary-prev-article t (and gnus-auto-select-same
7891                                     (gnus-summary-subject-string))))
7892
7893 (defun gnus-summary-next-page (lines &optional circular)
7894   "Show next page of selected article.
7895 If end of article, select next article.
7896 Argument LINES specifies lines to be scrolled up.
7897 If CIRCULAR is non-nil, go to the start of the article instead of 
7898 instead of selecting the next article when reaching the end of the
7899 current article." 
7900   (interactive "P")
7901   (setq gnus-summary-buffer (current-buffer))
7902   (gnus-set-global-variables)
7903   (let ((article (gnus-summary-article-number))
7904         (endp nil))
7905     (gnus-configure-windows 'article)
7906     (if (or (null gnus-current-article)
7907             (null gnus-article-current)
7908             (/= article (cdr gnus-article-current))
7909             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7910         ;; Selected subject is different from current article's.
7911         (gnus-summary-display-article article)
7912       (gnus-eval-in-buffer-window
7913        gnus-article-buffer
7914        (setq endp (gnus-article-next-page lines)))
7915       (if endp
7916           (cond (circular
7917                  (gnus-summary-beginning-of-article))
7918                 (lines
7919                  (gnus-message 3 "End of message"))
7920                 ((null lines)
7921                  (gnus-summary-next-unread-article)))))
7922     (gnus-summary-recenter)
7923     (gnus-summary-position-cursor)))
7924
7925 (defun gnus-summary-prev-page (lines)
7926   "Show previous page of selected article.
7927 Argument LINES specifies lines to be scrolled down."
7928   (interactive "P")
7929   (gnus-set-global-variables)
7930   (let ((article (gnus-summary-article-number)))
7931     (gnus-configure-windows 'article)
7932     (if (or (null gnus-current-article)
7933             (null gnus-article-current)
7934             (/= article (cdr gnus-article-current))
7935             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7936         ;; Selected subject is different from current article's.
7937         (gnus-summary-display-article article)
7938       (gnus-summary-recenter)
7939       (gnus-eval-in-buffer-window gnus-article-buffer
7940         (gnus-article-prev-page lines))))
7941   (gnus-summary-position-cursor))
7942
7943 (defun gnus-summary-scroll-up (lines)
7944   "Scroll up (or down) one line current article.
7945 Argument LINES specifies lines to be scrolled up (or down if negative)."
7946   (interactive "p")
7947   (gnus-set-global-variables)
7948   (gnus-configure-windows 'article)
7949   (or (gnus-summary-select-article nil nil 'pseudo)
7950       (gnus-eval-in-buffer-window 
7951        gnus-article-buffer
7952        (cond ((> lines 0)
7953               (if (gnus-article-next-page lines)
7954                   (gnus-message 3 "End of message")))
7955              ((< lines 0)
7956               (gnus-article-prev-page (- lines))))))
7957   (gnus-summary-recenter)
7958   (gnus-summary-position-cursor))
7959
7960 (defun gnus-summary-next-same-subject ()
7961   "Select next article which has the same subject as current one."
7962   (interactive)
7963   (gnus-set-global-variables)
7964   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7965
7966 (defun gnus-summary-prev-same-subject ()
7967   "Select previous article which has the same subject as current one."
7968   (interactive)
7969   (gnus-set-global-variables)
7970   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7971
7972 (defun gnus-summary-next-unread-same-subject ()
7973   "Select next unread article which has the same subject as current one."
7974   (interactive)
7975   (gnus-set-global-variables)
7976   (gnus-summary-next-article t (gnus-summary-subject-string)))
7977
7978 (defun gnus-summary-prev-unread-same-subject ()
7979   "Select previous unread article which has the same subject as current one."
7980   (interactive)
7981   (gnus-set-global-variables)
7982   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7983
7984 (defun gnus-summary-first-unread-article ()
7985   "Select the first unread article. 
7986 Return nil if there are no unread articles."
7987   (interactive)
7988   (gnus-set-global-variables)
7989   (prog1
7990       (if (gnus-summary-first-subject t)
7991           (gnus-summary-display-article (gnus-summary-article-number)))
7992     (gnus-summary-position-cursor)))
7993
7994 (defun gnus-summary-best-unread-article ()
7995   "Select the unread article with the highest score."
7996   (interactive)
7997   (gnus-set-global-variables)
7998   (let ((scored gnus-newsgroup-scored)
7999         (best -1000000)
8000         article art)
8001     (while scored
8002       (or (> best (cdr (car scored)))
8003           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8004                (not (memq art gnus-newsgroup-marked))
8005                (not (memq art gnus-newsgroup-dormant))
8006                (if (= best (cdr (car scored)))
8007                    (setq article (min art article))
8008                  (setq article art)
8009                  (setq best (cdr (car scored))))))
8010       (setq scored (cdr scored)))
8011     (if article 
8012         (gnus-summary-goto-article article)
8013       (gnus-summary-first-unread-article))
8014     (gnus-summary-position-cursor)))
8015
8016 (defun gnus-summary-goto-article (article &optional all-headers)
8017   "Fetch ARTICLE and display it if it exists.
8018 If ALL-HEADERS is non-nil, no header lines are hidden."
8019   (interactive
8020    (list
8021     (string-to-int
8022      (completing-read 
8023       "Article number: "
8024       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8025               gnus-newsgroup-headers) 
8026       nil 'require-match))))
8027   (prog1
8028       (and (gnus-summary-goto-subject article)
8029            (gnus-summary-display-article article all-headers))
8030     (gnus-summary-position-cursor)))
8031
8032 (defun gnus-summary-goto-last-article ()
8033   "Go to the previously read article."
8034   (interactive)
8035   (prog1
8036       (and gnus-last-article
8037            (gnus-summary-goto-article gnus-last-article))
8038     (gnus-summary-position-cursor)))
8039
8040 (defun gnus-summary-pop-article (number)
8041   "Pop one article off the history and go to the previous.
8042 NUMBER articles will be popped off."
8043   (interactive "p")
8044   (let (to)
8045     (setq gnus-newsgroup-history
8046           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8047     (if to
8048         (gnus-summary-goto-article (car to))
8049       (error "Article history empty")))
8050   (gnus-summary-position-cursor))
8051
8052 ;; Summary article oriented commands
8053
8054 (defun gnus-summary-refer-parent-article (n)
8055   "Refer parent article N times.
8056 The difference between N and the number of articles fetched is returned."
8057   (interactive "p")
8058   (gnus-set-global-variables)
8059   (while 
8060       (and 
8061        (> n 0)
8062        (let ((ref (header-references (gnus-get-header-by-number
8063                                       (gnus-summary-article-number)))))
8064          (if (and ref (not (equal ref ""))
8065                   (string-match "<[^<>]*>[ \t]*$" ref))
8066              (gnus-summary-refer-article 
8067               (substring ref (match-beginning 0) (match-end 0)))
8068            (gnus-message 1 "No references in article %d"
8069                          (gnus-summary-article-number))
8070            nil)))
8071     (setq n (1- n)))
8072   (gnus-summary-position-cursor)
8073   n)
8074     
8075 (defun gnus-summary-refer-article (message-id)
8076   "Refer article specified by MESSAGE-ID.
8077 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8078   (interactive "sMessage-ID: ")
8079   (if (or (not (stringp message-id))
8080           (zerop (length message-id)))
8081       ()
8082     ;; Construct the correct Message-ID if necessary.
8083     ;; Suggested by tale@pawl.rpi.edu.
8084     (or (string-match "^<" message-id)
8085         (setq message-id (concat "<" message-id)))
8086     (or (string-match ">$" message-id)
8087         (setq message-id (concat message-id ">")))
8088     (let ((header (car (gnus-gethash (downcase message-id)
8089                                      gnus-newsgroup-dependencies))))
8090       (if header
8091           (or (gnus-summary-goto-article (header-number header))
8092               ;; The header has been read, but the article had been
8093               ;; expunged, so we insert it again.
8094               (progn
8095                 (gnus-summary-insert-line
8096                  nil header 0 nil gnus-read-mark nil nil
8097                  (header-subject header))
8098                 (forward-line -1)
8099                 (header-number header)))
8100         (let ((gnus-override-method gnus-refer-article-method)
8101               (gnus-ancient-mark gnus-read-mark)
8102               (tmp-point (window-start
8103                           (get-buffer-window gnus-article-buffer)))
8104               number tmp-buf)
8105           (and gnus-refer-article-method
8106                (or (gnus-server-opened gnus-refer-article-method)
8107                    (gnus-open-server gnus-refer-article-method)))
8108           ;; Save the old article buffer.
8109           (save-excursion
8110             (set-buffer gnus-article-buffer)
8111             (gnus-kill-buffer " *temp Article*")
8112             (setq tmp-buf (rename-buffer " *temp Article*")))
8113           (prog1
8114               (if (gnus-article-prepare 
8115                    message-id nil (gnus-read-header message-id))
8116                   (progn
8117                     (setq number (header-number gnus-current-headers))
8118                     (gnus-rebuild-thread message-id)
8119                     (gnus-summary-goto-subject number)
8120                     (gnus-summary-recenter)
8121                     (gnus-article-set-window-start 
8122                      (cdr (assq number gnus-newsgroup-bookmarks)))
8123                     message-id)
8124                 ;; We restore the old article buffer.
8125                 (save-excursion
8126                   (kill-buffer gnus-article-buffer)
8127                   (set-buffer tmp-buf)
8128                   (rename-buffer gnus-article-buffer)
8129                   (let ((buffer-read-only nil))
8130                     (and tmp-point
8131                          (set-window-start (get-buffer-window (current-buffer))
8132                                            tmp-point)))))))))))
8133
8134 (defun gnus-summary-enter-digest-group ()
8135   "Enter a digest group based on the current article."
8136   (interactive)
8137   (gnus-set-global-variables)
8138   (gnus-summary-select-article)
8139   ;; We do not want a narrowed article.
8140   (gnus-summary-stop-page-breaking)
8141   (let ((name (format "%s-%d" 
8142                       (gnus-group-prefixed-name 
8143                        gnus-newsgroup-name (list 'nndoc "")) 
8144                       gnus-current-article))
8145         (ogroup gnus-newsgroup-name)
8146         (buf (current-buffer)))
8147     (if (gnus-group-read-ephemeral-group 
8148          name (list 'nndoc name
8149                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8150                     '(nndoc-article-type digest))
8151          t)
8152         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8153                 (list (list (cons 'to-group ogroup))))
8154       (switch-to-buffer buf)
8155       (gnus-set-global-variables)
8156       (gnus-configure-windows 'summary)
8157       (gnus-message 3 "Article not a digest?"))))
8158
8159 (defun gnus-summary-isearch-article ()
8160   "Do incremental search forward on current article."
8161   (interactive)
8162   (gnus-set-global-variables)
8163   (gnus-summary-select-article)
8164   (gnus-eval-in-buffer-window 
8165    gnus-article-buffer (isearch-forward)))
8166
8167 (defun gnus-summary-search-article-forward (regexp &optional backward)
8168   "Search for an article containing REGEXP forward.
8169 If BACKWARD, search backward instead."
8170   (interactive
8171    (list (read-string
8172           (format "Search article %s (regexp%s): "
8173                   (if current-prefix-arg "backward" "forward")
8174                   (if gnus-last-search-regexp
8175                       (concat ", default " gnus-last-search-regexp)
8176                     "")))
8177          current-prefix-arg))
8178   (gnus-set-global-variables)
8179   (if (string-equal regexp "")
8180       (setq regexp (or gnus-last-search-regexp ""))
8181     (setq gnus-last-search-regexp regexp))
8182   (if (gnus-summary-search-article regexp backward)
8183       (gnus-article-set-window-start 
8184        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8185     (error "Search failed: \"%s\"" regexp)))
8186
8187 (defun gnus-summary-search-article-backward (regexp)
8188   "Search for an article containing REGEXP backward."
8189   (interactive
8190    (list (read-string
8191           (format "Search article backward (regexp%s): "
8192                   (if gnus-last-search-regexp
8193                       (concat ", default " gnus-last-search-regexp)
8194                     "")))))
8195   (gnus-summary-search-article-forward regexp 'backward))
8196
8197 (defun gnus-summary-search-article (regexp &optional backward)
8198   "Search for an article containing REGEXP.
8199 Optional argument BACKWARD means do search for backward.
8200 gnus-select-article-hook is not called during the search."
8201   (let ((gnus-select-article-hook nil)  ;Disable hook.
8202         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8203         (re-search
8204          (if backward
8205              (function re-search-backward) (function re-search-forward)))
8206         (found nil)
8207         (last nil))
8208     ;; Hidden thread subtrees must be searched for ,too.
8209     (gnus-summary-show-all-threads)
8210     (if (eobp) (forward-line -1))
8211     ;; First of all, search current article.
8212     ;; We don't want to read article again from NNTP server nor reset
8213     ;; current point.
8214     (gnus-summary-select-article)
8215     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8216     (setq last gnus-current-article)
8217     (gnus-eval-in-buffer-window gnus-article-buffer
8218       (save-restriction
8219         (widen)
8220         ;; Begin search from current point.
8221         (setq found (funcall re-search regexp nil t))))
8222     ;; Then search next articles.
8223     (while (and (not found)
8224                 (gnus-summary-display-article 
8225                  (gnus-summary-search-subject backward nil nil)))
8226       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8227       (gnus-eval-in-buffer-window gnus-article-buffer
8228         (save-restriction
8229           (widen)
8230           (goto-char (if backward (point-max) (point-min)))
8231           (setq found (funcall re-search regexp nil t)))))
8232     (message "")
8233     ;; Adjust article pointer.
8234     (or (eq last gnus-current-article)
8235         (setq gnus-last-article last))
8236     ;; Return T if found such article.
8237     found))
8238
8239 (defun gnus-summary-execute-command (header regexp command &optional backward)
8240   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8241 If HEADER is an empty string (or nil), the match is done on the entire
8242 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8243   (interactive
8244    (list (let ((completion-ignore-case t))
8245            (completing-read 
8246             "Header name: "
8247             (mapcar (lambda (string) (list string))
8248                     '("Number" "Subject" "From" "Lines" "Date"
8249                       "Message-ID" "Xref" "References"))
8250             nil 'require-match))
8251          (read-string "Regexp: ")
8252          (read-key-sequence "Command: ")
8253          current-prefix-arg))
8254   (gnus-set-global-variables)
8255   ;; Hidden thread subtrees must be searched as well.
8256   (gnus-summary-show-all-threads)
8257   ;; We don't want to change current point nor window configuration.
8258   (save-excursion
8259     (save-window-excursion
8260       (gnus-message 6 "Executing %s..." (key-description command))
8261       ;; We'd like to execute COMMAND interactively so as to give arguments.
8262       (gnus-execute header regexp
8263                     (` (lambda ()
8264                          (call-interactively '(, (key-binding command)))))
8265                     backward)
8266       (gnus-message 6 "Executing %s...done" (key-description command)))))
8267
8268 (defun gnus-summary-beginning-of-article ()
8269   "Scroll the article back to the beginning."
8270   (interactive)
8271   (gnus-set-global-variables)
8272   (gnus-summary-select-article)
8273   (gnus-configure-windows 'article)
8274   (gnus-eval-in-buffer-window
8275    gnus-article-buffer
8276    (widen)
8277    (goto-char (point-min))
8278    (and gnus-break-pages (gnus-narrow-to-page))))
8279
8280 (defun gnus-summary-end-of-article ()
8281   "Scroll to the end of the article."
8282   (interactive)
8283   (gnus-set-global-variables)
8284   (gnus-summary-select-article)
8285   (gnus-configure-windows 'article)
8286   (gnus-eval-in-buffer-window 
8287    gnus-article-buffer
8288    (widen)
8289    (goto-char (point-max))
8290    (and gnus-break-pages (gnus-narrow-to-page))))
8291
8292 (defun gnus-summary-show-article (no-refetch)
8293   "Force re-fetching of the current article.
8294 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8295 be performed.  The current article will simply be redisplayed."
8296   (interactive "P")
8297   (gnus-set-global-variables)
8298   (if (not no-refetch)
8299       (gnus-summary-select-article gnus-have-all-headers t)
8300     (or gnus-current-article
8301         (error "There is no current article"))
8302     (gnus-summary-goto-subject gnus-current-article)
8303     (gnus-configure-windows 'article)
8304     (gnus-summary-position-cursor)))
8305
8306 (defun gnus-summary-verbose-headers (arg)
8307   "Toggle permanent full header display.
8308 If ARG is a positive number, turn header display on.
8309 If ARG is a negative number, turn header display off."
8310   (interactive "P")
8311   (gnus-set-global-variables)
8312   (gnus-summary-toggle-header arg)
8313   (setq gnus-have-all-headers
8314         (cond ((or (not (numberp arg))
8315                    (zerop arg))
8316                (not gnus-have-all-headers))
8317               ((natnump arg)
8318                t))))
8319
8320 (defun gnus-summary-toggle-header (arg)
8321   "Show the headers if they are hidden, or hide them if they are shown.
8322 If ARG is a positive number, show the entire header.
8323 If ARG is a negative number, hide the unwanted header lines."
8324   (interactive "P")
8325   (gnus-set-global-variables)
8326   (save-excursion
8327     (set-buffer gnus-article-buffer)
8328     (let ((buffer-read-only nil))
8329       (if (numberp arg) 
8330           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8331                                                 gnus-hidden-properties)
8332             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8333         (if (text-property-any (point-min) (point-max) 'invisible t)
8334             (remove-text-properties (point-min) (point-max)
8335                                     gnus-hidden-properties)
8336           (let ((gnus-have-all-headers nil))
8337             (run-hooks 'gnus-article-display-hook))))
8338       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8339
8340 (defun gnus-summary-show-all-headers ()
8341   "Make all header lines visible."
8342   (interactive)
8343   (gnus-set-global-variables)
8344   (gnus-article-show-all-headers))
8345
8346 (defun gnus-summary-toggle-mime (arg)
8347   "Toggle MIME processing.
8348 If ARG is a positive number, turn MIME processing on."
8349   (interactive "P")
8350   (gnus-set-global-variables)
8351   (setq gnus-show-mime
8352         (if (null arg) (not gnus-show-mime)
8353           (> (prefix-numeric-value arg) 0)))
8354   (gnus-summary-select-article t 'force))
8355
8356 (defun gnus-summary-caesar-message (arg)
8357   "Caesar rotate the current article by 13.
8358 The numerical prefix specifies how manu places to rotate each letter
8359 forward."
8360   (interactive "P")
8361   (gnus-set-global-variables)
8362   (gnus-summary-select-article)
8363   (let ((mail-header-separator ""))
8364     (gnus-eval-in-buffer-window 
8365      gnus-article-buffer
8366      (save-restriction
8367        (widen)
8368        (let ((last (point)))
8369          (news-caesar-buffer-body arg)
8370          (goto-char last)
8371          (recenter 0))))))
8372
8373 (defun gnus-summary-stop-page-breaking ()
8374   "Stop page breaking in the current article."
8375   (interactive)
8376   (gnus-set-global-variables)
8377   (gnus-summary-select-article)
8378   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8379
8380 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8381
8382 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8383   "Move the current article to a different newsgroup.
8384 If N is a positive number, move the N next articles.
8385 If N is a negative number, move the N previous articles.
8386 If N is nil and any articles have been marked with the process mark,
8387 move those articles instead.
8388 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8389 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8390 re-spool using this method.
8391 For this function to work, both the current newsgroup and the
8392 newsgroup that you want to move to have to support the `request-move'
8393 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8394   (interactive "P")
8395   (gnus-set-global-variables)
8396   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8397       (error "The current newsgroup does not support article moving"))
8398   (let ((articles (gnus-summary-work-articles n))
8399         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8400         art-group to-method sel-met)
8401     (if (and (not to-newsgroup) (not select-method))
8402         (setq to-newsgroup
8403               (completing-read 
8404                (format "Where do you want to move %s? %s"
8405                        (if (> (length articles) 1)
8406                            (format "these %d articles" (length articles))
8407                          "this article")
8408                        (if gnus-current-move-group
8409                            (format "(%s default) " gnus-current-move-group)
8410                          ""))
8411                gnus-active-hashtb nil nil prefix)))
8412     (if to-newsgroup
8413         (progn
8414           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8415               (setq to-newsgroup (or gnus-current-move-group "")))
8416           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8417               (gnus-activate-newsgroup to-newsgroup)
8418               (error "No such group: %s" to-newsgroup))
8419           (setq gnus-current-move-group to-newsgroup)))
8420     (setq to-method (if select-method (list select-method "")
8421                       (gnus-find-method-for-group to-newsgroup)))
8422     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8423         (error "%s does not support article copying" (car to-method)))
8424     (or (gnus-server-opened to-method)
8425         (gnus-open-server to-method)
8426         (error "Can't open server %s" (car to-method)))
8427     (gnus-message 6 "Moving to %s: %s..." 
8428                   (or select-method to-newsgroup) articles)
8429     (while articles
8430       (if (setq art-group
8431                 (gnus-request-move-article 
8432                  (car articles)                   ; Article to move
8433                  gnus-newsgroup-name              ; From newsgrouo
8434                  (nth 1 (gnus-find-method-for-group 
8435                          gnus-newsgroup-name))    ; Server
8436                  (list 'gnus-request-accept-article 
8437                        (if select-method
8438                            (list 'quote select-method)
8439                          to-newsgroup)
8440                        (not (cdr articles)))     ; Accept form
8441                  (not (cdr articles))))          ; Only save nov last time
8442           (let* ((buffer-read-only nil)
8443                  (entry 
8444                   (or
8445                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8446                    (gnus-gethash 
8447                     (gnus-group-prefixed-name 
8448                      (car art-group) 
8449                      (if select-method (list select-method "")
8450                        (gnus-find-method-for-group to-newsgroup)))
8451                     gnus-newsrc-hashtb)))
8452                  (info (nth 2 entry))
8453                  (article (car articles)))
8454             (gnus-summary-goto-subject article)
8455             (beginning-of-line)
8456             (delete-region (point)
8457                            (progn (forward-line 1) (point)))
8458             (if (not (memq article gnus-newsgroup-unreads))
8459                 (setcar (cdr (cdr info))
8460                         (gnus-add-to-range (nth 2 info) 
8461                                            (list (cdr art-group)))))
8462             ;; Copy any marks over to the new group.
8463             (let ((marks '((tick . gnus-newsgroup-marked)
8464                            (dormant . gnus-newsgroup-dormant)
8465                            (expire . gnus-newsgroup-expirable)
8466                            (bookmark . gnus-newsgroup-bookmarks)
8467                         ;   (score . gnus-newsgroup-scored)
8468                            (reply . gnus-newsgroup-replied)))
8469                   (to-article (cdr art-group)))
8470               (while marks
8471                 (if (memq article (symbol-value (cdr (car marks))))
8472                     (gnus-add-marked-articles 
8473                      (car info) (car (car marks)) (list to-article) info))
8474                 (setq marks (cdr marks))))
8475             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8476             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8477             (setq gnus-newsgroup-dormant
8478                   (delq article gnus-newsgroup-dormant)))
8479         (gnus-message 1 "Couldn't move article %s" (car articles)))
8480       (gnus-summary-remove-process-mark (car articles))
8481       (setq articles (cdr articles)))))
8482
8483 (defun gnus-summary-respool-article (n &optional respool-method)
8484   "Respool the current article.
8485 The article will be squeezed through the mail spooling process again,
8486 which means that it will be put in some mail newsgroup or other
8487 depending on `nnmail-split-methods'.
8488 If N is a positive number, respool the N next articles.
8489 If N is a negative number, respool the N previous articles.
8490 If N is nil and any articles have been marked with the process mark,
8491 respool those articles instead.
8492
8493 Respooling can be done both from mail groups and \"real\" newsgroups.
8494 In the former case, the articles in question will be moved from the
8495 current group into whatever groups they are destined to.  In the
8496 latter case, they will be copied into the relevant groups."
8497   (interactive "P")
8498   (gnus-set-global-variables)
8499   (let ((respool-methods (gnus-methods-using 'respool))
8500         (methname 
8501          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8502     (or respool-method
8503         (setq respool-method
8504               (completing-read
8505                "What method do you want to use when respooling? "
8506                respool-methods nil t methname)))
8507     (or (string= respool-method "")
8508         (if (assoc (symbol-name
8509                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8510                    respool-methods)
8511             (gnus-summary-move-article n nil (intern respool-method))
8512           (gnus-summary-copy-article n nil (intern respool-method))))))
8513
8514 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8515 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8516   "Move the current article to a different newsgroup.
8517 If N is a positive number, move the N next articles.
8518 If N is a negative number, move the N previous articles.
8519 If N is nil and any articles have been marked with the process mark,
8520 move those articles instead.
8521 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8522 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8523 re-spool using this method.
8524 For this function to work, the newsgroup that you want to move to have
8525 to support the `request-move' and `request-accept'
8526 functions. (Ie. mail newsgroups at present.)"
8527   (interactive "P")
8528   (gnus-set-global-variables)
8529   (let ((articles (gnus-summary-work-articles n))
8530         (copy-buf (get-buffer-create "*copy work*"))
8531         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8532         art-group to-method)
8533     (buffer-disable-undo copy-buf)
8534     (if (and (not to-newsgroup) (not select-method))
8535         (setq to-newsgroup
8536               (completing-read 
8537                (format "Where do you want to copy %s? %s"
8538                        (if (> (length articles) 1)
8539                            (format "these %d articles" (length articles))
8540                          "this article")
8541                        (if gnus-current-move-group
8542                            (format "(%s default) " gnus-current-move-group)
8543                          ""))
8544                gnus-active-hashtb nil nil prefix)))
8545     (if to-newsgroup
8546         (progn
8547           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8548               (setq to-newsgroup (or gnus-current-move-group "")))
8549           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8550               (gnus-activate-newsgroup to-newsgroup)
8551               (error "No such group: %s" to-newsgroup))
8552           (setq gnus-current-move-group to-newsgroup)))
8553     (setq to-method (if select-method (list select-method "")
8554                       (gnus-find-method-for-group to-newsgroup)))
8555     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8556         (error "%s does not support article copying" (car to-method)))
8557     (or (gnus-server-opened to-method)
8558         (gnus-open-server to-method)
8559         (error "Can't open server %s" (car to-method)))
8560     (gnus-message 6 "Copying to %s: %s..." 
8561                   (or select-method to-newsgroup) articles)
8562     (while articles
8563       (if (setq art-group
8564                 (save-excursion
8565                   (set-buffer copy-buf)
8566                   (gnus-request-article-this-buffer
8567                    (car articles) gnus-newsgroup-name)
8568                   (gnus-request-accept-article
8569                    (if select-method (quote select-method) to-newsgroup)
8570                    (not (cdr articles)))))
8571           (let* ((entry 
8572                   (or
8573                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8574                    (gnus-gethash 
8575                     (gnus-group-prefixed-name 
8576                      (car art-group) 
8577                      (if select-method (list select-method "")
8578                        (gnus-find-method-for-group to-newsgroup)))
8579                     gnus-newsrc-hashtb)))
8580                  (info (nth 2 entry))
8581                  (article (car articles)))
8582             (if (not (memq article gnus-newsgroup-unreads))
8583                 (setcar (cdr (cdr info))
8584                         (gnus-add-to-range (nth 2 info) 
8585                                            (list (cdr art-group)))))
8586             ;; Copy any marks over to the new group.
8587             (let ((marks '((tick . gnus-newsgroup-marked)
8588                            (dormant . gnus-newsgroup-dormant)
8589                            (expire . gnus-newsgroup-expirable)
8590                            (bookmark . gnus-newsgroup-bookmarks)
8591                         ;   (score . gnus-newsgroup-scored)
8592                            (reply . gnus-newsgroup-replied)))
8593                   (to-article (cdr art-group)))
8594               (while marks
8595                 (if (memq article (symbol-value (cdr (car marks))))
8596                     (gnus-add-marked-articles 
8597                      (car info) (car (car marks)) (list to-article) info))
8598                 (setq marks (cdr marks)))))
8599         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8600       (gnus-summary-remove-process-mark (car articles))
8601       (setq articles (cdr articles)))
8602     (kill-buffer copy-buf)))
8603
8604 (defun gnus-summary-import-article (file)
8605   "Import a random file into a mail newsgroup."
8606   (interactive "fImport file: ")
8607   (let ((group gnus-newsgroup-name)
8608         atts)
8609     (or (gnus-check-backend-function 'request-accept-article group)
8610         (error "%s does not support article importing" group))
8611     (or (file-readable-p file)
8612         (not (file-regular-p file))
8613         (error "Can't read %s" file))
8614     (save-excursion
8615       (set-buffer (get-buffer-create " *import file*"))
8616       (buffer-disable-undo (current-buffer))
8617       (erase-buffer)
8618       (insert-file-contents file)
8619       (goto-char (point-min))
8620       (if (nnheader-article-p)
8621           ()
8622         (setq atts (file-attributes file))
8623         (insert "From: " (read-string "From: ") "\n"
8624                 "Subject: " (read-string "Subject: ") "\n"
8625                 "Date: " (current-time-string (nth 5 atts)) "\n"
8626                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8627       (gnus-request-accept-article group t)
8628       (kill-buffer (current-buffer)))))
8629
8630 (defun gnus-summary-expire-articles ()
8631   "Expire all articles that are marked as expirable in the current group."
8632   (interactive)
8633   (if (and gnus-newsgroup-expirable
8634            (gnus-check-backend-function 
8635             'request-expire-articles gnus-newsgroup-name))
8636       (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8637                                         gnus-newsrc-hashtb)))
8638              (total (memq 'total-expire (nth 5 info)))
8639              (expirable (if total
8640                             (gnus-list-of-read-articles gnus-newsgroup-name)
8641                           (setq gnus-newsgroup-expirable
8642                                 (sort gnus-newsgroup-expirable '<))))
8643              es)
8644         (gnus-message 6 "Expiring articles...")
8645         ;; The list of articles that weren't expired is returned.
8646         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8647         (or total (setq gnus-newsgroup-expirable es))
8648         ;; We go through the old list of expirable, and mark all
8649         ;; really expired articles as non-existent.
8650         (while expirable
8651           (or (memq (car expirable) gnus-newsgroup-expirable)
8652               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8653           (setq expirable (cdr expirable)))
8654         (gnus-message 6 "Expiring articles...done"))))
8655
8656 (defun gnus-summary-expire-articles-now ()
8657   "Expunge all expirable articles in the current group.
8658 This means that *all* articles that are marked as expirable will be
8659 deleted forever, right now."
8660   (interactive)
8661   (or gnus-expert-user
8662       (gnus-y-or-n-p
8663        "Are you really, really, really sure you want to expunge? ")
8664       (error "Phew!"))
8665   (let ((nnmail-expiry-wait -1)
8666         (nnmail-expiry-wait-function nil))
8667     (gnus-summary-expire-articles)))
8668
8669 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8670 (defun gnus-summary-delete-article (n)
8671   "Delete the N next (mail) articles.
8672 This command actually deletes articles. This is not a marking
8673 command. The article will disappear forever from you life, never to
8674 return. 
8675 If N is negative, delete backwards.
8676 If N is nil and articles have been marked with the process mark,
8677 delete these instead."
8678   (interactive "P")
8679   (or (gnus-check-backend-function 'request-expire-articles 
8680                                    gnus-newsgroup-name)
8681       (error "The current newsgroup does not support article deletion."))
8682   ;; Compute the list of articles to delete.
8683   (let ((articles (gnus-summary-work-articles n))
8684         not-deleted)
8685     (if (and gnus-novice-user
8686              (not (gnus-y-or-n-p 
8687                    (format "Do you really want to delete %s forever? "
8688                            (if (> (length articles) 1) "these articles"
8689                              "this article")))))
8690         ()
8691       ;; Delete the articles.
8692       (setq not-deleted (gnus-request-expire-articles 
8693                          articles gnus-newsgroup-name 'force))
8694       (while articles
8695         (gnus-summary-remove-process-mark (car articles))       
8696         ;; The backend might not have been able to delete the article
8697         ;; after all.  
8698         (or (memq (car articles) not-deleted)
8699             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8700         (setq articles (cdr articles))))
8701     (gnus-summary-position-cursor)
8702     not-deleted))
8703
8704 (defun gnus-summary-edit-article ()
8705   "Enter into a buffer and edit the current article.
8706 This will have permanent effect only in mail groups."
8707   (interactive)
8708   (or (gnus-check-backend-function 
8709        'request-replace-article gnus-newsgroup-name)
8710       (error "The current newsgroup does not support article editing."))
8711   (gnus-summary-select-article t)
8712   (gnus-configure-windows 'article)
8713   (select-window (get-buffer-window gnus-article-buffer))
8714   (gnus-message 6 "C-c C-c to end edits")
8715   (setq buffer-read-only nil)
8716   (text-mode)
8717   (use-local-map (copy-keymap (current-local-map)))
8718   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8719   (buffer-enable-undo)
8720   (goto-char (point-min))
8721   (search-forward "\n\n" nil t))
8722
8723 (defun gnus-summary-edit-article-done ()
8724   "Make edits to the current article permanent."
8725   (interactive)
8726   (if (not (gnus-request-replace-article 
8727             (cdr gnus-article-current) (car gnus-article-current) 
8728             (current-buffer)))
8729       (error "Couldn't replace article.")
8730     (gnus-article-mode)
8731     (use-local-map gnus-article-mode-map)
8732     (setq buffer-read-only t)
8733     (buffer-disable-undo (current-buffer))
8734     (pop-to-buffer gnus-summary-buffer)))      
8735
8736 (defun gnus-summary-fancy-query ()
8737   "Query where the fancy respool algorithm would put this article."
8738   (interactive)
8739   (gnus-summary-select-article)
8740   (save-excursion
8741     (set-buffer gnus-article-buffer)
8742     (save-restriction
8743       (goto-char (point-min))
8744       (search-forward "\n\n")
8745       (narrow-to-region (point-min) (point))
8746       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8747
8748 ;; Summary score commands.
8749
8750 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8751
8752 (defun gnus-summary-raise-score (n)
8753   "Raise the score of the current article by N."
8754   (interactive "p")
8755   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8756
8757 (defun gnus-summary-lower-score (n)
8758   "Lower the score of the current article by N."
8759   (interactive "p")
8760   (gnus-summary-raise-score (- n)))
8761
8762 (defun gnus-summary-set-score (n)
8763   "Set the score of the current article to N."
8764   (interactive "p")
8765   ;; Skip dummy header line.
8766   (save-excursion
8767     (gnus-summary-show-thread)
8768     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8769         (forward-line 1))
8770     (let ((buffer-read-only nil))
8771       ;; Set score.
8772       (gnus-summary-update-mark
8773        (if (= n (or gnus-summary-default-score 0)) ? 
8774          (if (< n (or gnus-summary-default-score 0)) 
8775              gnus-score-below-mark gnus-score-over-mark)) 'score))
8776     (let* ((article (gnus-summary-article-number))
8777            (score (assq article gnus-newsgroup-scored)))
8778       (if score (setcdr score n)
8779         (setq gnus-newsgroup-scored 
8780               (cons (cons article n) gnus-newsgroup-scored))))
8781     (gnus-summary-update-line)))
8782
8783 (defun gnus-summary-current-score ()
8784   "Return the score of the current article."
8785   (interactive)
8786   (message "%s" (gnus-summary-article-score)))
8787
8788 ;; Summary marking commands.
8789
8790 (defun gnus-summary-raise-same-subject-and-select (score)
8791   "Raise articles which has the same subject with SCORE and select the next."
8792   (interactive "p")
8793   (let ((subject (gnus-summary-subject-string)))
8794     (gnus-summary-raise-score score)
8795     (while (gnus-summary-search-subject nil nil subject)
8796       (gnus-summary-raise-score score))
8797     (gnus-summary-next-article t)))
8798
8799 (defun gnus-summary-raise-same-subject (score)
8800   "Raise articles which has the same subject with SCORE."
8801   (interactive "p")
8802   (let ((subject (gnus-summary-subject-string)))
8803     (gnus-summary-raise-score score)
8804     (while (gnus-summary-search-subject nil nil subject)
8805       (gnus-summary-raise-score score))
8806     (gnus-summary-next-subject 1 t)))
8807
8808 (defun gnus-score-default (level)
8809   (if level (prefix-numeric-value level) 
8810     gnus-score-interactive-default-score))
8811
8812 (defun gnus-summary-raise-thread (score)
8813   "Raise the score of the articles in the current thread with SCORE."
8814   (interactive "P")
8815   (setq score (1- (gnus-score-default score)))
8816   (let (e)
8817     (save-excursion
8818       (let ((level (gnus-summary-thread-level)))
8819         (gnus-summary-raise-score score)
8820         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8821                     (> (gnus-summary-thread-level) level))
8822           (gnus-summary-raise-score score))
8823         (setq e (point))))
8824     (let ((gnus-summary-check-current t))
8825       (or (zerop (gnus-summary-next-subject 1 t))
8826           (goto-char e))))
8827   (gnus-summary-recenter)
8828   (gnus-summary-position-cursor)
8829   (gnus-set-mode-line 'summary))
8830
8831 (defun gnus-summary-lower-same-subject-and-select (score)
8832   "Raise articles which has the same subject with SCORE and select the next."
8833   (interactive "p")
8834   (gnus-summary-raise-same-subject-and-select (- score)))
8835
8836 (defun gnus-summary-lower-same-subject (score)
8837   "Raise articles which has the same subject with SCORE."
8838   (interactive "p")
8839   (gnus-summary-raise-same-subject (- score)))
8840
8841 (defun gnus-summary-lower-thread (score)
8842   "Lower score of articles in the current thread with SCORE."
8843   (interactive "P")
8844   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8845
8846 (defun gnus-summary-kill-same-subject-and-select (unmark)
8847   "Mark articles which has the same subject as read, and then select the next.
8848 If UNMARK is positive, remove any kind of mark.
8849 If UNMARK is negative, tick articles."
8850   (interactive "P")
8851   (if unmark
8852       (setq unmark (prefix-numeric-value unmark)))
8853   (let ((count
8854          (gnus-summary-mark-same-subject
8855           (gnus-summary-subject-string) unmark)))
8856     ;; Select next unread article. If auto-select-same mode, should
8857     ;; select the first unread article.
8858     (gnus-summary-next-article t (and gnus-auto-select-same
8859                                       (gnus-summary-subject-string)))
8860     (gnus-message 7 "%d articles are marked as %s"
8861                   count (if unmark "unread" "read"))))
8862
8863 (defun gnus-summary-kill-same-subject (unmark)
8864   "Mark articles which has the same subject as read. 
8865 If UNMARK is positive, remove any kind of mark.
8866 If UNMARK is negative, tick articles."
8867   (interactive "P")
8868   (if unmark
8869       (setq unmark (prefix-numeric-value unmark)))
8870   (let ((count
8871          (gnus-summary-mark-same-subject
8872           (gnus-summary-subject-string) unmark)))
8873     ;; If marked as read, go to next unread subject.
8874     (if (null unmark)
8875         ;; Go to next unread subject.
8876         (gnus-summary-next-subject 1 t))
8877     (gnus-message 7 "%d articles are marked as %s"
8878                   count (if unmark "unread" "read"))))
8879
8880 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8881   "Mark articles with same SUBJECT as read, and return marked number.
8882 If optional argument UNMARK is positive, remove any kinds of marks.
8883 If optional argument UNMARK is negative, mark articles as unread instead."
8884   (let ((count 1))
8885     (save-excursion
8886       (cond ((null unmark)
8887              (gnus-summary-mark-as-read nil gnus-killed-mark))
8888             ((> unmark 0)
8889              (gnus-summary-tick-article nil t))
8890             (t
8891              (gnus-summary-tick-article)))
8892       (while (and subject
8893                   (gnus-summary-search-forward nil subject))
8894         (cond ((null unmark)
8895                (gnus-summary-mark-as-read nil gnus-killed-mark))
8896               ((> unmark 0)
8897                (gnus-summary-tick-article nil t))
8898               (t
8899                (gnus-summary-tick-article)))
8900         (setq count (1+ count))))
8901     ;; Hide killed thread subtrees.  Does not work properly always.
8902     ;;(and (null unmark)
8903     ;;     gnus-thread-hide-killed
8904     ;;     (gnus-summary-hide-thread))
8905     ;; Return number of articles marked as read.
8906     count))
8907
8908 (defun gnus-summary-mark-as-processable (n &optional unmark)
8909   "Set the process mark on the next N articles.
8910 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8911 the process mark instead.  The difference between N and the actual
8912 number of articles marked is returned."
8913   (interactive "p")
8914   (let ((backward (< n 0))
8915         (n (abs n)))
8916   (while (and 
8917           (> n 0)
8918           (if unmark
8919               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8920             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8921           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8922     (setq n (1- n)))
8923   (if (/= 0 n) (gnus-message 7 "No more articles"))
8924   (gnus-summary-recenter)
8925   (gnus-summary-position-cursor)
8926   n))
8927
8928 (defun gnus-summary-unmark-as-processable (n)
8929   "Remove the process mark from the next N articles.
8930 If N is negative, mark backward instead.  The difference between N and
8931 the actual number of articles marked is returned."
8932   (interactive "p")
8933   (gnus-summary-mark-as-processable n t))
8934
8935 (defun gnus-summary-unmark-all-processable ()
8936   "Remove the process mark from all articles."
8937   (interactive)
8938   (save-excursion
8939     (while gnus-newsgroup-processable
8940       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8941   (gnus-summary-position-cursor))
8942
8943 (defun gnus-summary-mark-as-expirable (n)
8944   "Mark N articles forward as expirable.
8945 If N is negative, mark backward instead. The difference between N and
8946 the actual number of articles marked is returned."
8947   (interactive "p")
8948   (gnus-summary-mark-forward n gnus-expirable-mark))
8949
8950 (defun gnus-summary-mark-article-as-replied (article)
8951   "Mark ARTICLE replied and update the summary line."
8952   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8953   (let ((buffer-read-only nil))
8954     (if (gnus-summary-goto-subject article)
8955         (progn
8956           (gnus-summary-update-mark gnus-replied-mark 'replied)
8957           t))))
8958
8959 (defun gnus-summary-set-bookmark (article)
8960   "Set a bookmark in current article."
8961   (interactive (list (gnus-summary-article-number)))
8962   (if (or (not (get-buffer gnus-article-buffer))
8963           (not gnus-current-article)
8964           (not gnus-article-current)
8965           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8966       (error "No current article selected"))
8967   ;; Remove old bookmark, if one exists.
8968   (let ((old (assq article gnus-newsgroup-bookmarks)))
8969     (if old (setq gnus-newsgroup-bookmarks 
8970                   (delq old gnus-newsgroup-bookmarks))))
8971   ;; Set the new bookmark, which is on the form 
8972   ;; (article-number . line-number-in-body).
8973   (setq gnus-newsgroup-bookmarks 
8974         (cons 
8975          (cons article 
8976                (save-excursion
8977                  (set-buffer gnus-article-buffer)
8978                  (count-lines
8979                   (min (point)
8980                        (save-excursion
8981                          (goto-char (point-min))
8982                          (search-forward "\n\n" nil t)
8983                          (point)))
8984                   (point))))
8985          gnus-newsgroup-bookmarks))
8986   (gnus-message 6 "A bookmark has been added to the current article."))
8987
8988 (defun gnus-summary-remove-bookmark (article)
8989   "Remove the bookmark from the current article."
8990   (interactive (list (gnus-summary-article-number)))
8991   ;; Remove old bookmark, if one exists.
8992   (let ((old (assq article gnus-newsgroup-bookmarks)))
8993     (if old 
8994         (progn
8995           (setq gnus-newsgroup-bookmarks 
8996                 (delq old gnus-newsgroup-bookmarks))
8997           (gnus-message 6 "Removed bookmark."))
8998       (gnus-message 6 "No bookmark in current article."))))
8999
9000 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9001 (defun gnus-summary-mark-as-dormant (n)
9002   "Mark N articles forward as dormant.
9003 If N is negative, mark backward instead.  The difference between N and
9004 the actual number of articles marked is returned."
9005   (interactive "p")
9006   (gnus-summary-mark-forward n gnus-dormant-mark))
9007
9008 (defun gnus-summary-set-process-mark (article)
9009   "Set the process mark on ARTICLE and update the summary line."
9010   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9011   (let ((buffer-read-only nil))
9012     (if (gnus-summary-goto-subject article)
9013         (progn
9014           (gnus-summary-show-thread)
9015           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9016                (forward-line 1))
9017           (gnus-summary-update-mark gnus-process-mark 'replied)
9018           t))))
9019
9020 (defun gnus-summary-remove-process-mark (article)
9021   "Remove the process mark from ARTICLE and update the summary line."
9022   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9023   (let ((buffer-read-only nil))
9024     (if (gnus-summary-goto-subject article)
9025         (progn
9026           (gnus-summary-show-thread)
9027           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9028                (forward-line 1))
9029           (gnus-summary-update-mark ?  'replied)
9030           (if (memq article gnus-newsgroup-replied) 
9031               (gnus-summary-update-mark gnus-replied-mark 'replied))
9032           t))))
9033
9034 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9035   "Mark N articles as read forwards.
9036 If N is negative, mark backwards instead.
9037 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9038 marked as unread. 
9039 The difference between N and the actual number of articles marked is
9040 returned."
9041   (interactive "p")
9042   (gnus-set-global-variables)
9043   (let ((backward (< n 0))
9044         (n (abs n))
9045         (mark (or mark gnus-del-mark)))
9046   (while (and (> n 0)
9047               (gnus-summary-mark-article nil mark no-expire)
9048               (zerop (gnus-summary-next-subject 
9049                       (if backward -1 1) gnus-summary-goto-unread t)))
9050     (setq n (1- n)))
9051   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9052   (gnus-summary-recenter)
9053   (gnus-summary-position-cursor)
9054   (gnus-set-mode-line 'summary)
9055   n))
9056
9057 (defun gnus-summary-mark-article (&optional article mark no-expire)
9058   "Mark ARTICLE with MARK.
9059 MARK can be any character.
9060 Five MARK strings are reserved: ?  (unread), 
9061 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
9062 If MARK is nil, then the default character ?D is used.
9063 If ARTICLE is nil, then the article on the current line will be
9064 marked." 
9065   ;; If no mark is given, then we check auto-expiring.
9066   (and (not no-expire)
9067        gnus-newsgroup-auto-expire 
9068        (or (not mark)
9069            (and (numberp mark) 
9070                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9071                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9072                     (= mark gnus-read-mark))))
9073        (setq mark gnus-expirable-mark))
9074   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
9075          (article (or article (gnus-summary-article-number))))
9076     (or article (error "No article on current line"))
9077     (if (or (= mark gnus-unread-mark) 
9078             (= mark gnus-ticked-mark) 
9079             (= mark gnus-dormant-mark))
9080         (gnus-mark-article-as-unread article mark)
9081       (gnus-mark-article-as-read article mark))
9082
9083     ;; See whether the article is to be put in the cache.
9084     (and gnus-use-cache
9085          (not (= mark gnus-canceled-mark))
9086          (save-excursion
9087            (gnus-cache-possibly-enter-article 
9088             gnus-newsgroup-name article 
9089             (gnus-get-header-by-number article)
9090             (= mark gnus-ticked-mark)
9091             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9092
9093     (if (gnus-summary-goto-subject article)
9094         (let ((buffer-read-only nil))
9095           (gnus-summary-show-thread)
9096           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9097                (forward-line 1))
9098           ;; Fix the mark.
9099           (gnus-summary-update-mark mark 'unread)
9100           t))))
9101
9102 (defun gnus-summary-update-mark (mark type)
9103   (beginning-of-line)
9104   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9105         plist)
9106     (if (not forward)
9107         ()
9108       (forward-char forward)
9109       (setq plist (text-properties-at (point)))
9110       (delete-char 1)
9111       (insert mark)
9112       (and plist (add-text-properties (1- (point)) (point) plist))
9113       (and (eq type 'unread)
9114            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9115       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9116   
9117 (defun gnus-mark-article-as-read (article &optional mark)
9118   "Enter ARTICLE in the pertinent lists and remove it from others."
9119   ;; Make the article expirable.
9120   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
9121     (if (= mark gnus-expirable-mark)
9122         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9123       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9124     ;; Remove from unread and marked lists.
9125     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9126     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9127     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9128     ;; Possibly remove from cache, if that is used. 
9129     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9130
9131 (defun gnus-mark-article-as-unread (article &optional mark)
9132   "Enter ARTICLE in the pertinent lists and remove it from others."
9133   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
9134     ;; Add to unread list.
9135     (or (memq article gnus-newsgroup-unreads)
9136         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9137     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
9138     ;; lists.  Otherwise, it must be added to the list.
9139     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9140     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9141     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9142     (if (= mark gnus-ticked-mark)
9143         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9144     (if (= mark gnus-dormant-mark)
9145         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9146
9147 (defalias 'gnus-summary-mark-as-unread-forward 
9148   'gnus-summary-tick-article-forward)
9149 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9150                'gnus-summary-tick-article-forward)
9151 (defun gnus-summary-tick-article-forward (n)
9152   "Tick N articles forwards.
9153 If N is negative, tick backwards instead.
9154 The difference between N and the number of articles ticked is returned."
9155   (interactive "p")
9156   (gnus-summary-mark-forward n gnus-ticked-mark))
9157
9158 (defalias 'gnus-summary-mark-as-unread-backward 
9159   'gnus-summary-tick-article-backward)
9160 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9161                'gnus-summary-tick-article-backward)
9162 (defun gnus-summary-tick-article-backward (n)
9163   "Tick N articles backwards.
9164 The difference between N and the number of articles ticked is returned."
9165   (interactive "p")
9166   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9167
9168 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9169 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9170 (defun gnus-summary-tick-article (&optional article clear-mark)
9171   "Mark current article as unread.
9172 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9173 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9174   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9175                                        gnus-ticked-mark)))
9176
9177 (defun gnus-summary-mark-as-read-forward (n)
9178   "Mark N articles as read forwards.
9179 If N is negative, mark backwards instead.
9180 The difference between N and the actual number of articles marked is
9181 returned."
9182   (interactive "p")
9183   (gnus-summary-mark-forward n gnus-del-mark t))
9184
9185 (defun gnus-summary-mark-as-read-backward (n)
9186   "Mark the N articles as read backwards.
9187 The difference between N and the actual number of articles marked is
9188 returned."
9189   (interactive "p")
9190   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9191
9192 (defun gnus-summary-mark-as-read (&optional article mark)
9193   "Mark current article as read.
9194 ARTICLE specifies the article to be marked as read.
9195 MARK specifies a string to be inserted at the beginning of the line."
9196   (gnus-summary-mark-article article mark))
9197
9198 (defun gnus-summary-clear-mark-forward (n)
9199   "Clear marks from N articles forward.
9200 If N is negative, clear backward instead.
9201 The difference between N and the number of marks cleared is returned."
9202   (interactive "p")
9203   (gnus-summary-mark-forward n gnus-unread-mark))
9204
9205 (defun gnus-summary-clear-mark-backward (n)
9206   "Clear marks from N articles backward.
9207 The difference between N and the number of marks cleared is returned."
9208   (interactive "p")
9209   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9210
9211 (defun gnus-summary-mark-unread-as-read ()
9212   "Intended to be used by `gnus-summary-mark-article-hook'."
9213   (or (memq gnus-current-article gnus-newsgroup-marked)
9214       (memq gnus-current-article gnus-newsgroup-dormant)
9215       (memq gnus-current-article gnus-newsgroup-expirable)
9216       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
9217
9218 (defun gnus-summary-mark-region-as-read (point mark all)
9219   "Mark all unread articles between point and mark as read.
9220 If given a prefix, mark all articles between point and mark as read,
9221 even ticked and dormant ones."
9222   (interactive "r\nP")
9223   (save-excursion
9224     (goto-char point)
9225     (beginning-of-line)
9226     (while (and 
9227             (< (point) mark)
9228             (progn
9229               (and
9230                (or all
9231                    (and
9232                     (not (memq (gnus-summary-article-number)
9233                                gnus-newsgroup-marked))
9234                     (not (memq (gnus-summary-article-number)
9235                                gnus-newsgroup-dormant))))
9236                (gnus-summary-mark-article
9237                 (gnus-summary-article-number) gnus-del-mark))
9238               t)
9239             (zerop (forward-line 1))))))
9240
9241 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9242 (defalias 'gnus-summary-delete-marked-as-read 
9243   'gnus-summary-remove-lines-marked-as-read)
9244 (make-obsolete 'gnus-summary-delete-marked-as-read 
9245                'gnus-summary-remove-lines-marked-as-read)
9246 (defun gnus-summary-remove-lines-marked-as-read ()
9247   "Remove lines that are marked as read."
9248   (interactive)
9249   (gnus-summary-remove-lines-marked-with 
9250    (concat (mapconcat
9251             (lambda (char) (char-to-string (symbol-value char)))
9252             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9253               gnus-killed-mark gnus-kill-file-mark
9254               gnus-low-score-mark gnus-expirable-mark
9255               gnus-canceled-mark)
9256             ""))))
9257
9258 (defalias 'gnus-summary-delete-marked-with 
9259   'gnus-summary-remove-lines-marked-with)
9260 (make-obsolete 'gnus-summary-delete-marked-with 
9261                'gnus-summary-remove-lines-marked-with)
9262 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9263 (defun gnus-summary-remove-lines-marked-with (marks)
9264   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9265   (interactive "sMarks: ")
9266   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9267   (gnus-set-global-variables)
9268   (let ((buffer-read-only nil)
9269         (marks (concat "^[" marks "]")))
9270     (goto-char (point-min))
9271     (if gnus-newsgroup-adaptive
9272         (gnus-score-remove-lines-adaptive marks)
9273       (while (re-search-forward marks nil t)
9274         (gnus-delete-line)))
9275     ;; If we use dummy roots, we have to do an additional sweep over
9276     ;; the buffer.
9277     (if (not (eq gnus-summary-make-false-root 'dummy))
9278         ()
9279       (goto-char (point-min))
9280       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9281       (while (re-search-forward marks nil t)
9282         (if (gnus-subject-equal
9283              (gnus-summary-subject-string)
9284              (progn
9285                (forward-line 1)
9286                (gnus-summary-subject-string)))
9287             ()
9288           (forward-line -1)
9289           (gnus-delete-line)))))
9290   (or (zerop (buffer-size))
9291       (if (eobp)
9292           (gnus-summary-prev-subject 1)
9293         (gnus-summary-position-cursor))))
9294
9295 (defun gnus-summary-expunge-below (score)
9296   "Remove articles with score less than SCORE."
9297   (interactive "P")
9298   (gnus-set-global-variables)
9299   (setq score (if score
9300                   (prefix-numeric-value score)
9301                 (or gnus-summary-default-score 0)))
9302   (save-excursion
9303     (set-buffer gnus-summary-buffer)
9304     (goto-char (point-min))
9305     (let ((buffer-read-only nil)
9306           beg)
9307       (while (not (eobp))
9308         (if (< (gnus-summary-article-score) score)
9309             (progn
9310               (setq beg (point))
9311               (forward-line 1)
9312               (delete-region beg (point)))
9313           (forward-line 1)))
9314       ;; Adjust point.
9315       (or (zerop (buffer-size))
9316           (if (eobp)
9317               (gnus-summary-prev-subject 1)
9318             (gnus-summary-position-cursor))))))
9319
9320 (defun gnus-summary-mark-below (score mark)
9321   "Mark articles with score less than SCORE with MARK."
9322   (interactive "P\ncMark: ")
9323   (gnus-set-global-variables)
9324   (setq score (if score
9325                   (prefix-numeric-value score)
9326                 (or gnus-summary-default-score 0)))
9327   (save-excursion
9328     (set-buffer gnus-summary-buffer)
9329     (goto-char (point-min))
9330     (while (not (eobp))
9331       (and (< (gnus-summary-article-score) score)
9332            (gnus-summary-mark-article nil mark))
9333       (forward-line 1))))
9334
9335 (defun gnus-summary-kill-below (score)
9336   "Mark articles with score below SCORE as read."
9337   (interactive "P")
9338   (gnus-summary-mark-below score gnus-killed-mark))
9339
9340 (defun gnus-summary-clear-above (score)
9341   "Clear all marks from articles with score above SCORE."
9342   (interactive "P")
9343   (gnus-summary-mark-above score gnus-unread-mark))
9344
9345 (defun gnus-summary-tick-above (score)
9346   "Tick all articles with score above SCORE."
9347   (interactive "P")
9348   (gnus-summary-mark-above score gnus-ticked-mark))
9349
9350 (defun gnus-summary-mark-above (score mark)
9351   "Mark articles with score over SCORE with MARK."
9352   (interactive "P\ncMark: ")
9353   (setq score (if score
9354                   (prefix-numeric-value score)
9355                 (or gnus-summary-default-score 0)))
9356   (save-excursion
9357     (set-buffer gnus-summary-buffer)
9358     (goto-char (point-min))
9359     (while (not (eobp))
9360       (if (> (gnus-summary-article-score) score)
9361           (progn
9362             (gnus-summary-mark-article nil mark)
9363             (forward-line 1))
9364         (forward-line 1)))))
9365
9366 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9367 (defun gnus-summary-show-all-expunged ()
9368   "Display all the hidden articles that were expunged for low scores."
9369   (interactive)
9370   (let ((buffer-read-only nil))
9371     (let ((scored gnus-newsgroup-scored)
9372           headers h)
9373       (while scored
9374         (or (gnus-summary-goto-subject (car (car scored)))
9375             (and (setq h (gnus-get-header-by-number (car (car scored))))
9376                  (< (cdr (car scored)) gnus-summary-expunge-below)
9377                  (setq headers (cons h headers))))
9378         (setq scored (cdr scored)))
9379       (or headers (error "No expunged articles hidden."))
9380       (goto-char (point-min))
9381       (save-excursion 
9382         (gnus-summary-update-lines 
9383          (point)
9384          (progn
9385            (gnus-summary-prepare-threads (nreverse headers))
9386            (point)))))
9387     (goto-char (point-min))
9388     (gnus-summary-position-cursor)))
9389
9390 (defun gnus-summary-show-all-dormant ()
9391   "Display all the hidden articles that are marked as dormant."
9392   (interactive)
9393   (let ((buffer-read-only nil))
9394     (let ((dormant gnus-newsgroup-dormant)
9395           headers h)
9396       (while dormant
9397         (or (gnus-summary-goto-subject (car dormant))
9398             (and (setq h (gnus-get-header-by-number (car dormant)))
9399                  (setq headers (cons h headers))))
9400         (setq dormant (cdr dormant)))
9401       (or headers (error "No dormant articles hidden."))
9402       (goto-char (point-min))
9403       (save-excursion 
9404         (gnus-summary-update-lines 
9405          (point)
9406          (progn
9407            (gnus-summary-prepare-threads (nreverse headers))
9408            (point)))))
9409     (goto-char (point-min))
9410     (gnus-summary-position-cursor)))
9411
9412 (defun gnus-summary-hide-all-dormant ()
9413   "Hide all dormant articles."
9414   (interactive)
9415   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9416   (gnus-summary-position-cursor))
9417
9418 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9419   "Mark all articles not marked as unread in this newsgroup as read.
9420 If prefix argument ALL is non-nil, all articles are marked as read.
9421 If QUIETLY is non-nil, no questions will be asked.
9422 If TO-HERE is non-nil, it should be a point in the buffer. All
9423 articles before this point will be marked as read.
9424 The number of articles marked as read is returned."
9425   (interactive "P")
9426   (prog1
9427       (if (or quietly
9428               (not gnus-interactive-catchup) ;Without confirmation?
9429               gnus-expert-user
9430               (gnus-y-or-n-p
9431                (if all
9432                    "Mark absolutely all articles as read? "
9433                  "Mark all unread articles as read? ")))
9434           (if (and not-mark 
9435                    (not gnus-newsgroup-adaptive)
9436                    (not gnus-newsgroup-auto-expire))
9437               (progn
9438                 (and all (setq gnus-newsgroup-marked nil
9439                                gnus-newsgroup-dormant nil))
9440                 (setq gnus-newsgroup-unreads 
9441                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9442             ;; We actually mark all articles as canceled, which we
9443             ;; have to do when using auto-expiry or adaptive scoring. 
9444             (let ((unreads (length gnus-newsgroup-unreads)))
9445               (if (gnus-summary-first-subject (not all))
9446                   (while (and (if to-here (< (point) to-here) t)
9447                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9448                               (gnus-summary-search-subject nil (not all)))))
9449               (- unreads (length gnus-newsgroup-unreads))
9450               (or to-here
9451                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9452     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9453       (if (and (not to-here) (eq 'nnvirtual (car method)))
9454           (nnvirtual-catchup-group
9455            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9456     (gnus-summary-position-cursor)))
9457
9458 (defun gnus-summary-catchup-to-here (&optional all)
9459   "Mark all unticked articles before the current one as read.
9460 If ALL is non-nil, also mark ticked and dormant articles as read."
9461   (interactive)
9462   (save-excursion
9463     (and (zerop (forward-line -1))
9464          (progn
9465            (end-of-line)
9466            (gnus-summary-catchup all t (point))
9467            (gnus-set-mode-line 'summary))))
9468   (gnus-summary-position-cursor))
9469
9470 (defun gnus-summary-catchup-all (&optional quietly)
9471   "Mark all articles in this newsgroup as read."
9472   (interactive)
9473   (gnus-summary-catchup t quietly))
9474
9475 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9476   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9477 If prefix argument ALL is non-nil, all articles are marked as read."
9478   (interactive "P")
9479   (gnus-summary-catchup all quietly nil 'fast)
9480   ;; Select next newsgroup or exit.
9481   (if (eq gnus-auto-select-next 'quietly)
9482       (gnus-summary-next-group nil)
9483     (gnus-summary-exit)))
9484
9485 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9486   "Mark all articles in this newsgroup as read, and then exit."
9487   (interactive)
9488   (gnus-summary-catchup-and-exit t quietly))
9489
9490 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9491 (defun gnus-summary-catchup-and-goto-next-group (all)
9492   "Mark all articles in this group as read and select the next group.
9493 If given a prefix, mark all articles, unread as well as ticked, as
9494 read." 
9495   (interactive "P")
9496   (gnus-summary-catchup all)
9497   (gnus-summary-next-group))
9498
9499 ;; Thread-based commands.
9500
9501 (defun gnus-summary-toggle-threads (arg)
9502   "Toggle showing conversation threads.
9503 If ARG is positive number, turn showing conversation threads on."
9504   (interactive "P")
9505   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9506     (setq gnus-show-threads
9507           (if (null arg) (not gnus-show-threads)
9508             (> (prefix-numeric-value arg) 0)))
9509     (gnus-summary-prepare)
9510     (gnus-summary-goto-subject current)
9511     (gnus-summary-position-cursor)))
9512
9513 (defun gnus-summary-show-all-threads ()
9514   "Show all threads."
9515   (interactive)
9516   (save-excursion
9517     (let ((buffer-read-only nil))
9518       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9519   (gnus-summary-position-cursor))
9520
9521 (defun gnus-summary-show-thread ()
9522   "Show thread subtrees.
9523 Returns nil if no thread was there to be shown."
9524   (interactive)
9525   (prog1
9526       (save-excursion
9527         (let ((buffer-read-only nil)
9528               ;; first goto end then to beg, to have point at beg after let
9529               (end (progn (end-of-line) (point)))
9530               (beg (progn (beginning-of-line) (point))))
9531           (prog1
9532               ;; Any hidden lines here?
9533               (search-forward "\r" end t)
9534             (subst-char-in-region beg end ?\^M ?\n t))))
9535     (gnus-summary-position-cursor)))
9536
9537 (defun gnus-summary-hide-all-threads ()
9538   "Hide all thread subtrees."
9539   (interactive)
9540   (save-excursion
9541     (goto-char (point-min))
9542     (gnus-summary-hide-thread)
9543     (while (and (not (eobp)) (zerop (forward-line 1)))
9544       (gnus-summary-hide-thread)))
9545   (gnus-summary-position-cursor))
9546
9547 (defun gnus-summary-hide-thread ()
9548   "Hide thread subtrees.
9549 Returns nil if no threads were there to be hidden."
9550   (interactive)
9551   (let ((buffer-read-only nil)
9552         (start (point))
9553         (level (gnus-summary-thread-level))
9554         (end (point)))
9555     ;; Go forward until either the buffer ends or the subthread
9556     ;; ends. 
9557     (if (eobp)
9558         ()
9559       (while (and (zerop (forward-line 1))
9560                   (> (gnus-summary-thread-level) level))
9561         (setq end (point)))
9562       (prog1
9563           (save-excursion
9564             (goto-char end)
9565             (search-backward "\n" start t))
9566         (subst-char-in-region start end ?\n ?\^M t)
9567         (forward-line -1)))))
9568
9569 (defun gnus-summary-go-to-next-thread (&optional previous)
9570   "Go to the same level (or less) next thread.
9571 If PREVIOUS is non-nil, go to previous thread instead.
9572 Return the article number moved to, or nil if moving was impossible."
9573   (let ((level (gnus-summary-thread-level))
9574         (article (gnus-summary-article-number)))
9575     (if previous 
9576         (while (and (zerop (forward-line -1))
9577                     (> (gnus-summary-thread-level) level)))
9578       (while (and (save-excursion
9579                     (forward-line 1)
9580                     (not (eobp)))
9581                   (zerop (forward-line 1))
9582                   (> (gnus-summary-thread-level) level))))
9583     (gnus-summary-recenter)
9584     (gnus-summary-position-cursor)
9585     (let ((oart (gnus-summary-article-number)))
9586       (and (/= oart article) oart))))
9587
9588 (defun gnus-summary-next-thread (n)
9589   "Go to the same level next N'th thread.
9590 If N is negative, search backward instead.
9591 Returns the difference between N and the number of skips actually
9592 done."
9593   (interactive "p")
9594   (let ((backward (< n 0))
9595         (n (abs n)))
9596   (while (and (> n 0)
9597               (gnus-summary-go-to-next-thread backward))
9598     (setq n (1- n)))
9599   (gnus-summary-position-cursor)
9600   (if (/= 0 n) (gnus-message 7 "No more threads"))
9601   n))
9602
9603 (defun gnus-summary-prev-thread (n)
9604   "Go to the same level previous N'th thread.
9605 Returns the difference between N and the number of skips actually
9606 done."
9607   (interactive "p")
9608   (gnus-summary-next-thread (- n)))
9609
9610 (defun gnus-summary-go-down-thread (&optional same)
9611   "Go down one level in the current thread.
9612 If SAME is non-nil, also move to articles of the same level."
9613   (let ((level (gnus-summary-thread-level))
9614         (start (point)))
9615     (if (and (zerop (forward-line 1))
9616              (> (gnus-summary-thread-level) level))
9617         t
9618       (goto-char start)
9619       nil)))
9620
9621 (defun gnus-summary-go-up-thread ()
9622   "Go up one level in the current thread."
9623   (let ((level (gnus-summary-thread-level))
9624         (start (point)))
9625     (while (and (zerop (forward-line -1))
9626                 (>= (gnus-summary-thread-level) level)))
9627     (if (>= (gnus-summary-thread-level) level)
9628         (progn
9629           (goto-char start)
9630           nil)
9631       t)))
9632
9633 (defun gnus-summary-down-thread (n)
9634   "Go down thread N steps.
9635 If N is negative, go up instead.
9636 Returns the difference between N and how many steps down that were
9637 taken."
9638   (interactive "p")
9639   (let ((up (< n 0))
9640         (n (abs n)))
9641   (while (and (> n 0)
9642               (if up (gnus-summary-go-up-thread)
9643                 (gnus-summary-go-down-thread)))
9644     (setq n (1- n)))
9645   (gnus-summary-position-cursor)
9646   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9647   n))
9648
9649 (defun gnus-summary-up-thread (n)
9650   "Go up thread N steps.
9651 If N is negative, go up instead.
9652 Returns the difference between N and how many steps down that were
9653 taken."
9654   (interactive "p")
9655   (gnus-summary-down-thread (- n)))
9656
9657 (defun gnus-summary-kill-thread (unmark)
9658   "Mark articles under current thread as read.
9659 If the prefix argument is positive, remove any kinds of marks.
9660 If the prefix argument is negative, tick articles instead."
9661   (interactive "P")
9662   (if unmark
9663       (setq unmark (prefix-numeric-value unmark)))
9664   (let ((killing t)
9665         (level (gnus-summary-thread-level)))
9666     (save-excursion
9667       (while killing
9668         ;; Mark the article...
9669         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9670               ((> unmark 0) (gnus-summary-tick-article nil t))
9671               (t (gnus-summary-tick-article)))
9672         ;; ...and go forward until either the buffer ends or the subtree
9673         ;; ends. 
9674         (if (not (and (zerop (forward-line 1))
9675                       (> (gnus-summary-thread-level) level)))
9676             (setq killing nil))))
9677     ;; Hide killed subtrees.
9678     (and (null unmark)
9679          gnus-thread-hide-killed
9680          (gnus-summary-hide-thread))
9681     ;; If marked as read, go to next unread subject.
9682     (if (null unmark)
9683         ;; Go to next unread subject.
9684         (gnus-summary-next-subject 1 t)))
9685   (gnus-set-mode-line 'summary))
9686
9687 ;; Summary sorting commands
9688
9689 (defun gnus-summary-sort-by-number (&optional reverse)
9690   "Sort summary buffer by article number.
9691 Argument REVERSE means reverse order."
9692   (interactive "P")
9693   (gnus-summary-sort 
9694    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9695
9696 (defun gnus-summary-sort-by-author (&optional reverse)
9697   "Sort summary buffer by author name alphabetically.
9698 If case-fold-search is non-nil, case of letters is ignored.
9699 Argument REVERSE means reverse order."
9700   (interactive "P")
9701   (gnus-summary-sort
9702    (cons
9703     (lambda ()
9704       (let ((extract (funcall
9705                       gnus-extract-address-components
9706                       (header-from (gnus-get-header-by-number
9707                                     (gnus-summary-article-number))))))
9708         (or (car extract) (cdr extract))))
9709     'gnus-thread-sort-by-author)
9710    reverse))
9711
9712 (defun gnus-summary-sort-by-subject (&optional reverse)
9713   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9714 If case-fold-search is non-nil, case of letters is ignored.
9715 Argument REVERSE means reverse order."
9716   (interactive "P")
9717   (gnus-summary-sort
9718    (cons
9719     (lambda ()
9720       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9721     'gnus-thread-sort-by-subject)
9722    reverse))
9723
9724 (defun gnus-summary-sort-by-date (&optional reverse)
9725   "Sort summary buffer by date.
9726 Argument REVERSE means reverse order."
9727   (interactive "P")
9728   (gnus-summary-sort
9729    (cons
9730     (lambda ()
9731       (gnus-sortable-date
9732        (header-date 
9733         (gnus-get-header-by-number (gnus-summary-article-number)))))
9734     'gnus-thread-sort-by-date)
9735    reverse))
9736
9737 (defun gnus-summary-sort-by-score (&optional reverse)
9738   "Sort summary buffer by score.
9739 Argument REVERSE means reverse order."
9740   (interactive "P")
9741   (gnus-summary-sort 
9742    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9743    (not reverse)))
9744
9745 (defvar gnus-summary-already-sorted nil)
9746 (defun gnus-summary-sort (predicate reverse)
9747   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9748   (if gnus-summary-already-sorted
9749       ()
9750     (let (buffer-read-only)
9751       (if (not gnus-show-threads)
9752           (progn
9753             (goto-char (point-min))
9754             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9755         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9756               (gnus-summary-prepare-hook nil)
9757               (gnus-summary-already-sorted nil))
9758           (gnus-summary-prepare)
9759           (and gnus-show-threads
9760                gnus-thread-hide-subtree
9761                (gnus-summary-hide-all-threads))
9762           ;; If in async mode, we send some info to the backend.
9763           (and gnus-newsgroup-async
9764                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9765                (gnus-request-asynchronous 
9766                 gnus-newsgroup-name
9767                 (if (and gnus-asynchronous-article-function
9768                          (fboundp gnus-asynchronous-article-function))
9769                     (funcall gnus-asynchronous-article-function
9770                              gnus-newsgroup-threads)))))))))
9771
9772   
9773 (defun gnus-sortable-date (date)
9774   "Make sortable string by string-lessp from DATE.
9775 Timezone package is used."
9776   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9777          (year   (aref date 0))
9778          (month  (aref date 1))
9779          (day    (aref date 2)))
9780     (timezone-make-sortable-date 
9781      year month day 
9782      (timezone-make-time-string
9783       (aref date 3) (aref date 4) (aref date 5)))))
9784
9785
9786 ;; Summary saving commands.
9787
9788 (defun gnus-summary-save-article (n)
9789   "Save the current article using the default saver function.
9790 If N is a positive number, save the N next articles.
9791 If N is a negative number, save the N previous articles.
9792 If N is nil and any articles have been marked with the process mark,
9793 save those articles instead.
9794 The variable `gnus-default-article-saver' specifies the saver function."
9795   (interactive "P")
9796   (let ((articles (gnus-summary-work-articles n)))
9797     (while articles
9798       (let ((header (gnus-get-header-by-number (car articles))))
9799         (if (vectorp header)
9800             (progn
9801               (gnus-summary-select-article t nil nil (car articles))
9802               (or gnus-save-all-headers
9803                   (gnus-article-hide-headers t))
9804               ;; Remove any X-Gnus lines.
9805               (save-excursion
9806                 (save-restriction
9807                   (set-buffer gnus-article-buffer)
9808                   (let ((buffer-read-only nil))
9809                     (goto-char (point-min))
9810                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9811                                                   (point-max)))
9812                     (while (re-search-forward "^X-Gnus" nil t)
9813                       (beginning-of-line)
9814                       (delete-region (point)
9815                                      (progn (forward-line 1) (point))))
9816                     (widen))))
9817               (save-excursion
9818                 (if gnus-default-article-saver
9819                     (funcall gnus-default-article-saver)
9820                   (error "No default saver is defined."))))
9821           (if (assq 'name header)
9822               (gnus-copy-file (cdr (assq 'name header)))
9823             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9824       (gnus-summary-remove-process-mark (car articles))
9825       (setq articles (cdr articles)))
9826     (gnus-summary-position-cursor)
9827     n))
9828
9829 (defun gnus-summary-pipe-output (arg)
9830   "Pipe the current article to a subprocess.
9831 If N is a positive number, pipe the N next articles.
9832 If N is a negative number, pipe the N previous articles.
9833 If N is nil and any articles have been marked with the process mark,
9834 pipe those articles instead."
9835   (interactive "P")
9836   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9837     (gnus-summary-save-article arg)))
9838
9839 (defun gnus-summary-save-article-mail (arg)
9840   "Append the current article to an mail file.
9841 If N is a positive number, save the N next articles.
9842 If N is a negative number, save the N previous articles.
9843 If N is nil and any articles have been marked with the process mark,
9844 save those articles instead."
9845   (interactive "P")
9846   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9847     (gnus-summary-save-article arg)))
9848
9849 (defun gnus-summary-save-article-rmail (arg)
9850   "Append the current article to an rmail file.
9851 If N is a positive number, save the N next articles.
9852 If N is a negative number, save the N previous articles.
9853 If N is nil and any articles have been marked with the process mark,
9854 save those articles instead."
9855   (interactive "P")
9856   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9857     (gnus-summary-save-article arg)))
9858
9859 (defun gnus-summary-save-article-file (arg)
9860   "Append the current article to a file.
9861 If N is a positive number, save the N next articles.
9862 If N is a negative number, save the N previous articles.
9863 If N is nil and any articles have been marked with the process mark,
9864 save those articles instead."
9865   (interactive "P")
9866   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9867     (gnus-summary-save-article arg)))
9868
9869 (defun gnus-read-save-file-name (prompt default-name)
9870   (let ((methods gnus-split-methods)
9871         split-name)
9872     (if (not gnus-split-methods)
9873         ()
9874       (save-excursion
9875         (set-buffer gnus-article-buffer)
9876         (gnus-narrow-to-headers)
9877         (while methods
9878           (goto-char (point-min))
9879           (and (condition-case () 
9880                    (re-search-forward (car (car methods)) nil t)
9881                  (error nil))
9882                (setq split-name (cons (nth 1 (car methods)) split-name)))
9883           (setq methods (cdr methods)))
9884         (widen)))
9885     (cond ((null split-name)
9886            (read-file-name
9887             (concat prompt " (default "
9888                     (file-name-nondirectory default-name) ") ")
9889             (file-name-directory default-name)
9890             default-name))
9891           ((= 1 (length split-name))
9892            (read-file-name
9893             (concat prompt " (default " (car split-name) ") ")
9894             gnus-article-save-directory
9895             (concat gnus-article-save-directory (car split-name))))
9896           (t
9897            (setq split-name (mapcar (lambda (el) (list el))
9898                                     (nreverse split-name)))
9899            (let ((result (completing-read 
9900                           (concat prompt " ")
9901                           split-name nil nil)))
9902              (concat gnus-article-save-directory
9903                      (if (string= result "")
9904                          (car (car split-name))
9905                        result)))))))
9906
9907 (defun gnus-summary-save-in-rmail (&optional filename)
9908   "Append this article to Rmail file.
9909 Optional argument FILENAME specifies file name.
9910 Directory to save to is default to `gnus-article-save-directory' which
9911 is initialized from the SAVEDIR environment variable."
9912   (interactive)
9913   (let ((default-name
9914           (funcall gnus-rmail-save-name gnus-newsgroup-name
9915                    gnus-current-headers gnus-newsgroup-last-rmail)))
9916     (or filename
9917         (setq filename (gnus-read-save-file-name 
9918                         "Save in rmail file:" default-name)))
9919     (gnus-make-directory (file-name-directory filename))
9920     (gnus-eval-in-buffer-window 
9921      gnus-article-buffer
9922      (save-excursion
9923        (save-restriction
9924          (widen)
9925          (gnus-output-to-rmail filename))))
9926     ;; Remember the directory name to save articles.
9927     (setq gnus-newsgroup-last-rmail filename)))
9928
9929 (defun gnus-summary-save-in-mail (&optional filename)
9930   "Append this article to Unix mail file.
9931 Optional argument FILENAME specifies file name.
9932 Directory to save to is default to `gnus-article-save-directory' which
9933 is initialized from the SAVEDIR environment variable."
9934   (interactive)
9935   (let ((default-name
9936           (funcall gnus-mail-save-name gnus-newsgroup-name
9937                    gnus-current-headers gnus-newsgroup-last-mail)))
9938     (or filename
9939         (setq filename (gnus-read-save-file-name 
9940                         "Save in Unix mail file:" default-name)))
9941     (setq filename
9942           (expand-file-name filename
9943                             (and default-name
9944                                  (file-name-directory default-name))))
9945     (gnus-make-directory (file-name-directory filename))
9946     (gnus-eval-in-buffer-window 
9947      gnus-article-buffer
9948      (save-excursion
9949        (save-restriction
9950          (widen)
9951          (if (and (file-readable-p filename) (rmail-file-p filename))
9952              (gnus-output-to-rmail filename)
9953            (rmail-output filename 1 t t)))))
9954     ;; Remember the directory name to save articles.
9955     (setq gnus-newsgroup-last-mail filename)))
9956
9957 (defun gnus-summary-save-in-file (&optional filename)
9958   "Append this article to file.
9959 Optional argument FILENAME specifies file name.
9960 Directory to save to is default to `gnus-article-save-directory' which
9961 is initialized from the SAVEDIR environment variable."
9962   (interactive)
9963   (let ((default-name
9964           (funcall gnus-file-save-name gnus-newsgroup-name
9965                    gnus-current-headers gnus-newsgroup-last-file)))
9966     (or filename
9967         (setq filename (gnus-read-save-file-name 
9968                         "Save in file:" default-name)))
9969     (gnus-make-directory (file-name-directory filename))
9970     (gnus-eval-in-buffer-window 
9971      gnus-article-buffer
9972      (save-excursion
9973        (save-restriction
9974          (widen)
9975          (gnus-output-to-file filename))))
9976     ;; Remember the directory name to save articles.
9977     (setq gnus-newsgroup-last-file filename)))
9978
9979 (defun gnus-summary-save-in-pipe (&optional command)
9980   "Pipe this article to subprocess."
9981   (interactive)
9982   (let ((command (read-string "Shell command on article: "
9983                               gnus-last-shell-command)))
9984     (if (string-equal command "")
9985         (setq command gnus-last-shell-command))
9986     (gnus-eval-in-buffer-window 
9987      gnus-article-buffer
9988      (save-restriction
9989        (widen)
9990        (shell-command-on-region (point-min) (point-max) command nil)))
9991     (setq gnus-last-shell-command command)))
9992
9993 ;; Summary extract commands
9994
9995 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9996   (let ((buffer-read-only nil)
9997         (article (gnus-summary-article-number))
9998         b)
9999     (or (gnus-summary-goto-subject article)
10000         (error (format "No such article: %d" article)))
10001     (or gnus-newsgroup-headers-hashtb-by-number
10002         (gnus-make-headers-hashtable-by-number))
10003     (gnus-summary-position-cursor)
10004     ;; If all commands are to be bunched up on one line, we collect
10005     ;; them here.  
10006     (if gnus-view-pseudos-separately
10007         ()
10008       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10009             files action)
10010         (while ps
10011           (setq action (cdr (assq 'action (car ps))))
10012           (setq files (list (cdr (assq 'name (car ps)))))
10013           (while (and ps (cdr ps)
10014                       (string= (or action "1")
10015                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10016             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10017             (setcdr ps (cdr (cdr ps))))
10018           (if (not files)
10019               ()
10020             (if (not (string-match "%s" action))
10021                 (setq files (cons " " files)))
10022             (setq files (cons " " files))
10023             (and (assq 'execute (car ps))
10024                  (setcdr (assq 'execute (car ps))
10025                          (funcall (if (string-match "%s" action)
10026                                       'format 'concat)
10027                                   action 
10028                                   (mapconcat (lambda (f) f) files " ")))))
10029           (setq ps (cdr ps)))))
10030     (if (and gnus-view-pseudos (not not-view))
10031         (while pslist
10032           (and (assq 'execute (car pslist))
10033                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10034                                      (eq gnus-view-pseudos 'not-confirm)))
10035           (setq pslist (cdr pslist)))
10036       (save-excursion
10037         (while pslist
10038           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10039                                          (gnus-summary-article-number)))
10040           (forward-line 1)
10041           (setq b (point))
10042           (insert "          " (file-name-nondirectory 
10043                                 (cdr (assq 'name (car pslist))))
10044                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10045           (add-text-properties 
10046            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10047                           'gnus-mark gnus-unread-mark 
10048                           'gnus-level 0
10049                           'gnus-pseudo (car pslist)))
10050           (forward-line -1)
10051           (gnus-sethash (int-to-string gnus-reffed-article-number)
10052                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10053           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10054           (setq pslist (cdr pslist)))))))
10055
10056 (defun gnus-pseudos< (p1 p2)
10057   (let ((c1 (cdr (assq 'action p1)))
10058         (c2 (cdr (assq 'action p2))))
10059     (and c1 c2 (string< c1 c2))))
10060
10061 (defun gnus-request-pseudo-article (props)
10062   (cond ((assq 'execute props)
10063          (gnus-execute-command (cdr (assq 'execute props)))))
10064   (let ((gnus-current-article (gnus-summary-article-number)))
10065     (run-hooks 'gnus-mark-article-hook)))
10066
10067 (defun gnus-execute-command (command &optional automatic)
10068   (save-excursion
10069     (gnus-article-setup-buffer)
10070     (set-buffer gnus-article-buffer)
10071     (let ((command (if automatic command (read-string "Command: " command)))
10072           (buffer-read-only nil))
10073       (erase-buffer)
10074       (insert "$ " command "\n\n")
10075       (if gnus-view-pseudo-asynchronously
10076           (start-process "gnus-execute" nil "sh" "-c" command)
10077         (call-process "sh" nil t nil "-c" command)))))
10078
10079 (defun gnus-copy-file (file &optional to)
10080   "Copy FILE to TO."
10081   (interactive
10082    (list (read-file-name "Copy file: " default-directory)
10083          (read-file-name "Copy file to: " default-directory)))
10084   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10085   (and (file-directory-p to) 
10086        (setq to (concat (file-name-as-directory to)
10087                         (file-name-nondirectory file))))
10088   (copy-file file to))
10089
10090 ;; Summary kill commands.
10091
10092 (defun gnus-summary-edit-global-kill (article)
10093   "Edit the \"global\" kill file."
10094   (interactive (list (gnus-summary-article-number)))
10095   (gnus-group-edit-global-kill article))
10096
10097 (defun gnus-summary-edit-local-kill ()
10098   "Edit a local kill file applied to the current newsgroup."
10099   (interactive)
10100   (setq gnus-current-headers 
10101         (gnus-gethash 
10102          (int-to-string (gnus-summary-article-number))
10103          gnus-newsgroup-headers-hashtb-by-number))
10104   (gnus-set-global-variables)
10105   (gnus-group-edit-local-kill 
10106    (gnus-summary-article-number) gnus-newsgroup-name))
10107
10108 \f
10109 ;;;
10110 ;;; Gnus article mode
10111 ;;;
10112
10113 (put 'gnus-article-mode 'mode-class 'special)
10114
10115 (if gnus-article-mode-map
10116     nil
10117   (setq gnus-article-mode-map (make-keymap))
10118   (suppress-keymap gnus-article-mode-map)
10119   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10120   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10121   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10122   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10123   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10124   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10125   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10126   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10127   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10128   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10129   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10130   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10131   
10132   ;; Duplicate almost all summary keystrokes in the article mode map.
10133   (let ((commands 
10134          (list 
10135           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10136           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10137           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10138           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10139           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10140           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10141           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10142           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10143           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10144           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
10145           "\C-c\C-v\C-v" "\C-d" "v" 
10146 ;;        "Mt" "M!" "Md" "Mr"
10147 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10148 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10149 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10150 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10151 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10152 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10153 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10154 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10155 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10156 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10157 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10158           )))
10159     (while commands
10160       (define-key gnus-article-mode-map (car commands) 
10161         'gnus-article-summary-command)
10162       (setq commands (cdr commands))))
10163
10164   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10165 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10166                          "=" "n")))
10167     (while commands
10168       (define-key gnus-article-mode-map (car commands) 
10169         'gnus-article-summary-command-nosave)
10170       (setq commands (cdr commands)))))
10171
10172
10173 (defun gnus-article-mode ()
10174   "Major mode for displaying an article.
10175
10176 All normal editing commands are switched off.
10177
10178 The following commands are available:
10179
10180 \\<gnus-article-mode-map>
10181 \\[gnus-article-next-page]\t Scroll the article one page forwards
10182 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10183 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10184 \\[gnus-article-show-summary]\t Display the summary buffer
10185 \\[gnus-article-mail]\t Send a reply to the address near point
10186 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10187 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10188 \\[gnus-info-find-node]\t Go to the Gnus info node"
10189   (interactive)
10190   (if gnus-visual (gnus-article-make-menu-bar))
10191   (kill-all-local-variables)
10192   (setq mode-line-modified "-- ")
10193   (make-local-variable 'mode-line-format)
10194   (setq mode-line-format (copy-sequence mode-line-format))
10195   (and (equal (nth 3 mode-line-format) "   ")
10196        (setcar (nthcdr 3 mode-line-format) ""))
10197   (setq mode-name "Article")
10198   (setq major-mode 'gnus-article-mode)
10199   (make-local-variable 'minor-mode-alist)
10200   (or (assq 'gnus-show-mime minor-mode-alist)
10201       (setq minor-mode-alist
10202             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10203   (use-local-map gnus-article-mode-map)
10204   (make-local-variable 'page-delimiter)
10205   (setq page-delimiter gnus-page-delimiter)
10206   (buffer-disable-undo (current-buffer))
10207   (setq buffer-read-only t)             ;Disable modification
10208   (run-hooks 'gnus-article-mode-hook))
10209
10210 (defun gnus-article-setup-buffer ()
10211   "Initialize article mode buffer."
10212   (if (get-buffer gnus-article-buffer)
10213       (save-excursion
10214         (set-buffer gnus-article-buffer)
10215         (buffer-disable-undo (current-buffer))
10216         (setq buffer-read-only t)
10217         (gnus-add-current-to-buffer-list)
10218         (or (eq major-mode 'gnus-article-mode)
10219             (gnus-article-mode)))
10220     (save-excursion
10221       (set-buffer (get-buffer-create gnus-article-buffer))
10222       (gnus-add-current-to-buffer-list)
10223       (gnus-article-mode))))
10224
10225 ;; Set article window start at LINE, where LINE is the number of lines
10226 ;; from the head of the article.
10227 (defun gnus-article-set-window-start (&optional line)
10228   (set-window-start 
10229    (get-buffer-window gnus-article-buffer)
10230    (save-excursion
10231      (set-buffer gnus-article-buffer)
10232      (goto-char (point-min))
10233      (if (not line)
10234          (point-min)
10235        (gnus-message 6 "Moved to bookmark")
10236        (search-forward "\n\n" nil t)
10237        (forward-line line)
10238        (point)))))
10239
10240 (defun gnus-request-article-this-buffer (article group)
10241   "Get an article and insert it into this buffer."
10242   (setq group (or group gnus-newsgroup-name))
10243
10244   ;; Open server if it has closed.
10245   (gnus-check-news-server (gnus-find-method-for-group group))
10246
10247   ;; Using `gnus-request-article' directly will insert the article into
10248   ;; `nntp-server-buffer' - so we'll save some time by not having to
10249   ;; copy it from the server buffer into the article buffer.
10250
10251   ;; We only request an article by message-id when we do not have the
10252   ;; headers for it, so we'll have to get those.
10253   (and (stringp article) 
10254        (let ((gnus-override-method gnus-refer-article-method))
10255          (gnus-read-header article)))
10256
10257   ;; If the article number is negative, that means that this article
10258   ;; doesn't belong in this newsgroup (possibly), so we find its
10259   ;; message-id and request it by id instead of number.
10260   (if (not (numberp article))
10261       ()
10262     (save-excursion
10263       (set-buffer gnus-summary-buffer)
10264       (let ((header (gnus-get-header-by-number article)))
10265         (if (< article 0)
10266             (if (vectorp header)
10267                 ;; It's a real article.
10268                 (setq article (header-id header))
10269               ;; It is an extracted pseudo-article.
10270               (setq article 'pseudo)
10271               (gnus-request-pseudo-article header)))
10272
10273         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10274           (if (not (eq (car method) 'nneething))
10275               ()
10276             (let ((dir (concat (file-name-as-directory (nth 1 method))
10277                                (header-subject header))))
10278               (if (file-directory-p dir)
10279                   (progn
10280                     (setq article 'nneething)
10281                     (gnus-group-enter-directory dir)))))))))
10282
10283   ;; Check the cache.
10284   (if (and gnus-use-cache
10285            (numberp article)
10286            (gnus-cache-request-article article group))
10287       'article
10288     ;; Get the article and into the article buffer.
10289     (if (or (stringp article) (numberp article))
10290         (progn
10291           (erase-buffer)
10292           (let ((gnus-override-method 
10293                  (and (stringp article) gnus-refer-article-method)))
10294             (and (gnus-request-article article group (current-buffer))
10295                  'article)))
10296       article)))
10297
10298 (defun gnus-read-header (id)
10299   "Read the headers of article ID and enter them into the Gnus system."
10300   (or gnus-newsgroup-headers-hashtb-by-number
10301       (gnus-make-headers-hashtable-by-number))
10302   (let (header)
10303     (if (not (setq header 
10304                    (car (if (let ((gnus-nov-is-evil t))
10305                               (gnus-retrieve-headers 
10306                                (list id) gnus-newsgroup-name))
10307                             (gnus-get-newsgroup-headers)))))
10308         nil
10309       (if (stringp id)
10310           (header-set-number header gnus-reffed-article-number))
10311       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10312       (gnus-sethash (int-to-string (header-number header)) header
10313                     gnus-newsgroup-headers-hashtb-by-number)
10314       (if (stringp id)
10315           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10316       (setq gnus-current-headers header)
10317       header)))
10318
10319 (defun gnus-article-prepare (article &optional all-headers header)
10320   "Prepare ARTICLE in article mode buffer.
10321 ARTICLE should either be an article number or a Message-ID.
10322 If ARTICLE is an id, HEADER should be the article headers.
10323 If ALL-HEADERS is non-nil, no headers are hidden."
10324   (save-excursion
10325     ;; Make sure we start in a summary buffer.
10326     (or (eq major-mode 'gnus-summary-mode)
10327         (set-buffer gnus-summary-buffer))
10328     (setq gnus-summary-buffer (current-buffer))
10329     ;; Make sure the connection to the server is alive.
10330     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10331         (progn
10332           (gnus-check-news-server 
10333            (gnus-find-method-for-group gnus-newsgroup-name))
10334           (gnus-request-group gnus-newsgroup-name t)))
10335     (or gnus-newsgroup-headers-hashtb-by-number
10336         (gnus-make-headers-hashtable-by-number))
10337     (let* ((article (if header (header-number header) article))
10338            (summary-buffer (current-buffer))
10339            (internal-hook gnus-article-internal-prepare-hook)
10340            (group gnus-newsgroup-name)
10341            result)
10342       (save-excursion
10343         (gnus-article-setup-buffer)
10344         (set-buffer gnus-article-buffer)
10345         (if (not (setq result (let ((buffer-read-only nil))
10346                                 (gnus-request-article-this-buffer 
10347                                  article group))))
10348             ;; There is no such article.
10349             (save-excursion
10350               (if (not (numberp article))
10351                   ()
10352                 (setq gnus-article-current 
10353                       (cons gnus-newsgroup-name article))
10354                 (set-buffer gnus-summary-buffer)
10355                 (setq gnus-current-article article)
10356                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10357               (gnus-message 1 "No such article (may be canceled)")
10358               (ding)
10359               nil)
10360           (if (or (eq result 'pseudo) (eq result 'nneething))
10361               (progn
10362                 (save-excursion
10363                   (set-buffer summary-buffer)
10364                   (setq gnus-last-article gnus-current-article
10365                         gnus-newsgroup-history (cons gnus-current-article
10366                                                      gnus-newsgroup-history)
10367                         gnus-current-article 0
10368                         gnus-current-headers nil
10369                         gnus-article-current nil)
10370                   (if (eq result 'nneething)
10371                       (gnus-configure-windows 'summary)
10372                     (gnus-configure-windows 'article))
10373                   (gnus-set-global-variables))
10374                 (gnus-set-mode-line 'article))
10375             ;; The result from the `request' was an actual article -
10376             ;; or at least some text that is now displayed in the
10377             ;; article buffer.
10378             (if (and (numberp article)
10379                      (not (eq article gnus-current-article)))
10380                 ;; Seems like a new article has been selected.
10381                 ;; `gnus-current-article' must be an article number.
10382                 (save-excursion
10383                   (set-buffer summary-buffer)
10384                   (setq gnus-last-article gnus-current-article
10385                         gnus-newsgroup-history (cons gnus-current-article
10386                                                      gnus-newsgroup-history)
10387                         gnus-current-article article
10388                         gnus-current-headers 
10389                         (gnus-get-header-by-number gnus-current-article)
10390                         gnus-article-current 
10391                         (cons gnus-newsgroup-name gnus-current-article))
10392                   (gnus-summary-show-thread)
10393                   (run-hooks 'gnus-mark-article-hook)
10394                   (gnus-set-mode-line 'summary)
10395                   (and gnus-visual 
10396                        (run-hooks 'gnus-visual-mark-article-hook))
10397                   ;; Set the global newsgroup variables here.
10398                   ;; Suggested by Jim Sisolak
10399                   ;; <sisolak@trans4.neep.wisc.edu>.
10400                   (gnus-set-global-variables)
10401                   (setq gnus-have-all-headers 
10402                         (or all-headers gnus-show-all-headers))
10403                   (and gnus-use-cache 
10404                        (gnus-cache-possibly-enter-article
10405                         group article
10406                         (gnus-get-header-by-number article)
10407                         (memq article gnus-newsgroup-marked)
10408                         (memq article gnus-newsgroup-dormant)
10409                         (memq article gnus-newsgroup-unreads)))))
10410             ;; Hooks for getting information from the article.
10411             ;; This hook must be called before being narrowed.
10412             (let (buffer-read-only)
10413               (run-hooks 'internal-hook)
10414               (run-hooks 'gnus-article-prepare-hook)
10415               ;; Decode MIME message.
10416               (if (and gnus-show-mime
10417                        (or (not gnus-strict-mime)
10418                            (gnus-fetch-field "Mime-Version")))
10419                   (funcall gnus-show-mime-method))
10420               ;; Perform the article display hooks.
10421               (run-hooks 'gnus-article-display-hook))
10422             ;; Do page break.
10423             (goto-char (point-min))
10424             (and gnus-break-pages (gnus-narrow-to-page))
10425             (gnus-set-mode-line 'article)
10426             (gnus-configure-windows 'article)
10427             (goto-char (point-min))
10428             t))))))
10429
10430 (defun gnus-article-show-all-headers ()
10431   "Show all article headers in article mode buffer."
10432   (save-excursion 
10433     (gnus-article-setup-buffer)
10434     (set-buffer gnus-article-buffer)
10435     (let ((buffer-read-only nil))
10436       (remove-text-properties (point-min) (point-max) 
10437                               gnus-hidden-properties))))
10438
10439 (defun gnus-article-hide-headers-if-wanted ()
10440   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10441 Provided for backwards compatability."
10442   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10443       (gnus-article-hide-headers)))
10444
10445 (defun gnus-article-hide-headers (&optional delete)
10446   "Hide unwanted headers and possibly sort them as well."
10447   (interactive "P")
10448   (save-excursion
10449     (set-buffer gnus-article-buffer)
10450     (save-restriction
10451       (let ((sorted gnus-sorted-header-list)
10452             (buffer-read-only nil)
10453             want-list beg want-l)
10454         ;; First we narrow to just the headers.
10455         (widen)
10456         (goto-char (point-min))
10457         ;; Hide any "From " lines at the beginning of (mail) articles. 
10458         (while (looking-at "From ")
10459           (forward-line 1))
10460         (if (bobp) 
10461             (add-text-properties (point-min) (point) gnus-hidden-properties))
10462         ;; Then treat the rest of the header lines.
10463         (narrow-to-region 
10464          (point) 
10465          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10466         ;; Then we use the two regular expressions
10467         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10468         ;; select which header lines is to remain visible in the
10469         ;; article buffer.
10470         (goto-char (point-min))
10471         (while (re-search-forward "^[^ \t]*:" nil t)
10472           (beginning-of-line)
10473           ;; We add the headers we want to keep to a list and delete
10474           ;; them from the buffer.
10475           (if (or (and (stringp gnus-visible-headers)
10476                        (looking-at gnus-visible-headers))
10477                   (and (not (stringp gnus-visible-headers))
10478                        (stringp gnus-ignored-headers)
10479                        (not (looking-at gnus-ignored-headers))))
10480               (progn
10481                 (setq beg (point))
10482                 (forward-line 1)
10483                 ;; Be sure to get multi-line headers...
10484                 (re-search-forward "^[^ \t]*:" nil t)
10485                 (beginning-of-line)
10486                 (setq want-list 
10487                       (cons (buffer-substring beg (point)) want-list))
10488                 (delete-region beg (point))
10489                 (goto-char beg))
10490             (forward-line 1)))
10491         ;; Next we perform the sorting by looking at
10492         ;; `gnus-sorted-header-list'. 
10493         (goto-char (point-min))
10494         (while (and sorted want-list)
10495           (setq want-l want-list)
10496           (while (and want-l
10497                       (not (string-match (car sorted) (car want-l))))
10498             (setq want-l (cdr want-l)))
10499           (if want-l 
10500               (progn
10501                 (insert (car want-l))
10502                 (setq want-list (delq (car want-l) want-list))))
10503           (setq sorted (cdr sorted)))
10504         ;; Any headers that were not matched by the sorted list we
10505         ;; just tack on the end of the visible header list.
10506         (while want-list
10507           (insert (car want-list))
10508           (setq want-list (cdr want-list)))
10509         ;; And finally we make the unwanted headers invisible.
10510         (if delete
10511             (delete-region (point) (point-max))
10512           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10513           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10514
10515 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10516 (defun gnus-article-treat-overstrike ()
10517   "Translate overstrikes into bold text."
10518   (interactive)
10519   (save-excursion
10520     (set-buffer gnus-article-buffer)
10521     (let ((buffer-read-only nil))
10522       (while (search-forward "\b" nil t)
10523         (let ((next (following-char))
10524               (previous (char-after (- (point) 2))))
10525           (cond ((eq next previous)
10526                  (delete-region (- (point) 2) (point))
10527                  (put-text-property (point) (1+ (point))
10528                                     'face 'bold))
10529                 ((eq next ?_)
10530                  (delete-region (1- (point)) (1+ (point)))
10531                  (put-text-property (1- (point)) (point)
10532                                     'face 'underline))
10533                 ((eq previous ?_)
10534                  (delete-region (- (point) 2) (point))
10535                  (put-text-property (point) (1+ (point))
10536                                     'face 'underline))))))))
10537
10538 (defun gnus-article-word-wrap ()
10539   "Format too long lines."
10540   (interactive)
10541   (save-excursion
10542     (set-buffer gnus-article-buffer)
10543     (let ((buffer-read-only nil))
10544       (goto-char (point-min))
10545       (search-forward "\n\n" nil t)
10546       (end-of-line 1)
10547       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10548             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10549             (adaptive-fill-mode t))
10550         (while (not (eobp))
10551           (and (>= (current-column) (min fill-column (window-width)))
10552                (/= (preceding-char) ?:)
10553                (fill-paragraph nil))
10554           (end-of-line 2))))))
10555
10556 (defun gnus-article-remove-cr ()
10557   "Remove carriage returns from an article."
10558   (interactive)
10559   (save-excursion
10560     (set-buffer gnus-article-buffer)
10561     (let ((buffer-read-only nil))
10562       (goto-char (point-min))
10563       (while (search-forward "\r" nil t)
10564         (replace-match "" t t)))))
10565
10566 (defun gnus-article-display-x-face (&optional force)
10567   "Look for an X-Face header and display it if present."
10568   (interactive (list 'force))
10569   (save-excursion
10570     (set-buffer gnus-article-buffer)
10571     (let ((inhibit-point-motion-hooks t)
10572           (case-fold-search nil))
10573       (save-restriction
10574         (goto-char (point-min))
10575         (search-forward "\n\n")
10576         (narrow-to-region (point-min) (point))
10577         (goto-char (point-min))
10578         (if (or (not gnus-article-x-face-command)
10579                 (and (not force)
10580                      (or (not gnus-article-x-face-too-ugly)
10581                          (string-match gnus-article-x-face-too-ugly
10582                                        (mail-fetch-field "from"))))
10583                 (progn
10584                   (goto-char (point-min))
10585                   (not (re-search-forward "^X-Face: " nil t))))
10586             nil
10587           (let ((beg (point))
10588                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10589             (if (symbolp gnus-article-x-face-command)
10590                 (and (or (fboundp gnus-article-x-face-command)
10591                          (error "%s is not a function"
10592                                 gnus-article-x-face-command))
10593                      (funcall gnus-article-x-face-command beg end))
10594               (call-process-region beg end "sh" nil 0 nil
10595                                    "-c" gnus-article-x-face-command))))))))
10596
10597 (defun gnus-article-de-quoted-unreadable (&optional force)
10598   "Do a naïve translation of a quoted-printable-encoded article.
10599 This is in no way, shape or form meant as a replacement for real MIME
10600 processing, but is simply a stop-gap measure until MIME support is
10601 written.
10602 If FORCE, decode the article whether it is marked as quoted-printable
10603 or not." 
10604   (interactive (list 'force))
10605   (save-excursion
10606     (set-buffer gnus-article-buffer)
10607     (let ((case-fold-search t)
10608           (buffer-read-only nil)
10609           (type (gnus-fetch-field "content-transfer-encoding")))
10610       (if (or force (and type (string-match "quoted-printable" type)))
10611           (progn
10612             (goto-char (point-min))
10613             (search-forward "\n\n" nil 'move)
10614             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10615
10616 (defun gnus-mime-decode-quoted-printable (from to)
10617   ;; Decode quoted-printable from region between FROM and TO.
10618   (save-excursion
10619     (goto-char from)
10620     (while (search-forward "=" to t)
10621       (cond ((eq (following-char) ?\n)
10622              (delete-char -1)
10623              (delete-char 1))
10624             ((looking-at "[0-9A-F][0-9A-F]")
10625              (delete-char -1)
10626              (insert (hexl-hex-string-to-integer
10627                       (buffer-substring (point) (+ 2 (point)))))
10628              (delete-char 2))
10629             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10630
10631 (defvar gnus-article-time-units
10632   (list (cons 'year (* 365.25 24 60 60))
10633         (cons 'week (* 7 24 60 60))
10634         (cons 'day (* 24 60 60))
10635         (cons 'hour (* 60 60))
10636         (cons 'minute 60)
10637         (cons 'second 1)))
10638
10639 (defun gnus-article-date-ut (&optional type)
10640   "Convert DATE date to universal time in the current article.
10641 If TYPE is `local', convert to local time; if it is `lapsed', output
10642 how much time has lapsed since DATE."
10643   (interactive (list 'ut))
10644   (let ((date (header-date (or gnus-current-headers 
10645                                (gnus-get-header-by-number
10646                                 (gnus-summary-article-number))"")))
10647         (date-regexp "^Date: \\|^X-Sent: "))
10648     (if (or (not date)
10649             (string= date ""))
10650         ()
10651       (save-excursion
10652         (set-buffer gnus-article-buffer)
10653         (let ((buffer-read-only nil))
10654           (goto-char (point-min))
10655           (if (and (re-search-forward date-regexp nil t)
10656                    (progn 
10657                      (beginning-of-line)
10658                      (looking-at date-regexp)))
10659               (delete-region (gnus-point-at-bol)
10660                              (progn (end-of-line) (1+ (point))))
10661             (goto-char (point-min))
10662             (goto-char (- (search-forward "\n\n") 2)))
10663           (insert
10664            (cond 
10665             ((eq type 'local)
10666              (concat "Date: " (condition-case ()
10667                                   (timezone-make-date-arpa-standard date)
10668                                 (error date))
10669                      "\n"))
10670             ((eq type 'ut)
10671              (concat "Date: "
10672                      (condition-case ()
10673                          (timezone-make-date-arpa-standard date nil "UT")
10674                        (error date))
10675                      "\n"))
10676             ((eq type 'lapsed)
10677              ;; If the date is seriously mangled, the timezone
10678              ;; functions are liable to bug out, so we condition-case
10679              ;; the entire thing.  
10680              (let* ((sec (condition-case ()
10681                              (max (- (gnus-seconds-since-epoch 
10682                                       (timezone-make-date-arpa-standard
10683                                        (current-time-string) 
10684                                        (current-time-zone) "UT"))
10685                                      (gnus-seconds-since-epoch 
10686                                       (timezone-make-date-arpa-standard 
10687                                        date nil "UT")))
10688                                   0)
10689                            (error 0)))
10690                     num prev)
10691                (concat
10692                 "X-Sent: "
10693                 (mapconcat 
10694                  (lambda (unit)
10695                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10696                        ""
10697                      (setq sec (- sec (* num (cdr unit))))
10698                      (prog1
10699                          (concat (if prev ", " "") (int-to-string (floor num))
10700                                  " " (symbol-name (car unit))
10701                                  (if (> num 1) "s" ""))
10702                        (setq prev t))))
10703                  gnus-article-time-units "")
10704                 " ago\n")))
10705             (t
10706              (error "Unknown conversion type: %s" type)))))))))
10707
10708 (defun gnus-article-date-local ()
10709   "Convert the current article date to the local timezone."
10710   (interactive)
10711   (gnus-article-date-ut 'local))
10712
10713 (defun gnus-article-date-lapsed ()
10714   "Convert the current article date to time lapsed since it was sent."
10715   (interactive)
10716   (gnus-article-date-ut 'lapsed))
10717
10718 (defun gnus-article-maybe-highlight ()
10719   (if gnus-visual (gnus-article-highlight)))
10720
10721 ;; Article savers.
10722
10723 (defun gnus-output-to-rmail (file-name)
10724   "Append the current article to an Rmail file named FILE-NAME."
10725   (require 'rmail)
10726   ;; Most of these codes are borrowed from rmailout.el.
10727   (setq file-name (expand-file-name file-name))
10728   (setq rmail-default-rmail-file file-name)
10729   (let ((artbuf (current-buffer))
10730         (tmpbuf (get-buffer-create " *Gnus-output*")))
10731     (save-excursion
10732       (or (get-file-buffer file-name)
10733           (file-exists-p file-name)
10734           (if (gnus-yes-or-no-p
10735                (concat "\"" file-name "\" does not exist, create it? "))
10736               (let ((file-buffer (create-file-buffer file-name)))
10737                 (save-excursion
10738                   (set-buffer file-buffer)
10739                   (rmail-insert-rmail-file-header)
10740                   (let ((require-final-newline nil))
10741                     (write-region (point-min) (point-max) file-name t 1)))
10742                 (kill-buffer file-buffer))
10743             (error "Output file does not exist")))
10744       (set-buffer tmpbuf)
10745       (buffer-disable-undo (current-buffer))
10746       (erase-buffer)
10747       (insert-buffer-substring artbuf)
10748       (gnus-convert-article-to-rmail)
10749       ;; Decide whether to append to a file or to an Emacs buffer.
10750       (let ((outbuf (get-file-buffer file-name)))
10751         (if (not outbuf)
10752             (append-to-file (point-min) (point-max) file-name)
10753           ;; File has been visited, in buffer OUTBUF.
10754           (set-buffer outbuf)
10755           (let ((buffer-read-only nil)
10756                 (msg (and (boundp 'rmail-current-message)
10757                           (symbol-value 'rmail-current-message))))
10758             ;; If MSG is non-nil, buffer is in RMAIL mode.
10759             (if msg
10760                 (progn (widen)
10761                        (narrow-to-region (point-max) (point-max))))
10762             (insert-buffer-substring tmpbuf)
10763             (if msg
10764                 (progn
10765                   (goto-char (point-min))
10766                   (widen)
10767                   (search-backward "\^_")
10768                   (narrow-to-region (point) (point-max))
10769                   (goto-char (1+ (point-min)))
10770                   (rmail-count-new-messages t)
10771                   (rmail-show-message msg)))))))
10772     (kill-buffer tmpbuf)))
10773
10774 (defun gnus-output-to-file (file-name)
10775   "Append the current article to a file named FILE-NAME."
10776   (setq file-name (expand-file-name file-name))
10777   (let ((artbuf (current-buffer))
10778         (tmpbuf (get-buffer-create " *Gnus-output*")))
10779     (save-excursion
10780       (set-buffer tmpbuf)
10781       (buffer-disable-undo (current-buffer))
10782       (erase-buffer)
10783       (insert-buffer-substring artbuf)
10784       ;; Append newline at end of the buffer as separator, and then
10785       ;; save it to file.
10786       (goto-char (point-max))
10787       (insert "\n")
10788       (append-to-file (point-min) (point-max) file-name))
10789     (kill-buffer tmpbuf)))
10790
10791 (defun gnus-convert-article-to-rmail ()
10792   "Convert article in current buffer to Rmail message format."
10793   (let ((buffer-read-only nil))
10794     ;; Convert article directly into Babyl format.
10795     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10796     (goto-char (point-min))
10797     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10798     (while (search-forward "\n\^_" nil t) ;single char
10799       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10800     (goto-char (point-max))
10801     (insert "\^_")))
10802
10803 (defun gnus-narrow-to-page (&optional arg)
10804   "Make text outside current page invisible except for page delimiter.
10805 A numeric arg specifies to move forward or backward by that many pages,
10806 thus showing a page other than the one point was originally in."
10807   (interactive "P")
10808   (setq arg (if arg (prefix-numeric-value arg) 0))
10809   (save-excursion
10810     (forward-page -1)                   ;Beginning of current page.
10811     (widen)
10812     (if (> arg 0)
10813         (forward-page arg)
10814       (if (< arg 0)
10815           (forward-page (1- arg))))
10816     ;; Find the end of the page.
10817     (forward-page)
10818     ;; If we stopped due to end of buffer, stay there.
10819     ;; If we stopped after a page delimiter, put end of restriction
10820     ;; at the beginning of that line.
10821     ;; These are commented out.
10822     ;;    (if (save-excursion (beginning-of-line)
10823     ;;                  (looking-at page-delimiter))
10824     ;;  (beginning-of-line))
10825     (narrow-to-region (point)
10826                       (progn
10827                         ;; Find the top of the page.
10828                         (forward-page -1)
10829                         ;; If we found beginning of buffer, stay there.
10830                         ;; If extra text follows page delimiter on same line,
10831                         ;; include it.
10832                         ;; Otherwise, show text starting with following line.
10833                         (if (and (eolp) (not (bobp)))
10834                             (forward-line 1))
10835                         (point)))))
10836
10837 (defun gnus-gmt-to-local ()
10838   "Rewrite Date header described in GMT to local in current buffer.
10839 Intended to be used with gnus-article-prepare-hook."
10840   (save-excursion
10841     (save-restriction
10842       (widen)
10843       (goto-char (point-min))
10844       (narrow-to-region (point-min)
10845                         (progn (search-forward "\n\n" nil 'move) (point)))
10846       (goto-char (point-min))
10847       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10848           (let ((buffer-read-only nil)
10849                 (date (buffer-substring-no-properties
10850                        (match-beginning 1) (match-end 1))))
10851             (delete-region (match-beginning 1) (match-end 1))
10852             (insert
10853              (timezone-make-date-arpa-standard 
10854               date nil (current-time-zone))))))))
10855
10856
10857 ;; Article mode commands
10858
10859 (defun gnus-article-next-page (lines)
10860   "Show next page of current article.
10861 If end of article, return non-nil. Otherwise return nil.
10862 Argument LINES specifies lines to be scrolled up."
10863   (interactive "P")
10864   (move-to-window-line -1)
10865   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10866   (if (save-excursion
10867         (end-of-line)
10868         (and (pos-visible-in-window-p)  ;Not continuation line.
10869              (eobp)))
10870       ;; Nothing in this page.
10871       (if (or (not gnus-break-pages)
10872               (save-excursion
10873                 (save-restriction
10874                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10875           t                             ;Nothing more.
10876         (gnus-narrow-to-page 1)         ;Go to next page.
10877         nil)
10878     ;; More in this page.
10879     (condition-case ()
10880         (scroll-up lines)
10881       (end-of-buffer
10882        ;; Long lines may cause an end-of-buffer error.
10883        (goto-char (point-max))))
10884     nil))
10885
10886 (defun gnus-article-prev-page (lines)
10887   "Show previous page of current article.
10888 Argument LINES specifies lines to be scrolled down."
10889   (interactive "P")
10890   (move-to-window-line 0)
10891   (if (and gnus-break-pages
10892            (bobp)
10893            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10894       (progn
10895         (gnus-narrow-to-page -1) ;Go to previous page.
10896         (goto-char (point-max))
10897         (recenter -1))
10898     (scroll-down lines)))
10899
10900 (defun gnus-article-refer-article ()
10901   "Read article specified by message-id around point."
10902   (interactive)
10903   (search-forward ">" nil t)    ;Move point to end of "<....>".
10904   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10905       (let ((message-id
10906              (buffer-substring (match-beginning 1) (match-end 1))))
10907         (set-buffer gnus-summary-buffer)
10908         (gnus-summary-refer-article message-id))
10909     (error "No references around point")))
10910
10911 (defun gnus-article-show-summary ()
10912   "Reconfigure windows to show summary buffer."
10913   (interactive)
10914   (gnus-configure-windows 'article)
10915   (gnus-summary-goto-subject gnus-current-article))
10916
10917 (defun gnus-article-describe-briefly ()
10918   "Describe article mode commands briefly."
10919   (interactive)
10920   (gnus-message 6
10921    (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")))
10922
10923 (defun gnus-article-summary-command ()
10924   "Execute the last keystroke in the summary buffer."
10925   (interactive)
10926   (let ((obuf (current-buffer))
10927         (owin (current-window-configuration))
10928         func)
10929     (switch-to-buffer gnus-summary-buffer 'norecord)
10930     (setq func (lookup-key (current-local-map) (this-command-keys)))
10931     (call-interactively func)
10932     (set-buffer obuf)
10933     (set-window-configuration owin)
10934     (set-window-start (get-buffer-window (current-buffer)) (point))))
10935
10936 (defun gnus-article-summary-command-nosave ()
10937   "Execute the last keystroke in the summary buffer."
10938   (interactive)
10939   (let (func)
10940     (pop-to-buffer gnus-summary-buffer 'norecord)
10941     (setq func (lookup-key (current-local-map) (this-command-keys)))
10942     (call-interactively func)))
10943
10944 \f
10945 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10946
10947 ;;;###autoload
10948 (defalias 'gnus-batch-kill 'gnus-batch-score)
10949 ;;;###autoload
10950 (defun gnus-batch-score ()
10951   "Run batched scoring.
10952 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10953 Newsgroups is a list of strings in Bnews format.  If you want to score
10954 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10955 score the alt hierarchy, you'd say \"!alt.all\"."
10956   (interactive)
10957   (let* ((yes-and-no
10958           (gnus-newsrc-parse-options
10959            (apply (function concat)
10960                   (mapcar (lambda (g) (concat g " "))
10961                           command-line-args-left))))
10962          (gnus-expert-user t)
10963          (nnmail-spool-file nil)
10964          (gnus-use-dribble-file nil)
10965          (yes (car yes-and-no))
10966          (no (cdr yes-and-no))
10967          group newsrc entry
10968          ;; Disable verbose message.
10969          gnus-novice-user gnus-large-newsgroup)
10970     ;; Eat all arguments.
10971     (setq command-line-args-left nil)
10972     ;; Start Gnus.
10973     (gnus)
10974     ;; Apply kills to specified newsgroups in command line arguments.
10975     (setq newsrc (cdr gnus-newsrc-alist))
10976     (while newsrc
10977       (setq group (car (car newsrc)))
10978       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10979       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10980                (and (car entry)
10981                     (or (eq (car entry) t)
10982                         (not (zerop (car entry)))))
10983                (if yes (string-match yes group) t)
10984                (or (null no) (not (string-match no group))))
10985           (progn
10986             (gnus-summary-read-group group nil t)
10987             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10988                  (gnus-summary-exit))))
10989       (setq newsrc (cdr newsrc)))
10990     ;; Exit Emacs.
10991     (switch-to-buffer gnus-group-buffer)
10992     (gnus-group-save-newsrc)))
10993
10994 (defun gnus-apply-kill-file ()
10995   "Apply a kill file to the current newsgroup.
10996 Returns the number of articles marked as read."
10997   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10998           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10999       (gnus-apply-kill-file-internal)
11000     0))
11001
11002 (defun gnus-kill-save-kill-buffer ()
11003   (save-excursion
11004     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11005       (if (get-file-buffer file)
11006           (progn
11007             (set-buffer (get-file-buffer file))
11008             (and (buffer-modified-p) (save-buffer))
11009             (kill-buffer (current-buffer)))))))
11010
11011 (defvar gnus-kill-file-name "KILL"
11012   "Suffix of the kill files.")
11013
11014 (defun gnus-newsgroup-kill-file (newsgroup)
11015   "Return the name of a kill file name for NEWSGROUP.
11016 If NEWSGROUP is nil, return the global kill file name instead."
11017   (cond ((or (null newsgroup)
11018              (string-equal newsgroup ""))
11019          ;; The global KILL file is placed at top of the directory.
11020          (expand-file-name gnus-kill-file-name
11021                            (or gnus-kill-files-directory "~/News")))
11022         ((gnus-use-long-file-name 'not-kill)
11023          ;; Append ".KILL" to newsgroup name.
11024          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11025                            (or gnus-kill-files-directory "~/News")))
11026         (t
11027          ;; Place "KILL" under the hierarchical directory.
11028          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11029                                    "/" gnus-kill-file-name)
11030                            (or gnus-kill-files-directory "~/News")))))
11031
11032 \f
11033 ;;;
11034 ;;; Dribble file
11035 ;;;
11036
11037 (defvar gnus-dribble-ignore nil)
11038
11039 (defun gnus-dribble-file-name ()
11040   (concat gnus-startup-file "-dribble"))
11041
11042 (defun gnus-dribble-open ()
11043   (save-excursion 
11044     (set-buffer 
11045      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11046     (buffer-disable-undo (current-buffer))
11047     (bury-buffer gnus-dribble-buffer)
11048     (auto-save-mode t)
11049     (goto-char (point-max))))
11050
11051 (defun gnus-dribble-enter (string)
11052   (if (and (not gnus-dribble-ignore)
11053            gnus-dribble-buffer
11054            (buffer-name gnus-dribble-buffer))
11055       (let ((obuf (current-buffer)))
11056         (set-buffer gnus-dribble-buffer)
11057         (insert string "\n")
11058         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11059         (set-buffer obuf))))
11060
11061 (defun gnus-dribble-read-file ()
11062   (let ((dribble-file (gnus-dribble-file-name)))
11063     (save-excursion 
11064       (set-buffer (setq gnus-dribble-buffer 
11065                         (get-buffer-create 
11066                          (file-name-nondirectory dribble-file))))
11067       (gnus-add-current-to-buffer-list)
11068       (erase-buffer)
11069       (set-visited-file-name dribble-file)
11070       (buffer-disable-undo (current-buffer))
11071       (bury-buffer (current-buffer))
11072       (set-buffer-modified-p nil)
11073       (let ((auto (make-auto-save-file-name))
11074             (gnus-dribble-ignore t))
11075         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11076             (progn
11077               (if (file-newer-than-file-p auto dribble-file)
11078                   (setq dribble-file auto))
11079               (insert-file-contents dribble-file)
11080               (if (not (zerop (buffer-size)))
11081                   (set-buffer-modified-p t))
11082               (if (gnus-y-or-n-p 
11083                    "Auto-save file exists. Do you want to read it? ")
11084                   (progn
11085                     (gnus-message 5 "Reading %s..." dribble-file) 
11086                     (eval-buffer (current-buffer))
11087                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
11088
11089 (defun gnus-dribble-delete-file ()
11090   (if (file-exists-p (gnus-dribble-file-name))
11091       (delete-file (gnus-dribble-file-name)))
11092   (if gnus-dribble-buffer
11093       (save-excursion
11094         (set-buffer gnus-dribble-buffer)
11095         (let ((auto (make-auto-save-file-name)))
11096           (if (file-exists-p auto)
11097               (delete-file auto))
11098           (erase-buffer)
11099           (set-buffer-modified-p nil)))))
11100
11101 (defun gnus-dribble-save ()
11102   (if (and gnus-dribble-buffer
11103            (buffer-name gnus-dribble-buffer))
11104       (save-excursion
11105         (set-buffer gnus-dribble-buffer)
11106         (save-buffer))))
11107
11108 (defun gnus-dribble-clear ()
11109   (save-excursion
11110     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11111         (progn
11112           (set-buffer gnus-dribble-buffer)
11113           (erase-buffer)
11114           (set-buffer-modified-p nil)
11115           (setq buffer-saved-size (buffer-size))))))
11116
11117 ;;;
11118 ;;; Server Communication
11119 ;;;
11120
11121 ;; All the Gnus backends have the same interface, and should return
11122 ;; data in a similar format. Below is an overview of what functions
11123 ;; these packages must supply and what results they should return.
11124 ;;
11125 ;; Variables:
11126 ;;
11127 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11128 ;; buffer. 
11129 ;;
11130 ;; Functions for the imaginary backend `choke':
11131 ;;
11132 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11133 ;; Should return all headers for all ARTICLES, or return NOV lines for
11134 ;; the same.
11135 ;;
11136 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11137 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11138 ;; must be returned.
11139 ;;
11140 ;; `choke-close-group GROUP &optional SERVER'
11141 ;; Close group. Most backends won't have to do anything with this
11142 ;; call, but it is an opportunity to clean up, if that is needed. It
11143 ;; is called when Gnus exits a group.
11144 ;;
11145 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11146 ;; Return ARTICLE, which is either an article number or
11147 ;; message-id. Note that not all backends can return articles based on
11148 ;; message-id. 
11149 ;;
11150 ;; `choke-request-list SERVER'
11151 ;; Return a list of all newsgroups on SERVER.
11152 ;;
11153 ;; `choke-request-list-newsgroups SERVER'
11154 ;; Return a list of descriptions of all newsgroups on SERVER.
11155 ;;
11156 ;; `choke-request-newgroups DATE &optional SERVER'
11157 ;; Return a list of all groups that have arrived after DATE on
11158 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11159 ;; always check whether the groups are old or not. Backends that do
11160 ;; not store date information may just return the entire list of
11161 ;; groups, although this might not be a good idea in general.
11162 ;;
11163 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11164 ;; Should return a buffer that is suitable for "posting". nnspool and
11165 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11166 ;; buffer. This function should fill out the appropriate headers. 
11167 ;;
11168 ;; `choke-request-post &optional SERVER'
11169 ;; Function that will be called from a buffer to be posted. 
11170 ;;
11171 ;; `choke-open-server SERVER &optional ARGUMENT'
11172 ;; Open a connection to SERVER.
11173 ;;
11174 ;; `choke-close-server &optional SERVER'
11175 ;; Close the connection to SERVER.
11176 ;;
11177 ;; `choke-server-opened &optional SERVER'
11178 ;; Whether the conenction to SERVER is opened or not.
11179 ;;
11180 ;; `choke-server-status &optional SERVER'
11181 ;; Should return a status string (not in the nntp buffer, but as the
11182 ;; result of the function).
11183 ;;
11184 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11185 ;; Optional function for retrieving active file info on all groups in
11186 ;; GROUPS.  Two return formats are supported: The normal active file
11187 ;; format, and a list of GROUP lines.  This function should return (as
11188 ;; a function value) either `active' or `group', depending on what
11189 ;; format it returns.
11190 ;;
11191 ;; The following functions are optional and apply only to backends
11192 ;; that are able to control the contents of their groups totally
11193 ;; (ie. mail backends.)  Backends that aren't able to do that
11194 ;; shouldn't define these functions at all. Gnus will check for their
11195 ;; presence before attempting to call them.
11196 ;;
11197 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11198 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11199 ;; backends will not be able to expire articles. Should return a list
11200 ;; of all articles that were not expired.
11201 ;;
11202 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11203 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11204 ;; Removes any information it has added to the article (extra headers,
11205 ;; whatever - make it as clean as possible), and then passes the
11206 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11207 ;; function described below. If the ACCEPT-FORM returns a non-nil
11208 ;; value, the article should then be deleted. If LAST is nil, that
11209 ;; means that there will be further calls to this function. This might
11210 ;; be taken as an advice not to save buffers/internal variables just
11211 ;; yet, but wait until the last call to speed things up.
11212 ;;
11213 ;; `choke-request-accept-article GROUP &optional LAST' 
11214 ;; The contents of the current buffer will be put into GROUP.  There
11215 ;; should, of course, be an article in the current buffer.  This
11216 ;; function is normally only called by the function described above,
11217 ;; and LAST works the same way as in that function.
11218 ;;
11219 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11220 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11221 ;; This provides an easy interface for allowing editing of
11222 ;; articles. Note that even headers may be edited, so the backend has
11223 ;; to update any tables (nov buffers, etc) that it maintains after
11224 ;; replacing the article.
11225 ;;
11226 ;; `choke-request-create-group GROUP &optional SERVER'
11227 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11228 ;; might be a group that already exists, but hasn't been registered
11229 ;; yet. 
11230 ;;
11231 ;; All these functions must return nil if they couldn't service the
11232 ;; request. If the optional arguments are not supplied, some "current"
11233 ;; or "default" values should be used. In short, one should emulate an
11234 ;; NNTP server, in a way.
11235 ;;
11236 ;; If you want to write a new backend, you just have to supply the
11237 ;; functions listed above. In addition, you must enter the new backend
11238 ;; into the list of valid select methods:
11239 ;; (setq gnus-valid-select-methods 
11240 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11241 ;; The first element in this list is the name of the backend. Other
11242 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11243 ;; groups), `none' (neither), `respool' (for groups that can control
11244 ;; their contents). 
11245
11246 (defun gnus-start-news-server (&optional confirm)
11247   "Open a method for getting news.
11248 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11249   (let (how)
11250     (if gnus-current-select-method
11251         ;; Stream is already opened.
11252         nil
11253       ;; Open NNTP server.
11254       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11255       (if confirm
11256           (progn
11257             ;; Read server name with completion.
11258             (setq gnus-nntp-server
11259                   (completing-read "NNTP server: "
11260                                    (mapcar (lambda (server) (list server))
11261                                            (cons (list gnus-nntp-server)
11262                                                  gnus-secondary-servers))
11263                                    nil nil gnus-nntp-server))))
11264
11265       (if (and gnus-nntp-server 
11266                (stringp gnus-nntp-server)
11267                (not (string= gnus-nntp-server "")))
11268           (setq gnus-select-method
11269                 (cond ((or (string= gnus-nntp-server "")
11270                            (string= gnus-nntp-server "::"))
11271                        (list 'nnspool (system-name)))
11272                       ((string-match "^:" gnus-nntp-server)
11273                        (list 'nnmh gnus-nntp-server 
11274                              (list 'nnmh-directory 
11275                                    (file-name-as-directory
11276                                     (expand-file-name
11277                                      (concat "~/" (substring
11278                                                    gnus-nntp-server 1)))))
11279                              (list 'nnmh-get-new-mail nil)))
11280                       (t
11281                        (list 'nntp gnus-nntp-server)))))
11282
11283       (setq how (car gnus-select-method))
11284       (cond ((eq how 'nnspool)
11285              (require 'nnspool)
11286              (gnus-message 5 "Looking up local news spool..."))
11287             ((eq how 'nnmh)
11288              (require 'nnmh)
11289              (gnus-message 5 "Looking up mh spool..."))
11290             (t
11291              (require 'nntp)))
11292       (setq gnus-current-select-method gnus-select-method)
11293       (run-hooks 'gnus-open-server-hook)
11294       (or 
11295        ;; gnus-open-server-hook might have opened it
11296        (gnus-server-opened gnus-select-method)  
11297        (gnus-open-server gnus-select-method)
11298        (gnus-y-or-n-p
11299         (format
11300          "%s server on %s can't be opened. Continue? "
11301          (car gnus-select-method) (nth 1 gnus-select-method)))
11302        (progn
11303          (gnus-message 1 "Couldn't open server on %s" 
11304                        (nth 1 gnus-select-method))
11305          (ding)
11306          nil)))))
11307
11308 (defun gnus-check-news-server (&optional method)
11309   "If the news server is down, start it up again."
11310   (let ((method (if method method gnus-select-method)))
11311     (and (stringp method)
11312          (setq method (gnus-server-to-method method)))
11313     (if (gnus-server-opened method)
11314         ;; Stream is already opened.
11315         t
11316       ;; Open server.
11317       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11318       (run-hooks 'gnus-open-server-hook)
11319       (or (gnus-server-opened method)
11320           (gnus-open-server method))
11321       (message ""))))
11322
11323 (defun gnus-nntp-message (&optional message)
11324   "Check the status of the NNTP server.
11325 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11326 is returned insted of the status string."
11327   (let ((status (gnus-status-message (gnus-find-method-for-group 
11328                                       gnus-newsgroup-name)))
11329         (message (or message "")))
11330     (if (and (stringp status) (> (length status) 0))
11331         status message)))
11332
11333 (defun gnus-get-function (method function)
11334   (and (stringp method)
11335        (setq method (gnus-server-to-method method)))
11336   (let ((func (intern (format "%s-%s" (car method) function))))
11337     (if (not (fboundp func)) 
11338         (progn
11339           (require (car method))
11340           (if (not (fboundp func)) 
11341               (error "No such function: %s" func))))
11342     func))
11343
11344 ;;; Interface functions to the backends.
11345
11346 (defun gnus-open-server (method)
11347   (funcall (gnus-get-function method 'open-server)
11348            (nth 1 method) (nthcdr 2 method)))
11349
11350 (defun gnus-close-server (method)
11351   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11352
11353 (defun gnus-request-list (method)
11354   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11355
11356 (defun gnus-request-list-newsgroups (method)
11357   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11358
11359 (defun gnus-request-newgroups (date method)
11360   (funcall (gnus-get-function method 'request-newgroups) 
11361            date (nth 1 method)))
11362
11363 (defun gnus-server-opened (method)
11364   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11365
11366 (defun gnus-status-message (method)
11367   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11368                   method)))
11369     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11370
11371 (defun gnus-request-group (group &optional dont-check)
11372   (let ((method (gnus-find-method-for-group group)))
11373 ;    (and t (message "%s GROUP %s" (car method) group))
11374     (funcall (gnus-get-function method 'request-group) 
11375              (gnus-group-real-name group) (nth 1 method) dont-check)))
11376
11377 (defun gnus-request-asynchronous (group &optional articles)
11378   (let ((method (gnus-find-method-for-group group)))
11379     (funcall (gnus-get-function method 'request-asynchronous) 
11380              (gnus-group-real-name group) (nth 1 method) articles)))
11381
11382 (defun gnus-list-active-group (group)
11383   (let ((method (gnus-find-method-for-group group))
11384         (func 'list-active-group))
11385     (and (gnus-check-backend-function func group)
11386          (funcall (gnus-get-function method func) 
11387                   (gnus-group-real-name group) (nth 1 method)))))
11388
11389 (defun gnus-request-group-description (group)
11390   (let ((method (gnus-find-method-for-group group))
11391         (func 'request-group-description))
11392     (and (gnus-check-backend-function func group)
11393          (funcall (gnus-get-function method func) 
11394                   (gnus-group-real-name group) (nth 1 method)))))
11395
11396 (defun gnus-close-group (group)
11397   (let ((method (gnus-find-method-for-group group)))
11398     (funcall (gnus-get-function method 'close-group) 
11399              (gnus-group-real-name group) (nth 1 method))))
11400
11401 (defun gnus-retrieve-headers (articles group)
11402   (let ((method (gnus-find-method-for-group group)))
11403     (if (and gnus-use-cache (numberp (car articles)))
11404         (gnus-cache-retrieve-headers articles group)
11405       (funcall (gnus-get-function method 'retrieve-headers) 
11406                articles (gnus-group-real-name group) (nth 1 method)))))
11407
11408 (defun gnus-retrieve-groups (groups method)
11409   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11410
11411 (defun gnus-request-article (article group &optional buffer)
11412   (let ((method (gnus-find-method-for-group group)))
11413     (funcall (gnus-get-function method 'request-article) 
11414              article (gnus-group-real-name group) (nth 1 method) buffer)))
11415
11416 (defun gnus-request-head (article group)
11417   (let ((method (gnus-find-method-for-group group)))
11418     (funcall (gnus-get-function method 'request-head) 
11419              article (gnus-group-real-name group) (nth 1 method))))
11420
11421 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11422 (defun gnus-request-post-buffer (post group subject header artbuf
11423                                       info follow-to respect-poster)
11424    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11425                                             group gnus-newsrc-hashtb)))))
11426           (method
11427            (if (and gnus-post-method
11428                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11429                     (memq 'post (assoc
11430                                  (format "%s" (car (gnus-find-method-for-group
11431                                                     gnus-newsgroup-name)))
11432                                         gnus-valid-select-methods)))
11433                gnus-post-method
11434              (gnus-find-method-for-group gnus-newsgroup-name))))
11435      (or (gnus-server-opened method)
11436          (gnus-open-server method)
11437          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11438      (let ((mail-self-blind nil)
11439            (mail-archive-file-name nil))
11440        (funcall (gnus-get-function method 'request-post-buffer) 
11441                 post group subject header artbuf info follow-to
11442                 respect-poster))))
11443
11444 (defun gnus-request-post (method &optional force)
11445   (and (stringp method)
11446        (setq method (gnus-server-to-method method)))
11447   (and (not force) gnus-post-method
11448        (memq 'post (assoc (format "%s" (car method))
11449                           gnus-valid-select-methods))
11450        (setq method gnus-post-method))
11451   (funcall (gnus-get-function method 'request-post) 
11452            (nth 1 method)))
11453
11454 (defun gnus-request-expire-articles (articles group &optional force)
11455   (let ((method (gnus-find-method-for-group group)))
11456     (funcall (gnus-get-function method 'request-expire-articles) 
11457              articles (gnus-group-real-name group) (nth 1 method)
11458              force)))
11459
11460 (defun gnus-request-move-article 
11461   (article group server accept-function &optional last)
11462   (let ((method (gnus-find-method-for-group group)))
11463     (funcall (gnus-get-function method 'request-move-article) 
11464              article (gnus-group-real-name group) 
11465              (nth 1 method) accept-function last)))
11466
11467 (defun gnus-request-accept-article (group &optional last)
11468   (let ((func (if (symbolp group) group
11469                 (car (gnus-find-method-for-group group)))))
11470     (funcall (intern (format "%s-request-accept-article" func))
11471              (if (stringp group) (gnus-group-real-name group) group)
11472              last)))
11473
11474 (defun gnus-request-replace-article (article group buffer)
11475   (let ((func (car (gnus-find-method-for-group group))))
11476     (funcall (intern (format "%s-request-replace-article" func))
11477              article (gnus-group-real-name group) buffer)))
11478
11479 (defun gnus-request-create-group (group)
11480   (let ((method (gnus-find-method-for-group group)))
11481     (funcall (gnus-get-function method 'request-create-group) 
11482              (gnus-group-real-name group) (nth 1 method))))
11483
11484 (defun gnus-member-of-valid (symbol group)
11485   (memq symbol (assoc
11486                 (format "%s" (car (gnus-find-method-for-group group)))
11487                 gnus-valid-select-methods)))
11488
11489 (defsubst gnus-secondary-method-p (method)
11490   (member method gnus-secondary-select-methods))
11491
11492 (defun gnus-find-method-for-group (group &optional info)
11493   (or gnus-override-method
11494       (and (not group)
11495            gnus-select-method)
11496       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11497             method)
11498         (if (or (not info)
11499                 (not (setq method (nth 4 info))))
11500             (setq method gnus-select-method)
11501           (setq method
11502                 (cond ((stringp method)
11503                        (gnus-server-to-method method))
11504                       ((stringp (car method))
11505                        (gnus-server-extend-method group method))
11506                       (t
11507                        method))))
11508         (gnus-server-add-address method))))
11509
11510 (defun gnus-check-backend-function (func group)
11511   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11512                  group)))
11513     (fboundp (intern (format "%s-%s" method func)))))
11514
11515 (defun gnus-methods-using (method)
11516   (let ((valids gnus-valid-select-methods)
11517         outs)
11518     (while valids
11519       (if (memq method (car valids)) 
11520           (setq outs (cons (car valids) outs)))
11521       (setq valids (cdr valids)))
11522     outs))
11523
11524 ;;; 
11525 ;;; Active & Newsrc File Handling
11526 ;;;
11527
11528 ;; Newsrc related functions.
11529 ;; Gnus internal format of gnus-newsrc-alist:
11530 ;; (("alt.general" 3 (1 . 1))
11531 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11532 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11533 ;; The first item is the group name; the second is the subscription
11534 ;; level; the third is either a range of a list of ranges of read
11535 ;; articles, the optional fourth element is a list of marked articles,
11536 ;; the optional fifth element is the select method.
11537 ;;
11538 ;; Gnus internal format of gnus-newsrc-hashtb:
11539 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11540 ;; This is the entry for "alt.misc". The first element is the number
11541 ;; of unread articles in "alt.misc". The cdr of this entry is the
11542 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11543 ;; trivial to remove or add new elements into gnus-newsrc-alist
11544 ;; without scanning the entire list. So, to get the actual information
11545 ;; of "alt.misc", you'd say something like 
11546 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11547 ;;
11548 ;; Gnus internal format of gnus-active-hashtb:
11549 ;; ((1 . 1))
11550 ;;  (5 . 10))
11551 ;;  (67 . 99)) ...)
11552 ;; The only element in each entry in this hash table is a range of
11553 ;; (possibly) available articles. (Articles in this range may have
11554 ;; been expired or canceled.)
11555 ;;
11556 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11557 ;; ("alt.misc" "alt.test" "alt.general" ...)
11558
11559 (defun gnus-setup-news (&optional rawfile level)
11560   "Setup news information.
11561 If RAWFILE is non-nil, the .newsrc file will also be read.
11562 If LEVEL is non-nil, the news will be set up at level LEVEL."
11563   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11564     ;; Clear some variables to re-initialize news information.
11565     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11566
11567     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11568     (if init (gnus-read-newsrc-file rawfile))
11569
11570     ;; Read the active file and create `gnus-active-hashtb'.
11571     ;; If `gnus-read-active-file' is nil, then we just create an empty
11572     ;; hash table. The partial filling out of the hash table will be
11573     ;; done in `gnus-get-unread-articles'.
11574     (if (and gnus-read-active-file 
11575              (not level))
11576         (gnus-read-active-file)
11577       (setq gnus-active-hashtb (make-vector 4095 0)))
11578
11579     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11580
11581     ;; Find the number of unread articles in each non-dead group.
11582     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11583     ;; Find new newsgroups and treat them.
11584     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11585              (gnus-server-opened gnus-select-method))
11586         (gnus-find-new-newsgroups))
11587     (if (and init gnus-check-bogus-newsgroups 
11588              gnus-read-active-file (not level)
11589              (gnus-server-opened gnus-select-method))
11590         (gnus-check-bogus-newsgroups))))
11591
11592 (defun gnus-find-new-newsgroups ()
11593   "Search for new newsgroups and add them.
11594 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11595 The `-n' option line from .newsrc is respected."
11596   (interactive)
11597   (or (gnus-check-first-time-used)
11598       (if (or (consp gnus-check-new-newsgroups)
11599               (eq gnus-check-new-newsgroups 'ask-server))
11600           (gnus-ask-server-for-new-groups)
11601         (let ((groups 0)
11602               group new-newsgroups)
11603           (or gnus-have-read-active-file (gnus-read-active-file))
11604           (setq gnus-newsrc-last-checked-date (current-time-string))
11605           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11606           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11607           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11608           (mapatoms
11609            (lambda (sym)
11610              (setq group (symbol-name sym))
11611              (if (or (gnus-gethash group gnus-killed-hashtb)
11612                      (gnus-gethash group gnus-newsrc-hashtb))
11613                  ()
11614                (let ((do-sub (gnus-matches-options-n group)))
11615                  (cond ((eq do-sub 'subscribe)
11616                         (setq groups (1+ groups))
11617                         (gnus-sethash group group gnus-killed-hashtb)
11618                         (funcall 
11619                          gnus-subscribe-options-newsgroup-method group))
11620                        ((eq do-sub 'ignore)
11621                         nil)
11622                        (t
11623                         (setq groups (1+ groups))
11624                         (gnus-sethash group group gnus-killed-hashtb)
11625                         (if gnus-subscribe-hierarchical-interactive
11626                             (setq new-newsgroups (cons group new-newsgroups))
11627                           (funcall gnus-subscribe-newsgroup-method group)))))))
11628            gnus-active-hashtb)
11629           (if new-newsgroups 
11630               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11631           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11632           (if (> groups 0)
11633               (gnus-message 6 "%d new newsgroup%s arrived." 
11634                             groups (if (> groups 1) "s have" " has")))))))
11635
11636 (defun gnus-matches-options-n (group)
11637   ;; Returns `subscribe' if the group is to be uncoditionally
11638   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11639   ;; no match for the group.
11640
11641   ;; First we check the two user variables.
11642   (cond
11643    ((and gnus-options-subscribe
11644          (string-match gnus-options-subscribe group))
11645     'subscribe)
11646    ((and gnus-options-not-subscribe
11647          (string-match gnus-options-not-subscribe group))
11648     'ignore)
11649    ;; Then we go through the list that was retrieved from the .newsrc
11650    ;; file.  This list has elements on the form 
11651    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11652    ;; is in the reverse order of the options line) is returned.
11653    (t
11654     (let ((regs gnus-newsrc-options-n))
11655       (while (and regs
11656                   (not (string-match (car (car regs)) group)))
11657         (setq regs (cdr regs)))
11658       (and regs (cdr (car regs)))))))
11659
11660 (defun gnus-ask-server-for-new-groups ()
11661   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11662          (methods (cons gnus-select-method 
11663                         (append
11664                          (and (consp gnus-check-new-newsgroups)
11665                               gnus-check-new-newsgroups)
11666                          gnus-secondary-select-methods)))
11667          (groups 0)
11668          (new-date (current-time-string))
11669          hashtb group new-newsgroups got-new)
11670     ;; Go thorugh both primary and secondary select methods and
11671     ;; request new newsgroups.  
11672     (while methods
11673       (and (or (gnus-server-opened (car methods))
11674                (gnus-open-server (car methods)))
11675            (gnus-request-newgroups date (car methods))
11676            (save-excursion
11677              (setq got-new t)
11678              (set-buffer nntp-server-buffer)
11679              (or hashtb (setq hashtb (gnus-make-hashtable 
11680                                       (count-lines (point-min) (point-max)))))
11681              ;; Enter all the new groups in a hashtable.
11682              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11683       (setq methods (cdr methods)))
11684     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11685     ;; Now all new groups from all select methods are in `hashtb'.
11686     (mapatoms
11687      (lambda (group-sym)
11688        (setq group (symbol-name group-sym))
11689        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11690                (member group gnus-zombie-list)
11691                (member group gnus-killed-list))
11692            ;; The group is already known.
11693            ()
11694          (and (symbol-value group-sym)
11695               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11696          (let ((do-sub (gnus-matches-options-n group)))
11697            (cond ((eq do-sub 'subscribe)
11698                   (setq groups (1+ groups))
11699                   (gnus-sethash group group gnus-killed-hashtb)
11700                   (funcall 
11701                    gnus-subscribe-options-newsgroup-method group))
11702                  ((eq do-sub 'ignore)
11703                   nil)
11704                  (t
11705                   (setq groups (1+ groups))
11706                   (gnus-sethash group group gnus-killed-hashtb)
11707                   (if gnus-subscribe-hierarchical-interactive
11708                       (setq new-newsgroups (cons group new-newsgroups))
11709                     (funcall gnus-subscribe-newsgroup-method group)))))))
11710      hashtb)
11711     (if new-newsgroups 
11712         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11713     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11714     (if (> groups 0)
11715         (gnus-message 6 "%d new newsgroup%s arrived." 
11716                       groups (if (> groups 1) "s have" " has")))
11717     got-new))
11718
11719 (defun gnus-check-first-time-used ()
11720   (if (or (> (length gnus-newsrc-alist) 1)
11721           (file-exists-p gnus-startup-file)
11722           (file-exists-p (concat gnus-startup-file ".el"))
11723           (file-exists-p (concat gnus-startup-file ".eld")))
11724       nil
11725     (gnus-message 6 "First time user; subscribing you to default groups")
11726     (or gnus-have-read-active-file (gnus-read-active-file))
11727     (setq gnus-newsrc-last-checked-date (current-time-string))
11728     (let ((groups gnus-default-subscribed-newsgroups)
11729           group)
11730       (if (eq groups t)
11731           nil
11732         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11733         (mapatoms
11734          (lambda (sym)
11735            (setq group (symbol-name sym))
11736            (let ((do-sub (gnus-matches-options-n group)))
11737              (cond ((eq do-sub 'subscribe)
11738                     (gnus-sethash group group gnus-killed-hashtb)
11739                     (funcall 
11740                      gnus-subscribe-options-newsgroup-method group))
11741                    ((eq do-sub 'ignore)
11742                     nil)
11743                    (t
11744                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11745          gnus-active-hashtb)
11746         (while groups
11747           (if (gnus-gethash (car groups) gnus-active-hashtb)
11748               (gnus-group-change-level 
11749                (car groups) gnus-level-default-subscribed gnus-level-killed))
11750           (setq groups (cdr groups)))
11751         (gnus-group-make-help-group)
11752         (and gnus-novice-user
11753              (gnus-message 7 "`A k' to list killed groups"))))))
11754
11755 (defun gnus-subscribe-group (group previous &optional method)
11756   (gnus-group-change-level 
11757    (if method
11758        (list t group gnus-level-default-subscribed nil nil method)
11759      group) 
11760    gnus-level-default-subscribed gnus-level-killed previous t))
11761
11762 ;; `gnus-group-change-level' is the fundamental function for changing
11763 ;; subscription levels of newsgroups. This might mean just changing
11764 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11765 ;; again, which subscribes/unsubscribes a group, which is equally
11766 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11767 ;; from 8-9 to 1-7 means that you remove the group from the list of
11768 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11769 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11770 ;; which is trivial.
11771 ;; ENTRY can either be a string (newsgroup name) or a list (if
11772 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11773 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11774 ;; entries. 
11775 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11776 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11777 ;; after. 
11778 (defun gnus-group-change-level (entry level &optional oldlevel
11779                                       previous fromkilled)
11780   (let (group info active num)
11781     ;; Glean what info we can from the arguments
11782     (if (consp entry)
11783         (if fromkilled (setq group (nth 1 entry))
11784           (setq group (car (nth 2 entry))))
11785       (setq group entry))
11786     (if (and (stringp entry)
11787              oldlevel 
11788              (< oldlevel gnus-level-zombie))
11789         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11790     (if (and (not oldlevel)
11791              (consp entry))
11792         (setq oldlevel (car (cdr (nth 2 entry)))))
11793     (if (stringp previous)
11794         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11795
11796     (gnus-dribble-enter
11797      (format "(gnus-group-change-level %S %S %S %S %S)" 
11798              group level oldlevel (car (nth 2 previous)) fromkilled))
11799     
11800     ;; Then we remove the newgroup from any old structures, if needed.
11801     ;; If the group was killed, we remove it from the killed or zombie
11802     ;; list. If not, and it is in fact going to be killed, we remove
11803     ;; it from the newsrc hash table and assoc.
11804     (cond ((>= oldlevel gnus-level-zombie)
11805            (if (= oldlevel gnus-level-zombie)
11806                (setq gnus-zombie-list (delete group gnus-zombie-list))
11807              (setq gnus-killed-list (delete group gnus-killed-list))))
11808           (t
11809            (if (>= level gnus-level-zombie)
11810                (progn
11811                  (gnus-sethash (car (nth 2 entry))
11812                                nil gnus-newsrc-hashtb)
11813                  (if (nth 3 entry)
11814                      (setcdr (gnus-gethash (car (nth 3 entry))
11815                                            gnus-newsrc-hashtb)
11816                              (cdr entry)))
11817                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11818
11819     ;; Finally we enter (if needed) the list where it is supposed to
11820     ;; go, and change the subscription level. If it is to be killed,
11821     ;; we enter it into the killed or zombie list.
11822     (cond ((>= level gnus-level-zombie)
11823            ;; Remove from the hash table.
11824            (gnus-sethash group nil gnus-newsrc-hashtb)
11825            (or (gnus-group-foreign-p group)
11826                ;; We do not enter foreign groups into the list of dead
11827                ;; groups.  
11828                (if (= level gnus-level-zombie)
11829                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11830                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11831           (t
11832            ;; If the list is to be entered into the newsrc assoc, and
11833            ;; it was killed, we have to create an entry in the newsrc
11834            ;; hashtb format and fix the pointers in the newsrc assoc.
11835            (if (>= oldlevel gnus-level-zombie)
11836                (progn
11837                  (if (listp entry)
11838                      (progn
11839                        (setq info (cdr entry))
11840                        (setq num (car entry)))
11841                    (setq active (gnus-gethash group gnus-active-hashtb))
11842                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11843                    ;; Check whether the group is foreign. If so, the
11844                    ;; foreign select method has to be entered into the
11845                    ;; info. 
11846                    (let ((method (gnus-group-method-name group)))
11847                      (if (eq method gnus-select-method)
11848                          (setq info (list group level nil))
11849                        (setq info (list group level nil nil method)))))
11850                  (or previous 
11851                      (setq previous 
11852                            (let ((p gnus-newsrc-alist))
11853                              (while (cdr (cdr p))
11854                                (setq p (cdr p)))
11855                              p)))
11856                  (setq entry (cons info (cdr (cdr previous))))
11857                  (if (cdr previous)
11858                      (progn
11859                        (setcdr (cdr previous) entry)
11860                        (gnus-sethash group (cons num (cdr previous)) 
11861                                      gnus-newsrc-hashtb))
11862                    (setcdr previous entry)
11863                    (gnus-sethash group (cons num previous)
11864                                  gnus-newsrc-hashtb))
11865                  (if (cdr entry)
11866                      (setcdr (gnus-gethash (car (car (cdr entry)))
11867                                            gnus-newsrc-hashtb)
11868                              entry)))
11869              ;; It was alive, and it is going to stay alive, so we
11870              ;; just change the level and don't change any pointers or
11871              ;; hash table entries.
11872              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11873
11874 (defun gnus-kill-newsgroup (newsgroup)
11875   "Obsolete function. Kills a newsgroup."
11876   (gnus-group-change-level
11877    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11878
11879 (defun gnus-check-bogus-newsgroups (&optional confirm)
11880   "Remove bogus newsgroups.
11881 If CONFIRM is non-nil, the user has to confirm the deletion of every
11882 newsgroup." 
11883   (let ((newsrc (cdr gnus-newsrc-alist))
11884         bogus group entry)
11885     (gnus-message 5 "Checking bogus newsgroups...")
11886     (or gnus-have-read-active-file (gnus-read-active-file))
11887     ;; Find all bogus newsgroup that are subscribed.
11888     (while newsrc
11889       (setq group (car (car newsrc)))
11890       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11891               (nth 4 (car newsrc))      ; Foreign
11892               (and confirm
11893                    (not (gnus-y-or-n-p
11894                          (format "Remove bogus newsgroup: %s " group)))))
11895           ;; Don't remove.
11896           ()
11897         ;; Found a bogus newsgroup.
11898         (setq bogus (cons group bogus)))
11899       (setq newsrc (cdr newsrc)))
11900     ;; Remove all bogus subscribed groups by first killing them, and
11901     ;; then removing them from the list of killed groups.
11902     (while bogus
11903       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11904            (progn
11905              (gnus-group-change-level entry gnus-level-killed)
11906              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11907       (setq bogus (cdr bogus)))
11908     ;; Then we remove all bogus groups from the list of killed and
11909     ;; zombie groups. They are are removed without confirmation.
11910     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11911           killed)
11912       (while dead-lists
11913         (setq killed (symbol-value (car dead-lists)))
11914         (while killed
11915           (setq group (car killed))
11916           (or (gnus-gethash group gnus-active-hashtb)
11917               ;; The group is bogus.
11918               (set (car dead-lists)
11919                    (delete group (symbol-value (car dead-lists)))))
11920           (setq killed (cdr killed)))
11921         (setq dead-lists (cdr dead-lists))))
11922     (gnus-message 5 "Checking bogus newsgroups...done")))
11923
11924 (defun gnus-check-duplicate-killed-groups ()
11925   "Remove duplicates from the list of killed groups."
11926   (interactive)
11927   (let ((killed gnus-killed-list))
11928     (while killed
11929       (gnus-message 9 "%d" (length killed))
11930       (setcdr killed (delete (car killed) (cdr killed)))
11931       (setq killed (cdr killed)))))
11932
11933 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11934 ;; and compute how many unread articles there are in each group.
11935 (defun gnus-get-unread-articles (&optional level) 
11936   (let* ((newsrc (cdr gnus-newsrc-alist))
11937          (level (or level (1+ gnus-level-subscribed)))
11938          (foreign-level
11939           (min 
11940            (cond ((and gnus-activate-foreign-newsgroups 
11941                        (not (numberp gnus-activate-foreign-newsgroups)))
11942                   (1+ gnus-level-subscribed))
11943                  ((numberp gnus-activate-foreign-newsgroups)
11944                   gnus-activate-foreign-newsgroups)
11945                  (t 0))
11946            level))
11947          info group active virtuals method)
11948     (gnus-message 5 "Checking new news...")
11949
11950     (while newsrc
11951       (setq info (car newsrc)
11952             group (car info)
11953             active (gnus-gethash group gnus-active-hashtb))
11954
11955       ;; Check newsgroups. If the user doesn't want to check them, or
11956       ;; they can't be checked (for instance, if the news server can't
11957       ;; be reached) we just set the number of unread articles in this
11958       ;; newsgroup to t. This means that Gnus thinks that there are
11959       ;; unread articles, but it has no idea how many.
11960       (if (and (setq method (nth 4 info))
11961                (not (gnus-server-equal gnus-select-method
11962                                        (gnus-server-get-method nil method)))
11963                (not (gnus-secondary-method-p method)))
11964           ;; These groups are foreign. Check the level.
11965           (if (<= (nth 1 info) foreign-level)
11966               (if (eq (car (if (stringp method) 
11967                                (gnus-server-to-method method)
11968                              (nth 4 info))) 'nnvirtual)
11969                   ;; We have to activate the virtual groups after all
11970                   ;; the others, so we just pop them on a list for
11971                   ;; now. 
11972                   (setq virtuals (cons info virtuals))
11973                 (and (setq active (gnus-activate-newsgroup (car info)))
11974                      ;; Close the groups as we look at them!
11975                      (gnus-close-group group))))
11976                 
11977         ;; These groups are native or secondary. 
11978         (if (and (not gnus-have-read-active-file)
11979                  (<= (nth 1 info) level))
11980             (setq active (gnus-activate-newsgroup (car info)))))
11981       
11982       (if active
11983           (gnus-get-unread-articles-in-group info active)
11984         ;; The group couldn't be reached, so we nix out the number of
11985         ;; unread articles and stuff.
11986         (gnus-sethash group nil gnus-active-hashtb)
11987         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11988
11989       (setq newsrc (cdr newsrc)))
11990
11991     ;; Activate the virtual groups. This has to be done after all the
11992     ;; other groups. 
11993     ;; !!! If one virtual group contains another virtual group, even
11994     ;; doing it this way might cause problems.
11995    (while virtuals
11996       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11997            (gnus-get-unread-articles-in-group (car virtuals) active))
11998       (setq virtuals (cdr virtuals)))
11999
12000     (gnus-message 5 "Checking new news...done")))
12001
12002 ;; Create a hash table out of the newsrc alist. The `car's of the
12003 ;; alist elements are used as keys.
12004 (defun gnus-make-hashtable-from-newsrc-alist ()
12005   (let ((alist gnus-newsrc-alist)
12006         (ohashtb gnus-newsrc-hashtb)
12007         prev)
12008     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12009     (setq alist 
12010           (setq prev (setq gnus-newsrc-alist 
12011                            (if (equal (car (car gnus-newsrc-alist))
12012                                       "dummy.group")
12013                                gnus-newsrc-alist
12014                              (cons (list "dummy.group" 0 nil) alist)))))
12015     (while alist
12016       (gnus-sethash (car (car alist)) 
12017                     (cons (and ohashtb (car (gnus-gethash 
12018                                              (car (car alist)) ohashtb))) 
12019                           prev) gnus-newsrc-hashtb)
12020       (setq prev alist
12021             alist (cdr alist)))))
12022
12023 (defun gnus-make-hashtable-from-killed ()
12024   "Create a hash table from the killed and zombie lists."
12025   (let ((lists '(gnus-killed-list gnus-zombie-list))
12026         list)
12027     (setq gnus-killed-hashtb 
12028           (gnus-make-hashtable 
12029            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12030     (while lists
12031       (setq list (symbol-value (car lists)))
12032       (setq lists (cdr lists))
12033       (while list
12034         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12035         (setq list (cdr list))))))
12036
12037 (defun gnus-get-unread-articles-in-group (info active)
12038   (let* ((range (nth 2 info))
12039          (num 0)
12040          (marked (nth 3 info)))
12041     ;; If a cache is present, we may have to alter the active info.
12042     (and gnus-use-cache
12043          (gnus-cache-possibly-alter-active (car info) active))
12044     ;; Modify the list of read articles according to what articles 
12045     ;; are available; then tally the unread articles and add the
12046     ;; number to the group hash table entry.
12047     (cond ((zerop (cdr active))
12048            (setq num 0))
12049           ((not range)
12050            (setq num (- (1+ (cdr active)) (car active))))
12051           ((not (listp (cdr range)))
12052            ;; Fix a single (num . num) range according to the
12053            ;; active hash table.
12054            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12055            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12056            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12057            ;; Compute number of unread articles.
12058            (setq num (max 0 (- (cdr active) 
12059                                (- (1+ (cdr range)) (car range))))))
12060           (t
12061            ;; The read list is a list of ranges. Fix them according to
12062            ;; the active hash table.
12063            ;; First peel off any elements that are below the lower
12064            ;; active limit. 
12065            (while (and (cdr range) 
12066                        (>= (car active) 
12067                            (or (and (atom (car (cdr range))) (car (cdr range)))
12068                                (car (car (cdr range))))))
12069              (if (numberp (car range))
12070                  (setcar range 
12071                          (cons (car range) 
12072                                (or (and (numberp (car (cdr range)))
12073                                         (car (cdr range))) 
12074                                    (cdr (car (cdr range))))))
12075                (setcdr (car range) 
12076                        (or (and (numberp (nth 1 range)) (nth 1 range))
12077                            (cdr (car (cdr range))))))
12078              (setcdr range (cdr (cdr range))))
12079            ;; Adjust the first element to be the same as the lower limit. 
12080            (if (and (not (atom (car range))) 
12081                     (< (cdr (car range)) (car active)))
12082                (setcdr (car range) (1- (car active))))
12083            ;; Then we want to peel off any elements that are higher
12084            ;; than the upper active limit.  
12085            (let ((srange range))
12086              ;; Go past all legal elements.
12087              (while (and (cdr srange) 
12088                          (<= (or (and (atom (car (cdr srange)))
12089                                       (car (cdr srange)))
12090                                  (car (car (cdr srange)))) (cdr active)))
12091                (setq srange (cdr srange)))
12092              (if (cdr srange)
12093                  ;; Nuke all remaining illegal elements.
12094                  (setcdr srange nil))
12095
12096              ;; Adjust the final element.
12097              (if (and (not (atom (car srange)))
12098                       (> (cdr (car srange)) (cdr active)))
12099                  (setcdr (car srange) (cdr active))))
12100            ;; Compute the number of unread articles.
12101            (while range
12102              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12103                                          (cdr (car range))))
12104                                  (or (and (atom (car range)) (car range))
12105                                      (car (car range))))))
12106              (setq range (cdr range)))
12107            (setq num (max 0 (- (cdr active) num)))))
12108     (and info
12109          (progn
12110            (and (assq 'tick marked)
12111                 (inline (gnus-remove-illegal-marked-articles
12112                          (assq 'tick marked) (nth 2 info))))
12113            (and (assq 'dormant marked)
12114                 (inline (gnus-remove-illegal-marked-articles
12115                          (assq 'dormant marked) (nth 2 info))))
12116            (setcar
12117             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12118             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12119                                 (length (cdr (assq 'dormant marked)))))))))
12120     num))
12121
12122 (defun gnus-remove-illegal-marked-articles (marked ranges)
12123   (let ((m (cdr marked)))
12124     ;; Make sure that all ticked articles are a subset of the unread
12125     ;; articles. 
12126     (while m
12127       (if (gnus-member-of-range (car m) ranges)
12128           (setcdr marked (cdr m))
12129         (setq marked m))
12130       (setq m (cdr m)))))
12131
12132 (defun gnus-activate-newsgroup (group)
12133   (let ((method (gnus-find-method-for-group group))
12134         active)
12135     (and (or (gnus-server-opened method) (gnus-open-server method))
12136          (gnus-request-group group)
12137          (save-excursion
12138            (set-buffer nntp-server-buffer)
12139            (goto-char (point-min))
12140            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12141                 (progn
12142                   (goto-char (match-beginning 1))
12143                   (gnus-sethash 
12144                    group (setq active (cons (read (current-buffer))
12145                                             (read (current-buffer))))
12146                    gnus-active-hashtb))
12147                 active)))))
12148
12149 (defun gnus-update-read-articles 
12150   (group unread unselected ticked &optional domarks replied expirable killed
12151          dormant bookmark score)
12152   "Update the list of read and ticked articles in GROUP using the
12153 UNREAD and TICKED lists.
12154 Note: UNSELECTED has to be sorted over `<'.
12155 Returns whether the updating was successful."
12156   (let* ((active (or gnus-newsgroup-active 
12157                      (gnus-gethash group gnus-active-hashtb)))
12158          (entry (gnus-gethash group gnus-newsrc-hashtb))
12159          (info (nth 2 entry))
12160          (marked (nth 3 info))
12161          (prev 1)
12162          (unread (sort (copy-sequence unread) (function <)))
12163          read)
12164     (if (or (not info) (not active))
12165         ;; There is no info on this group if it was, in fact,
12166         ;; killed. Gnus stores no information on killed groups, so
12167         ;; there's nothing to be done. 
12168         ;; One could store the information somewhere temporarily,
12169         ;; perhaps... Hmmm... 
12170         ()
12171       ;; Remove any negative articles numbers.
12172       (while (and unread (< (car unread) 0))
12173         (setq unread (cdr unread)))
12174       ;; Remove any expired article numbers
12175       (while (and unread (< (car unread) (car active)))
12176         (setq unread (cdr unread)))
12177       (while (and ticked (< (car ticked) (car active)))
12178         (setq ticked (cdr ticked)))
12179       (while (and dormant (< (car dormant) (car active)))
12180         (setq dormant (cdr dormant)))
12181       (setq unread (sort (append unselected unread) '<))
12182       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12183       (setcar entry (max 0 (- (length unread) (length ticked) 
12184                               (length dormant))))
12185       ;; Compute the ranges of read articles by looking at the list of
12186       ;; unread articles.  
12187       (while unread
12188         (if (/= (car unread) prev)
12189             (setq read (cons (if (= prev (1- (car unread))) prev
12190                                (cons prev (1- (car unread)))) read)))
12191         (setq prev (1+ (car unread)))
12192         (setq unread (cdr unread)))
12193       (if (<= prev (cdr active))
12194           (setq read (cons (cons prev (cdr active)) read)))
12195       ;; Enter this list into the group info.
12196       (setcar (cdr (cdr info)) 
12197               (if (> (length read) 1) (nreverse read) read))
12198       ;; Enter the list of ticked articles.
12199       (gnus-set-marked-articles 
12200        info ticked
12201        (if domarks replied (cdr (assq 'reply marked)))
12202        (if domarks expirable (cdr (assq 'expire marked)))
12203        (if domarks killed (cdr (assq 'killed marked)))
12204        (if domarks dormant (cdr (assq 'dormant marked)))
12205        (if domarks bookmark (cdr (assq 'bookmark marked)))
12206        (if domarks score (cdr (assq 'score marked))))
12207       t)))
12208
12209 (defun gnus-make-articles-unread (group articles)
12210   "Mark ARTICLES in GROUP as unread."
12211   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12212                           (gnus-gethash (gnus-group-real-name group)
12213                                         gnus-newsrc-hashtb))))
12214          (ranges (nth 2 info))
12215          news)
12216     (while articles
12217       (and (gnus-member-of-range (car articles) ranges)
12218            (setq news (cons (car articles) news)))
12219       (setq articles (cdr articles)))
12220     (if (not news)
12221         ()
12222       (setcar (nthcdr 2 info)
12223               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12224       (gnus-group-update-group group t))))
12225
12226 ;; Get the active file(s) from the backend(s).
12227 (defun gnus-read-active-file ()
12228   (gnus-group-set-mode-line)
12229   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12230                          (gnus-open-server gnus-select-method))
12231                      ;; The native server is available.
12232                      (cons gnus-select-method gnus-secondary-select-methods)
12233                    ;; The native server is down, so we just do the
12234                    ;; secondary ones.   
12235                    gnus-secondary-select-methods))
12236         list-type)
12237     (setq gnus-have-read-active-file nil)
12238     (save-excursion
12239       (set-buffer nntp-server-buffer)
12240       (while methods
12241         (let* ((where (nth 1 (car methods)))
12242                (mesg (format "Reading active file%s via %s..."
12243                              (if (and where (not (zerop (length where))))
12244                                  (concat " from " where) "")
12245                              (car (car methods)))))
12246           (gnus-message 5 mesg)
12247           (gnus-check-news-server (car methods))
12248           (cond 
12249            ((and (eq gnus-read-active-file 'some)
12250                  (gnus-check-backend-function
12251                   'retrieve-groups (car (car methods))))
12252             (let ((newsrc (cdr gnus-newsrc-alist))
12253                   groups)
12254               (while newsrc
12255                 (and (gnus-server-equal 
12256                       (gnus-find-method-for-group
12257                        (car (car newsrc)) (car newsrc))
12258                       (gnus-server-get-method nil (car methods)))
12259                      (setq groups (cons (car (car newsrc)) groups)))
12260                 (setq newsrc (cdr newsrc)))
12261               (setq list-type (gnus-retrieve-groups groups (car methods)))
12262               (cond ((not list-type)
12263                      (gnus-message 
12264                       1 "Cannot read partial active file from %s server." 
12265                       (car (car methods)))
12266                      (ding)
12267                      (sit-for 2))
12268                     ((eq list-type 'active)
12269                      (gnus-active-to-gnus-format (car methods)))
12270                     (t
12271                      (gnus-groups-to-gnus-format (car methods))))))
12272            (t
12273             (if (not (gnus-request-list (car methods)))
12274                 (progn
12275                   (gnus-message 1 "Cannot read active file from %s server." 
12276                                 (car (car methods)))
12277                   (ding))
12278               (gnus-active-to-gnus-format (car methods))
12279               (setq gnus-have-read-active-file t)
12280               (gnus-message 5 "%sdone" mesg)))))
12281         (setq methods (cdr methods))))))
12282
12283 ;; Read an active file and place the results in `gnus-active-hashtb'.
12284 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12285   (let ((cur (current-buffer))
12286         (hashtb (or hashtb 
12287                     (if (and gnus-active-hashtb 
12288                              (not (equal method gnus-select-method)))
12289                         gnus-active-hashtb
12290                       (setq gnus-active-hashtb
12291                             (if (equal method gnus-select-method)
12292                                 (gnus-make-hashtable 
12293                                  (count-lines (point-min) (point-max)))
12294                               (gnus-make-hashtable 4096)))))))
12295     ;; Delete unnecessary lines.
12296     (goto-char (point-min))
12297     (while (search-forward "\nto." nil t)
12298       (delete-region (1+ (match-beginning 0)) 
12299                      (progn (forward-line 1) (point))))
12300     (or (string= gnus-ignored-newsgroups "")
12301         (progn
12302           (goto-char (point-min))
12303           (delete-matching-lines gnus-ignored-newsgroups)))
12304     ;; If these are groups from a foreign select method, we insert the
12305     ;; group prefix in front of the group names. 
12306     (and method (not (eq method gnus-select-method))
12307          (let ((prefix (gnus-group-prefixed-name "" method)))
12308            (goto-char (point-min))
12309            (while (and (not (eobp))
12310                        (progn (insert prefix)
12311                               (zerop (forward-line 1)))))))
12312     (goto-char (point-min))
12313     ;; Store active file in hashtable.
12314     (goto-char (point-min))
12315     (if (string-match "%[oO]" gnus-group-line-format)
12316         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12317         ;; If we want information on moderated groups, we use this
12318         ;; loop...   
12319         (let* ((mod-hashtb (make-vector 7 0))
12320                (m (intern "m" mod-hashtb))
12321                group max min)
12322           (while (not (eobp))
12323             (condition-case nil
12324                 (progn
12325                   (narrow-to-region (point) (gnus-point-at-eol))
12326                   (setq group (let ((obarray hashtb)) (read cur)))
12327                   (if (and (numberp (setq max (read cur)))
12328                            (numberp (setq min (read cur))))
12329                       (set group (cons min max))
12330                     (set group nil))
12331                   ;; Enter moderated groups into a list.
12332                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12333                       (setq gnus-moderated-list 
12334                             (cons (symbol-name group) gnus-moderated-list))))
12335               (error nil))
12336             (widen)
12337             (forward-line 1)))
12338       ;; And if we do not care about moderation, we use this loop,
12339       ;; which is faster.
12340       (let (group max min)
12341         (while (not (eobp))
12342           (condition-case ()
12343               (progn
12344                 (narrow-to-region (point) (gnus-point-at-eol))
12345                 ;; group gets set to a symbol interned in the hash table
12346                 ;; (what a hack!!)
12347                 (setq group (let ((obarray hashtb)) (read cur)))
12348                 (if (and (numberp (setq max (read cur)))
12349                          (numberp (setq min (read cur))))
12350                     (set group (cons min max))
12351                   (set group nil)))
12352             (error 
12353              (progn 
12354                (if ignore-errors
12355                    (set group nil)
12356                  (ding) 
12357                  (gnus-message 3 "Warning - illegal active: %s"
12358                                (buffer-substring 
12359                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12360                  nil))))
12361           (widen)
12362           (forward-line 1))))))
12363
12364 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12365   ;; Parse a "groups" active file.
12366   (let ((cur (current-buffer))
12367         (hashtb (or hashtb 
12368                     (if method
12369                         gnus-active-hashtb
12370                       (setq gnus-active-hashtb
12371                             (gnus-make-hashtable 
12372                              (count-lines (point-min) (point-max)))))))
12373         (prefix (and method (not (eq method gnus-select-method))
12374                      (gnus-group-prefixed-name "" method))))
12375
12376     (goto-char (point-min))
12377     (condition-case ()
12378         ;; We split this into to separate loops, one with the prefix
12379         ;; and one without to speed the reading up somewhat.
12380         (if prefix
12381             (let (min max opoint)
12382               (while (not (eobp))
12383                 (read cur) (read cur)
12384                 (setq min (read cur)
12385                       max (read cur)
12386                       opoint (point))
12387                 (skip-chars-forward " \t")
12388                 (insert prefix)
12389                 (goto-char opoint)
12390                 (set (let ((obarray hashtb)) (read cur)) 
12391                      (cons min max))
12392                 (forward-line 1)))
12393           (let (min max)
12394             (while (not (eobp))
12395               (if (= (following-char) ?2)
12396                   (progn
12397                     (read cur) (read cur)
12398                     (setq min (read cur)
12399                           max (read cur))
12400                     (set (let ((obarray hashtb)) (read cur)) 
12401                          (cons min max))))
12402               (forward-line 1))))
12403       (error 
12404        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12405
12406 (defun gnus-read-newsrc-file (&optional force)
12407   "Read startup file.
12408 If FORCE is non-nil, the .newsrc file is read."
12409   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12410   ;; Reset variables that might be defined in the .newsrc.eld file.
12411   (let ((variables gnus-variable-list))
12412     (while variables
12413       (set (car variables) nil)
12414       (setq variables (cdr variables))))
12415   (let* ((newsrc-file gnus-current-startup-file)
12416          (quick-file (concat newsrc-file ".el")))
12417     (save-excursion
12418       ;; We always load the .newsrc.eld file. If always contains
12419       ;; much information that can not be gotten from the .newsrc
12420       ;; file (ticked articles, killed groups, foreign methods, etc.)
12421       (gnus-read-newsrc-el-file quick-file)
12422  
12423       (if (or force
12424               (and (file-newer-than-file-p newsrc-file quick-file)
12425                    (file-newer-than-file-p newsrc-file 
12426                                            (concat quick-file "d")))
12427               (not gnus-newsrc-alist))
12428           ;; We read the .newsrc file. Note that if there if a
12429           ;; .newsrc.eld file exists, it has already been read, and
12430           ;; the `gnus-newsrc-hashtb' has been created. While reading
12431           ;; the .newsrc file, Gnus will only use the information it
12432           ;; can find there for changing the data already read -
12433           ;; ie. reading the .newsrc file will not trash the data
12434           ;; already read (except for read articles).
12435           (save-excursion
12436             (gnus-message 5 "Reading %s..." newsrc-file)
12437             (set-buffer (find-file-noselect newsrc-file))
12438             (buffer-disable-undo (current-buffer))
12439             (gnus-newsrc-to-gnus-format)
12440             (kill-buffer (current-buffer))
12441             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12442
12443 (defun gnus-read-newsrc-el-file (file)
12444   (let ((ding-file (concat file "d")))
12445     ;; We always, always read the .eld file.
12446     (gnus-message 5 "Reading %s..." ding-file)
12447     (let (gnus-newsrc-assoc)
12448       (condition-case nil
12449           (load ding-file t t t)
12450         (error nil))
12451       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12452     (let ((inhibit-quit t))
12453       (gnus-uncompress-newsrc-alist))
12454     (gnus-make-hashtable-from-newsrc-alist)
12455     (if (not (file-newer-than-file-p file ding-file))
12456         ()
12457       ;; Old format quick file
12458       (gnus-message 5 "Reading %s..." file)
12459       ;; The .el file is newer than the .eld file, so we read that one
12460       ;; as well. 
12461       (gnus-read-old-newsrc-el-file file))))
12462
12463 ;; Parse the old-style quick startup file
12464 (defun gnus-read-old-newsrc-el-file (file)
12465   (let (newsrc killed marked group m)
12466     (prog1
12467         (let ((gnus-killed-assoc nil)
12468               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12469           (prog1
12470               (condition-case nil
12471                   (load file t t t)
12472                 (error nil))
12473             (setq newsrc gnus-newsrc-assoc
12474                   killed gnus-killed-assoc
12475                   marked gnus-marked-assoc)))
12476       (setq gnus-newsrc-alist nil)
12477       (while newsrc
12478         (setq group (car newsrc))
12479         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12480           (if info
12481               (progn
12482                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12483                 (setcar (cdr info)
12484                         (if (nth 1 group) gnus-level-default-subscribed 
12485                           gnus-level-default-unsubscribed))
12486                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12487             (setq gnus-newsrc-alist
12488                   (cons 
12489                    (setq info
12490                          (list (car group)
12491                                (if (nth 1 group) gnus-level-default-subscribed
12492                                  gnus-level-default-unsubscribed) 
12493                                (cdr (cdr group))))
12494                    gnus-newsrc-alist)))
12495           (if (setq m (assoc (car group) marked))
12496             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12497         (setq newsrc (cdr newsrc)))
12498       (setq newsrc killed)
12499       (while newsrc
12500         (setcar newsrc (car (car newsrc)))
12501         (setq newsrc (cdr newsrc)))
12502       (setq gnus-killed-list killed))
12503     ;; The .el file version of this variable does not begin with
12504     ;; "options", while the .eld version does, so we just add it if it
12505     ;; isn't there.
12506     (and
12507      gnus-newsrc-options 
12508      (progn
12509        (and (not (string-match "^ *options" gnus-newsrc-options))
12510             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12511        (and (not (string-match "\n$" gnus-newsrc-options))
12512             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12513     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12514     (gnus-make-hashtable-from-newsrc-alist)))
12515       
12516 (defun gnus-make-newsrc-file (file)
12517   "Make server dependent file name by catenating FILE and server host name."
12518   (let* ((file (expand-file-name file nil))
12519          (real-file (concat file "-" (nth 1 gnus-select-method))))
12520     (if (file-exists-p real-file)
12521         real-file file)))
12522
12523 (defun gnus-uncompress-newsrc-alist ()
12524   ;; Uncompress all lists of marked articles in the newsrc assoc.
12525   (let ((newsrc gnus-newsrc-alist)
12526         marked)
12527     (while newsrc
12528       (if (not (setq marked (nth 3 (car newsrc))))
12529           ()
12530         (while marked
12531           (or (eq 'score (car (car marked)))
12532               (eq 'bookmark (car (car marked)))
12533               (eq 'killed (car (car marked)))
12534               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12535           (setq marked (cdr marked))))
12536       (setq newsrc (cdr newsrc)))))
12537
12538 (defun gnus-compress-newsrc-alist ()
12539   ;; Compress all lists of marked articles in the newsrc assoc.
12540   (let ((newsrc gnus-newsrc-alist)
12541         marked)
12542     (while newsrc
12543       (if (not (setq marked (nth 3 (car newsrc))))
12544           ()
12545         (while marked
12546           (or (eq 'score (car (car marked)))
12547               (eq 'bookmark (car (car marked)))
12548               (eq 'killed (car (car marked)))
12549               (setcdr (car marked) 
12550                       (condition-case ()
12551                           (gnus-compress-sequence 
12552                            (sort (cdr (car marked)) '<) t)
12553                         (error (cdr (car marked))))))
12554           (setq marked (cdr marked))))
12555       (setq newsrc (cdr newsrc)))))
12556
12557 (defun gnus-newsrc-to-gnus-format ()
12558   (setq gnus-newsrc-options "")
12559   (setq gnus-newsrc-options-n nil)
12560
12561   (or gnus-active-hashtb
12562       (setq gnus-active-hashtb (make-vector 4095 0)))
12563   (let ((buf (current-buffer))
12564         (already-read (> (length gnus-newsrc-alist) 1))
12565         group subscribed options-symbol newsrc
12566         symbol reads num1)
12567     (goto-char (point-min))
12568     ;; We intern the symbol `options' in the active hashtb so that we
12569     ;; can `eq' against it later.
12570     (setq options-symbol (intern "options" gnus-active-hashtb))
12571   
12572     (while (not (eobp))
12573       ;; We first read the first word on the line by narrowing and
12574       ;; then reading into `gnus-active-hashtb'.  Most groups will
12575       ;; already exist in that hashtb, so this will save some string
12576       ;; space.
12577       (narrow-to-region
12578        (point)
12579        (progn (skip-chars-forward "^ \t!:\n") (point)))
12580       (goto-char (point-min))
12581       (setq symbol 
12582             (and (/= (point-min) (point-max))
12583                  (let ((obarray gnus-active-hashtb)) (read buf))))
12584       (widen)
12585       ;; Now, the symbol we have read is either `options' or a group
12586       ;; name.  If it is an options line, we just add it to a string. 
12587       (cond 
12588        ((eq symbol options-symbol)
12589         (setq gnus-newsrc-options
12590               ;; This concatting is quite inefficient, but since our
12591               ;; thorough studies show that approx 99.37% of all
12592               ;; .newsrc files only contain a single options line, we
12593               ;; don't give a damn, frankly, my dear.
12594               (concat gnus-newsrc-options
12595                       (buffer-substring 
12596                        (gnus-point-at-bol)
12597                        ;; Options may continue on the next line.
12598                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12599                                 (progn (beginning-of-line) (point)))
12600                            (point))))))
12601        (symbol
12602         ;; It was a group name.
12603         (setq subscribed (= (following-char) ?:)
12604               group (symbol-name symbol)
12605               reads nil)
12606         (if (eolp)
12607             ;; If the line ends here, this is clearly a buggy line, so
12608             ;; we put point a the beginning of line and let the cond
12609             ;; below do the error handling.
12610             (beginning-of-line)
12611           ;; We skip to the beginning of the ranges.
12612           (skip-chars-forward "!: \t"))
12613         ;; We are now at the beginning of the list of read articles.
12614         ;; We read them range by range.
12615         (while
12616             (cond 
12617              ((looking-at "[0-9]+")
12618               ;; We narrow and read a number instead of buffer-substring/
12619               ;; string-to-int because it's faster. narrow/widen is
12620               ;; faster than save-restriction/narrow, and save-restriction
12621               ;; produces a garbage object.
12622               (setq num1 (progn
12623                            (narrow-to-region (match-beginning 0) (match-end 0))
12624                            (read buf)))
12625               (widen)
12626               ;; If the next character is a dash, then this is a range.
12627               (if (= (following-char) ?-)
12628                   (progn
12629                     ;; We read the upper bound of the range.
12630                     (forward-char 1)
12631                     (if (not (looking-at "[0-9]+"))
12632                         ;; This is a buggy line, by we pretend that
12633                         ;; it's kinda OK. Perhaps the user should be
12634                         ;; dinged? 
12635                         (setq reads (cons num1 reads))
12636                       (setq reads 
12637                             (cons 
12638                              (cons num1 (progn
12639                                           (narrow-to-region (match-beginning 0) 
12640                                                             (match-end 0))
12641                                           (read buf)))
12642                              reads))
12643                       (widen)))
12644                 ;; It was just a simple number, so we add it to the
12645                 ;; list of ranges.
12646                 (setq reads (cons num1 reads)))
12647               ;; If the next char in ?\n, then we have reached the end
12648               ;; of the line and return nil.
12649               (/= (following-char) ?\n))
12650              ((= (following-char) ?\n)
12651               ;; End of line, so we end.
12652               nil)
12653              (t
12654               ;; Not numbers and not eol, so this might be a buggy
12655               ;; line... 
12656               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12657                   (progn
12658                     ;; The line was buggy.
12659                     (setq group nil)
12660                     (gnus-message 3 "Mangled line: %s" 
12661                                   (buffer-substring (gnus-point-at-bol) 
12662                                                     (gnus-point-at-eol)))
12663                     (ding)
12664                     (sit-for 1)))
12665               nil))
12666           ;; Skip past ", ". Spaces are illegal in these ranges, but
12667           ;; we allow them, because it's a common mistake to put a
12668           ;; space after the comma.
12669           (skip-chars-forward ", "))
12670
12671         ;; We have already read .newsrc.eld, so we gently update the
12672         ;; data in the hash table with the information we have just
12673         ;; read. 
12674         (if (not group)
12675             ()
12676           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12677                 level)
12678             (if info
12679                 ;; There is an entry for this file in the alist.
12680                 (progn
12681                   (setcar (nthcdr 2 info) (nreverse reads))
12682                   ;; We update the level very gently.  In fact, we
12683                   ;; only change it if there's been a status change
12684                   ;; from subscribed to unsubscribed, or vice versa.
12685                   (setq level (nth 1 info))
12686                   (cond ((and (<= level gnus-level-subscribed)
12687                               (not subscribed))
12688                          (setq level (if reads
12689                                          gnus-level-default-unsubscribed 
12690                                        (1+ gnus-level-default-unsubscribed))))
12691                         ((and (> level gnus-level-subscribed) subscribed)
12692                          (setq level gnus-level-default-subscribed)))
12693                   (setcar (cdr info) level))
12694               ;; This is a new group.
12695               (setq info (list group 
12696                                (if subscribed
12697                                    gnus-level-default-subscribed 
12698                                  (if reads
12699                                      (1+ gnus-level-subscribed)
12700                                    gnus-level-default-unsubscribed))
12701                                (nreverse reads))))
12702             (setq newsrc (cons info newsrc))))))
12703       (forward-line 1))
12704     
12705     (setq newsrc (nreverse newsrc))
12706
12707     (if (not already-read)
12708         ()
12709       ;; We now have two newsrc lists - `newsrc', which is what we
12710       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12711       ;; what we've read from .newsrc.eld. We have to merge these
12712       ;; lists. We do this by "attaching" any (foreign) groups in the
12713       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12714       (let ((rc (cdr gnus-newsrc-alist))
12715             (prev gnus-newsrc-alist)
12716             entry mentry)
12717         (while rc
12718           (or (null (nth 4 (car rc))) ; It's a native group.
12719               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12720               (if (setq entry (assoc (car (car prev)) newsrc))
12721                   (setcdr (setq mentry (memq entry newsrc))
12722                           (cons (car rc) (cdr mentry)))
12723                 (setq newsrc (cons (car rc) newsrc))))
12724           (setq prev rc
12725                 rc (cdr rc)))))
12726
12727     (setq gnus-newsrc-alist newsrc)
12728     ;; We make the newsrc hashtb.
12729     (gnus-make-hashtable-from-newsrc-alist)
12730
12731     ;; Finally, if we read some options lines, we parse them.
12732     (or (string= gnus-newsrc-options "")
12733         (gnus-newsrc-parse-options gnus-newsrc-options))))
12734
12735 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12736 ;; The return value will be a list on the form
12737 ;; ((regexp1 . ignore)
12738 ;;  (regexp2 . subscribe)...)
12739 ;; When handling new newsgroups, groups that match a `ignore' regexp
12740 ;; will be ignored, and groups that match a `subscribe' regexp will be
12741 ;; subscribed. A line like
12742 ;; options -n !all rec.all
12743 ;; will lead to a list that looks like
12744 ;; (("^rec\\..+" . subscribe) 
12745 ;;  ("^.+" . ignore))
12746 ;; So all "rec.*" groups will be subscribed, while all the other
12747 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12748 ;; different from "options -n rec.all !all". 
12749 (defun gnus-newsrc-parse-options (options)
12750   (let (out eol)
12751     (save-excursion
12752       (gnus-set-work-buffer)
12753       (insert (regexp-quote options))
12754       ;; First we treat all continuation lines.
12755       (goto-char (point-min))
12756       (while (re-search-forward "\n[ \t]+" nil t)
12757         (replace-match " " t t))
12758       ;; Then we transform all "all"s into ".+"s.
12759       (goto-char (point-min))
12760       (while (re-search-forward "\\ball\\b" nil t)
12761         (replace-match ".+" t t))
12762       (goto-char (point-min))
12763       ;; We remove all other options than the "-n" ones.
12764       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12765         (replace-match " ")
12766         (forward-char -1))
12767       (goto-char (point-min))
12768
12769       ;; We are only interested in "options -n" lines - we
12770       ;; ignore the other option lines.
12771       (while (re-search-forward "[ \t]-n" nil t)
12772         (setq eol 
12773               (or (save-excursion
12774                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12775                          (- (point) 2)))
12776                   (gnus-point-at-eol)))
12777         ;; Search for all "words"...
12778         (while (re-search-forward "[^ \t,\n]+" eol t)
12779           (if (= (char-after (match-beginning 0)) ?!)
12780               ;; If the word begins with a bang (!), this is a "not"
12781               ;; spec. We put this spec (minus the bang) and the
12782               ;; symbol `ignore' into the list.
12783               (setq out (cons (cons (concat 
12784                                      "^" (buffer-substring 
12785                                           (1+ (match-beginning 0))
12786                                           (match-end 0)))
12787                                     'ignore) out))
12788             ;; There was no bang, so this is a "yes" spec.
12789             (setq out (cons (cons (concat 
12790                                    "^" (buffer-substring (match-beginning 0)
12791                                                          (match-end 0)))
12792                                   'subscribe) out)))))
12793     
12794       (setq gnus-newsrc-options-n out))))
12795                
12796
12797 (defun gnus-save-newsrc-file ()
12798   "Save .newsrc file."
12799   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12800   ;; from the variable gnus-newsrc-alist.
12801   (and (or gnus-newsrc-alist gnus-killed-list)
12802        gnus-current-startup-file
12803        (progn
12804          (run-hooks 'gnus-save-newsrc-hook)
12805          (save-excursion
12806            (if (or (not gnus-dribble-buffer)
12807                    (not (buffer-name gnus-dribble-buffer))
12808                    (zerop (save-excursion
12809                             (set-buffer gnus-dribble-buffer)
12810                             (buffer-size))))
12811                (gnus-message 4 "(No changes need to be saved)")
12812              (if gnus-save-newsrc-file
12813                  (progn
12814                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12815                    ;; Make backup file of master newsrc.
12816                    (gnus-gnus-to-newsrc-format)
12817                    (gnus-message 5 "Saving %s...done"
12818                                  gnus-current-startup-file)))
12819              ;; Quickly loadable .newsrc.
12820              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12821              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
12822              (gnus-add-current-to-buffer-list)
12823              (buffer-disable-undo (current-buffer))
12824              (erase-buffer)
12825              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12826              (gnus-gnus-to-quick-newsrc-format)
12827              (save-buffer)
12828              (kill-buffer (current-buffer))
12829              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12830              (gnus-dribble-delete-file))))))
12831
12832 (defun gnus-gnus-to-quick-newsrc-format ()
12833   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12834   (insert ";; (ding) Gnus startup file.\n")
12835   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12836   (insert ";; to read .newsrc.\n")
12837   (let ((variables gnus-variable-list)
12838         (inhibit-quit t)
12839         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12840         variable)
12841     ;; insert lisp expressions.
12842     (gnus-compress-newsrc-alist)
12843     (while variables
12844       (setq variable (car variables))
12845       (and (boundp variable)
12846            (symbol-value variable)
12847            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12848            (insert "(setq " (symbol-name variable) " '"
12849                    (prin1-to-string (symbol-value variable))
12850                    ")\n"))
12851       (setq variables (cdr variables)))
12852     (gnus-uncompress-newsrc-alist)))
12853
12854
12855 (defun gnus-gnus-to-newsrc-format ()
12856   ;; Generate and save the .newsrc file.
12857   (let ((newsrc (cdr gnus-newsrc-alist))
12858         info ranges range)
12859     (save-excursion
12860       (set-buffer (create-file-buffer gnus-startup-file))
12861       (set-visited-file-name gnus-startup-file)
12862       (buffer-disable-undo (current-buffer))
12863       (erase-buffer)
12864       ;; Write options.
12865       (if gnus-newsrc-options (insert gnus-newsrc-options))
12866       ;; Write subscribed and unsubscribed.
12867       (while newsrc
12868         (setq info (car newsrc))
12869         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12870             (progn
12871               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12872                                      "!" ":"))
12873               (if (setq ranges (nth 2 info))
12874                   (progn
12875                     (insert " ")
12876                     (if (not (listp (cdr ranges)))
12877                         (if (= (car ranges) (cdr ranges))
12878                             (insert (int-to-string (car ranges)))
12879                           (insert (int-to-string (car ranges)) "-" 
12880                                   (int-to-string (cdr ranges))))
12881                       (while ranges
12882                         (setq range (car ranges)
12883                               ranges (cdr ranges))
12884                         (if (or (atom range) (= (car range) (cdr range)))
12885                             (insert (int-to-string 
12886                                      (or (and (atom range) range) 
12887                                          (car range))))
12888                           (insert (int-to-string (car range)) "-"
12889                                   (int-to-string (cdr range))))
12890                         (if ranges (insert ","))))))
12891               (insert "\n")))
12892         (setq newsrc (cdr newsrc)))
12893       (save-buffer)
12894       (kill-buffer (current-buffer)))))
12895
12896 (defun gnus-read-all-descriptions-files ()
12897   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12898     (while methods
12899       (gnus-read-descriptions-file (car methods))
12900       (setq methods (cdr methods)))
12901     t))
12902
12903 (defun gnus-read-descriptions-file (&optional method)
12904   (let ((method (or method gnus-select-method)))
12905     ;; We create the hashtable whether we manage to read the desc file
12906     ;; to avoid trying to re-read after a failed read.
12907     (or gnus-description-hashtb
12908         (setq gnus-description-hashtb 
12909               (gnus-make-hashtable (length gnus-active-hashtb))))
12910     ;; Mark this method's desc file as read.
12911     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12912                   gnus-description-hashtb)
12913
12914     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12915     (cond 
12916      ((not (or (gnus-server-opened method)
12917                (gnus-open-server method)))
12918       (gnus-message 1 "Couldn't open server")
12919       nil)
12920      ((not (gnus-request-list-newsgroups method))
12921       (gnus-message 1 "Couldn't read newsgroups descriptions")
12922       nil)
12923      (t
12924       (let (group)
12925         (save-excursion
12926           (save-restriction
12927             (set-buffer nntp-server-buffer)
12928             (goto-char (point-min))
12929             (if (or (search-forward "\n.\n" nil t)
12930                     (goto-char (point-max)))
12931                 (progn
12932                   (beginning-of-line)
12933                   (narrow-to-region (point-min) (point))))
12934             (goto-char (point-min))
12935             (while (not (eobp))
12936               ;; If we get an error, we set group to 0, which is not a
12937               ;; symbol... 
12938               (setq group 
12939                     (condition-case ()
12940                         (let ((obarray gnus-description-hashtb))
12941                           ;; Group is set to a symbol interned in this
12942                           ;; hash table.
12943                           (read nntp-server-buffer))
12944                       (error 0)))
12945               (skip-chars-forward " \t")
12946               ;; ... which leads to this line being effectively ignored.
12947               (and (symbolp group)
12948                    (set group (buffer-substring 
12949                                (point) (progn (end-of-line) (point)))))
12950               (forward-line 1))))
12951         (gnus-message 5 "Reading descriptions file...done")
12952         t)))))
12953
12954 (defun gnus-group-get-description (group)
12955   ;; Get the description of a group by sending XGTITLE to the server.
12956   (and (gnus-request-group-description group)
12957        (save-excursion
12958          (set-buffer nntp-server-buffer)
12959          (goto-char (point-min))
12960          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12961               (buffer-substring (match-beginning 1) (match-end 1))))))
12962
12963 ;;;
12964 ;;; Server
12965 ;;;
12966
12967 (defvar gnus-server-mode-hook nil
12968   "Hook run in `gnus-server-mode' buffers.")
12969
12970 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12971   "Format of server lines.
12972 It works along the same lines as a normal formatting string,
12973 with some simple extensions.")
12974
12975 (defvar gnus-server-mode-line-format "(ding) List of servers"
12976   "The format specification for the server mode line.")
12977
12978 (defconst gnus-server-line-format-alist
12979   (list (list ?h 'how ?s)
12980         (list ?n 'name ?s)
12981         (list ?w 'where ?s)
12982         ))
12983
12984 (defconst gnus-server-mode-line-format-alist 
12985   (list (list ?S 'news-server ?s)
12986         (list ?M 'news-method ?s)
12987         (list ?u 'user-defined ?s)))
12988
12989 (defvar gnus-server-line-format-spec nil)
12990 (defvar gnus-server-mode-line-format-spec nil)
12991 (defvar gnus-server-killed-servers nil)
12992
12993 (defvar gnus-server-mode-map nil)
12994 (put 'gnus-server-mode 'mode-class 'special)
12995
12996 (if gnus-server-mode-map
12997     nil
12998   (setq gnus-server-mode-map (make-sparse-keymap))
12999   (suppress-keymap gnus-server-mode-map)
13000   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13001   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13002   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13003   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13004   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13005   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13006   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13007   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13008   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13009   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13010
13011 (defun gnus-server-mode ()
13012   "Major mode for listing and editing servers.
13013
13014 All normal editing commands are switched off.
13015 \\<gnus-server-mode-map>
13016
13017 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13018
13019 The following commands are available:
13020
13021 \\{gnus-server-mode-map}"
13022   (interactive)
13023   (if gnus-visual (gnus-server-make-menu-bar))
13024   (kill-all-local-variables)
13025   (setq mode-line-modified "-- ")
13026   (make-local-variable 'mode-line-format)
13027   (setq mode-line-format (copy-sequence mode-line-format))
13028   (and (equal (nth 3 mode-line-format) "   ")
13029        (setcar (nthcdr 3 mode-line-format) ""))
13030   (setq major-mode 'gnus-server-mode)
13031   (setq mode-name "Server")
13032 ;  (gnus-group-set-mode-line)
13033   (setq mode-line-process nil)
13034   (use-local-map gnus-server-mode-map)
13035   (buffer-disable-undo (current-buffer))
13036   (setq truncate-lines t)
13037   (setq buffer-read-only t)
13038   (run-hooks 'gnus-server-mode-hook))
13039
13040 (defun gnus-server-insert-server-line (sformat name method)
13041   (let* ((sformat (or sformat gnus-server-line-format-spec))
13042          (how (car method))
13043          (where (nth 1 method))
13044          b)
13045     (beginning-of-line)
13046     (setq b (point))
13047     ;; Insert the text.
13048     (insert (eval sformat))
13049     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13050
13051 (defun gnus-server-setup-buffer ()
13052   (if (get-buffer gnus-server-buffer)
13053       ()
13054     (save-excursion
13055       (set-buffer (get-buffer-create gnus-server-buffer))
13056       (gnus-server-mode)
13057       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13058
13059 (defun gnus-server-prepare ()
13060   (setq gnus-server-mode-line-format-spec 
13061         (gnus-parse-format gnus-server-mode-line-format 
13062                            gnus-server-mode-line-format-alist))
13063   (setq gnus-server-line-format-spec 
13064         (gnus-parse-format gnus-server-line-format 
13065                            gnus-server-line-format-alist))
13066   (let ((alist gnus-server-alist)
13067         (buffer-read-only nil))
13068     (erase-buffer)
13069     (while alist
13070       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13071       (setq alist (cdr alist))))
13072   (goto-char (point-min))
13073   (gnus-server-position-cursor))
13074
13075 (defun gnus-server-server-name ()
13076   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13077     (and server (symbol-name server))))
13078
13079 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13080
13081 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13082
13083 (defun gnus-server-update-server (server)
13084   (save-excursion
13085     (set-buffer gnus-server-buffer)
13086     (let ((buffer-read-only nil)
13087           (info (cdr (assoc server gnus-server-alist))))
13088       (gnus-dribble-enter 
13089        (concat "(gnus-server-set-info \"" server "\" '"
13090                (prin1-to-string info) ")"))
13091       ;; Buffer may be narrowed.
13092       (save-restriction
13093         (widen)
13094         (if (gnus-server-goto-server server)
13095             (delete-region (progn (beginning-of-line) (point))
13096                            (progn (forward-line 1) (point))))
13097         (let ((entry (assoc server gnus-server-alist)))
13098           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13099           (gnus-server-position-cursor))))))
13100
13101 (defun gnus-server-set-info (server info)
13102   ;; Enter a select method into the virtual server alist.
13103   (gnus-dribble-enter 
13104    (concat "(gnus-server-set-info \"" server "\" '"
13105            (prin1-to-string info) ")"))
13106   (let* ((server (nth 1 info))
13107          (entry (assoc server gnus-server-alist)))
13108     (if entry (setcdr entry info)
13109       (setq gnus-server-alist
13110             (nconc gnus-server-alist (list (cons server info)))))))
13111
13112 (defun gnus-server-to-method (server)
13113   ;; Map virtual server names to select methods.
13114   (or (and (equal server "native") gnus-select-method)
13115       (cdr (assoc server gnus-server-alist))))
13116
13117 (defun gnus-server-extend-method (group method)
13118   ;; This function "extends" a virtual server.  If the server is
13119   ;; "hello", and the select method is ("hello" (my-var "something")) 
13120   ;; in the group "alt.alt", this will result in a new virtual server
13121   ;; called "helly+alt.alt".
13122   (let ((entry
13123          (gnus-copy-sequence 
13124           (if (equal (car method) "native") gnus-select-method
13125               (cdr (assoc (car method) gnus-server-alist))))))
13126     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13127     (nconc entry (cdr method))))
13128
13129 (defun gnus-server-get-method (group method)
13130   ;; Input either a server name, and extended server name, or a
13131   ;; select method, and return a select method. 
13132   (cond ((stringp method)
13133          (gnus-server-to-method method))
13134         ((stringp (car method))
13135          (gnus-server-extend-method group method))
13136         (t
13137          (gnus-server-add-address method))))
13138
13139 (defun gnus-server-add-address (method)
13140   (let ((method-name (symbol-name (car method))))
13141     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13142              (not (assq (intern (concat method-name "-address")) method)))
13143         (append method (list (list (intern (concat method-name "-address"))
13144                                    (nth 1 method))))
13145       method)))
13146
13147 (defun gnus-server-equal (s1 s2)
13148   (or (equal s1 s2)
13149       (and (= (length s1) (length s2))
13150            (progn
13151              (while (and s1 (member (car s1) s2))
13152                (setq s1 (cdr s1)))
13153              (null s1)))))
13154
13155 ;;; Interactive server functions.
13156
13157 (defun gnus-server-kill-server (server)
13158   "Kill the server on the current line."
13159   (interactive (list (gnus-server-server-name)))
13160   (or (gnus-server-goto-server server)
13161       (if server (error "No such server: %s" server)
13162         (error "No server on the current line")))
13163   (let ((buffer-read-only nil))
13164     (delete-region (progn (beginning-of-line) (point))
13165                    (progn (forward-line 1) (point))))
13166   (setq gnus-server-killed-servers 
13167         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13168   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13169                                 gnus-server-alist))
13170   (gnus-server-position-cursor))
13171
13172 (defun gnus-server-yank-server ()
13173   "Yank the previously killed server."
13174   (interactive)
13175   (or gnus-server-killed-servers
13176       (error "No killed servers to be yanked"))
13177   (let ((alist gnus-server-alist)
13178         (server (gnus-server-server-name))
13179         (killed (car gnus-server-killed-servers)))
13180     (if (not server) 
13181         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13182       (if (string= server (car (car gnus-server-alist)))
13183           (setq gnus-server-alist (cons killed gnus-server-alist))
13184         (while (and (cdr alist)
13185                     (not (string= server (car (car (cdr alist))))))
13186           (setq alist (cdr alist)))
13187         (setcdr alist (cons killed (cdr alist)))))
13188     (gnus-server-update-server (car killed))
13189     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13190     (gnus-server-position-cursor)))
13191
13192 (defun gnus-server-exit ()
13193   "Return to the group buffer."
13194   (interactive)
13195   (kill-buffer (current-buffer))
13196   (switch-to-buffer gnus-group-buffer))
13197
13198 (defun gnus-server-list-servers ()
13199   "List all available servers."
13200   (interactive)
13201   (let ((cur (gnus-server-server-name)))
13202     (gnus-server-prepare)
13203     (if cur (gnus-server-goto-server cur)
13204       (goto-char (point-max))
13205       (forward-line -1))
13206     (gnus-server-position-cursor)))
13207
13208 (defun gnus-server-copy-server (from to)
13209   (interactive
13210    (list
13211     (or (gnus-server-server-name)
13212         (error "No server on the current line"))
13213     (read-string "Copy to: ")))
13214   (or from (error "No server on current line"))
13215   (or (and to (not (string= to ""))) (error "No name to copy to"))
13216   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13217   (or (assoc from gnus-server-alist) 
13218       (error "%s: no such server" from))
13219   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13220     (setcar to-entry to)
13221     (setcar (nthcdr 2 to-entry) to)
13222     (setq gnus-server-killed-servers 
13223           (cons to-entry gnus-server-killed-servers))
13224     (gnus-server-yank-server)))
13225
13226 (defun gnus-server-add-server (how where)
13227   (interactive 
13228    (list (intern (completing-read "Server method: "
13229                                   gnus-valid-select-methods nil t))
13230          (read-string "Server name: ")))
13231   (setq gnus-server-killed-servers 
13232         (cons (list where how where) gnus-server-killed-servers))
13233   (gnus-server-yank-server))
13234
13235 (defun gnus-server-goto-server (server)
13236   "Jump to a server line."
13237   (interactive
13238    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13239   (let ((to (text-property-any (point-min) (point-max) 
13240                                'gnus-server (intern server))))
13241     (and to
13242          (progn
13243            (goto-char to) 
13244            (gnus-server-position-cursor)))))
13245
13246 (defun gnus-server-edit-server (server)
13247   "Edit the server on the current line."
13248   (interactive (list (gnus-server-server-name)))
13249   (or server
13250       (error "No server on current line"))
13251   (let ((winconf (current-window-configuration)))
13252     (get-buffer-create gnus-server-edit-buffer)
13253     (gnus-configure-windows 'edit-server)
13254     (gnus-add-current-to-buffer-list)
13255     (emacs-lisp-mode)
13256     (make-local-variable 'gnus-prev-winconf)
13257     (setq gnus-prev-winconf winconf)
13258     (use-local-map (copy-keymap (current-local-map)))
13259     (let ((done-func '(lambda () 
13260                         "Exit editing mode and update the information."
13261                         (interactive)
13262                         (gnus-server-edit-server-done 'group))))
13263       (setcar (cdr (nth 4 done-func)) server)
13264       (local-set-key "\C-c\C-c" done-func))
13265     (erase-buffer)
13266     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13267     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13268
13269 (defun gnus-server-edit-server-done (server)
13270   (interactive)
13271   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13272   (goto-char (point-min))
13273   (let ((form (read (current-buffer)))
13274         (winconf gnus-prev-winconf))
13275     (gnus-server-set-info server form)
13276     (kill-buffer (current-buffer))
13277     (and winconf (set-window-configuration winconf))
13278     (set-buffer gnus-server-buffer)
13279     (gnus-server-update-server (gnus-server-server-name))
13280     (gnus-server-position-cursor)))
13281
13282 (defun gnus-server-read-server (server)
13283   "Browse a server."
13284   (interactive (list (gnus-server-server-name)))
13285   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13286
13287 (defun gnus-mouse-pick-server (e)
13288   (interactive "e")
13289   (mouse-set-point e)
13290   (gnus-server-read-server (gnus-server-server-name)))
13291
13292 ;;;
13293 ;;; entry points into gnus-score.el
13294 ;;;
13295
13296 ;;; Finding score files. 
13297
13298 (defvar gnus-global-score-files nil
13299   "*List of global score files and directories.
13300 Set this variable if you want to use people's score files.  One entry
13301 for each score file or each score file directory.  Gnus will decide
13302 by itself what score files are applicable to which group.
13303
13304 Say you want to use the single score file
13305 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13306 score files in the \"/ftp.some-where:/pub/score\" directory.
13307
13308  (setq gnus-global-score-files
13309        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13310          \"/ftp.some-where:/pub/score\"))")
13311
13312 (defun gnus-score-score-files (group)
13313   "Return a list of all possible score files."
13314   ;; Search and set any global score files.
13315   (and gnus-global-score-files 
13316        (or gnus-internal-global-score-files
13317            (gnus-score-search-global-directories gnus-global-score-files)))
13318   ;; Fix the kill-file dir variable.
13319   (setq gnus-kill-files-directory 
13320         (file-name-as-directory
13321          (or gnus-kill-files-directory "~/News/")))
13322   ;; If we can't read it, there are no score files.
13323   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13324       (setq gnus-score-file-list nil)
13325     (if (gnus-use-long-file-name 'not-score)
13326         ;; We want long file names.
13327         (if (or (not gnus-score-file-list)
13328                 (not (car gnus-score-file-list))
13329                 (gnus-file-newer-than gnus-kill-files-directory
13330                                       (car gnus-score-file-list)))
13331               (setq gnus-score-file-list 
13332                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13333                           (nreverse 
13334                            (directory-files 
13335                             gnus-kill-files-directory t 
13336                             (gnus-score-file-regexp))))))
13337       ;; We do not use long file names, so we have to do some
13338       ;; directory traversing.  
13339       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13340             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13341             dir files suffix)
13342         (while suffixes
13343           (setq dir (expand-file-name
13344                      (concat gnus-kill-files-directory
13345                              (gnus-replace-chars-in-string group ?. ?/))))
13346           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13347           (setq suffix (car suffixes)
13348                 suffixes (cdr suffixes))
13349           (if (file-exists-p (concat dir "/" suffix))
13350               (setq files (cons (concat dir "/" suffix) files)))
13351           (while (>= (1+ (length dir)) mdir)
13352             (and (file-exists-p (concat dir "/all/" suffix))
13353                  (setq files (cons (concat dir "/all/" suffix) files)))
13354             (string-match "/[^/]*$" dir)
13355             (setq dir (substring dir 0 (match-beginning 0)))))
13356         (setq gnus-score-file-list 
13357               (cons nil (nreverse files)))))
13358     (cdr gnus-score-file-list)))
13359
13360 (defun gnus-score-file-regexp ()
13361   (concat "\\(" gnus-score-file-suffix 
13362           "\\|" gnus-adaptive-file-suffix "\\)$"))
13363         
13364 (defun gnus-score-find-bnews (group)
13365   "Return a list of score files for GROUP.
13366 The score files are those files in the ~/News directory which matches
13367 GROUP using BNews sys file syntax."
13368   (let* ((sfiles (append (gnus-score-score-files group)
13369                          gnus-internal-global-score-files))
13370          (kill-dir (file-name-as-directory 
13371                     (expand-file-name gnus-kill-files-directory)))
13372          (klen (length kill-dir))
13373          ofiles not-match regexp)
13374     (save-excursion
13375       (set-buffer (get-buffer-create "*gnus score files*"))
13376       (buffer-disable-undo (current-buffer))
13377       ;; Go through all score file names and create regexp with them
13378       ;; as the source.  
13379       (while sfiles
13380         (erase-buffer)
13381         (insert (car sfiles))
13382         (goto-char (point-min))
13383         ;; First remove the suffix itself.
13384         (re-search-forward (concat "." (gnus-score-file-regexp)))
13385         (replace-match "" t t) 
13386         (goto-char (point-min))
13387         (if (looking-at (regexp-quote kill-dir))
13388             ;; If the file name was just "SCORE", `klen' is one character
13389             ;; too much.
13390             (delete-char (min (1- (point-max)) klen))
13391           (goto-char (point-max))
13392           (search-backward "/")
13393           (delete-region (1+ (point)) (point-min)))
13394         ;; If short file names were used, we have to translate slashes.
13395         (goto-char (point-min))
13396         (while (re-search-forward "[/:]" nil t)
13397           (replace-match "." t t))
13398         ;; Translate "all" to ".*".
13399         (while (search-forward "all" nil t)
13400           (replace-match ".*" t t))
13401         (goto-char (point-min))
13402         ;; Deal with "not."s.
13403         (if (looking-at "not.")
13404             (progn
13405               (setq not-match t)
13406               (setq regexp (buffer-substring 5 (point-max))))
13407           (setq regexp (buffer-substring 1 (point-max)))
13408           (setq not-match nil))
13409         ;; Finally - if this resulting regexp matches the group name,
13410         ;; we add this score file to the list of score files
13411         ;; applicable to this group.
13412         (if (or (and not-match
13413                      (not (string-match regexp group)))
13414                 (and (not not-match)
13415                      (string-match regexp group)))
13416             (setq ofiles (cons (car sfiles) ofiles)))
13417         (setq sfiles (cdr sfiles)))
13418       (kill-buffer (current-buffer))
13419       ;; Slight kludge here - the last score file returned should be
13420       ;; the local score file, whether it exists or not. This is so
13421       ;; that any score commands the user enters will go to the right
13422       ;; file, and not end up in some global score file.
13423       (let ((localscore
13424              (expand-file-name
13425               (if (gnus-use-long-file-name 'not-score)
13426                   (concat gnus-kill-files-directory group "." 
13427                           gnus-score-file-suffix)
13428                 (concat gnus-kill-files-directory
13429                         (gnus-replace-chars-in-string group ?. ?/)
13430                         "/" gnus-score-file-suffix)))))
13431         (and (member localscore ofiles)
13432              (delete localscore ofiles))
13433         (setq ofiles (cons localscore ofiles)))
13434       (nreverse ofiles))))
13435
13436 (defun gnus-score-find-single (group)
13437   "Return list containing the score file for GROUP."
13438   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13439         (gnus-score-file-name group)))
13440
13441 (defun gnus-score-find-hierarchical (group)
13442   "Return list of score files for GROUP.
13443 This includes the score file for the group and all its parents."
13444   (let ((all (copy-sequence '(nil)))
13445         (start 0))
13446     (while (string-match "\\." group (1+ start))
13447       (setq start (match-beginning 0))
13448       (setq all (cons (substring group 0 start) all)))
13449     (setq all (cons group all))
13450     (nconc
13451      (mapcar (lambda (newsgroup)
13452                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13453              (setq all (nreverse all)))
13454      (mapcar 'gnus-score-file-name all))))
13455
13456 (defvar gnus-score-file-alist-cache nil)
13457
13458 (defun gnus-score-find-alist (group)
13459   "Return list of score files for GROUP.
13460 The list is determined from the variable gnus-score-file-alist."
13461   (let ((alist gnus-score-file-multiple-match-alist)
13462         score-files)
13463     ;; if this group has been seen before, return the cached entry
13464     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13465         (cdr score-files)       ; ensures caching of groups with no matches
13466       ;; handle the multiple match alist
13467       (while alist
13468         (and (string-match (car (car alist)) group)
13469              (setq score-files
13470                    (nconc score-files (cdr (car alist)))))
13471         (setq alist (cdr alist)))
13472       (setq alist gnus-score-file-single-match-alist)
13473       ;; handle the single match alist
13474       (catch 'done
13475         (while alist
13476           (and (string-match (car (car alist)) group)
13477                ;; progn used just in case ("regexp") has no files
13478                ;; and score-files is still nil. -sj
13479                ;; this can be construed as a "stop searching here" feature :>
13480                ;; and used to simplify regexps in the single-alist 
13481                (progn
13482                  (setq score-files
13483                        (append score-files (cdr (car alist))))
13484                  (throw 'done nil)))
13485           (setq alist (cdr alist))))
13486       ;; cache the score files
13487       (setq gnus-score-file-alist-cache
13488             (cons (cons group score-files) gnus-score-file-alist-cache))
13489       score-files)))
13490
13491
13492 (defun gnus-possibly-score-headers (&optional trace)
13493   (let ((func gnus-score-find-score-files-function)
13494         score-files)
13495     (and func (not (listp func))
13496          (setq func (list func)))
13497     ;; Go through all the functions for finding score files (or actual
13498     ;; scores) and add them to a list.
13499     (setq score-files (copy-sequence
13500                        (gnus-score-find-alist gnus-newsgroup-name)))
13501     (while func
13502       (and (symbolp (car func))
13503            (fboundp (car func))
13504            (setq score-files 
13505                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13506       (setq func (cdr func)))
13507     (if score-files (gnus-score-headers score-files trace))))
13508
13509 (defun gnus-score-file-name (newsgroup &optional suffix)
13510   "Return the name of a score file for NEWSGROUP."
13511   (let ((suffix (or suffix gnus-score-file-suffix)))
13512     (cond  ((or (null newsgroup)
13513                 (string-equal newsgroup ""))
13514             ;; The global score file is placed at top of the directory.
13515             (expand-file-name 
13516              suffix (or gnus-kill-files-directory "~/News")))
13517            ((gnus-use-long-file-name 'not-score)
13518             ;; Append ".SCORE" to newsgroup name.
13519             (expand-file-name (concat newsgroup "." suffix)
13520                               (or gnus-kill-files-directory "~/News")))
13521            (t
13522             ;; Place "SCORE" under the hierarchical directory.
13523             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13524                                       "/" suffix)
13525                               (or gnus-kill-files-directory "~/News"))))))
13526
13527 (defun gnus-score-search-global-directories (files)
13528   "Scan all global score directories for score files."
13529   ;; Set the variable `gnus-internal-global-score-files' to all
13530   ;; available global score files.
13531   (interactive (list gnus-global-score-files))
13532   (let (out)
13533     (while files
13534       (if (string-match "/$" (car files))
13535           (setq out (nconc (directory-files 
13536                             (car files) t
13537                             (concat (gnus-score-file-regexp) "$"))))
13538         (setq out (cons (car files) out)))
13539       (setq files (cdr files)))
13540     (setq gnus-internal-global-score-files out)))
13541
13542 ;; Allow redefinition of Gnus functions.
13543
13544 (gnus-ems-redefine)
13545
13546 (provide 'gnus)
13547
13548 ;;; gnus.el ends here