*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find score files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own score file.
242 gnus-score-find-hierarchical: Also apply score files from parent groups.
243 gnus-score-find-bnews: Apply score files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread t
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups 4
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed 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 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
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 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
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-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
775 `gnus-group-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   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095 (add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1096
1097 (defvar gnus-article-x-face-command
1098   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1099   "String or function to be executed to display an X-Face header.
1100 If it is a string, the command will be executed in a sub-shell
1101 asynchronously. The compressed face will be piped to this command.") 
1102
1103 (defvar gnus-article-x-face-too-ugly nil
1104   "Regexp matching posters whose face shouldn't be shown automatically.")
1105
1106 (defvar gnus-select-group-hook nil
1107   "*A hook called when a newsgroup is selected.
1108
1109 If you'd like to simplify subjects like the
1110 `gnus-summary-next-same-subject' command does, you can use the
1111 following hook:
1112
1113  (setq gnus-select-group-hook
1114       (list
1115         (lambda ()
1116           (mapcar (lambda (header)
1117                      (header-set-subject
1118                       header
1119                       (gnus-simplify-subject
1120                        (header-subject header) 're-only)))
1121                   gnus-newsgroup-headers))))")
1122
1123 (defvar gnus-select-article-hook
1124   '(gnus-summary-show-thread)
1125   "*A hook called when an article is selected.
1126 The default hook shows conversation thread subtrees of the selected
1127 article automatically using `gnus-summary-show-thread'.")
1128
1129 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1130   "*A hook called to apply kill files to a group.
1131 This hook is intended to apply a kill file to the selected newsgroup.
1132 The function `gnus-apply-kill-file' is called by default.
1133
1134 Since a general kill file is too heavy to use only for a few
1135 newsgroups, I recommend you to use a lighter hook function. For
1136 example, if you'd like to apply a kill file to articles which contains
1137 a string `rmgroup' in subject in newsgroup `control', you can use the
1138 following hook:
1139
1140 \(setq gnus-apply-kill-hook
1141       (list
1142         (lambda ()
1143           (cond ((string-match \"control\" gnus-newsgroup-name)
1144                  (gnus-kill \"Subject\" \"rmgroup\")
1145                  (gnus-expunge \"X\"))))))")
1146
1147 (defvar gnus-visual-mark-article-hook 
1148   (list 'gnus-highlight-selected-summary)
1149   "*Hook run after selecting an article in the summary buffer.
1150 It is meant to be used for highlighting the article in some way.  It
1151 is not run if `gnus-visual' is nil.")
1152
1153 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1154   "*A hook called after preparing body, but before preparing header headers.
1155 The default hook (`gnus-inews-insert-signature') inserts a signature
1156 file specified by the variable `gnus-signature-file'.")
1157
1158 (defvar gnus-exit-group-hook nil
1159   "*A hook called when exiting (not quitting) summary mode.")
1160
1161 (defvar gnus-suspend-gnus-hook nil
1162   "*A hook called when suspending (not exiting) Gnus.")
1163
1164 (defvar gnus-exit-gnus-hook nil
1165   "*A hook called when exiting Gnus.")
1166
1167 (defvar gnus-save-newsrc-hook nil
1168   "*A hook called when saving the newsrc file.")
1169
1170 (defvar gnus-summary-update-hook 
1171   (list 'gnus-summary-highlight-line)
1172   "*A hook called when a summary line is changed.
1173 The hook will not be called if `gnus-visual' is nil.
1174
1175 The default function `gnus-summary-highlight-line' will
1176 highlight the line according to the `gnus-summary-highlight'
1177 variable.")
1178
1179 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1180   "*A hook called when an article is selected for the first time.
1181 The hook is intended to mark an article as read (or unread)
1182 automatically when it is selected.")
1183
1184 ;; Remove any hilit infestation.
1185 (add-hook 'gnus-startup-hook
1186           (lambda ()
1187             (remove-hook 'gnus-summary-prepare-hook
1188                          'hilit-rehighlight-buffer-quietly)
1189             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1190             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1191             (remove-hook 'gnus-article-prepare-hook
1192                          'hilit-rehighlight-buffer-quietly)))
1193
1194
1195 \f
1196 ;; Internal variables
1197
1198 ;; Avoid highlighting in kill files.
1199 (defvar gnus-summary-inhibit-highlight nil)
1200 (defvar gnus-newsgroup-selected-overlay nil)
1201
1202 (defvar gnus-article-mode-map nil)
1203 (defvar gnus-dribble-buffer nil)
1204 (defvar gnus-headers-retrieved-by nil)
1205 (defvar gnus-article-reply nil)
1206 (defvar gnus-override-method nil)
1207 (defvar gnus-article-check-size nil)
1208
1209 (defvar gnus-current-score-file nil)
1210 (defvar gnus-internal-global-score-files nil)
1211 (defvar gnus-score-file-list nil)
1212
1213
1214 (defvar gnus-current-move-group nil)
1215
1216 (defvar gnus-newsgroup-dependencies nil)
1217 (defvar gnus-newsgroup-threads nil)
1218 (defvar gnus-newsgroup-async nil)
1219 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1220
1221 (defvar gnus-newsgroup-adaptive nil)
1222
1223 (defvar gnus-summary-display-table nil)
1224
1225 (defconst gnus-group-line-format-alist
1226   (list (list ?M 'marked ?c)
1227         (list ?S 'subscribed ?c)
1228         (list ?L 'level ?d)
1229         (list ?N 'number ?s)
1230         (list ?I 'number-of-dormant ?d)
1231         (list ?T 'number-of-ticked ?d)
1232         (list ?R 'number-of-read ?s)
1233         (list ?t 'number-total ?d)
1234         (list ?y 'number-of-unread-unticked ?s)
1235         (list ?i 'number-of-ticked-and-dormant ?d)
1236         (list ?g 'group ?s)
1237         (list ?G 'qualified-group ?s)
1238         (list ?D 'newsgroup-description ?s)
1239         (list ?o 'moderated ?c)
1240         (list ?O 'moderated-string ?s)
1241         (list ?p 'process-marked ?c)
1242         (list ?s 'news-server ?s)
1243         (list ?n 'news-method ?s)
1244         (list ?z 'news-method-string ?s)
1245         (list ?u 'user-defined ?s)))
1246
1247 (defconst gnus-summary-line-format-alist 
1248   (list (list ?N 'number ?d)
1249         (list ?S 'subject ?s)
1250         (list ?s 'subject-or-nil ?s)
1251         (list ?n 'name ?s)
1252         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1253               ?s)
1254         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1255                       from) ?s)
1256         (list ?F 'from ?s)
1257         (list ?x (macroexpand '(header-xref header)) ?s)
1258         (list ?D (macroexpand '(header-date header)) ?s)
1259         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1260         (list ?M (macroexpand '(header-id header)) ?s)
1261         (list ?r (macroexpand '(header-references header)) ?s)
1262         (list ?c '(or (header-chars header) 0) ?d)
1263         (list ?L 'lines ?d)
1264         (list ?I 'indentation ?s)
1265         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1266         (list ?R 'replied ?c)
1267         (list ?\[ 'opening-bracket ?c)
1268         (list ?\] 'closing-bracket ?c)
1269         (list ?\> '(make-string level ? ) ?s)
1270         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1271         (list ?i 'score ?d)
1272         (list ?z 'score-char ?c)
1273         (list ?U 'unread ?c)
1274         (list ?t '(gnus-summary-number-of-articles-in-thread 
1275                    (and (boundp 'thread) (car thread)))
1276               ?d)
1277         (list ?e '(gnus-summary-number-of-articles-in-thread 
1278                    (and (boundp 'thread) (car thread)) t)
1279               ?c)
1280         (list ?u 'user-defined ?s))
1281   "An alist of format specifications that can appear in summary lines,
1282 and what variables they correspond with, along with the type of the
1283 variable (string, integer, character, etc).")
1284
1285 (defconst gnus-summary-dummy-line-format-alist
1286   (list (list ?S 'subject ?s)
1287         (list ?N 'number ?d)
1288         (list ?u 'user-defined ?s)))
1289
1290 (defconst gnus-summary-mode-line-format-alist 
1291   (list (list ?G 'group-name ?s)
1292         (list ?g '(gnus-short-group-name group-name) ?s)
1293         (list ?A 'article-number ?d)
1294         (list ?Z 'unread-and-unselected ?s)
1295         (list ?V 'gnus-version ?s)
1296         (list ?U 'unread ?d)
1297         (list ?S 'subject ?s)
1298         (list ?e 'unselected ?d)
1299         (list ?u 'user-defined ?s)
1300         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1301
1302 (defconst gnus-group-mode-line-format-alist 
1303   (list (list ?S 'news-server ?s)
1304         (list ?M 'news-method ?s)
1305         (list ?u 'user-defined ?s)))
1306
1307 (defvar gnus-have-read-active-file nil)
1308
1309 (defconst gnus-maintainer
1310   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1311   "The mail address of the Gnus maintainers.")
1312
1313 (defconst gnus-version "(ding) Gnus v0.99.14"
1314   "Version number for this version of Gnus.")
1315
1316 (defvar gnus-info-nodes
1317   '((gnus-group-mode            "(gnus)The Group Buffer")
1318     (gnus-summary-mode          "(gnus)The Summary Buffer")
1319     (gnus-article-mode          "(gnus)The Article Buffer"))
1320   "Assoc list of major modes and related Info nodes.")
1321
1322 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1323   "The location of the (ding) Gnus documentation group.")
1324
1325 (defvar gnus-group-buffer "*Group*")
1326 (defvar gnus-summary-buffer "*Summary*")
1327 (defvar gnus-article-buffer "*Article*")
1328 (defvar gnus-server-buffer "*Server*")
1329
1330 (defvar gnus-work-buffer " *gnus work*")
1331
1332 (defvar gnus-buffer-list nil
1333   "Gnus buffers that should be killed on exit.")
1334
1335 (defvar gnus-server-alist nil
1336   "List of available servers.")
1337
1338 (defvar gnus-variable-list
1339   '(gnus-newsrc-options gnus-newsrc-options-n
1340     gnus-newsrc-last-checked-date 
1341     gnus-newsrc-alist gnus-server-alist
1342     gnus-killed-list gnus-zombie-list)
1343   "Gnus variables saved in the quick startup file.")
1344
1345 (defvar gnus-overload-functions
1346   '((news-inews gnus-inews-news "rnewspost"))
1347   "Functions overloaded by gnus.
1348 It is a list of `(original overload &optional file)'.")
1349
1350 (defvar gnus-newsrc-options nil
1351   "Options line in the .newsrc file.")
1352
1353 (defvar gnus-newsrc-options-n nil
1354   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1355
1356 (defvar gnus-newsrc-last-checked-date nil
1357   "Date Gnus last asked server for new newsgroups.")
1358
1359 (defvar gnus-newsrc-alist nil
1360   "Assoc list of read articles.
1361 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1362
1363 (defvar gnus-newsrc-hashtb nil
1364   "Hashtable of gnus-newsrc-alist.")
1365
1366 (defvar gnus-killed-list nil
1367   "List of killed newsgroups.")
1368
1369 (defvar gnus-killed-hashtb nil
1370   "Hash table equivalent of gnus-killed-list.")
1371
1372 (defvar gnus-zombie-list nil
1373   "List of almost dead newsgroups.")
1374
1375 (defvar gnus-description-hashtb nil
1376   "Descriptions of newsgroups.")
1377
1378 (defvar gnus-list-of-killed-groups nil
1379   "List of newsgroups that have recently been killed by the user.")
1380
1381 (defvar gnus-active-hashtb nil
1382   "Hashtable of active articles.")
1383
1384 (defvar gnus-moderated-list nil
1385   "List of moderated newsgroups.")
1386
1387 (defvar gnus-group-marked nil)
1388
1389 (defvar gnus-current-startup-file nil
1390   "Startup file for the current host.")
1391
1392 (defvar gnus-last-search-regexp nil
1393   "Default regexp for article search command.")
1394
1395 (defvar gnus-last-shell-command nil
1396   "Default shell command on article.")
1397
1398 (defvar gnus-current-select-method nil
1399   "The current method for selecting a newsgroup.")
1400
1401 (defvar gnus-group-list-mode nil)
1402
1403 (defvar gnus-article-internal-prepare-hook nil)
1404
1405 (defvar gnus-newsgroup-name nil)
1406 (defvar gnus-newsgroup-begin nil)
1407 (defvar gnus-newsgroup-end nil)
1408 (defvar gnus-newsgroup-last-rmail nil)
1409 (defvar gnus-newsgroup-last-mail nil)
1410 (defvar gnus-newsgroup-last-folder nil)
1411 (defvar gnus-newsgroup-last-file nil)
1412 (defvar gnus-newsgroup-auto-expire nil)
1413 (defvar gnus-newsgroup-active nil)
1414
1415 (defvar gnus-newsgroup-unreads nil
1416   "List of unread articles in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-unselected nil
1419   "List of unselected unread articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-reads nil
1422   "Alist of read articles and article marks in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-marked nil
1425   "List of ticked articles in the current newsgroup (a subset of unread art).")
1426
1427 (defvar gnus-newsgroup-killed nil
1428   "List of ranges of articles that have been through the scoring process.")
1429
1430 (defvar gnus-newsgroup-kill-headers nil)
1431
1432 (defvar gnus-newsgroup-replied nil
1433   "List of articles that have been replied to in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-expirable nil
1436   "List of articles in the current newsgroup that can be expired.")
1437
1438 (defvar gnus-newsgroup-processable nil
1439   "List of articles in the current newsgroup that can be processed.")
1440
1441 (defvar gnus-newsgroup-bookmarks nil
1442   "List of articles in the current newsgroup that have bookmarks.")
1443
1444 (defvar gnus-newsgroup-dormant nil
1445   "List of dormant articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-scored nil
1448   "List of scored articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-headers nil
1451   "List of article headers in the current newsgroup.")
1452 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1453
1454 (defvar gnus-newsgroup-ancient nil
1455   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1456
1457 (defvar gnus-current-article nil)
1458 (defvar gnus-article-current nil)
1459 (defvar gnus-current-headers nil)
1460 (defvar gnus-have-all-headers nil)
1461 (defvar gnus-last-article nil)
1462 (defvar gnus-newsgroup-history nil)
1463 (defvar gnus-current-kill-article nil)
1464
1465 ;; Save window configuration.
1466 (defvar gnus-prev-winconf nil)
1467
1468 ;; Format specs
1469 (defvar gnus-summary-line-format-spec nil)
1470 (defvar gnus-summary-dummy-line-format-spec nil)
1471 (defvar gnus-group-line-format-spec nil)
1472 (defvar gnus-summary-mode-line-format-spec nil)
1473 (defvar gnus-article-mode-line-format-spec nil)
1474 (defvar gnus-group-mode-line-format-spec nil)
1475 (defvar gnus-summary-mark-positions nil)
1476 (defvar gnus-group-mark-positions nil)
1477
1478 (defvar gnus-summary-expunge-below nil)
1479 (defvar gnus-reffed-article-number nil)
1480
1481 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1482 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1483
1484 (defvar gnus-cache-removeable-articles nil)
1485
1486 (defconst gnus-summary-local-variables 
1487   '(gnus-newsgroup-name 
1488     gnus-newsgroup-begin gnus-newsgroup-end 
1489     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1490     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1491     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1492     gnus-newsgroup-unselected gnus-newsgroup-marked
1493     gnus-newsgroup-reads
1494     gnus-newsgroup-replied gnus-newsgroup-expirable
1495     gnus-newsgroup-processable gnus-newsgroup-killed
1496     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1497     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1498     gnus-current-article gnus-current-headers gnus-have-all-headers
1499     gnus-last-article gnus-article-internal-prepare-hook
1500     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1501     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1502     gnus-newsgroup-threads gnus-newsgroup-async
1503     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1504     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1505     gnus-newsgroup-history gnus-newsgroup-ancient
1506     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1507     gnus-cache-removeable-articles)
1508   "Variables that are buffer-local to the summary buffers.")
1509
1510 (defconst gnus-bug-message
1511   "Sending a bug report to the Gnus Towers.
1512 ========================================
1513
1514 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1515 be sent to the Gnus Bug Exterminators. 
1516
1517 At the bottom of the buffer you'll see lots of variable settings.
1518 Please do not delete those.  They will tell the Bug People what your
1519 environment is, so that it will be easier to locate the bugs.
1520
1521 If you have found a bug that makes Emacs go \"beep\", set
1522 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1523 and include the backtrace in your bug report.
1524
1525 Please describe the bug in annoying, painstaking detail.
1526
1527 Thank you for your help in stamping out bugs.
1528 ")
1529
1530 ;;; End of variables.
1531
1532 ;; Define some autoload functions Gnus might use.
1533 (eval-and-compile
1534
1535   ;; Various 
1536   (autoload 'metamail-buffer "metamail")
1537   (autoload 'Info-goto-node "info")
1538   (autoload 'hexl-hex-string-to-integer "hexl")
1539   (autoload 'pp "pp")
1540   (autoload 'pp-to-string "pp")
1541   (autoload 'pp-eval-expression "pp")
1542   (autoload 'mail-extract-address-components "mail-extr")
1543
1544   (autoload 'nnmail-split-fancy "nnmail")
1545   (autoload 'nnvirtual-catchup-group "nnvirtual")
1546
1547   ;; timezone
1548   (autoload 'timezone-make-date-arpa-standard "timezone")
1549   (autoload 'timezone-fix-time "timezone")
1550   (autoload 'timezone-make-sortable-date "timezone")
1551   (autoload 'timezone-make-time-string "timezone")
1552
1553   ;; rmail & friends
1554   (autoload 'mail-position-on-field "sendmail")
1555   (autoload 'mail-setup "sendmail")
1556   (autoload 'rmail-output "rmailout")
1557   (autoload 'news-mail-other-window "rnewspost")
1558   (autoload 'news-reply-yank-original "rnewspost")
1559   (autoload 'news-caesar-buffer-body "rnewspost")
1560   (autoload 'rmail-insert-rmail-file-header "rmail")
1561   (autoload 'rmail-count-new-messages "rmail")
1562   (autoload 'rmail-show-message "rmail")
1563
1564   ;; gnus-soup
1565   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1566   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1567   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1568   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1569   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1570   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1571   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1572
1573   ;; gnus-mh
1574   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1575   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1576   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1577   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1578   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1579   (autoload 'gnus-Folder-save-name "gnus-mh")
1580   (autoload 'gnus-folder-save-name "gnus-mh")
1581
1582   ;; gnus-vis misc
1583   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1584   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1585   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1586   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1587   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1588   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1589   (autoload 'gnus-summary-highlight-line "gnus-vis")
1590   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1591
1592   ;; gnus-vis article
1593   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1594   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1595   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1596   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1597   (autoload 'gnus-article-hide "gnus-vis" nil t)
1598   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1599   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1600   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1601   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1602   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1603   (autoload 'gnus-article-add-button "gnus-vis")
1604
1605   ;; gnus-cite
1606   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1607   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1608   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1609
1610   ;; gnus-kill
1611   (autoload 'gnus-kill "gnus-kill")
1612   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1613   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1614   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1615   (autoload 'gnus-execute "gnus-kill")
1616   (autoload 'gnus-expunge "gnus-kill")
1617
1618   ;; gnus-cache
1619   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1620   (autoload 'gnus-cache-save-buffers "gnus-cache")
1621   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1622   (autoload 'gnus-cache-request-article "gnus-cache")
1623   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1624   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1625   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1626   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1627
1628   ;; gnus-score
1629   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1630   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1631   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1632   (autoload 'gnus-score-save "gnus-score")
1633   (autoload 'gnus-score-headers "gnus-score")
1634   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1635   (autoload 'gnus-score-adaptive "gnus-score")
1636   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1637   (autoload 'gnus-score-find-trace "gnus-score")
1638
1639   ;; gnus-edit
1640   (autoload 'gnus-score-customize "gnus-edit" nil t)
1641
1642   ;; gnus-uu
1643   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1644   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1645   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1646   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1647   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1648   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1649   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1650   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1651   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1652   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1657   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1658   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1662   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1663   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1664   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1665
1666   ;; gnus-msg
1667   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1668   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1669   (autoload 'gnus-group-mail "gnus-msg" nil t)
1670   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1671   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1672   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1673   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1674   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1675   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1676   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1677   (autoload 'gnus-post-news "gnus-msg" nil t)
1678   (autoload 'gnus-inews-news "gnus-msg" nil t)
1679   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1680   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1681   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1682   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1683   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1684   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1685   (autoload 'gnus-mail-yank-original "gnus-msg")
1686   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1687   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1688   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1689   (autoload 'gnus-article-mail-with-original "gnus-msg")
1690   (autoload 'gnus-article-mail "gnus-msg")
1691   (autoload 'gnus-bug "gnus-msg" nil t)
1692
1693   ;; gnus-vm
1694   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1695   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1696   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1697   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1698   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1699   (autoload 'gnus-yank-article "gnus-vm" nil t)
1700
1701   )
1702
1703 \f
1704
1705 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1706 ;; If you want the cursor to go somewhere else, set these two
1707 ;; functions in some startup hook to whatever you want.
1708 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1709 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1710
1711 ;;; Various macros and substs.
1712
1713 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1714   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1715   (` (let ((GnusStartBufferWindow (selected-window)))
1716        (unwind-protect
1717            (progn
1718              (pop-to-buffer (, buffer))
1719              (,@ forms))
1720          (select-window GnusStartBufferWindow)))))
1721
1722 (defmacro gnus-gethash (string hashtable)
1723   "Get hash value of STRING in HASHTABLE."
1724   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1725   ;;(` (abbrev-expansion (, string) (, hashtable)))
1726   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1727
1728 (defmacro gnus-sethash (string value hashtable)
1729   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1730   ;; We cannot use define-abbrev since it only accepts string as value.
1731   ;; (set (intern string hashtable) value))
1732   (` (set (intern (, string) (, hashtable)) (, value))))
1733
1734 (defsubst gnus-buffer-substring (beg end)
1735   (buffer-substring (match-beginning beg) (match-end end)))
1736
1737 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1738 ;;   function `substring' might cut on a middle of multi-octet
1739 ;;   character.
1740 (defun gnus-truncate-string (str width)
1741   (substring str 0 width))
1742
1743 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1744 ;; to limit the length of a string. This function is necessary since
1745 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1746 (defsubst gnus-limit-string (str width)
1747   (if (> (length str) width)
1748       (substring str 0 width)
1749     str))
1750
1751 (defsubst gnus-simplify-subject-re (subject)
1752   "Remove \"Re:\" from subject lines."
1753   (let ((case-fold-search t))
1754     (if (string-match "^re: *" subject)
1755         (substring subject (match-end 0))
1756       subject)))
1757
1758 (defsubst gnus-goto-char (point)
1759   (and point (goto-char point)))
1760
1761 (defmacro gnus-buffer-exists-p (buffer)
1762   (` (and (, buffer)
1763           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1764                    (, buffer)))))
1765
1766 (defmacro gnus-kill-buffer (buffer)
1767   (` (if (gnus-buffer-exists-p (, buffer))
1768          (kill-buffer (, buffer)))))
1769
1770 (defsubst gnus-point-at-bol ()
1771   "Return point at the beginning of line."
1772   (let ((p (point)))
1773     (beginning-of-line)
1774     (prog1
1775         (point)
1776       (goto-char p))))
1777
1778 (defsubst gnus-point-at-eol ()
1779   "Return point at the beginning of line."
1780   (let ((p (point)))
1781     (end-of-line)
1782     (prog1
1783         (point)
1784       (goto-char p))))
1785
1786 ;; Delete the current line (and the next N lines.);
1787 (defmacro gnus-delete-line (&optional n)
1788   (` (delete-region (progn (beginning-of-line) (point))
1789                     (progn (forward-line (, (or n 1))) (point)))))
1790
1791 ;;; Load the compatability functions. 
1792
1793 (require 'gnus-ems)
1794
1795 \f
1796 ;;;
1797 ;;; Gnus Utility Functions
1798 ;;;
1799
1800 (defun gnus-extract-address-components (from)
1801   (let (name address)
1802     ;; First find the address - the thing with the @ in it.  This may
1803     ;; not be accurate in mail addresses, but does the trick most of
1804     ;; the time in news messages.
1805     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1806         (setq address (substring from (match-beginning 0) (match-end 0))))
1807     ;; Then we check whether the "name <address>" format is used.
1808     (and address
1809          (string-match (concat "<" (regexp-quote address) ">") from)
1810          (and (setq name (substring from 0 (1- (match-beginning 0))))
1811               ;; Strip any quotes from the name.
1812               (string-match "\".*\"" name)
1813               (setq name (substring name 1 (1- (match-end 0))))))
1814     ;; If not, then "address (name)" is used.
1815     (or name
1816         (and (string-match "(.+)" from)
1817              (setq name (substring from (1+ (match-beginning 0)) 
1818                                    (1- (match-end 0)))))
1819         (and (string-match "()" from)
1820              (setq name address))
1821         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1822         ;; XOVER might not support folded From headers.
1823         (and (string-match "(.*" from)
1824              (setq name (substring from (1+ (match-beginning 0)) 
1825                                    (match-end 0)))))
1826     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1827     (list (or name from) (or address from))))
1828
1829 (defun gnus-fetch-field (field)
1830   "Return the value of the header FIELD of current article."
1831   (save-excursion
1832     (save-restriction
1833       (let ((case-fold-search t))
1834         (gnus-narrow-to-headers)
1835         (mail-fetch-field field)))))
1836
1837 (defun gnus-goto-colon ()
1838   (beginning-of-line)
1839   (search-forward ":" (gnus-point-at-eol) t))
1840
1841 (defun gnus-narrow-to-headers ()
1842   (widen)
1843   (save-excursion
1844     (narrow-to-region
1845      (goto-char (point-min))
1846      (if (search-forward "\n\n" nil t)
1847          (1- (point))
1848        (point-max)))))
1849
1850 (defvar gnus-old-specs nil)
1851
1852 (defun gnus-update-format-specifications ()
1853   (gnus-make-thread-indent-array)
1854
1855   (let ((formats '(summary summary-dummy group 
1856                            summary-mode group-mode article-mode))
1857         old-format new-format)
1858     (while formats
1859       (setq new-format (symbol-value
1860                         (intern (format "gnus-%s-line-format" (car formats)))))
1861       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1862                (equal old-format new-format))
1863           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1864                (gnus-parse-format
1865                 new-format
1866                 (symbol-value 
1867                  (intern (format "gnus-%s-line-format-alist"
1868                                  (if (eq (car formats) 'article-mode)
1869                                      'summary-mode (car formats))))))))
1870       (setq gnus-old-specs (cons (cons (car formats) new-format)
1871                                  (delq (car formats) gnus-old-specs)))
1872       (setq formats (cdr formats))))
1873       
1874   (gnus-update-group-mark-positions)
1875   (gnus-update-summary-mark-positions)
1876
1877   (if (and (string-match "%D" gnus-group-line-format)
1878            (not gnus-description-hashtb)
1879            gnus-read-active-file)
1880       (gnus-read-all-descriptions-files)))
1881
1882 (defun gnus-update-summary-mark-positions ()
1883   (save-excursion
1884     (let ((gnus-replied-mark 129)
1885           (gnus-score-below-mark 130)
1886           (gnus-score-over-mark 130)
1887           (thread nil)
1888           pos)
1889       (gnus-set-work-buffer)
1890       (gnus-summary-insert-line 
1891        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1892       (goto-char (point-min))
1893       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1894                                          (- (point) 2)))))
1895       (goto-char (point-min))
1896       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1897                                           (- (point) 2))) pos))
1898       (goto-char (point-min))
1899       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1900                                         (- (point) 2))) pos))
1901       (setq gnus-summary-mark-positions pos))))
1902
1903 (defun gnus-update-group-mark-positions ()
1904   (save-excursion
1905     (let ((gnus-process-mark 128)
1906           (gnus-group-marked '("dummy.group")))
1907       (gnus-set-work-buffer)
1908       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1909       (goto-char (point-min))
1910       (setq gnus-group-mark-positions
1911             (list (cons 'process (and (search-forward "\200" nil t)
1912                                       (- (point) 2))))))))
1913
1914 (defun gnus-mouse-face-function (form)
1915   (` (let ((string (, form)))
1916        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1917        string)))
1918
1919 (defun gnus-max-width-function (el max-width)
1920   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1921   (` (let* ((val (eval (, el)))
1922             (valstr (if (numberp val)
1923                         (int-to-string val) val)))
1924        (if (> (length valstr) (, max-width))
1925            (substring valstr 0 (, max-width))
1926          valstr))))
1927
1928 (defun gnus-parse-format (format spec-alist)
1929   ;; This function parses the FORMAT string with the help of the
1930   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1931   ;; string.  If the FORMAT string contains the specifiers %( and %)
1932   ;; the text between them will have the mouse-face text property.
1933   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1934       (if (and gnus-visual gnus-mouse-face)
1935           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1936                 (button (substring format (match-beginning 2) (match-end 2)))
1937                 (post (substring format (match-beginning 3) (match-end 3))))
1938             (list 'concat
1939                   (gnus-parse-simple-format pre spec-alist)
1940                   (gnus-mouse-face-function 
1941                    (gnus-parse-simple-format button spec-alist))
1942                   (gnus-parse-simple-format post spec-alist)))
1943         (gnus-parse-simple-format
1944          (concat (substring format (match-beginning 1) (match-end 1))
1945                  (substring format (match-beginning 2) (match-end 2))
1946                  (substring format (match-beginning 3) (match-end 3)))
1947          spec-alist))
1948     (gnus-parse-simple-format format spec-alist)))
1949
1950 (defun gnus-parse-simple-format (format spec-alist)
1951   ;; This function parses the FORMAT string with the help of the
1952   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1953   ;; string. The list will consist of the symbol `format', a format
1954   ;; specification string, and a list of forms depending on the
1955   ;; SPEC-ALIST.
1956   (let ((max-width 0)
1957         spec flist fstring newspec elem beg)
1958     (save-excursion
1959       (gnus-set-work-buffer)
1960       (insert format)
1961       (goto-char (point-min))
1962       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1963         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1964                                                      (match-end 2))))
1965         ;; First check if there are any specs that look anything like
1966         ;; "%12,12A", ie. with a "max width specification". These have
1967         ;; to be treated specially.
1968         (if (setq beg (match-beginning 1))
1969             (setq max-width 
1970                   (string-to-int 
1971                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1972           (setq max-width 0)
1973           (setq beg (match-beginning 2)))
1974         ;; Find the specification from `spec-alist'.
1975         (if (not (setq elem (cdr (assq spec spec-alist))))
1976             (setq elem '("*" ?s)))
1977         ;; Treat user defined format specifiers specially
1978         (and (eq (car elem) 'user-defined)
1979              (setq elem
1980                    (list 
1981                     (list (intern (concat "gnus-user-format-function-"
1982                                           (buffer-substring
1983                                            (match-beginning 3)
1984                                            (match-end 3))))
1985                           'header)
1986                     ?s))
1987              (delete-region (match-beginning 3) (match-end 3)))
1988         (if (not (zerop max-width))
1989             (let ((el (car elem)))
1990               (cond ((= (car (cdr elem)) ?c) 
1991                      (setq el (list 'char-to-string el)))
1992                     ((= (car (cdr elem)) ?d)
1993                      (numberp el) (setq el (list 'int-to-string el))))
1994               (setq flist (cons (gnus-max-width-function el max-width)
1995                                 flist))
1996               (setq newspec ?s))
1997           (setq flist (cons (car elem) flist))
1998           (setq newspec (car (cdr elem))))
1999         ;; Remove the old specification (and possibly a ",12" string).
2000         (delete-region beg (match-end 2))
2001         ;; Insert the new specification.
2002         (goto-char beg)
2003         (insert newspec))
2004       (setq fstring (buffer-substring 1 (point-max))))
2005     (cons 'format (cons fstring (nreverse flist)))))
2006
2007 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2008 (defun gnus-read-init-file ()
2009   (and gnus-init-file
2010        (or (and (file-exists-p gnus-init-file) 
2011                 ;; Don't try to load a directory.
2012                 (not (file-directory-p gnus-init-file)))
2013            (file-exists-p (concat gnus-init-file ".el"))
2014            (file-exists-p (concat gnus-init-file ".elc")))
2015        (load gnus-init-file nil t)))
2016
2017 (defun gnus-set-work-buffer ()
2018   (if (get-buffer gnus-work-buffer)
2019       (progn
2020         (set-buffer gnus-work-buffer)
2021         (erase-buffer))
2022     (set-buffer (get-buffer-create gnus-work-buffer))
2023     (kill-all-local-variables)
2024     (buffer-disable-undo (current-buffer))
2025     (gnus-add-current-to-buffer-list)))
2026
2027 ;; Article file names when saving.
2028
2029 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2030   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2031 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2032 Otherwise, it is like ~/News/news/group/num."
2033   (let ((default
2034           (expand-file-name
2035            (concat (if (gnus-use-long-file-name 'not-save)
2036                        (gnus-capitalize-newsgroup newsgroup)
2037                      (gnus-newsgroup-directory-form newsgroup))
2038                    "/" (int-to-string (header-number headers)))
2039            (or gnus-article-save-directory "~/News"))))
2040     (if (and last-file
2041              (string-equal (file-name-directory default)
2042                            (file-name-directory last-file))
2043              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2044         default
2045       (or last-file default))))
2046
2047 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2048   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2049 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2050 Otherwise, it is like ~/News/news/group/num."
2051   (let ((default
2052           (expand-file-name
2053            (concat (if (gnus-use-long-file-name 'not-save)
2054                        newsgroup
2055                      (gnus-newsgroup-directory-form newsgroup))
2056                    "/" (int-to-string (header-number headers)))
2057            (or gnus-article-save-directory "~/News"))))
2058     (if (and last-file
2059              (string-equal (file-name-directory default)
2060                            (file-name-directory last-file))
2061              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2062         default
2063       (or last-file default))))
2064
2065 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2066   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2067 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2068 Otherwise, it is like ~/News/news/group/news."
2069   (or last-file
2070       (expand-file-name
2071        (if (gnus-use-long-file-name 'not-save)
2072            (gnus-capitalize-newsgroup newsgroup)
2073          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2074        (or gnus-article-save-directory "~/News"))))
2075
2076 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2077   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2078 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2079 Otherwise, it is like ~/News/news/group/news."
2080   (or last-file
2081       (expand-file-name
2082        (if (gnus-use-long-file-name 'not-save)
2083            newsgroup
2084          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2085        (or gnus-article-save-directory "~/News"))))
2086
2087 ;; For subscribing new newsgroup
2088
2089 (defun gnus-subscribe-hierarchical-interactive (groups)
2090   (let ((groups (sort groups 'string<))
2091         prefixes prefix start ans group starts)
2092     (while groups
2093       (setq prefixes (list "^"))
2094       (while (and groups prefixes)
2095         (while (not (string-match (car prefixes) (car groups)))
2096           (setq prefixes (cdr prefixes)))
2097         (setq prefix (car prefixes))
2098         (setq start (1- (length prefix)))
2099         (if (and (string-match "[^\\.]\\." (car groups) start)
2100                  (cdr groups)
2101                  (setq prefix 
2102                        (concat "^" (substring (car groups) 0 (match-end 0))))
2103                  (string-match prefix (car (cdr groups))))
2104             (progn
2105               (setq prefixes (cons prefix prefixes))
2106               (message "Descend hierarchy %s? ([y]nsq): " 
2107                        (substring prefix 1 (1- (length prefix))))
2108               (setq ans (read-char))
2109               (cond ((= ans ?n)
2110                      (while (and groups 
2111                                  (string-match prefix 
2112                                                (setq group (car groups))))
2113                        (setq gnus-killed-list 
2114                              (cons group gnus-killed-list))
2115                        (gnus-sethash group group gnus-killed-hashtb)
2116                        (setq groups (cdr groups)))
2117                      (setq starts (cdr starts)))
2118                     ((= ans ?s)
2119                      (while (and groups 
2120                                  (string-match prefix 
2121                                                (setq group (car groups))))
2122                        (gnus-sethash group group gnus-killed-hashtb)
2123                        (gnus-subscribe-alphabetically (car groups))
2124                        (setq groups (cdr groups)))
2125                      (setq starts (cdr starts)))
2126                     ((= ans ?q)
2127                      (while groups
2128                        (setq group (car groups))
2129                        (setq gnus-killed-list (cons group gnus-killed-list))
2130                        (gnus-sethash group group gnus-killed-hashtb)
2131                        (setq groups (cdr groups))))
2132                     (t nil)))
2133           (message "Subscribe %s? ([n]yq)" (car groups))
2134           (setq ans (read-char))
2135           (setq group (car groups))
2136           (cond ((= ans ?y)
2137                  (gnus-subscribe-alphabetically (car groups))
2138                  (gnus-sethash group group gnus-killed-hashtb))
2139                 ((= ans ?q)
2140                  (while groups
2141                    (setq group (car groups))
2142                    (setq gnus-killed-list (cons group gnus-killed-list))
2143                    (gnus-sethash group group gnus-killed-hashtb)
2144                    (setq groups (cdr groups))))
2145                 (t 
2146                  (setq gnus-killed-list (cons group gnus-killed-list))
2147                  (gnus-sethash group group gnus-killed-hashtb)))
2148           (setq groups (cdr groups)))))))
2149
2150 (defun gnus-subscribe-randomly (newsgroup)
2151   "Subscribe new NEWSGROUP by making it the first newsgroup."
2152   (gnus-subscribe-newsgroup newsgroup))
2153
2154 (defun gnus-subscribe-alphabetically (newgroup)
2155   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2156   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2157   (let ((groups (cdr gnus-newsrc-alist))
2158         before)
2159     (while (and (not before) groups)
2160       (if (string< newgroup (car (car groups)))
2161           (setq before (car (car groups)))
2162         (setq groups (cdr groups))))
2163     (gnus-subscribe-newsgroup newgroup before)))
2164
2165 (defun gnus-subscribe-hierarchically (newgroup)
2166   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2167   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2168   (save-excursion
2169     (set-buffer (find-file-noselect gnus-current-startup-file))
2170     (let ((groupkey newgroup)
2171           before)
2172       (while (and (not before) groupkey)
2173         (goto-char (point-min))
2174         (let ((groupkey-re
2175                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2176           (while (and (re-search-forward groupkey-re nil t)
2177                       (progn
2178                         (setq before (buffer-substring
2179                                       (match-beginning 1) (match-end 1)))
2180                         (string< before newgroup)))))
2181         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2182         (setq groupkey
2183               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2184                   (substring groupkey (match-beginning 1) (match-end 1)))))
2185       (gnus-subscribe-newsgroup newgroup before))))
2186
2187 (defun gnus-subscribe-interactively (newsgroup)
2188   "Subscribe new NEWSGROUP interactively.
2189 It is inserted in hierarchical newsgroup order if subscribed. If not,
2190 it is killed."
2191   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2192       (gnus-subscribe-hierarchically newsgroup)
2193     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2194
2195 (defun gnus-subscribe-zombies (newsgroup)
2196   "Make new NEWSGROUP a zombie group."
2197   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2198
2199 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2200   "Subscribe new NEWSGROUP.
2201 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2202 the first newsgroup."
2203   ;; We subscribe the group by changing its level to `subscribed'.
2204   (gnus-group-change-level 
2205    newsgroup gnus-level-default-subscribed
2206    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2207   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2208
2209 ;; For directories
2210
2211 (defun gnus-newsgroup-directory-form (newsgroup)
2212   "Make hierarchical directory name from NEWSGROUP name."
2213   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2214         (len (length newsgroup))
2215         idx)
2216     ;; If this is a foreign group, we don't want to translate the
2217     ;; entire name.  
2218     (if (setq idx (string-match ":" newsgroup))
2219         (aset newsgroup idx ?/)
2220       (setq idx 0))
2221     ;; Replace all occurrences of `.' with `/'.
2222     (while (< idx len)
2223       (if (= (aref newsgroup idx) ?.)
2224           (aset newsgroup idx ?/))
2225       (setq idx (1+ idx)))
2226     newsgroup))
2227
2228 (defun gnus-newsgroup-saveable-name (group)
2229   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2230   ;; with dots.
2231   (gnus-replace-chars-in-string group ?/ ?.))
2232
2233 (defun gnus-make-directory (dir)
2234   "Make DIRECTORY recursively."
2235   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2236   ;; of the many mysteries of the universe.
2237  (let* ((dir (expand-file-name dir default-directory))
2238          dirs err)
2239     (if (string-match "/$" dir)
2240         (setq dir (substring dir 0 (match-beginning 0))))
2241     ;; First go down the path until we find a directory that exists.
2242     (while (not (file-exists-p dir))
2243       (setq dirs (cons dir dirs))
2244       (string-match "/[^/]+$" dir)
2245       (setq dir (substring dir 0 (match-beginning 0))))
2246     ;; Then create all the subdirs.
2247     (while (and dirs (not err))
2248       (condition-case ()
2249           (make-directory (car dirs))
2250         (error (setq err t)))
2251       (setq dirs (cdr dirs)))
2252     ;; We return whether we were successful or not. 
2253     (not dirs)))
2254
2255 (defun gnus-capitalize-newsgroup (newsgroup)
2256   "Capitalize NEWSGROUP name."
2257   (and (not (zerop (length newsgroup)))
2258        (concat (char-to-string (upcase (aref newsgroup 0)))
2259                (substring newsgroup 1))))
2260
2261 ;; Var
2262
2263 (defun gnus-simplify-subject (subject &optional re-only)
2264   "Remove `Re:' and words in parentheses.
2265 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2266   (let ((case-fold-search t))           ;Ignore case.
2267     ;; Remove `Re:' and `Re^N:'.
2268     (if (string-match "^re:[ \t]*" subject)
2269         (setq subject (substring subject (match-end 0))))
2270     ;; Remove words in parentheses from end.
2271     (or re-only
2272         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2273           (setq subject (substring subject 0 (match-beginning 0)))))
2274     ;; Return subject string.
2275     subject))
2276
2277 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2278 ;; all whitespace.
2279 (defun gnus-simplify-subject-fuzzy (subject)
2280   (let ((case-fold-search t))
2281     (save-excursion
2282       (gnus-set-work-buffer)
2283       (insert subject)
2284       (inline (gnus-simplify-buffer-fuzzy))
2285       (buffer-string))))
2286
2287 (defun gnus-simplify-buffer-fuzzy ()
2288   (goto-char (point-min))
2289   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2290   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2291                             nil t)
2292     (replace-match "" t t))
2293   (goto-char (point-min))
2294   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2295     (replace-match "" t t))
2296   (goto-char (point-min))
2297   (while (re-search-forward "[ \t]+" nil t)
2298     (replace-match " " t t))
2299   (goto-char (point-min))
2300   (while (re-search-forward "[ \t]+$" nil t)
2301     (replace-match "" t t))
2302   (goto-char (point-min))
2303   (while (re-search-forward "^[ \t]+" nil t)
2304     (replace-match "" t t))
2305   (if gnus-simplify-subject-fuzzy-regexp
2306       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2307         (replace-match "" t t))))
2308
2309 ;; Add the current buffer to the list of buffers to be killed on exit. 
2310 (defun gnus-add-current-to-buffer-list ()
2311   (or (memq (current-buffer) gnus-buffer-list)
2312       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2313
2314 (defun gnus-string> (s1 s2)
2315   (not (or (string< s1 s2)
2316            (string= s1 s2))))
2317
2318 ;; Functions accessing headers.
2319 ;; Functions are more convenient than macros in some cases.
2320
2321 (defun gnus-header-number (header)
2322   (header-number header))
2323
2324 (defun gnus-header-subject (header)
2325   (header-subject header))
2326
2327 (defun gnus-header-from (header)
2328   (header-from header))
2329
2330 (defun gnus-header-xref (header)
2331   (header-xref header))
2332
2333 (defun gnus-header-lines (header)
2334   (header-lines header))
2335
2336 (defun gnus-header-date (header)
2337   (header-date header))
2338
2339 (defun gnus-header-id (header)
2340   (header-id header))
2341
2342 (defun gnus-header-references (header)
2343   (header-references header))
2344
2345 ;;; General various misc type functions.
2346
2347 (defun gnus-clear-system ()
2348   "Clear all variables and buffers."
2349   ;; Clear Gnus variables.
2350   (let ((variables gnus-variable-list))
2351     (while variables
2352       (set (car variables) nil)
2353       (setq variables (cdr variables))))
2354   ;; Clear other internal variables.
2355   (setq gnus-list-of-killed-groups nil
2356         gnus-have-read-active-file nil
2357         gnus-newsrc-alist nil
2358         gnus-newsrc-hashtb nil
2359         gnus-killed-list nil
2360         gnus-zombie-list nil
2361         gnus-killed-hashtb nil
2362         gnus-active-hashtb nil
2363         gnus-moderated-list nil
2364         gnus-description-hashtb nil
2365         gnus-newsgroup-headers nil
2366         gnus-newsgroup-headers-hashtb-by-number nil
2367         gnus-newsgroup-name nil
2368         gnus-server-alist nil
2369         gnus-current-select-method nil)
2370   ;; Reset any score variables.
2371   (and (boundp 'gnus-score-cache)
2372        (set 'gnus-score-cache nil))
2373   (and (boundp 'gnus-internal-global-score-files)
2374        (set 'gnus-internal-global-score-files nil))
2375   ;; Kill the startup file.
2376   (and gnus-current-startup-file
2377        (get-file-buffer gnus-current-startup-file)
2378        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2379   ;; Save any cache buffers.
2380   (and gnus-use-cache (gnus-cache-save-buffers))
2381   ;; Clear the dribble buffer.
2382   (gnus-dribble-clear)
2383   ;; Kill global KILL file buffer.
2384   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2385       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2386   (gnus-kill-buffer nntp-server-buffer)
2387   ;; Kill Gnus buffers.
2388   (while gnus-buffer-list
2389     (gnus-kill-buffer (car gnus-buffer-list))
2390     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2391
2392 (defun gnus-windows-old-to-new (setting)
2393   (if (symbolp setting)
2394       (setq setting 
2395             (cond ((eq setting 'SelectArticle)
2396                    'article)
2397                   ((eq setting 'SelectSubject)
2398                    'summary)
2399                   ((eq setting 'SelectNewsgroup)
2400                    'group)
2401                   (t setting))))
2402   (if (or (listp setting)
2403           (not (and gnus-window-configuration
2404                     (memq setting '(group summary article)))))
2405       setting
2406     (let* ((setting (if (eq setting 'group) 
2407                         (if (assq 'newsgroup gnus-window-configuration)
2408                             'newsgroup
2409                           'newsgroups) setting))
2410            (elem (car (cdr (assq setting gnus-window-configuration))))
2411            (total (apply '+ elem))
2412            (types '(group summary article))
2413            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2414            (i 0)
2415            perc
2416            out)
2417       (while (< i 3)
2418         (or (zerop (nth i elem))
2419             (progn
2420               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2421               (setq out (cons (if (eq pbuf (nth i types))
2422                                   (vector (nth i types) perc 'point)
2423                                 (vector (nth i types) perc))
2424                               out))))
2425         (setq i (1+ i)))
2426       (list (nreverse out)))))
2427            
2428 (defun gnus-add-configuration (conf)
2429   (setq gnus-buffer-configuration 
2430         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2431                          gnus-buffer-configuration))))
2432
2433 (defun gnus-configure-windows (setting &optional force)
2434   (setq setting (gnus-windows-old-to-new setting))
2435   (let ((r (if (symbolp setting)
2436                (cdr (assq setting gnus-buffer-configuration))
2437              setting))
2438         (in-buf (current-buffer))
2439         rule val w height hor ohor heights sub jump-buffer
2440         rel total to-buf all-visible)
2441     (or r (error "No such setting: %s" setting))
2442
2443     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2444         ;; All the windows mentioned are already visibe, so we just
2445         ;; put point in the assigned buffer, and do not touch the
2446         ;; winconf. 
2447         (select-window (get-buffer-window all-visible))
2448
2449       ;; Either remove all windows or just remove all Gnus windows.
2450       (if gnus-use-full-window
2451           (delete-other-windows)
2452         (gnus-remove-some-windows)
2453         (switch-to-buffer nntp-server-buffer))
2454
2455       (while r
2456         (setq hor (car r)
2457               ohor nil)
2458
2459         ;; We have to do the (possible) horizontal splitting before the
2460         ;; vertical. 
2461         (if (and (listp (car hor)) 
2462                  (eq (car (car hor)) 'horizontal))
2463             (progn
2464               (split-window 
2465                nil
2466                (if (integerp (nth 1 (car hor)))
2467                    (nth 1 (car hor))
2468                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2469                t)
2470               (setq hor (cdr hor))))
2471
2472         ;; Go through the rules and eval the elements that are to be
2473         ;; evaled.  
2474         (while hor
2475           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2476               (progn
2477                 ;; Expand short buffer name.
2478                 (setq w (aref val 0))
2479                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2480                      (progn
2481                        (setq val (apply 'vector (mapcar 'identity val)))
2482                        (aset val 0 w)))
2483                 (setq ohor (cons val ohor))))
2484           (setq hor (cdr hor)))
2485         (setq rule (cons (nreverse ohor) rule))
2486         (setq r (cdr r)))
2487       (setq rule (nreverse rule))
2488
2489       ;; We tally the window sizes.
2490       (setq total (window-height))
2491       (while rule
2492         (setq hor (car rule))
2493         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2494             (setq hor (cdr hor)))
2495         (setq sub 0)
2496         (while hor
2497           (setq rel (aref (car hor) 1)
2498                 heights (cons
2499                          (cond ((and (floatp rel) (= 1.0 rel))
2500                                 'x)
2501                                ((integerp rel)
2502                                 rel)
2503                                (t
2504                                 (max (floor (* total rel)) 4)))
2505                          heights)
2506                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2507                 hor (cdr hor)))
2508         (setq heights (nreverse heights)
2509               hor (car rule))
2510
2511         ;; We then go through these heighs and create windows for them.
2512         (while heights
2513           (setq height (car heights)
2514                 heights (cdr heights))
2515           (and (eq height 'x)
2516                (setq height (- total sub)))
2517           (and heights
2518                (split-window nil height))
2519           (setq to-buf (aref (car hor) 0))
2520           (switch-to-buffer 
2521            (cond ((not to-buf)
2522                   in-buf)
2523                  ((symbolp to-buf)
2524                   (symbol-value (aref (car hor) 0)))
2525                  (t
2526                   (aref (car hor) 0))))
2527           (and (> (length (car hor)) 2)
2528                (eq (aref (car hor) 2) 'point)
2529                (setq jump-buffer (current-buffer)))
2530           (other-window 1)
2531           (setq hor (cdr hor)))
2532       
2533         (setq rule (cdr rule)))
2534
2535       ;; Finally, we pop to the buffer that's supposed to have point. 
2536       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2537
2538       (select-window (get-buffer-window jump-buffer))
2539       (set-buffer jump-buffer))))
2540
2541 (defun gnus-all-windows-visible-p (rule)
2542   (let (invisible hor jump-buffer val buffer)
2543     ;; Go through the rules and eval the elements that are to be
2544     ;; evaled.  
2545     (while (and rule (not invisible))
2546       (setq hor (car rule)
2547             rule (cdr rule))
2548       (while (and hor (not invisible))
2549         (if (setq val (if (vectorp (car hor)) 
2550                           (car hor)
2551                         (if (not (eq (car (car hor)) 'horizontal))
2552                             (eval (car hor)))))
2553             (progn
2554               ;; Expand short buffer name.
2555               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2556                                (aref val 0)))
2557               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2558                              buffer))
2559               (and (> (length val) 2) (eq 'point (aref val 2))
2560                    (setq jump-buffer buffer))
2561               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2562         (setq hor (cdr hor))))
2563     (and (not invisible) jump-buffer)))
2564
2565 (defun gnus-window-left-corner (&optional window)
2566   (nth 1 (window-edges window)))
2567
2568 (defun gnus-remove-some-windows ()
2569   (let ((buffers gnus-window-to-buffer)
2570         (lowest (frame-height))
2571         buf bufs lowest-buf)
2572     (save-excursion
2573       ;; Remove windows on all known Gnus buffers.
2574       (while buffers
2575         (setq buf (cdr (car buffers)))
2576         (if (symbolp buf)
2577             (setq buf (and (boundp buf) (symbol-value buf))))
2578         (and buf 
2579              (get-buffer-window buf)
2580              (progn
2581                (setq bufs (cons buf bufs))
2582                (pop-to-buffer buf)
2583                (if (< (gnus-window-left-corner) lowest)
2584                    (progn
2585                      (setq lowest (gnus-window-left-corner))
2586                      (setq lowest-buf buf)))))
2587         (setq buffers (cdr buffers)))
2588       ;; Remove windows on *all* summary buffers.
2589       (let (wins)
2590         (walk-windows
2591          (lambda (win)
2592            (let ((buf (window-buffer win)))
2593              (if (string-match  "^\\*Summary" (buffer-name buf))
2594              (progn
2595                (setq bufs (cons buf bufs))
2596                (pop-to-buffer buf)
2597                (if (< (gnus-window-left-corner) lowest)
2598                    (progn
2599                      (setq lowest-buf buf)
2600                      (setq lowest (gnus-window-left-corner))))))))))
2601       (and lowest-buf 
2602            (progn
2603              (pop-to-buffer lowest-buf)
2604              (switch-to-buffer nntp-server-buffer)))
2605       (while bufs
2606         (and (not (eq (car bufs) lowest-buf))
2607              (delete-windows-on (car bufs)))
2608         (setq bufs (cdr bufs))))))
2609                           
2610 (defun gnus-version ()
2611   "Version numbers of this version of Gnus."
2612   (interactive)
2613   (let ((methods gnus-valid-select-methods)
2614         (mess gnus-version)
2615         meth)
2616     ;; Go through all the legal select methods and add their version
2617     ;; numbers to the total version string. Only the backends that are
2618     ;; currently in use will have their message numbers taken into
2619     ;; consideration. 
2620     (while methods
2621       (setq meth (intern (concat (car (car methods)) "-version")))
2622       (and (boundp meth)
2623            (stringp (symbol-value meth))
2624            (setq mess (concat mess "; " (symbol-value meth))))
2625       (setq methods (cdr methods)))
2626     (gnus-message 2 mess)))
2627
2628 (defun gnus-info-find-node ()
2629   "Find Info documentation of Gnus."
2630   (interactive)
2631   ;; Enlarge info window if needed.
2632   (let ((mode major-mode))
2633     (gnus-configure-windows 'info)
2634     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2635
2636 (defun gnus-overload-functions (&optional overloads)
2637   "Overload functions specified by optional argument OVERLOADS.
2638 If nothing is specified, use the variable gnus-overload-functions."
2639   (let ((defs nil)
2640         (overloads (or overloads gnus-overload-functions)))
2641     (while overloads
2642       (setq defs (car overloads))
2643       (setq overloads (cdr overloads))
2644       ;; Load file before overloading function if necessary.  Make
2645       ;; sure we cannot use `require' always.
2646       (and (not (fboundp (car defs)))
2647            (car (cdr (cdr defs)))
2648            (load (car (cdr (cdr defs))) nil 'nomessage))
2649       (fset (car defs) (car (cdr defs))))))
2650
2651 (defun gnus-replace-chars-in-string (string from to)
2652   "Replace characters in STRING from FROM to TO."
2653   (let ((string (substring string 0))   ;Copy string.
2654         (len (length string))
2655         (idx 0))
2656     ;; Replace all occurrences of FROM with TO.
2657     (while (< idx len)
2658       (if (= (aref string idx) from)
2659           (aset string idx to))
2660       (setq idx (1+ idx)))
2661     string))
2662
2663 (defun gnus-days-between (date1 date2)
2664   ;; Return the number of days between date1 and date2.
2665   (- (gnus-day-number date1) (gnus-day-number date2)))
2666
2667 (defun gnus-day-number (date)
2668   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2669                      (timezone-parse-date date))))
2670     (timezone-absolute-from-gregorian 
2671      (nth 1 dat) (nth 2 dat) (car dat))))
2672
2673 ;; Returns a floating point number that says how many seconds have
2674 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2675 (defun gnus-seconds-since-epoch (date)
2676   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2677                         (timezone-parse-date date)))
2678          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2679                         (timezone-parse-time
2680                          (aref (timezone-parse-date date) 3))))
2681          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2682                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2683          (tday (- (timezone-absolute-from-gregorian 
2684                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2685                   (timezone-absolute-from-gregorian 
2686                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2687     (+ (nth 2 ttime)
2688        (* (nth 1 ttime) 60)
2689        (* 1.0 (nth 0 ttime) 60 60)
2690        (* 1.0 tday 60 60 24))))
2691
2692 (defun gnus-file-newer-than (file date)
2693   (let ((fdate (nth 5 (file-attributes file))))
2694     (or (> (car fdate) (car date))
2695         (and (= (car fdate) (car date))
2696              (> (nth 1 fdate) (nth 1 date))))))
2697
2698 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2699 ;; the echo area.
2700 (defun gnus-y-or-n-p (prompt)
2701   (prog1
2702       (y-or-n-p prompt)
2703     (message "")))
2704
2705 (defun gnus-yes-or-no-p (prompt)
2706   (prog1
2707       (yes-or-no-p prompt)
2708     (message "")))
2709
2710 ;; Check whether to use long file names.
2711 (defun gnus-use-long-file-name (symbol)
2712   ;; The variable has to be set...
2713   (and gnus-use-long-file-name
2714        ;; If it isn't a list, then we return t.
2715        (or (not (listp gnus-use-long-file-name))
2716            ;; If it is a list, and the list contains `symbol', we
2717            ;; return nil.  
2718            (not (memq symbol gnus-use-long-file-name)))))
2719
2720 ;; I suspect there's a better way, but I haven't taken the time to do
2721 ;; it yet. -erik selberg@cs.washington.edu
2722 (defun gnus-dd-mmm (messy-date)
2723   "Return a string like DD-MMM from a big messy string"
2724   (let ((datevec (timezone-parse-date messy-date)))
2725     (format "%2s-%s"
2726             (or (aref datevec 2) "??")
2727             (capitalize
2728              (or (car 
2729                   (nth (1- (string-to-number (aref datevec 1)))
2730                        timezone-months-assoc))
2731                  "???")))))
2732
2733 ;; Make a hash table (default and minimum size is 255).
2734 ;; Optional argument HASHSIZE specifies the table size.
2735 (defun gnus-make-hashtable (&optional hashsize)
2736   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2737
2738 ;; Make a number that is suitable for hashing; bigger than MIN and one
2739 ;; less than 2^x.
2740 (defun gnus-create-hash-size (min)
2741   (let ((i 1))
2742     (while (< i min)
2743       (setq i (* 2 i)))
2744     (1- i)))
2745
2746 ;; Show message if message has a lower level than `gnus-verbose'. 
2747 ;; Guide-line for numbers:
2748 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2749 ;; for things that take a long time, 7 - not very important messages
2750 ;; on stuff, 9 - messages inside loops.
2751 (defun gnus-message (level &rest args)
2752   (if (<= level gnus-verbose)
2753       (apply 'message args)
2754     ;; We have to do this format thingie here even if the result isn't
2755     ;; shown - the return value has to be the same as the return value
2756     ;; from `message'.
2757     (apply 'format args)))
2758
2759 ;; Generate a unique new group name.
2760 (defun gnus-generate-new-group-name (leaf)
2761   (let ((name leaf)
2762         (num 0))
2763     (while (gnus-gethash name gnus-newsrc-hashtb)
2764       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2765     name))
2766
2767 (defun gnus-find-file-noselect (file &optional force)
2768   "Does vaguely the same as find-file-noselect. No hooks are run."
2769   (let (buf insert)
2770     (if (setq buf (get-file-buffer file))
2771         (setq insert force)
2772       (setq buf (create-file-buffer file))
2773       (setq insert t))
2774     (if (not insert)
2775         buf
2776       (save-excursion
2777         (set-buffer buf)
2778         (erase-buffer)
2779         (and (file-readable-p file)
2780              (insert-file-contents file))
2781         (set-visited-file-name file)
2782         (set-buffer-modified-p nil)
2783         (current-buffer)))))
2784
2785 ;;; List and range functions
2786
2787 (defun gnus-last-element (list)
2788   "Return last element of LIST."
2789   (while (cdr list)
2790     (setq list (cdr list)))
2791   (car list))
2792
2793 (defun gnus-copy-sequence (list)
2794   "Do a complete, total copy of a list."
2795   (if (and (consp list) (not (consp (cdr list))))
2796       (cons (car list) (cdr list))
2797     (mapcar (lambda (elem) (if (consp elem) 
2798                                (if (consp (cdr elem))
2799                                    (gnus-copy-sequence elem)
2800                                  (cons (car elem) (cdr elem)))
2801                              elem))
2802             list)))
2803
2804 (defun gnus-set-difference (list1 list2)
2805   "Return a list of elements of LIST1 that do not appear in LIST2."
2806   (let ((list1 (copy-sequence list1)))
2807     (while list2
2808       (setq list1 (delq (car list2) list1))
2809       (setq list2 (cdr list2)))
2810     list1))
2811
2812 (defun gnus-sorted-complement (list1 list2)
2813   "Return a list of elements of LIST1 that do not appear in LIST2.
2814 Both lists have to be sorted over <."
2815   (let (out)
2816     (if (or (null list1) (null list2))
2817         (or list1 list2)
2818       (while (and list1 list2)
2819         (cond ((= (car list1) (car list2))
2820                (setq list1 (cdr list1)
2821                      list2 (cdr list2)))
2822               ((< (car list1) (car list2))
2823                (setq out (cons (car list1) out))
2824                (setq list1 (cdr list1)))
2825               (t
2826                (setq out (cons (car list2) out))
2827                (setq list2 (cdr list2)))))
2828       (nconc (nreverse out) (or list1 list2)))))
2829
2830 (defun gnus-intersection (list1 list2)      
2831   (let ((result nil))
2832     (while list2
2833       (if (memq (car list2) list1)
2834           (setq result (cons (car list2) result)))
2835       (setq list2 (cdr list2)))
2836     result))
2837
2838 (defun gnus-sorted-intersection (list1 list2)
2839   ;; LIST1 and LIST2 have to be sorted over <.
2840   (let (out)
2841     (while (and list1 list2)
2842       (cond ((= (car list1) (car list2))
2843              (setq out (cons (car list1) out)
2844                    list1 (cdr list1)
2845                    list2 (cdr list2)))
2846             ((< (car list1) (car list2))
2847              (setq list1 (cdr list1)))
2848             (t
2849              (setq list2 (cdr list2)))))
2850     (nreverse out)))
2851
2852 (defun gnus-set-sorted-intersection (list1 list2)
2853   ;; LIST1 and LIST2 have to be sorted over <.
2854   ;; This function modifies LIST1.
2855   (let* ((top (cons nil list1))
2856          (prev top))
2857   (while (and list1 list2)
2858     (cond ((= (car list1) (car list2))
2859            (setq prev list1
2860                  list1 (cdr list1)
2861                  list2 (cdr list2)))
2862           ((< (car list1) (car list2))
2863            (setcdr prev (cdr list1))
2864            (setq list1 (cdr list1)))
2865           (t
2866            (setq list2 (cdr list2)))))
2867   (setcdr prev nil)
2868   (cdr top)))
2869
2870 (defun gnus-compress-sequence (numbers &optional always-list)
2871   "Convert list of numbers to a list of ranges or a single range.
2872 If ALWAYS-LIST is non-nil, this function will always release a list of
2873 ranges."
2874   (let* ((first (car numbers))
2875          (last (car numbers))
2876          result)
2877     (if (null numbers)
2878         nil
2879       (if (not (listp (cdr numbers)))
2880           numbers
2881         (while numbers
2882           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2883                 ((= (1+ last) (car numbers)) ;Still in sequence
2884                  (setq last (car numbers)))
2885                 (t                      ;End of one sequence
2886                  (setq result 
2887                        (cons (if (= first last) first
2888                                (cons first last)) result))
2889                  (setq first (car numbers))
2890                  (setq last  (car numbers))))
2891           (setq numbers (cdr numbers)))
2892         (if (and (not always-list) (null result))
2893             (if (= first last) (list first) (cons first last))
2894           (nreverse (cons (if (= first last) first (cons first last))
2895                           result)))))))
2896
2897 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2898 (defun gnus-uncompress-range (ranges)
2899   "Expand a list of ranges into a list of numbers.
2900 RANGES is either a single range on the form `(num . num)' or a list of
2901 these ranges."
2902   (let (first last result)
2903     (cond 
2904      ((null ranges)
2905       nil)
2906      ((not (listp (cdr ranges)))
2907       (setq first (car ranges))
2908       (setq last (cdr ranges))
2909       (while (<= first last)
2910         (setq result (cons first result))
2911         (setq first (1+ first)))
2912       (nreverse result))
2913      (t
2914       (while ranges
2915         (if (atom (car ranges))
2916             (if (numberp (car ranges))
2917                 (setq result (cons (car ranges) result)))
2918           (setq first (car (car ranges)))
2919           (setq last  (cdr (car ranges)))
2920           (while (<= first last)
2921             (setq result (cons first result))
2922             (setq first (1+ first))))
2923         (setq ranges (cdr ranges)))
2924       (nreverse result)))))
2925
2926 (defun gnus-add-to-range (ranges list)
2927   "Return a list of ranges that has all articles from both RANGES and LIST.
2928 Note: LIST has to be sorted over `<'."
2929   (if (not ranges)
2930       (gnus-compress-sequence list t)
2931     (setq list (copy-sequence list))
2932     (or (listp (cdr ranges))
2933         (setq ranges (list ranges)))
2934     (let ((out ranges)
2935           ilist lowest highest temp)
2936       (while (and ranges list)
2937         (setq ilist list)
2938         (setq lowest (or (and (atom (car ranges)) (car ranges))
2939                          (car (car ranges))))
2940         (while (and list (cdr list) (< (car (cdr list)) lowest))
2941           (setq list (cdr list)))
2942         (if (< (car ilist) lowest)
2943             (progn
2944               (setq temp list)
2945               (setq list (cdr list))
2946               (setcdr temp nil)
2947               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2948         (setq highest (or (and (atom (car ranges)) (car ranges))
2949                           (cdr (car ranges))))
2950         (while (and list (<= (car list) highest))
2951           (setq list (cdr list)))
2952         (setq ranges (cdr ranges)))
2953       (if list
2954           (setq out (nconc (gnus-compress-sequence list t) out)))
2955       (setq out (sort out (lambda (r1 r2) 
2956                             (< (or (and (atom r1) r1) (car r1))
2957                                (or (and (atom r2) r2) (car r2))))))
2958       (setq ranges out)
2959       (while ranges
2960         (if (atom (car ranges))
2961             (if (cdr ranges)
2962                 (if (atom (car (cdr ranges)))
2963                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2964                         (progn
2965                           (setcar ranges (cons (car ranges) 
2966                                                (car (cdr ranges))))
2967                           (setcdr ranges (cdr (cdr ranges)))))
2968                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2969                       (progn
2970                         (setcar (car (cdr ranges)) (car ranges))
2971                         (setcar ranges (car (cdr ranges)))
2972                         (setcdr ranges (cdr (cdr ranges)))))))
2973           (if (cdr ranges)
2974               (if (atom (car (cdr ranges)))
2975                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2976                       (progn
2977                         (setcdr (car ranges) (car (cdr ranges)))
2978                         (setcdr ranges (cdr (cdr ranges)))))
2979                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2980                     (progn
2981                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2982                       (setcdr ranges (cdr (cdr ranges))))))))
2983         (setq ranges (cdr ranges)))
2984       out)))
2985
2986 (defun gnus-remove-from-range (ranges list)
2987   "Return a list of ranges that has all articles from LIST removed from RANGES.
2988 Note: LIST has to be sorted over `<'."
2989   ;; !!! This function shouldn't look like this, but I've got a headache.
2990   (gnus-compress-sequence 
2991    (gnus-sorted-complement
2992     (gnus-uncompress-range ranges) list)))
2993
2994 (defun gnus-member-of-range (number ranges)
2995   (if (not (listp (cdr ranges)))
2996       (and (>= number (car ranges)) 
2997            (<= number (cdr ranges)))
2998     (let ((not-stop t))
2999       (while (and ranges 
3000                   (if (numberp (car ranges))
3001                       (>= number (car ranges))
3002                     (>= number (car (car ranges))))
3003                   not-stop)
3004         (if (if (numberp (car ranges))
3005                 (= number (car ranges))
3006               (and (>= number (car (car ranges)))
3007                    (<= number (cdr (car ranges)))))
3008             (setq not-stop nil))
3009         (setq ranges (cdr ranges)))
3010       (not not-stop))))
3011
3012 \f
3013 ;;;
3014 ;;; Gnus group mode
3015 ;;;
3016
3017 (defvar gnus-group-mode-map nil)
3018 (defvar gnus-group-group-map nil)
3019 (defvar gnus-group-mark-map nil)
3020 (defvar gnus-group-list-map nil)
3021 (defvar gnus-group-sub-map nil)
3022 (put 'gnus-group-mode 'mode-class 'special)
3023
3024 (if gnus-group-mode-map
3025     nil
3026   (setq gnus-group-mode-map (make-keymap))
3027   (suppress-keymap gnus-group-mode-map)
3028   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3029   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3030   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3031   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3032   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3033   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3034   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3035   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3036   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3037   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3038   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3039   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3040   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3041   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3042   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3043   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3044   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3045   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3046   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3047   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3048   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3049   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3050   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3051   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3052   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3053   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3054   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3055   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3056   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3057   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3058   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3059   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3060   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3061   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3062   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3063   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3064   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3065   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3066   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3067   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3068   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3069   (define-key gnus-group-mode-map "V" 'gnus-version)
3070   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3071   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3072   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3073   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3074   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3075   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3076   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3077   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3078   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3079   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3080   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3081   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3082   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3083   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3084   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3085
3086   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3087   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3088   (define-prefix-command 'gnus-group-mark-map)
3089   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3090   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3091   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3092   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3093
3094   (define-prefix-command 'gnus-group-group-map)
3095   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3096   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3097   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3098   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3099   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3100   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3101   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3102   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3103   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3104   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3105   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3106   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3107   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3108   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3109   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3110   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3111   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3112   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3113
3114   (define-prefix-command 'gnus-group-list-map)
3115   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3116   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3117   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3118   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3119   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3120   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3121   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3122   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3123   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3124
3125   (define-prefix-command 'gnus-group-sub-map)
3126   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3127   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3128   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3129   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3130   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3131   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3132   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3133   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3134
3135 (defun gnus-group-mode ()
3136   "Major mode for reading news.
3137
3138 All normal editing commands are switched off.
3139 \\<gnus-group-mode-map>
3140 The group buffer lists (some of) the groups available.  For instance,
3141 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3142 lists all zombie groups. 
3143
3144 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3145 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3146
3147 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3148
3149 The following commands are available:
3150
3151 \\{gnus-group-mode-map}"
3152   (interactive)
3153   (if gnus-visual (gnus-group-make-menu-bar))
3154   (kill-all-local-variables)
3155   (setq mode-line-modified "-- ")
3156   (make-local-variable 'mode-line-format)
3157   (setq mode-line-format (copy-sequence mode-line-format))
3158   (and (equal (nth 3 mode-line-format) "   ")
3159        (setcar (nthcdr 3 mode-line-format) ""))
3160   (setq major-mode 'gnus-group-mode)
3161   (setq mode-name "Group")
3162   (gnus-group-set-mode-line)
3163   (setq mode-line-process nil)
3164   (use-local-map gnus-group-mode-map)
3165   (buffer-disable-undo (current-buffer))
3166   (setq truncate-lines t)
3167   (setq buffer-read-only t)
3168   (run-hooks 'gnus-group-mode-hook))
3169
3170 (defun gnus-mouse-pick-group (e)
3171   (interactive "e")
3172   (mouse-set-point e)
3173   (gnus-group-read-group nil))
3174
3175 ;; Look at LEVEL and find out what the level is really supposed to be.
3176 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3177 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3178 (defun gnus-group-default-level (&optional level number-or-nil)
3179   (cond  
3180    (gnus-group-use-permanent-levels
3181     (setq gnus-group-default-list-level 
3182           (or level gnus-group-default-list-level))
3183     (or gnus-group-default-list-level gnus-level-subscribed))
3184    (number-or-nil
3185     level)
3186    (t
3187     (or level gnus-group-default-list-level gnus-level-subscribed))))
3188   
3189
3190 ;;;###autoload
3191 (defun gnus-no-server (&optional arg)
3192   "Read network news.
3193 If ARG is a positive number, Gnus will use that as the
3194 startup level. If ARG is nil, Gnus will be started at level 2. 
3195 If ARG is non-nil and not a positive number, Gnus will
3196 prompt the user for the name of an NNTP server to use.
3197 As opposed to `gnus', this command will not connect to the local server."
3198   (interactive "P")
3199   (setq gnus-group-use-permanent-levels t)
3200   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3201
3202 (defalias '\(ding\) 'gnus)
3203
3204 ;;;###autoload
3205 (defun gnus (&optional arg dont-connect)
3206   "Read network news.
3207 If ARG is non-nil and a positive number, Gnus will use that as the
3208 startup level. If ARG is non-nil and not a positive number, Gnus will
3209 prompt the user for the name of an NNTP server to use."
3210   (interactive "P")
3211   (if (get-buffer gnus-group-buffer)
3212       (progn
3213         (switch-to-buffer gnus-group-buffer)
3214         (gnus-group-get-new-news))
3215     (gnus-clear-system)
3216     (nnheader-init-server-buffer)
3217     (gnus-read-init-file)
3218
3219     (let ((level (and arg (numberp arg) (> arg 0) arg))
3220           did-connect)
3221       (unwind-protect
3222           (progn
3223             (gnus-group-setup-buffer)
3224             (or dont-connect 
3225                 (setq did-connect
3226                       (gnus-start-news-server (and arg (not level))))))
3227         (if (and (not dont-connect) 
3228                  (not did-connect))
3229             (gnus-group-quit)
3230           (run-hooks 'gnus-startup-hook)
3231           ;; NNTP server is successfully open. 
3232
3233           ;; Find the current startup file name.
3234           (setq gnus-current-startup-file 
3235                 (gnus-make-newsrc-file gnus-startup-file))
3236
3237           ;; Read the dribble file.
3238           (and gnus-use-dribble-file (gnus-dribble-read-file))
3239
3240           (gnus-summary-make-display-table)
3241           (let ((buffer-read-only nil))
3242             (erase-buffer)
3243             (if (not gnus-inhibit-startup-message)
3244                 (progn
3245                   (gnus-group-startup-message)
3246                   (sit-for 0))))
3247           (gnus-setup-news nil level)
3248           (gnus-group-list-groups level)
3249           (gnus-configure-windows 'group))))))
3250
3251 (defun gnus-unload ()
3252   "Unload all Gnus features."
3253   (interactive)
3254   (let ((history load-history)
3255         feature)
3256     (while history
3257       (and (string-match "^gnus" (car (car history)))
3258            (setq feature (cdr (assq 'provide (car history))))
3259            (unload-feature feature 'force))
3260       (setq history (cdr history)))))
3261
3262 (defun gnus-group-startup-message (&optional x y)
3263   "Insert startup message in current buffer."
3264   ;; Insert the message.
3265   (erase-buffer)
3266   (insert
3267    (format "
3268      %s
3269            A newsreader 
3270       for GNU Emacs
3271
3272         Based on GNUS 
3273              written by 
3274      Masanobu UMEDA
3275
3276        A Praxis Release
3277       larsi@ifi.uio.no
3278
3279            gnus-version))
3280   ;; And then hack it.
3281   ;; 18 is the longest line.
3282   (indent-rigidly (point-min) (point-max) 
3283                   (/ (max (- (window-width) (or x 28)) 0) 2))
3284   (goto-char (point-min))
3285   ;; +4 is fuzzy factor.
3286   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3287
3288   ;; Fontify some.
3289   (goto-char (point-min))
3290   (search-forward "Praxis")
3291   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3292   (goto-char (point-min)))
3293
3294 (defun gnus-group-setup-buffer ()
3295   (or (get-buffer gnus-group-buffer)
3296       (progn
3297         (switch-to-buffer gnus-group-buffer)
3298         (gnus-add-current-to-buffer-list)
3299         (gnus-group-mode)
3300         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3301
3302 (defun gnus-group-list-groups (&optional level unread)
3303   "List newsgroups with level LEVEL or lower that have unread articles.
3304 Default is all subscribed groups.
3305 If argument UNREAD is non-nil, groups with no unread articles are also
3306 listed." 
3307   (interactive (list (if current-prefix-arg
3308                          (prefix-numeric-value current-prefix-arg)
3309                        (or
3310                         (gnus-group-default-level nil t)
3311                         gnus-level-subscribed))))
3312   (or level
3313       (setq level (car gnus-group-list-mode)
3314             unread (cdr gnus-group-list-mode)))
3315   (setq level (gnus-group-default-level level))
3316   (gnus-group-setup-buffer)     ;May call from out of group buffer
3317   (let ((case-fold-search nil)
3318         (group (gnus-group-group-name)))
3319     (funcall gnus-group-prepare-function level unread nil)
3320     (if (zerop (buffer-size))
3321         (gnus-message 5 gnus-no-groups-message)
3322       (goto-char (point-min))
3323       (if (not group)
3324           ;; Go to the first group with unread articles.
3325           (gnus-group-search-forward nil nil nil t)
3326         ;; Find the right group to put point on. If the current group
3327         ;; has disapeared in the new listing, try to find the next
3328         ;; one. If no next one can be found, just leave point at the
3329         ;; first newsgroup in the buffer.
3330         (if (not (gnus-goto-char
3331                   (text-property-any (point-min) (point-max) 
3332                                      'gnus-group (intern group))))
3333             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3334               (while (and newsrc
3335                           (not (gnus-goto-char 
3336                                 (text-property-any 
3337                                  (point-min) (point-max) 'gnus-group 
3338                                  (intern (car (car newsrc)))))))
3339                 (setq newsrc (cdr newsrc)))
3340               (or newsrc (progn (goto-char (point-max))
3341                                 (forward-line -1))))))
3342       ;; Adjust cursor point.
3343       (gnus-group-position-cursor))))
3344
3345 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3346   "List all newsgroups with unread articles of level LEVEL or lower.
3347 If ALL is non-nil, list groups that have no unread articles.
3348 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3349 If REGEXP, only list groups matching REGEXP."
3350   (set-buffer gnus-group-buffer)
3351   (let ((buffer-read-only nil)
3352         (newsrc (cdr gnus-newsrc-alist))
3353         (lowest (or lowest 1))
3354         info clevel unread group)
3355     (erase-buffer)
3356     (if (< lowest gnus-level-zombie)
3357         ;; List living groups.
3358         (while newsrc
3359           (setq info (car newsrc)
3360                 group (car info)
3361                 newsrc (cdr newsrc)
3362                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3363           (and unread ; This group might be bogus
3364                (or (not regexp)
3365                    (string-match regexp group))
3366                (<= (setq clevel (car (cdr info))) level) 
3367                (>= clevel lowest)
3368                (or all            ; We list all groups?
3369                    (eq unread t)  ; We list unactivated groups
3370                    (> unread 0)   ; We list groups with unread articles
3371                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3372                (gnus-group-insert-group-line 
3373                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3374
3375     ;; List dead groups.
3376     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3377          (gnus-group-prepare-flat-list-dead 
3378           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3379           gnus-level-zombie ?Z
3380           regexp))
3381     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3382          (gnus-group-prepare-flat-list-dead 
3383           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3384           gnus-level-killed ?K regexp))
3385
3386     (gnus-group-set-mode-line)
3387     (setq gnus-group-list-mode (cons level all))
3388     (run-hooks 'gnus-group-prepare-hook)))
3389
3390 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3391   ;; List zombies and killed lists somehwat faster, which was
3392   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3393   ;; this by ignoring the group format specification altogether.
3394   (let (group beg)
3395     (while groups
3396       (setq group (car groups)
3397             groups (cdr groups))
3398       (if (or (not regexp)
3399               (string-match regexp group))
3400           (progn
3401             (setq beg (point))
3402             (insert (format " %c     *: %s\n" mark group))
3403             (add-text-properties 
3404              beg (1+ beg) 
3405              (list 'gnus-group (intern group)
3406                    'gnus-unread t
3407                    'gnus-level level)))))))
3408
3409 (defun gnus-group-real-name (group)
3410   "Find the real name of a foreign newsgroup."
3411   (if (string-match ":[^:]+$" group)
3412       (substring group (1+ (match-beginning 0)))
3413     group))
3414
3415 (defun gnus-group-prefixed-name (group method)
3416   "Return the whole name from GROUP and METHOD."
3417   (and (stringp method) (setq method (gnus-server-to-method method)))
3418   (concat (format "%s" (car method))
3419           (if (and 
3420                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3421                (not (string= (nth 1 method) "")))
3422               (concat "+" (nth 1 method)))
3423           ":" group))
3424
3425 (defun gnus-group-real-prefix (group)
3426   "Return the prefix of the current group name."
3427   (if (string-match "^[^:]+:" group)
3428       (substring group 0 (match-end 0))
3429     ""))
3430
3431 (defun gnus-group-method-name (group)
3432   "Return the method used for selecting GROUP."
3433   (let ((prefix (gnus-group-real-prefix group)))
3434     (if (equal prefix "")
3435         gnus-select-method
3436       (if (string-match "^[^\\+]+\\+" prefix)
3437           (list (intern (substring prefix 0 (1- (match-end 0))))
3438                 (substring prefix (match-end 0) (1- (length prefix))))
3439         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3440
3441 (defun gnus-group-foreign-p (group)
3442   "Return nil if GROUP is native, non-nil if it is foreign."
3443   (string-match ":" group))
3444
3445 (defun gnus-group-set-info (info &optional method-only-group part)
3446   (let* ((entry (gnus-gethash
3447                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3448          (part-info info)
3449          (info (if method-only-group (nth 2 entry) info)))
3450     (if (not method-only-group)
3451         ()
3452       (or entry
3453           (error "Trying to change non-existent group %s" method-only-group))
3454       ;; We have recevied parts of the actual group info - either the
3455       ;; select method or the group parameters.  We first check
3456       ;; whether we have to extend the info, and if so, do that.
3457       (let ((len (length info))
3458             (total (if (eq part 'method) 5 6)))
3459         (and (< len total)
3460              (setcdr (nthcdr (1- len) info)
3461                      (make-list (- total len) nil)))
3462         ;; Then we enter the new info.
3463         (setcar (nthcdr (1- total) info) part-info)))
3464     ;; We uncompress some lists of marked articles.
3465     (let (marked)
3466       (if (not (setq marked (nth 3 info)))
3467           ()
3468         (while marked
3469           (or (eq 'score (car (car marked)))
3470               (eq 'bookmark (car (car marked)))
3471               (eq 'killed (car (car marked)))
3472               (setcdr (car marked) 
3473                       (gnus-uncompress-range (cdr (car marked)))))
3474           (setq marked (cdr marked)))))
3475     (if entry
3476         ()
3477       ;; This is a new group, so we just create it.
3478       (save-excursion
3479         (set-buffer gnus-group-buffer)
3480         (if (nth 4 info)
3481             ;; It's a foreign group...
3482             (gnus-group-make-group 
3483              (gnus-group-real-name (car info))
3484              (prin1-to-string (car (nth 4 info)))
3485              (nth 1 (nth 4 info)))
3486           ;; It's a native group.
3487           (gnus-group-make-group
3488            (car info)
3489            (prin1-to-string (car gnus-select-method))
3490            (nth 1 gnus-select-method)))
3491         (gnus-message 6 "Note: New group created")
3492         (setq entry 
3493               (gnus-gethash (gnus-group-prefixed-name 
3494                              (gnus-group-real-name (car info))
3495                              (or (nth 4 info) gnus-select-method))
3496                             gnus-newsrc-hashtb))))
3497     ;; Whether it was a new group or not, we now have the entry, so we
3498     ;; can do the update.
3499     (if entry
3500         (progn
3501           (setcar (nthcdr 2 entry) info)
3502           (if (and (not (eq (car entry) t)) 
3503                    (gnus-gethash (car info) gnus-active-hashtb))
3504               (let ((marked (nth 3 info)))
3505                 (setcar entry 
3506                         (max 0 (- (length (gnus-list-of-unread-articles 
3507                                            (car info)))
3508                                   (length (cdr (assq 'tick marked)))
3509                                   (length (cdr (assq 'dormant marked)))))))))
3510       (error "No such group: %s" (car info)))))
3511
3512 (defun gnus-group-set-method-info (group select-method)
3513   (gnus-group-set-info select-method group 'method))
3514
3515 (defun gnus-group-set-params-info (group params)
3516   (gnus-group-set-info params group 'params))
3517
3518 (defun gnus-group-update-group-line ()
3519   "This function updates the current line in the newsgroup buffer and
3520 moves the point to the colon."
3521   (let* ((buffer-read-only nil)
3522          (group (gnus-group-group-name))
3523          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3524     (if entry
3525         (gnus-dribble-enter 
3526          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3527                  ")")))
3528     (beginning-of-line)
3529     (delete-region (point) (progn (forward-line 1) (point)))
3530     (gnus-group-insert-group-line-info group)
3531     (forward-line -1)
3532     (gnus-group-position-cursor)))
3533
3534 (defun gnus-group-insert-group-line-info (group)
3535   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3536         active info)
3537     (if entry
3538         (progn
3539           (setq info (nth 2 entry))
3540           (gnus-group-insert-group-line 
3541            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3542       (setq active (gnus-gethash group gnus-active-hashtb))
3543       (gnus-group-insert-group-line 
3544        nil group 
3545        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3546        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3547
3548 (defun gnus-group-insert-group-line (gformat group level marked number method)
3549   (let* ((gformat (or gformat gnus-group-line-format-spec))
3550          (active (gnus-gethash group gnus-active-hashtb))
3551          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3552          (number-of-dormant (length (cdr (assq 'dormant marked))))
3553          (number-of-ticked (length (cdr (assq 'tick marked))))
3554          (number-of-ticked-and-dormant
3555           (+ number-of-ticked number-of-dormant))
3556          (number-of-unread-unticked 
3557           (if (numberp number) (int-to-string (max 0 number))
3558             "*"))
3559          (number-of-read
3560           (if (numberp number)
3561               (max 0 (- number-total number))
3562             "*"))
3563          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3564                            ((<= level gnus-level-unsubscribed) ?U)
3565                            ((= level gnus-level-zombie) ?Z)
3566                            (t ?K)))
3567          (qualified-group (gnus-group-real-name group))
3568          (newsgroup-description 
3569           (if gnus-description-hashtb
3570               (or (gnus-gethash group gnus-description-hashtb) "")
3571             ""))
3572          (moderated (if (member group gnus-moderated-list) ?m ? ))
3573          (moderated-string (if (eq moderated ?m) "(m)" ""))
3574          (method (gnus-server-get-method group method))
3575          (news-server (or (car (cdr method)) ""))
3576          (news-method (or (car method) ""))
3577          (news-method-string 
3578           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3579          (marked (if (and 
3580                       (numberp number) 
3581                       (zerop number)
3582                       (> number-of-ticked 0))
3583                      ?* ? ))
3584          (number (if (eq number t) "*" (+ number number-of-dormant 
3585                                           number-of-ticked)))
3586          (process-marked (if (member qualified-group gnus-group-marked)
3587                              gnus-process-mark ? ))
3588          (buffer-read-only nil)
3589          header ; passed as parameter to user-funcs.
3590          b)
3591     (beginning-of-line)
3592     (setq b (point))
3593     ;; Insert the text.
3594     (insert (eval gformat))
3595
3596     (add-text-properties 
3597      b (1+ b) (list 'gnus-group (intern group)
3598                     'gnus-unread (if (numberp number)
3599                                      (string-to-int number-of-unread-unticked)
3600                                    t)
3601                     'gnus-marked marked
3602                     'gnus-level level))))
3603
3604 (defun gnus-group-update-group (group &optional visible-only)
3605   "Update newsgroup info of GROUP.
3606 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3607   (save-excursion
3608     (set-buffer gnus-group-buffer)
3609     (let ((buffer-read-only nil)
3610           visible)
3611       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3612         (if entry
3613             (gnus-dribble-enter 
3614              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3615                      ")"))))
3616       ;; Buffer may be narrowed.
3617       (save-restriction
3618         (widen)
3619         ;; Search a line to modify.  If the buffer is large, the search
3620         ;; takes long time.  In most cases, current point is on the line
3621         ;; we are looking for.  So, first of all, check current line. 
3622         (if (or (progn
3623                   (beginning-of-line)
3624                   (eq (get-text-property (point) 'gnus-group)
3625                       (intern group)))
3626                 (progn
3627                   (gnus-goto-char 
3628                    (text-property-any 
3629                     (point-min) (point-max) 'gnus-group (intern group)))))
3630             ;; GROUP is listed in current buffer. So, delete old line.
3631             (progn
3632               (setq visible t)
3633               (beginning-of-line)
3634               (delete-region (point) (progn (forward-line 1) (point))))
3635           ;; No such line in the buffer, find out where it's supposed to
3636           ;; go, and insert it there (or at the end of the buffer).
3637           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3638           (or visible-only
3639               (let ((entry 
3640                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3641                 (while (and entry
3642                             (car entry)
3643                             (not
3644                              (gnus-goto-char
3645                               (text-property-any
3646                                (point-min) (point-max) 
3647                                'gnus-group (intern (car (car entry)))))))
3648                   (setq entry (cdr entry)))
3649                 (or entry (goto-char (point-max)))))))
3650       (if (or visible (not visible-only))
3651           (gnus-group-insert-group-line-info group))
3652       (gnus-group-set-mode-line))))
3653
3654 (defun gnus-group-set-mode-line ()
3655   (if (memq 'group gnus-updated-mode-lines)
3656       (let* ((gformat (or gnus-group-mode-line-format-spec
3657                           (setq gnus-group-mode-line-format-spec
3658                                 (gnus-parse-format 
3659                                  gnus-group-mode-line-format 
3660                                  gnus-group-mode-line-format-alist))))
3661              (news-server (car (cdr gnus-select-method)))
3662              (news-method (car gnus-select-method))
3663              (max-len 60)
3664              (mode-string (eval gformat)))
3665         (setq mode-string (eval gformat))
3666         (if (> (length mode-string) max-len) 
3667             (setq mode-string (substring mode-string 0 (- max-len 4))))
3668         (setq mode-line-buffer-identification mode-string)
3669         (set-buffer-modified-p t))))
3670
3671 (defun gnus-group-group-name ()
3672   "Get the name of the newsgroup on the current line."
3673   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3674     (and group (symbol-name group))))
3675
3676 (defun gnus-group-group-level ()
3677   "Get the level of the newsgroup on the current line."
3678   (get-text-property (gnus-point-at-bol) 'gnus-level))
3679
3680 (defun gnus-group-group-unread ()
3681   "Get the number of unread articles of the newsgroup on the current line."
3682   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3683
3684 (defun gnus-group-search-forward (&optional backward all level first-too)
3685   "Find the next newsgroup with unread articles.
3686 If BACKWARD is non-nil, find the previous newsgroup instead.
3687 If ALL is non-nil, just find any newsgroup.
3688 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3689 group exists.
3690 If FIRST-TOO, the current line is also eligible as a target."
3691   (let ((way (if backward -1 1))
3692         (low gnus-level-killed)
3693         (beg (point))
3694         pos found lev)
3695     (if (and backward (progn (beginning-of-line)) (bobp))
3696         nil
3697       (or first-too (forward-line way))
3698       (while (and 
3699               (not (eobp))
3700               (not (setq 
3701                     found 
3702                     (and (or all
3703                              (and
3704                               (let ((unread 
3705                                      (get-text-property (point) 'gnus-unread)))
3706                                 (or (eq unread t) (and unread (> unread 0))))
3707                               (setq lev (get-text-property (point)
3708                                                            'gnus-level))
3709                               (<= lev gnus-level-subscribed)))
3710                          (or (not level)
3711                              (and (setq lev (get-text-property (point)
3712                                                                'gnus-level))
3713                                   (or (= lev level)
3714                                       (and (< lev low)
3715                                            (< level lev)
3716                                            (progn
3717                                              (setq low lev)
3718                                              (setq pos (point))
3719                                              nil))))))))
3720               (zerop (forward-line way)))))
3721     (if found 
3722         (progn (gnus-group-position-cursor) t)
3723       (goto-char (or pos beg))
3724       (and pos t))))
3725
3726 ;;; Gnus group mode commands
3727
3728 ;; Group marking.
3729
3730 (defun gnus-group-mark-group (n &optional unmark no-advance)
3731   "Mark the current group."
3732   (interactive "p")
3733   (let ((buffer-read-only nil)
3734         group)
3735     (while 
3736         (and (> n 0) 
3737              (setq group (gnus-group-group-name))
3738              (progn
3739                (beginning-of-line)
3740                (forward-char 
3741                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3742                (delete-char 1)
3743                (if unmark
3744                    (progn
3745                      (insert " ")
3746                      (setq gnus-group-marked (delete group gnus-group-marked)))
3747                  (insert "#")
3748                  (setq gnus-group-marked
3749                        (cons group (delete group gnus-group-marked))))
3750                t)
3751              (or no-advance (zerop (gnus-group-next-group 1))))
3752       (setq n (1- n)))
3753     (gnus-summary-position-cursor)
3754     n))
3755
3756 (defun gnus-group-unmark-group (n)
3757   "Remove the mark from the current group."
3758   (interactive "p")
3759   (gnus-group-mark-group n 'unmark))
3760
3761 (defun gnus-group-mark-region (unmark beg end)
3762   "Mark all groups between point and mark.
3763 If UNMARK, remove the mark instead."
3764   (interactive "P\nr")
3765   (let ((num (count-lines beg end)))
3766     (save-excursion
3767       (goto-char beg)
3768       (- num (gnus-group-mark-group num unmark)))))
3769
3770 (defun gnus-group-remove-mark (group)
3771   (and (gnus-group-goto-group group)
3772        (save-excursion
3773          (gnus-group-mark-group 1 'unmark t))))
3774
3775 ;; Return a list of groups to work on.  Take into consideration N (the
3776 ;; prefix) and the list of marked groups.
3777 (defun gnus-group-process-prefix (n)
3778   (cond (n
3779          (setq n (prefix-numeric-value n))
3780          ;; There is a prefix, so we return a list of the N next
3781          ;; groups. 
3782          (let ((way (if (< n 0) -1 1))
3783                (n (abs n))
3784                group groups)
3785            (save-excursion
3786              (while (and (> n 0)
3787                          (setq group (gnus-group-group-name)))
3788                (setq groups (cons group groups))
3789                (setq n (1- n))
3790                (forward-line way)))
3791            (nreverse groups)))
3792         (gnus-group-marked
3793          ;; No prefix, but a list of marked articles.
3794          (reverse gnus-group-marked))
3795         (t
3796          ;; Neither marked articles or a prefix, so we return the
3797          ;; current group.
3798          (let ((group (gnus-group-group-name)))
3799            (and group (list group))))))
3800
3801 ;; Selecting groups.
3802
3803 (defun gnus-group-read-group (&optional all no-article group)
3804   "Read news in this newsgroup.
3805 If the prefix argument ALL is non-nil, already read articles become
3806 readable. If the optional argument NO-ARTICLE is non-nil, no article
3807 will be auto-selected upon group entry."
3808   (interactive "P")
3809   (let ((group (or group (gnus-group-group-name)))
3810         number active marked entry)
3811     (or group (error "No group on current line"))
3812     (setq marked 
3813           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3814     ;; This group might be a dead group. In that case we have to get
3815     ;; the number of unread articles from `gnus-active-hashtb'.
3816     (if entry
3817         (setq number (car entry))
3818       (if (setq active (gnus-gethash group gnus-active-hashtb))
3819           (setq number (- (1+ (cdr active)) (car active)))))
3820     (gnus-summary-read-group 
3821      group (or all (and (numberp number) 
3822                         (zerop (+ number (length (cdr (assq 'tick marked)))
3823                                   (length (cdr (assq 'dormant marked)))))))
3824      no-article)))
3825
3826 (defun gnus-group-select-group (&optional all)
3827   "Select this newsgroup.
3828 No article is selected automatically.
3829 If argument ALL is non-nil, already read articles become readable."
3830   (interactive "P")
3831   (gnus-group-read-group all t))
3832
3833 (defun gnus-group-select-group-all ()
3834   "Select the current group and display all articles in it."
3835   (interactive)
3836   (gnus-group-select-group 'all))
3837
3838 ;; Enter a group that is not in the group buffer. Non-nil is returned
3839 ;; if selection was successful.
3840 (defun gnus-group-read-ephemeral-group 
3841   (group method &optional activate quit-config)
3842   (let ((group (if (gnus-group-foreign-p group) group
3843                  (gnus-group-prefixed-name group method))))
3844     (gnus-sethash 
3845      group
3846      (list t nil (list group gnus-level-default-subscribed nil nil 
3847                        (append method
3848                                (list
3849                                 (list 'quit-config 
3850                                       (if quit-config quit-config
3851                                         (cons (current-buffer) 'summary)))))))
3852      gnus-newsrc-hashtb)
3853     (set-buffer gnus-group-buffer)
3854     (or (gnus-check-server method)
3855         (error "Unable to contact server: %s" (gnus-status-message method)))
3856     (if activate (or (gnus-request-group group)
3857                      (error "Couldn't request group")))
3858     (condition-case ()
3859         (gnus-group-read-group t t group)
3860       (error nil)
3861       (quit nil))
3862     (not (equal major-mode 'gnus-group-mode))))
3863   
3864 (defun gnus-group-jump-to-group (group)
3865   "Jump to newsgroup GROUP."
3866   (interactive 
3867    (list (completing-read 
3868           "Group: " gnus-active-hashtb nil 
3869           (memq gnus-select-method gnus-have-read-active-file))))
3870
3871   (if (equal group "")
3872       (error "Empty group name"))
3873
3874   (let ((b (text-property-any 
3875             (point-min) (point-max) 'gnus-group (intern group))))
3876     (if b
3877         ;; Either go to the line in the group buffer...
3878         (goto-char b)
3879       ;; ... or insert the line.
3880       (or
3881        (gnus-gethash group gnus-active-hashtb)
3882        (gnus-activate-group group)
3883        (error "%s error: %s" group (gnus-status-message group)))
3884
3885       (gnus-group-update-group group)
3886       (goto-char (text-property-any 
3887                   (point-min) (point-max) 'gnus-group (intern group)))))
3888   ;; Adjust cursor point.
3889   (gnus-group-position-cursor))
3890
3891 (defun gnus-group-goto-group (group)
3892   "Goto to newsgroup GROUP."
3893   (let ((b (text-property-any (point-min) (point-max) 
3894                               'gnus-group (intern group))))
3895     (and b (goto-char b))))
3896
3897 (defun gnus-group-next-group (n)
3898   "Go to next N'th newsgroup.
3899 If N is negative, search backward instead.
3900 Returns the difference between N and the number of skips actually
3901 done."
3902   (interactive "p")
3903   (gnus-group-next-unread-group n t))
3904
3905 (defun gnus-group-next-unread-group (n &optional all level)
3906   "Go to next N'th unread newsgroup.
3907 If N is negative, search backward instead.
3908 If ALL is non-nil, choose any newsgroup, unread or not.
3909 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3910 such group can be found, the next group with a level higher than
3911 LEVEL.
3912 Returns the difference between N and the number of skips actually
3913 made."
3914   (interactive "p")
3915   (let ((backward (< n 0))
3916         (n (abs n)))
3917     (while (and (> n 0)
3918                 (gnus-group-search-forward 
3919                  backward (or (not gnus-group-goto-unread) all) level))
3920       (setq n (1- n)))
3921     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3922                                (if level " on this level or higher" "")))
3923     n))
3924
3925 (defun gnus-group-prev-group (n)
3926   "Go to previous N'th newsgroup.
3927 Returns the difference between N and the number of skips actually
3928 done."
3929   (interactive "p")
3930   (gnus-group-next-unread-group (- n) t))
3931
3932 (defun gnus-group-prev-unread-group (n)
3933   "Go to previous N'th unread newsgroup.
3934 Returns the difference between N and the number of skips actually
3935 done."  
3936   (interactive "p")
3937   (gnus-group-next-unread-group (- n)))
3938
3939 (defun gnus-group-next-unread-group-same-level (n)
3940   "Go to next N'th unread newsgroup on the same level.
3941 If N is negative, search backward instead.
3942 Returns the difference between N and the number of skips actually
3943 done."
3944   (interactive "p")
3945   (gnus-group-next-unread-group n t (gnus-group-group-level))
3946   (gnus-group-position-cursor))
3947
3948 (defun gnus-group-prev-unread-group-same-level (n)
3949   "Go to next N'th unread newsgroup on the same level.
3950 Returns the difference between N and the number of skips actually
3951 done."
3952   (interactive "p")
3953   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3954   (gnus-group-position-cursor))
3955
3956 (defun gnus-group-best-unread-group (&optional exclude-group)
3957   "Go to the group with the highest level.
3958 If EXCLUDE-GROUP, do not go to that group."
3959   (interactive)
3960   (goto-char (point-min))
3961   (let ((best 100000)
3962         unread best-point)
3963     (while (setq unread (get-text-property (point) 'gnus-unread))
3964       (if (and (numberp unread) (> unread 0))
3965           (progn
3966             (if (and (< (get-text-property (point) 'gnus-level) best)
3967                      (or (not exclude-group)
3968                          (not (equal exclude-group (gnus-group-group-name)))))
3969                 (progn 
3970                   (setq best (get-text-property (point) 'gnus-level))
3971                   (setq best-point (point))))))
3972       (forward-line 1))
3973     (if best-point (goto-char best-point))
3974     (gnus-summary-position-cursor)
3975     (and best-point (gnus-group-group-name))))
3976
3977 (defun gnus-group-first-unread-group ()
3978   "Go to the first group with unread articles."
3979   (interactive)
3980   (prog1
3981       (let ((opoint (point))
3982             unread)
3983         (goto-char (point-min))
3984         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
3985                 (not (zerop unread)) ; Has unread articles.
3986                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
3987             (point) ; Success.
3988           (goto-char opoint)
3989           nil)) ; Not success.
3990     (gnus-group-position-cursor)))
3991
3992 (defun gnus-group-enter-server-mode ()
3993   "Jump to the server buffer."
3994   (interactive)
3995   (gnus-server-setup-buffer)
3996   (gnus-configure-windows 'server)
3997   (gnus-server-prepare))
3998
3999 (defun gnus-group-make-group (name method &optional address)
4000   "Add a new newsgroup.
4001 The user will be prompted for a NAME, for a select METHOD, and an
4002 ADDRESS."
4003   (interactive
4004    (cons 
4005     (read-string "Group name: ")
4006     (let ((method
4007            (completing-read 
4008             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4009             nil t)))
4010       (if (assoc method gnus-valid-select-methods)
4011           (list method
4012                 (if (memq 'prompt-address
4013                           (assoc method gnus-valid-select-methods))
4014                     (read-string "Address: ")
4015                   ""))
4016         (list method nil)))))
4017   
4018   (let* ((meth (if address (list (intern method) address) method))
4019          (nname (gnus-group-prefixed-name name meth))
4020          info)
4021     (and (gnus-gethash nname gnus-newsrc-hashtb)
4022          (error "Group %s already exists" nname))
4023     (gnus-group-change-level 
4024      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4025      gnus-level-default-subscribed gnus-level-killed 
4026      (and (gnus-group-group-name)
4027           (gnus-gethash (gnus-group-group-name)
4028                         gnus-newsrc-hashtb))
4029      t)
4030     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4031     (gnus-dribble-enter 
4032      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4033     (gnus-group-insert-group-line-info nname)
4034
4035     (if (assoc method gnus-valid-select-methods)
4036         (require (intern method)))
4037     (and (gnus-check-backend-function 'request-create-group nname)
4038          (gnus-request-create-group nname))))
4039
4040 (defun gnus-group-edit-group (group &optional part)
4041   "Edit the group on the current line."
4042   (interactive (list (gnus-group-group-name)))
4043   (let ((done-func '(lambda () 
4044                       "Exit editing mode and update the information."
4045                       (interactive)
4046                       (gnus-group-edit-group-done 'part 'group)))
4047         (part (or part 'info))
4048         (winconf (current-window-configuration))
4049         info)
4050     (or group (error "No group on current line"))
4051     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4052         (error "Killed group; can't be edited"))
4053     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4054     (gnus-configure-windows 'edit-group)
4055     (gnus-add-current-to-buffer-list)
4056     (emacs-lisp-mode)
4057     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4058     (use-local-map (copy-keymap emacs-lisp-mode-map))
4059     (local-set-key "\C-c\C-c" done-func)
4060     (make-local-variable 'gnus-prev-winconf)
4061     (setq gnus-prev-winconf winconf)
4062     ;; We modify the func to let it know what part it is editing.
4063     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4064     (setcar (cdr (cdr (nth 4 done-func))) group)
4065     (erase-buffer)
4066     (insert
4067      (cond 
4068       ((eq part 'method)
4069        ";; Type `C-c C-c' after editing the select method.\n\n")
4070       ((eq part 'params)
4071        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4072       ((eq part 'info)
4073        ";; Type `C-c C-c' after editing the group info.\n\n")))
4074     (let ((cinfo (gnus-copy-sequence info))
4075           marked)
4076       (if (not (setq marked (nth 3 cinfo)))
4077           ()
4078         (while marked
4079           (or (eq 'score (car (car marked)))
4080               (eq 'bookmark (car (car marked)))
4081               (eq 'killed (car (car marked)))
4082               (not (numberp (car (cdr (car marked)))))
4083               (setcdr (car marked) 
4084                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4085           (setq marked (cdr marked))))
4086       (insert 
4087        (pp-to-string
4088         (cond ((eq part 'method)
4089                (or (nth 4 info) "native"))
4090               ((eq part 'params)
4091                (nth 5 info))
4092               (t
4093                cinfo)))
4094        "\n"))))
4095
4096 (defun gnus-group-edit-group-method (group)
4097   "Edit the select method of GROUP."
4098   (interactive (list (gnus-group-group-name)))
4099   (gnus-group-edit-group group 'method))
4100
4101 (defun gnus-group-edit-group-parameters (group)
4102   "Edit the group parameters of GROUP."
4103   (interactive (list (gnus-group-group-name)))
4104   (gnus-group-edit-group group 'params))
4105
4106 (defun gnus-group-edit-group-done (part group)
4107   "Get info from buffer, update variables and jump to the group buffer."
4108   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4109   (goto-char (point-min))
4110   (let ((form (read (current-buffer)))
4111         (winconf gnus-prev-winconf))
4112     (if (eq part 'info) 
4113         (gnus-group-set-info form)
4114       (gnus-group-set-info form group part))
4115     (kill-buffer (current-buffer))
4116     (and winconf (set-window-configuration winconf))
4117     (set-buffer gnus-group-buffer)
4118     (gnus-group-update-group (gnus-group-group-name))
4119     (gnus-group-position-cursor)))
4120
4121 (defun gnus-group-make-help-group ()
4122   "Create the (ding) Gnus documentation group."
4123   (interactive)
4124   (let ((path load-path)
4125         name)
4126     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4127                                    "gnus-help" '(nndoc "gnus-help")))
4128                        gnus-newsrc-hashtb)
4129          (error "Documentation group already exists"))
4130     (while (and path
4131                 (not (file-exists-p (concat (file-name-as-directory (car path))
4132                                             "doc.txt"))))
4133       (setq path (cdr path)))
4134     (or path (error "Couldn't find doc group"))
4135     (gnus-group-make-group 
4136      (gnus-group-real-name name)
4137      (list 'nndoc name
4138            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4139            (list 'nndoc-article-type 'mbox))))
4140   (gnus-group-position-cursor))
4141
4142 (defun gnus-group-make-doc-group (file type)
4143   "Create a group that uses a single file as the source."
4144   (interactive 
4145    (list (read-file-name "File name: ") 
4146          (let ((err "")
4147                found char)
4148            (while (not found)
4149              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4150              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4151                                ((= char ?b) 'babyl)
4152                                ((= char ?d) 'digest)
4153                                (t (setq err (format "%c unknown. " char))
4154                                   nil))))
4155            found)))
4156   (let* ((file (expand-file-name file))
4157          (name (gnus-generate-new-group-name
4158                 (gnus-group-prefixed-name
4159                  (file-name-nondirectory file) '(nndoc "")))))
4160     (gnus-group-make-group 
4161      (gnus-group-real-name name)
4162      (list 'nndoc name
4163            (list 'nndoc-address file)
4164            (list 'nndoc-article-type type)))))
4165
4166 (defun gnus-group-make-archive-group ()
4167   "Create the (ding) Gnus archive group."
4168   (interactive)
4169   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4170                      gnus-newsrc-hashtb)
4171        (error "Archive group already exists"))
4172   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4173   (gnus-group-position-cursor))
4174
4175 (defun gnus-group-make-directory-group (dir)
4176   "Create an nndir group.
4177 The user will be prompted for a directory. The contents of this
4178 directory will be used as a newsgroup. The directory should contain
4179 mail messages or news articles in files that have numeric names."
4180   (interactive
4181    (list (read-file-name "Create group from directory: ")))
4182   (or (file-exists-p dir) (error "No such directory"))
4183   (or (file-directory-p dir) (error "Not a directory"))
4184   (gnus-group-make-group dir "nndir" dir)
4185   (gnus-group-position-cursor))
4186
4187 (defun gnus-group-make-kiboze-group (group address scores)
4188   "Create an nnkiboze group.
4189 The user will be prompted for a name, a regexp to match groups, and
4190 score file entries for articles to include in the group."
4191   (interactive
4192    (list
4193     (read-string "nnkiboze group name: ")
4194     (read-string "Source groups (regexp): ")
4195     (let ((headers (mapcar (lambda (group) (list group))
4196                            '("subject" "from" "number" "date" "message-id"
4197                              "references" "chars" "lines" "xref")))
4198           scores header regexp regexps)
4199       (while (not (equal "" (setq header (completing-read 
4200                                           "Match on header: " headers nil t))))
4201         (setq regexps nil)
4202         (while (not (equal "" (setq regexp (read-string 
4203                                             (format "Match on %s (string): "
4204                                                     header)))))
4205           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4206         (setq scores (cons (cons header regexps) scores)))
4207       scores)))
4208   (gnus-group-make-group group "nnkiboze" address)
4209   (save-excursion
4210     (gnus-set-work-buffer)
4211     (let (emacs-lisp-mode-hook)
4212       (pp scores (current-buffer)))
4213     (write-region (point-min) (point-max) 
4214                   (concat (or gnus-kill-files-directory "~/News")
4215                           "nnkiboze:" group "." gnus-score-file-suffix)))
4216   (gnus-group-position-cursor))
4217
4218 (defun gnus-group-add-to-virtual (n vgroup)
4219   "Add the current group to a virtual group."
4220   (interactive
4221    (list current-prefix-arg
4222          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4223                           "nnvirtual:")))
4224   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4225       (error "%s is not an nnvirtual group" vgroup))
4226   (let* ((groups (gnus-group-process-prefix n))
4227          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4228     (setcar (cdr method)
4229             (concat 
4230              (nth 1 method) "\\|"
4231              (mapconcat 
4232               (lambda (s) 
4233                 (gnus-group-remove-mark s)
4234                 (concat "\\(^" (regexp-quote s) "$\\)"))
4235               groups "\\|"))))
4236   (gnus-group-position-cursor))
4237
4238 (defun gnus-group-make-empty-virtual (group)
4239   "Create a new, fresh, empty virtual group."
4240   (interactive "sCreate new, empty virtual group: ")
4241   (let* ((method (list 'nnvirtual "^$"))
4242          (pgroup (gnus-group-prefixed-name group method)))
4243     ;; Check whether it exists already.
4244     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4245          (error "Group %s already exists." pgroup))
4246     ;; Subscribe the new group after the group on the current line.
4247     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4248     (gnus-group-update-group pgroup)
4249     (forward-line -1)
4250     (gnus-group-position-cursor)))
4251
4252 (defun gnus-group-enter-directory (dir)
4253   "Enter an ephemeral nneething group."
4254   (interactive "DDirectory to read: ")
4255   (let* ((method (list 'nneething dir))
4256          (leaf (gnus-group-prefixed-name
4257                 (file-name-nondirectory (directory-file-name dir))
4258                 method))
4259          (name (gnus-generate-new-group-name leaf)))
4260     (let ((nneething-read-only t))
4261       (or (gnus-group-read-ephemeral-group 
4262            name method t
4263            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4264                                       'summary 'group)))
4265           (error "Couldn't enter %s" dir)))))
4266
4267 ;; Group sorting commands
4268 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4269
4270 (defun gnus-group-sort-groups ()
4271   "Sort the group buffer using `gnus-group-sort-function'."
4272   (interactive)
4273   (setq gnus-newsrc-alist 
4274         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4275   (gnus-make-hashtable-from-newsrc-alist)
4276   (gnus-group-list-groups))
4277
4278 (defun gnus-group-sort-by-alphabet (info1 info2)
4279   (string< (car info1) (car info2)))
4280
4281 (defun gnus-group-sort-by-unread (info1 info2)
4282   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4283         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4284     (< (or (and (numberp n1) n1) 0)
4285        (or (and (numberp n2) n2) 0))))
4286
4287 (defun gnus-group-sort-by-level (info1 info2)
4288   (< (nth 1 info1) (nth 1 info2)))
4289
4290 ;; Group catching up.
4291
4292 (defun gnus-group-catchup-current (&optional n all)
4293   "Mark all articles not marked as unread in current newsgroup as read.
4294 If prefix argument N is numeric, the ARG next newsgroups will be
4295 caught up. If ALL is non-nil, marked articles will also be marked as
4296 read. Cross references (Xref: header) of articles are ignored.
4297 The difference between N and actual number of newsgroups that were
4298 caught up is returned."
4299   (interactive "P")
4300   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4301                gnus-expert-user
4302                (gnus-y-or-n-p
4303                 (if all
4304                     "Do you really want to mark all articles as read? "
4305                   "Mark all unread articles as read? "))))
4306       n
4307     (let ((groups (gnus-group-process-prefix n))
4308           (ret 0))
4309       (while groups
4310         ;; Virtual groups have to be given special treatment. 
4311         (let ((method (gnus-find-method-for-group (car groups))))
4312           (if (eq 'nnvirtual (car method))
4313               (nnvirtual-catchup-group
4314                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4315         (gnus-group-remove-mark (car groups))
4316         (if (prog1
4317                 (gnus-group-goto-group (car groups))
4318               (gnus-group-catchup (car groups) all))
4319             (gnus-group-update-group-line)
4320           (setq ret (1+ ret)))
4321         (setq groups (cdr groups)))
4322       (gnus-group-next-unread-group 1)
4323       ret)))
4324
4325 (defun gnus-group-catchup-current-all (&optional n)
4326   "Mark all articles in current newsgroup as read.
4327 Cross references (Xref: header) of articles are ignored."
4328   (interactive "P")
4329   (gnus-group-catchup-current n 'all))
4330
4331 (defun gnus-group-catchup (group &optional all)
4332   "Mark all articles in GROUP as read.
4333 If ALL is non-nil, all articles are marked as read.
4334 The return value is the number of articles that were marked as read,
4335 or nil if no action could be taken."
4336   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4337          (num (car entry))
4338          (marked (nth 3 (nth 2 entry))))
4339     (if (not (numberp (car entry)))
4340         (gnus-message 1 "Can't catch up; non-active group")
4341       ;; Do the updating only if the newsgroup isn't killed.
4342       (if (not entry)
4343           ()
4344         (gnus-update-read-articles 
4345          group (and (not all) (append (cdr (assq 'tick marked))
4346                                       (cdr (assq 'dormant marked))))
4347          nil (and (not all) (cdr (assq 'tick marked))))
4348         (and all 
4349              (setq marked (nth 3 (nth 2 entry)))
4350              (setcar (nthcdr 3 (nth 2 entry)) 
4351                      (delq (assq 'dormant marked) 
4352                            (nth 3 (nth 2 entry)))))))
4353     num))
4354
4355 (defun gnus-group-expire-articles (&optional n)
4356   "Expire all expirable articles in the current newsgroup."
4357   (interactive "P")
4358   (let ((groups (gnus-group-process-prefix n))
4359         group)
4360     (or groups (error "No groups to expire"))
4361     (while groups
4362       (setq group (car groups)
4363             groups (cdr groups))
4364       (gnus-group-remove-mark group)
4365       (if (not (gnus-check-backend-function 'request-expire-articles group))
4366           ()
4367         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4368                (expirable (if (memq 'total-expire (nth 5 info))
4369                               (cons nil (gnus-list-of-read-articles group))
4370                             (assq 'expire (nth 3 info)))))
4371           (and expirable 
4372                (setcdr expirable
4373                        (gnus-request-expire-articles 
4374                         (cdr expirable) group))))))))
4375
4376 (defun gnus-group-expire-all-groups ()
4377   "Expire all expirable articles in all newsgroups."
4378   (interactive)
4379   (save-excursion
4380     (gnus-message 5 "Expiring...")
4381     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4382                                      (cdr gnus-newsrc-alist))))
4383       (gnus-group-expire-articles nil)))
4384   (gnus-group-position-cursor)
4385   (gnus-message 5 "Expiring...done"))
4386
4387 (defun gnus-group-set-current-level (n level)
4388   "Set the level of the next N groups to LEVEL."
4389   (interactive "P\nnLevel: ")
4390   (or (and (>= level 1) (<= level gnus-level-killed))
4391       (error "Illegal level: %d" level))
4392   (let ((groups (gnus-group-process-prefix n))
4393         group)
4394     (while groups
4395       (setq group (car groups)
4396             groups (cdr groups))
4397       (gnus-group-remove-mark group)
4398       (gnus-message 6 "Changed level of %s from %d to %d" 
4399                     group (gnus-group-group-level) level)
4400       (gnus-group-change-level group level
4401                                (gnus-group-group-level))
4402       (gnus-group-update-group-line)))
4403   (gnus-group-position-cursor))
4404
4405 (defun gnus-group-unsubscribe-current-group (&optional n)
4406   "Toggle subscription of the current group.
4407 If given numerical prefix, toggle the N next groups."
4408   (interactive "P")
4409   (let ((groups (gnus-group-process-prefix n))
4410         group)
4411     (while groups
4412       (setq group (car groups)
4413             groups (cdr groups))
4414       (gnus-group-remove-mark group)
4415       (gnus-group-unsubscribe-group
4416        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4417                  gnus-level-default-unsubscribed
4418                gnus-level-default-subscribed))
4419       (gnus-group-update-group-line))
4420     (gnus-group-next-group 1)))
4421
4422 (defun gnus-group-unsubscribe-group (group &optional level)
4423   "Toggle subscribe from/to unsubscribe GROUP.
4424 New newsgroup is added to .newsrc automatically."
4425   (interactive
4426    (list (completing-read
4427           "Group: " gnus-active-hashtb nil 
4428           (memq gnus-select-method gnus-have-read-active-file))))
4429   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4430     (cond (newsrc
4431            ;; Toggle subscription flag.
4432            (gnus-group-change-level 
4433             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4434                                            gnus-level-subscribed) 
4435                                        (1+ gnus-level-subscribed)
4436                                      gnus-level-default-subscribed)))
4437            (gnus-group-update-group group))
4438           ((and (stringp group)
4439                 (or (not (memq gnus-select-method gnus-have-read-active-file))
4440                     (gnus-gethash group gnus-active-hashtb)))
4441            ;; Add new newsgroup.
4442            (gnus-group-change-level 
4443             group 
4444             (if level level gnus-level-default-subscribed) 
4445             (or (and (member group gnus-zombie-list) 
4446                      gnus-level-zombie) 
4447                 gnus-level-killed)
4448             (and (gnus-group-group-name)
4449                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4450            (gnus-group-update-group group))
4451           (t (error "No such newsgroup: %s" group)))
4452     (gnus-group-position-cursor)))
4453
4454 (defun gnus-group-transpose-groups (n)
4455   "Move the current newsgroup up N places.
4456 If given a negative prefix, move down instead. The difference between
4457 N and the number of steps taken is returned." 
4458   (interactive "p")
4459   (or (gnus-group-group-name)
4460       (error "No group on current line"))
4461   (gnus-group-kill-group 1)
4462   (prog1
4463       (forward-line (- n))
4464     (gnus-group-yank-group)
4465     (gnus-group-position-cursor)))
4466
4467 (defun gnus-group-kill-all-zombies ()
4468   "Kill all zombie newsgroups."
4469   (interactive)
4470   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4471   (setq gnus-zombie-list nil)
4472   (gnus-group-list-groups))
4473
4474 (defun gnus-group-kill-region (begin end)
4475   "Kill newsgroups in current region (excluding current point).
4476 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4477   (interactive "r")
4478   (let ((lines
4479          ;; Count lines.
4480          (save-excursion
4481            (count-lines
4482             (progn
4483               (goto-char begin)
4484               (beginning-of-line)
4485               (point))
4486             (progn
4487               (goto-char end)
4488               (beginning-of-line)
4489               (point))))))
4490     (goto-char begin)
4491     (beginning-of-line)                 ;Important when LINES < 1
4492     (gnus-group-kill-group lines)))
4493
4494 (defun gnus-group-kill-group (&optional n)
4495   "The the next N groups.
4496 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4497 However, only groups that were alive can be yanked; already killed 
4498 groups or zombie groups can't be yanked.
4499 The return value is the name of the (last) group that was killed."
4500   (interactive "P")
4501   (let ((buffer-read-only nil)
4502         (groups (gnus-group-process-prefix n))
4503         group entry level)
4504     (while groups
4505       (setq group (car groups)
4506             groups (cdr groups))
4507       (gnus-group-remove-mark group)
4508       (setq level (gnus-group-group-level))
4509       (gnus-delete-line)
4510       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4511           (setq gnus-list-of-killed-groups 
4512                 (cons (cons (car entry) (nth 2 entry)) 
4513                       gnus-list-of-killed-groups)))
4514       (gnus-group-change-level 
4515        (if entry entry group) gnus-level-killed (if entry nil level)))
4516     (gnus-group-position-cursor)
4517     group))
4518
4519 (defun gnus-group-yank-group (&optional arg)
4520   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4521 inserting it before the current newsgroup.  The numeric ARG specifies
4522 how many newsgroups are to be yanked.  The name of the (last)
4523 newsgroup yanked is returned."
4524   (interactive "p")
4525   (if (not arg) (setq arg 1))
4526   (let (info group prev)
4527     (while (>= (setq arg (1- arg)) 0)
4528       (if (not (setq info (car gnus-list-of-killed-groups)))
4529           (error "No more newsgroups to yank"))
4530       (setq group (nth 2 info))
4531       ;; Find which newsgroup to insert this one before - search
4532       ;; backward until something suitable is found. If there are no
4533       ;; other newsgroups in this buffer, just make this newsgroup the
4534       ;; first newsgroup.
4535       (setq prev (gnus-group-group-name))
4536       (gnus-group-change-level 
4537        info (nth 2 info) gnus-level-killed 
4538        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4539        t)
4540       (gnus-group-insert-group-line-info (nth 1 info))
4541       (setq gnus-list-of-killed-groups 
4542             (cdr gnus-list-of-killed-groups)))
4543     (forward-line -1)
4544     (gnus-group-position-cursor)
4545     group))
4546       
4547 (defun gnus-group-list-all-groups (&optional arg)
4548   "List all newsgroups with level ARG or lower.
4549 Default is gnus-level-unsubscribed, which lists all subscribed and most
4550 unsubscribed groups."
4551   (interactive "P")
4552   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4553
4554 (defun gnus-group-list-killed ()
4555   "List all killed newsgroups in the group buffer."
4556   (interactive)
4557   (if (not gnus-killed-list)
4558       (gnus-message 6 "No killed groups")
4559     (let (gnus-group-list-mode)
4560       (funcall gnus-group-prepare-function 
4561                gnus-level-killed t gnus-level-killed))
4562     (goto-char (point-min)))
4563   (gnus-group-position-cursor))
4564
4565 (defun gnus-group-list-zombies ()
4566   "List all zombie newsgroups in the group buffer."
4567   (interactive)
4568   (if (not gnus-zombie-list)
4569       (gnus-message 6 "No zombie groups")
4570     (let (gnus-group-list-mode)
4571       (funcall gnus-group-prepare-function
4572                gnus-level-zombie t gnus-level-zombie))
4573     (goto-char (point-min)))
4574   (gnus-group-position-cursor))
4575
4576 (defun gnus-group-get-new-news (&optional arg)
4577   "Get newly arrived articles.
4578 If ARG is non-nil, it should be a number between one and nine to
4579 specify which levels you are interested in re-scanning."
4580   (interactive "P")
4581   (run-hooks 'gnus-get-new-news-hook)
4582   (setq arg (gnus-group-default-level arg t))
4583   (if (and gnus-read-active-file (not arg))
4584       (progn
4585         (gnus-read-active-file)
4586         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4587     (let ((gnus-read-active-file (not arg))
4588           (gnus-have-read-active-file 
4589            (and (not arg) gnus-have-read-active-file)))
4590       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4591   (gnus-group-list-groups))
4592
4593 (defun gnus-group-get-new-news-this-group (&optional n)
4594   "Check for newly arrived news in the current group (and the N-1 next groups).
4595 The difference between N and the number of newsgroup checked is returned.
4596 If N is negative, this group and the N-1 previous groups will be checked."
4597   (interactive "P")
4598   (let* ((groups (gnus-group-process-prefix n))
4599          (ret (if (numberp n) (- n (length groups)) 0))
4600          group)
4601     (while groups
4602       (setq group (car groups)
4603             groups (cdr groups))
4604       (gnus-group-remove-mark group)
4605       (or (gnus-get-new-news-in-group group)
4606           (progn 
4607             (ding) 
4608             (message "%s error: %s" group (gnus-status-message group))
4609             (sit-for 2))))
4610     (gnus-group-next-unread-group 1 t)
4611     (gnus-summary-position-cursor)
4612     ret))
4613
4614 (defun gnus-get-new-news-in-group (group)
4615   (and group 
4616        (gnus-activate-group group)
4617        (progn
4618          (gnus-get-unread-articles-in-group 
4619           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4620           (gnus-gethash group gnus-active-hashtb))
4621          (gnus-group-update-group-line)
4622          t)))
4623
4624 (defun gnus-group-fetch-faq (group)
4625   "Fetch the FAQ for the current group."
4626   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4627   (or group (error "No group name given"))
4628   (let ((file (concat gnus-group-faq-directory group))) 
4629     (if (not (file-exists-p file))
4630         (error "No such file: %s" file)
4631       (find-file file))))
4632   
4633 (defun gnus-group-describe-group (force &optional group)
4634   "Display a description of the current newsgroup."
4635   (interactive (list current-prefix-arg (gnus-group-group-name)))
4636   (and force (setq gnus-description-hashtb nil))
4637   (let ((method (gnus-find-method-for-group group))
4638         desc)
4639     (or group (error "No group name given"))
4640     (and (or (and gnus-description-hashtb
4641                   ;; We check whether this group's method has been
4642                   ;; queried for a description file.  
4643                   (gnus-gethash 
4644                    (gnus-group-prefixed-name "" method) 
4645                    gnus-description-hashtb))
4646              (setq desc (gnus-group-get-description group))
4647              (gnus-read-descriptions-file method))
4648          (message
4649           (or desc (gnus-gethash group gnus-description-hashtb)
4650               "No description available")))))
4651
4652 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4653 (defun gnus-group-describe-all-groups (&optional force)
4654   "Pop up a buffer with descriptions of all newsgroups."
4655   (interactive "P")
4656   (and force (setq gnus-description-hashtb nil))
4657   (if (not (or gnus-description-hashtb
4658                (gnus-read-all-descriptions-files)))
4659       (error "Couldn't request descriptions file"))
4660   (let ((buffer-read-only nil)
4661         b)
4662     (erase-buffer)
4663     (mapatoms
4664      (lambda (group)
4665        (setq b (point))
4666        (insert (format "      *: %-20s %s\n" (symbol-name group)
4667                        (symbol-value group)))
4668        (add-text-properties 
4669         b (1+ b) (list 'gnus-group group
4670                        'gnus-unread t 'gnus-marked nil
4671                        'gnus-level (1+ gnus-level-subscribed))))
4672      gnus-description-hashtb)
4673     (goto-char (point-min))
4674     (gnus-group-position-cursor)))
4675
4676 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4677 (defun gnus-group-apropos (regexp &optional search-description)
4678   "List all newsgroups that have names that match a regexp."
4679   (interactive "sGnus apropos (regexp): ")
4680   (let ((prev "")
4681         (obuf (current-buffer))
4682         groups des)
4683     ;; Go through all newsgroups that are known to Gnus.
4684     (mapatoms 
4685      (lambda (group)
4686        (and (symbol-name group)
4687             (string-match regexp (symbol-name group))
4688             (setq groups (cons (symbol-name group) groups))))
4689      gnus-active-hashtb)
4690     ;; Go through all descriptions that are known to Gnus. 
4691     (if search-description
4692         (mapatoms 
4693          (lambda (group)
4694            (and (string-match regexp (symbol-value group))
4695                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4696                 (setq groups (cons (symbol-name group) groups))))
4697          gnus-description-hashtb))
4698     (if (not groups)
4699         (gnus-message 3 "No groups matched \"%s\"." regexp)
4700       ;; Print out all the groups.
4701       (save-excursion
4702         (pop-to-buffer "*Gnus Help*")
4703         (buffer-disable-undo (current-buffer))
4704         (erase-buffer)
4705         (setq groups (sort groups 'string<))
4706         (while groups
4707           ;; Groups may be entered twice into the list of groups.
4708           (if (not (string= (car groups) prev))
4709               (progn
4710                 (insert (setq prev (car groups)) "\n")
4711                 (if (and gnus-description-hashtb
4712                          (setq des (gnus-gethash (car groups) 
4713                                                  gnus-description-hashtb)))
4714                     (insert "  " des "\n"))))
4715           (setq groups (cdr groups)))
4716         (goto-char (point-min))))
4717     (pop-to-buffer obuf)))
4718
4719 (defun gnus-group-description-apropos (regexp)
4720   "List all newsgroups that have names or descriptions that match a regexp."
4721   (interactive "sGnus description apropos (regexp): ")
4722   (if (not (or gnus-description-hashtb
4723                (gnus-read-all-descriptions-files)))
4724       (error "Couldn't request descriptions file"))
4725   (gnus-group-apropos regexp t))
4726
4727 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4728 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4729   "List all groups with unread articles that match REGEXP.
4730 If the prefix LEVEL is non-nil, it should be a number that says which
4731 level to cut off listing groups. 
4732 If ALL, also list groups with no unread articles.
4733 If LOWEST, don't list groups with level lower than LOWEST."
4734   (interactive "P\nsList newsgroups matching: ")
4735   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4736                            all (or lowest 1) regexp)
4737   (goto-char (point-min))
4738   (gnus-group-position-cursor))
4739
4740 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4741   "List all groups that match REGEXP.
4742 If the prefix LEVEL is non-nil, it should be a number that says which
4743 level to cut off listing groups. 
4744 If LOWEST, don't list groups with level lower than LOWEST."
4745   (interactive "P\nsList newsgroups matching: ")
4746   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4747
4748 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4749 (defun gnus-group-save-newsrc ()
4750   "Save the Gnus startup files."
4751   (interactive)
4752   (gnus-save-newsrc-file))
4753
4754 (defun gnus-group-restart (&optional arg)
4755   "Force Gnus to read the .newsrc file."
4756   (interactive "P")
4757   (gnus-save-newsrc-file)
4758   (gnus-setup-news 'force)
4759   (gnus-group-list-groups arg))
4760
4761 (defun gnus-group-read-init-file ()
4762   "Read the Gnus elisp init file."
4763   (interactive)
4764   (gnus-read-init-file))
4765
4766 (defun gnus-group-check-bogus-groups (&optional silent)
4767   "Check bogus newsgroups.
4768 If given a prefix, don't ask for confirmation before removing a bogus
4769 group."
4770   (interactive "P")
4771   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4772   (gnus-group-list-groups))
4773
4774 (defun gnus-group-edit-global-kill (&optional article group)
4775   "Edit the global kill file.
4776 If GROUP, edit that local kill file instead."
4777   (interactive "P")
4778   (setq gnus-current-kill-article article)
4779   (gnus-kill-file-edit-file group)
4780   (gnus-message 6
4781    (substitute-command-keys
4782     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4783
4784 (defun gnus-group-edit-local-kill (article group)
4785   "Edit a local kill file."
4786   (interactive (list nil (gnus-group-group-name)))
4787   (gnus-group-edit-global-kill article group))
4788
4789 (defun gnus-group-force-update ()
4790   "Update `.newsrc' file."
4791   (interactive)
4792   (gnus-save-newsrc-file))
4793
4794 (defun gnus-group-suspend ()
4795   "Suspend the current Gnus session.
4796 In fact, cleanup buffers except for group mode buffer.
4797 The hook gnus-suspend-gnus-hook is called before actually suspending."
4798   (interactive)
4799   (run-hooks 'gnus-suspend-gnus-hook)
4800   ;; Kill Gnus buffers except for group mode buffer.
4801   (let ((group-buf (get-buffer gnus-group-buffer)))
4802     ;; Do this on a separate list in case the user does a ^G before we finish
4803     (let ((gnus-buffer-list
4804            (delq group-buf (delq gnus-dribble-buffer
4805                                  (append gnus-buffer-list nil)))))
4806       (while gnus-buffer-list
4807         (gnus-kill-buffer (car gnus-buffer-list))
4808         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4809     (if group-buf
4810         (progn
4811           (setq gnus-buffer-list (list group-buf))
4812           (bury-buffer group-buf)
4813           (delete-windows-on group-buf t)))))
4814
4815 (defun gnus-group-clear-dribble ()
4816   "Clear all information from the dribble buffer."
4817   (interactive)
4818   (gnus-dribble-clear))
4819
4820 (defun gnus-group-exit ()
4821   "Quit reading news after updating .newsrc.eld and .newsrc.
4822 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4823   (interactive)
4824   (if (or noninteractive                ;For gnus-batch-kill
4825           (zerop (buffer-size))         ;No news is good news.
4826           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4827           (not gnus-interactive-exit)   ;Without confirmation
4828           gnus-expert-user
4829           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4830       (progn
4831         (run-hooks 'gnus-exit-gnus-hook)
4832         ;; Offer to save data from non-quitted summary buffers.
4833         (gnus-offer-save-summaries)
4834         ;; Save the newsrc file(s).
4835         (gnus-save-newsrc-file)
4836         ;; Kill-em-all.
4837         (gnus-close-backends)
4838         ;; Reset everything.
4839         (gnus-clear-system))))
4840
4841 (defun gnus-close-backends ()
4842   ;; Send a close request to all backends that support such a request. 
4843   (let ((methods gnus-valid-select-methods)
4844         func)
4845     (while methods
4846       (if (fboundp (setq func (intern (concat (car (car methods))
4847                                               "-request-close"))))
4848           (funcall func))
4849       (setq methods (cdr methods)))))
4850
4851 (defun gnus-group-quit ()
4852   "Quit reading news without updating .newsrc.eld or .newsrc.
4853 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4854   (interactive)
4855   (if (or noninteractive                ;For gnus-batch-kill
4856           (zerop (buffer-size))
4857           (not (gnus-server-opened gnus-select-method))
4858           gnus-expert-user
4859           (not gnus-current-startup-file)
4860           (gnus-yes-or-no-p
4861            (format "Quit reading news without saving %s? "
4862                    (file-name-nondirectory gnus-current-startup-file))))
4863       (progn
4864         (run-hooks 'gnus-exit-gnus-hook)
4865         (if gnus-use-full-window
4866             (delete-other-windows)
4867           (gnus-remove-some-windows))
4868         (gnus-dribble-save)
4869         (gnus-close-backends)
4870         (gnus-clear-system))))
4871
4872 (defun gnus-offer-save-summaries ()
4873   (let ((buffers (buffer-list))
4874         answer)
4875     (save-excursion
4876       (while (and buffers (not (eq answer ?q)))
4877         (and 
4878          ;; We look for buffers with "Summary" in the name.
4879          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4880          (progn
4881            (set-buffer (car buffers))
4882            ;; We check that this is, indeed, a summary buffer.
4883            (eq major-mode 'gnus-summary-mode)) 
4884          ;; We ask the user whether she wants to save the info.
4885          (or (eq answer ?!)
4886              (progn
4887                (setq answer nil)
4888                (while (not (memq answer '(?y ?n ?! ?q)))
4889                  (message (format "%sUpdate summary buffer %s? (y, n, !, q)"
4890                                   (if answer "Illegal char. " "")
4891                                   (buffer-name)))
4892                  (setq answer (read-char)))
4893                (or (eq answer ?y) (eq answer ?!))))
4894          ;; We do it by simply exiting.
4895          (gnus-summary-exit))
4896         (setq buffers (cdr buffers))))))
4897
4898 (defun gnus-group-describe-briefly ()
4899   "Give a one line description of the group mode commands."
4900   (interactive)
4901   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
4902
4903 (defun gnus-group-browse-foreign-server (method)
4904   "Browse a foreign news server.
4905 If called interactively, this function will ask for a select method
4906  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4907 If not, METHOD should be a list where the first element is the method
4908 and the second element is the address."
4909   (interactive
4910    (list (let ((how (completing-read 
4911                      "Which backend: "
4912                      (append gnus-valid-select-methods gnus-server-alist)
4913                      nil t "nntp")))
4914            ;; We either got a backend name or a virtual server name.
4915            ;; If the first, we also need an address.
4916            (if (assoc how gnus-valid-select-methods)
4917                (list (intern how)
4918                      ;; Suggested by mapjph@bath.ac.uk.
4919                      (completing-read 
4920                       "Address: " 
4921                       (mapcar (lambda (server) (list server))
4922                               gnus-secondary-servers)))
4923              ;; We got a server name, so we find the method.
4924              (gnus-server-to-method how)))))
4925   (gnus-browse-foreign-server method))
4926
4927 \f
4928 ;;;
4929 ;;; Browse Server Mode
4930 ;;;
4931
4932 (defvar gnus-browse-mode-hook nil)
4933 (defvar gnus-browse-mode-map nil)
4934 (put 'gnus-browse-mode 'mode-class 'special)
4935
4936 (if gnus-browse-mode-map
4937     nil
4938   (setq gnus-browse-mode-map (make-keymap))
4939   (suppress-keymap gnus-browse-mode-map)
4940   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4941   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4942   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4943   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4944   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4945   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4946   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4947   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4948   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4949   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4950   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4951   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4952   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4953   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4954   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4955   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4956   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4957   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4958   )
4959
4960 (defvar gnus-browse-current-method nil)
4961 (defvar gnus-browse-return-buffer nil)
4962
4963 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4964
4965 (defun gnus-browse-foreign-server (method &optional return-buffer)
4966   (setq gnus-browse-current-method method)
4967   (setq gnus-browse-return-buffer return-buffer)
4968   (let ((gnus-select-method method)
4969         groups group)
4970     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4971     (or (gnus-check-server method)
4972         (error "Unable to contact server: %s" (gnus-status-message method)))
4973     (or (gnus-request-list method)
4974         (error "Couldn't request list: %s" (gnus-status-message method)))
4975     (get-buffer-create gnus-browse-buffer)
4976     (gnus-add-current-to-buffer-list)
4977     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4978     (gnus-configure-windows 'browse)
4979     (buffer-disable-undo (current-buffer))
4980     (let ((buffer-read-only nil))
4981       (erase-buffer))
4982     (gnus-browse-mode)
4983     (setq mode-line-buffer-identification
4984           (format
4985            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4986     (save-excursion
4987       (set-buffer nntp-server-buffer)
4988       (let ((cur (current-buffer)))
4989         (goto-char (point-min))
4990         (or (string= gnus-ignored-newsgroups "")
4991             (delete-matching-lines gnus-ignored-newsgroups))
4992         (while (re-search-forward 
4993                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4994           (goto-char (match-end 1))
4995           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4996                                                      (match-end 1))
4997                                    (max 0 (- (1+ (read cur)) (read cur))))
4998                              groups)))))
4999     (setq groups (sort groups 
5000                        (lambda (l1 l2)
5001                          (string< (car l1) (car l2)))))
5002     (let ((buffer-read-only nil))
5003       (while groups
5004         (setq group (car groups))
5005         (insert 
5006          (format "K%7d: %s\n" (cdr group) (car group)))
5007         (setq groups (cdr groups))))
5008     (switch-to-buffer (current-buffer))
5009     (goto-char (point-min))
5010     (gnus-group-position-cursor)))
5011
5012 (defun gnus-browse-mode ()
5013   "Major mode for browsing a foreign server.
5014
5015 All normal editing commands are switched off.
5016
5017 \\<gnus-browse-mode-map>
5018 The only things you can do in this buffer is
5019
5020 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5021 The group will be inserted into the group buffer upon exit from this
5022 buffer.  
5023
5024 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5025
5026 3) `\\[gnus-browse-exit]' to return to the group buffer."
5027   (interactive)
5028   (kill-all-local-variables)
5029   (if gnus-visual (gnus-browse-make-menu-bar))
5030   (setq mode-line-modified "-- ")
5031   (make-local-variable 'mode-line-format)
5032   (setq mode-line-format (copy-sequence mode-line-format))
5033   (and (equal (nth 3 mode-line-format) "   ")
5034        (setcar (nthcdr 3 mode-line-format) ""))
5035   (setq major-mode 'gnus-browse-mode)
5036   (setq mode-name "Browse Server")
5037   (setq mode-line-process nil)
5038   (use-local-map gnus-browse-mode-map)
5039   (buffer-disable-undo (current-buffer))
5040   (setq truncate-lines t)
5041   (setq buffer-read-only t)
5042   (run-hooks 'gnus-browse-mode-hook))
5043
5044 (defun gnus-browse-read-group (&optional no-article)
5045   "Enter the group at the current line."
5046   (interactive)
5047   (let ((group (gnus-browse-group-name)))
5048     (or (gnus-group-read-ephemeral-group 
5049          group gnus-browse-current-method nil
5050          (cons (current-buffer) 'browse))
5051         (error "Couldn't enter %s" group))))
5052
5053 (defun gnus-browse-select-group ()
5054   "Select the current group."
5055   (interactive)
5056   (gnus-browse-read-group 'no))
5057
5058 (defun gnus-browse-next-group (n)
5059   "Go to the next group."
5060   (interactive "p")
5061   (prog1
5062       (forward-line n)
5063     (gnus-group-position-cursor)))
5064
5065 (defun gnus-browse-prev-group (n)
5066   "Go to the next group."
5067   (interactive "p")
5068   (gnus-browse-next-group (- n)))
5069
5070 (defun gnus-browse-unsubscribe-current-group (arg)
5071   "(Un)subscribe to the next ARG groups."
5072   (interactive "p")
5073   (and (eobp)
5074        (error "No group at current line."))
5075   (let ((ward (if (< arg 0) -1 1))
5076         (arg (abs arg)))
5077     (while (and (> arg 0)
5078                 (not (eobp))
5079                 (gnus-browse-unsubscribe-group)
5080                 (zerop (gnus-browse-next-group ward)))
5081       (setq arg (1- arg)))
5082     (gnus-group-position-cursor)
5083     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5084     arg))
5085
5086 (defun gnus-browse-group-name ()
5087   (save-excursion
5088     (beginning-of-line)
5089     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5090         ()
5091       (gnus-group-prefixed-name 
5092        (buffer-substring (match-beginning 1) (match-end 1))
5093        gnus-browse-current-method))))
5094   
5095 (defun gnus-browse-unsubscribe-group ()
5096   (let ((sub nil)
5097         (buffer-read-only nil)
5098         group)
5099     (save-excursion
5100       (beginning-of-line)
5101       (if (= (following-char) ?K) (setq sub t))
5102       (setq group (gnus-browse-group-name))
5103       (beginning-of-line)
5104       (delete-char 1)
5105       (if sub
5106           (progn
5107             (gnus-group-change-level 
5108              (list t group gnus-level-default-subscribed
5109                    nil nil gnus-browse-current-method) 
5110              gnus-level-default-subscribed gnus-level-killed
5111              (and (car (nth 1 gnus-newsrc-alist))
5112                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5113                                 gnus-newsrc-hashtb))
5114              t)
5115             (insert ? ))
5116         (gnus-group-change-level 
5117          group gnus-level-killed gnus-level-default-subscribed)
5118         (insert ?K)))
5119     t))
5120
5121 (defun gnus-browse-exit ()
5122   "Quit browsing and return to the group buffer."
5123   (interactive)
5124   (if (eq major-mode 'gnus-browse-mode)
5125       (kill-buffer (current-buffer)))
5126   (if gnus-browse-return-buffer
5127       (gnus-configure-windows 'server 'force)
5128     (gnus-configure-windows 'group 'force)
5129     (gnus-group-list-groups nil)))
5130
5131 (defun gnus-browse-describe-briefly ()
5132   "Give a one line description of the group mode commands."
5133   (interactive)
5134   (gnus-message 6
5135    (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")))
5136       
5137 \f
5138 ;;;
5139 ;;; Gnus summary mode
5140 ;;;
5141
5142 (defvar gnus-summary-mode-map nil)
5143 (defvar gnus-summary-mark-map nil)
5144 (defvar gnus-summary-mscore-map nil)
5145 (defvar gnus-summary-article-map nil)
5146 (defvar gnus-summary-thread-map nil)
5147 (defvar gnus-summary-goto-map nil)
5148 (defvar gnus-summary-exit-map nil)
5149 (defvar gnus-summary-interest-map nil)
5150 (defvar gnus-summary-sort-map nil)
5151 (defvar gnus-summary-backend-map nil)
5152 (defvar gnus-summary-save-map nil)
5153 (defvar gnus-summary-wash-map nil)
5154 (defvar gnus-summary-wash-hide-map nil)
5155 (defvar gnus-summary-wash-highlight-map nil)
5156 (defvar gnus-summary-wash-time-map nil)
5157 (defvar gnus-summary-help-map nil)
5158
5159 (put 'gnus-summary-mode 'mode-class 'special)
5160
5161 (if gnus-summary-mode-map
5162     nil
5163   (setq gnus-summary-mode-map (make-keymap))
5164   (suppress-keymap gnus-summary-mode-map)
5165
5166   ;; Non-orthogonal keys
5167
5168   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5169   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5170   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5171   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5172   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5173   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5174   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5175   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5176   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5177   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5178   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5179   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5180   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5181   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5182   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5183   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5184   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5185   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5186   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5187   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5188   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5189   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5190   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5191   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5192   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5193   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5194   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5195   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5196   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5197   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5198   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5199   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5200   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5201   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5202   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5203   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5204   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5205   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5206   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5207   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5208   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5209   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5210   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5211   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5212   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5213   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5214   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5215   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5216   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5217   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5218   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5219   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5220   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5221   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5222   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5223   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5224   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5225   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5226   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5227   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5228   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5229   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5230   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5231   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5232   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5233   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5234   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5235   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5236   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5237   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5238   (define-key gnus-summary-mode-map "V" 'gnus-version)
5239   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5240   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5241   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5242   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5243   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5244   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5245   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5246   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5247 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5248   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5249   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5250   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5251 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5252   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5253   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5254   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5255   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5256   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5257
5258
5259   ;; Sort of orthogonal keymap
5260   (define-prefix-command 'gnus-summary-mark-map)
5261   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5262   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5263   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5264   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5265   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5266   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5267   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5268   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5269   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5270   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5271   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5272   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5273   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5274   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5275   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5276   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5277   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5278   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5279   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5280   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5281   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5282   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5283   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5284   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5285
5286   (define-prefix-command 'gnus-summary-mscore-map)
5287   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5288   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5289   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5290   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5291   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5292
5293   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5294   
5295   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5296   
5297   (define-prefix-command 'gnus-summary-goto-map)
5298   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5299   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5300   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5301   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5302   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5303   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5304   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5305   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5306   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5307   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5308   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5309   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5310   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5311   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5312
5313
5314   (define-prefix-command 'gnus-summary-thread-map)
5315   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5316   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5317   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5318   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5319   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5320   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5321   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5322   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5323   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5324   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5325   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5326   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5327   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5328   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5329
5330   
5331   (define-prefix-command 'gnus-summary-exit-map)
5332   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5333   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5334   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5335   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5336   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5337   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5338   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5339   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5340   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5341   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5342   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5343
5344
5345   (define-prefix-command 'gnus-summary-article-map)
5346   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5347   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5348   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5349   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5350   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5351   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5352   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5353   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5354   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5355   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5356   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5357   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5358   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5359   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5360
5361
5362
5363   (define-prefix-command 'gnus-summary-wash-map)
5364   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5365
5366   (define-prefix-command 'gnus-summary-wash-hide-map)
5367   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5368   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5369   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5370   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5371   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5372   (define-key gnus-summary-wash-hide-map "\C-c" 'gnus-article-hide-citation-maybe)
5373
5374   (define-prefix-command 'gnus-summary-wash-highlight-map)
5375   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5376   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5377   (define-key gnus-summary-wash-highlight-map "h" 'gnus-article-highlight-headers)
5378   (define-key gnus-summary-wash-highlight-map "c" 'gnus-article-highlight-citation)
5379   (define-key gnus-summary-wash-highlight-map "s" 'gnus-article-highlight-signature)
5380
5381   (define-prefix-command 'gnus-summary-wash-time-map)
5382   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5383   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5384   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5385   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5386   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5387
5388   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5389   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5390   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5391   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5392   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5393   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5394   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5395   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5396   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5397   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5398
5399
5400   (define-prefix-command 'gnus-summary-help-map)
5401   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5402   (define-key gnus-summary-help-map "v" 'gnus-version)
5403   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5404   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5405   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5406   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5407
5408
5409   (define-prefix-command 'gnus-summary-backend-map)
5410   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5411   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5412   (define-key gnus-summary-backend-map "\M-\C-e" 
5413     'gnus-summary-expire-articles-now)
5414   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5415   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5416   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5417   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5418   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5419   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5420   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5421
5422
5423   (define-prefix-command 'gnus-summary-save-map)
5424   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5425   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5426   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5427   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5428   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5429   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5430   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5431   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5432 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5433
5434   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5435
5436   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5437 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5438 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5439 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5440 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5441 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5442 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5443   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5444 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5445 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5446
5447   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5448
5449 ;  (define-prefix-command 'gnus-summary-sort-map)
5450 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5451 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5452 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5453 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5454 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5455 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5456
5457   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5458   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5459   )
5460
5461
5462 \f
5463
5464 (defun gnus-summary-mode (&optional group)
5465   "Major mode for reading articles.
5466
5467 All normal editing commands are switched off.
5468 \\<gnus-summary-mode-map>
5469 Each line in this buffer represents one article.  To read an
5470 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5471 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5472 respectively.
5473
5474 You can also post articles and send mail from this buffer.  To 
5475 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5476 of an article, type `\\[gnus-summary-reply]'.
5477
5478 There are approx. one gazillion commands you can execute in this 
5479 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5480
5481 The following commands are available:
5482
5483 \\{gnus-summary-mode-map}"
5484   (interactive)
5485   (if gnus-visual (gnus-summary-make-menu-bar))
5486   (kill-all-local-variables)
5487   (let ((locals gnus-summary-local-variables))
5488     (while locals
5489       (if (consp (car locals))
5490           (progn
5491             (make-local-variable (car (car locals)))
5492             (set (car (car locals)) (eval (cdr (car locals)))))
5493         (make-local-variable (car locals))
5494         (set (car locals) nil))
5495       (setq locals (cdr locals))))
5496   (gnus-make-thread-indent-array)
5497   (setq mode-line-modified "-- ")
5498   (make-local-variable 'mode-line-format)
5499   (setq mode-line-format (copy-sequence mode-line-format))
5500   (and (equal (nth 3 mode-line-format) "   ")
5501        (setcar (nthcdr 3 mode-line-format) ""))
5502   (setq major-mode 'gnus-summary-mode)
5503   (setq mode-name "Summary")
5504   (make-local-variable 'minor-mode-alist)
5505   (use-local-map gnus-summary-mode-map)
5506   (buffer-disable-undo (current-buffer))
5507   (setq buffer-read-only t)             ;Disable modification
5508   (setq truncate-lines t)
5509   (setq selective-display t)
5510   (setq selective-display-ellipses t)   ;Display `...'
5511   (setq buffer-display-table gnus-summary-display-table)
5512   (setq gnus-newsgroup-name group)
5513   (run-hooks 'gnus-summary-mode-hook))
5514
5515 (defun gnus-summary-make-display-table ()
5516   ;; Change the display table.  Odd characters have a tendency to mess
5517   ;; up nicely formatted displays - we make all possible glyphs
5518   ;; display only a single character.
5519
5520   ;; We start from the standard display table, if any.
5521   (setq gnus-summary-display-table 
5522         (or (copy-sequence standard-display-table)
5523             (make-display-table)))
5524   ;; Nix out all the control chars...
5525   (let ((i 32))
5526     (while (>= (setq i (1- i)) 0)
5527       (aset gnus-summary-display-table i [??])))
5528   ;; ... but not newline and cr, of course. (cr is necessary for the
5529   ;; selective display).  
5530   (aset gnus-summary-display-table ?\n nil)
5531   (aset gnus-summary-display-table ?\r nil)
5532   ;; We nix out any glyphs over 126 that are not set already.  
5533   (let ((i 256))
5534     (while (>= (setq i (1- i)) 127)
5535       ;; Only modify if the entry is nil.
5536       (or (aref gnus-summary-display-table i) 
5537           (aset gnus-summary-display-table i [??])))))
5538
5539 (defun gnus-summary-clear-local-variables ()
5540   (let ((locals gnus-summary-local-variables))
5541     (while locals
5542       (if (consp (car locals))
5543           (and (vectorp (car (car locals)))
5544                (set (car (car locals)) nil))
5545         (and (vectorp (car locals))
5546              (set (car locals) nil)))
5547       (setq locals (cdr locals)))))
5548
5549 ;; Some summary mode macros.
5550
5551 ;; Return a header specified by a NUMBER.
5552 (defun gnus-get-header-by-number (number)
5553   (save-excursion
5554     (set-buffer gnus-summary-buffer)
5555     (or gnus-newsgroup-headers-hashtb-by-number
5556         (gnus-make-headers-hashtable-by-number))
5557     (gnus-gethash (int-to-string number)
5558                   gnus-newsgroup-headers-hashtb-by-number)))
5559
5560 ;; Fast version of the function above.
5561 (defmacro gnus-get-header-by-num (number)
5562   (` (gnus-gethash (int-to-string (, number)) 
5563                    gnus-newsgroup-headers-hashtb-by-number)))
5564
5565 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5566   "Search for article forward.
5567 If UNREAD is non-nil, only unread articles are selected.
5568 If SUBJECT is non-nil, the article which has the same subject will be
5569 searched for. 
5570 If BACKWARD is non-nil, the search will be performed backwards instead."
5571   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5572
5573 (defmacro gnus-summary-search-backward (&optional unread subject)
5574   "Search for article backward.
5575 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5576 If 2nd optional argument SUBJECT is non-nil, the article which has
5577 the same subject will be searched for."
5578   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5579
5580 (defmacro gnus-summary-article-number (&optional number-or-nil)
5581   "The article number of the article on the current line.
5582 If there isn's an article number here, then we return the current
5583 article number."
5584   (if number-or-nil
5585       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5586     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5587          gnus-current-article)))
5588
5589 (defmacro gnus-summary-thread-level ()
5590   "The thread level of the article on the current line."
5591   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5592        0))
5593
5594 (defmacro gnus-summary-article-mark ()
5595   "The mark on the current line."
5596   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5597
5598 (defun gnus-summary-subject-string ()
5599   "Return current subject string or nil if nothing."
5600   (let ((article (gnus-summary-article-number))
5601         header)
5602     (and article 
5603          (setq header (gnus-get-header-by-num article))
5604          (vectorp header)
5605          (header-subject header))))
5606
5607 ;; Various summary mode internalish functions.
5608
5609 (defun gnus-mouse-pick-article (e)
5610   (interactive "e")
5611   (mouse-set-point e)
5612   (gnus-summary-next-page nil t))
5613
5614 (defun gnus-summary-setup-buffer (group)
5615   "Initialize summary buffer."
5616   (let ((buffer (concat "*Summary " group "*")))
5617     (if (get-buffer buffer)
5618         (progn
5619           (set-buffer buffer)
5620           (not gnus-newsgroup-begin))
5621       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5622       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5623       (gnus-add-current-to-buffer-list)
5624       (gnus-summary-mode group)
5625       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5626       (setq gnus-newsgroup-name group)
5627       t)))
5628
5629 (defun gnus-set-global-variables ()
5630   ;; Set the global equivalents of the summary buffer-local variables
5631   ;; to the latest values they had. These reflect the summary buffer
5632   ;; that was in action when the last article was fetched.
5633   (if (eq major-mode 'gnus-summary-mode) 
5634       (progn
5635         (setq gnus-summary-buffer (current-buffer))
5636         (let ((name gnus-newsgroup-name)
5637               (marked gnus-newsgroup-marked)
5638               (unread gnus-newsgroup-unreads)
5639               (headers gnus-current-headers)
5640               (score-file gnus-current-score-file))
5641           (save-excursion
5642             (set-buffer gnus-group-buffer)
5643             (setq gnus-newsgroup-name name)
5644             (setq gnus-newsgroup-marked marked)
5645             (setq gnus-newsgroup-unreads unread)
5646             (setq gnus-current-headers headers)
5647             (setq gnus-current-score-file score-file))))))
5648
5649 (defun gnus-summary-insert-dummy-line (sformat subject number)
5650   (if (not sformat) 
5651       (setq sformat gnus-summary-dummy-line-format-spec))
5652   (let (b)
5653     (beginning-of-line)
5654     (setq b (point))
5655     (insert (eval sformat))
5656     (add-text-properties
5657      b (1+ b)
5658      (list 'gnus-number number 
5659            'gnus-mark gnus-dummy-mark
5660            'gnus-level 0))))
5661
5662 (defvar gnus-thread-indent-array nil)
5663 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5664 (defun gnus-make-thread-indent-array ()
5665   (let ((n 200))
5666     (if (and gnus-thread-indent-array
5667              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5668         nil
5669       (setq gnus-thread-indent-array (make-vector 201 "")
5670             gnus-thread-indent-array-level gnus-thread-indent-level)
5671       (while (>= n 0)
5672         (aset gnus-thread-indent-array n
5673               (make-string (* n gnus-thread-indent-level) ? ))
5674         (setq n (1- n))))))
5675
5676 (defun gnus-summary-insert-line 
5677   (sformat header level current unread replied expirable subject-or-nil
5678            &optional dummy score)
5679   (or sformat (setq sformat gnus-summary-line-format-spec))
5680   (let* ((indentation (aref gnus-thread-indent-array level))
5681          (lines (header-lines header))
5682          (score (or score gnus-summary-default-score 0))
5683          (score-char
5684           (if (or (null gnus-summary-default-score)
5685                   (<= (abs (- score gnus-summary-default-score))
5686                       gnus-summary-zcore-fuzz)) ? 
5687             (if (< score gnus-summary-default-score)
5688                 gnus-score-below-mark gnus-score-over-mark)))
5689          (replied (if replied gnus-replied-mark ? ))
5690          (from (header-from header))
5691          (name (cond 
5692                 ((string-match "(.+)" from)
5693                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5694                 ((string-match "<[^>]+> *$" from)
5695                  (let ((beg (match-beginning 0)))
5696                    (or (and (string-match "^\"[^\"]*\"" from)
5697                             (substring from (1+ (match-beginning 0))
5698                                        (1- (match-end 0))))
5699                        (substring from 0 beg))))
5700                 (t from)))
5701          (subject (header-subject header))
5702          (number (header-number header))
5703          (opening-bracket (if dummy ?\< ?\[))
5704          (closing-bracket (if dummy ?\> ?\]))
5705          (buffer-read-only nil)
5706          (b (progn (beginning-of-line) (point))))
5707     (or (numberp lines) (setq lines 0))
5708     (insert (eval sformat))
5709     (add-text-properties
5710      b (1+ b) (list 'gnus-number number 
5711                     'gnus-mark (or unread gnus-unread-mark)
5712                     'gnus-level level))))
5713
5714 (defun gnus-summary-update-line (&optional dont-update)
5715   ;; Update summary line after change.
5716   (or (not gnus-summary-default-score)
5717       gnus-summary-inhibit-highlight
5718       (let ((gnus-summary-inhibit-highlight t)
5719             (article (gnus-summary-article-number)))
5720         (progn
5721           (or dont-update
5722               (if (and gnus-summary-mark-below
5723                        (< (gnus-summary-article-score)
5724                           gnus-summary-mark-below))
5725                   (and (not (memq article gnus-newsgroup-marked))
5726                        (not (memq article gnus-newsgroup-dormant))
5727                        (memq article gnus-newsgroup-unreads)
5728                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5729                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5730                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5731           (and gnus-visual
5732                (run-hooks 'gnus-summary-update-hook))))))
5733
5734 (defun gnus-summary-update-lines (&optional beg end)
5735   ;; Mark article as read (or not) by taking into account scores.
5736   (let ((beg (or beg (point-min)))
5737         (end (or end (point-max))))
5738     (if (or (not gnus-summary-default-score)
5739             gnus-summary-inhibit-highlight)
5740         ()
5741       (let ((gnus-summary-inhibit-highlight t)
5742             article)
5743         (save-excursion
5744           (set-buffer gnus-summary-buffer)
5745           (goto-char beg)
5746           (beginning-of-line)
5747           (while (and (not (eobp)) (< (point) end))
5748             (if (and gnus-summary-mark-below
5749                      (< (or (cdr (assq 
5750                                   (setq article (get-text-property 
5751                                                  (point) 'gnus-number))
5752                                   gnus-newsgroup-scored))
5753                             gnus-summary-default-score 0)
5754                         gnus-summary-mark-below))
5755                 ;; We want to possibly mark it as read...
5756                 (and (not (memq article gnus-newsgroup-marked))
5757                      (not (memq article gnus-newsgroup-dormant))
5758                      (memq article gnus-newsgroup-unreads)
5759                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5760               ;; We want to possibly mark it as unread.
5761               (and (eq (get-text-property (point) 'gnus-mark)
5762                        gnus-low-score-mark)
5763                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5764             ;; Do the visual highlights at the same time.
5765             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5766             (forward-line 1)))))))
5767
5768 (defvar gnus-tmp-gathered nil)
5769
5770 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5771   ;; Sum up all elements (and sub-elements) in a list.
5772   (let* ((number
5773           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5774           (if (and (consp thread) (cdr thread))
5775               (apply
5776                '+ 1 (mapcar
5777                      'gnus-summary-number-of-articles-in-thread 
5778                      (cdr thread)))
5779             1)))
5780     (if char 
5781         (if (> number 1) gnus-not-empty-thread-mark
5782           gnus-empty-thread-mark)
5783       number)))
5784
5785 (defun gnus-summary-read-group 
5786   (group &optional show-all no-article kill-buffer)
5787   "Start reading news in newsgroup GROUP.
5788 If SHOW-ALL is non-nil, already read articles are also listed.
5789 If NO-ARTICLE is non-nil, no article is selected initially."
5790   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5791   (let* ((new-group (gnus-summary-setup-buffer group))
5792          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5793                                                   group))))
5794          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5795     (cond 
5796      ((not new-group)
5797       (gnus-set-global-variables)
5798       (gnus-kill-buffer kill-buffer)
5799       (gnus-configure-windows 'summary)
5800       (gnus-set-mode-line 'summary)
5801       (gnus-summary-position-cursor)
5802       (message "")
5803       t)
5804      ((null did-select) 
5805       (and (eq major-mode 'gnus-summary-mode)
5806            (not (equal (current-buffer) kill-buffer))
5807            (progn
5808              (kill-buffer (current-buffer))
5809              (if (not quit-config)
5810                  (progn
5811                    (set-buffer gnus-group-buffer)
5812                    (gnus-group-jump-to-group group)
5813                    (gnus-group-next-unread-group 1))
5814                (if (not (buffer-name (car quit-config)))
5815                    (gnus-configure-windows 'group 'force)
5816                  (set-buffer (car quit-config))
5817                  (and (eq major-mode 'gnus-summary-mode)
5818                       (gnus-set-global-variables))
5819                  (gnus-configure-windows (cdr quit-config))))))
5820       (message "Can't select group")
5821       nil)
5822      ((eq did-select 'quit)
5823       (and (eq major-mode 'gnus-summary-mode)
5824            (not (equal (current-buffer) kill-buffer))
5825            (kill-buffer (current-buffer)))
5826       (gnus-kill-buffer kill-buffer)
5827       (if (not quit-config)
5828           (progn
5829             (set-buffer gnus-group-buffer)
5830             (gnus-group-jump-to-group group)
5831             (gnus-group-next-unread-group 1)
5832             (gnus-configure-windows 'group 'force))
5833         (if (not (buffer-name (car quit-config)))
5834             (gnus-configure-windows 'group 'force)
5835           (set-buffer (car quit-config))
5836           (and (eq major-mode 'gnus-summary-mode)
5837                (gnus-set-global-variables))
5838           (gnus-configure-windows (cdr quit-config))))
5839       (signal 'quit nil))
5840      (t
5841       (gnus-set-global-variables)
5842       ;; Save the active value in effect when the group was entered.
5843       (setq gnus-newsgroup-active 
5844             (gnus-copy-sequence
5845              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5846       ;; You can change the subjects in this hook.
5847       (run-hooks 'gnus-select-group-hook)
5848       ;; Do score processing.
5849       (and gnus-use-scoring (gnus-possibly-score-headers))
5850       (gnus-update-format-specifications)
5851       ;; Generate the summary buffer.
5852       (gnus-summary-prepare)
5853       ;; Create the header hashtb.
5854       (gnus-make-headers-hashtable-by-number)
5855       (if (zerop (buffer-size))
5856           (cond (gnus-newsgroup-dormant
5857                  (gnus-summary-show-all-dormant))
5858                 ((and gnus-newsgroup-scored show-all)
5859                  (gnus-summary-show-all-expunged))))
5860       ;; Function `gnus-apply-kill-file' must be called in this hook.
5861       (run-hooks 'gnus-apply-kill-hook)
5862       (if (zerop (buffer-size))
5863           (progn
5864             ;; This newsgroup is empty.
5865             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5866             (gnus-message 6 "No unread news")
5867             (gnus-kill-buffer kill-buffer)
5868             nil)
5869         ;;(save-excursion
5870         ;;  (if kill-buffer
5871         ;;      (let ((gnus-summary-buffer kill-buffer))
5872         ;;      (gnus-configure-windows 'group))))
5873         ;; Hide conversation thread subtrees.  We cannot do this in
5874         ;; gnus-summary-prepare-hook since kill processing may not
5875         ;; work with hidden articles.
5876         (and gnus-show-threads
5877              gnus-thread-hide-subtree
5878              (gnus-summary-hide-all-threads))
5879         ;; Show first unread article if requested.
5880         (goto-char (point-min))
5881         (if (and (not no-article)
5882                  gnus-auto-select-first
5883                  (gnus-summary-first-unread-article))
5884             ()
5885           (gnus-configure-windows 'summary))
5886         (gnus-set-mode-line 'summary)
5887         (gnus-summary-position-cursor)
5888         ;; If in async mode, we send some info to the backend.
5889         (and gnus-newsgroup-async
5890              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5891              (gnus-request-asynchronous 
5892               gnus-newsgroup-name
5893               (if (and gnus-asynchronous-article-function
5894                        (fboundp gnus-asynchronous-article-function))
5895                   (funcall gnus-asynchronous-article-function
5896                            gnus-newsgroup-threads)
5897                 gnus-newsgroup-threads)))
5898         (gnus-kill-buffer kill-buffer)
5899         (if (not (get-buffer-window gnus-group-buffer))
5900             ()
5901           ;; gotta use windows, because recenter does wierd stuff if
5902           ;; the current buffer ain't the displayed window.
5903           (let ((owin (selected-window))) 
5904             (select-window (get-buffer-window gnus-group-buffer))
5905             (and (gnus-group-goto-group group)
5906                  (recenter))
5907             (select-window owin))))
5908       t))))
5909
5910 (defun gnus-summary-prepare ()
5911   ;; Generate the summary buffer.
5912   (let ((buffer-read-only nil))
5913     (erase-buffer)
5914     (gnus-summary-prepare-threads 
5915      (if gnus-show-threads
5916          (gnus-gather-threads 
5917           (gnus-sort-threads 
5918            (if (and gnus-summary-expunge-below
5919                     (not gnus-fetch-old-headers))
5920                (gnus-make-threads-and-expunge)
5921              (gnus-make-threads))))
5922        gnus-newsgroup-headers)
5923      'cull)
5924     (gnus-summary-update-lines)
5925     ;; Remove the final newline.
5926     ;;(goto-char (point-max))
5927     ;;(delete-char -1)
5928     ;; Call hooks for modifying summary buffer.
5929     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5930     (goto-char (point-min))
5931     (run-hooks 'gnus-summary-prepare-hook)))
5932
5933 (defun gnus-gather-threads (threads)
5934   "Gather threads that have lost their roots."
5935   (if (not gnus-summary-make-false-root)
5936       threads 
5937     (let ((hashtb (gnus-make-hashtable 1023))
5938           (prev threads)
5939           (result threads)
5940           subject hthread whole-subject)
5941       (while threads
5942         (setq whole-subject 
5943               (setq subject (header-subject (car (car threads)))))
5944         (if gnus-summary-gather-subject-limit
5945             (or (and (numberp gnus-summary-gather-subject-limit)
5946                      (> (length subject) gnus-summary-gather-subject-limit)
5947                      (setq subject
5948                            (substring subject 0 
5949                                       gnus-summary-gather-subject-limit)))
5950                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5951                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5952           (setq subject (gnus-simplify-subject-re subject)))
5953         (if (setq hthread 
5954                   (gnus-gethash subject hashtb))
5955             (progn
5956               (or (stringp (car (car hthread)))
5957                   (setcar hthread (list whole-subject (car hthread))))
5958               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5959                                            (list (car threads))))
5960               (setcdr prev (cdr threads))
5961               (setq threads prev))
5962           (gnus-sethash subject threads hashtb))
5963         (setq prev threads)
5964         (setq threads (cdr threads)))
5965       result)))
5966
5967 (defun gnus-make-threads ()
5968   ;; This function takes the dependencies already made by 
5969   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5970   ;; through the dependecies in the hash table and finds all the
5971   ;; roots. Roots do not refer back to any valid articles.
5972   (gnus-message 6 "Threading...")
5973   (let (roots new-roots)
5974     (and gnus-fetch-old-headers
5975          (eq gnus-headers-retrieved-by 'nov)
5976          (gnus-build-old-threads))
5977     (mapatoms
5978      (lambda (refs)
5979        (if (not (car (symbol-value refs)))
5980            (setq roots (append (cdr (symbol-value refs)) roots))
5981          ;; Ok, these refer back to valid articles, but if
5982          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5983          ;; the root has the same subject as its children. The children
5984          ;; that do not are made into roots and removed from the list
5985          ;; of children. 
5986          (or gnus-thread-ignore-subject
5987              (let* ((prev (symbol-value refs))
5988                     (subject (gnus-simplify-subject-re 
5989                               (header-subject (car prev))))
5990                     (headers (cdr prev)))
5991                (while headers
5992                  (if (not (string= subject
5993                                    (gnus-simplify-subject-re 
5994                                     (header-subject (car headers)))))
5995                      (progn
5996                        (setq new-roots (cons (car headers) new-roots))
5997                        (setcdr prev (cdr headers)))
5998                    (setq prev headers))
5999                  (setq headers (cdr headers)))))))
6000      gnus-newsgroup-dependencies)
6001
6002     ;; We enter the new roots into the dependencies structure to
6003     ;; ensure that any possible later thread-regeneration will be
6004     ;; possible. 
6005     (let ((r new-roots))
6006       (while r
6007         (gnus-sethash (concat (header-id (car r)) ".boo")
6008                       (list nil (car r)) gnus-newsgroup-dependencies)
6009         (setq r (cdr r))))
6010
6011     (setq roots (nconc new-roots roots))
6012
6013     (prog1
6014         (mapcar 'gnus-trim-thread
6015                 (apply 'append
6016                        (mapcar 'gnus-cut-thread
6017                                (mapcar 'gnus-make-sub-thread roots))))
6018       (gnus-message 6 "Threading...done"))))
6019
6020   
6021 (defun gnus-make-threads-and-expunge ()
6022   ;; This function takes the dependencies already made by 
6023   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6024   ;; through the dependecies in the hash table and finds all the
6025   ;; roots. Roots do not refer back to any valid articles.
6026   (gnus-message 6 "Threading...")
6027   (let ((default (or gnus-summary-default-score 0))
6028         (below gnus-summary-expunge-below)
6029         roots article new-roots)
6030     (and gnus-fetch-old-headers
6031          (eq gnus-headers-retrieved-by 'nov)
6032          (gnus-build-old-threads))
6033     (mapatoms
6034      (lambda (refs)
6035        (if (not (car (symbol-value refs)))
6036            ;; These articles do not refer back to any other articles -
6037            ;; they are roots.
6038            (let ((headers (cdr (symbol-value refs))))
6039              ;; We weed out the low-scored articles.
6040              (while headers
6041                (if (not (< (or (cdr (assq (header-number (car headers))
6042                                           gnus-newsgroup-scored)) default)
6043                            below))
6044                    ;; It is over.
6045                    (setq roots (cons (car headers) roots))
6046                  ;; It is below, so we mark it as read.
6047                  (setq gnus-newsgroup-unreads
6048                        (delq (header-number (car headers))
6049                              gnus-newsgroup-unreads))
6050                  (setq gnus-newsgroup-reads 
6051                        (cons (cons (header-number (car headers))
6052                                    gnus-low-score-mark) 
6053                              gnus-newsgroup-reads)))
6054                (setq headers (cdr headers))))
6055          ;; Ok, these refer back to valid articles, but if
6056          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6057          ;; the root has the same subject as its children. The children
6058          ;; that do not are made into roots and removed from the list
6059          ;; of children. 
6060          (or gnus-thread-ignore-subject
6061              (let* ((prev (symbol-value refs))
6062                     (subject (gnus-simplify-subject-re 
6063                               (header-subject (car prev))))
6064                     (headers (cdr prev)))
6065                (while headers
6066                  (if (not (string= subject
6067                                    (gnus-simplify-subject-re 
6068                                     (header-subject (car headers)))))
6069                      (progn
6070                        (if (not (< (or (cdr (assq (header-number (car headers))
6071                                                   gnus-newsgroup-scored))
6072                                        default) below))
6073                            (setq new-roots (cons (car headers) new-roots))
6074                          (setq gnus-newsgroup-unreads
6075                                (delq (header-number (car headers))
6076                                      gnus-newsgroup-unreads))
6077                          (setq gnus-newsgroup-reads
6078                                (cons (cons (header-number (car headers)) 
6079                                            gnus-low-score-mark) 
6080                                      gnus-newsgroup-reads)))
6081                        (setcdr prev (cdr headers)))
6082                    (setq prev headers))
6083                  (setq headers (cdr headers)))))
6084          ;; If this article is expunged, some of the children might be
6085          ;; roots.  
6086          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6087                                gnus-newsgroup-scored)) default)
6088                 below)
6089              (let* ((prev (symbol-value refs))
6090                     (headers (cdr prev)))
6091                (while headers
6092                  (setq article (header-number (car headers)))
6093                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6094                                  default) below))
6095                      (progn (setq new-roots (cons (car headers) new-roots))
6096                             (setq prev headers))
6097                    (setq gnus-newsgroup-unreads 
6098                          (delq article gnus-newsgroup-unreads))
6099                    (setq gnus-newsgroup-reads 
6100                          (cons (cons article gnus-low-score-mark) 
6101                                gnus-newsgroup-reads))
6102                    (setcdr prev (cdr headers)))
6103                  (setq headers (cdr headers))))
6104            ;; It was not expunged, but we look at expunged children.
6105            (let* ((prev (symbol-value refs))
6106                   (headers (cdr prev))
6107                   article)
6108              (while headers
6109                (setq article (header-number (car headers)))
6110                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6111                                default) below))
6112                    (setq prev headers)
6113                  (setq gnus-newsgroup-unreads 
6114                        (delq article gnus-newsgroup-unreads))
6115                  (setq gnus-newsgroup-reads 
6116                        (cons (cons article gnus-low-score-mark)
6117                              gnus-newsgroup-reads))
6118                  (setcdr prev (cdr headers)))
6119                (setq headers (cdr headers)))))))
6120      gnus-newsgroup-dependencies)
6121
6122     ;; We enter the new roots into the dependencies structure to
6123     ;; ensure that any possible later thread-regeneration will be
6124     ;; possible. 
6125     (let ((r new-roots))
6126       (while r
6127         (gnus-sethash (concat (header-id (car r)) ".boo")
6128                       (list nil (car r)) gnus-newsgroup-dependencies)
6129         (setq r (cdr r))))
6130
6131     (setq roots (nconc new-roots roots))
6132
6133     (prog1
6134         (mapcar 'gnus-trim-thread
6135                 (apply 'append
6136                        (mapcar 'gnus-cut-thread
6137                                (mapcar 'gnus-make-sub-thread roots))))
6138       (gnus-message 6 "Threading...done"))))
6139
6140   
6141 (defun gnus-cut-thread (thread)
6142   ;; Remove leaf dormant or ancient articles from THREAD.
6143   (let ((head (car thread))
6144         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6145     (if (and (null tail)
6146              (let ((number (header-number head)))
6147                (or (memq number gnus-newsgroup-ancient)
6148                    (memq number gnus-newsgroup-dormant)
6149                    (and gnus-summary-expunge-below
6150                         (eq gnus-fetch-old-headers 'some)
6151                         (< (or (cdr (assq number gnus-newsgroup-scored))
6152                                gnus-summary-default-score 0)
6153                            gnus-summary-expunge-below)
6154                         (progn
6155                           (setq gnus-newsgroup-unreads
6156                                 (delq number gnus-newsgroup-unreads))
6157                           (setq gnus-newsgroup-reads
6158                                 (cons (cons number gnus-low-score-mark)
6159                                       gnus-newsgroup-reads))
6160                           t)))))
6161         nil
6162       (list (cons head tail)))))
6163
6164 (defun gnus-trim-thread (thread)
6165   ;; Remove root ancient articles with only one child from THREAD.
6166   (if (and (eq gnus-fetch-old-headers 'some)
6167            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6168            (= (length thread) 2))
6169       (gnus-trim-thread (nth 1 thread))
6170     thread))
6171
6172 (defun gnus-make-sub-thread (root)
6173   ;; This function makes a sub-tree for a node in the tree.
6174   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6175                                               gnus-newsgroup-dependencies)))))
6176     (cons root (mapcar 'gnus-make-sub-thread children))))
6177
6178 (defun gnus-build-old-threads ()
6179   ;; Look at all the articles that refer back to old articles, and
6180   ;; fetch the headers for the articles that aren't there. This will
6181   ;; build complete threads - if the roots haven't been expired by the
6182   ;; server, that is.
6183   (let (id heads)
6184     (mapatoms
6185      (lambda (refs)
6186        (if (not (car (symbol-value refs)))
6187            (progn
6188              (setq heads (cdr (symbol-value refs)))
6189              (while heads
6190                (if (not (memq (header-number (car heads))
6191                               gnus-newsgroup-dormant))
6192                    (progn
6193                      (setq id (symbol-name refs))
6194                      (while (and (setq id (gnus-build-get-header id))
6195                                  (not (car (gnus-gethash 
6196                                             id gnus-newsgroup-dependencies)))))
6197                      (setq heads nil))
6198                  (setq heads (cdr heads)))))))
6199      gnus-newsgroup-dependencies)))
6200
6201 (defun gnus-build-get-header (id)
6202   ;; Look through the buffer of NOV lines and find the header to
6203   ;; ID. Enter this line into the dependencies hash table, and return
6204   ;; the id of the parent article (if any).
6205   (let ((deps gnus-newsgroup-dependencies)
6206         found header)
6207     (prog1
6208         (save-excursion
6209           (set-buffer nntp-server-buffer)
6210           (goto-char (point-min))
6211           (while (and (not found) (search-forward id nil t))
6212             (beginning-of-line)
6213             (setq found (looking-at 
6214                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6215                                  (regexp-quote id))))
6216             (or found (beginning-of-line 2)))
6217           (if found
6218               (let (ref)
6219                 (beginning-of-line)
6220                 (and
6221                  (setq header (gnus-nov-parse-line 
6222                                (read (current-buffer)) deps))
6223                  (setq ref (header-references header))
6224                  (string-match "\\(<[^>]+>\\) *$" ref)
6225                  (substring ref (match-beginning 1) (match-end 1))))))
6226       (and header
6227            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6228                  gnus-newsgroup-ancient (cons (header-number header)
6229                                               gnus-newsgroup-ancient))))))
6230
6231 ;; Re-build the thread containing ID.
6232 (defun gnus-rebuild-thread (id)
6233   (let ((dep gnus-newsgroup-dependencies)
6234         (buffer-read-only nil)
6235         parent headers refs thread art)
6236     (while (and id (setq headers
6237                          (car (setq art (gnus-gethash (downcase id) dep)))))
6238       (setq parent art)
6239       (setq id (and (setq refs (header-references headers))
6240                     (string-match "\\(<[^>]+>\\) *$" refs)
6241                     (substring refs (match-beginning 1) (match-end 1)))))
6242     (setq thread (gnus-make-sub-thread (car parent)))
6243     (gnus-rebuild-remove-articles thread)
6244     (let ((beg (point)))
6245       (gnus-summary-prepare-threads (list thread))
6246       (gnus-summary-update-lines beg (point)))))
6247
6248 ;; Delete all lines in the summary buffer that correspond to articles
6249 ;; in this thread.
6250 (defun gnus-rebuild-remove-articles (thread)
6251   (and (gnus-summary-goto-subject (header-number (car thread)))
6252        (gnus-delete-line))
6253   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6254
6255 (defun gnus-sort-threads (threads)
6256   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6257   (let ((fun gnus-thread-sort-functions))
6258     (while fun
6259       (setq threads (sort threads (car fun))
6260             fun (cdr fun))))
6261   threads)
6262
6263 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6264 (defmacro gnus-thread-header (thread)
6265   ;; Return header of first article in THREAD.
6266   ;; Note that THREAD must never, evr be anything else than a variable -
6267   ;; using some other form will lead to serious barfage.
6268   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6269   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6270   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6271         (vector thread) 2))
6272
6273 (defun gnus-thread-sort-by-number (h1 h2)
6274   "Sort threads by root article number."
6275   (< (header-number (gnus-thread-header h1))
6276      (header-number (gnus-thread-header h2))))
6277
6278 (defun gnus-thread-sort-by-author (h1 h2)
6279   "Sort threads by root author."
6280   (string-lessp
6281    (let ((extract (funcall 
6282                    gnus-extract-address-components
6283                    (header-from (gnus-thread-header h1)))))
6284      (or (car extract) (cdr extract)))
6285    (let ((extract (funcall
6286                    gnus-extract-address-components 
6287                    (header-from (gnus-thread-header h2)))))
6288      (or (car extract) (cdr extract)))))
6289
6290 (defun gnus-thread-sort-by-subject (h1 h2)
6291   "Sort threads by root subject."
6292   (string-lessp
6293    (downcase (gnus-simplify-subject 
6294               (header-subject (gnus-thread-header h1))))
6295    (downcase (gnus-simplify-subject 
6296               (header-subject (gnus-thread-header h2))))))
6297
6298 (defun gnus-thread-sort-by-date (h1 h2)
6299   "Sort threads by root article date."
6300   (string-lessp
6301    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6302    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6303
6304 (defun gnus-thread-sort-by-score (h1 h2)
6305   "Sort threads by root article score.
6306 Unscored articles will be counted as having a score of zero."
6307   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6308                     gnus-newsgroup-scored))
6309          gnus-summary-default-score 0)
6310      (or (cdr (assq (header-number (gnus-thread-header h2))
6311                     gnus-newsgroup-scored))
6312          gnus-summary-default-score 0)))
6313
6314 (defun gnus-thread-sort-by-total-score (h1 h2)
6315   "Sort threads by the sum of all scores in the thread.
6316 Unscored articles will be counted as having a score of zero."
6317   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6318
6319 (defun gnus-thread-total-score (thread)
6320   ;;  This function find the total score of THREAD.
6321   (if (consp thread)
6322       (if (stringp (car thread))
6323           (apply gnus-thread-score-function 0
6324                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6325         (gnus-thread-total-score-1 thread))
6326     (gnus-thread-total-score-1 (list thread))))
6327
6328 (defun gnus-thread-total-score-1 (root)
6329   ;; This function find the total score of the thread below ROOT.
6330   (setq root (car root))
6331   (apply gnus-thread-score-function
6332          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6333              gnus-summary-default-score 0)
6334          (mapcar 'gnus-thread-total-score
6335                  (cdr (gnus-gethash (downcase (header-id root))
6336                                     gnus-newsgroup-dependencies)))))
6337
6338 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6339 (defvar gnus-tmp-prev-subject "")
6340
6341 (defun gnus-summary-prepare-threads (threads &optional cull)
6342   "Prepare summary buffer from THREADS and indentation LEVEL.  
6343 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6344 or a straight list of headers."
6345   (message "Generating summary...")
6346   (let ((level 0)
6347         thread header number subject stack state gnus-tmp-gathered)
6348     (if (vectorp (car threads))
6349         ;; If this is a straight (sic) list of headers, then a
6350         ;; threaded summary display isn't required, so we just create
6351         ;; an unthreaded one.
6352         (gnus-summary-prepare-unthreaded threads cull)
6353
6354       ;; Do the threaded display.
6355
6356       (while (or threads stack)
6357         
6358         (if threads
6359             ;; If there are some threads, we do them before the
6360             ;; threads on the stack.
6361             (setq thread threads
6362                   header (car (car thread)))
6363           ;; There were no current threads, so we pop something off
6364           ;; the stack. 
6365           (setq state (car stack)
6366                 level (car state)
6367                 thread (cdr state)
6368                 stack (cdr stack)
6369                 header (car (car thread))))
6370
6371         (if (stringp header)
6372             (progn
6373               ;; The header is a dummy root.
6374               (cond 
6375                ((eq gnus-summary-make-false-root 'adopt)
6376                 ;; We let the first article adopt the rest.
6377                 (let ((th (car (cdr (car thread)))))
6378                   (while (cdr th)
6379                     (setq th (cdr th)))
6380                   (setcdr th (cdr (cdr (car thread))))
6381                   (setq gnus-tmp-gathered 
6382                         (nconc (mapcar
6383                                 (lambda (h) (header-number (car h)))
6384                                 (cdr (cdr (car thread))))
6385                                gnus-tmp-gathered))
6386                   (setcdr (cdr (car thread)) nil))
6387                 (setq level -1))
6388                ((eq gnus-summary-make-false-root 'empty)
6389                 ;; We print adopted articles with empty subject fields.
6390                 (setq gnus-tmp-gathered 
6391                       (nconc (mapcar
6392                               (lambda (h) (header-number (car h)))
6393                               (cdr (cdr (car thread))))
6394                              gnus-tmp-gathered))
6395                 (setq level -1))
6396                ((eq gnus-summary-make-false-root 'dummy)
6397                 ;; We output a dummy root.
6398                 (gnus-summary-insert-dummy-line 
6399                  nil header (header-number (car (car (cdr (car thread)))))))
6400                (t
6401                 ;; We do not make a root for the gathered
6402                 ;; sub-threads at all.  
6403                 (setq level -1))))
6404       
6405           (setq number (header-number header)
6406                 subject (header-subject header))
6407
6408           ;; Do the async thing.
6409           (and gnus-newsgroup-async
6410                (setq gnus-newsgroup-threads
6411                      (cons (cons number (header-lines header)) 
6412                            gnus-newsgroup-threads)))
6413
6414           ;; We may have to root out some bad articles...
6415           (and cull
6416                (= level 0)
6417                (cond ((and (memq (setq number (header-number header))
6418                                  gnus-newsgroup-dormant)
6419                            (null thread))
6420                       (setq header nil))
6421                      ((and gnus-summary-expunge-below
6422                            (< (or (cdr (assq number gnus-newsgroup-scored))
6423                                   gnus-summary-default-score 0)
6424                               gnus-summary-expunge-below))
6425                       (setq header nil)
6426                       (setq gnus-newsgroup-unreads 
6427                             (delq number gnus-newsgroup-unreads))
6428                       (setq gnus-newsgroup-reads
6429                             (cons (cons number gnus-low-score-mark)
6430                                   gnus-newsgroup-reads)))))
6431           
6432           (and
6433            header
6434            (progn
6435              (inline
6436                (gnus-summary-insert-line
6437                 nil header level nil 
6438                 (cond 
6439                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6440                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6441                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6442                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6443                  (t (or (cdr (assq number gnus-newsgroup-reads))
6444                         gnus-ancient-mark)))
6445                 (memq number gnus-newsgroup-replied)
6446                 (memq number gnus-newsgroup-expirable)
6447                 (if (and (eq gnus-summary-make-false-root 'empty)
6448                          (memq number gnus-tmp-gathered))
6449                     gnus-summary-same-subject
6450                   (if (or (zerop level)
6451                           (and gnus-thread-ignore-subject
6452                                (not (string= 
6453                                      (gnus-simplify-subject-re
6454                                       gnus-tmp-prev-subject)
6455                                      (gnus-simplify-subject-re
6456                                       subject)))))
6457                       subject
6458                     gnus-summary-same-subject))
6459                 (and (eq gnus-summary-make-false-root 'adopt)
6460                      (memq number gnus-tmp-gathered))
6461                 (cdr (assq number gnus-newsgroup-scored)))
6462
6463                (setq gnus-tmp-prev-subject subject)))))
6464
6465         (if (nth 1 thread) 
6466             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6467         (setq level (1+ level))
6468         (setq threads (cdr (car thread))))))
6469   (message "Generating summary...done"))
6470
6471
6472
6473 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6474   (let (header number)
6475
6476     ;; Do the async thing, if that is required.
6477     (if gnus-newsgroup-async
6478         (setq gnus-newsgroup-threads
6479               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6480                       headers)))
6481
6482     (while headers
6483       (setq header (car headers)
6484             headers (cdr headers)
6485             number (header-number header))
6486
6487       ;; We may have to root out some bad articles...
6488       (cond 
6489        ((and cull
6490              (memq (setq number (header-number header))
6491                    gnus-newsgroup-dormant)))
6492        ((and cull gnus-summary-expunge-below
6493              (< (or (cdr (assq number gnus-newsgroup-scored))
6494                     gnus-summary-default-score 0)
6495                 gnus-summary-expunge-below))
6496         (setq gnus-newsgroup-unreads 
6497               (delq number gnus-newsgroup-unreads))
6498         (setq gnus-newsgroup-reads
6499               (cons (cons number gnus-low-score-mark)
6500                     gnus-newsgroup-reads)))
6501        (t
6502         (gnus-summary-insert-line
6503          nil header 0 nil 
6504          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6505                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6506                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6507                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6508                (t (or (cdr (assq number gnus-newsgroup-reads))
6509                       gnus-ancient-mark)))
6510          (memq number gnus-newsgroup-replied)
6511          (memq number gnus-newsgroup-expirable)
6512          (header-subject header) nil
6513          (cdr (assq number gnus-newsgroup-scored))))))))
6514
6515 (defun gnus-select-newsgroup (group &optional read-all)
6516   "Select newsgroup GROUP.
6517 If READ-ALL is non-nil, all articles in the group are selected."
6518   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6519          (info (nth 2 entry))
6520          articles)
6521     (gnus-check-server
6522      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6523
6524     (or (gnus-check-server gnus-current-select-method)
6525         (error "Couldn't open server"))
6526     
6527     (or (and (null entry)
6528              (gnus-activate-group group))
6529         (and (eq (car entry) t)
6530              (gnus-activate-group (car info)))
6531         (gnus-request-group group t)
6532         (progn
6533           (kill-buffer (current-buffer))
6534           (error "Couldn't request group %s: %s" 
6535                  group (gnus-status-message group))))
6536
6537     (setq gnus-newsgroup-name group)
6538     (setq gnus-newsgroup-unselected nil)
6539     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6540
6541     (and gnus-asynchronous
6542          (gnus-check-backend-function 
6543           'request-asynchronous gnus-newsgroup-name)
6544          (setq gnus-newsgroup-async
6545                (gnus-request-asynchronous gnus-newsgroup-name)))
6546
6547     (setq articles (gnus-articles-to-read group read-all))
6548
6549     (cond 
6550      ((null articles) 
6551       (gnus-message 3 "Couldn't select newsgroup")
6552       'quit)
6553      ((eq articles 0) nil)
6554      (t
6555       ;; Init the dependencies hash table.
6556       (setq gnus-newsgroup-dependencies 
6557             (gnus-make-hashtable (length articles)))
6558       ;; Retrieve the headers and read them in.
6559       (gnus-message 5 "Fetching headers...")
6560       (setq gnus-newsgroup-headers 
6561             (if (eq 'nov (setq gnus-headers-retrieved-by
6562                                ;; This is a naughty hack. To get the
6563                                ;; retrieval of old headers to work, we
6564                                ;; set `nntp-nov-gap' to nil (locally),
6565                                ;; and then just retrieve the headers.
6566                                ;; Mucho magic.
6567                                (if gnus-fetch-old-headers
6568                                    (let (nntp-nov-gap)
6569                                      (gnus-retrieve-headers 
6570                                       (if (not (eq 1 (car articles)))
6571                                           (cons 1 articles)
6572                                         articles)
6573                                       gnus-newsgroup-name))
6574                                  (gnus-retrieve-headers 
6575                                   articles gnus-newsgroup-name))))
6576                 (progn
6577                   (gnus-get-newsgroup-headers-xover articles))
6578               ;; If we were to fetch old headers, but the backend didn't
6579               ;; support XOVER, then it is possible we fetched one article
6580               ;; that we shouldn't have. If that's the case, we remove it.
6581               (if (or (not gnus-fetch-old-headers)
6582                       (eq 1 (car articles)))
6583                   ()
6584                 (save-excursion
6585                   (set-buffer nntp-server-buffer)
6586                   (goto-char (point-min))
6587                   (and 
6588                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6589                    (delete-region       ; So we delete this head.
6590                     (point) 
6591                     (search-forward "\n.\n" nil t)))))
6592               (gnus-get-newsgroup-headers)))
6593       (gnus-message 5 "Fetching headers...done")      
6594       ;; Remove canceled articles from the list of unread articles.
6595       (setq gnus-newsgroup-unreads
6596             (gnus-set-sorted-intersection 
6597              gnus-newsgroup-unreads
6598              (mapcar (lambda (headers) (header-number headers))
6599                      gnus-newsgroup-headers)))
6600       ;; Adjust and set lists of article marks.
6601       (and info
6602            (let (marked)
6603              (gnus-adjust-marked-articles info)
6604              (setq gnus-newsgroup-marked 
6605                    (copy-sequence
6606                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6607              (setq gnus-newsgroup-replied 
6608                    (copy-sequence (cdr (assq 'reply marked))))
6609              (setq gnus-newsgroup-expirable
6610                    (copy-sequence (cdr (assq 'expire marked))))
6611              (setq gnus-newsgroup-killed
6612                    (copy-sequence (cdr (assq 'killed marked))))
6613              (setq gnus-newsgroup-bookmarks 
6614                    (copy-sequence (cdr (assq 'bookmark marked))))
6615              (setq gnus-newsgroup-dormant 
6616                    (copy-sequence (cdr (assq 'dormant marked))))
6617              (setq gnus-newsgroup-scored 
6618                    (copy-sequence (cdr (assq 'score marked))))
6619              (setq gnus-newsgroup-processable nil)))
6620       ;; Check whether auto-expire is to be done in this group.
6621       (setq gnus-newsgroup-auto-expire
6622             (or (and (stringp gnus-auto-expirable-newsgroups)
6623                      (string-match gnus-auto-expirable-newsgroups group))
6624                 (memq 'auto-expire (nth 5 info))))
6625       ;; First and last article in this newsgroup.
6626       (and gnus-newsgroup-headers
6627            (setq gnus-newsgroup-begin 
6628                  (header-number (car gnus-newsgroup-headers)))
6629            (setq gnus-newsgroup-end
6630                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6631       (setq gnus-reffed-article-number -1)
6632       ;; GROUP is successfully selected.
6633       (or gnus-newsgroup-headers t)))))
6634
6635 (defun gnus-articles-to-read (group read-all)
6636   ;; Find out what articles the user wants to read.
6637   (let* ((articles
6638           ;; Select all articles if `read-all' is non-nil, or if all the
6639           ;; unread articles are dormant articles.
6640           (if (or (and read-all (not (numberp read-all)))
6641                   (= (length gnus-newsgroup-unreads) 
6642                      (length gnus-newsgroup-dormant)))
6643               (gnus-uncompress-range 
6644                (gnus-gethash group gnus-active-hashtb))
6645             gnus-newsgroup-unreads))
6646          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6647          (scored (length scored-list))
6648          (number (length articles))
6649          (marked (+ (length gnus-newsgroup-marked)
6650                     (length gnus-newsgroup-dormant)))
6651          (select
6652           (cond 
6653            ((numberp read-all)
6654             read-all)
6655            (t
6656             (condition-case ()
6657                 (cond ((and (or (<= scored marked)
6658                                 (= scored number))
6659                             (numberp gnus-large-newsgroup)
6660                             (> number gnus-large-newsgroup))
6661                        (let ((input
6662                               (read-string
6663                                (format
6664                                 "How many articles from %s (default %d): "
6665                                 gnus-newsgroup-name number))))
6666                          (if (string-match "^[ \t]*$" input)
6667                              number input)))
6668                       ((and (> scored marked) (< scored number))
6669                        (let ((input
6670                               (read-string
6671                                (format 
6672                                 "%s %s (%d scored, %d total): "
6673                                 "How many articles from"
6674                                 group scored number))))
6675                          (if (string-match "^[ \t]*$" input)
6676                              number input)))
6677                       (t number))
6678               (quit nil))))))
6679     (setq select (if (stringp select) (string-to-number select) select))
6680     (if (or (null select) (zerop select))
6681         select
6682       (if (and (not (zerop scored)) (<= (abs select) scored))
6683           (progn
6684             (setq articles (sort scored-list '<))
6685             (setq number (length articles)))
6686         (setq articles (copy-sequence articles)))
6687
6688       (if (< (abs select) number)
6689           (if (< select 0) 
6690               ;; Select the N oldest articles.
6691               (setcdr (nthcdr (1- (abs select)) articles) nil)
6692             ;; Select the N most recent articles.
6693             (setq articles (nthcdr (- number select) articles))))
6694       (setq gnus-newsgroup-unselected
6695             (gnus-sorted-intersection
6696              gnus-newsgroup-unreads
6697              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6698       articles)))
6699
6700 (defun gnus-killed-articles (killed articles)
6701   (let (out)
6702     (while articles
6703       (if (inline (gnus-member-of-range (car articles) killed))
6704           (setq out (cons (car articles) out)))
6705       (setq articles (cdr articles)))
6706     out))
6707
6708 (defun gnus-adjust-marked-articles (info &optional active)
6709   "Remove all marked articles that are no longer legal."
6710   (let ((marked-lists (nth 3 info))
6711         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6712         m prev)
6713     ;; There are many types of marked articles.
6714     (while marked-lists
6715       (setq m (cdr (setq prev (car marked-lists))))
6716       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6717              ;; Make sure that all ticked articles are a subset of the
6718              ;; unread/unselected articles.
6719              (while m
6720                (if (or (memq (car m) gnus-newsgroup-unreads)
6721                        (memq (car m) gnus-newsgroup-unselected))
6722                    (setq prev m)
6723                  (setcdr prev (cdr m)))
6724                (setq m (cdr m))))
6725             ((eq 'score (car prev))
6726              ;; Scored articles should be a subset of
6727              ;; unread/unselected articles. 
6728              (while m
6729                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6730                        (memq (car (car m)) gnus-newsgroup-unreads))
6731                    (setq prev m)
6732                  (setcdr prev (cdr m)))
6733                (setq m (cdr m))))
6734             ((eq 'bookmark (car prev))
6735              ;; Bookmarks should be a subset of active articles.
6736              (while m
6737                (if (< (car (car m)) (car active))
6738                    (setcdr prev (cdr m))
6739                  (setq prev m))
6740                (setq m (cdr m))))
6741             ((eq 'killed (car prev))
6742              ;; Articles that have been through the kill process are
6743              ;; to be a subset of active articles.
6744              (while (and m (< (or (and (numberp (car m)) (car m))
6745                                   (cdr (car m)))
6746                               (car active)))
6747                (setcdr prev (cdr m))
6748                (setq m (cdr m)))
6749              (if (and m (< (or (and (numberp (car m)) (car m))
6750                                (car (car m)))
6751                            (car active))) 
6752                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6753             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6754              ;; The replied and expirable articles have to be articles
6755              ;; that are active. 
6756              (while m
6757                (if (< (car m) (car active))
6758                    (setcdr prev (cdr m))
6759                  (setq prev m))
6760                (setq m (cdr m)))))
6761       (setq marked-lists (cdr marked-lists)))
6762     ;; Remove all lists that are empty.
6763     (setq marked-lists (nth 3 info))
6764     (if marked-lists
6765         (progn
6766           (while (= 1 (length (car marked-lists)))
6767             (setq marked-lists (cdr marked-lists)))
6768           (setq m (cdr (setq prev marked-lists)))
6769           (while m
6770             (if (= 1 (length (car m)))
6771                 (setcdr prev (cdr m))
6772               (setq prev m))
6773             (setq m (cdr m)))
6774           (setcar (nthcdr 3 info) marked-lists)))
6775     ;; Finally, if there are no marked lists at all left, and if there
6776     ;; are no elements after the lists in the info list, we just chop
6777     ;; the info list off before the marked lists.
6778     (and (null marked-lists) 
6779          (not (nthcdr 4 info))
6780          (setcdr (nthcdr 2 info) nil)))
6781   info)
6782
6783 (defun gnus-set-marked-articles 
6784   (info ticked replied expirable killed dormant bookmark score) 
6785   "Enter the various lists of marked articles into the newsgroup info list."
6786   (let (newmarked)
6787     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6788     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6789     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6790                                          newmarked)))
6791     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6792     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6793     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6794                                         newmarked)))
6795     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6796     (if (nthcdr 3 info)
6797         (progn
6798           (setcar (nthcdr 3 info) newmarked)
6799           (and (not newmarked)
6800                (not (nthcdr 4 info))
6801                (setcdr (nthcdr 2 info) nil)))
6802       (if newmarked
6803           (setcdr (nthcdr 2 info) (list newmarked))))))
6804
6805 (defun gnus-add-marked-articles (group type articles &optional info force)
6806   ;; Add ARTICLES of TYPE to the info of GROUP.
6807   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6808   ;; add, but replace marked articles of TYPE with ARTICLES.
6809   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6810         marked m)
6811     (or (not info)
6812         (and (not (setq marked (nthcdr 3 info)))
6813              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6814         (and (not (setq m (assq type (car marked))))
6815              (setcar marked (cons (cons type articles) (car marked))))
6816         (if force
6817             (setcdr m articles)
6818           (nconc m articles)))))
6819          
6820 (defun gnus-set-mode-line (where)
6821   "This function sets the mode line of the article or summary buffers.
6822 If WHERE is `summary', the summary mode line format will be used."
6823   (if (memq where gnus-updated-mode-lines)
6824       (let (mode-string)
6825         (save-excursion
6826           (set-buffer gnus-summary-buffer)
6827           (let* ((mformat (if (eq where 'article) 
6828                               gnus-article-mode-line-format-spec
6829                             gnus-summary-mode-line-format-spec))
6830                  (group-name gnus-newsgroup-name)
6831                  (article-number (or gnus-current-article 0))
6832                  (unread (- (length gnus-newsgroup-unreads)
6833                             (length gnus-newsgroup-dormant)))
6834                  (unread-and-unticked 
6835                   (- unread (length gnus-newsgroup-marked)))
6836                  (unselected (length gnus-newsgroup-unselected))
6837                  (unread-and-unselected
6838                   (cond ((and (zerop unread-and-unticked)
6839                               (zerop unselected)) "")
6840                         ((zerop unselected) 
6841                          (format "{%d more}" unread-and-unticked))
6842                         (t (format "{%d(+%d) more}"
6843                                    unread-and-unticked unselected))))
6844                  (subject
6845                   (if gnus-current-headers
6846                       (header-subject gnus-current-headers) ""))
6847                  (max-len (and gnus-mode-non-string-length
6848                                (- (frame-width) gnus-mode-non-string-length)))
6849                  header) ;; passed as argument to any user-format-funcs
6850             (setq mode-string (eval mformat))
6851             (or (numberp max-len)
6852                 (setq max-len (length mode-string)))
6853             (if (< max-len 4) (setq max-len 4))
6854             (if (> (length mode-string) max-len)
6855                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6856                 ;;  function `substring' might cut on a middle
6857                 ;;  of multi-octet character.
6858                 (setq mode-string 
6859                       (concat (gnus-truncate-string mode-string (- max-len 3))
6860                               "...")))
6861             (setq mode-string (format (format "%%-%ds" max-len)
6862                                       mode-string))))
6863         (setq mode-line-buffer-identification mode-string)
6864         (set-buffer-modified-p t))))
6865
6866 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6867   "Go through the HEADERS list and add all Xrefs to a hash table.
6868 The resulting hash table is returned, or nil if no Xrefs were found."
6869   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6870          (prefix (if (and 
6871                       (gnus-group-foreign-p from-newsgroup)
6872                       (not (memq 'virtual 
6873                                  (assoc (symbol-name (car from-method))
6874                                         gnus-valid-select-methods))))
6875                      (gnus-group-real-prefix from-newsgroup)))
6876          (xref-hashtb (make-vector 63 0))
6877          start group entry number xrefs header)
6878     (while headers
6879       (setq header (car headers))
6880       (if (and (setq xrefs (header-xref header))
6881                (not (memq (header-number header) unreads)))
6882           (progn
6883             (setq start 0)
6884             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6885               (setq start (match-end 0))
6886               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6887                                                     (match-end 1))))
6888               (setq number 
6889                     (string-to-int (substring xrefs (match-beginning 2) 
6890                                               (match-end 2))))
6891               (if (setq entry (gnus-gethash group xref-hashtb))
6892                   (setcdr entry (cons number (cdr entry)))
6893                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6894       (setq headers (cdr headers)))
6895     (if start xref-hashtb nil)))
6896
6897 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6898   "Look through all the headers and mark the Xrefs as read."
6899   (let ((virtual (memq 'virtual 
6900                        (assoc (symbol-name (car (gnus-find-method-for-group 
6901                                                  from-newsgroup)))
6902                               gnus-valid-select-methods)))
6903         name entry info xref-hashtb idlist method
6904         nth4)
6905     (save-excursion
6906       (set-buffer gnus-group-buffer)
6907       (if (setq xref-hashtb 
6908                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6909           (mapatoms 
6910            (lambda (group)
6911              (if (string= from-newsgroup (setq name (symbol-name group)))
6912                  ()
6913                (setq idlist (symbol-value group))
6914                ;; Dead groups are not updated.
6915                (if (and (prog1 
6916                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6917                                   info (nth 2 entry))
6918                           (if (stringp (setq nth4 (nth 4 info)))
6919                               (setq nth4 (gnus-server-to-method nth4))))
6920                         ;; Only do the xrefs if the group has the same
6921                         ;; select method as the group we have just read.
6922                         (or (gnus-methods-equal-p 
6923                              nth4 (gnus-find-method-for-group from-newsgroup))
6924                             virtual
6925                             (equal nth4 
6926                                    (setq method (gnus-find-method-for-group 
6927                                                  from-newsgroup)))
6928                             (and (equal (car nth4) (car method))
6929                                  (equal (nth 1 nth4) (nth 1 method))))
6930                         gnus-use-cross-reference
6931                         (or (not (eq gnus-use-cross-reference t))
6932                             virtual
6933                             ;; Only do cross-references on subscribed
6934                             ;; groups, if that is what is wanted.  
6935                             (<= (nth 1 info) gnus-level-subscribed)))
6936                    (gnus-group-make-articles-read name idlist expirable))))
6937            xref-hashtb)))))
6938
6939 (defun gnus-group-make-articles-read (group articles expirable)
6940   (let* ((num 0)
6941          (entry (gnus-gethash group gnus-newsrc-hashtb))
6942          (info (nth 2 entry))
6943          (active (gnus-gethash group gnus-active-hashtb))
6944          exps expirable range)
6945     ;; First peel off all illegal article numbers.
6946     (if active
6947         (let ((ids articles)
6948               (ticked (cdr (assq 'tick (nth 3 info))))
6949               (dormant (cdr (assq 'dormant (nth 3 info))))
6950               id)
6951           (setq exps nil)
6952           (while ids
6953             (setq id (car ids))
6954             (if (or (> id (cdr active))
6955                     (< id (car active))
6956                     (memq id ticked)
6957                     (memq id dormant))
6958                 (setq articles (delq id articles)))
6959             (and (memq id expirable)
6960                  (setq exps (cons id exps)))
6961             (setq ids (cdr ids)))))
6962     ;; Update expirable articles.
6963     (gnus-add-marked-articles nil 'expirable exps info)
6964     (and active
6965          (null (nth 2 info))
6966          (> (car active) 1)
6967          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6968     (setcar (nthcdr 2 info)
6969             (setq range
6970                   (gnus-add-to-range 
6971                    (nth 2 info) 
6972                    (setq articles (sort articles '<)))))
6973     ;; Then we have to re-compute how many unread
6974     ;; articles there are in this group.
6975     (if active
6976         (progn
6977           (cond 
6978            ((not range)
6979             (setq num (- (1+ (cdr active)) (car active))))
6980            ((not (listp (cdr range)))
6981             (setq num (- (cdr active) (- (1+ (cdr range)) 
6982                                          (car range)))))
6983            (t
6984             (while range
6985               (if (numberp (car range))
6986                   (setq num (1+ num))
6987                 (setq num (+ num (- (1+ (cdr (car range)))
6988                                     (car (car range))))))
6989               (setq range (cdr range)))
6990             (setq num (- (cdr active) num))))
6991           ;; Update the number of unread articles.
6992           (setcar 
6993            entry 
6994            (max 0 (- num 
6995                      (length (cdr (assq 'tick (nth 3 info))))
6996                      (length 
6997                       (cdr (assq 'dormant (nth 3 info)))))))
6998           ;; Update the group buffer.
6999           (gnus-group-update-group group t)))))
7000
7001 (defun gnus-methods-equal-p (m1 m2)
7002   (let ((m1 (or m1 gnus-select-method))
7003         (m2 (or m2 gnus-select-method)))
7004     (or (equal m1 m2)
7005         (and (eq (car m1) (car m2))
7006              (or (not (memq 'address (assoc (symbol-name (car m1))
7007                                             gnus-valid-select-methods)))
7008                  (equal (nth 1 m1) (nth 1 m2)))))))
7009
7010 (defsubst gnus-header-value ()
7011   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7012
7013 (defvar gnus-newsgroup-none-id 0)
7014
7015 (defun gnus-get-newsgroup-headers ()
7016   (setq gnus-article-internal-prepare-hook nil)
7017   (let ((cur nntp-server-buffer)
7018         (dependencies gnus-newsgroup-dependencies)
7019         headers id dep end ref)
7020     (save-excursion
7021       (set-buffer nntp-server-buffer)
7022       (goto-char (point-min))
7023       ;; Search to the beginning of the next header. Error messages
7024       ;; do not begin with 2 or 3.
7025       (while (re-search-forward "^[23][0-9]+ " nil t)
7026         (let ((header (make-vector 9 nil))
7027               (case-fold-search t)
7028               (p (point))
7029               in-reply-to)
7030           (setq id nil
7031                 ref nil)
7032           (header-set-number header (read cur))
7033           ;; This implementation of this function, with nine
7034           ;; search-forwards instead of the one re-search-forward and
7035           ;; a case (which basically was the old function) is actually
7036           ;; about twice as fast, even though it looks messier. You
7037           ;; can't have everything, I guess. Speed and elegance
7038           ;; doesn't always come hand in hand.
7039           (save-restriction
7040             (narrow-to-region (point) (or (save-excursion 
7041                                             (search-forward "\n.\n" nil t))
7042                                           (point)))
7043             (if (search-forward "\nfrom: " nil t)
7044                 (header-set-from header (gnus-header-value))
7045               (header-set-from header "(nobody)"))
7046             (goto-char p)
7047             (if (search-forward "\nsubject: " nil t)
7048                 (header-set-subject header (gnus-header-value))
7049               (header-set-subject header "(none)"))
7050             (goto-char p)
7051             (and (search-forward "\nxref: " nil t)
7052                  (header-set-xref header (gnus-header-value)))
7053             (goto-char p)
7054             (or (numberp (and (search-forward "\nlines: " nil t)
7055                               (header-set-lines header (read cur))))
7056                 (header-set-lines header 0))
7057             (goto-char p)
7058             (and (search-forward "\ndate: " nil t)
7059                  (header-set-date header (gnus-header-value)))
7060             (goto-char p)
7061             (if (search-forward "\nmessage-id: " nil t)
7062                 (header-set-id header (setq id (gnus-header-value)))
7063               ;; If there was no message-id, we just fake one to make
7064               ;; subsequent routines simpler.
7065               (header-set-id 
7066                header 
7067                (setq id (concat "none+" 
7068                                 (int-to-string 
7069                                  (setq gnus-newsgroup-none-id 
7070                                        (1+ gnus-newsgroup-none-id)))))))
7071             (goto-char p)
7072             (if (search-forward "\nreferences: " nil t)
7073                 (progn
7074                   (header-set-references header (gnus-header-value))
7075                   (setq end (match-end 0))
7076                   (save-excursion
7077                     (setq ref 
7078                           (downcase
7079                            (buffer-substring
7080                             (progn 
7081                               (end-of-line)
7082                               (search-backward ">" end t)
7083                               (1+ (point)))
7084                             (progn
7085                               (search-backward "<" end t)
7086                               (point)))))))
7087               ;; Get the references from the in-reply-to header if there
7088               ;; ware no references and the in-reply-to header looks
7089               ;; promising. 
7090               (if (and (search-forward "\nin-reply-to: " nil t)
7091                        (setq in-reply-to (gnus-header-value))
7092                        (string-match "<[^>]+>" in-reply-to))
7093                   (progn
7094                     (header-set-references 
7095                      header 
7096                      (setq ref (substring in-reply-to (match-beginning 0)
7097                                           (match-end 0))))
7098                     (setq ref (downcase ref)))
7099                 (setq ref "none")))
7100             ;; We do some threading while we read the headers. The
7101             ;; message-id and the last reference are both entered into
7102             ;; the same hash table. Some tippy-toeing around has to be
7103             ;; done in case an article has arrived before the article
7104             ;; which it refers to.
7105             (if (boundp (setq dep (intern (downcase id) dependencies)))
7106                 (if (car (symbol-value dep))
7107                     ;; An article with this Message-ID has already
7108                     ;; been seen, so we ignore this one, except we add
7109                     ;; any additional Xrefs (in case the two articles
7110                     ;; came from different servers.
7111                     (progn
7112                       (header-set-xref 
7113                        (car (symbol-value dep))
7114                        (concat (or (header-xref (car (symbol-value dep))) "")
7115                                (or (header-xref header) "")))
7116                       (setq header nil))
7117                   (setcar (symbol-value dep) header))
7118               (set dep (list header)))
7119             (if header
7120                 (progn
7121                   (if (boundp (setq dep (intern ref dependencies)))
7122                       (setcdr (symbol-value dep) 
7123                               (cons header (cdr (symbol-value dep))))
7124                     (set dep (list nil header)))
7125                   (setq headers (cons header headers))))
7126             (goto-char (point-max))))))
7127     (nreverse headers)))
7128
7129 ;; The following macros and functions were written by Felix Lee
7130 ;; <flee@cse.psu.edu>. 
7131
7132 (defmacro gnus-nov-read-integer ()
7133   '(prog1
7134        (if (= (following-char) ?\t)
7135            0
7136          (let ((num (condition-case nil (read buffer) (error nil))))
7137            (if (numberp num) num 0)))
7138      (or (eobp) (forward-char 1))))
7139
7140 (defmacro gnus-nov-skip-field ()
7141   '(search-forward "\t" eol 'move))
7142
7143 (defmacro gnus-nov-field ()
7144   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7145
7146 ;; Goes through the xover lines and returns a list of vectors
7147 (defun gnus-get-newsgroup-headers-xover (sequence)
7148   "Parse the news overview data in the server buffer, and return a
7149 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7150   ;; Get the Xref when the users reads the articles since most/some
7151   ;; NNTP servers do not include Xrefs when using XOVER.
7152   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7153   (let ((cur nntp-server-buffer)
7154         (dependencies gnus-newsgroup-dependencies)
7155         number headers header)
7156     (save-excursion
7157       (set-buffer nntp-server-buffer)
7158       (goto-char (point-min))
7159       (while (and sequence (not (eobp)))
7160         (setq number (read cur))
7161         (while (and sequence (< (car sequence) number))
7162           (setq sequence (cdr sequence)))
7163         (and sequence 
7164              (eq number (car sequence))
7165              (progn
7166                (setq sequence (cdr sequence))
7167                (if (setq header 
7168                          (inline (gnus-nov-parse-line number dependencies)))
7169                    (setq headers (cons header headers)))))
7170         (forward-line 1))
7171       (setq headers (nreverse headers)))
7172     headers))
7173
7174 ;; This function has to be called with point after the article number
7175 ;; on the beginning of the line.
7176 (defun gnus-nov-parse-line (number dependencies)
7177   (let ((none 0)
7178         (eol (gnus-point-at-eol)) 
7179         (buffer (current-buffer))
7180         header ref id dep)
7181
7182     ;; overview: [num subject from date id refs chars lines misc]
7183     (narrow-to-region (point) eol)
7184     (forward-char)
7185
7186     (condition-case nil
7187         (setq header
7188               (vector 
7189                number                   ; number
7190                (gnus-nov-field)         ; subject
7191                (gnus-nov-field)         ; from
7192                (gnus-nov-field)         ; date
7193                (setq id (or (gnus-nov-field)
7194                             (concat "none+"
7195                                     (int-to-string 
7196                                      (setq none (1+ none)))))) ; id
7197                (progn
7198                  (save-excursion
7199                    (let ((beg (point)))
7200                      (search-forward "\t" eol)
7201                      (if (search-backward ">" beg t)
7202                          (setq ref 
7203                                (downcase 
7204                                 (buffer-substring 
7205                                  (1+ (point))
7206                                  (progn
7207                                    (search-backward "<" beg t)
7208                                    (point)))))
7209                        (setq ref nil))))
7210                  (gnus-nov-field))      ; refs
7211                (gnus-nov-read-integer)  ; chars
7212                (gnus-nov-read-integer)  ; lines
7213                (if (= (following-char) ?\n)
7214                    nil
7215                  (gnus-nov-field))      ; misc
7216                ))
7217       (error (progn 
7218                (ding)
7219                (message "Strange nov line.")
7220                (setq header nil)
7221                (goto-char eol))))
7222
7223     (widen)
7224
7225     ;; We build the thread tree.
7226     (and header
7227          (if (boundp (setq dep (intern (downcase id) dependencies)))
7228              (if (car (symbol-value dep))
7229                  ;; An article with this Message-ID has already been seen,
7230                  ;; so we ignore this one, except we add any additional
7231                  ;; Xrefs (in case the two articles came from different
7232                  ;; servers.
7233                  (progn
7234                    (header-set-xref 
7235                     (car (symbol-value dep))
7236                     (concat (or (header-xref (car (symbol-value dep))) "")
7237                             (or (header-xref header) "")))
7238                    (setq header nil))
7239                (setcar (symbol-value dep) header))
7240            (set dep (list header))))
7241     (if header
7242         (progn
7243           (if (boundp (setq dep (intern (or ref "none") 
7244                                         dependencies)))
7245               (setcdr (symbol-value dep) 
7246                       (cons header (cdr (symbol-value dep))))
7247             (set dep (list nil header)))))
7248     header))
7249
7250 (defun gnus-article-get-xrefs ()
7251   "Fill in the Xref value in `gnus-current-headers', if necessary.
7252 This is meant to be called in `gnus-article-internal-prepare-hook'."
7253   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7254                                  gnus-current-headers)))
7255     (or (not gnus-use-cross-reference)
7256         (not headers)
7257         (and (header-xref headers)
7258              (not (string= (header-xref headers) "")))
7259         (let ((case-fold-search t)
7260               xref)
7261           (save-restriction
7262             (gnus-narrow-to-headers)
7263             (goto-char (point-min))
7264             (if (or (and (eq (downcase (following-char)) ?x)
7265                          (looking-at "Xref:"))
7266                     (search-forward "\nXref:" nil t))
7267                 (progn
7268                   (goto-char (1+ (match-end 0)))
7269                   (setq xref (buffer-substring (point) 
7270                                                (progn (end-of-line) (point))))
7271                   (header-set-xref headers xref))))))))
7272
7273 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7274 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7275
7276 (defun gnus-make-headers-hashtable-by-number ()
7277   "Make hashtable for the variable gnus-newsgroup-headers by number."
7278   (save-excursion
7279     (set-buffer gnus-summary-buffer)
7280     (let ((headers gnus-newsgroup-headers)
7281           header)
7282       (setq gnus-newsgroup-headers-hashtb-by-number
7283             (gnus-make-hashtable (length headers)))
7284       (while headers
7285         (setq header (car headers))
7286         (gnus-sethash (int-to-string (header-number header))
7287                       header gnus-newsgroup-headers-hashtb-by-number)
7288         (setq headers (cdr headers))))))
7289
7290 (defun gnus-more-header-backward ()
7291   "Find new header backward."
7292   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7293         (artnum gnus-newsgroup-begin)
7294         (header nil))
7295     (while (and (not header)
7296                 (> artnum first))
7297       (setq artnum (1- artnum))
7298       (setq header (gnus-read-header artnum)))
7299     header))
7300
7301 (defun gnus-more-header-forward (&optional backward)
7302   "Find new header forward.
7303 If BACKWARD, find new header backward instead."
7304   (if backward
7305       (gnus-more-header-backward)
7306     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7307           (artnum gnus-newsgroup-end)
7308           (header nil))
7309       (while (and (not header)
7310                   (< artnum last))
7311         (setq artnum (1+ artnum))
7312         (setq header (gnus-read-header artnum)))
7313       header)))
7314
7315 (defun gnus-extend-newsgroup (header &optional backward)
7316   "Extend newsgroup selection with HEADER.
7317 Optional argument BACKWARD means extend toward backward."
7318   (if header
7319       (let ((artnum (header-number header)))
7320         (setq gnus-newsgroup-headers
7321               (if backward
7322                   (cons header gnus-newsgroup-headers)
7323                 (nconc gnus-newsgroup-headers (list header))))
7324         (setq gnus-newsgroup-unselected
7325               (delq artnum gnus-newsgroup-unselected))
7326         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7327         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7328
7329 (defun gnus-summary-work-articles (n)
7330   "Return a list of articles to be worked upon. The prefix argument,
7331 the list of process marked articles, and the current article will be
7332 taken into consideration."
7333   (let (articles)
7334     (if (and n (numberp n))
7335         (let ((backward (< n 0))
7336               (n (abs n)))
7337           (save-excursion
7338             (while (and (> n 0)
7339                         (setq articles (cons (gnus-summary-article-number) 
7340                                              articles))
7341                         (gnus-summary-search-forward nil nil backward))
7342               (setq n (1- n))))
7343           (sort articles (function <)))
7344       (or (reverse gnus-newsgroup-processable)
7345           (list (gnus-summary-article-number))))))
7346
7347 (defun gnus-summary-search-group (&optional backward use-level)
7348   "Search for next unread newsgroup.
7349 If optional argument BACKWARD is non-nil, search backward instead."
7350   (save-excursion
7351     (set-buffer gnus-group-buffer)
7352     (if (gnus-group-search-forward 
7353          backward nil (if use-level (gnus-group-group-level) nil))
7354         (gnus-group-group-name))))
7355
7356 (defun gnus-summary-best-group (&optional exclude-group)
7357   "Find the name of the best unread group.
7358 If EXCLUDE-GROUP, do not go to this group."
7359   (save-excursion
7360     (set-buffer gnus-group-buffer)
7361     (save-excursion
7362       (gnus-group-best-unread-group exclude-group))))
7363
7364 (defun gnus-subject-equal (s1 s2)
7365   (cond
7366    ((null gnus-summary-gather-subject-limit)
7367     (equal (gnus-simplify-subject-re s1)
7368            (gnus-simplify-subject-re s2)))
7369    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7370     (equal (gnus-simplify-subject-fuzzy s1)
7371            (gnus-simplify-subject-fuzzy s2)))
7372    ((numberp gnus-summary-gather-subject-limit)
7373     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7374            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7375    (t
7376     (equal s1 s2))))
7377     
7378 (defun gnus-summary-search-subject (&optional backward unread subject)
7379   "Search for article forward.
7380 If BACKWARD is non-nil, search backward.
7381 If UNREAD is non-nil, only unread articles are selected.
7382 If SUBJECT is non-nil, the article which has the same subject will be
7383 searched for." 
7384   (let ((func (if backward 'previous-single-property-change
7385                 'next-single-property-change))
7386         (beg (point))
7387         (did t)
7388         pos psubject)
7389     (beginning-of-line)
7390     (and gnus-summary-check-current unread
7391          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7392          (setq did nil))
7393     (if (not did)
7394         ()
7395       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7396       (while
7397           (and 
7398            (setq pos (funcall func (point) 'gnus-number))
7399            (goto-char (if backward (1- pos) pos))
7400            (setq did
7401                  (not (and
7402                        (or (not unread)
7403                            (eq (get-text-property (point) 'gnus-mark)
7404                                gnus-unread-mark))
7405                        (or (not subject)
7406                            (and (setq psubject (inline (gnus-summary-subject-string)))
7407                                 (inline (gnus-subject-equal subject psubject)))))))
7408            (if backward (if (bobp) nil (forward-char -1) t)
7409              (if (eobp) nil (forward-char 1) t)))))
7410     (if did
7411         (progn (goto-char beg) nil)
7412       (prog1
7413           (get-text-property (point) 'gnus-number)
7414         (gnus-summary-position-cursor)))))
7415
7416 (defun gnus-summary-pseudo-article ()
7417   "The thread level of the article on the current line."
7418   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7419
7420 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7421 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7422 (defun gnus-summary-article-score ()
7423   "Return current article score."
7424   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7425       gnus-summary-default-score 0))
7426
7427 (defun gnus-summary-recenter ()
7428   "Center point in the summary window.
7429 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7430 displayed, no centering will be performed." 
7431   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7432   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7433   (let* ((top (cond ((< (window-height) 4) 0)
7434                     ((< (window-height) 7) 1)
7435                     (t 2)))
7436          (height (1- (window-height)))
7437          (bottom (save-excursion (goto-char (point-max))
7438                                  (forward-line (- height))
7439                                  (point)))
7440          (window (get-buffer-window (current-buffer))))
7441     (and 
7442      ;; The user has to want it,
7443      gnus-auto-center-summary 
7444      ;; the article buffer must be displayed,
7445      (get-buffer-window gnus-article-buffer)
7446      ;; Set the window start to either `bottom', which is the biggest
7447      ;; possible valid number, or the second line from the top,
7448      ;; whichever is the least.
7449      (set-window-start
7450       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7451
7452 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7453 (defun gnus-short-group-name (group &optional levels)
7454   "Collapse GROUP name LEVELS."
7455   (let* ((name "") (foreign "") (depth -1) (skip 1)
7456          (levels (or levels
7457                      (progn
7458                        (while (string-match "\\." group skip)
7459                          (setq skip (match-end 0)
7460                                depth (+ depth 1)))
7461                        depth))))
7462     (if (string-match ":" group)
7463         (setq foreign (substring group 0 (match-end 0))
7464               group (substring group (match-end 0))))
7465     (while group
7466       (if (and (string-match "\\." group) (> levels 0))
7467           (setq name (concat name (substring group 0 1))
7468                 group (substring group (match-end 0))
7469                 levels (- levels 1)
7470                 name (concat name "."))
7471         (setq name (concat foreign name group)
7472               group nil)))
7473     name))
7474
7475 (defun gnus-summary-jump-to-group (newsgroup)
7476   "Move point to NEWSGROUP in group mode buffer."
7477   ;; Keep update point of group mode buffer if visible.
7478   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7479       (save-window-excursion
7480         ;; Take care of tree window mode.
7481         (if (get-buffer-window gnus-group-buffer)
7482             (pop-to-buffer gnus-group-buffer))
7483         (gnus-group-jump-to-group newsgroup))
7484     (save-excursion
7485       ;; Take care of tree window mode.
7486       (if (get-buffer-window gnus-group-buffer)
7487           (pop-to-buffer gnus-group-buffer)
7488         (set-buffer gnus-group-buffer))
7489       (gnus-group-jump-to-group newsgroup))))
7490
7491 ;; This function returns a list of article numbers based on the
7492 ;; difference between the ranges of read articles in this group and
7493 ;; the range of active articles.
7494 (defun gnus-list-of-unread-articles (group)
7495   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7496          (active (gnus-gethash group gnus-active-hashtb))
7497          (last (cdr active))
7498          first nlast unread)
7499     ;; If none are read, then all are unread. 
7500     (if (not read)
7501         (setq first (car active))
7502       ;; If the range of read articles is a single range, then the
7503       ;; first unread article is the article after the last read
7504       ;; article. Sounds logical, doesn't it?
7505       (if (not (listp (cdr read)))
7506           (setq first (1+ (cdr read)))
7507         ;; `read' is a list of ranges.
7508         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7509                                 (car (car read)))) 1)
7510             (setq first 1))
7511         (while read
7512           (if first 
7513               (while (< first nlast)
7514                 (setq unread (cons first unread))
7515                 (setq first (1+ first))))
7516           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7517           (setq nlast (if (atom (car (cdr read))) 
7518                           (car (cdr read))
7519                         (car (car (cdr read)))))
7520           (setq read (cdr read)))))
7521     ;; And add the last unread articles.
7522     (while (<= first last)
7523       (setq unread (cons first unread))
7524       (setq first (1+ first)))
7525     ;; Return the list of unread articles.
7526     (nreverse unread)))
7527
7528 (defun gnus-list-of-read-articles (group)
7529   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7530         (active (gnus-gethash group gnus-active-hashtb)))
7531     (and info active
7532          (gnus-sorted-complement 
7533           (gnus-uncompress-range active) 
7534           (gnus-list-of-unread-articles group)))))
7535
7536 ;; Various summary commands
7537
7538 (defun gnus-summary-universal-argument ()
7539   "Perform any operation on all articles marked with the process mark."
7540   (interactive)
7541   (gnus-set-global-variables)
7542   (let ((articles (reverse gnus-newsgroup-processable))
7543         func)
7544     (or articles (error "No articles marked"))
7545     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7546         (error "Undefined key"))
7547     (while articles
7548       (gnus-summary-goto-subject (car articles))
7549       (command-execute func)
7550       (gnus-summary-remove-process-mark (car articles))
7551       (setq articles (cdr articles)))))
7552
7553 (defun gnus-summary-toggle-truncation (&optional arg)
7554   "Toggle truncation of summary lines.
7555 With arg, turn line truncation on iff arg is positive."
7556   (interactive "P")
7557   (setq truncate-lines
7558         (if (null arg) (not truncate-lines)
7559           (> (prefix-numeric-value arg) 0)))
7560   (redraw-display))
7561
7562 (defun gnus-summary-reselect-current-group (&optional all)
7563   "Once exit and then reselect the current newsgroup.
7564 The prefix argument ALL means to select all articles."
7565   (interactive "P")
7566   (gnus-set-global-variables)
7567   (let ((current-subject (gnus-summary-article-number))
7568         (group gnus-newsgroup-name))
7569     (setq gnus-newsgroup-begin nil)
7570     (gnus-summary-exit t)
7571     ;; We have to adjust the point of group mode buffer because the
7572     ;; current point was moved to the next unread newsgroup by
7573     ;; exiting.
7574     (gnus-summary-jump-to-group group)
7575     (gnus-group-read-group all t)
7576     (gnus-summary-goto-subject current-subject)))
7577
7578 (defun gnus-summary-rescan-group (&optional all)
7579   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7580   (interactive "P")
7581   (gnus-set-global-variables)
7582   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7583   (let ((group gnus-newsgroup-name))
7584     (gnus-summary-exit)
7585     (gnus-summary-jump-to-group group)
7586     (save-excursion
7587       (set-buffer gnus-group-buffer)
7588       (gnus-group-get-new-news-this-group 1))
7589     (gnus-summary-jump-to-group group)
7590     (gnus-group-read-group all)))
7591
7592 (defun gnus-summary-update-info ()
7593   (let* ((group gnus-newsgroup-name))
7594     (if gnus-newsgroup-kill-headers
7595         (setq gnus-newsgroup-killed
7596               (gnus-compress-sequence
7597                (nconc
7598                 (gnus-set-sorted-intersection
7599                  (gnus-uncompress-range gnus-newsgroup-killed)
7600                  (setq gnus-newsgroup-unselected
7601                        (sort gnus-newsgroup-unselected '<)))
7602                 (setq gnus-newsgroup-unreads
7603                       (sort gnus-newsgroup-unreads '<))) t)))
7604     (or (listp (cdr gnus-newsgroup-killed))
7605         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7606     (let ((headers gnus-newsgroup-headers))
7607       (gnus-close-group group)
7608       (run-hooks 'gnus-exit-group-hook)
7609       (gnus-update-read-articles 
7610        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7611        gnus-newsgroup-marked
7612        t gnus-newsgroup-replied gnus-newsgroup-expirable
7613        gnus-newsgroup-killed gnus-newsgroup-dormant
7614        gnus-newsgroup-bookmarks 
7615        (and gnus-save-score gnus-newsgroup-scored))
7616       (and gnus-use-cross-reference
7617            (gnus-mark-xrefs-as-read 
7618             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7619       ;; Do adaptive scoring, and possibly save score files.
7620       (and gnus-newsgroup-adaptive
7621            (gnus-score-adaptive))
7622       (and gnus-use-scoring 
7623            (fboundp 'gnus-score-save)
7624            (funcall 'gnus-score-save))
7625       ;; Do not switch windows but change the buffer to work.
7626       (set-buffer gnus-group-buffer)
7627       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7628           (gnus-group-update-group group)))))
7629   
7630 (defun gnus-summary-exit (&optional temporary)
7631   "Exit reading current newsgroup, and then return to group selection mode.
7632 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7633   (interactive)
7634   (gnus-set-global-variables)
7635   (gnus-kill-save-kill-buffer)
7636   (let* ((group gnus-newsgroup-name)
7637          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7638                                                   gnus-newsgroup-name))))
7639          (mode major-mode)
7640          (buf (current-buffer)))
7641     (run-hooks 'gnus-summary-prepare-exit-hook)
7642     (gnus-summary-update-info) ; Make all changes in this group permanent.
7643     (set-buffer buf)
7644     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7645     ;; Make sure where I was, and go to next newsgroup.
7646     (set-buffer gnus-group-buffer)
7647     (or quit-config
7648         (progn
7649           (gnus-group-jump-to-group group)
7650           (gnus-group-next-unread-group 1)))
7651     (if temporary
7652         nil                             ;Nothing to do.
7653       ;; We set all buffer-local variables to nil. It is unclear why
7654       ;; this is needed, but if we don't, buffer-local variables are
7655       ;; not garbage-collected, it seems. This would the lead to en
7656       ;; ever-growing Emacs.
7657       (set-buffer buf)
7658       (gnus-summary-clear-local-variables)
7659       ;; We clear the global counterparts of the buffer-local
7660       ;; variables as well, just to be on the safe side.
7661       (gnus-configure-windows 'group 'force)
7662       (gnus-summary-clear-local-variables)
7663       ;; Return to group mode buffer. 
7664       (if (eq mode 'gnus-summary-mode)
7665           (gnus-kill-buffer buf))
7666       (if (get-buffer gnus-article-buffer)
7667           (bury-buffer gnus-article-buffer))
7668       (setq gnus-current-select-method gnus-select-method)
7669       (pop-to-buffer gnus-group-buffer)
7670       (if (not quit-config)
7671           (progn
7672             (gnus-group-jump-to-group group)
7673             (gnus-group-next-unread-group 1))
7674         (if (not (buffer-name (car quit-config)))
7675             (gnus-configure-windows 'group 'force)
7676           (set-buffer (car quit-config))
7677           (and (eq major-mode 'gnus-summary-mode)
7678                (gnus-set-global-variables))
7679           (gnus-configure-windows (cdr quit-config))))
7680       (run-hooks 'gnus-summary-exit-hook))))
7681
7682 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7683 (defun gnus-summary-exit-no-update (&optional no-questions)
7684   "Quit reading current newsgroup without updating read article info."
7685   (interactive)
7686   (gnus-set-global-variables)
7687   (let* ((group gnus-newsgroup-name)
7688          (quit-config (nth 1 (assoc 'quit-config 
7689                                     (gnus-find-method-for-group group)))))
7690     (if (or no-questions
7691             gnus-expert-user
7692             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7693         (progn
7694           (gnus-close-group group)
7695           (gnus-summary-clear-local-variables)
7696           (set-buffer gnus-group-buffer)
7697           (gnus-summary-clear-local-variables)
7698           ;; Return to group selection mode.
7699           (gnus-configure-windows 'group 'force)
7700           (if (get-buffer gnus-summary-buffer)
7701               (kill-buffer gnus-summary-buffer))
7702           (if (get-buffer gnus-article-buffer)
7703               (bury-buffer gnus-article-buffer))
7704           (if (equal (gnus-group-group-name) group)
7705               (gnus-group-next-unread-group 1))
7706           (if quit-config
7707               (progn
7708                 (if (not (buffer-name (car quit-config)))
7709                     (gnus-configure-windows 'group 'force)
7710                   (set-buffer (car quit-config))
7711                   (and (eq major-mode 'gnus-summary-mode)
7712                        (gnus-set-global-variables))
7713                   (gnus-configure-windows (cdr quit-config)))))))))
7714
7715 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7716 (defun gnus-summary-fetch-faq (group)
7717   "Fetch the FAQ for the current group."
7718   (interactive (list gnus-newsgroup-name))
7719   (let ((gnus-faq-buffer 
7720          (find-file (concat gnus-group-faq-directory 
7721                             (gnus-group-real-name group)))))
7722   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7723
7724 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7725 (defun gnus-summary-describe-group (&optional force)
7726   "Describe the current newsgroup."
7727   (interactive "P")
7728   (gnus-group-describe-group force gnus-newsgroup-name))
7729
7730 (defun gnus-summary-describe-briefly ()
7731   "Describe summary mode commands briefly."
7732   (interactive)
7733   (gnus-message 6
7734     (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")))
7735
7736 ;; Walking around group mode buffer from summary mode.
7737
7738 (defun gnus-summary-next-group (&optional no-article target-group backward)
7739   "Exit current newsgroup and then select next unread newsgroup.
7740 If prefix argument NO-ARTICLE is non-nil, no article is selected
7741 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7742 previous group instead."
7743   (interactive "P")
7744   (gnus-set-global-variables)
7745   (let ((current-group gnus-newsgroup-name)
7746         (current-buffer (current-buffer))
7747         entered)
7748     ;; First we semi-exit this group to update Xrefs and all variables.
7749     ;; We can't do a real exit, because the window conf must remain
7750     ;; the same in case the user is prompted for info, and we don't
7751     ;; want the window conf to change before that...
7752     (gnus-summary-exit t)
7753     (while (not entered)
7754       ;; Then we find what group we are supposed to enter.
7755       (set-buffer gnus-group-buffer)
7756       (gnus-group-jump-to-group current-group)
7757       (setq target-group 
7758             (or target-group        
7759                 (if (eq gnus-keep-same-level 'best) 
7760                     (gnus-summary-best-group gnus-newsgroup-name)
7761                   (gnus-summary-search-group backward gnus-keep-same-level))))
7762       (if (not target-group)
7763           ;; There are no further groups, so we return to the group
7764           ;; buffer.
7765           (progn
7766             (gnus-message 5 "Returning to the group buffer")
7767             (setq entered t)
7768             (set-buffer current-buffer)
7769             (gnus-summary-exit))
7770         ;; We try to enter the target group.
7771         (gnus-group-jump-to-group target-group)
7772         (let ((unreads (gnus-group-group-unread)))
7773           (if (and (or (eq t unreads)
7774                        (and unreads (not (zerop unreads))))
7775                    (gnus-summary-read-group
7776                     target-group nil no-article current-buffer))
7777               (setq entered t)
7778             (setq current-group target-group
7779                   target-group nil)))))))
7780
7781 (defun gnus-summary-next-group-old (&optional no-article group backward)
7782   "Exit current newsgroup and then select next unread newsgroup.
7783 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7784 If BACKWARD, go to previous group instead."
7785   (interactive "P")
7786   (gnus-set-global-variables)
7787   (let ((ingroup gnus-newsgroup-name)
7788         (sumbuf (current-buffer))
7789         num)
7790     (set-buffer gnus-group-buffer)
7791     (if (and group
7792              (or (and (numberp (setq num (car (gnus-gethash
7793                                                group gnus-newsrc-hashtb))))
7794                       (< num 1))
7795                  (null num)))
7796         (progn
7797           (gnus-group-jump-to-group group)
7798           (setq group nil))
7799       (gnus-group-jump-to-group ingroup))
7800     (gnus-summary-search-group backward)
7801     (let ((group (or group (gnus-summary-search-group backward))))
7802       (set-buffer sumbuf)
7803       (gnus-summary-exit t)             ;Update all information.
7804       (if (null group)
7805           (gnus-summary-exit-no-update t)
7806         (gnus-group-jump-to-group ingroup)
7807         (setq group (gnus-summary-search-group backward))
7808         (gnus-message 5 "Selecting %s..." group)
7809         (set-buffer gnus-group-buffer)
7810         ;; We are now in group mode buffer.
7811         ;; Make sure group mode buffer point is on GROUP.
7812         (gnus-group-jump-to-group group)
7813         (if (not (eq gnus-auto-select-next 'quietly))
7814             (progn
7815               (gnus-summary-read-group group nil no-article sumbuf)
7816               (and (string= gnus-newsgroup-name ingroup)
7817                    (bufferp sumbuf) (buffer-name sumbuf)
7818                    (progn
7819                      (set-buffer (setq gnus-summary-buffer sumbuf))
7820                      (gnus-summary-exit-no-update t))))
7821           (let ((prevgroup group))
7822             (gnus-group-jump-to-group ingroup)
7823             (setq group (gnus-summary-search-group backward))
7824             (gnus-summary-read-group group nil no-article sumbuf)
7825             (while (and (string= gnus-newsgroup-name ingroup)
7826                         (bufferp sumbuf) 
7827                         (buffer-name sumbuf)
7828                         (not (string= prevgroup (gnus-group-group-name))))
7829               (set-buffer gnus-group-buffer)
7830               (gnus-summary-read-group 
7831                (setq prevgroup (gnus-group-group-name)) 
7832                nil no-article sumbuf))
7833             (and (string= prevgroup (gnus-group-group-name))
7834                  ;; We have reached the final group in the group
7835                  ;; buffer.
7836                  (progn
7837                    (if (buffer-name sumbuf)
7838                        (progn
7839                          (set-buffer sumbuf)
7840                          (gnus-summary-exit)))))))))))
7841
7842 (defun gnus-summary-prev-group (&optional no-article)
7843   "Exit current newsgroup and then select previous unread newsgroup.
7844 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7845   (interactive "P")
7846   (gnus-summary-next-group no-article nil t))
7847
7848 ;; Walking around summary lines.
7849
7850 (defun gnus-summary-first-subject (&optional unread)
7851   "Go to the first unread subject.
7852 If UNREAD is non-nil, go to the first unread article.
7853 Returns nil if there are no unread articles."
7854   (interactive "P")
7855   (prog1
7856       (cond ((not unread)
7857              (goto-char (point-min)))
7858             ((gnus-goto-char 
7859               (text-property-any 
7860                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7861              t)
7862             (t 
7863              ;; There are no unread articles.
7864              (gnus-message 3 "No more unread articles")
7865              nil))
7866     (gnus-summary-position-cursor)))
7867
7868 (defun gnus-summary-next-subject (n &optional unread dont-display)
7869   "Go to next N'th summary line.
7870 If N is negative, go to the previous N'th subject line.
7871 If UNREAD is non-nil, only unread articles are selected.
7872 The difference between N and the actual number of steps taken is
7873 returned."
7874   (interactive "p")
7875   (let ((backward (< n 0))
7876         (n (abs n)))
7877     (while (and (> n 0)
7878                 (gnus-summary-search-forward unread nil backward))
7879       (setq n (1- n)))
7880     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7881                                (if unread " unread" "")))
7882     (or dont-display
7883         (progn
7884           (gnus-summary-recenter)
7885           (gnus-summary-position-cursor)))
7886   n))
7887
7888 (defun gnus-summary-next-unread-subject (n)
7889   "Go to next N'th unread summary line."
7890   (interactive "p")
7891   (gnus-summary-next-subject n t))
7892
7893 (defun gnus-summary-prev-subject (n &optional unread)
7894   "Go to previous N'th summary line.
7895 If optional argument UNREAD is non-nil, only unread article is selected."
7896   (interactive "p")
7897   (gnus-summary-next-subject (- n) unread))
7898
7899 (defun gnus-summary-prev-unread-subject (n)
7900   "Go to previous N'th unread summary line."
7901   (interactive "p")
7902   (gnus-summary-next-subject (- n) t))
7903
7904 (defun gnus-summary-goto-subject (article)
7905   "Go the subject line of ARTICLE."
7906   (interactive
7907    (list
7908     (string-to-int
7909      (completing-read "Article number: "
7910                       (mapcar
7911                        (lambda (headers)
7912                          (list
7913                           (int-to-string (header-number headers))))
7914                        gnus-newsgroup-headers)
7915                       nil 'require-match))))
7916   (or article (error "No article number"))
7917   (let ((b (point)))
7918     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7919                                                 'gnus-number article)))
7920         ()
7921       (gnus-summary-show-thread)
7922       ;; Skip dummy articles. 
7923       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7924           (forward-line 1))
7925       (prog1
7926           (if (not (eobp))
7927               article
7928             (goto-char b)
7929             nil)
7930         (gnus-summary-position-cursor)))))
7931
7932 ;; Walking around summary lines with displaying articles.
7933
7934 (defun gnus-summary-expand-window (&optional arg)
7935   "Make the summary buffer take up the entire Emacs frame.
7936 Given a prefix, will force an `article' buffer configuration."
7937   (interactive "P")
7938   (gnus-set-global-variables)
7939   (if arg
7940       (gnus-configure-windows 'article 'force)
7941     (gnus-configure-windows 'summary 'force)))
7942
7943 (defun gnus-summary-display-article (article &optional all-header)
7944   "Display ARTICLE in article buffer."
7945   (gnus-set-global-variables)
7946   (if (null article)
7947       nil
7948     (prog1
7949         (gnus-article-prepare article all-header)
7950       (gnus-summary-show-thread)
7951       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7952           (progn
7953             (forward-line 1)
7954             (gnus-summary-position-cursor)))
7955       (run-hooks 'gnus-select-article-hook)
7956       (gnus-summary-recenter)
7957       (gnus-summary-goto-subject article)
7958       ;; Successfully display article.
7959       (gnus-summary-update-line)
7960       (gnus-article-set-window-start 
7961        (cdr (assq article gnus-newsgroup-bookmarks)))
7962       t)))
7963
7964 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7965   "Select the current article.
7966 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7967 non-nil, the article will be re-fetched even if it already present in
7968 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7969 be displayed."
7970   (and (not pseudo) (gnus-summary-pseudo-article)
7971        (error "This is a pseudo-article."))
7972   (let ((article (or article (gnus-summary-article-number)))
7973         (all-headers (not (not all-headers))) ;Must be T or NIL.
7974         did) 
7975     (prog1
7976         (save-excursion
7977           (set-buffer gnus-summary-buffer)
7978           (if (or (null gnus-current-article)
7979                   (null gnus-article-current)
7980                   (null (get-buffer gnus-article-buffer))
7981                   (not (eq article (cdr gnus-article-current)))
7982                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7983                   force)
7984               ;; The requested article is different from the current article.
7985               (progn
7986                 (gnus-summary-display-article article all-headers)
7987                 (setq did article))
7988             (if (or all-headers gnus-show-all-headers) 
7989                 (gnus-article-show-all-headers))
7990             nil))
7991       (if did 
7992           (gnus-article-set-window-start 
7993            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7994
7995 (defun gnus-summary-set-current-mark (&optional current-mark)
7996   "Obsolete function."
7997   nil)
7998
7999 (defun gnus-summary-next-article (&optional unread subject backward)
8000   "Select the next article.
8001 If UNREAD, only unread articles are selected.
8002 If SUBJECT, only articles with SUBJECT are selected.
8003 If BACKWARD, the previous article is selected instead of the next."
8004   (interactive "P")
8005   (gnus-set-global-variables)
8006   (let (header)
8007     (cond
8008      ;; Is there such an article?
8009      ((or (gnus-summary-display-article 
8010            (gnus-summary-search-forward unread subject backward))
8011           (eq (gnus-summary-article-mark) gnus-canceled-mark))
8012       (gnus-summary-position-cursor))
8013      ;; If not, we try the first unread, if that is wanted.
8014      ((and subject
8015            gnus-auto-select-same
8016            (gnus-summary-first-unread-article))
8017       (gnus-message 6 "Wrapped"))
8018      ;; Try to get next/previous article not displayed in this group.
8019      ((and gnus-auto-extend-newsgroup
8020            (not unread) (not subject)
8021            (setq header (gnus-more-header-forward backward)))
8022       (gnus-extend-newsgroup header backward)
8023       (let ((buffer-read-only nil))
8024         (goto-char (if backward (point-min) (point-max)))
8025         (gnus-summary-prepare-threads (list header)))
8026       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8027                                    gnus-newsgroup-end)))
8028      ;; Go to next/previous group.
8029      (t
8030       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8031           (gnus-summary-jump-to-group gnus-newsgroup-name))
8032       (let ((cmd (aref (this-command-keys) 0))
8033             (group 
8034              (if (eq gnus-keep-same-level 'best) 
8035                  (gnus-summary-best-group gnus-newsgroup-name)
8036                (gnus-summary-search-group backward gnus-keep-same-level))))
8037         ;; For some reason, the group window gets selected. We change
8038         ;; it back.  
8039         (select-window (get-buffer-window (current-buffer)))
8040         ;; Keep just the event type of CMD.
8041         (and (listp cmd) (setq cmd (car cmd)))
8042         ;; Select next unread newsgroup automagically.
8043         (cond 
8044          ((not gnus-auto-select-next)
8045           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8046          ((eq gnus-auto-select-next 'quietly)
8047           ;; Select quietly.
8048           (if (assoc 'quit-config (gnus-find-method-for-group 
8049                                    gnus-newsgroup-name))
8050               (gnus-summary-exit)
8051             (gnus-message 7 "No more%s articles (%s)..."
8052                           (if unread " unread" "") 
8053                           (if group (concat "selecting " group)
8054                             "exiting"))
8055             (gnus-summary-next-group nil group backward)))
8056          (t
8057           (let ((keystrokes '(?\C-n ?\C-p))
8058                 key)
8059             (while (or (null key) (memq key keystrokes))
8060               (gnus-message 
8061                7 "No more%s articles%s" (if unread " unread" "")
8062                (if (and group (not (assoc 'quit-config
8063                                           (gnus-find-method-for-group 
8064                                            gnus-newsgroup-name))))
8065                    (format " (Type %s for %s [%s])"
8066                            (single-key-description cmd) group
8067                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8068                  (format " (Type %s to exit %s)"
8069                          (single-key-description cmd)
8070                          gnus-newsgroup-name)))
8071               ;; Confirm auto selection.
8072               (let* ((event (read-event)))
8073                 (setq key (if (listp event) (car event) event))
8074                 (if (memq key keystrokes)
8075                     (let ((obuf (current-buffer)))
8076                       (switch-to-buffer gnus-group-buffer)
8077                       (and group
8078                            (gnus-group-jump-to-group group))
8079                       (condition-case ()
8080                           (execute-kbd-macro (char-to-string key))
8081                         (error (ding) nil))
8082                       (setq group (gnus-group-group-name))
8083                       (switch-to-buffer obuf)))))
8084             (if (equal key cmd)
8085                 (if (or (not group) (assoc 'quit-config
8086                                            (gnus-find-method-for-group
8087                                             gnus-newsgroup-name)))
8088                     (gnus-summary-exit)
8089                   (gnus-summary-next-group nil group backward))
8090               (setq unread-command-events (list key)))))))))))
8091
8092 (defun gnus-summary-next-unread-article ()
8093   "Select unread article after current one."
8094   (interactive)
8095   (gnus-summary-next-article t (and gnus-auto-select-same
8096                                     (gnus-summary-subject-string))))
8097
8098 (defun gnus-summary-prev-article (&optional unread subject)
8099   "Select the article after the current one.
8100 If UNREAD is non-nil, only unread articles are selected."
8101   (interactive "P")
8102   (gnus-summary-next-article unread subject t))
8103
8104 (defun gnus-summary-prev-unread-article ()
8105   "Select unred article before current one."
8106   (interactive)
8107   (gnus-summary-prev-article t (and gnus-auto-select-same
8108                                     (gnus-summary-subject-string))))
8109
8110 (defun gnus-summary-next-page (&optional lines circular)
8111   "Show next page of selected article.
8112 If end of article, select next article.
8113 Argument LINES specifies lines to be scrolled up.
8114 If CIRCULAR is non-nil, go to the start of the article instead of 
8115 instead of selecting the next article when reaching the end of the
8116 current article." 
8117   (interactive "P")
8118   (setq gnus-summary-buffer (current-buffer))
8119   (gnus-set-global-variables)
8120   (let ((article (gnus-summary-article-number))
8121         (endp nil))
8122     (gnus-configure-windows 'article)
8123     (if (or (null gnus-current-article)
8124             (null gnus-article-current)
8125             (/= article (cdr gnus-article-current))
8126             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8127         ;; Selected subject is different from current article's.
8128         (gnus-summary-display-article article)
8129       (gnus-eval-in-buffer-window
8130        gnus-article-buffer
8131        (setq endp (gnus-article-next-page lines)))
8132       (if endp
8133           (cond (circular
8134                  (gnus-summary-beginning-of-article))
8135                 (lines
8136                  (gnus-message 3 "End of message"))
8137                 ((null lines)
8138                  (gnus-summary-next-unread-article)))))
8139     (gnus-summary-recenter)
8140     (gnus-summary-position-cursor)))
8141
8142 (defun gnus-summary-prev-page (&optional lines)
8143   "Show previous page of selected article.
8144 Argument LINES specifies lines to be scrolled down."
8145   (interactive "P")
8146   (gnus-set-global-variables)
8147   (let ((article (gnus-summary-article-number)))
8148     (gnus-configure-windows 'article)
8149     (if (or (null gnus-current-article)
8150             (null gnus-article-current)
8151             (/= article (cdr gnus-article-current))
8152             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8153         ;; Selected subject is different from current article's.
8154         (gnus-summary-display-article article)
8155       (gnus-summary-recenter)
8156       (gnus-eval-in-buffer-window gnus-article-buffer
8157         (gnus-article-prev-page lines))))
8158   (gnus-summary-position-cursor))
8159
8160 (defun gnus-summary-scroll-up (lines)
8161   "Scroll up (or down) one line current article.
8162 Argument LINES specifies lines to be scrolled up (or down if negative)."
8163   (interactive "p")
8164   (gnus-set-global-variables)
8165   (gnus-configure-windows 'article)
8166   (or (gnus-summary-select-article nil nil 'pseudo)
8167       (gnus-eval-in-buffer-window 
8168        gnus-article-buffer
8169        (cond ((> lines 0)
8170               (if (gnus-article-next-page lines)
8171                   (gnus-message 3 "End of message")))
8172              ((< lines 0)
8173               (gnus-article-prev-page (- lines))))))
8174   (gnus-summary-recenter)
8175   (gnus-summary-position-cursor))
8176
8177 (defun gnus-summary-next-same-subject ()
8178   "Select next article which has the same subject as current one."
8179   (interactive)
8180   (gnus-set-global-variables)
8181   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8182
8183 (defun gnus-summary-prev-same-subject ()
8184   "Select previous article which has the same subject as current one."
8185   (interactive)
8186   (gnus-set-global-variables)
8187   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8188
8189 (defun gnus-summary-next-unread-same-subject ()
8190   "Select next unread article which has the same subject as current one."
8191   (interactive)
8192   (gnus-set-global-variables)
8193   (gnus-summary-next-article t (gnus-summary-subject-string)))
8194
8195 (defun gnus-summary-prev-unread-same-subject ()
8196   "Select previous unread article which has the same subject as current one."
8197   (interactive)
8198   (gnus-set-global-variables)
8199   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8200
8201 (defun gnus-summary-first-unread-article ()
8202   "Select the first unread article. 
8203 Return nil if there are no unread articles."
8204   (interactive)
8205   (gnus-set-global-variables)
8206   (prog1
8207       (if (gnus-summary-first-subject t)
8208           (progn
8209             (gnus-summary-show-thread)
8210             (gnus-summary-first-subject t)
8211             (gnus-summary-display-article (gnus-summary-article-number))))
8212     (gnus-summary-position-cursor)))
8213
8214 (defun gnus-summary-best-unread-article ()
8215   "Select the unread article with the highest score."
8216   (interactive)
8217   (gnus-set-global-variables)
8218   (let ((scored gnus-newsgroup-scored)
8219         (best -1000000)
8220         article art)
8221     (while scored
8222       (or (> best (cdr (car scored)))
8223           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8224                (not (memq art gnus-newsgroup-marked))
8225                (not (memq art gnus-newsgroup-dormant))
8226                (if (= best (cdr (car scored)))
8227                    (setq article (min art article))
8228                  (setq article art)
8229                  (setq best (cdr (car scored))))))
8230       (setq scored (cdr scored)))
8231     (cond
8232      ((or (not article) (null gnus-newsgroup-unreads))
8233       ;; We didn't find any scored articles, so we just jump to the
8234       ;; first article. 
8235       (gnus-summary-first-unread-article))
8236      ((> best gnus-summary-default-score)
8237       ;; We found one, and it's bigger than the default score, so we
8238       ;; select it.
8239       (gnus-summary-goto-article article))
8240      (t
8241       ;; We found an article, but it has a score lower than the
8242       ;; defaults, so we try to find an article with the default
8243       ;; score. 
8244       (goto-char (point-min))
8245       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8246                       (not (eq (cdr (assq (gnus-summary-article-number)
8247                                           gnus-newsgroup-scored))
8248                                gnus-summary-default-score)))
8249                   (zerop (forward-line 1))
8250                   (not (eobp))))
8251       (if (= (gnus-summary-article-mark) gnus-unread-mark)
8252           ;; We jump to the article we have finally found.
8253           (gnus-summary-goto-article (gnus-summary-article-number))
8254         ;; Or there were no default-scored articles.
8255         (gnus-summary-goto-article article))))
8256     (gnus-summary-position-cursor)))
8257
8258 (defun gnus-summary-goto-article (article &optional all-headers)
8259   "Fetch ARTICLE and display it if it exists.
8260 If ALL-HEADERS is non-nil, no header lines are hidden."
8261   (interactive
8262    (list
8263     (string-to-int
8264      (completing-read 
8265       "Article number: "
8266       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8267               gnus-newsgroup-headers) 
8268       nil 'require-match))))
8269   (prog1
8270       (and (gnus-summary-goto-subject article)
8271            (gnus-summary-display-article article all-headers))
8272     (gnus-summary-position-cursor)))
8273
8274 (defun gnus-summary-goto-last-article ()
8275   "Go to the previously read article."
8276   (interactive)
8277   (prog1
8278       (and gnus-last-article
8279            (gnus-summary-goto-article gnus-last-article))
8280     (gnus-summary-position-cursor)))
8281
8282 (defun gnus-summary-pop-article (number)
8283   "Pop one article off the history and go to the previous.
8284 NUMBER articles will be popped off."
8285   (interactive "p")
8286   (let (to)
8287     (setq gnus-newsgroup-history
8288           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8289     (if to
8290         (gnus-summary-goto-article (car to))
8291       (error "Article history empty")))
8292   (gnus-summary-position-cursor))
8293
8294 ;; Summary article oriented commands
8295
8296 (defun gnus-summary-refer-parent-article (n)
8297   "Refer parent article N times.
8298 The difference between N and the number of articles fetched is returned."
8299   (interactive "p")
8300   (gnus-set-global-variables)
8301   (while 
8302       (and 
8303        (> n 0)
8304        (let ((ref (header-references (gnus-get-header-by-num
8305                                       (gnus-summary-article-number)))))
8306          (if (and ref (not (equal ref ""))
8307                   (string-match "<[^<>]*>[ \t]*$" ref))
8308              (gnus-summary-refer-article 
8309               (substring ref (match-beginning 0) (match-end 0)))
8310            (gnus-message 1 "No references in article %d"
8311                          (gnus-summary-article-number))
8312            nil)))
8313     (setq n (1- n)))
8314   (gnus-summary-position-cursor)
8315   n)
8316     
8317 (defun gnus-summary-refer-article (message-id)
8318   "Refer article specified by MESSAGE-ID.
8319 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8320   (interactive "sMessage-ID: ")
8321   (if (or (not (stringp message-id))
8322           (zerop (length message-id)))
8323       ()
8324     ;; Construct the correct Message-ID if necessary.
8325     ;; Suggested by tale@pawl.rpi.edu.
8326     (or (string-match "^<" message-id)
8327         (setq message-id (concat "<" message-id)))
8328     (or (string-match ">$" message-id)
8329         (setq message-id (concat message-id ">")))
8330     (let ((header (car (gnus-gethash (downcase message-id)
8331                                      gnus-newsgroup-dependencies))))
8332       (if header
8333           (or (gnus-summary-goto-article (header-number header))
8334               ;; The header has been read, but the article had been
8335               ;; expunged, so we insert it again.
8336               (progn
8337                 (gnus-summary-insert-line
8338                  nil header 0 nil gnus-read-mark nil nil
8339                  (header-subject header))
8340                 (forward-line -1)
8341                 (header-number header)))
8342         (let ((gnus-override-method gnus-refer-article-method)
8343               (gnus-ancient-mark gnus-read-mark)
8344               (tmp-point (window-start
8345                           (get-buffer-window gnus-article-buffer)))
8346               number tmp-buf)
8347           (and gnus-refer-article-method
8348                (gnus-check-server gnus-refer-article-method))
8349           ;; Save the old article buffer.
8350           (save-excursion
8351             (set-buffer gnus-article-buffer)
8352             (gnus-kill-buffer " *temp Article*")
8353             (setq tmp-buf (rename-buffer " *temp Article*")))
8354           (prog1
8355               (if (gnus-article-prepare 
8356                    message-id nil (gnus-read-header message-id))
8357                   (progn
8358                     (setq number (header-number gnus-current-headers))
8359                     (gnus-rebuild-thread message-id)
8360                     (gnus-summary-goto-subject number)
8361                     (gnus-summary-recenter)
8362                     (gnus-article-set-window-start 
8363                      (cdr (assq number gnus-newsgroup-bookmarks)))
8364                     message-id)
8365                 ;; We restore the old article buffer.
8366                 (save-excursion
8367                   (kill-buffer gnus-article-buffer)
8368                   (set-buffer tmp-buf)
8369                   (rename-buffer gnus-article-buffer)
8370                   (let ((buffer-read-only nil))
8371                     (and tmp-point
8372                          (set-window-start (get-buffer-window (current-buffer))
8373                                            tmp-point)))))))))))
8374
8375 (defun gnus-summary-enter-digest-group ()
8376   "Enter a digest group based on the current article."
8377   (interactive)
8378   (gnus-set-global-variables)
8379   (gnus-summary-select-article)
8380   ;; We do not want a narrowed article.
8381   (gnus-summary-stop-page-breaking)
8382   (let ((name (format "%s-%d" 
8383                       (gnus-group-prefixed-name 
8384                        gnus-newsgroup-name (list 'nndoc "")) 
8385                       gnus-current-article))
8386         (ogroup gnus-newsgroup-name)
8387         (buf (current-buffer)))
8388     (if (gnus-group-read-ephemeral-group 
8389          name (list 'nndoc name
8390                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8391                     '(nndoc-article-type digest))
8392          t)
8393         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8394                 (list (list (cons 'to-group ogroup))))
8395       (switch-to-buffer buf)
8396       (gnus-set-global-variables)
8397       (gnus-configure-windows 'summary)
8398       (gnus-message 3 "Article not a digest?"))))
8399
8400 (defun gnus-summary-isearch-article ()
8401   "Do incremental search forward on current article."
8402   (interactive)
8403   (gnus-set-global-variables)
8404   (gnus-summary-select-article)
8405   (gnus-eval-in-buffer-window 
8406    gnus-article-buffer (isearch-forward)))
8407
8408 (defun gnus-summary-search-article-forward (regexp &optional backward)
8409   "Search for an article containing REGEXP forward.
8410 If BACKWARD, search backward instead."
8411   (interactive
8412    (list (read-string
8413           (format "Search article %s (regexp%s): "
8414                   (if current-prefix-arg "backward" "forward")
8415                   (if gnus-last-search-regexp
8416                       (concat ", default " gnus-last-search-regexp)
8417                     "")))
8418          current-prefix-arg))
8419   (gnus-set-global-variables)
8420   (if (string-equal regexp "")
8421       (setq regexp (or gnus-last-search-regexp ""))
8422     (setq gnus-last-search-regexp regexp))
8423   (if (gnus-summary-search-article regexp backward)
8424       (gnus-article-set-window-start 
8425        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8426     (error "Search failed: \"%s\"" regexp)))
8427
8428 (defun gnus-summary-search-article-backward (regexp)
8429   "Search for an article containing REGEXP backward."
8430   (interactive
8431    (list (read-string
8432           (format "Search article backward (regexp%s): "
8433                   (if gnus-last-search-regexp
8434                       (concat ", default " gnus-last-search-regexp)
8435                     "")))))
8436   (gnus-summary-search-article-forward regexp 'backward))
8437
8438 (defun gnus-summary-search-article (regexp &optional backward)
8439   "Search for an article containing REGEXP.
8440 Optional argument BACKWARD means do search for backward.
8441 gnus-select-article-hook is not called during the search."
8442   (let ((gnus-select-article-hook nil)  ;Disable hook.
8443         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8444         (re-search
8445          (if backward
8446              (function re-search-backward) (function re-search-forward)))
8447         (found nil)
8448         (last nil))
8449     ;; Hidden thread subtrees must be searched for ,too.
8450     (gnus-summary-show-all-threads)
8451     (if (eobp) (forward-line -1))
8452     ;; First of all, search current article.
8453     ;; We don't want to read article again from NNTP server nor reset
8454     ;; current point.
8455     (gnus-summary-select-article)
8456     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8457     (setq last gnus-current-article)
8458     (gnus-eval-in-buffer-window gnus-article-buffer
8459       (save-restriction
8460         (widen)
8461         ;; Begin search from current point.
8462         (setq found (funcall re-search regexp nil t))))
8463     ;; Then search next articles.
8464     (while (and (not found)
8465                 (gnus-summary-display-article 
8466                  (gnus-summary-search-subject backward nil nil)))
8467       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8468       (gnus-eval-in-buffer-window gnus-article-buffer
8469         (save-restriction
8470           (widen)
8471           (goto-char (if backward (point-max) (point-min)))
8472           (setq found (funcall re-search regexp nil t)))))
8473     (message "")
8474     ;; Adjust article pointer.
8475     (or (eq last gnus-current-article)
8476         (setq gnus-last-article last))
8477     ;; Return T if found such article.
8478     found))
8479
8480 (defun gnus-summary-execute-command (header regexp command &optional backward)
8481   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8482 If HEADER is an empty string (or nil), the match is done on the entire
8483 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8484   (interactive
8485    (list (let ((completion-ignore-case t))
8486            (completing-read 
8487             "Header name: "
8488             (mapcar (lambda (string) (list string))
8489                     '("Number" "Subject" "From" "Lines" "Date"
8490                       "Message-ID" "Xref" "References"))
8491             nil 'require-match))
8492          (read-string "Regexp: ")
8493          (read-key-sequence "Command: ")
8494          current-prefix-arg))
8495   (gnus-set-global-variables)
8496   ;; Hidden thread subtrees must be searched as well.
8497   (gnus-summary-show-all-threads)
8498   ;; We don't want to change current point nor window configuration.
8499   (save-excursion
8500     (save-window-excursion
8501       (gnus-message 6 "Executing %s..." (key-description command))
8502       ;; We'd like to execute COMMAND interactively so as to give arguments.
8503       (gnus-execute header regexp
8504                     (` (lambda ()
8505                          (call-interactively '(, (key-binding command)))))
8506                     backward)
8507       (gnus-message 6 "Executing %s...done" (key-description command)))))
8508
8509 (defun gnus-summary-beginning-of-article ()
8510   "Scroll the article back to the beginning."
8511   (interactive)
8512   (gnus-set-global-variables)
8513   (gnus-summary-select-article)
8514   (gnus-configure-windows 'article)
8515   (gnus-eval-in-buffer-window
8516    gnus-article-buffer
8517    (widen)
8518    (goto-char (point-min))
8519    (and gnus-break-pages (gnus-narrow-to-page))))
8520
8521 (defun gnus-summary-end-of-article ()
8522   "Scroll to the end of the article."
8523   (interactive)
8524   (gnus-set-global-variables)
8525   (gnus-summary-select-article)
8526   (gnus-configure-windows 'article)
8527   (gnus-eval-in-buffer-window 
8528    gnus-article-buffer
8529    (widen)
8530    (goto-char (point-max))
8531    (and gnus-break-pages (gnus-narrow-to-page))))
8532
8533 (defun gnus-summary-show-article ()
8534   "Force re-fetching of the current article."
8535   (interactive)
8536   (gnus-set-global-variables)
8537   (or gnus-current-article
8538       (error "There is no current article"))
8539   (gnus-summary-goto-subject gnus-current-article)
8540   (gnus-summary-select-article nil 'force)
8541   (gnus-configure-windows 'article)
8542   (gnus-summary-position-cursor))
8543
8544 (defun gnus-summary-verbose-headers (&optional arg)
8545   "Toggle permanent full header display.
8546 If ARG is a positive number, turn header display on.
8547 If ARG is a negative number, turn header display off."
8548   (interactive "P")
8549   (gnus-set-global-variables)
8550   (gnus-summary-toggle-header arg)
8551   (setq gnus-show-all-headers
8552         (cond ((or (not (numberp arg))
8553                    (zerop arg))
8554                (not gnus-show-all-headers))
8555               ((natnump arg)
8556                t))))
8557
8558 (defun gnus-summary-toggle-header (&optional arg)
8559   "Show the headers if they are hidden, or hide them if they are shown.
8560 If ARG is a positive number, show the entire header.
8561 If ARG is a negative number, hide the unwanted header lines."
8562   (interactive "P")
8563   (gnus-set-global-variables)
8564   (save-excursion
8565     (set-buffer gnus-article-buffer)
8566     (let ((buffer-read-only nil))
8567       (if (numberp arg) 
8568           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8569                                                 gnus-hidden-properties)
8570             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8571         (if (text-property-any (point-min) (point-max) 'invisible t)
8572             (remove-text-properties 
8573              (point-min) (point-max) gnus-hidden-properties)
8574           ;; We hide the headers. This song and dance act below is
8575           ;; done because `gnus-have-all-headers' is buffer-local to
8576           ;; the summary buffer, and we only want to temporarily
8577           ;; change it in that buffer. Ugh.
8578           (let ((have gnus-have-all-headers))
8579             (save-excursion
8580               (set-buffer gnus-summary-buffer)
8581               (setq gnus-have-all-headers nil)
8582               (save-excursion
8583                 (set-buffer gnus-article-buffer)
8584                 (run-hooks 'gnus-article-display-hook))
8585               (setq gnus-have-all-headers have)))))
8586       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8587
8588 (defun gnus-summary-show-all-headers ()
8589   "Make all header lines visible."
8590   (interactive)
8591   (gnus-set-global-variables)
8592   (gnus-article-show-all-headers))
8593
8594 (defun gnus-summary-toggle-mime (&optional arg)
8595   "Toggle MIME processing.
8596 If ARG is a positive number, turn MIME processing on."
8597   (interactive "P")
8598   (gnus-set-global-variables)
8599   (setq gnus-show-mime
8600         (if (null arg) (not gnus-show-mime)
8601           (> (prefix-numeric-value arg) 0)))
8602   (gnus-summary-select-article t 'force))
8603
8604 (defun gnus-summary-caesar-message (&optional arg)
8605   "Caesar rotate the current article by 13.
8606 The numerical prefix specifies how manu places to rotate each letter
8607 forward."
8608   (interactive "P")
8609   (gnus-set-global-variables)
8610   (gnus-summary-select-article)
8611   (let ((mail-header-separator ""))
8612     (gnus-eval-in-buffer-window 
8613      gnus-article-buffer
8614      (save-restriction
8615        (widen)
8616        (let ((start (window-start)))
8617          (news-caesar-buffer-body arg)
8618          (set-window-start (get-buffer-window (current-buffer)) start))))))
8619
8620 (defun gnus-summary-stop-page-breaking ()
8621   "Stop page breaking in the current article."
8622   (interactive)
8623   (gnus-set-global-variables)
8624   (gnus-summary-select-article)
8625   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8626
8627 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8628
8629 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8630   "Move the current article to a different newsgroup.
8631 If N is a positive number, move the N next articles.
8632 If N is a negative number, move the N previous articles.
8633 If N is nil and any articles have been marked with the process mark,
8634 move those articles instead.
8635 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8636 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8637 re-spool using this method.
8638 For this function to work, both the current newsgroup and the
8639 newsgroup that you want to move to have to support the `request-move'
8640 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8641   (interactive "P")
8642   (gnus-set-global-variables)
8643   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8644       (error "The current newsgroup does not support article moving"))
8645   (let ((articles (gnus-summary-work-articles n))
8646         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8647         art-group to-method sel-met)
8648     (if (and (not to-newsgroup) (not select-method))
8649         (setq to-newsgroup
8650               (completing-read 
8651                (format "Where do you want to move %s? %s"
8652                        (if (> (length articles) 1)
8653                            (format "these %d articles" (length articles))
8654                          "this article")
8655                        (if gnus-current-move-group
8656                            (format "(%s default) " gnus-current-move-group)
8657                          ""))
8658                gnus-active-hashtb nil nil prefix)))
8659     (if to-newsgroup
8660         (progn
8661           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8662               (setq to-newsgroup (or gnus-current-move-group "")))
8663           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8664               (gnus-activate-group to-newsgroup)
8665               (error "No such group: %s" to-newsgroup))
8666           (setq gnus-current-move-group to-newsgroup)))
8667     (setq to-method (if select-method (list select-method "")
8668                       (gnus-find-method-for-group to-newsgroup)))
8669     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8670         (error "%s does not support article copying" (car to-method)))
8671     (or (gnus-check-server to-method)
8672         (error "Can't open server %s" (car to-method)))
8673     (gnus-message 6 "Moving to %s: %s..." 
8674                   (or select-method to-newsgroup) articles)
8675     (while articles
8676       (if (setq art-group
8677                 (gnus-request-move-article 
8678                  (car articles)                   ; Article to move
8679                  gnus-newsgroup-name              ; From newsgrouo
8680                  (nth 1 (gnus-find-method-for-group 
8681                          gnus-newsgroup-name))    ; Server
8682                  (list 'gnus-request-accept-article 
8683                        (if select-method
8684                            (list 'quote select-method)
8685                          to-newsgroup)
8686                        (not (cdr articles)))     ; Accept form
8687                  (not (cdr articles))))          ; Only save nov last time
8688           (let* ((buffer-read-only nil)
8689                  (entry 
8690                   (or
8691                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8692                    (gnus-gethash 
8693                     (gnus-group-prefixed-name 
8694                      (car art-group) 
8695                      (if select-method (list select-method "")
8696                        (gnus-find-method-for-group to-newsgroup)))
8697                     gnus-newsrc-hashtb)))
8698                  (info (nth 2 entry))
8699                  (article (car articles)))
8700             (gnus-summary-goto-subject article)
8701             (beginning-of-line)
8702             (delete-region (point) (progn (forward-line 1) (point)))
8703             ;; Update the group that has been moved to.
8704             (if (not info)
8705                 ()                      ; This group does not exist yet.
8706               (if (not (memq article gnus-newsgroup-unreads))
8707                   (setcar (cdr (cdr info))
8708                           (gnus-add-to-range (nth 2 info) 
8709                                              (list (cdr art-group)))))
8710               ;; Copy any marks over to the new group.
8711               (let ((marks '((tick . gnus-newsgroup-marked)
8712                              (dormant . gnus-newsgroup-dormant)
8713                              (expire . gnus-newsgroup-expirable)
8714                              (bookmark . gnus-newsgroup-bookmarks)
8715                              (reply . gnus-newsgroup-replied)))
8716                     (to-article (cdr art-group)))
8717                 (while marks
8718                   (if (memq article (symbol-value (cdr (car marks))))
8719                       (gnus-add-marked-articles 
8720                        (car info) (car (car marks)) (list to-article) info))
8721                   (setq marks (cdr marks)))))
8722             ;; Update marks.
8723             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8724             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8725             (setq gnus-newsgroup-dormant
8726                   (delq article gnus-newsgroup-dormant))
8727             (setq gnus-newsgroup-reads
8728                   (cons (cons article gnus-canceled-mark)
8729                         gnus-newsgroup-reads)))
8730         (gnus-message 1 "Couldn't move article %s" (car articles)))
8731       (gnus-summary-remove-process-mark (car articles))
8732       (setq articles (cdr articles)))))
8733
8734 (defun gnus-summary-respool-article (&optional n respool-method)
8735   "Respool the current article.
8736 The article will be squeezed through the mail spooling process again,
8737 which means that it will be put in some mail newsgroup or other
8738 depending on `nnmail-split-methods'.
8739 If N is a positive number, respool the N next articles.
8740 If N is a negative number, respool the N previous articles.
8741 If N is nil and any articles have been marked with the process mark,
8742 respool those articles instead.
8743
8744 Respooling can be done both from mail groups and \"real\" newsgroups.
8745 In the former case, the articles in question will be moved from the
8746 current group into whatever groups they are destined to.  In the
8747 latter case, they will be copied into the relevant groups."
8748   (interactive "P")
8749   (gnus-set-global-variables)
8750   (let ((respool-methods (gnus-methods-using 'respool))
8751         (methname 
8752          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8753     (or respool-method
8754         (setq respool-method
8755               (completing-read
8756                "What method do you want to use when respooling? "
8757                respool-methods nil t methname)))
8758     (or (string= respool-method "")
8759         (if (assoc (symbol-name
8760                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8761                    respool-methods)
8762             (gnus-summary-move-article n nil (intern respool-method))
8763           (gnus-summary-copy-article n nil (intern respool-method))))))
8764
8765 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8766 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8767   "Move the current article to a different newsgroup.
8768 If N is a positive number, move the N next articles.
8769 If N is a negative number, move the N previous articles.
8770 If N is nil and any articles have been marked with the process mark,
8771 move those articles instead.
8772 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8773 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8774 re-spool using this method.
8775 For this function to work, the newsgroup that you want to move to have
8776 to support the `request-move' and `request-accept'
8777 functions. (Ie. mail newsgroups at present.)"
8778   (interactive "P")
8779   (gnus-set-global-variables)
8780   (let ((articles (gnus-summary-work-articles n))
8781         (copy-buf (get-buffer-create "*copy work*"))
8782         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8783         art-group to-method)
8784     (buffer-disable-undo copy-buf)
8785     (if (and (not to-newsgroup) (not select-method))
8786         (setq to-newsgroup
8787               (completing-read 
8788                (format "Where do you want to copy %s? %s"
8789                        (if (> (length articles) 1)
8790                            (format "these %d articles" (length articles))
8791                          "this article")
8792                        (if gnus-current-move-group
8793                            (format "(%s default) " gnus-current-move-group)
8794                          ""))
8795                gnus-active-hashtb nil nil prefix)))
8796     (if to-newsgroup
8797         (progn
8798           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8799               (setq to-newsgroup (or gnus-current-move-group "")))
8800           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8801               (gnus-activate-group to-newsgroup)
8802               (error "No such group: %s" to-newsgroup))
8803           (setq gnus-current-move-group to-newsgroup)))
8804     (setq to-method (if select-method (list select-method "")
8805                       (gnus-find-method-for-group to-newsgroup)))
8806     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8807         (error "%s does not support article copying" (car to-method)))
8808     (or (gnus-check-server to-method)
8809         (error "Can't open server %s" (car to-method)))
8810     (while articles
8811       (gnus-message 6 "Copying to %s: %s..." 
8812                     (or select-method to-newsgroup) articles)
8813       (if (setq art-group
8814                 (save-excursion
8815                   (set-buffer copy-buf)
8816                   (gnus-request-article-this-buffer
8817                    (car articles) gnus-newsgroup-name)
8818                   (gnus-request-accept-article
8819                    (if select-method (quote select-method) to-newsgroup)
8820                    (not (cdr articles)))))
8821           (let* ((entry 
8822                   (or
8823                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8824                    (gnus-gethash 
8825                     (gnus-group-prefixed-name 
8826                      (car art-group) 
8827                      (if select-method (list select-method "")
8828                        (gnus-find-method-for-group to-newsgroup)))
8829                     gnus-newsrc-hashtb)))
8830                  (info (nth 2 entry))
8831                  (article (car articles)))
8832             ;; We copy the info over to the new group.
8833             (if (not info)
8834                 ()                      ; This group does not exist (yet).
8835               (if (not (memq article gnus-newsgroup-unreads))
8836                   (setcar (cdr (cdr info))
8837                           (gnus-add-to-range (nth 2 info) 
8838                                              (list (cdr art-group)))))
8839               ;; Copy any marks over to the new group.
8840               (let ((marks '((tick . gnus-newsgroup-marked)
8841                              (dormant . gnus-newsgroup-dormant)
8842                              (expire . gnus-newsgroup-expirable)
8843                              (bookmark . gnus-newsgroup-bookmarks)
8844                              (reply . gnus-newsgroup-replied)))
8845                     (to-article (cdr art-group)))
8846                 (while marks
8847                   (if (memq article (symbol-value (cdr (car marks))))
8848                       (gnus-add-marked-articles 
8849                        (car info) (car (car marks)) (list to-article) info))
8850                   (setq marks (cdr marks))))))
8851         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8852       (gnus-summary-remove-process-mark (car articles))
8853       (setq articles (cdr articles)))
8854     (kill-buffer copy-buf)))
8855
8856 (defun gnus-summary-import-article (file)
8857   "Import a random file into a mail newsgroup."
8858   (interactive "fImport file: ")
8859   (let ((group gnus-newsgroup-name)
8860         atts)
8861     (or (gnus-check-backend-function 'request-accept-article group)
8862         (error "%s does not support article importing" group))
8863     (or (file-readable-p file)
8864         (not (file-regular-p file))
8865         (error "Can't read %s" file))
8866     (save-excursion
8867       (set-buffer (get-buffer-create " *import file*"))
8868       (buffer-disable-undo (current-buffer))
8869       (erase-buffer)
8870       (insert-file-contents file)
8871       (goto-char (point-min))
8872       (if (nnheader-article-p)
8873           ()
8874         (setq atts (file-attributes file))
8875         (insert "From: " (read-string "From: ") "\n"
8876                 "Subject: " (read-string "Subject: ") "\n"
8877                 "Date: " (current-time-string (nth 5 atts)) "\n"
8878                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8879       (gnus-request-accept-article group t)
8880       (kill-buffer (current-buffer)))))
8881
8882 (defun gnus-summary-expire-articles ()
8883   "Expire all articles that are marked as expirable in the current group."
8884   (interactive)
8885   (if (not (gnus-check-backend-function 
8886             'request-expire-articles gnus-newsgroup-name))
8887       ()
8888     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8889                                       gnus-newsrc-hashtb)))
8890            (total (memq 'total-expire (nth 5 info)))
8891            (expirable (if total
8892                           (gnus-list-of-read-articles gnus-newsgroup-name)
8893                         (setq gnus-newsgroup-expirable
8894                               (sort gnus-newsgroup-expirable '<))))
8895            es)
8896       (if (not expirable)
8897           ()
8898         (gnus-message 6 "Expiring articles...")
8899         ;; The list of articles that weren't expired is returned.
8900         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8901         (or total (setq gnus-newsgroup-expirable es))
8902         ;; We go through the old list of expirable, and mark all
8903         ;; really expired articles as non-existant.
8904         (or (eq es expirable) ; If nothing was expired, we don't mark.
8905             (let ((gnus-use-cache nil))
8906               (while expirable
8907                 (or (memq (car expirable) es)
8908                     (gnus-summary-mark-article
8909                      (car expirable) gnus-canceled-mark))
8910                 (setq expirable (cdr expirable)))))
8911         (gnus-message 6 "Expiring articles...done")))))
8912
8913 (defun gnus-summary-expire-articles-now ()
8914   "Expunge all expirable articles in the current group.
8915 This means that *all* articles that are marked as expirable will be
8916 deleted forever, right now."
8917   (interactive)
8918   (or gnus-expert-user
8919       (gnus-y-or-n-p
8920        "Are you really, really, really sure you want to expunge? ")
8921       (error "Phew!"))
8922   (let ((nnmail-expiry-wait -1)
8923         (nnmail-expiry-wait-function nil))
8924     (gnus-summary-expire-articles)))
8925
8926 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8927 (defun gnus-summary-delete-article (&optional n)
8928   "Delete the N next (mail) articles.
8929 This command actually deletes articles. This is not a marking
8930 command. The article will disappear forever from you life, never to
8931 return. 
8932 If N is negative, delete backwards.
8933 If N is nil and articles have been marked with the process mark,
8934 delete these instead."
8935   (interactive "P")
8936   (or (gnus-check-backend-function 'request-expire-articles 
8937                                    gnus-newsgroup-name)
8938       (error "The current newsgroup does not support article deletion."))
8939   ;; Compute the list of articles to delete.
8940   (let ((articles (gnus-summary-work-articles n))
8941         not-deleted)
8942     (if (and gnus-novice-user
8943              (not (gnus-y-or-n-p 
8944                    (format "Do you really want to delete %s forever? "
8945                            (if (> (length articles) 1) "these articles"
8946                              "this article")))))
8947         ()
8948       ;; Delete the articles.
8949       (setq not-deleted (gnus-request-expire-articles 
8950                          articles gnus-newsgroup-name 'force))
8951       (while articles
8952         (gnus-summary-remove-process-mark (car articles))       
8953         ;; The backend might not have been able to delete the article
8954         ;; after all.  
8955         (or (memq (car articles) not-deleted)
8956             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8957         (setq articles (cdr articles))))
8958     (gnus-summary-position-cursor)
8959     not-deleted))
8960
8961 (defun gnus-summary-edit-article ()
8962   "Enter into a buffer and edit the current article.
8963 This will have permanent effect only in mail groups."
8964   (interactive)
8965   (or (gnus-check-backend-function 
8966        'request-replace-article gnus-newsgroup-name)
8967       (error "The current newsgroup does not support article editing."))
8968   (gnus-summary-select-article t)
8969   (gnus-configure-windows 'article)
8970   (select-window (get-buffer-window gnus-article-buffer))
8971   (gnus-message 6 "C-c C-c to end edits")
8972   (setq buffer-read-only nil)
8973   (text-mode)
8974   (use-local-map (copy-keymap (current-local-map)))
8975   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8976   (buffer-enable-undo)
8977   (widen)
8978   (goto-char (point-min))
8979   (search-forward "\n\n" nil t))
8980
8981 (defun gnus-summary-edit-article-done ()
8982   "Make edits to the current article permanent."
8983   (interactive)
8984   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8985     (erase-buffer)
8986     (insert buf)
8987     (if (not (gnus-request-replace-article 
8988               (cdr gnus-article-current) (car gnus-article-current) 
8989               (current-buffer)))
8990         (error "Couldn't replace article.")
8991       (gnus-article-mode)
8992       (use-local-map gnus-article-mode-map)
8993       (setq buffer-read-only t)
8994       (buffer-disable-undo (current-buffer))
8995       (gnus-configure-windows 'summary))
8996       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))))
8997
8998 (defun gnus-summary-edit-article-postpone ()
8999   "Postpone changes to the current article."
9000   (interactive)
9001   (gnus-article-mode)
9002   (use-local-map gnus-article-mode-map)
9003   (setq buffer-read-only t)
9004   (buffer-disable-undo (current-buffer))
9005   (gnus-configure-windows 'summary)
9006   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9007
9008 (defun gnus-summary-fancy-query ()
9009   "Query where the fancy respool algorithm would put this article."
9010   (interactive)
9011   (gnus-summary-select-article)
9012   (save-excursion
9013     (set-buffer gnus-article-buffer)
9014     (save-restriction
9015       (goto-char (point-min))
9016       (search-forward "\n\n")
9017       (narrow-to-region (point-min) (point))
9018       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9019
9020 ;; Summary score commands.
9021
9022 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9023
9024 (defun gnus-summary-raise-score (n)
9025   "Raise the score of the current article by N."
9026   (interactive "p")
9027   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9028
9029 (defun gnus-summary-set-score (n)
9030   "Set the score of the current article to N."
9031   (interactive "p")
9032   ;; Skip dummy header line.
9033   (save-excursion
9034     (gnus-summary-show-thread)
9035     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9036         (forward-line 1))
9037     (let ((buffer-read-only nil))
9038       ;; Set score.
9039       (gnus-summary-update-mark
9040        (if (= n (or gnus-summary-default-score 0)) ? 
9041          (if (< n (or gnus-summary-default-score 0)) 
9042              gnus-score-below-mark gnus-score-over-mark)) 'score))
9043     (let* ((article (gnus-summary-article-number))
9044            (score (assq article gnus-newsgroup-scored)))
9045       (if score (setcdr score n)
9046         (setq gnus-newsgroup-scored 
9047               (cons (cons article n) gnus-newsgroup-scored))))
9048     (gnus-summary-update-line)))
9049
9050 (defun gnus-summary-current-score ()
9051   "Return the score of the current article."
9052   (interactive)
9053   (message "%s" (gnus-summary-article-score)))
9054
9055 ;; Summary marking commands.
9056
9057 (defun gnus-summary-raise-same-subject-and-select (score)
9058   "Raise articles which has the same subject with SCORE and select the next."
9059   (interactive "p")
9060   (let ((subject (gnus-summary-subject-string)))
9061     (gnus-summary-raise-score score)
9062     (while (gnus-summary-search-subject nil nil subject)
9063       (gnus-summary-raise-score score))
9064     (gnus-summary-next-article t)))
9065
9066 (defun gnus-summary-raise-same-subject (score)
9067   "Raise articles which has the same subject with SCORE."
9068   (interactive "p")
9069   (let ((subject (gnus-summary-subject-string)))
9070     (gnus-summary-raise-score score)
9071     (while (gnus-summary-search-subject nil nil subject)
9072       (gnus-summary-raise-score score))
9073     (gnus-summary-next-subject 1 t)))
9074
9075 (defun gnus-score-default (level)
9076   (if level (prefix-numeric-value level) 
9077     gnus-score-interactive-default-score))
9078
9079 (defun gnus-summary-raise-thread (&optional score)
9080   "Raise the score of the articles in the current thread with SCORE."
9081   (interactive "P")
9082   (setq score (1- (gnus-score-default score)))
9083   (let (e)
9084     (save-excursion
9085       (let ((level (gnus-summary-thread-level)))
9086         (gnus-summary-raise-score score)
9087         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9088                     (> (gnus-summary-thread-level) level))
9089           (gnus-summary-raise-score score))
9090         (setq e (point))))
9091     (let ((gnus-summary-check-current t))
9092       (or (zerop (gnus-summary-next-subject 1 t))
9093           (goto-char e))))
9094   (gnus-summary-recenter)
9095   (gnus-summary-position-cursor)
9096   (gnus-set-mode-line 'summary))
9097
9098 (defun gnus-summary-lower-same-subject-and-select (score)
9099   "Raise articles which has the same subject with SCORE and select the next."
9100   (interactive "p")
9101   (gnus-summary-raise-same-subject-and-select (- score)))
9102
9103 (defun gnus-summary-lower-same-subject (score)
9104   "Raise articles which has the same subject with SCORE."
9105   (interactive "p")
9106   (gnus-summary-raise-same-subject (- score)))
9107
9108 (defun gnus-summary-lower-thread (&optional score)
9109   "Lower score of articles in the current thread with SCORE."
9110   (interactive "P")
9111   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9112
9113 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9114   "Mark articles which has the same subject as read, and then select the next.
9115 If UNMARK is positive, remove any kind of mark.
9116 If UNMARK is negative, tick articles."
9117   (interactive "P")
9118   (if unmark
9119       (setq unmark (prefix-numeric-value unmark)))
9120   (let ((count
9121          (gnus-summary-mark-same-subject
9122           (gnus-summary-subject-string) unmark)))
9123     ;; Select next unread article. If auto-select-same mode, should
9124     ;; select the first unread article.
9125     (gnus-summary-next-article t (and gnus-auto-select-same
9126                                       (gnus-summary-subject-string)))
9127     (gnus-message 7 "%d article%s marked as %s"
9128                   count (if (= count 1) " is" "s are")
9129                   (if unmark "unread" "read"))))
9130
9131 (defun gnus-summary-kill-same-subject (&optional unmark)
9132   "Mark articles which has the same subject as read. 
9133 If UNMARK is positive, remove any kind of mark.
9134 If UNMARK is negative, tick articles."
9135   (interactive "P")
9136   (if unmark
9137       (setq unmark (prefix-numeric-value unmark)))
9138   (let ((count
9139          (gnus-summary-mark-same-subject
9140           (gnus-summary-subject-string) unmark)))
9141     ;; If marked as read, go to next unread subject.
9142     (if (null unmark)
9143         ;; Go to next unread subject.
9144         (gnus-summary-next-subject 1 t))
9145     (gnus-message 7 "%d articles are marked as %s"
9146                   count (if unmark "unread" "read"))))
9147
9148 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9149   "Mark articles with same SUBJECT as read, and return marked number.
9150 If optional argument UNMARK is positive, remove any kinds of marks.
9151 If optional argument UNMARK is negative, mark articles as unread instead."
9152   (let ((count 1))
9153     (save-excursion
9154       (cond 
9155        ((null unmark)                   ; Mark as read.
9156         (while (and 
9157                 (progn
9158                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9159                   (gnus-summary-show-thread) t)
9160                 (gnus-summary-search-forward nil subject))
9161           (setq count (1+ count))))
9162        ((> unmark 0)                    ; Tick.
9163         (while (and
9164                 (progn
9165                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9166                   (gnus-summary-show-thread) t)
9167                 (gnus-summary-search-forward nil subject))
9168           (setq count (1+ count))))
9169        (t                               ; Mark as unread.
9170         (while (and
9171                 (progn
9172                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9173                   (gnus-summary-show-thread) t)
9174                 (gnus-summary-search-forward nil subject))
9175           (setq count (1+ count)))))
9176       (gnus-set-mode-line 'summary)
9177       ;; Return the number of marked articles.
9178       count)))
9179
9180 (defun gnus-summary-mark-as-processable (n &optional unmark)
9181   "Set the process mark on the next N articles.
9182 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9183 the process mark instead.  The difference between N and the actual
9184 number of articles marked is returned."
9185   (interactive "p")
9186   (let ((backward (< n 0))
9187         (n (abs n)))
9188   (while (and 
9189           (> n 0)
9190           (if unmark
9191               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9192             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9193           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9194     (setq n (1- n)))
9195   (if (/= 0 n) (gnus-message 7 "No more articles"))
9196   (gnus-summary-recenter)
9197   (gnus-summary-position-cursor)
9198   n))
9199
9200 (defun gnus-summary-unmark-as-processable (n)
9201   "Remove the process mark from the next N articles.
9202 If N is negative, mark backward instead.  The difference between N and
9203 the actual number of articles marked is returned."
9204   (interactive "p")
9205   (gnus-summary-mark-as-processable n t))
9206
9207 (defun gnus-summary-unmark-all-processable ()
9208   "Remove the process mark from all articles."
9209   (interactive)
9210   (save-excursion
9211     (while gnus-newsgroup-processable
9212       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9213   (gnus-summary-position-cursor))
9214
9215 (defun gnus-summary-mark-as-expirable (n)
9216   "Mark N articles forward as expirable.
9217 If N is negative, mark backward instead. The difference between N and
9218 the actual number of articles marked is returned."
9219   (interactive "p")
9220   (gnus-summary-mark-forward n gnus-expirable-mark))
9221
9222 (defun gnus-summary-mark-article-as-replied (article)
9223   "Mark ARTICLE replied and update the summary line."
9224   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9225   (let ((buffer-read-only nil))
9226     (if (gnus-summary-goto-subject article)
9227         (progn
9228           (gnus-summary-update-mark gnus-replied-mark 'replied)
9229           t))))
9230
9231 (defun gnus-summary-set-bookmark (article)
9232   "Set a bookmark in current article."
9233   (interactive (list (gnus-summary-article-number)))
9234   (if (or (not (get-buffer gnus-article-buffer))
9235           (not gnus-current-article)
9236           (not gnus-article-current)
9237           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9238       (error "No current article selected"))
9239   ;; Remove old bookmark, if one exists.
9240   (let ((old (assq article gnus-newsgroup-bookmarks)))
9241     (if old (setq gnus-newsgroup-bookmarks 
9242                   (delq old gnus-newsgroup-bookmarks))))
9243   ;; Set the new bookmark, which is on the form 
9244   ;; (article-number . line-number-in-body).
9245   (setq gnus-newsgroup-bookmarks 
9246         (cons 
9247          (cons article 
9248                (save-excursion
9249                  (set-buffer gnus-article-buffer)
9250                  (count-lines
9251                   (min (point)
9252                        (save-excursion
9253                          (goto-char (point-min))
9254                          (search-forward "\n\n" nil t)
9255                          (point)))
9256                   (point))))
9257          gnus-newsgroup-bookmarks))
9258   (gnus-message 6 "A bookmark has been added to the current article."))
9259
9260 (defun gnus-summary-remove-bookmark (article)
9261   "Remove the bookmark from the current article."
9262   (interactive (list (gnus-summary-article-number)))
9263   ;; Remove old bookmark, if one exists.
9264   (let ((old (assq article gnus-newsgroup-bookmarks)))
9265     (if old 
9266         (progn
9267           (setq gnus-newsgroup-bookmarks 
9268                 (delq old gnus-newsgroup-bookmarks))
9269           (gnus-message 6 "Removed bookmark."))
9270       (gnus-message 6 "No bookmark in current article."))))
9271
9272 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9273 (defun gnus-summary-mark-as-dormant (n)
9274   "Mark N articles forward as dormant.
9275 If N is negative, mark backward instead.  The difference between N and
9276 the actual number of articles marked is returned."
9277   (interactive "p")
9278   (gnus-summary-mark-forward n gnus-dormant-mark))
9279
9280 (defun gnus-summary-set-process-mark (article)
9281   "Set the process mark on ARTICLE and update the summary line."
9282   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9283   (let ((buffer-read-only nil))
9284     (if (gnus-summary-goto-subject article)
9285         (progn
9286           (gnus-summary-show-thread)
9287           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9288                (forward-line 1))
9289           (gnus-summary-update-mark gnus-process-mark 'replied)
9290           t))))
9291
9292 (defun gnus-summary-remove-process-mark (article)
9293   "Remove the process mark from ARTICLE and update the summary line."
9294   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9295   (let ((buffer-read-only nil))
9296     (if (gnus-summary-goto-subject article)
9297         (progn
9298           (gnus-summary-show-thread)
9299           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9300                (forward-line 1))
9301           (gnus-summary-update-mark ?  'replied)
9302           (if (memq article gnus-newsgroup-replied) 
9303               (gnus-summary-update-mark gnus-replied-mark 'replied))
9304           t))))
9305
9306 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9307   "Mark N articles as read forwards.
9308 If N is negative, mark backwards instead.
9309 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9310 marked as unread. 
9311 The difference between N and the actual number of articles marked is
9312 returned."
9313   (interactive "p")
9314   (gnus-set-global-variables)
9315   (let ((backward (< n 0))
9316         (gnus-summary-goto-unread
9317          (and gnus-summary-goto-unread
9318               (not (memq mark (list gnus-unread-mark
9319                                     gnus-ticked-mark gnus-dormant-mark)))))
9320         (n (abs n))
9321         (mark (or mark gnus-del-mark)))
9322   (while (and (> n 0)
9323               (gnus-summary-mark-article nil mark no-expire)
9324               (zerop (gnus-summary-next-subject 
9325                       (if backward -1 1) gnus-summary-goto-unread t)))
9326     (setq n (1- n)))
9327   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9328   (gnus-summary-recenter)
9329   (gnus-summary-position-cursor)
9330   (gnus-set-mode-line 'summary)
9331   n))
9332
9333 (defun gnus-summary-mark-article-as-read (mark)
9334   "Mark the current article quickly as read with MARK."
9335   (let ((article (gnus-summary-article-number)))
9336     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9337     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9338     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9339     (setq gnus-newsgroup-reads
9340           (cons (cons article mark) gnus-newsgroup-reads))
9341     ;; Possibly remove from cache, if that is used. 
9342     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9343     (and gnus-newsgroup-auto-expire 
9344          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9345              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9346              (= mark gnus-read-mark))
9347          (progn
9348            (setq mark gnus-expirable-mark)
9349            (setq gnus-newsgroup-expirable 
9350                  (cons article gnus-newsgroup-expirable))))
9351     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9352       (forward-line 1))
9353     ;; Fix the mark.
9354     (gnus-summary-update-mark mark 'unread)
9355     t))
9356
9357 (defun gnus-summary-mark-article-as-unread (mark)
9358   "Mark the current article quickly as unread with MARK."
9359   (let ((article (gnus-summary-article-number)))
9360     (or (memq article gnus-newsgroup-unreads)
9361         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9362     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9363     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9364     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9365     (setq gnus-newsgroup-reads
9366           (delq (assq article gnus-newsgroup-reads)
9367                 gnus-newsgroup-reads))
9368     (if (= mark gnus-ticked-mark)
9369         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9370     (if (= mark gnus-dormant-mark)
9371         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9372
9373     ;; See whether the article is to be put in the cache.
9374     (and gnus-use-cache
9375          (vectorp (gnus-get-header-by-num article))
9376          (save-excursion
9377            (gnus-cache-possibly-enter-article 
9378             gnus-newsgroup-name article 
9379             (gnus-get-header-by-num article)
9380             (= mark gnus-ticked-mark)
9381             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9382
9383     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9384       (forward-line 1))
9385     ;; Fix the mark.
9386     (gnus-summary-update-mark mark 'unread)
9387     t))
9388
9389 (defun gnus-summary-mark-article (&optional article mark no-expire)
9390   "Mark ARTICLE with MARK.  MARK can be any character.
9391 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9392 (dormant) and `?E' (expirable).
9393 If MARK is nil, then the default character `?D' is used.
9394 If ARTICLE is nil, then the article on the current line will be
9395 marked." 
9396   (and (stringp mark)
9397        (setq mark (aref mark 0)))
9398   ;; If no mark is given, then we check auto-expiring.
9399   (and (not no-expire)
9400        gnus-newsgroup-auto-expire 
9401        (or (not mark)
9402            (and (numberp mark) 
9403                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9404                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9405                     (= mark gnus-read-mark))))
9406        (setq mark gnus-expirable-mark))
9407   (let* ((mark (or mark gnus-del-mark))
9408          (article (or article (gnus-summary-article-number))))
9409     (or article (error "No article on current line"))
9410     (if (or (= mark gnus-unread-mark) 
9411             (= mark gnus-ticked-mark) 
9412             (= mark gnus-dormant-mark))
9413         (gnus-mark-article-as-unread article mark)
9414       (gnus-mark-article-as-read article mark))
9415
9416     ;; See whether the article is to be put in the cache.
9417     (and gnus-use-cache
9418          (not (= mark gnus-canceled-mark))
9419          (vectorp (gnus-get-header-by-num article))
9420          (save-excursion
9421            (gnus-cache-possibly-enter-article 
9422             gnus-newsgroup-name article 
9423             (gnus-get-header-by-num article)
9424             (= mark gnus-ticked-mark)
9425             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9426
9427     (if (gnus-summary-goto-subject article)
9428         (let ((buffer-read-only nil))
9429           (gnus-summary-show-thread)
9430           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9431                (forward-line 1))
9432           ;; Fix the mark.
9433           (gnus-summary-update-mark mark 'unread)
9434           t))))
9435
9436 (defun gnus-summary-update-mark (mark type)
9437   (beginning-of-line)
9438   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9439         (buffer-read-only nil)
9440         plist)
9441     (if (not forward)
9442         ()
9443       (forward-char forward)
9444       (setq plist (text-properties-at (point)))
9445       (delete-char 1)
9446       (insert mark)
9447       (and plist (add-text-properties (1- (point)) (point) plist))
9448       (and (eq type 'unread)
9449            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9450       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9451   
9452 (defun gnus-mark-article-as-read (article &optional mark)
9453   "Enter ARTICLE in the pertinent lists and remove it from others."
9454   ;; Make the article expirable.
9455   (let ((mark (or mark gnus-del-mark)))
9456     (if (= mark gnus-expirable-mark)
9457         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9458       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9459     ;; Remove from unread and marked lists.
9460     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9461     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9462     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9463     (setq gnus-newsgroup-reads 
9464           (cons (cons article mark) gnus-newsgroup-reads))
9465     ;; Possibly remove from cache, if that is used. 
9466     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9467
9468 (defun gnus-mark-article-as-unread (article &optional mark)
9469   "Enter ARTICLE in the pertinent lists and remove it from others."
9470   (let ((mark (or mark gnus-ticked-mark)))
9471     ;; Add to unread list.
9472     (or (memq article gnus-newsgroup-unreads)
9473         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9474     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9475     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9476     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9477     (setq gnus-newsgroup-reads
9478           (delq (assq article gnus-newsgroup-reads)
9479                 gnus-newsgroup-reads))
9480     (if (= mark gnus-ticked-mark)
9481         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9482     (if (= mark gnus-dormant-mark)
9483         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9484
9485 (defalias 'gnus-summary-mark-as-unread-forward 
9486   'gnus-summary-tick-article-forward)
9487 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9488                'gnus-summary-tick-article-forward)
9489 (defun gnus-summary-tick-article-forward (n)
9490   "Tick N articles forwards.
9491 If N is negative, tick backwards instead.
9492 The difference between N and the number of articles ticked is returned."
9493   (interactive "p")
9494   (gnus-summary-mark-forward n gnus-ticked-mark))
9495
9496 (defalias 'gnus-summary-mark-as-unread-backward 
9497   'gnus-summary-tick-article-backward)
9498 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9499                'gnus-summary-tick-article-backward)
9500 (defun gnus-summary-tick-article-backward (n)
9501   "Tick N articles backwards.
9502 The difference between N and the number of articles ticked is returned."
9503   (interactive "p")
9504   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9505
9506 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9507 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9508 (defun gnus-summary-tick-article (&optional article clear-mark)
9509   "Mark current article as unread.
9510 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9511 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9512   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9513                                        gnus-ticked-mark)))
9514
9515 (defun gnus-summary-mark-as-read-forward (n)
9516   "Mark N articles as read forwards.
9517 If N is negative, mark backwards instead.
9518 The difference between N and the actual number of articles marked is
9519 returned."
9520   (interactive "p")
9521   (gnus-summary-mark-forward n gnus-del-mark t))
9522
9523 (defun gnus-summary-mark-as-read-backward (n)
9524   "Mark the N articles as read backwards.
9525 The difference between N and the actual number of articles marked is
9526 returned."
9527   (interactive "p")
9528   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9529
9530 (defun gnus-summary-mark-as-read (&optional article mark)
9531   "Mark current article as read.
9532 ARTICLE specifies the article to be marked as read.
9533 MARK specifies a string to be inserted at the beginning of the line."
9534   (gnus-summary-mark-article article mark))
9535
9536 (defun gnus-summary-clear-mark-forward (n)
9537   "Clear marks from N articles forward.
9538 If N is negative, clear backward instead.
9539 The difference between N and the number of marks cleared is returned."
9540   (interactive "p")
9541   (gnus-summary-mark-forward n gnus-unread-mark))
9542
9543 (defun gnus-summary-clear-mark-backward (n)
9544   "Clear marks from N articles backward.
9545 The difference between N and the number of marks cleared is returned."
9546   (interactive "p")
9547   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9548
9549 (defun gnus-summary-mark-unread-as-read ()
9550   "Intended to be used by `gnus-summary-mark-article-hook'."
9551   (or (memq gnus-current-article gnus-newsgroup-marked)
9552       (memq gnus-current-article gnus-newsgroup-dormant)
9553       (memq gnus-current-article gnus-newsgroup-expirable)
9554       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9555
9556 (defun gnus-summary-mark-region-as-read (point mark all)
9557   "Mark all unread articles between point and mark as read.
9558 If given a prefix, mark all articles between point and mark as read,
9559 even ticked and dormant ones."
9560   (interactive "r\nP")
9561   (save-excursion
9562     (goto-char point)
9563     (beginning-of-line)
9564     (while (and 
9565             (< (point) mark)
9566             (progn
9567               (and
9568                (or all
9569                    (and
9570                     (not (memq (gnus-summary-article-number)
9571                                gnus-newsgroup-marked))
9572                     (not (memq (gnus-summary-article-number)
9573                                gnus-newsgroup-dormant))))
9574                (gnus-summary-mark-article
9575                 (gnus-summary-article-number) gnus-del-mark))
9576               t)
9577             (zerop (forward-line 1))))))
9578
9579 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9580 (defalias 'gnus-summary-delete-marked-as-read 
9581   'gnus-summary-remove-lines-marked-as-read)
9582 (make-obsolete 'gnus-summary-delete-marked-as-read 
9583                'gnus-summary-remove-lines-marked-as-read)
9584 (defun gnus-summary-remove-lines-marked-as-read ()
9585   "Remove lines that are marked as read."
9586   (interactive)
9587   (gnus-summary-remove-lines-marked-with 
9588    (concat (mapconcat
9589             (lambda (char) (char-to-string (symbol-value char)))
9590             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9591               gnus-killed-mark gnus-kill-file-mark
9592               gnus-low-score-mark gnus-expirable-mark
9593               gnus-canceled-mark gnus-catchup-mark)
9594             ""))))
9595
9596 (defalias 'gnus-summary-delete-marked-with 
9597   'gnus-summary-remove-lines-marked-with)
9598 (make-obsolete 'gnus-summary-delete-marked-with 
9599                'gnus-summary-remove-lines-marked-with)
9600 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9601 (defun gnus-summary-remove-lines-marked-with (marks)
9602   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9603   (interactive "sMarks: ")
9604   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9605   (gnus-set-global-variables)
9606   (let ((buffer-read-only nil)
9607         (marks (concat "^[" marks "]")))
9608     (goto-char (point-min))
9609     (if gnus-newsgroup-adaptive
9610         (gnus-score-remove-lines-adaptive marks)
9611       (while (re-search-forward marks nil t)
9612         (gnus-delete-line)))
9613     ;; If we use dummy roots, we have to do an additional sweep over
9614     ;; the buffer.
9615     (if (not (eq gnus-summary-make-false-root 'dummy))
9616         ()
9617       (goto-char (point-min))
9618       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9619       (while (re-search-forward marks nil t)
9620         (if (gnus-subject-equal
9621              (gnus-summary-subject-string)
9622              (progn
9623                (forward-line 1)
9624                (gnus-summary-subject-string)))
9625             ()
9626           (forward-line -1)
9627           (gnus-delete-line)))))
9628   (or (zerop (buffer-size))
9629       (if (eobp)
9630           (gnus-summary-prev-subject 1)
9631         (gnus-summary-position-cursor))))
9632
9633 (defun gnus-summary-expunge-below (&optional score)
9634   "Remove articles with score less than SCORE."
9635   (interactive "P")
9636   (gnus-set-global-variables)
9637   (setq score (if score
9638                   (prefix-numeric-value score)
9639                 (or gnus-summary-default-score 0)))
9640   (save-excursion
9641     (set-buffer gnus-summary-buffer)
9642     (goto-char (point-min))
9643     (let ((buffer-read-only nil)
9644           beg)
9645       (while (not (eobp))
9646         (if (< (gnus-summary-article-score) score)
9647             (progn
9648               (setq beg (point))
9649               (forward-line 1)
9650               (delete-region beg (point)))
9651           (forward-line 1)))
9652       ;; Adjust point.
9653       (or (zerop (buffer-size))
9654           (if (eobp)
9655               (gnus-summary-prev-subject 1)
9656             (gnus-summary-position-cursor))))))
9657
9658 (defun gnus-summary-mark-below (score mark)
9659   "Mark articles with score less than SCORE with MARK."
9660   (interactive "P\ncMark: ")
9661   (gnus-set-global-variables)
9662   (setq score (if score
9663                   (prefix-numeric-value score)
9664                 (or gnus-summary-default-score 0)))
9665   (save-excursion
9666     (set-buffer gnus-summary-buffer)
9667     (goto-char (point-min))
9668     (while (not (eobp))
9669       (and (< (gnus-summary-article-score) score)
9670            (gnus-summary-mark-article nil mark))
9671       (forward-line 1))))
9672
9673 (defun gnus-summary-kill-below (&optional score)
9674   "Mark articles with score below SCORE as read."
9675   (interactive "P")
9676   (gnus-set-global-variables)
9677   (gnus-summary-mark-below score gnus-killed-mark))
9678
9679 (defun gnus-summary-clear-above (&optional score)
9680   "Clear all marks from articles with score above SCORE."
9681   (interactive "P")
9682   (gnus-set-global-variables)
9683   (gnus-summary-mark-above score gnus-unread-mark))
9684
9685 (defun gnus-summary-tick-above (&optional score)
9686   "Tick all articles with score above SCORE."
9687   (interactive "P")
9688   (gnus-set-global-variables)
9689   (gnus-summary-mark-above score gnus-ticked-mark))
9690
9691 (defun gnus-summary-mark-above (score mark)
9692   "Mark articles with score over SCORE with MARK."
9693   (interactive "P\ncMark: ")
9694   (gnus-set-global-variables)
9695   (setq score (if score
9696                   (prefix-numeric-value score)
9697                 (or gnus-summary-default-score 0)))
9698   (save-excursion
9699     (set-buffer gnus-summary-buffer)
9700     (goto-char (point-min))
9701     (while (not (eobp))
9702       (if (> (gnus-summary-article-score) score)
9703           (progn
9704             (gnus-summary-mark-article nil mark)
9705             (forward-line 1))
9706         (forward-line 1)))))
9707
9708 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9709 (defun gnus-summary-show-all-expunged ()
9710   "Display all the hidden articles that were expunged for low scores."
9711   (interactive)
9712   (gnus-set-global-variables)
9713   (let ((buffer-read-only nil))
9714     (let ((scored gnus-newsgroup-scored)
9715           headers h)
9716       (while scored
9717         (or (gnus-summary-goto-subject (car (car scored)))
9718             (and (setq h (gnus-get-header-by-num (car (car scored))))
9719                  (< (cdr (car scored)) gnus-summary-expunge-below)
9720                  (setq headers (cons h headers))))
9721         (setq scored (cdr scored)))
9722       (or headers (error "No expunged articles hidden."))
9723       (goto-char (point-min))
9724       (save-excursion 
9725         (gnus-summary-update-lines 
9726          (point)
9727          (progn
9728            (gnus-summary-prepare-unthreaded (nreverse headers))
9729            (point)))))
9730     (goto-char (point-min))
9731     (gnus-summary-position-cursor)))
9732
9733 (defun gnus-summary-show-all-dormant ()
9734   "Display all the hidden articles that are marked as dormant."
9735   (interactive)
9736   (gnus-set-global-variables)
9737   (let ((buffer-read-only nil))
9738     (let ((dormant gnus-newsgroup-dormant)
9739           headers h)
9740       (while dormant
9741         (or (gnus-summary-goto-subject (car dormant))
9742             (and (setq h (gnus-get-header-by-num (car dormant)))
9743                  (setq headers (cons h headers))))
9744         (setq dormant (cdr dormant)))
9745       (or headers (error "No dormant articles hidden."))
9746       (goto-char (point-min))
9747       (save-excursion 
9748         (gnus-summary-update-lines 
9749          (point)
9750          (progn
9751            (gnus-summary-prepare-unthreaded (nreverse headers))
9752            (point)))))
9753     (goto-char (point-min))
9754     (gnus-summary-position-cursor)))
9755
9756 (defun gnus-summary-hide-all-dormant ()
9757   "Hide all dormant articles."
9758   (interactive)
9759   (gnus-set-global-variables)
9760   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9761   (gnus-summary-position-cursor))
9762
9763 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9764   "Mark all articles not marked as unread in this newsgroup as read.
9765 If prefix argument ALL is non-nil, all articles are marked as read.
9766 If QUIETLY is non-nil, no questions will be asked.
9767 If TO-HERE is non-nil, it should be a point in the buffer. All
9768 articles before this point will be marked as read.
9769 The number of articles marked as read is returned."
9770   (interactive "P")
9771   (gnus-set-global-variables)
9772   (prog1
9773       (if (or quietly
9774               (not gnus-interactive-catchup) ;Without confirmation?
9775               gnus-expert-user
9776               (gnus-y-or-n-p
9777                (if all
9778                    "Mark absolutely all articles as read? "
9779                  "Mark all unread articles as read? ")))
9780           (if (and not-mark 
9781                    (not gnus-newsgroup-adaptive)
9782                    (not gnus-newsgroup-auto-expire))
9783               (progn
9784                 (and all (setq gnus-newsgroup-marked nil
9785                                gnus-newsgroup-dormant nil))
9786                 (setq gnus-newsgroup-unreads 
9787                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9788             ;; We actually mark all articles as canceled, which we
9789             ;; have to do when using auto-expiry or adaptive scoring. 
9790             (let ((unreads (length gnus-newsgroup-unreads)))
9791               (gnus-summary-show-all-threads)
9792               (if (gnus-summary-first-subject (not all))
9793                   (while (and 
9794                           (if to-here (< (point) to-here) t)
9795                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9796                           (gnus-summary-search-subject nil (not all)))))
9797               (- unreads (length gnus-newsgroup-unreads))
9798               (or to-here
9799                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9800     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9801       (if (and (not to-here) (eq 'nnvirtual (car method)))
9802           (nnvirtual-catchup-group
9803            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9804     (gnus-summary-position-cursor)))
9805
9806 (defun gnus-summary-catchup-to-here (&optional all)
9807   "Mark all unticked articles before the current one as read.
9808 If ALL is non-nil, also mark ticked and dormant articles as read."
9809   (interactive)
9810   (gnus-set-global-variables)
9811   (save-excursion
9812     (and (zerop (forward-line -1))
9813          (progn
9814            (end-of-line)
9815            (gnus-summary-catchup all t (point))
9816            (gnus-set-mode-line 'summary))))
9817   (gnus-summary-position-cursor))
9818
9819 (defun gnus-summary-catchup-all (&optional quietly)
9820   "Mark all articles in this newsgroup as read."
9821   (interactive)
9822   (gnus-set-global-variables)
9823   (gnus-summary-catchup t quietly))
9824
9825 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9826   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9827 If prefix argument ALL is non-nil, all articles are marked as read."
9828   (interactive "P")
9829   (gnus-set-global-variables)
9830   (gnus-summary-catchup all quietly nil 'fast)
9831   ;; Select next newsgroup or exit.
9832   (if (eq gnus-auto-select-next 'quietly)
9833       (gnus-summary-next-group nil)
9834     (gnus-summary-exit)))
9835
9836 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9837   "Mark all articles in this newsgroup as read, and then exit."
9838   (interactive)
9839   (gnus-set-global-variables)
9840   (gnus-summary-catchup-and-exit t quietly))
9841
9842 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9843 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9844   "Mark all articles in this group as read and select the next group.
9845 If given a prefix, mark all articles, unread as well as ticked, as
9846 read." 
9847   (interactive "P")
9848   (gnus-set-global-variables)
9849   (gnus-summary-catchup all)
9850   (gnus-summary-next-group))
9851
9852 ;; Thread-based commands.
9853
9854 (defun gnus-summary-toggle-threads (&optional arg)
9855   "Toggle showing conversation threads.
9856 If ARG is positive number, turn showing conversation threads on."
9857   (interactive "P")
9858   (gnus-set-global-variables)
9859   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9860     (setq gnus-show-threads
9861           (if (null arg) (not gnus-show-threads)
9862             (> (prefix-numeric-value arg) 0)))
9863     (gnus-summary-prepare)
9864     (gnus-summary-goto-subject current)
9865     (gnus-summary-position-cursor)))
9866
9867 (defun gnus-summary-show-all-threads ()
9868   "Show all threads."
9869   (interactive)
9870   (gnus-set-global-variables)
9871   (save-excursion
9872     (let ((buffer-read-only nil))
9873       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9874   (gnus-summary-position-cursor))
9875
9876 (defun gnus-summary-show-thread ()
9877   "Show thread subtrees.
9878 Returns nil if no thread was there to be shown."
9879   (interactive)
9880   (gnus-set-global-variables)
9881   (let ((buffer-read-only nil)
9882         (orig (point))
9883         ;; first goto end then to beg, to have point at beg after let
9884         (end (progn (end-of-line) (point)))
9885         (beg (progn (beginning-of-line) (point))))
9886     (prog1
9887         ;; Any hidden lines here?
9888         (search-forward "\r" end t)
9889       (subst-char-in-region beg end ?\^M ?\n t)
9890       (goto-char orig)
9891       (gnus-summary-position-cursor))))
9892
9893 (defun gnus-summary-hide-all-threads ()
9894   "Hide all thread subtrees."
9895   (interactive)
9896   (gnus-set-global-variables)
9897   (save-excursion
9898     (goto-char (point-min))
9899     (gnus-summary-hide-thread)
9900     (while (and (not (eobp)) (zerop (forward-line 1)))
9901       (gnus-summary-hide-thread)))
9902   (gnus-summary-position-cursor))
9903
9904 (defun gnus-summary-hide-thread ()
9905   "Hide thread subtrees.
9906 Returns nil if no threads were there to be hidden."
9907   (interactive)
9908   (gnus-set-global-variables)
9909   (let ((buffer-read-only nil)
9910         (start (point))
9911         (level (gnus-summary-thread-level))
9912         (end (point)))
9913     ;; Go forward until either the buffer ends or the subthread
9914     ;; ends. 
9915     (if (eobp)
9916         ()
9917       (while (and (zerop (forward-line 1))
9918                   (> (gnus-summary-thread-level) level))
9919         (setq end (point)))
9920       (prog1
9921           (save-excursion
9922             (goto-char end)
9923             (search-backward "\n" start t))
9924         (subst-char-in-region start end ?\n ?\^M t)
9925         (forward-line -1)))))
9926
9927 (defun gnus-summary-go-to-next-thread (&optional previous)
9928   "Go to the same level (or less) next thread.
9929 If PREVIOUS is non-nil, go to previous thread instead.
9930 Return the article number moved to, or nil if moving was impossible."
9931   (let ((level (gnus-summary-thread-level))
9932         (article (gnus-summary-article-number)))
9933     (if previous 
9934         (while (and (zerop (forward-line -1))
9935                     (> (gnus-summary-thread-level) level)))
9936       (while (and (save-excursion
9937                     (forward-line 1)
9938                     (not (eobp)))
9939                   (zerop (forward-line 1))
9940                   (> (gnus-summary-thread-level) level))))
9941     (gnus-summary-recenter)
9942     (gnus-summary-position-cursor)
9943     (let ((oart (gnus-summary-article-number)))
9944       (and (/= oart article) oart))))
9945
9946 (defun gnus-summary-next-thread (n)
9947   "Go to the same level next N'th thread.
9948 If N is negative, search backward instead.
9949 Returns the difference between N and the number of skips actually
9950 done."
9951   (interactive "p")
9952   (gnus-set-global-variables)
9953   (let ((backward (< n 0))
9954         (n (abs n)))
9955   (while (and (> n 0)
9956               (gnus-summary-go-to-next-thread backward))
9957     (setq n (1- n)))
9958   (gnus-summary-position-cursor)
9959   (if (/= 0 n) (gnus-message 7 "No more threads"))
9960   n))
9961
9962 (defun gnus-summary-prev-thread (n)
9963   "Go to the same level previous N'th thread.
9964 Returns the difference between N and the number of skips actually
9965 done."
9966   (interactive "p")
9967   (gnus-set-global-variables)
9968   (gnus-summary-next-thread (- n)))
9969
9970 (defun gnus-summary-go-down-thread (&optional same)
9971   "Go down one level in the current thread.
9972 If SAME is non-nil, also move to articles of the same level."
9973   (let ((level (gnus-summary-thread-level))
9974         (start (point)))
9975     (if (and (zerop (forward-line 1))
9976              (> (gnus-summary-thread-level) level))
9977         t
9978       (goto-char start)
9979       nil)))
9980
9981 (defun gnus-summary-go-up-thread ()
9982   "Go up one level in the current thread."
9983   (let ((level (gnus-summary-thread-level))
9984         (start (point)))
9985     (while (and (zerop (forward-line -1))
9986                 (>= (gnus-summary-thread-level) level)))
9987     (if (>= (gnus-summary-thread-level) level)
9988         (progn
9989           (goto-char start)
9990           nil)
9991       t)))
9992
9993 (defun gnus-summary-down-thread (n)
9994   "Go down thread N steps.
9995 If N is negative, go up instead.
9996 Returns the difference between N and how many steps down that were
9997 taken."
9998   (interactive "p")
9999   (gnus-set-global-variables)
10000   (let ((up (< n 0))
10001         (n (abs n)))
10002   (while (and (> n 0)
10003               (if up (gnus-summary-go-up-thread)
10004                 (gnus-summary-go-down-thread)))
10005     (setq n (1- n)))
10006   (gnus-summary-position-cursor)
10007   (if (/= 0 n) (gnus-message 7 "Can't go further"))
10008   n))
10009
10010 (defun gnus-summary-up-thread (n)
10011   "Go up thread N steps.
10012 If N is negative, go up instead.
10013 Returns the difference between N and how many steps down that were
10014 taken."
10015   (interactive "p")
10016   (gnus-set-global-variables)
10017   (gnus-summary-down-thread (- n)))
10018
10019 (defun gnus-summary-kill-thread (&optional unmark)
10020   "Mark articles under current thread as read.
10021 If the prefix argument is positive, remove any kinds of marks.
10022 If the prefix argument is negative, tick articles instead."
10023   (interactive "P")
10024   (gnus-set-global-variables)
10025   (if unmark
10026       (setq unmark (prefix-numeric-value unmark)))
10027   (let ((killing t)
10028         (level (gnus-summary-thread-level)))
10029     (save-excursion
10030       (while killing
10031         ;; Mark the article...
10032         (cond ((null unmark) (gnus-summary-mark-article-as-read
10033                                gnus-killed-mark))
10034               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10035                              gnus-unread-mark))
10036               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10037         ;; ...and go forward until either the buffer ends or the subtree
10038         ;; ends. 
10039         (if (not (and (zerop (forward-line 1))
10040                       (> (gnus-summary-thread-level) level)))
10041             (setq killing nil))))
10042     ;; Hide killed subtrees.
10043     (and (null unmark)
10044          gnus-thread-hide-killed
10045          (gnus-summary-hide-thread))
10046     ;; If marked as read, go to next unread subject.
10047     (if (null unmark)
10048         ;; Go to next unread subject.
10049         (gnus-summary-next-subject 1 t)))
10050   (gnus-set-mode-line 'summary))
10051
10052 ;; Summary sorting commands
10053
10054 (defun gnus-summary-sort-by-number (&optional reverse)
10055   "Sort summary buffer by article number.
10056 Argument REVERSE means reverse order."
10057   (interactive "P")
10058   (gnus-set-global-variables)
10059   (gnus-summary-sort 
10060    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10061    ;; a function, so we wrap it.
10062    (cons (lambda () (gnus-summary-article-number))
10063          'gnus-thread-sort-by-number) reverse))
10064
10065 (defun gnus-summary-sort-by-author (&optional reverse)
10066   "Sort summary buffer by author name alphabetically.
10067 If case-fold-search is non-nil, case of letters is ignored.
10068 Argument REVERSE means reverse order."
10069   (interactive "P")
10070   (gnus-set-global-variables)
10071   (gnus-summary-sort
10072    (cons
10073     (lambda ()
10074       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10075              (extract (funcall
10076                        gnus-extract-address-components
10077                        (header-from header))))
10078         (concat (or (car extract) (cdr extract))
10079                 "\r" (header-subject header))))
10080     'gnus-thread-sort-by-author)
10081    reverse))
10082
10083 (defun gnus-summary-sort-by-subject (&optional reverse)
10084   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10085 If case-fold-search is non-nil, case of letters is ignored.
10086 Argument REVERSE means reverse order."
10087   (interactive "P")
10088   (gnus-set-global-variables)
10089   (gnus-summary-sort
10090    (cons
10091     (lambda ()
10092       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10093              (extract (funcall
10094                        gnus-extract-address-components
10095                        (header-from header))))
10096         (concat 
10097          (downcase (gnus-simplify-subject (gnus-summary-subject-string)))
10098          "\r" (or (car extract) (cdr extract)))))
10099     'gnus-thread-sort-by-subject)
10100    reverse))
10101
10102 (defun gnus-summary-sort-by-date (&optional reverse)
10103   "Sort summary buffer by date.
10104 Argument REVERSE means reverse order."
10105   (interactive "P")
10106   (gnus-set-global-variables)
10107   (gnus-summary-sort
10108    (cons
10109     (lambda ()
10110       (gnus-sortable-date
10111        (header-date 
10112         (gnus-get-header-by-num (gnus-summary-article-number)))))
10113     'gnus-thread-sort-by-date)
10114    reverse))
10115
10116 (defun gnus-summary-sort-by-score (&optional reverse)
10117   "Sort summary buffer by score.
10118 Argument REVERSE means reverse order."
10119   (interactive "P")
10120   (gnus-set-global-variables)
10121   (gnus-summary-sort 
10122    (cons (lambda () (gnus-summary-article-score))
10123          'gnus-thread-sort-by-score)
10124    (not reverse)))
10125
10126 (defvar gnus-summary-already-sorted nil)
10127 (defun gnus-summary-sort (predicate reverse)
10128   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10129   (if gnus-summary-already-sorted
10130       ()
10131     (let (buffer-read-only)
10132       (if (not gnus-show-threads)
10133           ;; We do untreaded sorting...
10134           (progn
10135             (goto-char (point-min))
10136             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10137         ;; ... or we do threaded sorting.
10138         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10139               (gnus-summary-prepare-hook nil)
10140               (gnus-summary-already-sorted nil))
10141           ;; We do that by simply regenerating the threads.
10142           (gnus-summary-prepare)
10143           (and gnus-show-threads
10144                gnus-thread-hide-subtree
10145                (gnus-summary-hide-all-threads))
10146           ;; If in async mode, we send some info to the backend.
10147           (and gnus-newsgroup-async
10148                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10149                (gnus-request-asynchronous 
10150                 gnus-newsgroup-name
10151                 (if (and gnus-asynchronous-article-function
10152                          (fboundp gnus-asynchronous-article-function))
10153                     (funcall gnus-asynchronous-article-function
10154                              gnus-newsgroup-threads)))))))))
10155
10156   
10157 (defun gnus-sortable-date (date)
10158   "Make sortable string by string-lessp from DATE.
10159 Timezone package is used."
10160   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10161          (year (aref date 0))
10162          (month (aref date 1))
10163          (day (aref date 2)))
10164     (timezone-make-sortable-date 
10165      year month day 
10166      (timezone-make-time-string
10167       (aref date 3) (aref date 4) (aref date 5)))))
10168
10169
10170 ;; Summary saving commands.
10171
10172 (defun gnus-summary-save-article (&optional n)
10173   "Save the current article using the default saver function.
10174 If N is a positive number, save the N next articles.
10175 If N is a negative number, save the N previous articles.
10176 If N is nil and any articles have been marked with the process mark,
10177 save those articles instead.
10178 The variable `gnus-default-article-saver' specifies the saver function."
10179   (interactive "P")
10180   (gnus-set-global-variables)
10181   (let ((articles (gnus-summary-work-articles n)))
10182     (while articles
10183       (let ((header (gnus-get-header-by-num (car articles))))
10184         (if (vectorp header)
10185             (progn
10186               (save-window-excursion
10187                 (gnus-summary-select-article t nil nil (car articles)))
10188               (or gnus-save-all-headers
10189                   (gnus-article-hide-headers t))
10190               ;; Remove any X-Gnus lines.
10191               (save-excursion
10192                 (save-restriction
10193                   (set-buffer gnus-article-buffer)
10194                   (let ((buffer-read-only nil))
10195                     (goto-char (point-min))
10196                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10197                                                   (point-max)))
10198                     (while (re-search-forward "^X-Gnus" nil t)
10199                       (beginning-of-line)
10200                       (delete-region (point)
10201                                      (progn (forward-line 1) (point))))
10202                     (widen))))
10203               (save-window-excursion
10204                 (if gnus-default-article-saver
10205                     (funcall gnus-default-article-saver)
10206                   (error "No default saver is defined."))))
10207           (if (assq 'name header)
10208               (gnus-copy-file (cdr (assq 'name header)))
10209             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10210       (gnus-summary-remove-process-mark (car articles))
10211       (setq articles (cdr articles)))
10212     (gnus-summary-position-cursor)
10213     n))
10214
10215 (defun gnus-summary-pipe-output (&optional arg)
10216   "Pipe the current article to a subprocess.
10217 If N is a positive number, pipe the N next articles.
10218 If N is a negative number, pipe the N previous articles.
10219 If N is nil and any articles have been marked with the process mark,
10220 pipe those articles instead."
10221   (interactive "P")
10222   (gnus-set-global-variables)
10223   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10224     (gnus-summary-save-article arg)))
10225
10226 (defun gnus-summary-save-article-mail (&optional arg)
10227   "Append the current article to an mail file.
10228 If N is a positive number, save the N next articles.
10229 If N is a negative number, save the N previous articles.
10230 If N is nil and any articles have been marked with the process mark,
10231 save those articles instead."
10232   (interactive "P")
10233   (gnus-set-global-variables)
10234   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10235     (gnus-summary-save-article arg)))
10236
10237 (defun gnus-summary-save-article-rmail (&optional arg)
10238   "Append the current article to an rmail file.
10239 If N is a positive number, save the N next articles.
10240 If N is a negative number, save the N previous articles.
10241 If N is nil and any articles have been marked with the process mark,
10242 save those articles instead."
10243   (interactive "P")
10244   (gnus-set-global-variables)
10245   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10246     (gnus-summary-save-article arg)))
10247
10248 (defun gnus-summary-save-article-file (&optional arg)
10249   "Append the current article to a file.
10250 If N is a positive number, save the N next articles.
10251 If N is a negative number, save the N previous articles.
10252 If N is nil and any articles have been marked with the process mark,
10253 save those articles instead."
10254   (interactive "P")
10255   (gnus-set-global-variables)
10256   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10257     (gnus-summary-save-article arg)))
10258
10259 (defun gnus-read-save-file-name (prompt default-name)
10260   (let ((methods gnus-split-methods)
10261         split-name)
10262     (if (not gnus-split-methods)
10263         ()
10264       (save-excursion
10265         (set-buffer gnus-article-buffer)
10266         (gnus-narrow-to-headers)
10267         (while methods
10268           (goto-char (point-min))
10269           (and (condition-case () 
10270                    (re-search-forward (car (car methods)) nil t)
10271                  (error nil))
10272                (setq split-name (cons (nth 1 (car methods)) split-name)))
10273           (setq methods (cdr methods)))
10274         (widen)))
10275     (cond ((null split-name)
10276            (read-file-name
10277             (concat prompt " (default "
10278                     (file-name-nondirectory default-name) ") ")
10279             (file-name-directory default-name)
10280             default-name))
10281           ((= 1 (length split-name))
10282            (read-file-name
10283             (concat prompt " (default " (car split-name) ") ")
10284             gnus-article-save-directory
10285             (concat gnus-article-save-directory (car split-name))))
10286           (t
10287            (setq split-name (mapcar (lambda (el) (list el))
10288                                     (nreverse split-name)))
10289            (let ((result (completing-read 
10290                           (concat prompt " ")
10291                           split-name nil nil)))
10292              (concat gnus-article-save-directory
10293                      (if (string= result "")
10294                          (car (car split-name))
10295                        result)))))))
10296
10297 (defun gnus-summary-save-in-rmail (&optional filename)
10298   "Append this article to Rmail file.
10299 Optional argument FILENAME specifies file name.
10300 Directory to save to is default to `gnus-article-save-directory' which
10301 is initialized from the SAVEDIR environment variable."
10302   (interactive)
10303   (gnus-set-global-variables)
10304   (let ((default-name
10305           (funcall gnus-rmail-save-name gnus-newsgroup-name
10306                    gnus-current-headers gnus-newsgroup-last-rmail)))
10307     (or filename
10308         (setq filename (gnus-read-save-file-name 
10309                         "Save in rmail file:" default-name)))
10310     (gnus-make-directory (file-name-directory filename))
10311     (gnus-eval-in-buffer-window 
10312      gnus-article-buffer
10313      (save-excursion
10314        (save-restriction
10315          (widen)
10316          (gnus-output-to-rmail filename))))
10317     ;; Remember the directory name to save articles
10318     (setq gnus-newsgroup-last-rmail filename)))
10319
10320 (defun gnus-summary-save-in-mail (&optional filename)
10321   "Append this article to Unix mail file.
10322 Optional argument FILENAME specifies file name.
10323 Directory to save to is default to `gnus-article-save-directory' which
10324 is initialized from the SAVEDIR environment variable."
10325   (interactive)
10326   (gnus-set-global-variables)
10327   (let ((default-name
10328           (funcall gnus-mail-save-name gnus-newsgroup-name
10329                    gnus-current-headers gnus-newsgroup-last-mail)))
10330     (or filename
10331         (setq filename (gnus-read-save-file-name 
10332                         "Save in Unix mail file:" default-name)))
10333     (setq filename
10334           (expand-file-name filename
10335                             (and default-name
10336                                  (file-name-directory default-name))))
10337     (gnus-make-directory (file-name-directory filename))
10338     (gnus-eval-in-buffer-window 
10339      gnus-article-buffer
10340      (save-excursion
10341        (save-restriction
10342          (widen)
10343          (if (and (file-readable-p filename) (rmail-file-p filename))
10344              (gnus-output-to-rmail filename)
10345            (rmail-output filename 1 t t)))))
10346     ;; Remember the directory name to save articles.
10347     (setq gnus-newsgroup-last-mail filename)))
10348
10349 (defun gnus-summary-save-in-file (&optional filename)
10350   "Append this article to file.
10351 Optional argument FILENAME specifies file name.
10352 Directory to save to is default to `gnus-article-save-directory' which
10353 is initialized from the SAVEDIR environment variable."
10354   (interactive)
10355   (gnus-set-global-variables)
10356   (let ((default-name
10357           (funcall gnus-file-save-name gnus-newsgroup-name
10358                    gnus-current-headers gnus-newsgroup-last-file)))
10359     (or filename
10360         (setq filename (gnus-read-save-file-name 
10361                         "Save in file:" default-name)))
10362     (gnus-make-directory (file-name-directory filename))
10363     (gnus-eval-in-buffer-window 
10364      gnus-article-buffer
10365      (save-excursion
10366        (save-restriction
10367          (widen)
10368          (gnus-output-to-file filename))))
10369     ;; Remember the directory name to save articles.
10370     (setq gnus-newsgroup-last-file filename)))
10371
10372 (defun gnus-summary-save-in-pipe (&optional command)
10373   "Pipe this article to subprocess."
10374   (interactive)
10375   (gnus-set-global-variables)
10376   (let ((command (read-string "Shell command on article: "
10377                               gnus-last-shell-command)))
10378     (if (string-equal command "")
10379         (setq command gnus-last-shell-command))
10380     (gnus-eval-in-buffer-window 
10381      gnus-article-buffer
10382      (save-restriction
10383        (widen)
10384        (shell-command-on-region (point-min) (point-max) command nil)))
10385     (setq gnus-last-shell-command command)))
10386
10387 ;; Summary extract commands
10388
10389 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10390   (let ((buffer-read-only nil)
10391         (article (gnus-summary-article-number))
10392         b)
10393     (or (gnus-summary-goto-subject article)
10394         (error (format "No such article: %d" article)))
10395     (gnus-summary-position-cursor)
10396     ;; If all commands are to be bunched up on one line, we collect
10397     ;; them here.  
10398     (if gnus-view-pseudos-separately
10399         ()
10400       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10401             files action)
10402         (while ps
10403           (setq action (cdr (assq 'action (car ps))))
10404           (setq files (list (cdr (assq 'name (car ps)))))
10405           (while (and ps (cdr ps)
10406                       (string= (or action "1")
10407                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10408             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10409             (setcdr ps (cdr (cdr ps))))
10410           (if (not files)
10411               ()
10412             (if (not (string-match "%s" action))
10413                 (setq files (cons " " files)))
10414             (setq files (cons " " files))
10415             (and (assq 'execute (car ps))
10416                  (setcdr (assq 'execute (car ps))
10417                          (funcall (if (string-match "%s" action)
10418                                       'format 'concat)
10419                                   action 
10420                                   (mapconcat (lambda (f) f) files " ")))))
10421           (setq ps (cdr ps)))))
10422     (if (and gnus-view-pseudos (not not-view))
10423         (while pslist
10424           (and (assq 'execute (car pslist))
10425                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10426                                      (eq gnus-view-pseudos 'not-confirm)))
10427           (setq pslist (cdr pslist)))
10428       (save-excursion
10429         (while pslist
10430           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10431                                          (gnus-summary-article-number)))
10432           (forward-line 1)
10433           (setq b (point))
10434           (insert "          " (file-name-nondirectory 
10435                                 (cdr (assq 'name (car pslist))))
10436                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10437           (add-text-properties 
10438            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10439                           'gnus-mark gnus-unread-mark 
10440                           'gnus-level 0
10441                           'gnus-pseudo (car pslist)))
10442           (forward-line -1)
10443           (gnus-sethash (int-to-string gnus-reffed-article-number)
10444                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10445           (setq gnus-newsgroup-unreads
10446                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10447           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10448           (setq pslist (cdr pslist)))))))
10449
10450 (defun gnus-pseudos< (p1 p2)
10451   (let ((c1 (cdr (assq 'action p1)))
10452         (c2 (cdr (assq 'action p2))))
10453     (and c1 c2 (string< c1 c2))))
10454
10455 (defun gnus-request-pseudo-article (props)
10456   (cond ((assq 'execute props)
10457          (gnus-execute-command (cdr (assq 'execute props)))))
10458   (let ((gnus-current-article (gnus-summary-article-number)))
10459     (run-hooks 'gnus-mark-article-hook)))
10460
10461 (defun gnus-execute-command (command &optional automatic)
10462   (save-excursion
10463     (gnus-article-setup-buffer)
10464     (set-buffer gnus-article-buffer)
10465     (let ((command (if automatic command (read-string "Command: " command)))
10466           (buffer-read-only nil))
10467       (erase-buffer)
10468       (insert "$ " command "\n\n")
10469       (if gnus-view-pseudo-asynchronously
10470           (start-process "gnus-execute" nil "sh" "-c" command)
10471         (call-process "sh" nil t nil "-c" command)))))
10472
10473 (defun gnus-copy-file (file &optional to)
10474   "Copy FILE to TO."
10475   (interactive
10476    (list (read-file-name "Copy file: " default-directory)
10477          (read-file-name "Copy file to: " default-directory)))
10478   (gnus-set-global-variables)
10479   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10480   (and (file-directory-p to) 
10481        (setq to (concat (file-name-as-directory to)
10482                         (file-name-nondirectory file))))
10483   (copy-file file to))
10484
10485 ;; Summary kill commands.
10486
10487 (defun gnus-summary-edit-global-kill (article)
10488   "Edit the \"global\" kill file."
10489   (interactive (list (gnus-summary-article-number)))
10490   (gnus-set-global-variables)
10491   (gnus-group-edit-global-kill article))
10492
10493 (defun gnus-summary-edit-local-kill ()
10494   "Edit a local kill file applied to the current newsgroup."
10495   (interactive)
10496   (gnus-set-global-variables)
10497   (setq gnus-current-headers 
10498         (gnus-gethash 
10499          (int-to-string (gnus-summary-article-number))
10500          gnus-newsgroup-headers-hashtb-by-number))
10501   (gnus-set-global-variables)
10502   (gnus-group-edit-local-kill 
10503    (gnus-summary-article-number) gnus-newsgroup-name))
10504
10505 \f
10506 ;;;
10507 ;;; Gnus article mode
10508 ;;;
10509
10510 (put 'gnus-article-mode 'mode-class 'special)
10511
10512 (if gnus-article-mode-map
10513     nil
10514   (setq gnus-article-mode-map (make-keymap))
10515   (suppress-keymap gnus-article-mode-map)
10516   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10517   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10518   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10519   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10520   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10521   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10522   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10523   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10524   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10525   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10526   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10527   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10528   
10529   ;; Duplicate almost all summary keystrokes in the article mode map.
10530   (let ((commands 
10531          (list 
10532           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10533           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10534           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10535           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10536           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10537           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10538           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10539           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10540           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10541           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10542           "\C-c\C-v\C-v" "\C-d" "v" 
10543 ;;        "Mt" "M!" "Md" "Mr"
10544 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10545 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10546 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10547 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10548 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10549 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10550 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10551 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10552 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10553 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10554 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10555           )))
10556     (while (and nil commands) ; disabled
10557       (define-key gnus-article-mode-map (car commands) 
10558         'gnus-article-summary-command)
10559       (setq commands (cdr commands))))
10560
10561   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10562 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10563                          "=" "n"  "^" "\M-^")))
10564     (while (and nil commands) ; disabled
10565       (define-key gnus-article-mode-map (car commands) 
10566         'gnus-article-summary-command-nosave)
10567       (setq commands (cdr commands)))))
10568
10569
10570 (defun gnus-article-mode ()
10571   "Major mode for displaying an article.
10572
10573 All normal editing commands are switched off.
10574
10575 The following commands are available:
10576
10577 \\<gnus-article-mode-map>
10578 \\[gnus-article-next-page]\t Scroll the article one page forwards
10579 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10580 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10581 \\[gnus-article-show-summary]\t Display the summary buffer
10582 \\[gnus-article-mail]\t Send a reply to the address near point
10583 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10584 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10585 \\[gnus-info-find-node]\t Go to the Gnus info node"
10586   (interactive)
10587   (if gnus-visual (gnus-article-make-menu-bar))
10588   (kill-all-local-variables)
10589   (setq mode-line-modified "-- ")
10590   (make-local-variable 'mode-line-format)
10591   (setq mode-line-format (copy-sequence mode-line-format))
10592   (and (equal (nth 3 mode-line-format) "   ")
10593        (setcar (nthcdr 3 mode-line-format) ""))
10594   (setq mode-name "Article")
10595   (setq major-mode 'gnus-article-mode)
10596   (make-local-variable 'minor-mode-alist)
10597   (or (assq 'gnus-show-mime minor-mode-alist)
10598       (setq minor-mode-alist
10599             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10600   (use-local-map gnus-article-mode-map)
10601   (make-local-variable 'page-delimiter)
10602   (setq page-delimiter gnus-page-delimiter)
10603   (buffer-disable-undo (current-buffer))
10604   (setq buffer-read-only t)             ;Disable modification
10605   (run-hooks 'gnus-article-mode-hook))
10606
10607 (defun gnus-article-setup-buffer ()
10608   "Initialize article mode buffer."
10609   (if (get-buffer gnus-article-buffer)
10610       (save-excursion
10611         (set-buffer gnus-article-buffer)
10612         (buffer-disable-undo (current-buffer))
10613         (setq buffer-read-only t)
10614         (gnus-add-current-to-buffer-list)
10615         (or (eq major-mode 'gnus-article-mode)
10616             (gnus-article-mode)))
10617     (save-excursion
10618       (set-buffer (get-buffer-create gnus-article-buffer))
10619       (gnus-add-current-to-buffer-list)
10620       (gnus-article-mode))))
10621
10622 ;; Set article window start at LINE, where LINE is the number of lines
10623 ;; from the head of the article.
10624 (defun gnus-article-set-window-start (&optional line)
10625   (set-window-start 
10626    (get-buffer-window gnus-article-buffer)
10627    (save-excursion
10628      (set-buffer gnus-article-buffer)
10629      (goto-char (point-min))
10630      (if (not line)
10631          (point-min)
10632        (gnus-message 6 "Moved to bookmark")
10633        (search-forward "\n\n" nil t)
10634        (forward-line line)
10635        (point)))))
10636
10637 (defun gnus-request-article-this-buffer (article group)
10638   "Get an article and insert it into this buffer."
10639   (setq group (or group gnus-newsgroup-name))
10640
10641   ;; Open server if it has closed.
10642   (gnus-check-server (gnus-find-method-for-group group))
10643
10644   ;; Using `gnus-request-article' directly will insert the article into
10645   ;; `nntp-server-buffer' - so we'll save some time by not having to
10646   ;; copy it from the server buffer into the article buffer.
10647
10648   ;; We only request an article by message-id when we do not have the
10649   ;; headers for it, so we'll have to get those.
10650   (and (stringp article) 
10651        (let ((gnus-override-method gnus-refer-article-method))
10652          (gnus-read-header article)))
10653
10654   ;; If the article number is negative, that means that this article
10655   ;; doesn't belong in this newsgroup (possibly), so we find its
10656   ;; message-id and request it by id instead of number.
10657   (if (not (numberp article))
10658       ()
10659     (save-excursion
10660       (set-buffer gnus-summary-buffer)
10661       (let ((header (gnus-get-header-by-num article)))
10662         (if (< article 0)
10663             (if (vectorp header)
10664                 ;; It's a real article.
10665                 (setq article (header-id header))
10666               ;; It is an extracted pseudo-article.
10667               (setq article 'pseudo)
10668               (gnus-request-pseudo-article header)))
10669
10670         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10671           (if (not (eq (car method) 'nneething))
10672               ()
10673             (let ((dir (concat (file-name-as-directory (nth 1 method))
10674                                (header-subject header))))
10675               (if (file-directory-p dir)
10676                   (progn
10677                     (setq article 'nneething)
10678                     (gnus-group-enter-directory dir)))))))))
10679
10680   ;; Check the cache.
10681   (if (and gnus-use-cache
10682            (numberp article)
10683            (gnus-cache-request-article article group))
10684       'article
10685     ;; Get the article and into the article buffer.
10686     (if (or (stringp article) (numberp article))
10687         (progn
10688           (erase-buffer)
10689           (let ((gnus-override-method 
10690                  (and (stringp article) gnus-refer-article-method)))
10691             (and (gnus-request-article article group (current-buffer))
10692                  'article)))
10693       article)))
10694
10695 (defun gnus-read-header (id)
10696   "Read the headers of article ID and enter them into the Gnus system."
10697   (let (header)
10698     (if (not (setq header 
10699                    (car (if (let ((gnus-nov-is-evil t))
10700                               (gnus-retrieve-headers 
10701                                (list id) gnus-newsgroup-name))
10702                             (gnus-get-newsgroup-headers)))))
10703         nil
10704       (if (stringp id)
10705           (header-set-number header gnus-reffed-article-number))
10706       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10707       (gnus-sethash (int-to-string (header-number header)) header
10708                     gnus-newsgroup-headers-hashtb-by-number)
10709       (if (stringp id)
10710           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10711       (setq gnus-current-headers header)
10712       header)))
10713
10714 (defun gnus-article-prepare (article &optional all-headers header)
10715   "Prepare ARTICLE in article mode buffer.
10716 ARTICLE should either be an article number or a Message-ID.
10717 If ARTICLE is an id, HEADER should be the article headers.
10718 If ALL-HEADERS is non-nil, no headers are hidden."
10719   (save-excursion
10720     ;; Make sure we start in a summary buffer.
10721     (or (eq major-mode 'gnus-summary-mode)
10722         (set-buffer gnus-summary-buffer))
10723     (setq gnus-summary-buffer (current-buffer))
10724     ;; Make sure the connection to the server is alive.
10725     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10726         (progn
10727           (gnus-check-server 
10728            (gnus-find-method-for-group gnus-newsgroup-name))
10729           (gnus-request-group gnus-newsgroup-name t)))
10730     (let* ((article (if header (header-number header) article))
10731            (summary-buffer (current-buffer))
10732            (internal-hook gnus-article-internal-prepare-hook)
10733            (group gnus-newsgroup-name)
10734            result)
10735       (save-excursion
10736         (gnus-article-setup-buffer)
10737         (set-buffer gnus-article-buffer)
10738         (if (not (setq result (let ((buffer-read-only nil))
10739                                 (gnus-request-article-this-buffer 
10740                                  article group))))
10741             ;; There is no such article.
10742             (save-excursion
10743               (if (not (numberp article))
10744                   ()
10745                 (setq gnus-article-current 
10746                       (cons gnus-newsgroup-name article))
10747                 (set-buffer gnus-summary-buffer)
10748                 (setq gnus-current-article article)
10749                 (gnus-summary-mark-article article gnus-canceled-mark))
10750               (gnus-message 1 "No such article (may be canceled)")
10751               (ding)
10752               nil)
10753           (if (or (eq result 'pseudo) (eq result 'nneething))
10754               (progn
10755                 (save-excursion
10756                   (set-buffer summary-buffer)
10757                   (setq gnus-last-article gnus-current-article
10758                         gnus-newsgroup-history (cons gnus-current-article
10759                                                      gnus-newsgroup-history)
10760                         gnus-current-article 0
10761                         gnus-current-headers nil
10762                         gnus-article-current nil)
10763                   (if (eq result 'nneething)
10764                       (gnus-configure-windows 'summary)
10765                     (gnus-configure-windows 'article))
10766                   (gnus-set-global-variables))
10767                 (gnus-set-mode-line 'article))
10768             ;; The result from the `request' was an actual article -
10769             ;; or at least some text that is now displayed in the
10770             ;; article buffer.
10771             (if (and (numberp article)
10772                      (not (eq article gnus-current-article)))
10773                 ;; Seems like a new article has been selected.
10774                 ;; `gnus-current-article' must be an article number.
10775                 (save-excursion
10776                   (set-buffer summary-buffer)
10777                   (setq gnus-last-article gnus-current-article
10778                         gnus-newsgroup-history (cons gnus-current-article
10779                                                      gnus-newsgroup-history)
10780                         gnus-current-article article
10781                         gnus-current-headers 
10782                         (gnus-get-header-by-num gnus-current-article)
10783                         gnus-article-current 
10784                         (cons gnus-newsgroup-name gnus-current-article))
10785                   (gnus-summary-show-thread)
10786                   (run-hooks 'gnus-mark-article-hook)
10787                   (gnus-set-mode-line 'summary)
10788                   (and gnus-visual 
10789                        (run-hooks 'gnus-visual-mark-article-hook))
10790                   ;; Set the global newsgroup variables here.
10791                   ;; Suggested by Jim Sisolak
10792                   ;; <sisolak@trans4.neep.wisc.edu>.
10793                   (gnus-set-global-variables)
10794                   (setq gnus-have-all-headers 
10795                         (or all-headers gnus-show-all-headers))
10796                   (and gnus-use-cache 
10797                        (vectorp (gnus-get-header-by-number article))
10798                        (gnus-cache-possibly-enter-article
10799                         group article
10800                         (gnus-get-header-by-number article)
10801                         (memq article gnus-newsgroup-marked)
10802                         (memq article gnus-newsgroup-dormant)
10803                         (memq article gnus-newsgroup-unreads)))))
10804             ;; Hooks for getting information from the article.
10805             ;; This hook must be called before being narrowed.
10806             (let (buffer-read-only)
10807               (run-hooks 'internal-hook)
10808               (run-hooks 'gnus-article-prepare-hook)
10809               ;; Decode MIME message.
10810               (if (and gnus-show-mime
10811                        (or (not gnus-strict-mime)
10812                            (gnus-fetch-field "Mime-Version")))
10813                   (funcall gnus-show-mime-method))
10814               ;; Perform the article display hooks.
10815               (run-hooks 'gnus-article-display-hook))
10816             ;; Do page break.
10817             (goto-char (point-min))
10818             (and gnus-break-pages (gnus-narrow-to-page))
10819             (gnus-set-mode-line 'article)
10820             (gnus-configure-windows 'article)
10821             (goto-char (point-min))
10822             t))))))
10823
10824 (defun gnus-article-show-all-headers ()
10825   "Show all article headers in article mode buffer."
10826   (save-excursion 
10827     (gnus-article-setup-buffer)
10828     (set-buffer gnus-article-buffer)
10829     (let ((buffer-read-only nil))
10830       (remove-text-properties (point-min) (point-max) 
10831                               gnus-hidden-properties))))
10832
10833 (defun gnus-article-hide-headers-if-wanted ()
10834   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10835 Provided for backwards compatability."
10836   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10837       (gnus-article-hide-headers)))
10838
10839 (defun gnus-article-hide-headers (&optional delete)
10840   "Hide unwanted headers and possibly sort them as well."
10841   (interactive "P")
10842   (save-excursion
10843     (set-buffer gnus-article-buffer)
10844     (save-restriction
10845       (let ((sorted gnus-sorted-header-list)
10846             (buffer-read-only nil)
10847             want-list beg want-l)
10848         ;; First we narrow to just the headers.
10849         (widen)
10850         (goto-char (point-min))
10851         ;; Hide any "From " lines at the beginning of (mail) articles. 
10852         (while (looking-at "From ")
10853           (forward-line 1))
10854         (if (bobp) 
10855             (add-text-properties (point-min) (point) gnus-hidden-properties))
10856         ;; Then treat the rest of the header lines.
10857         (narrow-to-region 
10858          (point) 
10859          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10860         ;; Then we use the two regular expressions
10861         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10862         ;; select which header lines is to remain visible in the
10863         ;; article buffer.
10864         (goto-char (point-min))
10865         (while (re-search-forward "^[^ \t]*:" nil t)
10866           (beginning-of-line)
10867           ;; We add the headers we want to keep to a list and delete
10868           ;; them from the buffer.
10869           (if (or (and (stringp gnus-visible-headers)
10870                        (looking-at gnus-visible-headers))
10871                   (and (not (stringp gnus-visible-headers))
10872                        (stringp gnus-ignored-headers)
10873                        (not (looking-at gnus-ignored-headers))))
10874               (progn
10875                 (setq beg (point))
10876                 (forward-line 1)
10877                 ;; Be sure to get multi-line headers...
10878                 (re-search-forward "^[^ \t]*:" nil t)
10879                 (beginning-of-line)
10880                 (setq want-list 
10881                       (cons (buffer-substring beg (point)) want-list))
10882                 (delete-region beg (point))
10883                 (goto-char beg))
10884             (forward-line 1)))
10885         ;; Next we perform the sorting by looking at
10886         ;; `gnus-sorted-header-list'. 
10887         (goto-char (point-min))
10888         (while (and sorted want-list)
10889           (setq want-l want-list)
10890           (while (and want-l
10891                       (not (string-match (car sorted) (car want-l))))
10892             (setq want-l (cdr want-l)))
10893           (if want-l 
10894               (progn
10895                 (insert (car want-l))
10896                 (setq want-list (delq (car want-l) want-list))))
10897           (setq sorted (cdr sorted)))
10898         ;; Any headers that were not matched by the sorted list we
10899         ;; just tack on the end of the visible header list.
10900         (while want-list
10901           (insert (car want-list))
10902           (setq want-list (cdr want-list)))
10903         ;; And finally we make the unwanted headers invisible.
10904         (if delete
10905             (delete-region (point) (point-max))
10906           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10907           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10908
10909 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10910 (defun gnus-article-treat-overstrike ()
10911   "Translate overstrikes into bold text."
10912   (interactive)
10913   (save-excursion
10914     (set-buffer gnus-article-buffer)
10915     (let ((buffer-read-only nil))
10916       (while (search-forward "\b" nil t)
10917         (let ((next (following-char))
10918               (previous (char-after (- (point) 2))))
10919           (cond ((eq next previous)
10920                  (put-text-property (- (point) 2) (point)
10921                                     'invisible t)
10922                  (put-text-property (point) (1+ (point))
10923                                     'face 'bold))
10924                 ((eq next ?_)
10925                  (put-text-property (1- (point)) (1+ (point))
10926                                     'invisible t)
10927                  (put-text-property (1- (point)) (point)
10928                                     'face 'underline))
10929                 ((eq previous ?_)
10930                  (put-text-property (- (point) 2) (point)
10931                                     'invisible t)
10932                  (put-text-property (point) (1+ (point))
10933                                     'face 'underline))))))))
10934
10935 (defun gnus-article-word-wrap ()
10936   "Format too long lines."
10937   (interactive)
10938   (save-excursion
10939     (set-buffer gnus-article-buffer)
10940     (let ((buffer-read-only nil))
10941       (goto-char (point-min))
10942       (search-forward "\n\n" nil t)
10943       (end-of-line 1)
10944       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10945             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10946             (adaptive-fill-mode t))
10947         (while (not (eobp))
10948           (and (>= (current-column) (min fill-column (window-width)))
10949                (/= (preceding-char) ?:)
10950                (fill-paragraph nil))
10951           (end-of-line 2))))))
10952
10953 (defun gnus-article-remove-cr ()
10954   "Remove carriage returns from an article."
10955   (interactive)
10956   (save-excursion
10957     (set-buffer gnus-article-buffer)
10958     (let ((buffer-read-only nil))
10959       (goto-char (point-min))
10960       (while (search-forward "\r" nil t)
10961         (replace-match "" t t)))))
10962
10963 (defun gnus-article-display-x-face (&optional force)
10964   "Look for an X-Face header and display it if present."
10965   (interactive (list 'force))
10966   (save-excursion
10967     (set-buffer gnus-article-buffer)
10968     (let ((inhibit-point-motion-hooks t)
10969           (case-fold-search nil))
10970       (save-restriction
10971         (goto-char (point-min))
10972         (search-forward "\n\n")
10973         (narrow-to-region (point-min) (point))
10974         (goto-char (point-min))
10975         (if (or (not gnus-article-x-face-command)
10976                 (and (not force)
10977                      (or (not gnus-article-x-face-too-ugly)
10978                          (string-match gnus-article-x-face-too-ugly
10979                                        (mail-fetch-field "from"))))
10980                 (progn
10981                   (goto-char (point-min))
10982                   (not (re-search-forward "^X-Face: " nil t))))
10983             nil
10984           (let ((beg (point))
10985                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10986             (if (symbolp gnus-article-x-face-command)
10987                 (and (or (fboundp gnus-article-x-face-command)
10988                          (error "%s is not a function"
10989                                 gnus-article-x-face-command))
10990                      (funcall gnus-article-x-face-command beg end))
10991               (call-process-region beg end "sh" nil 0 nil
10992                                    "-c" gnus-article-x-face-command))))))))
10993
10994 (defun gnus-article-de-quoted-unreadable (&optional force)
10995   "Do a naïve translation of a quoted-printable-encoded article.
10996 This is in no way, shape or form meant as a replacement for real MIME
10997 processing, but is simply a stop-gap measure until MIME support is
10998 written.
10999 If FORCE, decode the article whether it is marked as quoted-printable
11000 or not." 
11001   (interactive (list 'force))
11002   (save-excursion
11003     (set-buffer gnus-article-buffer)
11004     (let ((case-fold-search t)
11005           (buffer-read-only nil)
11006           (type (gnus-fetch-field "content-transfer-encoding")))
11007       (if (or force (and type (string-match "quoted-printable" type)))
11008           (progn
11009             (goto-char (point-min))
11010             (search-forward "\n\n" nil 'move)
11011             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11012
11013 (defun gnus-mime-decode-quoted-printable (from to)
11014   ;; Decode quoted-printable from region between FROM and TO.
11015   (save-excursion
11016     (goto-char from)
11017     (while (search-forward "=" to t)
11018       (cond ((eq (following-char) ?\n)
11019              (delete-char -1)
11020              (delete-char 1))
11021             ((looking-at "[0-9A-F][0-9A-F]")
11022              (delete-char -1)
11023              (insert (hexl-hex-string-to-integer
11024                       (buffer-substring (point) (+ 2 (point)))))
11025              (delete-char 2))
11026             ((looking-at "=")
11027              (delete-char 1))
11028             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11029
11030 (defvar gnus-article-time-units
11031   (list (cons 'year (* 365.25 24 60 60))
11032         (cons 'week (* 7 24 60 60))
11033         (cons 'day (* 24 60 60))
11034         (cons 'hour (* 60 60))
11035         (cons 'minute 60)
11036         (cons 'second 1)))
11037
11038 (defun gnus-article-date-ut (&optional type)
11039   "Convert DATE date to universal time in the current article.
11040 If TYPE is `local', convert to local time; if it is `lapsed', output
11041 how much time has lapsed since DATE."
11042   (interactive (list 'ut))
11043   (let ((date (header-date (or gnus-current-headers 
11044                                (gnus-get-header-by-number
11045                                 (gnus-summary-article-number))"")))
11046         (date-regexp "^Date: \\|^X-Sent: "))
11047     (if (or (not date)
11048             (string= date ""))
11049         ()
11050       (save-excursion
11051         (set-buffer gnus-article-buffer)
11052         (let ((buffer-read-only nil))
11053           (goto-char (point-min))
11054           (if (and (re-search-forward date-regexp nil t)
11055                    (progn 
11056                      (beginning-of-line)
11057                      (looking-at date-regexp)))
11058               (delete-region (gnus-point-at-bol)
11059                              (progn (end-of-line) (1+ (point))))
11060             (goto-char (point-min))
11061             (goto-char (- (search-forward "\n\n") 2)))
11062           (insert
11063            (cond 
11064             ((eq type 'local)
11065              (concat "Date: " (condition-case ()
11066                                   (timezone-make-date-arpa-standard date)
11067                                 (error date))
11068                      "\n"))
11069             ((eq type 'ut)
11070              (concat "Date: "
11071                      (condition-case ()
11072                          (timezone-make-date-arpa-standard date nil "UT")
11073                        (error date))
11074                      "\n"))
11075             ((eq type 'lapsed)
11076              ;; If the date is seriously mangled, the timezone
11077              ;; functions are liable to bug out, so we condition-case
11078              ;; the entire thing.  
11079              (let* ((real-sec (condition-case ()
11080                                   (- (gnus-seconds-since-epoch 
11081                                       (timezone-make-date-arpa-standard
11082                                        (current-time-string) 
11083                                        (current-time-zone) "UT"))
11084                                      (gnus-seconds-since-epoch 
11085                                       (timezone-make-date-arpa-standard 
11086                                        date nil "UT")))
11087                                 (error 0)))
11088                     (sec (abs real-sec))
11089                     num prev)
11090                (if (zerop sec)
11091                    "X-Sent: Now\n"
11092                  (concat
11093                   "X-Sent: "
11094                   (mapconcat 
11095                    (lambda (unit)
11096                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11097                          ""
11098                        (setq sec (- sec (* num (cdr unit))))
11099                        (prog1
11100                            (concat (if prev ", " "") (int-to-string 
11101                                                       (floor num))
11102                                    " " (symbol-name (car unit))
11103                                    (if (> num 1) "s" ""))
11104                          (setq prev t))))
11105                    gnus-article-time-units "")
11106                   (if (> real-sec 0)
11107                       " ago\n"
11108                     " in the future\n")))))
11109             (t
11110              (error "Unknown conversion type: %s" type)))))))))
11111
11112 (defun gnus-article-date-local ()
11113   "Convert the current article date to the local timezone."
11114   (interactive)
11115   (gnus-article-date-ut 'local))
11116
11117 (defun gnus-article-date-lapsed ()
11118   "Convert the current article date to time lapsed since it was sent."
11119   (interactive)
11120   (gnus-article-date-ut 'lapsed))
11121
11122 (defun gnus-article-maybe-highlight ()
11123   "Do some article highlighting if `gnus-visual' is non-nil."
11124   (if gnus-visual (gnus-article-highlight-some)))
11125
11126 ;; Article savers.
11127
11128 (defun gnus-output-to-rmail (file-name)
11129   "Append the current article to an Rmail file named FILE-NAME."
11130   (require 'rmail)
11131   ;; Most of these codes are borrowed from rmailout.el.
11132   (setq file-name (expand-file-name file-name))
11133   (setq rmail-default-rmail-file file-name)
11134   (let ((artbuf (current-buffer))
11135         (tmpbuf (get-buffer-create " *Gnus-output*")))
11136     (save-excursion
11137       (or (get-file-buffer file-name)
11138           (file-exists-p file-name)
11139           (if (gnus-yes-or-no-p
11140                (concat "\"" file-name "\" does not exist, create it? "))
11141               (let ((file-buffer (create-file-buffer file-name)))
11142                 (save-excursion
11143                   (set-buffer file-buffer)
11144                   (rmail-insert-rmail-file-header)
11145                   (let ((require-final-newline nil))
11146                     (write-region (point-min) (point-max) file-name t 1)))
11147                 (kill-buffer file-buffer))
11148             (error "Output file does not exist")))
11149       (set-buffer tmpbuf)
11150       (buffer-disable-undo (current-buffer))
11151       (erase-buffer)
11152       (insert-buffer-substring artbuf)
11153       (gnus-convert-article-to-rmail)
11154       ;; Decide whether to append to a file or to an Emacs buffer.
11155       (let ((outbuf (get-file-buffer file-name)))
11156         (if (not outbuf)
11157             (append-to-file (point-min) (point-max) file-name)
11158           ;; File has been visited, in buffer OUTBUF.
11159           (set-buffer outbuf)
11160           (let ((buffer-read-only nil)
11161                 (msg (and (boundp 'rmail-current-message)
11162                           (symbol-value 'rmail-current-message))))
11163             ;; If MSG is non-nil, buffer is in RMAIL mode.
11164             (if msg
11165                 (progn (widen)
11166                        (narrow-to-region (point-max) (point-max))))
11167             (insert-buffer-substring tmpbuf)
11168             (if msg
11169                 (progn
11170                   (goto-char (point-min))
11171                   (widen)
11172                   (search-backward "\^_")
11173                   (narrow-to-region (point) (point-max))
11174                   (goto-char (1+ (point-min)))
11175                   (rmail-count-new-messages t)
11176                   (rmail-show-message msg)))))))
11177     (kill-buffer tmpbuf)))
11178
11179 (defun gnus-output-to-file (file-name)
11180   "Append the current article to a file named FILE-NAME."
11181   (setq file-name (expand-file-name file-name))
11182   (let ((artbuf (current-buffer))
11183         (tmpbuf (get-buffer-create " *Gnus-output*")))
11184     (save-excursion
11185       (set-buffer tmpbuf)
11186       (buffer-disable-undo (current-buffer))
11187       (erase-buffer)
11188       (insert-buffer-substring artbuf)
11189       ;; Append newline at end of the buffer as separator, and then
11190       ;; save it to file.
11191       (goto-char (point-max))
11192       (insert "\n")
11193       (append-to-file (point-min) (point-max) file-name))
11194     (kill-buffer tmpbuf)))
11195
11196 (defun gnus-convert-article-to-rmail ()
11197   "Convert article in current buffer to Rmail message format."
11198   (let ((buffer-read-only nil))
11199     ;; Convert article directly into Babyl format.
11200     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11201     (goto-char (point-min))
11202     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11203     (while (search-forward "\n\^_" nil t) ;single char
11204       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11205     (goto-char (point-max))
11206     (insert "\^_")))
11207
11208 (defun gnus-narrow-to-page (&optional arg)
11209   "Make text outside current page invisible except for page delimiter.
11210 A numeric arg specifies to move forward or backward by that many pages,
11211 thus showing a page other than the one point was originally in."
11212   (interactive "P")
11213   (setq arg (if arg (prefix-numeric-value arg) 0))
11214   (save-excursion
11215     (forward-page -1)                   ;Beginning of current page.
11216     (widen)
11217     (if (> arg 0)
11218         (forward-page arg)
11219       (if (< arg 0)
11220           (forward-page (1- arg))))
11221     ;; Find the end of the page.
11222     (forward-page)
11223     ;; If we stopped due to end of buffer, stay there.
11224     ;; If we stopped after a page delimiter, put end of restriction
11225     ;; at the beginning of that line.
11226     ;; These are commented out.
11227     ;;    (if (save-excursion (beginning-of-line)
11228     ;;                  (looking-at page-delimiter))
11229     ;;  (beginning-of-line))
11230     (narrow-to-region (point)
11231                       (progn
11232                         ;; Find the top of the page.
11233                         (forward-page -1)
11234                         ;; If we found beginning of buffer, stay there.
11235                         ;; If extra text follows page delimiter on same line,
11236                         ;; include it.
11237                         ;; Otherwise, show text starting with following line.
11238                         (if (and (eolp) (not (bobp)))
11239                             (forward-line 1))
11240                         (point)))))
11241
11242 (defun gnus-gmt-to-local ()
11243   "Rewrite Date header described in GMT to local in current buffer.
11244 Intended to be used with gnus-article-prepare-hook."
11245   (save-excursion
11246     (save-restriction
11247       (widen)
11248       (goto-char (point-min))
11249       (narrow-to-region (point-min)
11250                         (progn (search-forward "\n\n" nil 'move) (point)))
11251       (goto-char (point-min))
11252       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11253           (let ((buffer-read-only nil)
11254                 (date (buffer-substring-no-properties
11255                        (match-beginning 1) (match-end 1))))
11256             (delete-region (match-beginning 1) (match-end 1))
11257             (insert
11258              (timezone-make-date-arpa-standard 
11259               date nil (current-time-zone))))))))
11260
11261
11262 ;; Article mode commands
11263
11264 (defun gnus-article-next-page (&optional lines)
11265   "Show next page of current article.
11266 If end of article, return non-nil. Otherwise return nil.
11267 Argument LINES specifies lines to be scrolled up."
11268   (interactive "P")
11269   (move-to-window-line -1)
11270   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11271   (if (save-excursion
11272         (end-of-line)
11273         (and (pos-visible-in-window-p)  ;Not continuation line.
11274              (eobp)))
11275       ;; Nothing in this page.
11276       (if (or (not gnus-break-pages)
11277               (save-excursion
11278                 (save-restriction
11279                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11280           t                             ;Nothing more.
11281         (gnus-narrow-to-page 1)         ;Go to next page.
11282         nil)
11283     ;; More in this page.
11284     (condition-case ()
11285         (scroll-up lines)
11286       (end-of-buffer
11287        ;; Long lines may cause an end-of-buffer error.
11288        (goto-char (point-max))))
11289     nil))
11290
11291 (defun gnus-article-prev-page (&optional lines)
11292   "Show previous page of current article.
11293 Argument LINES specifies lines to be scrolled down."
11294   (interactive "P")
11295   (move-to-window-line 0)
11296   (if (and gnus-break-pages
11297            (bobp)
11298            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11299       (progn
11300         (gnus-narrow-to-page -1) ;Go to previous page.
11301         (goto-char (point-max))
11302         (recenter -1))
11303     (scroll-down lines)))
11304
11305 (defun gnus-article-refer-article ()
11306   "Read article specified by message-id around point."
11307   (interactive)
11308   (search-forward ">" nil t)    ;Move point to end of "<....>".
11309   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11310       (let ((message-id
11311              (buffer-substring (match-beginning 1) (match-end 1))))
11312         (set-buffer gnus-summary-buffer)
11313         (gnus-summary-refer-article message-id))
11314     (error "No references around point")))
11315
11316 (defun gnus-article-show-summary ()
11317   "Reconfigure windows to show summary buffer."
11318   (interactive)
11319   (gnus-configure-windows 'article)
11320   (gnus-summary-goto-subject gnus-current-article))
11321
11322 (defun gnus-article-describe-briefly ()
11323   "Describe article mode commands briefly."
11324   (interactive)
11325   (gnus-message 6
11326    (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")))
11327
11328 (defun gnus-article-summary-command ()
11329   "Execute the last keystroke in the summary buffer."
11330   (interactive)
11331   (let ((obuf (current-buffer))
11332         (owin (current-window-configuration))
11333         func)
11334     (switch-to-buffer gnus-summary-buffer 'norecord)
11335     (setq func (lookup-key (current-local-map) (this-command-keys)))
11336     (call-interactively func)
11337     (set-buffer obuf)
11338     (set-window-configuration owin)
11339     (set-window-point (get-buffer-window (current-buffer)) (point))))
11340
11341 (defun gnus-article-summary-command-nosave ()
11342   "Execute the last keystroke in the summary buffer."
11343   (interactive)
11344   (let (func)
11345     (pop-to-buffer gnus-summary-buffer 'norecord)
11346     (setq func (lookup-key (current-local-map) (this-command-keys)))
11347     (call-interactively func)))
11348
11349 \f
11350 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11351
11352 ;;;###autoload
11353 (defalias 'gnus-batch-kill 'gnus-batch-score)
11354 ;;;###autoload
11355 (defun gnus-batch-score ()
11356   "Run batched scoring.
11357 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11358 Newsgroups is a list of strings in Bnews format.  If you want to score
11359 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11360 score the alt hierarchy, you'd say \"!alt.all\"."
11361   (interactive)
11362   (let* ((yes-and-no
11363           (gnus-newsrc-parse-options
11364            (apply (function concat)
11365                   (mapcar (lambda (g) (concat g " "))
11366                           command-line-args-left))))
11367          (gnus-expert-user t)
11368          (nnmail-spool-file nil)
11369          (gnus-use-dribble-file nil)
11370          (yes (car yes-and-no))
11371          (no (cdr yes-and-no))
11372          group newsrc entry
11373          ;; Disable verbose message.
11374          gnus-novice-user gnus-large-newsgroup)
11375     ;; Eat all arguments.
11376     (setq command-line-args-left nil)
11377     ;; Start Gnus.
11378     (gnus)
11379     ;; Apply kills to specified newsgroups in command line arguments.
11380     (setq newsrc (cdr gnus-newsrc-alist))
11381     (while newsrc
11382       (setq group (car (car newsrc)))
11383       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11384       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11385                (and (car entry)
11386                     (or (eq (car entry) t)
11387                         (not (zerop (car entry)))))
11388                (if yes (string-match yes group) t)
11389                (or (null no) (not (string-match no group))))
11390           (progn
11391             (gnus-summary-read-group group nil t)
11392             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11393                  (gnus-summary-exit))))
11394       (setq newsrc (cdr newsrc)))
11395     ;; Exit Emacs.
11396     (switch-to-buffer gnus-group-buffer)
11397     (gnus-group-save-newsrc)))
11398
11399 (defun gnus-apply-kill-file ()
11400   "Apply a kill file to the current newsgroup.
11401 Returns the number of articles marked as read."
11402   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11403           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11404       (gnus-apply-kill-file-internal)
11405     0))
11406
11407 (defun gnus-kill-save-kill-buffer ()
11408   (save-excursion
11409     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11410       (if (get-file-buffer file)
11411           (progn
11412             (set-buffer (get-file-buffer file))
11413             (and (buffer-modified-p) (save-buffer))
11414             (kill-buffer (current-buffer)))))))
11415
11416 (defvar gnus-kill-file-name "KILL"
11417   "Suffix of the kill files.")
11418
11419 (defun gnus-newsgroup-kill-file (newsgroup)
11420   "Return the name of a kill file name for NEWSGROUP.
11421 If NEWSGROUP is nil, return the global kill file name instead."
11422   (cond ((or (null newsgroup)
11423              (string-equal newsgroup ""))
11424          ;; The global KILL file is placed at top of the directory.
11425          (expand-file-name gnus-kill-file-name
11426                            (or gnus-kill-files-directory "~/News")))
11427         ((gnus-use-long-file-name 'not-kill)
11428          ;; Append ".KILL" to newsgroup name.
11429          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11430                                    "." gnus-kill-file-name)
11431                            (or gnus-kill-files-directory "~/News")))
11432         (t
11433          ;; Place "KILL" under the hierarchical directory.
11434          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11435                                    "/" gnus-kill-file-name)
11436                            (or gnus-kill-files-directory "~/News")))))
11437
11438 \f
11439 ;;;
11440 ;;; Dribble file
11441 ;;;
11442
11443 (defvar gnus-dribble-ignore nil)
11444 (defvar gnus-dribble-eval-file nil)
11445
11446 (defun gnus-dribble-file-name ()
11447   (concat gnus-current-startup-file "-dribble"))
11448
11449 (defun gnus-dribble-enter (string)
11450   (if (and (not gnus-dribble-ignore)
11451            gnus-dribble-buffer
11452            (buffer-name gnus-dribble-buffer))
11453       (let ((obuf (current-buffer)))
11454         (set-buffer gnus-dribble-buffer)
11455         (insert string "\n")
11456         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11457         (set-buffer obuf))))
11458
11459 (defun gnus-dribble-read-file ()
11460   (let ((dribble-file (gnus-dribble-file-name)))
11461     (save-excursion 
11462       (set-buffer (setq gnus-dribble-buffer 
11463                         (get-buffer-create 
11464                          (file-name-nondirectory dribble-file))))
11465       (gnus-add-current-to-buffer-list)
11466       (erase-buffer)
11467       (set-visited-file-name dribble-file)
11468       (buffer-disable-undo (current-buffer))
11469       (bury-buffer (current-buffer))
11470       (set-buffer-modified-p nil)
11471       (let ((auto (make-auto-save-file-name))
11472             (gnus-dribble-ignore t))
11473         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11474             (progn
11475               (if (file-newer-than-file-p auto dribble-file)
11476                   (setq dribble-file auto))
11477               (insert-file-contents dribble-file)
11478               (if (not (zerop (buffer-size)))
11479                   (set-buffer-modified-p t))
11480               (if (gnus-y-or-n-p 
11481                    "Auto-save file exists. Do you want to read it? ")
11482                   (setq gnus-dribble-eval-file t))))))))
11483
11484 (defun gnus-dribble-eval-file ()
11485   (if (not gnus-dribble-eval-file)
11486       ()
11487     (setq gnus-dribble-eval-file nil)
11488     (save-excursion
11489       (let ((gnus-dribble-ignore t))
11490         (set-buffer gnus-dribble-buffer)
11491         (eval-buffer (current-buffer))))))
11492
11493 (defun gnus-dribble-delete-file ()
11494   (if (file-exists-p (gnus-dribble-file-name))
11495       (delete-file (gnus-dribble-file-name)))
11496   (if gnus-dribble-buffer
11497       (save-excursion
11498         (set-buffer gnus-dribble-buffer)
11499         (let ((auto (make-auto-save-file-name)))
11500           (if (file-exists-p auto)
11501               (delete-file auto))
11502           (erase-buffer)
11503           (set-buffer-modified-p nil)))))
11504
11505 (defun gnus-dribble-save ()
11506   (if (and gnus-dribble-buffer
11507            (buffer-name gnus-dribble-buffer))
11508       (save-excursion
11509         (set-buffer gnus-dribble-buffer)
11510         (save-buffer))))
11511
11512 (defun gnus-dribble-clear ()
11513   (save-excursion
11514     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11515         (progn
11516           (set-buffer gnus-dribble-buffer)
11517           (erase-buffer)
11518           (set-buffer-modified-p nil)
11519           (setq buffer-saved-size (buffer-size))))))
11520
11521 ;;;
11522 ;;; Server Communication
11523 ;;;
11524
11525 ;; All the Gnus backends have the same interface, and should return
11526 ;; data in a similar format. Below is an overview of what functions
11527 ;; these packages must supply and what results they should return.
11528 ;;
11529 ;; Variables:
11530 ;;
11531 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11532 ;; buffer. 
11533 ;;
11534 ;; Functions for the imaginary backend `choke':
11535 ;;
11536 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11537 ;; Should return all headers for all ARTICLES, or return NOV lines for
11538 ;; the same.
11539 ;;
11540 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11541 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11542 ;; must be returned.
11543 ;;
11544 ;; `choke-close-group GROUP &optional SERVER'
11545 ;; Close group. Most backends won't have to do anything with this
11546 ;; call, but it is an opportunity to clean up, if that is needed. It
11547 ;; is called when Gnus exits a group.
11548 ;;
11549 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11550 ;; Return ARTICLE, which is either an article number or
11551 ;; message-id. Note that not all backends can return articles based on
11552 ;; message-id. 
11553 ;;
11554 ;; `choke-request-list SERVER'
11555 ;; Return a list of all newsgroups on SERVER.
11556 ;;
11557 ;; `choke-request-list-newsgroups SERVER'
11558 ;; Return a list of descriptions of all newsgroups on SERVER.
11559 ;;
11560 ;; `choke-request-newgroups DATE &optional SERVER'
11561 ;; Return a list of all groups that have arrived after DATE on
11562 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11563 ;; always check whether the groups are old or not. Backends that do
11564 ;; not store date information may just return the entire list of
11565 ;; groups, although this might not be a good idea in general.
11566 ;;
11567 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11568 ;; Should return a buffer that is suitable for "posting". nnspool and
11569 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11570 ;; buffer. This function should fill out the appropriate headers. 
11571 ;;
11572 ;; `choke-request-post &optional SERVER'
11573 ;; Function that will be called from a buffer to be posted. 
11574 ;;
11575 ;; `choke-open-server SERVER &optional ARGUMENT'
11576 ;; Open a connection to SERVER.
11577 ;;
11578 ;; `choke-close-server &optional SERVER'
11579 ;; Close the connection to SERVER.
11580 ;;
11581 ;; `choke-server-opened &optional SERVER'
11582 ;; Whether the conenction to SERVER is opened or not.
11583 ;;
11584 ;; `choke-server-status &optional SERVER'
11585 ;; Should return a status string (not in the nntp buffer, but as the
11586 ;; result of the function).
11587 ;;
11588 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11589 ;; Optional function for retrieving active file info on all groups in
11590 ;; GROUPS.  Two return formats are supported: The normal active file
11591 ;; format, and a list of GROUP lines.  This function should return (as
11592 ;; a function value) either `active' or `group', depending on what
11593 ;; format it returns.
11594 ;;
11595 ;; The following functions are optional and apply only to backends
11596 ;; that are able to control the contents of their groups totally
11597 ;; (ie. mail backends.)  Backends that aren't able to do that
11598 ;; shouldn't define these functions at all. Gnus will check for their
11599 ;; presence before attempting to call them.
11600 ;;
11601 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11602 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11603 ;; backends will not be able to expire articles. Should return a list
11604 ;; of all articles that were not expired.
11605 ;;
11606 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11607 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11608 ;; Removes any information it has added to the article (extra headers,
11609 ;; whatever - make it as clean as possible), and then passes the
11610 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11611 ;; function described below. If the ACCEPT-FORM returns a non-nil
11612 ;; value, the article should then be deleted. If LAST is nil, that
11613 ;; means that there will be further calls to this function. This might
11614 ;; be taken as an advice not to save buffers/internal variables just
11615 ;; yet, but wait until the last call to speed things up.
11616 ;;
11617 ;; `choke-request-accept-article GROUP &optional LAST' 
11618 ;; The contents of the current buffer will be put into GROUP.  There
11619 ;; should, of course, be an article in the current buffer.  This
11620 ;; function is normally only called by the function described above,
11621 ;; and LAST works the same way as in that function.
11622 ;;
11623 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11624 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11625 ;; This provides an easy interface for allowing editing of
11626 ;; articles. Note that even headers may be edited, so the backend has
11627 ;; to update any tables (nov buffers, etc) that it maintains after
11628 ;; replacing the article.
11629 ;;
11630 ;; `choke-request-create-group GROUP &optional SERVER'
11631 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11632 ;; might be a group that already exists, but hasn't been registered
11633 ;; yet. 
11634 ;;
11635 ;; All these functions must return nil if they couldn't service the
11636 ;; request. If the optional arguments are not supplied, some "current"
11637 ;; or "default" values should be used. In short, one should emulate an
11638 ;; NNTP server, in a way.
11639 ;;
11640 ;; If you want to write a new backend, you just have to supply the
11641 ;; functions listed above. In addition, you must enter the new backend
11642 ;; into the list of valid select methods:
11643 ;; (setq gnus-valid-select-methods 
11644 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11645 ;; The first element in this list is the name of the backend. Other
11646 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11647 ;; groups), `none' (neither), `respool' (for groups that can control
11648 ;; their contents). 
11649
11650 (defun gnus-start-news-server (&optional confirm)
11651   "Open a method for getting news.
11652 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11653   (let (how)
11654     (if gnus-current-select-method
11655         ;; Stream is already opened.
11656         nil
11657       ;; Open NNTP server.
11658       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11659       (if confirm
11660           (progn
11661             ;; Read server name with completion.
11662             (setq gnus-nntp-server
11663                   (completing-read "NNTP server: "
11664                                    (mapcar (lambda (server) (list server))
11665                                            (cons (list gnus-nntp-server)
11666                                                  gnus-secondary-servers))
11667                                    nil nil gnus-nntp-server))))
11668
11669       (if (and gnus-nntp-server 
11670                (stringp gnus-nntp-server)
11671                (not (string= gnus-nntp-server "")))
11672           (setq gnus-select-method
11673                 (cond ((or (string= gnus-nntp-server "")
11674                            (string= gnus-nntp-server "::"))
11675                        (list 'nnspool (system-name)))
11676                       ((string-match "^:" gnus-nntp-server)
11677                        (list 'nnmh gnus-nntp-server 
11678                              (list 'nnmh-directory 
11679                                    (file-name-as-directory
11680                                     (expand-file-name
11681                                      (concat "~/" (substring
11682                                                    gnus-nntp-server 1)))))
11683                              (list 'nnmh-get-new-mail nil)))
11684                       (t
11685                        (list 'nntp gnus-nntp-server)))))
11686
11687       (setq how (car gnus-select-method))
11688       (cond ((eq how 'nnspool)
11689              (require 'nnspool)
11690              (gnus-message 5 "Looking up local news spool..."))
11691             ((eq how 'nnmh)
11692              (require 'nnmh)
11693              (gnus-message 5 "Looking up mh spool..."))
11694             (t
11695              (require 'nntp)))
11696       (setq gnus-current-select-method gnus-select-method)
11697       (run-hooks 'gnus-open-server-hook)
11698       (or 
11699        ;; gnus-open-server-hook might have opened it
11700        (gnus-server-opened gnus-select-method)  
11701        (gnus-open-server gnus-select-method)
11702        (gnus-y-or-n-p
11703         (format
11704          "%s server on %s can't be opened. Continue? "
11705          (car gnus-select-method) (nth 1 gnus-select-method)))
11706        (progn
11707          (gnus-message 1 "Couldn't open server on %s" 
11708                        (nth 1 gnus-select-method))
11709          (ding)
11710          nil)))))
11711
11712 (defun gnus-check-server (&optional method)
11713   "If the news server is down, start it up again."
11714   (let ((method (if method method gnus-select-method)))
11715     (and (stringp method)
11716          (setq method (gnus-server-to-method method)))
11717     (if (gnus-server-opened method)
11718         ;; Stream is already opened.
11719         t
11720       ;; Open server.
11721       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11722       (run-hooks 'gnus-open-server-hook)
11723       (prog1
11724           (gnus-open-server method)
11725         (message "")))))
11726
11727 (defun gnus-nntp-message (&optional message)
11728   "Check the status of the NNTP server.
11729 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11730 is returned insted of the status string."
11731   (let ((status (gnus-status-message (gnus-find-method-for-group 
11732                                       gnus-newsgroup-name)))
11733         (message (or message "")))
11734     (if (and (stringp status) (> (length status) 0))
11735         status message)))
11736
11737 (defun gnus-get-function (method function)
11738   (and (stringp method)
11739        (setq method (gnus-server-to-method method)))
11740   (let ((func (intern (format "%s-%s" (car method) function))))
11741     (if (not (fboundp func)) 
11742         (progn
11743           (require (car method))
11744           (if (not (fboundp func)) 
11745               (error "No such function: %s" func))))
11746     func))
11747
11748 ;;; Interface functions to the backends.
11749
11750 (defun gnus-open-server (method)
11751   (funcall (gnus-get-function method 'open-server)
11752            (nth 1 method) (nthcdr 2 method)))
11753
11754 (defun gnus-close-server (method)
11755   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11756
11757 (defun gnus-request-list (method)
11758   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11759
11760 (defun gnus-request-list-newsgroups (method)
11761   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11762
11763 (defun gnus-request-newgroups (date method)
11764   (funcall (gnus-get-function method 'request-newgroups) 
11765            date (nth 1 method)))
11766
11767 (defun gnus-server-opened (method)
11768   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11769
11770 (defun gnus-status-message (method)
11771   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11772                   method)))
11773     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11774
11775 (defun gnus-request-group (group &optional dont-check)
11776   (let ((method (gnus-find-method-for-group group)))
11777 ;    (and t (message "%s GROUP %s" (car method) group))
11778     (funcall (gnus-get-function method 'request-group) 
11779              (gnus-group-real-name group) (nth 1 method) dont-check)))
11780
11781 (defun gnus-request-asynchronous (group &optional articles)
11782   (let ((method (gnus-find-method-for-group group)))
11783     (funcall (gnus-get-function method 'request-asynchronous) 
11784              (gnus-group-real-name group) (nth 1 method) articles)))
11785
11786 (defun gnus-list-active-group (group)
11787   (let ((method (gnus-find-method-for-group group))
11788         (func 'list-active-group))
11789     (and (gnus-check-backend-function func group)
11790          (funcall (gnus-get-function method func) 
11791                   (gnus-group-real-name group) (nth 1 method)))))
11792
11793 (defun gnus-request-group-description (group)
11794   (let ((method (gnus-find-method-for-group group))
11795         (func 'request-group-description))
11796     (and (gnus-check-backend-function func group)
11797          (funcall (gnus-get-function method func) 
11798                   (gnus-group-real-name group) (nth 1 method)))))
11799
11800 (defun gnus-close-group (group)
11801   (let ((method (gnus-find-method-for-group group)))
11802     (funcall (gnus-get-function method 'close-group) 
11803              (gnus-group-real-name group) (nth 1 method))))
11804
11805 (defun gnus-retrieve-headers (articles group)
11806   (let ((method (gnus-find-method-for-group group)))
11807     (if (and gnus-use-cache (numberp (car articles)))
11808         (gnus-cache-retrieve-headers articles group)
11809       (funcall (gnus-get-function method 'retrieve-headers) 
11810                articles (gnus-group-real-name group) (nth 1 method)))))
11811
11812 (defun gnus-retrieve-groups (groups method)
11813   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11814
11815 (defun gnus-request-article (article group &optional buffer)
11816   (let ((method (gnus-find-method-for-group group)))
11817     (funcall (gnus-get-function method 'request-article) 
11818              article (gnus-group-real-name group) (nth 1 method) buffer)))
11819
11820 (defun gnus-request-head (article group)
11821   (let ((method (gnus-find-method-for-group group)))
11822     (funcall (gnus-get-function method 'request-head) 
11823              article (gnus-group-real-name group) (nth 1 method))))
11824
11825 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11826 (defun gnus-request-post-buffer (post group subject header artbuf
11827                                       info follow-to respect-poster)
11828    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11829                                             group gnus-newsrc-hashtb)))))
11830           (method
11831            (if (and gnus-post-method
11832                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11833                     (memq 'post (assoc
11834                                  (format "%s" (car (gnus-find-method-for-group
11835                                                     gnus-newsgroup-name)))
11836                                         gnus-valid-select-methods)))
11837                gnus-post-method
11838              (gnus-find-method-for-group gnus-newsgroup-name))))
11839      (or (gnus-check-server method)
11840          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11841      (let ((mail-self-blind nil)
11842            (mail-archive-file-name nil))
11843        (funcall (gnus-get-function method 'request-post-buffer) 
11844                 post group subject header artbuf info follow-to
11845                 respect-poster))))
11846
11847 (defun gnus-request-post (method &optional force)
11848   (and (stringp method)
11849        (setq method (gnus-server-to-method method)))
11850   (and (not force) gnus-post-method
11851        (memq 'post (assoc (format "%s" (car method))
11852                           gnus-valid-select-methods))
11853        (setq method gnus-post-method))
11854   (funcall (gnus-get-function method 'request-post) 
11855            (nth 1 method)))
11856
11857 (defun gnus-request-expire-articles (articles group &optional force)
11858   (let ((method (gnus-find-method-for-group group)))
11859     (funcall (gnus-get-function method 'request-expire-articles) 
11860              articles (gnus-group-real-name group) (nth 1 method)
11861              force)))
11862
11863 (defun gnus-request-move-article 
11864   (article group server accept-function &optional last)
11865   (let ((method (gnus-find-method-for-group group)))
11866     (funcall (gnus-get-function method 'request-move-article) 
11867              article (gnus-group-real-name group) 
11868              (nth 1 method) accept-function last)))
11869
11870 (defun gnus-request-accept-article (group &optional last)
11871   (let ((func (if (symbolp group) group
11872                 (car (gnus-find-method-for-group group)))))
11873     (funcall (intern (format "%s-request-accept-article" func))
11874              (if (stringp group) (gnus-group-real-name group) group)
11875              last)))
11876
11877 (defun gnus-request-replace-article (article group buffer)
11878   (let ((func (car (gnus-find-method-for-group group))))
11879     (funcall (intern (format "%s-request-replace-article" func))
11880              article (gnus-group-real-name group) buffer)))
11881
11882 (defun gnus-request-create-group (group)
11883   (let ((method (gnus-find-method-for-group group)))
11884     (funcall (gnus-get-function method 'request-create-group) 
11885              (gnus-group-real-name group) (nth 1 method))))
11886
11887 (defun gnus-member-of-valid (symbol group)
11888   (memq symbol (assoc
11889                 (format "%s" (car (gnus-find-method-for-group group)))
11890                 gnus-valid-select-methods)))
11891
11892 (defun gnus-secondary-method-p (method)
11893   (let ((methods gnus-secondary-select-methods)
11894         (gmethod (gnus-server-get-method nil method)))
11895     (while (and methods
11896                 (not (equal (gnus-server-get-method nil (car methods)) 
11897                             gmethod)))
11898       (setq methods (cdr methods)))
11899     methods))
11900
11901 (defun gnus-find-method-for-group (group &optional info)
11902   (or gnus-override-method
11903       (and (not group)
11904            gnus-select-method)
11905       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11906             method)
11907         (if (or (not info)
11908                 (not (setq method (nth 4 info))))
11909             (setq method gnus-select-method)
11910           (setq method
11911                 (cond ((stringp method)
11912                        (gnus-server-to-method method))
11913                       ((stringp (car method))
11914                        (gnus-server-extend-method group method))
11915                       (t
11916                        method))))
11917         (gnus-server-add-address method))))
11918
11919 (defun gnus-check-backend-function (func group)
11920   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11921                  group)))
11922     (fboundp (intern (format "%s-%s" method func)))))
11923
11924 (defun gnus-methods-using (method)
11925   (let ((valids gnus-valid-select-methods)
11926         outs)
11927     (while valids
11928       (if (memq method (car valids)) 
11929           (setq outs (cons (car valids) outs)))
11930       (setq valids (cdr valids)))
11931     outs))
11932
11933 ;;; 
11934 ;;; Active & Newsrc File Handling
11935 ;;;
11936
11937 ;; Newsrc related functions.
11938 ;; Gnus internal format of gnus-newsrc-alist:
11939 ;; (("alt.general" 3 (1 . 1))
11940 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11941 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11942 ;; The first item is the group name; the second is the subscription
11943 ;; level; the third is either a range of a list of ranges of read
11944 ;; articles, the optional fourth element is a list of marked articles,
11945 ;; the optional fifth element is the select method.
11946 ;;
11947 ;; Gnus internal format of gnus-newsrc-hashtb:
11948 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11949 ;; This is the entry for "alt.misc". The first element is the number
11950 ;; of unread articles in "alt.misc". The cdr of this entry is the
11951 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11952 ;; trivial to remove or add new elements into gnus-newsrc-alist
11953 ;; without scanning the entire list. So, to get the actual information
11954 ;; of "alt.misc", you'd say something like 
11955 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11956 ;;
11957 ;; Gnus internal format of gnus-active-hashtb:
11958 ;; ((1 . 1))
11959 ;;  (5 . 10))
11960 ;;  (67 . 99)) ...)
11961 ;; The only element in each entry in this hash table is a range of
11962 ;; (possibly) available articles. (Articles in this range may have
11963 ;; been expired or canceled.)
11964 ;;
11965 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11966 ;; ("alt.misc" "alt.test" "alt.general" ...)
11967
11968 (defun gnus-setup-news (&optional rawfile level)
11969   "Setup news information.
11970 If RAWFILE is non-nil, the .newsrc file will also be read.
11971 If LEVEL is non-nil, the news will be set up at level LEVEL."
11972   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11973     ;; Clear some variables to re-initialize news information.
11974     (if init (setq gnus-newsrc-alist nil 
11975                    gnus-active-hashtb nil))
11976
11977     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11978     (if init (gnus-read-newsrc-file rawfile))
11979
11980     ;; If we don't read the complete active file, we fill in the
11981     ;; hashtb here. 
11982     (if (or (null gnus-read-active-file)
11983             (eq gnus-read-active-file 'some))
11984         (gnus-update-active-hashtb-from-killed))
11985
11986     ;; Read the active file and create `gnus-active-hashtb'.
11987     ;; If `gnus-read-active-file' is nil, then we just create an empty
11988     ;; hash table. The partial filling out of the hash table will be
11989     ;; done in `gnus-get-unread-articles'.
11990     (and gnus-read-active-file 
11991          (not level)
11992          (gnus-read-active-file))
11993
11994     ;; Possibly eval the dribble file.
11995     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11996
11997     (gnus-update-format-specifications)
11998
11999     ;; Find the number of unread articles in each non-dead group.
12000     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12001       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12002     ;; Find new newsgroups and treat them.
12003     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12004              (gnus-server-opened gnus-select-method))
12005         (gnus-find-new-newsgroups))
12006     (if (and init gnus-check-bogus-newsgroups 
12007              gnus-read-active-file (not level)
12008              (gnus-server-opened gnus-select-method))
12009         (gnus-check-bogus-newsgroups))))
12010
12011 (defun gnus-find-new-newsgroups ()
12012   "Search for new newsgroups and add them.
12013 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12014 The `-n' option line from .newsrc is respected."
12015   (interactive)
12016   (or (gnus-check-first-time-used)
12017       (if (or (consp gnus-check-new-newsgroups)
12018               (eq gnus-check-new-newsgroups 'ask-server))
12019           (gnus-ask-server-for-new-groups)
12020         (let ((groups 0)
12021               group new-newsgroups)
12022           (gnus-message 5 "Checking for new newsgroups...")
12023           (or gnus-have-read-active-file (gnus-read-active-file))
12024           (setq gnus-newsrc-last-checked-date (current-time-string))
12025           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12026           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12027           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12028           (mapatoms
12029            (lambda (sym)
12030              (if (or (null (setq group (symbol-name sym)))
12031                      (null (symbol-value sym))
12032                      (gnus-gethash group gnus-killed-hashtb)
12033                      (gnus-gethash group gnus-newsrc-hashtb))
12034                  ()
12035                (let ((do-sub (gnus-matches-options-n group)))
12036                  (cond 
12037                   ((eq do-sub 'subscribe)
12038                    (setq groups (1+ groups))
12039                    (gnus-sethash group group gnus-killed-hashtb)
12040                    (funcall gnus-subscribe-options-newsgroup-method group))
12041                   ((eq do-sub 'ignore)
12042                    nil)
12043                   (t
12044                    (setq groups (1+ groups))
12045                    (gnus-sethash group group gnus-killed-hashtb)
12046                    (if gnus-subscribe-hierarchical-interactive
12047                        (setq new-newsgroups (cons group new-newsgroups))
12048                      (funcall gnus-subscribe-newsgroup-method group)))))))
12049            gnus-active-hashtb)
12050           (if new-newsgroups 
12051               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12052           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12053           (if (> groups 0)
12054               (gnus-message 6 "%d new newsgroup%s arrived." 
12055                             groups (if (> groups 1) "s have" " has"))
12056             (gnus-message 6 "No new newsgroups."))))))
12057
12058 (defun gnus-matches-options-n (group)
12059   ;; Returns `subscribe' if the group is to be uncoditionally
12060   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12061   ;; no match for the group.
12062
12063   ;; First we check the two user variables.
12064   (cond
12065    ((and gnus-options-subscribe
12066          (string-match gnus-options-subscribe group))
12067     'subscribe)
12068    ((and gnus-options-not-subscribe
12069          (string-match gnus-options-not-subscribe group))
12070     'ignore)
12071    ;; Then we go through the list that was retrieved from the .newsrc
12072    ;; file.  This list has elements on the form 
12073    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12074    ;; is in the reverse order of the options line) is returned.
12075    (t
12076     (let ((regs gnus-newsrc-options-n))
12077       (while (and regs
12078                   (not (string-match (car (car regs)) group)))
12079         (setq regs (cdr regs)))
12080       (and regs (cdr (car regs)))))))
12081
12082 (defun gnus-ask-server-for-new-groups ()
12083   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12084          (methods (cons gnus-select-method 
12085                         (append
12086                          (and (consp gnus-check-new-newsgroups)
12087                               gnus-check-new-newsgroups)
12088                          gnus-secondary-select-methods)))
12089          (groups 0)
12090          (new-date (current-time-string))
12091          hashtb group new-newsgroups got-new method)
12092     ;; Go thorugh both primary and secondary select methods and
12093     ;; request new newsgroups.  
12094     (while methods
12095       (setq method (gnus-server-get-method nil (car methods)))
12096       (and (gnus-check-server method)
12097            (gnus-request-newgroups date method)
12098            (save-excursion
12099              (setq got-new t)
12100              (set-buffer nntp-server-buffer)
12101              (or hashtb (setq hashtb (gnus-make-hashtable 
12102                                       (count-lines (point-min) (point-max)))))
12103              ;; Enter all the new groups in a hashtable.
12104              (gnus-active-to-gnus-format method hashtb 'ignore)))
12105       (setq methods (cdr methods)))
12106     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12107     ;; Now all new groups from all select methods are in `hashtb'.
12108     (mapatoms
12109      (lambda (group-sym)
12110        (setq group (symbol-name group-sym))
12111        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12112                (member group gnus-zombie-list)
12113                (member group gnus-killed-list))
12114            ;; The group is already known.
12115            ()
12116          (and (symbol-value group-sym)
12117               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12118          (let ((do-sub (gnus-matches-options-n group)))
12119            (cond ((eq do-sub 'subscribe)
12120                   (setq groups (1+ groups))
12121                   (gnus-sethash group group gnus-killed-hashtb)
12122                   (funcall 
12123                    gnus-subscribe-options-newsgroup-method group))
12124                  ((eq do-sub 'ignore)
12125                   nil)
12126                  (t
12127                   (setq groups (1+ groups))
12128                   (gnus-sethash group group gnus-killed-hashtb)
12129                   (if gnus-subscribe-hierarchical-interactive
12130                       (setq new-newsgroups (cons group new-newsgroups))
12131                     (funcall gnus-subscribe-newsgroup-method group)))))))
12132      hashtb)
12133     (if new-newsgroups 
12134         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12135     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12136     (if (> groups 0)
12137         (gnus-message 6 "%d new newsgroup%s arrived." 
12138                       groups (if (> groups 1) "s have" " has")))
12139     got-new))
12140
12141 (defun gnus-check-first-time-used ()
12142   (if (or (> (length gnus-newsrc-alist) 1)
12143           (file-exists-p gnus-startup-file)
12144           (file-exists-p (concat gnus-startup-file ".el"))
12145           (file-exists-p (concat gnus-startup-file ".eld")))
12146       nil
12147     (gnus-message 6 "First time user; subscribing you to default groups")
12148     (or gnus-have-read-active-file (gnus-read-active-file))
12149     (setq gnus-newsrc-last-checked-date (current-time-string))
12150     (let ((groups gnus-default-subscribed-newsgroups)
12151           group)
12152       (if (eq groups t)
12153           nil
12154         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12155         (mapatoms
12156          (lambda (sym)
12157            (if (null (setq group (symbol-name sym)))
12158                ()
12159              (let ((do-sub (gnus-matches-options-n group)))
12160                (cond 
12161                 ((eq do-sub 'subscribe)
12162                  (gnus-sethash group group gnus-killed-hashtb)
12163                  (funcall gnus-subscribe-options-newsgroup-method group))
12164                 ((eq do-sub 'ignore)
12165                  nil)
12166                 (t
12167                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12168          gnus-active-hashtb)
12169         (while groups
12170           (if (gnus-gethash (car groups) gnus-active-hashtb)
12171               (gnus-group-change-level 
12172                (car groups) gnus-level-default-subscribed gnus-level-killed))
12173           (setq groups (cdr groups)))
12174         (gnus-group-make-help-group)
12175         (and gnus-novice-user
12176              (gnus-message 7 "`A k' to list killed groups"))))))
12177
12178 (defun gnus-subscribe-group (group previous &optional method)
12179   (gnus-group-change-level 
12180    (if method
12181        (list t group gnus-level-default-subscribed nil nil method)
12182      group) 
12183    gnus-level-default-subscribed gnus-level-killed previous t))
12184
12185 ;; `gnus-group-change-level' is the fundamental function for changing
12186 ;; subscription levels of newsgroups. This might mean just changing
12187 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12188 ;; again, which subscribes/unsubscribes a group, which is equally
12189 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12190 ;; from 8-9 to 1-7 means that you remove the group from the list of
12191 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12192 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12193 ;; which is trivial.
12194 ;; ENTRY can either be a string (newsgroup name) or a list (if
12195 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12196 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12197 ;; entries. 
12198 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12199 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12200 ;; after. 
12201 (defun gnus-group-change-level (entry level &optional oldlevel
12202                                       previous fromkilled)
12203   (let (group info active num)
12204     ;; Glean what info we can from the arguments
12205     (if (consp entry)
12206         (if fromkilled (setq group (nth 1 entry))
12207           (setq group (car (nth 2 entry))))
12208       (setq group entry))
12209     (if (and (stringp entry)
12210              oldlevel 
12211              (< oldlevel gnus-level-zombie))
12212         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12213     (if (and (not oldlevel)
12214              (consp entry))
12215         (setq oldlevel (car (cdr (nth 2 entry)))))
12216     (if (stringp previous)
12217         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12218
12219     (if (and (>= oldlevel gnus-level-zombie)
12220              (gnus-gethash group gnus-newsrc-hashtb))
12221         ;; We are trying to subscribe a group that is already
12222         ;; subscribed. 
12223         () ; Do nothing. 
12224
12225       (gnus-dribble-enter
12226        (format "(gnus-group-change-level %S %S %S %S %S)" 
12227                group level oldlevel (car (nth 2 previous)) fromkilled))
12228     
12229       ;; Then we remove the newgroup from any old structures, if needed.
12230       ;; If the group was killed, we remove it from the killed or zombie
12231       ;; list. If not, and it is in fact going to be killed, we remove
12232       ;; it from the newsrc hash table and assoc.
12233       (cond ((>= oldlevel gnus-level-zombie)
12234              (if (= oldlevel gnus-level-zombie)
12235                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12236                (setq gnus-killed-list (delete group gnus-killed-list))))
12237             (t
12238              (if (and (>= level gnus-level-zombie)
12239                       entry)
12240                  (progn
12241                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12242                    (if (nth 3 entry)
12243                        (setcdr (gnus-gethash (car (nth 3 entry))
12244                                              gnus-newsrc-hashtb)
12245                                (cdr entry)))
12246                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12247
12248       ;; Finally we enter (if needed) the list where it is supposed to
12249       ;; go, and change the subscription level. If it is to be killed,
12250       ;; we enter it into the killed or zombie list.
12251       (cond ((>= level gnus-level-zombie)
12252              ;; Remove from the hash table.
12253              (gnus-sethash group nil gnus-newsrc-hashtb)
12254              (or (gnus-group-foreign-p group)
12255                  ;; We do not enter foreign groups into the list of dead
12256                  ;; groups.  
12257                  (if (= level gnus-level-zombie)
12258                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12259                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12260             (t
12261              ;; If the list is to be entered into the newsrc assoc, and
12262              ;; it was killed, we have to create an entry in the newsrc
12263              ;; hashtb format and fix the pointers in the newsrc assoc.
12264              (if (>= oldlevel gnus-level-zombie)
12265                  (progn
12266                    (if (listp entry)
12267                        (progn
12268                          (setq info (cdr entry))
12269                          (setq num (car entry)))
12270                      (setq active (gnus-gethash group gnus-active-hashtb))
12271                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12272                      ;; Check whether the group is foreign. If so, the
12273                      ;; foreign select method has to be entered into the
12274                      ;; info. 
12275                      (let ((method (gnus-group-method-name group)))
12276                        (if (eq method gnus-select-method)
12277                            (setq info (list group level nil))
12278                          (setq info (list group level nil nil method)))))
12279                    (or previous 
12280                        (setq previous 
12281                              (let ((p gnus-newsrc-alist))
12282                                (while (cdr (cdr p))
12283                                  (setq p (cdr p)))
12284                                p)))
12285                    (setq entry (cons info (cdr (cdr previous))))
12286                    (if (cdr previous)
12287                        (progn
12288                          (setcdr (cdr previous) entry)
12289                          (gnus-sethash group (cons num (cdr previous)) 
12290                                        gnus-newsrc-hashtb))
12291                      (setcdr previous entry)
12292                      (gnus-sethash group (cons num previous)
12293                                    gnus-newsrc-hashtb))
12294                    (if (cdr entry)
12295                        (setcdr (gnus-gethash (car (car (cdr entry)))
12296                                              gnus-newsrc-hashtb)
12297                                entry)))
12298                ;; It was alive, and it is going to stay alive, so we
12299                ;; just change the level and don't change any pointers or
12300                ;; hash table entries.
12301                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12302
12303 (defun gnus-kill-newsgroup (newsgroup)
12304   "Obsolete function. Kills a newsgroup."
12305   (gnus-group-change-level
12306    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12307
12308 (defun gnus-check-bogus-newsgroups (&optional confirm)
12309   "Remove bogus newsgroups.
12310 If CONFIRM is non-nil, the user has to confirm the deletion of every
12311 newsgroup." 
12312   (let ((newsrc (cdr gnus-newsrc-alist))
12313         bogus group entry)
12314     (gnus-message 5 "Checking bogus newsgroups...")
12315     (or gnus-have-read-active-file (gnus-read-active-file))
12316     ;; Find all bogus newsgroup that are subscribed.
12317     (while newsrc
12318       (setq group (car (car newsrc)))
12319       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12320               (nth 4 (car newsrc))      ; Foreign
12321               (and confirm
12322                    (not (gnus-y-or-n-p
12323                          (format "Remove bogus newsgroup: %s " group)))))
12324           ;; Don't remove.
12325           ()
12326         ;; Found a bogus newsgroup.
12327         (setq bogus (cons group bogus)))
12328       (setq newsrc (cdr newsrc)))
12329     ;; Remove all bogus subscribed groups by first killing them, and
12330     ;; then removing them from the list of killed groups.
12331     (while bogus
12332       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12333            (progn
12334              (gnus-group-change-level entry gnus-level-killed)
12335              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12336       (setq bogus (cdr bogus)))
12337     ;; Then we remove all bogus groups from the list of killed and
12338     ;; zombie groups. They are are removed without confirmation.
12339     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12340           killed)
12341       (while dead-lists
12342         (setq killed (symbol-value (car dead-lists)))
12343         (while killed
12344           (setq group (car killed))
12345           (or (gnus-gethash group gnus-active-hashtb)
12346               ;; The group is bogus.
12347               (set (car dead-lists)
12348                    (delete group (symbol-value (car dead-lists)))))
12349           (setq killed (cdr killed)))
12350         (setq dead-lists (cdr dead-lists))))
12351     (gnus-message 5 "Checking bogus newsgroups...done")))
12352
12353 (defun gnus-check-duplicate-killed-groups ()
12354   "Remove duplicates from the list of killed groups."
12355   (interactive)
12356   (let ((killed gnus-killed-list))
12357     (while killed
12358       (gnus-message 9 "%d" (length killed))
12359       (setcdr killed (delete (car killed) (cdr killed)))
12360       (setq killed (cdr killed)))))
12361
12362 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12363 ;; and compute how many unread articles there are in each group.
12364 (defun gnus-get-unread-articles (&optional level) 
12365   (let* ((newsrc (cdr gnus-newsrc-alist))
12366          (level (or level (1+ gnus-level-subscribed)))
12367          (foreign-level
12368           (min 
12369            (cond ((and gnus-activate-foreign-newsgroups 
12370                        (not (numberp gnus-activate-foreign-newsgroups)))
12371                   (1+ gnus-level-subscribed))
12372                  ((numberp gnus-activate-foreign-newsgroups)
12373                   gnus-activate-foreign-newsgroups)
12374                  (t 0))
12375            level))
12376          info group active virtuals method)
12377     (gnus-message 5 "Checking new news...")
12378
12379     (while newsrc
12380       (setq info (car newsrc)
12381             group (car info)
12382             active (gnus-gethash group gnus-active-hashtb))
12383
12384       ;; Check newsgroups. If the user doesn't want to check them, or
12385       ;; they can't be checked (for instance, if the news server can't
12386       ;; be reached) we just set the number of unread articles in this
12387       ;; newsgroup to t. This means that Gnus thinks that there are
12388       ;; unread articles, but it has no idea how many.
12389       (if (and (setq method (nth 4 info))
12390                (not (gnus-server-equal gnus-select-method
12391                                        (gnus-server-get-method nil method)))
12392                (not (gnus-secondary-method-p method)))
12393           ;; These groups are foreign. Check the level.
12394           (if (<= (nth 1 info) foreign-level)
12395               (if (eq (car (if (stringp method) 
12396                                (gnus-server-to-method method)
12397                              (nth 4 info))) 'nnvirtual)
12398                   ;; We have to activate the virtual groups after all
12399                   ;; the others, so we just pop them on a list for
12400                   ;; now. 
12401                   (setq virtuals (cons info virtuals))
12402                 (and (setq active (gnus-activate-group (car info)))
12403                      ;; Close the groups as we look at them!
12404                      (gnus-close-group group))))
12405
12406         (or gnus-read-active-file (gnus-check-server method))
12407         ;; These groups are native or secondary. 
12408         (if (and (not gnus-read-active-file)
12409                  (<= (nth 1 info) level))
12410             (setq active (gnus-activate-group (car info)))))
12411       
12412       (if active
12413           (gnus-get-unread-articles-in-group info active)
12414         ;; The group couldn't be reached, so we nix out the number of
12415         ;; unread articles and stuff.
12416         (gnus-sethash group nil gnus-active-hashtb)
12417         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12418
12419       (setq newsrc (cdr newsrc)))
12420
12421     ;; Activate the virtual groups. This has to be done after all the
12422     ;; other groups. 
12423     ;; !!! If one virtual group contains another virtual group, even
12424     ;; doing it this way might cause problems.
12425     (while virtuals
12426       (and (setq active (gnus-activate-group (car (car virtuals))))
12427            (gnus-get-unread-articles-in-group (car virtuals) active))
12428       (setq virtuals (cdr virtuals)))
12429
12430     (gnus-message 5 "Checking new news...done")))
12431
12432 ;; Create a hash table out of the newsrc alist. The `car's of the
12433 ;; alist elements are used as keys.
12434 (defun gnus-make-hashtable-from-newsrc-alist ()
12435   (let ((alist gnus-newsrc-alist)
12436         (ohashtb gnus-newsrc-hashtb)
12437         prev)
12438     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12439     (setq alist 
12440           (setq prev (setq gnus-newsrc-alist 
12441                            (if (equal (car (car gnus-newsrc-alist))
12442                                       "dummy.group")
12443                                gnus-newsrc-alist
12444                              (cons (list "dummy.group" 0 nil) alist)))))
12445     (while alist
12446       (gnus-sethash (car (car alist)) 
12447                     (cons (and ohashtb (car (gnus-gethash 
12448                                              (car (car alist)) ohashtb))) 
12449                           prev) gnus-newsrc-hashtb)
12450       (setq prev alist
12451             alist (cdr alist)))))
12452
12453 (defun gnus-make-hashtable-from-killed ()
12454   "Create a hash table from the killed and zombie lists."
12455   (let ((lists '(gnus-killed-list gnus-zombie-list))
12456         list)
12457     (setq gnus-killed-hashtb 
12458           (gnus-make-hashtable 
12459            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12460     (while lists
12461       (setq list (symbol-value (car lists)))
12462       (setq lists (cdr lists))
12463       (while list
12464         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12465         (setq list (cdr list))))))
12466
12467 (defun gnus-get-unread-articles-in-group (info active)
12468   (let* ((range (nth 2 info))
12469          (num 0)
12470          (marked (nth 3 info)))
12471     ;; If a cache is present, we may have to alter the active info.
12472     (and gnus-use-cache
12473          (gnus-cache-possibly-alter-active (car info) active))
12474     ;; Modify the list of read articles according to what articles 
12475     ;; are available; then tally the unread articles and add the
12476     ;; number to the group hash table entry.
12477     (cond 
12478      ((zerop (cdr active))
12479       (setq num 0))
12480      ((not range)
12481       (setq num (- (1+ (cdr active)) (car active))))
12482      ((not (listp (cdr range)))
12483       ;; Fix a single (num . num) range according to the
12484       ;; active hash table.
12485       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12486       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12487       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12488       ;; Compute number of unread articles.
12489       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12490      (t
12491       ;; The read list is a list of ranges. Fix them according to
12492       ;; the active hash table.
12493       ;; First peel off any elements that are below the lower
12494       ;; active limit. 
12495       (while (and (cdr range) 
12496                   (>= (car active) 
12497                       (or (and (atom (car (cdr range))) (car (cdr range)))
12498                           (car (car (cdr range))))))
12499         (if (numberp (car range))
12500             (setcar range 
12501                     (cons (car range) 
12502                           (or (and (numberp (car (cdr range)))
12503                                    (car (cdr range))) 
12504                               (cdr (car (cdr range))))))
12505           (setcdr (car range) 
12506                   (or (and (numberp (nth 1 range)) (nth 1 range))
12507                       (cdr (car (cdr range))))))
12508         (setcdr range (cdr (cdr range))))
12509       ;; Adjust the first element to be the same as the lower limit. 
12510       (if (and (not (atom (car range))) 
12511                (< (cdr (car range)) (car active)))
12512           (setcdr (car range) (1- (car active))))
12513       ;; Then we want to peel off any elements that are higher
12514       ;; than the upper active limit.  
12515       (let ((srange range))
12516         ;; Go past all legal elements.
12517         (while (and (cdr srange) 
12518                     (<= (or (and (atom (car (cdr srange)))
12519                                  (car (cdr srange)))
12520                             (car (car (cdr srange)))) (cdr active)))
12521           (setq srange (cdr srange)))
12522         (if (cdr srange)
12523             ;; Nuke all remaining illegal elements.
12524             (setcdr srange nil))
12525
12526         ;; Adjust the final element.
12527         (if (and (not (atom (car srange)))
12528                  (> (cdr (car srange)) (cdr active)))
12529             (setcdr (car srange) (cdr active))))
12530       ;; Compute the number of unread articles.
12531       (while range
12532         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12533                                     (cdr (car range))))
12534                             (or (and (atom (car range)) (car range))
12535                                 (car (car range))))))
12536         (setq range (cdr range)))
12537       (setq num (max 0 (- (cdr active) num)))))
12538     (and info
12539          (progn
12540            (and (assq 'tick marked)
12541                 (inline (gnus-remove-illegal-marked-articles
12542                          (assq 'tick marked) (nth 2 info))))
12543            (and (assq 'dormant marked)
12544                 (inline (gnus-remove-illegal-marked-articles
12545                          (assq 'dormant marked) (nth 2 info))))
12546            (setcar
12547             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12548             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12549                                 (length (cdr (assq 'dormant marked)))))))))
12550     num))
12551
12552 (defun gnus-remove-illegal-marked-articles (marked ranges)
12553   (let ((m (cdr marked)))
12554     ;; Make sure that all ticked articles are a subset of the unread
12555     ;; articles. 
12556     (while m
12557       (if (gnus-member-of-range (car m) ranges)
12558           (setcdr marked (cdr m))
12559         (setq marked m))
12560       (setq m (cdr m)))))
12561
12562 (defun gnus-activate-group (group)
12563   ;; Check whether a group has been activated or not.
12564   (let ((method (gnus-find-method-for-group group))
12565         active)
12566     (and (gnus-check-server method)
12567          ;; We escape all bugs and quits here to make it possible to
12568          ;; continue if a group is so out-there that it reports bugs
12569          ;; and stuff.
12570          (condition-case ()
12571              (gnus-request-group group)
12572            (error nil)
12573            (quit nil))
12574          (save-excursion
12575            (set-buffer nntp-server-buffer)
12576            (goto-char (point-min))
12577            ;; Parse the result we got from `gnus-request-group'.
12578            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12579                 (progn
12580                   (goto-char (match-beginning 1))
12581                   (gnus-sethash 
12582                    group (setq active (cons (read (current-buffer))
12583                                             (read (current-buffer))))
12584                    gnus-active-hashtb))
12585                 ;; Return the new active info.
12586                 active)))))
12587
12588 (defun gnus-update-read-articles 
12589   (group unread unselected ticked &optional domarks replied expirable killed
12590          dormant bookmark score)
12591   "Update the list of read and ticked articles in GROUP using the
12592 UNREAD and TICKED lists.
12593 Note: UNSELECTED has to be sorted over `<'.
12594 Returns whether the updating was successful."
12595   (let* ((active (or gnus-newsgroup-active 
12596                      (gnus-gethash group gnus-active-hashtb)))
12597          (entry (gnus-gethash group gnus-newsrc-hashtb))
12598          (info (nth 2 entry))
12599          (marked (nth 3 info))
12600          (prev 1)
12601          (unread (sort (copy-sequence unread) (function <)))
12602          read)
12603     (if (or (not info) (not active))
12604         ;; There is no info on this group if it was, in fact,
12605         ;; killed. Gnus stores no information on killed groups, so
12606         ;; there's nothing to be done. 
12607         ;; One could store the information somewhere temporarily,
12608         ;; perhaps... Hmmm... 
12609         ()
12610       ;; Remove any negative articles numbers.
12611       (while (and unread (< (car unread) 0))
12612         (setq unread (cdr unread)))
12613       ;; Remove any expired article numbers
12614       (while (and unread (< (car unread) (car active)))
12615         (setq unread (cdr unread)))
12616       (while (and ticked (< (car ticked) (car active)))
12617         (setq ticked (cdr ticked)))
12618       (while (and dormant (< (car dormant) (car active)))
12619         (setq dormant (cdr dormant)))
12620       (setq unread (sort (append unselected unread) '<))
12621       ;; Compute the ranges of read articles by looking at the list of
12622       ;; unread articles.  
12623       (while unread
12624         (if (/= (car unread) prev)
12625             (setq read (cons (if (= prev (1- (car unread))) prev
12626                                (cons prev (1- (car unread)))) read)))
12627         (setq prev (1+ (car unread)))
12628         (setq unread (cdr unread)))
12629       (if (<= prev (cdr active))
12630           (setq read (cons (cons prev (cdr active)) read)))
12631       ;; Enter this list into the group info.
12632       (setcar (cdr (cdr info)) 
12633               (if (> (length read) 1) (nreverse read) read))
12634       ;; Enter the list of ticked articles.
12635       (gnus-set-marked-articles 
12636        info ticked
12637        (if domarks replied (cdr (assq 'reply marked)))
12638        (if domarks expirable (cdr (assq 'expire marked)))
12639        (if domarks killed (cdr (assq 'killed marked)))
12640        (if domarks dormant (cdr (assq 'dormant marked)))
12641        (if domarks bookmark (cdr (assq 'bookmark marked)))
12642        (if domarks score (cdr (assq 'score marked))))
12643       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12644       (gnus-get-unread-articles-in-group 
12645        info (gnus-gethash group gnus-active-hashtb))
12646       t)))
12647
12648 (defun gnus-make-articles-unread (group articles)
12649   "Mark ARTICLES in GROUP as unread."
12650   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12651                           (gnus-gethash (gnus-group-real-name group)
12652                                         gnus-newsrc-hashtb))))
12653          (ranges (nth 2 info))
12654          news)
12655     (while articles
12656       (and (gnus-member-of-range (car articles) ranges)
12657            (setq news (cons (car articles) news)))
12658       (setq articles (cdr articles)))
12659     (if (not news)
12660         ()
12661       (setcar (nthcdr 2 info)
12662               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12663       (gnus-group-update-group group t))))
12664
12665 ;; Enter all dead groups into the hashtb.
12666 (defun gnus-update-active-hashtb-from-killed ()
12667   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12668         (lists (list gnus-killed-list gnus-zombie-list))
12669         killed)
12670     (while lists
12671       (setq killed (car lists))
12672       (while killed
12673         (gnus-sethash (car killed) nil hashtb)
12674         (setq killed (cdr killed)))
12675       (setq lists (cdr lists)))))
12676
12677 ;; Get the active file(s) from the backend(s).
12678 (defun gnus-read-active-file ()
12679   (gnus-group-set-mode-line)
12680   (let ((methods (if (gnus-check-server gnus-select-method)
12681                      ;; The native server is available.
12682                      (cons gnus-select-method gnus-secondary-select-methods)
12683                    ;; The native server is down, so we just do the
12684                    ;; secondary ones.   
12685                    gnus-secondary-select-methods))
12686         list-type)
12687     (setq gnus-have-read-active-file nil)
12688     (save-excursion
12689       (set-buffer nntp-server-buffer)
12690       (while methods
12691         (let* ((method (gnus-server-get-method nil (car methods)))
12692                (where (nth 1 method))
12693                (mesg (format "Reading active file%s via %s..."
12694                              (if (and where (not (zerop (length where))))
12695                                  (concat " from " where) "")
12696                              (car method))))
12697           (gnus-message 5 mesg)
12698           (gnus-check-server method)
12699           (cond 
12700            ((and (eq gnus-read-active-file 'some)
12701                  (gnus-check-backend-function 'retrieve-groups (car method)))
12702             (let ((newsrc (cdr gnus-newsrc-alist))
12703                   (gmethod (gnus-server-get-method nil method))
12704                   groups)
12705               (while newsrc
12706                 (and (gnus-server-equal 
12707                       (gnus-find-method-for-group 
12708                        (car (car newsrc)) (car newsrc))
12709                       gmethod)
12710                      (setq groups (cons (gnus-group-real-name 
12711                                          (car (car newsrc))) groups)))
12712                 (setq newsrc (cdr newsrc)))
12713               (gnus-check-server method)
12714               (setq list-type (gnus-retrieve-groups groups method))
12715               (cond ((not list-type)
12716                      (gnus-message 
12717                       1 "Cannot read partial active file from %s server." 
12718                       (car method))
12719                      (ding)
12720                      (sit-for 2))
12721                     ((eq list-type 'active)
12722                      (gnus-active-to-gnus-format method gnus-active-hashtb))
12723                     (t
12724                      (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12725            (t
12726             (if (not (gnus-request-list method))
12727                 (progn
12728                   (gnus-message 1 "Cannot read active file from %s server." 
12729                                 (car method))
12730                   (ding))
12731               (gnus-active-to-gnus-format method)
12732               ;; We mark this active file as read.
12733               (setq gnus-have-read-active-file
12734                     (cons method gnus-have-read-active-file))
12735               (gnus-message 5 "%sdone" mesg)))))
12736         (setq methods (cdr methods))))))
12737
12738 ;; Read an active file and place the results in `gnus-active-hashtb'.
12739 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12740   (let ((cur (current-buffer))
12741         (hashtb (or hashtb 
12742                     (if (and gnus-active-hashtb 
12743                              (not (equal method gnus-select-method)))
12744                         gnus-active-hashtb
12745                       (setq gnus-active-hashtb
12746                             (if (equal method gnus-select-method)
12747                                 (gnus-make-hashtable 
12748                                  (count-lines (point-min) (point-max)))
12749                               (gnus-make-hashtable 4096))))))
12750         (flag-hashtb (gnus-make-hashtable 60)))
12751     ;; Delete unnecessary lines.
12752     (goto-char (point-min))
12753     (while (search-forward "\nto." nil t)
12754       (delete-region (1+ (match-beginning 0)) 
12755                      (progn (forward-line 1) (point))))
12756     (or (string= gnus-ignored-newsgroups "")
12757         (progn
12758           (goto-char (point-min))
12759           (delete-matching-lines gnus-ignored-newsgroups)))
12760     ;; Make the group names readable as a lisp expression even if they
12761     ;; contain special characters.
12762     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12763     (goto-char (point-max))
12764     (while (re-search-backward "[][';?()#]" nil t)
12765       (insert ?\\))
12766     ;; If these are groups from a foreign select method, we insert the
12767     ;; group prefix in front of the group names. 
12768     (and method (not (gnus-server-equal
12769                       (gnus-server-get-method nil method)
12770                       (gnus-server-get-method nil gnus-select-method)))
12771          (let ((prefix (gnus-group-prefixed-name "" method)))
12772            (goto-char (point-min))
12773            (while (and (not (eobp))
12774                        (progn (insert prefix)
12775                               (zerop (forward-line 1)))))))
12776     ;; Store the active file in a hash table.
12777     (goto-char (point-min))
12778     (if (string-match "%[oO]" gnus-group-line-format)
12779         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12780         ;; If we want information on moderated groups, we use this
12781         ;; loop...   
12782         (let* ((mod-hashtb (make-vector 7 0))
12783                (m (intern "m" mod-hashtb))
12784                group max min)
12785           (while (not (eobp))
12786             (condition-case nil
12787                 (progn
12788                   (narrow-to-region (point) (gnus-point-at-eol))
12789                   (setq group (let ((obarray hashtb)) (read cur)))
12790                   (if (and (numberp (setq max (read cur)))
12791                            (numberp (setq min (read cur)))
12792                            (progn 
12793                              (skip-chars-forward " \t")
12794                              (not
12795                               (or (= (following-char) ?=)
12796                                   (= (following-char) ?x)
12797                                   (= (following-char) ?j)))))
12798                       (set group (cons min max))
12799                     (set group nil))
12800                   ;; Enter moderated groups into a list.
12801                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12802                       (setq gnus-moderated-list 
12803                             (cons (symbol-name group) gnus-moderated-list))))
12804               (error 
12805                (and (symbolp group)
12806                     (set group nil))))
12807             (widen)
12808             (forward-line 1)))
12809       ;; And if we do not care about moderation, we use this loop,
12810       ;; which is faster.
12811       (let (group max min)
12812         (while (not (eobp))
12813           (condition-case ()
12814               (progn
12815                 (narrow-to-region (point) (gnus-point-at-eol))
12816                 ;; group gets set to a symbol interned in the hash table
12817                 ;; (what a hack!!) - jwz
12818                 (setq group (let ((obarray hashtb)) (read cur)))
12819                 (if (and (numberp (setq max (read cur)))
12820                          (numberp (setq min (read cur)))
12821                          (progn 
12822                            (skip-chars-forward " \t")
12823                            (not
12824                             (or (= (following-char) ?=)
12825                                 (= (following-char) ?x)
12826                                 (= (following-char) ?j)))))
12827                     (set group (cons min max))
12828                   (set group nil)))
12829             (error 
12830              (progn 
12831                (and (symbolp group)
12832                     (set group nil))
12833                (if ignore-errors
12834                    ()
12835                  (gnus-message 3 "Warning - illegal active: %s"
12836                                (buffer-substring 
12837                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12838                  nil))))
12839           (widen)
12840           (forward-line 1))))))
12841
12842 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12843   ;; Parse a "groups" active file.
12844   (let ((cur (current-buffer))
12845         (hashtb (or hashtb 
12846                     (if (and method gnus-active-hashtb)
12847                         gnus-active-hashtb
12848                       (setq gnus-active-hashtb
12849                             (gnus-make-hashtable 
12850                              (count-lines (point-min) (point-max)))))))
12851         (prefix (and method (not (eq method gnus-select-method))
12852                      (gnus-group-prefixed-name "" method))))
12853
12854     (goto-char (point-min))
12855     ;; We split this into to separate loops, one with the prefix
12856     ;; and one without to speed the reading up somewhat.
12857     (if prefix
12858         (let (min max opoint group)
12859           (while (not (eobp))
12860             (condition-case ()
12861                 (progn
12862                   (read cur) (read cur)
12863                   (setq min (read cur)
12864                         max (read cur)
12865                         opoint (point))
12866                   (skip-chars-forward " \t")
12867                   (insert prefix)
12868                   (goto-char opoint)
12869                   (set (let ((obarray hashtb)) (read cur)) 
12870                        (cons min max)))
12871               (error (if group (set group nil))))
12872             (forward-line 1)))
12873       (let (min max group)
12874         (while (not (eobp))
12875           (condition-case ()
12876               (if (= (following-char) ?2)
12877                   (progn
12878                     (read cur) (read cur)
12879                     (setq min (read cur)
12880                           max (read cur))
12881                     (set (setq group (let ((obarray hashtb)) (read cur)))
12882                          (cons min max))))
12883             (error (if group (set group nil))))
12884           (forward-line 1))))))
12885
12886 (defun gnus-read-newsrc-file (&optional force)
12887   "Read startup file.
12888 If FORCE is non-nil, the .newsrc file is read."
12889   ;; Reset variables that might be defined in the .newsrc.eld file.
12890   (let ((variables gnus-variable-list))
12891     (while variables
12892       (set (car variables) nil)
12893       (setq variables (cdr variables))))
12894   (let* ((newsrc-file gnus-current-startup-file)
12895          (quick-file (concat newsrc-file ".el")))
12896     (save-excursion
12897       ;; We always load the .newsrc.eld file. If always contains
12898       ;; much information that can not be gotten from the .newsrc
12899       ;; file (ticked articles, killed groups, foreign methods, etc.)
12900       (gnus-read-newsrc-el-file quick-file)
12901  
12902       (if (or force
12903               (and (file-newer-than-file-p newsrc-file quick-file)
12904                    (file-newer-than-file-p newsrc-file 
12905                                            (concat quick-file "d")))
12906               (not gnus-newsrc-alist))
12907           ;; We read the .newsrc file. Note that if there if a
12908           ;; .newsrc.eld file exists, it has already been read, and
12909           ;; the `gnus-newsrc-hashtb' has been created. While reading
12910           ;; the .newsrc file, Gnus will only use the information it
12911           ;; can find there for changing the data already read -
12912           ;; ie. reading the .newsrc file will not trash the data
12913           ;; already read (except for read articles).
12914           (save-excursion
12915             (gnus-message 5 "Reading %s..." newsrc-file)
12916             (set-buffer (find-file-noselect newsrc-file))
12917             (buffer-disable-undo (current-buffer))
12918             (gnus-newsrc-to-gnus-format)
12919             (kill-buffer (current-buffer))
12920             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12921
12922 (defun gnus-read-newsrc-el-file (file)
12923   (let ((ding-file (concat file "d")))
12924     ;; We always, always read the .eld file.
12925     (gnus-message 5 "Reading %s..." ding-file)
12926     (let (gnus-newsrc-assoc)
12927       (condition-case nil
12928           (load ding-file t t t)
12929         (error nil))
12930       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12931     (let ((inhibit-quit t))
12932       (gnus-uncompress-newsrc-alist))
12933     (gnus-make-hashtable-from-newsrc-alist)
12934     (if (not (file-newer-than-file-p file ding-file))
12935         ()
12936       ;; Old format quick file
12937       (gnus-message 5 "Reading %s..." file)
12938       ;; The .el file is newer than the .eld file, so we read that one
12939       ;; as well. 
12940       (gnus-read-old-newsrc-el-file file))))
12941
12942 ;; Parse the old-style quick startup file
12943 (defun gnus-read-old-newsrc-el-file (file)
12944   (let (newsrc killed marked group m)
12945     (prog1
12946         (let ((gnus-killed-assoc nil)
12947               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12948           (prog1
12949               (condition-case nil
12950                   (load file t t t)
12951                 (error nil))
12952             (setq newsrc gnus-newsrc-assoc
12953                   killed gnus-killed-assoc
12954                   marked gnus-marked-assoc)))
12955       (setq gnus-newsrc-alist nil)
12956       (while newsrc
12957         (setq group (car newsrc))
12958         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12959           (if info
12960               (progn
12961                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12962                 (setcar (cdr info)
12963                         (if (nth 1 group) gnus-level-default-subscribed 
12964                           gnus-level-default-unsubscribed))
12965                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12966             (setq gnus-newsrc-alist
12967                   (cons 
12968                    (setq info
12969                          (list (car group)
12970                                (if (nth 1 group) gnus-level-default-subscribed
12971                                  gnus-level-default-unsubscribed) 
12972                                (cdr (cdr group))))
12973                    gnus-newsrc-alist)))
12974           (if (setq m (assoc (car group) marked))
12975             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12976         (setq newsrc (cdr newsrc)))
12977       (setq newsrc killed)
12978       (while newsrc
12979         (setcar newsrc (car (car newsrc)))
12980         (setq newsrc (cdr newsrc)))
12981       (setq gnus-killed-list killed))
12982     ;; The .el file version of this variable does not begin with
12983     ;; "options", while the .eld version does, so we just add it if it
12984     ;; isn't there.
12985     (and
12986      gnus-newsrc-options 
12987      (progn
12988        (and (not (string-match "^ *options" gnus-newsrc-options))
12989             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12990        (and (not (string-match "\n$" gnus-newsrc-options))
12991             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12992     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12993     (gnus-make-hashtable-from-newsrc-alist)))
12994       
12995 (defun gnus-make-newsrc-file (file)
12996   "Make server dependent file name by catenating FILE and server host name."
12997   (let* ((file (expand-file-name file nil))
12998          (real-file (concat file "-" (nth 1 gnus-select-method))))
12999     (if (or (file-exists-p real-file)
13000             (file-exists-p (concat real-file ".el"))
13001             (file-exists-p (concat real-file ".eld")))
13002         real-file file)))
13003
13004 (defun gnus-uncompress-newsrc-alist ()
13005   ;; Uncompress all lists of marked articles in the newsrc assoc.
13006   (let ((newsrc gnus-newsrc-alist)
13007         marked)
13008     (while newsrc
13009       (if (not (setq marked (nth 3 (car newsrc))))
13010           ()
13011         (while marked
13012           (or (eq 'score (car (car marked)))
13013               (eq 'bookmark (car (car marked)))
13014               (eq 'killed (car (car marked)))
13015               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13016           (setq marked (cdr marked))))
13017       (setq newsrc (cdr newsrc)))))
13018
13019 (defun gnus-compress-newsrc-alist ()
13020   ;; Compress all lists of marked articles in the newsrc assoc.
13021   (let ((newsrc gnus-newsrc-alist)
13022         marked)
13023     (while newsrc
13024       (if (not (setq marked (nth 3 (car newsrc))))
13025           ()
13026         (while marked
13027           (or (eq 'score (car (car marked)))
13028               (eq 'bookmark (car (car marked)))
13029               (eq 'killed (car (car marked)))
13030               (setcdr (car marked) 
13031                       (condition-case ()
13032                           (gnus-compress-sequence 
13033                            (sort (cdr (car marked)) '<) t)
13034                         (error (cdr (car marked))))))
13035           (setq marked (cdr marked))))
13036       (setq newsrc (cdr newsrc)))))
13037
13038 (defun gnus-newsrc-to-gnus-format ()
13039   (setq gnus-newsrc-options "")
13040   (setq gnus-newsrc-options-n nil)
13041
13042   (or gnus-active-hashtb
13043       (setq gnus-active-hashtb (make-vector 4095 0)))
13044   (let ((buf (current-buffer))
13045         (already-read (> (length gnus-newsrc-alist) 1))
13046         group subscribed options-symbol newsrc Options-symbol
13047         symbol reads num1)
13048     (goto-char (point-min))
13049     ;; We intern the symbol `options' in the active hashtb so that we
13050     ;; can `eq' against it later.
13051     (setq options-symbol (intern "options" gnus-active-hashtb))
13052     (setq Options-symbol (intern "Options" gnus-active-hashtb))
13053   
13054     (while (not (eobp))
13055       ;; We first read the first word on the line by narrowing and
13056       ;; then reading into `gnus-active-hashtb'.  Most groups will
13057       ;; already exist in that hashtb, so this will save some string
13058       ;; space.
13059       (narrow-to-region
13060        (point)
13061        (progn (skip-chars-forward "^ \t!:\n") (point)))
13062       (goto-char (point-min))
13063       (setq symbol 
13064             (and (/= (point-min) (point-max))
13065                  (let ((obarray gnus-active-hashtb)) (read buf))))
13066       (widen)
13067       ;; Now, the symbol we have read is either `options' or a group
13068       ;; name.  If it is an options line, we just add it to a string. 
13069       (cond 
13070        ((or (eq symbol options-symbol)
13071             (eq symbol Options-symbol))
13072         (setq gnus-newsrc-options
13073               ;; This concatting is quite inefficient, but since our
13074               ;; thorough studies show that approx 99.37% of all
13075               ;; .newsrc files only contain a single options line, we
13076               ;; don't give a damn, frankly, my dear.
13077               (concat gnus-newsrc-options
13078                       (buffer-substring 
13079                        (gnus-point-at-bol)
13080                        ;; Options may continue on the next line.
13081                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13082                                 (progn (beginning-of-line) (point)))
13083                            (point))))))
13084        (symbol
13085         ;; It was a group name.
13086         (setq subscribed (= (following-char) ?:)
13087               group (symbol-name symbol)
13088               reads nil)
13089         (if (eolp)
13090             ;; If the line ends here, this is clearly a buggy line, so
13091             ;; we put point a the beginning of line and let the cond
13092             ;; below do the error handling.
13093             (beginning-of-line)
13094           ;; We skip to the beginning of the ranges.
13095           (skip-chars-forward "!: \t"))
13096         ;; We are now at the beginning of the list of read articles.
13097         ;; We read them range by range.
13098         (while
13099             (cond 
13100              ((looking-at "[0-9]+")
13101               ;; We narrow and read a number instead of buffer-substring/
13102               ;; string-to-int because it's faster. narrow/widen is
13103               ;; faster than save-restriction/narrow, and save-restriction
13104               ;; produces a garbage object.
13105               (setq num1 (progn
13106                            (narrow-to-region (match-beginning 0) (match-end 0))
13107                            (read buf)))
13108               (widen)
13109               ;; If the next character is a dash, then this is a range.
13110               (if (= (following-char) ?-)
13111                   (progn
13112                     ;; We read the upper bound of the range.
13113                     (forward-char 1)
13114                     (if (not (looking-at "[0-9]+"))
13115                         ;; This is a buggy line, by we pretend that
13116                         ;; it's kinda OK. Perhaps the user should be
13117                         ;; dinged? 
13118                         (setq reads (cons num1 reads))
13119                       (setq reads 
13120                             (cons 
13121                              (cons num1 (progn
13122                                           (narrow-to-region (match-beginning 0) 
13123                                                             (match-end 0))
13124                                           (read buf)))
13125                              reads))
13126                       (widen)))
13127                 ;; It was just a simple number, so we add it to the
13128                 ;; list of ranges.
13129                 (setq reads (cons num1 reads)))
13130               ;; If the next char in ?\n, then we have reached the end
13131               ;; of the line and return nil.
13132               (/= (following-char) ?\n))
13133              ((= (following-char) ?\n)
13134               ;; End of line, so we end.
13135               nil)
13136              (t
13137               ;; Not numbers and not eol, so this might be a buggy
13138               ;; line... 
13139               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13140                   (progn
13141                     ;; The line was buggy.
13142                     (setq group nil)
13143                     (gnus-message 3 "Mangled line: %s" 
13144                                   (buffer-substring (gnus-point-at-bol) 
13145                                                     (gnus-point-at-eol)))
13146                     (ding)
13147                     (sit-for 1)))
13148               nil))
13149           ;; Skip past ", ". Spaces are illegal in these ranges, but
13150           ;; we allow them, because it's a common mistake to put a
13151           ;; space after the comma.
13152           (skip-chars-forward ", "))
13153
13154         ;; We have already read .newsrc.eld, so we gently update the
13155         ;; data in the hash table with the information we have just
13156         ;; read. 
13157         (if (not group)
13158             ()
13159           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13160                 level)
13161             (if info
13162                 ;; There is an entry for this file in the alist.
13163                 (progn
13164                   (setcar (nthcdr 2 info) (nreverse reads))
13165                   ;; We update the level very gently.  In fact, we
13166                   ;; only change it if there's been a status change
13167                   ;; from subscribed to unsubscribed, or vice versa.
13168                   (setq level (nth 1 info))
13169                   (cond ((and (<= level gnus-level-subscribed)
13170                               (not subscribed))
13171                          (setq level (if reads
13172                                          gnus-level-default-unsubscribed 
13173                                        (1+ gnus-level-default-unsubscribed))))
13174                         ((and (> level gnus-level-subscribed) subscribed)
13175                          (setq level gnus-level-default-subscribed)))
13176                   (setcar (cdr info) level))
13177               ;; This is a new group.
13178               (setq info (list group 
13179                                (if subscribed
13180                                    gnus-level-default-subscribed 
13181                                  (if reads
13182                                      (1+ gnus-level-subscribed)
13183                                    gnus-level-default-unsubscribed))
13184                                (nreverse reads))))
13185             (setq newsrc (cons info newsrc))))))
13186       (forward-line 1))
13187     
13188     (setq newsrc (nreverse newsrc))
13189
13190     (if (not already-read)
13191         ()
13192       ;; We now have two newsrc lists - `newsrc', which is what we
13193       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13194       ;; what we've read from .newsrc.eld. We have to merge these
13195       ;; lists. We do this by "attaching" any (foreign) groups in the
13196       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13197       (let ((rc (cdr gnus-newsrc-alist))
13198             (prev gnus-newsrc-alist)
13199             entry mentry)
13200         (while rc
13201           (or (null (nth 4 (car rc))) ; It's a native group.
13202               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13203               (if (setq entry (assoc (car (car prev)) newsrc))
13204                   (setcdr (setq mentry (memq entry newsrc))
13205                           (cons (car rc) (cdr mentry)))
13206                 (setq newsrc (cons (car rc) newsrc))))
13207           (setq prev rc
13208                 rc (cdr rc)))))
13209
13210     (setq gnus-newsrc-alist newsrc)
13211     ;; We make the newsrc hashtb.
13212     (gnus-make-hashtable-from-newsrc-alist)
13213
13214     ;; Finally, if we read some options lines, we parse them.
13215     (or (string= gnus-newsrc-options "")
13216         (gnus-newsrc-parse-options gnus-newsrc-options))))
13217
13218 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13219 ;; The return value will be a list on the form
13220 ;; ((regexp1 . ignore)
13221 ;;  (regexp2 . subscribe)...)
13222 ;; When handling new newsgroups, groups that match a `ignore' regexp
13223 ;; will be ignored, and groups that match a `subscribe' regexp will be
13224 ;; subscribed. A line like
13225 ;; options -n !all rec.all
13226 ;; will lead to a list that looks like
13227 ;; (("^rec\\..+" . subscribe) 
13228 ;;  ("^.+" . ignore))
13229 ;; So all "rec.*" groups will be subscribed, while all the other
13230 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13231 ;; different from "options -n rec.all !all". 
13232 (defun gnus-newsrc-parse-options (options)
13233   (let (out eol)
13234     (save-excursion
13235       (gnus-set-work-buffer)
13236       (insert (regexp-quote options))
13237       ;; First we treat all continuation lines.
13238       (goto-char (point-min))
13239       (while (re-search-forward "\n[ \t]+" nil t)
13240         (replace-match " " t t))
13241       ;; Then we transform all "all"s into ".+"s.
13242       (goto-char (point-min))
13243       (while (re-search-forward "\\ball\\b" nil t)
13244         (replace-match ".+" t t))
13245       (goto-char (point-min))
13246       ;; We remove all other options than the "-n" ones.
13247       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13248         (replace-match " ")
13249         (forward-char -1))
13250       (goto-char (point-min))
13251
13252       ;; We are only interested in "options -n" lines - we
13253       ;; ignore the other option lines.
13254       (while (re-search-forward "[ \t]-n" nil t)
13255         (setq eol 
13256               (or (save-excursion
13257                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13258                          (- (point) 2)))
13259                   (gnus-point-at-eol)))
13260         ;; Search for all "words"...
13261         (while (re-search-forward "[^ \t,\n]+" eol t)
13262           (if (= (char-after (match-beginning 0)) ?!)
13263               ;; If the word begins with a bang (!), this is a "not"
13264               ;; spec. We put this spec (minus the bang) and the
13265               ;; symbol `ignore' into the list.
13266               (setq out (cons (cons (concat 
13267                                      "^" (buffer-substring 
13268                                           (1+ (match-beginning 0))
13269                                           (match-end 0)))
13270                                     'ignore) out))
13271             ;; There was no bang, so this is a "yes" spec.
13272             (setq out (cons (cons (concat 
13273                                    "^" (buffer-substring (match-beginning 0)
13274                                                          (match-end 0)))
13275                                   'subscribe) out)))))
13276     
13277       (setq gnus-newsrc-options-n out))))
13278                
13279
13280 (defun gnus-save-newsrc-file ()
13281   "Save .newsrc file."
13282   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13283   ;; from the variable gnus-newsrc-alist.
13284   (and (or gnus-newsrc-alist gnus-killed-list)
13285        gnus-current-startup-file
13286        (progn
13287          (run-hooks 'gnus-save-newsrc-hook)
13288          (save-excursion
13289            (if (and gnus-use-dribble-file
13290                     (or (not gnus-dribble-buffer)
13291                         (not (buffer-name gnus-dribble-buffer))
13292                         (zerop (save-excursion
13293                                  (set-buffer gnus-dribble-buffer)
13294                                  (buffer-size)))))
13295                (gnus-message 4 "(No changes need to be saved)")
13296              (if gnus-save-newsrc-file
13297                  (progn
13298                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13299                    ;; Make backup file of master newsrc.
13300                    (gnus-gnus-to-newsrc-format)
13301                    (gnus-message 5 "Saving %s...done"
13302                                  gnus-current-startup-file)))
13303              ;; Quickly loadable .newsrc.
13304              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13305              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13306              (gnus-add-current-to-buffer-list)
13307              (buffer-disable-undo (current-buffer))
13308              (erase-buffer)
13309              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13310              (gnus-gnus-to-quick-newsrc-format)
13311              (save-buffer)
13312              (kill-buffer (current-buffer))
13313              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13314              (gnus-dribble-delete-file))))))
13315
13316 (defun gnus-gnus-to-quick-newsrc-format ()
13317   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13318   (insert ";; (ding) Gnus startup file.\n")
13319   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13320   (insert ";; to read .newsrc.\n")
13321   (insert "(setq gnus-newsrc-file-version "
13322           (prin1-to-string gnus-version) ")\n")
13323   (let ((variables gnus-variable-list)
13324         (inhibit-quit t)
13325         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13326         variable)
13327     ;; insert lisp expressions.
13328     (gnus-compress-newsrc-alist)
13329     (while variables
13330       (setq variable (car variables))
13331       (and (boundp variable)
13332            (symbol-value variable)
13333            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13334            (insert "(setq " (symbol-name variable) " '"
13335                    (prin1-to-string (symbol-value variable))
13336                    ")\n"))
13337       (setq variables (cdr variables)))
13338     (gnus-uncompress-newsrc-alist)))
13339
13340
13341 (defun gnus-gnus-to-newsrc-format ()
13342   ;; Generate and save the .newsrc file.
13343   (let ((newsrc (cdr gnus-newsrc-alist))
13344         info ranges range)
13345     (save-excursion
13346       (set-buffer (create-file-buffer gnus-current-startup-file))
13347       (set-visited-file-name gnus-current-startup-file)
13348       (buffer-disable-undo (current-buffer))
13349       (erase-buffer)
13350       ;; Write options.
13351       (if gnus-newsrc-options (insert gnus-newsrc-options))
13352       ;; Write subscribed and unsubscribed.
13353       (while newsrc
13354         (setq info (car newsrc))
13355         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13356             (progn
13357               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13358                                      "!" ":"))
13359               (if (setq ranges (nth 2 info))
13360                   (progn
13361                     (insert " ")
13362                     (if (not (listp (cdr ranges)))
13363                         (if (= (car ranges) (cdr ranges))
13364                             (insert (int-to-string (car ranges)))
13365                           (insert (int-to-string (car ranges)) "-" 
13366                                   (int-to-string (cdr ranges))))
13367                       (while ranges
13368                         (setq range (car ranges)
13369                               ranges (cdr ranges))
13370                         (if (or (atom range) (= (car range) (cdr range)))
13371                             (insert (int-to-string 
13372                                      (or (and (atom range) range) 
13373                                          (car range))))
13374                           (insert (int-to-string (car range)) "-"
13375                                   (int-to-string (cdr range))))
13376                         (if ranges (insert ","))))))
13377               (insert "\n")))
13378         (setq newsrc (cdr newsrc)))
13379       (save-buffer)
13380       (kill-buffer (current-buffer)))))
13381
13382 (defun gnus-read-all-descriptions-files ()
13383   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13384     (while methods
13385       (gnus-read-descriptions-file (car methods))
13386       (setq methods (cdr methods)))
13387     t))
13388
13389 (defun gnus-read-descriptions-file (&optional method)
13390   (let ((method (or method gnus-select-method)))
13391     ;; We create the hashtable whether we manage to read the desc file
13392     ;; to avoid trying to re-read after a failed read.
13393     (or gnus-description-hashtb
13394         (setq gnus-description-hashtb 
13395               (gnus-make-hashtable (length gnus-active-hashtb))))
13396     ;; Mark this method's desc file as read.
13397     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13398                   gnus-description-hashtb)
13399
13400     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13401     (cond 
13402      ((not (gnus-check-server method))
13403       (gnus-message 1 "Couldn't open server")
13404       nil)
13405      ((not (gnus-request-list-newsgroups method))
13406       (gnus-message 1 "Couldn't read newsgroups descriptions")
13407       nil)
13408      (t
13409       (let (group)
13410         (save-excursion
13411           (save-restriction
13412             (set-buffer nntp-server-buffer)
13413             (goto-char (point-min))
13414             (if (or (search-forward "\n.\n" nil t)
13415                     (goto-char (point-max)))
13416                 (progn
13417                   (beginning-of-line)
13418                   (narrow-to-region (point-min) (point))))
13419             (goto-char (point-min))
13420             (while (not (eobp))
13421               ;; If we get an error, we set group to 0, which is not a
13422               ;; symbol... 
13423               (setq group 
13424                     (condition-case ()
13425                         (let ((obarray gnus-description-hashtb))
13426                           ;; Group is set to a symbol interned in this
13427                           ;; hash table.
13428                           (read nntp-server-buffer))
13429                       (error 0)))
13430               (skip-chars-forward " \t")
13431               ;; ... which leads to this line being effectively ignored.
13432               (and (symbolp group)
13433                    (set group (buffer-substring 
13434                                (point) (progn (end-of-line) (point)))))
13435               (forward-line 1))))
13436         (gnus-message 5 "Reading descriptions file...done")
13437         t)))))
13438
13439 (defun gnus-group-get-description (group)
13440   ;; Get the description of a group by sending XGTITLE to the server.
13441   (and (gnus-request-group-description group)
13442        (save-excursion
13443          (set-buffer nntp-server-buffer)
13444          (goto-char (point-min))
13445          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13446               (buffer-substring (match-beginning 1) (match-end 1))))))
13447
13448 ;;;
13449 ;;; Server
13450 ;;;
13451
13452 (defvar gnus-server-mode-hook nil
13453   "Hook run in `gnus-server-mode' buffers.")
13454
13455 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13456   "Format of server lines.
13457 It works along the same lines as a normal formatting string,
13458 with some simple extensions.")
13459
13460 (defvar gnus-server-mode-line-format "(ding) List of servers"
13461   "The format specification for the server mode line.")
13462
13463 (defconst gnus-server-line-format-alist
13464   (list (list ?h 'how ?s)
13465         (list ?n 'name ?s)
13466         (list ?w 'where ?s)
13467         ))
13468
13469 (defconst gnus-server-mode-line-format-alist 
13470   (list (list ?S 'news-server ?s)
13471         (list ?M 'news-method ?s)
13472         (list ?u 'user-defined ?s)))
13473
13474 (defvar gnus-server-line-format-spec nil)
13475 (defvar gnus-server-mode-line-format-spec nil)
13476 (defvar gnus-server-killed-servers nil)
13477
13478 (defvar gnus-server-mode-map nil)
13479 (put 'gnus-server-mode 'mode-class 'special)
13480
13481 (if gnus-server-mode-map
13482     nil
13483   (setq gnus-server-mode-map (make-sparse-keymap))
13484   (suppress-keymap gnus-server-mode-map)
13485   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13486   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13487   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13488   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13489   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13490   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13491   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13492   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13493   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13494   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13495
13496 (defun gnus-server-mode ()
13497   "Major mode for listing and editing servers.
13498
13499 All normal editing commands are switched off.
13500 \\<gnus-server-mode-map>
13501
13502 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13503
13504 The following commands are available:
13505
13506 \\{gnus-server-mode-map}"
13507   (interactive)
13508   (if gnus-visual (gnus-server-make-menu-bar))
13509   (kill-all-local-variables)
13510   (setq mode-line-modified "-- ")
13511   (make-local-variable 'mode-line-format)
13512   (setq mode-line-format (copy-sequence mode-line-format))
13513   (and (equal (nth 3 mode-line-format) "   ")
13514        (setcar (nthcdr 3 mode-line-format) ""))
13515   (setq major-mode 'gnus-server-mode)
13516   (setq mode-name "Server")
13517 ;  (gnus-group-set-mode-line)
13518   (setq mode-line-process nil)
13519   (use-local-map gnus-server-mode-map)
13520   (buffer-disable-undo (current-buffer))
13521   (setq truncate-lines t)
13522   (setq buffer-read-only t)
13523   (run-hooks 'gnus-server-mode-hook))
13524
13525 (defun gnus-server-insert-server-line (sformat name method)
13526   (let* ((sformat (or sformat gnus-server-line-format-spec))
13527          (how (car method))
13528          (where (nth 1 method))
13529          b)
13530     (beginning-of-line)
13531     (setq b (point))
13532     ;; Insert the text.
13533     (insert (eval sformat))
13534     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13535
13536 (defun gnus-server-setup-buffer ()
13537   (if (get-buffer gnus-server-buffer)
13538       ()
13539     (save-excursion
13540       (set-buffer (get-buffer-create gnus-server-buffer))
13541       (gnus-server-mode)
13542       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13543
13544 (defun gnus-server-prepare ()
13545   (setq gnus-server-mode-line-format-spec 
13546         (gnus-parse-format gnus-server-mode-line-format 
13547                            gnus-server-mode-line-format-alist))
13548   (setq gnus-server-line-format-spec 
13549         (gnus-parse-format gnus-server-line-format 
13550                            gnus-server-line-format-alist))
13551   (let ((alist gnus-server-alist)
13552         (buffer-read-only nil))
13553     (erase-buffer)
13554     (while alist
13555       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13556       (setq alist (cdr alist))))
13557   (goto-char (point-min))
13558   (gnus-server-position-cursor))
13559
13560 (defun gnus-server-server-name ()
13561   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13562     (and server (symbol-name server))))
13563
13564 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13565
13566 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13567
13568 (defun gnus-server-update-server (server)
13569   (save-excursion
13570     (set-buffer gnus-server-buffer)
13571     (let ((buffer-read-only nil)
13572           (info (cdr (assoc server gnus-server-alist))))
13573       (gnus-dribble-enter 
13574        (concat "(gnus-server-set-info \"" server "\" '"
13575                (prin1-to-string info) ")"))
13576       ;; Buffer may be narrowed.
13577       (save-restriction
13578         (widen)
13579         (if (gnus-server-goto-server server)
13580             (delete-region (progn (beginning-of-line) (point))
13581                            (progn (forward-line 1) (point))))
13582         (let ((entry (assoc server gnus-server-alist)))
13583           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13584           (gnus-server-position-cursor))))))
13585
13586 (defun gnus-server-set-info (server info)
13587   ;; Enter a select method into the virtual server alist.
13588   (gnus-dribble-enter 
13589    (concat "(gnus-server-set-info \"" server "\" '"
13590            (prin1-to-string info) ")"))
13591   (let* ((server (nth 1 info))
13592          (entry (assoc server gnus-server-alist)))
13593     (if entry (setcdr entry info)
13594       (setq gnus-server-alist
13595             (nconc gnus-server-alist (list (cons server info)))))))
13596
13597 (defun gnus-server-to-method (server)
13598   ;; Map virtual server names to select methods.
13599   (or (and (equal server "native") gnus-select-method)
13600       (cdr (assoc server gnus-server-alist))))
13601
13602 (defun gnus-server-extend-method (group method)
13603   ;; This function "extends" a virtual server.  If the server is
13604   ;; "hello", and the select method is ("hello" (my-var "something")) 
13605   ;; in the group "alt.alt", this will result in a new virtual server
13606   ;; called "helly+alt.alt".
13607   (let ((entry
13608          (gnus-copy-sequence 
13609           (if (equal (car method) "native") gnus-select-method
13610               (cdr (assoc (car method) gnus-server-alist))))))
13611     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13612     (nconc entry (cdr method))))
13613
13614 (defun gnus-server-get-method (group method)
13615   ;; Input either a server name, and extended server name, or a
13616   ;; select method, and return a select method. 
13617   (cond ((stringp method)
13618          (gnus-server-to-method method))
13619         ((and (stringp (car method)) group)
13620          (gnus-server-extend-method group method))
13621         (t
13622          (gnus-server-add-address method))))
13623
13624 (defun gnus-server-add-address (method)
13625   (let ((method-name (symbol-name (car method))))
13626     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13627              (not (assq (intern (concat method-name "-address")) method)))
13628         (append method (list (list (intern (concat method-name "-address"))
13629                                    (nth 1 method))))
13630       method)))
13631
13632 (defun gnus-server-equal (s1 s2)
13633   (or (equal s1 s2)
13634       (and (= (length s1) (length s2))
13635            (progn
13636              (while (and s1 (member (car s1) s2))
13637                (setq s1 (cdr s1)))
13638              (null s1)))))
13639
13640 ;;; Interactive server functions.
13641
13642 (defun gnus-server-kill-server (server)
13643   "Kill the server on the current line."
13644   (interactive (list (gnus-server-server-name)))
13645   (or (gnus-server-goto-server server)
13646       (if server (error "No such server: %s" server)
13647         (error "No server on the current line")))
13648   (let ((buffer-read-only nil))
13649     (delete-region (progn (beginning-of-line) (point))
13650                    (progn (forward-line 1) (point))))
13651   (setq gnus-server-killed-servers 
13652         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13653   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13654                                 gnus-server-alist))
13655   (gnus-server-position-cursor))
13656
13657 (defun gnus-server-yank-server ()
13658   "Yank the previously killed server."
13659   (interactive)
13660   (or gnus-server-killed-servers
13661       (error "No killed servers to be yanked"))
13662   (let ((alist gnus-server-alist)
13663         (server (gnus-server-server-name))
13664         (killed (car gnus-server-killed-servers)))
13665     (if (not server) 
13666         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13667       (if (string= server (car (car gnus-server-alist)))
13668           (setq gnus-server-alist (cons killed gnus-server-alist))
13669         (while (and (cdr alist)
13670                     (not (string= server (car (car (cdr alist))))))
13671           (setq alist (cdr alist)))
13672         (setcdr alist (cons killed (cdr alist)))))
13673     (gnus-server-update-server (car killed))
13674     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13675     (gnus-server-position-cursor)))
13676
13677 (defun gnus-server-exit ()
13678   "Return to the group buffer."
13679   (interactive)
13680   (kill-buffer (current-buffer))
13681   (switch-to-buffer gnus-group-buffer))
13682
13683 (defun gnus-server-list-servers ()
13684   "List all available servers."
13685   (interactive)
13686   (let ((cur (gnus-server-server-name)))
13687     (gnus-server-prepare)
13688     (if cur (gnus-server-goto-server cur)
13689       (goto-char (point-max))
13690       (forward-line -1))
13691     (gnus-server-position-cursor)))
13692
13693 (defun gnus-server-copy-server (from to)
13694   (interactive
13695    (list
13696     (or (gnus-server-server-name)
13697         (error "No server on the current line"))
13698     (read-string "Copy to: ")))
13699   (or from (error "No server on current line"))
13700   (or (and to (not (string= to ""))) (error "No name to copy to"))
13701   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13702   (or (assoc from gnus-server-alist) 
13703       (error "%s: no such server" from))
13704   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13705     (setcar to-entry to)
13706     (setcar (nthcdr 2 to-entry) to)
13707     (setq gnus-server-killed-servers 
13708           (cons to-entry gnus-server-killed-servers))
13709     (gnus-server-yank-server)))
13710
13711 (defun gnus-server-add-server (how where)
13712   (interactive 
13713    (list (intern (completing-read "Server method: "
13714                                   gnus-valid-select-methods nil t))
13715          (read-string "Server name: ")))
13716   (setq gnus-server-killed-servers 
13717         (cons (list where how where) gnus-server-killed-servers))
13718   (gnus-server-yank-server))
13719
13720 (defun gnus-server-goto-server (server)
13721   "Jump to a server line."
13722   (interactive
13723    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13724   (let ((to (text-property-any (point-min) (point-max) 
13725                                'gnus-server (intern server))))
13726     (and to
13727          (progn
13728            (goto-char to) 
13729            (gnus-server-position-cursor)))))
13730
13731 (defun gnus-server-edit-server (server)
13732   "Edit the server on the current line."
13733   (interactive (list (gnus-server-server-name)))
13734   (or server
13735       (error "No server on current line"))
13736   (let ((winconf (current-window-configuration)))
13737     (get-buffer-create gnus-server-edit-buffer)
13738     (gnus-configure-windows 'edit-server)
13739     (gnus-add-current-to-buffer-list)
13740     (emacs-lisp-mode)
13741     (make-local-variable 'gnus-prev-winconf)
13742     (setq gnus-prev-winconf winconf)
13743     (use-local-map (copy-keymap (current-local-map)))
13744     (let ((done-func '(lambda () 
13745                         "Exit editing mode and update the information."
13746                         (interactive)
13747                         (gnus-server-edit-server-done 'group))))
13748       (setcar (cdr (nth 4 done-func)) server)
13749       (local-set-key "\C-c\C-c" done-func))
13750     (erase-buffer)
13751     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13752     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13753
13754 (defun gnus-server-edit-server-done (server)
13755   (interactive)
13756   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13757   (goto-char (point-min))
13758   (let ((form (read (current-buffer)))
13759         (winconf gnus-prev-winconf))
13760     (gnus-server-set-info server form)
13761     (kill-buffer (current-buffer))
13762     (and winconf (set-window-configuration winconf))
13763     (set-buffer gnus-server-buffer)
13764     (gnus-server-update-server (gnus-server-server-name))
13765     (gnus-server-position-cursor)))
13766
13767 (defun gnus-server-read-server (server)
13768   "Browse a server."
13769   (interactive (list (gnus-server-server-name)))
13770   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13771
13772 (defun gnus-mouse-pick-server (e)
13773   (interactive "e")
13774   (mouse-set-point e)
13775   (gnus-server-read-server (gnus-server-server-name)))
13776
13777 ;;;
13778 ;;; entry points into gnus-score.el
13779 ;;;
13780
13781 ;;; Finding score files. 
13782
13783 (defvar gnus-global-score-files nil
13784   "*List of global score files and directories.
13785 Set this variable if you want to use people's score files.  One entry
13786 for each score file or each score file directory.  Gnus will decide
13787 by itself what score files are applicable to which group.
13788
13789 Say you want to use the single score file
13790 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13791 score files in the \"/ftp.some-where:/pub/score\" directory.
13792
13793  (setq gnus-global-score-files
13794        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13795          \"/ftp.some-where:/pub/score\"))")
13796
13797 (defun gnus-score-score-files (group)
13798   "Return a list of all possible score files."
13799   ;; Search and set any global score files.
13800   (and gnus-global-score-files 
13801        (or gnus-internal-global-score-files
13802            (gnus-score-search-global-directories gnus-global-score-files)))
13803   ;; Fix the kill-file dir variable.
13804   (setq gnus-kill-files-directory 
13805         (file-name-as-directory
13806          (or gnus-kill-files-directory "~/News/")))
13807   ;; If we can't read it, there are no score files.
13808   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13809       (setq gnus-score-file-list nil)
13810     (if (gnus-use-long-file-name 'not-score)
13811         ;; We want long file names.
13812         (if (or (not gnus-score-file-list)
13813                 (not (car gnus-score-file-list))
13814                 (gnus-file-newer-than gnus-kill-files-directory
13815                                       (car gnus-score-file-list)))
13816               (setq gnus-score-file-list 
13817                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13818                           (nreverse 
13819                            (directory-files 
13820                             gnus-kill-files-directory t 
13821                             (gnus-score-file-regexp))))))
13822       ;; We do not use long file names, so we have to do some
13823       ;; directory traversing.  
13824       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13825             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13826             dir files suffix)
13827         (while suffixes
13828           (setq dir (expand-file-name
13829                      (concat gnus-kill-files-directory
13830                              (gnus-replace-chars-in-string group ?. ?/))))
13831           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13832           (setq suffix (car suffixes)
13833                 suffixes (cdr suffixes))
13834           (if (file-exists-p (concat dir "/" suffix))
13835               (setq files (cons (concat dir "/" suffix) files)))
13836           (while (>= (1+ (length dir)) mdir)
13837             (and (file-exists-p (concat dir "/all/" suffix))
13838                  (setq files (cons (concat dir "/all/" suffix) files)))
13839             (string-match "/[^/]*$" dir)
13840             (setq dir (substring dir 0 (match-beginning 0)))))
13841         (setq gnus-score-file-list 
13842               (cons nil (nreverse files)))))
13843     (cdr gnus-score-file-list)))
13844
13845 (defun gnus-score-file-regexp ()
13846   (concat "\\(" gnus-score-file-suffix 
13847           "\\|" gnus-adaptive-file-suffix "\\)$"))
13848         
13849 (defun gnus-score-find-bnews (group)
13850   "Return a list of score files for GROUP.
13851 The score files are those files in the ~/News directory which matches
13852 GROUP using BNews sys file syntax."
13853   (let* ((sfiles (append (gnus-score-score-files group)
13854                          gnus-internal-global-score-files))
13855          (kill-dir (file-name-as-directory 
13856                     (expand-file-name gnus-kill-files-directory)))
13857          (klen (length kill-dir))
13858          ofiles not-match regexp)
13859     (save-excursion
13860       (set-buffer (get-buffer-create "*gnus score files*"))
13861       (buffer-disable-undo (current-buffer))
13862       ;; Go through all score file names and create regexp with them
13863       ;; as the source.  
13864       (while sfiles
13865         (erase-buffer)
13866         (insert (car sfiles))
13867         (goto-char (point-min))
13868         ;; First remove the suffix itself.
13869         (re-search-forward (concat "." (gnus-score-file-regexp)))
13870         (replace-match "" t t) 
13871         (goto-char (point-min))
13872         (if (looking-at (regexp-quote kill-dir))
13873             ;; If the file name was just "SCORE", `klen' is one character
13874             ;; too much.
13875             (delete-char (min (1- (point-max)) klen))
13876           (goto-char (point-max))
13877           (search-backward "/")
13878           (delete-region (1+ (point)) (point-min)))
13879         ;; If short file names were used, we have to translate slashes.
13880         (goto-char (point-min))
13881         (while (re-search-forward "[/:]" nil t)
13882           (replace-match "." t t))
13883         ;; Translate "all" to ".*".
13884         (while (search-forward "all" nil t)
13885           (replace-match ".*" t t))
13886         (goto-char (point-min))
13887         ;; Deal with "not."s.
13888         (if (looking-at "not.")
13889             (progn
13890               (setq not-match t)
13891               (setq regexp (buffer-substring 5 (point-max))))
13892           (setq regexp (buffer-substring 1 (point-max)))
13893           (setq not-match nil))
13894         ;; Finally - if this resulting regexp matches the group name,
13895         ;; we add this score file to the list of score files
13896         ;; applicable to this group.
13897         (if (or (and not-match
13898                      (not (string-match regexp group)))
13899                 (and (not not-match)
13900                      (string-match regexp group)))
13901             (setq ofiles (cons (car sfiles) ofiles)))
13902         (setq sfiles (cdr sfiles)))
13903       (kill-buffer (current-buffer))
13904       ;; Slight kludge here - the last score file returned should be
13905       ;; the local score file, whether it exists or not. This is so
13906       ;; that any score commands the user enters will go to the right
13907       ;; file, and not end up in some global score file.
13908       (let ((localscore
13909              (expand-file-name
13910               (if (gnus-use-long-file-name 'not-score)
13911                   (concat gnus-kill-files-directory group "." 
13912                           gnus-score-file-suffix)
13913                 (concat gnus-kill-files-directory
13914                         (gnus-replace-chars-in-string group ?. ?/)
13915                         "/" gnus-score-file-suffix)))))
13916         (and (member localscore ofiles)
13917              (delete localscore ofiles))
13918         (setq ofiles (cons localscore ofiles)))
13919       (nreverse ofiles))))
13920
13921 (defun gnus-score-find-single (group)
13922   "Return list containing the score file for GROUP."
13923   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13924         (gnus-score-file-name group)))
13925
13926 (defun gnus-score-find-hierarchical (group)
13927   "Return list of score files for GROUP.
13928 This includes the score file for the group and all its parents."
13929   (let ((all (copy-sequence '(nil)))
13930         (start 0))
13931     (while (string-match "\\." group (1+ start))
13932       (setq start (match-beginning 0))
13933       (setq all (cons (substring group 0 start) all)))
13934     (setq all (cons group all))
13935     (nconc
13936      (mapcar (lambda (newsgroup)
13937                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13938              (setq all (nreverse all)))
13939      (mapcar 'gnus-score-file-name all))))
13940
13941 (defvar gnus-score-file-alist-cache nil)
13942
13943 (defun gnus-score-find-alist (group)
13944   "Return list of score files for GROUP.
13945 The list is determined from the variable gnus-score-file-alist."
13946   (let ((alist gnus-score-file-multiple-match-alist)
13947         score-files)
13948     ;; if this group has been seen before, return the cached entry
13949     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13950         (cdr score-files)      ; ensures caching of groups with no matches
13951       ;; handle the multiple match alist
13952       (while alist
13953         (and (string-match (car (car alist)) group)
13954              (setq score-files
13955                    (nconc score-files (copy-sequence (cdr (car alist))))))
13956         (setq alist (cdr alist)))
13957       (setq alist gnus-score-file-single-match-alist)
13958       ;; handle the single match alist
13959       (while alist
13960         (and (string-match (car (car alist)) group)
13961              ;; progn used just in case ("regexp") has no files
13962              ;; and score-files is still nil. -sj
13963              ;; this can be construed as a "stop searching here" feature :>
13964              ;; and used to simplify regexps in the single-alist 
13965              (progn
13966                (setq score-files
13967                      (nconc score-files (copy-sequence (cdr (car alist)))))
13968                (setq alist nil)))
13969         (setq alist (cdr alist)))
13970       ;; cache the score files
13971       (setq gnus-score-file-alist-cache
13972             (cons (cons group score-files) gnus-score-file-alist-cache))
13973       score-files)))
13974
13975
13976 (defun gnus-possibly-score-headers (&optional trace)
13977   (let ((func gnus-score-find-score-files-function)
13978         score-files)
13979     (and func (not (listp func))
13980          (setq func (list func)))
13981     ;; Go through all the functions for finding score files (or actual
13982     ;; scores) and add them to a list.
13983     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13984     (while func
13985       (and (symbolp (car func))
13986            (fboundp (car func))
13987            (setq score-files 
13988                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13989       (setq func (cdr func)))
13990     (if score-files (gnus-score-headers score-files trace))))
13991
13992 (defun gnus-score-file-name (newsgroup &optional suffix)
13993   "Return the name of a score file for NEWSGROUP."
13994   (let ((suffix (or suffix gnus-score-file-suffix)))
13995     (cond 
13996      ((or (null newsgroup)
13997           (string-equal newsgroup ""))
13998       ;; The global score file is placed at top of the directory.
13999       (expand-file-name 
14000        suffix (or gnus-kill-files-directory "~/News")))
14001      ((gnus-use-long-file-name 'not-score)
14002       ;; Append ".SCORE" to newsgroup name.
14003       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14004                                 "." suffix)
14005                         (or gnus-kill-files-directory "~/News")))
14006      (t
14007       ;; Place "SCORE" under the hierarchical directory.
14008       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14009                                 "/" suffix)
14010                         (or gnus-kill-files-directory "~/News"))))))
14011
14012 (defun gnus-score-search-global-directories (files)
14013   "Scan all global score directories for score files."
14014   ;; Set the variable `gnus-internal-global-score-files' to all
14015   ;; available global score files.
14016   (interactive (list gnus-global-score-files))
14017   (let (out)
14018     (while files
14019       (if (string-match "/$" (car files))
14020           (setq out (nconc (directory-files 
14021                             (car files) t
14022                             (concat (gnus-score-file-regexp) "$"))))
14023         (setq out (cons (car files) out)))
14024       (setq files (cdr files)))
14025     (setq gnus-internal-global-score-files out)))
14026
14027 ;; Allow redefinition of Gnus functions.
14028
14029 (gnus-ems-redefine)
14030
14031 (provide 'gnus)
14032
14033 ;;; gnus.el ends here