*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is 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               ((< (apply '+ (x-color-values
1006                              (cdr (assq 'background-color params))))
1007                   (/ (apply '+ (x-color-values "white")) 3))
1008                'dark)
1009               (t 'light)))
1010     (error 'light))
1011   "A symbol indicating the Emacs background brightness.
1012 The symbol should be one of `light' or `dark'.
1013 If Emacs guesses this frame attribute wrongly, either set this variable in
1014 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1015 `~/.Xdefaults'.
1016 See also `gnus-display-type'.")
1017
1018 (defvar gnus-mouse-face 'highlight
1019   "*Face used for mouse highlighting in Gnus.
1020 No mouse highlights will be done if `gnus-visual' is nil.")
1021
1022 (defvar gnus-summary-mark-below nil
1023   "*Mark all articles with a score below this variable as read.
1024 This variable is local to each summary buffer and usually set by the
1025 score file.")  
1026
1027 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1028   "*List of functions used for sorting threads in the summary buffer.
1029 By default, threads are sorted by article number.
1030
1031 Each function takes two threads and return non-nil if the first thread
1032 should be sorted before the other.  If you use more than one function,
1033 the primary sort function should be the last.
1034
1035 Ready-mady functions include `gnus-thread-sort-by-number',
1036 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1037 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1038 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1039
1040 (defvar gnus-thread-score-function '+
1041   "*Function used for calculating the total score of a thread.
1042
1043 The function is called with the scores of the article and each
1044 subthread and should then return the score of the thread.
1045
1046 Some functions you can use are `+', `max', or `min'.")
1047
1048 (defvar gnus-options-subscribe nil
1049   "*All new groups matching this regexp will be subscribed unconditionally.
1050 Note that this variable deals only with new newsgroups.  This variable
1051 does not affect old newsgroups.")
1052
1053 (defvar gnus-options-not-subscribe nil
1054   "*All new groups matching this regexp will be ignored.
1055 Note that this variable deals only with new newsgroups.  This variable
1056 does not affect old (already subscribed) newsgroups.")
1057
1058 (defvar gnus-auto-expirable-newsgroups nil
1059   "*Groups in which to automatically mark read articles as expirable.
1060 If non-nil, this should be a regexp that should match all groups in
1061 which to perform auto-expiry.  This only makes sense for mail groups.")
1062
1063 (defvar gnus-hidden-properties '(invisible t intangible t)
1064   "Property list to use for hiding text.")
1065
1066 ;; Hooks.
1067
1068 (defvar gnus-group-mode-hook nil
1069   "*A hook for Gnus group mode.")
1070
1071 (defvar gnus-summary-mode-hook nil
1072   "*A hook for Gnus summary mode.
1073 This hook is run before any variables are set in the summary buffer.")
1074
1075 (defvar gnus-article-mode-hook nil
1076   "*A hook for Gnus article mode.")
1077
1078 (defun gnus-summary-exit-hook nil
1079   "*A hook called on exit from the summary buffer.
1080 It calls `gnus-summary-expire-articles' by default.")
1081 (add-hook 'gnus-summary-exit-hook 'gnus-summary-expire-articles)
1082
1083 (defvar gnus-open-server-hook nil
1084   "*A hook called just before opening connection to the news server.")
1085
1086 (defvar gnus-startup-hook nil
1087   "*A hook called at startup.
1088 This hook is called after Gnus is connected to the NNTP server.")
1089
1090 (defvar gnus-get-new-news-hook nil
1091   "*A hook run just before Gnus checks for new news.")
1092
1093 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1094   "*A function that is called to generate the group buffer.
1095 The function is called with three arguments: The first is a number;
1096 all group with a level less or equal to that number should be listed,
1097 if the second is non-nil, empty groups should also be displayed. If
1098 the third is non-nil, it is a number. No groups with a level lower
1099 than this number should be displayed.
1100
1101 The only current function implemented is `gnus-group-prepare-flat'.")
1102
1103 (defvar gnus-group-prepare-hook nil
1104   "*A hook called after the group buffer has been generated.
1105 If you want to modify the group buffer, you can use this hook.")
1106
1107 (defvar gnus-summary-prepare-hook nil
1108   "*A hook called after the summary buffer has been generated.
1109 If you want to modify the summary buffer, you can use this hook.")
1110
1111 (defvar gnus-article-prepare-hook nil
1112   "*A hook called after an article has been prepared in the article buffer.
1113 If you want to run a special decoding program like nkf, use this hook.")
1114
1115 (defvar gnus-article-display-hook nil
1116   "*A hook called after the article is displayed in the article buffer.
1117 The hook is designed to change the contents of the article
1118 buffer. Typical functions that this hook may contain are
1119 `gnus-article-hide-headers' (hide selected headers),
1120 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1121 `gnus-article-hide-signature' (hide signature) and
1122 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1123 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1124 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1125
1126 (defvar gnus-article-x-face-command
1127   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1128   "String or function to be executed to display an X-Face header.
1129 If it is a string, the command will be executed in a sub-shell
1130 asynchronously. The compressed face will be piped to this command.") 
1131
1132 (defvar gnus-article-x-face-too-ugly nil
1133   "Regexp matching posters whose face shouldn't be shown automatically.")
1134
1135 (defvar gnus-select-group-hook nil
1136   "*A hook called when a newsgroup is selected.
1137
1138 If you'd like to simplify subjects like the
1139 `gnus-summary-next-same-subject' command does, you can use the
1140 following hook:
1141
1142  (setq gnus-select-group-hook
1143       (list
1144         (lambda ()
1145           (mapcar (lambda (header)
1146                      (header-set-subject
1147                       header
1148                       (gnus-simplify-subject
1149                        (header-subject header) 're-only)))
1150                   gnus-newsgroup-headers))))")
1151
1152 (defvar gnus-select-article-hook
1153   '(gnus-summary-show-thread)
1154   "*A hook called when an article is selected.
1155 The default hook shows conversation thread subtrees of the selected
1156 article automatically using `gnus-summary-show-thread'.")
1157
1158 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1159   "*A hook called to apply kill files to a group.
1160 This hook is intended to apply a kill file to the selected newsgroup.
1161 The function `gnus-apply-kill-file' is called by default.
1162
1163 Since a general kill file is too heavy to use only for a few
1164 newsgroups, I recommend you to use a lighter hook function. For
1165 example, if you'd like to apply a kill file to articles which contains
1166 a string `rmgroup' in subject in newsgroup `control', you can use the
1167 following hook:
1168
1169 \(setq gnus-apply-kill-hook
1170       (list
1171         (lambda ()
1172           (cond ((string-match \"control\" gnus-newsgroup-name)
1173                  (gnus-kill \"Subject\" \"rmgroup\")
1174                  (gnus-expunge \"X\"))))))")
1175
1176 (defvar gnus-visual-mark-article-hook 
1177   (list 'gnus-highlight-selected-summary)
1178   "*Hook run after selecting an article in the summary buffer.
1179 It is meant to be used for highlighting the article in some way.  It
1180 is not run if `gnus-visual' is nil.")
1181
1182 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1183   "*A hook called after preparing body, but before preparing header headers.
1184 The default hook (`gnus-inews-insert-signature') inserts a signature
1185 file specified by the variable `gnus-signature-file'.")
1186
1187 (defvar gnus-exit-group-hook nil
1188   "*A hook called when exiting (not quitting) summary mode.")
1189
1190 (defvar gnus-suspend-gnus-hook nil
1191   "*A hook called when suspending (not exiting) Gnus.")
1192
1193 (defvar gnus-exit-gnus-hook nil
1194   "*A hook called when exiting Gnus.")
1195
1196 (defvar gnus-save-newsrc-hook nil
1197   "*A hook called when saving the newsrc file.")
1198
1199 (defvar gnus-summary-update-hook 
1200   (list 'gnus-summary-highlight-line)
1201   "*A hook called when a summary line is changed.
1202 The hook will not be called if `gnus-visual' is nil.
1203
1204 The default function `gnus-summary-highlight-line' will
1205 highlight the line according to the `gnus-summary-highlight'
1206 variable.")
1207
1208 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1209   "*A hook called when an article is selected for the first time.
1210 The hook is intended to mark an article as read (or unread)
1211 automatically when it is selected.")
1212
1213 ;; Remove any hilit infestation.
1214 (add-hook 'gnus-startup-hook
1215           (lambda ()
1216             (remove-hook 'gnus-summary-prepare-hook
1217                          'hilit-rehighlight-buffer-quietly)
1218             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1219             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1220             (remove-hook 'gnus-article-prepare-hook
1221                          'hilit-rehighlight-buffer-quietly)))
1222
1223
1224 \f
1225 ;; Internal variables
1226
1227 ;; Avoid highlighting in kill files.
1228 (defvar gnus-summary-inhibit-highlight nil)
1229 (defvar gnus-newsgroup-selected-overlay nil)
1230
1231 (defvar gnus-article-mode-map nil)
1232 (defvar gnus-dribble-buffer nil)
1233 (defvar gnus-headers-retrieved-by nil)
1234 (defvar gnus-article-reply nil)
1235 (defvar gnus-override-method nil)
1236 (defvar gnus-article-check-size nil)
1237
1238 (defvar gnus-current-score-file nil)
1239 (defvar gnus-internal-global-score-files nil)
1240 (defvar gnus-score-file-list nil)
1241
1242
1243 (defvar gnus-current-move-group nil)
1244
1245 (defvar gnus-newsgroup-dependencies nil)
1246 (defvar gnus-newsgroup-threads nil)
1247 (defvar gnus-newsgroup-async nil)
1248 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1249
1250 (defvar gnus-newsgroup-adaptive nil)
1251
1252 (defvar gnus-summary-display-table nil)
1253
1254 (defconst gnus-group-line-format-alist
1255   (list (list ?M 'marked ?c)
1256         (list ?S 'subscribed ?c)
1257         (list ?L 'level ?d)
1258         (list ?N 'number ?s)
1259         (list ?I 'number-of-dormant ?d)
1260         (list ?T 'number-of-ticked ?d)
1261         (list ?R 'number-of-read ?s)
1262         (list ?t 'number-total ?d)
1263         (list ?y 'number-of-unread-unticked ?s)
1264         (list ?i 'number-of-ticked-and-dormant ?d)
1265         (list ?g 'group ?s)
1266         (list ?G 'qualified-group ?s)
1267         (list ?D 'newsgroup-description ?s)
1268         (list ?o 'moderated ?c)
1269         (list ?O 'moderated-string ?s)
1270         (list ?p 'process-marked ?c)
1271         (list ?s 'news-server ?s)
1272         (list ?n 'news-method ?s)
1273         (list ?z 'news-method-string ?s)
1274         (list ?u 'user-defined ?s)))
1275
1276 (defconst gnus-summary-line-format-alist 
1277   (list (list ?N 'number ?d)
1278         (list ?S 'subject ?s)
1279         (list ?s 'subject-or-nil ?s)
1280         (list ?n 'name ?s)
1281         (list ?A 'address ?s)
1282         (list ?F 'from ?s)
1283         (list ?x (macroexpand '(header-xref header)) ?s)
1284         (list ?D (macroexpand '(header-date header)) ?s)
1285         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1286         (list ?M (macroexpand '(header-id header)) ?s)
1287         (list ?r (macroexpand '(header-references header)) ?s)
1288         (list ?c '(or (header-chars header) 0) ?d)
1289         (list ?L 'lines ?d)
1290         (list ?I 'indentation ?s)
1291         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1292         (list ?R 'replied ?c)
1293         (list ?\[ 'opening-bracket ?c)
1294         (list ?\] 'closing-bracket ?c)
1295         (list ?\> '(make-string level ? ) ?s)
1296         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1297         (list ?i 'score ?d)
1298         (list ?z 'score-char ?c)
1299         (list ?U 'unread ?c)
1300         (list ?t '(gnus-summary-number-of-articles-in-thread 
1301                    (or (prog1 gnus-tmp-adopt-thread 
1302                          (setq gnus-tmp-adopt-thread nil))
1303                        (if (boundp 'thread) (symbol-value 'thread)
1304                          thread nil)))
1305                    ?d)
1306         (list ?e '(gnus-summary-number-of-articles-in-thread 
1307                    (or gnus-tmp-adopt-thread 
1308                        (if (boundp 'thread) (symbol-value 'thread)
1309                          thread nil)) t)
1310                    ?c)
1311         (list ?u 'user-defined ?s))
1312   "An alist of format specifications that can appear in summary lines,
1313 and what variables they correspond with, along with the type of the
1314 variable (string, integer, character, etc).")
1315
1316 (defconst gnus-summary-dummy-line-format-alist
1317   (list (list ?S 'subject ?s)
1318         (list ?N 'number ?d)
1319         (list ?u 'user-defined ?s)))
1320
1321 (defconst gnus-summary-mode-line-format-alist 
1322   (list (list ?G 'group-name ?s)
1323         (list ?g '(gnus-short-group-name group-name) ?s)
1324         (list ?A 'article-number ?d)
1325         (list ?Z 'unread-and-unselected ?s)
1326         (list ?V 'gnus-version ?s)
1327         (list ?U 'unread ?d)
1328         (list ?S 'subject ?s)
1329         (list ?e 'unselected ?d)
1330         (list ?u 'user-defined ?s)
1331         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1332
1333 (defconst gnus-group-mode-line-format-alist 
1334   (list (list ?S 'news-server ?s)
1335         (list ?M 'news-method ?s)
1336         (list ?u 'user-defined ?s)))
1337
1338 (defvar gnus-have-read-active-file nil)
1339
1340 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1341   "The mail address of the Gnus maintainers.")
1342
1343 (defconst gnus-version "(ding) Gnus v0.96"
1344   "Version number for this version of Gnus.")
1345
1346 (defvar gnus-info-nodes
1347   '((gnus-group-mode            "(gnus)The Group Buffer")
1348     (gnus-summary-mode          "(gnus)The Summary Buffer")
1349     (gnus-article-mode          "(gnus)The Article Buffer"))
1350   "Assoc list of major modes and related Info nodes.")
1351
1352 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1353   "The location of the (ding) Gnus documentation group.")
1354
1355 (defvar gnus-group-buffer "*Group*")
1356 (defvar gnus-summary-buffer "*Summary*")
1357 (defvar gnus-article-buffer "*Article*")
1358 (defvar gnus-server-buffer "*Server*")
1359
1360 (defvar gnus-work-buffer " *gnus work*")
1361
1362 (defvar gnus-buffer-list nil
1363   "Gnus buffers that should be killed on exit.")
1364
1365 (defvar gnus-server-alist nil
1366   "List of available servers.")
1367
1368 (defvar gnus-variable-list
1369   '(gnus-newsrc-options gnus-newsrc-options-n
1370     gnus-newsrc-last-checked-date 
1371     gnus-newsrc-alist gnus-server-alist
1372     gnus-killed-list gnus-zombie-list)
1373   "Gnus variables saved in the quick startup file.")
1374
1375 (defvar gnus-overload-functions
1376   '((news-inews gnus-inews-news "rnewspost"))
1377   "Functions overloaded by gnus.
1378 It is a list of `(original overload &optional file)'.")
1379
1380 (defvar gnus-newsrc-options nil
1381   "Options line in the .newsrc file.")
1382
1383 (defvar gnus-newsrc-options-n nil
1384   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1385
1386 (defvar gnus-newsrc-last-checked-date nil
1387   "Date Gnus last asked server for new newsgroups.")
1388
1389 (defvar gnus-newsrc-alist nil
1390   "Assoc list of read articles.
1391 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1392
1393 (defvar gnus-newsrc-hashtb nil
1394   "Hashtable of gnus-newsrc-alist.")
1395
1396 (defvar gnus-killed-list nil
1397   "List of killed newsgroups.")
1398
1399 (defvar gnus-killed-hashtb nil
1400   "Hash table equivalent of gnus-killed-list.")
1401
1402 (defvar gnus-zombie-list nil
1403   "List of almost dead newsgroups.")
1404
1405 (defvar gnus-description-hashtb nil
1406   "Descriptions of newsgroups.")
1407
1408 (defvar gnus-list-of-killed-groups nil
1409   "List of newsgroups that have recently been killed by the user.")
1410
1411 (defvar gnus-active-hashtb nil
1412   "Hashtable of active articles.")
1413
1414 (defvar gnus-moderated-list nil
1415   "List of moderated newsgroups.")
1416
1417 (defvar gnus-group-marked nil)
1418
1419 (defvar gnus-current-startup-file nil
1420   "Startup file for the current host.")
1421
1422 (defvar gnus-last-search-regexp nil
1423   "Default regexp for article search command.")
1424
1425 (defvar gnus-last-shell-command nil
1426   "Default shell command on article.")
1427
1428 (defvar gnus-current-select-method nil
1429   "The current method for selecting a newsgroup.")
1430
1431 (defvar gnus-have-all-newsgroups nil)
1432
1433 (defvar gnus-article-internal-prepare-hook nil)
1434
1435 (defvar gnus-newsgroup-name nil)
1436 (defvar gnus-newsgroup-begin nil)
1437 (defvar gnus-newsgroup-end nil)
1438 (defvar gnus-newsgroup-last-rmail nil)
1439 (defvar gnus-newsgroup-last-mail nil)
1440 (defvar gnus-newsgroup-last-folder nil)
1441 (defvar gnus-newsgroup-last-file nil)
1442 (defvar gnus-newsgroup-auto-expire nil)
1443 (defvar gnus-newsgroup-active nil)
1444
1445 (defvar gnus-newsgroup-unreads nil
1446   "List of unread articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-unselected nil
1449   "List of unselected unread articles in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-marked nil
1452   "List of ticked articles in the current newsgroup (a subset of unread art).")
1453
1454 (defvar gnus-newsgroup-killed nil
1455   "List of ranges of articles that have been through the scoring process.")
1456
1457 (defvar gnus-newsgroup-kill-headers nil)
1458
1459 (defvar gnus-newsgroup-replied nil
1460   "List of articles that have been replied to in the current newsgroup.")
1461
1462 (defvar gnus-newsgroup-expirable nil
1463   "List of articles in the current newsgroup that can be expired.")
1464
1465 (defvar gnus-newsgroup-processable nil
1466   "List of articles in the current newsgroup that can be processed.")
1467
1468 (defvar gnus-newsgroup-bookmarks nil
1469   "List of articles in the current newsgroup that have bookmarks.")
1470
1471 (defvar gnus-newsgroup-dormant nil
1472   "List of dormant articles in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-scored nil
1475   "List of scored articles in the current newsgroup.")
1476
1477 (defvar gnus-newsgroup-headers nil
1478   "List of article headers in the current newsgroup.")
1479 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1480
1481 (defvar gnus-newsgroup-ancient nil
1482   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1483
1484 (defvar gnus-current-article nil)
1485 (defvar gnus-article-current nil)
1486 (defvar gnus-current-headers nil)
1487 (defvar gnus-have-all-headers nil)
1488 (defvar gnus-last-article nil)
1489 (defvar gnus-newsgroup-history nil)
1490 (defvar gnus-current-kill-article nil)
1491
1492 ;; Save window configuration.
1493 (defvar gnus-prev-winconf nil)
1494
1495 ;; Format specs
1496 (defvar gnus-summary-line-format-spec nil)
1497 (defvar gnus-summary-dummy-line-format-spec nil)
1498 (defvar gnus-group-line-format-spec nil)
1499 (defvar gnus-summary-mode-line-format-spec nil)
1500 (defvar gnus-article-mode-line-format-spec nil)
1501 (defvar gnus-group-mode-line-format-spec nil)
1502 (defvar gnus-summary-mark-positions nil)
1503
1504 (defvar gnus-summary-expunge-below nil)
1505 (defvar gnus-reffed-article-number nil)
1506
1507 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1508 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1509
1510 (defvar gnus-cache-removeable-articles nil)
1511
1512 (defconst gnus-summary-local-variables 
1513   '(gnus-newsgroup-name 
1514     gnus-newsgroup-begin gnus-newsgroup-end 
1515     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1516     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1517     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1518     gnus-newsgroup-unselected gnus-newsgroup-marked
1519     gnus-newsgroup-replied gnus-newsgroup-expirable
1520     gnus-newsgroup-processable gnus-newsgroup-killed
1521     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1522     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1523     gnus-current-article gnus-current-headers gnus-have-all-headers
1524     gnus-last-article gnus-article-internal-prepare-hook
1525     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1526     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1527     gnus-newsgroup-threads gnus-newsgroup-async
1528     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1529     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1530     gnus-newsgroup-history gnus-newsgroup-ancient
1531     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1532     gnus-cache-removeable-articles)
1533   "Variables that are buffer-local to the summary buffers.")
1534
1535 (defconst gnus-bug-message
1536   "Sending a bug report to the Gnus Towers.
1537 ========================================
1538
1539 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1540 be sent to the Gnus Bug Exterminators. 
1541
1542 At the bottom of the buffer you'll see lots of variable settings.
1543 Please do not delete those.  They will tell the Bug People what your
1544 environment is, so that it will be easier to locate the bugs.
1545
1546 If you have found a bug that makes Emacs go \"beep\", set
1547 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1548 and include the backtrace in your bug report.
1549
1550 Please describe the bug in annoying, painstaking detail.
1551
1552 Thank you for your help in stamping out bugs.
1553 ")
1554
1555 ;;; End of variables.
1556
1557 ;; Define some autoload functions Gnus might use.
1558 (eval-and-compile
1559
1560   ;; Various 
1561   (autoload 'metamail-buffer "metamail")
1562   (autoload 'Info-goto-node "info")
1563   (autoload 'hexl-hex-string-to-integer "hexl")
1564   (autoload 'pp "pp")
1565   (autoload 'pp-to-string "pp")
1566   (autoload 'pp-eval-expression "pp")
1567   (autoload 'mail-extract-address-components "mail-extr")
1568
1569   (autoload 'nnmail-split-fancy "nnmail")
1570   (autoload 'nnvirtual-catchup-group "nnvirtual")
1571
1572   ;; timezone
1573   (autoload 'timezone-make-date-arpa-standard "timezone")
1574   (autoload 'timezone-fix-time "timezone")
1575   (autoload 'timezone-make-sortable-date "timezone")
1576   (autoload 'timezone-make-time-string "timezone")
1577
1578   ;; rmail & friends
1579   (autoload 'mail-position-on-field "sendmail")
1580   (autoload 'mail-setup "sendmail")
1581   (autoload 'rmail-output "rmailout")
1582   (autoload 'news-mail-other-window "rnewspost")
1583   (autoload 'news-reply-yank-original "rnewspost")
1584   (autoload 'news-caesar-buffer-body "rnewspost")
1585   (autoload 'rmail-insert-rmail-file-header "rmail")
1586   (autoload 'rmail-count-new-messages "rmail")
1587   (autoload 'rmail-show-message "rmail")
1588
1589   ;; gnus-soup
1590   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1591   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1592   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1593   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1594   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1595   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1596   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1597
1598   ;; gnus-mh
1599   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1600   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1601   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1602   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1603   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1604   (autoload 'gnus-Folder-save-name "gnus-mh")
1605   (autoload 'gnus-folder-save-name "gnus-mh")
1606
1607   ;; gnus-vis misc
1608   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1609   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1610   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1611   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1612   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1613   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1614   (autoload 'gnus-summary-highlight-line "gnus-vis")
1615   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1616
1617   ;; gnus-vis article
1618   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1619   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1620   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1621   (autoload 'gnus-article-hide "gnus-vis" nil t)
1622   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1623   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1624   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1625   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1626   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1627   (autoload 'gnus-article-add-button "gnus-vis")
1628
1629   ;; gnus-cite
1630   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1631   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1632   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1633
1634   ;; gnus-kill
1635   (autoload 'gnus-kill "gnus-kill")
1636   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1637   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1638   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1639   (autoload 'gnus-execute "gnus-kill")
1640   (autoload 'gnus-expunge "gnus-kill")
1641
1642   ;; gnus-cache
1643   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1644   (autoload 'gnus-cache-save-buffers "gnus-cache")
1645   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1646   (autoload 'gnus-cache-request-article "gnus-cache")
1647   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1648   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1649   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1650   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1651
1652   ;; gnus-score
1653   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1654   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1655   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1656   (autoload 'gnus-score-save "gnus-score")
1657   (autoload 'gnus-score-headers "gnus-score")
1658   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1659   (autoload 'gnus-score-adaptive "gnus-score")
1660   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1661   (autoload 'gnus-score-find-trace "gnus-score")
1662
1663   ;; gnus-edit
1664   (autoload 'gnus-score-customize "gnus-edit" nil t)
1665
1666   ;; gnus-uu
1667   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1668   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1669   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1670   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1671   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1672   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1673   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1674   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1675   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1676   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1677   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1678   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1679   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1680   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1681   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1682   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1683   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1684   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1685   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1686   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1687   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1688   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1689
1690   ;; gnus-msg
1691   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1692   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1693   (autoload 'gnus-group-mail "gnus-msg" nil t)
1694   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1695   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1696   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1697   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1698   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1699   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1700   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1701   (autoload 'gnus-post-news "gnus-msg" nil t)
1702   (autoload 'gnus-inews-news "gnus-msg" nil t)
1703   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1704   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1705   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1706   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1707   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1708   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1709   (autoload 'gnus-mail-yank-original "gnus-msg")
1710   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1711   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1712   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1713   (autoload 'gnus-article-mail-with-original "gnus-msg")
1714   (autoload 'gnus-article-mail "gnus-msg")
1715   (autoload 'gnus-bug "gnus-msg" nil t)
1716
1717   ;; gnus-vm
1718   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1719   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1720   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1721   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1722   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1723   (autoload 'gnus-yank-article "gnus-vm" nil t)
1724
1725   )
1726
1727 \f
1728
1729 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1730 ;; If you want the cursor to go somewhere else, set these two
1731 ;; functions in some startup hook to whatever you want.
1732 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1733 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1734
1735 ;;; Various macros and substs.
1736
1737 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1738   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1739   (` (let ((GnusStartBufferWindow (selected-window)))
1740        (unwind-protect
1741            (progn
1742              (pop-to-buffer (, buffer))
1743              (,@ forms))
1744          (select-window GnusStartBufferWindow)))))
1745
1746 (defmacro gnus-gethash (string hashtable)
1747   "Get hash value of STRING in HASHTABLE."
1748   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1749   ;;(` (abbrev-expansion (, string) (, hashtable)))
1750   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1751
1752 (defmacro gnus-sethash (string value hashtable)
1753   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1754   ;; We cannot use define-abbrev since it only accepts string as value.
1755   ;; (set (intern string hashtable) value))
1756   (` (set (intern (, string) (, hashtable)) (, value))))
1757
1758 (defsubst gnus-buffer-substring (beg end)
1759   (buffer-substring (match-beginning beg) (match-end end)))
1760
1761 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1762 ;;   function `substring' might cut on a middle of multi-octet
1763 ;;   character.
1764
1765 (defun gnus-truncate-string (str width)
1766   (substring str 0 width))
1767
1768 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1769 ;; to limit the length of a string. This function is necessary since
1770 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1771 (defsubst gnus-limit-string (str width)
1772   (if (> (length str) width)
1773       (substring str 0 width)
1774     str))
1775
1776 (defsubst gnus-simplify-subject-re (subject)
1777   "Remove \"Re:\" from subject lines."
1778   (let ((case-fold-search t))
1779     (if (string-match "^re: *" subject)
1780         (substring subject (match-end 0))
1781       subject)))
1782
1783 (defsubst gnus-goto-char (point)
1784   (and point (goto-char point)))
1785
1786 (defmacro gnus-buffer-exists-p (buffer)
1787   (` (and (, buffer)
1788           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1789                    (, buffer)))))
1790
1791 (defmacro gnus-kill-buffer (buffer)
1792   (` (if (gnus-buffer-exists-p (, buffer))
1793          (kill-buffer (, buffer)))))
1794
1795 (defsubst gnus-point-at-bol ()
1796   "Return point at the beginning of line."
1797   (let ((p (point)))
1798     (beginning-of-line)
1799     (prog1
1800         (point)
1801       (goto-char p))))
1802
1803 (defsubst gnus-point-at-eol ()
1804   "Return point at the beginning of line."
1805   (let ((p (point)))
1806     (end-of-line)
1807     (prog1
1808         (point)
1809       (goto-char p))))
1810
1811 ;; Delete the current line (and the next N lines.);
1812 (defmacro gnus-delete-line (&optional n)
1813   (` (delete-region (progn (beginning-of-line) (point))
1814                     (progn (forward-line (, (or n 1))) (point)))))
1815
1816 ;;; Load the compatability functions. 
1817
1818 (require 'gnus-ems)
1819
1820 \f
1821 ;;;
1822 ;;; Gnus Utility Functions
1823 ;;;
1824
1825 (defun gnus-extract-address-components (from)
1826   (let (name address)
1827     ;; First find the address - the thing with the @ in it.  This may
1828     ;; not be accurate in mail addresses, but does the trick most of
1829     ;; the time in news messages.
1830     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1831         (setq address (substring from (match-beginning 0) (match-end 0))))
1832     ;; Then we check whether the "name <address>" format is used.
1833     (and address
1834          (string-match (concat "<" (regexp-quote address) ">") from)
1835          (and (setq name (substring from 0 (1- (match-beginning 0))))
1836               ;; Strip any quotes from the name.
1837               (string-match "\".*\"" name)
1838               (setq name (substring name 1 (1- (match-end 0))))))
1839     ;; If not, then "address (name)" is used.
1840     (or name
1841         (and (string-match "(.+)" from)
1842              (setq name (substring from (1+ (match-beginning 0)) 
1843                                    (1- (match-end 0)))))
1844         (and (string-match "()" from)
1845              (setq name address))
1846         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1847         ;; XOVER might not support folded From headers.
1848         (and (string-match "(.*" from)
1849              (setq name (substring from (1+ (match-beginning 0)) 
1850                                    (match-end 0)))))
1851     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1852     (list (or name from) (or address from))))
1853
1854 (defun gnus-fetch-field (field)
1855   "Return the value of the header FIELD of current article."
1856   (save-excursion
1857     (save-restriction
1858       (let ((case-fold-search t))
1859         (gnus-narrow-to-headers)
1860         (mail-fetch-field field)))))
1861
1862 (defun gnus-goto-colon ()
1863   (beginning-of-line)
1864   (search-forward ":" (gnus-point-at-eol) t))
1865
1866 (defun gnus-narrow-to-headers ()
1867   (widen)
1868   (save-excursion
1869     (narrow-to-region
1870      (goto-char (point-min))
1871      (if (search-forward "\n\n" nil t)
1872          (1- (point))
1873        (point-max)))))
1874
1875 (defun gnus-update-format-specifications ()
1876   (gnus-make-thread-indent-array)
1877   (setq gnus-summary-line-format-spec 
1878         (gnus-parse-format
1879          gnus-summary-line-format gnus-summary-line-format-alist))
1880   (gnus-update-summary-mark-positions)
1881   (setq gnus-summary-dummy-line-format-spec 
1882         (gnus-parse-format gnus-summary-dummy-line-format 
1883                            gnus-summary-dummy-line-format-alist))
1884   (setq gnus-group-line-format-spec
1885         (gnus-parse-format 
1886          gnus-group-line-format 
1887          gnus-group-line-format-alist))
1888   (if (and (string-match "%D" gnus-group-line-format)
1889            (not gnus-description-hashtb)
1890            gnus-read-active-file)
1891       (gnus-read-all-descriptions-files))
1892   (setq gnus-summary-mode-line-format-spec 
1893         (gnus-parse-format gnus-summary-mode-line-format 
1894                            gnus-summary-mode-line-format-alist))
1895   (setq gnus-article-mode-line-format-spec 
1896         (gnus-parse-format gnus-article-mode-line-format 
1897                            gnus-summary-mode-line-format-alist))
1898   (setq gnus-group-mode-line-format-spec 
1899         (gnus-parse-format gnus-group-mode-line-format 
1900                            gnus-group-mode-line-format-alist)))
1901
1902 (defun gnus-update-summary-mark-positions ()
1903   (save-excursion
1904     (let ((gnus-replied-mark 129)
1905           (gnus-score-below-mark 130)
1906           (gnus-score-over-mark 130)
1907           (thread nil)
1908           pos)
1909       (gnus-set-work-buffer)
1910       (gnus-summary-insert-line 
1911        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1912       (goto-char (point-min))
1913       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1914                                          (- (point) 2)))))
1915       (goto-char (point-min))
1916       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1917                                           (- (point) 2))) pos))
1918       (goto-char (point-min))
1919       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1920                                         (- (point) 2))) pos))
1921       (setq gnus-summary-mark-positions pos))))
1922
1923 (defun gnus-format-max-width (form length)
1924   (let* ((val (eval form))
1925          (valstr (if (numberp val) (int-to-string val) val)))
1926     (gnus-limit-string valstr length)))
1927
1928 (defun gnus-set-mouse-face (string)
1929   ;; Set mouse face property on STRING.
1930   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1931   string)
1932
1933 (defun gnus-parse-format (format spec-alist)
1934   ;; This function parses the FORMAT string with the help of the
1935   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1936   ;; string.  If the FORMAT string contains the specifiers %( and %)
1937   ;; the text between them will have the mouse-face text property.
1938   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1939       (if (and gnus-visual gnus-mouse-face)
1940           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1941                 (button (substring format (match-beginning 2) (match-end 2)))
1942                 (post (substring format (match-beginning 3) (match-end 3))))
1943             (list 'concat
1944                   (gnus-parse-simple-format pre spec-alist)
1945                   (list 'gnus-set-mouse-face
1946                         (gnus-parse-simple-format button spec-alist))
1947                   (gnus-parse-simple-format post spec-alist)))
1948         (gnus-parse-simple-format
1949          (concat (substring format (match-beginning 1) (match-end 1))
1950                  (substring format (match-beginning 2) (match-end 2))
1951                  (substring format (match-beginning 3) (match-end 3)))
1952          spec-alist))
1953     (gnus-parse-simple-format format spec-alist)))
1954
1955 (defun gnus-parse-simple-format (format spec-alist)
1956   ;; This function parses the FORMAT string with the help of the
1957   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1958   ;; string. The list will consist of the symbol `format', a format
1959   ;; specification string, and a list of forms depending on the
1960   ;; SPEC-ALIST.
1961   (let ((max-width 0)
1962         spec flist fstring newspec elem beg)
1963     (save-excursion
1964       (gnus-set-work-buffer)
1965       (insert format)
1966       (goto-char (point-min))
1967       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1968         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1969                                                      (match-end 2))))
1970         ;; First check if there are any specs that look anything like
1971         ;; "%12,12A", ie. with a "max width specification". These have
1972         ;; to be treated specially.
1973         (if (setq beg (match-beginning 1))
1974             (setq max-width 
1975                   (string-to-int 
1976                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1977           (setq max-width 0)
1978           (setq beg (match-beginning 2)))
1979         ;; Find the specification from `spec-alist'.
1980         (if (not (setq elem (cdr (assq spec spec-alist))))
1981             (setq elem '("*" ?s)))
1982         ;; Treat user defined format specifiers specially
1983         (and (eq (car elem) 'user-defined)
1984              (setq elem
1985                    (list 
1986                     (list (intern (concat "gnus-user-format-function-"
1987                                           (buffer-substring
1988                                            (match-beginning 3)
1989                                            (match-end 3))))
1990                           'header)
1991                     ?s))
1992              (delete-region (match-beginning 3) (match-end 3)))
1993         (if (not (zerop max-width))
1994             (let ((el (car elem)))
1995               (cond ((= (car (cdr elem)) ?c) 
1996                      (setq el (list 'char-to-string el)))
1997                     ((= (car (cdr elem)) ?d)
1998                      (numberp el) (setq el (list 'int-to-string el))))
1999               (setq flist (cons (list 'gnus-format-max-width el max-width) 
2000                                 flist))
2001               (setq newspec ?s))
2002           (setq flist (cons (car elem) flist))
2003           (setq newspec (car (cdr elem))))
2004         ;; Remove the old specification (and possibly a ",12" string).
2005         (delete-region beg (match-end 2))
2006         ;; Insert the new specification.
2007         (goto-char beg)
2008         (insert newspec))
2009       (setq fstring (buffer-substring 1 (point-max))))
2010     (cons 'format (cons fstring (nreverse flist)))))
2011
2012 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2013 (defun gnus-read-init-file ()
2014   (and gnus-init-file
2015        (or (and (file-exists-p gnus-init-file) 
2016                 ;; Don't try to load a directory.
2017                 (not (file-directory-p gnus-init-file)))
2018            (file-exists-p (concat gnus-init-file ".el"))
2019            (file-exists-p (concat gnus-init-file ".elc")))
2020        (load gnus-init-file nil t)))
2021
2022 (defun gnus-set-work-buffer ()
2023   (if (get-buffer gnus-work-buffer)
2024       (progn
2025         (set-buffer gnus-work-buffer)
2026         (erase-buffer))
2027     (set-buffer (get-buffer-create gnus-work-buffer))
2028     (kill-all-local-variables)
2029     (buffer-disable-undo (current-buffer))
2030     (gnus-add-current-to-buffer-list)))
2031
2032 ;; Article file names when saving.
2033
2034 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2035   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2036 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2037 Otherwise, it is like ~/News/news/group/num."
2038   (let ((default
2039           (expand-file-name
2040            (concat (if (gnus-use-long-file-name 'not-save)
2041                        (gnus-capitalize-newsgroup newsgroup)
2042                      (gnus-newsgroup-directory-form newsgroup))
2043                    "/" (int-to-string (header-number headers)))
2044            (or gnus-article-save-directory "~/News"))))
2045     (if (and last-file
2046              (string-equal (file-name-directory default)
2047                            (file-name-directory last-file))
2048              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2049         default
2050       (or last-file default))))
2051
2052 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2053   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2054 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2055 Otherwise, it is like ~/News/news/group/num."
2056   (let ((default
2057           (expand-file-name
2058            (concat (if (gnus-use-long-file-name 'not-save)
2059                        newsgroup
2060                      (gnus-newsgroup-directory-form newsgroup))
2061                    "/" (int-to-string (header-number headers)))
2062            (or gnus-article-save-directory "~/News"))))
2063     (if (and last-file
2064              (string-equal (file-name-directory default)
2065                            (file-name-directory last-file))
2066              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2067         default
2068       (or last-file default))))
2069
2070 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2071   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2072 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2073 Otherwise, it is like ~/News/news/group/news."
2074   (or last-file
2075       (expand-file-name
2076        (if (gnus-use-long-file-name 'not-save)
2077            (gnus-capitalize-newsgroup newsgroup)
2078          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2079        (or gnus-article-save-directory "~/News"))))
2080
2081 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2082   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2083 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2084 Otherwise, it is like ~/News/news/group/news."
2085   (or last-file
2086       (expand-file-name
2087        (if (gnus-use-long-file-name 'not-save)
2088            newsgroup
2089          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2090        (or gnus-article-save-directory "~/News"))))
2091
2092 ;; For subscribing new newsgroup
2093
2094 (defun gnus-subscribe-hierarchical-interactive (groups)
2095   (let ((groups (sort groups 'string<))
2096         prefixes prefix start ans group starts)
2097     (while groups
2098       (setq prefixes (list "^"))
2099       (while (and groups prefixes)
2100         (while (not (string-match (car prefixes) (car groups)))
2101           (setq prefixes (cdr prefixes)))
2102         (setq prefix (car prefixes))
2103         (setq start (1- (length prefix)))
2104         (if (and (string-match "[^\\.]\\." (car groups) start)
2105                  (cdr groups)
2106                  (setq prefix 
2107                        (concat "^" (substring (car groups) 0 (match-end 0))))
2108                  (string-match prefix (car (cdr groups))))
2109             (progn
2110               (setq prefixes (cons prefix prefixes))
2111               (message "Descend hierarchy %s? ([y]nsq): " 
2112                        (substring prefix 1 (1- (length prefix))))
2113               (setq ans (read-char))
2114               (cond ((= ans ?n)
2115                      (while (and groups 
2116                                  (string-match prefix 
2117                                                (setq group (car groups))))
2118                        (setq gnus-killed-list 
2119                              (cons group gnus-killed-list))
2120                        (gnus-sethash group group gnus-killed-hashtb)
2121                        (setq groups (cdr groups)))
2122                      (setq starts (cdr starts)))
2123                     ((= ans ?s)
2124                      (while (and groups 
2125                                  (string-match prefix 
2126                                                (setq group (car groups))))
2127                        (gnus-sethash group group gnus-killed-hashtb)
2128                        (gnus-subscribe-alphabetically (car groups))
2129                        (setq groups (cdr groups)))
2130                      (setq starts (cdr starts)))
2131                     ((= ans ?q)
2132                      (while groups
2133                        (setq group (car groups))
2134                        (setq gnus-killed-list (cons group gnus-killed-list))
2135                        (gnus-sethash group group gnus-killed-hashtb)
2136                        (setq groups (cdr groups))))
2137                     (t nil)))
2138           (message "Subscribe %s? ([n]yq)" (car groups))
2139           (setq ans (read-char))
2140           (setq group (car groups))
2141           (cond ((= ans ?y)
2142                  (gnus-subscribe-alphabetically (car groups))
2143                  (gnus-sethash group group gnus-killed-hashtb))
2144                 ((= ans ?q)
2145                  (while groups
2146                    (setq group (car groups))
2147                    (setq gnus-killed-list (cons group gnus-killed-list))
2148                    (gnus-sethash group group gnus-killed-hashtb)
2149                    (setq groups (cdr groups))))
2150                 (t 
2151                  (setq gnus-killed-list (cons group gnus-killed-list))
2152                  (gnus-sethash group group gnus-killed-hashtb)))
2153           (setq groups (cdr groups)))))))
2154
2155 (defun gnus-subscribe-randomly (newsgroup)
2156   "Subscribe new NEWSGROUP by making it the first newsgroup."
2157   (gnus-subscribe-newsgroup newsgroup))
2158
2159 (defun gnus-subscribe-alphabetically (newgroup)
2160   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2161   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2162   (let ((groups (cdr gnus-newsrc-alist))
2163         before)
2164     (while (and (not before) groups)
2165       (if (string< newgroup (car (car groups)))
2166           (setq before (car (car groups)))
2167         (setq groups (cdr groups))))
2168     (gnus-subscribe-newsgroup newgroup before)))
2169
2170 (defun gnus-subscribe-hierarchically (newgroup)
2171   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2172   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2173   (save-excursion
2174     (set-buffer (find-file-noselect gnus-current-startup-file))
2175     (let ((groupkey newgroup)
2176           before)
2177       (while (and (not before) groupkey)
2178         (goto-char (point-min))
2179         (let ((groupkey-re
2180                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2181           (while (and (re-search-forward groupkey-re nil t)
2182                       (progn
2183                         (setq before (buffer-substring
2184                                       (match-beginning 1) (match-end 1)))
2185                         (string< before newgroup)))))
2186         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2187         (setq groupkey
2188               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2189                   (substring groupkey (match-beginning 1) (match-end 1)))))
2190       (gnus-subscribe-newsgroup newgroup before))))
2191
2192 (defun gnus-subscribe-interactively (newsgroup)
2193   "Subscribe new NEWSGROUP interactively.
2194 It is inserted in hierarchical newsgroup order if subscribed. If not,
2195 it is killed."
2196   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2197       (gnus-subscribe-hierarchically newsgroup)
2198     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2199
2200 (defun gnus-subscribe-zombies (newsgroup)
2201   "Make new NEWSGROUP a zombie group."
2202   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2203
2204 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2205   "Subscribe new NEWSGROUP.
2206 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2207 the first newsgroup."
2208   ;; We subscribe the group by changing its level to `subscribed'.
2209   (gnus-group-change-level 
2210    newsgroup gnus-level-default-subscribed
2211    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2212   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2213
2214 ;; For directories
2215
2216 (defun gnus-newsgroup-directory-form (newsgroup)
2217   "Make hierarchical directory name from NEWSGROUP name."
2218   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2219         (len (length newsgroup))
2220         idx)
2221     ;; If this is a foreign group, we don't want to translate the
2222     ;; entire name.  
2223     (if (setq idx (string-match ":" newsgroup))
2224         (aset newsgroup idx ?/)
2225       (setq idx 0))
2226     ;; Replace all occurrences of `.' with `/'.
2227     (while (< idx len)
2228       (if (= (aref newsgroup idx) ?.)
2229           (aset newsgroup idx ?/))
2230       (setq idx (1+ idx)))
2231     newsgroup))
2232
2233 (defun gnus-make-directory (dir)
2234   "Make DIRECTORY recursively."
2235   (let* ((dir (expand-file-name dir default-directory))
2236          dirs)
2237     (if (string-match "/$" dir)
2238         (setq dir (substring dir 0 (match-beginning 0))))
2239     (while (not (file-exists-p dir))
2240       (setq dirs (cons dir dirs))
2241       (string-match "/[^/]+$" dir)
2242       (setq dir (substring dir 0 (match-beginning 0))))
2243     (while dirs
2244       (make-directory (car dirs))
2245       (setq dirs (cdr dirs)))))
2246
2247 (defun gnus-capitalize-newsgroup (newsgroup)
2248   "Capitalize NEWSGROUP name."
2249   (and (not (zerop (length newsgroup)))
2250        (concat (char-to-string (upcase (aref newsgroup 0)))
2251                (substring newsgroup 1))))
2252
2253 ;; Var
2254
2255 (defun gnus-simplify-subject (subject &optional re-only)
2256   "Remove `Re:' and words in parentheses.
2257 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2258   (let ((case-fold-search t))           ;Ignore case.
2259     ;; Remove `Re:' and `Re^N:'.
2260     (if (string-match "^re:[ \t]*" subject)
2261         (setq subject (substring subject (match-end 0))))
2262     ;; Remove words in parentheses from end.
2263     (or re-only
2264         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2265           (setq subject (substring subject 0 (match-beginning 0)))))
2266     ;; Return subject string.
2267     subject))
2268
2269 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2270 ;; all whitespace.
2271 (defun gnus-simplify-subject-fuzzy (subject)
2272   (let ((case-fold-search t))
2273     (save-excursion
2274       (gnus-set-work-buffer)
2275       (insert subject)
2276       (inline (gnus-simplify-buffer-fuzzy))
2277       (buffer-string))))
2278
2279 (defun gnus-simplify-buffer-fuzzy ()
2280   (goto-char (point-min))
2281   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2282   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2283                             nil t)
2284     (replace-match "" t t))
2285   (goto-char (point-min))
2286   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2287     (replace-match "" t t))
2288   (goto-char (point-min))
2289   (while (re-search-forward "[ \t]+" nil t)
2290     (replace-match " " t t))
2291   (goto-char (point-min))
2292   (while (re-search-forward "[ \t]+$" nil t)
2293     (replace-match "" t t))
2294   (goto-char (point-min))
2295   (while (re-search-forward "^[ \t]+" nil t)
2296     (replace-match "" t t))
2297   (if gnus-simplify-subject-fuzzy-regexp
2298       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2299         (replace-match "" t t)))
2300     )
2301
2302 ;; Add the current buffer to the list of buffers to be killed on exit. 
2303 (defun gnus-add-current-to-buffer-list ()
2304   (or (memq (current-buffer) gnus-buffer-list)
2305       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2306
2307 (defun gnus-string> (s1 s2)
2308   (not (or (string< s1 s2)
2309            (string= s1 s2))))
2310
2311 ;; Functions accessing headers.
2312 ;; Functions are more convenient than macros in some cases.
2313
2314 (defun gnus-header-number (header)
2315   (header-number header))
2316
2317 (defun gnus-header-subject (header)
2318   (header-subject header))
2319
2320 (defun gnus-header-from (header)
2321   (header-from header))
2322
2323 (defun gnus-header-xref (header)
2324   (header-xref header))
2325
2326 (defun gnus-header-lines (header)
2327   (header-lines header))
2328
2329 (defun gnus-header-date (header)
2330   (header-date header))
2331
2332 (defun gnus-header-id (header)
2333   (header-id header))
2334
2335 (defun gnus-header-references (header)
2336   (header-references header))
2337
2338 ;;; General various misc type functions.
2339
2340 (defun gnus-clear-system ()
2341   "Clear all variables and buffers."
2342   ;; Clear Gnus variables.
2343   (let ((variables gnus-variable-list))
2344     (while variables
2345       (set (car variables) nil)
2346       (setq variables (cdr variables))))
2347   ;; Clear other internal variables.
2348   (setq gnus-list-of-killed-groups nil
2349         gnus-have-read-active-file nil
2350         gnus-newsrc-alist nil
2351         gnus-newsrc-hashtb nil
2352         gnus-killed-list nil
2353         gnus-zombie-list nil
2354         gnus-killed-hashtb nil
2355         gnus-active-hashtb nil
2356         gnus-moderated-list nil
2357         gnus-description-hashtb nil
2358         gnus-newsgroup-headers nil
2359         gnus-newsgroup-headers-hashtb-by-number nil
2360         gnus-newsgroup-name nil
2361         gnus-server-alist nil
2362         gnus-current-select-method nil)
2363   ;; Reset any score variables.
2364   (and (boundp 'gnus-score-cache)
2365        (set 'gnus-score-cache nil))
2366   (and (boundp 'gnus-internal-global-score-files)
2367        (set 'gnus-internal-global-score-files nil))
2368   ;; Kill the startup file.
2369   (and gnus-current-startup-file
2370        (get-file-buffer gnus-current-startup-file)
2371        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2372   ;; Save any cache buffers.
2373   (and gnus-use-cache (gnus-cache-save-buffers))
2374   ;; Clear the dribble buffer.
2375   (gnus-dribble-clear)
2376   ;; Kill global KILL file buffer.
2377   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2378       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2379   (gnus-kill-buffer nntp-server-buffer)
2380   ;; Kill Gnus buffers.
2381   (while gnus-buffer-list
2382     (gnus-kill-buffer (car gnus-buffer-list))
2383     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2384
2385 (defun gnus-windows-old-to-new (setting)
2386   (if (symbolp setting)
2387       (setq setting 
2388             (cond ((eq setting 'SelectArticle)
2389                    'article)
2390                   ((eq setting 'SelectSubject)
2391                    'summary)
2392                   ((eq setting 'SelectNewsgroup)
2393                    'group)
2394                   (t setting))))
2395   (if (or (listp setting)
2396           (not (and gnus-window-configuration
2397                     (memq setting '(group summary article)))))
2398       setting
2399     (let* ((setting (if (eq setting 'group) 
2400                         (if (assq 'newsgroup gnus-window-configuration)
2401                             'newsgroup
2402                           'newsgroups) setting))
2403            (elem (car (cdr (assq setting gnus-window-configuration))))
2404            (total (apply '+ elem))
2405            (types '(group summary article))
2406            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2407            (i 0)
2408            perc
2409            out)
2410       (while (< i 3)
2411         (or (zerop (nth i elem))
2412             (progn
2413               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2414               (setq out (cons (if (eq pbuf (nth i types))
2415                                   (vector (nth i types) perc 'point)
2416                                 (vector (nth i types) perc))
2417                               out))))
2418         (setq i (1+ i)))
2419       (list (nreverse out)))))
2420            
2421 (defun gnus-add-configuration (conf)
2422   (setq gnus-buffer-configuration 
2423         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2424                          gnus-buffer-configuration))))
2425
2426 (defun gnus-configure-windows (setting)
2427   (setq setting (gnus-windows-old-to-new setting))
2428   (let ((r (if (symbolp setting)
2429                   (cdr (assq setting gnus-buffer-configuration))
2430                 setting))
2431         (in-buf (current-buffer))
2432         rule val w height hor ohor heights sub jump-buffer
2433         rel total to-buf)
2434     (or r (error "No such setting: %s" setting))
2435
2436     ;; Either remove all windows or just remove all Gnus windows.
2437     (if gnus-use-full-window
2438         (delete-other-windows)
2439       (gnus-remove-some-windows)
2440       (switch-to-buffer nntp-server-buffer))
2441
2442     (while r
2443       (setq hor (car r)
2444             ohor nil)
2445
2446       ;; We have to do the (possible) horizontal splitting before the
2447       ;; vertical. 
2448       (if (and (listp (car hor)) 
2449                (eq (car (car hor)) 'horizontal))
2450           (progn
2451             (split-window 
2452              nil
2453              (if (integerp (nth 1 (car hor)))
2454                  (nth 1 (car hor))
2455                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2456              t)
2457             (setq hor (cdr hor))))
2458
2459       ;; Go through the rules and eval the elements that are to be
2460       ;; evaled.  
2461       (while hor
2462         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2463             (progn
2464               ;; Expand short buffer name.
2465               (setq w (aref val 0))
2466               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2467                    (progn
2468                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2469                      (aset val 0 w)))
2470               (setq ohor (cons val ohor))))
2471         (setq hor (cdr hor)))
2472       (setq rule (cons (nreverse ohor) rule))
2473       (setq r (cdr r)))
2474     (setq rule (nreverse rule))
2475
2476     ;; We tally the window sizes.
2477     (setq total (window-height))
2478     (while rule
2479       (setq hor (car rule))
2480       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2481           (setq hor (cdr hor)))
2482       (setq sub 0)
2483       (while hor
2484         (setq rel (aref (car hor) 1)
2485               heights (cons
2486                        (cond ((and (floatp rel) (= 1.0 rel))
2487                               'x)
2488                              ((integerp rel)
2489                               rel)
2490                              (t
2491                               (max (floor (* total rel)) 4)))
2492                        heights)
2493               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2494               hor (cdr hor)))
2495       (setq heights (nreverse heights)
2496             hor (car rule))
2497
2498       ;; We then go through these heighs and create windows for them.
2499       (while heights
2500         (setq height (car heights)
2501               heights (cdr heights))
2502         (and (eq height 'x)
2503              (setq height (- total sub)))
2504         (and heights
2505              (split-window nil height))
2506         (setq to-buf (aref (car hor) 0))
2507         (switch-to-buffer 
2508          (cond ((not to-buf)
2509                 in-buf)
2510                ((symbolp to-buf)
2511                 (symbol-value (aref (car hor) 0)))
2512                (t
2513                 (aref (car hor) 0))))
2514         (and (> (length (car hor)) 2)
2515              (eq (aref (car hor) 2) 'point)
2516              (setq jump-buffer (current-buffer)))
2517         (other-window 1)
2518         (setq hor (cdr hor)))
2519       
2520       (setq rule (cdr rule)))
2521
2522     ;; Finally, we pop to the buffer that's supposed to have point. 
2523     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2524
2525     (select-window (get-buffer-window jump-buffer))
2526     (set-buffer jump-buffer)))
2527       
2528 (defun gnus-remove-some-windows ()
2529   (let ((buffers gnus-window-to-buffer)
2530         (first t)
2531         buf)
2532     (save-excursion
2533       ;; Remove windows on all known Gnus buffers.
2534       (while buffers
2535         (setq buf (cdr (car buffers)))
2536         (if (symbolp buf)
2537             (setq buf (and (boundp buf) (symbol-value buf))))
2538         (and buf 
2539              (get-buffer-window buf)
2540              (progn
2541                (if first
2542                    (progn
2543                      (pop-to-buffer buf)
2544                      (switch-to-buffer nntp-server-buffer)
2545                      (setq first nil))
2546                  (delete-window (get-buffer-window buf)))))
2547         (setq buffers (cdr buffers)))
2548       ;; Remove windows on *all* summary buffers.
2549       (let ((buffers (buffer-list)))
2550         (while buffers
2551           (if (and (string-match 
2552                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2553                    (get-buffer-window (car buffers)))
2554               (delete-window (get-buffer-window (car buffers))))
2555           (setq buffers (cdr buffers)))))))
2556                           
2557 (defun gnus-version ()
2558   "Version numbers of this version of Gnus."
2559   (interactive)
2560   (let ((methods gnus-valid-select-methods)
2561         (mess gnus-version)
2562         meth)
2563     ;; Go through all the legal select methods and add their version
2564     ;; numbers to the total version string. Only the backends that are
2565     ;; currently in use will have their message numbers taken into
2566     ;; consideration. 
2567     (while methods
2568       (setq meth (intern (concat (car (car methods)) "-version")))
2569       (and (boundp meth)
2570            (stringp (symbol-value meth))
2571            (setq mess (concat mess "; " (symbol-value meth))))
2572       (setq methods (cdr methods)))
2573     (gnus-message 2 mess)))
2574
2575 (defun gnus-info-find-node ()
2576   "Find Info documentation of Gnus."
2577   (interactive)
2578   ;; Enlarge info window if needed.
2579   (let ((mode major-mode))
2580     (gnus-configure-windows 'info)
2581     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2582
2583 (defun gnus-overload-functions (&optional overloads)
2584   "Overload functions specified by optional argument OVERLOADS.
2585 If nothing is specified, use the variable gnus-overload-functions."
2586   (let ((defs nil)
2587         (overloads (or overloads gnus-overload-functions)))
2588     (while overloads
2589       (setq defs (car overloads))
2590       (setq overloads (cdr overloads))
2591       ;; Load file before overloading function if necessary.  Make
2592       ;; sure we cannot use `require' always.
2593       (and (not (fboundp (car defs)))
2594            (car (cdr (cdr defs)))
2595            (load (car (cdr (cdr defs))) nil 'nomessage))
2596       (fset (car defs) (car (cdr defs))))))
2597
2598 (defun gnus-replace-chars-in-string (string from to)
2599   "Replace characters in STRING from FROM to TO."
2600   (let ((string (substring string 0))   ;Copy string.
2601         (len (length string))
2602         (idx 0))
2603     ;; Replace all occurrences of FROM with TO.
2604     (while (< idx len)
2605       (if (= (aref string idx) from)
2606           (aset string idx to))
2607       (setq idx (1+ idx)))
2608     string))
2609
2610 (defun gnus-days-between (date1 date2)
2611   ;; Return the number of days between date1 and date2.
2612   (- (gnus-day-number date1) (gnus-day-number date2)))
2613
2614 (defun gnus-day-number (date)
2615   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2616                      (timezone-parse-date date))))
2617     (timezone-absolute-from-gregorian 
2618      (nth 1 dat) (nth 2 dat) (car dat))))
2619
2620 ;; Returns a floating point number that says how many seconds have
2621 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2622 (defun gnus-seconds-since-epoch (date)
2623   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2624                         (timezone-parse-date date)))
2625          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2626                         (timezone-parse-time
2627                          (aref (timezone-parse-date date) 3))))
2628          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2629                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2630          (tday (- (timezone-absolute-from-gregorian 
2631                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2632                   (timezone-absolute-from-gregorian 
2633                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2634     (+ (nth 2 ttime)
2635        (* (nth 1 ttime) 60)
2636        (* 1.0 (nth 0 ttime) 60 60)
2637        (* 1.0 tday 60 60 24))))
2638
2639 (defun gnus-file-newer-than (file date)
2640   (let ((fdate (nth 5 (file-attributes file))))
2641     (or (> (car fdate) (car date))
2642         (and (= (car fdate) (car date))
2643              (> (nth 1 fdate) (nth 1 date))))))
2644
2645 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2646 ;; the echo area.
2647 (defun gnus-y-or-n-p (prompt)
2648   (prog1
2649       (y-or-n-p prompt)
2650     (message "")))
2651
2652 (defun gnus-yes-or-no-p (prompt)
2653   (prog1
2654       (yes-or-no-p prompt)
2655     (message "")))
2656
2657 ;; Check whether to use long file names.
2658 (defun gnus-use-long-file-name (symbol)
2659   ;; The variable has to be set...
2660   (and gnus-use-long-file-name
2661        ;; If it isn't a list, then we return t.
2662        (or (not (listp gnus-use-long-file-name))
2663            ;; If it is a list, and the list contains `symbol', we
2664            ;; return nil.  
2665            (not (memq symbol gnus-use-long-file-name)))))
2666
2667 ;; I suspect there's a better way, but I haven't taken the time to do
2668 ;; it yet. -erik selberg@cs.washington.edu
2669 (defun gnus-dd-mmm (messy-date)
2670   "Return a string like DD-MMM from a big messy string"
2671   (let ((datevec (timezone-parse-date messy-date)))
2672     (format "%2s-%s"
2673             (or (aref datevec 2) "??")
2674             (capitalize
2675              (or (car 
2676                   (nth (1- (string-to-number (aref datevec 1)))
2677                        timezone-months-assoc))
2678                  "???")))))
2679
2680 ;; Make a hash table (default and minimum size is 255).
2681 ;; Optional argument HASHSIZE specifies the table size.
2682 (defun gnus-make-hashtable (&optional hashsize)
2683   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2684
2685 ;; Make a number that is suitable for hashing; bigger than MIN and one
2686 ;; less than 2^x.
2687 (defun gnus-create-hash-size (min)
2688   (let ((i 1))
2689     (while (< i min)
2690       (setq i (* 2 i)))
2691     (1- i)))
2692
2693 ;; Show message if message has a lower level than `gnus-verbose'. 
2694 ;; Guide-line for numbers:
2695 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2696 ;; for things that take a long time, 7 - not very important messages
2697 ;; on stuff, 9 - messages inside loops.
2698 (defun gnus-message (level &rest args)
2699   (if (<= level gnus-verbose)
2700       (apply 'message args)
2701     ;; We have to do this format thingie here even if the result isn't
2702     ;; shown - the return value has to be the same as the return value
2703     ;; from `message'.
2704     (apply 'format args)))
2705
2706 ;; Generate a unique new group name.
2707 (defun gnus-generate-new-group-name (leaf)
2708   (let ((name leaf)
2709         (num 0))
2710     (while (gnus-gethash name gnus-newsrc-hashtb)
2711       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2712     name))
2713
2714 (defun gnus-find-file-noselect (file &optional force)
2715   "Does vaguely the same as find-file-noselect. No hooks are run."
2716   (let (buf insert)
2717     (if (setq buf (get-file-buffer file))
2718         (setq insert force)
2719       (setq buf (create-file-buffer file))
2720       (setq insert t))
2721     (if (not insert)
2722         buf
2723       (save-excursion
2724         (set-buffer buf)
2725         (erase-buffer)
2726         (and (file-readable-p file)
2727              (insert-file-contents file))
2728         (set-visited-file-name file)
2729         (set-buffer-modified-p nil)
2730         (current-buffer)))))
2731
2732 ;;; List and range functions
2733
2734 (defun gnus-last-element (list)
2735   "Return last element of LIST."
2736   (while (cdr list)
2737     (setq list (cdr list)))
2738   (car list))
2739
2740 (defun gnus-copy-sequence (list)
2741   "Do a complete, total copy of a list."
2742   (if (and (consp list) (not (consp (cdr list))))
2743       (cons (car list) (cdr list))
2744     (mapcar (lambda (elem) (if (consp elem) 
2745                                (if (consp (cdr elem))
2746                                    (gnus-copy-sequence elem)
2747                                  (cons (car elem) (cdr elem)))
2748                              elem))
2749             list)))
2750
2751 (defun gnus-set-difference (list1 list2)
2752   "Return a list of elements of LIST1 that do not appear in LIST2."
2753   (let ((list1 (copy-sequence list1)))
2754     (while list2
2755       (setq list1 (delq (car list2) list1))
2756       (setq list2 (cdr list2)))
2757     list1))
2758
2759 (defun gnus-sorted-complement (list1 list2)
2760   "Return a list of elements of LIST1 that do not appear in LIST2.
2761 Both lists have to be sorted over <."
2762   (let (out)
2763     (if (or (null list1) (null list2))
2764         (or list1 list2)
2765       (while (and list1 list2)
2766         (cond ((= (car list1) (car list2))
2767                (setq list1 (cdr list1)
2768                      list2 (cdr list2)))
2769               ((< (car list1) (car list2))
2770                (setq out (cons (car list1) out))
2771                (setq list1 (cdr list1)))
2772               (t
2773                (setq out (cons (car list2) out))
2774                (setq list2 (cdr list2)))))
2775       (nconc (nreverse out) (or list1 list2)))))
2776
2777 (defun gnus-intersection (list1 list2)      
2778   (let ((result nil))
2779     (while list2
2780       (if (memq (car list2) list1)
2781           (setq result (cons (car list2) result)))
2782       (setq list2 (cdr list2)))
2783     result))
2784
2785 (defun gnus-sorted-intersection (list1 list2)
2786   ;; LIST1 and LIST2 have to be sorted over <.
2787   (let (out)
2788     (while (and list1 list2)
2789       (cond ((= (car list1) (car list2))
2790              (setq out (cons (car list1) out)
2791                    list1 (cdr list1)
2792                    list2 (cdr list2)))
2793             ((< (car list1) (car list2))
2794              (setq list1 (cdr list1)))
2795             (t
2796              (setq list2 (cdr list2)))))
2797     (nreverse out)))
2798
2799 (defun gnus-set-sorted-intersection (list1 list2)
2800   ;; LIST1 and LIST2 have to be sorted over <.
2801   ;; This function modifies LIST1.
2802   (let* ((top (cons nil list1))
2803          (prev top))
2804   (while (and list1 list2)
2805     (cond ((= (car list1) (car list2))
2806            (setq prev list1
2807                  list1 (cdr list1)
2808                  list2 (cdr list2)))
2809           ((< (car list1) (car list2))
2810            (setcdr prev (cdr list1))
2811            (setq list1 (cdr list1)))
2812           (t
2813            (setq list2 (cdr list2)))))
2814   (setcdr prev nil)
2815   (cdr top)))
2816
2817 (defun gnus-compress-sequence (numbers &optional always-list)
2818   "Convert list of numbers to a list of ranges or a single range.
2819 If ALWAYS-LIST is non-nil, this function will always release a list of
2820 ranges."
2821   (let* ((first (car numbers))
2822          (last (car numbers))
2823          result)
2824     (if (null numbers)
2825         nil
2826       (if (not (listp (cdr numbers)))
2827           numbers
2828         (while numbers
2829           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2830                 ((= (1+ last) (car numbers)) ;Still in sequence
2831                  (setq last (car numbers)))
2832                 (t                      ;End of one sequence
2833                  (setq result 
2834                        (cons (if (= first last) first
2835                                (cons first last)) result))
2836                  (setq first (car numbers))
2837                  (setq last  (car numbers))))
2838           (setq numbers (cdr numbers)))
2839         (if (and (not always-list) (null result))
2840             (if (= first last) (list first) (cons first last))
2841           (nreverse (cons (if (= first last) first (cons first last))
2842                           result)))))))
2843
2844 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2845 (defun gnus-uncompress-range (ranges)
2846   "Expand a list of ranges into a list of numbers.
2847 RANGES is either a single range on the form `(num . num)' or a list of
2848 these ranges."
2849   (let (first last result)
2850     (cond 
2851      ((null ranges)
2852       nil)
2853      ((not (listp (cdr ranges)))
2854       (setq first (car ranges))
2855       (setq last (cdr ranges))
2856       (while (<= first last)
2857         (setq result (cons first result))
2858         (setq first (1+ first)))
2859       (nreverse result))
2860      (t
2861       (while ranges
2862         (if (atom (car ranges))
2863             (if (numberp (car ranges))
2864                 (setq result (cons (car ranges) result)))
2865           (setq first (car (car ranges)))
2866           (setq last  (cdr (car ranges)))
2867           (while (<= first last)
2868             (setq result (cons first result))
2869             (setq first (1+ first))))
2870         (setq ranges (cdr ranges)))
2871       (nreverse result)))))
2872
2873 (defun gnus-add-to-range (ranges list)
2874   "Return a list of ranges that has all articles from both RANGES and LIST.
2875 Note: LIST has to be sorted over `<'."
2876   (if (not ranges)
2877       (gnus-compress-sequence list t)
2878     (setq list (copy-sequence list))
2879     (or (listp (cdr ranges))
2880         (setq ranges (list ranges)))
2881     (let ((out ranges)
2882           ilist lowest highest temp)
2883       (while (and ranges list)
2884         (setq ilist list)
2885         (setq lowest (or (and (atom (car ranges)) (car ranges))
2886                          (car (car ranges))))
2887         (while (and list (cdr list) (< (car (cdr list)) lowest))
2888           (setq list (cdr list)))
2889         (if (< (car ilist) lowest)
2890             (progn
2891               (setq temp list)
2892               (setq list (cdr list))
2893               (setcdr temp nil)
2894               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2895         (setq highest (or (and (atom (car ranges)) (car ranges))
2896                           (cdr (car ranges))))
2897         (while (and list (<= (car list) highest))
2898           (setq list (cdr list)))
2899         (setq ranges (cdr ranges)))
2900       (if list
2901           (setq out (nconc (gnus-compress-sequence list t) out)))
2902       (setq out (sort out (lambda (r1 r2) 
2903                             (< (or (and (atom r1) r1) (car r1))
2904                                (or (and (atom r2) r2) (car r2))))))
2905       (setq ranges out)
2906       (while ranges
2907         (if (atom (car ranges))
2908             (if (cdr ranges)
2909                 (if (atom (car (cdr ranges)))
2910                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2911                         (progn
2912                           (setcar ranges (cons (car ranges) 
2913                                                (car (cdr ranges))))
2914                           (setcdr ranges (cdr (cdr ranges)))))
2915                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2916                       (progn
2917                         (setcar (car (cdr ranges)) (car ranges))
2918                         (setcar ranges (car (cdr ranges)))
2919                         (setcdr ranges (cdr (cdr ranges)))))))
2920           (if (cdr ranges)
2921               (if (atom (car (cdr ranges)))
2922                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2923                       (progn
2924                         (setcdr (car ranges) (car (cdr ranges)))
2925                         (setcdr ranges (cdr (cdr ranges)))))
2926                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2927                     (progn
2928                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2929                       (setcdr ranges (cdr (cdr ranges))))))))
2930         (setq ranges (cdr ranges)))
2931       out)))
2932
2933 (defun gnus-remove-from-range (ranges list)
2934   "Return a list of ranges that has all articles from LIST removed from RANGES.
2935 Note: LIST has to be sorted over `<'."
2936   ;; !!! This function shouldn't look like this, but I've got a headache.
2937   (gnus-compress-sequence 
2938    (gnus-sorted-complement
2939     (gnus-uncompress-range ranges) list)))
2940
2941 (defun gnus-member-of-range (number ranges)
2942   (if (not (listp (cdr ranges)))
2943       (and (>= number (car ranges)) 
2944            (<= number (cdr ranges)))
2945     (let ((not-stop t))
2946       (while (and ranges 
2947                   (if (numberp (car ranges))
2948                       (>= number (car ranges))
2949                     (>= number (car (car ranges))))
2950                   not-stop)
2951         (if (if (numberp (car ranges))
2952                 (= number (car ranges))
2953               (and (>= number (car (car ranges)))
2954                    (<= number (cdr (car ranges)))))
2955             (setq not-stop nil))
2956         (setq ranges (cdr ranges)))
2957       (not not-stop))))
2958
2959 \f
2960 ;;;
2961 ;;; Gnus group mode
2962 ;;;
2963
2964 (defvar gnus-group-mode-map nil)
2965 (defvar gnus-group-group-map nil)
2966 (defvar gnus-group-mark-map nil)
2967 (defvar gnus-group-list-map nil)
2968 (defvar gnus-group-sub-map nil)
2969 (put 'gnus-group-mode 'mode-class 'special)
2970
2971 (if gnus-group-mode-map
2972     nil
2973   (setq gnus-group-mode-map (make-keymap))
2974   (suppress-keymap gnus-group-mode-map)
2975   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2976   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2977   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2978   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2979   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2980   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2981   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2982   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2983   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2984   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2985   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2986   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2987   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2988   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2989   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2990   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2991   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2992   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2993   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2994   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2995   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2996   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2997   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2998   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2999   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3000   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3001   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3002   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3003   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3004   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3005   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3006   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3007   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3008   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3009   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3010   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3011   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3012   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3013   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3014   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3015   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3016   (define-key gnus-group-mode-map "V" 'gnus-version)
3017   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3018   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3019   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3020   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3021   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3022   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3023   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3024   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3025   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3026   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3027   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3028   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3029   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3030   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3031   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3032
3033   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3034   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3035   (define-prefix-command 'gnus-group-mark-map)
3036   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3037   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3038   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3039   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3040
3041   (define-prefix-command 'gnus-group-group-map)
3042   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3043   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3044   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3045   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3046   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3047   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3048   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3049   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3050   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3051   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3052   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3053   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3054   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3055   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3056   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3057   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3058   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3059   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3060
3061   (define-prefix-command 'gnus-group-list-map)
3062   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3063   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3064   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3065   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3066   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3067   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3068   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3069   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3070   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3071
3072   (define-prefix-command 'gnus-group-sub-map)
3073   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3074   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3075   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3076   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3077   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3078   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3079   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3080   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3081
3082 (defun gnus-group-mode ()
3083   "Major mode for reading news.
3084
3085 All normal editing commands are switched off.
3086 \\<gnus-group-mode-map>
3087 The group buffer lists (some of) the groups available.  For instance,
3088 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3089 lists all zombie groups. 
3090
3091 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3092 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3093
3094 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3095
3096 The following commands are available:
3097
3098 \\{gnus-group-mode-map}"
3099   (interactive)
3100   (if gnus-visual (gnus-group-make-menu-bar))
3101   (kill-all-local-variables)
3102   (setq mode-line-modified "-- ")
3103   (make-local-variable 'mode-line-format)
3104   (setq mode-line-format (copy-sequence mode-line-format))
3105   (and (equal (nth 3 mode-line-format) "   ")
3106        (setcar (nthcdr 3 mode-line-format) ""))
3107   (setq major-mode 'gnus-group-mode)
3108   (setq mode-name "Group")
3109   (gnus-group-set-mode-line)
3110   (setq mode-line-process nil)
3111   (use-local-map gnus-group-mode-map)
3112   (buffer-disable-undo (current-buffer))
3113   (setq truncate-lines t)
3114   (setq buffer-read-only t)
3115   (run-hooks 'gnus-group-mode-hook))
3116
3117 (defun gnus-mouse-pick-group (e)
3118   (interactive "e")
3119   (mouse-set-point e)
3120   (gnus-group-read-group nil))
3121
3122 ;;;###autoload
3123 (defun gnus-no-server (&optional arg)
3124   "Read network news.
3125 If ARG is a positive number, Gnus will use that as the
3126 startup level. If ARG is nil, Gnus will be started at level 2. 
3127 If ARG is non-nil and not a positive number, Gnus will
3128 prompt the user for the name of an NNTP server to use.
3129 As opposed to `gnus', this command will not connect to the local server."
3130   (interactive "P")
3131   (setq gnus-group-use-permanent-levels t)
3132   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3133
3134 (defalias '\(ding\) 'gnus)
3135
3136 ;;;###autoload
3137 (defun gnus (&optional arg dont-connect)
3138   "Read network news.
3139 If ARG is non-nil and a positive number, Gnus will use that as the
3140 startup level. If ARG is non-nil and not a positive number, Gnus will
3141 prompt the user for the name of an NNTP server to use."
3142   (interactive "P")
3143   (if (get-buffer gnus-group-buffer)
3144       (progn
3145         (switch-to-buffer gnus-group-buffer)
3146         (gnus-group-get-new-news))
3147     (gnus-clear-system)
3148     (nnheader-init-server-buffer)
3149     (gnus-read-init-file)
3150     (let ((level (and arg (numberp arg) (> arg 0) arg))
3151           did-connect)
3152       (unwind-protect
3153           (progn
3154             (gnus-group-setup-buffer)
3155             (or dont-connect 
3156                 (setq did-connect
3157                       (gnus-start-news-server (and arg (not level))))))
3158         (if (and (not dont-connect) 
3159                  (not did-connect))
3160             (gnus-group-quit)
3161           (run-hooks 'gnus-startup-hook)
3162           ;; NNTP server is successfully open. 
3163           (gnus-update-format-specifications)
3164           (gnus-summary-make-display-table)
3165           (let ((buffer-read-only nil))
3166             (erase-buffer)
3167             (if (not gnus-inhibit-startup-message)
3168                 (progn
3169                   (gnus-group-startup-message)
3170                   (sit-for 0))))
3171           (gnus-setup-news nil level)
3172           (and gnus-use-dribble-file (gnus-dribble-open))
3173           (gnus-group-list-groups level)
3174           (gnus-configure-windows 'group))))))
3175
3176 (defun gnus-group-startup-message (&optional x y)
3177   "Insert startup message in current buffer."
3178   ;; Insert the message.
3179   (erase-buffer)
3180   (insert
3181    (format "
3182      %s
3183            A newsreader 
3184       for GNU Emacs
3185
3186         Based on GNUS 
3187              written by 
3188      Masanobu UMEDA
3189
3190        A Praxis Release
3191       larsi@ifi.uio.no
3192
3193            gnus-version))
3194   ;; And then hack it.
3195   ;; 18 is the longest line.
3196   (indent-rigidly (point-min) (point-max) 
3197                   (/ (max (- (window-width) (or x 28)) 0) 2))
3198   (goto-char (point-min))
3199   ;; +4 is fuzzy factor.
3200   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3201
3202   ;; Fontify some.
3203   (goto-char (point-min))
3204   (search-forward "Praxis")
3205   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3206   (goto-char (point-min)))
3207
3208 (defun gnus-group-setup-buffer ()
3209   (or (get-buffer gnus-group-buffer)
3210       (progn
3211         (switch-to-buffer gnus-group-buffer)
3212         (gnus-add-current-to-buffer-list)
3213         (gnus-group-mode)
3214         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3215
3216 (defun gnus-group-list-groups (level &optional unread)
3217   "List newsgroups with level LEVEL or lower that have unread articles.
3218 Default is all subscribed groups.
3219 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3220   (interactive (list (and current-prefix-arg
3221                           (prefix-numeric-value current-prefix-arg))))
3222   (if gnus-group-use-permanent-levels
3223       (progn
3224         (setq gnus-group-default-list-level 
3225               (or level gnus-group-default-list-level))
3226         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3227     (setq level (or level gnus-group-default-list-level 
3228                     gnus-level-subscribed)))
3229   (gnus-group-setup-buffer)     ;May call from out of group buffer
3230   (let ((case-fold-search nil)
3231         (group (gnus-group-group-name)))
3232     (funcall gnus-group-prepare-function level unread nil)
3233     (if (zerop (buffer-size))
3234         (gnus-message 5 gnus-no-groups-message)
3235       (goto-char (point-min))
3236       (if (not group)
3237           ;; Go to the first group with unread articles.
3238           (gnus-group-search-forward nil nil nil t)
3239         ;; Find the right group to put point on. If the current group
3240         ;; has disapeared in the new listing, try to find the next
3241         ;; one. If no next one can be found, just leave point at the
3242         ;; first newsgroup in the buffer.
3243         (if (not (gnus-goto-char
3244                   (text-property-any (point-min) (point-max) 
3245                                      'gnus-group (intern group))))
3246             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3247               (while (and newsrc
3248                           (not (gnus-goto-char 
3249                                 (text-property-any 
3250                                  (point-min) (point-max) 'gnus-group 
3251                                  (intern (car (car newsrc)))))))
3252                 (setq newsrc (cdr newsrc)))
3253               (or newsrc (progn (goto-char (point-max))
3254                                 (forward-line -1))))))
3255       ;; Adjust cursor point.
3256       (gnus-group-position-cursor))))
3257
3258 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3259   "List all newsgroups with unread articles of level LEVEL or lower.
3260 If ALL is non-nil, list groups that have no unread articles.
3261 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3262 If REGEXP, only list groups matching REGEXP."
3263   (set-buffer gnus-group-buffer)
3264   (let ((buffer-read-only nil)
3265         (newsrc (cdr gnus-newsrc-alist))
3266         (lowest (or lowest 1))
3267         info clevel unread group)
3268     (erase-buffer)
3269     (if (< lowest gnus-level-zombie)
3270         ;; List living groups.
3271         (while newsrc
3272           (setq info (car newsrc)
3273                 group (car info)
3274                 newsrc (cdr newsrc)
3275                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3276           (and unread ; This group might be bogus
3277                (or (not regexp)
3278                    (string-match regexp group))
3279                (<= (setq clevel (car (cdr info))) level) 
3280                (>= clevel lowest)
3281                (or all            ; We list all groups?
3282                    (eq unread t)  ; We list unactivated groups
3283                    (> unread 0)   ; We list groups with unread articles
3284                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3285                (gnus-group-insert-group-line 
3286                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3287
3288     ;; List dead groups.
3289     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3290          (gnus-group-prepare-flat-list-dead 
3291           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3292           gnus-level-zombie ?Z
3293           regexp))
3294     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3295          (gnus-group-prepare-flat-list-dead 
3296           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3297           gnus-level-killed ?K regexp))
3298
3299     (gnus-group-set-mode-line)
3300     (setq gnus-have-all-newsgroups all)
3301     (run-hooks 'gnus-group-prepare-hook)))
3302
3303 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3304   ;; List zombies and killed lists somehwat faster, which was
3305   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3306   ;; this by ignoring the group format specification altogether.
3307   (let (group beg)
3308     (while groups
3309       (setq group (car groups)
3310             groups (cdr groups))
3311       (if (or (not regexp)
3312               (string-match regexp group))
3313           (progn
3314             (setq beg (point))
3315             (insert (format " %c     *: %s\n" mark group))
3316             (add-text-properties 
3317              beg (1+ beg) 
3318              (list 'gnus-group (intern group)
3319                    'gnus-unread t
3320                    'gnus-level level)))))))
3321
3322 (defun gnus-group-real-name (group)
3323   "Find the real name of a foreign newsgroup."
3324   (if (string-match ":[^:]+$" group)
3325       (substring group (1+ (match-beginning 0)))
3326     group))
3327
3328 (defun gnus-group-prefixed-name (group method)
3329   "Return the whole name from GROUP and METHOD."
3330   (and (stringp method) (setq method (gnus-server-to-method method)))
3331   (concat (format "%s" (car method))
3332           (if (and 
3333                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3334                (not (string= (nth 1 method) "")))
3335               (concat "+" (nth 1 method)))
3336           ":" group))
3337
3338 (defun gnus-group-real-prefix (group)
3339   "Return the prefix of the current group name."
3340   (if (string-match "^[^:]+:" group)
3341       (substring group 0 (match-end 0))
3342     ""))
3343
3344 (defun gnus-group-method-name (group)
3345   "Return the method used for selecting GROUP."
3346   (let ((prefix (gnus-group-real-prefix group)))
3347     (if (equal prefix "")
3348         gnus-select-method
3349       (if (string-match "^[^\\+]+\\+" prefix)
3350           (list (intern (substring prefix 0 (1- (match-end 0))))
3351                 (substring prefix (match-end 0) (1- (length prefix))))
3352         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3353
3354 (defun gnus-group-foreign-p (group)
3355   "Return nil if GROUP is native, non-nil if it is foreign."
3356   (string-match ":" group))
3357
3358 (defun gnus-group-set-info (info &optional method-only-group part)
3359   (let* ((entry (gnus-gethash
3360                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3361          (part-info info)
3362          (info (if method-only-group (nth 2 entry) info)))
3363     (if (not method-only-group)
3364         ()
3365       (or entry
3366           (error "Trying to change non-existent group %s" method-only-group))
3367       ;; We have recevied parts of the actual group info - either the
3368       ;; select method or the group parameters.  We first check
3369       ;; whether we have to extend the info, and if so, do that.
3370       (let ((len (length info))
3371             (total (if (eq part 'method) 5 6)))
3372         (and (< len total)
3373              (setcdr (nthcdr (1- len) info)
3374                      (make-list (- total len) nil)))
3375         ;; Then we enter the new info.
3376         (setcar (nthcdr (1- total) info) part-info)))
3377     ;; We uncompress some lists of marked articles.
3378     (let (marked)
3379       (if (not (setq marked (nth 3 info)))
3380           ()
3381         (while marked
3382           (or (eq 'score (car (car marked)))
3383               (eq 'bookmark (car (car marked)))
3384               (eq 'killed (car (car marked)))
3385               (setcdr (car marked) 
3386                       (gnus-uncompress-range (cdr (car marked)))))
3387           (setq marked (cdr marked)))))
3388     (if entry
3389         ()
3390       ;; This is a new group, so we just create it.
3391       (save-excursion
3392         (set-buffer gnus-group-buffer)
3393         (if (nth 4 info)
3394             ;; It's a foreign group...
3395             (gnus-group-make-group 
3396              (gnus-group-real-name (car info))
3397              (prin1-to-string (car (nth 4 info)))
3398              (nth 1 (nth 4 info)))
3399           ;; It's a native group.
3400           (gnus-group-make-group
3401            (car info)
3402            (prin1-to-string (car gnus-select-method))
3403            (nth 1 gnus-select-method)))
3404         (gnus-message 6 "Note: New group created")
3405         (setq entry 
3406               (gnus-gethash (gnus-group-prefixed-name 
3407                              (gnus-group-real-name (car info))
3408                              (or (nth 4 info) gnus-select-method))
3409                             gnus-newsrc-hashtb))))
3410     ;; Whether it was a new group or not, we now have the entry, so we
3411     ;; can do the update.
3412     (if entry
3413         (progn
3414           (setcar (nthcdr 2 entry) info)
3415           (if (and (not (eq (car entry) t)) 
3416                    (gnus-gethash (car info) gnus-active-hashtb))
3417               (let ((marked (nth 3 info)))
3418                 (setcar entry 
3419                         (max 0 (- (length (gnus-list-of-unread-articles 
3420                                            (car info)))
3421                                   (length (cdr (assq 'tick marked)))
3422                                   (length (cdr (assq 'dormant marked)))))))))
3423       (error "No such group: %s" (car info)))))
3424
3425 (defun gnus-group-set-method-info (group select-method)
3426   (gnus-group-set-info select-method group 'method))
3427
3428 (defun gnus-group-set-params-info (group params)
3429   (gnus-group-set-info params group 'params))
3430
3431 (defun gnus-group-update-group-line ()
3432   "This function updates the current line in the newsgroup buffer and
3433 moves the point to the colon."
3434   (let* ((buffer-read-only nil)
3435          (group (gnus-group-group-name))
3436          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3437     (if entry
3438         (gnus-dribble-enter 
3439          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3440                  ")")))
3441     (beginning-of-line)
3442     (delete-region (point) (progn (forward-line 1) (point)))
3443     (gnus-group-insert-group-line-info group)
3444     (forward-line -1)
3445     (gnus-group-position-cursor)))
3446
3447 (defun gnus-group-insert-group-line-info (group)
3448   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3449         active info)
3450     (if entry
3451         (progn
3452           (setq info (nth 2 entry))
3453           (gnus-group-insert-group-line 
3454            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3455       (setq active (gnus-gethash group gnus-active-hashtb))
3456       (gnus-group-insert-group-line 
3457        nil group 
3458        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3459        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3460
3461 (defun gnus-group-insert-group-line (gformat group level marked number method)
3462   (let* ((gformat (or gformat gnus-group-line-format-spec))
3463          (active (gnus-gethash group gnus-active-hashtb))
3464          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3465          (number-of-dormant (length (cdr (assq 'dormant marked))))
3466          (number-of-ticked (length (cdr (assq 'tick marked))))
3467          (number-of-ticked-and-dormant
3468           (+ number-of-ticked number-of-dormant))
3469          (number-of-unread-unticked 
3470           (if (numberp number) (int-to-string (max 0 number))
3471             "*"))
3472          (number-of-read
3473           (if (numberp number)
3474               (max 0 (- number-total number))
3475             "*"))
3476          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3477                            ((<= level gnus-level-unsubscribed) ?U)
3478                            ((= level gnus-level-zombie) ?Z)
3479                            (t ?K)))
3480          (qualified-group (gnus-group-real-name group))
3481          (newsgroup-description 
3482           (if gnus-description-hashtb
3483               (or (gnus-gethash group gnus-description-hashtb) "")
3484             ""))
3485          (moderated (if (member group gnus-moderated-list) ?m ? ))
3486          (moderated-string (if (eq moderated ?m) "(m)" ""))
3487          (method (gnus-server-get-method group method))
3488          (news-server (or (car (cdr method)) ""))
3489          (news-method (or (car method) ""))
3490          (news-method-string 
3491           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3492          (marked (if (and 
3493                       (numberp number) 
3494                       (zerop number)
3495                       (> number-of-ticked 0))
3496                      ?* ? ))
3497          (number (if (eq number t) "*" (+ number number-of-dormant 
3498                                           number-of-ticked)))
3499          (process-marked (if (member qualified-group gnus-group-marked)
3500                              gnus-process-mark ? ))
3501          (buffer-read-only nil)
3502          header ; passed as parameter to user-funcs.
3503          b)
3504     (beginning-of-line)
3505     (setq b (point))
3506     ;; Insert the text.
3507     (insert (eval gformat))
3508
3509     (add-text-properties 
3510      b (1+ b) (list 'gnus-group (intern group)
3511                     'gnus-unread (if (numberp number)
3512                                      (string-to-int number-of-unread-unticked)
3513                                    t)
3514                     'gnus-marked marked
3515                     'gnus-level level))))
3516
3517 (defun gnus-group-update-group (group &optional visible-only)
3518   "Update newsgroup info of GROUP.
3519 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3520   (save-excursion
3521     (set-buffer gnus-group-buffer)
3522     (let ((buffer-read-only nil)
3523           visible)
3524       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3525         (if entry
3526             (gnus-dribble-enter 
3527              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3528                      ")"))))
3529       ;; Buffer may be narrowed.
3530       (save-restriction
3531         (widen)
3532         ;; Search a line to modify.  If the buffer is large, the search
3533         ;; takes long time.  In most cases, current point is on the line
3534         ;; we are looking for.  So, first of all, check current line. 
3535         (if (or (progn
3536                   (beginning-of-line)
3537                   (eq (get-text-property (point) 'gnus-group)
3538                       (intern group)))
3539                 (progn
3540                   (gnus-goto-char 
3541                    (text-property-any 
3542                     (point-min) (point-max) 'gnus-group (intern group)))))
3543             ;; GROUP is listed in current buffer. So, delete old line.
3544             (progn
3545               (setq visible t)
3546               (beginning-of-line)
3547               (delete-region (point) (progn (forward-line 1) (point))))
3548           ;; No such line in the buffer, find out where it's supposed to
3549           ;; go, and insert it there (or at the end of the buffer).
3550           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3551           (or visible-only
3552               (let ((entry 
3553                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3554                 (while (and entry
3555                             (car entry)
3556                             (not
3557                              (gnus-goto-char
3558                               (text-property-any
3559                                (point-min) (point-max) 
3560                                'gnus-group (intern (car (car entry)))))))
3561                   (setq entry (cdr entry)))
3562                 (or entry (goto-char (point-max)))))))
3563       (if (or visible (not visible-only))
3564           (gnus-group-insert-group-line-info group))
3565       (gnus-group-set-mode-line))))
3566
3567 (defun gnus-group-set-mode-line ()
3568   (if (memq 'group gnus-updated-mode-lines)
3569       (let* ((gformat (or gnus-group-mode-line-format-spec
3570                           (setq gnus-group-mode-line-format-spec
3571                                 (gnus-parse-format 
3572                                  gnus-group-mode-line-format 
3573                                  gnus-group-mode-line-format-alist))))
3574              (news-server (car (cdr gnus-select-method)))
3575              (news-method (car gnus-select-method))
3576              (max-len 60)
3577              (mode-string (eval gformat)))
3578         (setq mode-string (eval gformat))
3579         (if (> (length mode-string) max-len) 
3580             (setq mode-string (substring mode-string 0 (- max-len 4))))
3581         (setq mode-line-buffer-identification mode-string)
3582         (set-buffer-modified-p t))))
3583
3584 (defun gnus-group-group-name ()
3585   "Get the name of the newsgroup on the current line."
3586   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3587     (and group (symbol-name group))))
3588
3589 (defun gnus-group-group-level ()
3590   "Get the level of the newsgroup on the current line."
3591   (get-text-property (gnus-point-at-bol) 'gnus-level))
3592
3593 (defun gnus-group-group-unread ()
3594   "Get the number of unread articles of the newsgroup on the current line."
3595   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3596
3597 (defun gnus-group-search-forward (&optional backward all level first-too)
3598   "Find the next newsgroup with unread articles.
3599 If BACKWARD is non-nil, find the previous newsgroup instead.
3600 If ALL is non-nil, just find any newsgroup.
3601 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3602 group exists.
3603 If FIRST-TOO, the current line is also eligible as a target."
3604   (let ((way (if backward -1 1))
3605         (low gnus-level-killed)
3606         (beg (point))
3607         pos found lev)
3608     (if (and backward (progn (beginning-of-line)) (bobp))
3609         nil
3610       (or first-too (forward-line way))
3611       (while (and 
3612               (not (eobp))
3613               (not (setq 
3614                     found 
3615                     (and (or all
3616                              (and
3617                               (let ((unread 
3618                                      (get-text-property (point) 'gnus-unread)))
3619                                 (or (eq unread t) (and unread (> unread 0))))
3620                               (setq lev (get-text-property (point)
3621                                                            'gnus-level))
3622                               (<= lev gnus-level-subscribed)))
3623                          (or (not level)
3624                              (and (setq lev (get-text-property (point)
3625                                                                'gnus-level))
3626                                   (or (= lev level)
3627                                       (and (< lev low)
3628                                            (< level lev)
3629                                            (progn
3630                                              (setq low lev)
3631                                              (setq pos (point))
3632                                              nil))))))))
3633               (zerop (forward-line way)))))
3634     (if found 
3635         (progn (gnus-group-position-cursor) t)
3636       (goto-char (or pos beg))
3637       (and pos t))))
3638
3639 ;;; Gnus group mode commands
3640
3641 ;; Group marking.
3642
3643 (defun gnus-group-mark-group (n &optional unmark no-advance)
3644   "Mark the current group."
3645   (interactive "p")
3646   (let ((buffer-read-only nil)
3647         group)
3648     (while 
3649         (and (> n 0) 
3650              (setq group (gnus-group-group-name))
3651              (progn
3652                (beginning-of-line)
3653                (forward-char 2)
3654                (delete-char 1)
3655                (if unmark
3656                    (progn
3657                      (insert " ")
3658                      (setq gnus-group-marked (delete group gnus-group-marked)))
3659                  (insert "#")
3660                  (setq gnus-group-marked
3661                        (cons group (delete group gnus-group-marked))))
3662                t)
3663              (or no-advance (zerop (gnus-group-next-group 1))))
3664       (setq n (1- n)))
3665     (gnus-summary-position-cursor)
3666     n))
3667
3668 (defun gnus-group-unmark-group (n)
3669   "Remove the mark from the current group."
3670   (interactive "p")
3671   (gnus-group-mark-group n 'unmark))
3672
3673 (defun gnus-group-mark-region (unmark beg end)
3674   "Mark all groups between point and mark.
3675 If UNMARK, remove the mark instead."
3676   (interactive "P\nr")
3677   (let ((num (count-lines beg end)))
3678     (save-excursion
3679       (goto-char beg)
3680       (- num (gnus-group-mark-group num unmark)))))
3681
3682 (defun gnus-group-remove-mark (group)
3683   (and (gnus-group-goto-group group)
3684        (save-excursion
3685          (gnus-group-mark-group 1 'unmark t))))
3686
3687 ;; Return a list of groups to work on.  Take into consideration N (the
3688 ;; prefix) and the list of marked groups.
3689 (defun gnus-group-process-prefix (n)
3690   (cond (n
3691          (setq n (prefix-numeric-value n))
3692          ;; There is a prefix, so we return a list of the N next
3693          ;; groups. 
3694          (let ((way (if (< n 0) -1 1))
3695                (n (abs n))
3696                group groups)
3697            (save-excursion
3698              (while (and (> n 0)
3699                          (setq group (gnus-group-group-name)))
3700                (setq groups (cons group groups))
3701                (setq n (1- n))
3702                (forward-line way)))
3703            (nreverse groups)))
3704         (gnus-group-marked
3705          ;; No prefix, but a list of marked articles.
3706          (reverse gnus-group-marked))
3707         (t
3708          ;; Neither marked articles or a prefix, so we return the
3709          ;; current group.
3710          (let ((group (gnus-group-group-name)))
3711            (and group (list group))))))
3712
3713 ;; Selecting groups.
3714
3715 (defun gnus-group-read-group (all &optional no-article group)
3716   "Read news in this newsgroup.
3717 If argument ALL is non-nil, already read articles become readable.
3718 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3719   (interactive "P")
3720   (let ((group (or group (gnus-group-group-name)))
3721         number active marked entry)
3722     (or group (error "No group on current line"))
3723     (setq marked 
3724           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3725     ;; This group might be a dead group. In that case we have to get
3726     ;; the number of unread articles from `gnus-active-hashtb'.
3727     (if entry
3728         (setq number (car entry))
3729       (if (setq active (gnus-gethash group gnus-active-hashtb))
3730           (setq number (- (1+ (cdr active)) (car active)))))
3731     (gnus-summary-read-group 
3732      group (or all (and (numberp number) 
3733                         (zerop (+ number (length (cdr (assq 'tick marked)))
3734                                   (length (cdr (assq 'dormant marked)))))))
3735      no-article)))
3736
3737 (defun gnus-group-select-group (all)
3738   "Select this newsgroup.
3739 No article is selected automatically.
3740 If argument ALL is non-nil, already read articles become readable."
3741   (interactive "P")
3742   (gnus-group-read-group all t))
3743
3744 ;; Enter a group that is not in the group buffer. Non-nil is returned
3745 ;; if selection was successful.
3746 (defun gnus-group-read-ephemeral-group 
3747   (group method &optional activate quit-config)
3748   (let ((group (if (gnus-group-foreign-p group) group
3749                  (gnus-group-prefixed-name group method))))
3750     (gnus-sethash 
3751      group
3752      (list t nil (list group gnus-level-default-subscribed nil nil 
3753                        (append method
3754                                (list
3755                                 (list 'quit-config 
3756                                       (if quit-config quit-config
3757                                         (cons (current-buffer) 'summary)))))))
3758      gnus-newsrc-hashtb)
3759     (set-buffer gnus-group-buffer)
3760     (or (gnus-server-opened method)
3761         (gnus-open-server method)
3762         (error "Unable to contact server: %s" (gnus-status-message method)))
3763     (if activate (or (gnus-request-group group)
3764                      (error "Couldn't request group")))
3765     (condition-case ()
3766         (gnus-group-read-group t t group)
3767       (error nil)
3768       (quit nil))
3769     (not (equal major-mode 'gnus-group-mode))))
3770   
3771 (defun gnus-group-jump-to-group (group)
3772   "Jump to newsgroup GROUP."
3773   (interactive 
3774    (list (completing-read 
3775           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3776
3777   (if (equal group "")
3778       (error "Empty group name"))
3779
3780   (let ((b (text-property-any 
3781             (point-min) (point-max) 'gnus-group (intern group))))
3782     (if b
3783         ;; Either go to the line in the group buffer...
3784         (goto-char b)
3785       ;; ... or insert the line.
3786       (or
3787        (gnus-gethash group gnus-active-hashtb)
3788        (gnus-activate-newsgroup group)
3789        (error "%s error: %s" group (gnus-status-message group)))
3790
3791       (gnus-group-update-group group)
3792       (goto-char (text-property-any 
3793                   (point-min) (point-max) 'gnus-group (intern group)))))
3794   ;; Adjust cursor point.
3795   (gnus-group-position-cursor))
3796
3797 (defun gnus-group-goto-group (group)
3798   "Goto to newsgroup GROUP."
3799   (let ((b (text-property-any (point-min) (point-max) 
3800                               'gnus-group (intern group))))
3801     (and b (goto-char b))))
3802
3803 (defun gnus-group-next-group (n)
3804   "Go to next N'th newsgroup.
3805 If N is negative, search backward instead.
3806 Returns the difference between N and the number of skips actually
3807 done."
3808   (interactive "p")
3809   (gnus-group-next-unread-group n t))
3810
3811 (defun gnus-group-next-unread-group (n &optional all level)
3812   "Go to next N'th unread newsgroup.
3813 If N is negative, search backward instead.
3814 If ALL is non-nil, choose any newsgroup, unread or not.
3815 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3816 such group can be found, the next group with a level higher than
3817 LEVEL.
3818 Returns the difference between N and the number of skips actually
3819 made."
3820   (interactive "p")
3821   (let ((backward (< n 0))
3822         (n (abs n)))
3823     (while (and (> n 0)
3824                 (gnus-group-search-forward 
3825                  backward (or (not gnus-group-goto-unread) all) level))
3826       (setq n (1- n)))
3827     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3828                                (if level " on this level or higher" "")))
3829     n))
3830
3831 (defun gnus-group-prev-group (n)
3832   "Go to previous N'th newsgroup.
3833 Returns the difference between N and the number of skips actually
3834 done."
3835   (interactive "p")
3836   (gnus-group-next-unread-group (- n) t))
3837
3838 (defun gnus-group-prev-unread-group (n)
3839   "Go to previous N'th unread newsgroup.
3840 Returns the difference between N and the number of skips actually
3841 done."  
3842   (interactive "p")
3843   (gnus-group-next-unread-group (- n)))
3844
3845 (defun gnus-group-next-unread-group-same-level (n)
3846   "Go to next N'th unread newsgroup on the same level.
3847 If N is negative, search backward instead.
3848 Returns the difference between N and the number of skips actually
3849 done."
3850   (interactive "p")
3851   (gnus-group-next-unread-group n t (gnus-group-group-level))
3852   (gnus-group-position-cursor))
3853
3854 (defun gnus-group-prev-unread-group-same-level (n)
3855   "Go to next N'th unread newsgroup on the same level.
3856 Returns the difference between N and the number of skips actually
3857 done."
3858   (interactive "p")
3859   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3860   (gnus-group-position-cursor))
3861
3862 (defun gnus-group-best-unread-group (&optional exclude-group)
3863   "Go to the group with the highest level.
3864 If EXCLUDE-GROUP, do not go to that group."
3865   (interactive)
3866   (goto-char (point-min))
3867   (let ((best 100000)
3868         unread best-point)
3869     (while (setq unread (get-text-property (point) 'gnus-unread))
3870       (if (and (numberp unread) (> unread 0))
3871           (progn
3872             (if (and (< (get-text-property (point) 'gnus-level) best)
3873                      (or (not exclude-group)
3874                          (not (equal exclude-group (gnus-group-group-name)))))
3875                 (progn 
3876                   (setq best (get-text-property (point) 'gnus-level))
3877                   (setq best-point (point))))))
3878       (forward-line 1))
3879     (if best-point (goto-char best-point))
3880     (gnus-summary-position-cursor)
3881     (and best-point (gnus-group-group-name))))
3882
3883 (defun gnus-group-first-unread-group ()
3884   "Go to the first group with unread articles."
3885   (interactive)
3886   (goto-char (point-min))
3887   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3888       (gnus-group-next-unread-group 1))
3889   (gnus-group-position-cursor))
3890
3891 (defun gnus-group-enter-server-mode ()
3892   "Jump to the server buffer."
3893   (interactive)
3894   (gnus-server-setup-buffer)
3895   (gnus-configure-windows 'server)
3896   (gnus-server-prepare))
3897
3898 (defun gnus-group-make-group (name method &optional address)
3899   "Add a new newsgroup.
3900 The user will be prompted for a NAME, for a select METHOD, and an
3901 ADDRESS."
3902   (interactive
3903    (cons 
3904     (read-string "Group name: ")
3905     (let ((method
3906            (completing-read 
3907             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3908             nil t)))
3909       (if (assoc method gnus-valid-select-methods)
3910           (list method
3911                 (if (memq 'prompt-address
3912                           (assoc method gnus-valid-select-methods))
3913                     (read-string "Address: ")
3914                   ""))
3915         (list method nil)))))
3916   
3917   (let* ((meth (if address (list (intern method) address) method))
3918          (nname (gnus-group-prefixed-name name meth))
3919          info)
3920     (and (gnus-gethash nname gnus-newsrc-hashtb)
3921          (error "Group %s already exists" nname))
3922     (gnus-group-change-level 
3923      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3924      gnus-level-default-subscribed gnus-level-killed 
3925      (and (gnus-group-group-name)
3926           (gnus-gethash (gnus-group-group-name)
3927                         gnus-newsrc-hashtb))
3928      t)
3929     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3930     (gnus-dribble-enter 
3931      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3932     (gnus-group-insert-group-line-info nname)
3933
3934     (if (assoc method gnus-valid-select-methods)
3935         (require (intern method)))
3936     (and (gnus-check-backend-function 'request-create-group nname)
3937          (gnus-request-create-group nname))))
3938
3939 (defun gnus-group-edit-group (group &optional part)
3940   "Edit the group on the current line."
3941   (interactive (list (gnus-group-group-name)))
3942   (let ((done-func '(lambda () 
3943                       "Exit editing mode and update the information."
3944                       (interactive)
3945                       (gnus-group-edit-group-done 'part 'group)))
3946         (part (or part 'info))
3947         (winconf (current-window-configuration))
3948         info)
3949     (or group (error "No group on current line"))
3950     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3951         (error "Killed group; can't be edited"))
3952     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3953     (gnus-configure-windows 'edit-group)
3954     (gnus-add-current-to-buffer-list)
3955     (emacs-lisp-mode)
3956     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3957     (use-local-map (copy-keymap emacs-lisp-mode-map))
3958     (local-set-key "\C-c\C-c" done-func)
3959     (make-local-variable 'gnus-prev-winconf)
3960     (setq gnus-prev-winconf winconf)
3961     ;; We modify the func to let it know what part it is editing.
3962     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3963     (setcar (cdr (cdr (nth 4 done-func))) group)
3964     (erase-buffer)
3965     (insert
3966      (cond 
3967       ((eq part 'method)
3968        ";; Type `C-c C-c' after editing the select method.\n\n")
3969       ((eq part 'params)
3970        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3971       ((eq part 'info)
3972        ";; Type `C-c C-c' after editing the group info.\n\n")))
3973     (let ((cinfo (gnus-copy-sequence info))
3974           marked)
3975       (if (not (setq marked (nth 3 cinfo)))
3976           ()
3977         (while marked
3978           (or (eq 'score (car (car marked)))
3979               (eq 'bookmark (car (car marked)))
3980               (eq 'killed (car (car marked)))
3981               (not (numberp (car (cdr (car marked)))))
3982               (setcdr (car marked) 
3983                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3984           (setq marked (cdr marked))))
3985       (insert 
3986        (pp-to-string
3987         (cond ((eq part 'method)
3988                (or (nth 4 info) "native"))
3989               ((eq part 'params)
3990                (nth 5 info))
3991               (t
3992                cinfo)))
3993        "\n"))))
3994
3995 (defun gnus-group-edit-group-method (group)
3996   "Edit the select method of GROUP."
3997   (interactive (list (gnus-group-group-name)))
3998   (gnus-group-edit-group group 'method))
3999
4000 (defun gnus-group-edit-group-parameters (group)
4001   "Edit the group parameters of GROUP."
4002   (interactive (list (gnus-group-group-name)))
4003   (gnus-group-edit-group group 'params))
4004
4005 (defun gnus-group-edit-group-done (part group)
4006   "Get info from buffer, update variables and jump to the group buffer."
4007   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4008   (goto-char (point-min))
4009   (let ((form (read (current-buffer)))
4010         (winconf gnus-prev-winconf))
4011     (if (eq part 'info) 
4012         (gnus-group-set-info form)
4013       (gnus-group-set-info form group part))
4014     (kill-buffer (current-buffer))
4015     (and winconf (set-window-configuration winconf))
4016     (set-buffer gnus-group-buffer)
4017     (gnus-group-update-group (gnus-group-group-name))
4018     (gnus-group-position-cursor)))
4019
4020 (defun gnus-group-make-help-group ()
4021   "Create the (ding) Gnus documentation group."
4022   (interactive)
4023   (let ((path load-path)
4024         name)
4025     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4026                                    "gnus-help" '(nndoc "gnus-help")))
4027                        gnus-newsrc-hashtb)
4028          (error "Documentation group already exists"))
4029     (while (and path
4030                 (not (file-exists-p (concat (file-name-as-directory (car path))
4031                                             "doc.txt"))))
4032       (setq path (cdr path)))
4033     (or path (error "Couldn't find doc group"))
4034     (gnus-group-make-group 
4035      (gnus-group-real-name name)
4036      (list 'nndoc name
4037            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4038            (list 'nndoc-article-type 'mbox))))
4039   (gnus-group-position-cursor))
4040
4041 (defun gnus-group-make-doc-group (file type)
4042   "Create a group that uses a single file as the source."
4043   (interactive 
4044    (list (read-file-name "File name: ") 
4045          (let ((err "")
4046                found char)
4047            (while (not found)
4048              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4049              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4050                                ((= char ?b) 'babyl)
4051                                ((= char ?d) 'digest)
4052                                (t (setq err "%c unknown. " char)
4053                                   nil))))
4054            found)))
4055   (let* ((file (expand-file-name file))
4056          (name (gnus-generate-new-group-name
4057                 (gnus-group-prefixed-name
4058                  (file-name-nondirectory file) '(nndoc "")))))
4059     (gnus-group-make-group 
4060      (gnus-group-real-name name)
4061      (list 'nndoc name
4062            (list 'nndoc-address file)
4063            (list 'nndoc-article-type type)))))
4064
4065 (defun gnus-group-make-archive-group ()
4066   "Create the (ding) Gnus archive group."
4067   (interactive)
4068   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4069                      gnus-newsrc-hashtb)
4070        (error "Archive group already exists"))
4071   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4072   (gnus-group-position-cursor))
4073
4074 (defun gnus-group-make-directory-group (dir)
4075   "Create an nndir group.
4076 The user will be prompted for a directory. The contents of this
4077 directory will be used as a newsgroup. The directory should contain
4078 mail messages or news articles in files that have numeric names."
4079   (interactive
4080    (list (read-file-name "Create group from directory: ")))
4081   (or (file-exists-p dir) (error "No such directory"))
4082   (or (file-directory-p dir) (error "Not a directory"))
4083   (gnus-group-make-group dir "nndir" dir)
4084   (gnus-group-position-cursor))
4085
4086 (defun gnus-group-make-kiboze-group (group address scores)
4087   "Create an nnkiboze group.
4088 The user will be prompted for a name, a regexp to match groups, and
4089 score file entries for articles to include in the group."
4090   (interactive
4091    (list
4092     (read-string "nnkiboze group name: ")
4093     (read-string "Source groups (regexp): ")
4094     (let ((headers (mapcar (lambda (group) (list group))
4095                            '("subject" "from" "number" "date" "message-id"
4096                              "references" "chars" "lines" "xref")))
4097           scores header regexp regexps)
4098       (while (not (equal "" (setq header (completing-read 
4099                                           "Match on header: " headers nil t))))
4100         (setq regexps nil)
4101         (while (not (equal "" (setq regexp (read-string 
4102                                             (format "Match on %s (string): "
4103                                                     header)))))
4104           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4105         (setq scores (cons (cons header regexps) scores)))
4106       scores)))
4107   (gnus-group-make-group group "nnkiboze" address)
4108   (save-excursion
4109     (gnus-set-work-buffer)
4110     (let (emacs-lisp-mode-hook)
4111       (pp scores (current-buffer)))
4112     (write-region (point-min) (point-max) 
4113                   (concat (or gnus-kill-files-directory "~/News")
4114                           "nnkiboze:" group "." gnus-score-file-suffix)))
4115   (gnus-group-position-cursor))
4116
4117 (defun gnus-group-add-to-virtual (n vgroup)
4118   "Add the current group to a virtual group."
4119   (interactive
4120    (list current-prefix-arg
4121          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4122                           "nnvirtual:")))
4123   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4124       (error "%s is not an nnvirtual group" vgroup))
4125   (let* ((groups (gnus-group-process-prefix n))
4126          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4127     (setcar (cdr method)
4128             (concat 
4129              (nth 1 method) "\\|"
4130              (mapconcat 
4131               (lambda (s) 
4132                 (gnus-group-remove-mark s)
4133                 (concat "\\(^" (regexp-quote s) "$\\)"))
4134               groups "\\|"))))
4135   (gnus-group-position-cursor))
4136
4137 (defun gnus-group-make-empty-virtual (group)
4138   "Create a new, fresh, empty virtual group."
4139   (interactive "sCreate new, empty virtual group: ")
4140   (let* ((method (list 'nnvirtual "^$"))
4141          (pgroup (gnus-group-prefixed-name group method)))
4142     ;; Check whether it exists already.
4143     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4144          (error "Group %s already exists." pgroup))
4145     ;; Subscribe the new group after the group on the current line.
4146     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4147     (gnus-group-update-group pgroup)
4148     (forward-line -1)
4149     (gnus-group-position-cursor)))
4150
4151 (defun gnus-group-enter-directory (dir)
4152   "Enter an ephemeral nneething group."
4153   (interactive "DDirectory to read: ")
4154   (let* ((method (list 'nneething dir))
4155          (leaf (gnus-group-prefixed-name
4156                 (file-name-nondirectory (directory-file-name dir))
4157                 method))
4158          (name (gnus-generate-new-group-name leaf)))
4159     (let ((nneething-read-only t))
4160       (or (gnus-group-read-ephemeral-group 
4161            name method t
4162            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4163                                       'summary 'group)))
4164           (error "Couldn't enter %s" dir)))))
4165
4166 ;; Group sorting commands
4167 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4168
4169 (defun gnus-group-sort-groups ()
4170   "Sort the group buffer using `gnus-group-sort-function'."
4171   (interactive)
4172   (setq gnus-newsrc-alist 
4173         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4174   (gnus-make-hashtable-from-newsrc-alist)
4175   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4176
4177 (defun gnus-group-sort-by-alphabet (info1 info2)
4178   (string< (car info1) (car info2)))
4179
4180 (defun gnus-group-sort-by-unread (info1 info2)
4181   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4182         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4183     (< (or (and (numberp n1) n1) 0)
4184        (or (and (numberp n2) n2) 0))))
4185
4186 (defun gnus-group-sort-by-level (info1 info2)
4187   (< (nth 1 info1) (nth 1 info2)))
4188
4189 ;; Group catching up.
4190
4191 (defun gnus-group-catchup-current (n &optional all)
4192   "Mark all articles not marked as unread in current newsgroup as read.
4193 If prefix argument N is numeric, the ARG next newsgroups will be
4194 caught up. If ALL is non-nil, marked articles will also be marked as
4195 read. Cross references (Xref: header) of articles are ignored.
4196 The difference between N and actual number of newsgroups that were
4197 caught up is returned."
4198   (interactive "P")
4199   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4200                gnus-expert-user
4201                (gnus-y-or-n-p
4202                 (if all
4203                     "Do you really want to mark all articles as read? "
4204                   "Mark all unread articles as read? "))))
4205       n
4206     (let ((groups (gnus-group-process-prefix n))
4207           (ret 0))
4208       (while groups
4209         ;; Virtual groups have to be given special treatment. 
4210         (let ((method (gnus-find-method-for-group (car groups))))
4211           (if (eq 'nnvirtual (car method))
4212               (nnvirtual-catchup-group
4213                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4214         (gnus-group-remove-mark (car groups))
4215         (if (prog1
4216                 (gnus-group-goto-group (car groups))
4217               (gnus-group-catchup (car groups) all))
4218             (gnus-group-update-group-line)
4219           (setq ret (1+ ret)))
4220         (setq groups (cdr groups)))
4221       (gnus-group-next-unread-group 1)
4222       ret)))
4223
4224 (defun gnus-group-catchup-current-all (n)
4225   "Mark all articles in current newsgroup as read.
4226 Cross references (Xref: header) of articles are ignored."
4227   (interactive "P")
4228   (gnus-group-catchup-current n 'all))
4229
4230 (defun gnus-group-catchup (group &optional all)
4231   "Mark all articles in GROUP as read.
4232 If ALL is non-nil, all articles are marked as read.
4233 The return value is the number of articles that were marked as read,
4234 or nil if no action could be taken."
4235   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4236          (num (car entry))
4237          (marked (nth 3 (nth 2 entry))))
4238     (if (not (numberp (car entry)))
4239         (gnus-message 1 "Can't catch up; non-active group")
4240       ;; Do the updating only if the newsgroup isn't killed.
4241       (if (not entry)
4242           ()
4243         (gnus-update-read-articles 
4244          group (and (not all) (append (cdr (assq 'tick marked))
4245                                       (cdr (assq 'dormant marked))))
4246          nil (and (not all) (cdr (assq 'tick marked))))
4247         (and all marked
4248              (setcar (nthcdr 3 (nth 2 entry)) 
4249                      (delq (assq 'dormant marked) 
4250                            (nth 3 (nth 2 entry)))))))
4251     num))
4252
4253 (defun gnus-group-expire-articles (n)
4254   "Expire all expirable articles in the current newsgroup."
4255   (interactive "P")
4256   (let ((groups (gnus-group-process-prefix n))
4257         group)
4258     (or groups (error "No groups to expire"))
4259     (while groups
4260       (setq group (car groups)
4261             groups (cdr groups))
4262       (gnus-group-remove-mark group)
4263       (if (not (gnus-check-backend-function 'request-expire-articles group))
4264           ()
4265         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4266                (expirable (if (memq 'total-expire (nth 5 info))
4267                               (cons nil (gnus-list-of-read-articles group))
4268                             (assq 'expire (nth 3 info)))))
4269           (and expirable 
4270                (setcdr expirable
4271                        (gnus-request-expire-articles 
4272                         (cdr expirable) group))))))))
4273
4274 (defun gnus-group-expire-all-groups ()
4275   "Expire all expirable articles in all newsgroups."
4276   (interactive)
4277   (save-excursion
4278     (gnus-message 5 "Expiring...")
4279     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4280                                      (cdr gnus-newsrc-alist))))
4281       (gnus-group-expire-articles nil)))
4282   (gnus-group-position-cursor)
4283   (gnus-message 5 "Expiring...done"))
4284
4285 (defun gnus-group-set-current-level (n level)
4286   "Set the level of the next N groups to LEVEL."
4287   (interactive "P\nnLevel: ")
4288   (or (and (>= level 1) (<= level gnus-level-killed))
4289       (error "Illegal level: %d" level))
4290   (let ((groups (gnus-group-process-prefix n))
4291         group)
4292     (while groups
4293       (setq group (car groups)
4294             groups (cdr groups))
4295       (gnus-group-remove-mark group)
4296       (gnus-message 6 "Changed level of %s from %d to %d" 
4297                     group (gnus-group-group-level) level)
4298       (gnus-group-change-level group level
4299                                (gnus-group-group-level))
4300       (gnus-group-update-group-line)))
4301   (gnus-group-position-cursor))
4302
4303 (defun gnus-group-unsubscribe-current-group (n)
4304   "Toggle subscription of the current group.
4305 If given numerical prefix, toggle the N next groups."
4306   (interactive "P")
4307   (let ((groups (gnus-group-process-prefix n))
4308         group)
4309     (while groups
4310       (setq group (car groups)
4311             groups (cdr groups))
4312       (gnus-group-remove-mark group)
4313       (gnus-group-unsubscribe-group
4314        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4315                  gnus-level-default-unsubscribed
4316                gnus-level-default-subscribed))
4317       (gnus-group-update-group-line))
4318     (gnus-group-next-group 1)))
4319
4320 (defun gnus-group-unsubscribe-group (group &optional level)
4321   "Toggle subscribe from/to unsubscribe GROUP.
4322 New newsgroup is added to .newsrc automatically."
4323   (interactive
4324    (list (completing-read "Group: " gnus-active-hashtb nil 
4325                           gnus-have-read-active-file)))
4326   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4327     (cond (newsrc
4328            ;; Toggle subscription flag.
4329            (gnus-group-change-level 
4330             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4331                                            gnus-level-subscribed) 
4332                                        (1+ gnus-level-subscribed)
4333                                      gnus-level-default-subscribed)))
4334            (gnus-group-update-group group))
4335           ((and (stringp group)
4336                 (or (not gnus-have-read-active-file)
4337                     (gnus-gethash group gnus-active-hashtb)))
4338            ;; Add new newsgroup.
4339            (gnus-group-change-level 
4340             group 
4341             (if level level gnus-level-default-subscribed) 
4342             (or (and (member group gnus-zombie-list) 
4343                      gnus-level-zombie) 
4344                 gnus-level-killed)
4345             (and (gnus-group-group-name)
4346                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4347            (gnus-group-update-group group))
4348           (t (error "No such newsgroup: %s" group)))
4349     (gnus-group-position-cursor)))
4350
4351 (defun gnus-group-transpose-groups (n)
4352   "Move the current newsgroup up N places.
4353 If given a negative prefix, move down instead. The difference between
4354 N and the number of steps taken is returned." 
4355   (interactive "p")
4356   (or (gnus-group-group-name)
4357       (error "No group on current line"))
4358   (gnus-group-kill-group 1)
4359   (prog1
4360       (forward-line (- n))
4361     (gnus-group-yank-group)
4362     (gnus-group-position-cursor)))
4363
4364 (defun gnus-group-kill-all-zombies ()
4365   "Kill all zombie newsgroups."
4366   (interactive)
4367   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4368   (setq gnus-zombie-list nil)
4369   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4370   (goto-char (point-min))
4371   (gnus-group-position-cursor))
4372
4373 (defun gnus-group-kill-region (begin end)
4374   "Kill newsgroups in current region (excluding current point).
4375 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4376   (interactive "r")
4377   (let ((lines
4378          ;; Count lines.
4379          (save-excursion
4380            (count-lines
4381             (progn
4382               (goto-char begin)
4383               (beginning-of-line)
4384               (point))
4385             (progn
4386               (goto-char end)
4387               (beginning-of-line)
4388               (point))))))
4389     (goto-char begin)
4390     (beginning-of-line)                 ;Important when LINES < 1
4391     (gnus-group-kill-group lines)))
4392
4393 (defun gnus-group-kill-group (n)
4394   "The the next N groups.
4395 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4396 However, only groups that were alive can be yanked; already killed 
4397 groups or zombie groups can't be yanked.
4398 The return value is the name of the (last) group that was killed."
4399   (interactive "P")
4400   (let ((buffer-read-only nil)
4401         (groups (gnus-group-process-prefix n))
4402         group entry level)
4403     (while groups
4404       (setq group (car groups)
4405             groups (cdr groups))
4406       (gnus-group-remove-mark group)
4407       (setq level (gnus-group-group-level))
4408       (gnus-delete-line)
4409       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4410           (setq gnus-list-of-killed-groups 
4411                 (cons (cons (car entry) (nth 2 entry)) 
4412                       gnus-list-of-killed-groups)))
4413       (gnus-group-change-level 
4414        (if entry entry group) gnus-level-killed (if entry nil level)))
4415     (gnus-group-position-cursor)
4416     group))
4417
4418 (defun gnus-group-yank-group (&optional arg)
4419   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4420 inserting it before the current newsgroup.  The numeric ARG specifies
4421 how many newsgroups are to be yanked.  The name of the (last)
4422 newsgroup yanked is returned."
4423   (interactive "p")
4424   (if (not arg) (setq arg 1))
4425   (let (info group prev)
4426     (while (>= (setq arg (1- arg)) 0)
4427       (if (not (setq info (car gnus-list-of-killed-groups)))
4428           (error "No more newsgroups to yank"))
4429       (setq group (nth 2 info))
4430       ;; Find which newsgroup to insert this one before - search
4431       ;; backward until something suitable is found. If there are no
4432       ;; other newsgroups in this buffer, just make this newsgroup the
4433       ;; first newsgroup.
4434       (setq prev (gnus-group-group-name))
4435       (gnus-group-change-level 
4436        info (nth 2 info) gnus-level-killed 
4437        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4438        t)
4439       (gnus-group-insert-group-line-info (nth 1 info))
4440       (setq gnus-list-of-killed-groups 
4441             (cdr gnus-list-of-killed-groups)))
4442     (forward-line -1)
4443     (gnus-group-position-cursor)
4444     group))
4445       
4446 (defun gnus-group-list-all-groups (arg)
4447   "List all newsgroups with level ARG or lower.
4448 Default is gnus-level-unsubscribed, which lists all subscribed and most
4449 unsubscribed groups."
4450   (interactive "P")
4451   (setq arg (or arg gnus-level-unsubscribed))
4452   (gnus-group-list-groups arg t))
4453
4454 (defun gnus-group-list-killed ()
4455   "List all killed newsgroups in the group buffer."
4456   (interactive)
4457   (if (not gnus-killed-list)
4458       (gnus-message 6 "No killed groups")
4459     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4460     (goto-char (point-min)))
4461   (gnus-group-position-cursor))
4462
4463 (defun gnus-group-list-zombies ()
4464   "List all zombie newsgroups in the group buffer."
4465   (interactive)
4466   (if (not gnus-zombie-list)
4467       (gnus-message 6 "No zombie groups")
4468     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4469     (goto-char (point-min)))
4470   (gnus-group-position-cursor))
4471
4472 (defun gnus-group-get-new-news (&optional arg)
4473   "Get newly arrived articles.
4474 If ARG is non-nil, it should be a number between one and nine to
4475 specify which levels you are interested in re-scanning."
4476   (interactive "P")
4477   (run-hooks 'gnus-get-new-news-hook)
4478   (if gnus-group-use-permanent-levels
4479       (setq arg
4480             (setq gnus-group-default-list-level 
4481                   (or arg gnus-group-default-list-level
4482                       gnus-level-subscribed))))
4483   (if (and gnus-read-active-file (not arg))
4484       (progn
4485         (gnus-read-active-file)
4486         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4487     (let ((gnus-read-active-file nil)
4488           (gnus-have-read-active-file (not arg)))
4489       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4490   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4491                               gnus-group-default-list-level
4492                               gnus-level-subscribed)
4493                           gnus-have-all-newsgroups))
4494
4495 (defun gnus-group-get-new-news-this-group (n)
4496   "Check for newly arrived news in the current group (and the N-1 next groups).
4497 The difference between N and the number of newsgroup checked is returned.
4498 If N is negative, this group and the N-1 previous groups will be checked."
4499   (interactive "P")
4500   (let* ((groups (gnus-group-process-prefix n))
4501          (ret (if (numberp n) (- n (length groups)) 0))
4502          group)
4503     (while groups
4504       (setq group (car groups)
4505             groups (cdr groups))
4506       (gnus-group-remove-mark group)
4507       (or (gnus-get-new-news-in-group group)
4508           (progn 
4509             (ding) 
4510             (message "%s error: %s" group (gnus-status-message group))
4511             (sit-for 2))))
4512     ;; !!! I don't know why the buffer scrolls forward when updating
4513     ;; the first line in the group buffer, but it does. So we set the
4514     ;; window start forcibly.
4515 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4516     (gnus-group-next-unread-group 1 t)
4517     (gnus-summary-position-cursor)
4518     ret))
4519
4520 (defun gnus-get-new-news-in-group (group)
4521   (and group 
4522        (gnus-activate-newsgroup group)
4523        (progn
4524          (gnus-get-unread-articles-in-group 
4525           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4526           (gnus-gethash group gnus-active-hashtb))
4527          (gnus-group-update-group-line)
4528          t)))
4529
4530 (defun gnus-group-fetch-faq (group)
4531   "Fetch the FAQ for the current group."
4532   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4533   (or group (error "No group name given"))
4534   (let ((file (concat gnus-group-faq-directory group))) 
4535     (if (not (file-exists-p file))
4536         (error "No such file: %s" file)
4537       (find-file file))))
4538   
4539 (defun gnus-group-describe-group (force &optional group)
4540   "Display a description of the current newsgroup."
4541   (interactive (list current-prefix-arg (gnus-group-group-name)))
4542   (and force (setq gnus-description-hashtb nil))
4543   (let ((method (gnus-find-method-for-group group))
4544         desc)
4545     (or group (error "No group name given"))
4546     (and (or (and gnus-description-hashtb
4547                   ;; We check whether this group's method has been
4548                   ;; queried for a description file.  
4549                   (gnus-gethash 
4550                    (gnus-group-prefixed-name "" method) 
4551                    gnus-description-hashtb))
4552              (setq desc (gnus-group-get-description group))
4553              (gnus-read-descriptions-file method))
4554          (message
4555           (or desc (gnus-gethash group gnus-description-hashtb)
4556               "No description available")))))
4557
4558 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4559 (defun gnus-group-describe-all-groups (force)
4560   "Pop up a buffer with descriptions of all newsgroups."
4561   (interactive "P")
4562   (and force (setq gnus-description-hashtb nil))
4563   (if (not (or gnus-description-hashtb
4564                (gnus-read-all-descriptions-files)))
4565       (error "Couldn't request descriptions file"))
4566   (let ((buffer-read-only nil)
4567         b)
4568     (erase-buffer)
4569     (mapatoms
4570      (lambda (group)
4571        (setq b (point))
4572        (insert (format "      *: %-20s %s\n" (symbol-name group)
4573                        (symbol-value group)))
4574        (add-text-properties 
4575         b (1+ b) (list 'gnus-group group
4576                        'gnus-unread t 'gnus-marked nil
4577                        'gnus-level (1+ gnus-level-subscribed))))
4578      gnus-description-hashtb)
4579     (goto-char (point-min))
4580     (gnus-group-position-cursor)))
4581
4582 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4583 (defun gnus-group-apropos (regexp &optional search-description)
4584   "List all newsgroups that have names that match a regexp."
4585   (interactive "sGnus apropos (regexp): ")
4586   (let ((prev "")
4587         (obuf (current-buffer))
4588         groups des)
4589     ;; Go through all newsgroups that are known to Gnus.
4590     (mapatoms 
4591      (lambda (group)
4592        (and (string-match regexp (symbol-name group))
4593             (setq groups (cons (symbol-name group) groups))))
4594      gnus-active-hashtb)
4595     ;; Go through all descriptions that are known to Gnus. 
4596     (if search-description
4597         (mapatoms 
4598          (lambda (group)
4599            (and (string-match regexp (symbol-value group))
4600                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4601                 (setq groups (cons (symbol-name group) groups))))
4602          gnus-description-hashtb))
4603     (if (not groups)
4604         (gnus-message 3 "No groups matched \"%s\"." regexp)
4605       ;; Print out all the groups.
4606       (save-excursion
4607         (pop-to-buffer "*Gnus Help*")
4608         (buffer-disable-undo (current-buffer))
4609         (erase-buffer)
4610         (setq groups (sort groups 'string<))
4611         (while groups
4612           ;; Groups may be entered twice into the list of groups.
4613           (if (not (string= (car groups) prev))
4614               (progn
4615                 (insert (setq prev (car groups)) "\n")
4616                 (if (and gnus-description-hashtb
4617                          (setq des (gnus-gethash (car groups) 
4618                                                  gnus-description-hashtb)))
4619                     (insert "  " des "\n"))))
4620           (setq groups (cdr groups)))
4621         (goto-char (point-min))))
4622     (pop-to-buffer obuf)))
4623
4624 (defun gnus-group-description-apropos (regexp)
4625   "List all newsgroups that have names or descriptions that match a regexp."
4626   (interactive "sGnus description apropos (regexp): ")
4627   (if (not (or gnus-description-hashtb
4628                (gnus-read-all-descriptions-files)))
4629       (error "Couldn't request descriptions file"))
4630   (gnus-group-apropos regexp t))
4631
4632 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4633 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4634   "List all groups with unread articles that match REGEXP.
4635 If the prefix LEVEL is non-nil, it should be a number that says which
4636 level to cut off listing groups. 
4637 If ALL, also list groups with no unread articles.
4638 If LOWEST, don't list groups with level lower than LOWEST."
4639   (interactive "P\nsList newsgroups matching: ")
4640   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4641                            all (or lowest 1) regexp)
4642   (goto-char (point-min))
4643   (gnus-group-position-cursor))
4644
4645 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4646   "List all groups that match REGEXP.
4647 If the prefix LEVEL is non-nil, it should be a number that says which
4648 level to cut off listing groups. 
4649 If LOWEST, don't list groups with level lower than LOWEST."
4650   (interactive "P\nsList newsgroups matching: ")
4651   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4652
4653 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4654 (defun gnus-group-save-newsrc ()
4655   "Save the Gnus startup files."
4656   (interactive)
4657   (gnus-save-newsrc-file))
4658
4659 (defun gnus-group-restart (&optional arg)
4660   "Force Gnus to read the .newsrc file."
4661   (interactive "P")
4662   (gnus-save-newsrc-file)
4663   (gnus-setup-news 'force)
4664   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4665
4666 (defun gnus-group-read-init-file ()
4667   "Read the Gnus elisp init file."
4668   (interactive)
4669   (gnus-read-init-file))
4670
4671 (defun gnus-group-check-bogus-groups (silent)
4672   "Check bogus newsgroups.
4673 If given a prefix, don't ask for confirmation before removing a bogus
4674 group."
4675   (interactive "P")
4676   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4677   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4678
4679 (defun gnus-group-edit-global-kill (article &optional group)
4680   "Edit the global kill file.
4681 If GROUP, edit that local kill file instead."
4682   (interactive "P")
4683   (setq gnus-current-kill-article article)
4684   (gnus-kill-file-edit-file group)
4685   (gnus-message 6
4686    (substitute-command-keys
4687     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4688
4689 (defun gnus-group-edit-local-kill (article group)
4690   "Edit a local kill file."
4691   (interactive (list nil (gnus-group-group-name)))
4692   (gnus-group-edit-global-kill article group))
4693
4694 (defun gnus-group-force-update ()
4695   "Update `.newsrc' file."
4696   (interactive)
4697   (gnus-save-newsrc-file))
4698
4699 (defun gnus-group-suspend ()
4700   "Suspend the current Gnus session.
4701 In fact, cleanup buffers except for group mode buffer.
4702 The hook gnus-suspend-gnus-hook is called before actually suspending."
4703   (interactive)
4704   (run-hooks 'gnus-suspend-gnus-hook)
4705   ;; Kill Gnus buffers except for group mode buffer.
4706   (let ((group-buf (get-buffer gnus-group-buffer)))
4707     ;; Do this on a separate list in case the user does a ^G before we finish
4708     (let ((gnus-buffer-list
4709            (delq group-buf (delq gnus-dribble-buffer
4710                                  (append gnus-buffer-list nil)))))
4711       (while gnus-buffer-list
4712         (gnus-kill-buffer (car gnus-buffer-list))
4713         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4714     (if group-buf
4715         (progn
4716           (setq gnus-buffer-list (list group-buf))
4717           (bury-buffer group-buf)
4718           (delete-windows-on group-buf t)))))
4719
4720 (defun gnus-group-clear-dribble ()
4721   "Clear all information from the dribble buffer."
4722   (interactive)
4723   (gnus-dribble-clear))
4724
4725 (defun gnus-group-exit ()
4726   "Quit reading news after updating .newsrc.eld and .newsrc.
4727 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4728   (interactive)
4729   (if (or noninteractive                ;For gnus-batch-kill
4730           (zerop (buffer-size))         ;No news is good news.
4731           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4732           (not gnus-interactive-exit)   ;Without confirmation
4733           gnus-expert-user
4734           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4735       (progn
4736         (if gnus-use-full-window
4737             (delete-other-windows)
4738           (gnus-remove-some-windows))
4739         (run-hooks 'gnus-exit-gnus-hook)
4740         (gnus-offer-save-summaries)
4741         (gnus-save-newsrc-file)
4742         (gnus-close-backends)
4743         (gnus-clear-system))))
4744
4745 (defun gnus-close-backends ()
4746   ;; Send a close request to all backends that support such a request. 
4747   (let ((methods gnus-valid-select-methods)
4748         func)
4749     (while methods
4750       (if (fboundp (setq func (intern (concat (car (car methods))
4751                                               "-request-close"))))
4752           (funcall func))
4753       (setq methods (cdr methods)))))
4754
4755 (defun gnus-group-quit ()
4756   "Quit reading news without updating .newsrc.eld or .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))
4761           (not (gnus-server-opened gnus-select-method))
4762           gnus-expert-user
4763           (not gnus-current-startup-file)
4764           (gnus-yes-or-no-p
4765            (format "Quit reading news without saving %s? "
4766                    (file-name-nondirectory gnus-current-startup-file))))
4767       (progn
4768         (run-hooks 'gnus-exit-gnus-hook)
4769         (if gnus-use-full-window
4770             (delete-other-windows)
4771           (gnus-remove-some-windows))
4772         (gnus-dribble-save)
4773         (gnus-close-backends)
4774         (gnus-clear-system))))
4775
4776 (defun gnus-offer-save-summaries ()
4777   (let ((buffers (buffer-list)))
4778     (save-excursion
4779       (while buffers
4780         (and 
4781          ;; We look for buffers with "Summary" in the name.
4782          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4783          (progn
4784            (set-buffer (car buffers))
4785            ;; We check that this is, indeed, a summary buffer.
4786            (eq major-mode 'gnus-summary-mode)) 
4787          ;; We ask the user whether she wants to save the info.
4788          (gnus-y-or-n-p
4789                (format "Update summary buffer %s? " (buffer-name)))
4790          ;; We do it by simply exiting.
4791          (gnus-summary-exit))
4792         (setq buffers (cdr buffers))))))
4793
4794 (defun gnus-group-describe-briefly ()
4795   "Give a one line description of the group mode commands."
4796   (interactive)
4797   (gnus-message 6
4798    (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")))
4799
4800 (defun gnus-group-browse-foreign-server (method)
4801   "Browse a foreign news server.
4802 If called interactively, this function will ask for a select method
4803  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4804 If not, METHOD should be a list where the first element is the method
4805 and the second element is the address."
4806   (interactive
4807    (list (let ((how (completing-read 
4808                      "Which backend: "
4809                      (append gnus-valid-select-methods gnus-server-alist)
4810                      nil t "nntp")))
4811            ;; We either got a backend name or a virtual server name.
4812            ;; If the first, we also need an address.
4813            (if (assoc how gnus-valid-select-methods)
4814                (list (intern how)
4815                      ;; Suggested by mapjph@bath.ac.uk.
4816                      (completing-read 
4817                       "Address: " 
4818                       (mapcar (lambda (server) (list server))
4819                               gnus-secondary-servers)))
4820              ;; We got a server name, so we find the method.
4821              (gnus-server-to-method how)))))
4822   (gnus-browse-foreign-server method))
4823
4824 \f
4825 ;;;
4826 ;;; Browse Server Mode
4827 ;;;
4828
4829 (defvar gnus-browse-mode-hook nil)
4830 (defvar gnus-browse-mode-map nil)
4831 (put 'gnus-browse-mode 'mode-class 'special)
4832
4833 (if gnus-browse-mode-map
4834     nil
4835   (setq gnus-browse-mode-map (make-keymap))
4836   (suppress-keymap gnus-browse-mode-map)
4837   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4838   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4839   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4840   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4841   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4842   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4843   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4844   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4845   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4846   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4847   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4848   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4849   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4850   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4851   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4852   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4853   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4854   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4855   )
4856
4857 (defvar gnus-browse-current-method nil)
4858 (defvar gnus-browse-return-buffer nil)
4859
4860 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4861
4862 (defun gnus-browse-foreign-server (method &optional return-buffer)
4863   (setq gnus-browse-current-method method)
4864   (setq gnus-browse-return-buffer return-buffer)
4865   (let ((gnus-select-method method)
4866         groups group)
4867     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4868     (or (gnus-server-opened method)
4869         (gnus-open-server method)
4870         (error "Unable to contact server: %s" (gnus-status-message method)))
4871     (or (gnus-request-list method)
4872         (error "Couldn't request list: %s" (gnus-status-message method)))
4873     (get-buffer-create gnus-browse-buffer)
4874     (gnus-add-current-to-buffer-list)
4875     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4876     (gnus-configure-windows 'browse)
4877     (buffer-disable-undo (current-buffer))
4878     (let ((buffer-read-only nil))
4879       (erase-buffer))
4880     (gnus-browse-mode)
4881     (setq mode-line-buffer-identification
4882           (format
4883            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4884     (save-excursion
4885       (set-buffer nntp-server-buffer)
4886       (let ((cur (current-buffer)))
4887         (goto-char (point-min))
4888         (or (string= gnus-ignored-newsgroups "")
4889             (delete-matching-lines gnus-ignored-newsgroups))
4890         (while (re-search-forward 
4891                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4892           (goto-char (match-end 1))
4893           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4894                                                      (match-end 1))
4895                                    (max 0 (- (1+ (read cur)) (read cur))))
4896                              groups)))))
4897     (setq groups (sort groups 
4898                        (lambda (l1 l2)
4899                          (string< (car l1) (car l2)))))
4900     (let ((buffer-read-only nil))
4901       (while groups
4902         (setq group (car groups))
4903         (insert 
4904          (format "K%7d: %s\n" (cdr group) (car group)))
4905         (setq groups (cdr groups))))
4906     (switch-to-buffer (current-buffer))
4907     (goto-char (point-min))
4908     (gnus-group-position-cursor)))
4909
4910 (defun gnus-browse-mode ()
4911   "Major mode for browsing a foreign server.
4912
4913 All normal editing commands are switched off.
4914
4915 \\<gnus-browse-mode-map>
4916 The only things you can do in this buffer is
4917
4918 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4919 The group will be inserted into the group buffer upon exit from this
4920 buffer.  
4921
4922 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4923
4924 3) `\\[gnus-browse-exit]' to return to the group buffer."
4925   (interactive)
4926   (kill-all-local-variables)
4927   (if gnus-visual (gnus-browse-make-menu-bar))
4928   (setq mode-line-modified "-- ")
4929   (make-local-variable 'mode-line-format)
4930   (setq mode-line-format (copy-sequence mode-line-format))
4931   (and (equal (nth 3 mode-line-format) "   ")
4932        (setcar (nthcdr 3 mode-line-format) ""))
4933   (setq major-mode 'gnus-browse-mode)
4934   (setq mode-name "Browse Server")
4935   (setq mode-line-process nil)
4936   (use-local-map gnus-browse-mode-map)
4937   (buffer-disable-undo (current-buffer))
4938   (setq truncate-lines t)
4939   (setq buffer-read-only t)
4940   (run-hooks 'gnus-browse-mode-hook))
4941
4942 (defun gnus-browse-read-group (&optional no-article)
4943   "Enter the group at the current line."
4944   (interactive)
4945   (let ((group (gnus-browse-group-name)))
4946     (or (gnus-group-read-ephemeral-group 
4947          group gnus-browse-current-method nil
4948          (cons (current-buffer) 'browse))
4949         (error "Couldn't enter %s" group))))
4950
4951 (defun gnus-browse-select-group ()
4952   "Select the current group."
4953   (interactive)
4954   (gnus-browse-read-group 'no))
4955
4956 (defun gnus-browse-next-group (n)
4957   "Go to the next group."
4958   (interactive "p")
4959   (prog1
4960       (forward-line n)
4961     (gnus-group-position-cursor)))
4962
4963 (defun gnus-browse-prev-group (n)
4964   "Go to the next group."
4965   (interactive "p")
4966   (gnus-browse-next-group (- n)))
4967
4968 (defun gnus-browse-unsubscribe-current-group (arg)
4969   "(Un)subscribe to the next ARG groups."
4970   (interactive "p")
4971   (and (eobp)
4972        (error "No group at current line."))
4973   (let ((ward (if (< arg 0) -1 1))
4974         (arg (abs arg)))
4975     (while (and (> arg 0)
4976                 (not (eobp))
4977                 (gnus-browse-unsubscribe-group)
4978                 (zerop (gnus-browse-next-group ward)))
4979       (setq arg (1- arg)))
4980     (gnus-group-position-cursor)
4981     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4982     arg))
4983
4984 (defun gnus-browse-group-name ()
4985   (save-excursion
4986     (beginning-of-line)
4987     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4988         ()
4989       (gnus-group-prefixed-name 
4990        (buffer-substring (match-beginning 1) (match-end 1))
4991        gnus-browse-current-method))))
4992   
4993 (defun gnus-browse-unsubscribe-group ()
4994   (let ((sub nil)
4995         (buffer-read-only nil)
4996         group)
4997     (save-excursion
4998       (beginning-of-line)
4999       (if (= (following-char) ?K) (setq sub t))
5000       (setq group (gnus-browse-group-name))
5001       (beginning-of-line)
5002       (delete-char 1)
5003       (if sub
5004           (progn
5005             (gnus-group-change-level 
5006              (list t group gnus-level-default-subscribed
5007                    nil nil gnus-browse-current-method) 
5008              gnus-level-default-subscribed gnus-level-killed
5009              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
5010              t)
5011             (insert ? ))
5012         (gnus-group-change-level 
5013          group gnus-level-killed gnus-level-default-subscribed)
5014         (insert ?K)))
5015     t))
5016
5017 (defun gnus-browse-exit ()
5018   "Quit browsing and return to the group buffer."
5019   (interactive)
5020   (if (eq major-mode 'gnus-browse-mode)
5021       (kill-buffer (current-buffer)))
5022   (if gnus-browse-return-buffer
5023       (gnus-configure-windows 'server)
5024     (gnus-configure-windows 'group)
5025     (gnus-group-list-groups nil)))
5026
5027 (defun gnus-browse-describe-briefly ()
5028   "Give a one line description of the group mode commands."
5029   (interactive)
5030   (gnus-message 6
5031    (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")))
5032       
5033 \f
5034 ;;;
5035 ;;; Gnus summary mode
5036 ;;;
5037
5038 (defvar gnus-summary-mode-map nil)
5039 (defvar gnus-summary-mark-map nil)
5040 (defvar gnus-summary-mscore-map nil)
5041 (defvar gnus-summary-article-map nil)
5042 (defvar gnus-summary-thread-map nil)
5043 (defvar gnus-summary-goto-map nil)
5044 (defvar gnus-summary-exit-map nil)
5045 (defvar gnus-summary-various-map nil)
5046 (defvar gnus-summary-interest-map nil)
5047 (defvar gnus-summary-sort-map nil)
5048 (defvar gnus-summary-backend-map nil)
5049 (defvar gnus-summary-save-map nil)
5050 (defvar gnus-summary-wash-map nil)
5051 (defvar gnus-summary-help-map nil)
5052
5053 (put 'gnus-summary-mode 'mode-class 'special)
5054
5055 (if gnus-summary-mode-map
5056     nil
5057   (setq gnus-summary-mode-map (make-keymap))
5058   (suppress-keymap gnus-summary-mode-map)
5059
5060   ;; Non-orthogonal keys
5061
5062   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5063   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5064   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5065   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5066   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5067   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5068   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5069   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5070   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5071   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5072   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5073   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5074   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5075   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5076   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5077   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5078   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5079   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5080   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5081   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5082   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5083   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5084   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5085   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5086   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5087   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5088   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5089   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5090   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5091   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5092   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5093   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5094   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5095   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5096   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5097   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5098   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5099   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5100   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5101   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5102   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5103   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5104   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5105   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5106   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5107   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5108   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5109   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5110   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5111   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5112   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5113   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5114   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5115   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5116   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5117   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5118   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5119   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5120   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5121   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5122   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5123   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5124   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5125   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5126   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5127   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5128   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5129   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5130   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5131   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5132   (define-key gnus-summary-mode-map "V" 'gnus-version)
5133   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5134   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5135   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5136   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5137   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5138   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5139   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5140   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5141 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5142   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5143   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5144   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5145 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5146   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5147   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5148   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5149   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5150   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5151
5152
5153   ;; Sort of orthogonal keymap
5154   (define-prefix-command 'gnus-summary-mark-map)
5155   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5156   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5157   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5158   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5159   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5160   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5161   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5162   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5163   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5164   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5165   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5166   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5167   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5168   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5169   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5170   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5171   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5172   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5173   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5174   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5175   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5176   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5177   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5178   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5179
5180   (define-prefix-command 'gnus-summary-mscore-map)
5181   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5182   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5183   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5184   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5185   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5186
5187   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5188   
5189   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5190   
5191   (define-prefix-command 'gnus-summary-goto-map)
5192   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5193   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5194   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5195   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5196   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5197   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5198   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5199   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5200   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5201   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5202   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5203   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5204   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5205   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5206
5207
5208   (define-prefix-command 'gnus-summary-thread-map)
5209   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5210   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5211   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5212   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5213   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5214   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5215   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5216   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5217   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5218   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5219   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5220   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5221   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5222   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5223
5224   
5225   (define-prefix-command 'gnus-summary-exit-map)
5226   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5227   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5228   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5229   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5230   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5231   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5232   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5233   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5234   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5235   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5236   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5237
5238
5239   (define-prefix-command 'gnus-summary-article-map)
5240   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5241   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5242   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5243   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5244   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5245   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5246   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5247   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5248   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5249   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5250   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5251   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5252   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5253   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5254   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5255   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5256   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5257   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5258
5259
5260   (define-prefix-command 'gnus-summary-wash-map)
5261   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5262   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5263   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5264   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5265   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5266   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5267   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5268   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5269   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5270   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5271   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5272   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5273
5274   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5275   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5276   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5277   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5278   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5279   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5280
5281
5282   (define-prefix-command 'gnus-summary-help-map)
5283   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5284   (define-key gnus-summary-help-map "v" 'gnus-version)
5285   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5286   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5287   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5288   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5289
5290
5291   (define-prefix-command 'gnus-summary-backend-map)
5292   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5293   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5294   (define-key gnus-summary-backend-map "\M-\C-e" 
5295     'gnus-summary-expire-articles-now)
5296   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5297   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5298   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5299   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5300   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5301   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5302   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5303
5304
5305   (define-prefix-command 'gnus-summary-save-map)
5306   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5307   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5308   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5309   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5310   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5311   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5312   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5313   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5314 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5315
5316   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5317   
5318   (define-prefix-command 'gnus-summary-various-map)
5319   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5320   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5321   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5322   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5323   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5324   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5325   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5326   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5327   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5328   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5329   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5330
5331   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5332
5333   (define-prefix-command 'gnus-summary-sort-map)
5334   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5335   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5336   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5337   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5338   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5339   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5340
5341   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5342   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5343   )
5344
5345
5346 \f
5347
5348 (defun gnus-summary-mode (&optional group)
5349   "Major mode for reading articles.
5350
5351 All normal editing commands are switched off.
5352 \\<gnus-summary-mode-map>
5353 Each line in this buffer represents one article.  To read an
5354 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5355 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5356 respectively.
5357
5358 You can also post articles and send mail from this buffer.  To 
5359 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5360 of an article, type `\\[gnus-summary-reply]'.
5361
5362 There are approx. one gazillion commands you can execute in this 
5363 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5364
5365 The following commands are available:
5366
5367 \\{gnus-summary-mode-map}"
5368   (interactive)
5369   (if gnus-visual (gnus-summary-make-menu-bar))
5370   (kill-all-local-variables)
5371   (let ((locals gnus-summary-local-variables))
5372     (while locals
5373       (if (consp (car locals))
5374           (progn
5375             (make-local-variable (car (car locals)))
5376             (set (car (car locals)) (eval (cdr (car locals)))))
5377         (make-local-variable (car locals))
5378         (set (car locals) nil))
5379       (setq locals (cdr locals))))
5380   (gnus-make-thread-indent-array)
5381   (gnus-update-format-specifications)
5382   (setq mode-line-modified "-- ")
5383   (make-local-variable 'mode-line-format)
5384   (setq mode-line-format (copy-sequence mode-line-format))
5385   (and (equal (nth 3 mode-line-format) "   ")
5386        (setcar (nthcdr 3 mode-line-format) ""))
5387   (setq major-mode 'gnus-summary-mode)
5388   (setq mode-name "Summary")
5389   (make-local-variable 'minor-mode-alist)
5390   (use-local-map gnus-summary-mode-map)
5391   (buffer-disable-undo (current-buffer))
5392   (setq buffer-read-only t)             ;Disable modification
5393   (setq truncate-lines t)
5394   (setq selective-display t)
5395   (setq selective-display-ellipses t)   ;Display `...'
5396   (setq buffer-display-table gnus-summary-display-table)
5397   (setq gnus-newsgroup-name group)
5398   (run-hooks 'gnus-summary-mode-hook))
5399
5400 (defun gnus-summary-make-display-table ()
5401   ;; Change the display table.  Odd characters have a tendency to mess
5402   ;; up nicely formatted displays - we make all possible glyphs
5403   ;; display only a single character.
5404
5405   ;; We start from the standard display table, if any.
5406   (setq gnus-summary-display-table 
5407         (or (copy-sequence standard-display-table)
5408             (make-display-table)))
5409   ;; Nix out all the control chars...
5410   (let ((i 32))
5411     (while (>= (setq i (1- i)) 0)
5412       (aset gnus-summary-display-table i [??])))
5413   ;; ... but not newline and cr, of course. (cr is necessary for the
5414   ;; selective display).  
5415   (aset gnus-summary-display-table ?\n nil)
5416   (aset gnus-summary-display-table ?\r nil)
5417   ;; We nix out any glyphs over 126 that are not set already.  
5418   (let ((i 256))
5419     (while (>= (setq i (1- i)) 127)
5420       ;; Only modify if the entry is nil.
5421       (or (aref gnus-summary-display-table i) 
5422           (aset gnus-summary-display-table i [??])))))
5423
5424 (defun gnus-summary-clear-local-variables ()
5425   (let ((locals gnus-summary-local-variables))
5426     (while locals
5427       (if (consp (car locals))
5428           (and (vectorp (car (car locals)))
5429                (set (car (car locals)) nil))
5430         (and (vectorp (car locals))
5431              (set (car locals) nil)))
5432       (setq locals (cdr locals)))))
5433
5434 (defun gnus-mouse-pick-article (e)
5435   (interactive "e")
5436   (mouse-set-point e)
5437   (gnus-summary-next-page nil t))
5438
5439 (defun gnus-summary-setup-buffer (group)
5440   "Initialize summary buffer."
5441   (let ((buffer (concat "*Summary " group "*")))
5442     (if (get-buffer buffer)
5443         (progn
5444           (set-buffer buffer)
5445           (not gnus-newsgroup-begin))
5446       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5447       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5448       (gnus-add-current-to-buffer-list)
5449       (gnus-summary-mode group)
5450       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5451       (setq gnus-newsgroup-name group)
5452       t)))
5453
5454 (defun gnus-set-global-variables ()
5455   ;; Set the global equivalents of the summary buffer-local variables
5456   ;; to the latest values they had. These reflect the summary buffer
5457   ;; that was in action when the last article was fetched.
5458   (if (eq major-mode 'gnus-summary-mode) 
5459       (progn
5460         (setq gnus-summary-buffer (current-buffer))
5461         (let ((name gnus-newsgroup-name)
5462               (marked gnus-newsgroup-marked)
5463               (unread gnus-newsgroup-unreads)
5464               (headers gnus-current-headers)
5465               (score-file gnus-current-score-file))
5466           (save-excursion
5467             (set-buffer gnus-group-buffer)
5468             (setq gnus-newsgroup-name name)
5469             (setq gnus-newsgroup-marked marked)
5470             (setq gnus-newsgroup-unreads unread)
5471             (setq gnus-current-headers headers)
5472             (setq gnus-current-score-file score-file))))))
5473
5474 (defun gnus-summary-insert-dummy-line (sformat subject number)
5475   (if (not sformat) 
5476       (setq sformat gnus-summary-dummy-line-format-spec))
5477   (let (b)
5478     (beginning-of-line)
5479     (setq b (point))
5480     (insert (eval sformat))
5481     (add-text-properties
5482      b (1+ b)
5483      (list 'gnus-number number 
5484            'gnus-mark gnus-dummy-mark
5485            'gnus-level 0))))
5486
5487 (defvar gnus-thread-indent-array nil)
5488 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5489 (defun gnus-make-thread-indent-array ()
5490   (let ((n 200))
5491     (if (and gnus-thread-indent-array
5492              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5493         nil
5494       (setq gnus-thread-indent-array (make-vector 201 "")
5495             gnus-thread-indent-array-level gnus-thread-indent-level)
5496       (while (>= n 0)
5497         (aset gnus-thread-indent-array n
5498               (make-string (* n gnus-thread-indent-level) ? ))
5499         (setq n (1- n))))))
5500
5501 (defun gnus-summary-insert-line 
5502   (sformat header level current unread replied expirable subject-or-nil
5503            &optional dummy score)
5504   (or sformat (setq sformat gnus-summary-line-format-spec))
5505   (let* ((indentation (aref gnus-thread-indent-array level))
5506          (lines (header-lines header))
5507          (score (or score gnus-summary-default-score 0))
5508          (score-char
5509           (if (or (null gnus-summary-default-score)
5510                   (<= (abs (- score gnus-summary-default-score))
5511                       gnus-summary-zcore-fuzz)) ? 
5512             (if (< score gnus-summary-default-score)
5513                 gnus-score-below-mark gnus-score-over-mark)))
5514          (replied (if replied gnus-replied-mark ? ))
5515          (from (header-from header))
5516          (name-address (funcall gnus-extract-address-components from))
5517          (address (car (cdr name-address)))
5518          (name (or (car name-address) (car (cdr name-address))))
5519          (subject (header-subject header))
5520          (number (header-number header))
5521          (opening-bracket (if dummy ?\< ?\[))
5522          (closing-bracket (if dummy ?\> ?\]))
5523          (buffer-read-only nil)
5524          (b (progn (beginning-of-line) (point))))
5525     (or (numberp lines) (setq lines 0))
5526     (insert (eval sformat))
5527     (add-text-properties
5528      b (1+ b) (list 'gnus-number number 
5529                     'gnus-mark (or unread gnus-unread-mark)
5530                     'gnus-level level))))
5531
5532 (defun gnus-summary-update-line (&optional dont-update)
5533   ;; Update summary line after change.
5534   (or (not gnus-summary-default-score)
5535       gnus-summary-inhibit-highlight
5536       (let ((gnus-summary-inhibit-highlight t)
5537             (article (gnus-summary-article-number)))
5538         (progn
5539           (or dont-update
5540               (if (and gnus-summary-mark-below
5541                        (< (gnus-summary-article-score)
5542                           gnus-summary-mark-below))
5543                   (and (not (memq article gnus-newsgroup-marked))
5544                        (not (memq article gnus-newsgroup-dormant))
5545                        (memq article gnus-newsgroup-unreads)
5546                        (gnus-summary-mark-article nil gnus-low-score-mark))
5547                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5548                      (gnus-summary-mark-article nil gnus-unread-mark))))
5549           (and gnus-visual
5550                (run-hooks 'gnus-summary-update-hook))))))
5551
5552 (defun gnus-summary-update-lines (&optional beg end)
5553   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5554   (let ((beg (or beg (point-min)))
5555         (end (or end (point-max))))
5556     (save-excursion
5557       (set-buffer gnus-summary-buffer)
5558       (goto-char beg)
5559       (while (and (not (eobp)) (< (point) end))
5560         (gnus-summary-update-line)
5561         (forward-line 1)))))
5562
5563 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5564   ;; Sum up all elements (and sub-elements) in a list.
5565   (let ((number 
5566          (if (listp thread) 
5567              (apply 
5568               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5569            1)))
5570     (if char 
5571         (if (> number 1) gnus-not-empty-thread-mark
5572           gnus-empty-thread-mark)
5573       number)))
5574
5575 (defun gnus-summary-read-group 
5576   (group &optional show-all no-article kill-buffer)
5577   "Start reading news in newsgroup GROUP.
5578 If SHOW-ALL is non-nil, already read articles are also listed.
5579 If NO-ARTICLE is non-nil, no article is selected initially."
5580   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5581   (let* ((new-group (gnus-summary-setup-buffer group))
5582          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5583                                                   group))))
5584          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5585     (cond 
5586      ((not new-group)
5587       (gnus-set-global-variables)
5588       (gnus-kill-buffer kill-buffer)
5589       (gnus-configure-windows 'summary)
5590       (gnus-set-mode-line 'summary)
5591       (gnus-summary-position-cursor)
5592       (message "")
5593       t)
5594      ((null did-select) 
5595       (and (eq major-mode 'gnus-summary-mode)
5596            (not (equal (current-buffer) kill-buffer))
5597            (progn
5598              (kill-buffer (current-buffer))
5599              (if (not quit-config)
5600                  (progn
5601                    (set-buffer gnus-group-buffer)
5602                    (gnus-group-jump-to-group group)
5603                    (gnus-group-next-unread-group 1))
5604                (if (not (buffer-name (car quit-config)))
5605                    (gnus-configure-windows 'group)
5606                  (set-buffer (car quit-config))
5607                  (and (eq major-mode 'gnus-summary-mode)
5608                       (gnus-set-global-variables))
5609                  (gnus-configure-windows (cdr quit-config))))))
5610       (message "Can't select group")
5611       nil)
5612      ((eq did-select 'quit)
5613       (and (eq major-mode 'gnus-summary-mode)
5614            (not (equal (current-buffer) kill-buffer))
5615            (kill-buffer (current-buffer)))
5616       (gnus-kill-buffer kill-buffer)
5617       (if (not quit-config)
5618           (progn
5619             (set-buffer gnus-group-buffer)
5620             (gnus-group-jump-to-group group)
5621             (gnus-group-next-unread-group 1)
5622             (gnus-configure-windows 'group))
5623         (if (not (buffer-name (car quit-config)))
5624             (gnus-configure-windows 'group)
5625           (set-buffer (car quit-config))
5626           (and (eq major-mode 'gnus-summary-mode)
5627                (gnus-set-global-variables))
5628           (gnus-configure-windows (cdr quit-config))))
5629       (signal 'quit nil))
5630      (t
5631       (gnus-set-global-variables)
5632       ;; Save the active value in effect when the group was entered.
5633       (setq gnus-newsgroup-active 
5634             (gnus-copy-sequence
5635              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5636       ;; You can change the subjects in this hook.
5637       (run-hooks 'gnus-select-group-hook)
5638       ;; Do score processing.
5639       (and gnus-use-scoring (gnus-possibly-score-headers))
5640       ;; Update the format specifiers.
5641       (gnus-update-format-specifications)
5642       ;; Generate the summary buffer.
5643       (gnus-summary-prepare)
5644       (if (zerop (buffer-size))
5645           (cond (gnus-newsgroup-dormant
5646                  (gnus-summary-show-all-dormant))
5647                 ((and gnus-newsgroup-scored show-all)
5648                  (gnus-summary-show-all-expunged))))
5649       ;; Function `gnus-apply-kill-file' must be called in this hook.
5650       (run-hooks 'gnus-apply-kill-hook)
5651       (if (zerop (buffer-size))
5652           (progn
5653             ;; This newsgroup is empty.
5654             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5655             (gnus-message 6 "No unread news")
5656             (gnus-kill-buffer kill-buffer)
5657             nil)
5658         ;;(save-excursion
5659         ;;  (if kill-buffer
5660         ;;      (let ((gnus-summary-buffer kill-buffer))
5661         ;;      (gnus-configure-windows 'group))))
5662         ;; Hide conversation thread subtrees.  We cannot do this in
5663         ;; gnus-summary-prepare-hook since kill processing may not
5664         ;; work with hidden articles.
5665         (and gnus-show-threads
5666              gnus-thread-hide-subtree
5667              (gnus-summary-hide-all-threads))
5668         ;; Show first unread article if requested.
5669         (goto-char (point-min))
5670         (if (and (not no-article)
5671                  gnus-auto-select-first
5672                  (gnus-summary-first-unread-article))
5673             ()
5674           (gnus-configure-windows 'summary))
5675         (gnus-set-mode-line 'summary)
5676         (gnus-summary-position-cursor)
5677         ;; If in async mode, we send some info to the backend.
5678         (and gnus-newsgroup-async
5679              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5680              (gnus-request-asynchronous 
5681               gnus-newsgroup-name
5682               (if (and gnus-asynchronous-article-function
5683                        (fboundp gnus-asynchronous-article-function))
5684                   (funcall gnus-asynchronous-article-function
5685                            gnus-newsgroup-threads)
5686                 gnus-newsgroup-threads)))
5687         (gnus-kill-buffer kill-buffer)
5688         (if (not (get-buffer-window gnus-group-buffer))
5689             ()
5690           ;; gotta use windows, because recenter does wierd stuff if
5691           ;; the current buffer ain't the displayed window.
5692           (let ((owin (selected-window))) 
5693             (select-window (get-buffer-window gnus-group-buffer))
5694             (and (gnus-group-goto-group group)
5695                  (recenter))
5696             (select-window owin))))
5697       t))))
5698
5699 (defun gnus-summary-prepare ()
5700   ;; Generate the summary buffer.
5701   (let ((buffer-read-only nil))
5702     (erase-buffer)
5703     (gnus-summary-prepare-threads 
5704      (if gnus-show-threads
5705          (gnus-gather-threads 
5706           (gnus-sort-threads 
5707            (if (and gnus-summary-expunge-below
5708                     (not gnus-fetch-old-headers))
5709                (gnus-make-threads-and-expunge)
5710              (gnus-make-threads))))
5711        gnus-newsgroup-headers)
5712      0 nil nil t)
5713     ;; Erase header retrieval message.
5714     (gnus-summary-update-lines)
5715     (message "")
5716     ;; Remove the final newline.
5717     ;;(goto-char (point-max))
5718     ;;(delete-char -1)
5719     ;; Call hooks for modifying summary buffer.
5720     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5721     (goto-char (point-min))
5722     (run-hooks 'gnus-summary-prepare-hook)))
5723
5724 (defun gnus-gather-threads (threads)
5725   "Gather threads that have lost their roots."
5726   (if (not gnus-summary-make-false-root)
5727       threads 
5728     (let ((hashtb (gnus-make-hashtable 1023))
5729           (prev threads)
5730           (result threads)
5731           subject hthread whole-subject)
5732       (while threads
5733         (setq whole-subject 
5734               (setq subject (header-subject (car (car threads)))))
5735         (if gnus-summary-gather-subject-limit
5736             (or (and (numberp gnus-summary-gather-subject-limit)
5737                      (> (length subject) gnus-summary-gather-subject-limit)
5738                      (setq subject
5739                            (substring subject 0 
5740                                       gnus-summary-gather-subject-limit)))
5741                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5742                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5743           (setq subject (gnus-simplify-subject-re subject)))
5744         (if (setq hthread 
5745                   (gnus-gethash subject hashtb))
5746             (progn
5747               (or (stringp (car (car hthread)))
5748                   (setcar hthread (list whole-subject (car hthread))))
5749               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5750                                            (list (car threads))))
5751               (setcdr prev (cdr threads))
5752               (setq threads prev))
5753           (gnus-sethash subject threads hashtb))
5754         (setq prev threads)
5755         (setq threads (cdr threads)))
5756       result)))
5757
5758 (defun gnus-make-threads ()
5759   ;; This function takes the dependencies already made by 
5760   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5761   ;; through the dependecies in the hash table and finds all the
5762   ;; roots. Roots do not refer back to any valid articles.
5763   (let (roots)
5764     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5765          (gnus-build-old-threads))
5766     (mapatoms
5767      (lambda (refs)
5768        (if (not (car (symbol-value refs)))
5769            (setq roots (append (cdr (symbol-value refs)) roots))
5770          ;; Ok, these refer back to valid articles, but if
5771          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5772          ;; the root has the same subject as its children. The children
5773          ;; that do not are made into roots and removed from the list
5774          ;; of children. 
5775          (or gnus-thread-ignore-subject
5776              (let* ((prev (symbol-value refs))
5777                     (subject (gnus-simplify-subject-re 
5778                               (header-subject (car prev))))
5779                     (headers (cdr prev)))
5780                (while headers
5781                  (if (not (string= subject
5782                                    (gnus-simplify-subject-re 
5783                                     (header-subject (car headers)))))
5784                      (progn
5785                        (setq roots (cons (car headers) roots))
5786                        (setcdr prev (cdr headers)))
5787                    (setq prev headers))
5788                  (setq headers (cdr headers)))))))
5789      gnus-newsgroup-dependencies)
5790     
5791     (mapcar 'gnus-trim-thread
5792             (apply 'append
5793                    (mapcar 'gnus-cut-thread
5794                            (mapcar 'gnus-make-sub-thread roots))))))
5795   
5796 (defun gnus-make-threads-and-expunge ()
5797   ;; This function takes the dependencies already made by 
5798   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5799   ;; through the dependecies in the hash table and finds all the
5800   ;; roots. Roots do not refer back to any valid articles.
5801   (let ((default (or gnus-summary-default-score 0))
5802         (below gnus-summary-expunge-below)
5803         roots article)
5804     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5805          (gnus-build-old-threads))
5806     (mapatoms
5807      (lambda (refs)
5808        (if (not (car (symbol-value refs)))
5809            ;; These articles do not refer back to any other articles -
5810            ;; they are roots.
5811            (let ((headers (cdr (symbol-value refs))))
5812              ;; We weed out the low-scored articles.
5813              (while headers
5814                (if (not (< (or (cdr (assq (header-number (car headers))
5815                                           gnus-newsgroup-scored)) default)
5816                            below))
5817                    ;; It is over.
5818                    (setq roots (cons (car headers) roots))
5819                  ;; It is below, so we mark it as read.
5820                  (setq gnus-newsgroup-unreads
5821                        (delq (header-number (car headers))
5822                              gnus-newsgroup-unreads)))
5823                (setq headers (cdr headers))))
5824          ;; Ok, these refer back to valid articles, but if
5825          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5826          ;; the root has the same subject as its children. The children
5827          ;; that do not are made into roots and removed from the list
5828          ;; of children. 
5829          (or gnus-thread-ignore-subject
5830              (let* ((prev (symbol-value refs))
5831                     (subject (gnus-simplify-subject-re 
5832                               (header-subject (car prev))))
5833                     (headers (cdr prev)))
5834                (while headers
5835                  (if (not (string= subject
5836                                    (gnus-simplify-subject-re 
5837                                     (header-subject (car headers)))))
5838                      (progn
5839                        (if (not (< (or (cdr (assq (header-number (car headers))
5840                                                   gnus-newsgroup-scored))
5841                                        default) below))
5842                            (setq roots (cons (car headers) roots))
5843                          (setq gnus-newsgroup-unreads
5844                                (delq (header-number (car headers))
5845                                      gnus-newsgroup-unreads)))
5846                        (setcdr prev (cdr headers)))
5847                    (setq prev headers))
5848                  (setq headers (cdr headers)))))
5849          ;; If this article is expunged, some of the children might be
5850          ;; roots.  
5851          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5852                                gnus-newsgroup-scored)) default)
5853                 below)
5854              (let* ((prev (symbol-value refs))
5855                     (headers (cdr prev)))
5856                (while headers
5857                  (setq article (header-number (car headers)))
5858                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5859                                  default) below))
5860                      (progn (setq roots (cons (car headers) roots))
5861                             (setq prev headers))
5862                    (setq gnus-newsgroup-unreads 
5863                          (delq article gnus-newsgroup-unreads))
5864                    (setcdr prev (cdr headers)))
5865                  (setq headers (cdr headers))))
5866            ;; It was not expunged, but we look at expunged children.
5867            (let* ((prev (symbol-value refs))
5868                   (headers (cdr prev))
5869                   article)
5870              (while headers
5871                (setq article (header-number (car headers)))
5872                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5873                                default) below))
5874                    (setq prev headers)
5875                  (setq gnus-newsgroup-unreads 
5876                        (delq article gnus-newsgroup-unreads))
5877                  (setcdr prev (cdr headers)))
5878                (setq headers (cdr headers)))))))
5879      gnus-newsgroup-dependencies)
5880
5881     (mapcar 'gnus-trim-thread
5882             (apply 'append
5883                    (mapcar 'gnus-cut-thread
5884                            (mapcar 'gnus-make-sub-thread roots))))))
5885   
5886 (defun gnus-cut-thread (thread)
5887   ;; Remove leaf dormant or ancient articles from THREAD.
5888   (let ((head (car thread))
5889         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5890     (if (and (null tail)
5891              (let ((number (header-number head)))
5892                (or (memq number gnus-newsgroup-ancient)
5893                    (memq number gnus-newsgroup-dormant)
5894                    (and gnus-summary-expunge-below
5895                         (eq gnus-fetch-old-headers 'some)
5896                         (< (or (cdr (assq number gnus-newsgroup-scored))
5897                                gnus-summary-default-score 0)
5898                            gnus-summary-expunge-below)
5899                         (progn
5900                           (setq gnus-newsgroup-unreads
5901                                 (delq number gnus-newsgroup-unreads))
5902                           t)))))
5903         nil
5904       (list (cons head tail)))))
5905
5906 (defun gnus-trim-thread (thread)
5907   ;; Remove root ancient articles with only one child from THREAD.
5908   (if (and (eq gnus-fetch-old-headers 'some)
5909            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5910            (= (length thread) 2))
5911       (gnus-trim-thread (nth 1 thread))
5912     thread))
5913
5914 (defun gnus-make-sub-thread (root)
5915   ;; This function makes a sub-tree for a node in the tree.
5916   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5917                                               gnus-newsgroup-dependencies)))))
5918     (cons root (mapcar 'gnus-make-sub-thread children))))
5919
5920 (defun gnus-build-old-threads ()
5921   ;; Look at all the articles that refer back to old articles, and
5922   ;; fetch the headers for the articles that aren't there. This will
5923   ;; build complete threads - if the roots haven't been expired by the
5924   ;; server, that is.
5925   (let (id heads)
5926     (mapatoms
5927      (lambda (refs)
5928        (if (not (car (symbol-value refs)))
5929            (progn
5930              (setq heads (cdr (symbol-value refs)))
5931              (while heads
5932                (if (not (memq (header-number (car heads))
5933                               gnus-newsgroup-dormant))
5934                    (progn
5935                      (setq id (symbol-name refs))
5936                      (while (and (setq id (gnus-build-get-header id))
5937                                  (not (car (gnus-gethash 
5938                                             id gnus-newsgroup-dependencies)))))
5939                      (setq heads nil))
5940                  (setq heads (cdr heads)))))))
5941      gnus-newsgroup-dependencies)))
5942
5943 (defun gnus-build-get-header (id)
5944   ;; Look through the buffer of NOV lines and find the header to
5945   ;; ID. Enter this line into the dependencies hash table, and return
5946   ;; the id of the parent article (if any).
5947   (let ((deps gnus-newsgroup-dependencies)
5948         found header)
5949     (prog1
5950         (save-excursion
5951           (set-buffer nntp-server-buffer)
5952           (goto-char (point-min))
5953           (while (and (not found) (search-forward id nil t))
5954             (beginning-of-line)
5955             (setq found (looking-at 
5956                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5957                                  (regexp-quote id))))
5958             (or found (beginning-of-line 2)))
5959           (if found
5960               (let (ref)
5961                 (beginning-of-line)
5962                 (and
5963                  (setq header (gnus-nov-parse-line 
5964                                (read (current-buffer)) deps))
5965                  (setq ref (header-references header))
5966                  (string-match "\\(<[^>]+>\\) *$" ref)
5967                  (substring ref (match-beginning 1) (match-end 1))))))
5968       (and header
5969            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5970                  gnus-newsgroup-ancient (cons (header-number header)
5971                                               gnus-newsgroup-ancient))))))
5972
5973 ;; Re-build the thread containing ID.
5974 (defun gnus-rebuild-thread (id)
5975   (let ((dep gnus-newsgroup-dependencies)
5976         (buffer-read-only nil)
5977         parent headers refs thread art)
5978     (while (and id (setq headers
5979                          (car (setq art (gnus-gethash (downcase id) dep)))))
5980       (setq parent art)
5981       (setq id (and (setq refs (header-references headers))
5982                     (string-match "\\(<[^>]+>\\) *$" refs)
5983                     (substring refs (match-beginning 1) (match-end 1)))))
5984     (setq thread (gnus-make-sub-thread (car parent)))
5985     (gnus-rebuild-remove-articles thread)
5986     (let ((beg (point)))
5987       (gnus-summary-prepare-threads (list thread) 0)
5988       (gnus-summary-update-lines beg (point)))))
5989
5990 ;; Delete all lines in the summary buffer that correspond to articles
5991 ;; in this thread.
5992 (defun gnus-rebuild-remove-articles (thread)
5993   (and (gnus-summary-goto-subject (header-number (car thread)))
5994        (gnus-delete-line))
5995   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5996
5997 (defun gnus-sort-threads (threads)
5998   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5999   (let ((fun gnus-thread-sort-functions))
6000     (while fun
6001       (setq threads (sort threads (car fun))
6002             fun (cdr fun))))
6003   threads)
6004
6005 (defun gnus-thread-header (thread)
6006   ;; Return header of first article in THREAD.
6007   (if (consp thread)
6008       (if (stringp (car thread))
6009           (car (car (cdr thread)))
6010         (car thread))
6011     thread))
6012
6013 (defun gnus-thread-sort-by-number (h1 h2)
6014   "Sort threads by root article number."
6015   (let ((h1 (gnus-thread-header h1))
6016         (h2 (gnus-thread-header h2)))
6017     (< (header-number h1) (header-number h2))))
6018
6019 (defun gnus-thread-sort-by-author (h1 h2)
6020   "Sort threads by root author."
6021   (let ((h1 (gnus-thread-header h1))
6022         (h2 (gnus-thread-header h2)))
6023     (string-lessp
6024      (let ((extract (funcall 
6025                      gnus-extract-address-components (header-from h1))))
6026        (or (car extract) (cdr extract)))
6027      (let ((extract (funcall
6028                      gnus-extract-address-components (header-from h2))))
6029        (or (car extract) (cdr extract))))))
6030
6031 (defun gnus-thread-sort-by-subject (h1 h2)
6032   "Sort threads by root subject."
6033   (let ((h1 (gnus-thread-header h1))
6034         (h2 (gnus-thread-header h2)))
6035     (string-lessp
6036      (downcase (gnus-simplify-subject (header-subject h1)))
6037      (downcase (gnus-simplify-subject (header-subject h2))))))
6038
6039 (defun gnus-thread-sort-by-date (h1 h2)
6040   "Sort threads by root article date."
6041   (let ((h1 (gnus-thread-header h1))
6042         (h2 (gnus-thread-header h2)))
6043     (string-lessp
6044      (gnus-sortable-date (header-date h1))
6045      (gnus-sortable-date (header-date h2)))))
6046
6047 (defun gnus-thread-sort-by-score (h1 h2)
6048   "Sort threads by root article score.
6049 Unscored articles will be counted as having a score of zero."
6050   (let ((h1 (gnus-thread-header h1))
6051         (h2 (gnus-thread-header h2)))
6052     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
6053           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
6054       (> (or (cdr s1) gnus-summary-default-score 0)
6055          (or (cdr s2) gnus-summary-default-score 0)))))
6056
6057 (defun gnus-thread-sort-by-total-score (h1 h2)
6058   "Sort threads by the sum of all scores in the thread.
6059 Unscored articles will be counted as having a score of zero."
6060   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6061
6062 (defun gnus-thread-total-score (thread)
6063   ;;  This function find the total score of THREAD.
6064   (if (consp thread)
6065       (if (stringp (car thread))
6066           (apply gnus-thread-score-function 0
6067                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6068         (gnus-thread-total-score-1 thread))
6069     (gnus-thread-total-score-1 (list thread))))
6070
6071 (defun gnus-thread-total-score-1 (root)
6072   ;; This function find the total score of the thread below ROOT.
6073   (setq root (car root))
6074   (apply gnus-thread-score-function
6075          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6076              gnus-summary-default-score 0)
6077          (mapcar 'gnus-thread-total-score
6078                  (cdr (gnus-gethash (downcase (header-id root))
6079                                     gnus-newsgroup-dependencies)))))
6080
6081 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6082 (defvar gnus-tmp-prev-subject "")
6083 (defvar gnus-tmp-adopt-thread nil)
6084
6085 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6086 (defun gnus-summary-prepare-threads 
6087   (threads level &optional not-child no-subject cull)
6088   "Prepare summary buffer from THREADS and indentation LEVEL.  
6089 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6090 or a straight list of headers."
6091   (let (thread header number subject clevel)
6092     (while threads
6093       (setq thread (car threads)
6094             threads (cdr threads))
6095       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6096       ;; `thread' is the header.
6097       (if (consp thread)
6098           (setq header (car thread))
6099         (setq header thread)
6100         (and cull
6101              (or (memq (setq number (header-number header))
6102                        gnus-newsgroup-dormant)
6103                  (and gnus-summary-expunge-below
6104                       (< (or (cdr (assq number gnus-newsgroup-scored))
6105                              gnus-summary-default-score 0)
6106                          gnus-summary-expunge-below)))
6107              (progn
6108                (setq header nil)
6109                (setq gnus-newsgroup-unreads 
6110                      (delq number gnus-newsgroup-unreads)))))
6111       (cond 
6112        ((stringp header)
6113         ;; The header is a dummy root.
6114         (cond ((eq gnus-summary-make-false-root 'adopt)
6115                ;; We let the first article adopt the rest.
6116                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6117                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6118                (setq thread (cdr (cdr thread)))
6119                (while thread
6120                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6121                  (setq thread (cdr thread))))
6122               ((eq gnus-summary-make-false-root 'dummy)
6123                ;; We output a dummy root.
6124                (gnus-summary-insert-dummy-line 
6125                 nil header (header-number (car (car (cdr thread)))))
6126                (setq clevel 1))
6127               ((eq gnus-summary-make-false-root 'empty)
6128                ;; We print the articles with empty subject fields. 
6129                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6130                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6131                (setq thread (cdr (cdr thread)))
6132                (while thread
6133                  (gnus-summary-prepare-threads 
6134                   (list (car thread)) 0 nil
6135                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6136                             (not (string=  
6137                                   (gnus-simplify-subject-re 
6138                                    (header-subject (car (car thread))))
6139                                   (gnus-simplify-subject-re header))))))
6140                  (setq thread (cdr thread))))
6141               (t
6142                ;; We do not make a root for the gathered
6143                ;; sub-threads at all.  
6144                (setq clevel 0)))
6145         ;; Print the sub-threads.
6146         (and (consp thread) (cdr thread)
6147              (gnus-summary-prepare-threads (cdr thread) clevel)))
6148        ;; The header is a real article.
6149        (header
6150         (setq number (header-number header)
6151               subject (header-subject header))
6152         (and gnus-newsgroup-async
6153              (setq gnus-newsgroup-threads
6154                    (cons (cons (header-number header)
6155                                (header-lines header)) gnus-newsgroup-threads)))
6156         (gnus-summary-insert-line
6157          nil header level nil 
6158          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6159                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6160                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6161                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6162                (t gnus-ancient-mark))
6163          (memq number gnus-newsgroup-replied)
6164          (memq number gnus-newsgroup-expirable)
6165          (if no-subject 
6166              gnus-summary-same-subject
6167            (if (or (zerop level)
6168                    (and gnus-thread-ignore-subject
6169                         (not (string= 
6170                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6171                               (gnus-simplify-subject-re subject)))))
6172                subject
6173              gnus-summary-same-subject))
6174          not-child
6175          (cdr (assq number gnus-newsgroup-scored)))
6176         (setq gnus-tmp-prev-subject subject)
6177         ;; Recursively print subthreads.
6178         (and (consp thread) (cdr thread)
6179              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6180
6181 (defun gnus-select-newsgroup (group &optional read-all)
6182   "Select newsgroup GROUP.
6183 If READ-ALL is non-nil, all articles in the group are selected."
6184   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6185          (info (nth 2 entry))
6186          articles)
6187     (gnus-check-news-server
6188      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6189
6190     (or (gnus-server-opened gnus-current-select-method)
6191         (gnus-open-server gnus-current-select-method)
6192         (error "Couldn't open server"))
6193     
6194     (or (and (eq (car entry) t)
6195              (gnus-activate-newsgroup (car info)))
6196         (gnus-request-group group t)
6197         (progn
6198           (kill-buffer (current-buffer))
6199           (error "Couldn't request group %s: %s" 
6200                  group (gnus-status-message group))))
6201
6202     (setq gnus-newsgroup-name group)
6203     (setq gnus-newsgroup-unselected nil)
6204     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6205
6206     (and gnus-asynchronous
6207          (gnus-check-backend-function 
6208           'request-asynchronous gnus-newsgroup-name)
6209          (setq gnus-newsgroup-async
6210                (gnus-request-asynchronous gnus-newsgroup-name)))
6211
6212     (setq articles (gnus-articles-to-read group read-all))
6213
6214     (cond 
6215      ((null articles) 
6216       (gnus-message 3 "Couldn't select newsgroup")
6217       'quit)
6218      ((eq articles 0) nil)
6219      (t
6220       ;; Init the dependencies hash table.
6221       (setq gnus-newsgroup-dependencies 
6222             (gnus-make-hashtable (length articles)))
6223       ;; Retrieve the headers and read them in.
6224       (setq gnus-newsgroup-headers 
6225             (if (eq 'nov (setq gnus-headers-retrieved-by
6226                                (gnus-retrieve-headers 
6227                                 (if (and gnus-fetch-old-headers 
6228                                          (not (eq 1 (car articles))))
6229                                     (cons 1 articles)
6230                                   articles)
6231                                 gnus-newsgroup-name)))
6232                 (progn
6233                   (gnus-get-newsgroup-headers-xover articles))
6234               ;; If we were to fetch old headers, but the backend didn't
6235               ;; support XOVER, then it is possible we fetched one article
6236               ;; that we shouldn't have. If that's the case, we pop it off the
6237               ;; list of headers.
6238               (if (not gnus-fetch-old-headers)
6239                   ()
6240                 (save-excursion
6241                   (set-buffer nntp-server-buffer)
6242                   (goto-char (point-min))
6243                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6244                        (delete-region 
6245                         (point) 
6246                         (search-forward "\n.\n" nil t)))))
6247               (gnus-get-newsgroup-headers)))
6248       ;; Remove canceled articles from the list of unread articles.
6249       (setq gnus-newsgroup-unreads
6250             (gnus-set-sorted-intersection 
6251              gnus-newsgroup-unreads
6252              (mapcar (lambda (headers) (header-number headers))
6253                      gnus-newsgroup-headers)))
6254       ;; Adjust and set lists of article marks.
6255       (and info
6256            (let (marked)
6257              (gnus-adjust-marked-articles info)
6258              (setq gnus-newsgroup-marked 
6259                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6260              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6261              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6262              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6263              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6264              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6265              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6266              (setq gnus-newsgroup-processable nil)))
6267       ;; Check whether auto-expire is to be done in this group.
6268       (setq gnus-newsgroup-auto-expire
6269             (or (and (stringp gnus-auto-expirable-newsgroups)
6270                      (string-match gnus-auto-expirable-newsgroups group))
6271                 (memq 'auto-expire (nth 5 info))))
6272       ;; First and last article in this newsgroup.
6273       (and gnus-newsgroup-headers
6274            (setq gnus-newsgroup-begin 
6275                  (header-number (car gnus-newsgroup-headers)))
6276            (setq gnus-newsgroup-end
6277                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6278       (setq gnus-reffed-article-number -1)
6279       ;; GROUP is successfully selected.
6280       (or gnus-newsgroup-headers t)))))
6281
6282 (defun gnus-articles-to-read (group read-all)
6283   ;; Find out what articles the user wants to read.
6284   (let* ((articles
6285           ;; Select all articles if `read-all' is non-nil, or if all the
6286           ;; unread articles are dormant articles.
6287           (if (or (and read-all (not (numberp read-all)))
6288                   (= (length gnus-newsgroup-unreads) 
6289                      (length gnus-newsgroup-dormant)))
6290               (gnus-uncompress-range 
6291                (gnus-gethash group gnus-active-hashtb))
6292             gnus-newsgroup-unreads))
6293          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6294          (scored (length scored-list))
6295          (number (length articles))
6296          (marked (+ (length gnus-newsgroup-marked)
6297                     (length gnus-newsgroup-dormant)))
6298          (select
6299           (cond 
6300            ((numberp read-all)
6301             read-all)
6302            (t
6303             (condition-case ()
6304                 (cond ((and (or (<= scored marked)
6305                                 (= scored number))
6306                             (numberp gnus-large-newsgroup)
6307                             (> number gnus-large-newsgroup))
6308                        (let ((input
6309                               (read-string
6310                                (format
6311                                 "How many articles from %s (default %d): "
6312                                 gnus-newsgroup-name number))))
6313                          (if (string-match "^[ \t]*$" input)
6314                              number input)))
6315                       ((and (> scored marked) (< scored number))
6316                        (let ((input
6317                               (read-string
6318                                (format 
6319                                 "%s %s (%d scored, %d total): "
6320                                 "How many articles from"
6321                                 group scored number))))
6322                          (if (string-match "^[ \t]*$" input)
6323                              number input)))
6324                       (t number))
6325               (quit nil))))))
6326     (setq select (if (stringp select) (string-to-number select) select))
6327     (if (or (null select) (zerop select))
6328         select
6329       (if (and (not (zerop scored)) (<= (abs select) scored))
6330           (progn
6331             (setq articles (sort scored-list '<))
6332             (setq number (length articles)))
6333         (setq articles (copy-sequence articles)))
6334
6335       (if (< (abs select) number)
6336           (if (< select 0) 
6337               ;; Select the N oldest articles.
6338               (setcdr (nthcdr (1- (abs select)) articles) nil)
6339             ;; Select the N most recent articles.
6340             (setq articles (nthcdr (- number select) articles))))
6341       (setq gnus-newsgroup-unselected
6342             (gnus-sorted-intersection
6343              gnus-newsgroup-unreads
6344              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6345       articles)))
6346
6347 (defun gnus-killed-articles (killed articles)
6348   (let (out)
6349     (while articles
6350       (if (inline (gnus-member-of-range (car articles) killed))
6351           (setq out (cons (car articles) out)))
6352       (setq articles (cdr articles)))
6353     out))
6354
6355 (defun gnus-adjust-marked-articles (info &optional active)
6356   "Remove all marked articles that are no longer legal."
6357   (let ((marked-lists (nth 3 info))
6358         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6359         m prev)
6360     ;; There are many types of marked articles.
6361     (while marked-lists
6362       (setq m (cdr (setq prev (car marked-lists))))
6363       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6364              ;; Make sure that all ticked articles are a subset of the
6365              ;; unread/unselected articles.
6366              (while m
6367                (if (or (memq (car m) gnus-newsgroup-unreads)
6368                        (memq (car m) gnus-newsgroup-unselected))
6369                    (setq prev m)
6370                  (setcdr prev (cdr m)))
6371                (setq m (cdr m))))
6372             ((eq 'score (car prev))
6373              ;; Scored articles should be a subset of
6374              ;; unread/unselected articles. 
6375              (while m
6376                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6377                        (memq (car (car m)) gnus-newsgroup-unreads))
6378                    (setq prev m)
6379                  (setcdr prev (cdr m)))
6380                (setq m (cdr m))))
6381             ((eq 'bookmark (car prev))
6382              ;; Bookmarks should be a subset of active articles.
6383              (while m
6384                (if (< (car (car m)) (car active))
6385                    (setcdr prev (cdr m))
6386                  (setq prev m))
6387                (setq m (cdr m))))
6388             ((eq 'killed (car prev))
6389              ;; Articles that have been through the kill process are
6390              ;; to be a subset of active articles.
6391              (while (and m (< (or (and (numberp (car m)) (car m))
6392                                   (cdr (car m)))
6393                               (car active)))
6394                (setcdr prev (cdr m))
6395                (setq m (cdr m)))
6396              (if (and m (< (or (and (numberp (car m)) (car m))
6397                                (car (car m)))
6398                            (car active))) 
6399                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6400             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6401              ;; The replied and expirable articles have to be articles
6402              ;; that are active. 
6403              (while m
6404                (if (< (car m) (car active))
6405                    (setcdr prev (cdr m))
6406                  (setq prev m))
6407                (setq m (cdr m)))))
6408       (setq marked-lists (cdr marked-lists)))
6409     ;; Remove all lists that are empty.
6410     (setq marked-lists (nth 3 info))
6411     (if marked-lists
6412         (progn
6413           (while (= 1 (length (car marked-lists)))
6414             (setq marked-lists (cdr marked-lists)))
6415           (setq m (cdr (setq prev marked-lists)))
6416           (while m
6417             (if (= 1 (length (car m)))
6418                 (setcdr prev (cdr m))
6419               (setq prev m))
6420             (setq m (cdr m)))
6421           (setcar (nthcdr 3 info) marked-lists)))
6422     ;; Finally, if there are no marked lists at all left, and if there
6423     ;; are no elements after the lists in the info list, we just chop
6424     ;; the info list off before the marked lists.
6425     (and (null marked-lists) 
6426          (not (nthcdr 4 info))
6427          (setcdr (nthcdr 2 info) nil)))
6428   info)
6429
6430 (defun gnus-set-marked-articles 
6431   (info ticked replied expirable killed dormant bookmark score) 
6432   "Enter the various lists of marked articles into the newsgroup info list."
6433   (let (newmarked)
6434     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6435     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6436     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6437                                          newmarked)))
6438     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6439     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6440     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6441                                         newmarked)))
6442     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6443     (if (nthcdr 3 info)
6444         (progn
6445           (setcar (nthcdr 3 info) newmarked)
6446           (and (not newmarked)
6447                (not (nthcdr 4 info))
6448                (setcdr (nthcdr 2 info) nil)))
6449       (if newmarked
6450           (setcdr (nthcdr 2 info) (list newmarked))))))
6451
6452 (defun gnus-add-marked-articles (group type articles &optional info force)
6453   ;; Add ARTICLES of TYPE to the info of GROUP.
6454   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6455   ;; add, but replace marked articles of TYPE with ARTICLES.
6456   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6457         marked m)
6458     (or (not info)
6459         (and (not (setq marked (nthcdr 3 info)))
6460              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6461         (and (not (setq m (assq type (car marked))))
6462              (setcar marked (cons (cons type articles) (car marked))))
6463         (if force
6464             (setcdr m articles)
6465           (nconc m articles)))))
6466          
6467 (defun gnus-set-mode-line (where)
6468   "This function sets the mode line of the article or summary buffers.
6469 If WHERE is `summary', the summary mode line format will be used."
6470   (if (memq where gnus-updated-mode-lines)
6471       (let (mode-string)
6472         (save-excursion
6473           (set-buffer gnus-summary-buffer)
6474           (let* ((mformat (if (eq where 'article) 
6475                               gnus-article-mode-line-format-spec
6476                             gnus-summary-mode-line-format-spec))
6477                  (group-name gnus-newsgroup-name)
6478                  (article-number (or gnus-current-article 0))
6479                  (unread (- (length gnus-newsgroup-unreads)
6480                             (length gnus-newsgroup-dormant)))
6481                  (unread-and-unticked 
6482                   (- unread (length gnus-newsgroup-marked)))
6483                  (unselected (length gnus-newsgroup-unselected))
6484                  (unread-and-unselected
6485                   (cond ((and (zerop unread-and-unticked)
6486                               (zerop unselected)) "")
6487                         ((zerop unselected) 
6488                          (format "{%d more}" unread-and-unticked))
6489                         (t (format "{%d(+%d) more}"
6490                                    unread-and-unticked unselected))))
6491                  (subject
6492                   (if gnus-current-headers
6493                       (header-subject gnus-current-headers) ""))
6494                  (max-len (and gnus-mode-non-string-length
6495                                (- (frame-width) gnus-mode-non-string-length)))
6496                  header) ;; passed as argument to any user-format-funcs
6497             (setq mode-string (eval mformat))
6498             (or (numberp max-len)
6499                 (setq max-len (length mode-string)))
6500             (if (< max-len 4) (setq max-len 4))
6501             (if (> (length mode-string) max-len)
6502                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6503                 ;;  function `substring' might cut on a middle
6504                 ;;  of multi-octet character.
6505                 (setq mode-string 
6506                       (concat (gnus-truncate-string mode-string (- max-len 3))
6507                               "...")))
6508             (setq mode-string (format (format "%%-%ds" max-len)
6509                                       mode-string))))
6510         (setq mode-line-buffer-identification mode-string)
6511         (set-buffer-modified-p t))))
6512
6513 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6514   "Go through the HEADERS list and add all Xrefs to a hash table.
6515 The resulting hash table is returned, or nil if no Xrefs were found."
6516   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6517          (prefix (if (and 
6518                       (gnus-group-foreign-p from-newsgroup)
6519                       (not (memq 'virtual 
6520                                  (assoc (symbol-name (car from-method))
6521                                         gnus-valid-select-methods))))
6522                      (gnus-group-real-prefix from-newsgroup)))
6523          (xref-hashtb (make-vector 63 0))
6524          start group entry number xrefs header)
6525     (while headers
6526       (setq header (car headers))
6527       (if (and (setq xrefs (header-xref header))
6528                (not (memq (header-number header) unreads)))
6529           (progn
6530             (setq start 0)
6531             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6532               (setq start (match-end 0))
6533               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6534                                                     (match-end 1))))
6535               (setq number 
6536                     (string-to-int (substring xrefs (match-beginning 2) 
6537                                               (match-end 2))))
6538               (if (setq entry (gnus-gethash group xref-hashtb))
6539                   (setcdr entry (cons number (cdr entry)))
6540                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6541       (setq headers (cdr headers)))
6542     (if start xref-hashtb nil)))
6543
6544 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6545   "Look through all the headers and mark the Xrefs as read."
6546   (let ((virtual (memq 'virtual 
6547                        (assoc (symbol-name (car (gnus-find-method-for-group 
6548                                                  from-newsgroup)))
6549                               gnus-valid-select-methods)))
6550         name entry info xref-hashtb idlist method
6551         nth4)
6552     (save-excursion
6553       (set-buffer gnus-group-buffer)
6554       (if (setq xref-hashtb 
6555                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6556           (mapatoms 
6557            (lambda (group)
6558              (if (string= from-newsgroup (setq name (symbol-name group)))
6559                  ()
6560                (setq idlist (symbol-value group))
6561                ;; Dead groups are not updated.
6562                (if (and (prog1 
6563                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6564                                   info (nth 2 entry))
6565                           (if (stringp (setq nth4 (nth 4 info)))
6566                               (setq nth4 (gnus-server-to-method nth4))))
6567                         ;; Only do the xrefs if the group has the same
6568                         ;; select method as the group we have just read.
6569                         (or (gnus-methods-equal-p 
6570                              nth4 (gnus-find-method-for-group from-newsgroup))
6571                             virtual
6572                             (equal nth4 
6573                                    (setq method (gnus-find-method-for-group 
6574                                                  from-newsgroup)))
6575                             (and (equal (car nth4) (car method))
6576                                  (equal (nth 1 nth4) (nth 1 method))))
6577                         gnus-use-cross-reference
6578                         (or (not (eq gnus-use-cross-reference t))
6579                             virtual
6580                             ;; Only do cross-references on subscribed
6581                             ;; groups, if that is what is wanted.  
6582                             (<= (nth 1 info) gnus-level-subscribed)))
6583                    (gnus-group-make-articles-read name idlist expirable))))
6584            xref-hashtb)))))
6585
6586 (defun gnus-group-make-articles-read (group articles expirable)
6587   (let* ((num 0)
6588          (entry (gnus-gethash group gnus-newsrc-hashtb))
6589          (info (nth 2 entry))
6590          (active (gnus-gethash group gnus-active-hashtb))
6591          exps expirable range)
6592     ;; First peel off all illegal article numbers.
6593     (if active
6594         (let ((ids articles)
6595               (ticked (cdr (assq 'tick (nth 3 info))))
6596               (dormant (cdr (assq 'dormant (nth 3 info))))
6597               id)
6598           (setq exps nil)
6599           (while ids
6600             (setq id (car ids))
6601             (if (or (> id (cdr active))
6602                     (< id (car active))
6603                     (memq id ticked)
6604                     (memq id dormant))
6605                 (setq articles (delq id articles)))
6606             (and (memq id expirable)
6607                  (setq exps (cons id exps)))
6608             (setq ids (cdr ids)))))
6609     ;; Update expirable articles.
6610     (gnus-add-marked-articles nil 'expirable exps info)
6611     (and active
6612          (null (nth 2 info))
6613          (> (car active) 1)
6614          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6615     (setcar (nthcdr 2 info)
6616             (setq range
6617                   (gnus-add-to-range 
6618                    (nth 2 info) 
6619                    (setq articles (sort articles '<)))))
6620     ;; Then we have to re-compute how many unread
6621     ;; articles there are in this group.
6622     (if active
6623         (progn
6624           (cond 
6625            ((not range)
6626             (setq num (- (1+ (cdr active)) (car active))))
6627            ((not (listp (cdr range)))
6628             (setq num (- (cdr active) (- (1+ (cdr range)) 
6629                                          (car range)))))
6630            (t
6631             (while range
6632               (if (numberp (car range))
6633                   (setq num (1+ num))
6634                 (setq num (+ num (- (1+ (cdr (car range)))
6635                                     (car (car range))))))
6636               (setq range (cdr range)))
6637             (setq num (- (cdr active) num))))
6638           ;; Update the number of unread articles.
6639           (setcar 
6640            entry 
6641            (max 0 (- num 
6642                      (length (cdr (assq 'tick (nth 3 info))))
6643                      (length 
6644                       (cdr (assq 'dormant (nth 3 info)))))))
6645           ;; Update the group buffer.
6646           (gnus-group-update-group group t)))))
6647
6648 (defun gnus-methods-equal-p (m1 m2)
6649   (let ((m1 (or m1 gnus-select-method))
6650         (m2 (or m2 gnus-select-method)))
6651     (or (equal m1 m2)
6652         (and (eq (car m1) (car m2))
6653              (or (not (memq 'address (assoc (symbol-name (car m1))
6654                                             gnus-valid-select-methods)))
6655                  (equal (nth 1 m1) (nth 1 m2)))))))
6656
6657 (defsubst gnus-header-value ()
6658   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6659
6660 (defvar gnus-newsgroup-none-id 0)
6661
6662 (defun gnus-get-newsgroup-headers ()
6663   (setq gnus-article-internal-prepare-hook nil)
6664   (let ((cur nntp-server-buffer)
6665         (dependencies gnus-newsgroup-dependencies)
6666         headers id dep end ref)
6667     (save-excursion
6668       (set-buffer nntp-server-buffer)
6669       (goto-char (point-min))
6670       ;; Search to the beginning of the next header. Error messages
6671       ;; do not begin with 2 or 3.
6672       (while (re-search-forward "^[23][0-9]+ " nil t)
6673         (let ((header (make-vector 9 nil))
6674               (case-fold-search t)
6675               (p (point))
6676               in-reply-to)
6677           (setq id nil
6678                 ref nil)
6679           (header-set-number header (read cur))
6680           ;; This implementation of this function, with nine
6681           ;; search-forwards instead of the one re-search-forward and
6682           ;; a case (which basically was the old function) is actually
6683           ;; about twice as fast, even though it looks messier. You
6684           ;; can't have everything, I guess. Speed and elegance
6685           ;; doesn't always come hand in hand.
6686           (save-restriction
6687             (narrow-to-region (point) (or (save-excursion 
6688                                             (search-forward "\n.\n" nil t))
6689                                           (point)))
6690             (if (search-forward "\nfrom: " nil t)
6691                 (header-set-from header (gnus-header-value))
6692               (header-set-from header "(nobody)"))
6693             (goto-char p)
6694             (if (search-forward "\nsubject: " nil t)
6695                 (header-set-subject header (gnus-header-value))
6696               (header-set-subject header "(none)"))
6697             (goto-char p)
6698             (and (search-forward "\nxref: " nil t)
6699                  (header-set-xref header (gnus-header-value)))
6700             (goto-char p)
6701             (or (numberp (and (search-forward "\nlines: " nil t)
6702                               (header-set-lines header (read cur))))
6703                 (header-set-lines header 0))
6704             (goto-char p)
6705             (and (search-forward "\ndate: " nil t)
6706                  (header-set-date header (gnus-header-value)))
6707             (goto-char p)
6708             (if (search-forward "\nmessage-id: " nil t)
6709                 (header-set-id header (setq id (gnus-header-value)))
6710               ;; If there was no message-id, we just fake one to make
6711               ;; subsequent routines simpler.
6712               (header-set-id 
6713                header 
6714                (setq id (concat "none+" 
6715                                 (int-to-string 
6716                                  (setq gnus-newsgroup-none-id 
6717                                        (1+ gnus-newsgroup-none-id)))))))
6718             (goto-char p)
6719             (if (search-forward "\nreferences: " nil t)
6720                 (progn
6721                   (header-set-references header (gnus-header-value))
6722                   (setq end (match-end 0))
6723                   (save-excursion
6724                     (setq ref 
6725                           (downcase
6726                            (buffer-substring
6727                             (progn 
6728                               (end-of-line)
6729                               (search-backward ">" end t)
6730                               (1+ (point)))
6731                             (progn
6732                               (search-backward "<" end t)
6733                               (point)))))))
6734               ;; Get the references from the in-reply-to header if there
6735               ;; ware no references and the in-reply-to header looks
6736               ;; promising. 
6737               (if (and (search-forward "\nin-reply-to: " nil t)
6738                        (setq in-reply-to (gnus-header-value))
6739                        (string-match "<[^>]+>" in-reply-to))
6740                   (progn
6741                     (header-set-references 
6742                      header 
6743                      (setq ref (substring in-reply-to (match-beginning 0)
6744                                           (match-end 0))))
6745                     (setq ref (downcase ref)))
6746                 (setq ref "none")))
6747             ;; We do some threading while we read the headers. The
6748             ;; message-id and the last reference are both entered into
6749             ;; the same hash table. Some tippy-toeing around has to be
6750             ;; done in case an article has arrived before the article
6751             ;; which it refers to.
6752             (if (boundp (setq dep (intern (downcase id) dependencies)))
6753                 (if (car (symbol-value dep))
6754                     ;; An article with this Message-ID has already
6755                     ;; been seen, so we ignore this one, except we add
6756                     ;; any additional Xrefs (in case the two articles
6757                     ;; came from different servers.
6758                     (progn
6759                       (header-set-xref 
6760                        (car (symbol-value dep))
6761                        (concat (or (header-xref (car (symbol-value dep))) "")
6762                                (or (header-xref header) "")))
6763                       (setq header nil))
6764                   (setcar (symbol-value dep) header))
6765               (set dep (list header)))
6766             (if header
6767                 (progn
6768                   (if (boundp (setq dep (intern ref dependencies)))
6769                       (setcdr (symbol-value dep) 
6770                               (cons header (cdr (symbol-value dep))))
6771                     (set dep (list nil header)))
6772                   (setq headers (cons header headers))))
6773             (goto-char (point-max))))))
6774     (nreverse headers)))
6775
6776 ;; The following macros and functions were written by Felix Lee
6777 ;; <flee@cse.psu.edu>. 
6778
6779 (defmacro gnus-nov-read-integer ()
6780   '(prog1
6781        (if (= (following-char) ?\t)
6782            0
6783          (let ((num (condition-case nil (read buffer) (error nil))))
6784            (if (numberp num) num 0)))
6785      (or (eobp) (forward-char 1))))
6786
6787 (defmacro gnus-nov-skip-field ()
6788   '(search-forward "\t" eol 'move))
6789
6790 (defmacro gnus-nov-field ()
6791   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6792
6793 ;; Goes through the xover lines and returns a list of vectors
6794 (defun gnus-get-newsgroup-headers-xover (sequence)
6795   "Parse the news overview data in the server buffer, and return a
6796 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6797   ;; Get the Xref when the users reads the articles since most/some
6798   ;; NNTP servers do not include Xrefs when using XOVER.
6799   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6800   (let ((cur nntp-server-buffer)
6801         (dependencies gnus-newsgroup-dependencies)
6802         number headers header)
6803     (save-excursion
6804       (set-buffer nntp-server-buffer)
6805       (goto-char (point-min))
6806       (while (and sequence (not (eobp)))
6807         (setq number (read cur))
6808         (while (and sequence (< (car sequence) number))
6809           (setq sequence (cdr sequence)))
6810         (and sequence 
6811              (eq number (car sequence))
6812              (progn
6813                (setq sequence (cdr sequence))
6814                (if (setq header 
6815                          (inline (gnus-nov-parse-line number dependencies)))
6816                    (setq headers (cons header headers)))))
6817         (forward-line 1))
6818       (setq headers (nreverse headers)))
6819     headers))
6820
6821 ;; This function has to be called with point after the article number
6822 ;; on the beginning of the line.
6823 (defun gnus-nov-parse-line (number dependencies)
6824   (let ((none 0)
6825         (eol (gnus-point-at-eol)) 
6826         (buffer (current-buffer))
6827         header ref id dep)
6828
6829     ;; overview: [num subject from date id refs chars lines misc]
6830     (narrow-to-region (point) eol)
6831     (forward-char)
6832
6833     (condition-case nil
6834         (setq header
6835               (vector 
6836                number                   ; number
6837                (gnus-nov-field)         ; subject
6838                (gnus-nov-field)         ; from
6839                (gnus-nov-field)         ; date
6840                (setq id (or (gnus-nov-field)
6841                             (concat "none+"
6842                                     (int-to-string 
6843                                      (setq none (1+ none)))))) ; id
6844                (progn
6845                  (save-excursion
6846                    (let ((beg (point)))
6847                      (search-forward "\t" eol)
6848                      (if (search-backward ">" beg t)
6849                          (setq ref 
6850                                (downcase 
6851                                 (buffer-substring 
6852                                  (1+ (point))
6853                                  (progn
6854                                    (search-backward "<" beg t)
6855                                    (point)))))
6856                        (setq ref nil))))
6857                  (gnus-nov-field))      ; refs
6858                (gnus-nov-read-integer)  ; chars
6859                (gnus-nov-read-integer)  ; lines
6860                (if (= (following-char) ?\n)
6861                    nil
6862                  (gnus-nov-field))      ; misc
6863                ))
6864       (error (progn 
6865                (ding)
6866                (message "Strange nov line.")
6867                (setq header nil)
6868                (goto-char eol))))
6869
6870     (widen)
6871
6872     ;; We build the thread tree.
6873     (and header
6874          (if (boundp (setq dep (intern (downcase id) dependencies)))
6875              (if (car (symbol-value dep))
6876                  ;; An article with this Message-ID has already been seen,
6877                  ;; so we ignore this one, except we add any additional
6878                  ;; Xrefs (in case the two articles came from different
6879                  ;; servers.
6880                  (progn
6881                    (header-set-xref 
6882                     (car (symbol-value dep))
6883                     (concat (or (header-xref (car (symbol-value dep))) "")
6884                             (or (header-xref header) "")))
6885                    (setq header nil))
6886                (setcar (symbol-value dep) header))
6887            (set dep (list header))))
6888     (if header
6889         (progn
6890           (if (boundp (setq dep (intern (or ref "none") 
6891                                         dependencies)))
6892               (setcdr (symbol-value dep) 
6893                       (cons header (cdr (symbol-value dep))))
6894             (set dep (list nil header)))))
6895     header))
6896
6897 (defun gnus-article-get-xrefs ()
6898   "Fill in the Xref value in `gnus-current-headers', if necessary.
6899 This is meant to be called in `gnus-article-internal-prepare-hook'."
6900   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6901                                  gnus-current-headers)))
6902     (or (not gnus-use-cross-reference)
6903         (not headers)
6904         (and (header-xref headers)
6905              (not (string= (header-xref headers) "")))
6906         (let ((case-fold-search t)
6907               xref)
6908           (save-restriction
6909             (gnus-narrow-to-headers)
6910             (goto-char (point-min))
6911             (if (or (and (eq (downcase (following-char)) ?x)
6912                          (looking-at "Xref:"))
6913                     (search-forward "\nXref:" nil t))
6914                 (progn
6915                   (goto-char (1+ (match-end 0)))
6916                   (setq xref (buffer-substring (point) 
6917                                                (progn (end-of-line) (point))))
6918                   (header-set-xref headers xref))))))))
6919
6920 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6921 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6922
6923 ;; Return a header specified by a NUMBER.
6924 (defun gnus-get-header-by-number (number)
6925   (save-excursion
6926     (set-buffer gnus-summary-buffer)
6927     (or gnus-newsgroup-headers-hashtb-by-number
6928         (gnus-make-headers-hashtable-by-number))
6929     (gnus-gethash (int-to-string number)
6930                   gnus-newsgroup-headers-hashtb-by-number)))
6931
6932 (defun gnus-make-headers-hashtable-by-number ()
6933   "Make hashtable for the variable gnus-newsgroup-headers by number."
6934   (save-excursion
6935     (set-buffer gnus-summary-buffer)
6936     (let ((headers gnus-newsgroup-headers)
6937           header)
6938       (setq gnus-newsgroup-headers-hashtb-by-number
6939             (gnus-make-hashtable (length headers)))
6940       (while headers
6941         (setq header (car headers))
6942         (gnus-sethash (int-to-string (header-number header))
6943                       header gnus-newsgroup-headers-hashtb-by-number)
6944         (setq headers (cdr headers))))))
6945
6946 (defun gnus-more-header-backward ()
6947   "Find new header backward."
6948   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6949         (artnum gnus-newsgroup-begin)
6950         (header nil))
6951     (while (and (not header)
6952                 (> artnum first))
6953       (setq artnum (1- artnum))
6954       (setq header (gnus-read-header artnum)))
6955     header))
6956
6957 (defun gnus-more-header-forward (&optional backward)
6958   "Find new header forward.
6959 If BACKWARD, find new header backward instead."
6960   (if backward
6961       (gnus-more-header-backward)
6962     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6963           (artnum gnus-newsgroup-end)
6964           (header nil))
6965       (while (and (not header)
6966                   (< artnum last))
6967         (setq artnum (1+ artnum))
6968         (setq header (gnus-read-header artnum)))
6969       header)))
6970
6971 (defun gnus-extend-newsgroup (header &optional backward)
6972   "Extend newsgroup selection with HEADER.
6973 Optional argument BACKWARD means extend toward backward."
6974   (if header
6975       (let ((artnum (header-number header)))
6976         (setq gnus-newsgroup-headers
6977               (if backward
6978                   (cons header gnus-newsgroup-headers)
6979                 (nconc gnus-newsgroup-headers (list header))))
6980         (setq gnus-newsgroup-unselected
6981               (delq artnum gnus-newsgroup-unselected))
6982         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6983         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6984
6985 (defun gnus-summary-work-articles (n)
6986   "Return a list of articles to be worked upon. The prefix argument,
6987 the list of process marked articles, and the current article will be
6988 taken into consideration."
6989   (let (articles)
6990     (if (and n (numberp n))
6991         (let ((backward (< n 0))
6992               (n (abs n)))
6993           (save-excursion
6994             (while (and (> n 0)
6995                         (setq articles (cons (gnus-summary-article-number) 
6996                                              articles))
6997                         (gnus-summary-search-forward nil nil backward))
6998               (setq n (1- n))))
6999           (sort articles (function <)))
7000       (or (reverse gnus-newsgroup-processable)
7001           (list (gnus-summary-article-number))))))
7002
7003 (defun gnus-summary-search-group (&optional backward use-level)
7004   "Search for next unread newsgroup.
7005 If optional argument BACKWARD is non-nil, search backward instead."
7006   (save-excursion
7007     (set-buffer gnus-group-buffer)
7008     (if (gnus-group-search-forward 
7009          backward nil (if use-level (gnus-group-group-level) nil))
7010         (gnus-group-group-name))))
7011
7012 (defun gnus-summary-best-group (&optional exclude-group)
7013   "Find the name of the best unread group.
7014 If EXCLUDE-GROUP, do not go to this group."
7015   (save-excursion
7016     (set-buffer gnus-group-buffer)
7017     (save-excursion
7018       (gnus-group-best-unread-group exclude-group))))
7019
7020 (defun gnus-summary-search-subject (&optional backward unread subject)
7021   "Search for article forward.
7022 If BACKWARD is non-nil, search backward.
7023 If UNREAD is non-nil, only unread articles are selected.
7024 If SUBJECT is non-nil, the article which has the same subject will be
7025 searched for." 
7026   (let ((func (if backward 'previous-single-property-change
7027                 'next-single-property-change))
7028         (beg (point))
7029         (did t)
7030         pos psubject)
7031     (beginning-of-line)
7032     (and gnus-summary-check-current unread
7033          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7034          (setq did nil))
7035     (if (not did)
7036         ()
7037       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7038       (while
7039           (and 
7040            (setq pos (funcall func (point) 'gnus-number))
7041            (goto-char (if backward (1- pos) pos))
7042            (setq did
7043                  (not (and
7044                        (or (not unread)
7045                            (eq (get-text-property (point) 'gnus-mark)
7046                                gnus-unread-mark))
7047                        (or (not subject)
7048                            (and (setq psubject (gnus-summary-subject-string))
7049                                 (gnus-subject-equal subject psubject))))))
7050            (if backward (if (bobp) nil (forward-char -1) t)
7051              (if (eobp) nil (forward-char 1) t)))))
7052     (if did
7053         (progn (goto-char beg) nil)
7054       (prog1
7055           (get-text-property (point) 'gnus-number)
7056         (gnus-summary-position-cursor)))))
7057
7058 (defun gnus-subject-equal (s1 s2)
7059   (cond
7060    ((null gnus-summary-gather-subject-limit)
7061     (equal (gnus-simplify-subject-re s1)
7062            (gnus-simplify-subject-re s2)))
7063    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7064     (equal (gnus-simplify-subject-fuzzy s1)
7065            (gnus-simplify-subject-fuzzy s2)))
7066    ((numberp gnus-summary-gather-subject-limit)
7067     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7068            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7069    (t
7070     (equal s1 s2))))
7071     
7072 (defun gnus-summary-search-forward (&optional unread subject backward)
7073   "Search for article forward.
7074 If UNREAD is non-nil, only unread articles are selected.
7075 If SUBJECT is non-nil, the article which has the same subject will be
7076 searched for. 
7077 If BACKWARD is non-nil, the search will be performed backwards instead."
7078   (gnus-summary-search-subject backward unread subject))
7079
7080 (defun gnus-summary-search-backward (&optional unread subject)
7081   "Search for article backward.
7082 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7083 If 2nd optional argument SUBJECT is non-nil, the article which has
7084 the same subject will be searched for."
7085   (gnus-summary-search-forward unread subject t))
7086
7087 (defun gnus-summary-article-number (&optional number-or-nil)
7088   "The article number of the article on the current line.
7089 If there isn's an article number here, then we return the current
7090 article number."
7091   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7092     (if number-or-nil number (or number gnus-current-article))))
7093
7094 (defun gnus-summary-thread-level ()
7095   "The thread level of the article on the current line."
7096   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7097       0))
7098
7099 (defun gnus-summary-pseudo-article ()
7100   "The thread level of the article on the current line."
7101   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7102
7103 (defun gnus-summary-article-mark ()
7104   "The mark on the current line."
7105   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7106
7107 (defun gnus-summary-subject-string ()
7108   "Return current subject string or nil if nothing."
7109   (let ((article (gnus-summary-article-number))
7110         header)
7111     (and article 
7112          (setq header (gnus-get-header-by-number article))
7113          (vectorp header)
7114          (header-subject header))))
7115
7116 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7117 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7118 (defun gnus-summary-article-score ()
7119   "Return current article score."
7120   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7121       gnus-summary-default-score 0))
7122
7123 (defun gnus-summary-recenter ()
7124   "Center point in the summary window.
7125 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7126 displayed, no centering will be performed." 
7127   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7128   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7129   (let* ((top (cond ((< (window-height) 4) 0)
7130                     ((< (window-height) 7) 1)
7131                     (t 2)))
7132          (height (1- (window-height)))
7133          (bottom (save-excursion (goto-char (point-max))
7134                                  (forward-line (- height))
7135                                  (point)))
7136          (window (get-buffer-window (current-buffer))))
7137     (and 
7138      ;; The user has to want it,
7139      gnus-auto-center-summary 
7140      ;; the article buffer must be displayed,
7141      (get-buffer-window gnus-article-buffer)
7142      ;; Set the window start to either `bottom', which is the biggest
7143      ;; possible valid number, or the second line from the top,
7144      ;; whichever is the least.
7145      (set-window-start
7146       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7147
7148 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7149 (defun gnus-short-group-name (group &optional levels)
7150   "Collapse GROUP name LEVELS."
7151   (let* ((name "") (foreign "") (depth -1) (skip 1)
7152          (levels (or levels
7153                      (progn
7154                        (while (string-match "\\." group skip)
7155                          (setq skip (match-end 0)
7156                                depth (+ depth 1)))
7157                        depth))))
7158     (if (string-match ":" group)
7159         (setq foreign (substring group 0 (match-end 0))
7160               group (substring group (match-end 0))))
7161     (while group
7162       (if (and (string-match "\\." group) (> levels 0))
7163           (setq name (concat name (substring group 0 1))
7164                 group (substring group (match-end 0))
7165                 levels (- levels 1)
7166                 name (concat name "."))
7167         (setq name (concat foreign name group)
7168               group nil)))
7169     name))
7170
7171 (defun gnus-summary-jump-to-group (newsgroup)
7172   "Move point to NEWSGROUP in group mode buffer."
7173   ;; Keep update point of group mode buffer if visible.
7174   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7175       (save-window-excursion
7176         ;; Take care of tree window mode.
7177         (if (get-buffer-window gnus-group-buffer)
7178             (pop-to-buffer gnus-group-buffer))
7179         (gnus-group-jump-to-group newsgroup))
7180     (save-excursion
7181       ;; Take care of tree window mode.
7182       (if (get-buffer-window gnus-group-buffer)
7183           (pop-to-buffer gnus-group-buffer)
7184         (set-buffer gnus-group-buffer))
7185       (gnus-group-jump-to-group newsgroup))))
7186
7187 ;; This function returns a list of article numbers based on the
7188 ;; difference between the ranges of read articles in this group and
7189 ;; the range of active articles.
7190 (defun gnus-list-of-unread-articles (group)
7191   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7192          (active (gnus-gethash group gnus-active-hashtb))
7193          (last (cdr active))
7194          first nlast unread)
7195     ;; If none are read, then all are unread. 
7196     (if (not read)
7197         (setq first (car active))
7198       ;; If the range of read articles is a single range, then the
7199       ;; first unread article is the article after the last read
7200       ;; article. Sounds logical, doesn't it?
7201       (if (not (listp (cdr read)))
7202           (setq first (1+ (cdr read)))
7203         ;; `read' is a list of ranges.
7204         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7205                                 (car (car read)))) 1)
7206             (setq first 1))
7207         (while read
7208           (if first 
7209               (while (< first nlast)
7210                 (setq unread (cons first unread))
7211                 (setq first (1+ first))))
7212           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7213           (setq nlast (if (atom (car (cdr read))) 
7214                           (car (cdr read))
7215                         (car (car (cdr read)))))
7216           (setq read (cdr read)))))
7217     ;; And add the last unread articles.
7218     (while (<= first last)
7219       (setq unread (cons first unread))
7220       (setq first (1+ first)))
7221     ;; Return the list of unread articles.
7222     (nreverse unread)))
7223
7224 (defun gnus-list-of-read-articles (group)
7225   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7226         (active (gnus-gethash group gnus-active-hashtb)))
7227     (and info active
7228          (gnus-sorted-complement 
7229           (gnus-uncompress-range active) 
7230           (gnus-list-of-unread-articles group)))))
7231
7232 ;; Various summary commands
7233
7234 (defun gnus-summary-universal-argument ()
7235   "Perform any operation on all articles marked with the process mark."
7236   (interactive)
7237   (gnus-set-global-variables)
7238   (let ((articles (reverse gnus-newsgroup-processable))
7239         func)
7240     (or articles (error "No articles marked"))
7241     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7242         (error "Undefined key"))
7243     (while articles
7244       (gnus-summary-goto-subject (car articles))
7245       (command-execute func)
7246       (gnus-summary-remove-process-mark (car articles))
7247       (setq articles (cdr articles)))))
7248
7249 (defun gnus-summary-toggle-truncation (arg)
7250   "Toggle truncation of summary lines.
7251 With arg, turn line truncation on iff arg is positive."
7252   (interactive "P")
7253   (setq truncate-lines
7254         (if (null arg) (not truncate-lines)
7255           (> (prefix-numeric-value arg) 0)))
7256   (redraw-display))
7257
7258 (defun gnus-summary-reselect-current-group (all)
7259   "Once exit and then reselect the current newsgroup.
7260 The prefix argument ALL means to select all articles."
7261   (interactive "P")
7262   (gnus-set-global-variables)
7263   (let ((current-subject (gnus-summary-article-number))
7264         (group gnus-newsgroup-name))
7265     (setq gnus-newsgroup-begin nil)
7266     (gnus-summary-exit t)
7267     ;; We have to adjust the point of group mode buffer because the
7268     ;; current point was moved to the next unread newsgroup by
7269     ;; exiting.
7270     (gnus-summary-jump-to-group group)
7271     (gnus-group-read-group all t)
7272     (gnus-summary-goto-subject current-subject)))
7273
7274 (defun gnus-summary-rescan-group (all)
7275   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7276   (interactive "P")
7277   (gnus-set-global-variables)
7278   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7279   (let ((group gnus-newsgroup-name))
7280     (gnus-summary-exit)
7281     (gnus-summary-jump-to-group group)
7282     (save-excursion
7283       (set-buffer gnus-group-buffer)
7284       (gnus-group-get-new-news-this-group 1))
7285     (gnus-summary-jump-to-group group)
7286     (gnus-group-read-group all)))
7287
7288 (defun gnus-summary-update-info ()
7289   (let* ((group gnus-newsgroup-name))
7290     (if gnus-newsgroup-kill-headers
7291         (setq gnus-newsgroup-killed
7292               (gnus-compress-sequence
7293                (nconc
7294                 (gnus-set-sorted-intersection
7295                  (gnus-uncompress-range gnus-newsgroup-killed)
7296                  (setq gnus-newsgroup-unselected
7297                        (sort gnus-newsgroup-unselected '<)))
7298                 (setq gnus-newsgroup-unreads
7299                       (sort gnus-newsgroup-unreads '<))) t)))
7300     (or (listp (cdr gnus-newsgroup-killed))
7301         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7302     (let ((headers gnus-newsgroup-headers))
7303       (gnus-close-group group)
7304       (run-hooks 'gnus-exit-group-hook)
7305       (gnus-update-read-articles 
7306        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7307        gnus-newsgroup-marked
7308        t gnus-newsgroup-replied gnus-newsgroup-expirable
7309        gnus-newsgroup-killed gnus-newsgroup-dormant
7310        gnus-newsgroup-bookmarks 
7311        (and gnus-save-score gnus-newsgroup-scored))
7312       (and gnus-use-cross-reference
7313            (gnus-mark-xrefs-as-read 
7314             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7315       ;; Do adaptive scoring, and possibly save score files.
7316       (and gnus-newsgroup-adaptive
7317            (gnus-score-adaptive))
7318       (and gnus-use-scoring 
7319            (fboundp 'gnus-score-save)
7320            (funcall 'gnus-score-save))
7321       ;; Do not switch windows but change the buffer to work.
7322       (set-buffer gnus-group-buffer)
7323       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7324           (gnus-group-update-group group)))))
7325   
7326 (defun gnus-summary-exit (&optional temporary)
7327   "Exit reading current newsgroup, and then return to group selection mode.
7328 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7329   (interactive)
7330   (gnus-set-global-variables)
7331   (gnus-kill-save-kill-buffer)
7332   (let* ((group gnus-newsgroup-name)
7333          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7334                                                   gnus-newsgroup-name))))
7335          (mode major-mode)
7336          (buf (current-buffer)))
7337     (run-hooks 'gnus-summary-exit-hook)
7338     (gnus-summary-update-info) ; Make all changes in this group permanent.
7339     (set-buffer buf)
7340     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7341     ;; Make sure where I was, and go to next newsgroup.
7342     (set-buffer gnus-group-buffer)
7343     (or quit-config
7344         (progn
7345           (gnus-group-jump-to-group group)
7346           (gnus-group-next-unread-group 1)))
7347     (if temporary
7348         nil                             ;Nothing to do.
7349       ;; We set all buffer-local variables to nil. It is unclear why
7350       ;; this is needed, but if we don't, buffer-local variables are
7351       ;; not garbage-collected, it seems. This would the lead to en
7352       ;; ever-growing Emacs.
7353       (set-buffer buf)
7354       (gnus-summary-clear-local-variables)
7355       ;; We clear the global counterparts of the buffer-local
7356       ;; variables as well, just to be on the safe side.
7357       (gnus-configure-windows 'group)
7358       (gnus-summary-clear-local-variables)
7359       ;; Return to group mode buffer. 
7360       (if (eq mode 'gnus-summary-mode)
7361           (gnus-kill-buffer buf))
7362       (if (get-buffer gnus-article-buffer)
7363           (bury-buffer gnus-article-buffer))
7364       (setq gnus-current-select-method gnus-select-method)
7365       (pop-to-buffer gnus-group-buffer)
7366       (if (not quit-config)
7367           (progn
7368             (gnus-group-jump-to-group group)
7369             (gnus-group-next-unread-group 1))
7370         (if (not (buffer-name (car quit-config)))
7371             (gnus-configure-windows 'group)
7372           (set-buffer (car quit-config))
7373           (and (eq major-mode 'gnus-summary-mode)
7374                (gnus-set-global-variables))
7375           (gnus-configure-windows (cdr quit-config)))))))
7376
7377 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7378 (defun gnus-summary-exit-no-update (&optional no-questions)
7379   "Quit reading current newsgroup without updating read article info."
7380   (interactive)
7381   (gnus-set-global-variables)
7382   (let* ((group gnus-newsgroup-name)
7383          (quit-config (nth 1 (assoc 'quit-config 
7384                                     (gnus-find-method-for-group group)))))
7385     (if (or no-questions
7386             gnus-expert-user
7387             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7388         (progn
7389           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7390           (gnus-close-group group)
7391           (gnus-summary-clear-local-variables)
7392           (set-buffer gnus-group-buffer)
7393           (gnus-summary-clear-local-variables)
7394           ;; Return to group selection mode.
7395           (gnus-configure-windows 'group)
7396           (if (get-buffer gnus-summary-buffer)
7397               (kill-buffer gnus-summary-buffer))
7398           (if (get-buffer gnus-article-buffer)
7399               (bury-buffer gnus-article-buffer))
7400           (if (equal (gnus-group-group-name) group)
7401               (gnus-group-next-unread-group 1))
7402           (if quit-config
7403               (progn
7404                 (if (not (buffer-name (car quit-config)))
7405                     (gnus-configure-windows 'group)
7406                   (set-buffer (car quit-config))
7407                   (and (eq major-mode 'gnus-summary-mode)
7408                        (gnus-set-global-variables))
7409                   (gnus-configure-windows (cdr quit-config)))))))))
7410
7411 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7412 (defun gnus-summary-fetch-faq (group)
7413   "Fetch the FAQ for the current group."
7414   (interactive (list gnus-newsgroup-name))
7415   (let ((gnus-faq-buffer 
7416          (find-file (concat gnus-group-faq-directory 
7417                             (gnus-group-real-name group)))))
7418   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7419
7420 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7421 (defun gnus-summary-describe-group (force)
7422   "Describe the current newsgroup."
7423   (interactive "P")
7424   (gnus-group-describe-group force gnus-newsgroup-name))
7425
7426 (defun gnus-summary-describe-briefly ()
7427   "Describe summary mode commands briefly."
7428   (interactive)
7429   (gnus-message 6
7430     (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")))
7431
7432 ;; Walking around group mode buffer from summary mode.
7433
7434 (defun gnus-summary-next-group (&optional no-article target-group backward)
7435   "Exit current newsgroup and then select next unread newsgroup.
7436 If prefix argument NO-ARTICLE is non-nil, no article is selected
7437 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7438 previous group instead."
7439   (interactive "P")
7440   (gnus-set-global-variables)
7441   (let ((current-group gnus-newsgroup-name)
7442         (current-buffer (current-buffer))
7443         entered)
7444     ;; First we semi-exit this group to update Xrefs and all variables.
7445     ;; We can't do a real exit, because the window conf must remain
7446     ;; the same in case the user is prompted for info, and we don't
7447     ;; want the window conf to change before that...
7448     (gnus-summary-exit t)
7449     (while (not entered)
7450       ;; Then we find what group we are supposed to enter.
7451       (set-buffer gnus-group-buffer)
7452       (gnus-group-jump-to-group current-group)
7453       (setq target-group 
7454             (or target-group        
7455                 (if (eq gnus-keep-same-level 'best) 
7456                     (gnus-summary-best-group gnus-newsgroup-name)
7457                   (gnus-summary-search-group backward gnus-keep-same-level))))
7458       (if (not target-group)
7459           ;; There are no further groups, so we return to the group
7460           ;; buffer.
7461           (progn
7462             (gnus-message 5 "Returning to the group buffer")
7463             (setq entered t)
7464             (set-buffer current-buffer)
7465             (gnus-summary-exit))
7466         ;; We try to enter the target group.
7467         (gnus-group-jump-to-group target-group)
7468         (let ((unreads (gnus-group-group-unread)))
7469           (if (and (or (eq t unreads)
7470                        (and unreads (not (zerop unreads))))
7471                    (gnus-summary-read-group
7472                     target-group nil no-article current-buffer))
7473               (setq entered t)
7474             (setq current-group target-group
7475                   target-group nil)))))))
7476
7477 (defun gnus-summary-next-group-old (&optional no-article group backward)
7478   "Exit current newsgroup and then select next unread newsgroup.
7479 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7480 If BACKWARD, go to previous group instead."
7481   (interactive "P")
7482   (gnus-set-global-variables)
7483   (let ((ingroup gnus-newsgroup-name)
7484         (sumbuf (current-buffer))
7485         num)
7486     (set-buffer gnus-group-buffer)
7487     (if (and group
7488              (or (and (numberp (setq num (car (gnus-gethash
7489                                                group gnus-newsrc-hashtb))))
7490                       (< num 1))
7491                  (null num)))
7492         (progn
7493           (gnus-group-jump-to-group group)
7494           (setq group nil))
7495       (gnus-group-jump-to-group ingroup))
7496     (gnus-summary-search-group backward)
7497     (let ((group (or group (gnus-summary-search-group backward))))
7498       (set-buffer sumbuf)
7499       (gnus-summary-exit t)             ;Update all information.
7500       (if (null group)
7501           (gnus-summary-exit-no-update t)
7502         (gnus-group-jump-to-group ingroup)
7503         (setq group (gnus-summary-search-group backward))
7504         (gnus-message 5 "Selecting %s..." group)
7505         (set-buffer gnus-group-buffer)
7506         ;; We are now in group mode buffer.
7507         ;; Make sure group mode buffer point is on GROUP.
7508         (gnus-group-jump-to-group group)
7509         (if (not (eq gnus-auto-select-next 'quietly))
7510             (progn
7511               (gnus-summary-read-group group nil no-article sumbuf)
7512               (and (string= gnus-newsgroup-name ingroup)
7513                    (bufferp sumbuf) (buffer-name sumbuf)
7514                    (progn
7515                      (set-buffer (setq gnus-summary-buffer sumbuf))
7516                      (gnus-summary-exit-no-update t))))
7517           (let ((prevgroup group))
7518             (gnus-group-jump-to-group ingroup)
7519             (setq group (gnus-summary-search-group backward))
7520             (gnus-summary-read-group group nil no-article sumbuf)
7521             (while (and (string= gnus-newsgroup-name ingroup)
7522                         (bufferp sumbuf) 
7523                         (buffer-name sumbuf)
7524                         (not (string= prevgroup (gnus-group-group-name))))
7525               (set-buffer gnus-group-buffer)
7526               (gnus-summary-read-group 
7527                (setq prevgroup (gnus-group-group-name)) 
7528                nil no-article sumbuf))
7529             (and (string= prevgroup (gnus-group-group-name))
7530                  ;; We have reached the final group in the group
7531                  ;; buffer.
7532                  (progn
7533                    (if (buffer-name sumbuf)
7534                        (progn
7535                          (set-buffer sumbuf)
7536                          (gnus-summary-exit)))))))))))
7537
7538 (defun gnus-summary-prev-group (no-article)
7539   "Exit current newsgroup and then select previous unread newsgroup.
7540 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7541   (interactive "P")
7542   (gnus-summary-next-group no-article nil t))
7543
7544 ;; Walking around summary lines.
7545
7546 (defun gnus-summary-first-subject (unread)
7547   "Go to the first unread subject.
7548 If UNREAD is non-nil, go to the first unread article.
7549 Returns nil if there are no unread articles."
7550   (interactive "P")
7551   (prog1
7552       (cond ((not unread)
7553              (goto-char (point-min)))
7554             ((gnus-goto-char 
7555               (text-property-any 
7556                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7557              t)
7558             (t 
7559              ;; There are no unread articles.
7560              (gnus-message 3 "No more unread articles")
7561              nil))
7562     (gnus-summary-position-cursor)))
7563
7564 (defun gnus-summary-next-subject (n &optional unread dont-display)
7565   "Go to next N'th summary line.
7566 If N is negative, go to the previous N'th subject line.
7567 If UNREAD is non-nil, only unread articles are selected.
7568 The difference between N and the actual number of steps taken is
7569 returned."
7570   (interactive "p")
7571   (let ((backward (< n 0))
7572         (n (abs n)))
7573     (while (and (> n 0)
7574                 (gnus-summary-search-forward unread nil backward))
7575       (setq n (1- n)))
7576     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7577                                (if unread " unread" "")))
7578     (or dont-display
7579         (progn
7580           (gnus-summary-recenter)
7581           (gnus-summary-position-cursor)))
7582   n))
7583
7584 (defun gnus-summary-next-unread-subject (n)
7585   "Go to next N'th unread summary line."
7586   (interactive "p")
7587   (gnus-summary-next-subject n t))
7588
7589 (defun gnus-summary-prev-subject (n &optional unread)
7590   "Go to previous N'th summary line.
7591 If optional argument UNREAD is non-nil, only unread article is selected."
7592   (interactive "p")
7593   (gnus-summary-next-subject (- n) unread))
7594
7595 (defun gnus-summary-prev-unread-subject (n)
7596   "Go to previous N'th unread summary line."
7597   (interactive "p")
7598   (gnus-summary-next-subject (- n) t))
7599
7600 (defun gnus-summary-goto-subject (article)
7601   "Go the subject line of ARTICLE."
7602   (interactive
7603    (list
7604     (string-to-int
7605      (completing-read "Article number: "
7606                       (mapcar
7607                        (lambda (headers)
7608                          (list
7609                           (int-to-string (header-number headers))))
7610                        gnus-newsgroup-headers)
7611                       nil 'require-match))))
7612   (or article (error "No article number"))
7613   (let ((b (point)))
7614     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7615                                                 'gnus-number article)))
7616         ()
7617       (gnus-summary-show-thread)
7618       ;; Skip dummy articles. 
7619       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7620           (forward-line 1))
7621       (prog1
7622           (if (not (eobp))
7623               article
7624             (goto-char b)
7625             nil)
7626         (gnus-summary-position-cursor)))))
7627
7628 ;; Walking around summary lines with displaying articles.
7629
7630 (defun gnus-summary-expand-window ()
7631   "Make the summary buffer take up the entire Emacs frame."
7632   (interactive)
7633   (gnus-set-global-variables)
7634   (gnus-configure-windows 'summary))
7635
7636 (defun gnus-summary-display-article (article &optional all-header)
7637   "Display ARTICLE in article buffer."
7638   (gnus-set-global-variables)
7639   (if (null article)
7640       nil
7641     (prog1
7642         (gnus-article-prepare article all-header)
7643       (gnus-summary-show-thread)
7644       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7645           (progn
7646             (forward-line 1)
7647             (gnus-summary-position-cursor)))
7648       (run-hooks 'gnus-select-article-hook)
7649       (gnus-summary-recenter)
7650       (gnus-summary-goto-subject article)
7651       ;; Successfully display article.
7652       (gnus-summary-update-line)
7653       (gnus-article-set-window-start 
7654        (cdr (assq article gnus-newsgroup-bookmarks)))
7655       t)))
7656
7657 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7658   "Select the current article.
7659 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7660 non-nil, the article will be re-fetched even if it already present in
7661 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7662 be displayed."
7663   (and (not pseudo) (gnus-summary-pseudo-article)
7664        (error "This is a pseudo-article."))
7665   (let ((article (or article (gnus-summary-article-number)))
7666         (all-headers (not (not all-headers))) ;Must be T or NIL.
7667         did) 
7668     (prog1
7669         (save-excursion
7670           (set-buffer gnus-summary-buffer)
7671           (if (or (null gnus-current-article)
7672                   (null gnus-article-current)
7673                   (null (get-buffer gnus-article-buffer))
7674                   (not (eq article (cdr gnus-article-current)))
7675                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7676                   force)
7677               ;; The requested article is different from the current article.
7678               (progn
7679                 (gnus-summary-display-article article all-headers)
7680                 (setq did article))
7681             (if (or all-headers gnus-show-all-headers) 
7682                 (gnus-article-show-all-headers))
7683             nil))
7684       (if did 
7685           (gnus-article-set-window-start 
7686            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7687
7688 (defun gnus-summary-set-current-mark (&optional current-mark)
7689   "Obsolete function."
7690   nil)
7691
7692 (defun gnus-summary-next-article (unread &optional subject backward)
7693   "Select the next article.
7694 If UNREAD, only unread articles are selected.
7695 If SUBJECT, only articles with SUBJECT are selected.
7696 If BACKWARD, the previous article is selected instead of the next."
7697   (interactive "P")
7698   (gnus-set-global-variables)
7699   (let (header)
7700     (cond
7701      ;; Is there such an article?
7702      ((gnus-summary-display-article 
7703        (gnus-summary-search-forward unread subject backward))
7704       (gnus-summary-position-cursor))
7705      ;; If not, we try the first unread, if that is wanted.
7706      ((and subject
7707            gnus-auto-select-same
7708            (gnus-summary-first-unread-article))
7709       (gnus-message 6 "Wrapped"))
7710      ;; Try to get next/previous article not displayed in this group.
7711      ((and gnus-auto-extend-newsgroup
7712            (not unread) (not subject)
7713            (setq header (gnus-more-header-forward backward)))
7714       (gnus-extend-newsgroup header backward)
7715       (let ((buffer-read-only nil))
7716         (goto-char (if backward (point-min) (point-max)))
7717         (gnus-summary-prepare-threads (list header) 0))
7718       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7719                                    gnus-newsgroup-end)))
7720      ;; Go to next/previous group.
7721      (t
7722       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7723           (gnus-summary-jump-to-group gnus-newsgroup-name))
7724       (let ((cmd (aref (this-command-keys) 0))
7725             (group 
7726              (if (eq gnus-keep-same-level 'best) 
7727                  (gnus-summary-best-group gnus-newsgroup-name)
7728                (gnus-summary-search-group backward gnus-keep-same-level))))
7729         ;; For some reason, the group window gets selected. We change
7730         ;; it back.  
7731         (select-window (get-buffer-window (current-buffer)))
7732         ;; Keep just the event type of CMD.
7733         (and (listp cmd) (setq cmd (car cmd)))
7734         ;; Select next unread newsgroup automagically.
7735         (cond 
7736          ((not gnus-auto-select-next)
7737           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7738          ((eq gnus-auto-select-next 'quietly)
7739           ;; Select quietly.
7740           (if (assoc 'quit-config (gnus-find-method-for-group 
7741                                    gnus-newsgroup-name))
7742               (gnus-summary-exit)
7743             (gnus-message 7 "No more%s articles (%s)..."
7744                           (if unread " unread" "") 
7745                           (if group (concat "selecting " group)
7746                             "exiting"))
7747             (gnus-summary-next-group nil group backward)))
7748          (t
7749           (let ((keystrokes '(?\C-n ?\C-p))
7750                 key)
7751             (while (or (null key) (memq key keystrokes))
7752               (gnus-message 
7753                7 "No more%s articles%s" (if unread " unread" "")
7754                (if (and group (not (assoc 'quit-config
7755                                           (gnus-find-method-for-group 
7756                                            gnus-newsgroup-name))))
7757                    (format " (Type %s for %s [%s])"
7758                            (single-key-description cmd) group
7759                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7760                  (format " (Type %s to exit %s)"
7761                          (single-key-description cmd)
7762                          gnus-newsgroup-name)))
7763               ;; Confirm auto selection.
7764               (let* ((event (read-event)))
7765                 (setq key (if (listp event) (car event) event))
7766                 (if (memq key keystrokes)
7767                     (let ((obuf (current-buffer)))
7768                       (switch-to-buffer gnus-group-buffer)
7769                       (gnus-group-jump-to-group group)
7770                       (execute-kbd-macro (char-to-string key))
7771                       (setq group (gnus-group-group-name))
7772                       (switch-to-buffer obuf)))))
7773             (if (equal key cmd)
7774                 (if (or (not group) (assoc 'quit-config
7775                                            (gnus-find-method-for-group
7776                                             gnus-newsgroup-name)))
7777                     (gnus-summary-exit)
7778                   (gnus-summary-next-group nil group backward))
7779               (setq unread-command-events (list key)))))))))))
7780
7781 (defun gnus-summary-next-unread-article ()
7782   "Select unread article after current one."
7783   (interactive)
7784   (gnus-summary-next-article t (and gnus-auto-select-same
7785                                     (gnus-summary-subject-string))))
7786
7787 (defun gnus-summary-prev-article (unread &optional subject)
7788   "Select the article after the current one.
7789 If UNREAD is non-nil, only unread articles are selected."
7790   (interactive "P")
7791   (gnus-summary-next-article unread subject t))
7792
7793 (defun gnus-summary-prev-unread-article ()
7794   "Select unred article before current one."
7795   (interactive)
7796   (gnus-summary-prev-article t (and gnus-auto-select-same
7797                                     (gnus-summary-subject-string))))
7798
7799 (defun gnus-summary-next-page (lines &optional circular)
7800   "Show next page of selected article.
7801 If end of article, select next article.
7802 Argument LINES specifies lines to be scrolled up.
7803 If CIRCULAR is non-nil, go to the start of the article instead of 
7804 instead of selecting the next article when reaching the end of the
7805 current article." 
7806   (interactive "P")
7807   (setq gnus-summary-buffer (current-buffer))
7808   (gnus-set-global-variables)
7809   (let ((article (gnus-summary-article-number))
7810         (endp nil))
7811     (gnus-configure-windows 'article)
7812     (if (or (null gnus-current-article)
7813             (null gnus-article-current)
7814             (/= article (cdr gnus-article-current))
7815             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7816         ;; Selected subject is different from current article's.
7817         (gnus-summary-display-article article)
7818       (gnus-eval-in-buffer-window
7819        gnus-article-buffer
7820        (setq endp (gnus-article-next-page lines)))
7821       (if endp
7822           (cond (circular
7823                  (gnus-summary-beginning-of-article))
7824                 (lines
7825                  (gnus-message 3 "End of message"))
7826                 ((null lines)
7827                  (gnus-summary-next-unread-article)))))
7828     (gnus-summary-recenter)
7829     (gnus-summary-position-cursor)))
7830
7831 (defun gnus-summary-prev-page (lines)
7832   "Show previous page of selected article.
7833 Argument LINES specifies lines to be scrolled down."
7834   (interactive "P")
7835   (gnus-set-global-variables)
7836   (let ((article (gnus-summary-article-number)))
7837     (gnus-configure-windows 'article)
7838     (if (or (null gnus-current-article)
7839             (null gnus-article-current)
7840             (/= article (cdr gnus-article-current))
7841             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7842         ;; Selected subject is different from current article's.
7843         (gnus-summary-display-article article)
7844       (gnus-summary-recenter)
7845       (gnus-eval-in-buffer-window gnus-article-buffer
7846         (gnus-article-prev-page lines))))
7847   (gnus-summary-position-cursor))
7848
7849 (defun gnus-summary-scroll-up (lines)
7850   "Scroll up (or down) one line current article.
7851 Argument LINES specifies lines to be scrolled up (or down if negative)."
7852   (interactive "p")
7853   (gnus-set-global-variables)
7854   (gnus-configure-windows 'article)
7855   (or (gnus-summary-select-article nil nil 'pseudo)
7856       (gnus-eval-in-buffer-window 
7857        gnus-article-buffer
7858        (cond ((> lines 0)
7859               (if (gnus-article-next-page lines)
7860                   (gnus-message 3 "End of message")))
7861              ((< lines 0)
7862               (gnus-article-prev-page (- lines))))))
7863   (gnus-summary-recenter)
7864   (gnus-summary-position-cursor))
7865
7866 (defun gnus-summary-next-same-subject ()
7867   "Select next article which has the same subject as current one."
7868   (interactive)
7869   (gnus-set-global-variables)
7870   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7871
7872 (defun gnus-summary-prev-same-subject ()
7873   "Select previous article which has the same subject as current one."
7874   (interactive)
7875   (gnus-set-global-variables)
7876   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7877
7878 (defun gnus-summary-next-unread-same-subject ()
7879   "Select next unread article which has the same subject as current one."
7880   (interactive)
7881   (gnus-set-global-variables)
7882   (gnus-summary-next-article t (gnus-summary-subject-string)))
7883
7884 (defun gnus-summary-prev-unread-same-subject ()
7885   "Select previous unread article which has the same subject as current one."
7886   (interactive)
7887   (gnus-set-global-variables)
7888   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7889
7890 (defun gnus-summary-first-unread-article ()
7891   "Select the first unread article. 
7892 Return nil if there are no unread articles."
7893   (interactive)
7894   (gnus-set-global-variables)
7895   (prog1
7896       (if (gnus-summary-first-subject t)
7897           (gnus-summary-display-article (gnus-summary-article-number)))
7898     (gnus-summary-position-cursor)))
7899
7900 (defun gnus-summary-best-unread-article ()
7901   "Select the unread article with the highest score."
7902   (interactive)
7903   (gnus-set-global-variables)
7904   (let ((scored gnus-newsgroup-scored)
7905         (best -1000000)
7906         article art)
7907     (while scored
7908       (or (> best (cdr (car scored)))
7909           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7910                (not (memq art gnus-newsgroup-marked))
7911                (not (memq art gnus-newsgroup-dormant))
7912                (if (= best (cdr (car scored)))
7913                    (setq article (min art article))
7914                  (setq article art)
7915                  (setq best (cdr (car scored))))))
7916       (setq scored (cdr scored)))
7917     (if article 
7918         (gnus-summary-goto-article article)
7919       (gnus-summary-first-unread-article))
7920     (gnus-summary-position-cursor)))
7921
7922 (defun gnus-summary-goto-article (article &optional all-headers)
7923   "Fetch ARTICLE and display it if it exists.
7924 If ALL-HEADERS is non-nil, no header lines are hidden."
7925   (interactive
7926    (list
7927     (string-to-int
7928      (completing-read 
7929       "Article number: "
7930       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7931               gnus-newsgroup-headers) 
7932       nil 'require-match))))
7933   (prog1
7934       (and (gnus-summary-goto-subject article)
7935            (gnus-summary-display-article article all-headers))
7936     (gnus-summary-position-cursor)))
7937
7938 (defun gnus-summary-goto-last-article ()
7939   "Go to the previously read article."
7940   (interactive)
7941   (prog1
7942       (and gnus-last-article
7943            (gnus-summary-goto-article gnus-last-article))
7944     (gnus-summary-position-cursor)))
7945
7946 (defun gnus-summary-pop-article (number)
7947   "Pop one article off the history and go to the previous.
7948 NUMBER articles will be popped off."
7949   (interactive "p")
7950   (let (to)
7951     (setq gnus-newsgroup-history
7952           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7953     (if to
7954         (gnus-summary-goto-article (car to))
7955       (error "Article history empty")))
7956   (gnus-summary-position-cursor))
7957
7958 ;; Summary article oriented commands
7959
7960 (defun gnus-summary-refer-parent-article (n)
7961   "Refer parent article N times.
7962 The difference between N and the number of articles fetched is returned."
7963   (interactive "p")
7964   (gnus-set-global-variables)
7965   (while 
7966       (and 
7967        (> n 0)
7968        (let ((ref (header-references (gnus-get-header-by-number
7969                                       (gnus-summary-article-number)))))
7970          (if (and ref (not (equal ref ""))
7971                   (string-match "<[^<>]*>[ \t]*$" ref))
7972              (gnus-summary-refer-article 
7973               (substring ref (match-beginning 0) (match-end 0)))
7974            (gnus-message 1 "No references in article %d"
7975                          (gnus-summary-article-number))
7976            nil)))
7977     (setq n (1- n)))
7978   (gnus-summary-position-cursor)
7979   n)
7980     
7981 (defun gnus-summary-refer-article (message-id)
7982   "Refer article specified by MESSAGE-ID.
7983 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7984   (interactive "sMessage-ID: ")
7985   (if (or (not (stringp message-id))
7986           (zerop (length message-id)))
7987       ()
7988     ;; Construct the correct Message-ID if necessary.
7989     ;; Suggested by tale@pawl.rpi.edu.
7990     (or (string-match "^<" message-id)
7991         (setq message-id (concat "<" message-id)))
7992     (or (string-match ">$" message-id)
7993         (setq message-id (concat message-id ">")))
7994     (let ((header (car (gnus-gethash (downcase message-id)
7995                                      gnus-newsgroup-dependencies))))
7996       (if header
7997           (or (gnus-summary-goto-article (header-number header))
7998               ;; The header has been read, but the article had been
7999               ;; expunged, so we insert it again.
8000               (progn
8001                 (gnus-summary-insert-line
8002                  nil header 0 nil gnus-read-mark nil nil
8003                  (header-subject header))
8004                 (forward-line -1)
8005                 (header-number header)))
8006         (let ((gnus-override-method gnus-refer-article-method)
8007               (gnus-ancient-mark gnus-read-mark)
8008               (tmp-point (window-start
8009                           (get-buffer-window gnus-article-buffer)))
8010               number tmp-buf)
8011           (and gnus-refer-article-method
8012                (or (gnus-server-opened gnus-refer-article-method)
8013                    (gnus-open-server gnus-refer-article-method)))
8014           ;; Save the old article buffer.
8015           (save-excursion
8016             (set-buffer gnus-article-buffer)
8017             (gnus-kill-buffer " *temp Article*")
8018             (setq tmp-buf (rename-buffer " *temp Article*")))
8019           (prog1
8020               (if (gnus-article-prepare 
8021                    message-id nil (gnus-read-header message-id))
8022                   (progn
8023                     (setq number (header-number gnus-current-headers))
8024                     (gnus-rebuild-thread message-id)
8025                     (gnus-summary-goto-subject number)
8026                     (gnus-summary-recenter)
8027                     (gnus-article-set-window-start 
8028                      (cdr (assq number gnus-newsgroup-bookmarks)))
8029                     message-id)
8030                 ;; We restore the old article buffer.
8031                 (save-excursion
8032                   (kill-buffer gnus-article-buffer)
8033                   (set-buffer tmp-buf)
8034                   (rename-buffer gnus-article-buffer)
8035                   (let ((buffer-read-only nil))
8036                     (and tmp-point
8037                          (set-window-start (get-buffer-window (current-buffer))
8038                                            tmp-point)))))))))))
8039
8040 (defun gnus-summary-enter-digest-group ()
8041   "Enter a digest group based on the current article."
8042   (interactive)
8043   (gnus-set-global-variables)
8044   (gnus-summary-select-article)
8045   ;; We do not want a narrowed article.
8046   (gnus-summary-stop-page-breaking)
8047   (let ((name (format "%s-%d" 
8048                       (gnus-group-prefixed-name 
8049                        gnus-newsgroup-name (list 'nndoc "")) 
8050                       gnus-current-article))
8051         (ogroup gnus-newsgroup-name)
8052         (buf (current-buffer)))
8053     (if (gnus-group-read-ephemeral-group 
8054          name (list 'nndoc name
8055                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8056                     '(nndoc-article-type digest))
8057          t)
8058         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8059                 (list (list (cons 'to-group ogroup))))
8060       (switch-to-buffer buf)
8061       (gnus-set-global-variables)
8062       (gnus-configure-windows 'summary)
8063       (gnus-message 3 "Article not a digest?"))))
8064
8065 (defun gnus-summary-isearch-article ()
8066   "Do incremental search forward on current article."
8067   (interactive)
8068   (gnus-set-global-variables)
8069   (gnus-summary-select-article)
8070   (gnus-eval-in-buffer-window 
8071    gnus-article-buffer (isearch-forward)))
8072
8073 (defun gnus-summary-search-article-forward (regexp &optional backward)
8074   "Search for an article containing REGEXP forward.
8075 If BACKWARD, search backward instead."
8076   (interactive
8077    (list (read-string
8078           (format "Search article %s (regexp%s): "
8079                   (if current-prefix-arg "backward" "forward")
8080                   (if gnus-last-search-regexp
8081                       (concat ", default " gnus-last-search-regexp)
8082                     "")))
8083          current-prefix-arg))
8084   (gnus-set-global-variables)
8085   (if (string-equal regexp "")
8086       (setq regexp (or gnus-last-search-regexp ""))
8087     (setq gnus-last-search-regexp regexp))
8088   (if (gnus-summary-search-article regexp backward)
8089       (gnus-article-set-window-start 
8090        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8091     (error "Search failed: \"%s\"" regexp)))
8092
8093 (defun gnus-summary-search-article-backward (regexp)
8094   "Search for an article containing REGEXP backward."
8095   (interactive
8096    (list (read-string
8097           (format "Search article backward (regexp%s): "
8098                   (if gnus-last-search-regexp
8099                       (concat ", default " gnus-last-search-regexp)
8100                     "")))))
8101   (gnus-summary-search-article-forward regexp 'backward))
8102
8103 (defun gnus-summary-search-article (regexp &optional backward)
8104   "Search for an article containing REGEXP.
8105 Optional argument BACKWARD means do search for backward.
8106 gnus-select-article-hook is not called during the search."
8107   (let ((gnus-select-article-hook nil)  ;Disable hook.
8108         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8109         (re-search
8110          (if backward
8111              (function re-search-backward) (function re-search-forward)))
8112         (found nil)
8113         (last nil))
8114     ;; Hidden thread subtrees must be searched for ,too.
8115     (gnus-summary-show-all-threads)
8116     (if (eobp) (forward-line -1))
8117     ;; First of all, search current article.
8118     ;; We don't want to read article again from NNTP server nor reset
8119     ;; current point.
8120     (gnus-summary-select-article)
8121     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8122     (setq last gnus-current-article)
8123     (gnus-eval-in-buffer-window gnus-article-buffer
8124       (save-restriction
8125         (widen)
8126         ;; Begin search from current point.
8127         (setq found (funcall re-search regexp nil t))))
8128     ;; Then search next articles.
8129     (while (and (not found)
8130                 (gnus-summary-display-article 
8131                  (gnus-summary-search-subject backward nil nil)))
8132       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8133       (gnus-eval-in-buffer-window gnus-article-buffer
8134         (save-restriction
8135           (widen)
8136           (goto-char (if backward (point-max) (point-min)))
8137           (setq found (funcall re-search regexp nil t)))))
8138     (message "")
8139     ;; Adjust article pointer.
8140     (or (eq last gnus-current-article)
8141         (setq gnus-last-article last))
8142     ;; Return T if found such article.
8143     found))
8144
8145 (defun gnus-summary-execute-command (header regexp command &optional backward)
8146   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8147 If HEADER is an empty string (or nil), the match is done on the entire
8148 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8149   (interactive
8150    (list (let ((completion-ignore-case t))
8151            (completing-read 
8152             "Header name: "
8153             (mapcar (lambda (string) (list string))
8154                     '("Number" "Subject" "From" "Lines" "Date"
8155                       "Message-ID" "Xref" "References"))
8156             nil 'require-match))
8157          (read-string "Regexp: ")
8158          (read-key-sequence "Command: ")
8159          current-prefix-arg))
8160   (gnus-set-global-variables)
8161   ;; Hidden thread subtrees must be searched as well.
8162   (gnus-summary-show-all-threads)
8163   ;; We don't want to change current point nor window configuration.
8164   (save-excursion
8165     (save-window-excursion
8166       (gnus-message 6 "Executing %s..." (key-description command))
8167       ;; We'd like to execute COMMAND interactively so as to give arguments.
8168       (gnus-execute header regexp
8169                     (` (lambda ()
8170                          (call-interactively '(, (key-binding command)))))
8171                     backward)
8172       (gnus-message 6 "Executing %s...done" (key-description command)))))
8173
8174 (defun gnus-summary-beginning-of-article ()
8175   "Scroll the article back to the beginning."
8176   (interactive)
8177   (gnus-set-global-variables)
8178   (gnus-summary-select-article)
8179   (gnus-configure-windows 'article)
8180   (gnus-eval-in-buffer-window
8181    gnus-article-buffer
8182    (widen)
8183    (goto-char (point-min))
8184    (and gnus-break-pages (gnus-narrow-to-page))))
8185
8186 (defun gnus-summary-end-of-article ()
8187   "Scroll to the end of the article."
8188   (interactive)
8189   (gnus-set-global-variables)
8190   (gnus-summary-select-article)
8191   (gnus-configure-windows 'article)
8192   (gnus-eval-in-buffer-window 
8193    gnus-article-buffer
8194    (widen)
8195    (goto-char (point-max))
8196    (and gnus-break-pages (gnus-narrow-to-page))))
8197
8198 (defun gnus-summary-show-article (no-refetch)
8199   "Force re-fetching of the current article.
8200 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8201 be performed.  The current article will simply be redisplayed."
8202   (interactive "P")
8203   (gnus-set-global-variables)
8204   (if (not no-refetch)
8205       (gnus-summary-select-article gnus-have-all-headers t)
8206     (or gnus-current-article
8207         (error "There is no current article"))
8208     (gnus-summary-goto-subject gnus-current-article)
8209     (gnus-configure-windows 'article)
8210     (gnus-summary-position-cursor)))
8211
8212 (defun gnus-summary-verbose-headers (arg)
8213   "Toggle permanent full header display.
8214 If ARG is a positive number, turn header display on.
8215 If ARG is a negative number, turn header display off."
8216   (interactive "P")
8217   (gnus-set-global-variables)
8218   (gnus-summary-toggle-header arg)
8219   (setq gnus-have-all-headers
8220         (cond ((or (not (numberp arg))
8221                    (zerop arg))
8222                (not gnus-have-all-headers))
8223               ((natnump arg)
8224                t))))
8225
8226 (defun gnus-summary-toggle-header (arg)
8227   "Show the headers if they are hidden, or hide them if they are shown.
8228 If ARG is a positive number, show the entire header.
8229 If ARG is a negative number, hide the unwanted header lines."
8230   (interactive "P")
8231   (gnus-set-global-variables)
8232   (save-excursion
8233     (set-buffer gnus-article-buffer)
8234     (let ((buffer-read-only nil))
8235       (if (numberp arg) 
8236           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8237                                                 gnus-hidden-properties)
8238             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8239         (if (text-property-any (point-min) (point-max) 'invisible t)
8240             (remove-text-properties (point-min) (point-max)
8241                                     gnus-hidden-properties)
8242           (let ((gnus-have-all-headers nil))
8243             (run-hooks 'gnus-article-display-hook))))
8244       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8245
8246 (defun gnus-summary-show-all-headers ()
8247   "Make all header lines visible."
8248   (interactive)
8249   (gnus-set-global-variables)
8250   (gnus-article-show-all-headers))
8251
8252 (defun gnus-summary-toggle-mime (arg)
8253   "Toggle MIME processing.
8254 If ARG is a positive number, turn MIME processing on."
8255   (interactive "P")
8256   (gnus-set-global-variables)
8257   (setq gnus-show-mime
8258         (if (null arg) (not gnus-show-mime)
8259           (> (prefix-numeric-value arg) 0)))
8260   (gnus-summary-select-article t 'force))
8261
8262 (defun gnus-summary-caesar-message (arg)
8263   "Caesar rotate the current article by 13.
8264 The numerical prefix specifies how manu places to rotate each letter
8265 forward."
8266   (interactive "P")
8267   (gnus-set-global-variables)
8268   (gnus-summary-select-article)
8269   (let ((mail-header-separator ""))
8270     (gnus-eval-in-buffer-window 
8271      gnus-article-buffer
8272      (save-restriction
8273        (widen)
8274        (let ((last (point)))
8275          (news-caesar-buffer-body arg)
8276          (goto-char last)
8277          (recenter 0))))))
8278
8279 (defun gnus-summary-stop-page-breaking ()
8280   "Stop page breaking in the current article."
8281   (interactive)
8282   (gnus-set-global-variables)
8283   (gnus-summary-select-article)
8284   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8285
8286 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8287
8288 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8289   "Move the current article to a different newsgroup.
8290 If N is a positive number, move the N next articles.
8291 If N is a negative number, move the N previous articles.
8292 If N is nil and any articles have been marked with the process mark,
8293 move those articles instead.
8294 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8295 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8296 re-spool using this method.
8297 For this function to work, both the current newsgroup and the
8298 newsgroup that you want to move to have to support the `request-move'
8299 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8300   (interactive "P")
8301   (gnus-set-global-variables)
8302   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8303       (error "The current newsgroup does not support article moving"))
8304   (let ((articles (gnus-summary-work-articles n))
8305         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8306         art-group to-method sel-met)
8307     (if (and (not to-newsgroup) (not select-method))
8308         (setq to-newsgroup
8309               (completing-read 
8310                (format "Where do you want to move %s? %s"
8311                        (if (> (length articles) 1)
8312                            (format "these %d articles" (length articles))
8313                          "this article")
8314                        (if gnus-current-move-group
8315                            (format "(%s default) " gnus-current-move-group)
8316                          ""))
8317                gnus-active-hashtb nil nil prefix)))
8318     (if to-newsgroup
8319         (progn
8320           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8321               (setq to-newsgroup (or gnus-current-move-group "")))
8322           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8323               (gnus-activate-newsgroup to-newsgroup)
8324               (error "No such group: %s" to-newsgroup))
8325           (setq gnus-current-move-group to-newsgroup)))
8326     (setq to-method (if select-method (list select-method "")
8327                       (gnus-find-method-for-group to-newsgroup)))
8328     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8329         (error "%s does not support article copying" (car to-method)))
8330     (or (gnus-server-opened to-method)
8331         (gnus-open-server to-method)
8332         (error "Can't open server %s" (car to-method)))
8333     (gnus-message 6 "Moving to %s: %s..." 
8334                   (or select-method to-newsgroup) articles)
8335     (while articles
8336       (if (setq art-group
8337                 (gnus-request-move-article 
8338                  (car articles)                   ; Article to move
8339                  gnus-newsgroup-name              ; From newsgrouo
8340                  (nth 1 (gnus-find-method-for-group 
8341                          gnus-newsgroup-name))    ; Server
8342                  (list 'gnus-request-accept-article 
8343                        (if select-method
8344                            (list 'quote select-method)
8345                          to-newsgroup)
8346                        (not (cdr articles)))     ; Accept form
8347                  (not (cdr articles))))          ; Only save nov last time
8348           (let* ((buffer-read-only nil)
8349                  (entry 
8350                   (or
8351                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8352                    (gnus-gethash 
8353                     (gnus-group-prefixed-name 
8354                      (car art-group) 
8355                      (if select-method (list select-method "")
8356                        (gnus-find-method-for-group to-newsgroup)))
8357                     gnus-newsrc-hashtb)))
8358                  (info (nth 2 entry))
8359                  (article (car articles)))
8360             (gnus-summary-goto-subject article)
8361             (beginning-of-line)
8362             (delete-region (point)
8363                            (progn (forward-line 1) (point)))
8364             (if (not (memq article gnus-newsgroup-unreads))
8365                 (setcar (cdr (cdr info))
8366                         (gnus-add-to-range (nth 2 info) 
8367                                            (list (cdr art-group)))))
8368             ;; Copy any marks over to the new group.
8369             (let ((marks '((tick . gnus-newsgroup-marked)
8370                            (dormant . gnus-newsgroup-dormant)
8371                            (expire . gnus-newsgroup-expirable)
8372                            (bookmark . gnus-newsgroup-bookmarks)
8373                         ;   (score . gnus-newsgroup-scored)
8374                            (reply . gnus-newsgroup-replied)))
8375                   (to-article (cdr art-group)))
8376               (while marks
8377                 (if (memq article (symbol-value (cdr (car marks))))
8378                     (gnus-add-marked-articles 
8379                      (car info) (car (car marks)) (list to-article) info))
8380                 (setq marks (cdr marks))))
8381             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8382             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8383             (setq gnus-newsgroup-dormant
8384                   (delq article gnus-newsgroup-dormant)))
8385         (gnus-message 1 "Couldn't move article %s" (car articles)))
8386       (gnus-summary-remove-process-mark (car articles))
8387       (setq articles (cdr articles)))))
8388
8389 (defun gnus-summary-respool-article (n &optional respool-method)
8390   "Respool the current article.
8391 The article will be squeezed through the mail spooling process again,
8392 which means that it will be put in some mail newsgroup or other
8393 depending on `nnmail-split-methods'.
8394 If N is a positive number, respool the N next articles.
8395 If N is a negative number, respool the N previous articles.
8396 If N is nil and any articles have been marked with the process mark,
8397 respool those articles instead.
8398
8399 Respooling can be done both from mail groups and \"real\" newsgroups.
8400 In the former case, the articles in question will be moved from the
8401 current group into whatever groups they are destined to.  In the
8402 latter case, they will be copied into the relevant groups."
8403   (interactive "P")
8404   (gnus-set-global-variables)
8405   (let ((respool-methods (gnus-methods-using 'respool))
8406         (methname 
8407          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8408     (or respool-method
8409         (setq respool-method
8410               (completing-read
8411                "What method do you want to use when respooling? "
8412                respool-methods nil t methname)))
8413     (or (string= respool-method "")
8414         (if (assoc (symbol-name
8415                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8416                    respool-methods)
8417             (gnus-summary-move-article n nil (intern respool-method))
8418           (gnus-summary-copy-article n nil (intern respool-method))))))
8419
8420 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8421 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8422   "Move the current article to a different newsgroup.
8423 If N is a positive number, move the N next articles.
8424 If N is a negative number, move the N previous articles.
8425 If N is nil and any articles have been marked with the process mark,
8426 move those articles instead.
8427 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8428 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8429 re-spool using this method.
8430 For this function to work, the newsgroup that you want to move to have
8431 to support the `request-move' and `request-accept'
8432 functions. (Ie. mail newsgroups at present.)"
8433   (interactive "P")
8434   (gnus-set-global-variables)
8435   (let ((articles (gnus-summary-work-articles n))
8436         (copy-buf (get-buffer-create "*copy work*"))
8437         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8438         art-group to-method)
8439     (buffer-disable-undo copy-buf)
8440     (if (and (not to-newsgroup) (not select-method))
8441         (setq to-newsgroup
8442               (completing-read 
8443                (format "Where do you want to copy %s? %s"
8444                        (if (> (length articles) 1)
8445                            (format "these %d articles" (length articles))
8446                          "this article")
8447                        (if gnus-current-move-group
8448                            (format "(%s default) " gnus-current-move-group)
8449                          ""))
8450                gnus-active-hashtb nil nil prefix)))
8451     (if to-newsgroup
8452         (progn
8453           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8454               (setq to-newsgroup (or gnus-current-move-group "")))
8455           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8456               (gnus-activate-newsgroup to-newsgroup)
8457               (error "No such group: %s" to-newsgroup))
8458           (setq gnus-current-move-group to-newsgroup)))
8459     (setq to-method (if select-method (list select-method "")
8460                       (gnus-find-method-for-group to-newsgroup)))
8461     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8462         (error "%s does not support article copying" (car to-method)))
8463     (or (gnus-server-opened to-method)
8464         (gnus-open-server to-method)
8465         (error "Can't open server %s" (car to-method)))
8466     (gnus-message 6 "Copying to %s: %s..." 
8467                   (or select-method to-newsgroup) articles)
8468     (while articles
8469       (if (setq art-group
8470                 (save-excursion
8471                   (set-buffer copy-buf)
8472                   (gnus-request-article-this-buffer
8473                    (car articles) gnus-newsgroup-name)
8474                   (gnus-request-accept-article
8475                    (if select-method (quote select-method) to-newsgroup)
8476                    (not (cdr articles)))))
8477           (let* ((entry 
8478                   (or
8479                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8480                    (gnus-gethash 
8481                     (gnus-group-prefixed-name 
8482                      (car art-group) 
8483                      (if select-method (list select-method "")
8484                        (gnus-find-method-for-group to-newsgroup)))
8485                     gnus-newsrc-hashtb)))
8486                  (info (nth 2 entry))
8487                  (article (car articles)))
8488             (if (not (memq article gnus-newsgroup-unreads))
8489                 (setcar (cdr (cdr info))
8490                         (gnus-add-to-range (nth 2 info) 
8491                                            (list (cdr art-group)))))
8492             ;; Copy any marks over to the new group.
8493             (let ((marks '((tick . gnus-newsgroup-marked)
8494                            (dormant . gnus-newsgroup-dormant)
8495                            (expire . gnus-newsgroup-expirable)
8496                            (bookmark . gnus-newsgroup-bookmarks)
8497                         ;   (score . gnus-newsgroup-scored)
8498                            (reply . gnus-newsgroup-replied)))
8499                   (to-article (cdr art-group)))
8500               (while marks
8501                 (if (memq article (symbol-value (cdr (car marks))))
8502                     (gnus-add-marked-articles 
8503                      (car info) (car (car marks)) (list to-article) info))
8504                 (setq marks (cdr marks)))))
8505         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8506       (gnus-summary-remove-process-mark (car articles))
8507       (setq articles (cdr articles)))
8508     (kill-buffer copy-buf)))
8509
8510 (defun gnus-summary-import-article (file)
8511   "Import a random file into a mail newsgroup."
8512   (interactive "fImport file: ")
8513   (let ((group gnus-newsgroup-name)
8514         atts)
8515     (or (gnus-check-backend-function 'request-accept-article group)
8516         (error "%s does not support article importing" group))
8517     (or (file-readable-p file)
8518         (not (file-regular-p file))
8519         (error "Can't read %s" file))
8520     (save-excursion
8521       (set-buffer (get-buffer-create " *import file*"))
8522       (buffer-disable-undo (current-buffer))
8523       (erase-buffer)
8524       (insert-file-contents file)
8525       (goto-char (point-min))
8526       (if (nnheader-article-p)
8527           ()
8528         (setq atts (file-attributes file))
8529         (insert "From: " (read-string "From: ") "\n"
8530                 "Subject: " (read-string "Subject: ") "\n"
8531                 "Date: " (current-time-string (nth 5 atts)) "\n"
8532                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8533       (gnus-request-accept-article group t)
8534       (kill-buffer (current-buffer)))))
8535
8536 (defun gnus-summary-expire-articles ()
8537   "Expire all articles that are marked as expirable in the current group."
8538   (interactive)
8539   (if (and gnus-newsgroup-expirable
8540            (gnus-check-backend-function 
8541             'request-expire-articles gnus-newsgroup-name))
8542       (let ((expirable gnus-newsgroup-expirable))
8543         (gnus-message 6 "Expiring articles...")
8544         ;; The list of articles that weren't expired is returned.
8545         (setq gnus-newsgroup-expirable 
8546               (gnus-request-expire-articles gnus-newsgroup-expirable
8547                                             gnus-newsgroup-name))
8548         ;; We go through the old list of expirable, and mark all
8549         ;; really expired articles as non-existent.
8550         (while expirable
8551           (or (memq (car expirable) gnus-newsgroup-expirable)
8552               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8553           (setq expirable (cdr expirable)))
8554         (gnus-message 6 "Expiring articles...done"))))
8555
8556
8557 (defun gnus-summary-expire-articles-now ()
8558   "Expunge all expirable articles in the current group.
8559 This means that *all* articles that are marked as expirable will be
8560 deleted forever, right now."
8561   (interactive)
8562   (or gnus-expert-user
8563       (gnus-y-or-n-p
8564        "Are you really, really, really sure you want to expunge? ")
8565       (error "Phew!"))
8566   (let ((nnmail-expiry-wait -1)
8567         (nnmail-expiry-wait-function nil))
8568     (gnus-summary-expire-articles)))
8569
8570 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8571 (defun gnus-summary-delete-article (n)
8572   "Delete the N next (mail) articles.
8573 This command actually deletes articles. This is not a marking
8574 command. The article will disappear forever from you life, never to
8575 return. 
8576 If N is negative, delete backwards.
8577 If N is nil and articles have been marked with the process mark,
8578 delete these instead."
8579   (interactive "P")
8580   (or (gnus-check-backend-function 'request-expire-articles 
8581                                    gnus-newsgroup-name)
8582       (error "The current newsgroup does not support article deletion."))
8583   ;; Compute the list of articles to delete.
8584   (let ((articles (gnus-summary-work-articles n))
8585         not-deleted)
8586     (if (and gnus-novice-user
8587              (not (gnus-y-or-n-p 
8588                    (format "Do you really want to delete %s forever? "
8589                            (if (> (length articles) 1) "these articles"
8590                              "this article")))))
8591         ()
8592       ;; Delete the articles.
8593       (setq not-deleted (gnus-request-expire-articles 
8594                          articles gnus-newsgroup-name 'force))
8595       (while articles
8596         (gnus-summary-remove-process-mark (car articles))       
8597         ;; The backend might not have been able to delete the article
8598         ;; after all.  
8599         (or (memq (car articles) not-deleted)
8600             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8601         (setq articles (cdr articles))))
8602     (gnus-summary-position-cursor)
8603     not-deleted))
8604
8605 (defun gnus-summary-edit-article ()
8606   "Enter into a buffer and edit the current article.
8607 This will have permanent effect only in mail groups."
8608   (interactive)
8609   (or (gnus-check-backend-function 
8610        'request-replace-article gnus-newsgroup-name)
8611       (error "The current newsgroup does not support article editing."))
8612   (gnus-summary-select-article t)
8613   (gnus-configure-windows 'article)
8614   (select-window (get-buffer-window gnus-article-buffer))
8615   (gnus-message 6 "C-c C-c to end edits")
8616   (setq buffer-read-only nil)
8617   (text-mode)
8618   (use-local-map (copy-keymap (current-local-map)))
8619   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8620   (buffer-enable-undo)
8621   (goto-char (point-min))
8622   (search-forward "\n\n" nil t))
8623
8624 (defun gnus-summary-edit-article-done ()
8625   "Make edits to the current article permanent."
8626   (interactive)
8627   (if (not (gnus-request-replace-article 
8628             (cdr gnus-article-current) (car gnus-article-current) 
8629             (current-buffer)))
8630       (error "Couldn't replace article.")
8631     (gnus-article-mode)
8632     (use-local-map gnus-article-mode-map)
8633     (setq buffer-read-only t)
8634     (buffer-disable-undo (current-buffer))
8635     (pop-to-buffer gnus-summary-buffer)))      
8636
8637 (defun gnus-summary-fancy-query ()
8638   "Query where the fancy respool algorithm would put this article."
8639   (interactive)
8640   (gnus-summary-select-article)
8641   (save-excursion
8642     (set-buffer gnus-article-buffer)
8643     (save-restriction
8644       (goto-char (point-min))
8645       (search-forward "\n\n")
8646       (narrow-to-region (point-min) (point))
8647       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8648
8649 ;; Summary score commands.
8650
8651 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8652
8653 (defun gnus-summary-raise-score (n)
8654   "Raise the score of the current article by N."
8655   (interactive "p")
8656   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8657
8658 (defun gnus-summary-lower-score (n)
8659   "Lower the score of the current article by N."
8660   (interactive "p")
8661   (gnus-summary-raise-score (- n)))
8662
8663 (defun gnus-summary-set-score (n)
8664   "Set the score of the current article to N."
8665   (interactive "p")
8666   ;; Skip dummy header line.
8667   (save-excursion
8668     (gnus-summary-show-thread)
8669     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8670         (forward-line 1))
8671     (let ((buffer-read-only nil))
8672       ;; Set score.
8673       (gnus-summary-update-mark
8674        (if (= n (or gnus-summary-default-score 0)) ? 
8675          (if (< n (or gnus-summary-default-score 0)) 
8676              gnus-score-below-mark gnus-score-over-mark)) 'score))
8677     (let* ((article (gnus-summary-article-number))
8678            (score (assq article gnus-newsgroup-scored)))
8679       (if score (setcdr score n)
8680         (setq gnus-newsgroup-scored 
8681               (cons (cons article n) gnus-newsgroup-scored))))
8682     (gnus-summary-update-line)))
8683
8684 (defun gnus-summary-current-score ()
8685   "Return the score of the current article."
8686   (interactive)
8687   (message "%s" (gnus-summary-article-score)))
8688
8689 ;; Summary marking commands.
8690
8691 (defun gnus-summary-raise-same-subject-and-select (score)
8692   "Raise articles which has the same subject with SCORE and select the next."
8693   (interactive "p")
8694   (let ((subject (gnus-summary-subject-string)))
8695     (gnus-summary-raise-score score)
8696     (while (gnus-summary-search-subject nil nil subject)
8697       (gnus-summary-raise-score score))
8698     (gnus-summary-next-article t)))
8699
8700 (defun gnus-summary-raise-same-subject (score)
8701   "Raise articles which has the same subject with SCORE."
8702   (interactive "p")
8703   (let ((subject (gnus-summary-subject-string)))
8704     (gnus-summary-raise-score score)
8705     (while (gnus-summary-search-subject nil nil subject)
8706       (gnus-summary-raise-score score))
8707     (gnus-summary-next-subject 1 t)))
8708
8709 (defun gnus-score-default (level)
8710   (if level (prefix-numeric-value level) 
8711     gnus-score-interactive-default-score))
8712
8713 (defun gnus-summary-raise-thread (score)
8714   "Raise the score of the articles in the current thread with SCORE."
8715   (interactive "P")
8716   (setq score (1- (gnus-score-default score)))
8717   (let (e)
8718     (save-excursion
8719       (let ((level (gnus-summary-thread-level)))
8720         (gnus-summary-raise-score score)
8721         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8722                     (> (gnus-summary-thread-level) level))
8723           (gnus-summary-raise-score score))
8724         (setq e (point))))
8725     (let ((gnus-summary-check-current t))
8726       (or (zerop (gnus-summary-next-subject 1 t))
8727           (goto-char e))))
8728   (gnus-summary-recenter)
8729   (gnus-summary-position-cursor)
8730   (gnus-set-mode-line 'summary))
8731
8732 (defun gnus-summary-lower-same-subject-and-select (score)
8733   "Raise articles which has the same subject with SCORE and select the next."
8734   (interactive "p")
8735   (gnus-summary-raise-same-subject-and-select (- score)))
8736
8737 (defun gnus-summary-lower-same-subject (score)
8738   "Raise articles which has the same subject with SCORE."
8739   (interactive "p")
8740   (gnus-summary-raise-same-subject (- score)))
8741
8742 (defun gnus-summary-lower-thread (score)
8743   "Lower score of articles in the current thread with SCORE."
8744   (interactive "P")
8745   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8746
8747 (defun gnus-summary-kill-same-subject-and-select (unmark)
8748   "Mark articles which has the same subject as read, and then select the next.
8749 If UNMARK is positive, remove any kind of mark.
8750 If UNMARK is negative, tick articles."
8751   (interactive "P")
8752   (if unmark
8753       (setq unmark (prefix-numeric-value unmark)))
8754   (let ((count
8755          (gnus-summary-mark-same-subject
8756           (gnus-summary-subject-string) unmark)))
8757     ;; Select next unread article. If auto-select-same mode, should
8758     ;; select the first unread article.
8759     (gnus-summary-next-article t (and gnus-auto-select-same
8760                                       (gnus-summary-subject-string)))
8761     (gnus-message 7 "%d articles are marked as %s"
8762                   count (if unmark "unread" "read"))))
8763
8764 (defun gnus-summary-kill-same-subject (unmark)
8765   "Mark articles which has the same subject as read. 
8766 If UNMARK is positive, remove any kind of mark.
8767 If UNMARK is negative, tick articles."
8768   (interactive "P")
8769   (if unmark
8770       (setq unmark (prefix-numeric-value unmark)))
8771   (let ((count
8772          (gnus-summary-mark-same-subject
8773           (gnus-summary-subject-string) unmark)))
8774     ;; If marked as read, go to next unread subject.
8775     (if (null unmark)
8776         ;; Go to next unread subject.
8777         (gnus-summary-next-subject 1 t))
8778     (gnus-message 7 "%d articles are marked as %s"
8779                   count (if unmark "unread" "read"))))
8780
8781 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8782   "Mark articles with same SUBJECT as read, and return marked number.
8783 If optional argument UNMARK is positive, remove any kinds of marks.
8784 If optional argument UNMARK is negative, mark articles as unread instead."
8785   (let ((count 1))
8786     (save-excursion
8787       (cond ((null unmark)
8788              (gnus-summary-mark-as-read nil gnus-killed-mark))
8789             ((> unmark 0)
8790              (gnus-summary-tick-article nil t))
8791             (t
8792              (gnus-summary-tick-article)))
8793       (while (and subject
8794                   (gnus-summary-search-forward nil subject))
8795         (cond ((null unmark)
8796                (gnus-summary-mark-as-read nil gnus-killed-mark))
8797               ((> unmark 0)
8798                (gnus-summary-tick-article nil t))
8799               (t
8800                (gnus-summary-tick-article)))
8801         (setq count (1+ count))))
8802     ;; Hide killed thread subtrees.  Does not work properly always.
8803     ;;(and (null unmark)
8804     ;;     gnus-thread-hide-killed
8805     ;;     (gnus-summary-hide-thread))
8806     ;; Return number of articles marked as read.
8807     count))
8808
8809 (defun gnus-summary-mark-as-processable (n &optional unmark)
8810   "Set the process mark on the next N articles.
8811 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8812 the process mark instead.  The difference between N and the actual
8813 number of articles marked is returned."
8814   (interactive "p")
8815   (let ((backward (< n 0))
8816         (n (abs n)))
8817   (while (and 
8818           (> n 0)
8819           (if unmark
8820               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8821             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8822           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8823     (setq n (1- n)))
8824   (if (/= 0 n) (gnus-message 7 "No more articles"))
8825   (gnus-summary-recenter)
8826   (gnus-summary-position-cursor)
8827   n))
8828
8829 (defun gnus-summary-unmark-as-processable (n)
8830   "Remove the process mark from the next N articles.
8831 If N is negative, mark backward instead.  The difference between N and
8832 the actual number of articles marked is returned."
8833   (interactive "p")
8834   (gnus-summary-mark-as-processable n t))
8835
8836 (defun gnus-summary-unmark-all-processable ()
8837   "Remove the process mark from all articles."
8838   (interactive)
8839   (save-excursion
8840     (while gnus-newsgroup-processable
8841       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8842   (gnus-summary-position-cursor))
8843
8844 (defun gnus-summary-mark-as-expirable (n)
8845   "Mark N articles forward as expirable.
8846 If N is negative, mark backward instead. The difference between N and
8847 the actual number of articles marked is returned."
8848   (interactive "p")
8849   (gnus-summary-mark-forward n gnus-expirable-mark))
8850
8851 (defun gnus-summary-mark-article-as-replied (article)
8852   "Mark ARTICLE replied and update the summary line."
8853   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8854   (let ((buffer-read-only nil))
8855     (if (gnus-summary-goto-subject article)
8856         (progn
8857           (gnus-summary-update-mark gnus-replied-mark 'replied)
8858           t))))
8859
8860 (defun gnus-summary-set-bookmark (article)
8861   "Set a bookmark in current article."
8862   (interactive (list (gnus-summary-article-number)))
8863   (if (or (not (get-buffer gnus-article-buffer))
8864           (not gnus-current-article)
8865           (not gnus-article-current)
8866           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8867       (error "No current article selected"))
8868   ;; Remove old bookmark, if one exists.
8869   (let ((old (assq article gnus-newsgroup-bookmarks)))
8870     (if old (setq gnus-newsgroup-bookmarks 
8871                   (delq old gnus-newsgroup-bookmarks))))
8872   ;; Set the new bookmark, which is on the form 
8873   ;; (article-number . line-number-in-body).
8874   (setq gnus-newsgroup-bookmarks 
8875         (cons 
8876          (cons article 
8877                (save-excursion
8878                  (set-buffer gnus-article-buffer)
8879                  (count-lines
8880                   (min (point)
8881                        (save-excursion
8882                          (goto-char (point-min))
8883                          (search-forward "\n\n" nil t)
8884                          (point)))
8885                   (point))))
8886          gnus-newsgroup-bookmarks))
8887   (gnus-message 6 "A bookmark has been added to the current article."))
8888
8889 (defun gnus-summary-remove-bookmark (article)
8890   "Remove the bookmark from the current article."
8891   (interactive (list (gnus-summary-article-number)))
8892   ;; Remove old bookmark, if one exists.
8893   (let ((old (assq article gnus-newsgroup-bookmarks)))
8894     (if old 
8895         (progn
8896           (setq gnus-newsgroup-bookmarks 
8897                 (delq old gnus-newsgroup-bookmarks))
8898           (gnus-message 6 "Removed bookmark."))
8899       (gnus-message 6 "No bookmark in current article."))))
8900
8901 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8902 (defun gnus-summary-mark-as-dormant (n)
8903   "Mark N articles forward as dormant.
8904 If N is negative, mark backward instead.  The difference between N and
8905 the actual number of articles marked is returned."
8906   (interactive "p")
8907   (gnus-summary-mark-forward n gnus-dormant-mark))
8908
8909 (defun gnus-summary-set-process-mark (article)
8910   "Set the process mark on ARTICLE and update the summary line."
8911   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8912   (let ((buffer-read-only nil))
8913     (if (gnus-summary-goto-subject article)
8914         (progn
8915           (gnus-summary-show-thread)
8916           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8917                (forward-line 1))
8918           (gnus-summary-update-mark gnus-process-mark 'replied)
8919           t))))
8920
8921 (defun gnus-summary-remove-process-mark (article)
8922   "Remove the process mark from ARTICLE and update the summary line."
8923   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8924   (let ((buffer-read-only nil))
8925     (if (gnus-summary-goto-subject article)
8926         (progn
8927           (gnus-summary-show-thread)
8928           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8929                (forward-line 1))
8930           (gnus-summary-update-mark ?  'replied)
8931           (if (memq article gnus-newsgroup-replied) 
8932               (gnus-summary-update-mark gnus-replied-mark 'replied))
8933           t))))
8934
8935 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8936   "Mark N articles as read forwards.
8937 If N is negative, mark backwards instead.
8938 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8939 marked as unread. 
8940 The difference between N and the actual number of articles marked is
8941 returned."
8942   (interactive "p")
8943   (gnus-set-global-variables)
8944   (let ((backward (< n 0))
8945         (n (abs n))
8946         (mark (or mark gnus-del-mark)))
8947   (while (and (> n 0)
8948               (gnus-summary-mark-article nil mark no-expire)
8949               (zerop (gnus-summary-next-subject 
8950                       (if backward -1 1) gnus-summary-goto-unread t)))
8951     (setq n (1- n)))
8952   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8953   (gnus-summary-recenter)
8954   (gnus-summary-position-cursor)
8955   (gnus-set-mode-line 'summary)
8956   n))
8957
8958 (defun gnus-summary-mark-article (&optional article mark no-expire)
8959   "Mark ARTICLE with MARK.
8960 MARK can be any character.
8961 Five MARK strings are reserved: ?  (unread), 
8962 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8963 If MARK is nil, then the default character ?D is used.
8964 If ARTICLE is nil, then the article on the current line will be
8965 marked." 
8966   ;; If no mark is given, then we check auto-expiring.
8967   (and (not no-expire)
8968        gnus-newsgroup-auto-expire 
8969        (or (not mark)
8970            (and (numberp mark) 
8971                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8972                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8973                     (= mark gnus-read-mark))))
8974        (setq mark gnus-expirable-mark))
8975   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8976          (article (or article (gnus-summary-article-number))))
8977     (or article (error "No article on current line"))
8978     (if (or (= mark gnus-unread-mark) 
8979             (= mark gnus-ticked-mark) 
8980             (= mark gnus-dormant-mark))
8981         (gnus-mark-article-as-unread article mark)
8982       (gnus-mark-article-as-read article mark))
8983
8984     ;; See whether the article is to be put in the cache.
8985     (and gnus-use-cache
8986          (not (= mark gnus-canceled-mark))
8987          (save-excursion
8988            (gnus-cache-possibly-enter-article 
8989             gnus-newsgroup-name article 
8990             (gnus-get-header-by-number article)
8991             (= mark gnus-ticked-mark)
8992             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8993
8994     (if (gnus-summary-goto-subject article)
8995         (let ((buffer-read-only nil))
8996           (gnus-summary-show-thread)
8997           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8998                (forward-line 1))
8999           ;; Fix the mark.
9000           (gnus-summary-update-mark mark 'unread)
9001           t))))
9002
9003 (defun gnus-summary-update-mark (mark type)
9004   (beginning-of-line)
9005   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9006         plist)
9007     (if (not forward)
9008         ()
9009       (forward-char forward)
9010       (setq plist (text-properties-at (point)))
9011       (delete-char 1)
9012       (insert mark)
9013       (and plist (add-text-properties (1- (point)) (point) plist))
9014       (and (eq type 'unread)
9015            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9016       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9017   
9018 (defun gnus-mark-article-as-read (article &optional mark)
9019   "Enter ARTICLE in the pertinent lists and remove it from others."
9020   ;; Make the article expirable.
9021   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
9022     (if (= mark gnus-expirable-mark)
9023         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9024       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9025     ;; Remove from unread and marked lists.
9026     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9027     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9028     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9029     ;; Possibly remove from cache, if that is used. 
9030     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9031
9032 (defun gnus-mark-article-as-unread (article &optional mark)
9033   "Enter ARTICLE in the pertinent lists and remove it from others."
9034   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
9035     ;; Add to unread list.
9036     (or (memq article gnus-newsgroup-unreads)
9037         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9038     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
9039     ;; lists.  Otherwise, it must be added to the list.
9040     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9041     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9042     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9043     (if (= mark gnus-ticked-mark)
9044         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9045     (if (= mark gnus-dormant-mark)
9046         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9047
9048 (defalias 'gnus-summary-mark-as-unread-forward 
9049   'gnus-summary-tick-article-forward)
9050 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9051                'gnus-summary-tick-article-forward)
9052 (defun gnus-summary-tick-article-forward (n)
9053   "Tick N articles forwards.
9054 If N is negative, tick backwards instead.
9055 The difference between N and the number of articles ticked is returned."
9056   (interactive "p")
9057   (gnus-summary-mark-forward n gnus-ticked-mark))
9058
9059 (defalias 'gnus-summary-mark-as-unread-backward 
9060   'gnus-summary-tick-article-backward)
9061 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9062                'gnus-summary-tick-article-backward)
9063 (defun gnus-summary-tick-article-backward (n)
9064   "Tick N articles backwards.
9065 The difference between N and the number of articles ticked is returned."
9066   (interactive "p")
9067   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9068
9069 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9070 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9071 (defun gnus-summary-tick-article (&optional article clear-mark)
9072   "Mark current article as unread.
9073 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9074 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9075   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9076                                        gnus-ticked-mark)))
9077
9078 (defun gnus-summary-mark-as-read-forward (n)
9079   "Mark N articles as read forwards.
9080 If N is negative, mark backwards instead.
9081 The difference between N and the actual number of articles marked is
9082 returned."
9083   (interactive "p")
9084   (gnus-summary-mark-forward n gnus-del-mark t))
9085
9086 (defun gnus-summary-mark-as-read-backward (n)
9087   "Mark the N articles as read backwards.
9088 The difference between N and the actual number of articles marked is
9089 returned."
9090   (interactive "p")
9091   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9092
9093 (defun gnus-summary-mark-as-read (&optional article mark)
9094   "Mark current article as read.
9095 ARTICLE specifies the article to be marked as read.
9096 MARK specifies a string to be inserted at the beginning of the line."
9097   (gnus-summary-mark-article article mark))
9098
9099 (defun gnus-summary-clear-mark-forward (n)
9100   "Clear marks from N articles forward.
9101 If N is negative, clear backward instead.
9102 The difference between N and the number of marks cleared is returned."
9103   (interactive "p")
9104   (gnus-summary-mark-forward n gnus-unread-mark))
9105
9106 (defun gnus-summary-clear-mark-backward (n)
9107   "Clear marks from N articles backward.
9108 The difference between N and the number of marks cleared is returned."
9109   (interactive "p")
9110   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9111
9112 (defun gnus-summary-mark-unread-as-read ()
9113   "Intended to be used by `gnus-summary-mark-article-hook'."
9114   (or (memq gnus-current-article gnus-newsgroup-marked)
9115       (memq gnus-current-article gnus-newsgroup-dormant)
9116       (memq gnus-current-article gnus-newsgroup-expirable)
9117       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
9118
9119 (defun gnus-summary-mark-region-as-read (point mark all)
9120   "Mark all unread articles between point and mark as read.
9121 If given a prefix, mark all articles between point and mark as read,
9122 even ticked and dormant ones."
9123   (interactive "r\nP")
9124   (save-excursion
9125     (goto-char point)
9126     (beginning-of-line)
9127     (while (and 
9128             (< (point) mark)
9129             (progn
9130               (and
9131                (or all
9132                    (and
9133                     (not (memq (gnus-summary-article-number)
9134                                gnus-newsgroup-marked))
9135                     (not (memq (gnus-summary-article-number)
9136                                gnus-newsgroup-dormant))))
9137                (gnus-summary-mark-article
9138                 (gnus-summary-article-number) gnus-del-mark))
9139               t)
9140             (zerop (forward-line 1))))))
9141
9142 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9143 (defalias 'gnus-summary-delete-marked-as-read 
9144   'gnus-summary-remove-lines-marked-as-read)
9145 (make-obsolete 'gnus-summary-delete-marked-as-read 
9146                'gnus-summary-remove-lines-marked-as-read)
9147 (defun gnus-summary-remove-lines-marked-as-read ()
9148   "Remove lines that are marked as read."
9149   (interactive)
9150   (gnus-summary-remove-lines-marked-with 
9151    (concat (mapconcat
9152             (lambda (char) (char-to-string (symbol-value char)))
9153             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9154               gnus-killed-mark gnus-kill-file-mark
9155               gnus-low-score-mark gnus-expirable-mark
9156               gnus-canceled-mark)
9157             ""))))
9158
9159 (defalias 'gnus-summary-delete-marked-with 
9160   'gnus-summary-remove-lines-marked-with)
9161 (make-obsolete 'gnus-summary-delete-marked-with 
9162                'gnus-summary-remove-lines-marked-with)
9163 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9164 (defun gnus-summary-remove-lines-marked-with (marks)
9165   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9166   (interactive "sMarks: ")
9167   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9168   (gnus-set-global-variables)
9169   (let ((buffer-read-only nil)
9170         (marks (concat "^[" marks "]")))
9171     (goto-char (point-min))
9172     (if gnus-newsgroup-adaptive
9173         (gnus-score-remove-lines-adaptive marks)
9174       (while (re-search-forward marks nil t)
9175         (gnus-delete-line)))
9176     ;; If we use dummy roots, we have to do an additional sweep over
9177     ;; the buffer.
9178     (if (not (eq gnus-summary-make-false-root 'dummy))
9179         ()
9180       (goto-char (point-min))
9181       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9182       (while (re-search-forward marks nil t)
9183         (if (gnus-subject-equal
9184              (gnus-summary-subject-string)
9185              (progn
9186                (forward-line 1)
9187                (gnus-summary-subject-string)))
9188             ()
9189           (forward-line -1)
9190           (gnus-delete-line)))))
9191   (or (zerop (buffer-size))
9192       (if (eobp)
9193           (gnus-summary-prev-subject 1)
9194         (gnus-summary-position-cursor))))
9195
9196 (defun gnus-summary-expunge-below (score)
9197   "Remove articles with score less than SCORE."
9198   (interactive "P")
9199   (gnus-set-global-variables)
9200   (setq score (if score
9201                   (prefix-numeric-value score)
9202                 (or gnus-summary-default-score 0)))
9203   (save-excursion
9204     (set-buffer gnus-summary-buffer)
9205     (goto-char (point-min))
9206     (let ((buffer-read-only nil)
9207           beg)
9208       (while (not (eobp))
9209         (if (< (gnus-summary-article-score) score)
9210             (progn
9211               (setq beg (point))
9212               (forward-line 1)
9213               (delete-region beg (point)))
9214           (forward-line 1)))
9215       ;; Adjust point.
9216       (or (zerop (buffer-size))
9217           (if (eobp)
9218               (gnus-summary-prev-subject 1)
9219             (gnus-summary-position-cursor))))))
9220
9221 (defun gnus-summary-mark-below (score mark)
9222   "Mark articles with score less than SCORE with MARK."
9223   (interactive "P\ncMark: ")
9224   (gnus-set-global-variables)
9225   (setq score (if score
9226                   (prefix-numeric-value score)
9227                 (or gnus-summary-default-score 0)))
9228   (save-excursion
9229     (set-buffer gnus-summary-buffer)
9230     (goto-char (point-min))
9231     (while (not (eobp))
9232       (and (< (gnus-summary-article-score) score)
9233            (gnus-summary-mark-article nil mark))
9234       (forward-line 1))))
9235
9236 (defun gnus-summary-kill-below (score)
9237   "Mark articles with score below SCORE as read."
9238   (interactive "P")
9239   (gnus-summary-mark-below score gnus-killed-mark))
9240
9241 (defun gnus-summary-clear-above (score)
9242   "Clear all marks from articles with score above SCORE."
9243   (interactive "P")
9244   (gnus-summary-mark-above score gnus-unread-mark))
9245
9246 (defun gnus-summary-tick-above (score)
9247   "Tick all articles with score above SCORE."
9248   (interactive "P")
9249   (gnus-summary-mark-above score gnus-ticked-mark))
9250
9251 (defun gnus-summary-mark-above (score mark)
9252   "Mark articles with score over SCORE with MARK."
9253   (interactive "P\ncMark: ")
9254   (setq score (if score
9255                   (prefix-numeric-value score)
9256                 (or gnus-summary-default-score 0)))
9257   (save-excursion
9258     (set-buffer gnus-summary-buffer)
9259     (goto-char (point-min))
9260     (while (not (eobp))
9261       (if (> (gnus-summary-article-score) score)
9262           (progn
9263             (gnus-summary-mark-article nil mark)
9264             (forward-line 1))
9265         (forward-line 1)))))
9266
9267 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9268 (defun gnus-summary-show-all-expunged ()
9269   "Display all the hidden articles that were expunged for low scores."
9270   (interactive)
9271   (let ((buffer-read-only nil))
9272     (let ((scored gnus-newsgroup-scored)
9273           headers h)
9274       (while scored
9275         (or (gnus-summary-goto-subject (car (car scored)))
9276             (and (setq h (gnus-get-header-by-number (car (car scored))))
9277                  (< (cdr (car scored)) gnus-summary-expunge-below)
9278                  (setq headers (cons h headers))))
9279         (setq scored (cdr scored)))
9280       (or headers (error "No expunged articles hidden."))
9281       (goto-char (point-min))
9282       (save-excursion 
9283         (gnus-summary-update-lines 
9284          (point)
9285          (progn
9286            (gnus-summary-prepare-threads (nreverse headers) 0)
9287            (point)))))
9288     (goto-char (point-min))
9289     (gnus-summary-position-cursor)))
9290
9291 (defun gnus-summary-show-all-dormant ()
9292   "Display all the hidden articles that are marked as dormant."
9293   (interactive)
9294   (let ((buffer-read-only nil))
9295     (let ((dormant gnus-newsgroup-dormant)
9296           headers h)
9297       (while dormant
9298         (or (gnus-summary-goto-subject (car dormant))
9299             (and (setq h (gnus-get-header-by-number (car dormant)))
9300                  (setq headers (cons h headers))))
9301         (setq dormant (cdr dormant)))
9302       (or headers (error "No dormant articles hidden."))
9303       (goto-char (point-min))
9304       (save-excursion 
9305         (gnus-summary-update-lines 
9306          (point)
9307          (progn
9308            (gnus-summary-prepare-threads (nreverse headers) 0)
9309            (point)))))
9310     (goto-char (point-min))
9311     (gnus-summary-position-cursor)))
9312
9313 (defun gnus-summary-hide-all-dormant ()
9314   "Hide all dormant articles."
9315   (interactive)
9316   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9317   (gnus-summary-position-cursor))
9318
9319 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9320   "Mark all articles not marked as unread in this newsgroup as read.
9321 If prefix argument ALL is non-nil, all articles are marked as read.
9322 If QUIETLY is non-nil, no questions will be asked.
9323 If TO-HERE is non-nil, it should be a point in the buffer. All
9324 articles before this point will be marked as read.
9325 The number of articles marked as read is returned."
9326   (interactive "P")
9327   (prog1
9328       (if (or quietly
9329               (not gnus-interactive-catchup) ;Without confirmation?
9330               gnus-expert-user
9331               (gnus-y-or-n-p
9332                (if all
9333                    "Mark absolutely all articles as read? "
9334                  "Mark all unread articles as read? ")))
9335           (if (and not-mark 
9336                    (not gnus-newsgroup-adaptive)
9337                    (not gnus-newsgroup-auto-expire))
9338               (progn
9339                 (and all (setq gnus-newsgroup-marked nil
9340                                gnus-newsgroup-dormant nil))
9341                 (setq gnus-newsgroup-unreads 
9342                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9343             ;; We actually mark all articles as canceled, which we
9344             ;; have to do when using auto-expiry or adaptive scoring. 
9345             (let ((unreads (length gnus-newsgroup-unreads)))
9346               (if (gnus-summary-first-subject (not all))
9347                   (while (and (if to-here (< (point) to-here) t)
9348                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9349                               (gnus-summary-search-subject nil (not all)))))
9350               (- unreads (length gnus-newsgroup-unreads))
9351               (or to-here
9352                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9353     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9354       (if (and (not to-here) (eq 'nnvirtual (car method)))
9355           (nnvirtual-catchup-group
9356            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9357     (gnus-summary-position-cursor)))
9358
9359 (defun gnus-summary-catchup-to-here (&optional all)
9360   "Mark all unticked articles before the current one as read.
9361 If ALL is non-nil, also mark ticked and dormant articles as read."
9362   (interactive)
9363   (save-excursion
9364     (and (zerop (forward-line -1))
9365          (progn
9366            (end-of-line)
9367            (gnus-summary-catchup all t (point))
9368            (gnus-set-mode-line 'summary))))
9369   (gnus-summary-position-cursor))
9370
9371 (defun gnus-summary-catchup-all (&optional quietly)
9372   "Mark all articles in this newsgroup as read."
9373   (interactive)
9374   (gnus-summary-catchup t quietly))
9375
9376 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9377   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9378 If prefix argument ALL is non-nil, all articles are marked as read."
9379   (interactive "P")
9380   (gnus-summary-catchup all quietly nil 'fast)
9381   ;; Select next newsgroup or exit.
9382   (if (eq gnus-auto-select-next 'quietly)
9383       (gnus-summary-next-group nil)
9384     (gnus-summary-exit)))
9385
9386 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9387   "Mark all articles in this newsgroup as read, and then exit."
9388   (interactive)
9389   (gnus-summary-catchup-and-exit t quietly))
9390
9391 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9392 (defun gnus-summary-catchup-and-goto-next-group (all)
9393   "Mark all articles in this group as read and select the next group.
9394 If given a prefix, mark all articles, unread as well as ticked, as
9395 read." 
9396   (interactive "P")
9397   (gnus-summary-catchup all)
9398   (gnus-summary-next-group))
9399
9400 ;; Thread-based commands.
9401
9402 (defun gnus-summary-toggle-threads (arg)
9403   "Toggle showing conversation threads.
9404 If ARG is positive number, turn showing conversation threads on."
9405   (interactive "P")
9406   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9407     (setq gnus-show-threads
9408           (if (null arg) (not gnus-show-threads)
9409             (> (prefix-numeric-value arg) 0)))
9410     (gnus-summary-prepare)
9411     (gnus-summary-goto-subject current)
9412     (gnus-summary-position-cursor)))
9413
9414 (defun gnus-summary-show-all-threads ()
9415   "Show all threads."
9416   (interactive)
9417   (save-excursion
9418     (let ((buffer-read-only nil))
9419       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9420   (gnus-summary-position-cursor))
9421
9422 (defun gnus-summary-show-thread ()
9423   "Show thread subtrees.
9424 Returns nil if no thread was there to be shown."
9425   (interactive)
9426   (prog1
9427       (save-excursion
9428         (let ((buffer-read-only nil)
9429               ;; first goto end then to beg, to have point at beg after let
9430               (end (progn (end-of-line) (point)))
9431               (beg (progn (beginning-of-line) (point))))
9432           (prog1
9433               ;; Any hidden lines here?
9434               (search-forward "\r" end t)
9435             (subst-char-in-region beg end ?\^M ?\n t))))
9436     (gnus-summary-position-cursor)))
9437
9438 (defun gnus-summary-hide-all-threads ()
9439   "Hide all thread subtrees."
9440   (interactive)
9441   (save-excursion
9442     (goto-char (point-min))
9443     (gnus-summary-hide-thread)
9444     (while (and (not (eobp)) (zerop (forward-line 1)))
9445       (gnus-summary-hide-thread)))
9446   (gnus-summary-position-cursor))
9447
9448 (defun gnus-summary-hide-thread ()
9449   "Hide thread subtrees.
9450 Returns nil if no threads were there to be hidden."
9451   (interactive)
9452   (let ((buffer-read-only nil)
9453         (start (point))
9454         (level (gnus-summary-thread-level))
9455         (end (point)))
9456     ;; Go forward until either the buffer ends or the subthread
9457     ;; ends. 
9458     (if (eobp)
9459         ()
9460       (while (and (zerop (forward-line 1))
9461                   (> (gnus-summary-thread-level) level))
9462         (setq end (point)))
9463       (prog1
9464           (save-excursion
9465             (goto-char end)
9466             (search-backward "\n" start t))
9467         (subst-char-in-region start end ?\n ?\^M t)
9468         (forward-line -1)))))
9469
9470 (defun gnus-summary-go-to-next-thread (&optional previous)
9471   "Go to the same level (or less) next thread.
9472 If PREVIOUS is non-nil, go to previous thread instead.
9473 Return the article number moved to, or nil if moving was impossible."
9474   (let ((level (gnus-summary-thread-level))
9475         (article (gnus-summary-article-number)))
9476     (if previous 
9477         (while (and (zerop (forward-line -1))
9478                     (> (gnus-summary-thread-level) level)))
9479       (while (and (save-excursion
9480                     (forward-line 1)
9481                     (not (eobp)))
9482                   (zerop (forward-line 1))
9483                   (> (gnus-summary-thread-level) level))))
9484     (gnus-summary-recenter)
9485     (gnus-summary-position-cursor)
9486     (let ((oart (gnus-summary-article-number)))
9487       (and (/= oart article) oart))))
9488
9489 (defun gnus-summary-next-thread (n)
9490   "Go to the same level next N'th thread.
9491 If N is negative, search backward instead.
9492 Returns the difference between N and the number of skips actually
9493 done."
9494   (interactive "p")
9495   (let ((backward (< n 0))
9496         (n (abs n)))
9497   (while (and (> n 0)
9498               (gnus-summary-go-to-next-thread backward))
9499     (setq n (1- n)))
9500   (gnus-summary-position-cursor)
9501   (if (/= 0 n) (gnus-message 7 "No more threads"))
9502   n))
9503
9504 (defun gnus-summary-prev-thread (n)
9505   "Go to the same level previous N'th thread.
9506 Returns the difference between N and the number of skips actually
9507 done."
9508   (interactive "p")
9509   (gnus-summary-next-thread (- n)))
9510
9511 (defun gnus-summary-go-down-thread (&optional same)
9512   "Go down one level in the current thread.
9513 If SAME is non-nil, also move to articles of the same level."
9514   (let ((level (gnus-summary-thread-level))
9515         (start (point)))
9516     (if (and (zerop (forward-line 1))
9517              (> (gnus-summary-thread-level) level))
9518         t
9519       (goto-char start)
9520       nil)))
9521
9522 (defun gnus-summary-go-up-thread ()
9523   "Go up one level in the current thread."
9524   (let ((level (gnus-summary-thread-level))
9525         (start (point)))
9526     (while (and (zerop (forward-line -1))
9527                 (>= (gnus-summary-thread-level) level)))
9528     (if (>= (gnus-summary-thread-level) level)
9529         (progn
9530           (goto-char start)
9531           nil)
9532       t)))
9533
9534 (defun gnus-summary-down-thread (n)
9535   "Go down thread N steps.
9536 If N is negative, go up instead.
9537 Returns the difference between N and how many steps down that were
9538 taken."
9539   (interactive "p")
9540   (let ((up (< n 0))
9541         (n (abs n)))
9542   (while (and (> n 0)
9543               (if up (gnus-summary-go-up-thread)
9544                 (gnus-summary-go-down-thread)))
9545     (setq n (1- n)))
9546   (gnus-summary-position-cursor)
9547   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9548   n))
9549
9550 (defun gnus-summary-up-thread (n)
9551   "Go up thread N steps.
9552 If N is negative, go up instead.
9553 Returns the difference between N and how many steps down that were
9554 taken."
9555   (interactive "p")
9556   (gnus-summary-down-thread (- n)))
9557
9558 (defun gnus-summary-kill-thread (unmark)
9559   "Mark articles under current thread as read.
9560 If the prefix argument is positive, remove any kinds of marks.
9561 If the prefix argument is negative, tick articles instead."
9562   (interactive "P")
9563   (if unmark
9564       (setq unmark (prefix-numeric-value unmark)))
9565   (let ((killing t)
9566         (level (gnus-summary-thread-level)))
9567     (save-excursion
9568       (while killing
9569         ;; Mark the article...
9570         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9571               ((> unmark 0) (gnus-summary-tick-article nil t))
9572               (t (gnus-summary-tick-article)))
9573         ;; ...and go forward until either the buffer ends or the subtree
9574         ;; ends. 
9575         (if (not (and (zerop (forward-line 1))
9576                       (> (gnus-summary-thread-level) level)))
9577             (setq killing nil))))
9578     ;; Hide killed subtrees.
9579     (and (null unmark)
9580          gnus-thread-hide-killed
9581          (gnus-summary-hide-thread))
9582     ;; If marked as read, go to next unread subject.
9583     (if (null unmark)
9584         ;; Go to next unread subject.
9585         (gnus-summary-next-subject 1 t)))
9586   (gnus-set-mode-line 'summary))
9587
9588 ;; Summary sorting commands
9589
9590 (defun gnus-summary-sort-by-number (&optional reverse)
9591   "Sort summary buffer by article number.
9592 Argument REVERSE means reverse order."
9593   (interactive "P")
9594   (gnus-summary-sort 
9595    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9596
9597 (defun gnus-summary-sort-by-author (&optional reverse)
9598   "Sort summary buffer by author name alphabetically.
9599 If case-fold-search is non-nil, case of letters is ignored.
9600 Argument REVERSE means reverse order."
9601   (interactive "P")
9602   (gnus-summary-sort
9603    (cons
9604     (lambda ()
9605       (let ((extract (funcall
9606                       gnus-extract-address-components
9607                       (header-from (gnus-get-header-by-number
9608                                     (gnus-summary-article-number))))))
9609         (or (car extract) (cdr extract))))
9610     'gnus-thread-sort-by-author)
9611    reverse))
9612
9613 (defun gnus-summary-sort-by-subject (&optional reverse)
9614   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9615 If case-fold-search is non-nil, case of letters is ignored.
9616 Argument REVERSE means reverse order."
9617   (interactive "P")
9618   (gnus-summary-sort
9619    (cons
9620     (lambda ()
9621       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9622     'gnus-thread-sort-by-subject)
9623    reverse))
9624
9625 (defun gnus-summary-sort-by-date (&optional reverse)
9626   "Sort summary buffer by date.
9627 Argument REVERSE means reverse order."
9628   (interactive "P")
9629   (gnus-summary-sort
9630    (cons
9631     (lambda ()
9632       (gnus-sortable-date
9633        (header-date 
9634         (gnus-get-header-by-number (gnus-summary-article-number)))))
9635     'gnus-thread-sort-by-date)
9636    reverse))
9637
9638 (defun gnus-summary-sort-by-score (&optional reverse)
9639   "Sort summary buffer by score.
9640 Argument REVERSE means reverse order."
9641   (interactive "P")
9642   (gnus-summary-sort 
9643    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9644    (not reverse)))
9645
9646 (defvar gnus-summary-already-sorted nil)
9647 (defun gnus-summary-sort (predicate reverse)
9648   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9649   (if gnus-summary-already-sorted
9650       ()
9651     (let (buffer-read-only)
9652       (if (not gnus-show-threads)
9653           (progn
9654             (goto-char (point-min))
9655             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9656         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9657               (gnus-summary-prepare-hook nil)
9658               (gnus-summary-already-sorted nil))
9659           (gnus-summary-prepare)
9660           (and gnus-show-threads
9661                gnus-thread-hide-subtree
9662                (gnus-summary-hide-all-threads))
9663           ;; If in async mode, we send some info to the backend.
9664           (and gnus-newsgroup-async
9665                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9666                (gnus-request-asynchronous 
9667                 gnus-newsgroup-name
9668                 (if (and gnus-asynchronous-article-function
9669                          (fboundp gnus-asynchronous-article-function))
9670                     (funcall gnus-asynchronous-article-function
9671                              gnus-newsgroup-threads)))))))))
9672
9673   
9674 (defun gnus-sortable-date (date)
9675   "Make sortable string by string-lessp from DATE.
9676 Timezone package is used."
9677   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9678          (year   (aref date 0))
9679          (month  (aref date 1))
9680          (day    (aref date 2)))
9681     (timezone-make-sortable-date 
9682      year month day 
9683      (timezone-make-time-string
9684       (aref date 3) (aref date 4) (aref date 5)))))
9685
9686
9687 ;; Summary saving commands.
9688
9689 (defun gnus-summary-save-article (n)
9690   "Save the current article using the default saver function.
9691 If N is a positive number, save the N next articles.
9692 If N is a negative number, save the N previous articles.
9693 If N is nil and any articles have been marked with the process mark,
9694 save those articles instead.
9695 The variable `gnus-default-article-saver' specifies the saver function."
9696   (interactive "P")
9697   (let ((articles (gnus-summary-work-articles n)))
9698     (while articles
9699       (let ((header (gnus-get-header-by-number (car articles))))
9700         (if (vectorp header)
9701             (progn
9702               (gnus-summary-select-article t nil nil (car articles))
9703               (or gnus-save-all-headers
9704                   (gnus-article-hide-headers t))
9705               ;; Remove any X-Gnus lines.
9706               (save-excursion
9707                 (save-restriction
9708                   (set-buffer gnus-article-buffer)
9709                   (let ((buffer-read-only nil))
9710                     (goto-char (point-min))
9711                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9712                                                   (point-max)))
9713                     (while (re-search-forward "^X-Gnus" nil t)
9714                       (beginning-of-line)
9715                       (delete-region (point)
9716                                      (progn (forward-line 1) (point))))
9717                     (widen))))
9718               (save-excursion
9719                 (if gnus-default-article-saver
9720                     (funcall gnus-default-article-saver)
9721                   (error "No default saver is defined."))))
9722           (if (assq 'name header)
9723               (gnus-copy-file (cdr (assq 'name header)))
9724             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9725       (gnus-summary-remove-process-mark (car articles))
9726       (setq articles (cdr articles)))
9727     (gnus-summary-position-cursor)
9728     n))
9729
9730 (defun gnus-summary-pipe-output (arg)
9731   "Pipe the current article to a subprocess.
9732 If N is a positive number, pipe the N next articles.
9733 If N is a negative number, pipe the N previous articles.
9734 If N is nil and any articles have been marked with the process mark,
9735 pipe those articles instead."
9736   (interactive "P")
9737   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9738     (gnus-summary-save-article arg)))
9739
9740 (defun gnus-summary-save-article-mail (arg)
9741   "Append the current article to an mail file.
9742 If N is a positive number, save the N next articles.
9743 If N is a negative number, save the N previous articles.
9744 If N is nil and any articles have been marked with the process mark,
9745 save those articles instead."
9746   (interactive "P")
9747   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9748     (gnus-summary-save-article arg)))
9749
9750 (defun gnus-summary-save-article-rmail (arg)
9751   "Append the current article to an rmail file.
9752 If N is a positive number, save the N next articles.
9753 If N is a negative number, save the N previous articles.
9754 If N is nil and any articles have been marked with the process mark,
9755 save those articles instead."
9756   (interactive "P")
9757   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9758     (gnus-summary-save-article arg)))
9759
9760 (defun gnus-summary-save-article-file (arg)
9761   "Append the current article to a file.
9762 If N is a positive number, save the N next articles.
9763 If N is a negative number, save the N previous articles.
9764 If N is nil and any articles have been marked with the process mark,
9765 save those articles instead."
9766   (interactive "P")
9767   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9768     (gnus-summary-save-article arg)))
9769
9770 (defun gnus-read-save-file-name (prompt default-name)
9771   (let ((methods gnus-split-methods)
9772         split-name)
9773     (if (not gnus-split-methods)
9774         ()
9775       (save-excursion
9776         (set-buffer gnus-article-buffer)
9777         (gnus-narrow-to-headers)
9778         (while methods
9779           (goto-char (point-min))
9780           (and (condition-case () 
9781                    (re-search-forward (car (car methods)) nil t)
9782                  (error nil))
9783                (setq split-name (cons (nth 1 (car methods)) split-name)))
9784           (setq methods (cdr methods)))
9785         (widen)))
9786     (cond ((null split-name)
9787            (read-file-name
9788             (concat prompt " (default "
9789                     (file-name-nondirectory default-name) ") ")
9790             (file-name-directory default-name)
9791             default-name))
9792           ((= 1 (length split-name))
9793            (read-file-name
9794             (concat prompt " (default " (car split-name) ") ")
9795             gnus-article-save-directory
9796             (concat gnus-article-save-directory (car split-name))))
9797           (t
9798            (setq split-name (mapcar (lambda (el) (list el))
9799                                     (nreverse split-name)))
9800            (let ((result (completing-read 
9801                           (concat prompt " ")
9802                           split-name nil nil)))
9803              (concat gnus-article-save-directory
9804                      (if (string= result "")
9805                          (car (car split-name))
9806                        result)))))))
9807
9808 (defun gnus-summary-save-in-rmail (&optional filename)
9809   "Append this article to Rmail file.
9810 Optional argument FILENAME specifies file name.
9811 Directory to save to is default to `gnus-article-save-directory' which
9812 is initialized from the SAVEDIR environment variable."
9813   (interactive)
9814   (let ((default-name
9815           (funcall gnus-rmail-save-name gnus-newsgroup-name
9816                    gnus-current-headers gnus-newsgroup-last-rmail)))
9817     (or filename
9818         (setq filename (gnus-read-save-file-name 
9819                         "Save in rmail file:" default-name)))
9820     (gnus-make-directory (file-name-directory filename))
9821     (gnus-eval-in-buffer-window 
9822      gnus-article-buffer
9823      (save-excursion
9824        (save-restriction
9825          (widen)
9826          (gnus-output-to-rmail filename))))
9827     ;; Remember the directory name to save articles.
9828     (setq gnus-newsgroup-last-rmail filename)))
9829
9830 (defun gnus-summary-save-in-mail (&optional filename)
9831   "Append this article to Unix mail file.
9832 Optional argument FILENAME specifies file name.
9833 Directory to save to is default to `gnus-article-save-directory' which
9834 is initialized from the SAVEDIR environment variable."
9835   (interactive)
9836   (let ((default-name
9837           (funcall gnus-mail-save-name gnus-newsgroup-name
9838                    gnus-current-headers gnus-newsgroup-last-mail)))
9839     (or filename
9840         (setq filename (gnus-read-save-file-name 
9841                         "Save in Unix mail file:" default-name)))
9842     (setq filename
9843           (expand-file-name filename
9844                             (and default-name
9845                                  (file-name-directory default-name))))
9846     (gnus-make-directory (file-name-directory filename))
9847     (gnus-eval-in-buffer-window 
9848      gnus-article-buffer
9849      (save-excursion
9850        (save-restriction
9851          (widen)
9852          (if (and (file-readable-p filename) (rmail-file-p filename))
9853              (gnus-output-to-rmail filename)
9854            (rmail-output filename 1 t t)))))
9855     ;; Remember the directory name to save articles.
9856     (setq gnus-newsgroup-last-mail filename)))
9857
9858 (defun gnus-summary-save-in-file (&optional filename)
9859   "Append this article to file.
9860 Optional argument FILENAME specifies file name.
9861 Directory to save to is default to `gnus-article-save-directory' which
9862 is initialized from the SAVEDIR environment variable."
9863   (interactive)
9864   (let ((default-name
9865           (funcall gnus-file-save-name gnus-newsgroup-name
9866                    gnus-current-headers gnus-newsgroup-last-file)))
9867     (or filename
9868         (setq filename (gnus-read-save-file-name 
9869                         "Save in file:" default-name)))
9870     (gnus-make-directory (file-name-directory filename))
9871     (gnus-eval-in-buffer-window 
9872      gnus-article-buffer
9873      (save-excursion
9874        (save-restriction
9875          (widen)
9876          (gnus-output-to-file filename))))
9877     ;; Remember the directory name to save articles.
9878     (setq gnus-newsgroup-last-file filename)))
9879
9880 (defun gnus-summary-save-in-pipe (&optional command)
9881   "Pipe this article to subprocess."
9882   (interactive)
9883   (let ((command (read-string "Shell command on article: "
9884                               gnus-last-shell-command)))
9885     (if (string-equal command "")
9886         (setq command gnus-last-shell-command))
9887     (gnus-eval-in-buffer-window 
9888      gnus-article-buffer
9889      (save-restriction
9890        (widen)
9891        (shell-command-on-region (point-min) (point-max) command nil)))
9892     (setq gnus-last-shell-command command)))
9893
9894 ;; Summary extract commands
9895
9896 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9897   (let ((buffer-read-only nil)
9898         (article (gnus-summary-article-number))
9899         b)
9900     (or (gnus-summary-goto-subject article)
9901         (error (format "No such article: %d" article)))
9902     (or gnus-newsgroup-headers-hashtb-by-number
9903         (gnus-make-headers-hashtable-by-number))
9904     (gnus-summary-position-cursor)
9905     ;; If all commands are to be bunched up on one line, we collect
9906     ;; them here.  
9907     (if gnus-view-pseudos-separately
9908         ()
9909       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9910             files action)
9911         (while ps
9912           (setq action (cdr (assq 'action (car ps))))
9913           (setq files (list (cdr (assq 'name (car ps)))))
9914           (while (and ps (cdr ps)
9915                       (string= (or action "1")
9916                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9917             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9918             (setcdr ps (cdr (cdr ps))))
9919           (if (not files)
9920               ()
9921             (if (not (string-match "%s" action))
9922                 (setq files (cons " " files)))
9923             (setq files (cons " " files))
9924             (and (assq 'execute (car ps))
9925                  (setcdr (assq 'execute (car ps))
9926                          (funcall (if (string-match "%s" action)
9927                                       'format 'concat)
9928                                   action 
9929                                   (mapconcat (lambda (f) f) files " ")))))
9930           (setq ps (cdr ps)))))
9931     (if (and gnus-view-pseudos (not not-view))
9932         (while pslist
9933           (and (assq 'execute (car pslist))
9934                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9935                                      (eq gnus-view-pseudos 'not-confirm)))
9936           (setq pslist (cdr pslist)))
9937       (save-excursion
9938         (while pslist
9939           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9940                                          (gnus-summary-article-number)))
9941           (forward-line 1)
9942           (setq b (point))
9943           (insert "          " (file-name-nondirectory 
9944                                 (cdr (assq 'name (car pslist))))
9945                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9946           (add-text-properties 
9947            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9948                           'gnus-mark gnus-unread-mark 
9949                           'gnus-level 0
9950                           'gnus-pseudo (car pslist)))
9951           (forward-line -1)
9952           (gnus-sethash (int-to-string gnus-reffed-article-number)
9953                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9954           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9955           (setq pslist (cdr pslist)))))))
9956
9957 (defun gnus-pseudos< (p1 p2)
9958   (let ((c1 (cdr (assq 'action p1)))
9959         (c2 (cdr (assq 'action p2))))
9960     (and c1 c2 (string< c1 c2))))
9961
9962 (defun gnus-request-pseudo-article (props)
9963   (cond ((assq 'execute props)
9964          (gnus-execute-command (cdr (assq 'execute props)))))
9965   (let ((gnus-current-article (gnus-summary-article-number)))
9966     (run-hooks 'gnus-mark-article-hook)))
9967
9968 (defun gnus-execute-command (command &optional automatic)
9969   (save-excursion
9970     (gnus-article-setup-buffer)
9971     (set-buffer gnus-article-buffer)
9972     (let ((command (if automatic command (read-string "Command: " command)))
9973           (buffer-read-only nil))
9974       (erase-buffer)
9975       (insert "$ " command "\n\n")
9976       (if gnus-view-pseudo-asynchronously
9977           (start-process "gnus-execute" nil "sh" "-c" command)
9978         (call-process "sh" nil t nil "-c" command)))))
9979
9980 (defun gnus-copy-file (file &optional to)
9981   "Copy FILE to TO."
9982   (interactive
9983    (list (read-file-name "Copy file: " default-directory)
9984          (read-file-name "Copy file to: " default-directory)))
9985   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9986   (and (file-directory-p to) 
9987        (setq to (concat (file-name-as-directory to)
9988                         (file-name-nondirectory file))))
9989   (copy-file file to))
9990
9991 ;; Summary kill commands.
9992
9993 (defun gnus-summary-edit-global-kill (article)
9994   "Edit the \"global\" kill file."
9995   (interactive (list (gnus-summary-article-number)))
9996   (gnus-group-edit-global-kill article))
9997
9998 (defun gnus-summary-edit-local-kill ()
9999   "Edit a local kill file applied to the current newsgroup."
10000   (interactive)
10001   (setq gnus-current-headers 
10002         (gnus-gethash 
10003          (int-to-string (gnus-summary-article-number))
10004          gnus-newsgroup-headers-hashtb-by-number))
10005   (gnus-set-global-variables)
10006   (gnus-group-edit-local-kill 
10007    (gnus-summary-article-number) gnus-newsgroup-name))
10008
10009 \f
10010 ;;;
10011 ;;; Gnus article mode
10012 ;;;
10013
10014 (put 'gnus-article-mode 'mode-class 'special)
10015
10016 (if gnus-article-mode-map
10017     nil
10018   (setq gnus-article-mode-map (make-keymap))
10019   (suppress-keymap gnus-article-mode-map)
10020   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10021   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10022   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10023   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10024   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10025   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10026   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10027   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10028   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10029   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10030   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10031   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10032   
10033   ;; Duplicate almost all summary keystrokes in the article mode map.
10034   (let ((commands 
10035          (list 
10036           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10037           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10038           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10039           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10040           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10041           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10042           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10043           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10044           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10045           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
10046           "\C-c\C-v\C-v" "\C-d" "v" 
10047 ;;        "Mt" "M!" "Md" "Mr"
10048 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10049 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10050 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10051 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10052 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10053 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10054 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10055 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10056 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10057 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10058 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10059           )))
10060     (while commands
10061       (define-key gnus-article-mode-map (car commands) 
10062         'gnus-article-summary-command)
10063       (setq commands (cdr commands))))
10064
10065   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10066 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10067                          "=" "n")))
10068     (while commands
10069       (define-key gnus-article-mode-map (car commands) 
10070         'gnus-article-summary-command-nosave)
10071       (setq commands (cdr commands)))))
10072
10073
10074 (defun gnus-article-mode ()
10075   "Major mode for displaying an article.
10076
10077 All normal editing commands are switched off.
10078
10079 The following commands are available:
10080
10081 \\<gnus-article-mode-map>
10082 \\[gnus-article-next-page]\t Scroll the article one page forwards
10083 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10084 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10085 \\[gnus-article-show-summary]\t Display the summary buffer
10086 \\[gnus-article-mail]\t Send a reply to the address near point
10087 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10088 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10089 \\[gnus-info-find-node]\t Go to the Gnus info node"
10090   (interactive)
10091   (if gnus-visual (gnus-article-make-menu-bar))
10092   (kill-all-local-variables)
10093   (setq mode-line-modified "-- ")
10094   (make-local-variable 'mode-line-format)
10095   (setq mode-line-format (copy-sequence mode-line-format))
10096   (and (equal (nth 3 mode-line-format) "   ")
10097        (setcar (nthcdr 3 mode-line-format) ""))
10098   (setq mode-name "Article")
10099   (setq major-mode 'gnus-article-mode)
10100   (make-local-variable 'minor-mode-alist)
10101   (or (assq 'gnus-show-mime minor-mode-alist)
10102       (setq minor-mode-alist
10103             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10104   (use-local-map gnus-article-mode-map)
10105   (make-local-variable 'page-delimiter)
10106   (setq page-delimiter gnus-page-delimiter)
10107   (buffer-disable-undo (current-buffer))
10108   (setq buffer-read-only t)             ;Disable modification
10109   (run-hooks 'gnus-article-mode-hook))
10110
10111 (defun gnus-article-setup-buffer ()
10112   "Initialize article mode buffer."
10113   (if (get-buffer gnus-article-buffer)
10114       (save-excursion
10115         (set-buffer gnus-article-buffer)
10116         (buffer-disable-undo (current-buffer))
10117         (setq buffer-read-only t)
10118         (gnus-add-current-to-buffer-list)
10119         (or (eq major-mode 'gnus-article-mode)
10120             (gnus-article-mode)))
10121     (save-excursion
10122       (set-buffer (get-buffer-create gnus-article-buffer))
10123       (gnus-add-current-to-buffer-list)
10124       (gnus-article-mode))))
10125
10126 ;; Set article window start at LINE, where LINE is the number of lines
10127 ;; from the head of the article.
10128 (defun gnus-article-set-window-start (&optional line)
10129   (set-window-start 
10130    (get-buffer-window gnus-article-buffer)
10131    (save-excursion
10132      (set-buffer gnus-article-buffer)
10133      (goto-char (point-min))
10134      (if (not line)
10135          (point-min)
10136        (gnus-message 6 "Moved to bookmark")
10137        (search-forward "\n\n" nil t)
10138        (forward-line line)
10139        (point)))))
10140
10141 (defun gnus-request-article-this-buffer (article group)
10142   "Get an article and insert it into this buffer."
10143   (setq group (or group gnus-newsgroup-name))
10144
10145   ;; Open server if it has closed.
10146   (gnus-check-news-server (gnus-find-method-for-group group))
10147
10148   ;; Using `gnus-request-article' directly will insert the article into
10149   ;; `nntp-server-buffer' - so we'll save some time by not having to
10150   ;; copy it from the server buffer into the article buffer.
10151
10152   ;; We only request an article by message-id when we do not have the
10153   ;; headers for it, so we'll have to get those.
10154   (and (stringp article) 
10155        (let ((gnus-override-method gnus-refer-article-method))
10156          (gnus-read-header article)))
10157
10158   ;; If the article number is negative, that means that this article
10159   ;; doesn't belong in this newsgroup (possibly), so we find its
10160   ;; message-id and request it by id instead of number.
10161   (if (not (numberp article))
10162       ()
10163     (save-excursion
10164       (set-buffer gnus-summary-buffer)
10165       (let ((header (gnus-get-header-by-number article)))
10166         (if (< article 0)
10167             (if (vectorp header)
10168                 ;; It's a real article.
10169                 (setq article (header-id header))
10170               ;; It is an extracted pseudo-article.
10171               (setq article 'pseudo)
10172               (gnus-request-pseudo-article header)))
10173
10174         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10175           (if (not (eq (car method) 'nneething))
10176               ()
10177             (let ((dir (concat (file-name-as-directory (nth 1 method))
10178                                (header-subject header))))
10179               (if (file-directory-p dir)
10180                   (progn
10181                     (setq article 'nneething)
10182                     (gnus-group-enter-directory dir)))))))))
10183
10184   ;; Check the cache.
10185   (if (and gnus-use-cache
10186            (numberp article)
10187            (gnus-cache-request-article article group))
10188       'article
10189     ;; Get the article and into the article buffer.
10190     (if (or (stringp article) (numberp article))
10191         (progn
10192           (erase-buffer)
10193           (let ((gnus-override-method 
10194                  (and (stringp article) gnus-refer-article-method)))
10195             (and (gnus-request-article article group (current-buffer))
10196                  'article)))
10197       article)))
10198
10199 (defun gnus-read-header (id)
10200   "Read the headers of article ID and enter them into the Gnus system."
10201   (or gnus-newsgroup-headers-hashtb-by-number
10202       (gnus-make-headers-hashtable-by-number))
10203   (let (header)
10204     (if (not (setq header 
10205                    (car (if (let ((gnus-nov-is-evil t))
10206                               (gnus-retrieve-headers 
10207                                (list id) gnus-newsgroup-name))
10208                             (gnus-get-newsgroup-headers)))))
10209         nil
10210       (if (stringp id)
10211           (header-set-number header gnus-reffed-article-number))
10212       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10213       (gnus-sethash (int-to-string (header-number header)) header
10214                     gnus-newsgroup-headers-hashtb-by-number)
10215       (if (stringp id)
10216           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10217       (setq gnus-current-headers header)
10218       header)))
10219
10220 (defun gnus-article-prepare (article &optional all-headers header)
10221   "Prepare ARTICLE in article mode buffer.
10222 ARTICLE should either be an article number or a Message-ID.
10223 If ARTICLE is an id, HEADER should be the article headers.
10224 If ALL-HEADERS is non-nil, no headers are hidden."
10225   (save-excursion
10226     ;; Make sure we start in a summary buffer.
10227     (or (eq major-mode 'gnus-summary-mode)
10228         (set-buffer gnus-summary-buffer))
10229     (setq gnus-summary-buffer (current-buffer))
10230     ;; Make sure the connection to the server is alive.
10231     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10232         (progn
10233           (gnus-check-news-server 
10234            (gnus-find-method-for-group gnus-newsgroup-name))
10235           (gnus-request-group gnus-newsgroup-name t)))
10236     (or gnus-newsgroup-headers-hashtb-by-number
10237         (gnus-make-headers-hashtable-by-number))
10238     (let* ((article (if header (header-number header) article))
10239            (summary-buffer (current-buffer))
10240            (internal-hook gnus-article-internal-prepare-hook)
10241            (group gnus-newsgroup-name)
10242            result)
10243       (save-excursion
10244         (gnus-article-setup-buffer)
10245         (set-buffer gnus-article-buffer)
10246         (if (not (setq result (let ((buffer-read-only nil))
10247                                 (gnus-request-article-this-buffer 
10248                                  article group))))
10249             ;; There is no such article.
10250             (save-excursion
10251               (if (not (numberp article))
10252                   ()
10253                 (setq gnus-article-current 
10254                       (cons gnus-newsgroup-name article))
10255                 (set-buffer gnus-summary-buffer)
10256                 (setq gnus-current-article article)
10257                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10258               (gnus-message 1 "No such article (may be canceled)")
10259               (ding)
10260               nil)
10261           (if (or (eq result 'pseudo) (eq result 'nneething))
10262               (progn
10263                 (save-excursion
10264                   (set-buffer summary-buffer)
10265                   (setq gnus-last-article gnus-current-article
10266                         gnus-newsgroup-history (cons gnus-current-article
10267                                                      gnus-newsgroup-history)
10268                         gnus-current-article 0
10269                         gnus-current-headers nil
10270                         gnus-article-current nil)
10271                   (if (eq result 'nneething)
10272                       (gnus-configure-windows 'summary)
10273                     (gnus-configure-windows 'article))
10274                   (gnus-set-global-variables))
10275                 (gnus-set-mode-line 'article))
10276             ;; The result from the `request' was an actual article -
10277             ;; or at least some text that is now displayed in the
10278             ;; article buffer.
10279             (if (and (numberp article)
10280                      (not (eq article gnus-current-article)))
10281                 ;; Seems like a new article has been selected.
10282                 ;; `gnus-current-article' must be an article number.
10283                 (save-excursion
10284                   (set-buffer summary-buffer)
10285                   (setq gnus-last-article gnus-current-article
10286                         gnus-newsgroup-history (cons gnus-current-article
10287                                                      gnus-newsgroup-history)
10288                         gnus-current-article article
10289                         gnus-current-headers 
10290                         (gnus-get-header-by-number gnus-current-article)
10291                         gnus-article-current 
10292                         (cons gnus-newsgroup-name gnus-current-article))
10293                   (gnus-summary-show-thread)
10294                   (run-hooks 'gnus-mark-article-hook)
10295                   (gnus-set-mode-line 'summary)
10296                   (and gnus-visual 
10297                        (run-hooks 'gnus-visual-mark-article-hook))
10298                   ;; Set the global newsgroup variables here.
10299                   ;; Suggested by Jim Sisolak
10300                   ;; <sisolak@trans4.neep.wisc.edu>.
10301                   (gnus-set-global-variables)
10302                   (setq gnus-have-all-headers 
10303                         (or all-headers gnus-show-all-headers))
10304                   (and gnus-use-cache 
10305                        (gnus-cache-possibly-enter-article
10306                         group article
10307                         (gnus-get-header-by-number article)
10308                         (memq article gnus-newsgroup-marked)
10309                         (memq article gnus-newsgroup-dormant)
10310                         (memq article gnus-newsgroup-unreads)))))
10311             ;; Hooks for getting information from the article.
10312             ;; This hook must be called before being narrowed.
10313             (let (buffer-read-only)
10314               (run-hooks 'internal-hook)
10315               (run-hooks 'gnus-article-prepare-hook)
10316               ;; Decode MIME message.
10317               (if (and gnus-show-mime
10318                        (or (not gnus-strict-mime)
10319                            (gnus-fetch-field "Mime-Version")))
10320                   (funcall gnus-show-mime-method))
10321               ;; Perform the article display hooks.
10322               (run-hooks 'gnus-article-display-hook))
10323             ;; Do page break.
10324             (goto-char (point-min))
10325             (and gnus-break-pages (gnus-narrow-to-page))
10326             (gnus-set-mode-line 'article)
10327             (gnus-configure-windows 'article)
10328             (goto-char (point-min))
10329             t))))))
10330
10331 (defun gnus-article-show-all-headers ()
10332   "Show all article headers in article mode buffer."
10333   (save-excursion 
10334     (gnus-article-setup-buffer)
10335     (set-buffer gnus-article-buffer)
10336     (let ((buffer-read-only nil))
10337       (remove-text-properties (point-min) (point-max) 
10338                               gnus-hidden-properties))))
10339
10340 (defun gnus-article-hide-headers-if-wanted ()
10341   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10342 Provided for backwards compatability."
10343   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10344       (gnus-article-hide-headers)))
10345
10346 (defun gnus-article-hide-headers (&optional delete)
10347   "Hide unwanted headers and possibly sort them as well."
10348   (interactive "P")
10349   (save-excursion
10350     (set-buffer gnus-article-buffer)
10351     (save-restriction
10352       (let ((sorted gnus-sorted-header-list)
10353             (buffer-read-only nil)
10354             want-list beg want-l)
10355         ;; First we narrow to just the headers.
10356         (widen)
10357         (goto-char (point-min))
10358         ;; Hide any "From " lines at the beginning of (mail) articles. 
10359         (while (looking-at "From ")
10360           (forward-line 1))
10361         (if (bobp) 
10362             (add-text-properties (point-min) (point) gnus-hidden-properties))
10363         ;; Then treat the rest of the header lines.
10364         (narrow-to-region 
10365          (point) 
10366          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10367         ;; Then we use the two regular expressions
10368         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10369         ;; select which header lines is to remain visible in the
10370         ;; article buffer.
10371         (goto-char (point-min))
10372         (while (re-search-forward "^[^ \t]*:" nil t)
10373           (beginning-of-line)
10374           ;; We add the headers we want to keep to a list and delete
10375           ;; them from the buffer.
10376           (if (or (and (stringp gnus-visible-headers)
10377                        (looking-at gnus-visible-headers))
10378                   (and (not (stringp gnus-visible-headers))
10379                        (stringp gnus-ignored-headers)
10380                        (not (looking-at gnus-ignored-headers))))
10381               (progn
10382                 (setq beg (point))
10383                 (forward-line 1)
10384                 ;; Be sure to get multi-line headers...
10385                 (re-search-forward "^[^ \t]*:" nil t)
10386                 (beginning-of-line)
10387                 (setq want-list 
10388                       (cons (buffer-substring beg (point)) want-list))
10389                 (delete-region beg (point))
10390                 (goto-char beg))
10391             (forward-line 1)))
10392         ;; Next we perform the sorting by looking at
10393         ;; `gnus-sorted-header-list'. 
10394         (goto-char (point-min))
10395         (while (and sorted want-list)
10396           (setq want-l want-list)
10397           (while (and want-l
10398                       (not (string-match (car sorted) (car want-l))))
10399             (setq want-l (cdr want-l)))
10400           (if want-l 
10401               (progn
10402                 (insert (car want-l))
10403                 (setq want-list (delq (car want-l) want-list))))
10404           (setq sorted (cdr sorted)))
10405         ;; Any headers that were not matched by the sorted list we
10406         ;; just tack on the end of the visible header list.
10407         (while want-list
10408           (insert (car want-list))
10409           (setq want-list (cdr want-list)))
10410         ;; And finally we make the unwanted headers invisible.
10411         (if delete
10412             (delete-region (point) (point-max))
10413           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10414           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10415
10416 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10417 (defun gnus-article-treat-overstrike ()
10418   "Translate overstrikes into bold text."
10419   (interactive)
10420   (save-excursion
10421     (set-buffer gnus-article-buffer)
10422     (let ((buffer-read-only nil))
10423       (while (search-forward "\b" nil t)
10424         (let ((next (following-char))
10425               (previous (char-after (- (point) 2))))
10426           (cond ((eq next previous)
10427                  (delete-region (- (point) 2) (point))
10428                  (put-text-property (point) (1+ (point))
10429                                     'face 'bold))
10430                 ((eq next ?_)
10431                  (delete-region (1- (point)) (1+ (point)))
10432                  (put-text-property (1- (point)) (point)
10433                                     'face 'underline))
10434                 ((eq previous ?_)
10435                  (delete-region (- (point) 2) (point))
10436                  (put-text-property (point) (1+ (point))
10437                                     'face 'underline))))))))
10438
10439 (defun gnus-article-word-wrap ()
10440   "Format too long lines."
10441   (interactive)
10442   (save-excursion
10443     (set-buffer gnus-article-buffer)
10444     (let ((buffer-read-only nil))
10445       (goto-char (point-min))
10446       (search-forward "\n\n" nil t)
10447       (end-of-line 1)
10448       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10449             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10450             (adaptive-fill-mode t))
10451         (while (not (eobp))
10452           (and (>= (current-column) (min fill-column (window-width)))
10453                (/= (preceding-char) ?:)
10454                (fill-paragraph nil))
10455           (end-of-line 2))))))
10456
10457 (defun gnus-article-remove-cr ()
10458   "Remove carriage returns from an article."
10459   (interactive)
10460   (save-excursion
10461     (set-buffer gnus-article-buffer)
10462     (let ((buffer-read-only nil))
10463       (goto-char (point-min))
10464       (while (search-forward "\r" nil t)
10465         (replace-match "" t t)))))
10466
10467 (defun gnus-article-display-x-face (&optional force)
10468   "Look for an X-Face header and display it if present."
10469   (interactive (list 'force))
10470   (save-excursion
10471     (set-buffer gnus-article-buffer)
10472     (let ((inhibit-point-motion-hooks t)
10473           (case-fold-search nil))
10474       (save-restriction
10475         (goto-char (point-min))
10476         (search-forward "\n\n")
10477         (narrow-to-region (point-min) (point))
10478         (goto-char (point-min))
10479         (if (or (not gnus-article-x-face-command)
10480                 (and (not force)
10481                      (or (not gnus-article-x-face-too-ugly)
10482                          (string-match gnus-article-x-face-too-ugly
10483                                        (mail-fetch-field "from"))))
10484                 (progn
10485                   (goto-char (point-min))
10486                   (not (re-search-forward "^X-Face: " nil t))))
10487             nil
10488           (let ((beg (point))
10489                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10490             (if (symbolp gnus-article-x-face-command)
10491                 (and (or (fboundp gnus-article-x-face-command)
10492                          (error "%s is not a function"
10493                                 gnus-article-x-face-command))
10494                      (funcall gnus-article-x-face-command beg end))
10495               (call-process-region beg end "sh" nil 0 nil
10496                                    "-c" gnus-article-x-face-command))))))))
10497
10498 (defun gnus-article-de-quoted-unreadable (&optional force)
10499   "Do a naïve translation of a quoted-printable-encoded article.
10500 This is in no way, shape or form meant as a replacement for real MIME
10501 processing, but is simply a stop-gap measure until MIME support is
10502 written.
10503 If FORCE, decode the article whether it is marked as quoted-printable
10504 or not." 
10505   (interactive (list 'force))
10506   (save-excursion
10507     (set-buffer gnus-article-buffer)
10508     (let ((case-fold-search t)
10509           (buffer-read-only nil)
10510           (type (gnus-fetch-field "content-transfer-encoding")))
10511       (if (or force (and type (string-match "quoted-printable" type)))
10512           (progn
10513             (goto-char (point-min))
10514             (search-forward "\n\n" nil 'move)
10515             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10516
10517 (defun gnus-mime-decode-quoted-printable (from to)
10518   ;; Decode quoted-printable from region between FROM and TO.
10519   (save-excursion
10520     (goto-char from)
10521     (while (search-forward "=" to t)
10522       (cond ((eq (following-char) ?\n)
10523              (delete-char -1)
10524              (delete-char 1))
10525             ((looking-at "[0-9A-F][0-9A-F]")
10526              (delete-char -1)
10527              (insert (hexl-hex-string-to-integer
10528                       (buffer-substring (point) (+ 2 (point)))))
10529              (delete-char 2))
10530             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10531
10532 (defvar gnus-article-time-units
10533   (list (cons 'year (* 365.25 24 60 60))
10534         (cons 'week (* 7 24 60 60))
10535         (cons 'day (* 24 60 60))
10536         (cons 'hour (* 60 60))
10537         (cons 'minute 60)
10538         (cons 'second 1)))
10539
10540 (defun gnus-article-date-ut (&optional type)
10541   "Convert DATE date to universal time in the current article.
10542 If TYPE is `local', convert to local time; if it is `lapsed', output
10543 how much time has lapsed since DATE."
10544   (interactive (list 'ut))
10545   (let ((date (header-date (or gnus-current-headers 
10546                                (gnus-get-header-by-number
10547                                 (gnus-summary-article-number))"")))
10548         (date-regexp "^Date: \\|^X-Sent: "))
10549     (if (or (not date)
10550             (string= date ""))
10551         ()
10552       (save-excursion
10553         (set-buffer gnus-article-buffer)
10554         (let ((buffer-read-only nil))
10555           (goto-char (point-min))
10556           (if (and (re-search-forward date-regexp nil t)
10557                    (progn 
10558                      (beginning-of-line)
10559                      (looking-at date-regexp)))
10560               (delete-region (gnus-point-at-bol)
10561                              (progn (end-of-line) (1+ (point))))
10562             (goto-char (point-min))
10563             (goto-char (- (search-forward "\n\n") 2)))
10564           (insert
10565            (cond 
10566             ((eq type 'local)
10567              (concat "Date: " (condition-case ()
10568                                   (timezone-make-date-arpa-standard date)
10569                                 (error date))
10570                      "\n"))
10571             ((eq type 'ut)
10572              (concat "Date: "
10573                      (condition-case ()
10574                          (timezone-make-date-arpa-standard date nil "UT")
10575                        (error date))
10576                      "\n"))
10577             ((eq type 'lapsed)
10578              ;; If the date is seriously mangled, the timezone
10579              ;; functions are liable to bug out, so we condition-case
10580              ;; the entire thing.  
10581              (let* ((sec (condition-case ()
10582                              (max (- (gnus-seconds-since-epoch 
10583                                       (timezone-make-date-arpa-standard
10584                                        (current-time-string) 
10585                                        (current-time-zone) "UT"))
10586                                      (gnus-seconds-since-epoch 
10587                                       (timezone-make-date-arpa-standard 
10588                                        date nil "UT")))
10589                                   0)
10590                            (error 0)))
10591                     num prev)
10592                (concat
10593                 "X-Sent: "
10594                 (mapconcat 
10595                  (lambda (unit)
10596                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10597                        ""
10598                      (setq sec (- sec (* num (cdr unit))))
10599                      (prog1
10600                          (concat (if prev ", " "") (int-to-string (floor num))
10601                                  " " (symbol-name (car unit))
10602                                  (if (> num 1) "s" ""))
10603                        (setq prev t))))
10604                  gnus-article-time-units "")
10605                 " ago\n")))
10606             (t
10607              (error "Unknown conversion type: %s" type)))))))))
10608
10609 (defun gnus-article-date-local ()
10610   "Convert the current article date to the local timezone."
10611   (interactive)
10612   (gnus-article-date-ut 'local))
10613
10614 (defun gnus-article-date-lapsed ()
10615   "Convert the current article date to time lapsed since it was sent."
10616   (interactive)
10617   (gnus-article-date-ut 'lapsed))
10618
10619 (defun gnus-article-maybe-highlight ()
10620   (if gnus-visual (gnus-article-highlight)))
10621
10622 ;; Article savers.
10623
10624 (defun gnus-output-to-rmail (file-name)
10625   "Append the current article to an Rmail file named FILE-NAME."
10626   (require 'rmail)
10627   ;; Most of these codes are borrowed from rmailout.el.
10628   (setq file-name (expand-file-name file-name))
10629   (setq rmail-default-rmail-file file-name)
10630   (let ((artbuf (current-buffer))
10631         (tmpbuf (get-buffer-create " *Gnus-output*")))
10632     (save-excursion
10633       (or (get-file-buffer file-name)
10634           (file-exists-p file-name)
10635           (if (gnus-yes-or-no-p
10636                (concat "\"" file-name "\" does not exist, create it? "))
10637               (let ((file-buffer (create-file-buffer file-name)))
10638                 (save-excursion
10639                   (set-buffer file-buffer)
10640                   (rmail-insert-rmail-file-header)
10641                   (let ((require-final-newline nil))
10642                     (write-region (point-min) (point-max) file-name t 1)))
10643                 (kill-buffer file-buffer))
10644             (error "Output file does not exist")))
10645       (set-buffer tmpbuf)
10646       (buffer-disable-undo (current-buffer))
10647       (erase-buffer)
10648       (insert-buffer-substring artbuf)
10649       (gnus-convert-article-to-rmail)
10650       ;; Decide whether to append to a file or to an Emacs buffer.
10651       (let ((outbuf (get-file-buffer file-name)))
10652         (if (not outbuf)
10653             (append-to-file (point-min) (point-max) file-name)
10654           ;; File has been visited, in buffer OUTBUF.
10655           (set-buffer outbuf)
10656           (let ((buffer-read-only nil)
10657                 (msg (and (boundp 'rmail-current-message)
10658                           (symbol-value 'rmail-current-message))))
10659             ;; If MSG is non-nil, buffer is in RMAIL mode.
10660             (if msg
10661                 (progn (widen)
10662                        (narrow-to-region (point-max) (point-max))))
10663             (insert-buffer-substring tmpbuf)
10664             (if msg
10665                 (progn
10666                   (goto-char (point-min))
10667                   (widen)
10668                   (search-backward "\^_")
10669                   (narrow-to-region (point) (point-max))
10670                   (goto-char (1+ (point-min)))
10671                   (rmail-count-new-messages t)
10672                   (rmail-show-message msg)))))))
10673     (kill-buffer tmpbuf)))
10674
10675 (defun gnus-output-to-file (file-name)
10676   "Append the current article to a file named FILE-NAME."
10677   (setq file-name (expand-file-name file-name))
10678   (let ((artbuf (current-buffer))
10679         (tmpbuf (get-buffer-create " *Gnus-output*")))
10680     (save-excursion
10681       (set-buffer tmpbuf)
10682       (buffer-disable-undo (current-buffer))
10683       (erase-buffer)
10684       (insert-buffer-substring artbuf)
10685       ;; Append newline at end of the buffer as separator, and then
10686       ;; save it to file.
10687       (goto-char (point-max))
10688       (insert "\n")
10689       (append-to-file (point-min) (point-max) file-name))
10690     (kill-buffer tmpbuf)))
10691
10692 (defun gnus-convert-article-to-rmail ()
10693   "Convert article in current buffer to Rmail message format."
10694   (let ((buffer-read-only nil))
10695     ;; Convert article directly into Babyl format.
10696     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10697     (goto-char (point-min))
10698     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10699     (while (search-forward "\n\^_" nil t) ;single char
10700       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10701     (goto-char (point-max))
10702     (insert "\^_")))
10703
10704 (defun gnus-narrow-to-page (&optional arg)
10705   "Make text outside current page invisible except for page delimiter.
10706 A numeric arg specifies to move forward or backward by that many pages,
10707 thus showing a page other than the one point was originally in."
10708   (interactive "P")
10709   (setq arg (if arg (prefix-numeric-value arg) 0))
10710   (save-excursion
10711     (forward-page -1)                   ;Beginning of current page.
10712     (widen)
10713     (if (> arg 0)
10714         (forward-page arg)
10715       (if (< arg 0)
10716           (forward-page (1- arg))))
10717     ;; Find the end of the page.
10718     (forward-page)
10719     ;; If we stopped due to end of buffer, stay there.
10720     ;; If we stopped after a page delimiter, put end of restriction
10721     ;; at the beginning of that line.
10722     ;; These are commented out.
10723     ;;    (if (save-excursion (beginning-of-line)
10724     ;;                  (looking-at page-delimiter))
10725     ;;  (beginning-of-line))
10726     (narrow-to-region (point)
10727                       (progn
10728                         ;; Find the top of the page.
10729                         (forward-page -1)
10730                         ;; If we found beginning of buffer, stay there.
10731                         ;; If extra text follows page delimiter on same line,
10732                         ;; include it.
10733                         ;; Otherwise, show text starting with following line.
10734                         (if (and (eolp) (not (bobp)))
10735                             (forward-line 1))
10736                         (point)))))
10737
10738 (defun gnus-gmt-to-local ()
10739   "Rewrite Date header described in GMT to local in current buffer.
10740 Intended to be used with gnus-article-prepare-hook."
10741   (save-excursion
10742     (save-restriction
10743       (widen)
10744       (goto-char (point-min))
10745       (narrow-to-region (point-min)
10746                         (progn (search-forward "\n\n" nil 'move) (point)))
10747       (goto-char (point-min))
10748       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10749           (let ((buffer-read-only nil)
10750                 (date (buffer-substring-no-properties
10751                        (match-beginning 1) (match-end 1))))
10752             (delete-region (match-beginning 1) (match-end 1))
10753             (insert
10754              (timezone-make-date-arpa-standard 
10755               date nil (current-time-zone))))))))
10756
10757
10758 ;; Article mode commands
10759
10760 (defun gnus-article-next-page (lines)
10761   "Show next page of current article.
10762 If end of article, return non-nil. Otherwise return nil.
10763 Argument LINES specifies lines to be scrolled up."
10764   (interactive "P")
10765   (move-to-window-line -1)
10766   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10767   (if (save-excursion
10768         (end-of-line)
10769         (and (pos-visible-in-window-p)  ;Not continuation line.
10770              (eobp)))
10771       ;; Nothing in this page.
10772       (if (or (not gnus-break-pages)
10773               (save-excursion
10774                 (save-restriction
10775                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10776           t                             ;Nothing more.
10777         (gnus-narrow-to-page 1)         ;Go to next page.
10778         nil)
10779     ;; More in this page.
10780     (condition-case ()
10781         (scroll-up lines)
10782       (end-of-buffer
10783        ;; Long lines may cause an end-of-buffer error.
10784        (goto-char (point-max))))
10785     nil))
10786
10787 (defun gnus-article-prev-page (lines)
10788   "Show previous page of current article.
10789 Argument LINES specifies lines to be scrolled down."
10790   (interactive "P")
10791   (move-to-window-line 0)
10792   (if (and gnus-break-pages
10793            (bobp)
10794            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10795       (progn
10796         (gnus-narrow-to-page -1) ;Go to previous page.
10797         (goto-char (point-max))
10798         (recenter -1))
10799     (scroll-down lines)))
10800
10801 (defun gnus-article-refer-article ()
10802   "Read article specified by message-id around point."
10803   (interactive)
10804   (search-forward ">" nil t)    ;Move point to end of "<....>".
10805   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10806       (let ((message-id
10807              (buffer-substring (match-beginning 1) (match-end 1))))
10808         (set-buffer gnus-summary-buffer)
10809         (gnus-summary-refer-article message-id))
10810     (error "No references around point")))
10811
10812 (defun gnus-article-show-summary ()
10813   "Reconfigure windows to show summary buffer."
10814   (interactive)
10815   (gnus-configure-windows 'article)
10816   (gnus-summary-goto-subject gnus-current-article))
10817
10818 (defun gnus-article-describe-briefly ()
10819   "Describe article mode commands briefly."
10820   (interactive)
10821   (gnus-message 6
10822    (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")))
10823
10824 (defun gnus-article-summary-command ()
10825   "Execute the last keystroke in the summary buffer."
10826   (interactive)
10827   (let ((obuf (current-buffer))
10828         (owin (current-window-configuration))
10829         func)
10830     (switch-to-buffer gnus-summary-buffer 'norecord)
10831     (setq func (lookup-key (current-local-map) (this-command-keys)))
10832     (call-interactively func)
10833     (set-buffer obuf)
10834     (set-window-configuration owin)
10835     (set-window-start (get-buffer-window (current-buffer)) (point))))
10836
10837 (defun gnus-article-summary-command-nosave ()
10838   "Execute the last keystroke in the summary buffer."
10839   (interactive)
10840   (let (func)
10841     (pop-to-buffer gnus-summary-buffer 'norecord)
10842     (setq func (lookup-key (current-local-map) (this-command-keys)))
10843     (call-interactively func)))
10844
10845 \f
10846 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10847
10848 ;;;###autoload
10849 (defalias 'gnus-batch-kill 'gnus-batch-score)
10850 ;;;###autoload
10851 (defun gnus-batch-score ()
10852   "Run batched scoring.
10853 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10854 Newsgroups is a list of strings in Bnews format.  If you want to score
10855 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10856 score the alt hierarchy, you'd say \"!alt.all\"."
10857   (interactive)
10858   (let* ((yes-and-no
10859           (gnus-newsrc-parse-options
10860            (apply (function concat)
10861                   (mapcar (lambda (g) (concat g " "))
10862                           command-line-args-left))))
10863          (gnus-expert-user t)
10864          (nnmail-spool-file nil)
10865          (gnus-use-dribble-file nil)
10866          (yes (car yes-and-no))
10867          (no (cdr yes-and-no))
10868          group newsrc entry
10869          ;; Disable verbose message.
10870          gnus-novice-user gnus-large-newsgroup)
10871     ;; Eat all arguments.
10872     (setq command-line-args-left nil)
10873     ;; Start Gnus.
10874     (gnus)
10875     ;; Apply kills to specified newsgroups in command line arguments.
10876     (setq newsrc (cdr gnus-newsrc-alist))
10877     (while newsrc
10878       (setq group (car (car newsrc)))
10879       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10880       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10881                (and (car entry)
10882                     (or (eq (car entry) t)
10883                         (not (zerop (car entry)))))
10884                (if yes (string-match yes group) t)
10885                (or (null no) (not (string-match no group))))
10886           (progn
10887             (gnus-summary-read-group group nil t)
10888             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10889                  (gnus-summary-exit))))
10890       (setq newsrc (cdr newsrc)))
10891     ;; Exit Emacs.
10892     (switch-to-buffer gnus-group-buffer)
10893     (gnus-group-save-newsrc)))
10894
10895 (defun gnus-apply-kill-file ()
10896   "Apply a kill file to the current newsgroup.
10897 Returns the number of articles marked as read."
10898   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10899           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10900       (gnus-apply-kill-file-internal)
10901     0))
10902
10903 (defun gnus-kill-save-kill-buffer ()
10904   (save-excursion
10905     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10906       (if (get-file-buffer file)
10907           (progn
10908             (set-buffer (get-file-buffer file))
10909             (and (buffer-modified-p) (save-buffer))
10910             (kill-buffer (current-buffer)))))))
10911
10912 (defvar gnus-kill-file-name "KILL"
10913   "Suffix of the kill files.")
10914
10915 (defun gnus-newsgroup-kill-file (newsgroup)
10916   "Return the name of a kill file name for NEWSGROUP.
10917 If NEWSGROUP is nil, return the global kill file name instead."
10918   (cond ((or (null newsgroup)
10919              (string-equal newsgroup ""))
10920          ;; The global KILL file is placed at top of the directory.
10921          (expand-file-name gnus-kill-file-name
10922                            (or gnus-kill-files-directory "~/News")))
10923         ((gnus-use-long-file-name 'not-kill)
10924          ;; Append ".KILL" to newsgroup name.
10925          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10926                            (or gnus-kill-files-directory "~/News")))
10927         (t
10928          ;; Place "KILL" under the hierarchical directory.
10929          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10930                                    "/" gnus-kill-file-name)
10931                            (or gnus-kill-files-directory "~/News")))))
10932
10933 \f
10934 ;;;
10935 ;;; Dribble file
10936 ;;;
10937
10938 (defvar gnus-dribble-ignore nil)
10939
10940 (defun gnus-dribble-file-name ()
10941   (concat gnus-startup-file "-dribble"))
10942
10943 (defun gnus-dribble-open ()
10944   (save-excursion 
10945     (set-buffer 
10946      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10947     (buffer-disable-undo (current-buffer))
10948     (bury-buffer gnus-dribble-buffer)
10949     (auto-save-mode t)
10950     (goto-char (point-max))))
10951
10952 (defun gnus-dribble-enter (string)
10953   (if (and (not gnus-dribble-ignore)
10954            gnus-dribble-buffer
10955            (buffer-name gnus-dribble-buffer))
10956       (let ((obuf (current-buffer)))
10957         (set-buffer gnus-dribble-buffer)
10958         (insert string "\n")
10959         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10960         (set-buffer obuf))))
10961
10962 (defun gnus-dribble-read-file ()
10963   (let ((dribble-file (gnus-dribble-file-name)))
10964     (save-excursion 
10965       (set-buffer (setq gnus-dribble-buffer 
10966                         (get-buffer-create 
10967                          (file-name-nondirectory dribble-file))))
10968       (gnus-add-current-to-buffer-list)
10969       (erase-buffer)
10970       (set-visited-file-name dribble-file)
10971       (buffer-disable-undo (current-buffer))
10972       (bury-buffer (current-buffer))
10973       (set-buffer-modified-p nil)
10974       (let ((auto (make-auto-save-file-name))
10975             (gnus-dribble-ignore t))
10976         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10977             (progn
10978               (if (file-newer-than-file-p auto dribble-file)
10979                   (setq dribble-file auto))
10980               (insert-file-contents dribble-file)
10981               (if (not (zerop (buffer-size)))
10982                   (set-buffer-modified-p t))
10983               (if (gnus-y-or-n-p 
10984                    "Auto-save file exists. Do you want to read it? ")
10985                   (progn
10986                     (gnus-message 5 "Reading %s..." dribble-file) 
10987                     (eval-buffer (current-buffer))
10988                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10989
10990 (defun gnus-dribble-delete-file ()
10991   (if (file-exists-p (gnus-dribble-file-name))
10992       (delete-file (gnus-dribble-file-name)))
10993   (if gnus-dribble-buffer
10994       (save-excursion
10995         (set-buffer gnus-dribble-buffer)
10996         (let ((auto (make-auto-save-file-name)))
10997           (if (file-exists-p auto)
10998               (delete-file auto))
10999           (erase-buffer)
11000           (set-buffer-modified-p nil)))))
11001
11002 (defun gnus-dribble-save ()
11003   (if (and gnus-dribble-buffer
11004            (buffer-name gnus-dribble-buffer))
11005       (save-excursion
11006         (set-buffer gnus-dribble-buffer)
11007         (save-buffer))))
11008
11009 (defun gnus-dribble-clear ()
11010   (save-excursion
11011     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11012         (progn
11013           (set-buffer gnus-dribble-buffer)
11014           (erase-buffer)
11015           (set-buffer-modified-p nil)
11016           (setq buffer-saved-size (buffer-size))))))
11017
11018 ;;;
11019 ;;; Server Communication
11020 ;;;
11021
11022 ;; All the Gnus backends have the same interface, and should return
11023 ;; data in a similar format. Below is an overview of what functions
11024 ;; these packages must supply and what results they should return.
11025 ;;
11026 ;; Variables:
11027 ;;
11028 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11029 ;; buffer. 
11030 ;;
11031 ;; Functions for the imaginary backend `choke':
11032 ;;
11033 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11034 ;; Should return all headers for all ARTICLES, or return NOV lines for
11035 ;; the same.
11036 ;;
11037 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11038 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11039 ;; must be returned.
11040 ;;
11041 ;; `choke-close-group GROUP &optional SERVER'
11042 ;; Close group. Most backends won't have to do anything with this
11043 ;; call, but it is an opportunity to clean up, if that is needed. It
11044 ;; is called when Gnus exits a group.
11045 ;;
11046 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11047 ;; Return ARTICLE, which is either an article number or
11048 ;; message-id. Note that not all backends can return articles based on
11049 ;; message-id. 
11050 ;;
11051 ;; `choke-request-list SERVER'
11052 ;; Return a list of all newsgroups on SERVER.
11053 ;;
11054 ;; `choke-request-list-newsgroups SERVER'
11055 ;; Return a list of descriptions of all newsgroups on SERVER.
11056 ;;
11057 ;; `choke-request-newgroups DATE &optional SERVER'
11058 ;; Return a list of all groups that have arrived after DATE on
11059 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11060 ;; always check whether the groups are old or not. Backends that do
11061 ;; not store date information may just return the entire list of
11062 ;; groups, although this might not be a good idea in general.
11063 ;;
11064 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11065 ;; Should return a buffer that is suitable for "posting". nnspool and
11066 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11067 ;; buffer. This function should fill out the appropriate headers. 
11068 ;;
11069 ;; `choke-request-post &optional SERVER'
11070 ;; Function that will be called from a buffer to be posted. 
11071 ;;
11072 ;; `choke-open-server SERVER &optional ARGUMENT'
11073 ;; Open a connection to SERVER.
11074 ;;
11075 ;; `choke-close-server &optional SERVER'
11076 ;; Close the connection to SERVER.
11077 ;;
11078 ;; `choke-server-opened &optional SERVER'
11079 ;; Whether the conenction to SERVER is opened or not.
11080 ;;
11081 ;; `choke-server-status &optional SERVER'
11082 ;; Should return a status string (not in the nntp buffer, but as the
11083 ;; result of the function).
11084 ;;
11085 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11086 ;; Optional function for retrieving active file info on all groups in
11087 ;; GROUPS.  Two return formats are supported: The normal active file
11088 ;; format, and a list of GROUP lines.  This function should return (as
11089 ;; a function value) either `active' or `group', depending on what
11090 ;; format it returns.
11091 ;;
11092 ;; The following functions are optional and apply only to backends
11093 ;; that are able to control the contents of their groups totally
11094 ;; (ie. mail backends.)  Backends that aren't able to do that
11095 ;; shouldn't define these functions at all. Gnus will check for their
11096 ;; presence before attempting to call them.
11097 ;;
11098 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11099 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11100 ;; backends will not be able to expire articles. Should return a list
11101 ;; of all articles that were not expired.
11102 ;;
11103 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11104 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11105 ;; Removes any information it has added to the article (extra headers,
11106 ;; whatever - make it as clean as possible), and then passes the
11107 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11108 ;; function described below. If the ACCEPT-FORM returns a non-nil
11109 ;; value, the article should then be deleted. If LAST is nil, that
11110 ;; means that there will be further calls to this function. This might
11111 ;; be taken as an advice not to save buffers/internal variables just
11112 ;; yet, but wait until the last call to speed things up.
11113 ;;
11114 ;; `choke-request-accept-article GROUP &optional LAST' 
11115 ;; The contents of the current buffer will be put into GROUP.  There
11116 ;; should, of course, be an article in the current buffer.  This
11117 ;; function is normally only called by the function described above,
11118 ;; and LAST works the same way as in that function.
11119 ;;
11120 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11121 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11122 ;; This provides an easy interface for allowing editing of
11123 ;; articles. Note that even headers may be edited, so the backend has
11124 ;; to update any tables (nov buffers, etc) that it maintains after
11125 ;; replacing the article.
11126 ;;
11127 ;; `choke-request-create-group GROUP &optional SERVER'
11128 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11129 ;; might be a group that already exists, but hasn't been registered
11130 ;; yet. 
11131 ;;
11132 ;; All these functions must return nil if they couldn't service the
11133 ;; request. If the optional arguments are not supplied, some "current"
11134 ;; or "default" values should be used. In short, one should emulate an
11135 ;; NNTP server, in a way.
11136 ;;
11137 ;; If you want to write a new backend, you just have to supply the
11138 ;; functions listed above. In addition, you must enter the new backend
11139 ;; into the list of valid select methods:
11140 ;; (setq gnus-valid-select-methods 
11141 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11142 ;; The first element in this list is the name of the backend. Other
11143 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11144 ;; groups), `none' (neither), `respool' (for groups that can control
11145 ;; their contents). 
11146
11147 (defun gnus-start-news-server (&optional confirm)
11148   "Open a method for getting news.
11149 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11150   (let (how)
11151     (if gnus-current-select-method
11152         ;; Stream is already opened.
11153         nil
11154       ;; Open NNTP server.
11155       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11156       (if confirm
11157           (progn
11158             ;; Read server name with completion.
11159             (setq gnus-nntp-server
11160                   (completing-read "NNTP server: "
11161                                    (mapcar (lambda (server) (list server))
11162                                            (cons (list gnus-nntp-server)
11163                                                  gnus-secondary-servers))
11164                                    nil nil gnus-nntp-server))))
11165
11166       (if (and gnus-nntp-server 
11167                (stringp gnus-nntp-server)
11168                (not (string= gnus-nntp-server "")))
11169           (setq gnus-select-method
11170                 (cond ((or (string= gnus-nntp-server "")
11171                            (string= gnus-nntp-server "::"))
11172                        (list 'nnspool (system-name)))
11173                       ((string-match "^:" gnus-nntp-server)
11174                        (list 'nnmh gnus-nntp-server 
11175                              (list 'nnmh-directory 
11176                                    (file-name-as-directory
11177                                     (expand-file-name
11178                                      (concat "~/" (substring
11179                                                    gnus-nntp-server 1)))))
11180                              (list 'nnmh-get-new-mail nil)))
11181                       (t
11182                        (list 'nntp gnus-nntp-server)))))
11183
11184       (setq how (car gnus-select-method))
11185       (cond ((eq how 'nnspool)
11186              (require 'nnspool)
11187              (gnus-message 5 "Looking up local news spool..."))
11188             ((eq how 'nnmh)
11189              (require 'nnmh)
11190              (gnus-message 5 "Looking up mh spool..."))
11191             (t
11192              (require 'nntp)))
11193       (setq gnus-current-select-method gnus-select-method)
11194       (run-hooks 'gnus-open-server-hook)
11195       (or 
11196        ;; gnus-open-server-hook might have opened it
11197        (gnus-server-opened gnus-select-method)  
11198        (gnus-open-server gnus-select-method)
11199        (gnus-y-or-n-p
11200         (format
11201          "%s server on %s can't be opened. Continue? "
11202          (car gnus-select-method) (nth 1 gnus-select-method)))
11203        (progn
11204          (gnus-message 1 "Couldn't open server on %s" 
11205                        (nth 1 gnus-select-method))
11206          (ding)
11207          nil)))))
11208
11209 (defun gnus-check-news-server (&optional method)
11210   "If the news server is down, start it up again."
11211   (let ((method (if method method gnus-select-method)))
11212     (and (stringp method)
11213          (setq method (gnus-server-to-method method)))
11214     (if (gnus-server-opened method)
11215         ;; Stream is already opened.
11216         t
11217       ;; Open server.
11218       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11219       (run-hooks 'gnus-open-server-hook)
11220       (or (gnus-server-opened method)
11221           (gnus-open-server method))
11222       (message ""))))
11223
11224 (defun gnus-nntp-message (&optional message)
11225   "Check the status of the NNTP server.
11226 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11227 is returned insted of the status string."
11228   (let ((status (gnus-status-message (gnus-find-method-for-group 
11229                                       gnus-newsgroup-name)))
11230         (message (or message "")))
11231     (if (and (stringp status) (> (length status) 0))
11232         status message)))
11233
11234 (defun gnus-get-function (method function)
11235   (and (stringp method)
11236        (setq method (gnus-server-to-method method)))
11237   (let ((func (intern (format "%s-%s" (car method) function))))
11238     (if (not (fboundp func)) 
11239         (progn
11240           (require (car method))
11241           (if (not (fboundp func)) 
11242               (error "No such function: %s" func))))
11243     func))
11244
11245 ;;; Interface functions to the backends.
11246
11247 (defun gnus-open-server (method)
11248   (funcall (gnus-get-function method 'open-server)
11249            (nth 1 method) (nthcdr 2 method)))
11250
11251 (defun gnus-close-server (method)
11252   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11253
11254 (defun gnus-request-list (method)
11255   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11256
11257 (defun gnus-request-list-newsgroups (method)
11258   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11259
11260 (defun gnus-request-newgroups (date method)
11261   (funcall (gnus-get-function method 'request-newgroups) 
11262            date (nth 1 method)))
11263
11264 (defun gnus-server-opened (method)
11265   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11266
11267 (defun gnus-status-message (method)
11268   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11269                   method)))
11270     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11271
11272 (defun gnus-request-group (group &optional dont-check)
11273   (let ((method (gnus-find-method-for-group group)))
11274 ;    (and t (message "%s GROUP %s" (car method) group))
11275     (funcall (gnus-get-function method 'request-group) 
11276              (gnus-group-real-name group) (nth 1 method) dont-check)))
11277
11278 (defun gnus-request-asynchronous (group &optional articles)
11279   (let ((method (gnus-find-method-for-group group)))
11280     (funcall (gnus-get-function method 'request-asynchronous) 
11281              (gnus-group-real-name group) (nth 1 method) articles)))
11282
11283 (defun gnus-list-active-group (group)
11284   (let ((method (gnus-find-method-for-group group))
11285         (func 'list-active-group))
11286     (and (gnus-check-backend-function func group)
11287          (funcall (gnus-get-function method func) 
11288                   (gnus-group-real-name group) (nth 1 method)))))
11289
11290 (defun gnus-request-group-description (group)
11291   (let ((method (gnus-find-method-for-group group))
11292         (func 'request-group-description))
11293     (and (gnus-check-backend-function func group)
11294          (funcall (gnus-get-function method func) 
11295                   (gnus-group-real-name group) (nth 1 method)))))
11296
11297 (defun gnus-close-group (group)
11298   (let ((method (gnus-find-method-for-group group)))
11299     (funcall (gnus-get-function method 'close-group) 
11300              (gnus-group-real-name group) (nth 1 method))))
11301
11302 (defun gnus-retrieve-headers (articles group)
11303   (let ((method (gnus-find-method-for-group group)))
11304     (if (and gnus-use-cache (numberp (car articles)))
11305         (gnus-cache-retrieve-headers articles group)
11306       (funcall (gnus-get-function method 'retrieve-headers) 
11307                articles (gnus-group-real-name group) (nth 1 method)))))
11308
11309 (defun gnus-retrieve-groups (groups method)
11310   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11311
11312 (defun gnus-request-article (article group &optional buffer)
11313   (let ((method (gnus-find-method-for-group group)))
11314     (funcall (gnus-get-function method 'request-article) 
11315              article (gnus-group-real-name group) (nth 1 method) buffer)))
11316
11317 (defun gnus-request-head (article group)
11318   (let ((method (gnus-find-method-for-group group)))
11319     (funcall (gnus-get-function method 'request-head) 
11320              article (gnus-group-real-name group) (nth 1 method))))
11321
11322 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11323 (defun gnus-request-post-buffer (post group subject header artbuf
11324                                       info follow-to respect-poster)
11325    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11326                                             group gnus-newsrc-hashtb)))))
11327           (method
11328            (if (and gnus-post-method
11329                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11330                     (memq 'post (assoc
11331                                  (format "%s" (car (gnus-find-method-for-group
11332                                                     gnus-newsgroup-name)))
11333                                         gnus-valid-select-methods)))
11334                gnus-post-method
11335              (gnus-find-method-for-group gnus-newsgroup-name))))
11336      (or (gnus-server-opened method)
11337          (gnus-open-server method)
11338          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11339      (let ((mail-self-blind nil)
11340            (mail-archive-file-name nil))
11341        (funcall (gnus-get-function method 'request-post-buffer) 
11342                 post group subject header artbuf info follow-to
11343                 respect-poster))))
11344
11345 (defun gnus-request-post (method &optional force)
11346   (and (stringp method)
11347        (setq method (gnus-server-to-method method)))
11348   (and (not force) gnus-post-method
11349        (memq 'post (assoc (format "%s" (car method))
11350                           gnus-valid-select-methods))
11351        (setq method gnus-post-method))
11352   (funcall (gnus-get-function method 'request-post) 
11353            (nth 1 method)))
11354
11355 (defun gnus-request-expire-articles (articles group &optional force)
11356   (let ((method (gnus-find-method-for-group group)))
11357     (funcall (gnus-get-function method 'request-expire-articles) 
11358              articles (gnus-group-real-name group) (nth 1 method)
11359              force)))
11360
11361 (defun gnus-request-move-article 
11362   (article group server accept-function &optional last)
11363   (let ((method (gnus-find-method-for-group group)))
11364     (funcall (gnus-get-function method 'request-move-article) 
11365              article (gnus-group-real-name group) 
11366              (nth 1 method) accept-function last)))
11367
11368 (defun gnus-request-accept-article (group &optional last)
11369   (let ((func (if (symbolp group) group
11370                 (car (gnus-find-method-for-group group)))))
11371     (funcall (intern (format "%s-request-accept-article" func))
11372              (if (stringp group) (gnus-group-real-name group) group)
11373              last)))
11374
11375 (defun gnus-request-replace-article (article group buffer)
11376   (let ((func (car (gnus-find-method-for-group group))))
11377     (funcall (intern (format "%s-request-replace-article" func))
11378              article (gnus-group-real-name group) buffer)))
11379
11380 (defun gnus-request-create-group (group)
11381   (let ((method (gnus-find-method-for-group group)))
11382     (funcall (gnus-get-function method 'request-create-group) 
11383              (gnus-group-real-name group) (nth 1 method))))
11384
11385 (defun gnus-member-of-valid (symbol group)
11386   (memq symbol (assoc
11387                 (format "%s" (car (gnus-find-method-for-group group)))
11388                 gnus-valid-select-methods)))
11389
11390 (defsubst gnus-secondary-method-p (method)
11391   (member method gnus-secondary-select-methods))
11392
11393 (defun gnus-find-method-for-group (group &optional info)
11394   (or gnus-override-method
11395       (and (not group)
11396            gnus-select-method)
11397       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11398             method)
11399         (if (or (not info)
11400                 (not (setq method (nth 4 info))))
11401             (setq method gnus-select-method)
11402           (setq method
11403                 (cond ((stringp method)
11404                        (gnus-server-to-method method))
11405                       ((stringp (car method))
11406                        (gnus-server-extend-method group method))
11407                       (t
11408                        method))))
11409         (gnus-server-add-address method))))
11410
11411 (defun gnus-check-backend-function (func group)
11412   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11413                  group)))
11414     (fboundp (intern (format "%s-%s" method func)))))
11415
11416 (defun gnus-methods-using (method)
11417   (let ((valids gnus-valid-select-methods)
11418         outs)
11419     (while valids
11420       (if (memq method (car valids)) 
11421           (setq outs (cons (car valids) outs)))
11422       (setq valids (cdr valids)))
11423     outs))
11424
11425 ;;; 
11426 ;;; Active & Newsrc File Handling
11427 ;;;
11428
11429 ;; Newsrc related functions.
11430 ;; Gnus internal format of gnus-newsrc-alist:
11431 ;; (("alt.general" 3 (1 . 1))
11432 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11433 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11434 ;; The first item is the group name; the second is the subscription
11435 ;; level; the third is either a range of a list of ranges of read
11436 ;; articles, the optional fourth element is a list of marked articles,
11437 ;; the optional fifth element is the select method.
11438 ;;
11439 ;; Gnus internal format of gnus-newsrc-hashtb:
11440 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11441 ;; This is the entry for "alt.misc". The first element is the number
11442 ;; of unread articles in "alt.misc". The cdr of this entry is the
11443 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11444 ;; trivial to remove or add new elements into gnus-newsrc-alist
11445 ;; without scanning the entire list. So, to get the actual information
11446 ;; of "alt.misc", you'd say something like 
11447 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11448 ;;
11449 ;; Gnus internal format of gnus-active-hashtb:
11450 ;; ((1 . 1))
11451 ;;  (5 . 10))
11452 ;;  (67 . 99)) ...)
11453 ;; The only element in each entry in this hash table is a range of
11454 ;; (possibly) available articles. (Articles in this range may have
11455 ;; been expired or canceled.)
11456 ;;
11457 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11458 ;; ("alt.misc" "alt.test" "alt.general" ...)
11459
11460 (defun gnus-setup-news (&optional rawfile level)
11461   "Setup news information.
11462 If RAWFILE is non-nil, the .newsrc file will also be read.
11463 If LEVEL is non-nil, the news will be set up at level LEVEL."
11464   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11465     ;; Clear some variables to re-initialize news information.
11466     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11467
11468     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11469     (if init (gnus-read-newsrc-file rawfile))
11470
11471     ;; Read the active file and create `gnus-active-hashtb'.
11472     ;; If `gnus-read-active-file' is nil, then we just create an empty
11473     ;; hash table. The partial filling out of the hash table will be
11474     ;; done in `gnus-get-unread-articles'.
11475     (if (and gnus-read-active-file 
11476              (not level))
11477         (gnus-read-active-file)
11478       (setq gnus-active-hashtb (make-vector 4095 0)))
11479
11480     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11481
11482     ;; Find the number of unread articles in each non-dead group.
11483     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11484     ;; Find new newsgroups and treat them.
11485     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11486              (gnus-server-opened gnus-select-method))
11487         (gnus-find-new-newsgroups))
11488     (if (and init gnus-check-bogus-newsgroups 
11489              gnus-read-active-file (not level)
11490              (gnus-server-opened gnus-select-method))
11491         (gnus-check-bogus-newsgroups))))
11492
11493 (defun gnus-find-new-newsgroups ()
11494   "Search for new newsgroups and add them.
11495 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11496 The `-n' option line from .newsrc is respected."
11497   (interactive)
11498   (or (gnus-check-first-time-used)
11499       (if (or (consp gnus-check-new-newsgroups)
11500               (eq gnus-check-new-newsgroups 'ask-server))
11501           (gnus-ask-server-for-new-groups)
11502         (let ((groups 0)
11503               group new-newsgroups)
11504           (or gnus-have-read-active-file (gnus-read-active-file))
11505           (setq gnus-newsrc-last-checked-date (current-time-string))
11506           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11507           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11508           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11509           (mapatoms
11510            (lambda (sym)
11511              (setq group (symbol-name sym))
11512              (if (or (gnus-gethash group gnus-killed-hashtb)
11513                      (gnus-gethash group gnus-newsrc-hashtb))
11514                  ()
11515                (let ((do-sub (gnus-matches-options-n group)))
11516                  (cond ((eq do-sub 'subscribe)
11517                         (setq groups (1+ groups))
11518                         (gnus-sethash group group gnus-killed-hashtb)
11519                         (funcall 
11520                          gnus-subscribe-options-newsgroup-method group))
11521                        ((eq do-sub 'ignore)
11522                         nil)
11523                        (t
11524                         (setq groups (1+ groups))
11525                         (gnus-sethash group group gnus-killed-hashtb)
11526                         (if gnus-subscribe-hierarchical-interactive
11527                             (setq new-newsgroups (cons group new-newsgroups))
11528                           (funcall gnus-subscribe-newsgroup-method group)))))))
11529            gnus-active-hashtb)
11530           (if new-newsgroups 
11531               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11532           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11533           (if (> groups 0)
11534               (gnus-message 6 "%d new newsgroup%s arrived." 
11535                             groups (if (> groups 1) "s have" " has")))))))
11536
11537 (defun gnus-matches-options-n (group)
11538   ;; Returns `subscribe' if the group is to be uncoditionally
11539   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11540   ;; no match for the group.
11541
11542   ;; First we check the two user variables.
11543   (cond
11544    ((and gnus-options-subscribe
11545          (string-match gnus-options-subscribe group))
11546     'subscribe)
11547    ((and gnus-options-not-subscribe
11548          (string-match gnus-options-not-subscribe group))
11549     'ignore)
11550    ;; Then we go through the list that was retrieved from the .newsrc
11551    ;; file.  This list has elements on the form 
11552    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11553    ;; is in the reverse order of the options line) is returned.
11554    (t
11555     (let ((regs gnus-newsrc-options-n))
11556       (while (and regs
11557                   (not (string-match (car (car regs)) group)))
11558         (setq regs (cdr regs)))
11559       (and regs (cdr (car regs)))))))
11560
11561 (defun gnus-ask-server-for-new-groups ()
11562   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11563          (methods (cons gnus-select-method 
11564                         (append
11565                          (and (consp gnus-check-new-newsgroups)
11566                               gnus-check-new-newsgroups)
11567                          gnus-secondary-select-methods)))
11568          (groups 0)
11569          (new-date (current-time-string))
11570          hashtb group new-newsgroups got-new)
11571     ;; Go thorugh both primary and secondary select methods and
11572     ;; request new newsgroups.  
11573     (while methods
11574       (and (or (gnus-server-opened (car methods))
11575                (gnus-open-server (car methods)))
11576            (gnus-request-newgroups date (car methods))
11577            (save-excursion
11578              (setq got-new t)
11579              (set-buffer nntp-server-buffer)
11580              (or hashtb (setq hashtb (gnus-make-hashtable 
11581                                       (count-lines (point-min) (point-max)))))
11582              ;; Enter all the new groups in a hashtable.
11583              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11584       (setq methods (cdr methods)))
11585     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11586     ;; Now all new groups from all select methods are in `hashtb'.
11587     (mapatoms
11588      (lambda (group-sym)
11589        (setq group (symbol-name group-sym))
11590        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11591                (member group gnus-zombie-list)
11592                (member group gnus-killed-list))
11593            ;; The group is already known.
11594            ()
11595          (and (symbol-value group-sym)
11596               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11597          (let ((do-sub (gnus-matches-options-n group)))
11598            (cond ((eq do-sub 'subscribe)
11599                   (setq groups (1+ groups))
11600                   (gnus-sethash group group gnus-killed-hashtb)
11601                   (funcall 
11602                    gnus-subscribe-options-newsgroup-method group))
11603                  ((eq do-sub 'ignore)
11604                   nil)
11605                  (t
11606                   (setq groups (1+ groups))
11607                   (gnus-sethash group group gnus-killed-hashtb)
11608                   (if gnus-subscribe-hierarchical-interactive
11609                       (setq new-newsgroups (cons group new-newsgroups))
11610                     (funcall gnus-subscribe-newsgroup-method group)))))))
11611      hashtb)
11612     (if new-newsgroups 
11613         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11614     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11615     (if (> groups 0)
11616         (gnus-message 6 "%d new newsgroup%s arrived." 
11617                       groups (if (> groups 1) "s have" " has")))
11618     got-new))
11619
11620 (defun gnus-check-first-time-used ()
11621   (if (or (> (length gnus-newsrc-alist) 1)
11622           (file-exists-p gnus-startup-file)
11623           (file-exists-p (concat gnus-startup-file ".el"))
11624           (file-exists-p (concat gnus-startup-file ".eld")))
11625       nil
11626     (gnus-message 6 "First time user; subscribing you to default groups")
11627     (or gnus-have-read-active-file (gnus-read-active-file))
11628     (setq gnus-newsrc-last-checked-date (current-time-string))
11629     (let ((groups gnus-default-subscribed-newsgroups)
11630           group)
11631       (if (eq groups t)
11632           nil
11633         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11634         (mapatoms
11635          (lambda (sym)
11636            (setq group (symbol-name sym))
11637            (let ((do-sub (gnus-matches-options-n group)))
11638              (cond ((eq do-sub 'subscribe)
11639                     (gnus-sethash group group gnus-killed-hashtb)
11640                     (funcall 
11641                      gnus-subscribe-options-newsgroup-method group))
11642                    ((eq do-sub 'ignore)
11643                     nil)
11644                    (t
11645                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11646          gnus-active-hashtb)
11647         (while groups
11648           (if (gnus-gethash (car groups) gnus-active-hashtb)
11649               (gnus-group-change-level 
11650                (car groups) gnus-level-default-subscribed gnus-level-killed))
11651           (setq groups (cdr groups)))
11652         (gnus-group-make-help-group)
11653         (and gnus-novice-user
11654              (gnus-message 7 "`A k' to list killed groups"))))))
11655
11656 (defun gnus-subscribe-group (group previous &optional method)
11657   (gnus-group-change-level 
11658    (if method
11659        (list t group gnus-level-default-subscribed nil nil method)
11660      group) 
11661    gnus-level-default-subscribed gnus-level-killed previous t))
11662
11663 ;; `gnus-group-change-level' is the fundamental function for changing
11664 ;; subscription levels of newsgroups. This might mean just changing
11665 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11666 ;; again, which subscribes/unsubscribes a group, which is equally
11667 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11668 ;; from 8-9 to 1-7 means that you remove the group from the list of
11669 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11670 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11671 ;; which is trivial.
11672 ;; ENTRY can either be a string (newsgroup name) or a list (if
11673 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11674 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11675 ;; entries. 
11676 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11677 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11678 ;; after. 
11679 (defun gnus-group-change-level (entry level &optional oldlevel
11680                                       previous fromkilled)
11681   (let (group info active num)
11682     ;; Glean what info we can from the arguments
11683     (if (consp entry)
11684         (if fromkilled (setq group (nth 1 entry))
11685           (setq group (car (nth 2 entry))))
11686       (setq group entry))
11687     (if (and (stringp entry)
11688              oldlevel 
11689              (< oldlevel gnus-level-zombie))
11690         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11691     (if (and (not oldlevel)
11692              (consp entry))
11693         (setq oldlevel (car (cdr (nth 2 entry)))))
11694     (if (stringp previous)
11695         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11696
11697     (gnus-dribble-enter
11698      (format "(gnus-group-change-level %S %S %S %S %S)" 
11699              group level oldlevel (car (nth 2 previous)) fromkilled))
11700     
11701     ;; Then we remove the newgroup from any old structures, if needed.
11702     ;; If the group was killed, we remove it from the killed or zombie
11703     ;; list. If not, and it is in fact going to be killed, we remove
11704     ;; it from the newsrc hash table and assoc.
11705     (cond ((>= oldlevel gnus-level-zombie)
11706            (if (= oldlevel gnus-level-zombie)
11707                (setq gnus-zombie-list (delete group gnus-zombie-list))
11708              (setq gnus-killed-list (delete group gnus-killed-list))))
11709           (t
11710            (if (>= level gnus-level-zombie)
11711                (progn
11712                  (gnus-sethash (car (nth 2 entry))
11713                                nil gnus-newsrc-hashtb)
11714                  (if (nth 3 entry)
11715                      (setcdr (gnus-gethash (car (nth 3 entry))
11716                                            gnus-newsrc-hashtb)
11717                              (cdr entry)))
11718                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11719
11720     ;; Finally we enter (if needed) the list where it is supposed to
11721     ;; go, and change the subscription level. If it is to be killed,
11722     ;; we enter it into the killed or zombie list.
11723     (cond ((>= level gnus-level-zombie)
11724            ;; Remove from the hash table.
11725            (gnus-sethash group nil gnus-newsrc-hashtb)
11726            (or (gnus-group-foreign-p group)
11727                ;; We do not enter foreign groups into the list of dead
11728                ;; groups.  
11729                (if (= level gnus-level-zombie)
11730                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11731                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11732           (t
11733            ;; If the list is to be entered into the newsrc assoc, and
11734            ;; it was killed, we have to create an entry in the newsrc
11735            ;; hashtb format and fix the pointers in the newsrc assoc.
11736            (if (>= oldlevel gnus-level-zombie)
11737                (progn
11738                  (if (listp entry)
11739                      (progn
11740                        (setq info (cdr entry))
11741                        (setq num (car entry)))
11742                    (setq active (gnus-gethash group gnus-active-hashtb))
11743                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11744                    ;; Check whether the group is foreign. If so, the
11745                    ;; foreign select method has to be entered into the
11746                    ;; info. 
11747                    (let ((method (gnus-group-method-name group)))
11748                      (if (eq method gnus-select-method)
11749                          (setq info (list group level nil))
11750                        (setq info (list group level nil nil method)))))
11751                  (or previous 
11752                      (setq previous 
11753                            (let ((p gnus-newsrc-alist))
11754                              (while (cdr (cdr p))
11755                                (setq p (cdr p)))
11756                              p)))
11757                  (setq entry (cons info (cdr (cdr previous))))
11758                  (if (cdr previous)
11759                      (progn
11760                        (setcdr (cdr previous) entry)
11761                        (gnus-sethash group (cons num (cdr previous)) 
11762                                      gnus-newsrc-hashtb))
11763                    (setcdr previous entry)
11764                    (gnus-sethash group (cons num previous)
11765                                  gnus-newsrc-hashtb))
11766                  (if (cdr entry)
11767                      (setcdr (gnus-gethash (car (car (cdr entry)))
11768                                            gnus-newsrc-hashtb)
11769                              entry)))
11770              ;; It was alive, and it is going to stay alive, so we
11771              ;; just change the level and don't change any pointers or
11772              ;; hash table entries.
11773              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11774
11775 (defun gnus-kill-newsgroup (newsgroup)
11776   "Obsolete function. Kills a newsgroup."
11777   (gnus-group-change-level
11778    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11779
11780 (defun gnus-check-bogus-newsgroups (&optional confirm)
11781   "Remove bogus newsgroups.
11782 If CONFIRM is non-nil, the user has to confirm the deletion of every
11783 newsgroup." 
11784   (let ((newsrc (cdr gnus-newsrc-alist))
11785         bogus group entry)
11786     (gnus-message 5 "Checking bogus newsgroups...")
11787     (or gnus-have-read-active-file (gnus-read-active-file))
11788     ;; Find all bogus newsgroup that are subscribed.
11789     (while newsrc
11790       (setq group (car (car newsrc)))
11791       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11792               (nth 4 (car newsrc))      ; Foreign
11793               (and confirm
11794                    (not (gnus-y-or-n-p
11795                          (format "Remove bogus newsgroup: %s " group)))))
11796           ;; Don't remove.
11797           ()
11798         ;; Found a bogus newsgroup.
11799         (setq bogus (cons group bogus)))
11800       (setq newsrc (cdr newsrc)))
11801     ;; Remove all bogus subscribed groups by first killing them, and
11802     ;; then removing them from the list of killed groups.
11803     (while bogus
11804       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11805            (progn
11806              (gnus-group-change-level entry gnus-level-killed)
11807              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11808       (setq bogus (cdr bogus)))
11809     ;; Then we remove all bogus groups from the list of killed and
11810     ;; zombie groups. They are are removed without confirmation.
11811     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11812           killed)
11813       (while dead-lists
11814         (setq killed (symbol-value (car dead-lists)))
11815         (while killed
11816           (setq group (car killed))
11817           (or (gnus-gethash group gnus-active-hashtb)
11818               ;; The group is bogus.
11819               (set (car dead-lists)
11820                    (delete group (symbol-value (car dead-lists)))))
11821           (setq killed (cdr killed)))
11822         (setq dead-lists (cdr dead-lists))))
11823     (gnus-message 5 "Checking bogus newsgroups...done")))
11824
11825 (defun gnus-check-duplicate-killed-groups ()
11826   "Remove duplicates from the list of killed groups."
11827   (interactive)
11828   (let ((killed gnus-killed-list))
11829     (while killed
11830       (gnus-message 9 "%d" (length killed))
11831       (setcdr killed (delete (car killed) (cdr killed)))
11832       (setq killed (cdr killed)))))
11833
11834 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11835 ;; and compute how many unread articles there are in each group.
11836 (defun gnus-get-unread-articles (&optional level) 
11837   (let* ((newsrc (cdr gnus-newsrc-alist))
11838          (level (or level (1+ gnus-level-subscribed)))
11839          (foreign-level
11840           (min 
11841            (cond ((and gnus-activate-foreign-newsgroups 
11842                        (not (numberp gnus-activate-foreign-newsgroups)))
11843                   (1+ gnus-level-subscribed))
11844                  ((numberp gnus-activate-foreign-newsgroups)
11845                   gnus-activate-foreign-newsgroups)
11846                  (t 0))
11847            level))
11848          info group active virtuals method)
11849     (gnus-message 5 "Checking new news...")
11850
11851     (while newsrc
11852       (setq info (car newsrc)
11853             group (car info)
11854             active (gnus-gethash group gnus-active-hashtb))
11855
11856       ;; Check newsgroups. If the user doesn't want to check them, or
11857       ;; they can't be checked (for instance, if the news server can't
11858       ;; be reached) we just set the number of unread articles in this
11859       ;; newsgroup to t. This means that Gnus thinks that there are
11860       ;; unread articles, but it has no idea how many.
11861       (if (and (setq method (nth 4 info))
11862                (not (gnus-server-equal gnus-select-method
11863                                        (gnus-server-get-method nil method)))
11864                (not (gnus-secondary-method-p method)))
11865           ;; These groups are foreign. Check the level.
11866           (if (<= (nth 1 info) foreign-level)
11867               (if (eq (car (if (stringp method) 
11868                                (gnus-server-to-method method)
11869                              (nth 4 info))) 'nnvirtual)
11870                   ;; We have to activate the virtual groups after all
11871                   ;; the others, so we just pop them on a list for
11872                   ;; now. 
11873                   (setq virtuals (cons info virtuals))
11874                 (and (setq active (gnus-activate-newsgroup (car info)))
11875                      ;; Close the groups as we look at them!
11876                      (gnus-close-group group))))
11877                 
11878         ;; These groups are native or secondary. 
11879         (if (and (not gnus-have-read-active-file)
11880                  (<= (nth 1 info) level))
11881             (setq active (gnus-activate-newsgroup (car info)))))
11882       
11883       (if active
11884           (gnus-get-unread-articles-in-group info active)
11885         ;; The group couldn't be reached, so we nix out the number of
11886         ;; unread articles and stuff.
11887         (gnus-sethash group nil gnus-active-hashtb)
11888         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11889
11890       (setq newsrc (cdr newsrc)))
11891
11892     ;; Activate the virtual groups. This has to be done after all the
11893     ;; other groups. 
11894     ;; !!! If one virtual group contains another virtual group, even
11895     ;; doing it this way might cause problems.
11896    (while virtuals
11897       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11898            (gnus-get-unread-articles-in-group (car virtuals) active))
11899       (setq virtuals (cdr virtuals)))
11900
11901     (gnus-message 5 "Checking new news...done")))
11902
11903 ;; Create a hash table out of the newsrc alist. The `car's of the
11904 ;; alist elements are used as keys.
11905 (defun gnus-make-hashtable-from-newsrc-alist ()
11906   (let ((alist gnus-newsrc-alist)
11907         (ohashtb gnus-newsrc-hashtb)
11908         prev)
11909     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11910     (setq alist 
11911           (setq prev (setq gnus-newsrc-alist 
11912                            (if (equal (car (car gnus-newsrc-alist))
11913                                       "dummy.group")
11914                                gnus-newsrc-alist
11915                              (cons (list "dummy.group" 0 nil) alist)))))
11916     (while alist
11917       (gnus-sethash (car (car alist)) 
11918                     (cons (and ohashtb (car (gnus-gethash 
11919                                              (car (car alist)) ohashtb))) 
11920                           prev) gnus-newsrc-hashtb)
11921       (setq prev alist
11922             alist (cdr alist)))))
11923
11924 (defun gnus-make-hashtable-from-killed ()
11925   "Create a hash table from the killed and zombie lists."
11926   (let ((lists '(gnus-killed-list gnus-zombie-list))
11927         list)
11928     (setq gnus-killed-hashtb 
11929           (gnus-make-hashtable 
11930            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11931     (while lists
11932       (setq list (symbol-value (car lists)))
11933       (setq lists (cdr lists))
11934       (while list
11935         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11936         (setq list (cdr list))))))
11937
11938 (defun gnus-get-unread-articles-in-group (info active)
11939   (let* ((range (nth 2 info))
11940          (num 0)
11941          (marked (nth 3 info)))
11942     ;; If a cache is present, we may have to alter the active info.
11943     (and gnus-use-cache
11944          (gnus-cache-possibly-alter-active (car info) active))
11945     ;; Modify the list of read articles according to what articles 
11946     ;; are available; then tally the unread articles and add the
11947     ;; number to the group hash table entry.
11948     (cond ((zerop (cdr active))
11949            (setq num 0))
11950           ((not range)
11951            (setq num (- (1+ (cdr active)) (car active))))
11952           ((not (listp (cdr range)))
11953            ;; Fix a single (num . num) range according to the
11954            ;; active hash table.
11955            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11956            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11957            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11958            ;; Compute number of unread articles.
11959            (setq num (max 0 (- (cdr active) 
11960                                (- (1+ (cdr range)) (car range))))))
11961           (t
11962            ;; The read list is a list of ranges. Fix them according to
11963            ;; the active hash table.
11964            ;; First peel off any elements that are below the lower
11965            ;; active limit. 
11966            (while (and (cdr range) 
11967                        (>= (car active) 
11968                            (or (and (atom (car (cdr range))) (car (cdr range)))
11969                                (car (car (cdr range))))))
11970              (if (numberp (car range))
11971                  (setcar range 
11972                          (cons (car range) 
11973                                (or (and (numberp (car (cdr range)))
11974                                         (car (cdr range))) 
11975                                    (cdr (car (cdr range))))))
11976                (setcdr (car range) 
11977                        (or (and (numberp (nth 1 range)) (nth 1 range))
11978                            (cdr (car (cdr range))))))
11979              (setcdr range (cdr (cdr range))))
11980            ;; Adjust the first element to be the same as the lower limit. 
11981            (if (and (not (atom (car range))) 
11982                     (< (cdr (car range)) (car active)))
11983                (setcdr (car range) (1- (car active))))
11984            ;; Then we want to peel off any elements that are higher
11985            ;; than the upper active limit.  
11986            (let ((srange range))
11987              ;; Go past all legal elements.
11988              (while (and (cdr srange) 
11989                          (<= (or (and (atom (car (cdr srange)))
11990                                       (car (cdr srange)))
11991                                  (car (car (cdr srange)))) (cdr active)))
11992                (setq srange (cdr srange)))
11993              (if (cdr srange)
11994                  ;; Nuke all remaining illegal elements.
11995                  (setcdr srange nil))
11996
11997              ;; Adjust the final element.
11998              (if (and (not (atom (car srange)))
11999                       (> (cdr (car srange)) (cdr active)))
12000                  (setcdr (car srange) (cdr active))))
12001            ;; Compute the number of unread articles.
12002            (while range
12003              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12004                                          (cdr (car range))))
12005                                  (or (and (atom (car range)) (car range))
12006                                      (car (car range))))))
12007              (setq range (cdr range)))
12008            (setq num (max 0 (- (cdr active) num)))))
12009     (and info
12010          (progn
12011            (and (assq 'tick marked)
12012                 (inline (gnus-remove-illegal-marked-articles
12013                          (assq 'tick marked) (nth 2 info))))
12014            (and (assq 'dormant marked)
12015                 (inline (gnus-remove-illegal-marked-articles
12016                          (assq 'dormant marked) (nth 2 info))))
12017            (setcar
12018             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12019             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12020                                 (length (cdr (assq 'dormant marked)))))))))
12021     num))
12022
12023 (defun gnus-remove-illegal-marked-articles (marked ranges)
12024   (let ((m (cdr marked)))
12025     ;; Make sure that all ticked articles are a subset of the unread
12026     ;; articles. 
12027     (while m
12028       (if (gnus-member-of-range (car m) ranges)
12029           (setcdr marked (cdr m))
12030         (setq marked m))
12031       (setq m (cdr m)))))
12032
12033 (defun gnus-activate-newsgroup (group)
12034   (let ((method (gnus-find-method-for-group group))
12035         active)
12036     (and (or (gnus-server-opened method) (gnus-open-server method))
12037          (gnus-request-group group)
12038          (save-excursion
12039            (set-buffer nntp-server-buffer)
12040            (goto-char (point-min))
12041            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12042                 (progn
12043                   (goto-char (match-beginning 1))
12044                   (gnus-sethash 
12045                    group (setq active (cons (read (current-buffer))
12046                                             (read (current-buffer))))
12047                    gnus-active-hashtb))
12048                 active)))))
12049
12050 (defun gnus-update-read-articles 
12051   (group unread unselected ticked &optional domarks replied expirable killed
12052          dormant bookmark score)
12053   "Update the list of read and ticked articles in GROUP using the
12054 UNREAD and TICKED lists.
12055 Note: UNSELECTED has to be sorted over `<'.
12056 Returns whether the updating was successful."
12057   (let* ((active (or gnus-newsgroup-active 
12058                      (gnus-gethash group gnus-active-hashtb)))
12059          (entry (gnus-gethash group gnus-newsrc-hashtb))
12060          (info (nth 2 entry))
12061          (marked (nth 3 info))
12062          (prev 1)
12063          (unread (sort (copy-sequence unread) (function <)))
12064          read)
12065     (if (or (not info) (not active))
12066         ;; There is no info on this group if it was, in fact,
12067         ;; killed. Gnus stores no information on killed groups, so
12068         ;; there's nothing to be done. 
12069         ;; One could store the information somewhere temporarily,
12070         ;; perhaps... Hmmm... 
12071         ()
12072       ;; Remove any negative articles numbers.
12073       (while (and unread (< (car unread) 0))
12074         (setq unread (cdr unread)))
12075       ;; Remove any expired article numbers
12076       (while (and unread (< (car unread) (car active)))
12077         (setq unread (cdr unread)))
12078       (while (and ticked (< (car ticked) (car active)))
12079         (setq ticked (cdr ticked)))
12080       (while (and dormant (< (car dormant) (car active)))
12081         (setq dormant (cdr dormant)))
12082       (setq unread (sort (append unselected unread) '<))
12083       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12084       (setcar entry (max 0 (- (length unread) (length ticked) 
12085                               (length dormant))))
12086       ;; Compute the ranges of read articles by looking at the list of
12087       ;; unread articles.  
12088       (while unread
12089         (if (/= (car unread) prev)
12090             (setq read (cons (if (= prev (1- (car unread))) prev
12091                                (cons prev (1- (car unread)))) read)))
12092         (setq prev (1+ (car unread)))
12093         (setq unread (cdr unread)))
12094       (if (<= prev (cdr active))
12095           (setq read (cons (cons prev (cdr active)) read)))
12096       ;; Enter this list into the group info.
12097       (setcar (cdr (cdr info)) 
12098               (if (> (length read) 1) (nreverse read) read))
12099       ;; Enter the list of ticked articles.
12100       (gnus-set-marked-articles 
12101        info ticked
12102        (if domarks replied (cdr (assq 'reply marked)))
12103        (if domarks expirable (cdr (assq 'expire marked)))
12104        (if domarks killed (cdr (assq 'killed marked)))
12105        (if domarks dormant (cdr (assq 'dormant marked)))
12106        (if domarks bookmark (cdr (assq 'bookmark marked)))
12107        (if domarks score (cdr (assq 'score marked))))
12108       t)))
12109
12110 (defun gnus-make-articles-unread (group articles)
12111   "Mark ARTICLES in GROUP as unread."
12112   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12113                           (gnus-gethash (gnus-group-real-name group)
12114                                         gnus-newsrc-hashtb))))
12115          (ranges (nth 2 info))
12116          news)
12117     (while articles
12118       (and (gnus-member-of-range (car articles) ranges)
12119            (setq news (cons (car articles) news)))
12120       (setq articles (cdr articles)))
12121     (if (not news)
12122         ()
12123       (setcar (nthcdr 2 info)
12124               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12125       (gnus-group-update-group group t))))
12126
12127 ;; Get the active file(s) from the backend(s).
12128 (defun gnus-read-active-file ()
12129   (gnus-group-set-mode-line)
12130   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12131                          (gnus-open-server gnus-select-method))
12132                      ;; The native server is available.
12133                      (cons gnus-select-method gnus-secondary-select-methods)
12134                    ;; The native server is down, so we just do the
12135                    ;; secondary ones.   
12136                    gnus-secondary-select-methods))
12137         list-type)
12138     (setq gnus-have-read-active-file nil)
12139     (save-excursion
12140       (set-buffer nntp-server-buffer)
12141       (while methods
12142         (let* ((where (nth 1 (car methods)))
12143                (mesg (format "Reading active file%s via %s..."
12144                              (if (and where (not (zerop (length where))))
12145                                  (concat " from " where) "")
12146                              (car (car methods)))))
12147           (gnus-message 5 mesg)
12148           (gnus-check-news-server (car methods))
12149           (cond 
12150            ((and (eq gnus-read-active-file 'some)
12151                  (gnus-check-backend-function
12152                   'retrieve-groups (car (car methods))))
12153             (let ((newsrc (cdr gnus-newsrc-alist))
12154                   groups)
12155               (while newsrc
12156                 (and (gnus-server-equal 
12157                       (gnus-find-method-for-group
12158                        (car (car newsrc)) (car newsrc))
12159                       (gnus-server-get-method nil (car methods)))
12160                      (setq groups (cons (car (car newsrc)) groups)))
12161                 (setq newsrc (cdr newsrc)))
12162               (setq list-type (gnus-retrieve-groups groups (car methods)))
12163               (cond ((not list-type)
12164                      (gnus-message 
12165                       1 "Cannot read partial active file from %s server." 
12166                       (car (car methods)))
12167                      (ding)
12168                      (sit-for 2))
12169                     ((eq list-type 'active)
12170                      (gnus-active-to-gnus-format (car methods)))
12171                     (t
12172                      (gnus-groups-to-gnus-format (car methods))))))
12173            (t
12174             (if (not (gnus-request-list (car methods)))
12175                 (progn
12176                   (gnus-message 1 "Cannot read active file from %s server." 
12177                                 (car (car methods)))
12178                   (ding))
12179               (gnus-active-to-gnus-format (car methods))
12180               (setq gnus-have-read-active-file t)
12181               (gnus-message 5 "%sdone" mesg)))))
12182         (setq methods (cdr methods))))))
12183
12184 ;; Read an active file and place the results in `gnus-active-hashtb'.
12185 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12186   (let ((cur (current-buffer))
12187         (hashtb (or hashtb 
12188                     (if (and gnus-active-hashtb 
12189                              (not (equal method gnus-select-method)))
12190                         gnus-active-hashtb
12191                       (setq gnus-active-hashtb
12192                             (if (equal method gnus-select-method)
12193                                 (gnus-make-hashtable 
12194                                  (count-lines (point-min) (point-max)))
12195                               (gnus-make-hashtable 4096)))))))
12196     ;; Delete unnecessary lines.
12197     (goto-char (point-min))
12198     (while (search-forward "\nto." nil t)
12199       (delete-region (1+ (match-beginning 0)) 
12200                      (progn (forward-line 1) (point))))
12201     (or (string= gnus-ignored-newsgroups "")
12202         (progn
12203           (goto-char (point-min))
12204           (delete-matching-lines gnus-ignored-newsgroups)))
12205     ;; If these are groups from a foreign select method, we insert the
12206     ;; group prefix in front of the group names. 
12207     (and method (not (eq method gnus-select-method))
12208          (let ((prefix (gnus-group-prefixed-name "" method)))
12209            (goto-char (point-min))
12210            (while (and (not (eobp))
12211                        (progn (insert prefix)
12212                               (zerop (forward-line 1)))))))
12213     (goto-char (point-min))
12214     ;; Store active file in hashtable.
12215     (goto-char (point-min))
12216     (if (string-match "%[oO]" gnus-group-line-format)
12217         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12218         ;; If we want information on moderated groups, we use this
12219         ;; loop...   
12220         (let* ((mod-hashtb (make-vector 7 0))
12221                (m (intern "m" mod-hashtb))
12222                group max min)
12223           (while (not (eobp))
12224             (condition-case nil
12225                 (progn
12226                   (narrow-to-region (point) (gnus-point-at-eol))
12227                   (setq group (let ((obarray hashtb)) (read cur)))
12228                   (if (and (numberp (setq max (read cur)))
12229                            (numberp (setq min (read cur))))
12230                       (set group (cons min max))
12231                     (set group nil))
12232                   ;; Enter moderated groups into a list.
12233                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12234                       (setq gnus-moderated-list 
12235                             (cons (symbol-name group) gnus-moderated-list))))
12236               (error nil))
12237             (widen)
12238             (forward-line 1)))
12239       ;; And if we do not care about moderation, we use this loop,
12240       ;; which is faster.
12241       (let (group max min)
12242         (while (not (eobp))
12243           (condition-case ()
12244               (progn
12245                 (narrow-to-region (point) (gnus-point-at-eol))
12246                 ;; group gets set to a symbol interned in the hash table
12247                 ;; (what a hack!!)
12248                 (setq group (let ((obarray hashtb)) (read cur)))
12249                 (if (and (numberp (setq max (read cur)))
12250                          (numberp (setq min (read cur))))
12251                     (set group (cons min max))
12252                   (set group nil)))
12253             (error 
12254              (progn 
12255                (if ignore-errors
12256                    (set group nil)
12257                  (ding) 
12258                  (gnus-message 3 "Warning - illegal active: %s"
12259                                (buffer-substring 
12260                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12261                  nil))))
12262           (widen)
12263           (forward-line 1))))))
12264
12265 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12266   ;; Parse a "groups" active file.
12267   (let ((cur (current-buffer))
12268         (hashtb (or hashtb 
12269                     (if method
12270                         gnus-active-hashtb
12271                       (setq gnus-active-hashtb
12272                             (gnus-make-hashtable 
12273                              (count-lines (point-min) (point-max)))))))
12274         (prefix (and method (not (eq method gnus-select-method))
12275                      (gnus-group-prefixed-name "" method))))
12276
12277     (goto-char (point-min))
12278     (condition-case ()
12279         ;; We split this into to separate loops, one with the prefix
12280         ;; and one without to speed the reading up somewhat.
12281         (if prefix
12282             (let (min max opoint)
12283               (while (not (eobp))
12284                 (read cur) (read cur)
12285                 (setq min (read cur)
12286                       max (read cur)
12287                       opoint (point))
12288                 (skip-chars-forward " \t")
12289                 (insert prefix)
12290                 (goto-char opoint)
12291                 (set (let ((obarray hashtb)) (read cur)) 
12292                      (cons min max))
12293                 (forward-line 1)))
12294           (let (min max)
12295             (while (not (eobp))
12296               (if (= (following-char) ?2)
12297                   (progn
12298                     (read cur) (read cur)
12299                     (setq min (read cur)
12300                           max (read cur))
12301                     (set (let ((obarray hashtb)) (read cur)) 
12302                          (cons min max))))
12303               (forward-line 1))))
12304       (error 
12305        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12306
12307 (defun gnus-read-newsrc-file (&optional force)
12308   "Read startup file.
12309 If FORCE is non-nil, the .newsrc file is read."
12310   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12311   ;; Reset variables that might be defined in the .newsrc.eld file.
12312   (let ((variables gnus-variable-list))
12313     (while variables
12314       (set (car variables) nil)
12315       (setq variables (cdr variables))))
12316   (let* ((newsrc-file gnus-current-startup-file)
12317          (quick-file (concat newsrc-file ".el")))
12318     (save-excursion
12319       ;; We always load the .newsrc.eld file. If always contains
12320       ;; much information that can not be gotten from the .newsrc
12321       ;; file (ticked articles, killed groups, foreign methods, etc.)
12322       (gnus-read-newsrc-el-file quick-file)
12323  
12324       (if (or force
12325               (and (file-newer-than-file-p newsrc-file quick-file)
12326                    (file-newer-than-file-p newsrc-file 
12327                                            (concat quick-file "d")))
12328               (not gnus-newsrc-alist))
12329           ;; We read the .newsrc file. Note that if there if a
12330           ;; .newsrc.eld file exists, it has already been read, and
12331           ;; the `gnus-newsrc-hashtb' has been created. While reading
12332           ;; the .newsrc file, Gnus will only use the information it
12333           ;; can find there for changing the data already read -
12334           ;; ie. reading the .newsrc file will not trash the data
12335           ;; already read (except for read articles).
12336           (save-excursion
12337             (gnus-message 5 "Reading %s..." newsrc-file)
12338             (set-buffer (find-file-noselect newsrc-file))
12339             (buffer-disable-undo (current-buffer))
12340             (gnus-newsrc-to-gnus-format)
12341             (kill-buffer (current-buffer))
12342             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12343
12344 (defun gnus-read-newsrc-el-file (file)
12345   (let ((ding-file (concat file "d")))
12346     ;; We always, always read the .eld file.
12347     (gnus-message 5 "Reading %s..." ding-file)
12348     (let (gnus-newsrc-assoc)
12349       (condition-case nil
12350           (load ding-file t t t)
12351         (error nil))
12352       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12353     (let ((inhibit-quit t))
12354       (gnus-uncompress-newsrc-alist))
12355     (gnus-make-hashtable-from-newsrc-alist)
12356     (if (not (file-newer-than-file-p file ding-file))
12357         ()
12358       ;; Old format quick file
12359       (gnus-message 5 "Reading %s..." file)
12360       ;; The .el file is newer than the .eld file, so we read that one
12361       ;; as well. 
12362       (gnus-read-old-newsrc-el-file file))))
12363
12364 ;; Parse the old-style quick startup file
12365 (defun gnus-read-old-newsrc-el-file (file)
12366   (let (newsrc killed marked group m)
12367     (prog1
12368         (let ((gnus-killed-assoc nil)
12369               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12370           (prog1
12371               (condition-case nil
12372                   (load file t t t)
12373                 (error nil))
12374             (setq newsrc gnus-newsrc-assoc
12375                   killed gnus-killed-assoc
12376                   marked gnus-marked-assoc)))
12377       (setq gnus-newsrc-alist nil)
12378       (while newsrc
12379         (setq group (car newsrc))
12380         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12381           (if info
12382               (progn
12383                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12384                 (setcar (cdr info)
12385                         (if (nth 1 group) gnus-level-default-subscribed 
12386                           gnus-level-default-unsubscribed))
12387                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12388             (setq gnus-newsrc-alist
12389                   (cons 
12390                    (setq info
12391                          (list (car group)
12392                                (if (nth 1 group) gnus-level-default-subscribed
12393                                  gnus-level-default-unsubscribed) 
12394                                (cdr (cdr group))))
12395                    gnus-newsrc-alist)))
12396           (if (setq m (assoc (car group) marked))
12397             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12398         (setq newsrc (cdr newsrc)))
12399       (setq newsrc killed)
12400       (while newsrc
12401         (setcar newsrc (car (car newsrc)))
12402         (setq newsrc (cdr newsrc)))
12403       (setq gnus-killed-list killed))
12404     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12405     (gnus-make-hashtable-from-newsrc-alist)))
12406       
12407 (defun gnus-make-newsrc-file (file)
12408   "Make server dependent file name by catenating FILE and server host name."
12409   (let* ((file (expand-file-name file nil))
12410          (real-file (concat file "-" (nth 1 gnus-select-method))))
12411     (if (file-exists-p real-file)
12412         real-file file)))
12413
12414 (defun gnus-uncompress-newsrc-alist ()
12415   ;; Uncompress all lists of marked articles in the newsrc assoc.
12416   (let ((newsrc gnus-newsrc-alist)
12417         marked)
12418     (while newsrc
12419       (if (not (setq marked (nth 3 (car newsrc))))
12420           ()
12421         (while marked
12422           (or (eq 'score (car (car marked)))
12423               (eq 'bookmark (car (car marked)))
12424               (eq 'killed (car (car marked)))
12425               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12426           (setq marked (cdr marked))))
12427       (setq newsrc (cdr newsrc)))))
12428
12429 (defun gnus-compress-newsrc-alist ()
12430   ;; Compress all lists of marked articles in the newsrc assoc.
12431   (let ((newsrc gnus-newsrc-alist)
12432         marked)
12433     (while newsrc
12434       (if (not (setq marked (nth 3 (car newsrc))))
12435           ()
12436         (while marked
12437           (or (eq 'score (car (car marked)))
12438               (eq 'bookmark (car (car marked)))
12439               (eq 'killed (car (car marked)))
12440               (setcdr (car marked) 
12441                       (condition-case ()
12442                           (gnus-compress-sequence 
12443                            (sort (cdr (car marked)) '<) t)
12444                         (error (cdr (car marked))))))
12445           (setq marked (cdr marked))))
12446       (setq newsrc (cdr newsrc)))))
12447
12448 (defun gnus-newsrc-to-gnus-format ()
12449   (setq gnus-newsrc-options "")
12450   (setq gnus-newsrc-options-n nil)
12451
12452   (or gnus-active-hashtb
12453       (setq gnus-active-hashtb (make-vector 4095 0)))
12454   (let ((buf (current-buffer))
12455         (already-read (> (length gnus-newsrc-alist) 1))
12456         group subscribed options-symbol newsrc
12457         symbol reads num1)
12458     (goto-char (point-min))
12459     ;; We intern the symbol `options' in the active hashtb so that we
12460     ;; can `eq' against it later.
12461     (setq options-symbol (intern "options" gnus-active-hashtb))
12462   
12463     (while (not (eobp))
12464       ;; We first read the first word on the line by narrowing and
12465       ;; then reading into `gnus-active-hashtb'.  Most groups will
12466       ;; already exist in that hashtb, so this will save some string
12467       ;; space.
12468       (narrow-to-region
12469        (point)
12470        (progn (skip-chars-forward "^ \t!:\n") (point)))
12471       (goto-char (point-min))
12472       (setq symbol 
12473             (and (/= (point-min) (point-max))
12474                  (let ((obarray gnus-active-hashtb)) (read buf))))
12475       (widen)
12476       ;; Now, the symbol we have read is either `options' or a group
12477       ;; name.  If it is an options line, we just add it to a string. 
12478       (cond 
12479        ((eq symbol options-symbol)
12480         (setq gnus-newsrc-options
12481               ;; This concatting is quite inefficient, but since our
12482               ;; thorough studies show that approx 99.37% of all
12483               ;; .newsrc files only contain a single options line, we
12484               ;; don't give a damn, frankly, my dear.
12485               (concat gnus-newsrc-options
12486                       (buffer-substring 
12487                        (gnus-point-at-bol)
12488                        ;; Options may continue on the next line.
12489                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12490                                 (progn (beginning-of-line) (point)))
12491                            (point))))))
12492        (symbol
12493         ;; It was a group name.
12494         (setq subscribed (= (following-char) ?:)
12495               group (symbol-name symbol)
12496               reads nil)
12497         (if (eolp)
12498             ;; If the line ends here, this is clearly a buggy line, so
12499             ;; we put point a the beginning of line and let the cond
12500             ;; below do the error handling.
12501             (beginning-of-line)
12502           ;; We skip to the beginning of the ranges.
12503           (skip-chars-forward "!: \t"))
12504         ;; We are now at the beginning of the list of read articles.
12505         ;; We read them range by range.
12506         (while
12507             (cond 
12508              ((looking-at "[0-9]+")
12509               ;; We narrow and read a number instead of buffer-substring/
12510               ;; string-to-int because it's faster. narrow/widen is
12511               ;; faster than save-restriction/narrow, and save-restriction
12512               ;; produces a garbage object.
12513               (setq num1 (progn
12514                            (narrow-to-region (match-beginning 0) (match-end 0))
12515                            (read buf)))
12516               (widen)
12517               ;; If the next character is a dash, then this is a range.
12518               (if (= (following-char) ?-)
12519                   (progn
12520                     ;; We read the upper bound of the range.
12521                     (forward-char 1)
12522                     (if (not (looking-at "[0-9]+"))
12523                         ;; This is a buggy line, by we pretend that
12524                         ;; it's kinda OK. Perhaps the user should be
12525                         ;; dinged? 
12526                         (setq reads (cons num1 reads))
12527                       (setq reads 
12528                             (cons 
12529                              (cons num1 (progn
12530                                           (narrow-to-region (match-beginning 0) 
12531                                                             (match-end 0))
12532                                           (read buf)))
12533                              reads))
12534                       (widen)))
12535                 ;; It was just a simple number, so we add it to the
12536                 ;; list of ranges.
12537                 (setq reads (cons num1 reads)))
12538               ;; If the next char in ?\n, then we have reached the end
12539               ;; of the line and return nil.
12540               (/= (following-char) ?\n))
12541              ((= (following-char) ?\n)
12542               ;; End of line, so we end.
12543               nil)
12544              (t
12545               ;; Not numbers and not eol, so this might be a buggy
12546               ;; line... 
12547               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12548                   (progn
12549                     ;; The line was buggy.
12550                     (setq group nil)
12551                     (gnus-message 3 "Mangled line: %s" 
12552                                   (buffer-substring (gnus-point-at-bol) 
12553                                                     (gnus-point-at-eol)))
12554                     (ding)
12555                     (sit-for 1)))
12556               nil))
12557           ;; Skip past ", ". Spaces are illegal in these ranges, but
12558           ;; we allow them, because it's a common mistake to put a
12559           ;; space after the comma.
12560           (skip-chars-forward ", "))
12561
12562         ;; We have already read .newsrc.eld, so we gently update the
12563         ;; data in the hash table with the information we have just
12564         ;; read. 
12565         (if (not group)
12566             ()
12567           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12568                 level)
12569             (if info
12570                 ;; There is an entry for this file in the alist.
12571                 (progn
12572                   (setcar (nthcdr 2 info) (nreverse reads))
12573                   ;; We update the level very gently.  In fact, we
12574                   ;; only change it if there's been a status change
12575                   ;; from subscribed to unsubscribed, or vice versa.
12576                   (setq level (nth 1 info))
12577                   (cond ((and (<= level gnus-level-subscribed)
12578                               (not subscribed))
12579                          (setq level (if reads
12580                                          gnus-level-default-unsubscribed 
12581                                        (1+ gnus-level-default-unsubscribed))))
12582                         ((and (> level gnus-level-subscribed) subscribed)
12583                          (setq level gnus-level-default-subscribed)))
12584                   (setcar (cdr info) level))
12585               ;; This is a new group.
12586               (setq info (list group 
12587                                (if subscribed
12588                                    gnus-level-default-subscribed 
12589                                  (if reads
12590                                      (1+ gnus-level-subscribed)
12591                                    gnus-level-default-unsubscribed))
12592                                (nreverse reads))))
12593             (setq newsrc (cons info newsrc))))))
12594       (forward-line 1))
12595     
12596     (setq newsrc (nreverse newsrc))
12597
12598     (if (not already-read)
12599         ()
12600       ;; We now have two newsrc lists - `newsrc', which is what we
12601       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12602       ;; what we've read from .newsrc.eld. We have to merge these
12603       ;; lists. We do this by "attaching" any (foreign) groups in the
12604       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12605       (let ((rc (cdr gnus-newsrc-alist))
12606             (prev gnus-newsrc-alist)
12607             entry mentry)
12608         (while rc
12609           (or (null (nth 4 (car rc))) ; It's a native group.
12610               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12611               (if (setq entry (assoc (car (car prev)) newsrc))
12612                   (setcdr (setq mentry (memq entry newsrc))
12613                           (cons (car rc) (cdr mentry)))
12614                 (setq newsrc (cons (car rc) newsrc))))
12615           (setq prev rc
12616                 rc (cdr rc)))))
12617
12618     (setq gnus-newsrc-alist newsrc)
12619     ;; We make the newsrc hashtb.
12620     (gnus-make-hashtable-from-newsrc-alist)
12621
12622     ;; Finally, if we read some options lines, we parse them.
12623     (or (string= gnus-newsrc-options "")
12624         (gnus-newsrc-parse-options gnus-newsrc-options))))
12625
12626 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12627 ;; The return value will be a list on the form
12628 ;; ((regexp1 . ignore)
12629 ;;  (regexp2 . subscribe)...)
12630 ;; When handling new newsgroups, groups that match a `ignore' regexp
12631 ;; will be ignored, and groups that match a `subscribe' regexp will be
12632 ;; subscribed. A line like
12633 ;; options -n !all rec.all
12634 ;; will lead to a list that looks like
12635 ;; (("^rec\\..+" . subscribe) 
12636 ;;  ("^.+" . ignore))
12637 ;; So all "rec.*" groups will be subscribed, while all the other
12638 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12639 ;; different from "options -n rec.all !all". 
12640 (defun gnus-newsrc-parse-options (options)
12641   (let (out eol)
12642     (save-excursion
12643       (gnus-set-work-buffer)
12644       (insert (regexp-quote options))
12645       ;; First we treat all continuation lines.
12646       (goto-char (point-min))
12647       (while (re-search-forward "\n[ \t]+" nil t)
12648         (replace-match " " t t))
12649       ;; Then we transform all "all"s into ".+"s.
12650       (goto-char (point-min))
12651       (while (re-search-forward "\\ball\\b" nil t)
12652         (replace-match ".+" t t))
12653       (goto-char (point-min))
12654       ;; We remove all other options than the "-n" ones.
12655       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12656         (replace-match " ")
12657         (forward-char -1))
12658       (goto-char (point-min))
12659
12660       ;; We are only interested in "options -n" lines - we
12661       ;; ignore the other option lines.
12662       (while (re-search-forward "[ \t]-n" nil t)
12663         (setq eol 
12664               (or (save-excursion
12665                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12666                          (- (point) 2)))
12667                   (gnus-point-at-eol)))
12668         ;; Search for all "words"...
12669         (while (re-search-forward "[^ \t,\n]+" eol t)
12670           (if (= (char-after (match-beginning 0)) ?!)
12671               ;; If the word begins with a bang (!), this is a "not"
12672               ;; spec. We put this spec (minus the bang) and the
12673               ;; symbol `ignore' into the list.
12674               (setq out (cons (cons (concat 
12675                                      "^" (buffer-substring 
12676                                           (1+ (match-beginning 0))
12677                                           (match-end 0)))
12678                                     'ignore) out))
12679             ;; There was no bang, so this is a "yes" spec.
12680             (setq out (cons (cons (concat 
12681                                    "^" (buffer-substring (match-beginning 0)
12682                                                          (match-end 0)))
12683                                   'subscribe) out)))))
12684     
12685       (setq gnus-newsrc-options-n out))))
12686                
12687
12688 (defun gnus-save-newsrc-file ()
12689   "Save .newsrc file."
12690   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12691   ;; from the variable gnus-newsrc-alist.
12692   (and (or gnus-newsrc-alist gnus-killed-list)
12693        gnus-current-startup-file
12694        (progn
12695          (run-hooks 'gnus-save-newsrc-hook)
12696          (save-excursion
12697            (if (or (not gnus-dribble-buffer)
12698                    (not (buffer-name gnus-dribble-buffer))
12699                    (zerop (save-excursion
12700                             (set-buffer gnus-dribble-buffer)
12701                             (buffer-size))))
12702                (gnus-message 4 "(No changes need to be saved)")
12703              (if gnus-save-newsrc-file
12704                  (progn
12705                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12706                    ;; Make backup file of master newsrc.
12707                    (gnus-gnus-to-newsrc-format)
12708                    (gnus-message 5 "Saving %s...done"
12709                                  gnus-current-startup-file)))
12710              ;; Quickly loadable .newsrc.
12711              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12712              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
12713              (gnus-add-current-to-buffer-list)
12714              (buffer-disable-undo (current-buffer))
12715              (erase-buffer)
12716              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12717              (gnus-gnus-to-quick-newsrc-format)
12718              (save-buffer)
12719              (kill-buffer (current-buffer))
12720              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12721              (gnus-dribble-delete-file))))))
12722
12723 (defun gnus-gnus-to-quick-newsrc-format ()
12724   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12725   (insert ";; (ding) Gnus startup file.\n")
12726   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12727   (insert ";; to read .newsrc.\n")
12728   (let ((variables gnus-variable-list)
12729         (inhibit-quit t)
12730         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12731         variable)
12732     ;; insert lisp expressions.
12733     (gnus-compress-newsrc-alist)
12734     (while variables
12735       (setq variable (car variables))
12736       (and (boundp variable)
12737            (symbol-value variable)
12738            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12739            (insert "(setq " (symbol-name variable) " '"
12740                    (prin1-to-string (symbol-value variable))
12741                    ")\n"))
12742       (setq variables (cdr variables)))
12743     (gnus-uncompress-newsrc-alist)))
12744
12745
12746 (defun gnus-gnus-to-newsrc-format ()
12747   ;; Generate and save the .newsrc file.
12748   (let ((newsrc (cdr gnus-newsrc-alist))
12749         info ranges range)
12750     (save-excursion
12751       (set-buffer (create-file-buffer gnus-startup-file))
12752       (set-visited-file-name gnus-startup-file)
12753       (buffer-disable-undo (current-buffer))
12754       (erase-buffer)
12755       ;; Write options.
12756       (if gnus-newsrc-options (insert gnus-newsrc-options))
12757       ;; Write subscribed and unsubscribed.
12758       (while newsrc
12759         (setq info (car newsrc))
12760         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12761             (progn
12762               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12763                                      "!" ":"))
12764               (if (setq ranges (nth 2 info))
12765                   (progn
12766                     (insert " ")
12767                     (if (not (listp (cdr ranges)))
12768                         (if (= (car ranges) (cdr ranges))
12769                             (insert (int-to-string (car ranges)))
12770                           (insert (int-to-string (car ranges)) "-" 
12771                                   (int-to-string (cdr ranges))))
12772                       (while ranges
12773                         (setq range (car ranges)
12774                               ranges (cdr ranges))
12775                         (if (or (atom range) (= (car range) (cdr range)))
12776                             (insert (int-to-string 
12777                                      (or (and (atom range) range) 
12778                                          (car range))))
12779                           (insert (int-to-string (car range)) "-"
12780                                   (int-to-string (cdr range))))
12781                         (if ranges (insert ","))))))
12782               (insert "\n")))
12783         (setq newsrc (cdr newsrc)))
12784       (save-buffer)
12785       (kill-buffer (current-buffer)))))
12786
12787 (defun gnus-read-all-descriptions-files ()
12788   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12789     (while methods
12790       (gnus-read-descriptions-file (car methods))
12791       (setq methods (cdr methods)))
12792     t))
12793
12794 (defun gnus-read-descriptions-file (&optional method)
12795   (let ((method (or method gnus-select-method)))
12796     ;; We create the hashtable whether we manage to read the desc file
12797     ;; to avoid trying to re-read after a failed read.
12798     (or gnus-description-hashtb
12799         (setq gnus-description-hashtb 
12800               (gnus-make-hashtable (length gnus-active-hashtb))))
12801     ;; Mark this method's desc file as read.
12802     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12803                   gnus-description-hashtb)
12804
12805     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12806     (cond 
12807      ((not (or (gnus-server-opened method)
12808                (gnus-open-server method)))
12809       (gnus-message 1 "Couldn't open server")
12810       nil)
12811      ((not (gnus-request-list-newsgroups method))
12812       (gnus-message 1 "Couldn't read newsgroups descriptions")
12813       nil)
12814      (t
12815       (let (group)
12816         (save-excursion
12817           (save-restriction
12818             (set-buffer nntp-server-buffer)
12819             (goto-char (point-min))
12820             (if (or (search-forward "\n.\n" nil t)
12821                     (goto-char (point-max)))
12822                 (progn
12823                   (beginning-of-line)
12824                   (narrow-to-region (point-min) (point))))
12825             (goto-char (point-min))
12826             (while (not (eobp))
12827               ;; If we get an error, we set group to 0, which is not a
12828               ;; symbol... 
12829               (setq group 
12830                     (condition-case ()
12831                         (let ((obarray gnus-description-hashtb))
12832                           ;; Group is set to a symbol interned in this
12833                           ;; hash table.
12834                           (read nntp-server-buffer))
12835                       (error 0)))
12836               (skip-chars-forward " \t")
12837               ;; ... which leads to this line being effectively ignored.
12838               (and (symbolp group)
12839                    (set group (buffer-substring 
12840                                (point) (progn (end-of-line) (point)))))
12841               (forward-line 1))))
12842         (gnus-message 5 "Reading descriptions file...done")
12843         t)))))
12844
12845 (defun gnus-group-get-description (group)
12846   ;; Get the description of a group by sending XGTITLE to the server.
12847   (and (gnus-request-group-description group)
12848        (save-excursion
12849          (set-buffer nntp-server-buffer)
12850          (goto-char (point-min))
12851          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12852               (buffer-substring (match-beginning 1) (match-end 1))))))
12853
12854 ;;;
12855 ;;; Server
12856 ;;;
12857
12858 (defvar gnus-server-mode-hook nil
12859   "Hook run in `gnus-server-mode' buffers.")
12860
12861 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12862   "Format of server lines.
12863 It works along the same lines as a normal formatting string,
12864 with some simple extensions.")
12865
12866 (defvar gnus-server-mode-line-format "(ding) List of servers"
12867   "The format specification for the server mode line.")
12868
12869 (defconst gnus-server-line-format-alist
12870   (list (list ?h 'how ?s)
12871         (list ?n 'name ?s)
12872         (list ?w 'where ?s)
12873         ))
12874
12875 (defconst gnus-server-mode-line-format-alist 
12876   (list (list ?S 'news-server ?s)
12877         (list ?M 'news-method ?s)
12878         (list ?u 'user-defined ?s)))
12879
12880 (defvar gnus-server-line-format-spec nil)
12881 (defvar gnus-server-mode-line-format-spec nil)
12882 (defvar gnus-server-killed-servers nil)
12883
12884 (defvar gnus-server-mode-map nil)
12885 (put 'gnus-server-mode 'mode-class 'special)
12886
12887 (if gnus-server-mode-map
12888     nil
12889   (setq gnus-server-mode-map (make-sparse-keymap))
12890   (suppress-keymap gnus-server-mode-map)
12891   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12892   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12893   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12894   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12895   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12896   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12897   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12898   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12899   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12900   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12901
12902 (defun gnus-server-mode ()
12903   "Major mode for listing and editing servers.
12904
12905 All normal editing commands are switched off.
12906 \\<gnus-server-mode-map>
12907
12908 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12909
12910 The following commands are available:
12911
12912 \\{gnus-server-mode-map}"
12913   (interactive)
12914   (if gnus-visual (gnus-server-make-menu-bar))
12915   (kill-all-local-variables)
12916   (setq mode-line-modified "-- ")
12917   (make-local-variable 'mode-line-format)
12918   (setq mode-line-format (copy-sequence mode-line-format))
12919   (and (equal (nth 3 mode-line-format) "   ")
12920        (setcar (nthcdr 3 mode-line-format) ""))
12921   (setq major-mode 'gnus-server-mode)
12922   (setq mode-name "Server")
12923 ;  (gnus-group-set-mode-line)
12924   (setq mode-line-process nil)
12925   (use-local-map gnus-server-mode-map)
12926   (buffer-disable-undo (current-buffer))
12927   (setq truncate-lines t)
12928   (setq buffer-read-only t)
12929   (run-hooks 'gnus-server-mode-hook))
12930
12931 (defun gnus-server-insert-server-line (sformat name method)
12932   (let* ((sformat (or sformat gnus-server-line-format-spec))
12933          (how (car method))
12934          (where (nth 1 method))
12935          b)
12936     (beginning-of-line)
12937     (setq b (point))
12938     ;; Insert the text.
12939     (insert (eval sformat))
12940     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
12941
12942 (defun gnus-server-setup-buffer ()
12943   (if (get-buffer gnus-server-buffer)
12944       ()
12945     (save-excursion
12946       (set-buffer (get-buffer-create gnus-server-buffer))
12947       (gnus-server-mode)
12948       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12949
12950 (defun gnus-server-prepare ()
12951   (setq gnus-server-mode-line-format-spec 
12952         (gnus-parse-format gnus-server-mode-line-format 
12953                            gnus-server-mode-line-format-alist))
12954   (setq gnus-server-line-format-spec 
12955         (gnus-parse-format gnus-server-line-format 
12956                            gnus-server-line-format-alist))
12957   (let ((alist gnus-server-alist)
12958         (buffer-read-only nil))
12959     (erase-buffer)
12960     (while alist
12961       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12962       (setq alist (cdr alist))))
12963   (goto-char (point-min))
12964   (gnus-server-position-cursor))
12965
12966 (defun gnus-server-server-name ()
12967   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12968     (and server (symbol-name server))))
12969
12970 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12971
12972 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12973
12974 (defun gnus-server-update-server (server)
12975   (save-excursion
12976     (set-buffer gnus-server-buffer)
12977     (let ((buffer-read-only nil)
12978           (info (cdr (assoc server gnus-server-alist))))
12979       (gnus-dribble-enter 
12980        (concat "(gnus-server-set-info \"" server "\" '"
12981                (prin1-to-string info) ")"))
12982       ;; Buffer may be narrowed.
12983       (save-restriction
12984         (widen)
12985         (if (gnus-server-goto-server server)
12986             (delete-region (progn (beginning-of-line) (point))
12987                            (progn (forward-line 1) (point))))
12988         (let ((entry (assoc server gnus-server-alist)))
12989           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12990           (gnus-server-position-cursor))))))
12991
12992 (defun gnus-server-set-info (server info)
12993   ;; Enter a select method into the virtual server alist.
12994   (gnus-dribble-enter 
12995    (concat "(gnus-server-set-info \"" server "\" '"
12996            (prin1-to-string info) ")"))
12997   (let* ((server (nth 1 info))
12998          (entry (assoc server gnus-server-alist)))
12999     (if entry (setcdr entry info)
13000       (setq gnus-server-alist
13001             (nconc gnus-server-alist (list (cons server info)))))))
13002
13003 (defun gnus-server-to-method (server)
13004   ;; Map virtual server names to select methods.
13005   (or (and (equal server "native") gnus-select-method)
13006       (cdr (assoc server gnus-server-alist))))
13007
13008 (defun gnus-server-extend-method (group method)
13009   ;; This function "extends" a virtual server.  If the server is
13010   ;; "hello", and the select method is ("hello" (my-var "something")) 
13011   ;; in the group "alt.alt", this will result in a new virtual server
13012   ;; called "helly+alt.alt".
13013   (let ((entry
13014          (gnus-copy-sequence 
13015           (if (equal (car method) "native") gnus-select-method
13016               (cdr (assoc (car method) gnus-server-alist))))))
13017     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13018     (nconc entry (cdr method))))
13019
13020 (defun gnus-server-get-method (group method)
13021   ;; Input either a server name, and extended server name, or a
13022   ;; select method, and return a select method. 
13023   (cond ((stringp method)
13024          (gnus-server-to-method method))
13025         ((stringp (car method))
13026          (gnus-server-extend-method group method))
13027         (t
13028          (gnus-server-add-address method))))
13029
13030 (defun gnus-server-add-address (method)
13031   (let ((method-name (symbol-name (car method))))
13032     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13033              (not (assq (intern (concat method-name "-address")) method)))
13034         (append method (list (list (intern (concat method-name "-address"))
13035                                    (nth 1 method))))
13036       method)))
13037
13038 (defun gnus-server-equal (s1 s2)
13039   (or (equal s1 s2)
13040       (and (= (length s1) (length s2))
13041            (progn
13042              (while (and s1 (member (car s1) s2))
13043                (setq s1 (cdr s1)))
13044              (null s1)))))
13045
13046 ;;; Interactive server functions.
13047
13048 (defun gnus-server-kill-server (server)
13049   "Kill the server on the current line."
13050   (interactive (list (gnus-server-server-name)))
13051   (or (gnus-server-goto-server server)
13052       (if server (error "No such server: %s" server)
13053         (error "No server on the current line")))
13054   (let ((buffer-read-only nil))
13055     (delete-region (progn (beginning-of-line) (point))
13056                    (progn (forward-line 1) (point))))
13057   (setq gnus-server-killed-servers 
13058         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13059   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13060                                 gnus-server-alist))
13061   (gnus-server-position-cursor))
13062
13063 (defun gnus-server-yank-server ()
13064   "Yank the previously killed server."
13065   (interactive)
13066   (or gnus-server-killed-servers
13067       (error "No killed servers to be yanked"))
13068   (let ((alist gnus-server-alist)
13069         (server (gnus-server-server-name))
13070         (killed (car gnus-server-killed-servers)))
13071     (if (not server) 
13072         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13073       (if (string= server (car (car gnus-server-alist)))
13074           (setq gnus-server-alist (cons killed gnus-server-alist))
13075         (while (and (cdr alist)
13076                     (not (string= server (car (car (cdr alist))))))
13077           (setq alist (cdr alist)))
13078         (setcdr alist (cons killed (cdr alist)))))
13079     (gnus-server-update-server (car killed))
13080     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13081     (gnus-server-position-cursor)))
13082
13083 (defun gnus-server-exit ()
13084   "Return to the group buffer."
13085   (interactive)
13086   (kill-buffer (current-buffer))
13087   (switch-to-buffer gnus-group-buffer))
13088
13089 (defun gnus-server-list-servers ()
13090   "List all available servers."
13091   (interactive)
13092   (let ((cur (gnus-server-server-name)))
13093     (gnus-server-prepare)
13094     (if cur (gnus-server-goto-server cur)
13095       (goto-char (point-max))
13096       (forward-line -1))
13097     (gnus-server-position-cursor)))
13098
13099 (defun gnus-server-copy-server (from to)
13100   (interactive
13101    (list
13102     (or (gnus-server-server-name)
13103         (error "No server on the current line"))
13104     (read-string "Copy to: ")))
13105   (or from (error "No server on current line"))
13106   (or (and to (not (string= to ""))) (error "No name to copy to"))
13107   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13108   (or (assoc from gnus-server-alist) 
13109       (error "%s: no such server" from))
13110   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13111     (setcar to-entry to)
13112     (setcar (nthcdr 2 to-entry) to)
13113     (setq gnus-server-killed-servers 
13114           (cons to-entry gnus-server-killed-servers))
13115     (gnus-server-yank-server)))
13116
13117 (defun gnus-server-add-server (how where)
13118   (interactive 
13119    (list (intern (completing-read "Server method: "
13120                                   gnus-valid-select-methods nil t))
13121          (read-string "Server name: ")))
13122   (setq gnus-server-killed-servers 
13123         (cons (list where how where) gnus-server-killed-servers))
13124   (gnus-server-yank-server))
13125
13126 (defun gnus-server-goto-server (server)
13127   "Jump to a server line."
13128   (interactive
13129    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13130   (let ((to (text-property-any (point-min) (point-max) 
13131                                'gnus-server (intern server))))
13132     (and to
13133          (progn
13134            (goto-char to) 
13135            (gnus-server-position-cursor)))))
13136
13137 (defun gnus-server-edit-server (server)
13138   "Edit the server on the current line."
13139   (interactive (list (gnus-server-server-name)))
13140   (or server
13141       (error "No server on current line"))
13142   (let ((winconf (current-window-configuration)))
13143     (get-buffer-create gnus-server-edit-buffer)
13144     (gnus-configure-windows 'edit-server)
13145     (gnus-add-current-to-buffer-list)
13146     (emacs-lisp-mode)
13147     (make-local-variable 'gnus-prev-winconf)
13148     (setq gnus-prev-winconf winconf)
13149     (use-local-map (copy-keymap (current-local-map)))
13150     (let ((done-func '(lambda () 
13151                         "Exit editing mode and update the information."
13152                         (interactive)
13153                         (gnus-server-edit-server-done 'group))))
13154       (setcar (cdr (nth 4 done-func)) server)
13155       (local-set-key "\C-c\C-c" done-func))
13156     (erase-buffer)
13157     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13158     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13159
13160 (defun gnus-server-edit-server-done (server)
13161   (interactive)
13162   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13163   (goto-char (point-min))
13164   (let ((form (read (current-buffer)))
13165         (winconf gnus-prev-winconf))
13166     (gnus-server-set-info server form)
13167     (kill-buffer (current-buffer))
13168     (and winconf (set-window-configuration winconf))
13169     (set-buffer gnus-server-buffer)
13170     (gnus-server-update-server (gnus-server-server-name))
13171     (gnus-server-position-cursor)))
13172
13173 (defun gnus-server-read-server (server)
13174   "Browse a server."
13175   (interactive (list (gnus-server-server-name)))
13176   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13177
13178 (defun gnus-mouse-pick-server (e)
13179   (interactive "e")
13180   (mouse-set-point e)
13181   (gnus-server-read-server (gnus-server-server-name)))
13182
13183 ;;;
13184 ;;; entry points into gnus-score.el
13185 ;;;
13186
13187 ;;; Finding score files. 
13188
13189 (defvar gnus-global-score-files nil
13190   "*List of global score files and directories.
13191 Set this variable if you want to use people's score files.  One entry
13192 for each score file or each score file directory.  Gnus will decide
13193 by itself what score files are applicable to which group.
13194
13195 Say you want to use the single score file
13196 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13197 score files in the \"/ftp.some-where:/pub/score\" directory.
13198
13199  (setq gnus-global-score-files
13200        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13201          \"/ftp.some-where:/pub/score\"))")
13202
13203 (defun gnus-score-score-files (group)
13204   "Return a list of all possible score files."
13205   ;; Search and set any global score files.
13206   (and gnus-global-score-files 
13207        (or gnus-internal-global-score-files
13208            (gnus-score-search-global-directories gnus-global-score-files)))
13209   ;; Fix the kill-file dir variable.
13210   (setq gnus-kill-files-directory 
13211         (file-name-as-directory
13212          (or gnus-kill-files-directory "~/News/")))
13213   ;; If we can't read it, there are no score files.
13214   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13215       (setq gnus-score-file-list nil)
13216     (if (gnus-use-long-file-name 'not-score)
13217         ;; We want long file names.
13218         (if (or (not gnus-score-file-list)
13219                 (not (car gnus-score-file-list))
13220                 (gnus-file-newer-than gnus-kill-files-directory
13221                                       (car gnus-score-file-list)))
13222               (setq gnus-score-file-list 
13223                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13224                           (nreverse 
13225                            (directory-files 
13226                             gnus-kill-files-directory t 
13227                             (gnus-score-file-regexp))))))
13228       ;; We do not use long file names, so we have to do some
13229       ;; directory traversing.  
13230       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13231             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13232             dir files suffix)
13233         (while suffixes
13234           (setq dir (expand-file-name
13235                      (concat gnus-kill-files-directory
13236                              (gnus-replace-chars-in-string group ?. ?/))))
13237           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13238           (setq suffix (car suffixes)
13239                 suffixes (cdr suffixes))
13240           (if (file-exists-p (concat dir "/" suffix))
13241               (setq files (cons (concat dir "/" suffix) files)))
13242           (while (>= (1+ (length dir)) mdir)
13243             (and (file-exists-p (concat dir "/all/" suffix))
13244                  (setq files (cons (concat dir "/all/" suffix) files)))
13245             (string-match "/[^/]*$" dir)
13246             (setq dir (substring dir 0 (match-beginning 0)))))
13247         (setq gnus-score-file-list 
13248               (cons nil (nreverse files)))))
13249     (cdr gnus-score-file-list)))
13250
13251 (defun gnus-score-file-regexp ()
13252   (concat "\\(" gnus-score-file-suffix 
13253           "\\|" gnus-adaptive-file-suffix "\\)$"))
13254         
13255 (defun gnus-score-find-bnews (group)
13256   "Return a list of score files for GROUP.
13257 The score files are those files in the ~/News directory which matches
13258 GROUP using BNews sys file syntax."
13259   (let* ((sfiles (append (gnus-score-score-files group)
13260                          gnus-internal-global-score-files))
13261          (kill-dir (file-name-as-directory 
13262                     (expand-file-name gnus-kill-files-directory)))
13263          (klen (length kill-dir))
13264          ofiles not-match regexp)
13265     (save-excursion
13266       (set-buffer (get-buffer-create "*gnus score files*"))
13267       (buffer-disable-undo (current-buffer))
13268       ;; Go through all score file names and create regexp with them
13269       ;; as the source.  
13270       (while sfiles
13271         (erase-buffer)
13272         (insert (car sfiles))
13273         (goto-char (point-min))
13274         ;; First remove the suffix itself.
13275         (re-search-forward (concat "." (gnus-score-file-regexp)))
13276         (replace-match "" t t) 
13277         (goto-char (point-min))
13278         (if (looking-at (regexp-quote kill-dir))
13279             ;; If the file name was just "SCORE", `klen' is one character
13280             ;; too much.
13281             (delete-char (min (1- (point-max)) klen))
13282           (goto-char (point-max))
13283           (search-backward "/")
13284           (delete-region (1+ (point)) (point-min)))
13285         ;; If short file names were used, we have to translate slashes.
13286         (goto-char (point-min))
13287         (while (re-search-forward "[/:]" nil t)
13288           (replace-match "." t t))
13289         ;; Translate "all" to ".*".
13290         (while (search-forward "all" nil t)
13291           (replace-match ".*" t t))
13292         (goto-char (point-min))
13293         ;; Deal with "not."s.
13294         (if (looking-at "not.")
13295             (progn
13296               (setq not-match t)
13297               (setq regexp (buffer-substring 5 (point-max))))
13298           (setq regexp (buffer-substring 1 (point-max)))
13299           (setq not-match nil))
13300         ;; Finally - if this resulting regexp matches the group name,
13301         ;; we add this score file to the list of score files
13302         ;; applicable to this group.
13303         (if (or (and not-match
13304                      (not (string-match regexp group)))
13305                 (and (not not-match)
13306                      (string-match regexp group)))
13307             (setq ofiles (cons (car sfiles) ofiles)))
13308         (setq sfiles (cdr sfiles)))
13309       (kill-buffer (current-buffer))
13310       ;; Slight kludge here - the last score file returned should be
13311       ;; the local score file, whether it exists or not. This is so
13312       ;; that any score commands the user enters will go to the right
13313       ;; file, and not end up in some global score file.
13314       (let ((localscore
13315              (expand-file-name
13316               (if (gnus-use-long-file-name 'not-score)
13317                   (concat gnus-kill-files-directory group "." 
13318                           gnus-score-file-suffix)
13319                 (concat gnus-kill-files-directory
13320                         (gnus-replace-chars-in-string group ?. ?/)
13321                         "/" gnus-score-file-suffix)))))
13322         (and (member localscore ofiles)
13323              (delete localscore ofiles))
13324         (setq ofiles (cons localscore ofiles)))
13325       (nreverse ofiles))))
13326
13327 (defun gnus-score-find-single (group)
13328   "Return list containing the score file for GROUP."
13329   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13330         (gnus-score-file-name group)))
13331
13332 (defun gnus-score-find-hierarchical (group)
13333   "Return list of score files for GROUP.
13334 This includes the score file for the group and all its parents."
13335   (let ((all (copy-sequence '(nil)))
13336         (start 0))
13337     (while (string-match "\\." group (1+ start))
13338       (setq start (match-beginning 0))
13339       (setq all (cons (substring group 0 start) all)))
13340     (setq all (cons group all))
13341     (nconc
13342      (mapcar (lambda (newsgroup)
13343                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13344              (setq all (nreverse all)))
13345      (mapcar 'gnus-score-file-name all))))
13346
13347 (defvar gnus-score-file-alist-cache nil)
13348
13349 (defun gnus-score-find-alist (group)
13350   "Return list of score files for GROUP.
13351 The list is determined from the variable gnus-score-file-alist."
13352   (let ((alist gnus-score-file-multiple-match-alist)
13353         score-files)
13354     ;; if this group has been seen before, return the cached entry
13355     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13356         (cdr score-files)       ; ensures caching of groups with no matches
13357       ;; handle the multiple match alist
13358       (while alist
13359         (and (string-match (car (car alist)) group)
13360              (setq score-files
13361                    (nconc score-files (cdr (car alist)))))
13362         (setq alist (cdr alist)))
13363       (setq alist gnus-score-file-single-match-alist)
13364       ;; handle the single match alist
13365       (catch 'done
13366         (while alist
13367           (and (string-match (car (car alist)) group)
13368                ;; progn used just in case ("regexp") has no files
13369                ;; and score-files is still nil. -sj
13370                ;; this can be construed as a "stop searching here" feature :>
13371                ;; and used to simplify regexps in the single-alist 
13372                (progn
13373                  (setq score-files
13374                        (append score-files (cdr (car alist))))
13375                  (throw 'done nil)))
13376           (setq alist (cdr alist))))
13377       ;; cache the score files
13378       (setq gnus-score-file-alist-cache
13379             (cons (cons group score-files) gnus-score-file-alist-cache))
13380       score-files)))
13381
13382
13383 (defun gnus-possibly-score-headers (&optional trace)
13384   (let ((func gnus-score-find-score-files-function)
13385         score-files)
13386     (and func (not (listp func))
13387          (setq func (list func)))
13388     ;; Go through all the functions for finding score files (or actual
13389     ;; scores) and add them to a list.
13390     (setq score-files (copy-sequence
13391                        (gnus-score-find-alist gnus-newsgroup-name)))
13392     (while func
13393       (and (symbolp (car func))
13394            (fboundp (car func))
13395            (setq score-files 
13396                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13397       (setq func (cdr func)))
13398     (if score-files (gnus-score-headers score-files trace))))
13399
13400 (defun gnus-score-file-name (newsgroup &optional suffix)
13401   "Return the name of a score file for NEWSGROUP."
13402   (let ((suffix (or suffix gnus-score-file-suffix)))
13403     (cond  ((or (null newsgroup)
13404                 (string-equal newsgroup ""))
13405             ;; The global score file is placed at top of the directory.
13406             (expand-file-name 
13407              suffix (or gnus-kill-files-directory "~/News")))
13408            ((gnus-use-long-file-name 'not-score)
13409             ;; Append ".SCORE" to newsgroup name.
13410             (expand-file-name (concat newsgroup "." suffix)
13411                               (or gnus-kill-files-directory "~/News")))
13412            (t
13413             ;; Place "SCORE" under the hierarchical directory.
13414             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13415                                       "/" suffix)
13416                               (or gnus-kill-files-directory "~/News"))))))
13417
13418 (defun gnus-score-search-global-directories (files)
13419   "Scan all global score directories for score files."
13420   ;; Set the variable `gnus-internal-global-score-files' to all
13421   ;; available global score files.
13422   (interactive (list gnus-global-score-files))
13423   (let (out)
13424     (while files
13425       (if (string-match "/$" (car files))
13426           (setq out (nconc (directory-files 
13427                             (car files) t
13428                             (concat (gnus-score-file-regexp) "$"))))
13429         (setq out (cons (car files) out)))
13430       (setq files (cdr files)))
13431     (setq gnus-internal-global-score-files out)))
13432
13433 ;; Allow redefinition of Gnus functions.
13434
13435 (gnus-ems-redefine)
13436
13437 (provide 'gnus)
13438
13439 ;;; gnus.el ends here