*** 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 nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-nil, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [faq 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer)
731     (faq . gnus-faq-buffer))
732   "Mapping from short symbols to buffer names or buffer variables.")
733
734 (defvar gnus-carpal nil
735   "*If non-nil, display clickable icons.")
736
737 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
738   "*Function called with a group name when new group is detected.
739 A few pre-made functions are supplied: `gnus-subscribe-randomly'
740 inserts new groups at the beginning of the list of groups;
741 `gnus-subscribe-alphabetically' inserts new groups in strict
742 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
743 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
744 for your decision.")
745
746 ;; Suggested by a bug report by Hallvard B Furuseth.
747 ;; <h.b.furuseth@usit.uio.no>. 
748 (defvar gnus-subscribe-options-newsgroup-method
749   (function gnus-subscribe-alphabetically)
750   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
751 If, for instance, you want to subscribe to all newsgroups in the
752 \"no\" and \"alt\" hierarchies, you'd put the following in your
753 .newsrc file:
754
755 options -n no.all alt.all
756
757 Gnus will the subscribe all new newsgroups in these hierarchies with
758 the subscription method in this variable.")
759
760 (defvar gnus-subscribe-hierarchical-interactive nil
761   "*If non-nil, Gnus will offer to subscribe hierarchically.
762 When a new hierarchy appears, Gnus will ask the user:
763
764 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
765
766 If the user pressed `d', Gnus will descend the hierarchy, `y' will
767 subscribe to all newsgroups in the hierarchy and `s' will skip this
768 hierarchy in its entirety.")
769
770 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
771   "*Function used for sorting the group buffer.
772 This function will be called with group info entries as the arguments
773 for the groups to be sorted.  Pre-made functions include
774 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
775 `gnus-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   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
1096 (defvar gnus-article-x-face-command
1097   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1098   "String or function to be executed to display an X-Face header.
1099 If it is a string, the command will be executed in a sub-shell
1100 asynchronously. The compressed face will be piped to this command.") 
1101
1102 (defvar gnus-article-x-face-too-ugly nil
1103   "Regexp matching posters whose face shouldn't be shown automatically.")
1104
1105 (defvar gnus-select-group-hook nil
1106   "*A hook called when a newsgroup is selected.
1107
1108 If you'd like to simplify subjects like the
1109 `gnus-summary-next-same-subject' command does, you can use the
1110 following hook:
1111
1112  (setq gnus-select-group-hook
1113       (list
1114         (lambda ()
1115           (mapcar (lambda (header)
1116                      (header-set-subject
1117                       header
1118                       (gnus-simplify-subject
1119                        (header-subject header) 're-only)))
1120                   gnus-newsgroup-headers))))")
1121
1122 (defvar gnus-select-article-hook
1123   '(gnus-summary-show-thread)
1124   "*A hook called when an article is selected.
1125 The default hook shows conversation thread subtrees of the selected
1126 article automatically using `gnus-summary-show-thread'.")
1127
1128 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1129   "*A hook called to apply kill files to a group.
1130 This hook is intended to apply a kill file to the selected newsgroup.
1131 The function `gnus-apply-kill-file' is called by default.
1132
1133 Since a general kill file is too heavy to use only for a few
1134 newsgroups, I recommend you to use a lighter hook function. For
1135 example, if you'd like to apply a kill file to articles which contains
1136 a string `rmgroup' in subject in newsgroup `control', you can use the
1137 following hook:
1138
1139 \(setq gnus-apply-kill-hook
1140       (list
1141         (lambda ()
1142           (cond ((string-match \"control\" gnus-newsgroup-name)
1143                  (gnus-kill \"Subject\" \"rmgroup\")
1144                  (gnus-expunge \"X\"))))))")
1145
1146 (defvar gnus-visual-mark-article-hook 
1147   (list 'gnus-highlight-selected-summary)
1148   "*Hook run after selecting an article in the summary buffer.
1149 It is meant to be used for highlighting the article in some way.  It
1150 is not run if `gnus-visual' is nil.")
1151
1152 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1153   "*A hook called after preparing body, but before preparing header headers.
1154 The default hook (`gnus-inews-insert-signature') inserts a signature
1155 file specified by the variable `gnus-signature-file'.")
1156
1157 (defvar gnus-exit-group-hook nil
1158   "*A hook called when exiting (not quitting) summary mode.")
1159
1160 (defvar gnus-suspend-gnus-hook nil
1161   "*A hook called when suspending (not exiting) Gnus.")
1162
1163 (defvar gnus-exit-gnus-hook nil
1164   "*A hook called when exiting Gnus.")
1165
1166 (defvar gnus-save-newsrc-hook nil
1167   "*A hook called when saving the newsrc file.")
1168
1169 (defvar gnus-summary-update-hook 
1170   (list 'gnus-summary-highlight-line)
1171   "*A hook called when a summary line is changed.
1172 The hook will not be called if `gnus-visual' is nil.
1173
1174 The default function `gnus-summary-highlight-line' will
1175 highlight the line according to the `gnus-summary-highlight'
1176 variable.")
1177
1178 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1179   "*A hook called when an article is selected for the first time.
1180 The hook is intended to mark an article as read (or unread)
1181 automatically when it is selected.")
1182
1183 ;; Remove any hilit infestation.
1184 (add-hook 'gnus-startup-hook
1185           (lambda ()
1186             (remove-hook 'gnus-summary-prepare-hook
1187                          'hilit-rehighlight-buffer-quietly)
1188             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1189             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1190             (remove-hook 'gnus-article-prepare-hook
1191                          'hilit-rehighlight-buffer-quietly)))
1192
1193
1194 \f
1195 ;; Internal variables
1196
1197 ;; Avoid highlighting in kill files.
1198 (defvar gnus-summary-inhibit-highlight nil)
1199 (defvar gnus-newsgroup-selected-overlay nil)
1200
1201 (defvar gnus-article-mode-map nil)
1202 (defvar gnus-dribble-buffer nil)
1203 (defvar gnus-headers-retrieved-by nil)
1204 (defvar gnus-article-reply nil)
1205 (defvar gnus-override-method nil)
1206 (defvar gnus-article-check-size nil)
1207
1208 (defvar gnus-current-score-file nil)
1209 (defvar gnus-internal-global-score-files nil)
1210 (defvar gnus-score-file-list nil)
1211
1212
1213 (defvar gnus-current-move-group nil)
1214
1215 (defvar gnus-newsgroup-dependencies nil)
1216 (defvar gnus-newsgroup-threads nil)
1217 (defvar gnus-newsgroup-async nil)
1218 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1219
1220 (defvar gnus-newsgroup-adaptive nil)
1221
1222 (defvar gnus-summary-display-table nil)
1223
1224 (defconst gnus-group-line-format-alist
1225   (list (list ?M 'marked ?c)
1226         (list ?S 'subscribed ?c)
1227         (list ?L 'level ?d)
1228         (list ?N 'number ?s)
1229         (list ?I 'number-of-dormant ?d)
1230         (list ?T 'number-of-ticked ?d)
1231         (list ?R 'number-of-read ?s)
1232         (list ?t 'number-total ?d)
1233         (list ?y 'number-of-unread-unticked ?s)
1234         (list ?i 'number-of-ticked-and-dormant ?d)
1235         (list ?g 'group ?s)
1236         (list ?G 'qualified-group ?s)
1237         (list ?D 'newsgroup-description ?s)
1238         (list ?o 'moderated ?c)
1239         (list ?O 'moderated-string ?s)
1240         (list ?p 'process-marked ?c)
1241         (list ?s 'news-server ?s)
1242         (list ?n 'news-method ?s)
1243         (list ?z 'news-method-string ?s)
1244         (list ?u 'user-defined ?s)))
1245
1246 (defconst gnus-summary-line-format-alist 
1247   (list (list ?N 'number ?d)
1248         (list ?S 'subject ?s)
1249         (list ?s 'subject-or-nil ?s)
1250         (list ?n 'name ?s)
1251         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1252               ?s)
1253         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1254                       from) ?s)
1255         (list ?F 'from ?s)
1256         (list ?x (macroexpand '(header-xref header)) ?s)
1257         (list ?D (macroexpand '(header-date header)) ?s)
1258         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1259         (list ?M (macroexpand '(header-id header)) ?s)
1260         (list ?r (macroexpand '(header-references header)) ?s)
1261         (list ?c '(or (header-chars header) 0) ?d)
1262         (list ?L 'lines ?d)
1263         (list ?I 'indentation ?s)
1264         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1265         (list ?R 'replied ?c)
1266         (list ?\[ 'opening-bracket ?c)
1267         (list ?\] 'closing-bracket ?c)
1268         (list ?\> '(make-string level ? ) ?s)
1269         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1270         (list ?i 'score ?d)
1271         (list ?z 'score-char ?c)
1272         (list ?U 'unread ?c)
1273         (list ?t '(gnus-summary-number-of-articles-in-thread 
1274                    (and (boundp 'thread) (car thread)))
1275               ?d)
1276         (list ?e '(gnus-summary-number-of-articles-in-thread 
1277                    (and (boundp 'thread) (car thread)) t)
1278               ?c)
1279         (list ?u 'user-defined ?s))
1280   "An alist of format specifications that can appear in summary lines,
1281 and what variables they correspond with, along with the type of the
1282 variable (string, integer, character, etc).")
1283
1284 (defconst gnus-summary-dummy-line-format-alist
1285   (list (list ?S 'subject ?s)
1286         (list ?N 'number ?d)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defconst gnus-summary-mode-line-format-alist 
1290   (list (list ?G 'group-name ?s)
1291         (list ?g '(gnus-short-group-name group-name) ?s)
1292         (list ?A 'article-number ?d)
1293         (list ?Z 'unread-and-unselected ?s)
1294         (list ?V 'gnus-version ?s)
1295         (list ?U 'unread ?d)
1296         (list ?S 'subject ?s)
1297         (list ?e 'unselected ?d)
1298         (list ?u 'user-defined ?s)
1299         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1300
1301 (defconst gnus-group-mode-line-format-alist 
1302   (list (list ?S 'news-server ?s)
1303         (list ?M 'news-method ?s)
1304         (list ?u 'user-defined ?s)))
1305
1306 (defvar gnus-have-read-active-file nil)
1307
1308 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1309   "The mail address of the Gnus maintainers.")
1310
1311 (defconst gnus-version "(ding) Gnus v0.99.6"
1312   "Version number for this version of Gnus.")
1313
1314 (defvar gnus-info-nodes
1315   '((gnus-group-mode            "(gnus)The Group Buffer")
1316     (gnus-summary-mode          "(gnus)The Summary Buffer")
1317     (gnus-article-mode          "(gnus)The Article Buffer"))
1318   "Assoc list of major modes and related Info nodes.")
1319
1320 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1321   "The location of the (ding) Gnus documentation group.")
1322
1323 (defvar gnus-group-buffer "*Group*")
1324 (defvar gnus-summary-buffer "*Summary*")
1325 (defvar gnus-article-buffer "*Article*")
1326 (defvar gnus-server-buffer "*Server*")
1327
1328 (defvar gnus-work-buffer " *gnus work*")
1329
1330 (defvar gnus-buffer-list nil
1331   "Gnus buffers that should be killed on exit.")
1332
1333 (defvar gnus-server-alist nil
1334   "List of available servers.")
1335
1336 (defvar gnus-variable-list
1337   '(gnus-newsrc-options gnus-newsrc-options-n
1338     gnus-newsrc-last-checked-date 
1339     gnus-newsrc-alist gnus-server-alist
1340     gnus-killed-list gnus-zombie-list)
1341   "Gnus variables saved in the quick startup file.")
1342
1343 (defvar gnus-overload-functions
1344   '((news-inews gnus-inews-news "rnewspost"))
1345   "Functions overloaded by gnus.
1346 It is a list of `(original overload &optional file)'.")
1347
1348 (defvar gnus-newsrc-options nil
1349   "Options line in the .newsrc file.")
1350
1351 (defvar gnus-newsrc-options-n nil
1352   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1353
1354 (defvar gnus-newsrc-last-checked-date nil
1355   "Date Gnus last asked server for new newsgroups.")
1356
1357 (defvar gnus-newsrc-alist nil
1358   "Assoc list of read articles.
1359 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1360
1361 (defvar gnus-newsrc-hashtb nil
1362   "Hashtable of gnus-newsrc-alist.")
1363
1364 (defvar gnus-killed-list nil
1365   "List of killed newsgroups.")
1366
1367 (defvar gnus-killed-hashtb nil
1368   "Hash table equivalent of gnus-killed-list.")
1369
1370 (defvar gnus-zombie-list nil
1371   "List of almost dead newsgroups.")
1372
1373 (defvar gnus-description-hashtb nil
1374   "Descriptions of newsgroups.")
1375
1376 (defvar gnus-list-of-killed-groups nil
1377   "List of newsgroups that have recently been killed by the user.")
1378
1379 (defvar gnus-active-hashtb nil
1380   "Hashtable of active articles.")
1381
1382 (defvar gnus-moderated-list nil
1383   "List of moderated newsgroups.")
1384
1385 (defvar gnus-group-marked nil)
1386
1387 (defvar gnus-current-startup-file nil
1388   "Startup file for the current host.")
1389
1390 (defvar gnus-last-search-regexp nil
1391   "Default regexp for article search command.")
1392
1393 (defvar gnus-last-shell-command nil
1394   "Default shell command on article.")
1395
1396 (defvar gnus-current-select-method nil
1397   "The current method for selecting a newsgroup.")
1398
1399 (defvar gnus-have-all-newsgroups nil)
1400
1401 (defvar gnus-article-internal-prepare-hook nil)
1402
1403 (defvar gnus-newsgroup-name nil)
1404 (defvar gnus-newsgroup-begin nil)
1405 (defvar gnus-newsgroup-end nil)
1406 (defvar gnus-newsgroup-last-rmail nil)
1407 (defvar gnus-newsgroup-last-mail nil)
1408 (defvar gnus-newsgroup-last-folder nil)
1409 (defvar gnus-newsgroup-last-file nil)
1410 (defvar gnus-newsgroup-auto-expire nil)
1411 (defvar gnus-newsgroup-active nil)
1412
1413 (defvar gnus-newsgroup-unreads nil
1414   "List of unread articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-unselected nil
1417   "List of unselected unread articles in the current newsgroup.")
1418
1419 (defvar gnus-newsgroup-reads nil
1420   "Alist of read articles and article marks in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-marked nil
1423   "List of ticked articles in the current newsgroup (a subset of unread art).")
1424
1425 (defvar gnus-newsgroup-killed nil
1426   "List of ranges of articles that have been through the scoring process.")
1427
1428 (defvar gnus-newsgroup-kill-headers nil)
1429
1430 (defvar gnus-newsgroup-replied nil
1431   "List of articles that have been replied to in the current newsgroup.")
1432
1433 (defvar gnus-newsgroup-expirable nil
1434   "List of articles in the current newsgroup that can be expired.")
1435
1436 (defvar gnus-newsgroup-processable nil
1437   "List of articles in the current newsgroup that can be processed.")
1438
1439 (defvar gnus-newsgroup-bookmarks nil
1440   "List of articles in the current newsgroup that have bookmarks.")
1441
1442 (defvar gnus-newsgroup-dormant nil
1443   "List of dormant articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-scored nil
1446   "List of scored articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-headers nil
1449   "List of article headers in the current newsgroup.")
1450 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1451
1452 (defvar gnus-newsgroup-ancient nil
1453   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1454
1455 (defvar gnus-current-article nil)
1456 (defvar gnus-article-current nil)
1457 (defvar gnus-current-headers nil)
1458 (defvar gnus-have-all-headers nil)
1459 (defvar gnus-last-article nil)
1460 (defvar gnus-newsgroup-history nil)
1461 (defvar gnus-current-kill-article nil)
1462
1463 ;; Save window configuration.
1464 (defvar gnus-prev-winconf nil)
1465
1466 ;; Format specs
1467 (defvar gnus-summary-line-format-spec nil)
1468 (defvar gnus-summary-dummy-line-format-spec nil)
1469 (defvar gnus-group-line-format-spec nil)
1470 (defvar gnus-summary-mode-line-format-spec nil)
1471 (defvar gnus-article-mode-line-format-spec nil)
1472 (defvar gnus-group-mode-line-format-spec nil)
1473 (defvar gnus-summary-mark-positions nil)
1474 (defvar gnus-group-mark-positions nil)
1475
1476 (defvar gnus-summary-expunge-below nil)
1477 (defvar gnus-reffed-article-number nil)
1478
1479 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1480 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1481
1482 (defvar gnus-cache-removeable-articles nil)
1483
1484 (defconst gnus-summary-local-variables 
1485   '(gnus-newsgroup-name 
1486     gnus-newsgroup-begin gnus-newsgroup-end 
1487     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1488     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1489     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1490     gnus-newsgroup-unselected gnus-newsgroup-marked
1491     gnus-newsgroup-reads
1492     gnus-newsgroup-replied gnus-newsgroup-expirable
1493     gnus-newsgroup-processable gnus-newsgroup-killed
1494     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1495     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1496     gnus-current-article gnus-current-headers gnus-have-all-headers
1497     gnus-last-article gnus-article-internal-prepare-hook
1498     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1499     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1500     gnus-newsgroup-threads gnus-newsgroup-async
1501     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1502     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1503     gnus-newsgroup-history gnus-newsgroup-ancient
1504     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1505     gnus-cache-removeable-articles)
1506   "Variables that are buffer-local to the summary buffers.")
1507
1508 (defconst gnus-bug-message
1509   "Sending a bug report to the Gnus Towers.
1510 ========================================
1511
1512 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1513 be sent to the Gnus Bug Exterminators. 
1514
1515 At the bottom of the buffer you'll see lots of variable settings.
1516 Please do not delete those.  They will tell the Bug People what your
1517 environment is, so that it will be easier to locate the bugs.
1518
1519 If you have found a bug that makes Emacs go \"beep\", set
1520 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1521 and include the backtrace in your bug report.
1522
1523 Please describe the bug in annoying, painstaking detail.
1524
1525 Thank you for your help in stamping out bugs.
1526 ")
1527
1528 ;;; End of variables.
1529
1530 ;; Define some autoload functions Gnus might use.
1531 (eval-and-compile
1532
1533   ;; Various 
1534   (autoload 'metamail-buffer "metamail")
1535   (autoload 'Info-goto-node "info")
1536   (autoload 'hexl-hex-string-to-integer "hexl")
1537   (autoload 'pp "pp")
1538   (autoload 'pp-to-string "pp")
1539   (autoload 'pp-eval-expression "pp")
1540   (autoload 'mail-extract-address-components "mail-extr")
1541
1542   (autoload 'nnmail-split-fancy "nnmail")
1543   (autoload 'nnvirtual-catchup-group "nnvirtual")
1544
1545   ;; timezone
1546   (autoload 'timezone-make-date-arpa-standard "timezone")
1547   (autoload 'timezone-fix-time "timezone")
1548   (autoload 'timezone-make-sortable-date "timezone")
1549   (autoload 'timezone-make-time-string "timezone")
1550
1551   ;; rmail & friends
1552   (autoload 'mail-position-on-field "sendmail")
1553   (autoload 'mail-setup "sendmail")
1554   (autoload 'rmail-output "rmailout")
1555   (autoload 'news-mail-other-window "rnewspost")
1556   (autoload 'news-reply-yank-original "rnewspost")
1557   (autoload 'news-caesar-buffer-body "rnewspost")
1558   (autoload 'rmail-insert-rmail-file-header "rmail")
1559   (autoload 'rmail-count-new-messages "rmail")
1560   (autoload 'rmail-show-message "rmail")
1561
1562   ;; gnus-soup
1563   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1564   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1565   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1566   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1567   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1568   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1569   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1570
1571   ;; gnus-mh
1572   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1573   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1574   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1575   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1576   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1577   (autoload 'gnus-Folder-save-name "gnus-mh")
1578   (autoload 'gnus-folder-save-name "gnus-mh")
1579
1580   ;; gnus-vis misc
1581   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1582   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1583   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1584   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1585   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1586   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1587   (autoload 'gnus-summary-highlight-line "gnus-vis")
1588   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1589
1590   ;; gnus-vis article
1591   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1592   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1593   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1594   (autoload 'gnus-article-hide "gnus-vis" nil t)
1595   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1596   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1597   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1598   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1599   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1600   (autoload 'gnus-article-add-button "gnus-vis")
1601
1602   ;; gnus-cite
1603   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1604   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1605   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1606
1607   ;; gnus-kill
1608   (autoload 'gnus-kill "gnus-kill")
1609   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1610   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1611   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1612   (autoload 'gnus-execute "gnus-kill")
1613   (autoload 'gnus-expunge "gnus-kill")
1614
1615   ;; gnus-cache
1616   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1617   (autoload 'gnus-cache-save-buffers "gnus-cache")
1618   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1619   (autoload 'gnus-cache-request-article "gnus-cache")
1620   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1621   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1622   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1623   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1624
1625   ;; gnus-score
1626   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1627   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1628   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1629   (autoload 'gnus-score-save "gnus-score")
1630   (autoload 'gnus-score-headers "gnus-score")
1631   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1632   (autoload 'gnus-score-adaptive "gnus-score")
1633   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1634   (autoload 'gnus-score-find-trace "gnus-score")
1635
1636   ;; gnus-edit
1637   (autoload 'gnus-score-customize "gnus-edit" nil t)
1638
1639   ;; gnus-uu
1640   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1641   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1642   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1643   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1644   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1645   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1646   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1647   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1648   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1649   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1652   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1657   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1658   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1662
1663   ;; gnus-msg
1664   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1665   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1666   (autoload 'gnus-group-mail "gnus-msg" nil t)
1667   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1668   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1669   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1670   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1671   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1672   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1673   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1674   (autoload 'gnus-post-news "gnus-msg" nil t)
1675   (autoload 'gnus-inews-news "gnus-msg" nil t)
1676   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1677   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1678   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1679   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1680   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1681   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1682   (autoload 'gnus-mail-yank-original "gnus-msg")
1683   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1684   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1685   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1686   (autoload 'gnus-article-mail-with-original "gnus-msg")
1687   (autoload 'gnus-article-mail "gnus-msg")
1688   (autoload 'gnus-bug "gnus-msg" nil t)
1689
1690   ;; gnus-vm
1691   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1692   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1693   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1694   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1695   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1696   (autoload 'gnus-yank-article "gnus-vm" nil t)
1697
1698   )
1699
1700 \f
1701
1702 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1703 ;; If you want the cursor to go somewhere else, set these two
1704 ;; functions in some startup hook to whatever you want.
1705 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1706 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1707
1708 ;;; Various macros and substs.
1709
1710 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1711   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1712   (` (let ((GnusStartBufferWindow (selected-window)))
1713        (unwind-protect
1714            (progn
1715              (pop-to-buffer (, buffer))
1716              (,@ forms))
1717          (select-window GnusStartBufferWindow)))))
1718
1719 (defmacro gnus-gethash (string hashtable)
1720   "Get hash value of STRING in HASHTABLE."
1721   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1722   ;;(` (abbrev-expansion (, string) (, hashtable)))
1723   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1724
1725 (defmacro gnus-sethash (string value hashtable)
1726   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1727   ;; We cannot use define-abbrev since it only accepts string as value.
1728   ;; (set (intern string hashtable) value))
1729   (` (set (intern (, string) (, hashtable)) (, value))))
1730
1731 (defsubst gnus-buffer-substring (beg end)
1732   (buffer-substring (match-beginning beg) (match-end end)))
1733
1734 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1735 ;;   function `substring' might cut on a middle of multi-octet
1736 ;;   character.
1737 (defun gnus-truncate-string (str width)
1738   (substring str 0 width))
1739
1740 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1741 ;; to limit the length of a string. This function is necessary since
1742 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1743 (defsubst gnus-limit-string (str width)
1744   (if (> (length str) width)
1745       (substring str 0 width)
1746     str))
1747
1748 (defsubst gnus-simplify-subject-re (subject)
1749   "Remove \"Re:\" from subject lines."
1750   (let ((case-fold-search t))
1751     (if (string-match "^re: *" subject)
1752         (substring subject (match-end 0))
1753       subject)))
1754
1755 (defsubst gnus-goto-char (point)
1756   (and point (goto-char point)))
1757
1758 (defmacro gnus-buffer-exists-p (buffer)
1759   (` (and (, buffer)
1760           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1761                    (, buffer)))))
1762
1763 (defmacro gnus-kill-buffer (buffer)
1764   (` (if (gnus-buffer-exists-p (, buffer))
1765          (kill-buffer (, buffer)))))
1766
1767 (defsubst gnus-point-at-bol ()
1768   "Return point at the beginning of line."
1769   (let ((p (point)))
1770     (beginning-of-line)
1771     (prog1
1772         (point)
1773       (goto-char p))))
1774
1775 (defsubst gnus-point-at-eol ()
1776   "Return point at the beginning of line."
1777   (let ((p (point)))
1778     (end-of-line)
1779     (prog1
1780         (point)
1781       (goto-char p))))
1782
1783 ;; Delete the current line (and the next N lines.);
1784 (defmacro gnus-delete-line (&optional n)
1785   (` (delete-region (progn (beginning-of-line) (point))
1786                     (progn (forward-line (, (or n 1))) (point)))))
1787
1788 ;;; Load the compatability functions. 
1789
1790 (require 'gnus-ems)
1791
1792 \f
1793 ;;;
1794 ;;; Gnus Utility Functions
1795 ;;;
1796
1797 (defun gnus-extract-address-components (from)
1798   (let (name address)
1799     ;; First find the address - the thing with the @ in it.  This may
1800     ;; not be accurate in mail addresses, but does the trick most of
1801     ;; the time in news messages.
1802     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1803         (setq address (substring from (match-beginning 0) (match-end 0))))
1804     ;; Then we check whether the "name <address>" format is used.
1805     (and address
1806          (string-match (concat "<" (regexp-quote address) ">") from)
1807          (and (setq name (substring from 0 (1- (match-beginning 0))))
1808               ;; Strip any quotes from the name.
1809               (string-match "\".*\"" name)
1810               (setq name (substring name 1 (1- (match-end 0))))))
1811     ;; If not, then "address (name)" is used.
1812     (or name
1813         (and (string-match "(.+)" from)
1814              (setq name (substring from (1+ (match-beginning 0)) 
1815                                    (1- (match-end 0)))))
1816         (and (string-match "()" from)
1817              (setq name address))
1818         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1819         ;; XOVER might not support folded From headers.
1820         (and (string-match "(.*" from)
1821              (setq name (substring from (1+ (match-beginning 0)) 
1822                                    (match-end 0)))))
1823     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1824     (list (or name from) (or address from))))
1825
1826 (defun gnus-fetch-field (field)
1827   "Return the value of the header FIELD of current article."
1828   (save-excursion
1829     (save-restriction
1830       (let ((case-fold-search t))
1831         (gnus-narrow-to-headers)
1832         (mail-fetch-field field)))))
1833
1834 (defun gnus-goto-colon ()
1835   (beginning-of-line)
1836   (search-forward ":" (gnus-point-at-eol) t))
1837
1838 (defun gnus-narrow-to-headers ()
1839   (widen)
1840   (save-excursion
1841     (narrow-to-region
1842      (goto-char (point-min))
1843      (if (search-forward "\n\n" nil t)
1844          (1- (point))
1845        (point-max)))))
1846
1847 (defvar gnus-old-specs nil)
1848
1849 (defun gnus-update-format-specifications ()
1850   (gnus-make-thread-indent-array)
1851
1852   (let ((formats '(summary summary-dummy group 
1853                            summary-mode group-mode article-mode))
1854         old-format new-format)
1855     (while formats
1856       (setq new-format (symbol-value
1857                         (intern (format "gnus-%s-line-format" (car formats)))))
1858       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1859                (equal old-format new-format))
1860           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1861                (gnus-parse-format
1862                 new-format
1863                 (symbol-value 
1864                  (intern (format "gnus-%s-line-format-alist"
1865                                  (if (eq (car formats) 'article-mode)
1866                                      'summary-mode (car formats))))))))
1867       (setq gnus-old-specs (cons (cons (car formats) new-format)
1868                                  (delq (car formats) gnus-old-specs)))
1869       (setq formats (cdr formats))))
1870       
1871   (gnus-update-summary-mark-positions)
1872
1873   (if (and (string-match "%D" gnus-group-line-format)
1874            (not gnus-description-hashtb)
1875            gnus-read-active-file)
1876       (gnus-read-all-descriptions-files)))
1877
1878 (defun gnus-update-summary-mark-positions ()
1879   (save-excursion
1880     (let ((gnus-replied-mark 129)
1881           (gnus-score-below-mark 130)
1882           (gnus-score-over-mark 130)
1883           (thread nil)
1884           pos)
1885       (gnus-set-work-buffer)
1886       (gnus-summary-insert-line 
1887        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1888       (goto-char (point-min))
1889       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1890                                          (- (point) 2)))))
1891       (goto-char (point-min))
1892       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1893                                           (- (point) 2))) pos))
1894       (goto-char (point-min))
1895       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1896                                         (- (point) 2))) pos))
1897       (setq gnus-summary-mark-positions pos))))
1898
1899 (defun gnus-update-group-mark-positions ()
1900   (save-excursion
1901     (let ((gnus-process-mark 128)
1902           (gnus-group-marked '("dummy.group")))
1903       (gnus-set-work-buffer)
1904       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1905       (goto-char (point-min))
1906       (setq gnus-group-mark-positions
1907             (list (cons 'process (and (search-forward "\200" nil t)
1908                                       (- (point) 2))))))))
1909
1910 (defun gnus-mouse-face-function (form)
1911   (` (let ((string (, form)))
1912        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1913        string)))
1914
1915 (defun gnus-max-width-function (el max-width)
1916   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1917   (` (let* ((val (eval (, el)))
1918             (valstr (if (numberp val)
1919                         (int-to-string val) val)))
1920        (if (> (length valstr) (, max-width))
1921            (substring valstr 0 (, max-width))
1922          valstr))))
1923
1924 (defun gnus-parse-format (format spec-alist)
1925   ;; This function parses the FORMAT string with the help of the
1926   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1927   ;; string.  If the FORMAT string contains the specifiers %( and %)
1928   ;; the text between them will have the mouse-face text property.
1929   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1930       (if (and gnus-visual gnus-mouse-face)
1931           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1932                 (button (substring format (match-beginning 2) (match-end 2)))
1933                 (post (substring format (match-beginning 3) (match-end 3))))
1934             (list 'concat
1935                   (gnus-parse-simple-format pre spec-alist)
1936                   (gnus-mouse-face-function 
1937                    (gnus-parse-simple-format button spec-alist))
1938                   (gnus-parse-simple-format post spec-alist)))
1939         (gnus-parse-simple-format
1940          (concat (substring format (match-beginning 1) (match-end 1))
1941                  (substring format (match-beginning 2) (match-end 2))
1942                  (substring format (match-beginning 3) (match-end 3)))
1943          spec-alist))
1944     (gnus-parse-simple-format format spec-alist)))
1945
1946 (defun gnus-parse-simple-format (format spec-alist)
1947   ;; This function parses the FORMAT string with the help of the
1948   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1949   ;; string. The list will consist of the symbol `format', a format
1950   ;; specification string, and a list of forms depending on the
1951   ;; SPEC-ALIST.
1952   (let ((max-width 0)
1953         spec flist fstring newspec elem beg)
1954     (save-excursion
1955       (gnus-set-work-buffer)
1956       (insert format)
1957       (goto-char (point-min))
1958       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1959         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1960                                                      (match-end 2))))
1961         ;; First check if there are any specs that look anything like
1962         ;; "%12,12A", ie. with a "max width specification". These have
1963         ;; to be treated specially.
1964         (if (setq beg (match-beginning 1))
1965             (setq max-width 
1966                   (string-to-int 
1967                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1968           (setq max-width 0)
1969           (setq beg (match-beginning 2)))
1970         ;; Find the specification from `spec-alist'.
1971         (if (not (setq elem (cdr (assq spec spec-alist))))
1972             (setq elem '("*" ?s)))
1973         ;; Treat user defined format specifiers specially
1974         (and (eq (car elem) 'user-defined)
1975              (setq elem
1976                    (list 
1977                     (list (intern (concat "gnus-user-format-function-"
1978                                           (buffer-substring
1979                                            (match-beginning 3)
1980                                            (match-end 3))))
1981                           'header)
1982                     ?s))
1983              (delete-region (match-beginning 3) (match-end 3)))
1984         (if (not (zerop max-width))
1985             (let ((el (car elem)))
1986               (cond ((= (car (cdr elem)) ?c) 
1987                      (setq el (list 'char-to-string el)))
1988                     ((= (car (cdr elem)) ?d)
1989                      (numberp el) (setq el (list 'int-to-string el))))
1990               (setq flist (cons (gnus-max-width-function el max-width)
1991                                 flist))
1992               (setq newspec ?s))
1993           (setq flist (cons (car elem) flist))
1994           (setq newspec (car (cdr elem))))
1995         ;; Remove the old specification (and possibly a ",12" string).
1996         (delete-region beg (match-end 2))
1997         ;; Insert the new specification.
1998         (goto-char beg)
1999         (insert newspec))
2000       (setq fstring (buffer-substring 1 (point-max))))
2001     (cons 'format (cons fstring (nreverse flist)))))
2002
2003 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2004 (defun gnus-read-init-file ()
2005   (and gnus-init-file
2006        (or (and (file-exists-p gnus-init-file) 
2007                 ;; Don't try to load a directory.
2008                 (not (file-directory-p gnus-init-file)))
2009            (file-exists-p (concat gnus-init-file ".el"))
2010            (file-exists-p (concat gnus-init-file ".elc")))
2011        (load gnus-init-file nil t)))
2012
2013 (defun gnus-set-work-buffer ()
2014   (if (get-buffer gnus-work-buffer)
2015       (progn
2016         (set-buffer gnus-work-buffer)
2017         (erase-buffer))
2018     (set-buffer (get-buffer-create gnus-work-buffer))
2019     (kill-all-local-variables)
2020     (buffer-disable-undo (current-buffer))
2021     (gnus-add-current-to-buffer-list)))
2022
2023 ;; Article file names when saving.
2024
2025 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2026   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2027 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2028 Otherwise, it is like ~/News/news/group/num."
2029   (let ((default
2030           (expand-file-name
2031            (concat (if (gnus-use-long-file-name 'not-save)
2032                        (gnus-capitalize-newsgroup newsgroup)
2033                      (gnus-newsgroup-directory-form newsgroup))
2034                    "/" (int-to-string (header-number headers)))
2035            (or gnus-article-save-directory "~/News"))))
2036     (if (and last-file
2037              (string-equal (file-name-directory default)
2038                            (file-name-directory last-file))
2039              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2040         default
2041       (or last-file default))))
2042
2043 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2044   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2045 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2046 Otherwise, it is like ~/News/news/group/num."
2047   (let ((default
2048           (expand-file-name
2049            (concat (if (gnus-use-long-file-name 'not-save)
2050                        newsgroup
2051                      (gnus-newsgroup-directory-form newsgroup))
2052                    "/" (int-to-string (header-number headers)))
2053            (or gnus-article-save-directory "~/News"))))
2054     (if (and last-file
2055              (string-equal (file-name-directory default)
2056                            (file-name-directory last-file))
2057              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2058         default
2059       (or last-file default))))
2060
2061 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2062   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2063 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2064 Otherwise, it is like ~/News/news/group/news."
2065   (or last-file
2066       (expand-file-name
2067        (if (gnus-use-long-file-name 'not-save)
2068            (gnus-capitalize-newsgroup newsgroup)
2069          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2070        (or gnus-article-save-directory "~/News"))))
2071
2072 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2073   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2074 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2075 Otherwise, it is like ~/News/news/group/news."
2076   (or last-file
2077       (expand-file-name
2078        (if (gnus-use-long-file-name 'not-save)
2079            newsgroup
2080          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2081        (or gnus-article-save-directory "~/News"))))
2082
2083 ;; For subscribing new newsgroup
2084
2085 (defun gnus-subscribe-hierarchical-interactive (groups)
2086   (let ((groups (sort groups 'string<))
2087         prefixes prefix start ans group starts)
2088     (while groups
2089       (setq prefixes (list "^"))
2090       (while (and groups prefixes)
2091         (while (not (string-match (car prefixes) (car groups)))
2092           (setq prefixes (cdr prefixes)))
2093         (setq prefix (car prefixes))
2094         (setq start (1- (length prefix)))
2095         (if (and (string-match "[^\\.]\\." (car groups) start)
2096                  (cdr groups)
2097                  (setq prefix 
2098                        (concat "^" (substring (car groups) 0 (match-end 0))))
2099                  (string-match prefix (car (cdr groups))))
2100             (progn
2101               (setq prefixes (cons prefix prefixes))
2102               (message "Descend hierarchy %s? ([y]nsq): " 
2103                        (substring prefix 1 (1- (length prefix))))
2104               (setq ans (read-char))
2105               (cond ((= ans ?n)
2106                      (while (and groups 
2107                                  (string-match prefix 
2108                                                (setq group (car groups))))
2109                        (setq gnus-killed-list 
2110                              (cons group gnus-killed-list))
2111                        (gnus-sethash group group gnus-killed-hashtb)
2112                        (setq groups (cdr groups)))
2113                      (setq starts (cdr starts)))
2114                     ((= ans ?s)
2115                      (while (and groups 
2116                                  (string-match prefix 
2117                                                (setq group (car groups))))
2118                        (gnus-sethash group group gnus-killed-hashtb)
2119                        (gnus-subscribe-alphabetically (car groups))
2120                        (setq groups (cdr groups)))
2121                      (setq starts (cdr starts)))
2122                     ((= ans ?q)
2123                      (while groups
2124                        (setq group (car groups))
2125                        (setq gnus-killed-list (cons group gnus-killed-list))
2126                        (gnus-sethash group group gnus-killed-hashtb)
2127                        (setq groups (cdr groups))))
2128                     (t nil)))
2129           (message "Subscribe %s? ([n]yq)" (car groups))
2130           (setq ans (read-char))
2131           (setq group (car groups))
2132           (cond ((= ans ?y)
2133                  (gnus-subscribe-alphabetically (car groups))
2134                  (gnus-sethash group group gnus-killed-hashtb))
2135                 ((= ans ?q)
2136                  (while groups
2137                    (setq group (car groups))
2138                    (setq gnus-killed-list (cons group gnus-killed-list))
2139                    (gnus-sethash group group gnus-killed-hashtb)
2140                    (setq groups (cdr groups))))
2141                 (t 
2142                  (setq gnus-killed-list (cons group gnus-killed-list))
2143                  (gnus-sethash group group gnus-killed-hashtb)))
2144           (setq groups (cdr groups)))))))
2145
2146 (defun gnus-subscribe-randomly (newsgroup)
2147   "Subscribe new NEWSGROUP by making it the first newsgroup."
2148   (gnus-subscribe-newsgroup newsgroup))
2149
2150 (defun gnus-subscribe-alphabetically (newgroup)
2151   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2152   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2153   (let ((groups (cdr gnus-newsrc-alist))
2154         before)
2155     (while (and (not before) groups)
2156       (if (string< newgroup (car (car groups)))
2157           (setq before (car (car groups)))
2158         (setq groups (cdr groups))))
2159     (gnus-subscribe-newsgroup newgroup before)))
2160
2161 (defun gnus-subscribe-hierarchically (newgroup)
2162   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2163   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2164   (save-excursion
2165     (set-buffer (find-file-noselect gnus-current-startup-file))
2166     (let ((groupkey newgroup)
2167           before)
2168       (while (and (not before) groupkey)
2169         (goto-char (point-min))
2170         (let ((groupkey-re
2171                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2172           (while (and (re-search-forward groupkey-re nil t)
2173                       (progn
2174                         (setq before (buffer-substring
2175                                       (match-beginning 1) (match-end 1)))
2176                         (string< before newgroup)))))
2177         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2178         (setq groupkey
2179               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2180                   (substring groupkey (match-beginning 1) (match-end 1)))))
2181       (gnus-subscribe-newsgroup newgroup before))))
2182
2183 (defun gnus-subscribe-interactively (newsgroup)
2184   "Subscribe new NEWSGROUP interactively.
2185 It is inserted in hierarchical newsgroup order if subscribed. If not,
2186 it is killed."
2187   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2188       (gnus-subscribe-hierarchically newsgroup)
2189     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2190
2191 (defun gnus-subscribe-zombies (newsgroup)
2192   "Make new NEWSGROUP a zombie group."
2193   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2194
2195 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2196   "Subscribe new NEWSGROUP.
2197 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2198 the first newsgroup."
2199   ;; We subscribe the group by changing its level to `subscribed'.
2200   (gnus-group-change-level 
2201    newsgroup gnus-level-default-subscribed
2202    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2203   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2204
2205 ;; For directories
2206
2207 (defun gnus-newsgroup-directory-form (newsgroup)
2208   "Make hierarchical directory name from NEWSGROUP name."
2209   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2210         (len (length newsgroup))
2211         idx)
2212     ;; If this is a foreign group, we don't want to translate the
2213     ;; entire name.  
2214     (if (setq idx (string-match ":" newsgroup))
2215         (aset newsgroup idx ?/)
2216       (setq idx 0))
2217     ;; Replace all occurrences of `.' with `/'.
2218     (while (< idx len)
2219       (if (= (aref newsgroup idx) ?.)
2220           (aset newsgroup idx ?/))
2221       (setq idx (1+ idx)))
2222     newsgroup))
2223
2224 (defun gnus-make-directory (dir)
2225   "Make DIRECTORY recursively."
2226   (let* ((dir (expand-file-name dir default-directory))
2227          dirs)
2228     (if (string-match "/$" dir)
2229         (setq dir (substring dir 0 (match-beginning 0))))
2230     (while (not (file-exists-p dir))
2231       (setq dirs (cons dir dirs))
2232       (string-match "/[^/]+$" dir)
2233       (setq dir (substring dir 0 (match-beginning 0))))
2234     (while dirs
2235       (make-directory (car dirs))
2236       (setq dirs (cdr dirs)))))
2237
2238 (defun gnus-capitalize-newsgroup (newsgroup)
2239   "Capitalize NEWSGROUP name."
2240   (and (not (zerop (length newsgroup)))
2241        (concat (char-to-string (upcase (aref newsgroup 0)))
2242                (substring newsgroup 1))))
2243
2244 ;; Var
2245
2246 (defun gnus-simplify-subject (subject &optional re-only)
2247   "Remove `Re:' and words in parentheses.
2248 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2249   (let ((case-fold-search t))           ;Ignore case.
2250     ;; Remove `Re:' and `Re^N:'.
2251     (if (string-match "^re:[ \t]*" subject)
2252         (setq subject (substring subject (match-end 0))))
2253     ;; Remove words in parentheses from end.
2254     (or re-only
2255         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2256           (setq subject (substring subject 0 (match-beginning 0)))))
2257     ;; Return subject string.
2258     subject))
2259
2260 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2261 ;; all whitespace.
2262 (defun gnus-simplify-subject-fuzzy (subject)
2263   (let ((case-fold-search t))
2264     (save-excursion
2265       (gnus-set-work-buffer)
2266       (insert subject)
2267       (inline (gnus-simplify-buffer-fuzzy))
2268       (buffer-string))))
2269
2270 (defun gnus-simplify-buffer-fuzzy ()
2271   (goto-char (point-min))
2272   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2273   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2274                             nil t)
2275     (replace-match "" t t))
2276   (goto-char (point-min))
2277   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2278     (replace-match "" t t))
2279   (goto-char (point-min))
2280   (while (re-search-forward "[ \t]+" nil t)
2281     (replace-match " " t t))
2282   (goto-char (point-min))
2283   (while (re-search-forward "[ \t]+$" nil t)
2284     (replace-match "" t t))
2285   (goto-char (point-min))
2286   (while (re-search-forward "^[ \t]+" nil t)
2287     (replace-match "" t t))
2288   (if gnus-simplify-subject-fuzzy-regexp
2289       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2290         (replace-match "" t t))))
2291
2292 ;; Add the current buffer to the list of buffers to be killed on exit. 
2293 (defun gnus-add-current-to-buffer-list ()
2294   (or (memq (current-buffer) gnus-buffer-list)
2295       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2296
2297 (defun gnus-string> (s1 s2)
2298   (not (or (string< s1 s2)
2299            (string= s1 s2))))
2300
2301 ;; Functions accessing headers.
2302 ;; Functions are more convenient than macros in some cases.
2303
2304 (defun gnus-header-number (header)
2305   (header-number header))
2306
2307 (defun gnus-header-subject (header)
2308   (header-subject header))
2309
2310 (defun gnus-header-from (header)
2311   (header-from header))
2312
2313 (defun gnus-header-xref (header)
2314   (header-xref header))
2315
2316 (defun gnus-header-lines (header)
2317   (header-lines header))
2318
2319 (defun gnus-header-date (header)
2320   (header-date header))
2321
2322 (defun gnus-header-id (header)
2323   (header-id header))
2324
2325 (defun gnus-header-references (header)
2326   (header-references header))
2327
2328 ;;; General various misc type functions.
2329
2330 (defun gnus-clear-system ()
2331   "Clear all variables and buffers."
2332   ;; Clear Gnus variables.
2333   (let ((variables gnus-variable-list))
2334     (while variables
2335       (set (car variables) nil)
2336       (setq variables (cdr variables))))
2337   ;; Clear other internal variables.
2338   (setq gnus-list-of-killed-groups nil
2339         gnus-have-read-active-file nil
2340         gnus-newsrc-alist nil
2341         gnus-newsrc-hashtb nil
2342         gnus-killed-list nil
2343         gnus-zombie-list nil
2344         gnus-killed-hashtb nil
2345         gnus-active-hashtb nil
2346         gnus-moderated-list nil
2347         gnus-description-hashtb nil
2348         gnus-newsgroup-headers nil
2349         gnus-newsgroup-headers-hashtb-by-number nil
2350         gnus-newsgroup-name nil
2351         gnus-server-alist nil
2352         gnus-current-select-method nil)
2353   ;; Reset any score variables.
2354   (and (boundp 'gnus-score-cache)
2355        (set 'gnus-score-cache nil))
2356   (and (boundp 'gnus-internal-global-score-files)
2357        (set 'gnus-internal-global-score-files nil))
2358   ;; Kill the startup file.
2359   (and gnus-current-startup-file
2360        (get-file-buffer gnus-current-startup-file)
2361        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2362   ;; Save any cache buffers.
2363   (and gnus-use-cache (gnus-cache-save-buffers))
2364   ;; Clear the dribble buffer.
2365   (gnus-dribble-clear)
2366   ;; Kill global KILL file buffer.
2367   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2368       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2369   (gnus-kill-buffer nntp-server-buffer)
2370   ;; Kill Gnus buffers.
2371   (while gnus-buffer-list
2372     (gnus-kill-buffer (car gnus-buffer-list))
2373     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2374
2375 (defun gnus-windows-old-to-new (setting)
2376   (if (symbolp setting)
2377       (setq setting 
2378             (cond ((eq setting 'SelectArticle)
2379                    'article)
2380                   ((eq setting 'SelectSubject)
2381                    'summary)
2382                   ((eq setting 'SelectNewsgroup)
2383                    'group)
2384                   (t setting))))
2385   (if (or (listp setting)
2386           (not (and gnus-window-configuration
2387                     (memq setting '(group summary article)))))
2388       setting
2389     (let* ((setting (if (eq setting 'group) 
2390                         (if (assq 'newsgroup gnus-window-configuration)
2391                             'newsgroup
2392                           'newsgroups) setting))
2393            (elem (car (cdr (assq setting gnus-window-configuration))))
2394            (total (apply '+ elem))
2395            (types '(group summary article))
2396            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2397            (i 0)
2398            perc
2399            out)
2400       (while (< i 3)
2401         (or (zerop (nth i elem))
2402             (progn
2403               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2404               (setq out (cons (if (eq pbuf (nth i types))
2405                                   (vector (nth i types) perc 'point)
2406                                 (vector (nth i types) perc))
2407                               out))))
2408         (setq i (1+ i)))
2409       (list (nreverse out)))))
2410            
2411 (defun gnus-add-configuration (conf)
2412   (setq gnus-buffer-configuration 
2413         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2414                          gnus-buffer-configuration))))
2415
2416 (defun gnus-configure-windows (setting &optional force)
2417   (setq setting (gnus-windows-old-to-new setting))
2418   (let ((r (if (symbolp setting)
2419                (cdr (assq setting gnus-buffer-configuration))
2420              setting))
2421         (in-buf (current-buffer))
2422         rule val w height hor ohor heights sub jump-buffer
2423         rel total to-buf all-visible)
2424     (or r (error "No such setting: %s" setting))
2425
2426     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2427         ;; All the windows mentioned are already visibe, so we just
2428         ;; put point in the assigned buffer, and do not touch the
2429         ;; winconf. 
2430         (select-window (get-buffer-window all-visible))
2431
2432       ;; Either remove all windows or just remove all Gnus windows.
2433       (if gnus-use-full-window
2434           (delete-other-windows)
2435         (gnus-remove-some-windows)
2436         (switch-to-buffer nntp-server-buffer))
2437
2438       (while r
2439         (setq hor (car r)
2440               ohor nil)
2441
2442         ;; We have to do the (possible) horizontal splitting before the
2443         ;; vertical. 
2444         (if (and (listp (car hor)) 
2445                  (eq (car (car hor)) 'horizontal))
2446             (progn
2447               (split-window 
2448                nil
2449                (if (integerp (nth 1 (car hor)))
2450                    (nth 1 (car hor))
2451                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2452                t)
2453               (setq hor (cdr hor))))
2454
2455         ;; Go through the rules and eval the elements that are to be
2456         ;; evaled.  
2457         (while hor
2458           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2459               (progn
2460                 ;; Expand short buffer name.
2461                 (setq w (aref val 0))
2462                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2463                      (progn
2464                        (setq val (apply 'vector (mapcar 'identity val)))
2465                        (aset val 0 w)))
2466                 (setq ohor (cons val ohor))))
2467           (setq hor (cdr hor)))
2468         (setq rule (cons (nreverse ohor) rule))
2469         (setq r (cdr r)))
2470       (setq rule (nreverse rule))
2471
2472       ;; We tally the window sizes.
2473       (setq total (window-height))
2474       (while rule
2475         (setq hor (car rule))
2476         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2477             (setq hor (cdr hor)))
2478         (setq sub 0)
2479         (while hor
2480           (setq rel (aref (car hor) 1)
2481                 heights (cons
2482                          (cond ((and (floatp rel) (= 1.0 rel))
2483                                 'x)
2484                                ((integerp rel)
2485                                 rel)
2486                                (t
2487                                 (max (floor (* total rel)) 4)))
2488                          heights)
2489                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2490                 hor (cdr hor)))
2491         (setq heights (nreverse heights)
2492               hor (car rule))
2493
2494         ;; We then go through these heighs and create windows for them.
2495         (while heights
2496           (setq height (car heights)
2497                 heights (cdr heights))
2498           (and (eq height 'x)
2499                (setq height (- total sub)))
2500           (and heights
2501                (split-window nil height))
2502           (setq to-buf (aref (car hor) 0))
2503           (switch-to-buffer 
2504            (cond ((not to-buf)
2505                   in-buf)
2506                  ((symbolp to-buf)
2507                   (symbol-value (aref (car hor) 0)))
2508                  (t
2509                   (aref (car hor) 0))))
2510           (and (> (length (car hor)) 2)
2511                (eq (aref (car hor) 2) 'point)
2512                (setq jump-buffer (current-buffer)))
2513           (other-window 1)
2514           (setq hor (cdr hor)))
2515       
2516         (setq rule (cdr rule)))
2517
2518       ;; Finally, we pop to the buffer that's supposed to have point. 
2519       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2520
2521       (select-window (get-buffer-window jump-buffer))
2522       (set-buffer jump-buffer))))
2523
2524 (defun gnus-all-windows-visible-p (rule)
2525   (let (invisible hor jump-buffer val buffer)
2526     ;; Go through the rules and eval the elements that are to be
2527     ;; evaled.  
2528     (while (and rule (not invisible))
2529       (setq hor (car rule)
2530             rule (cdr rule))
2531       (while (and hor (not invisible))
2532         (if (setq val (if (vectorp (car hor)) 
2533                           (car hor)
2534                         (if (not (eq (car (car hor)) 'horizontal))
2535                             (eval (car hor)))))
2536             (progn
2537               ;; Expand short buffer name.
2538               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2539                                (aref val 0)))
2540               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2541                              buffer))
2542               (and (> (length val) 2) (eq 'point (aref val 2))
2543                    (setq jump-buffer buffer))
2544               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2545         (setq hor (cdr hor))))
2546     (and (not invisible) jump-buffer)))
2547
2548       
2549 (defun gnus-remove-some-windows ()
2550   (let ((buffers gnus-window-to-buffer)
2551         (first t)
2552         buf)
2553     (save-excursion
2554       ;; Remove windows on all known Gnus buffers.
2555       (while buffers
2556         (setq buf (cdr (car buffers)))
2557         (if (symbolp buf)
2558             (setq buf (and (boundp buf) (symbol-value buf))))
2559         (and buf 
2560              (get-buffer-window buf)
2561              (progn
2562                (if first
2563                    (progn
2564                      (pop-to-buffer buf)
2565                      (switch-to-buffer nntp-server-buffer)
2566                      (setq first nil))
2567                  (delete-window (get-buffer-window buf)))))
2568         (setq buffers (cdr buffers)))
2569       ;; Remove windows on *all* summary buffers.
2570       (let (wins)
2571         (walk-windows
2572          (lambda (win)
2573            (let ((buf (window-buffer win)))
2574              (if (string-match  "^\\*Summary" (buffer-name buf))
2575                  (if first
2576                      (progn
2577                        (select-window win)
2578                        (switch-to-buffer nntp-server-buffer)
2579                        (setq first nil))
2580                    (setq wins (cons win wins)))))))
2581         (while wins
2582           (delete-window (car wins))
2583           (setq wins (cdr wins)))))))
2584       
2585                           
2586 (defun gnus-version ()
2587   "Version numbers of this version of Gnus."
2588   (interactive)
2589   (let ((methods gnus-valid-select-methods)
2590         (mess gnus-version)
2591         meth)
2592     ;; Go through all the legal select methods and add their version
2593     ;; numbers to the total version string. Only the backends that are
2594     ;; currently in use will have their message numbers taken into
2595     ;; consideration. 
2596     (while methods
2597       (setq meth (intern (concat (car (car methods)) "-version")))
2598       (and (boundp meth)
2599            (stringp (symbol-value meth))
2600            (setq mess (concat mess "; " (symbol-value meth))))
2601       (setq methods (cdr methods)))
2602     (gnus-message 2 mess)))
2603
2604 (defun gnus-info-find-node ()
2605   "Find Info documentation of Gnus."
2606   (interactive)
2607   ;; Enlarge info window if needed.
2608   (let ((mode major-mode))
2609     (gnus-configure-windows 'info)
2610     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2611
2612 (defun gnus-overload-functions (&optional overloads)
2613   "Overload functions specified by optional argument OVERLOADS.
2614 If nothing is specified, use the variable gnus-overload-functions."
2615   (let ((defs nil)
2616         (overloads (or overloads gnus-overload-functions)))
2617     (while overloads
2618       (setq defs (car overloads))
2619       (setq overloads (cdr overloads))
2620       ;; Load file before overloading function if necessary.  Make
2621       ;; sure we cannot use `require' always.
2622       (and (not (fboundp (car defs)))
2623            (car (cdr (cdr defs)))
2624            (load (car (cdr (cdr defs))) nil 'nomessage))
2625       (fset (car defs) (car (cdr defs))))))
2626
2627 (defun gnus-replace-chars-in-string (string from to)
2628   "Replace characters in STRING from FROM to TO."
2629   (let ((string (substring string 0))   ;Copy string.
2630         (len (length string))
2631         (idx 0))
2632     ;; Replace all occurrences of FROM with TO.
2633     (while (< idx len)
2634       (if (= (aref string idx) from)
2635           (aset string idx to))
2636       (setq idx (1+ idx)))
2637     string))
2638
2639 (defun gnus-days-between (date1 date2)
2640   ;; Return the number of days between date1 and date2.
2641   (- (gnus-day-number date1) (gnus-day-number date2)))
2642
2643 (defun gnus-day-number (date)
2644   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2645                      (timezone-parse-date date))))
2646     (timezone-absolute-from-gregorian 
2647      (nth 1 dat) (nth 2 dat) (car dat))))
2648
2649 ;; Returns a floating point number that says how many seconds have
2650 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2651 (defun gnus-seconds-since-epoch (date)
2652   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2653                         (timezone-parse-date date)))
2654          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2655                         (timezone-parse-time
2656                          (aref (timezone-parse-date date) 3))))
2657          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2658                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2659          (tday (- (timezone-absolute-from-gregorian 
2660                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2661                   (timezone-absolute-from-gregorian 
2662                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2663     (+ (nth 2 ttime)
2664        (* (nth 1 ttime) 60)
2665        (* 1.0 (nth 0 ttime) 60 60)
2666        (* 1.0 tday 60 60 24))))
2667
2668 (defun gnus-file-newer-than (file date)
2669   (let ((fdate (nth 5 (file-attributes file))))
2670     (or (> (car fdate) (car date))
2671         (and (= (car fdate) (car date))
2672              (> (nth 1 fdate) (nth 1 date))))))
2673
2674 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2675 ;; the echo area.
2676 (defun gnus-y-or-n-p (prompt)
2677   (prog1
2678       (y-or-n-p prompt)
2679     (message "")))
2680
2681 (defun gnus-yes-or-no-p (prompt)
2682   (prog1
2683       (yes-or-no-p prompt)
2684     (message "")))
2685
2686 ;; Check whether to use long file names.
2687 (defun gnus-use-long-file-name (symbol)
2688   ;; The variable has to be set...
2689   (and gnus-use-long-file-name
2690        ;; If it isn't a list, then we return t.
2691        (or (not (listp gnus-use-long-file-name))
2692            ;; If it is a list, and the list contains `symbol', we
2693            ;; return nil.  
2694            (not (memq symbol gnus-use-long-file-name)))))
2695
2696 ;; I suspect there's a better way, but I haven't taken the time to do
2697 ;; it yet. -erik selberg@cs.washington.edu
2698 (defun gnus-dd-mmm (messy-date)
2699   "Return a string like DD-MMM from a big messy string"
2700   (let ((datevec (timezone-parse-date messy-date)))
2701     (format "%2s-%s"
2702             (or (aref datevec 2) "??")
2703             (capitalize
2704              (or (car 
2705                   (nth (1- (string-to-number (aref datevec 1)))
2706                        timezone-months-assoc))
2707                  "???")))))
2708
2709 ;; Make a hash table (default and minimum size is 255).
2710 ;; Optional argument HASHSIZE specifies the table size.
2711 (defun gnus-make-hashtable (&optional hashsize)
2712   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2713
2714 ;; Make a number that is suitable for hashing; bigger than MIN and one
2715 ;; less than 2^x.
2716 (defun gnus-create-hash-size (min)
2717   (let ((i 1))
2718     (while (< i min)
2719       (setq i (* 2 i)))
2720     (1- i)))
2721
2722 ;; Show message if message has a lower level than `gnus-verbose'. 
2723 ;; Guide-line for numbers:
2724 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2725 ;; for things that take a long time, 7 - not very important messages
2726 ;; on stuff, 9 - messages inside loops.
2727 (defun gnus-message (level &rest args)
2728   (if (<= level gnus-verbose)
2729       (apply 'message args)
2730     ;; We have to do this format thingie here even if the result isn't
2731     ;; shown - the return value has to be the same as the return value
2732     ;; from `message'.
2733     (apply 'format args)))
2734
2735 ;; Generate a unique new group name.
2736 (defun gnus-generate-new-group-name (leaf)
2737   (let ((name leaf)
2738         (num 0))
2739     (while (gnus-gethash name gnus-newsrc-hashtb)
2740       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2741     name))
2742
2743 (defun gnus-find-file-noselect (file &optional force)
2744   "Does vaguely the same as find-file-noselect. No hooks are run."
2745   (let (buf insert)
2746     (if (setq buf (get-file-buffer file))
2747         (setq insert force)
2748       (setq buf (create-file-buffer file))
2749       (setq insert t))
2750     (if (not insert)
2751         buf
2752       (save-excursion
2753         (set-buffer buf)
2754         (erase-buffer)
2755         (and (file-readable-p file)
2756              (insert-file-contents file))
2757         (set-visited-file-name file)
2758         (set-buffer-modified-p nil)
2759         (current-buffer)))))
2760
2761 ;;; List and range functions
2762
2763 (defun gnus-last-element (list)
2764   "Return last element of LIST."
2765   (while (cdr list)
2766     (setq list (cdr list)))
2767   (car list))
2768
2769 (defun gnus-copy-sequence (list)
2770   "Do a complete, total copy of a list."
2771   (if (and (consp list) (not (consp (cdr list))))
2772       (cons (car list) (cdr list))
2773     (mapcar (lambda (elem) (if (consp elem) 
2774                                (if (consp (cdr elem))
2775                                    (gnus-copy-sequence elem)
2776                                  (cons (car elem) (cdr elem)))
2777                              elem))
2778             list)))
2779
2780 (defun gnus-set-difference (list1 list2)
2781   "Return a list of elements of LIST1 that do not appear in LIST2."
2782   (let ((list1 (copy-sequence list1)))
2783     (while list2
2784       (setq list1 (delq (car list2) list1))
2785       (setq list2 (cdr list2)))
2786     list1))
2787
2788 (defun gnus-sorted-complement (list1 list2)
2789   "Return a list of elements of LIST1 that do not appear in LIST2.
2790 Both lists have to be sorted over <."
2791   (let (out)
2792     (if (or (null list1) (null list2))
2793         (or list1 list2)
2794       (while (and list1 list2)
2795         (cond ((= (car list1) (car list2))
2796                (setq list1 (cdr list1)
2797                      list2 (cdr list2)))
2798               ((< (car list1) (car list2))
2799                (setq out (cons (car list1) out))
2800                (setq list1 (cdr list1)))
2801               (t
2802                (setq out (cons (car list2) out))
2803                (setq list2 (cdr list2)))))
2804       (nconc (nreverse out) (or list1 list2)))))
2805
2806 (defun gnus-intersection (list1 list2)      
2807   (let ((result nil))
2808     (while list2
2809       (if (memq (car list2) list1)
2810           (setq result (cons (car list2) result)))
2811       (setq list2 (cdr list2)))
2812     result))
2813
2814 (defun gnus-sorted-intersection (list1 list2)
2815   ;; LIST1 and LIST2 have to be sorted over <.
2816   (let (out)
2817     (while (and list1 list2)
2818       (cond ((= (car list1) (car list2))
2819              (setq out (cons (car list1) out)
2820                    list1 (cdr list1)
2821                    list2 (cdr list2)))
2822             ((< (car list1) (car list2))
2823              (setq list1 (cdr list1)))
2824             (t
2825              (setq list2 (cdr list2)))))
2826     (nreverse out)))
2827
2828 (defun gnus-set-sorted-intersection (list1 list2)
2829   ;; LIST1 and LIST2 have to be sorted over <.
2830   ;; This function modifies LIST1.
2831   (let* ((top (cons nil list1))
2832          (prev top))
2833   (while (and list1 list2)
2834     (cond ((= (car list1) (car list2))
2835            (setq prev list1
2836                  list1 (cdr list1)
2837                  list2 (cdr list2)))
2838           ((< (car list1) (car list2))
2839            (setcdr prev (cdr list1))
2840            (setq list1 (cdr list1)))
2841           (t
2842            (setq list2 (cdr list2)))))
2843   (setcdr prev nil)
2844   (cdr top)))
2845
2846 (defun gnus-compress-sequence (numbers &optional always-list)
2847   "Convert list of numbers to a list of ranges or a single range.
2848 If ALWAYS-LIST is non-nil, this function will always release a list of
2849 ranges."
2850   (let* ((first (car numbers))
2851          (last (car numbers))
2852          result)
2853     (if (null numbers)
2854         nil
2855       (if (not (listp (cdr numbers)))
2856           numbers
2857         (while numbers
2858           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2859                 ((= (1+ last) (car numbers)) ;Still in sequence
2860                  (setq last (car numbers)))
2861                 (t                      ;End of one sequence
2862                  (setq result 
2863                        (cons (if (= first last) first
2864                                (cons first last)) result))
2865                  (setq first (car numbers))
2866                  (setq last  (car numbers))))
2867           (setq numbers (cdr numbers)))
2868         (if (and (not always-list) (null result))
2869             (if (= first last) (list first) (cons first last))
2870           (nreverse (cons (if (= first last) first (cons first last))
2871                           result)))))))
2872
2873 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2874 (defun gnus-uncompress-range (ranges)
2875   "Expand a list of ranges into a list of numbers.
2876 RANGES is either a single range on the form `(num . num)' or a list of
2877 these ranges."
2878   (let (first last result)
2879     (cond 
2880      ((null ranges)
2881       nil)
2882      ((not (listp (cdr ranges)))
2883       (setq first (car ranges))
2884       (setq last (cdr ranges))
2885       (while (<= first last)
2886         (setq result (cons first result))
2887         (setq first (1+ first)))
2888       (nreverse result))
2889      (t
2890       (while ranges
2891         (if (atom (car ranges))
2892             (if (numberp (car ranges))
2893                 (setq result (cons (car ranges) result)))
2894           (setq first (car (car ranges)))
2895           (setq last  (cdr (car ranges)))
2896           (while (<= first last)
2897             (setq result (cons first result))
2898             (setq first (1+ first))))
2899         (setq ranges (cdr ranges)))
2900       (nreverse result)))))
2901
2902 (defun gnus-add-to-range (ranges list)
2903   "Return a list of ranges that has all articles from both RANGES and LIST.
2904 Note: LIST has to be sorted over `<'."
2905   (if (not ranges)
2906       (gnus-compress-sequence list t)
2907     (setq list (copy-sequence list))
2908     (or (listp (cdr ranges))
2909         (setq ranges (list ranges)))
2910     (let ((out ranges)
2911           ilist lowest highest temp)
2912       (while (and ranges list)
2913         (setq ilist list)
2914         (setq lowest (or (and (atom (car ranges)) (car ranges))
2915                          (car (car ranges))))
2916         (while (and list (cdr list) (< (car (cdr list)) lowest))
2917           (setq list (cdr list)))
2918         (if (< (car ilist) lowest)
2919             (progn
2920               (setq temp list)
2921               (setq list (cdr list))
2922               (setcdr temp nil)
2923               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2924         (setq highest (or (and (atom (car ranges)) (car ranges))
2925                           (cdr (car ranges))))
2926         (while (and list (<= (car list) highest))
2927           (setq list (cdr list)))
2928         (setq ranges (cdr ranges)))
2929       (if list
2930           (setq out (nconc (gnus-compress-sequence list t) out)))
2931       (setq out (sort out (lambda (r1 r2) 
2932                             (< (or (and (atom r1) r1) (car r1))
2933                                (or (and (atom r2) r2) (car r2))))))
2934       (setq ranges out)
2935       (while ranges
2936         (if (atom (car ranges))
2937             (if (cdr ranges)
2938                 (if (atom (car (cdr ranges)))
2939                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2940                         (progn
2941                           (setcar ranges (cons (car ranges) 
2942                                                (car (cdr ranges))))
2943                           (setcdr ranges (cdr (cdr ranges)))))
2944                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2945                       (progn
2946                         (setcar (car (cdr ranges)) (car ranges))
2947                         (setcar ranges (car (cdr ranges)))
2948                         (setcdr ranges (cdr (cdr ranges)))))))
2949           (if (cdr ranges)
2950               (if (atom (car (cdr ranges)))
2951                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2952                       (progn
2953                         (setcdr (car ranges) (car (cdr ranges)))
2954                         (setcdr ranges (cdr (cdr ranges)))))
2955                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2956                     (progn
2957                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2958                       (setcdr ranges (cdr (cdr ranges))))))))
2959         (setq ranges (cdr ranges)))
2960       out)))
2961
2962 (defun gnus-remove-from-range (ranges list)
2963   "Return a list of ranges that has all articles from LIST removed from RANGES.
2964 Note: LIST has to be sorted over `<'."
2965   ;; !!! This function shouldn't look like this, but I've got a headache.
2966   (gnus-compress-sequence 
2967    (gnus-sorted-complement
2968     (gnus-uncompress-range ranges) list)))
2969
2970 (defun gnus-member-of-range (number ranges)
2971   (if (not (listp (cdr ranges)))
2972       (and (>= number (car ranges)) 
2973            (<= number (cdr ranges)))
2974     (let ((not-stop t))
2975       (while (and ranges 
2976                   (if (numberp (car ranges))
2977                       (>= number (car ranges))
2978                     (>= number (car (car ranges))))
2979                   not-stop)
2980         (if (if (numberp (car ranges))
2981                 (= number (car ranges))
2982               (and (>= number (car (car ranges)))
2983                    (<= number (cdr (car ranges)))))
2984             (setq not-stop nil))
2985         (setq ranges (cdr ranges)))
2986       (not not-stop))))
2987
2988 \f
2989 ;;;
2990 ;;; Gnus group mode
2991 ;;;
2992
2993 (defvar gnus-group-mode-map nil)
2994 (defvar gnus-group-group-map nil)
2995 (defvar gnus-group-mark-map nil)
2996 (defvar gnus-group-list-map nil)
2997 (defvar gnus-group-sub-map nil)
2998 (put 'gnus-group-mode 'mode-class 'special)
2999
3000 (if gnus-group-mode-map
3001     nil
3002   (setq gnus-group-mode-map (make-keymap))
3003   (suppress-keymap gnus-group-mode-map)
3004   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3005   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3006   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3007   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3008   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3009   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3010   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3011   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3012   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3013   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3014   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3015   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3016   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3017   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3018   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3019   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3020   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3021   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3022   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3023   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3024   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3025   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3026   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3027   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3028   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3029   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3030   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3031   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3032   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3033   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3034   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3035   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3036   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3037   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3038   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3039   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3040   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3041   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3042   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3043   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3044   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3045   (define-key gnus-group-mode-map "V" 'gnus-version)
3046   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3047   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3048   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3049   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3050   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3051   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3052   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3053   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3054   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3055   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3056   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3057   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3058   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3059   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3060   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3061
3062   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3063   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3064   (define-prefix-command 'gnus-group-mark-map)
3065   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3066   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3067   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3068   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3069
3070   (define-prefix-command 'gnus-group-group-map)
3071   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3072   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3073   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3074   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3075   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3076   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3077   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3078   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3079   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3080   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3081   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3082   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3083   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3084   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3085   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3086   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3087   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3088   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3089
3090   (define-prefix-command 'gnus-group-list-map)
3091   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3092   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3093   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3094   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3095   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3096   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3097   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3098   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3099   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3100
3101   (define-prefix-command 'gnus-group-sub-map)
3102   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3103   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3104   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3105   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3106   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3107   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3108   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3109   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3110
3111 (defun gnus-group-mode ()
3112   "Major mode for reading news.
3113
3114 All normal editing commands are switched off.
3115 \\<gnus-group-mode-map>
3116 The group buffer lists (some of) the groups available.  For instance,
3117 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3118 lists all zombie groups. 
3119
3120 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3121 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3122
3123 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3124
3125 The following commands are available:
3126
3127 \\{gnus-group-mode-map}"
3128   (interactive)
3129   (if gnus-visual (gnus-group-make-menu-bar))
3130   (kill-all-local-variables)
3131   (setq mode-line-modified "-- ")
3132   (make-local-variable 'mode-line-format)
3133   (setq mode-line-format (copy-sequence mode-line-format))
3134   (and (equal (nth 3 mode-line-format) "   ")
3135        (setcar (nthcdr 3 mode-line-format) ""))
3136   (setq major-mode 'gnus-group-mode)
3137   (setq mode-name "Group")
3138   (gnus-group-set-mode-line)
3139   (setq mode-line-process nil)
3140   (use-local-map gnus-group-mode-map)
3141   (buffer-disable-undo (current-buffer))
3142   (setq truncate-lines t)
3143   (setq buffer-read-only t)
3144   (run-hooks 'gnus-group-mode-hook))
3145
3146 (defun gnus-mouse-pick-group (e)
3147   (interactive "e")
3148   (mouse-set-point e)
3149   (gnus-group-read-group nil))
3150
3151 ;;;###autoload
3152 (defun gnus-no-server (&optional arg)
3153   "Read network news.
3154 If ARG is a positive number, Gnus will use that as the
3155 startup level. If ARG is nil, Gnus will be started at level 2. 
3156 If ARG is non-nil and not a positive number, Gnus will
3157 prompt the user for the name of an NNTP server to use.
3158 As opposed to `gnus', this command will not connect to the local server."
3159   (interactive "P")
3160   (setq gnus-group-use-permanent-levels t)
3161   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3162
3163 (defalias '\(ding\) 'gnus)
3164
3165 ;;;###autoload
3166 (defun gnus (&optional arg dont-connect)
3167   "Read network news.
3168 If ARG is non-nil and a positive number, Gnus will use that as the
3169 startup level. If ARG is non-nil and not a positive number, Gnus will
3170 prompt the user for the name of an NNTP server to use."
3171   (interactive "P")
3172   (if (get-buffer gnus-group-buffer)
3173       (progn
3174         (switch-to-buffer gnus-group-buffer)
3175         (gnus-group-get-new-news))
3176     (gnus-clear-system)
3177     (nnheader-init-server-buffer)
3178     (gnus-read-init-file)
3179
3180     (let ((level (and arg (numberp arg) (> arg 0) arg))
3181           did-connect)
3182       (unwind-protect
3183           (progn
3184             (gnus-group-setup-buffer)
3185             (or dont-connect 
3186                 (setq did-connect
3187                       (gnus-start-news-server (and arg (not level))))))
3188         (if (and (not dont-connect) 
3189                  (not did-connect))
3190             (gnus-group-quit)
3191           (run-hooks 'gnus-startup-hook)
3192           ;; NNTP server is successfully open. 
3193
3194           ;; Find the current startup file name.
3195           (setq gnus-current-startup-file 
3196                 (gnus-make-newsrc-file gnus-startup-file))
3197
3198           ;; Read the dribble file.
3199           (and gnus-use-dribble-file (gnus-dribble-read-file))
3200
3201           (gnus-update-format-specifications)
3202           (gnus-summary-make-display-table)
3203           (let ((buffer-read-only nil))
3204             (erase-buffer)
3205             (if (not gnus-inhibit-startup-message)
3206                 (progn
3207                   (gnus-group-startup-message)
3208                   (sit-for 0))))
3209           (gnus-setup-news nil level)
3210           (gnus-group-list-groups level)
3211           (gnus-configure-windows 'group))))))
3212
3213 (defun gnus-unload ()
3214   "Unload all Gnus features."
3215   (interactive)
3216   (let ((history load-history)
3217         feature)
3218     (while history
3219       (and (string-match "^gnus" (car (car history)))
3220            (setq feature (cdr (assq 'provide (car history))))
3221            (unload-feature feature 'force))
3222       (setq history (cdr history)))))
3223
3224 (defun gnus-group-startup-message (&optional x y)
3225   "Insert startup message in current buffer."
3226   ;; Insert the message.
3227   (erase-buffer)
3228   (insert
3229    (format "
3230      %s
3231            A newsreader 
3232       for GNU Emacs
3233
3234         Based on GNUS 
3235              written by 
3236      Masanobu UMEDA
3237
3238        A Praxis Release
3239       larsi@ifi.uio.no
3240
3241            gnus-version))
3242   ;; And then hack it.
3243   ;; 18 is the longest line.
3244   (indent-rigidly (point-min) (point-max) 
3245                   (/ (max (- (window-width) (or x 28)) 0) 2))
3246   (goto-char (point-min))
3247   ;; +4 is fuzzy factor.
3248   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3249
3250   ;; Fontify some.
3251   (goto-char (point-min))
3252   (search-forward "Praxis")
3253   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3254   (goto-char (point-min)))
3255
3256 (defun gnus-group-setup-buffer ()
3257   (or (get-buffer gnus-group-buffer)
3258       (progn
3259         (switch-to-buffer gnus-group-buffer)
3260         (gnus-add-current-to-buffer-list)
3261         (gnus-group-mode)
3262         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3263
3264 (defun gnus-group-list-groups (level &optional unread)
3265   "List newsgroups with level LEVEL or lower that have unread articles.
3266 Default is all subscribed groups.
3267 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3268   (interactive (list (and current-prefix-arg
3269                           (prefix-numeric-value current-prefix-arg))))
3270   (if gnus-group-use-permanent-levels
3271       (progn
3272         (setq gnus-group-default-list-level 
3273               (or level gnus-group-default-list-level))
3274         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3275     (setq level (or level gnus-group-default-list-level 
3276                     gnus-level-subscribed)))
3277   (gnus-group-setup-buffer)     ;May call from out of group buffer
3278   (let ((case-fold-search nil)
3279         (group (gnus-group-group-name)))
3280     (funcall gnus-group-prepare-function level unread nil)
3281     (if (zerop (buffer-size))
3282         (gnus-message 5 gnus-no-groups-message)
3283       (goto-char (point-min))
3284       (if (not group)
3285           ;; Go to the first group with unread articles.
3286           (gnus-group-search-forward nil nil nil t)
3287         ;; Find the right group to put point on. If the current group
3288         ;; has disapeared in the new listing, try to find the next
3289         ;; one. If no next one can be found, just leave point at the
3290         ;; first newsgroup in the buffer.
3291         (if (not (gnus-goto-char
3292                   (text-property-any (point-min) (point-max) 
3293                                      'gnus-group (intern group))))
3294             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3295               (while (and newsrc
3296                           (not (gnus-goto-char 
3297                                 (text-property-any 
3298                                  (point-min) (point-max) 'gnus-group 
3299                                  (intern (car (car newsrc)))))))
3300                 (setq newsrc (cdr newsrc)))
3301               (or newsrc (progn (goto-char (point-max))
3302                                 (forward-line -1))))))
3303       ;; Adjust cursor point.
3304       (gnus-group-position-cursor))))
3305
3306 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3307   "List all newsgroups with unread articles of level LEVEL or lower.
3308 If ALL is non-nil, list groups that have no unread articles.
3309 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3310 If REGEXP, only list groups matching REGEXP."
3311   (set-buffer gnus-group-buffer)
3312   (let ((buffer-read-only nil)
3313         (newsrc (cdr gnus-newsrc-alist))
3314         (lowest (or lowest 1))
3315         info clevel unread group)
3316     (erase-buffer)
3317     (if (< lowest gnus-level-zombie)
3318         ;; List living groups.
3319         (while newsrc
3320           (setq info (car newsrc)
3321                 group (car info)
3322                 newsrc (cdr newsrc)
3323                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3324           (and unread ; This group might be bogus
3325                (or (not regexp)
3326                    (string-match regexp group))
3327                (<= (setq clevel (car (cdr info))) level) 
3328                (>= clevel lowest)
3329                (or all            ; We list all groups?
3330                    (eq unread t)  ; We list unactivated groups
3331                    (> unread 0)   ; We list groups with unread articles
3332                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3333                (gnus-group-insert-group-line 
3334                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3335
3336     ;; List dead groups.
3337     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3338          (gnus-group-prepare-flat-list-dead 
3339           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3340           gnus-level-zombie ?Z
3341           regexp))
3342     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3343          (gnus-group-prepare-flat-list-dead 
3344           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3345           gnus-level-killed ?K regexp))
3346
3347     (gnus-group-set-mode-line)
3348     (setq gnus-have-all-newsgroups all)
3349     (run-hooks 'gnus-group-prepare-hook)))
3350
3351 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3352   ;; List zombies and killed lists somehwat faster, which was
3353   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3354   ;; this by ignoring the group format specification altogether.
3355   (let (group beg)
3356     (while groups
3357       (setq group (car groups)
3358             groups (cdr groups))
3359       (if (or (not regexp)
3360               (string-match regexp group))
3361           (progn
3362             (setq beg (point))
3363             (insert (format " %c     *: %s\n" mark group))
3364             (add-text-properties 
3365              beg (1+ beg) 
3366              (list 'gnus-group (intern group)
3367                    'gnus-unread t
3368                    'gnus-level level)))))))
3369
3370 (defun gnus-group-real-name (group)
3371   "Find the real name of a foreign newsgroup."
3372   (if (string-match ":[^:]+$" group)
3373       (substring group (1+ (match-beginning 0)))
3374     group))
3375
3376 (defun gnus-group-prefixed-name (group method)
3377   "Return the whole name from GROUP and METHOD."
3378   (and (stringp method) (setq method (gnus-server-to-method method)))
3379   (concat (format "%s" (car method))
3380           (if (and 
3381                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3382                (not (string= (nth 1 method) "")))
3383               (concat "+" (nth 1 method)))
3384           ":" group))
3385
3386 (defun gnus-group-real-prefix (group)
3387   "Return the prefix of the current group name."
3388   (if (string-match "^[^:]+:" group)
3389       (substring group 0 (match-end 0))
3390     ""))
3391
3392 (defun gnus-group-method-name (group)
3393   "Return the method used for selecting GROUP."
3394   (let ((prefix (gnus-group-real-prefix group)))
3395     (if (equal prefix "")
3396         gnus-select-method
3397       (if (string-match "^[^\\+]+\\+" prefix)
3398           (list (intern (substring prefix 0 (1- (match-end 0))))
3399                 (substring prefix (match-end 0) (1- (length prefix))))
3400         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3401
3402 (defun gnus-group-foreign-p (group)
3403   "Return nil if GROUP is native, non-nil if it is foreign."
3404   (string-match ":" group))
3405
3406 (defun gnus-group-set-info (info &optional method-only-group part)
3407   (let* ((entry (gnus-gethash
3408                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3409          (part-info info)
3410          (info (if method-only-group (nth 2 entry) info)))
3411     (if (not method-only-group)
3412         ()
3413       (or entry
3414           (error "Trying to change non-existent group %s" method-only-group))
3415       ;; We have recevied parts of the actual group info - either the
3416       ;; select method or the group parameters.  We first check
3417       ;; whether we have to extend the info, and if so, do that.
3418       (let ((len (length info))
3419             (total (if (eq part 'method) 5 6)))
3420         (and (< len total)
3421              (setcdr (nthcdr (1- len) info)
3422                      (make-list (- total len) nil)))
3423         ;; Then we enter the new info.
3424         (setcar (nthcdr (1- total) info) part-info)))
3425     ;; We uncompress some lists of marked articles.
3426     (let (marked)
3427       (if (not (setq marked (nth 3 info)))
3428           ()
3429         (while marked
3430           (or (eq 'score (car (car marked)))
3431               (eq 'bookmark (car (car marked)))
3432               (eq 'killed (car (car marked)))
3433               (setcdr (car marked) 
3434                       (gnus-uncompress-range (cdr (car marked)))))
3435           (setq marked (cdr marked)))))
3436     (if entry
3437         ()
3438       ;; This is a new group, so we just create it.
3439       (save-excursion
3440         (set-buffer gnus-group-buffer)
3441         (if (nth 4 info)
3442             ;; It's a foreign group...
3443             (gnus-group-make-group 
3444              (gnus-group-real-name (car info))
3445              (prin1-to-string (car (nth 4 info)))
3446              (nth 1 (nth 4 info)))
3447           ;; It's a native group.
3448           (gnus-group-make-group
3449            (car info)
3450            (prin1-to-string (car gnus-select-method))
3451            (nth 1 gnus-select-method)))
3452         (gnus-message 6 "Note: New group created")
3453         (setq entry 
3454               (gnus-gethash (gnus-group-prefixed-name 
3455                              (gnus-group-real-name (car info))
3456                              (or (nth 4 info) gnus-select-method))
3457                             gnus-newsrc-hashtb))))
3458     ;; Whether it was a new group or not, we now have the entry, so we
3459     ;; can do the update.
3460     (if entry
3461         (progn
3462           (setcar (nthcdr 2 entry) info)
3463           (if (and (not (eq (car entry) t)) 
3464                    (gnus-gethash (car info) gnus-active-hashtb))
3465               (let ((marked (nth 3 info)))
3466                 (setcar entry 
3467                         (max 0 (- (length (gnus-list-of-unread-articles 
3468                                            (car info)))
3469                                   (length (cdr (assq 'tick marked)))
3470                                   (length (cdr (assq 'dormant marked)))))))))
3471       (error "No such group: %s" (car info)))))
3472
3473 (defun gnus-group-set-method-info (group select-method)
3474   (gnus-group-set-info select-method group 'method))
3475
3476 (defun gnus-group-set-params-info (group params)
3477   (gnus-group-set-info params group 'params))
3478
3479 (defun gnus-group-update-group-line ()
3480   "This function updates the current line in the newsgroup buffer and
3481 moves the point to the colon."
3482   (let* ((buffer-read-only nil)
3483          (group (gnus-group-group-name))
3484          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3485     (if entry
3486         (gnus-dribble-enter 
3487          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3488                  ")")))
3489     (beginning-of-line)
3490     (delete-region (point) (progn (forward-line 1) (point)))
3491     (gnus-group-insert-group-line-info group)
3492     (forward-line -1)
3493     (gnus-group-position-cursor)))
3494
3495 (defun gnus-group-insert-group-line-info (group)
3496   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3497         active info)
3498     (if entry
3499         (progn
3500           (setq info (nth 2 entry))
3501           (gnus-group-insert-group-line 
3502            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3503       (setq active (gnus-gethash group gnus-active-hashtb))
3504       (gnus-group-insert-group-line 
3505        nil group 
3506        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3507        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3508
3509 (defun gnus-group-insert-group-line (gformat group level marked number method)
3510   (let* ((gformat (or gformat gnus-group-line-format-spec))
3511          (active (gnus-gethash group gnus-active-hashtb))
3512          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3513          (number-of-dormant (length (cdr (assq 'dormant marked))))
3514          (number-of-ticked (length (cdr (assq 'tick marked))))
3515          (number-of-ticked-and-dormant
3516           (+ number-of-ticked number-of-dormant))
3517          (number-of-unread-unticked 
3518           (if (numberp number) (int-to-string (max 0 number))
3519             "*"))
3520          (number-of-read
3521           (if (numberp number)
3522               (max 0 (- number-total number))
3523             "*"))
3524          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3525                            ((<= level gnus-level-unsubscribed) ?U)
3526                            ((= level gnus-level-zombie) ?Z)
3527                            (t ?K)))
3528          (qualified-group (gnus-group-real-name group))
3529          (newsgroup-description 
3530           (if gnus-description-hashtb
3531               (or (gnus-gethash group gnus-description-hashtb) "")
3532             ""))
3533          (moderated (if (member group gnus-moderated-list) ?m ? ))
3534          (moderated-string (if (eq moderated ?m) "(m)" ""))
3535          (method (gnus-server-get-method group method))
3536          (news-server (or (car (cdr method)) ""))
3537          (news-method (or (car method) ""))
3538          (news-method-string 
3539           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3540          (marked (if (and 
3541                       (numberp number) 
3542                       (zerop number)
3543                       (> number-of-ticked 0))
3544                      ?* ? ))
3545          (number (if (eq number t) "*" (+ number number-of-dormant 
3546                                           number-of-ticked)))
3547          (process-marked (if (member qualified-group gnus-group-marked)
3548                              gnus-process-mark ? ))
3549          (buffer-read-only nil)
3550          header ; passed as parameter to user-funcs.
3551          b)
3552     (beginning-of-line)
3553     (setq b (point))
3554     ;; Insert the text.
3555     (insert (eval gformat))
3556
3557     (add-text-properties 
3558      b (1+ b) (list 'gnus-group (intern group)
3559                     'gnus-unread (if (numberp number)
3560                                      (string-to-int number-of-unread-unticked)
3561                                    t)
3562                     'gnus-marked marked
3563                     'gnus-level level))))
3564
3565 (defun gnus-group-update-group (group &optional visible-only)
3566   "Update newsgroup info of GROUP.
3567 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3568   (save-excursion
3569     (set-buffer gnus-group-buffer)
3570     (let ((buffer-read-only nil)
3571           visible)
3572       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3573         (if entry
3574             (gnus-dribble-enter 
3575              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3576                      ")"))))
3577       ;; Buffer may be narrowed.
3578       (save-restriction
3579         (widen)
3580         ;; Search a line to modify.  If the buffer is large, the search
3581         ;; takes long time.  In most cases, current point is on the line
3582         ;; we are looking for.  So, first of all, check current line. 
3583         (if (or (progn
3584                   (beginning-of-line)
3585                   (eq (get-text-property (point) 'gnus-group)
3586                       (intern group)))
3587                 (progn
3588                   (gnus-goto-char 
3589                    (text-property-any 
3590                     (point-min) (point-max) 'gnus-group (intern group)))))
3591             ;; GROUP is listed in current buffer. So, delete old line.
3592             (progn
3593               (setq visible t)
3594               (beginning-of-line)
3595               (delete-region (point) (progn (forward-line 1) (point))))
3596           ;; No such line in the buffer, find out where it's supposed to
3597           ;; go, and insert it there (or at the end of the buffer).
3598           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3599           (or visible-only
3600               (let ((entry 
3601                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3602                 (while (and entry
3603                             (car entry)
3604                             (not
3605                              (gnus-goto-char
3606                               (text-property-any
3607                                (point-min) (point-max) 
3608                                'gnus-group (intern (car (car entry)))))))
3609                   (setq entry (cdr entry)))
3610                 (or entry (goto-char (point-max)))))))
3611       (if (or visible (not visible-only))
3612           (gnus-group-insert-group-line-info group))
3613       (gnus-group-set-mode-line))))
3614
3615 (defun gnus-group-set-mode-line ()
3616   (if (memq 'group gnus-updated-mode-lines)
3617       (let* ((gformat (or gnus-group-mode-line-format-spec
3618                           (setq gnus-group-mode-line-format-spec
3619                                 (gnus-parse-format 
3620                                  gnus-group-mode-line-format 
3621                                  gnus-group-mode-line-format-alist))))
3622              (news-server (car (cdr gnus-select-method)))
3623              (news-method (car gnus-select-method))
3624              (max-len 60)
3625              (mode-string (eval gformat)))
3626         (setq mode-string (eval gformat))
3627         (if (> (length mode-string) max-len) 
3628             (setq mode-string (substring mode-string 0 (- max-len 4))))
3629         (setq mode-line-buffer-identification mode-string)
3630         (set-buffer-modified-p t))))
3631
3632 (defun gnus-group-group-name ()
3633   "Get the name of the newsgroup on the current line."
3634   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3635     (and group (symbol-name group))))
3636
3637 (defun gnus-group-group-level ()
3638   "Get the level of the newsgroup on the current line."
3639   (get-text-property (gnus-point-at-bol) 'gnus-level))
3640
3641 (defun gnus-group-group-unread ()
3642   "Get the number of unread articles of the newsgroup on the current line."
3643   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3644
3645 (defun gnus-group-search-forward (&optional backward all level first-too)
3646   "Find the next newsgroup with unread articles.
3647 If BACKWARD is non-nil, find the previous newsgroup instead.
3648 If ALL is non-nil, just find any newsgroup.
3649 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3650 group exists.
3651 If FIRST-TOO, the current line is also eligible as a target."
3652   (let ((way (if backward -1 1))
3653         (low gnus-level-killed)
3654         (beg (point))
3655         pos found lev)
3656     (if (and backward (progn (beginning-of-line)) (bobp))
3657         nil
3658       (or first-too (forward-line way))
3659       (while (and 
3660               (not (eobp))
3661               (not (setq 
3662                     found 
3663                     (and (or all
3664                              (and
3665                               (let ((unread 
3666                                      (get-text-property (point) 'gnus-unread)))
3667                                 (or (eq unread t) (and unread (> unread 0))))
3668                               (setq lev (get-text-property (point)
3669                                                            'gnus-level))
3670                               (<= lev gnus-level-subscribed)))
3671                          (or (not level)
3672                              (and (setq lev (get-text-property (point)
3673                                                                'gnus-level))
3674                                   (or (= lev level)
3675                                       (and (< lev low)
3676                                            (< level lev)
3677                                            (progn
3678                                              (setq low lev)
3679                                              (setq pos (point))
3680                                              nil))))))))
3681               (zerop (forward-line way)))))
3682     (if found 
3683         (progn (gnus-group-position-cursor) t)
3684       (goto-char (or pos beg))
3685       (and pos t))))
3686
3687 ;;; Gnus group mode commands
3688
3689 ;; Group marking.
3690
3691 (defun gnus-group-mark-group (n &optional unmark no-advance)
3692   "Mark the current group."
3693   (interactive "p")
3694   (let ((buffer-read-only nil)
3695         group)
3696     (while 
3697         (and (> n 0) 
3698              (setq group (gnus-group-group-name))
3699              (progn
3700                (beginning-of-line)
3701                (forward-char 
3702                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3703                (delete-char 1)
3704                (if unmark
3705                    (progn
3706                      (insert " ")
3707                      (setq gnus-group-marked (delete group gnus-group-marked)))
3708                  (insert "#")
3709                  (setq gnus-group-marked
3710                        (cons group (delete group gnus-group-marked))))
3711                t)
3712              (or no-advance (zerop (gnus-group-next-group 1))))
3713       (setq n (1- n)))
3714     (gnus-summary-position-cursor)
3715     n))
3716
3717 (defun gnus-group-unmark-group (n)
3718   "Remove the mark from the current group."
3719   (interactive "p")
3720   (gnus-group-mark-group n 'unmark))
3721
3722 (defun gnus-group-mark-region (unmark beg end)
3723   "Mark all groups between point and mark.
3724 If UNMARK, remove the mark instead."
3725   (interactive "P\nr")
3726   (let ((num (count-lines beg end)))
3727     (save-excursion
3728       (goto-char beg)
3729       (- num (gnus-group-mark-group num unmark)))))
3730
3731 (defun gnus-group-remove-mark (group)
3732   (and (gnus-group-goto-group group)
3733        (save-excursion
3734          (gnus-group-mark-group 1 'unmark t))))
3735
3736 ;; Return a list of groups to work on.  Take into consideration N (the
3737 ;; prefix) and the list of marked groups.
3738 (defun gnus-group-process-prefix (n)
3739   (cond (n
3740          (setq n (prefix-numeric-value n))
3741          ;; There is a prefix, so we return a list of the N next
3742          ;; groups. 
3743          (let ((way (if (< n 0) -1 1))
3744                (n (abs n))
3745                group groups)
3746            (save-excursion
3747              (while (and (> n 0)
3748                          (setq group (gnus-group-group-name)))
3749                (setq groups (cons group groups))
3750                (setq n (1- n))
3751                (forward-line way)))
3752            (nreverse groups)))
3753         (gnus-group-marked
3754          ;; No prefix, but a list of marked articles.
3755          (reverse gnus-group-marked))
3756         (t
3757          ;; Neither marked articles or a prefix, so we return the
3758          ;; current group.
3759          (let ((group (gnus-group-group-name)))
3760            (and group (list group))))))
3761
3762 ;; Selecting groups.
3763
3764 (defun gnus-group-read-group (&optional all no-article group)
3765   "Read news in this newsgroup.
3766 If the prefix argument ALL is non-nil, already read articles become
3767 readable. If the optional argument NO-ARTICLE is non-nil, no article
3768 will be auto-selected upon group entry."
3769   (interactive "P")
3770   (let ((group (or group (gnus-group-group-name)))
3771         number active marked entry)
3772     (or group (error "No group on current line"))
3773     (setq marked 
3774           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3775     ;; This group might be a dead group. In that case we have to get
3776     ;; the number of unread articles from `gnus-active-hashtb'.
3777     (if entry
3778         (setq number (car entry))
3779       (if (setq active (gnus-gethash group gnus-active-hashtb))
3780           (setq number (- (1+ (cdr active)) (car active)))))
3781     (gnus-summary-read-group 
3782      group (or all (and (numberp number) 
3783                         (zerop (+ number (length (cdr (assq 'tick marked)))
3784                                   (length (cdr (assq 'dormant marked)))))))
3785      no-article)))
3786
3787 (defun gnus-group-select-group (&optional all)
3788   "Select this newsgroup.
3789 No article is selected automatically.
3790 If argument ALL is non-nil, already read articles become readable."
3791   (interactive "P")
3792   (gnus-group-read-group all t))
3793
3794 ;; Enter a group that is not in the group buffer. Non-nil is returned
3795 ;; if selection was successful.
3796 (defun gnus-group-read-ephemeral-group 
3797   (group method &optional activate quit-config)
3798   (let ((group (if (gnus-group-foreign-p group) group
3799                  (gnus-group-prefixed-name group method))))
3800     (gnus-sethash 
3801      group
3802      (list t nil (list group gnus-level-default-subscribed nil nil 
3803                        (append method
3804                                (list
3805                                 (list 'quit-config 
3806                                       (if quit-config quit-config
3807                                         (cons (current-buffer) 'summary)))))))
3808      gnus-newsrc-hashtb)
3809     (set-buffer gnus-group-buffer)
3810     (or (gnus-server-opened method)
3811         (gnus-open-server method)
3812         (error "Unable to contact server: %s" (gnus-status-message method)))
3813     (if activate (or (gnus-request-group group)
3814                      (error "Couldn't request group")))
3815     (condition-case ()
3816         (gnus-group-read-group t t group)
3817       (error nil)
3818       (quit nil))
3819     (not (equal major-mode 'gnus-group-mode))))
3820   
3821 (defun gnus-group-jump-to-group (group)
3822   "Jump to newsgroup GROUP."
3823   (interactive 
3824    (list (completing-read 
3825           "Group: " gnus-active-hashtb nil 
3826           (memq gnus-select-method gnus-have-read-active-file))))
3827
3828   (if (equal group "")
3829       (error "Empty group name"))
3830
3831   (let ((b (text-property-any 
3832             (point-min) (point-max) 'gnus-group (intern group))))
3833     (if b
3834         ;; Either go to the line in the group buffer...
3835         (goto-char b)
3836       ;; ... or insert the line.
3837       (or
3838        (gnus-gethash group gnus-active-hashtb)
3839        (gnus-activate-newsgroup group)
3840        (error "%s error: %s" group (gnus-status-message group)))
3841
3842       (gnus-group-update-group group)
3843       (goto-char (text-property-any 
3844                   (point-min) (point-max) 'gnus-group (intern group)))))
3845   ;; Adjust cursor point.
3846   (gnus-group-position-cursor))
3847
3848 (defun gnus-group-goto-group (group)
3849   "Goto to newsgroup GROUP."
3850   (let ((b (text-property-any (point-min) (point-max) 
3851                               'gnus-group (intern group))))
3852     (and b (goto-char b))))
3853
3854 (defun gnus-group-next-group (n)
3855   "Go to next N'th newsgroup.
3856 If N is negative, search backward instead.
3857 Returns the difference between N and the number of skips actually
3858 done."
3859   (interactive "p")
3860   (gnus-group-next-unread-group n t))
3861
3862 (defun gnus-group-next-unread-group (n &optional all level)
3863   "Go to next N'th unread newsgroup.
3864 If N is negative, search backward instead.
3865 If ALL is non-nil, choose any newsgroup, unread or not.
3866 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3867 such group can be found, the next group with a level higher than
3868 LEVEL.
3869 Returns the difference between N and the number of skips actually
3870 made."
3871   (interactive "p")
3872   (let ((backward (< n 0))
3873         (n (abs n)))
3874     (while (and (> n 0)
3875                 (gnus-group-search-forward 
3876                  backward (or (not gnus-group-goto-unread) all) level))
3877       (setq n (1- n)))
3878     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3879                                (if level " on this level or higher" "")))
3880     n))
3881
3882 (defun gnus-group-prev-group (n)
3883   "Go to previous N'th newsgroup.
3884 Returns the difference between N and the number of skips actually
3885 done."
3886   (interactive "p")
3887   (gnus-group-next-unread-group (- n) t))
3888
3889 (defun gnus-group-prev-unread-group (n)
3890   "Go to previous N'th unread newsgroup.
3891 Returns the difference between N and the number of skips actually
3892 done."  
3893   (interactive "p")
3894   (gnus-group-next-unread-group (- n)))
3895
3896 (defun gnus-group-next-unread-group-same-level (n)
3897   "Go to next N'th unread newsgroup on the same level.
3898 If N is negative, search backward instead.
3899 Returns the difference between N and the number of skips actually
3900 done."
3901   (interactive "p")
3902   (gnus-group-next-unread-group n t (gnus-group-group-level))
3903   (gnus-group-position-cursor))
3904
3905 (defun gnus-group-prev-unread-group-same-level (n)
3906   "Go to next N'th unread newsgroup on the same level.
3907 Returns the difference between N and the number of skips actually
3908 done."
3909   (interactive "p")
3910   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3911   (gnus-group-position-cursor))
3912
3913 (defun gnus-group-best-unread-group (&optional exclude-group)
3914   "Go to the group with the highest level.
3915 If EXCLUDE-GROUP, do not go to that group."
3916   (interactive)
3917   (goto-char (point-min))
3918   (let ((best 100000)
3919         unread best-point)
3920     (while (setq unread (get-text-property (point) 'gnus-unread))
3921       (if (and (numberp unread) (> unread 0))
3922           (progn
3923             (if (and (< (get-text-property (point) 'gnus-level) best)
3924                      (or (not exclude-group)
3925                          (not (equal exclude-group (gnus-group-group-name)))))
3926                 (progn 
3927                   (setq best (get-text-property (point) 'gnus-level))
3928                   (setq best-point (point))))))
3929       (forward-line 1))
3930     (if best-point (goto-char best-point))
3931     (gnus-summary-position-cursor)
3932     (and best-point (gnus-group-group-name))))
3933
3934 (defun gnus-group-first-unread-group ()
3935   "Go to the first group with unread articles."
3936   (interactive)
3937   (goto-char (point-min))
3938   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3939       (gnus-group-next-unread-group 1))
3940   (gnus-group-position-cursor))
3941
3942 (defun gnus-group-enter-server-mode ()
3943   "Jump to the server buffer."
3944   (interactive)
3945   (gnus-server-setup-buffer)
3946   (gnus-configure-windows 'server)
3947   (gnus-server-prepare))
3948
3949 (defun gnus-group-make-group (name method &optional address)
3950   "Add a new newsgroup.
3951 The user will be prompted for a NAME, for a select METHOD, and an
3952 ADDRESS."
3953   (interactive
3954    (cons 
3955     (read-string "Group name: ")
3956     (let ((method
3957            (completing-read 
3958             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3959             nil t)))
3960       (if (assoc method gnus-valid-select-methods)
3961           (list method
3962                 (if (memq 'prompt-address
3963                           (assoc method gnus-valid-select-methods))
3964                     (read-string "Address: ")
3965                   ""))
3966         (list method nil)))))
3967   
3968   (let* ((meth (if address (list (intern method) address) method))
3969          (nname (gnus-group-prefixed-name name meth))
3970          info)
3971     (and (gnus-gethash nname gnus-newsrc-hashtb)
3972          (error "Group %s already exists" nname))
3973     (gnus-group-change-level 
3974      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3975      gnus-level-default-subscribed gnus-level-killed 
3976      (and (gnus-group-group-name)
3977           (gnus-gethash (gnus-group-group-name)
3978                         gnus-newsrc-hashtb))
3979      t)
3980     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3981     (gnus-dribble-enter 
3982      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3983     (gnus-group-insert-group-line-info nname)
3984
3985     (if (assoc method gnus-valid-select-methods)
3986         (require (intern method)))
3987     (and (gnus-check-backend-function 'request-create-group nname)
3988          (gnus-request-create-group nname))))
3989
3990 (defun gnus-group-edit-group (group &optional part)
3991   "Edit the group on the current line."
3992   (interactive (list (gnus-group-group-name)))
3993   (let ((done-func '(lambda () 
3994                       "Exit editing mode and update the information."
3995                       (interactive)
3996                       (gnus-group-edit-group-done 'part 'group)))
3997         (part (or part 'info))
3998         (winconf (current-window-configuration))
3999         info)
4000     (or group (error "No group on current line"))
4001     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4002         (error "Killed group; can't be edited"))
4003     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4004     (gnus-configure-windows 'edit-group)
4005     (gnus-add-current-to-buffer-list)
4006     (emacs-lisp-mode)
4007     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4008     (use-local-map (copy-keymap emacs-lisp-mode-map))
4009     (local-set-key "\C-c\C-c" done-func)
4010     (make-local-variable 'gnus-prev-winconf)
4011     (setq gnus-prev-winconf winconf)
4012     ;; We modify the func to let it know what part it is editing.
4013     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4014     (setcar (cdr (cdr (nth 4 done-func))) group)
4015     (erase-buffer)
4016     (insert
4017      (cond 
4018       ((eq part 'method)
4019        ";; Type `C-c C-c' after editing the select method.\n\n")
4020       ((eq part 'params)
4021        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4022       ((eq part 'info)
4023        ";; Type `C-c C-c' after editing the group info.\n\n")))
4024     (let ((cinfo (gnus-copy-sequence info))
4025           marked)
4026       (if (not (setq marked (nth 3 cinfo)))
4027           ()
4028         (while marked
4029           (or (eq 'score (car (car marked)))
4030               (eq 'bookmark (car (car marked)))
4031               (eq 'killed (car (car marked)))
4032               (not (numberp (car (cdr (car marked)))))
4033               (setcdr (car marked) 
4034                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4035           (setq marked (cdr marked))))
4036       (insert 
4037        (pp-to-string
4038         (cond ((eq part 'method)
4039                (or (nth 4 info) "native"))
4040               ((eq part 'params)
4041                (nth 5 info))
4042               (t
4043                cinfo)))
4044        "\n"))))
4045
4046 (defun gnus-group-edit-group-method (group)
4047   "Edit the select method of GROUP."
4048   (interactive (list (gnus-group-group-name)))
4049   (gnus-group-edit-group group 'method))
4050
4051 (defun gnus-group-edit-group-parameters (group)
4052   "Edit the group parameters of GROUP."
4053   (interactive (list (gnus-group-group-name)))
4054   (gnus-group-edit-group group 'params))
4055
4056 (defun gnus-group-edit-group-done (part group)
4057   "Get info from buffer, update variables and jump to the group buffer."
4058   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4059   (goto-char (point-min))
4060   (let ((form (read (current-buffer)))
4061         (winconf gnus-prev-winconf))
4062     (if (eq part 'info) 
4063         (gnus-group-set-info form)
4064       (gnus-group-set-info form group part))
4065     (kill-buffer (current-buffer))
4066     (and winconf (set-window-configuration winconf))
4067     (set-buffer gnus-group-buffer)
4068     (gnus-group-update-group (gnus-group-group-name))
4069     (gnus-group-position-cursor)))
4070
4071 (defun gnus-group-make-help-group ()
4072   "Create the (ding) Gnus documentation group."
4073   (interactive)
4074   (let ((path load-path)
4075         name)
4076     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4077                                    "gnus-help" '(nndoc "gnus-help")))
4078                        gnus-newsrc-hashtb)
4079          (error "Documentation group already exists"))
4080     (while (and path
4081                 (not (file-exists-p (concat (file-name-as-directory (car path))
4082                                             "doc.txt"))))
4083       (setq path (cdr path)))
4084     (or path (error "Couldn't find doc group"))
4085     (gnus-group-make-group 
4086      (gnus-group-real-name name)
4087      (list 'nndoc name
4088            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4089            (list 'nndoc-article-type 'mbox))))
4090   (gnus-group-position-cursor))
4091
4092 (defun gnus-group-make-doc-group (file type)
4093   "Create a group that uses a single file as the source."
4094   (interactive 
4095    (list (read-file-name "File name: ") 
4096          (let ((err "")
4097                found char)
4098            (while (not found)
4099              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4100              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4101                                ((= char ?b) 'babyl)
4102                                ((= char ?d) 'digest)
4103                                (t (setq err (format "%c unknown. " char))
4104                                   nil))))
4105            found)))
4106   (let* ((file (expand-file-name file))
4107          (name (gnus-generate-new-group-name
4108                 (gnus-group-prefixed-name
4109                  (file-name-nondirectory file) '(nndoc "")))))
4110     (gnus-group-make-group 
4111      (gnus-group-real-name name)
4112      (list 'nndoc name
4113            (list 'nndoc-address file)
4114            (list 'nndoc-article-type type)))))
4115
4116 (defun gnus-group-make-archive-group ()
4117   "Create the (ding) Gnus archive group."
4118   (interactive)
4119   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4120                      gnus-newsrc-hashtb)
4121        (error "Archive group already exists"))
4122   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4123   (gnus-group-position-cursor))
4124
4125 (defun gnus-group-make-directory-group (dir)
4126   "Create an nndir group.
4127 The user will be prompted for a directory. The contents of this
4128 directory will be used as a newsgroup. The directory should contain
4129 mail messages or news articles in files that have numeric names."
4130   (interactive
4131    (list (read-file-name "Create group from directory: ")))
4132   (or (file-exists-p dir) (error "No such directory"))
4133   (or (file-directory-p dir) (error "Not a directory"))
4134   (gnus-group-make-group dir "nndir" dir)
4135   (gnus-group-position-cursor))
4136
4137 (defun gnus-group-make-kiboze-group (group address scores)
4138   "Create an nnkiboze group.
4139 The user will be prompted for a name, a regexp to match groups, and
4140 score file entries for articles to include in the group."
4141   (interactive
4142    (list
4143     (read-string "nnkiboze group name: ")
4144     (read-string "Source groups (regexp): ")
4145     (let ((headers (mapcar (lambda (group) (list group))
4146                            '("subject" "from" "number" "date" "message-id"
4147                              "references" "chars" "lines" "xref")))
4148           scores header regexp regexps)
4149       (while (not (equal "" (setq header (completing-read 
4150                                           "Match on header: " headers nil t))))
4151         (setq regexps nil)
4152         (while (not (equal "" (setq regexp (read-string 
4153                                             (format "Match on %s (string): "
4154                                                     header)))))
4155           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4156         (setq scores (cons (cons header regexps) scores)))
4157       scores)))
4158   (gnus-group-make-group group "nnkiboze" address)
4159   (save-excursion
4160     (gnus-set-work-buffer)
4161     (let (emacs-lisp-mode-hook)
4162       (pp scores (current-buffer)))
4163     (write-region (point-min) (point-max) 
4164                   (concat (or gnus-kill-files-directory "~/News")
4165                           "nnkiboze:" group "." gnus-score-file-suffix)))
4166   (gnus-group-position-cursor))
4167
4168 (defun gnus-group-add-to-virtual (n vgroup)
4169   "Add the current group to a virtual group."
4170   (interactive
4171    (list current-prefix-arg
4172          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4173                           "nnvirtual:")))
4174   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4175       (error "%s is not an nnvirtual group" vgroup))
4176   (let* ((groups (gnus-group-process-prefix n))
4177          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4178     (setcar (cdr method)
4179             (concat 
4180              (nth 1 method) "\\|"
4181              (mapconcat 
4182               (lambda (s) 
4183                 (gnus-group-remove-mark s)
4184                 (concat "\\(^" (regexp-quote s) "$\\)"))
4185               groups "\\|"))))
4186   (gnus-group-position-cursor))
4187
4188 (defun gnus-group-make-empty-virtual (group)
4189   "Create a new, fresh, empty virtual group."
4190   (interactive "sCreate new, empty virtual group: ")
4191   (let* ((method (list 'nnvirtual "^$"))
4192          (pgroup (gnus-group-prefixed-name group method)))
4193     ;; Check whether it exists already.
4194     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4195          (error "Group %s already exists." pgroup))
4196     ;; Subscribe the new group after the group on the current line.
4197     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4198     (gnus-group-update-group pgroup)
4199     (forward-line -1)
4200     (gnus-group-position-cursor)))
4201
4202 (defun gnus-group-enter-directory (dir)
4203   "Enter an ephemeral nneething group."
4204   (interactive "DDirectory to read: ")
4205   (let* ((method (list 'nneething dir))
4206          (leaf (gnus-group-prefixed-name
4207                 (file-name-nondirectory (directory-file-name dir))
4208                 method))
4209          (name (gnus-generate-new-group-name leaf)))
4210     (let ((nneething-read-only t))
4211       (or (gnus-group-read-ephemeral-group 
4212            name method t
4213            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4214                                       'summary 'group)))
4215           (error "Couldn't enter %s" dir)))))
4216
4217 ;; Group sorting commands
4218 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4219
4220 (defun gnus-group-sort-groups ()
4221   "Sort the group buffer using `gnus-group-sort-function'."
4222   (interactive)
4223   (setq gnus-newsrc-alist 
4224         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4225   (gnus-make-hashtable-from-newsrc-alist)
4226   (gnus-group-list-groups (if gnus-have-all-newsgroups gnus-level-unsubscribed)
4227                           gnus-have-all-newsgroups))
4228
4229 (defun gnus-group-sort-by-alphabet (info1 info2)
4230   (string< (car info1) (car info2)))
4231
4232 (defun gnus-group-sort-by-unread (info1 info2)
4233   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4234         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4235     (< (or (and (numberp n1) n1) 0)
4236        (or (and (numberp n2) n2) 0))))
4237
4238 (defun gnus-group-sort-by-level (info1 info2)
4239   (< (nth 1 info1) (nth 1 info2)))
4240
4241 ;; Group catching up.
4242
4243 (defun gnus-group-catchup-current (&optional n all)
4244   "Mark all articles not marked as unread in current newsgroup as read.
4245 If prefix argument N is numeric, the ARG next newsgroups will be
4246 caught up. If ALL is non-nil, marked articles will also be marked as
4247 read. Cross references (Xref: header) of articles are ignored.
4248 The difference between N and actual number of newsgroups that were
4249 caught up is returned."
4250   (interactive "P")
4251   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4252                gnus-expert-user
4253                (gnus-y-or-n-p
4254                 (if all
4255                     "Do you really want to mark all articles as read? "
4256                   "Mark all unread articles as read? "))))
4257       n
4258     (let ((groups (gnus-group-process-prefix n))
4259           (ret 0))
4260       (while groups
4261         ;; Virtual groups have to be given special treatment. 
4262         (let ((method (gnus-find-method-for-group (car groups))))
4263           (if (eq 'nnvirtual (car method))
4264               (nnvirtual-catchup-group
4265                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4266         (gnus-group-remove-mark (car groups))
4267         (if (prog1
4268                 (gnus-group-goto-group (car groups))
4269               (gnus-group-catchup (car groups) all))
4270             (gnus-group-update-group-line)
4271           (setq ret (1+ ret)))
4272         (setq groups (cdr groups)))
4273       (gnus-group-next-unread-group 1)
4274       ret)))
4275
4276 (defun gnus-group-catchup-current-all (&optional n)
4277   "Mark all articles in current newsgroup as read.
4278 Cross references (Xref: header) of articles are ignored."
4279   (interactive "P")
4280   (gnus-group-catchup-current n 'all))
4281
4282 (defun gnus-group-catchup (group &optional all)
4283   "Mark all articles in GROUP as read.
4284 If ALL is non-nil, all articles are marked as read.
4285 The return value is the number of articles that were marked as read,
4286 or nil if no action could be taken."
4287   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4288          (num (car entry))
4289          (marked (nth 3 (nth 2 entry))))
4290     (if (not (numberp (car entry)))
4291         (gnus-message 1 "Can't catch up; non-active group")
4292       ;; Do the updating only if the newsgroup isn't killed.
4293       (if (not entry)
4294           ()
4295         (gnus-update-read-articles 
4296          group (and (not all) (append (cdr (assq 'tick marked))
4297                                       (cdr (assq 'dormant marked))))
4298          nil (and (not all) (cdr (assq 'tick marked))))
4299         (and all marked
4300              (setcar (nthcdr 3 (nth 2 entry)) 
4301                      (delq (assq 'dormant marked) 
4302                            (nth 3 (nth 2 entry)))))))
4303     num))
4304
4305 (defun gnus-group-expire-articles (&optional n)
4306   "Expire all expirable articles in the current newsgroup."
4307   (interactive "P")
4308   (let ((groups (gnus-group-process-prefix n))
4309         group)
4310     (or groups (error "No groups to expire"))
4311     (while groups
4312       (setq group (car groups)
4313             groups (cdr groups))
4314       (gnus-group-remove-mark group)
4315       (if (not (gnus-check-backend-function 'request-expire-articles group))
4316           ()
4317         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4318                (expirable (if (memq 'total-expire (nth 5 info))
4319                               (cons nil (gnus-list-of-read-articles group))
4320                             (assq 'expire (nth 3 info)))))
4321           (and expirable 
4322                (setcdr expirable
4323                        (gnus-request-expire-articles 
4324                         (cdr expirable) group))))))))
4325
4326 (defun gnus-group-expire-all-groups ()
4327   "Expire all expirable articles in all newsgroups."
4328   (interactive)
4329   (save-excursion
4330     (gnus-message 5 "Expiring...")
4331     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4332                                      (cdr gnus-newsrc-alist))))
4333       (gnus-group-expire-articles nil)))
4334   (gnus-group-position-cursor)
4335   (gnus-message 5 "Expiring...done"))
4336
4337 (defun gnus-group-set-current-level (n level)
4338   "Set the level of the next N groups to LEVEL."
4339   (interactive "P\nnLevel: ")
4340   (or (and (>= level 1) (<= level gnus-level-killed))
4341       (error "Illegal level: %d" level))
4342   (let ((groups (gnus-group-process-prefix n))
4343         group)
4344     (while groups
4345       (setq group (car groups)
4346             groups (cdr groups))
4347       (gnus-group-remove-mark group)
4348       (gnus-message 6 "Changed level of %s from %d to %d" 
4349                     group (gnus-group-group-level) level)
4350       (gnus-group-change-level group level
4351                                (gnus-group-group-level))
4352       (gnus-group-update-group-line)))
4353   (gnus-group-position-cursor))
4354
4355 (defun gnus-group-unsubscribe-current-group (&optional n)
4356   "Toggle subscription of the current group.
4357 If given numerical prefix, toggle the N next groups."
4358   (interactive "P")
4359   (let ((groups (gnus-group-process-prefix n))
4360         group)
4361     (while groups
4362       (setq group (car groups)
4363             groups (cdr groups))
4364       (gnus-group-remove-mark group)
4365       (gnus-group-unsubscribe-group
4366        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4367                  gnus-level-default-unsubscribed
4368                gnus-level-default-subscribed))
4369       (gnus-group-update-group-line))
4370     (gnus-group-next-group 1)))
4371
4372 (defun gnus-group-unsubscribe-group (group &optional level)
4373   "Toggle subscribe from/to unsubscribe GROUP.
4374 New newsgroup is added to .newsrc automatically."
4375   (interactive
4376    (list (completing-read
4377           "Group: " gnus-active-hashtb nil 
4378           (memq gnus-select-method gnus-have-read-active-file))))
4379   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4380     (cond (newsrc
4381            ;; Toggle subscription flag.
4382            (gnus-group-change-level 
4383             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4384                                            gnus-level-subscribed) 
4385                                        (1+ gnus-level-subscribed)
4386                                      gnus-level-default-subscribed)))
4387            (gnus-group-update-group group))
4388           ((and (stringp group)
4389                 (or (not gnus-have-read-active-file)
4390                     (gnus-gethash group gnus-active-hashtb)))
4391            ;; Add new newsgroup.
4392            (gnus-group-change-level 
4393             group 
4394             (if level level gnus-level-default-subscribed) 
4395             (or (and (member group gnus-zombie-list) 
4396                      gnus-level-zombie) 
4397                 gnus-level-killed)
4398             (and (gnus-group-group-name)
4399                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4400            (gnus-group-update-group group))
4401           (t (error "No such newsgroup: %s" group)))
4402     (gnus-group-position-cursor)))
4403
4404 (defun gnus-group-transpose-groups (n)
4405   "Move the current newsgroup up N places.
4406 If given a negative prefix, move down instead. The difference between
4407 N and the number of steps taken is returned." 
4408   (interactive "p")
4409   (or (gnus-group-group-name)
4410       (error "No group on current line"))
4411   (gnus-group-kill-group 1)
4412   (prog1
4413       (forward-line (- n))
4414     (gnus-group-yank-group)
4415     (gnus-group-position-cursor)))
4416
4417 (defun gnus-group-kill-all-zombies ()
4418   "Kill all zombie newsgroups."
4419   (interactive)
4420   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4421   (setq gnus-zombie-list nil)
4422   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4423   (goto-char (point-min))
4424   (gnus-group-position-cursor))
4425
4426 (defun gnus-group-kill-region (begin end)
4427   "Kill newsgroups in current region (excluding current point).
4428 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4429   (interactive "r")
4430   (let ((lines
4431          ;; Count lines.
4432          (save-excursion
4433            (count-lines
4434             (progn
4435               (goto-char begin)
4436               (beginning-of-line)
4437               (point))
4438             (progn
4439               (goto-char end)
4440               (beginning-of-line)
4441               (point))))))
4442     (goto-char begin)
4443     (beginning-of-line)                 ;Important when LINES < 1
4444     (gnus-group-kill-group lines)))
4445
4446 (defun gnus-group-kill-group (&optional n)
4447   "The the next N groups.
4448 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4449 However, only groups that were alive can be yanked; already killed 
4450 groups or zombie groups can't be yanked.
4451 The return value is the name of the (last) group that was killed."
4452   (interactive "P")
4453   (let ((buffer-read-only nil)
4454         (groups (gnus-group-process-prefix n))
4455         group entry level)
4456     (while groups
4457       (setq group (car groups)
4458             groups (cdr groups))
4459       (gnus-group-remove-mark group)
4460       (setq level (gnus-group-group-level))
4461       (gnus-delete-line)
4462       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4463           (setq gnus-list-of-killed-groups 
4464                 (cons (cons (car entry) (nth 2 entry)) 
4465                       gnus-list-of-killed-groups)))
4466       (gnus-group-change-level 
4467        (if entry entry group) gnus-level-killed (if entry nil level)))
4468     (gnus-group-position-cursor)
4469     group))
4470
4471 (defun gnus-group-yank-group (&optional arg)
4472   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4473 inserting it before the current newsgroup.  The numeric ARG specifies
4474 how many newsgroups are to be yanked.  The name of the (last)
4475 newsgroup yanked is returned."
4476   (interactive "p")
4477   (if (not arg) (setq arg 1))
4478   (let (info group prev)
4479     (while (>= (setq arg (1- arg)) 0)
4480       (if (not (setq info (car gnus-list-of-killed-groups)))
4481           (error "No more newsgroups to yank"))
4482       (setq group (nth 2 info))
4483       ;; Find which newsgroup to insert this one before - search
4484       ;; backward until something suitable is found. If there are no
4485       ;; other newsgroups in this buffer, just make this newsgroup the
4486       ;; first newsgroup.
4487       (setq prev (gnus-group-group-name))
4488       (gnus-group-change-level 
4489        info (nth 2 info) gnus-level-killed 
4490        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4491        t)
4492       (gnus-group-insert-group-line-info (nth 1 info))
4493       (setq gnus-list-of-killed-groups 
4494             (cdr gnus-list-of-killed-groups)))
4495     (forward-line -1)
4496     (gnus-group-position-cursor)
4497     group))
4498       
4499 (defun gnus-group-list-all-groups (&optional arg)
4500   "List all newsgroups with level ARG or lower.
4501 Default is gnus-level-unsubscribed, which lists all subscribed and most
4502 unsubscribed groups."
4503   (interactive "P")
4504   (setq arg (or arg gnus-level-unsubscribed))
4505   (gnus-group-list-groups arg t))
4506
4507 (defun gnus-group-list-killed ()
4508   "List all killed newsgroups in the group buffer."
4509   (interactive)
4510   (if (not gnus-killed-list)
4511       (gnus-message 6 "No killed groups")
4512     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4513     (goto-char (point-min)))
4514   (gnus-group-position-cursor))
4515
4516 (defun gnus-group-list-zombies ()
4517   "List all zombie newsgroups in the group buffer."
4518   (interactive)
4519   (if (not gnus-zombie-list)
4520       (gnus-message 6 "No zombie groups")
4521     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4522     (goto-char (point-min)))
4523   (gnus-group-position-cursor))
4524
4525 (defun gnus-group-get-new-news (&optional arg)
4526   "Get newly arrived articles.
4527 If ARG is non-nil, it should be a number between one and nine to
4528 specify which levels you are interested in re-scanning."
4529   (interactive "P")
4530   (run-hooks 'gnus-get-new-news-hook)
4531   (if gnus-group-use-permanent-levels
4532       (setq arg
4533             (setq gnus-group-default-list-level 
4534                   (or arg gnus-group-default-list-level
4535                       gnus-level-subscribed))))
4536   (if (and gnus-read-active-file (not arg))
4537       (progn
4538         (gnus-read-active-file)
4539         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4540     (let ((gnus-read-active-file (not arg))
4541           (gnus-have-read-active-file 
4542            (and (not arg) gnus-have-read-active-file)))
4543       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4544   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4545                               gnus-group-default-list-level
4546                               gnus-level-subscribed)
4547                           gnus-have-all-newsgroups))
4548
4549 (defun gnus-group-get-new-news-this-group (&optional n)
4550   "Check for newly arrived news in the current group (and the N-1 next groups).
4551 The difference between N and the number of newsgroup checked is returned.
4552 If N is negative, this group and the N-1 previous groups will be checked."
4553   (interactive "P")
4554   (let* ((groups (gnus-group-process-prefix n))
4555          (ret (if (numberp n) (- n (length groups)) 0))
4556          group)
4557     (while groups
4558       (setq group (car groups)
4559             groups (cdr groups))
4560       (gnus-group-remove-mark group)
4561       (or (gnus-get-new-news-in-group group)
4562           (progn 
4563             (ding) 
4564             (message "%s error: %s" group (gnus-status-message group))
4565             (sit-for 2))))
4566     ;; !!! I don't know why the buffer scrolls forward when updating
4567     ;; the first line in the group buffer, but it does. So we set the
4568     ;; window start forcibly.
4569 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4570     (gnus-group-next-unread-group 1 t)
4571     (gnus-summary-position-cursor)
4572     ret))
4573
4574 (defun gnus-get-new-news-in-group (group)
4575   (and group 
4576        (gnus-activate-newsgroup group)
4577        (progn
4578          (gnus-get-unread-articles-in-group 
4579           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4580           (gnus-gethash group gnus-active-hashtb))
4581          (gnus-group-update-group-line)
4582          t)))
4583
4584 (defun gnus-group-fetch-faq (group)
4585   "Fetch the FAQ for the current group."
4586   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4587   (or group (error "No group name given"))
4588   (let ((file (concat gnus-group-faq-directory group))) 
4589     (if (not (file-exists-p file))
4590         (error "No such file: %s" file)
4591       (find-file file))))
4592   
4593 (defun gnus-group-describe-group (force &optional group)
4594   "Display a description of the current newsgroup."
4595   (interactive (list current-prefix-arg (gnus-group-group-name)))
4596   (and force (setq gnus-description-hashtb nil))
4597   (let ((method (gnus-find-method-for-group group))
4598         desc)
4599     (or group (error "No group name given"))
4600     (and (or (and gnus-description-hashtb
4601                   ;; We check whether this group's method has been
4602                   ;; queried for a description file.  
4603                   (gnus-gethash 
4604                    (gnus-group-prefixed-name "" method) 
4605                    gnus-description-hashtb))
4606              (setq desc (gnus-group-get-description group))
4607              (gnus-read-descriptions-file method))
4608          (message
4609           (or desc (gnus-gethash group gnus-description-hashtb)
4610               "No description available")))))
4611
4612 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4613 (defun gnus-group-describe-all-groups (&optional force)
4614   "Pop up a buffer with descriptions of all newsgroups."
4615   (interactive "P")
4616   (and force (setq gnus-description-hashtb nil))
4617   (if (not (or gnus-description-hashtb
4618                (gnus-read-all-descriptions-files)))
4619       (error "Couldn't request descriptions file"))
4620   (let ((buffer-read-only nil)
4621         b)
4622     (erase-buffer)
4623     (mapatoms
4624      (lambda (group)
4625        (setq b (point))
4626        (insert (format "      *: %-20s %s\n" (symbol-name group)
4627                        (symbol-value group)))
4628        (add-text-properties 
4629         b (1+ b) (list 'gnus-group group
4630                        'gnus-unread t 'gnus-marked nil
4631                        'gnus-level (1+ gnus-level-subscribed))))
4632      gnus-description-hashtb)
4633     (goto-char (point-min))
4634     (gnus-group-position-cursor)))
4635
4636 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4637 (defun gnus-group-apropos (regexp &optional search-description)
4638   "List all newsgroups that have names that match a regexp."
4639   (interactive "sGnus apropos (regexp): ")
4640   (let ((prev "")
4641         (obuf (current-buffer))
4642         groups des)
4643     ;; Go through all newsgroups that are known to Gnus.
4644     (mapatoms 
4645      (lambda (group)
4646        (and (symbol-name group)
4647             (string-match regexp (symbol-name group))
4648             (setq groups (cons (symbol-name group) groups))))
4649      gnus-active-hashtb)
4650     ;; Go through all descriptions that are known to Gnus. 
4651     (if search-description
4652         (mapatoms 
4653          (lambda (group)
4654            (and (string-match regexp (symbol-value group))
4655                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4656                 (setq groups (cons (symbol-name group) groups))))
4657          gnus-description-hashtb))
4658     (if (not groups)
4659         (gnus-message 3 "No groups matched \"%s\"." regexp)
4660       ;; Print out all the groups.
4661       (save-excursion
4662         (pop-to-buffer "*Gnus Help*")
4663         (buffer-disable-undo (current-buffer))
4664         (erase-buffer)
4665         (setq groups (sort groups 'string<))
4666         (while groups
4667           ;; Groups may be entered twice into the list of groups.
4668           (if (not (string= (car groups) prev))
4669               (progn
4670                 (insert (setq prev (car groups)) "\n")
4671                 (if (and gnus-description-hashtb
4672                          (setq des (gnus-gethash (car groups) 
4673                                                  gnus-description-hashtb)))
4674                     (insert "  " des "\n"))))
4675           (setq groups (cdr groups)))
4676         (goto-char (point-min))))
4677     (pop-to-buffer obuf)))
4678
4679 (defun gnus-group-description-apropos (regexp)
4680   "List all newsgroups that have names or descriptions that match a regexp."
4681   (interactive "sGnus description apropos (regexp): ")
4682   (if (not (or gnus-description-hashtb
4683                (gnus-read-all-descriptions-files)))
4684       (error "Couldn't request descriptions file"))
4685   (gnus-group-apropos regexp t))
4686
4687 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4688 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4689   "List all groups with unread articles that match REGEXP.
4690 If the prefix LEVEL is non-nil, it should be a number that says which
4691 level to cut off listing groups. 
4692 If ALL, also list groups with no unread articles.
4693 If LOWEST, don't list groups with level lower than LOWEST."
4694   (interactive "P\nsList newsgroups matching: ")
4695   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4696                            all (or lowest 1) regexp)
4697   (goto-char (point-min))
4698   (gnus-group-position-cursor))
4699
4700 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4701   "List all groups that match REGEXP.
4702 If the prefix LEVEL is non-nil, it should be a number that says which
4703 level to cut off listing groups. 
4704 If LOWEST, don't list groups with level lower than LOWEST."
4705   (interactive "P\nsList newsgroups matching: ")
4706   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4707
4708 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4709 (defun gnus-group-save-newsrc ()
4710   "Save the Gnus startup files."
4711   (interactive)
4712   (gnus-save-newsrc-file))
4713
4714 (defun gnus-group-restart (&optional arg)
4715   "Force Gnus to read the .newsrc file."
4716   (interactive "P")
4717   (gnus-save-newsrc-file)
4718   (gnus-setup-news 'force)
4719   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4720
4721 (defun gnus-group-read-init-file ()
4722   "Read the Gnus elisp init file."
4723   (interactive)
4724   (gnus-read-init-file))
4725
4726 (defun gnus-group-check-bogus-groups (&optional silent)
4727   "Check bogus newsgroups.
4728 If given a prefix, don't ask for confirmation before removing a bogus
4729 group."
4730   (interactive "P")
4731   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4732   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4733
4734 (defun gnus-group-edit-global-kill (&optional article group)
4735   "Edit the global kill file.
4736 If GROUP, edit that local kill file instead."
4737   (interactive "P")
4738   (setq gnus-current-kill-article article)
4739   (gnus-kill-file-edit-file group)
4740   (gnus-message 6
4741    (substitute-command-keys
4742     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4743
4744 (defun gnus-group-edit-local-kill (article group)
4745   "Edit a local kill file."
4746   (interactive (list nil (gnus-group-group-name)))
4747   (gnus-group-edit-global-kill article group))
4748
4749 (defun gnus-group-force-update ()
4750   "Update `.newsrc' file."
4751   (interactive)
4752   (gnus-save-newsrc-file))
4753
4754 (defun gnus-group-suspend ()
4755   "Suspend the current Gnus session.
4756 In fact, cleanup buffers except for group mode buffer.
4757 The hook gnus-suspend-gnus-hook is called before actually suspending."
4758   (interactive)
4759   (run-hooks 'gnus-suspend-gnus-hook)
4760   ;; Kill Gnus buffers except for group mode buffer.
4761   (let ((group-buf (get-buffer gnus-group-buffer)))
4762     ;; Do this on a separate list in case the user does a ^G before we finish
4763     (let ((gnus-buffer-list
4764            (delq group-buf (delq gnus-dribble-buffer
4765                                  (append gnus-buffer-list nil)))))
4766       (while gnus-buffer-list
4767         (gnus-kill-buffer (car gnus-buffer-list))
4768         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4769     (if group-buf
4770         (progn
4771           (setq gnus-buffer-list (list group-buf))
4772           (bury-buffer group-buf)
4773           (delete-windows-on group-buf t)))))
4774
4775 (defun gnus-group-clear-dribble ()
4776   "Clear all information from the dribble buffer."
4777   (interactive)
4778   (gnus-dribble-clear))
4779
4780 (defun gnus-group-exit ()
4781   "Quit reading news after updating .newsrc.eld and .newsrc.
4782 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4783   (interactive)
4784   (if (or noninteractive                ;For gnus-batch-kill
4785           (zerop (buffer-size))         ;No news is good news.
4786           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4787           (not gnus-interactive-exit)   ;Without confirmation
4788           gnus-expert-user
4789           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4790       (progn
4791         (run-hooks 'gnus-exit-gnus-hook)
4792         ;; Offer to save data from non-quitted summary buffers.
4793         (gnus-offer-save-summaries)
4794         ;; Save the newsrc file(s).
4795         (gnus-save-newsrc-file)
4796         ;; Kill-em-all.
4797         (gnus-close-backends)
4798         ;; Reset everything.
4799         (gnus-clear-system))))
4800
4801 (defun gnus-close-backends ()
4802   ;; Send a close request to all backends that support such a request. 
4803   (let ((methods gnus-valid-select-methods)
4804         func)
4805     (while methods
4806       (if (fboundp (setq func (intern (concat (car (car methods))
4807                                               "-request-close"))))
4808           (funcall func))
4809       (setq methods (cdr methods)))))
4810
4811 (defun gnus-group-quit ()
4812   "Quit reading news without updating .newsrc.eld or .newsrc.
4813 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4814   (interactive)
4815   (if (or noninteractive                ;For gnus-batch-kill
4816           (zerop (buffer-size))
4817           (not (gnus-server-opened gnus-select-method))
4818           gnus-expert-user
4819           (not gnus-current-startup-file)
4820           (gnus-yes-or-no-p
4821            (format "Quit reading news without saving %s? "
4822                    (file-name-nondirectory gnus-current-startup-file))))
4823       (progn
4824         (run-hooks 'gnus-exit-gnus-hook)
4825         (if gnus-use-full-window
4826             (delete-other-windows)
4827           (gnus-remove-some-windows))
4828         (gnus-dribble-save)
4829         (gnus-close-backends)
4830         (gnus-clear-system))))
4831
4832 (defun gnus-offer-save-summaries ()
4833   (let ((buffers (buffer-list)))
4834     (save-excursion
4835       (while buffers
4836         (and 
4837          ;; We look for buffers with "Summary" in the name.
4838          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4839          (progn
4840            (set-buffer (car buffers))
4841            ;; We check that this is, indeed, a summary buffer.
4842            (eq major-mode 'gnus-summary-mode)) 
4843          ;; We ask the user whether she wants to save the info.
4844          (gnus-y-or-n-p
4845                (format "Update summary buffer %s? " (buffer-name)))
4846          ;; We do it by simply exiting.
4847          (gnus-summary-exit))
4848         (setq buffers (cdr buffers))))))
4849
4850 (defun gnus-group-describe-briefly ()
4851   "Give a one line description of the group mode commands."
4852   (interactive)
4853   (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")))
4854
4855 (defun gnus-group-browse-foreign-server (method)
4856   "Browse a foreign news server.
4857 If called interactively, this function will ask for a select method
4858  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4859 If not, METHOD should be a list where the first element is the method
4860 and the second element is the address."
4861   (interactive
4862    (list (let ((how (completing-read 
4863                      "Which backend: "
4864                      (append gnus-valid-select-methods gnus-server-alist)
4865                      nil t "nntp")))
4866            ;; We either got a backend name or a virtual server name.
4867            ;; If the first, we also need an address.
4868            (if (assoc how gnus-valid-select-methods)
4869                (list (intern how)
4870                      ;; Suggested by mapjph@bath.ac.uk.
4871                      (completing-read 
4872                       "Address: " 
4873                       (mapcar (lambda (server) (list server))
4874                               gnus-secondary-servers)))
4875              ;; We got a server name, so we find the method.
4876              (gnus-server-to-method how)))))
4877   (gnus-browse-foreign-server method))
4878
4879 \f
4880 ;;;
4881 ;;; Browse Server Mode
4882 ;;;
4883
4884 (defvar gnus-browse-mode-hook nil)
4885 (defvar gnus-browse-mode-map nil)
4886 (put 'gnus-browse-mode 'mode-class 'special)
4887
4888 (if gnus-browse-mode-map
4889     nil
4890   (setq gnus-browse-mode-map (make-keymap))
4891   (suppress-keymap gnus-browse-mode-map)
4892   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4893   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4894   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4895   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4896   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4897   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4898   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4899   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4900   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4901   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4902   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4903   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4904   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4905   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4906   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4907   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4908   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4909   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4910   )
4911
4912 (defvar gnus-browse-current-method nil)
4913 (defvar gnus-browse-return-buffer nil)
4914
4915 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4916
4917 (defun gnus-browse-foreign-server (method &optional return-buffer)
4918   (setq gnus-browse-current-method method)
4919   (setq gnus-browse-return-buffer return-buffer)
4920   (let ((gnus-select-method method)
4921         groups group)
4922     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4923     (or (gnus-server-opened method)
4924         (gnus-open-server method)
4925         (error "Unable to contact server: %s" (gnus-status-message method)))
4926     (or (gnus-request-list method)
4927         (error "Couldn't request list: %s" (gnus-status-message method)))
4928     (get-buffer-create gnus-browse-buffer)
4929     (gnus-add-current-to-buffer-list)
4930     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4931     (gnus-configure-windows 'browse)
4932     (buffer-disable-undo (current-buffer))
4933     (let ((buffer-read-only nil))
4934       (erase-buffer))
4935     (gnus-browse-mode)
4936     (setq mode-line-buffer-identification
4937           (format
4938            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4939     (save-excursion
4940       (set-buffer nntp-server-buffer)
4941       (let ((cur (current-buffer)))
4942         (goto-char (point-min))
4943         (or (string= gnus-ignored-newsgroups "")
4944             (delete-matching-lines gnus-ignored-newsgroups))
4945         (while (re-search-forward 
4946                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4947           (goto-char (match-end 1))
4948           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4949                                                      (match-end 1))
4950                                    (max 0 (- (1+ (read cur)) (read cur))))
4951                              groups)))))
4952     (setq groups (sort groups 
4953                        (lambda (l1 l2)
4954                          (string< (car l1) (car l2)))))
4955     (let ((buffer-read-only nil))
4956       (while groups
4957         (setq group (car groups))
4958         (insert 
4959          (format "K%7d: %s\n" (cdr group) (car group)))
4960         (setq groups (cdr groups))))
4961     (switch-to-buffer (current-buffer))
4962     (goto-char (point-min))
4963     (gnus-group-position-cursor)))
4964
4965 (defun gnus-browse-mode ()
4966   "Major mode for browsing a foreign server.
4967
4968 All normal editing commands are switched off.
4969
4970 \\<gnus-browse-mode-map>
4971 The only things you can do in this buffer is
4972
4973 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4974 The group will be inserted into the group buffer upon exit from this
4975 buffer.  
4976
4977 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4978
4979 3) `\\[gnus-browse-exit]' to return to the group buffer."
4980   (interactive)
4981   (kill-all-local-variables)
4982   (if gnus-visual (gnus-browse-make-menu-bar))
4983   (setq mode-line-modified "-- ")
4984   (make-local-variable 'mode-line-format)
4985   (setq mode-line-format (copy-sequence mode-line-format))
4986   (and (equal (nth 3 mode-line-format) "   ")
4987        (setcar (nthcdr 3 mode-line-format) ""))
4988   (setq major-mode 'gnus-browse-mode)
4989   (setq mode-name "Browse Server")
4990   (setq mode-line-process nil)
4991   (use-local-map gnus-browse-mode-map)
4992   (buffer-disable-undo (current-buffer))
4993   (setq truncate-lines t)
4994   (setq buffer-read-only t)
4995   (run-hooks 'gnus-browse-mode-hook))
4996
4997 (defun gnus-browse-read-group (&optional no-article)
4998   "Enter the group at the current line."
4999   (interactive)
5000   (let ((group (gnus-browse-group-name)))
5001     (or (gnus-group-read-ephemeral-group 
5002          group gnus-browse-current-method nil
5003          (cons (current-buffer) 'browse))
5004         (error "Couldn't enter %s" group))))
5005
5006 (defun gnus-browse-select-group ()
5007   "Select the current group."
5008   (interactive)
5009   (gnus-browse-read-group 'no))
5010
5011 (defun gnus-browse-next-group (n)
5012   "Go to the next group."
5013   (interactive "p")
5014   (prog1
5015       (forward-line n)
5016     (gnus-group-position-cursor)))
5017
5018 (defun gnus-browse-prev-group (n)
5019   "Go to the next group."
5020   (interactive "p")
5021   (gnus-browse-next-group (- n)))
5022
5023 (defun gnus-browse-unsubscribe-current-group (arg)
5024   "(Un)subscribe to the next ARG groups."
5025   (interactive "p")
5026   (and (eobp)
5027        (error "No group at current line."))
5028   (let ((ward (if (< arg 0) -1 1))
5029         (arg (abs arg)))
5030     (while (and (> arg 0)
5031                 (not (eobp))
5032                 (gnus-browse-unsubscribe-group)
5033                 (zerop (gnus-browse-next-group ward)))
5034       (setq arg (1- arg)))
5035     (gnus-group-position-cursor)
5036     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5037     arg))
5038
5039 (defun gnus-browse-group-name ()
5040   (save-excursion
5041     (beginning-of-line)
5042     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5043         ()
5044       (gnus-group-prefixed-name 
5045        (buffer-substring (match-beginning 1) (match-end 1))
5046        gnus-browse-current-method))))
5047   
5048 (defun gnus-browse-unsubscribe-group ()
5049   (let ((sub nil)
5050         (buffer-read-only nil)
5051         group)
5052     (save-excursion
5053       (beginning-of-line)
5054       (if (= (following-char) ?K) (setq sub t))
5055       (setq group (gnus-browse-group-name))
5056       (beginning-of-line)
5057       (delete-char 1)
5058       (if sub
5059           (progn
5060             (gnus-group-change-level 
5061              (list t group gnus-level-default-subscribed
5062                    nil nil gnus-browse-current-method) 
5063              gnus-level-default-subscribed gnus-level-killed
5064              (and (car (nth 1 gnus-newsrc-alist))
5065                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5066                                 gnus-newsrc-hashtb))
5067              t)
5068             (insert ? ))
5069         (gnus-group-change-level 
5070          group gnus-level-killed gnus-level-default-subscribed)
5071         (insert ?K)))
5072     t))
5073
5074 (defun gnus-browse-exit ()
5075   "Quit browsing and return to the group buffer."
5076   (interactive)
5077   (if (eq major-mode 'gnus-browse-mode)
5078       (kill-buffer (current-buffer)))
5079   (if gnus-browse-return-buffer
5080       (gnus-configure-windows 'server 'force)
5081     (gnus-configure-windows 'group 'force)
5082     (gnus-group-list-groups nil)))
5083
5084 (defun gnus-browse-describe-briefly ()
5085   "Give a one line description of the group mode commands."
5086   (interactive)
5087   (gnus-message 6
5088    (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")))
5089       
5090 \f
5091 ;;;
5092 ;;; Gnus summary mode
5093 ;;;
5094
5095 (defvar gnus-summary-mode-map nil)
5096 (defvar gnus-summary-mark-map nil)
5097 (defvar gnus-summary-mscore-map nil)
5098 (defvar gnus-summary-article-map nil)
5099 (defvar gnus-summary-thread-map nil)
5100 (defvar gnus-summary-goto-map nil)
5101 (defvar gnus-summary-exit-map nil)
5102 (defvar gnus-summary-various-map nil)
5103 (defvar gnus-summary-interest-map nil)
5104 (defvar gnus-summary-sort-map nil)
5105 (defvar gnus-summary-backend-map nil)
5106 (defvar gnus-summary-save-map nil)
5107 (defvar gnus-summary-wash-map nil)
5108 (defvar gnus-summary-help-map nil)
5109
5110 (put 'gnus-summary-mode 'mode-class 'special)
5111
5112 (if gnus-summary-mode-map
5113     nil
5114   (setq gnus-summary-mode-map (make-keymap))
5115   (suppress-keymap gnus-summary-mode-map)
5116
5117   ;; Non-orthogonal keys
5118
5119   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5120   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5121   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5122   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5123   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5124   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5125   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5126   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5127   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5128   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5129   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5130   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5131   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5132   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5133   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5134   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5135   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5136   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5137   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5138   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5139   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5140   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5141   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5142   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5143   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5144   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5145   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5146   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5147   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5148   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5149   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5150   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5151   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5152   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5153   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5154   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5155   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5156   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5157   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5158   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5159   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5160   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5161   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5162   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5163   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5164   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5165   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5166   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5167   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5168   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5169   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5170   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5171   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5172   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5173   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5174   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5175   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5176   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5177   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5178   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5179   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5180   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5181   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5182   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5183   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5184   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5185   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5186   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5187   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5188   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5189   (define-key gnus-summary-mode-map "V" 'gnus-version)
5190   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5191   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5192   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5193   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5194   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5195   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5196   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5197   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5198 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5199   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5200   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5201   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5202 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5203   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5204   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5205   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5206   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5207   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5208
5209
5210   ;; Sort of orthogonal keymap
5211   (define-prefix-command 'gnus-summary-mark-map)
5212   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5213   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5214   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5215   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5216   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5217   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5218   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5219   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5220   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5221   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5222   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5223   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5224   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5225   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5226   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5227   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5228   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5229   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5230   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5231   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5232   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5233   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5234   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5235   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5236
5237   (define-prefix-command 'gnus-summary-mscore-map)
5238   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5239   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5240   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5241   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5242   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5243
5244   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5245   
5246   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5247   
5248   (define-prefix-command 'gnus-summary-goto-map)
5249   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5250   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5251   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5252   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5253   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5254   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5255   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5256   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5257   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5258   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5259   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5260   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5261   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5262   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5263
5264
5265   (define-prefix-command 'gnus-summary-thread-map)
5266   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5267   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5268   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5269   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5270   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5271   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5272   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5273   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5274   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5275   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5276   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5277   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5278   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5279   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5280
5281   
5282   (define-prefix-command 'gnus-summary-exit-map)
5283   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5284   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5285   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5286   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5287   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5288   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5289   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5290   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5291   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5292   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5293   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5294
5295
5296   (define-prefix-command 'gnus-summary-article-map)
5297   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5298   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5299   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5300   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5301   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5302   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5303   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5304   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5305   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5306   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5307   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5308   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5309   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5310   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5311
5312
5313
5314   (define-prefix-command 'gnus-summary-wash-map)
5315   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5316   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5317   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5318   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5319   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5320   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5321   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5322   (define-key gnus-summary-wash-map "M" 'gnus-article-remove-cr)
5323   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5324   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5325   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5326   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5327   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5328
5329   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5330   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5331   (define-key gnus-summary-wash-map "G" 'gnus-summary-toggle-header)
5332   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5333
5334   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5335   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5336   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5337   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5338   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5339   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5340
5341
5342   (define-prefix-command 'gnus-summary-help-map)
5343   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5344   (define-key gnus-summary-help-map "v" 'gnus-version)
5345   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5346   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5347   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5348   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5349
5350
5351   (define-prefix-command 'gnus-summary-backend-map)
5352   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5353   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5354   (define-key gnus-summary-backend-map "\M-\C-e" 
5355     'gnus-summary-expire-articles-now)
5356   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5357   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5358   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5359   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5360   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5361   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5362   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5363
5364
5365   (define-prefix-command 'gnus-summary-save-map)
5366   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5367   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5368   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5369   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5370   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5371   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5372   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5373   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5374 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5375
5376   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5377
5378   (define-prefix-command 'gnus-summary-various-map)
5379   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5380     (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5381 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5382 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5383 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5384 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5385 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5386 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5387   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5388 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5389 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5390
5391   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5392
5393 ;  (define-prefix-command 'gnus-summary-sort-map)
5394 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5395 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5396 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5397 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5398 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5399 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5400
5401   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5402   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5403   )
5404
5405
5406 \f
5407
5408 (defun gnus-summary-mode (&optional group)
5409   "Major mode for reading articles.
5410
5411 All normal editing commands are switched off.
5412 \\<gnus-summary-mode-map>
5413 Each line in this buffer represents one article.  To read an
5414 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5415 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5416 respectively.
5417
5418 You can also post articles and send mail from this buffer.  To 
5419 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5420 of an article, type `\\[gnus-summary-reply]'.
5421
5422 There are approx. one gazillion commands you can execute in this 
5423 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5424
5425 The following commands are available:
5426
5427 \\{gnus-summary-mode-map}"
5428   (interactive)
5429   (if gnus-visual (gnus-summary-make-menu-bar))
5430   (kill-all-local-variables)
5431   (let ((locals gnus-summary-local-variables))
5432     (while locals
5433       (if (consp (car locals))
5434           (progn
5435             (make-local-variable (car (car locals)))
5436             (set (car (car locals)) (eval (cdr (car locals)))))
5437         (make-local-variable (car locals))
5438         (set (car locals) nil))
5439       (setq locals (cdr locals))))
5440   (gnus-make-thread-indent-array)
5441   (setq mode-line-modified "-- ")
5442   (make-local-variable 'mode-line-format)
5443   (setq mode-line-format (copy-sequence mode-line-format))
5444   (and (equal (nth 3 mode-line-format) "   ")
5445        (setcar (nthcdr 3 mode-line-format) ""))
5446   (setq major-mode 'gnus-summary-mode)
5447   (setq mode-name "Summary")
5448   (make-local-variable 'minor-mode-alist)
5449   (use-local-map gnus-summary-mode-map)
5450   (buffer-disable-undo (current-buffer))
5451   (setq buffer-read-only t)             ;Disable modification
5452   (setq truncate-lines t)
5453   (setq selective-display t)
5454   (setq selective-display-ellipses t)   ;Display `...'
5455   (setq buffer-display-table gnus-summary-display-table)
5456   (setq gnus-newsgroup-name group)
5457   (run-hooks 'gnus-summary-mode-hook))
5458
5459 (defun gnus-summary-make-display-table ()
5460   ;; Change the display table.  Odd characters have a tendency to mess
5461   ;; up nicely formatted displays - we make all possible glyphs
5462   ;; display only a single character.
5463
5464   ;; We start from the standard display table, if any.
5465   (setq gnus-summary-display-table 
5466         (or (copy-sequence standard-display-table)
5467             (make-display-table)))
5468   ;; Nix out all the control chars...
5469   (let ((i 32))
5470     (while (>= (setq i (1- i)) 0)
5471       (aset gnus-summary-display-table i [??])))
5472   ;; ... but not newline and cr, of course. (cr is necessary for the
5473   ;; selective display).  
5474   (aset gnus-summary-display-table ?\n nil)
5475   (aset gnus-summary-display-table ?\r nil)
5476   ;; We nix out any glyphs over 126 that are not set already.  
5477   (let ((i 256))
5478     (while (>= (setq i (1- i)) 127)
5479       ;; Only modify if the entry is nil.
5480       (or (aref gnus-summary-display-table i) 
5481           (aset gnus-summary-display-table i [??])))))
5482
5483 (defun gnus-summary-clear-local-variables ()
5484   (let ((locals gnus-summary-local-variables))
5485     (while locals
5486       (if (consp (car locals))
5487           (and (vectorp (car (car locals)))
5488                (set (car (car locals)) nil))
5489         (and (vectorp (car locals))
5490              (set (car locals) nil)))
5491       (setq locals (cdr locals)))))
5492
5493 ;; Some summary mode macros.
5494
5495 ;; Return a header specified by a NUMBER.
5496 (defun gnus-get-header-by-number (number)
5497   (save-excursion
5498     (set-buffer gnus-summary-buffer)
5499     (or gnus-newsgroup-headers-hashtb-by-number
5500         (gnus-make-headers-hashtable-by-number))
5501     (gnus-gethash (int-to-string number)
5502                   gnus-newsgroup-headers-hashtb-by-number)))
5503
5504 ;; Fast version of the function above.
5505 (defmacro gnus-get-header-by-num (number)
5506   (` (gnus-gethash (int-to-string (, number)) 
5507                    gnus-newsgroup-headers-hashtb-by-number)))
5508
5509 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5510   "Search for article forward.
5511 If UNREAD is non-nil, only unread articles are selected.
5512 If SUBJECT is non-nil, the article which has the same subject will be
5513 searched for. 
5514 If BACKWARD is non-nil, the search will be performed backwards instead."
5515   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5516
5517 (defmacro gnus-summary-search-backward (&optional unread subject)
5518   "Search for article backward.
5519 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5520 If 2nd optional argument SUBJECT is non-nil, the article which has
5521 the same subject will be searched for."
5522   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5523
5524 (defmacro gnus-summary-article-number (&optional number-or-nil)
5525   "The article number of the article on the current line.
5526 If there isn's an article number here, then we return the current
5527 article number."
5528   (if number-or-nil
5529       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5530     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5531          gnus-current-article)))
5532
5533 (defmacro gnus-summary-thread-level ()
5534   "The thread level of the article on the current line."
5535   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5536        0))
5537
5538 (defmacro gnus-summary-article-mark ()
5539   "The mark on the current line."
5540   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5541
5542 (defun gnus-summary-subject-string ()
5543   "Return current subject string or nil if nothing."
5544   (let ((article (gnus-summary-article-number))
5545         header)
5546     (and article 
5547          (setq header (gnus-get-header-by-num article))
5548          (vectorp header)
5549          (header-subject header))))
5550
5551 ;; Various summary mode internalish functions.
5552
5553 (defun gnus-mouse-pick-article (e)
5554   (interactive "e")
5555   (mouse-set-point e)
5556   (gnus-summary-next-page nil t))
5557
5558 (defun gnus-summary-setup-buffer (group)
5559   "Initialize summary buffer."
5560   (let ((buffer (concat "*Summary " group "*")))
5561     (if (get-buffer buffer)
5562         (progn
5563           (set-buffer buffer)
5564           (not gnus-newsgroup-begin))
5565       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5566       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5567       (gnus-add-current-to-buffer-list)
5568       (gnus-summary-mode group)
5569       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5570       (setq gnus-newsgroup-name group)
5571       t)))
5572
5573 (defun gnus-set-global-variables ()
5574   ;; Set the global equivalents of the summary buffer-local variables
5575   ;; to the latest values they had. These reflect the summary buffer
5576   ;; that was in action when the last article was fetched.
5577   (if (eq major-mode 'gnus-summary-mode) 
5578       (progn
5579         (setq gnus-summary-buffer (current-buffer))
5580         (let ((name gnus-newsgroup-name)
5581               (marked gnus-newsgroup-marked)
5582               (unread gnus-newsgroup-unreads)
5583               (headers gnus-current-headers)
5584               (score-file gnus-current-score-file))
5585           (save-excursion
5586             (set-buffer gnus-group-buffer)
5587             (setq gnus-newsgroup-name name)
5588             (setq gnus-newsgroup-marked marked)
5589             (setq gnus-newsgroup-unreads unread)
5590             (setq gnus-current-headers headers)
5591             (setq gnus-current-score-file score-file))))))
5592
5593 (defun gnus-summary-insert-dummy-line (sformat subject number)
5594   (if (not sformat) 
5595       (setq sformat gnus-summary-dummy-line-format-spec))
5596   (let (b)
5597     (beginning-of-line)
5598     (setq b (point))
5599     (insert (eval sformat))
5600     (add-text-properties
5601      b (1+ b)
5602      (list 'gnus-number number 
5603            'gnus-mark gnus-dummy-mark
5604            'gnus-level 0))))
5605
5606 (defvar gnus-thread-indent-array nil)
5607 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5608 (defun gnus-make-thread-indent-array ()
5609   (let ((n 200))
5610     (if (and gnus-thread-indent-array
5611              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5612         nil
5613       (setq gnus-thread-indent-array (make-vector 201 "")
5614             gnus-thread-indent-array-level gnus-thread-indent-level)
5615       (while (>= n 0)
5616         (aset gnus-thread-indent-array n
5617               (make-string (* n gnus-thread-indent-level) ? ))
5618         (setq n (1- n))))))
5619
5620 (defun gnus-summary-insert-line 
5621   (sformat header level current unread replied expirable subject-or-nil
5622            &optional dummy score)
5623   (or sformat (setq sformat gnus-summary-line-format-spec))
5624   (let* ((indentation (aref gnus-thread-indent-array level))
5625          (lines (header-lines header))
5626          (score (or score gnus-summary-default-score 0))
5627          (score-char
5628           (if (or (null gnus-summary-default-score)
5629                   (<= (abs (- score gnus-summary-default-score))
5630                       gnus-summary-zcore-fuzz)) ? 
5631             (if (< score gnus-summary-default-score)
5632                 gnus-score-below-mark gnus-score-over-mark)))
5633          (replied (if replied gnus-replied-mark ? ))
5634          (from (header-from header))
5635          (name (cond 
5636                 ((string-match "(.+)" from)
5637                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5638                 ((string-match "<[^>]+> *$" from)
5639                  (let ((beg (match-beginning 0)))
5640                    (or (and (string-match "^\"[^\"]*\"" from)
5641                             (substring from (1+ (match-beginning 0))
5642                                        (1- (match-end 0))))
5643                        (substring from 0 beg))))
5644                 (t from)))
5645          (subject (header-subject header))
5646          (number (header-number header))
5647          (opening-bracket (if dummy ?\< ?\[))
5648          (closing-bracket (if dummy ?\> ?\]))
5649          (buffer-read-only nil)
5650          (b (progn (beginning-of-line) (point))))
5651     (or (numberp lines) (setq lines 0))
5652     (insert (eval sformat))
5653     (add-text-properties
5654      b (1+ b) (list 'gnus-number number 
5655                     'gnus-mark (or unread gnus-unread-mark)
5656                     'gnus-level level))))
5657
5658 (defun gnus-summary-update-line (&optional dont-update)
5659   ;; Update summary line after change.
5660   (or (not gnus-summary-default-score)
5661       gnus-summary-inhibit-highlight
5662       (let ((gnus-summary-inhibit-highlight t)
5663             (article (gnus-summary-article-number)))
5664         (progn
5665           (or dont-update
5666               (if (and gnus-summary-mark-below
5667                        (< (gnus-summary-article-score)
5668                           gnus-summary-mark-below))
5669                   (and (not (memq article gnus-newsgroup-marked))
5670                        (not (memq article gnus-newsgroup-dormant))
5671                        (memq article gnus-newsgroup-unreads)
5672                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5673                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5674                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5675           (and gnus-visual
5676                (run-hooks 'gnus-summary-update-hook))))))
5677
5678 (defun gnus-summary-update-lines (&optional beg end)
5679   ;; Mark article as read (or not) by taking into account scores.
5680   (let ((beg (or beg (point-min)))
5681         (end (or end (point-max))))
5682     (if (or (not gnus-summary-default-score)
5683             gnus-summary-inhibit-highlight)
5684         ()
5685       (let ((gnus-summary-inhibit-highlight t)
5686             article)
5687         (save-excursion
5688           (set-buffer gnus-summary-buffer)
5689           (goto-char beg)
5690           (beginning-of-line)
5691           (while (and (not (eobp)) (< (point) end))
5692             (if (and gnus-summary-mark-below
5693                      (< (or (cdr (assq 
5694                                   (setq article (get-text-property 
5695                                                  (point) 'gnus-number))
5696                                   gnus-newsgroup-scored))
5697                             gnus-summary-default-score 0)
5698                         gnus-summary-mark-below))
5699                 ;; We want to possibly mark it as read...
5700                 (and (not (memq article gnus-newsgroup-marked))
5701                      (not (memq article gnus-newsgroup-dormant))
5702                      (memq article gnus-newsgroup-unreads)
5703                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5704               ;; We want to possibly mark it as unread.
5705               (and (eq (get-text-property (point) 'gnus-mark)
5706                        gnus-low-score-mark)
5707                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5708             ;; Do the visual highlights at the same time.
5709             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5710             (forward-line 1)))))))
5711
5712 (defvar gnus-tmp-gathered nil)
5713
5714 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5715   ;; Sum up all elements (and sub-elements) in a list.
5716   (let* ((number
5717           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5718           (if (and (consp thread) (cdr thread))
5719               (apply
5720                '+ 1 (mapcar
5721                      (function
5722                       (lambda (arg)
5723                         (if (memq (header-number (car arg)) gnus-tmp-gathered)
5724                             0
5725                           (gnus-summary-number-of-articles-in-thread arg))))
5726                      (cdr thread)))
5727             1)))
5728     (if char 
5729         (if (> number 1) gnus-not-empty-thread-mark
5730           gnus-empty-thread-mark)
5731       number)))
5732
5733 (defun gnus-summary-read-group 
5734   (group &optional show-all no-article kill-buffer)
5735   "Start reading news in newsgroup GROUP.
5736 If SHOW-ALL is non-nil, already read articles are also listed.
5737 If NO-ARTICLE is non-nil, no article is selected initially."
5738   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5739   (let* ((new-group (gnus-summary-setup-buffer group))
5740          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5741                                                   group))))
5742          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5743     (cond 
5744      ((not new-group)
5745       (gnus-set-global-variables)
5746       (gnus-kill-buffer kill-buffer)
5747       (gnus-configure-windows 'summary)
5748       (gnus-set-mode-line 'summary)
5749       (gnus-summary-position-cursor)
5750       (message "")
5751       t)
5752      ((null did-select) 
5753       (and (eq major-mode 'gnus-summary-mode)
5754            (not (equal (current-buffer) kill-buffer))
5755            (progn
5756              (kill-buffer (current-buffer))
5757              (if (not quit-config)
5758                  (progn
5759                    (set-buffer gnus-group-buffer)
5760                    (gnus-group-jump-to-group group)
5761                    (gnus-group-next-unread-group 1))
5762                (if (not (buffer-name (car quit-config)))
5763                    (gnus-configure-windows 'group 'force)
5764                  (set-buffer (car quit-config))
5765                  (and (eq major-mode 'gnus-summary-mode)
5766                       (gnus-set-global-variables))
5767                  (gnus-configure-windows (cdr quit-config))))))
5768       (message "Can't select group")
5769       nil)
5770      ((eq did-select 'quit)
5771       (and (eq major-mode 'gnus-summary-mode)
5772            (not (equal (current-buffer) kill-buffer))
5773            (kill-buffer (current-buffer)))
5774       (gnus-kill-buffer kill-buffer)
5775       (if (not quit-config)
5776           (progn
5777             (set-buffer gnus-group-buffer)
5778             (gnus-group-jump-to-group group)
5779             (gnus-group-next-unread-group 1)
5780             (gnus-configure-windows 'group 'force))
5781         (if (not (buffer-name (car quit-config)))
5782             (gnus-configure-windows 'group 'force)
5783           (set-buffer (car quit-config))
5784           (and (eq major-mode 'gnus-summary-mode)
5785                (gnus-set-global-variables))
5786           (gnus-configure-windows (cdr quit-config))))
5787       (signal 'quit nil))
5788      (t
5789       (gnus-set-global-variables)
5790       ;; Save the active value in effect when the group was entered.
5791       (setq gnus-newsgroup-active 
5792             (gnus-copy-sequence
5793              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5794       ;; You can change the subjects in this hook.
5795       (run-hooks 'gnus-select-group-hook)
5796       ;; Do score processing.
5797       (and gnus-use-scoring (gnus-possibly-score-headers))
5798       (gnus-update-format-specifications)
5799       ;; Generate the summary buffer.
5800       (gnus-summary-prepare)
5801       (if (zerop (buffer-size))
5802           (cond (gnus-newsgroup-dormant
5803                  (gnus-summary-show-all-dormant))
5804                 ((and gnus-newsgroup-scored show-all)
5805                  (gnus-summary-show-all-expunged))))
5806       ;; Function `gnus-apply-kill-file' must be called in this hook.
5807       (run-hooks 'gnus-apply-kill-hook)
5808       (if (zerop (buffer-size))
5809           (progn
5810             ;; This newsgroup is empty.
5811             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5812             (gnus-message 6 "No unread news")
5813             (gnus-kill-buffer kill-buffer)
5814             nil)
5815         ;;(save-excursion
5816         ;;  (if kill-buffer
5817         ;;      (let ((gnus-summary-buffer kill-buffer))
5818         ;;      (gnus-configure-windows 'group))))
5819         ;; Hide conversation thread subtrees.  We cannot do this in
5820         ;; gnus-summary-prepare-hook since kill processing may not
5821         ;; work with hidden articles.
5822         (and gnus-show-threads
5823              gnus-thread-hide-subtree
5824              (gnus-summary-hide-all-threads))
5825         ;; Show first unread article if requested.
5826         (goto-char (point-min))
5827         (if (and (not no-article)
5828                  gnus-auto-select-first
5829                  (gnus-summary-first-unread-article))
5830             ()
5831           (gnus-configure-windows 'summary))
5832         (gnus-set-mode-line 'summary)
5833         (gnus-summary-position-cursor)
5834         ;; If in async mode, we send some info to the backend.
5835         (and gnus-newsgroup-async
5836              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5837              (gnus-request-asynchronous 
5838               gnus-newsgroup-name
5839               (if (and gnus-asynchronous-article-function
5840                        (fboundp gnus-asynchronous-article-function))
5841                   (funcall gnus-asynchronous-article-function
5842                            gnus-newsgroup-threads)
5843                 gnus-newsgroup-threads)))
5844         (gnus-kill-buffer kill-buffer)
5845         (if (not (get-buffer-window gnus-group-buffer))
5846             ()
5847           ;; gotta use windows, because recenter does wierd stuff if
5848           ;; the current buffer ain't the displayed window.
5849           (let ((owin (selected-window))) 
5850             (select-window (get-buffer-window gnus-group-buffer))
5851             (and (gnus-group-goto-group group)
5852                  (recenter))
5853             (select-window owin))))
5854       t))))
5855
5856 (defun gnus-summary-prepare ()
5857   ;; Generate the summary buffer.
5858   (let ((buffer-read-only nil))
5859     (erase-buffer)
5860     (gnus-message 5 "Threading...")
5861     (gnus-summary-prepare-threads 
5862      (if gnus-show-threads
5863          (gnus-gather-threads 
5864           (gnus-sort-threads 
5865            (if (and gnus-summary-expunge-below
5866                     (not gnus-fetch-old-headers))
5867                (gnus-make-threads-and-expunge)
5868              (gnus-make-threads))))
5869        gnus-newsgroup-headers)
5870      'cull)
5871     (gnus-message 5 "Threading...done")
5872     (gnus-summary-update-lines)
5873     ;; Remove the final newline.
5874     ;;(goto-char (point-max))
5875     ;;(delete-char -1)
5876     ;; Call hooks for modifying summary buffer.
5877     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5878     (goto-char (point-min))
5879     (run-hooks 'gnus-summary-prepare-hook)))
5880
5881 (defun gnus-gather-threads (threads)
5882   "Gather threads that have lost their roots."
5883   (if (not gnus-summary-make-false-root)
5884       threads 
5885     (let ((hashtb (gnus-make-hashtable 1023))
5886           (prev threads)
5887           (result threads)
5888           subject hthread whole-subject)
5889       (while threads
5890         (setq whole-subject 
5891               (setq subject (header-subject (car (car threads)))))
5892         (if gnus-summary-gather-subject-limit
5893             (or (and (numberp gnus-summary-gather-subject-limit)
5894                      (> (length subject) gnus-summary-gather-subject-limit)
5895                      (setq subject
5896                            (substring subject 0 
5897                                       gnus-summary-gather-subject-limit)))
5898                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5899                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5900           (setq subject (gnus-simplify-subject-re subject)))
5901         (if (setq hthread 
5902                   (gnus-gethash subject hashtb))
5903             (progn
5904               (or (stringp (car (car hthread)))
5905                   (setcar hthread (list whole-subject (car hthread))))
5906               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5907                                            (list (car threads))))
5908               (setcdr prev (cdr threads))
5909               (setq threads prev))
5910           (gnus-sethash subject threads hashtb))
5911         (setq prev threads)
5912         (setq threads (cdr threads)))
5913       result)))
5914
5915 (defun gnus-make-threads ()
5916   ;; This function takes the dependencies already made by 
5917   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5918   ;; through the dependecies in the hash table and finds all the
5919   ;; roots. Roots do not refer back to any valid articles.
5920   (let (roots new-roots)
5921     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5922          (gnus-build-old-threads))
5923     (mapatoms
5924      (lambda (refs)
5925        (if (not (car (symbol-value refs)))
5926            (setq roots (append (cdr (symbol-value refs)) roots))
5927          ;; Ok, these refer back to valid articles, but if
5928          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5929          ;; the root has the same subject as its children. The children
5930          ;; that do not are made into roots and removed from the list
5931          ;; of children. 
5932          (or gnus-thread-ignore-subject
5933              (let* ((prev (symbol-value refs))
5934                     (subject (gnus-simplify-subject-re 
5935                               (header-subject (car prev))))
5936                     (headers (cdr prev)))
5937                (while headers
5938                  (if (not (string= subject
5939                                    (gnus-simplify-subject-re 
5940                                     (header-subject (car headers)))))
5941                      (progn
5942                        (setq new-roots (cons (car headers) new-roots))
5943                        (setcdr prev (cdr headers)))
5944                    (setq prev headers))
5945                  (setq headers (cdr headers)))))))
5946      gnus-newsgroup-dependencies)
5947
5948     ;; We enter the new roots into the dependencies structure to
5949     ;; ensure that any possible later thread-regeneration will be
5950     ;; possible. 
5951     (let ((r new-roots))
5952       (while r
5953         (gnus-sethash (concat (header-id (car r)) ".boo")
5954                       (list nil (car r)) gnus-newsgroup-dependencies)
5955         (setq r (cdr r))))
5956
5957     (setq roots (nconc new-roots roots))
5958     
5959     (mapcar 'gnus-trim-thread
5960             (apply 'append
5961                    (mapcar 'gnus-cut-thread
5962                            (mapcar 'gnus-make-sub-thread roots))))))
5963   
5964 (defun gnus-make-threads-and-expunge ()
5965   ;; This function takes the dependencies already made by 
5966   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5967   ;; through the dependecies in the hash table and finds all the
5968   ;; roots. Roots do not refer back to any valid articles.
5969   (let ((default (or gnus-summary-default-score 0))
5970         (below gnus-summary-expunge-below)
5971         roots article new-roots)
5972     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5973          (gnus-build-old-threads))
5974     (mapatoms
5975      (lambda (refs)
5976        (if (not (car (symbol-value refs)))
5977            ;; These articles do not refer back to any other articles -
5978            ;; they are roots.
5979            (let ((headers (cdr (symbol-value refs))))
5980              ;; We weed out the low-scored articles.
5981              (while headers
5982                (if (not (< (or (cdr (assq (header-number (car headers))
5983                                           gnus-newsgroup-scored)) default)
5984                            below))
5985                    ;; It is over.
5986                    (setq roots (cons (car headers) roots))
5987                  ;; It is below, so we mark it as read.
5988                  (setq gnus-newsgroup-unreads
5989                        (delq (header-number (car headers))
5990                              gnus-newsgroup-unreads))
5991                  (setq gnus-newsgroup-reads 
5992                        (cons (cons (header-number (car headers))
5993                                    gnus-low-score-mark) 
5994                              gnus-newsgroup-reads)))
5995                (setq headers (cdr headers))))
5996          ;; Ok, these refer back to valid articles, but if
5997          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5998          ;; the root has the same subject as its children. The children
5999          ;; that do not are made into roots and removed from the list
6000          ;; of children. 
6001          (or gnus-thread-ignore-subject
6002              (let* ((prev (symbol-value refs))
6003                     (subject (gnus-simplify-subject-re 
6004                               (header-subject (car prev))))
6005                     (headers (cdr prev)))
6006                (while headers
6007                  (if (not (string= subject
6008                                    (gnus-simplify-subject-re 
6009                                     (header-subject (car headers)))))
6010                      (progn
6011                        (if (not (< (or (cdr (assq (header-number (car headers))
6012                                                   gnus-newsgroup-scored))
6013                                        default) below))
6014                            (setq new-roots (cons (car headers) new-roots))
6015                          (setq gnus-newsgroup-unreads
6016                                (delq (header-number (car headers))
6017                                      gnus-newsgroup-unreads))
6018                          (setq gnus-newsgroup-reads
6019                                (cons (cons (header-number (car headers)) 
6020                                            gnus-low-score-mark) 
6021                                      gnus-newsgroup-reads)))
6022                        (setcdr prev (cdr headers)))
6023                    (setq prev headers))
6024                  (setq headers (cdr headers)))))
6025          ;; If this article is expunged, some of the children might be
6026          ;; roots.  
6027          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6028                                gnus-newsgroup-scored)) default)
6029                 below)
6030              (let* ((prev (symbol-value refs))
6031                     (headers (cdr prev)))
6032                (while headers
6033                  (setq article (header-number (car headers)))
6034                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6035                                  default) below))
6036                      (progn (setq new-roots (cons (car headers) new-roots))
6037                             (setq prev headers))
6038                    (setq gnus-newsgroup-unreads 
6039                          (delq article gnus-newsgroup-unreads))
6040                    (setq gnus-newsgroup-reads 
6041                          (cons (cons article gnus-low-score-mark) 
6042                                gnus-newsgroup-reads))
6043                    (setcdr prev (cdr headers)))
6044                  (setq headers (cdr headers))))
6045            ;; It was not expunged, but we look at expunged children.
6046            (let* ((prev (symbol-value refs))
6047                   (headers (cdr prev))
6048                   article)
6049              (while headers
6050                (setq article (header-number (car headers)))
6051                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6052                                default) below))
6053                    (setq prev headers)
6054                  (setq gnus-newsgroup-unreads 
6055                        (delq article gnus-newsgroup-unreads))
6056                  (setq gnus-newsgroup-reads 
6057                        (cons (cons article gnus-low-score-mark)
6058                              gnus-newsgroup-reads))
6059                  (setcdr prev (cdr headers)))
6060                (setq headers (cdr headers)))))))
6061      gnus-newsgroup-dependencies)
6062
6063     ;; We enter the new roots into the dependencies structure to
6064     ;; ensure that any possible later thread-regeneration will be
6065     ;; possible. 
6066     (let ((r new-roots))
6067       (while r
6068         (gnus-sethash (concat (header-id (car r)) ".boo")
6069                       (list nil (car r)) gnus-newsgroup-dependencies)
6070         (setq r (cdr r))))
6071
6072     (setq roots (nconc new-roots roots))
6073     
6074     (mapcar 'gnus-trim-thread
6075             (apply 'append
6076                    (mapcar 'gnus-cut-thread
6077                            (mapcar 'gnus-make-sub-thread roots))))))
6078   
6079 (defun gnus-cut-thread (thread)
6080   ;; Remove leaf dormant or ancient articles from THREAD.
6081   (let ((head (car thread))
6082         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6083     (if (and (null tail)
6084              (let ((number (header-number head)))
6085                (or (memq number gnus-newsgroup-ancient)
6086                    (memq number gnus-newsgroup-dormant)
6087                    (and gnus-summary-expunge-below
6088                         (eq gnus-fetch-old-headers 'some)
6089                         (< (or (cdr (assq number gnus-newsgroup-scored))
6090                                gnus-summary-default-score 0)
6091                            gnus-summary-expunge-below)
6092                         (progn
6093                           (setq gnus-newsgroup-unreads
6094                                 (delq number gnus-newsgroup-unreads))
6095                           (setq gnus-newsgroup-reads
6096                                 (cons (cons number gnus-low-score-mark)
6097                                       gnus-newsgroup-reads))
6098                           t)))))
6099         nil
6100       (list (cons head tail)))))
6101
6102 (defun gnus-trim-thread (thread)
6103   ;; Remove root ancient articles with only one child from THREAD.
6104   (if (and (eq gnus-fetch-old-headers 'some)
6105            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6106            (= (length thread) 2))
6107       (gnus-trim-thread (nth 1 thread))
6108     thread))
6109
6110 (defun gnus-make-sub-thread (root)
6111   ;; This function makes a sub-tree for a node in the tree.
6112   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6113                                               gnus-newsgroup-dependencies)))))
6114     (cons root (mapcar 'gnus-make-sub-thread children))))
6115
6116 (defun gnus-build-old-threads ()
6117   ;; Look at all the articles that refer back to old articles, and
6118   ;; fetch the headers for the articles that aren't there. This will
6119   ;; build complete threads - if the roots haven't been expired by the
6120   ;; server, that is.
6121   (let (id heads)
6122     (mapatoms
6123      (lambda (refs)
6124        (if (not (car (symbol-value refs)))
6125            (progn
6126              (setq heads (cdr (symbol-value refs)))
6127              (while heads
6128                (if (not (memq (header-number (car heads))
6129                               gnus-newsgroup-dormant))
6130                    (progn
6131                      (setq id (symbol-name refs))
6132                      (while (and (setq id (gnus-build-get-header id))
6133                                  (not (car (gnus-gethash 
6134                                             id gnus-newsgroup-dependencies)))))
6135                      (setq heads nil))
6136                  (setq heads (cdr heads)))))))
6137      gnus-newsgroup-dependencies)))
6138
6139 (defun gnus-build-get-header (id)
6140   ;; Look through the buffer of NOV lines and find the header to
6141   ;; ID. Enter this line into the dependencies hash table, and return
6142   ;; the id of the parent article (if any).
6143   (let ((deps gnus-newsgroup-dependencies)
6144         found header)
6145     (prog1
6146         (save-excursion
6147           (set-buffer nntp-server-buffer)
6148           (goto-char (point-min))
6149           (while (and (not found) (search-forward id nil t))
6150             (beginning-of-line)
6151             (setq found (looking-at 
6152                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6153                                  (regexp-quote id))))
6154             (or found (beginning-of-line 2)))
6155           (if found
6156               (let (ref)
6157                 (beginning-of-line)
6158                 (and
6159                  (setq header (gnus-nov-parse-line 
6160                                (read (current-buffer)) deps))
6161                  (setq ref (header-references header))
6162                  (string-match "\\(<[^>]+>\\) *$" ref)
6163                  (substring ref (match-beginning 1) (match-end 1))))))
6164       (and header
6165            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6166                  gnus-newsgroup-ancient (cons (header-number header)
6167                                               gnus-newsgroup-ancient))))))
6168
6169 ;; Re-build the thread containing ID.
6170 (defun gnus-rebuild-thread (id)
6171   (let ((dep gnus-newsgroup-dependencies)
6172         (buffer-read-only nil)
6173         parent headers refs thread art)
6174     (while (and id (setq headers
6175                          (car (setq art (gnus-gethash (downcase id) dep)))))
6176       (setq parent art)
6177       (setq id (and (setq refs (header-references headers))
6178                     (string-match "\\(<[^>]+>\\) *$" refs)
6179                     (substring refs (match-beginning 1) (match-end 1)))))
6180     (setq thread (gnus-make-sub-thread (car parent)))
6181     (gnus-rebuild-remove-articles thread)
6182     (let ((beg (point)))
6183       (gnus-summary-prepare-threads (list thread))
6184       (gnus-summary-update-lines beg (point)))))
6185
6186 ;; Delete all lines in the summary buffer that correspond to articles
6187 ;; in this thread.
6188 (defun gnus-rebuild-remove-articles (thread)
6189   (and (gnus-summary-goto-subject (header-number (car thread)))
6190        (gnus-delete-line))
6191   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6192
6193 (defun gnus-sort-threads (threads)
6194   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6195   (let ((fun gnus-thread-sort-functions))
6196     (while fun
6197       (setq threads (sort threads (car fun))
6198             fun (cdr fun))))
6199   threads)
6200
6201 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6202 (defmacro gnus-thread-header (thread)
6203   ;; Return header of first article in THREAD.
6204   ;; Note that THREAD must never, evr be anything else than a variable -
6205   ;; using some other form will lead to serious barfage.
6206   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6207   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6208   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6209         (vector thread) 2))
6210
6211 (defun gnus-thread-sort-by-number (h1 h2)
6212   "Sort threads by root article number."
6213   (< (header-number (gnus-thread-header h1))
6214      (header-number (gnus-thread-header h2))))
6215
6216 (defun gnus-thread-sort-by-author (h1 h2)
6217   "Sort threads by root author."
6218   (string-lessp
6219    (let ((extract (funcall 
6220                    gnus-extract-address-components
6221                    (header-from (gnus-thread-header h1)))))
6222      (or (car extract) (cdr extract)))
6223    (let ((extract (funcall
6224                    gnus-extract-address-components 
6225                    (header-from (gnus-thread-header h2)))))
6226      (or (car extract) (cdr extract)))))
6227
6228 (defun gnus-thread-sort-by-subject (h1 h2)
6229   "Sort threads by root subject."
6230   (string-lessp
6231    (downcase (gnus-simplify-subject 
6232               (header-subject (gnus-thread-header h1))))
6233    (downcase (gnus-simplify-subject 
6234               (header-subject (gnus-thread-header h2))))))
6235
6236 (defun gnus-thread-sort-by-date (h1 h2)
6237   "Sort threads by root article date."
6238   (string-lessp
6239    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6240    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6241
6242 (defun gnus-thread-sort-by-score (h1 h2)
6243   "Sort threads by root article score.
6244 Unscored articles will be counted as having a score of zero."
6245   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6246                     gnus-newsgroup-scored))
6247          gnus-summary-default-score 0)
6248      (or (cdr (assq (header-number (gnus-thread-header h2))
6249                     gnus-newsgroup-scored))
6250          gnus-summary-default-score 0)))
6251
6252 (defun gnus-thread-sort-by-total-score (h1 h2)
6253   "Sort threads by the sum of all scores in the thread.
6254 Unscored articles will be counted as having a score of zero."
6255   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6256
6257 (defun gnus-thread-total-score (thread)
6258   ;;  This function find the total score of THREAD.
6259   (if (consp thread)
6260       (if (stringp (car thread))
6261           (apply gnus-thread-score-function 0
6262                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6263         (gnus-thread-total-score-1 thread))
6264     (gnus-thread-total-score-1 (list thread))))
6265
6266 (defun gnus-thread-total-score-1 (root)
6267   ;; This function find the total score of the thread below ROOT.
6268   (setq root (car root))
6269   (apply gnus-thread-score-function
6270          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6271              gnus-summary-default-score 0)
6272          (mapcar 'gnus-thread-total-score
6273                  (cdr (gnus-gethash (downcase (header-id root))
6274                                     gnus-newsgroup-dependencies)))))
6275
6276 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6277 (defvar gnus-tmp-prev-subject "")
6278
6279 (defun gnus-summary-prepare-threads (threads &optional cull)
6280   "Prepare summary buffer from THREADS and indentation LEVEL.  
6281 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6282 or a straight list of headers."
6283   (let ((level 0)
6284         thread header number subject stack state gnus-tmp-gathered)
6285     (if (vectorp (car threads))
6286         ;; If this is a straight (sic) list of headers, then a
6287         ;; threaded summary display isn't required, so we just create
6288         ;; an unthreaded one.
6289         (gnus-summary-prepare-unthreaded threads cull)
6290
6291       ;; Do the threaded display.
6292
6293       (while (or threads stack)
6294         
6295         (if threads
6296             ;; If there are some threads, we do them before the
6297             ;; threads on the stack.
6298             (setq thread threads
6299                   header (car (car thread)))
6300           ;; There were no current threads, so we pop something off
6301           ;; the stack. 
6302           (setq state (car stack)
6303                 level (car state)
6304                 thread (cdr state)
6305                 stack (cdr stack)
6306                 header (car (car thread))))
6307
6308         (if (stringp header)
6309             (progn
6310               ;; The header is a dummy root.
6311               (cond 
6312                ((eq gnus-summary-make-false-root 'adopt)
6313                 ;; We let the first article adopt the rest.
6314                 (let ((th (car (cdr (car thread)))))
6315                   (while (cdr th)
6316                     (setq th (cdr th)))
6317                   (setcdr th (cdr (cdr (car thread))))
6318                   (setq gnus-tmp-gathered 
6319                         (nconc (mapcar
6320                                 (lambda (h) (header-number (car h)))
6321                                 (cdr (cdr (car thread))))
6322                                gnus-tmp-gathered))
6323                   (setcdr (cdr (car thread)) nil))
6324                 (setq level -1))
6325                ((eq gnus-summary-make-false-root 'empty)
6326                 ;; We print adopted articles with empty subject fields.
6327                 (setq gnus-tmp-gathered 
6328                       (nconc (mapcar
6329                               (lambda (h) (header-number (car h)))
6330                               (cdr (cdr (car thread))))
6331                              gnus-tmp-gathered))
6332                 (setq level -1))
6333                ((eq gnus-summary-make-false-root 'dummy)
6334                 ;; We output a dummy root.
6335                 (gnus-summary-insert-dummy-line 
6336                  nil header (header-number (car (car (cdr (car thread)))))))
6337                (t
6338                 ;; We do not make a root for the gathered
6339                 ;; sub-threads at all.  
6340                 (setq level -1))))
6341       
6342           (setq number (header-number header)
6343                 subject (header-subject header))
6344
6345           ;; Do the async thing.
6346           (and gnus-newsgroup-async
6347                (setq gnus-newsgroup-threads
6348                      (cons (cons number (header-lines header)) 
6349                            gnus-newsgroup-threads)))
6350
6351           ;; We may have to root out some bad articles...
6352           (and cull
6353                (= level 0)
6354                (cond ((and (memq (setq number (header-number header))
6355                                  gnus-newsgroup-dormant)
6356                            (null thread))
6357                       (setq header nil))
6358                      ((and gnus-summary-expunge-below
6359                            (< (or (cdr (assq number gnus-newsgroup-scored))
6360                                   gnus-summary-default-score 0)
6361                               gnus-summary-expunge-below))
6362                       (setq header nil)
6363                       (setq gnus-newsgroup-unreads 
6364                             (delq number gnus-newsgroup-unreads))
6365                       (setq gnus-newsgroup-reads
6366                             (cons (cons number gnus-low-score-mark)
6367                                   gnus-newsgroup-reads)))))
6368           
6369           (and
6370            header
6371            (progn
6372              (inline
6373                (gnus-summary-insert-line
6374                 nil header level nil 
6375                 (cond 
6376                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6377                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6378                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6379                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6380                  (t (or (cdr (assq number gnus-newsgroup-reads))
6381                         gnus-ancient-mark)))
6382                 (memq number gnus-newsgroup-replied)
6383                 (memq number gnus-newsgroup-expirable)
6384                 (if (and (eq gnus-summary-make-false-root 'empty)
6385                          (memq number gnus-tmp-gathered))
6386                     gnus-summary-same-subject
6387                   (if (or (zerop level)
6388                           (and gnus-thread-ignore-subject
6389                                (not (string= 
6390                                      (gnus-simplify-subject-re
6391                                       gnus-tmp-prev-subject)
6392                                      (gnus-simplify-subject-re
6393                                       subject)))))
6394                       subject
6395                     gnus-summary-same-subject))
6396                 (and (eq gnus-summary-make-false-root 'adopt)
6397                      (memq number gnus-tmp-gathered))
6398                 (cdr (assq number gnus-newsgroup-scored)))
6399
6400                (setq gnus-tmp-prev-subject subject)))))
6401
6402         (if (nth 1 thread) 
6403             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6404         (setq level (1+ level))
6405         (setq threads (cdr (car thread)))))))
6406
6407
6408 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6409   (let (header number)
6410
6411     ;; Do the async thing, if that is required.
6412     (if gnus-newsgroup-async
6413         (setq gnus-newsgroup-threads
6414               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6415                       headers)))
6416
6417     (while headers
6418       (setq header (car headers)
6419             headers (cdr headers)
6420             number (header-number header))
6421
6422       ;; We may have to root out some bad articles...
6423       (cond 
6424        ((and cull
6425              (memq (setq number (header-number header))
6426                    gnus-newsgroup-dormant)))
6427        ((and cull gnus-summary-expunge-below
6428              (< (or (cdr (assq number gnus-newsgroup-scored))
6429                     gnus-summary-default-score 0)
6430                 gnus-summary-expunge-below))
6431         (setq gnus-newsgroup-unreads 
6432               (delq number gnus-newsgroup-unreads))
6433         (setq gnus-newsgroup-reads
6434               (cons (cons number gnus-low-score-mark)
6435                     gnus-newsgroup-reads)))
6436        (t
6437         (gnus-summary-insert-line
6438          nil header 0 nil 
6439          (cond ((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          (header-subject header) nil
6448          (cdr (assq number gnus-newsgroup-scored))))))))
6449
6450 (defun gnus-select-newsgroup (group &optional read-all)
6451   "Select newsgroup GROUP.
6452 If READ-ALL is non-nil, all articles in the group are selected."
6453   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6454          (info (nth 2 entry))
6455          articles)
6456     (gnus-check-news-server
6457      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6458
6459     (or (gnus-server-opened gnus-current-select-method)
6460         (gnus-open-server gnus-current-select-method)
6461         (error "Couldn't open server"))
6462     
6463     (or (and (eq (car entry) t)
6464              (gnus-activate-newsgroup (car info)))
6465         (gnus-request-group group t)
6466         (progn
6467           (kill-buffer (current-buffer))
6468           (error "Couldn't request group %s: %s" 
6469                  group (gnus-status-message group))))
6470
6471     (setq gnus-newsgroup-name group)
6472     (setq gnus-newsgroup-unselected nil)
6473     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6474
6475     (and gnus-asynchronous
6476          (gnus-check-backend-function 
6477           'request-asynchronous gnus-newsgroup-name)
6478          (setq gnus-newsgroup-async
6479                (gnus-request-asynchronous gnus-newsgroup-name)))
6480
6481     (setq articles (gnus-articles-to-read group read-all))
6482
6483     (cond 
6484      ((null articles) 
6485       (gnus-message 3 "Couldn't select newsgroup")
6486       'quit)
6487      ((eq articles 0) nil)
6488      (t
6489       ;; Init the dependencies hash table.
6490       (setq gnus-newsgroup-dependencies 
6491             (gnus-make-hashtable (length articles)))
6492       ;; Retrieve the headers and read them in.
6493       (gnus-message 5 "Fetching headers...")
6494       (setq gnus-newsgroup-headers 
6495             (if (eq 'nov (setq gnus-headers-retrieved-by
6496                                ;; This is a naughty hack. To get the
6497                                ;; retrieval of old headers to work, we
6498                                ;; set `nntp-nov-gap' to nil (locally),
6499                                ;; and then just retrieve the headers.
6500                                ;; Mucho magic.
6501                                (if gnus-fetch-old-headers
6502                                    (let (nntp-nov-gap)
6503                                      (gnus-retrieve-headers 
6504                                       (if (not (eq 1 (car articles)))
6505                                           (cons 1 articles)
6506                                         articles)
6507                                       gnus-newsgroup-name))
6508                                  (gnus-retrieve-headers 
6509                                   articles gnus-newsgroup-name))))
6510                 (progn
6511                   (gnus-get-newsgroup-headers-xover articles))
6512               ;; If we were to fetch old headers, but the backend didn't
6513               ;; support XOVER, then it is possible we fetched one article
6514               ;; that we shouldn't have. If that's the case, we remove it.
6515               (if (not gnus-fetch-old-headers)
6516                   ()
6517                 (save-excursion
6518                   (set-buffer nntp-server-buffer)
6519                   (goto-char (point-min))
6520                   (and 
6521                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6522                    (delete-region       ; So we delete this head.
6523                     (point) 
6524                     (search-forward "\n.\n" nil t)))))
6525               (gnus-get-newsgroup-headers)))
6526       (gnus-message 5 "Fetching headers...done")      
6527       ;; Remove canceled articles from the list of unread articles.
6528       (setq gnus-newsgroup-unreads
6529             (gnus-set-sorted-intersection 
6530              gnus-newsgroup-unreads
6531              (mapcar (lambda (headers) (header-number headers))
6532                      gnus-newsgroup-headers)))
6533       ;; Adjust and set lists of article marks.
6534       (and info
6535            (let (marked)
6536              (gnus-adjust-marked-articles info)
6537              (setq gnus-newsgroup-marked 
6538                    (copy-sequence
6539                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6540              (setq gnus-newsgroup-replied 
6541                    (copy-sequence (cdr (assq 'reply marked))))
6542              (setq gnus-newsgroup-expirable
6543                    (copy-sequence (cdr (assq 'expire marked))))
6544              (setq gnus-newsgroup-killed
6545                    (copy-sequence (cdr (assq 'killed marked))))
6546              (setq gnus-newsgroup-bookmarks 
6547                    (copy-sequence (cdr (assq 'bookmark marked))))
6548              (setq gnus-newsgroup-dormant 
6549                    (copy-sequence (cdr (assq 'dormant marked))))
6550              (setq gnus-newsgroup-scored 
6551                    (copy-sequence (cdr (assq 'score marked))))
6552              (setq gnus-newsgroup-processable nil)))
6553       ;; Create the header hashtb.
6554       (or gnus-newsgroup-headers-hashtb-by-number
6555           (gnus-make-headers-hashtable-by-number))
6556       ;; Check whether auto-expire is to be done in this group.
6557       (setq gnus-newsgroup-auto-expire
6558             (or (and (stringp gnus-auto-expirable-newsgroups)
6559                      (string-match gnus-auto-expirable-newsgroups group))
6560                 (memq 'auto-expire (nth 5 info))))
6561       ;; First and last article in this newsgroup.
6562       (and gnus-newsgroup-headers
6563            (setq gnus-newsgroup-begin 
6564                  (header-number (car gnus-newsgroup-headers)))
6565            (setq gnus-newsgroup-end
6566                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6567       (setq gnus-reffed-article-number -1)
6568       ;; GROUP is successfully selected.
6569       (or gnus-newsgroup-headers t)))))
6570
6571 (defun gnus-articles-to-read (group read-all)
6572   ;; Find out what articles the user wants to read.
6573   (let* ((articles
6574           ;; Select all articles if `read-all' is non-nil, or if all the
6575           ;; unread articles are dormant articles.
6576           (if (or (and read-all (not (numberp read-all)))
6577                   (= (length gnus-newsgroup-unreads) 
6578                      (length gnus-newsgroup-dormant)))
6579               (gnus-uncompress-range 
6580                (gnus-gethash group gnus-active-hashtb))
6581             gnus-newsgroup-unreads))
6582          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6583          (scored (length scored-list))
6584          (number (length articles))
6585          (marked (+ (length gnus-newsgroup-marked)
6586                     (length gnus-newsgroup-dormant)))
6587          (select
6588           (cond 
6589            ((numberp read-all)
6590             read-all)
6591            (t
6592             (condition-case ()
6593                 (cond ((and (or (<= scored marked)
6594                                 (= scored number))
6595                             (numberp gnus-large-newsgroup)
6596                             (> number gnus-large-newsgroup))
6597                        (let ((input
6598                               (read-string
6599                                (format
6600                                 "How many articles from %s (default %d): "
6601                                 gnus-newsgroup-name number))))
6602                          (if (string-match "^[ \t]*$" input)
6603                              number input)))
6604                       ((and (> scored marked) (< scored number))
6605                        (let ((input
6606                               (read-string
6607                                (format 
6608                                 "%s %s (%d scored, %d total): "
6609                                 "How many articles from"
6610                                 group scored number))))
6611                          (if (string-match "^[ \t]*$" input)
6612                              number input)))
6613                       (t number))
6614               (quit nil))))))
6615     (setq select (if (stringp select) (string-to-number select) select))
6616     (if (or (null select) (zerop select))
6617         select
6618       (if (and (not (zerop scored)) (<= (abs select) scored))
6619           (progn
6620             (setq articles (sort scored-list '<))
6621             (setq number (length articles)))
6622         (setq articles (copy-sequence articles)))
6623
6624       (if (< (abs select) number)
6625           (if (< select 0) 
6626               ;; Select the N oldest articles.
6627               (setcdr (nthcdr (1- (abs select)) articles) nil)
6628             ;; Select the N most recent articles.
6629             (setq articles (nthcdr (- number select) articles))))
6630       (setq gnus-newsgroup-unselected
6631             (gnus-sorted-intersection
6632              gnus-newsgroup-unreads
6633              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6634       articles)))
6635
6636 (defun gnus-killed-articles (killed articles)
6637   (let (out)
6638     (while articles
6639       (if (inline (gnus-member-of-range (car articles) killed))
6640           (setq out (cons (car articles) out)))
6641       (setq articles (cdr articles)))
6642     out))
6643
6644 (defun gnus-adjust-marked-articles (info &optional active)
6645   "Remove all marked articles that are no longer legal."
6646   (let ((marked-lists (nth 3 info))
6647         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6648         m prev)
6649     ;; There are many types of marked articles.
6650     (while marked-lists
6651       (setq m (cdr (setq prev (car marked-lists))))
6652       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6653              ;; Make sure that all ticked articles are a subset of the
6654              ;; unread/unselected articles.
6655              (while m
6656                (if (or (memq (car m) gnus-newsgroup-unreads)
6657                        (memq (car m) gnus-newsgroup-unselected))
6658                    (setq prev m)
6659                  (setcdr prev (cdr m)))
6660                (setq m (cdr m))))
6661             ((eq 'score (car prev))
6662              ;; Scored articles should be a subset of
6663              ;; unread/unselected articles. 
6664              (while m
6665                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6666                        (memq (car (car m)) gnus-newsgroup-unreads))
6667                    (setq prev m)
6668                  (setcdr prev (cdr m)))
6669                (setq m (cdr m))))
6670             ((eq 'bookmark (car prev))
6671              ;; Bookmarks should be a subset of active articles.
6672              (while m
6673                (if (< (car (car m)) (car active))
6674                    (setcdr prev (cdr m))
6675                  (setq prev m))
6676                (setq m (cdr m))))
6677             ((eq 'killed (car prev))
6678              ;; Articles that have been through the kill process are
6679              ;; to be a subset of active articles.
6680              (while (and m (< (or (and (numberp (car m)) (car m))
6681                                   (cdr (car m)))
6682                               (car active)))
6683                (setcdr prev (cdr m))
6684                (setq m (cdr m)))
6685              (if (and m (< (or (and (numberp (car m)) (car m))
6686                                (car (car m)))
6687                            (car active))) 
6688                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6689             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6690              ;; The replied and expirable articles have to be articles
6691              ;; that are active. 
6692              (while m
6693                (if (< (car m) (car active))
6694                    (setcdr prev (cdr m))
6695                  (setq prev m))
6696                (setq m (cdr m)))))
6697       (setq marked-lists (cdr marked-lists)))
6698     ;; Remove all lists that are empty.
6699     (setq marked-lists (nth 3 info))
6700     (if marked-lists
6701         (progn
6702           (while (= 1 (length (car marked-lists)))
6703             (setq marked-lists (cdr marked-lists)))
6704           (setq m (cdr (setq prev marked-lists)))
6705           (while m
6706             (if (= 1 (length (car m)))
6707                 (setcdr prev (cdr m))
6708               (setq prev m))
6709             (setq m (cdr m)))
6710           (setcar (nthcdr 3 info) marked-lists)))
6711     ;; Finally, if there are no marked lists at all left, and if there
6712     ;; are no elements after the lists in the info list, we just chop
6713     ;; the info list off before the marked lists.
6714     (and (null marked-lists) 
6715          (not (nthcdr 4 info))
6716          (setcdr (nthcdr 2 info) nil)))
6717   info)
6718
6719 (defun gnus-set-marked-articles 
6720   (info ticked replied expirable killed dormant bookmark score) 
6721   "Enter the various lists of marked articles into the newsgroup info list."
6722   (let (newmarked)
6723     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6724     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6725     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6726                                          newmarked)))
6727     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6728     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6729     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6730                                         newmarked)))
6731     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6732     (if (nthcdr 3 info)
6733         (progn
6734           (setcar (nthcdr 3 info) newmarked)
6735           (and (not newmarked)
6736                (not (nthcdr 4 info))
6737                (setcdr (nthcdr 2 info) nil)))
6738       (if newmarked
6739           (setcdr (nthcdr 2 info) (list newmarked))))))
6740
6741 (defun gnus-add-marked-articles (group type articles &optional info force)
6742   ;; Add ARTICLES of TYPE to the info of GROUP.
6743   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6744   ;; add, but replace marked articles of TYPE with ARTICLES.
6745   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6746         marked m)
6747     (or (not info)
6748         (and (not (setq marked (nthcdr 3 info)))
6749              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6750         (and (not (setq m (assq type (car marked))))
6751              (setcar marked (cons (cons type articles) (car marked))))
6752         (if force
6753             (setcdr m articles)
6754           (nconc m articles)))))
6755          
6756 (defun gnus-set-mode-line (where)
6757   "This function sets the mode line of the article or summary buffers.
6758 If WHERE is `summary', the summary mode line format will be used."
6759   (if (memq where gnus-updated-mode-lines)
6760       (let (mode-string)
6761         (save-excursion
6762           (set-buffer gnus-summary-buffer)
6763           (let* ((mformat (if (eq where 'article) 
6764                               gnus-article-mode-line-format-spec
6765                             gnus-summary-mode-line-format-spec))
6766                  (group-name gnus-newsgroup-name)
6767                  (article-number (or gnus-current-article 0))
6768                  (unread (- (length gnus-newsgroup-unreads)
6769                             (length gnus-newsgroup-dormant)))
6770                  (unread-and-unticked 
6771                   (- unread (length gnus-newsgroup-marked)))
6772                  (unselected (length gnus-newsgroup-unselected))
6773                  (unread-and-unselected
6774                   (cond ((and (zerop unread-and-unticked)
6775                               (zerop unselected)) "")
6776                         ((zerop unselected) 
6777                          (format "{%d more}" unread-and-unticked))
6778                         (t (format "{%d(+%d) more}"
6779                                    unread-and-unticked unselected))))
6780                  (subject
6781                   (if gnus-current-headers
6782                       (header-subject gnus-current-headers) ""))
6783                  (max-len (and gnus-mode-non-string-length
6784                                (- (frame-width) gnus-mode-non-string-length)))
6785                  header) ;; passed as argument to any user-format-funcs
6786             (setq mode-string (eval mformat))
6787             (or (numberp max-len)
6788                 (setq max-len (length mode-string)))
6789             (if (< max-len 4) (setq max-len 4))
6790             (if (> (length mode-string) max-len)
6791                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6792                 ;;  function `substring' might cut on a middle
6793                 ;;  of multi-octet character.
6794                 (setq mode-string 
6795                       (concat (gnus-truncate-string mode-string (- max-len 3))
6796                               "...")))
6797             (setq mode-string (format (format "%%-%ds" max-len)
6798                                       mode-string))))
6799         (setq mode-line-buffer-identification mode-string)
6800         (set-buffer-modified-p t))))
6801
6802 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6803   "Go through the HEADERS list and add all Xrefs to a hash table.
6804 The resulting hash table is returned, or nil if no Xrefs were found."
6805   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6806          (prefix (if (and 
6807                       (gnus-group-foreign-p from-newsgroup)
6808                       (not (memq 'virtual 
6809                                  (assoc (symbol-name (car from-method))
6810                                         gnus-valid-select-methods))))
6811                      (gnus-group-real-prefix from-newsgroup)))
6812          (xref-hashtb (make-vector 63 0))
6813          start group entry number xrefs header)
6814     (while headers
6815       (setq header (car headers))
6816       (if (and (setq xrefs (header-xref header))
6817                (not (memq (header-number header) unreads)))
6818           (progn
6819             (setq start 0)
6820             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6821               (setq start (match-end 0))
6822               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6823                                                     (match-end 1))))
6824               (setq number 
6825                     (string-to-int (substring xrefs (match-beginning 2) 
6826                                               (match-end 2))))
6827               (if (setq entry (gnus-gethash group xref-hashtb))
6828                   (setcdr entry (cons number (cdr entry)))
6829                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6830       (setq headers (cdr headers)))
6831     (if start xref-hashtb nil)))
6832
6833 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6834   "Look through all the headers and mark the Xrefs as read."
6835   (let ((virtual (memq 'virtual 
6836                        (assoc (symbol-name (car (gnus-find-method-for-group 
6837                                                  from-newsgroup)))
6838                               gnus-valid-select-methods)))
6839         name entry info xref-hashtb idlist method
6840         nth4)
6841     (save-excursion
6842       (set-buffer gnus-group-buffer)
6843       (if (setq xref-hashtb 
6844                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6845           (mapatoms 
6846            (lambda (group)
6847              (if (string= from-newsgroup (setq name (symbol-name group)))
6848                  ()
6849                (setq idlist (symbol-value group))
6850                ;; Dead groups are not updated.
6851                (if (and (prog1 
6852                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6853                                   info (nth 2 entry))
6854                           (if (stringp (setq nth4 (nth 4 info)))
6855                               (setq nth4 (gnus-server-to-method nth4))))
6856                         ;; Only do the xrefs if the group has the same
6857                         ;; select method as the group we have just read.
6858                         (or (gnus-methods-equal-p 
6859                              nth4 (gnus-find-method-for-group from-newsgroup))
6860                             virtual
6861                             (equal nth4 
6862                                    (setq method (gnus-find-method-for-group 
6863                                                  from-newsgroup)))
6864                             (and (equal (car nth4) (car method))
6865                                  (equal (nth 1 nth4) (nth 1 method))))
6866                         gnus-use-cross-reference
6867                         (or (not (eq gnus-use-cross-reference t))
6868                             virtual
6869                             ;; Only do cross-references on subscribed
6870                             ;; groups, if that is what is wanted.  
6871                             (<= (nth 1 info) gnus-level-subscribed)))
6872                    (gnus-group-make-articles-read name idlist expirable))))
6873            xref-hashtb)))))
6874
6875 (defun gnus-group-make-articles-read (group articles expirable)
6876   (let* ((num 0)
6877          (entry (gnus-gethash group gnus-newsrc-hashtb))
6878          (info (nth 2 entry))
6879          (active (gnus-gethash group gnus-active-hashtb))
6880          exps expirable range)
6881     ;; First peel off all illegal article numbers.
6882     (if active
6883         (let ((ids articles)
6884               (ticked (cdr (assq 'tick (nth 3 info))))
6885               (dormant (cdr (assq 'dormant (nth 3 info))))
6886               id)
6887           (setq exps nil)
6888           (while ids
6889             (setq id (car ids))
6890             (if (or (> id (cdr active))
6891                     (< id (car active))
6892                     (memq id ticked)
6893                     (memq id dormant))
6894                 (setq articles (delq id articles)))
6895             (and (memq id expirable)
6896                  (setq exps (cons id exps)))
6897             (setq ids (cdr ids)))))
6898     ;; Update expirable articles.
6899     (gnus-add-marked-articles nil 'expirable exps info)
6900     (and active
6901          (null (nth 2 info))
6902          (> (car active) 1)
6903          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6904     (setcar (nthcdr 2 info)
6905             (setq range
6906                   (gnus-add-to-range 
6907                    (nth 2 info) 
6908                    (setq articles (sort articles '<)))))
6909     ;; Then we have to re-compute how many unread
6910     ;; articles there are in this group.
6911     (if active
6912         (progn
6913           (cond 
6914            ((not range)
6915             (setq num (- (1+ (cdr active)) (car active))))
6916            ((not (listp (cdr range)))
6917             (setq num (- (cdr active) (- (1+ (cdr range)) 
6918                                          (car range)))))
6919            (t
6920             (while range
6921               (if (numberp (car range))
6922                   (setq num (1+ num))
6923                 (setq num (+ num (- (1+ (cdr (car range)))
6924                                     (car (car range))))))
6925               (setq range (cdr range)))
6926             (setq num (- (cdr active) num))))
6927           ;; Update the number of unread articles.
6928           (setcar 
6929            entry 
6930            (max 0 (- num 
6931                      (length (cdr (assq 'tick (nth 3 info))))
6932                      (length 
6933                       (cdr (assq 'dormant (nth 3 info)))))))
6934           ;; Update the group buffer.
6935           (gnus-group-update-group group t)))))
6936
6937 (defun gnus-methods-equal-p (m1 m2)
6938   (let ((m1 (or m1 gnus-select-method))
6939         (m2 (or m2 gnus-select-method)))
6940     (or (equal m1 m2)
6941         (and (eq (car m1) (car m2))
6942              (or (not (memq 'address (assoc (symbol-name (car m1))
6943                                             gnus-valid-select-methods)))
6944                  (equal (nth 1 m1) (nth 1 m2)))))))
6945
6946 (defsubst gnus-header-value ()
6947   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6948
6949 (defvar gnus-newsgroup-none-id 0)
6950
6951 (defun gnus-get-newsgroup-headers ()
6952   (setq gnus-article-internal-prepare-hook nil)
6953   (let ((cur nntp-server-buffer)
6954         (dependencies gnus-newsgroup-dependencies)
6955         headers id dep end ref)
6956     (save-excursion
6957       (set-buffer nntp-server-buffer)
6958       (goto-char (point-min))
6959       ;; Search to the beginning of the next header. Error messages
6960       ;; do not begin with 2 or 3.
6961       (while (re-search-forward "^[23][0-9]+ " nil t)
6962         (let ((header (make-vector 9 nil))
6963               (case-fold-search t)
6964               (p (point))
6965               in-reply-to)
6966           (setq id nil
6967                 ref nil)
6968           (header-set-number header (read cur))
6969           ;; This implementation of this function, with nine
6970           ;; search-forwards instead of the one re-search-forward and
6971           ;; a case (which basically was the old function) is actually
6972           ;; about twice as fast, even though it looks messier. You
6973           ;; can't have everything, I guess. Speed and elegance
6974           ;; doesn't always come hand in hand.
6975           (save-restriction
6976             (narrow-to-region (point) (or (save-excursion 
6977                                             (search-forward "\n.\n" nil t))
6978                                           (point)))
6979             (if (search-forward "\nfrom: " nil t)
6980                 (header-set-from header (gnus-header-value))
6981               (header-set-from header "(nobody)"))
6982             (goto-char p)
6983             (if (search-forward "\nsubject: " nil t)
6984                 (header-set-subject header (gnus-header-value))
6985               (header-set-subject header "(none)"))
6986             (goto-char p)
6987             (and (search-forward "\nxref: " nil t)
6988                  (header-set-xref header (gnus-header-value)))
6989             (goto-char p)
6990             (or (numberp (and (search-forward "\nlines: " nil t)
6991                               (header-set-lines header (read cur))))
6992                 (header-set-lines header 0))
6993             (goto-char p)
6994             (and (search-forward "\ndate: " nil t)
6995                  (header-set-date header (gnus-header-value)))
6996             (goto-char p)
6997             (if (search-forward "\nmessage-id: " nil t)
6998                 (header-set-id header (setq id (gnus-header-value)))
6999               ;; If there was no message-id, we just fake one to make
7000               ;; subsequent routines simpler.
7001               (header-set-id 
7002                header 
7003                (setq id (concat "none+" 
7004                                 (int-to-string 
7005                                  (setq gnus-newsgroup-none-id 
7006                                        (1+ gnus-newsgroup-none-id)))))))
7007             (goto-char p)
7008             (if (search-forward "\nreferences: " nil t)
7009                 (progn
7010                   (header-set-references header (gnus-header-value))
7011                   (setq end (match-end 0))
7012                   (save-excursion
7013                     (setq ref 
7014                           (downcase
7015                            (buffer-substring
7016                             (progn 
7017                               (end-of-line)
7018                               (search-backward ">" end t)
7019                               (1+ (point)))
7020                             (progn
7021                               (search-backward "<" end t)
7022                               (point)))))))
7023               ;; Get the references from the in-reply-to header if there
7024               ;; ware no references and the in-reply-to header looks
7025               ;; promising. 
7026               (if (and (search-forward "\nin-reply-to: " nil t)
7027                        (setq in-reply-to (gnus-header-value))
7028                        (string-match "<[^>]+>" in-reply-to))
7029                   (progn
7030                     (header-set-references 
7031                      header 
7032                      (setq ref (substring in-reply-to (match-beginning 0)
7033                                           (match-end 0))))
7034                     (setq ref (downcase ref)))
7035                 (setq ref "none")))
7036             ;; We do some threading while we read the headers. The
7037             ;; message-id and the last reference are both entered into
7038             ;; the same hash table. Some tippy-toeing around has to be
7039             ;; done in case an article has arrived before the article
7040             ;; which it refers to.
7041             (if (boundp (setq dep (intern (downcase id) dependencies)))
7042                 (if (car (symbol-value dep))
7043                     ;; An article with this Message-ID has already
7044                     ;; been seen, so we ignore this one, except we add
7045                     ;; any additional Xrefs (in case the two articles
7046                     ;; came from different servers.
7047                     (progn
7048                       (header-set-xref 
7049                        (car (symbol-value dep))
7050                        (concat (or (header-xref (car (symbol-value dep))) "")
7051                                (or (header-xref header) "")))
7052                       (setq header nil))
7053                   (setcar (symbol-value dep) header))
7054               (set dep (list header)))
7055             (if header
7056                 (progn
7057                   (if (boundp (setq dep (intern ref dependencies)))
7058                       (setcdr (symbol-value dep) 
7059                               (cons header (cdr (symbol-value dep))))
7060                     (set dep (list nil header)))
7061                   (setq headers (cons header headers))))
7062             (goto-char (point-max))))))
7063     (nreverse headers)))
7064
7065 ;; The following macros and functions were written by Felix Lee
7066 ;; <flee@cse.psu.edu>. 
7067
7068 (defmacro gnus-nov-read-integer ()
7069   '(prog1
7070        (if (= (following-char) ?\t)
7071            0
7072          (let ((num (condition-case nil (read buffer) (error nil))))
7073            (if (numberp num) num 0)))
7074      (or (eobp) (forward-char 1))))
7075
7076 (defmacro gnus-nov-skip-field ()
7077   '(search-forward "\t" eol 'move))
7078
7079 (defmacro gnus-nov-field ()
7080   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7081
7082 ;; Goes through the xover lines and returns a list of vectors
7083 (defun gnus-get-newsgroup-headers-xover (sequence)
7084   "Parse the news overview data in the server buffer, and return a
7085 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7086   ;; Get the Xref when the users reads the articles since most/some
7087   ;; NNTP servers do not include Xrefs when using XOVER.
7088   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7089   (let ((cur nntp-server-buffer)
7090         (dependencies gnus-newsgroup-dependencies)
7091         number headers header)
7092     (save-excursion
7093       (set-buffer nntp-server-buffer)
7094       (goto-char (point-min))
7095       (while (and sequence (not (eobp)))
7096         (setq number (read cur))
7097         (while (and sequence (< (car sequence) number))
7098           (setq sequence (cdr sequence)))
7099         (and sequence 
7100              (eq number (car sequence))
7101              (progn
7102                (setq sequence (cdr sequence))
7103                (if (setq header 
7104                          (inline (gnus-nov-parse-line number dependencies)))
7105                    (setq headers (cons header headers)))))
7106         (forward-line 1))
7107       (setq headers (nreverse headers)))
7108     headers))
7109
7110 ;; This function has to be called with point after the article number
7111 ;; on the beginning of the line.
7112 (defun gnus-nov-parse-line (number dependencies)
7113   (let ((none 0)
7114         (eol (gnus-point-at-eol)) 
7115         (buffer (current-buffer))
7116         header ref id dep)
7117
7118     ;; overview: [num subject from date id refs chars lines misc]
7119     (narrow-to-region (point) eol)
7120     (forward-char)
7121
7122     (condition-case nil
7123         (setq header
7124               (vector 
7125                number                   ; number
7126                (gnus-nov-field)         ; subject
7127                (gnus-nov-field)         ; from
7128                (gnus-nov-field)         ; date
7129                (setq id (or (gnus-nov-field)
7130                             (concat "none+"
7131                                     (int-to-string 
7132                                      (setq none (1+ none)))))) ; id
7133                (progn
7134                  (save-excursion
7135                    (let ((beg (point)))
7136                      (search-forward "\t" eol)
7137                      (if (search-backward ">" beg t)
7138                          (setq ref 
7139                                (downcase 
7140                                 (buffer-substring 
7141                                  (1+ (point))
7142                                  (progn
7143                                    (search-backward "<" beg t)
7144                                    (point)))))
7145                        (setq ref nil))))
7146                  (gnus-nov-field))      ; refs
7147                (gnus-nov-read-integer)  ; chars
7148                (gnus-nov-read-integer)  ; lines
7149                (if (= (following-char) ?\n)
7150                    nil
7151                  (gnus-nov-field))      ; misc
7152                ))
7153       (error (progn 
7154                (ding)
7155                (message "Strange nov line.")
7156                (setq header nil)
7157                (goto-char eol))))
7158
7159     (widen)
7160
7161     ;; We build the thread tree.
7162     (and header
7163          (if (boundp (setq dep (intern (downcase id) dependencies)))
7164              (if (car (symbol-value dep))
7165                  ;; An article with this Message-ID has already been seen,
7166                  ;; so we ignore this one, except we add any additional
7167                  ;; Xrefs (in case the two articles came from different
7168                  ;; servers.
7169                  (progn
7170                    (header-set-xref 
7171                     (car (symbol-value dep))
7172                     (concat (or (header-xref (car (symbol-value dep))) "")
7173                             (or (header-xref header) "")))
7174                    (setq header nil))
7175                (setcar (symbol-value dep) header))
7176            (set dep (list header))))
7177     (if header
7178         (progn
7179           (if (boundp (setq dep (intern (or ref "none") 
7180                                         dependencies)))
7181               (setcdr (symbol-value dep) 
7182                       (cons header (cdr (symbol-value dep))))
7183             (set dep (list nil header)))))
7184     header))
7185
7186 (defun gnus-article-get-xrefs ()
7187   "Fill in the Xref value in `gnus-current-headers', if necessary.
7188 This is meant to be called in `gnus-article-internal-prepare-hook'."
7189   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7190                                  gnus-current-headers)))
7191     (or (not gnus-use-cross-reference)
7192         (not headers)
7193         (and (header-xref headers)
7194              (not (string= (header-xref headers) "")))
7195         (let ((case-fold-search t)
7196               xref)
7197           (save-restriction
7198             (gnus-narrow-to-headers)
7199             (goto-char (point-min))
7200             (if (or (and (eq (downcase (following-char)) ?x)
7201                          (looking-at "Xref:"))
7202                     (search-forward "\nXref:" nil t))
7203                 (progn
7204                   (goto-char (1+ (match-end 0)))
7205                   (setq xref (buffer-substring (point) 
7206                                                (progn (end-of-line) (point))))
7207                   (header-set-xref headers xref))))))))
7208
7209 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7210 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7211
7212 (defun gnus-make-headers-hashtable-by-number ()
7213   "Make hashtable for the variable gnus-newsgroup-headers by number."
7214   (save-excursion
7215     (set-buffer gnus-summary-buffer)
7216     (let ((headers gnus-newsgroup-headers)
7217           header)
7218       (setq gnus-newsgroup-headers-hashtb-by-number
7219             (gnus-make-hashtable (length headers)))
7220       (while headers
7221         (setq header (car headers))
7222         (gnus-sethash (int-to-string (header-number header))
7223                       header gnus-newsgroup-headers-hashtb-by-number)
7224         (setq headers (cdr headers))))))
7225
7226 (defun gnus-more-header-backward ()
7227   "Find new header backward."
7228   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7229         (artnum gnus-newsgroup-begin)
7230         (header nil))
7231     (while (and (not header)
7232                 (> artnum first))
7233       (setq artnum (1- artnum))
7234       (setq header (gnus-read-header artnum)))
7235     header))
7236
7237 (defun gnus-more-header-forward (&optional backward)
7238   "Find new header forward.
7239 If BACKWARD, find new header backward instead."
7240   (if backward
7241       (gnus-more-header-backward)
7242     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7243           (artnum gnus-newsgroup-end)
7244           (header nil))
7245       (while (and (not header)
7246                   (< artnum last))
7247         (setq artnum (1+ artnum))
7248         (setq header (gnus-read-header artnum)))
7249       header)))
7250
7251 (defun gnus-extend-newsgroup (header &optional backward)
7252   "Extend newsgroup selection with HEADER.
7253 Optional argument BACKWARD means extend toward backward."
7254   (if header
7255       (let ((artnum (header-number header)))
7256         (setq gnus-newsgroup-headers
7257               (if backward
7258                   (cons header gnus-newsgroup-headers)
7259                 (nconc gnus-newsgroup-headers (list header))))
7260         (setq gnus-newsgroup-unselected
7261               (delq artnum gnus-newsgroup-unselected))
7262         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7263         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7264
7265 (defun gnus-summary-work-articles (n)
7266   "Return a list of articles to be worked upon. The prefix argument,
7267 the list of process marked articles, and the current article will be
7268 taken into consideration."
7269   (let (articles)
7270     (if (and n (numberp n))
7271         (let ((backward (< n 0))
7272               (n (abs n)))
7273           (save-excursion
7274             (while (and (> n 0)
7275                         (setq articles (cons (gnus-summary-article-number) 
7276                                              articles))
7277                         (gnus-summary-search-forward nil nil backward))
7278               (setq n (1- n))))
7279           (sort articles (function <)))
7280       (or (reverse gnus-newsgroup-processable)
7281           (list (gnus-summary-article-number))))))
7282
7283 (defun gnus-summary-search-group (&optional backward use-level)
7284   "Search for next unread newsgroup.
7285 If optional argument BACKWARD is non-nil, search backward instead."
7286   (save-excursion
7287     (set-buffer gnus-group-buffer)
7288     (if (gnus-group-search-forward 
7289          backward nil (if use-level (gnus-group-group-level) nil))
7290         (gnus-group-group-name))))
7291
7292 (defun gnus-summary-best-group (&optional exclude-group)
7293   "Find the name of the best unread group.
7294 If EXCLUDE-GROUP, do not go to this group."
7295   (save-excursion
7296     (set-buffer gnus-group-buffer)
7297     (save-excursion
7298       (gnus-group-best-unread-group exclude-group))))
7299
7300 (defun gnus-subject-equal (s1 s2)
7301   (cond
7302    ((null gnus-summary-gather-subject-limit)
7303     (equal (gnus-simplify-subject-re s1)
7304            (gnus-simplify-subject-re s2)))
7305    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7306     (equal (gnus-simplify-subject-fuzzy s1)
7307            (gnus-simplify-subject-fuzzy s2)))
7308    ((numberp gnus-summary-gather-subject-limit)
7309     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7310            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7311    (t
7312     (equal s1 s2))))
7313     
7314 (defun gnus-summary-search-subject (&optional backward unread subject)
7315   "Search for article forward.
7316 If BACKWARD is non-nil, search backward.
7317 If UNREAD is non-nil, only unread articles are selected.
7318 If SUBJECT is non-nil, the article which has the same subject will be
7319 searched for." 
7320   (let ((func (if backward 'previous-single-property-change
7321                 'next-single-property-change))
7322         (beg (point))
7323         (did t)
7324         pos psubject)
7325     (beginning-of-line)
7326     (and gnus-summary-check-current unread
7327          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7328          (setq did nil))
7329     (if (not did)
7330         ()
7331       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7332       (while
7333           (and 
7334            (setq pos (funcall func (point) 'gnus-number))
7335            (goto-char (if backward (1- pos) pos))
7336            (setq did
7337                  (not (and
7338                        (or (not unread)
7339                            (eq (get-text-property (point) 'gnus-mark)
7340                                gnus-unread-mark))
7341                        (or (not subject)
7342                            (and (setq psubject (inline (gnus-summary-subject-string)))
7343                                 (inline (gnus-subject-equal subject psubject)))))))
7344            (if backward (if (bobp) nil (forward-char -1) t)
7345              (if (eobp) nil (forward-char 1) t)))))
7346     (if did
7347         (progn (goto-char beg) nil)
7348       (prog1
7349           (get-text-property (point) 'gnus-number)
7350         (gnus-summary-position-cursor)))))
7351
7352 (defun gnus-summary-pseudo-article ()
7353   "The thread level of the article on the current line."
7354   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7355
7356 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7357 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7358 (defun gnus-summary-article-score ()
7359   "Return current article score."
7360   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7361       gnus-summary-default-score 0))
7362
7363 (defun gnus-summary-recenter ()
7364   "Center point in the summary window.
7365 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7366 displayed, no centering will be performed." 
7367   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7368   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7369   (let* ((top (cond ((< (window-height) 4) 0)
7370                     ((< (window-height) 7) 1)
7371                     (t 2)))
7372          (height (1- (window-height)))
7373          (bottom (save-excursion (goto-char (point-max))
7374                                  (forward-line (- height))
7375                                  (point)))
7376          (window (get-buffer-window (current-buffer))))
7377     (and 
7378      ;; The user has to want it,
7379      gnus-auto-center-summary 
7380      ;; the article buffer must be displayed,
7381      (get-buffer-window gnus-article-buffer)
7382      ;; Set the window start to either `bottom', which is the biggest
7383      ;; possible valid number, or the second line from the top,
7384      ;; whichever is the least.
7385      (set-window-start
7386       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7387
7388 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7389 (defun gnus-short-group-name (group &optional levels)
7390   "Collapse GROUP name LEVELS."
7391   (let* ((name "") (foreign "") (depth -1) (skip 1)
7392          (levels (or levels
7393                      (progn
7394                        (while (string-match "\\." group skip)
7395                          (setq skip (match-end 0)
7396                                depth (+ depth 1)))
7397                        depth))))
7398     (if (string-match ":" group)
7399         (setq foreign (substring group 0 (match-end 0))
7400               group (substring group (match-end 0))))
7401     (while group
7402       (if (and (string-match "\\." group) (> levels 0))
7403           (setq name (concat name (substring group 0 1))
7404                 group (substring group (match-end 0))
7405                 levels (- levels 1)
7406                 name (concat name "."))
7407         (setq name (concat foreign name group)
7408               group nil)))
7409     name))
7410
7411 (defun gnus-summary-jump-to-group (newsgroup)
7412   "Move point to NEWSGROUP in group mode buffer."
7413   ;; Keep update point of group mode buffer if visible.
7414   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7415       (save-window-excursion
7416         ;; Take care of tree window mode.
7417         (if (get-buffer-window gnus-group-buffer)
7418             (pop-to-buffer gnus-group-buffer))
7419         (gnus-group-jump-to-group newsgroup))
7420     (save-excursion
7421       ;; Take care of tree window mode.
7422       (if (get-buffer-window gnus-group-buffer)
7423           (pop-to-buffer gnus-group-buffer)
7424         (set-buffer gnus-group-buffer))
7425       (gnus-group-jump-to-group newsgroup))))
7426
7427 ;; This function returns a list of article numbers based on the
7428 ;; difference between the ranges of read articles in this group and
7429 ;; the range of active articles.
7430 (defun gnus-list-of-unread-articles (group)
7431   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7432          (active (gnus-gethash group gnus-active-hashtb))
7433          (last (cdr active))
7434          first nlast unread)
7435     ;; If none are read, then all are unread. 
7436     (if (not read)
7437         (setq first (car active))
7438       ;; If the range of read articles is a single range, then the
7439       ;; first unread article is the article after the last read
7440       ;; article. Sounds logical, doesn't it?
7441       (if (not (listp (cdr read)))
7442           (setq first (1+ (cdr read)))
7443         ;; `read' is a list of ranges.
7444         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7445                                 (car (car read)))) 1)
7446             (setq first 1))
7447         (while read
7448           (if first 
7449               (while (< first nlast)
7450                 (setq unread (cons first unread))
7451                 (setq first (1+ first))))
7452           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7453           (setq nlast (if (atom (car (cdr read))) 
7454                           (car (cdr read))
7455                         (car (car (cdr read)))))
7456           (setq read (cdr read)))))
7457     ;; And add the last unread articles.
7458     (while (<= first last)
7459       (setq unread (cons first unread))
7460       (setq first (1+ first)))
7461     ;; Return the list of unread articles.
7462     (nreverse unread)))
7463
7464 (defun gnus-list-of-read-articles (group)
7465   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7466         (active (gnus-gethash group gnus-active-hashtb)))
7467     (and info active
7468          (gnus-sorted-complement 
7469           (gnus-uncompress-range active) 
7470           (gnus-list-of-unread-articles group)))))
7471
7472 ;; Various summary commands
7473
7474 (defun gnus-summary-universal-argument ()
7475   "Perform any operation on all articles marked with the process mark."
7476   (interactive)
7477   (gnus-set-global-variables)
7478   (let ((articles (reverse gnus-newsgroup-processable))
7479         func)
7480     (or articles (error "No articles marked"))
7481     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7482         (error "Undefined key"))
7483     (while articles
7484       (gnus-summary-goto-subject (car articles))
7485       (command-execute func)
7486       (gnus-summary-remove-process-mark (car articles))
7487       (setq articles (cdr articles)))))
7488
7489 (defun gnus-summary-toggle-truncation (&optional arg)
7490   "Toggle truncation of summary lines.
7491 With arg, turn line truncation on iff arg is positive."
7492   (interactive "P")
7493   (setq truncate-lines
7494         (if (null arg) (not truncate-lines)
7495           (> (prefix-numeric-value arg) 0)))
7496   (redraw-display))
7497
7498 (defun gnus-summary-reselect-current-group (&optional all)
7499   "Once exit and then reselect the current newsgroup.
7500 The prefix argument ALL means to select all articles."
7501   (interactive "P")
7502   (gnus-set-global-variables)
7503   (let ((current-subject (gnus-summary-article-number))
7504         (group gnus-newsgroup-name))
7505     (setq gnus-newsgroup-begin nil)
7506     (gnus-summary-exit t)
7507     ;; We have to adjust the point of group mode buffer because the
7508     ;; current point was moved to the next unread newsgroup by
7509     ;; exiting.
7510     (gnus-summary-jump-to-group group)
7511     (gnus-group-read-group all t)
7512     (gnus-summary-goto-subject current-subject)))
7513
7514 (defun gnus-summary-rescan-group (&optional all)
7515   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7516   (interactive "P")
7517   (gnus-set-global-variables)
7518   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7519   (let ((group gnus-newsgroup-name))
7520     (gnus-summary-exit)
7521     (gnus-summary-jump-to-group group)
7522     (save-excursion
7523       (set-buffer gnus-group-buffer)
7524       (gnus-group-get-new-news-this-group 1))
7525     (gnus-summary-jump-to-group group)
7526     (gnus-group-read-group all)))
7527
7528 (defun gnus-summary-update-info ()
7529   (let* ((group gnus-newsgroup-name))
7530     (if gnus-newsgroup-kill-headers
7531         (setq gnus-newsgroup-killed
7532               (gnus-compress-sequence
7533                (nconc
7534                 (gnus-set-sorted-intersection
7535                  (gnus-uncompress-range gnus-newsgroup-killed)
7536                  (setq gnus-newsgroup-unselected
7537                        (sort gnus-newsgroup-unselected '<)))
7538                 (setq gnus-newsgroup-unreads
7539                       (sort gnus-newsgroup-unreads '<))) t)))
7540     (or (listp (cdr gnus-newsgroup-killed))
7541         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7542     (let ((headers gnus-newsgroup-headers))
7543       (gnus-close-group group)
7544       (run-hooks 'gnus-exit-group-hook)
7545       (gnus-update-read-articles 
7546        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7547        gnus-newsgroup-marked
7548        t gnus-newsgroup-replied gnus-newsgroup-expirable
7549        gnus-newsgroup-killed gnus-newsgroup-dormant
7550        gnus-newsgroup-bookmarks 
7551        (and gnus-save-score gnus-newsgroup-scored))
7552       (and gnus-use-cross-reference
7553            (gnus-mark-xrefs-as-read 
7554             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7555       ;; Do adaptive scoring, and possibly save score files.
7556       (and gnus-newsgroup-adaptive
7557            (gnus-score-adaptive))
7558       (and gnus-use-scoring 
7559            (fboundp 'gnus-score-save)
7560            (funcall 'gnus-score-save))
7561       ;; Do not switch windows but change the buffer to work.
7562       (set-buffer gnus-group-buffer)
7563       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7564           (gnus-group-update-group group)))))
7565   
7566 (defun gnus-summary-exit (&optional temporary)
7567   "Exit reading current newsgroup, and then return to group selection mode.
7568 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7569   (interactive)
7570   (gnus-set-global-variables)
7571   (gnus-kill-save-kill-buffer)
7572   (let* ((group gnus-newsgroup-name)
7573          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7574                                                   gnus-newsgroup-name))))
7575          (mode major-mode)
7576          (buf (current-buffer)))
7577     (run-hooks 'gnus-summary-prepare-exit-hook)
7578     (gnus-summary-update-info) ; Make all changes in this group permanent.
7579     (set-buffer buf)
7580     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7581     ;; Make sure where I was, and go to next newsgroup.
7582     (set-buffer gnus-group-buffer)
7583     (or quit-config
7584         (progn
7585           (gnus-group-jump-to-group group)
7586           (gnus-group-next-unread-group 1)))
7587     (if temporary
7588         nil                             ;Nothing to do.
7589       ;; We set all buffer-local variables to nil. It is unclear why
7590       ;; this is needed, but if we don't, buffer-local variables are
7591       ;; not garbage-collected, it seems. This would the lead to en
7592       ;; ever-growing Emacs.
7593       (set-buffer buf)
7594       (gnus-summary-clear-local-variables)
7595       ;; We clear the global counterparts of the buffer-local
7596       ;; variables as well, just to be on the safe side.
7597       (gnus-configure-windows 'group 'force)
7598       (gnus-summary-clear-local-variables)
7599       ;; Return to group mode buffer. 
7600       (if (eq mode 'gnus-summary-mode)
7601           (gnus-kill-buffer buf))
7602       (if (get-buffer gnus-article-buffer)
7603           (bury-buffer gnus-article-buffer))
7604       (setq gnus-current-select-method gnus-select-method)
7605       (pop-to-buffer gnus-group-buffer)
7606       (if (not quit-config)
7607           (progn
7608             (gnus-group-jump-to-group group)
7609             (gnus-group-next-unread-group 1))
7610         (if (not (buffer-name (car quit-config)))
7611             (gnus-configure-windows 'group 'force)
7612           (set-buffer (car quit-config))
7613           (and (eq major-mode 'gnus-summary-mode)
7614                (gnus-set-global-variables))
7615           (gnus-configure-windows (cdr quit-config))))
7616       (run-hooks 'gnus-summary-exit-hook))))
7617
7618 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7619 (defun gnus-summary-exit-no-update (&optional no-questions)
7620   "Quit reading current newsgroup without updating read article info."
7621   (interactive)
7622   (gnus-set-global-variables)
7623   (let* ((group gnus-newsgroup-name)
7624          (quit-config (nth 1 (assoc 'quit-config 
7625                                     (gnus-find-method-for-group group)))))
7626     (if (or no-questions
7627             gnus-expert-user
7628             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7629         (progn
7630           (gnus-close-group group)
7631           (gnus-summary-clear-local-variables)
7632           (set-buffer gnus-group-buffer)
7633           (gnus-summary-clear-local-variables)
7634           ;; Return to group selection mode.
7635           (gnus-configure-windows 'group 'force)
7636           (if (get-buffer gnus-summary-buffer)
7637               (kill-buffer gnus-summary-buffer))
7638           (if (get-buffer gnus-article-buffer)
7639               (bury-buffer gnus-article-buffer))
7640           (if (equal (gnus-group-group-name) group)
7641               (gnus-group-next-unread-group 1))
7642           (if quit-config
7643               (progn
7644                 (if (not (buffer-name (car quit-config)))
7645                     (gnus-configure-windows 'group 'force)
7646                   (set-buffer (car quit-config))
7647                   (and (eq major-mode 'gnus-summary-mode)
7648                        (gnus-set-global-variables))
7649                   (gnus-configure-windows (cdr quit-config)))))))))
7650
7651 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7652 (defun gnus-summary-fetch-faq (group)
7653   "Fetch the FAQ for the current group."
7654   (interactive (list gnus-newsgroup-name))
7655   (let ((gnus-faq-buffer 
7656          (find-file (concat gnus-group-faq-directory 
7657                             (gnus-group-real-name group)))))
7658   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7659
7660 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7661 (defun gnus-summary-describe-group (&optional force)
7662   "Describe the current newsgroup."
7663   (interactive "P")
7664   (gnus-group-describe-group force gnus-newsgroup-name))
7665
7666 (defun gnus-summary-describe-briefly ()
7667   "Describe summary mode commands briefly."
7668   (interactive)
7669   (gnus-message 6
7670     (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")))
7671
7672 ;; Walking around group mode buffer from summary mode.
7673
7674 (defun gnus-summary-next-group (&optional no-article target-group backward)
7675   "Exit current newsgroup and then select next unread newsgroup.
7676 If prefix argument NO-ARTICLE is non-nil, no article is selected
7677 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7678 previous group instead."
7679   (interactive "P")
7680   (gnus-set-global-variables)
7681   (let ((current-group gnus-newsgroup-name)
7682         (current-buffer (current-buffer))
7683         entered)
7684     ;; First we semi-exit this group to update Xrefs and all variables.
7685     ;; We can't do a real exit, because the window conf must remain
7686     ;; the same in case the user is prompted for info, and we don't
7687     ;; want the window conf to change before that...
7688     (gnus-summary-exit t)
7689     (while (not entered)
7690       ;; Then we find what group we are supposed to enter.
7691       (set-buffer gnus-group-buffer)
7692       (gnus-group-jump-to-group current-group)
7693       (setq target-group 
7694             (or target-group        
7695                 (if (eq gnus-keep-same-level 'best) 
7696                     (gnus-summary-best-group gnus-newsgroup-name)
7697                   (gnus-summary-search-group backward gnus-keep-same-level))))
7698       (if (not target-group)
7699           ;; There are no further groups, so we return to the group
7700           ;; buffer.
7701           (progn
7702             (gnus-message 5 "Returning to the group buffer")
7703             (setq entered t)
7704             (set-buffer current-buffer)
7705             (gnus-summary-exit))
7706         ;; We try to enter the target group.
7707         (gnus-group-jump-to-group target-group)
7708         (let ((unreads (gnus-group-group-unread)))
7709           (if (and (or (eq t unreads)
7710                        (and unreads (not (zerop unreads))))
7711                    (gnus-summary-read-group
7712                     target-group nil no-article current-buffer))
7713               (setq entered t)
7714             (setq current-group target-group
7715                   target-group nil)))))))
7716
7717 (defun gnus-summary-next-group-old (&optional no-article group backward)
7718   "Exit current newsgroup and then select next unread newsgroup.
7719 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7720 If BACKWARD, go to previous group instead."
7721   (interactive "P")
7722   (gnus-set-global-variables)
7723   (let ((ingroup gnus-newsgroup-name)
7724         (sumbuf (current-buffer))
7725         num)
7726     (set-buffer gnus-group-buffer)
7727     (if (and group
7728              (or (and (numberp (setq num (car (gnus-gethash
7729                                                group gnus-newsrc-hashtb))))
7730                       (< num 1))
7731                  (null num)))
7732         (progn
7733           (gnus-group-jump-to-group group)
7734           (setq group nil))
7735       (gnus-group-jump-to-group ingroup))
7736     (gnus-summary-search-group backward)
7737     (let ((group (or group (gnus-summary-search-group backward))))
7738       (set-buffer sumbuf)
7739       (gnus-summary-exit t)             ;Update all information.
7740       (if (null group)
7741           (gnus-summary-exit-no-update t)
7742         (gnus-group-jump-to-group ingroup)
7743         (setq group (gnus-summary-search-group backward))
7744         (gnus-message 5 "Selecting %s..." group)
7745         (set-buffer gnus-group-buffer)
7746         ;; We are now in group mode buffer.
7747         ;; Make sure group mode buffer point is on GROUP.
7748         (gnus-group-jump-to-group group)
7749         (if (not (eq gnus-auto-select-next 'quietly))
7750             (progn
7751               (gnus-summary-read-group group nil no-article sumbuf)
7752               (and (string= gnus-newsgroup-name ingroup)
7753                    (bufferp sumbuf) (buffer-name sumbuf)
7754                    (progn
7755                      (set-buffer (setq gnus-summary-buffer sumbuf))
7756                      (gnus-summary-exit-no-update t))))
7757           (let ((prevgroup group))
7758             (gnus-group-jump-to-group ingroup)
7759             (setq group (gnus-summary-search-group backward))
7760             (gnus-summary-read-group group nil no-article sumbuf)
7761             (while (and (string= gnus-newsgroup-name ingroup)
7762                         (bufferp sumbuf) 
7763                         (buffer-name sumbuf)
7764                         (not (string= prevgroup (gnus-group-group-name))))
7765               (set-buffer gnus-group-buffer)
7766               (gnus-summary-read-group 
7767                (setq prevgroup (gnus-group-group-name)) 
7768                nil no-article sumbuf))
7769             (and (string= prevgroup (gnus-group-group-name))
7770                  ;; We have reached the final group in the group
7771                  ;; buffer.
7772                  (progn
7773                    (if (buffer-name sumbuf)
7774                        (progn
7775                          (set-buffer sumbuf)
7776                          (gnus-summary-exit)))))))))))
7777
7778 (defun gnus-summary-prev-group (&optional no-article)
7779   "Exit current newsgroup and then select previous unread newsgroup.
7780 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7781   (interactive "P")
7782   (gnus-summary-next-group no-article nil t))
7783
7784 ;; Walking around summary lines.
7785
7786 (defun gnus-summary-first-subject (&optional unread)
7787   "Go to the first unread subject.
7788 If UNREAD is non-nil, go to the first unread article.
7789 Returns nil if there are no unread articles."
7790   (interactive "P")
7791   (prog1
7792       (cond ((not unread)
7793              (goto-char (point-min)))
7794             ((gnus-goto-char 
7795               (text-property-any 
7796                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7797              t)
7798             (t 
7799              ;; There are no unread articles.
7800              (gnus-message 3 "No more unread articles")
7801              nil))
7802     (gnus-summary-position-cursor)))
7803
7804 (defun gnus-summary-next-subject (n &optional unread dont-display)
7805   "Go to next N'th summary line.
7806 If N is negative, go to the previous N'th subject line.
7807 If UNREAD is non-nil, only unread articles are selected.
7808 The difference between N and the actual number of steps taken is
7809 returned."
7810   (interactive "p")
7811   (let ((backward (< n 0))
7812         (n (abs n)))
7813     (while (and (> n 0)
7814                 (gnus-summary-search-forward unread nil backward))
7815       (setq n (1- n)))
7816     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7817                                (if unread " unread" "")))
7818     (or dont-display
7819         (progn
7820           (gnus-summary-recenter)
7821           (gnus-summary-position-cursor)))
7822   n))
7823
7824 (defun gnus-summary-next-unread-subject (n)
7825   "Go to next N'th unread summary line."
7826   (interactive "p")
7827   (gnus-summary-next-subject n t))
7828
7829 (defun gnus-summary-prev-subject (n &optional unread)
7830   "Go to previous N'th summary line.
7831 If optional argument UNREAD is non-nil, only unread article is selected."
7832   (interactive "p")
7833   (gnus-summary-next-subject (- n) unread))
7834
7835 (defun gnus-summary-prev-unread-subject (n)
7836   "Go to previous N'th unread summary line."
7837   (interactive "p")
7838   (gnus-summary-next-subject (- n) t))
7839
7840 (defun gnus-summary-goto-subject (article)
7841   "Go the subject line of ARTICLE."
7842   (interactive
7843    (list
7844     (string-to-int
7845      (completing-read "Article number: "
7846                       (mapcar
7847                        (lambda (headers)
7848                          (list
7849                           (int-to-string (header-number headers))))
7850                        gnus-newsgroup-headers)
7851                       nil 'require-match))))
7852   (or article (error "No article number"))
7853   (let ((b (point)))
7854     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7855                                                 'gnus-number article)))
7856         ()
7857       (gnus-summary-show-thread)
7858       ;; Skip dummy articles. 
7859       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7860           (forward-line 1))
7861       (prog1
7862           (if (not (eobp))
7863               article
7864             (goto-char b)
7865             nil)
7866         (gnus-summary-position-cursor)))))
7867
7868 ;; Walking around summary lines with displaying articles.
7869
7870 (defun gnus-summary-expand-window (&optional arg)
7871   "Make the summary buffer take up the entire Emacs frame.
7872 Given a prefix, will force an `article' buffer configuration."
7873   (interactive "P")
7874   (gnus-set-global-variables)
7875   (if arg
7876       (gnus-configure-windows 'article 'force)
7877     (gnus-configure-windows 'summary 'force)))
7878
7879 (defun gnus-summary-display-article (article &optional all-header)
7880   "Display ARTICLE in article buffer."
7881   (gnus-set-global-variables)
7882   (if (null article)
7883       nil
7884     (prog1
7885         (gnus-article-prepare article all-header)
7886       (gnus-summary-show-thread)
7887       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7888           (progn
7889             (forward-line 1)
7890             (gnus-summary-position-cursor)))
7891       (run-hooks 'gnus-select-article-hook)
7892       (gnus-summary-recenter)
7893       (gnus-summary-goto-subject article)
7894       ;; Successfully display article.
7895       (gnus-summary-update-line)
7896       (gnus-article-set-window-start 
7897        (cdr (assq article gnus-newsgroup-bookmarks)))
7898       t)))
7899
7900 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7901   "Select the current article.
7902 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7903 non-nil, the article will be re-fetched even if it already present in
7904 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7905 be displayed."
7906   (and (not pseudo) (gnus-summary-pseudo-article)
7907        (error "This is a pseudo-article."))
7908   (let ((article (or article (gnus-summary-article-number)))
7909         (all-headers (not (not all-headers))) ;Must be T or NIL.
7910         did) 
7911     (prog1
7912         (save-excursion
7913           (set-buffer gnus-summary-buffer)
7914           (if (or (null gnus-current-article)
7915                   (null gnus-article-current)
7916                   (null (get-buffer gnus-article-buffer))
7917                   (not (eq article (cdr gnus-article-current)))
7918                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7919                   force)
7920               ;; The requested article is different from the current article.
7921               (progn
7922                 (gnus-summary-display-article article all-headers)
7923                 (setq did article))
7924             (if (or all-headers gnus-show-all-headers) 
7925                 (gnus-article-show-all-headers))
7926             nil))
7927       (if did 
7928           (gnus-article-set-window-start 
7929            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7930
7931 (defun gnus-summary-set-current-mark (&optional current-mark)
7932   "Obsolete function."
7933   nil)
7934
7935 (defun gnus-summary-next-article (&optional unread subject backward)
7936   "Select the next article.
7937 If UNREAD, only unread articles are selected.
7938 If SUBJECT, only articles with SUBJECT are selected.
7939 If BACKWARD, the previous article is selected instead of the next."
7940   (interactive "P")
7941   (gnus-set-global-variables)
7942   (let (header)
7943     (cond
7944      ;; Is there such an article?
7945      ((or (gnus-summary-display-article 
7946            (gnus-summary-search-forward unread subject backward))
7947           (eq (gnus-summary-article-mark) gnus-canceled-mark))
7948       (gnus-summary-position-cursor))
7949      ;; If not, we try the first unread, if that is wanted.
7950      ((and subject
7951            gnus-auto-select-same
7952            (gnus-summary-first-unread-article))
7953       (gnus-message 6 "Wrapped"))
7954      ;; Try to get next/previous article not displayed in this group.
7955      ((and gnus-auto-extend-newsgroup
7956            (not unread) (not subject)
7957            (setq header (gnus-more-header-forward backward)))
7958       (gnus-extend-newsgroup header backward)
7959       (let ((buffer-read-only nil))
7960         (goto-char (if backward (point-min) (point-max)))
7961         (gnus-summary-prepare-threads (list header)))
7962       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7963                                    gnus-newsgroup-end)))
7964      ;; Go to next/previous group.
7965      (t
7966       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7967           (gnus-summary-jump-to-group gnus-newsgroup-name))
7968       (let ((cmd (aref (this-command-keys) 0))
7969             (group 
7970              (if (eq gnus-keep-same-level 'best) 
7971                  (gnus-summary-best-group gnus-newsgroup-name)
7972                (gnus-summary-search-group backward gnus-keep-same-level))))
7973         ;; For some reason, the group window gets selected. We change
7974         ;; it back.  
7975         (select-window (get-buffer-window (current-buffer)))
7976         ;; Keep just the event type of CMD.
7977         (and (listp cmd) (setq cmd (car cmd)))
7978         ;; Select next unread newsgroup automagically.
7979         (cond 
7980          ((not gnus-auto-select-next)
7981           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7982          ((eq gnus-auto-select-next 'quietly)
7983           ;; Select quietly.
7984           (if (assoc 'quit-config (gnus-find-method-for-group 
7985                                    gnus-newsgroup-name))
7986               (gnus-summary-exit)
7987             (gnus-message 7 "No more%s articles (%s)..."
7988                           (if unread " unread" "") 
7989                           (if group (concat "selecting " group)
7990                             "exiting"))
7991             (gnus-summary-next-group nil group backward)))
7992          (t
7993           (let ((keystrokes '(?\C-n ?\C-p))
7994                 key)
7995             (while (or (null key) (memq key keystrokes))
7996               (gnus-message 
7997                7 "No more%s articles%s" (if unread " unread" "")
7998                (if (and group (not (assoc 'quit-config
7999                                           (gnus-find-method-for-group 
8000                                            gnus-newsgroup-name))))
8001                    (format " (Type %s for %s [%s])"
8002                            (single-key-description cmd) group
8003                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8004                  (format " (Type %s to exit %s)"
8005                          (single-key-description cmd)
8006                          gnus-newsgroup-name)))
8007               ;; Confirm auto selection.
8008               (let* ((event (read-event)))
8009                 (setq key (if (listp event) (car event) event))
8010                 (if (memq key keystrokes)
8011                     (let ((obuf (current-buffer)))
8012                       (switch-to-buffer gnus-group-buffer)
8013                       (and group
8014                            (gnus-group-jump-to-group group))
8015                       (condition-case ()
8016                           (execute-kbd-macro (char-to-string key))
8017                         (error (ding) nil))
8018                       (setq group (gnus-group-group-name))
8019                       (switch-to-buffer obuf)))))
8020             (if (equal key cmd)
8021                 (if (or (not group) (assoc 'quit-config
8022                                            (gnus-find-method-for-group
8023                                             gnus-newsgroup-name)))
8024                     (gnus-summary-exit)
8025                   (gnus-summary-next-group nil group backward))
8026               (setq unread-command-events (list key)))))))))))
8027
8028 (defun gnus-summary-next-unread-article ()
8029   "Select unread article after current one."
8030   (interactive)
8031   (gnus-summary-next-article t (and gnus-auto-select-same
8032                                     (gnus-summary-subject-string))))
8033
8034 (defun gnus-summary-prev-article (&optional unread subject)
8035   "Select the article after the current one.
8036 If UNREAD is non-nil, only unread articles are selected."
8037   (interactive "P")
8038   (gnus-summary-next-article unread subject t))
8039
8040 (defun gnus-summary-prev-unread-article ()
8041   "Select unred article before current one."
8042   (interactive)
8043   (gnus-summary-prev-article t (and gnus-auto-select-same
8044                                     (gnus-summary-subject-string))))
8045
8046 (defun gnus-summary-next-page (&optional lines circular)
8047   "Show next page of selected article.
8048 If end of article, select next article.
8049 Argument LINES specifies lines to be scrolled up.
8050 If CIRCULAR is non-nil, go to the start of the article instead of 
8051 instead of selecting the next article when reaching the end of the
8052 current article." 
8053   (interactive "P")
8054   (setq gnus-summary-buffer (current-buffer))
8055   (gnus-set-global-variables)
8056   (let ((article (gnus-summary-article-number))
8057         (endp nil))
8058     (gnus-configure-windows 'article)
8059     (if (or (null gnus-current-article)
8060             (null gnus-article-current)
8061             (/= article (cdr gnus-article-current))
8062             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8063         ;; Selected subject is different from current article's.
8064         (gnus-summary-display-article article)
8065       (gnus-eval-in-buffer-window
8066        gnus-article-buffer
8067        (setq endp (gnus-article-next-page lines)))
8068       (if endp
8069           (cond (circular
8070                  (gnus-summary-beginning-of-article))
8071                 (lines
8072                  (gnus-message 3 "End of message"))
8073                 ((null lines)
8074                  (gnus-summary-next-unread-article)))))
8075     (gnus-summary-recenter)
8076     (gnus-summary-position-cursor)))
8077
8078 (defun gnus-summary-prev-page (&optional lines)
8079   "Show previous page of selected article.
8080 Argument LINES specifies lines to be scrolled down."
8081   (interactive "P")
8082   (gnus-set-global-variables)
8083   (let ((article (gnus-summary-article-number)))
8084     (gnus-configure-windows 'article)
8085     (if (or (null gnus-current-article)
8086             (null gnus-article-current)
8087             (/= article (cdr gnus-article-current))
8088             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8089         ;; Selected subject is different from current article's.
8090         (gnus-summary-display-article article)
8091       (gnus-summary-recenter)
8092       (gnus-eval-in-buffer-window gnus-article-buffer
8093         (gnus-article-prev-page lines))))
8094   (gnus-summary-position-cursor))
8095
8096 (defun gnus-summary-scroll-up (lines)
8097   "Scroll up (or down) one line current article.
8098 Argument LINES specifies lines to be scrolled up (or down if negative)."
8099   (interactive "p")
8100   (gnus-set-global-variables)
8101   (gnus-configure-windows 'article)
8102   (or (gnus-summary-select-article nil nil 'pseudo)
8103       (gnus-eval-in-buffer-window 
8104        gnus-article-buffer
8105        (cond ((> lines 0)
8106               (if (gnus-article-next-page lines)
8107                   (gnus-message 3 "End of message")))
8108              ((< lines 0)
8109               (gnus-article-prev-page (- lines))))))
8110   (gnus-summary-recenter)
8111   (gnus-summary-position-cursor))
8112
8113 (defun gnus-summary-next-same-subject ()
8114   "Select next article which has the same subject as current one."
8115   (interactive)
8116   (gnus-set-global-variables)
8117   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8118
8119 (defun gnus-summary-prev-same-subject ()
8120   "Select previous article which has the same subject as current one."
8121   (interactive)
8122   (gnus-set-global-variables)
8123   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8124
8125 (defun gnus-summary-next-unread-same-subject ()
8126   "Select next unread article which has the same subject as current one."
8127   (interactive)
8128   (gnus-set-global-variables)
8129   (gnus-summary-next-article t (gnus-summary-subject-string)))
8130
8131 (defun gnus-summary-prev-unread-same-subject ()
8132   "Select previous unread article which has the same subject as current one."
8133   (interactive)
8134   (gnus-set-global-variables)
8135   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8136
8137 (defun gnus-summary-first-unread-article ()
8138   "Select the first unread article. 
8139 Return nil if there are no unread articles."
8140   (interactive)
8141   (gnus-set-global-variables)
8142   (prog1
8143       (if (gnus-summary-first-subject t)
8144           (gnus-summary-display-article (gnus-summary-article-number)))
8145     (gnus-summary-position-cursor)))
8146
8147 (defun gnus-summary-best-unread-article ()
8148   "Select the unread article with the highest score."
8149   (interactive)
8150   (gnus-set-global-variables)
8151   (let ((scored gnus-newsgroup-scored)
8152         (best -1000000)
8153         article art)
8154     (while scored
8155       (or (> best (cdr (car scored)))
8156           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8157                (not (memq art gnus-newsgroup-marked))
8158                (not (memq art gnus-newsgroup-dormant))
8159                (if (= best (cdr (car scored)))
8160                    (setq article (min art article))
8161                  (setq article art)
8162                  (setq best (cdr (car scored))))))
8163       (setq scored (cdr scored)))
8164     (cond
8165      ((or (not article) (null gnus-newsgroup-unreads))
8166       ;; We didn't find any scored articles, so we just jump to the
8167       ;; first article. 
8168       (gnus-summary-first-unread-article))
8169      ((> best gnus-summary-default-score)
8170       ;; We found one, and it's bigger than the default score, so we
8171       ;; select it.
8172       (gnus-summary-goto-article article))
8173      (t
8174       ;; We found an article, but it has a score lower than the
8175       ;; defaults, so we try to find an article with the default
8176       ;; score. 
8177       (goto-char (point-min))
8178       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8179                       (not (eq (cdr (memq (gnus-summary-article-number)
8180                                           gnus-newsgroup-scored))
8181                                gnus-summary-default-score)))
8182                   (zerop (forward-line 1))
8183                   (not (eobp))))
8184       ;; We jump to the article we have finally found.
8185       (gnus-summary-goto-article (gnus-summary-article-number))))
8186     (gnus-summary-position-cursor)))
8187
8188 (defun gnus-summary-goto-article (article &optional all-headers)
8189   "Fetch ARTICLE and display it if it exists.
8190 If ALL-HEADERS is non-nil, no header lines are hidden."
8191   (interactive
8192    (list
8193     (string-to-int
8194      (completing-read 
8195       "Article number: "
8196       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8197               gnus-newsgroup-headers) 
8198       nil 'require-match))))
8199   (prog1
8200       (and (gnus-summary-goto-subject article)
8201            (gnus-summary-display-article article all-headers))
8202     (gnus-summary-position-cursor)))
8203
8204 (defun gnus-summary-goto-last-article ()
8205   "Go to the previously read article."
8206   (interactive)
8207   (prog1
8208       (and gnus-last-article
8209            (gnus-summary-goto-article gnus-last-article))
8210     (gnus-summary-position-cursor)))
8211
8212 (defun gnus-summary-pop-article (number)
8213   "Pop one article off the history and go to the previous.
8214 NUMBER articles will be popped off."
8215   (interactive "p")
8216   (let (to)
8217     (setq gnus-newsgroup-history
8218           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8219     (if to
8220         (gnus-summary-goto-article (car to))
8221       (error "Article history empty")))
8222   (gnus-summary-position-cursor))
8223
8224 ;; Summary article oriented commands
8225
8226 (defun gnus-summary-refer-parent-article (n)
8227   "Refer parent article N times.
8228 The difference between N and the number of articles fetched is returned."
8229   (interactive "p")
8230   (gnus-set-global-variables)
8231   (while 
8232       (and 
8233        (> n 0)
8234        (let ((ref (header-references (gnus-get-header-by-num
8235                                       (gnus-summary-article-number)))))
8236          (if (and ref (not (equal ref ""))
8237                   (string-match "<[^<>]*>[ \t]*$" ref))
8238              (gnus-summary-refer-article 
8239               (substring ref (match-beginning 0) (match-end 0)))
8240            (gnus-message 1 "No references in article %d"
8241                          (gnus-summary-article-number))
8242            nil)))
8243     (setq n (1- n)))
8244   (gnus-summary-position-cursor)
8245   n)
8246     
8247 (defun gnus-summary-refer-article (message-id)
8248   "Refer article specified by MESSAGE-ID.
8249 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8250   (interactive "sMessage-ID: ")
8251   (if (or (not (stringp message-id))
8252           (zerop (length message-id)))
8253       ()
8254     ;; Construct the correct Message-ID if necessary.
8255     ;; Suggested by tale@pawl.rpi.edu.
8256     (or (string-match "^<" message-id)
8257         (setq message-id (concat "<" message-id)))
8258     (or (string-match ">$" message-id)
8259         (setq message-id (concat message-id ">")))
8260     (let ((header (car (gnus-gethash (downcase message-id)
8261                                      gnus-newsgroup-dependencies))))
8262       (if header
8263           (or (gnus-summary-goto-article (header-number header))
8264               ;; The header has been read, but the article had been
8265               ;; expunged, so we insert it again.
8266               (progn
8267                 (gnus-summary-insert-line
8268                  nil header 0 nil gnus-read-mark nil nil
8269                  (header-subject header))
8270                 (forward-line -1)
8271                 (header-number header)))
8272         (let ((gnus-override-method gnus-refer-article-method)
8273               (gnus-ancient-mark gnus-read-mark)
8274               (tmp-point (window-start
8275                           (get-buffer-window gnus-article-buffer)))
8276               number tmp-buf)
8277           (and gnus-refer-article-method
8278                (or (gnus-server-opened gnus-refer-article-method)
8279                    (gnus-open-server gnus-refer-article-method)))
8280           ;; Save the old article buffer.
8281           (save-excursion
8282             (set-buffer gnus-article-buffer)
8283             (gnus-kill-buffer " *temp Article*")
8284             (setq tmp-buf (rename-buffer " *temp Article*")))
8285           (prog1
8286               (if (gnus-article-prepare 
8287                    message-id nil (gnus-read-header message-id))
8288                   (progn
8289                     (setq number (header-number gnus-current-headers))
8290                     (gnus-rebuild-thread message-id)
8291                     (gnus-summary-goto-subject number)
8292                     (gnus-summary-recenter)
8293                     (gnus-article-set-window-start 
8294                      (cdr (assq number gnus-newsgroup-bookmarks)))
8295                     message-id)
8296                 ;; We restore the old article buffer.
8297                 (save-excursion
8298                   (kill-buffer gnus-article-buffer)
8299                   (set-buffer tmp-buf)
8300                   (rename-buffer gnus-article-buffer)
8301                   (let ((buffer-read-only nil))
8302                     (and tmp-point
8303                          (set-window-start (get-buffer-window (current-buffer))
8304                                            tmp-point)))))))))))
8305
8306 (defun gnus-summary-enter-digest-group ()
8307   "Enter a digest group based on the current article."
8308   (interactive)
8309   (gnus-set-global-variables)
8310   (gnus-summary-select-article)
8311   ;; We do not want a narrowed article.
8312   (gnus-summary-stop-page-breaking)
8313   (let ((name (format "%s-%d" 
8314                       (gnus-group-prefixed-name 
8315                        gnus-newsgroup-name (list 'nndoc "")) 
8316                       gnus-current-article))
8317         (ogroup gnus-newsgroup-name)
8318         (buf (current-buffer)))
8319     (if (gnus-group-read-ephemeral-group 
8320          name (list 'nndoc name
8321                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8322                     '(nndoc-article-type digest))
8323          t)
8324         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8325                 (list (list (cons 'to-group ogroup))))
8326       (switch-to-buffer buf)
8327       (gnus-set-global-variables)
8328       (gnus-configure-windows 'summary)
8329       (gnus-message 3 "Article not a digest?"))))
8330
8331 (defun gnus-summary-isearch-article ()
8332   "Do incremental search forward on current article."
8333   (interactive)
8334   (gnus-set-global-variables)
8335   (gnus-summary-select-article)
8336   (gnus-eval-in-buffer-window 
8337    gnus-article-buffer (isearch-forward)))
8338
8339 (defun gnus-summary-search-article-forward (regexp &optional backward)
8340   "Search for an article containing REGEXP forward.
8341 If BACKWARD, search backward instead."
8342   (interactive
8343    (list (read-string
8344           (format "Search article %s (regexp%s): "
8345                   (if current-prefix-arg "backward" "forward")
8346                   (if gnus-last-search-regexp
8347                       (concat ", default " gnus-last-search-regexp)
8348                     "")))
8349          current-prefix-arg))
8350   (gnus-set-global-variables)
8351   (if (string-equal regexp "")
8352       (setq regexp (or gnus-last-search-regexp ""))
8353     (setq gnus-last-search-regexp regexp))
8354   (if (gnus-summary-search-article regexp backward)
8355       (gnus-article-set-window-start 
8356        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8357     (error "Search failed: \"%s\"" regexp)))
8358
8359 (defun gnus-summary-search-article-backward (regexp)
8360   "Search for an article containing REGEXP backward."
8361   (interactive
8362    (list (read-string
8363           (format "Search article backward (regexp%s): "
8364                   (if gnus-last-search-regexp
8365                       (concat ", default " gnus-last-search-regexp)
8366                     "")))))
8367   (gnus-summary-search-article-forward regexp 'backward))
8368
8369 (defun gnus-summary-search-article (regexp &optional backward)
8370   "Search for an article containing REGEXP.
8371 Optional argument BACKWARD means do search for backward.
8372 gnus-select-article-hook is not called during the search."
8373   (let ((gnus-select-article-hook nil)  ;Disable hook.
8374         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8375         (re-search
8376          (if backward
8377              (function re-search-backward) (function re-search-forward)))
8378         (found nil)
8379         (last nil))
8380     ;; Hidden thread subtrees must be searched for ,too.
8381     (gnus-summary-show-all-threads)
8382     (if (eobp) (forward-line -1))
8383     ;; First of all, search current article.
8384     ;; We don't want to read article again from NNTP server nor reset
8385     ;; current point.
8386     (gnus-summary-select-article)
8387     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8388     (setq last gnus-current-article)
8389     (gnus-eval-in-buffer-window gnus-article-buffer
8390       (save-restriction
8391         (widen)
8392         ;; Begin search from current point.
8393         (setq found (funcall re-search regexp nil t))))
8394     ;; Then search next articles.
8395     (while (and (not found)
8396                 (gnus-summary-display-article 
8397                  (gnus-summary-search-subject backward nil nil)))
8398       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8399       (gnus-eval-in-buffer-window gnus-article-buffer
8400         (save-restriction
8401           (widen)
8402           (goto-char (if backward (point-max) (point-min)))
8403           (setq found (funcall re-search regexp nil t)))))
8404     (message "")
8405     ;; Adjust article pointer.
8406     (or (eq last gnus-current-article)
8407         (setq gnus-last-article last))
8408     ;; Return T if found such article.
8409     found))
8410
8411 (defun gnus-summary-execute-command (header regexp command &optional backward)
8412   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8413 If HEADER is an empty string (or nil), the match is done on the entire
8414 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8415   (interactive
8416    (list (let ((completion-ignore-case t))
8417            (completing-read 
8418             "Header name: "
8419             (mapcar (lambda (string) (list string))
8420                     '("Number" "Subject" "From" "Lines" "Date"
8421                       "Message-ID" "Xref" "References"))
8422             nil 'require-match))
8423          (read-string "Regexp: ")
8424          (read-key-sequence "Command: ")
8425          current-prefix-arg))
8426   (gnus-set-global-variables)
8427   ;; Hidden thread subtrees must be searched as well.
8428   (gnus-summary-show-all-threads)
8429   ;; We don't want to change current point nor window configuration.
8430   (save-excursion
8431     (save-window-excursion
8432       (gnus-message 6 "Executing %s..." (key-description command))
8433       ;; We'd like to execute COMMAND interactively so as to give arguments.
8434       (gnus-execute header regexp
8435                     (` (lambda ()
8436                          (call-interactively '(, (key-binding command)))))
8437                     backward)
8438       (gnus-message 6 "Executing %s...done" (key-description command)))))
8439
8440 (defun gnus-summary-beginning-of-article ()
8441   "Scroll the article back to the beginning."
8442   (interactive)
8443   (gnus-set-global-variables)
8444   (gnus-summary-select-article)
8445   (gnus-configure-windows 'article)
8446   (gnus-eval-in-buffer-window
8447    gnus-article-buffer
8448    (widen)
8449    (goto-char (point-min))
8450    (and gnus-break-pages (gnus-narrow-to-page))))
8451
8452 (defun gnus-summary-end-of-article ()
8453   "Scroll to the end of the article."
8454   (interactive)
8455   (gnus-set-global-variables)
8456   (gnus-summary-select-article)
8457   (gnus-configure-windows 'article)
8458   (gnus-eval-in-buffer-window 
8459    gnus-article-buffer
8460    (widen)
8461    (goto-char (point-max))
8462    (and gnus-break-pages (gnus-narrow-to-page))))
8463
8464 (defun gnus-summary-show-article (&optional no-refetch)
8465   "Force re-fetching of the current article.
8466 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8467 be performed.  The current article will simply be redisplayed."
8468   (interactive "P")
8469   (gnus-set-global-variables)
8470   (if (not no-refetch)
8471       (gnus-summary-select-article gnus-have-all-headers t)
8472     (or gnus-current-article
8473         (error "There is no current article"))
8474     (gnus-summary-goto-subject gnus-current-article)
8475     (gnus-configure-windows 'article)
8476     (gnus-summary-position-cursor)))
8477
8478 (defun gnus-summary-verbose-headers (&optional arg)
8479   "Toggle permanent full header display.
8480 If ARG is a positive number, turn header display on.
8481 If ARG is a negative number, turn header display off."
8482   (interactive "P")
8483   (gnus-set-global-variables)
8484   (gnus-summary-toggle-header arg)
8485   (setq gnus-show-all-headers
8486         (cond ((or (not (numberp arg))
8487                    (zerop arg))
8488                (not gnus-show-all-headers))
8489               ((natnump arg)
8490                t))))
8491
8492 (defun gnus-summary-toggle-header (&optional arg)
8493   "Show the headers if they are hidden, or hide them if they are shown.
8494 If ARG is a positive number, show the entire header.
8495 If ARG is a negative number, hide the unwanted header lines."
8496   (interactive "P")
8497   (gnus-set-global-variables)
8498   (save-excursion
8499     (set-buffer gnus-article-buffer)
8500     (let ((buffer-read-only nil))
8501       (if (numberp arg) 
8502           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8503                                                 gnus-hidden-properties)
8504             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8505         (if (text-property-any (point-min) (point-max) 'invisible t)
8506             (remove-text-properties 
8507              (point-min) (point-max) gnus-hidden-properties)
8508           ;; We hide the headers. This song and dance act below is
8509           ;; done because `gnus-have-all-headers' is buffer-local to
8510           ;; the summary buffer, and we only want to temporarily
8511           ;; change it in that buffer. Ugh.
8512           (let ((have gnus-have-all-headers))
8513             (save-excursion
8514               (set-buffer gnus-summary-buffer)
8515               (setq gnus-have-all-headers nil)
8516               (save-excursion
8517                 (set-buffer gnus-article-buffer)
8518                 (run-hooks 'gnus-article-display-hook))
8519               (setq gnus-have-all-headers have)))))
8520       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8521
8522 (defun gnus-summary-show-all-headers ()
8523   "Make all header lines visible."
8524   (interactive)
8525   (gnus-set-global-variables)
8526   (gnus-article-show-all-headers))
8527
8528 (defun gnus-summary-toggle-mime (&optional arg)
8529   "Toggle MIME processing.
8530 If ARG is a positive number, turn MIME processing on."
8531   (interactive "P")
8532   (gnus-set-global-variables)
8533   (setq gnus-show-mime
8534         (if (null arg) (not gnus-show-mime)
8535           (> (prefix-numeric-value arg) 0)))
8536   (gnus-summary-select-article t 'force))
8537
8538 (defun gnus-summary-caesar-message (&optional arg)
8539   "Caesar rotate the current article by 13.
8540 The numerical prefix specifies how manu places to rotate each letter
8541 forward."
8542   (interactive "P")
8543   (gnus-set-global-variables)
8544   (gnus-summary-select-article)
8545   (let ((mail-header-separator ""))
8546     (gnus-eval-in-buffer-window 
8547      gnus-article-buffer
8548      (save-restriction
8549        (widen)
8550        (let ((last (point)))
8551          (news-caesar-buffer-body arg)
8552          (goto-char last)
8553          (recenter 0))))))
8554
8555 (defun gnus-summary-stop-page-breaking ()
8556   "Stop page breaking in the current article."
8557   (interactive)
8558   (gnus-set-global-variables)
8559   (gnus-summary-select-article)
8560   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8561
8562 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8563
8564 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8565   "Move the current article to a different newsgroup.
8566 If N is a positive number, move the N next articles.
8567 If N is a negative number, move the N previous articles.
8568 If N is nil and any articles have been marked with the process mark,
8569 move those articles instead.
8570 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8571 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8572 re-spool using this method.
8573 For this function to work, both the current newsgroup and the
8574 newsgroup that you want to move to have to support the `request-move'
8575 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8576   (interactive "P")
8577   (gnus-set-global-variables)
8578   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8579       (error "The current newsgroup does not support article moving"))
8580   (let ((articles (gnus-summary-work-articles n))
8581         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8582         art-group to-method sel-met)
8583     (if (and (not to-newsgroup) (not select-method))
8584         (setq to-newsgroup
8585               (completing-read 
8586                (format "Where do you want to move %s? %s"
8587                        (if (> (length articles) 1)
8588                            (format "these %d articles" (length articles))
8589                          "this article")
8590                        (if gnus-current-move-group
8591                            (format "(%s default) " gnus-current-move-group)
8592                          ""))
8593                gnus-active-hashtb nil nil prefix)))
8594     (if to-newsgroup
8595         (progn
8596           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8597               (setq to-newsgroup (or gnus-current-move-group "")))
8598           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8599               (gnus-activate-newsgroup to-newsgroup)
8600               (error "No such group: %s" to-newsgroup))
8601           (setq gnus-current-move-group to-newsgroup)))
8602     (setq to-method (if select-method (list select-method "")
8603                       (gnus-find-method-for-group to-newsgroup)))
8604     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8605         (error "%s does not support article copying" (car to-method)))
8606     (or (gnus-server-opened to-method)
8607         (gnus-open-server to-method)
8608         (error "Can't open server %s" (car to-method)))
8609     (gnus-message 6 "Moving to %s: %s..." 
8610                   (or select-method to-newsgroup) articles)
8611     (while articles
8612       (if (setq art-group
8613                 (gnus-request-move-article 
8614                  (car articles)                   ; Article to move
8615                  gnus-newsgroup-name              ; From newsgrouo
8616                  (nth 1 (gnus-find-method-for-group 
8617                          gnus-newsgroup-name))    ; Server
8618                  (list 'gnus-request-accept-article 
8619                        (if select-method
8620                            (list 'quote select-method)
8621                          to-newsgroup)
8622                        (not (cdr articles)))     ; Accept form
8623                  (not (cdr articles))))          ; Only save nov last time
8624           (let* ((buffer-read-only nil)
8625                  (entry 
8626                   (or
8627                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8628                    (gnus-gethash 
8629                     (gnus-group-prefixed-name 
8630                      (car art-group) 
8631                      (if select-method (list select-method "")
8632                        (gnus-find-method-for-group to-newsgroup)))
8633                     gnus-newsrc-hashtb)))
8634                  (info (nth 2 entry))
8635                  (article (car articles)))
8636             (gnus-summary-goto-subject article)
8637             (beginning-of-line)
8638             (delete-region (point)
8639                            (progn (forward-line 1) (point)))
8640             (if (not (memq article gnus-newsgroup-unreads))
8641                 (setcar (cdr (cdr info))
8642                         (gnus-add-to-range (nth 2 info) 
8643                                            (list (cdr art-group)))))
8644             ;; Copy any marks over to the new group.
8645             (let ((marks '((tick . gnus-newsgroup-marked)
8646                            (dormant . gnus-newsgroup-dormant)
8647                            (expire . gnus-newsgroup-expirable)
8648                            (bookmark . gnus-newsgroup-bookmarks)
8649                         ;   (score . gnus-newsgroup-scored)
8650                            (reply . gnus-newsgroup-replied)))
8651                   (to-article (cdr art-group)))
8652               (while marks
8653                 (if (memq article (symbol-value (cdr (car marks))))
8654                     (gnus-add-marked-articles 
8655                      (car info) (car (car marks)) (list to-article) info))
8656                 (setq marks (cdr marks))))
8657             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8658             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8659             (setq gnus-newsgroup-dormant
8660                   (delq article gnus-newsgroup-dormant))
8661             (setq gnus-newsgroup-reads
8662                   (cons (cons article gnus-canceled-mark)
8663                         gnus-newsgroup-reads)))
8664         (gnus-message 1 "Couldn't move article %s" (car articles)))
8665       (gnus-summary-remove-process-mark (car articles))
8666       (setq articles (cdr articles)))))
8667
8668 (defun gnus-summary-respool-article (&optional n respool-method)
8669   "Respool the current article.
8670 The article will be squeezed through the mail spooling process again,
8671 which means that it will be put in some mail newsgroup or other
8672 depending on `nnmail-split-methods'.
8673 If N is a positive number, respool the N next articles.
8674 If N is a negative number, respool the N previous articles.
8675 If N is nil and any articles have been marked with the process mark,
8676 respool those articles instead.
8677
8678 Respooling can be done both from mail groups and \"real\" newsgroups.
8679 In the former case, the articles in question will be moved from the
8680 current group into whatever groups they are destined to.  In the
8681 latter case, they will be copied into the relevant groups."
8682   (interactive "P")
8683   (gnus-set-global-variables)
8684   (let ((respool-methods (gnus-methods-using 'respool))
8685         (methname 
8686          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8687     (or respool-method
8688         (setq respool-method
8689               (completing-read
8690                "What method do you want to use when respooling? "
8691                respool-methods nil t methname)))
8692     (or (string= respool-method "")
8693         (if (assoc (symbol-name
8694                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8695                    respool-methods)
8696             (gnus-summary-move-article n nil (intern respool-method))
8697           (gnus-summary-copy-article n nil (intern respool-method))))))
8698
8699 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8700 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8701   "Move the current article to a different newsgroup.
8702 If N is a positive number, move the N next articles.
8703 If N is a negative number, move the N previous articles.
8704 If N is nil and any articles have been marked with the process mark,
8705 move those articles instead.
8706 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8707 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8708 re-spool using this method.
8709 For this function to work, the newsgroup that you want to move to have
8710 to support the `request-move' and `request-accept'
8711 functions. (Ie. mail newsgroups at present.)"
8712   (interactive "P")
8713   (gnus-set-global-variables)
8714   (let ((articles (gnus-summary-work-articles n))
8715         (copy-buf (get-buffer-create "*copy work*"))
8716         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8717         art-group to-method)
8718     (buffer-disable-undo copy-buf)
8719     (if (and (not to-newsgroup) (not select-method))
8720         (setq to-newsgroup
8721               (completing-read 
8722                (format "Where do you want to copy %s? %s"
8723                        (if (> (length articles) 1)
8724                            (format "these %d articles" (length articles))
8725                          "this article")
8726                        (if gnus-current-move-group
8727                            (format "(%s default) " gnus-current-move-group)
8728                          ""))
8729                gnus-active-hashtb nil nil prefix)))
8730     (if to-newsgroup
8731         (progn
8732           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8733               (setq to-newsgroup (or gnus-current-move-group "")))
8734           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8735               (gnus-activate-newsgroup to-newsgroup)
8736               (error "No such group: %s" to-newsgroup))
8737           (setq gnus-current-move-group to-newsgroup)))
8738     (setq to-method (if select-method (list select-method "")
8739                       (gnus-find-method-for-group to-newsgroup)))
8740     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8741         (error "%s does not support article copying" (car to-method)))
8742     (or (gnus-server-opened to-method)
8743         (gnus-open-server to-method)
8744         (error "Can't open server %s" (car to-method)))
8745     (while articles
8746       (gnus-message 6 "Copying to %s: %s..." 
8747                     (or select-method to-newsgroup) articles)
8748       (if (setq art-group
8749                 (save-excursion
8750                   (set-buffer copy-buf)
8751                   (gnus-request-article-this-buffer
8752                    (car articles) gnus-newsgroup-name)
8753                   (gnus-request-accept-article
8754                    (if select-method (quote select-method) to-newsgroup)
8755                    (not (cdr articles)))))
8756           (let* ((entry 
8757                   (or
8758                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8759                    (gnus-gethash 
8760                     (gnus-group-prefixed-name 
8761                      (car art-group) 
8762                      (if select-method (list select-method "")
8763                        (gnus-find-method-for-group to-newsgroup)))
8764                     gnus-newsrc-hashtb)))
8765                  (info (nth 2 entry))
8766                  (article (car articles)))
8767             (if (not (memq article gnus-newsgroup-unreads))
8768                 (setcar (cdr (cdr info))
8769                         (gnus-add-to-range (nth 2 info) 
8770                                            (list (cdr art-group)))))
8771             ;; Copy any marks over to the new group.
8772             (let ((marks '((tick . gnus-newsgroup-marked)
8773                            (dormant . gnus-newsgroup-dormant)
8774                            (expire . gnus-newsgroup-expirable)
8775                            (bookmark . gnus-newsgroup-bookmarks)
8776                         ;   (score . gnus-newsgroup-scored)
8777                            (reply . gnus-newsgroup-replied)))
8778                   (to-article (cdr art-group)))
8779               (while marks
8780                 (if (memq article (symbol-value (cdr (car marks))))
8781                     (gnus-add-marked-articles 
8782                      (car info) (car (car marks)) (list to-article) info))
8783                 (setq marks (cdr marks)))))
8784         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8785       (gnus-summary-remove-process-mark (car articles))
8786       (setq articles (cdr articles)))
8787     (kill-buffer copy-buf)))
8788
8789 (defun gnus-summary-import-article (file)
8790   "Import a random file into a mail newsgroup."
8791   (interactive "fImport file: ")
8792   (let ((group gnus-newsgroup-name)
8793         atts)
8794     (or (gnus-check-backend-function 'request-accept-article group)
8795         (error "%s does not support article importing" group))
8796     (or (file-readable-p file)
8797         (not (file-regular-p file))
8798         (error "Can't read %s" file))
8799     (save-excursion
8800       (set-buffer (get-buffer-create " *import file*"))
8801       (buffer-disable-undo (current-buffer))
8802       (erase-buffer)
8803       (insert-file-contents file)
8804       (goto-char (point-min))
8805       (if (nnheader-article-p)
8806           ()
8807         (setq atts (file-attributes file))
8808         (insert "From: " (read-string "From: ") "\n"
8809                 "Subject: " (read-string "Subject: ") "\n"
8810                 "Date: " (current-time-string (nth 5 atts)) "\n"
8811                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8812       (gnus-request-accept-article group t)
8813       (kill-buffer (current-buffer)))))
8814
8815 (defun gnus-summary-expire-articles ()
8816   "Expire all articles that are marked as expirable in the current group."
8817   (interactive)
8818   (if (not (gnus-check-backend-function 
8819             'request-expire-articles gnus-newsgroup-name))
8820       ()
8821     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8822                                       gnus-newsrc-hashtb)))
8823            (total (memq 'total-expire (nth 5 info)))
8824            (expirable (if total
8825                           (gnus-list-of-read-articles gnus-newsgroup-name)
8826                         (setq gnus-newsgroup-expirable
8827                               (sort gnus-newsgroup-expirable '<))))
8828            es)
8829       (if (not expirable)
8830           ()
8831         (gnus-message 6 "Expiring articles...")
8832         ;; The list of articles that weren't expired is returned.
8833         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8834         (or total (setq gnus-newsgroup-expirable es))
8835         ;; We go through the old list of expirable, and mark all
8836         ;; really expired articles as non-existant.
8837         (or (eq es expirable) ; If nothing was expired, we don't mark.
8838             (let ((gnus-use-cache nil))
8839               (while expirable
8840                 (or (memq (car expirable) es)
8841                     (gnus-summary-mark-article
8842                      (car expirable) gnus-canceled-mark))
8843                 (setq expirable (cdr expirable)))))
8844         (gnus-message 6 "Expiring articles...done")))))
8845
8846 (defun gnus-summary-expire-articles-now ()
8847   "Expunge all expirable articles in the current group.
8848 This means that *all* articles that are marked as expirable will be
8849 deleted forever, right now."
8850   (interactive)
8851   (or gnus-expert-user
8852       (gnus-y-or-n-p
8853        "Are you really, really, really sure you want to expunge? ")
8854       (error "Phew!"))
8855   (let ((nnmail-expiry-wait -1)
8856         (nnmail-expiry-wait-function nil))
8857     (gnus-summary-expire-articles)))
8858
8859 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8860 (defun gnus-summary-delete-article (&optional n)
8861   "Delete the N next (mail) articles.
8862 This command actually deletes articles. This is not a marking
8863 command. The article will disappear forever from you life, never to
8864 return. 
8865 If N is negative, delete backwards.
8866 If N is nil and articles have been marked with the process mark,
8867 delete these instead."
8868   (interactive "P")
8869   (or (gnus-check-backend-function 'request-expire-articles 
8870                                    gnus-newsgroup-name)
8871       (error "The current newsgroup does not support article deletion."))
8872   ;; Compute the list of articles to delete.
8873   (let ((articles (gnus-summary-work-articles n))
8874         not-deleted)
8875     (if (and gnus-novice-user
8876              (not (gnus-y-or-n-p 
8877                    (format "Do you really want to delete %s forever? "
8878                            (if (> (length articles) 1) "these articles"
8879                              "this article")))))
8880         ()
8881       ;; Delete the articles.
8882       (setq not-deleted (gnus-request-expire-articles 
8883                          articles gnus-newsgroup-name 'force))
8884       (while articles
8885         (gnus-summary-remove-process-mark (car articles))       
8886         ;; The backend might not have been able to delete the article
8887         ;; after all.  
8888         (or (memq (car articles) not-deleted)
8889             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8890         (setq articles (cdr articles))))
8891     (gnus-summary-position-cursor)
8892     not-deleted))
8893
8894 (defun gnus-summary-edit-article ()
8895   "Enter into a buffer and edit the current article.
8896 This will have permanent effect only in mail groups."
8897   (interactive)
8898   (or (gnus-check-backend-function 
8899        'request-replace-article gnus-newsgroup-name)
8900       (error "The current newsgroup does not support article editing."))
8901   (gnus-summary-select-article t)
8902   (gnus-configure-windows 'article)
8903   (select-window (get-buffer-window gnus-article-buffer))
8904   (gnus-message 6 "C-c C-c to end edits")
8905   (setq buffer-read-only nil)
8906   (text-mode)
8907   (use-local-map (copy-keymap (current-local-map)))
8908   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8909   (buffer-enable-undo)
8910   (widen)
8911   (goto-char (point-min))
8912   (search-forward "\n\n" nil t))
8913
8914 (defun gnus-summary-edit-article-done ()
8915   "Make edits to the current article permanent."
8916   (interactive)
8917   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8918     (erase-buffer)
8919     (insert buf)
8920     (if (not (gnus-request-replace-article 
8921               (cdr gnus-article-current) (car gnus-article-current) 
8922               (current-buffer)))
8923         (error "Couldn't replace article.")
8924       (gnus-article-mode)
8925       (use-local-map gnus-article-mode-map)
8926       (setq buffer-read-only t)
8927       (buffer-disable-undo (current-buffer))
8928       (gnus-configure-windows 'summary))))
8929
8930 (defun gnus-summary-fancy-query ()
8931   "Query where the fancy respool algorithm would put this article."
8932   (interactive)
8933   (gnus-summary-select-article)
8934   (save-excursion
8935     (set-buffer gnus-article-buffer)
8936     (save-restriction
8937       (goto-char (point-min))
8938       (search-forward "\n\n")
8939       (narrow-to-region (point-min) (point))
8940       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8941
8942 ;; Summary score commands.
8943
8944 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8945
8946 (defun gnus-summary-raise-score (n)
8947   "Raise the score of the current article by N."
8948   (interactive "p")
8949   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8950
8951 (defun gnus-summary-set-score (n)
8952   "Set the score of the current article to N."
8953   (interactive "p")
8954   ;; Skip dummy header line.
8955   (save-excursion
8956     (gnus-summary-show-thread)
8957     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8958         (forward-line 1))
8959     (let ((buffer-read-only nil))
8960       ;; Set score.
8961       (gnus-summary-update-mark
8962        (if (= n (or gnus-summary-default-score 0)) ? 
8963          (if (< n (or gnus-summary-default-score 0)) 
8964              gnus-score-below-mark gnus-score-over-mark)) 'score))
8965     (let* ((article (gnus-summary-article-number))
8966            (score (assq article gnus-newsgroup-scored)))
8967       (if score (setcdr score n)
8968         (setq gnus-newsgroup-scored 
8969               (cons (cons article n) gnus-newsgroup-scored))))
8970     (gnus-summary-update-line)))
8971
8972 (defun gnus-summary-current-score ()
8973   "Return the score of the current article."
8974   (interactive)
8975   (message "%s" (gnus-summary-article-score)))
8976
8977 ;; Summary marking commands.
8978
8979 (defun gnus-summary-raise-same-subject-and-select (score)
8980   "Raise articles which has the same subject with SCORE and select the next."
8981   (interactive "p")
8982   (let ((subject (gnus-summary-subject-string)))
8983     (gnus-summary-raise-score score)
8984     (while (gnus-summary-search-subject nil nil subject)
8985       (gnus-summary-raise-score score))
8986     (gnus-summary-next-article t)))
8987
8988 (defun gnus-summary-raise-same-subject (score)
8989   "Raise articles which has the same subject with SCORE."
8990   (interactive "p")
8991   (let ((subject (gnus-summary-subject-string)))
8992     (gnus-summary-raise-score score)
8993     (while (gnus-summary-search-subject nil nil subject)
8994       (gnus-summary-raise-score score))
8995     (gnus-summary-next-subject 1 t)))
8996
8997 (defun gnus-score-default (level)
8998   (if level (prefix-numeric-value level) 
8999     gnus-score-interactive-default-score))
9000
9001 (defun gnus-summary-raise-thread (&optional score)
9002   "Raise the score of the articles in the current thread with SCORE."
9003   (interactive "P")
9004   (setq score (1- (gnus-score-default score)))
9005   (let (e)
9006     (save-excursion
9007       (let ((level (gnus-summary-thread-level)))
9008         (gnus-summary-raise-score score)
9009         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9010                     (> (gnus-summary-thread-level) level))
9011           (gnus-summary-raise-score score))
9012         (setq e (point))))
9013     (let ((gnus-summary-check-current t))
9014       (or (zerop (gnus-summary-next-subject 1 t))
9015           (goto-char e))))
9016   (gnus-summary-recenter)
9017   (gnus-summary-position-cursor)
9018   (gnus-set-mode-line 'summary))
9019
9020 (defun gnus-summary-lower-same-subject-and-select (score)
9021   "Raise articles which has the same subject with SCORE and select the next."
9022   (interactive "p")
9023   (gnus-summary-raise-same-subject-and-select (- score)))
9024
9025 (defun gnus-summary-lower-same-subject (score)
9026   "Raise articles which has the same subject with SCORE."
9027   (interactive "p")
9028   (gnus-summary-raise-same-subject (- score)))
9029
9030 (defun gnus-summary-lower-thread (&optional score)
9031   "Lower score of articles in the current thread with SCORE."
9032   (interactive "P")
9033   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9034
9035 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9036   "Mark articles which has the same subject as read, and then select the next.
9037 If UNMARK is positive, remove any kind of mark.
9038 If UNMARK is negative, tick articles."
9039   (interactive "P")
9040   (if unmark
9041       (setq unmark (prefix-numeric-value unmark)))
9042   (let ((count
9043          (gnus-summary-mark-same-subject
9044           (gnus-summary-subject-string) unmark)))
9045     ;; Select next unread article. If auto-select-same mode, should
9046     ;; select the first unread article.
9047     (gnus-summary-next-article t (and gnus-auto-select-same
9048                                       (gnus-summary-subject-string)))
9049     (gnus-message 7 "%d articles are marked as %s"
9050                   count (if unmark "unread" "read"))))
9051
9052 (defun gnus-summary-kill-same-subject (&optional unmark)
9053   "Mark articles which has the same subject as read. 
9054 If UNMARK is positive, remove any kind of mark.
9055 If UNMARK is negative, tick articles."
9056   (interactive "P")
9057   (if unmark
9058       (setq unmark (prefix-numeric-value unmark)))
9059   (let ((count
9060          (gnus-summary-mark-same-subject
9061           (gnus-summary-subject-string) unmark)))
9062     ;; If marked as read, go to next unread subject.
9063     (if (null unmark)
9064         ;; Go to next unread subject.
9065         (gnus-summary-next-subject 1 t))
9066     (gnus-message 7 "%d articles are marked as %s"
9067                   count (if unmark "unread" "read"))))
9068
9069 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9070   "Mark articles with same SUBJECT as read, and return marked number.
9071 If optional argument UNMARK is positive, remove any kinds of marks.
9072 If optional argument UNMARK is negative, mark articles as unread instead."
9073   (let ((count 1))
9074     (save-excursion
9075       (cond 
9076        ((null unmark)                   ; Mark as read.
9077         (while (and 
9078                 (progn
9079                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9080                   (gnus-summary-show-thread) t)
9081                 (gnus-summary-search-forward nil subject))
9082           (setq count (1+ count))))
9083        ((> unmark 0)                    ; Tick.
9084         (while (and
9085                 (progn
9086                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9087                   (gnus-summary-show-thread) t)
9088                 (gnus-summary-search-forward nil subject))
9089           (setq count (1+ count))))
9090        (t                               ; Mark as unread.
9091         (while (and
9092                 (progn
9093                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9094                   (gnus-summary-show-thread) t)
9095                 (gnus-summary-search-forward nil subject))
9096           (setq count (1+ count)))))
9097       ;; Return the number of marked articles.
9098       count)))
9099
9100 (defun gnus-summary-mark-as-processable (n &optional unmark)
9101   "Set the process mark on the next N articles.
9102 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9103 the process mark instead.  The difference between N and the actual
9104 number of articles marked is returned."
9105   (interactive "p")
9106   (let ((backward (< n 0))
9107         (n (abs n)))
9108   (while (and 
9109           (> n 0)
9110           (if unmark
9111               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9112             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9113           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9114     (setq n (1- n)))
9115   (if (/= 0 n) (gnus-message 7 "No more articles"))
9116   (gnus-summary-recenter)
9117   (gnus-summary-position-cursor)
9118   n))
9119
9120 (defun gnus-summary-unmark-as-processable (n)
9121   "Remove the process mark from the next N articles.
9122 If N is negative, mark backward instead.  The difference between N and
9123 the actual number of articles marked is returned."
9124   (interactive "p")
9125   (gnus-summary-mark-as-processable n t))
9126
9127 (defun gnus-summary-unmark-all-processable ()
9128   "Remove the process mark from all articles."
9129   (interactive)
9130   (save-excursion
9131     (while gnus-newsgroup-processable
9132       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9133   (gnus-summary-position-cursor))
9134
9135 (defun gnus-summary-mark-as-expirable (n)
9136   "Mark N articles forward as expirable.
9137 If N is negative, mark backward instead. The difference between N and
9138 the actual number of articles marked is returned."
9139   (interactive "p")
9140   (gnus-summary-mark-forward n gnus-expirable-mark))
9141
9142 (defun gnus-summary-mark-article-as-replied (article)
9143   "Mark ARTICLE replied and update the summary line."
9144   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9145   (let ((buffer-read-only nil))
9146     (if (gnus-summary-goto-subject article)
9147         (progn
9148           (gnus-summary-update-mark gnus-replied-mark 'replied)
9149           t))))
9150
9151 (defun gnus-summary-set-bookmark (article)
9152   "Set a bookmark in current article."
9153   (interactive (list (gnus-summary-article-number)))
9154   (if (or (not (get-buffer gnus-article-buffer))
9155           (not gnus-current-article)
9156           (not gnus-article-current)
9157           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9158       (error "No current article selected"))
9159   ;; Remove old bookmark, if one exists.
9160   (let ((old (assq article gnus-newsgroup-bookmarks)))
9161     (if old (setq gnus-newsgroup-bookmarks 
9162                   (delq old gnus-newsgroup-bookmarks))))
9163   ;; Set the new bookmark, which is on the form 
9164   ;; (article-number . line-number-in-body).
9165   (setq gnus-newsgroup-bookmarks 
9166         (cons 
9167          (cons article 
9168                (save-excursion
9169                  (set-buffer gnus-article-buffer)
9170                  (count-lines
9171                   (min (point)
9172                        (save-excursion
9173                          (goto-char (point-min))
9174                          (search-forward "\n\n" nil t)
9175                          (point)))
9176                   (point))))
9177          gnus-newsgroup-bookmarks))
9178   (gnus-message 6 "A bookmark has been added to the current article."))
9179
9180 (defun gnus-summary-remove-bookmark (article)
9181   "Remove the bookmark from the current article."
9182   (interactive (list (gnus-summary-article-number)))
9183   ;; Remove old bookmark, if one exists.
9184   (let ((old (assq article gnus-newsgroup-bookmarks)))
9185     (if old 
9186         (progn
9187           (setq gnus-newsgroup-bookmarks 
9188                 (delq old gnus-newsgroup-bookmarks))
9189           (gnus-message 6 "Removed bookmark."))
9190       (gnus-message 6 "No bookmark in current article."))))
9191
9192 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9193 (defun gnus-summary-mark-as-dormant (n)
9194   "Mark N articles forward as dormant.
9195 If N is negative, mark backward instead.  The difference between N and
9196 the actual number of articles marked is returned."
9197   (interactive "p")
9198   (gnus-summary-mark-forward n gnus-dormant-mark))
9199
9200 (defun gnus-summary-set-process-mark (article)
9201   "Set the process mark on ARTICLE and update the summary line."
9202   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9203   (let ((buffer-read-only nil))
9204     (if (gnus-summary-goto-subject article)
9205         (progn
9206           (gnus-summary-show-thread)
9207           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9208                (forward-line 1))
9209           (gnus-summary-update-mark gnus-process-mark 'replied)
9210           t))))
9211
9212 (defun gnus-summary-remove-process-mark (article)
9213   "Remove the process mark from ARTICLE and update the summary line."
9214   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9215   (let ((buffer-read-only nil))
9216     (if (gnus-summary-goto-subject article)
9217         (progn
9218           (gnus-summary-show-thread)
9219           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9220                (forward-line 1))
9221           (gnus-summary-update-mark ?  'replied)
9222           (if (memq article gnus-newsgroup-replied) 
9223               (gnus-summary-update-mark gnus-replied-mark 'replied))
9224           t))))
9225
9226 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9227   "Mark N articles as read forwards.
9228 If N is negative, mark backwards instead.
9229 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9230 marked as unread. 
9231 The difference between N and the actual number of articles marked is
9232 returned."
9233   (interactive "p")
9234   (gnus-set-global-variables)
9235   (let ((backward (< n 0))
9236         (gnus-summary-goto-unread
9237          (and gnus-summary-goto-unread
9238               (not (memq mark (list gnus-unread-mark
9239                                     gnus-ticked-mark gnus-dormant-mark)))))
9240         (n (abs n))
9241         (mark (or mark gnus-del-mark)))
9242   (while (and (> n 0)
9243               (gnus-summary-mark-article nil mark no-expire)
9244               (zerop (gnus-summary-next-subject 
9245                       (if backward -1 1) gnus-summary-goto-unread t)))
9246     (setq n (1- n)))
9247   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9248   (gnus-summary-recenter)
9249   (gnus-summary-position-cursor)
9250   (gnus-set-mode-line 'summary)
9251   n))
9252
9253 (defun gnus-summary-mark-article-as-read (mark)
9254   "Mark the current article quickly as read with MARK."
9255   (let ((article (gnus-summary-article-number)))
9256     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9257     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9258     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9259     (setq gnus-newsgroup-reads
9260           (cons (cons article mark) gnus-newsgroup-reads))
9261     ;; Possibly remove from cache, if that is used. 
9262     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9263     (and gnus-newsgroup-auto-expire 
9264          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9265              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9266              (= mark gnus-read-mark))
9267          (progn
9268            (setq mark gnus-expirable-mark)
9269            (setq gnus-newsgroup-expirable 
9270                  (cons article gnus-newsgroup-expirable))))
9271     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9272       (forward-line 1))
9273     ;; Fix the mark.
9274     (gnus-summary-update-mark mark 'unread)
9275     t))
9276
9277 (defun gnus-summary-mark-article-as-unread (mark)
9278   "Mark the current article quickly as unread with MARK."
9279   (let ((article (gnus-summary-article-number)))
9280     (or (memq article gnus-newsgroup-unreads)
9281         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9282     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9283     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9284     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9285     (setq gnus-newsgroup-reads
9286           (delq (assq article gnus-newsgroup-reads)
9287                 gnus-newsgroup-reads))
9288     (if (= mark gnus-ticked-mark)
9289         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9290     (if (= mark gnus-dormant-mark)
9291         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9292
9293     ;; See whether the article is to be put in the cache.
9294     (and gnus-use-cache
9295          (vectorp (gnus-get-header-by-num article))
9296          (save-excursion
9297            (gnus-cache-possibly-enter-article 
9298             gnus-newsgroup-name article 
9299             (gnus-get-header-by-num article)
9300             (= mark gnus-ticked-mark)
9301             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9302
9303     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9304       (forward-line 1))
9305     ;; Fix the mark.
9306     (gnus-summary-update-mark mark 'unread)
9307     t))
9308
9309 (defun gnus-summary-mark-article (&optional article mark no-expire)
9310   "Mark ARTICLE with MARK.  MARK can be any character.
9311 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9312 (dormant) and `?E' (expirable).
9313 If MARK is nil, then the default character `?D' is used.
9314 If ARTICLE is nil, then the article on the current line will be
9315 marked." 
9316   (and (stringp mark)
9317        (setq mark (aref mark 0)))
9318   ;; If no mark is given, then we check auto-expiring.
9319   (and (not no-expire)
9320        gnus-newsgroup-auto-expire 
9321        (or (not mark)
9322            (and (numberp mark) 
9323                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9324                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9325                     (= mark gnus-read-mark))))
9326        (setq mark gnus-expirable-mark))
9327   (let* ((mark (or mark gnus-del-mark))
9328          (article (or article (gnus-summary-article-number))))
9329     (or article (error "No article on current line"))
9330     (if (or (= mark gnus-unread-mark) 
9331             (= mark gnus-ticked-mark) 
9332             (= mark gnus-dormant-mark))
9333         (gnus-mark-article-as-unread article mark)
9334       (gnus-mark-article-as-read article mark))
9335
9336     ;; See whether the article is to be put in the cache.
9337     (and gnus-use-cache
9338          (not (= mark gnus-canceled-mark))
9339          (vectorp (gnus-get-header-by-num article))
9340          (save-excursion
9341            (gnus-cache-possibly-enter-article 
9342             gnus-newsgroup-name article 
9343             (gnus-get-header-by-num article)
9344             (= mark gnus-ticked-mark)
9345             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9346
9347     (if (gnus-summary-goto-subject article)
9348         (let ((buffer-read-only nil))
9349           (gnus-summary-show-thread)
9350           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9351                (forward-line 1))
9352           ;; Fix the mark.
9353           (gnus-summary-update-mark mark 'unread)
9354           t))))
9355
9356 (defun gnus-summary-update-mark (mark type)
9357   (beginning-of-line)
9358   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9359         (buffer-read-only nil)
9360         plist)
9361     (if (not forward)
9362         ()
9363       (forward-char forward)
9364       (setq plist (text-properties-at (point)))
9365       (delete-char 1)
9366       (insert mark)
9367       (and plist (add-text-properties (1- (point)) (point) plist))
9368       (and (eq type 'unread)
9369            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9370       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9371   
9372 (defun gnus-mark-article-as-read (article &optional mark)
9373   "Enter ARTICLE in the pertinent lists and remove it from others."
9374   ;; Make the article expirable.
9375   (let ((mark (or mark gnus-del-mark)))
9376     (if (= mark gnus-expirable-mark)
9377         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9378       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9379     ;; Remove from unread and marked lists.
9380     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9381     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9382     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9383     (setq gnus-newsgroup-reads 
9384           (cons (cons article mark) gnus-newsgroup-reads))
9385     ;; Possibly remove from cache, if that is used. 
9386     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9387
9388 (defun gnus-mark-article-as-unread (article &optional mark)
9389   "Enter ARTICLE in the pertinent lists and remove it from others."
9390   (let ((mark (or mark gnus-ticked-mark)))
9391     ;; Add to unread list.
9392     (or (memq article gnus-newsgroup-unreads)
9393         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9394     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9395     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9396     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9397     (setq gnus-newsgroup-reads
9398           (delq (assq article gnus-newsgroup-reads)
9399                 gnus-newsgroup-reads))
9400     (if (= mark gnus-ticked-mark)
9401         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9402     (if (= mark gnus-dormant-mark)
9403         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9404
9405 (defalias 'gnus-summary-mark-as-unread-forward 
9406   'gnus-summary-tick-article-forward)
9407 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9408                'gnus-summary-tick-article-forward)
9409 (defun gnus-summary-tick-article-forward (n)
9410   "Tick N articles forwards.
9411 If N is negative, tick backwards instead.
9412 The difference between N and the number of articles ticked is returned."
9413   (interactive "p")
9414   (gnus-summary-mark-forward n gnus-ticked-mark))
9415
9416 (defalias 'gnus-summary-mark-as-unread-backward 
9417   'gnus-summary-tick-article-backward)
9418 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9419                'gnus-summary-tick-article-backward)
9420 (defun gnus-summary-tick-article-backward (n)
9421   "Tick N articles backwards.
9422 The difference between N and the number of articles ticked is returned."
9423   (interactive "p")
9424   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9425
9426 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9427 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9428 (defun gnus-summary-tick-article (&optional article clear-mark)
9429   "Mark current article as unread.
9430 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9431 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9432   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9433                                        gnus-ticked-mark)))
9434
9435 (defun gnus-summary-mark-as-read-forward (n)
9436   "Mark N articles as read forwards.
9437 If N is negative, mark backwards instead.
9438 The difference between N and the actual number of articles marked is
9439 returned."
9440   (interactive "p")
9441   (gnus-summary-mark-forward n gnus-del-mark t))
9442
9443 (defun gnus-summary-mark-as-read-backward (n)
9444   "Mark the N articles as read backwards.
9445 The difference between N and the actual number of articles marked is
9446 returned."
9447   (interactive "p")
9448   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9449
9450 (defun gnus-summary-mark-as-read (&optional article mark)
9451   "Mark current article as read.
9452 ARTICLE specifies the article to be marked as read.
9453 MARK specifies a string to be inserted at the beginning of the line."
9454   (gnus-summary-mark-article article mark))
9455
9456 (defun gnus-summary-clear-mark-forward (n)
9457   "Clear marks from N articles forward.
9458 If N is negative, clear backward instead.
9459 The difference between N and the number of marks cleared is returned."
9460   (interactive "p")
9461   (gnus-summary-mark-forward n gnus-unread-mark))
9462
9463 (defun gnus-summary-clear-mark-backward (n)
9464   "Clear marks from N articles backward.
9465 The difference between N and the number of marks cleared is returned."
9466   (interactive "p")
9467   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9468
9469 (defun gnus-summary-mark-unread-as-read ()
9470   "Intended to be used by `gnus-summary-mark-article-hook'."
9471   (and (memq gnus-current-article gnus-newsgroup-unreads)
9472        (or (memq gnus-current-article gnus-newsgroup-marked)
9473            (memq gnus-current-article gnus-newsgroup-dormant)
9474            (memq gnus-current-article gnus-newsgroup-expirable)
9475            (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9476
9477 (defun gnus-summary-mark-region-as-read (point mark all)
9478   "Mark all unread articles between point and mark as read.
9479 If given a prefix, mark all articles between point and mark as read,
9480 even ticked and dormant ones."
9481   (interactive "r\nP")
9482   (save-excursion
9483     (goto-char point)
9484     (beginning-of-line)
9485     (while (and 
9486             (< (point) mark)
9487             (progn
9488               (and
9489                (or all
9490                    (and
9491                     (not (memq (gnus-summary-article-number)
9492                                gnus-newsgroup-marked))
9493                     (not (memq (gnus-summary-article-number)
9494                                gnus-newsgroup-dormant))))
9495                (gnus-summary-mark-article
9496                 (gnus-summary-article-number) gnus-del-mark))
9497               t)
9498             (zerop (forward-line 1))))))
9499
9500 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9501 (defalias 'gnus-summary-delete-marked-as-read 
9502   'gnus-summary-remove-lines-marked-as-read)
9503 (make-obsolete 'gnus-summary-delete-marked-as-read 
9504                'gnus-summary-remove-lines-marked-as-read)
9505 (defun gnus-summary-remove-lines-marked-as-read ()
9506   "Remove lines that are marked as read."
9507   (interactive)
9508   (gnus-summary-remove-lines-marked-with 
9509    (concat (mapconcat
9510             (lambda (char) (char-to-string (symbol-value char)))
9511             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9512               gnus-killed-mark gnus-kill-file-mark
9513               gnus-low-score-mark gnus-expirable-mark
9514               gnus-canceled-mark)
9515             ""))))
9516
9517 (defalias 'gnus-summary-delete-marked-with 
9518   'gnus-summary-remove-lines-marked-with)
9519 (make-obsolete 'gnus-summary-delete-marked-with 
9520                'gnus-summary-remove-lines-marked-with)
9521 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9522 (defun gnus-summary-remove-lines-marked-with (marks)
9523   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9524   (interactive "sMarks: ")
9525   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9526   (gnus-set-global-variables)
9527   (let ((buffer-read-only nil)
9528         (marks (concat "^[" marks "]")))
9529     (goto-char (point-min))
9530     (if gnus-newsgroup-adaptive
9531         (gnus-score-remove-lines-adaptive marks)
9532       (while (re-search-forward marks nil t)
9533         (gnus-delete-line)))
9534     ;; If we use dummy roots, we have to do an additional sweep over
9535     ;; the buffer.
9536     (if (not (eq gnus-summary-make-false-root 'dummy))
9537         ()
9538       (goto-char (point-min))
9539       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9540       (while (re-search-forward marks nil t)
9541         (if (gnus-subject-equal
9542              (gnus-summary-subject-string)
9543              (progn
9544                (forward-line 1)
9545                (gnus-summary-subject-string)))
9546             ()
9547           (forward-line -1)
9548           (gnus-delete-line)))))
9549   (or (zerop (buffer-size))
9550       (if (eobp)
9551           (gnus-summary-prev-subject 1)
9552         (gnus-summary-position-cursor))))
9553
9554 (defun gnus-summary-expunge-below (&optional score)
9555   "Remove articles with score less than SCORE."
9556   (interactive "P")
9557   (gnus-set-global-variables)
9558   (setq score (if score
9559                   (prefix-numeric-value score)
9560                 (or gnus-summary-default-score 0)))
9561   (save-excursion
9562     (set-buffer gnus-summary-buffer)
9563     (goto-char (point-min))
9564     (let ((buffer-read-only nil)
9565           beg)
9566       (while (not (eobp))
9567         (if (< (gnus-summary-article-score) score)
9568             (progn
9569               (setq beg (point))
9570               (forward-line 1)
9571               (delete-region beg (point)))
9572           (forward-line 1)))
9573       ;; Adjust point.
9574       (or (zerop (buffer-size))
9575           (if (eobp)
9576               (gnus-summary-prev-subject 1)
9577             (gnus-summary-position-cursor))))))
9578
9579 (defun gnus-summary-mark-below (score mark)
9580   "Mark articles with score less than SCORE with MARK."
9581   (interactive "P\ncMark: ")
9582   (gnus-set-global-variables)
9583   (setq score (if score
9584                   (prefix-numeric-value score)
9585                 (or gnus-summary-default-score 0)))
9586   (save-excursion
9587     (set-buffer gnus-summary-buffer)
9588     (goto-char (point-min))
9589     (while (not (eobp))
9590       (and (< (gnus-summary-article-score) score)
9591            (gnus-summary-mark-article nil mark))
9592       (forward-line 1))))
9593
9594 (defun gnus-summary-kill-below (&optional score)
9595   "Mark articles with score below SCORE as read."
9596   (interactive "P")
9597   (gnus-set-global-variables)
9598   (gnus-summary-mark-below score gnus-killed-mark))
9599
9600 (defun gnus-summary-clear-above (&optional score)
9601   "Clear all marks from articles with score above SCORE."
9602   (interactive "P")
9603   (gnus-set-global-variables)
9604   (gnus-summary-mark-above score gnus-unread-mark))
9605
9606 (defun gnus-summary-tick-above (&optional score)
9607   "Tick all articles with score above SCORE."
9608   (interactive "P")
9609   (gnus-set-global-variables)
9610   (gnus-summary-mark-above score gnus-ticked-mark))
9611
9612 (defun gnus-summary-mark-above (score mark)
9613   "Mark articles with score over SCORE with MARK."
9614   (interactive "P\ncMark: ")
9615   (gnus-set-global-variables)
9616   (setq score (if score
9617                   (prefix-numeric-value score)
9618                 (or gnus-summary-default-score 0)))
9619   (save-excursion
9620     (set-buffer gnus-summary-buffer)
9621     (goto-char (point-min))
9622     (while (not (eobp))
9623       (if (> (gnus-summary-article-score) score)
9624           (progn
9625             (gnus-summary-mark-article nil mark)
9626             (forward-line 1))
9627         (forward-line 1)))))
9628
9629 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9630 (defun gnus-summary-show-all-expunged ()
9631   "Display all the hidden articles that were expunged for low scores."
9632   (interactive)
9633   (gnus-set-global-variables)
9634   (let ((buffer-read-only nil))
9635     (let ((scored gnus-newsgroup-scored)
9636           headers h)
9637       (while scored
9638         (or (gnus-summary-goto-subject (car (car scored)))
9639             (and (setq h (gnus-get-header-by-num (car (car scored))))
9640                  (< (cdr (car scored)) gnus-summary-expunge-below)
9641                  (setq headers (cons h headers))))
9642         (setq scored (cdr scored)))
9643       (or headers (error "No expunged articles hidden."))
9644       (goto-char (point-min))
9645       (save-excursion 
9646         (gnus-summary-update-lines 
9647          (point)
9648          (progn
9649            (gnus-summary-prepare-unthreaded (nreverse headers))
9650            (point)))))
9651     (goto-char (point-min))
9652     (gnus-summary-position-cursor)))
9653
9654 (defun gnus-summary-show-all-dormant ()
9655   "Display all the hidden articles that are marked as dormant."
9656   (interactive)
9657   (gnus-set-global-variables)
9658   (let ((buffer-read-only nil))
9659     (let ((dormant gnus-newsgroup-dormant)
9660           headers h)
9661       (while dormant
9662         (or (gnus-summary-goto-subject (car dormant))
9663             (and (setq h (gnus-get-header-by-num (car dormant)))
9664                  (setq headers (cons h headers))))
9665         (setq dormant (cdr dormant)))
9666       (or headers (error "No dormant articles hidden."))
9667       (goto-char (point-min))
9668       (save-excursion 
9669         (gnus-summary-update-lines 
9670          (point)
9671          (progn
9672            (gnus-summary-prepare-unthreaded (nreverse headers))
9673            (point)))))
9674     (goto-char (point-min))
9675     (gnus-summary-position-cursor)))
9676
9677 (defun gnus-summary-hide-all-dormant ()
9678   "Hide all dormant articles."
9679   (interactive)
9680   (gnus-set-global-variables)
9681   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9682   (gnus-summary-position-cursor))
9683
9684 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9685   "Mark all articles not marked as unread in this newsgroup as read.
9686 If prefix argument ALL is non-nil, all articles are marked as read.
9687 If QUIETLY is non-nil, no questions will be asked.
9688 If TO-HERE is non-nil, it should be a point in the buffer. All
9689 articles before this point will be marked as read.
9690 The number of articles marked as read is returned."
9691   (interactive "P")
9692   (gnus-set-global-variables)
9693   (prog1
9694       (if (or quietly
9695               (not gnus-interactive-catchup) ;Without confirmation?
9696               gnus-expert-user
9697               (gnus-y-or-n-p
9698                (if all
9699                    "Mark absolutely all articles as read? "
9700                  "Mark all unread articles as read? ")))
9701           (if (and not-mark 
9702                    (not gnus-newsgroup-adaptive)
9703                    (not gnus-newsgroup-auto-expire))
9704               (progn
9705                 (and all (setq gnus-newsgroup-marked nil
9706                                gnus-newsgroup-dormant nil))
9707                 (setq gnus-newsgroup-unreads 
9708                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9709             ;; We actually mark all articles as canceled, which we
9710             ;; have to do when using auto-expiry or adaptive scoring. 
9711             (let ((unreads (length gnus-newsgroup-unreads)))
9712               (gnus-summary-show-all-threads)
9713               (if (gnus-summary-first-subject (not all))
9714                   (while (and 
9715                           (if to-here (< (point) to-here) t)
9716                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9717                           (gnus-summary-search-subject nil (not all)))))
9718               (- unreads (length gnus-newsgroup-unreads))
9719               (or to-here
9720                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9721     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9722       (if (and (not to-here) (eq 'nnvirtual (car method)))
9723           (nnvirtual-catchup-group
9724            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9725     (gnus-summary-position-cursor)))
9726
9727 (defun gnus-summary-catchup-to-here (&optional all)
9728   "Mark all unticked articles before the current one as read.
9729 If ALL is non-nil, also mark ticked and dormant articles as read."
9730   (interactive)
9731   (gnus-set-global-variables)
9732   (save-excursion
9733     (and (zerop (forward-line -1))
9734          (progn
9735            (end-of-line)
9736            (gnus-summary-catchup all t (point))
9737            (gnus-set-mode-line 'summary))))
9738   (gnus-summary-position-cursor))
9739
9740 (defun gnus-summary-catchup-all (&optional quietly)
9741   "Mark all articles in this newsgroup as read."
9742   (interactive)
9743   (gnus-set-global-variables)
9744   (gnus-summary-catchup t quietly))
9745
9746 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9747   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9748 If prefix argument ALL is non-nil, all articles are marked as read."
9749   (interactive "P")
9750   (gnus-set-global-variables)
9751   (gnus-summary-catchup all quietly nil 'fast)
9752   ;; Select next newsgroup or exit.
9753   (if (eq gnus-auto-select-next 'quietly)
9754       (gnus-summary-next-group nil)
9755     (gnus-summary-exit)))
9756
9757 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9758   "Mark all articles in this newsgroup as read, and then exit."
9759   (interactive)
9760   (gnus-set-global-variables)
9761   (gnus-summary-catchup-and-exit t quietly))
9762
9763 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9764 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9765   "Mark all articles in this group as read and select the next group.
9766 If given a prefix, mark all articles, unread as well as ticked, as
9767 read." 
9768   (interactive "P")
9769   (gnus-set-global-variables)
9770   (gnus-summary-catchup all)
9771   (gnus-summary-next-group))
9772
9773 ;; Thread-based commands.
9774
9775 (defun gnus-summary-toggle-threads (&optional arg)
9776   "Toggle showing conversation threads.
9777 If ARG is positive number, turn showing conversation threads on."
9778   (interactive "P")
9779   (gnus-set-global-variables)
9780   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9781     (setq gnus-show-threads
9782           (if (null arg) (not gnus-show-threads)
9783             (> (prefix-numeric-value arg) 0)))
9784     (gnus-summary-prepare)
9785     (gnus-summary-goto-subject current)
9786     (gnus-summary-position-cursor)))
9787
9788 (defun gnus-summary-show-all-threads ()
9789   "Show all threads."
9790   (interactive)
9791   (gnus-set-global-variables)
9792   (save-excursion
9793     (let ((buffer-read-only nil))
9794       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9795   (gnus-summary-position-cursor))
9796
9797 (defun gnus-summary-show-thread ()
9798   "Show thread subtrees.
9799 Returns nil if no thread was there to be shown."
9800   (interactive)
9801   (gnus-set-global-variables)
9802   (let ((buffer-read-only nil)
9803         (orig (point))
9804         ;; first goto end then to beg, to have point at beg after let
9805         (end (progn (end-of-line) (point)))
9806         (beg (progn (beginning-of-line) (point))))
9807     (prog1
9808         ;; Any hidden lines here?
9809         (search-forward "\r" end t)
9810       (subst-char-in-region beg end ?\^M ?\n t)
9811       (goto-char orig)
9812       (gnus-summary-position-cursor))))
9813
9814 (defun gnus-summary-hide-all-threads ()
9815   "Hide all thread subtrees."
9816   (interactive)
9817   (gnus-set-global-variables)
9818   (save-excursion
9819     (goto-char (point-min))
9820     (gnus-summary-hide-thread)
9821     (while (and (not (eobp)) (zerop (forward-line 1)))
9822       (gnus-summary-hide-thread)))
9823   (gnus-summary-position-cursor))
9824
9825 (defun gnus-summary-hide-thread ()
9826   "Hide thread subtrees.
9827 Returns nil if no threads were there to be hidden."
9828   (interactive)
9829   (gnus-set-global-variables)
9830   (let ((buffer-read-only nil)
9831         (start (point))
9832         (level (gnus-summary-thread-level))
9833         (end (point)))
9834     ;; Go forward until either the buffer ends or the subthread
9835     ;; ends. 
9836     (if (eobp)
9837         ()
9838       (while (and (zerop (forward-line 1))
9839                   (> (gnus-summary-thread-level) level))
9840         (setq end (point)))
9841       (prog1
9842           (save-excursion
9843             (goto-char end)
9844             (search-backward "\n" start t))
9845         (subst-char-in-region start end ?\n ?\^M t)
9846         (forward-line -1)))))
9847
9848 (defun gnus-summary-go-to-next-thread (&optional previous)
9849   "Go to the same level (or less) next thread.
9850 If PREVIOUS is non-nil, go to previous thread instead.
9851 Return the article number moved to, or nil if moving was impossible."
9852   (let ((level (gnus-summary-thread-level))
9853         (article (gnus-summary-article-number)))
9854     (if previous 
9855         (while (and (zerop (forward-line -1))
9856                     (> (gnus-summary-thread-level) level)))
9857       (while (and (save-excursion
9858                     (forward-line 1)
9859                     (not (eobp)))
9860                   (zerop (forward-line 1))
9861                   (> (gnus-summary-thread-level) level))))
9862     (gnus-summary-recenter)
9863     (gnus-summary-position-cursor)
9864     (let ((oart (gnus-summary-article-number)))
9865       (and (/= oart article) oart))))
9866
9867 (defun gnus-summary-next-thread (n)
9868   "Go to the same level next N'th thread.
9869 If N is negative, search backward instead.
9870 Returns the difference between N and the number of skips actually
9871 done."
9872   (interactive "p")
9873   (gnus-set-global-variables)
9874   (let ((backward (< n 0))
9875         (n (abs n)))
9876   (while (and (> n 0)
9877               (gnus-summary-go-to-next-thread backward))
9878     (setq n (1- n)))
9879   (gnus-summary-position-cursor)
9880   (if (/= 0 n) (gnus-message 7 "No more threads"))
9881   n))
9882
9883 (defun gnus-summary-prev-thread (n)
9884   "Go to the same level previous N'th thread.
9885 Returns the difference between N and the number of skips actually
9886 done."
9887   (interactive "p")
9888   (gnus-set-global-variables)
9889   (gnus-summary-next-thread (- n)))
9890
9891 (defun gnus-summary-go-down-thread (&optional same)
9892   "Go down one level in the current thread.
9893 If SAME is non-nil, also move to articles of the same level."
9894   (let ((level (gnus-summary-thread-level))
9895         (start (point)))
9896     (if (and (zerop (forward-line 1))
9897              (> (gnus-summary-thread-level) level))
9898         t
9899       (goto-char start)
9900       nil)))
9901
9902 (defun gnus-summary-go-up-thread ()
9903   "Go up one level in the current thread."
9904   (let ((level (gnus-summary-thread-level))
9905         (start (point)))
9906     (while (and (zerop (forward-line -1))
9907                 (>= (gnus-summary-thread-level) level)))
9908     (if (>= (gnus-summary-thread-level) level)
9909         (progn
9910           (goto-char start)
9911           nil)
9912       t)))
9913
9914 (defun gnus-summary-down-thread (n)
9915   "Go down thread N steps.
9916 If N is negative, go up instead.
9917 Returns the difference between N and how many steps down that were
9918 taken."
9919   (interactive "p")
9920   (gnus-set-global-variables)
9921   (let ((up (< n 0))
9922         (n (abs n)))
9923   (while (and (> n 0)
9924               (if up (gnus-summary-go-up-thread)
9925                 (gnus-summary-go-down-thread)))
9926     (setq n (1- n)))
9927   (gnus-summary-position-cursor)
9928   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9929   n))
9930
9931 (defun gnus-summary-up-thread (n)
9932   "Go up thread N steps.
9933 If N is negative, go up instead.
9934 Returns the difference between N and how many steps down that were
9935 taken."
9936   (interactive "p")
9937   (gnus-set-global-variables)
9938   (gnus-summary-down-thread (- n)))
9939
9940 (defun gnus-summary-kill-thread (&optional unmark)
9941   "Mark articles under current thread as read.
9942 If the prefix argument is positive, remove any kinds of marks.
9943 If the prefix argument is negative, tick articles instead."
9944   (interactive "P")
9945   (gnus-set-global-variables)
9946   (if unmark
9947       (setq unmark (prefix-numeric-value unmark)))
9948   (let ((killing t)
9949         (level (gnus-summary-thread-level)))
9950     (save-excursion
9951       (while killing
9952         ;; Mark the article...
9953         (cond ((null unmark) (gnus-summary-mark-article-as-read
9954                                gnus-killed-mark))
9955               ((> unmark 0) (gnus-summary-mark-article-as-unread 
9956                              gnus-unread-mark))
9957               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9958         ;; ...and go forward until either the buffer ends or the subtree
9959         ;; ends. 
9960         (if (not (and (zerop (forward-line 1))
9961                       (> (gnus-summary-thread-level) level)))
9962             (setq killing nil))))
9963     ;; Hide killed subtrees.
9964     (and (null unmark)
9965          gnus-thread-hide-killed
9966          (gnus-summary-hide-thread))
9967     ;; If marked as read, go to next unread subject.
9968     (if (null unmark)
9969         ;; Go to next unread subject.
9970         (gnus-summary-next-subject 1 t)))
9971   (gnus-set-mode-line 'summary))
9972
9973 ;; Summary sorting commands
9974
9975 (defun gnus-summary-sort-by-number (&optional reverse)
9976   "Sort summary buffer by article number.
9977 Argument REVERSE means reverse order."
9978   (interactive "P")
9979   (gnus-set-global-variables)
9980   (gnus-summary-sort 
9981    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
9982    ;; a function, so we wrap it.
9983    (cons (lambda () (gnus-summary-article-number))
9984          'gnus-thread-sort-by-number) reverse))
9985
9986 (defun gnus-summary-sort-by-author (&optional reverse)
9987   "Sort summary buffer by author name alphabetically.
9988 If case-fold-search is non-nil, case of letters is ignored.
9989 Argument REVERSE means reverse order."
9990   (interactive "P")
9991   (gnus-set-global-variables)
9992   (gnus-summary-sort
9993    (cons
9994     (lambda ()
9995       (let ((extract (funcall
9996                       gnus-extract-address-components
9997                       (header-from (gnus-get-header-by-num
9998                                     (gnus-summary-article-number))))))
9999         (or (car extract) (cdr extract))))
10000     'gnus-thread-sort-by-author)
10001    reverse))
10002
10003 (defun gnus-summary-sort-by-subject (&optional reverse)
10004   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10005 If case-fold-search is non-nil, case of letters is ignored.
10006 Argument REVERSE means reverse order."
10007   (interactive "P")
10008   (gnus-set-global-variables)
10009   (gnus-summary-sort
10010    (cons
10011     (lambda ()
10012       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
10013     'gnus-thread-sort-by-subject)
10014    reverse))
10015
10016 (defun gnus-summary-sort-by-date (&optional reverse)
10017   "Sort summary buffer by date.
10018 Argument REVERSE means reverse order."
10019   (interactive "P")
10020   (gnus-set-global-variables)
10021   (gnus-summary-sort
10022    (cons
10023     (lambda ()
10024       (gnus-sortable-date
10025        (header-date 
10026         (gnus-get-header-by-num (gnus-summary-article-number)))))
10027     'gnus-thread-sort-by-date)
10028    reverse))
10029
10030 (defun gnus-summary-sort-by-score (&optional reverse)
10031   "Sort summary buffer by score.
10032 Argument REVERSE means reverse order."
10033   (interactive "P")
10034   (gnus-set-global-variables)
10035   (gnus-summary-sort 
10036    (cons (lambda () (gnus-summary-article-score))
10037          'gnus-thread-sort-by-score)
10038    (not reverse)))
10039
10040 (defvar gnus-summary-already-sorted nil)
10041 (defun gnus-summary-sort (predicate reverse)
10042   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10043   (if gnus-summary-already-sorted
10044       ()
10045     (let (buffer-read-only)
10046       (if (not gnus-show-threads)
10047           ;; We do untreaded sorting...
10048           (progn
10049             (goto-char (point-min))
10050             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10051         ;; ... or we do threaded sorting.
10052         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10053               (gnus-summary-prepare-hook nil)
10054               (gnus-summary-already-sorted nil))
10055           ;; We do that by simply regenerating the threads.
10056           (gnus-summary-prepare)
10057           (and gnus-show-threads
10058                gnus-thread-hide-subtree
10059                (gnus-summary-hide-all-threads))
10060           ;; If in async mode, we send some info to the backend.
10061           (and gnus-newsgroup-async
10062                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10063                (gnus-request-asynchronous 
10064                 gnus-newsgroup-name
10065                 (if (and gnus-asynchronous-article-function
10066                          (fboundp gnus-asynchronous-article-function))
10067                     (funcall gnus-asynchronous-article-function
10068                              gnus-newsgroup-threads)))))))))
10069
10070   
10071 (defun gnus-sortable-date (date)
10072   "Make sortable string by string-lessp from DATE.
10073 Timezone package is used."
10074   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10075          (year (aref date 0))
10076          (month (aref date 1))
10077          (day (aref date 2)))
10078     (timezone-make-sortable-date 
10079      year month day 
10080      (timezone-make-time-string
10081       (aref date 3) (aref date 4) (aref date 5)))))
10082
10083
10084 ;; Summary saving commands.
10085
10086 (defun gnus-summary-save-article (&optional n)
10087   "Save the current article using the default saver function.
10088 If N is a positive number, save the N next articles.
10089 If N is a negative number, save the N previous articles.
10090 If N is nil and any articles have been marked with the process mark,
10091 save those articles instead.
10092 The variable `gnus-default-article-saver' specifies the saver function."
10093   (interactive "P")
10094   (gnus-set-global-variables)
10095   (let ((articles (gnus-summary-work-articles n)))
10096     (while articles
10097       (let ((header (gnus-get-header-by-num (car articles))))
10098         (if (vectorp header)
10099             (progn
10100               (save-window-excursion
10101                 (gnus-summary-select-article t nil nil (car articles)))
10102               (or gnus-save-all-headers
10103                   (gnus-article-hide-headers t))
10104               ;; Remove any X-Gnus lines.
10105               (save-excursion
10106                 (save-restriction
10107                   (set-buffer gnus-article-buffer)
10108                   (let ((buffer-read-only nil))
10109                     (goto-char (point-min))
10110                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10111                                                   (point-max)))
10112                     (while (re-search-forward "^X-Gnus" nil t)
10113                       (beginning-of-line)
10114                       (delete-region (point)
10115                                      (progn (forward-line 1) (point))))
10116                     (widen))))
10117               (save-window-excursion
10118                 (if gnus-default-article-saver
10119                     (funcall gnus-default-article-saver)
10120                   (error "No default saver is defined."))))
10121           (if (assq 'name header)
10122               (gnus-copy-file (cdr (assq 'name header)))
10123             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10124       (gnus-summary-remove-process-mark (car articles))
10125       (setq articles (cdr articles)))
10126     (gnus-summary-position-cursor)
10127     n))
10128
10129 (defun gnus-summary-pipe-output (&optional arg)
10130   "Pipe the current article to a subprocess.
10131 If N is a positive number, pipe the N next articles.
10132 If N is a negative number, pipe the N previous articles.
10133 If N is nil and any articles have been marked with the process mark,
10134 pipe those articles instead."
10135   (interactive "P")
10136   (gnus-set-global-variables)
10137   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10138     (gnus-summary-save-article arg)))
10139
10140 (defun gnus-summary-save-article-mail (&optional arg)
10141   "Append the current article to an mail file.
10142 If N is a positive number, save the N next articles.
10143 If N is a negative number, save the N previous articles.
10144 If N is nil and any articles have been marked with the process mark,
10145 save those articles instead."
10146   (interactive "P")
10147   (gnus-set-global-variables)
10148   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10149     (gnus-summary-save-article arg)))
10150
10151 (defun gnus-summary-save-article-rmail (&optional arg)
10152   "Append the current article to an rmail file.
10153 If N is a positive number, save the N next articles.
10154 If N is a negative number, save the N previous articles.
10155 If N is nil and any articles have been marked with the process mark,
10156 save those articles instead."
10157   (interactive "P")
10158   (gnus-set-global-variables)
10159   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10160     (gnus-summary-save-article arg)))
10161
10162 (defun gnus-summary-save-article-file (&optional arg)
10163   "Append the current article to a file.
10164 If N is a positive number, save the N next articles.
10165 If N is a negative number, save the N previous articles.
10166 If N is nil and any articles have been marked with the process mark,
10167 save those articles instead."
10168   (interactive "P")
10169   (gnus-set-global-variables)
10170   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10171     (gnus-summary-save-article arg)))
10172
10173 (defun gnus-read-save-file-name (prompt default-name)
10174   (let ((methods gnus-split-methods)
10175         split-name)
10176     (if (not gnus-split-methods)
10177         ()
10178       (save-excursion
10179         (set-buffer gnus-article-buffer)
10180         (gnus-narrow-to-headers)
10181         (while methods
10182           (goto-char (point-min))
10183           (and (condition-case () 
10184                    (re-search-forward (car (car methods)) nil t)
10185                  (error nil))
10186                (setq split-name (cons (nth 1 (car methods)) split-name)))
10187           (setq methods (cdr methods)))
10188         (widen)))
10189     (cond ((null split-name)
10190            (read-file-name
10191             (concat prompt " (default "
10192                     (file-name-nondirectory default-name) ") ")
10193             (file-name-directory default-name)
10194             default-name))
10195           ((= 1 (length split-name))
10196            (read-file-name
10197             (concat prompt " (default " (car split-name) ") ")
10198             gnus-article-save-directory
10199             (concat gnus-article-save-directory (car split-name))))
10200           (t
10201            (setq split-name (mapcar (lambda (el) (list el))
10202                                     (nreverse split-name)))
10203            (let ((result (completing-read 
10204                           (concat prompt " ")
10205                           split-name nil nil)))
10206              (concat gnus-article-save-directory
10207                      (if (string= result "")
10208                          (car (car split-name))
10209                        result)))))))
10210
10211 (defun gnus-summary-save-in-rmail (&optional filename)
10212   "Append this article to Rmail file.
10213 Optional argument FILENAME specifies file name.
10214 Directory to save to is default to `gnus-article-save-directory' which
10215 is initialized from the SAVEDIR environment variable."
10216   (interactive)
10217   (gnus-set-global-variables)
10218   (let ((default-name
10219           (funcall gnus-rmail-save-name gnus-newsgroup-name
10220                    gnus-current-headers gnus-newsgroup-last-rmail)))
10221     (or filename
10222         (setq filename (gnus-read-save-file-name 
10223                         "Save in rmail file:" default-name)))
10224     (gnus-make-directory (file-name-directory filename))
10225     (gnus-eval-in-buffer-window 
10226      gnus-article-buffer
10227      (save-excursion
10228        (save-restriction
10229          (widen)
10230          (gnus-output-to-rmail filename))))
10231     ;; Remember the directory name to save articles
10232     (setq gnus-newsgroup-last-rmail filename)))
10233
10234 (defun gnus-summary-save-in-mail (&optional filename)
10235   "Append this article to Unix mail file.
10236 Optional argument FILENAME specifies file name.
10237 Directory to save to is default to `gnus-article-save-directory' which
10238 is initialized from the SAVEDIR environment variable."
10239   (interactive)
10240   (gnus-set-global-variables)
10241   (let ((default-name
10242           (funcall gnus-mail-save-name gnus-newsgroup-name
10243                    gnus-current-headers gnus-newsgroup-last-mail)))
10244     (or filename
10245         (setq filename (gnus-read-save-file-name 
10246                         "Save in Unix mail file:" default-name)))
10247     (setq filename
10248           (expand-file-name filename
10249                             (and default-name
10250                                  (file-name-directory default-name))))
10251     (gnus-make-directory (file-name-directory filename))
10252     (gnus-eval-in-buffer-window 
10253      gnus-article-buffer
10254      (save-excursion
10255        (save-restriction
10256          (widen)
10257          (if (and (file-readable-p filename) (rmail-file-p filename))
10258              (gnus-output-to-rmail filename)
10259            (rmail-output filename 1 t t)))))
10260     ;; Remember the directory name to save articles.
10261     (setq gnus-newsgroup-last-mail filename)))
10262
10263 (defun gnus-summary-save-in-file (&optional filename)
10264   "Append this article to file.
10265 Optional argument FILENAME specifies file name.
10266 Directory to save to is default to `gnus-article-save-directory' which
10267 is initialized from the SAVEDIR environment variable."
10268   (interactive)
10269   (gnus-set-global-variables)
10270   (let ((default-name
10271           (funcall gnus-file-save-name gnus-newsgroup-name
10272                    gnus-current-headers gnus-newsgroup-last-file)))
10273     (or filename
10274         (setq filename (gnus-read-save-file-name 
10275                         "Save in file:" default-name)))
10276     (gnus-make-directory (file-name-directory filename))
10277     (gnus-eval-in-buffer-window 
10278      gnus-article-buffer
10279      (save-excursion
10280        (save-restriction
10281          (widen)
10282          (gnus-output-to-file filename))))
10283     ;; Remember the directory name to save articles.
10284     (setq gnus-newsgroup-last-file filename)))
10285
10286 (defun gnus-summary-save-in-pipe (&optional command)
10287   "Pipe this article to subprocess."
10288   (interactive)
10289   (gnus-set-global-variables)
10290   (let ((command (read-string "Shell command on article: "
10291                               gnus-last-shell-command)))
10292     (if (string-equal command "")
10293         (setq command gnus-last-shell-command))
10294     (gnus-eval-in-buffer-window 
10295      gnus-article-buffer
10296      (save-restriction
10297        (widen)
10298        (shell-command-on-region (point-min) (point-max) command nil)))
10299     (setq gnus-last-shell-command command)))
10300
10301 ;; Summary extract commands
10302
10303 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10304   (let ((buffer-read-only nil)
10305         (article (gnus-summary-article-number))
10306         b)
10307     (or (gnus-summary-goto-subject article)
10308         (error (format "No such article: %d" article)))
10309     (or gnus-newsgroup-headers-hashtb-by-number
10310         (gnus-make-headers-hashtable-by-number))
10311     (gnus-summary-position-cursor)
10312     ;; If all commands are to be bunched up on one line, we collect
10313     ;; them here.  
10314     (if gnus-view-pseudos-separately
10315         ()
10316       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10317             files action)
10318         (while ps
10319           (setq action (cdr (assq 'action (car ps))))
10320           (setq files (list (cdr (assq 'name (car ps)))))
10321           (while (and ps (cdr ps)
10322                       (string= (or action "1")
10323                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10324             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10325             (setcdr ps (cdr (cdr ps))))
10326           (if (not files)
10327               ()
10328             (if (not (string-match "%s" action))
10329                 (setq files (cons " " files)))
10330             (setq files (cons " " files))
10331             (and (assq 'execute (car ps))
10332                  (setcdr (assq 'execute (car ps))
10333                          (funcall (if (string-match "%s" action)
10334                                       'format 'concat)
10335                                   action 
10336                                   (mapconcat (lambda (f) f) files " ")))))
10337           (setq ps (cdr ps)))))
10338     (if (and gnus-view-pseudos (not not-view))
10339         (while pslist
10340           (and (assq 'execute (car pslist))
10341                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10342                                      (eq gnus-view-pseudos 'not-confirm)))
10343           (setq pslist (cdr pslist)))
10344       (save-excursion
10345         (while pslist
10346           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10347                                          (gnus-summary-article-number)))
10348           (forward-line 1)
10349           (setq b (point))
10350           (insert "          " (file-name-nondirectory 
10351                                 (cdr (assq 'name (car pslist))))
10352                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10353           (add-text-properties 
10354            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10355                           'gnus-mark gnus-unread-mark 
10356                           'gnus-level 0
10357                           'gnus-pseudo (car pslist)))
10358           (forward-line -1)
10359           (gnus-sethash (int-to-string gnus-reffed-article-number)
10360                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10361           (setq gnus-newsgroup-unreads
10362                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10363           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10364           (setq pslist (cdr pslist)))))))
10365
10366 (defun gnus-pseudos< (p1 p2)
10367   (let ((c1 (cdr (assq 'action p1)))
10368         (c2 (cdr (assq 'action p2))))
10369     (and c1 c2 (string< c1 c2))))
10370
10371 (defun gnus-request-pseudo-article (props)
10372   (cond ((assq 'execute props)
10373          (gnus-execute-command (cdr (assq 'execute props)))))
10374   (let ((gnus-current-article (gnus-summary-article-number)))
10375     (run-hooks 'gnus-mark-article-hook)))
10376
10377 (defun gnus-execute-command (command &optional automatic)
10378   (save-excursion
10379     (gnus-article-setup-buffer)
10380     (set-buffer gnus-article-buffer)
10381     (let ((command (if automatic command (read-string "Command: " command)))
10382           (buffer-read-only nil))
10383       (erase-buffer)
10384       (insert "$ " command "\n\n")
10385       (if gnus-view-pseudo-asynchronously
10386           (start-process "gnus-execute" nil "sh" "-c" command)
10387         (call-process "sh" nil t nil "-c" command)))))
10388
10389 (defun gnus-copy-file (file &optional to)
10390   "Copy FILE to TO."
10391   (interactive
10392    (list (read-file-name "Copy file: " default-directory)
10393          (read-file-name "Copy file to: " default-directory)))
10394   (gnus-set-global-variables)
10395   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10396   (and (file-directory-p to) 
10397        (setq to (concat (file-name-as-directory to)
10398                         (file-name-nondirectory file))))
10399   (copy-file file to))
10400
10401 ;; Summary kill commands.
10402
10403 (defun gnus-summary-edit-global-kill (article)
10404   "Edit the \"global\" kill file."
10405   (interactive (list (gnus-summary-article-number)))
10406   (gnus-set-global-variables)
10407   (gnus-group-edit-global-kill article))
10408
10409 (defun gnus-summary-edit-local-kill ()
10410   "Edit a local kill file applied to the current newsgroup."
10411   (interactive)
10412   (gnus-set-global-variables)
10413   (setq gnus-current-headers 
10414         (gnus-gethash 
10415          (int-to-string (gnus-summary-article-number))
10416          gnus-newsgroup-headers-hashtb-by-number))
10417   (gnus-set-global-variables)
10418   (gnus-group-edit-local-kill 
10419    (gnus-summary-article-number) gnus-newsgroup-name))
10420
10421 \f
10422 ;;;
10423 ;;; Gnus article mode
10424 ;;;
10425
10426 (put 'gnus-article-mode 'mode-class 'special)
10427
10428 (if gnus-article-mode-map
10429     nil
10430   (setq gnus-article-mode-map (make-keymap))
10431   (suppress-keymap gnus-article-mode-map)
10432   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10433   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10434   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10435   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10436   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10437   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10438   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10439   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10440   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10441   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10442   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10443   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10444   
10445   ;; Duplicate almost all summary keystrokes in the article mode map.
10446   (let ((commands 
10447          (list 
10448           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10449           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10450           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10451           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10452           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10453           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10454           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10455           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10456           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10457           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10458           "\C-c\C-v\C-v" "\C-d" "v" 
10459 ;;        "Mt" "M!" "Md" "Mr"
10460 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10461 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10462 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10463 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10464 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10465 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10466 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10467 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10468 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10469 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10470 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10471           )))
10472     (while commands
10473       (define-key gnus-article-mode-map (car commands) 
10474         'gnus-article-summary-command)
10475       (setq commands (cdr commands))))
10476
10477   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10478 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10479                          "=" "n"  "^" "\M-^")))
10480     (while commands
10481       (define-key gnus-article-mode-map (car commands) 
10482         'gnus-article-summary-command-nosave)
10483       (setq commands (cdr commands)))))
10484
10485
10486 (defun gnus-article-mode ()
10487   "Major mode for displaying an article.
10488
10489 All normal editing commands are switched off.
10490
10491 The following commands are available:
10492
10493 \\<gnus-article-mode-map>
10494 \\[gnus-article-next-page]\t Scroll the article one page forwards
10495 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10496 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10497 \\[gnus-article-show-summary]\t Display the summary buffer
10498 \\[gnus-article-mail]\t Send a reply to the address near point
10499 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10500 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10501 \\[gnus-info-find-node]\t Go to the Gnus info node"
10502   (interactive)
10503   (if gnus-visual (gnus-article-make-menu-bar))
10504   (kill-all-local-variables)
10505   (setq mode-line-modified "-- ")
10506   (make-local-variable 'mode-line-format)
10507   (setq mode-line-format (copy-sequence mode-line-format))
10508   (and (equal (nth 3 mode-line-format) "   ")
10509        (setcar (nthcdr 3 mode-line-format) ""))
10510   (setq mode-name "Article")
10511   (setq major-mode 'gnus-article-mode)
10512   (make-local-variable 'minor-mode-alist)
10513   (or (assq 'gnus-show-mime minor-mode-alist)
10514       (setq minor-mode-alist
10515             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10516   (use-local-map gnus-article-mode-map)
10517   (make-local-variable 'page-delimiter)
10518   (setq page-delimiter gnus-page-delimiter)
10519   (buffer-disable-undo (current-buffer))
10520   (setq buffer-read-only t)             ;Disable modification
10521   (run-hooks 'gnus-article-mode-hook))
10522
10523 (defun gnus-article-setup-buffer ()
10524   "Initialize article mode buffer."
10525   (if (get-buffer gnus-article-buffer)
10526       (save-excursion
10527         (set-buffer gnus-article-buffer)
10528         (buffer-disable-undo (current-buffer))
10529         (setq buffer-read-only t)
10530         (gnus-add-current-to-buffer-list)
10531         (or (eq major-mode 'gnus-article-mode)
10532             (gnus-article-mode)))
10533     (save-excursion
10534       (set-buffer (get-buffer-create gnus-article-buffer))
10535       (gnus-add-current-to-buffer-list)
10536       (gnus-article-mode))))
10537
10538 ;; Set article window start at LINE, where LINE is the number of lines
10539 ;; from the head of the article.
10540 (defun gnus-article-set-window-start (&optional line)
10541   (set-window-start 
10542    (get-buffer-window gnus-article-buffer)
10543    (save-excursion
10544      (set-buffer gnus-article-buffer)
10545      (goto-char (point-min))
10546      (if (not line)
10547          (point-min)
10548        (gnus-message 6 "Moved to bookmark")
10549        (search-forward "\n\n" nil t)
10550        (forward-line line)
10551        (point)))))
10552
10553 (defun gnus-request-article-this-buffer (article group)
10554   "Get an article and insert it into this buffer."
10555   (setq group (or group gnus-newsgroup-name))
10556
10557   ;; Open server if it has closed.
10558   (gnus-check-news-server (gnus-find-method-for-group group))
10559
10560   ;; Using `gnus-request-article' directly will insert the article into
10561   ;; `nntp-server-buffer' - so we'll save some time by not having to
10562   ;; copy it from the server buffer into the article buffer.
10563
10564   ;; We only request an article by message-id when we do not have the
10565   ;; headers for it, so we'll have to get those.
10566   (and (stringp article) 
10567        (let ((gnus-override-method gnus-refer-article-method))
10568          (gnus-read-header article)))
10569
10570   ;; If the article number is negative, that means that this article
10571   ;; doesn't belong in this newsgroup (possibly), so we find its
10572   ;; message-id and request it by id instead of number.
10573   (if (not (numberp article))
10574       ()
10575     (save-excursion
10576       (set-buffer gnus-summary-buffer)
10577       (let ((header (gnus-get-header-by-num article)))
10578         (if (< article 0)
10579             (if (vectorp header)
10580                 ;; It's a real article.
10581                 (setq article (header-id header))
10582               ;; It is an extracted pseudo-article.
10583               (setq article 'pseudo)
10584               (gnus-request-pseudo-article header)))
10585
10586         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10587           (if (not (eq (car method) 'nneething))
10588               ()
10589             (let ((dir (concat (file-name-as-directory (nth 1 method))
10590                                (header-subject header))))
10591               (if (file-directory-p dir)
10592                   (progn
10593                     (setq article 'nneething)
10594                     (gnus-group-enter-directory dir)))))))))
10595
10596   ;; Check the cache.
10597   (if (and gnus-use-cache
10598            (numberp article)
10599            (gnus-cache-request-article article group))
10600       'article
10601     ;; Get the article and into the article buffer.
10602     (if (or (stringp article) (numberp article))
10603         (progn
10604           (erase-buffer)
10605           (let ((gnus-override-method 
10606                  (and (stringp article) gnus-refer-article-method)))
10607             (and (gnus-request-article article group (current-buffer))
10608                  'article)))
10609       article)))
10610
10611 (defun gnus-read-header (id)
10612   "Read the headers of article ID and enter them into the Gnus system."
10613   (or gnus-newsgroup-headers-hashtb-by-number
10614       (gnus-make-headers-hashtable-by-number))
10615   (let (header)
10616     (if (not (setq header 
10617                    (car (if (let ((gnus-nov-is-evil t))
10618                               (gnus-retrieve-headers 
10619                                (list id) gnus-newsgroup-name))
10620                             (gnus-get-newsgroup-headers)))))
10621         nil
10622       (if (stringp id)
10623           (header-set-number header gnus-reffed-article-number))
10624       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10625       (gnus-sethash (int-to-string (header-number header)) header
10626                     gnus-newsgroup-headers-hashtb-by-number)
10627       (if (stringp id)
10628           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10629       (setq gnus-current-headers header)
10630       header)))
10631
10632 (defun gnus-article-prepare (article &optional all-headers header)
10633   "Prepare ARTICLE in article mode buffer.
10634 ARTICLE should either be an article number or a Message-ID.
10635 If ARTICLE is an id, HEADER should be the article headers.
10636 If ALL-HEADERS is non-nil, no headers are hidden."
10637   (save-excursion
10638     ;; Make sure we start in a summary buffer.
10639     (or (eq major-mode 'gnus-summary-mode)
10640         (set-buffer gnus-summary-buffer))
10641     (setq gnus-summary-buffer (current-buffer))
10642     ;; Make sure the connection to the server is alive.
10643     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10644         (progn
10645           (gnus-check-news-server 
10646            (gnus-find-method-for-group gnus-newsgroup-name))
10647           (gnus-request-group gnus-newsgroup-name t)))
10648     (or gnus-newsgroup-headers-hashtb-by-number
10649         (gnus-make-headers-hashtable-by-number))
10650     (let* ((article (if header (header-number header) article))
10651            (summary-buffer (current-buffer))
10652            (internal-hook gnus-article-internal-prepare-hook)
10653            (group gnus-newsgroup-name)
10654            result)
10655       (save-excursion
10656         (gnus-article-setup-buffer)
10657         (set-buffer gnus-article-buffer)
10658         (if (not (setq result (let ((buffer-read-only nil))
10659                                 (gnus-request-article-this-buffer 
10660                                  article group))))
10661             ;; There is no such article.
10662             (save-excursion
10663               (if (not (numberp article))
10664                   ()
10665                 (setq gnus-article-current 
10666                       (cons gnus-newsgroup-name article))
10667                 (set-buffer gnus-summary-buffer)
10668                 (setq gnus-current-article article)
10669                 (gnus-summary-mark-article article gnus-canceled-mark))
10670               (gnus-message 1 "No such article (may be canceled)")
10671               (ding)
10672               nil)
10673           (if (or (eq result 'pseudo) (eq result 'nneething))
10674               (progn
10675                 (save-excursion
10676                   (set-buffer summary-buffer)
10677                   (setq gnus-last-article gnus-current-article
10678                         gnus-newsgroup-history (cons gnus-current-article
10679                                                      gnus-newsgroup-history)
10680                         gnus-current-article 0
10681                         gnus-current-headers nil
10682                         gnus-article-current nil)
10683                   (if (eq result 'nneething)
10684                       (gnus-configure-windows 'summary)
10685                     (gnus-configure-windows 'article))
10686                   (gnus-set-global-variables))
10687                 (gnus-set-mode-line 'article))
10688             ;; The result from the `request' was an actual article -
10689             ;; or at least some text that is now displayed in the
10690             ;; article buffer.
10691             (if (and (numberp article)
10692                      (not (eq article gnus-current-article)))
10693                 ;; Seems like a new article has been selected.
10694                 ;; `gnus-current-article' must be an article number.
10695                 (save-excursion
10696                   (set-buffer summary-buffer)
10697                   (setq gnus-last-article gnus-current-article
10698                         gnus-newsgroup-history (cons gnus-current-article
10699                                                      gnus-newsgroup-history)
10700                         gnus-current-article article
10701                         gnus-current-headers 
10702                         (gnus-get-header-by-num gnus-current-article)
10703                         gnus-article-current 
10704                         (cons gnus-newsgroup-name gnus-current-article))
10705                   (gnus-summary-show-thread)
10706                   (run-hooks 'gnus-mark-article-hook)
10707                   (gnus-set-mode-line 'summary)
10708                   (and gnus-visual 
10709                        (run-hooks 'gnus-visual-mark-article-hook))
10710                   ;; Set the global newsgroup variables here.
10711                   ;; Suggested by Jim Sisolak
10712                   ;; <sisolak@trans4.neep.wisc.edu>.
10713                   (gnus-set-global-variables)
10714                   (setq gnus-have-all-headers 
10715                         (or all-headers gnus-show-all-headers))
10716                   (and gnus-use-cache 
10717                        (vectorp (gnus-get-header-by-number article))
10718                        (gnus-cache-possibly-enter-article
10719                         group article
10720                         (gnus-get-header-by-number article)
10721                         (memq article gnus-newsgroup-marked)
10722                         (memq article gnus-newsgroup-dormant)
10723                         (memq article gnus-newsgroup-unreads)))))
10724             ;; Hooks for getting information from the article.
10725             ;; This hook must be called before being narrowed.
10726             (let (buffer-read-only)
10727               (run-hooks 'internal-hook)
10728               (run-hooks 'gnus-article-prepare-hook)
10729               ;; Decode MIME message.
10730               (if (and gnus-show-mime
10731                        (or (not gnus-strict-mime)
10732                            (gnus-fetch-field "Mime-Version")))
10733                   (funcall gnus-show-mime-method))
10734               ;; Perform the article display hooks.
10735               (run-hooks 'gnus-article-display-hook))
10736             ;; Do page break.
10737             (goto-char (point-min))
10738             (and gnus-break-pages (gnus-narrow-to-page))
10739             (gnus-set-mode-line 'article)
10740             (gnus-configure-windows 'article)
10741             (goto-char (point-min))
10742             t))))))
10743
10744 (defun gnus-article-show-all-headers ()
10745   "Show all article headers in article mode buffer."
10746   (save-excursion 
10747     (gnus-article-setup-buffer)
10748     (set-buffer gnus-article-buffer)
10749     (let ((buffer-read-only nil))
10750       (remove-text-properties (point-min) (point-max) 
10751                               gnus-hidden-properties))))
10752
10753 (defun gnus-article-hide-headers-if-wanted ()
10754   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10755 Provided for backwards compatability."
10756   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10757       (gnus-article-hide-headers)))
10758
10759 (defun gnus-article-hide-headers (&optional delete)
10760   "Hide unwanted headers and possibly sort them as well."
10761   (interactive "P")
10762   (save-excursion
10763     (set-buffer gnus-article-buffer)
10764     (save-restriction
10765       (let ((sorted gnus-sorted-header-list)
10766             (buffer-read-only nil)
10767             want-list beg want-l)
10768         ;; First we narrow to just the headers.
10769         (widen)
10770         (goto-char (point-min))
10771         ;; Hide any "From " lines at the beginning of (mail) articles. 
10772         (while (looking-at "From ")
10773           (forward-line 1))
10774         (if (bobp) 
10775             (add-text-properties (point-min) (point) gnus-hidden-properties))
10776         ;; Then treat the rest of the header lines.
10777         (narrow-to-region 
10778          (point) 
10779          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10780         ;; Then we use the two regular expressions
10781         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10782         ;; select which header lines is to remain visible in the
10783         ;; article buffer.
10784         (goto-char (point-min))
10785         (while (re-search-forward "^[^ \t]*:" nil t)
10786           (beginning-of-line)
10787           ;; We add the headers we want to keep to a list and delete
10788           ;; them from the buffer.
10789           (if (or (and (stringp gnus-visible-headers)
10790                        (looking-at gnus-visible-headers))
10791                   (and (not (stringp gnus-visible-headers))
10792                        (stringp gnus-ignored-headers)
10793                        (not (looking-at gnus-ignored-headers))))
10794               (progn
10795                 (setq beg (point))
10796                 (forward-line 1)
10797                 ;; Be sure to get multi-line headers...
10798                 (re-search-forward "^[^ \t]*:" nil t)
10799                 (beginning-of-line)
10800                 (setq want-list 
10801                       (cons (buffer-substring beg (point)) want-list))
10802                 (delete-region beg (point))
10803                 (goto-char beg))
10804             (forward-line 1)))
10805         ;; Next we perform the sorting by looking at
10806         ;; `gnus-sorted-header-list'. 
10807         (goto-char (point-min))
10808         (while (and sorted want-list)
10809           (setq want-l want-list)
10810           (while (and want-l
10811                       (not (string-match (car sorted) (car want-l))))
10812             (setq want-l (cdr want-l)))
10813           (if want-l 
10814               (progn
10815                 (insert (car want-l))
10816                 (setq want-list (delq (car want-l) want-list))))
10817           (setq sorted (cdr sorted)))
10818         ;; Any headers that were not matched by the sorted list we
10819         ;; just tack on the end of the visible header list.
10820         (while want-list
10821           (insert (car want-list))
10822           (setq want-list (cdr want-list)))
10823         ;; And finally we make the unwanted headers invisible.
10824         (if delete
10825             (delete-region (point) (point-max))
10826           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10827           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10828
10829 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10830 (defun gnus-article-treat-overstrike ()
10831   "Translate overstrikes into bold text."
10832   (interactive)
10833   (save-excursion
10834     (set-buffer gnus-article-buffer)
10835     (let ((buffer-read-only nil))
10836       (while (search-forward "\b" nil t)
10837         (let ((next (following-char))
10838               (previous (char-after (- (point) 2))))
10839           (cond ((eq next previous)
10840                  (delete-region (- (point) 2) (point))
10841                  (put-text-property (point) (1+ (point))
10842                                     'face 'bold))
10843                 ((eq next ?_)
10844                  (delete-region (1- (point)) (1+ (point)))
10845                  (put-text-property (1- (point)) (point)
10846                                     'face 'underline))
10847                 ((eq previous ?_)
10848                  (delete-region (- (point) 2) (point))
10849                  (put-text-property (point) (1+ (point))
10850                                     'face 'underline))))))))
10851
10852 (defun gnus-article-word-wrap ()
10853   "Format too long lines."
10854   (interactive)
10855   (save-excursion
10856     (set-buffer gnus-article-buffer)
10857     (let ((buffer-read-only nil))
10858       (goto-char (point-min))
10859       (search-forward "\n\n" nil t)
10860       (end-of-line 1)
10861       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10862             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10863             (adaptive-fill-mode t))
10864         (while (not (eobp))
10865           (and (>= (current-column) (min fill-column (window-width)))
10866                (/= (preceding-char) ?:)
10867                (fill-paragraph nil))
10868           (end-of-line 2))))))
10869
10870 (defun gnus-article-remove-cr ()
10871   "Remove carriage returns from an article."
10872   (interactive)
10873   (save-excursion
10874     (set-buffer gnus-article-buffer)
10875     (let ((buffer-read-only nil))
10876       (goto-char (point-min))
10877       (while (search-forward "\r" nil t)
10878         (replace-match "" t t)))))
10879
10880 (defun gnus-article-display-x-face (&optional force)
10881   "Look for an X-Face header and display it if present."
10882   (interactive (list 'force))
10883   (save-excursion
10884     (set-buffer gnus-article-buffer)
10885     (let ((inhibit-point-motion-hooks t)
10886           (case-fold-search nil))
10887       (save-restriction
10888         (goto-char (point-min))
10889         (search-forward "\n\n")
10890         (narrow-to-region (point-min) (point))
10891         (goto-char (point-min))
10892         (if (or (not gnus-article-x-face-command)
10893                 (and (not force)
10894                      (or (not gnus-article-x-face-too-ugly)
10895                          (string-match gnus-article-x-face-too-ugly
10896                                        (mail-fetch-field "from"))))
10897                 (progn
10898                   (goto-char (point-min))
10899                   (not (re-search-forward "^X-Face: " nil t))))
10900             nil
10901           (let ((beg (point))
10902                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10903             (if (symbolp gnus-article-x-face-command)
10904                 (and (or (fboundp gnus-article-x-face-command)
10905                          (error "%s is not a function"
10906                                 gnus-article-x-face-command))
10907                      (funcall gnus-article-x-face-command beg end))
10908               (call-process-region beg end "sh" nil 0 nil
10909                                    "-c" gnus-article-x-face-command))))))))
10910
10911 (defun gnus-article-de-quoted-unreadable (&optional force)
10912   "Do a naïve translation of a quoted-printable-encoded article.
10913 This is in no way, shape or form meant as a replacement for real MIME
10914 processing, but is simply a stop-gap measure until MIME support is
10915 written.
10916 If FORCE, decode the article whether it is marked as quoted-printable
10917 or not." 
10918   (interactive (list 'force))
10919   (save-excursion
10920     (set-buffer gnus-article-buffer)
10921     (let ((case-fold-search t)
10922           (buffer-read-only nil)
10923           (type (gnus-fetch-field "content-transfer-encoding")))
10924       (if (or force (and type (string-match "quoted-printable" type)))
10925           (progn
10926             (goto-char (point-min))
10927             (search-forward "\n\n" nil 'move)
10928             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10929
10930 (defun gnus-mime-decode-quoted-printable (from to)
10931   ;; Decode quoted-printable from region between FROM and TO.
10932   (save-excursion
10933     (goto-char from)
10934     (while (search-forward "=" to t)
10935       (cond ((eq (following-char) ?\n)
10936              (delete-char -1)
10937              (delete-char 1))
10938             ((looking-at "[0-9A-F][0-9A-F]")
10939              (delete-char -1)
10940              (insert (hexl-hex-string-to-integer
10941                       (buffer-substring (point) (+ 2 (point)))))
10942              (delete-char 2))
10943             ((looking-at "=")
10944              (delete-char 1))
10945             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10946
10947 (defvar gnus-article-time-units
10948   (list (cons 'year (* 365.25 24 60 60))
10949         (cons 'week (* 7 24 60 60))
10950         (cons 'day (* 24 60 60))
10951         (cons 'hour (* 60 60))
10952         (cons 'minute 60)
10953         (cons 'second 1)))
10954
10955 (defun gnus-article-date-ut (&optional type)
10956   "Convert DATE date to universal time in the current article.
10957 If TYPE is `local', convert to local time; if it is `lapsed', output
10958 how much time has lapsed since DATE."
10959   (interactive (list 'ut))
10960   (let ((date (header-date (or gnus-current-headers 
10961                                (gnus-get-header-by-number
10962                                 (gnus-summary-article-number))"")))
10963         (date-regexp "^Date: \\|^X-Sent: "))
10964     (if (or (not date)
10965             (string= date ""))
10966         ()
10967       (save-excursion
10968         (set-buffer gnus-article-buffer)
10969         (let ((buffer-read-only nil))
10970           (goto-char (point-min))
10971           (if (and (re-search-forward date-regexp nil t)
10972                    (progn 
10973                      (beginning-of-line)
10974                      (looking-at date-regexp)))
10975               (delete-region (gnus-point-at-bol)
10976                              (progn (end-of-line) (1+ (point))))
10977             (goto-char (point-min))
10978             (goto-char (- (search-forward "\n\n") 2)))
10979           (insert
10980            (cond 
10981             ((eq type 'local)
10982              (concat "Date: " (condition-case ()
10983                                   (timezone-make-date-arpa-standard date)
10984                                 (error date))
10985                      "\n"))
10986             ((eq type 'ut)
10987              (concat "Date: "
10988                      (condition-case ()
10989                          (timezone-make-date-arpa-standard date nil "UT")
10990                        (error date))
10991                      "\n"))
10992             ((eq type 'lapsed)
10993              ;; If the date is seriously mangled, the timezone
10994              ;; functions are liable to bug out, so we condition-case
10995              ;; the entire thing.  
10996              (let* ((real-sec (condition-case ()
10997                                   (- (gnus-seconds-since-epoch 
10998                                       (timezone-make-date-arpa-standard
10999                                        (current-time-string) 
11000                                        (current-time-zone) "UT"))
11001                                      (gnus-seconds-since-epoch 
11002                                       (timezone-make-date-arpa-standard 
11003                                        date nil "UT")))
11004                                 (error 0)))
11005                     (sec (abs real-sec))
11006                     num prev)
11007                (if (zerop sec)
11008                    "X-Sent: Now\n"
11009                  (concat
11010                   "X-Sent: "
11011                   (mapconcat 
11012                    (lambda (unit)
11013                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11014                          ""
11015                        (setq sec (- sec (* num (cdr unit))))
11016                        (prog1
11017                            (concat (if prev ", " "") (int-to-string 
11018                                                       (floor num))
11019                                    " " (symbol-name (car unit))
11020                                    (if (> num 1) "s" ""))
11021                          (setq prev t))))
11022                    gnus-article-time-units "")
11023                   (if (> real-sec 0)
11024                       " ago\n"
11025                     " in the future\n")))))
11026             (t
11027              (error "Unknown conversion type: %s" type)))))))))
11028
11029 (defun gnus-article-date-local ()
11030   "Convert the current article date to the local timezone."
11031   (interactive)
11032   (gnus-article-date-ut 'local))
11033
11034 (defun gnus-article-date-lapsed ()
11035   "Convert the current article date to time lapsed since it was sent."
11036   (interactive)
11037   (gnus-article-date-ut 'lapsed))
11038
11039 (defun gnus-article-maybe-highlight ()
11040   (if gnus-visual (gnus-article-highlight)))
11041
11042 ;; Article savers.
11043
11044 (defun gnus-output-to-rmail (file-name)
11045   "Append the current article to an Rmail file named FILE-NAME."
11046   (require 'rmail)
11047   ;; Most of these codes are borrowed from rmailout.el.
11048   (setq file-name (expand-file-name file-name))
11049   (setq rmail-default-rmail-file file-name)
11050   (let ((artbuf (current-buffer))
11051         (tmpbuf (get-buffer-create " *Gnus-output*")))
11052     (save-excursion
11053       (or (get-file-buffer file-name)
11054           (file-exists-p file-name)
11055           (if (gnus-yes-or-no-p
11056                (concat "\"" file-name "\" does not exist, create it? "))
11057               (let ((file-buffer (create-file-buffer file-name)))
11058                 (save-excursion
11059                   (set-buffer file-buffer)
11060                   (rmail-insert-rmail-file-header)
11061                   (let ((require-final-newline nil))
11062                     (write-region (point-min) (point-max) file-name t 1)))
11063                 (kill-buffer file-buffer))
11064             (error "Output file does not exist")))
11065       (set-buffer tmpbuf)
11066       (buffer-disable-undo (current-buffer))
11067       (erase-buffer)
11068       (insert-buffer-substring artbuf)
11069       (gnus-convert-article-to-rmail)
11070       ;; Decide whether to append to a file or to an Emacs buffer.
11071       (let ((outbuf (get-file-buffer file-name)))
11072         (if (not outbuf)
11073             (append-to-file (point-min) (point-max) file-name)
11074           ;; File has been visited, in buffer OUTBUF.
11075           (set-buffer outbuf)
11076           (let ((buffer-read-only nil)
11077                 (msg (and (boundp 'rmail-current-message)
11078                           (symbol-value 'rmail-current-message))))
11079             ;; If MSG is non-nil, buffer is in RMAIL mode.
11080             (if msg
11081                 (progn (widen)
11082                        (narrow-to-region (point-max) (point-max))))
11083             (insert-buffer-substring tmpbuf)
11084             (if msg
11085                 (progn
11086                   (goto-char (point-min))
11087                   (widen)
11088                   (search-backward "\^_")
11089                   (narrow-to-region (point) (point-max))
11090                   (goto-char (1+ (point-min)))
11091                   (rmail-count-new-messages t)
11092                   (rmail-show-message msg)))))))
11093     (kill-buffer tmpbuf)))
11094
11095 (defun gnus-output-to-file (file-name)
11096   "Append the current article to a file named FILE-NAME."
11097   (setq file-name (expand-file-name file-name))
11098   (let ((artbuf (current-buffer))
11099         (tmpbuf (get-buffer-create " *Gnus-output*")))
11100     (save-excursion
11101       (set-buffer tmpbuf)
11102       (buffer-disable-undo (current-buffer))
11103       (erase-buffer)
11104       (insert-buffer-substring artbuf)
11105       ;; Append newline at end of the buffer as separator, and then
11106       ;; save it to file.
11107       (goto-char (point-max))
11108       (insert "\n")
11109       (append-to-file (point-min) (point-max) file-name))
11110     (kill-buffer tmpbuf)))
11111
11112 (defun gnus-convert-article-to-rmail ()
11113   "Convert article in current buffer to Rmail message format."
11114   (let ((buffer-read-only nil))
11115     ;; Convert article directly into Babyl format.
11116     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11117     (goto-char (point-min))
11118     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11119     (while (search-forward "\n\^_" nil t) ;single char
11120       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11121     (goto-char (point-max))
11122     (insert "\^_")))
11123
11124 (defun gnus-narrow-to-page (&optional arg)
11125   "Make text outside current page invisible except for page delimiter.
11126 A numeric arg specifies to move forward or backward by that many pages,
11127 thus showing a page other than the one point was originally in."
11128   (interactive "P")
11129   (setq arg (if arg (prefix-numeric-value arg) 0))
11130   (save-excursion
11131     (forward-page -1)                   ;Beginning of current page.
11132     (widen)
11133     (if (> arg 0)
11134         (forward-page arg)
11135       (if (< arg 0)
11136           (forward-page (1- arg))))
11137     ;; Find the end of the page.
11138     (forward-page)
11139     ;; If we stopped due to end of buffer, stay there.
11140     ;; If we stopped after a page delimiter, put end of restriction
11141     ;; at the beginning of that line.
11142     ;; These are commented out.
11143     ;;    (if (save-excursion (beginning-of-line)
11144     ;;                  (looking-at page-delimiter))
11145     ;;  (beginning-of-line))
11146     (narrow-to-region (point)
11147                       (progn
11148                         ;; Find the top of the page.
11149                         (forward-page -1)
11150                         ;; If we found beginning of buffer, stay there.
11151                         ;; If extra text follows page delimiter on same line,
11152                         ;; include it.
11153                         ;; Otherwise, show text starting with following line.
11154                         (if (and (eolp) (not (bobp)))
11155                             (forward-line 1))
11156                         (point)))))
11157
11158 (defun gnus-gmt-to-local ()
11159   "Rewrite Date header described in GMT to local in current buffer.
11160 Intended to be used with gnus-article-prepare-hook."
11161   (save-excursion
11162     (save-restriction
11163       (widen)
11164       (goto-char (point-min))
11165       (narrow-to-region (point-min)
11166                         (progn (search-forward "\n\n" nil 'move) (point)))
11167       (goto-char (point-min))
11168       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11169           (let ((buffer-read-only nil)
11170                 (date (buffer-substring-no-properties
11171                        (match-beginning 1) (match-end 1))))
11172             (delete-region (match-beginning 1) (match-end 1))
11173             (insert
11174              (timezone-make-date-arpa-standard 
11175               date nil (current-time-zone))))))))
11176
11177
11178 ;; Article mode commands
11179
11180 (defun gnus-article-next-page (&optional lines)
11181   "Show next page of current article.
11182 If end of article, return non-nil. Otherwise return nil.
11183 Argument LINES specifies lines to be scrolled up."
11184   (interactive "P")
11185   (move-to-window-line -1)
11186   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11187   (if (save-excursion
11188         (end-of-line)
11189         (and (pos-visible-in-window-p)  ;Not continuation line.
11190              (eobp)))
11191       ;; Nothing in this page.
11192       (if (or (not gnus-break-pages)
11193               (save-excursion
11194                 (save-restriction
11195                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11196           t                             ;Nothing more.
11197         (gnus-narrow-to-page 1)         ;Go to next page.
11198         nil)
11199     ;; More in this page.
11200     (condition-case ()
11201         (scroll-up lines)
11202       (end-of-buffer
11203        ;; Long lines may cause an end-of-buffer error.
11204        (goto-char (point-max))))
11205     nil))
11206
11207 (defun gnus-article-prev-page (&optional lines)
11208   "Show previous page of current article.
11209 Argument LINES specifies lines to be scrolled down."
11210   (interactive "P")
11211   (move-to-window-line 0)
11212   (if (and gnus-break-pages
11213            (bobp)
11214            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11215       (progn
11216         (gnus-narrow-to-page -1) ;Go to previous page.
11217         (goto-char (point-max))
11218         (recenter -1))
11219     (scroll-down lines)))
11220
11221 (defun gnus-article-refer-article ()
11222   "Read article specified by message-id around point."
11223   (interactive)
11224   (search-forward ">" nil t)    ;Move point to end of "<....>".
11225   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11226       (let ((message-id
11227              (buffer-substring (match-beginning 1) (match-end 1))))
11228         (set-buffer gnus-summary-buffer)
11229         (gnus-summary-refer-article message-id))
11230     (error "No references around point")))
11231
11232 (defun gnus-article-show-summary ()
11233   "Reconfigure windows to show summary buffer."
11234   (interactive)
11235   (gnus-configure-windows 'article)
11236   (gnus-summary-goto-subject gnus-current-article))
11237
11238 (defun gnus-article-describe-briefly ()
11239   "Describe article mode commands briefly."
11240   (interactive)
11241   (gnus-message 6
11242    (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")))
11243
11244 (defun gnus-article-summary-command ()
11245   "Execute the last keystroke in the summary buffer."
11246   (interactive)
11247   (let ((obuf (current-buffer))
11248         (owin (current-window-configuration))
11249         func)
11250     (switch-to-buffer gnus-summary-buffer 'norecord)
11251     (setq func (lookup-key (current-local-map) (this-command-keys)))
11252     (call-interactively func)
11253     (set-buffer obuf)
11254     (set-window-configuration owin)
11255     (set-window-start (get-buffer-window (current-buffer)) (point))))
11256
11257 (defun gnus-article-summary-command-nosave ()
11258   "Execute the last keystroke in the summary buffer."
11259   (interactive)
11260   (let (func)
11261     (pop-to-buffer gnus-summary-buffer 'norecord)
11262     (setq func (lookup-key (current-local-map) (this-command-keys)))
11263     (call-interactively func)))
11264
11265 \f
11266 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11267
11268 ;;;###autoload
11269 (defalias 'gnus-batch-kill 'gnus-batch-score)
11270 ;;;###autoload
11271 (defun gnus-batch-score ()
11272   "Run batched scoring.
11273 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11274 Newsgroups is a list of strings in Bnews format.  If you want to score
11275 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11276 score the alt hierarchy, you'd say \"!alt.all\"."
11277   (interactive)
11278   (let* ((yes-and-no
11279           (gnus-newsrc-parse-options
11280            (apply (function concat)
11281                   (mapcar (lambda (g) (concat g " "))
11282                           command-line-args-left))))
11283          (gnus-expert-user t)
11284          (nnmail-spool-file nil)
11285          (gnus-use-dribble-file nil)
11286          (yes (car yes-and-no))
11287          (no (cdr yes-and-no))
11288          group newsrc entry
11289          ;; Disable verbose message.
11290          gnus-novice-user gnus-large-newsgroup)
11291     ;; Eat all arguments.
11292     (setq command-line-args-left nil)
11293     ;; Start Gnus.
11294     (gnus)
11295     ;; Apply kills to specified newsgroups in command line arguments.
11296     (setq newsrc (cdr gnus-newsrc-alist))
11297     (while newsrc
11298       (setq group (car (car newsrc)))
11299       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11300       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11301                (and (car entry)
11302                     (or (eq (car entry) t)
11303                         (not (zerop (car entry)))))
11304                (if yes (string-match yes group) t)
11305                (or (null no) (not (string-match no group))))
11306           (progn
11307             (gnus-summary-read-group group nil t)
11308             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11309                  (gnus-summary-exit))))
11310       (setq newsrc (cdr newsrc)))
11311     ;; Exit Emacs.
11312     (switch-to-buffer gnus-group-buffer)
11313     (gnus-group-save-newsrc)))
11314
11315 (defun gnus-apply-kill-file ()
11316   "Apply a kill file to the current newsgroup.
11317 Returns the number of articles marked as read."
11318   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11319           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11320       (gnus-apply-kill-file-internal)
11321     0))
11322
11323 (defun gnus-kill-save-kill-buffer ()
11324   (save-excursion
11325     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11326       (if (get-file-buffer file)
11327           (progn
11328             (set-buffer (get-file-buffer file))
11329             (and (buffer-modified-p) (save-buffer))
11330             (kill-buffer (current-buffer)))))))
11331
11332 (defvar gnus-kill-file-name "KILL"
11333   "Suffix of the kill files.")
11334
11335 (defun gnus-newsgroup-kill-file (newsgroup)
11336   "Return the name of a kill file name for NEWSGROUP.
11337 If NEWSGROUP is nil, return the global kill file name instead."
11338   (cond ((or (null newsgroup)
11339              (string-equal newsgroup ""))
11340          ;; The global KILL file is placed at top of the directory.
11341          (expand-file-name gnus-kill-file-name
11342                            (or gnus-kill-files-directory "~/News")))
11343         ((gnus-use-long-file-name 'not-kill)
11344          ;; Append ".KILL" to newsgroup name.
11345          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11346                            (or gnus-kill-files-directory "~/News")))
11347         (t
11348          ;; Place "KILL" under the hierarchical directory.
11349          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11350                                    "/" gnus-kill-file-name)
11351                            (or gnus-kill-files-directory "~/News")))))
11352
11353 \f
11354 ;;;
11355 ;;; Dribble file
11356 ;;;
11357
11358 (defvar gnus-dribble-ignore nil)
11359 (defvar gnus-dribble-eval-file nil)
11360
11361 (defun gnus-dribble-file-name ()
11362   (concat gnus-current-startup-file "-dribble"))
11363
11364 (defun gnus-dribble-enter (string)
11365   (if (and (not gnus-dribble-ignore)
11366            gnus-dribble-buffer
11367            (buffer-name gnus-dribble-buffer))
11368       (let ((obuf (current-buffer)))
11369         (set-buffer gnus-dribble-buffer)
11370         (insert string "\n")
11371         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11372         (set-buffer obuf))))
11373
11374 (defun gnus-dribble-read-file ()
11375   (let ((dribble-file (gnus-dribble-file-name)))
11376     (save-excursion 
11377       (set-buffer (setq gnus-dribble-buffer 
11378                         (get-buffer-create 
11379                          (file-name-nondirectory dribble-file))))
11380       (gnus-add-current-to-buffer-list)
11381       (erase-buffer)
11382       (set-visited-file-name dribble-file)
11383       (buffer-disable-undo (current-buffer))
11384       (bury-buffer (current-buffer))
11385       (set-buffer-modified-p nil)
11386       (let ((auto (make-auto-save-file-name))
11387             (gnus-dribble-ignore t))
11388         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11389             (progn
11390               (if (file-newer-than-file-p auto dribble-file)
11391                   (setq dribble-file auto))
11392               (insert-file-contents dribble-file)
11393               (if (not (zerop (buffer-size)))
11394                   (set-buffer-modified-p t))
11395               (if (gnus-y-or-n-p 
11396                    "Auto-save file exists. Do you want to read it? ")
11397                   (setq gnus-dribble-eval-file t))))))))
11398
11399 (defun gnus-dribble-eval-file ()
11400   (if (not gnus-dribble-eval-file)
11401       ()
11402     (setq gnus-dribble-eval-file nil)
11403     (save-excursion
11404       (let ((gnus-dribble-ignore t))
11405         (set-buffer gnus-dribble-buffer)
11406         (eval-buffer (current-buffer))))))
11407
11408 (defun gnus-dribble-delete-file ()
11409   (if (file-exists-p (gnus-dribble-file-name))
11410       (delete-file (gnus-dribble-file-name)))
11411   (if gnus-dribble-buffer
11412       (save-excursion
11413         (set-buffer gnus-dribble-buffer)
11414         (let ((auto (make-auto-save-file-name)))
11415           (if (file-exists-p auto)
11416               (delete-file auto))
11417           (erase-buffer)
11418           (set-buffer-modified-p nil)))))
11419
11420 (defun gnus-dribble-save ()
11421   (if (and gnus-dribble-buffer
11422            (buffer-name gnus-dribble-buffer))
11423       (save-excursion
11424         (set-buffer gnus-dribble-buffer)
11425         (save-buffer))))
11426
11427 (defun gnus-dribble-clear ()
11428   (save-excursion
11429     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11430         (progn
11431           (set-buffer gnus-dribble-buffer)
11432           (erase-buffer)
11433           (set-buffer-modified-p nil)
11434           (setq buffer-saved-size (buffer-size))))))
11435
11436 ;;;
11437 ;;; Server Communication
11438 ;;;
11439
11440 ;; All the Gnus backends have the same interface, and should return
11441 ;; data in a similar format. Below is an overview of what functions
11442 ;; these packages must supply and what results they should return.
11443 ;;
11444 ;; Variables:
11445 ;;
11446 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11447 ;; buffer. 
11448 ;;
11449 ;; Functions for the imaginary backend `choke':
11450 ;;
11451 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11452 ;; Should return all headers for all ARTICLES, or return NOV lines for
11453 ;; the same.
11454 ;;
11455 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11456 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11457 ;; must be returned.
11458 ;;
11459 ;; `choke-close-group GROUP &optional SERVER'
11460 ;; Close group. Most backends won't have to do anything with this
11461 ;; call, but it is an opportunity to clean up, if that is needed. It
11462 ;; is called when Gnus exits a group.
11463 ;;
11464 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11465 ;; Return ARTICLE, which is either an article number or
11466 ;; message-id. Note that not all backends can return articles based on
11467 ;; message-id. 
11468 ;;
11469 ;; `choke-request-list SERVER'
11470 ;; Return a list of all newsgroups on SERVER.
11471 ;;
11472 ;; `choke-request-list-newsgroups SERVER'
11473 ;; Return a list of descriptions of all newsgroups on SERVER.
11474 ;;
11475 ;; `choke-request-newgroups DATE &optional SERVER'
11476 ;; Return a list of all groups that have arrived after DATE on
11477 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11478 ;; always check whether the groups are old or not. Backends that do
11479 ;; not store date information may just return the entire list of
11480 ;; groups, although this might not be a good idea in general.
11481 ;;
11482 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11483 ;; Should return a buffer that is suitable for "posting". nnspool and
11484 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11485 ;; buffer. This function should fill out the appropriate headers. 
11486 ;;
11487 ;; `choke-request-post &optional SERVER'
11488 ;; Function that will be called from a buffer to be posted. 
11489 ;;
11490 ;; `choke-open-server SERVER &optional ARGUMENT'
11491 ;; Open a connection to SERVER.
11492 ;;
11493 ;; `choke-close-server &optional SERVER'
11494 ;; Close the connection to SERVER.
11495 ;;
11496 ;; `choke-server-opened &optional SERVER'
11497 ;; Whether the conenction to SERVER is opened or not.
11498 ;;
11499 ;; `choke-server-status &optional SERVER'
11500 ;; Should return a status string (not in the nntp buffer, but as the
11501 ;; result of the function).
11502 ;;
11503 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11504 ;; Optional function for retrieving active file info on all groups in
11505 ;; GROUPS.  Two return formats are supported: The normal active file
11506 ;; format, and a list of GROUP lines.  This function should return (as
11507 ;; a function value) either `active' or `group', depending on what
11508 ;; format it returns.
11509 ;;
11510 ;; The following functions are optional and apply only to backends
11511 ;; that are able to control the contents of their groups totally
11512 ;; (ie. mail backends.)  Backends that aren't able to do that
11513 ;; shouldn't define these functions at all. Gnus will check for their
11514 ;; presence before attempting to call them.
11515 ;;
11516 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11517 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11518 ;; backends will not be able to expire articles. Should return a list
11519 ;; of all articles that were not expired.
11520 ;;
11521 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11522 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11523 ;; Removes any information it has added to the article (extra headers,
11524 ;; whatever - make it as clean as possible), and then passes the
11525 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11526 ;; function described below. If the ACCEPT-FORM returns a non-nil
11527 ;; value, the article should then be deleted. If LAST is nil, that
11528 ;; means that there will be further calls to this function. This might
11529 ;; be taken as an advice not to save buffers/internal variables just
11530 ;; yet, but wait until the last call to speed things up.
11531 ;;
11532 ;; `choke-request-accept-article GROUP &optional LAST' 
11533 ;; The contents of the current buffer will be put into GROUP.  There
11534 ;; should, of course, be an article in the current buffer.  This
11535 ;; function is normally only called by the function described above,
11536 ;; and LAST works the same way as in that function.
11537 ;;
11538 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11539 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11540 ;; This provides an easy interface for allowing editing of
11541 ;; articles. Note that even headers may be edited, so the backend has
11542 ;; to update any tables (nov buffers, etc) that it maintains after
11543 ;; replacing the article.
11544 ;;
11545 ;; `choke-request-create-group GROUP &optional SERVER'
11546 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11547 ;; might be a group that already exists, but hasn't been registered
11548 ;; yet. 
11549 ;;
11550 ;; All these functions must return nil if they couldn't service the
11551 ;; request. If the optional arguments are not supplied, some "current"
11552 ;; or "default" values should be used. In short, one should emulate an
11553 ;; NNTP server, in a way.
11554 ;;
11555 ;; If you want to write a new backend, you just have to supply the
11556 ;; functions listed above. In addition, you must enter the new backend
11557 ;; into the list of valid select methods:
11558 ;; (setq gnus-valid-select-methods 
11559 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11560 ;; The first element in this list is the name of the backend. Other
11561 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11562 ;; groups), `none' (neither), `respool' (for groups that can control
11563 ;; their contents). 
11564
11565 (defun gnus-start-news-server (&optional confirm)
11566   "Open a method for getting news.
11567 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11568   (let (how)
11569     (if gnus-current-select-method
11570         ;; Stream is already opened.
11571         nil
11572       ;; Open NNTP server.
11573       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11574       (if confirm
11575           (progn
11576             ;; Read server name with completion.
11577             (setq gnus-nntp-server
11578                   (completing-read "NNTP server: "
11579                                    (mapcar (lambda (server) (list server))
11580                                            (cons (list gnus-nntp-server)
11581                                                  gnus-secondary-servers))
11582                                    nil nil gnus-nntp-server))))
11583
11584       (if (and gnus-nntp-server 
11585                (stringp gnus-nntp-server)
11586                (not (string= gnus-nntp-server "")))
11587           (setq gnus-select-method
11588                 (cond ((or (string= gnus-nntp-server "")
11589                            (string= gnus-nntp-server "::"))
11590                        (list 'nnspool (system-name)))
11591                       ((string-match "^:" gnus-nntp-server)
11592                        (list 'nnmh gnus-nntp-server 
11593                              (list 'nnmh-directory 
11594                                    (file-name-as-directory
11595                                     (expand-file-name
11596                                      (concat "~/" (substring
11597                                                    gnus-nntp-server 1)))))
11598                              (list 'nnmh-get-new-mail nil)))
11599                       (t
11600                        (list 'nntp gnus-nntp-server)))))
11601
11602       (setq how (car gnus-select-method))
11603       (cond ((eq how 'nnspool)
11604              (require 'nnspool)
11605              (gnus-message 5 "Looking up local news spool..."))
11606             ((eq how 'nnmh)
11607              (require 'nnmh)
11608              (gnus-message 5 "Looking up mh spool..."))
11609             (t
11610              (require 'nntp)))
11611       (setq gnus-current-select-method gnus-select-method)
11612       (run-hooks 'gnus-open-server-hook)
11613       (or 
11614        ;; gnus-open-server-hook might have opened it
11615        (gnus-server-opened gnus-select-method)  
11616        (gnus-open-server gnus-select-method)
11617        (gnus-y-or-n-p
11618         (format
11619          "%s server on %s can't be opened. Continue? "
11620          (car gnus-select-method) (nth 1 gnus-select-method)))
11621        (progn
11622          (gnus-message 1 "Couldn't open server on %s" 
11623                        (nth 1 gnus-select-method))
11624          (ding)
11625          nil)))))
11626
11627 (defun gnus-check-news-server (&optional method)
11628   "If the news server is down, start it up again."
11629   (let ((method (if method method gnus-select-method)))
11630     (and (stringp method)
11631          (setq method (gnus-server-to-method method)))
11632     (if (gnus-server-opened method)
11633         ;; Stream is already opened.
11634         t
11635       ;; Open server.
11636       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11637       (run-hooks 'gnus-open-server-hook)
11638       (or (gnus-server-opened method)
11639           (gnus-open-server method))
11640       (message ""))))
11641
11642 (defun gnus-nntp-message (&optional message)
11643   "Check the status of the NNTP server.
11644 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11645 is returned insted of the status string."
11646   (let ((status (gnus-status-message (gnus-find-method-for-group 
11647                                       gnus-newsgroup-name)))
11648         (message (or message "")))
11649     (if (and (stringp status) (> (length status) 0))
11650         status message)))
11651
11652 (defun gnus-get-function (method function)
11653   (and (stringp method)
11654        (setq method (gnus-server-to-method method)))
11655   (let ((func (intern (format "%s-%s" (car method) function))))
11656     (if (not (fboundp func)) 
11657         (progn
11658           (require (car method))
11659           (if (not (fboundp func)) 
11660               (error "No such function: %s" func))))
11661     func))
11662
11663 ;;; Interface functions to the backends.
11664
11665 (defun gnus-open-server (method)
11666   (funcall (gnus-get-function method 'open-server)
11667            (nth 1 method) (nthcdr 2 method)))
11668
11669 (defun gnus-close-server (method)
11670   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11671
11672 (defun gnus-request-list (method)
11673   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11674
11675 (defun gnus-request-list-newsgroups (method)
11676   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11677
11678 (defun gnus-request-newgroups (date method)
11679   (funcall (gnus-get-function method 'request-newgroups) 
11680            date (nth 1 method)))
11681
11682 (defun gnus-server-opened (method)
11683   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11684
11685 (defun gnus-status-message (method)
11686   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11687                   method)))
11688     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11689
11690 (defun gnus-request-group (group &optional dont-check)
11691   (let ((method (gnus-find-method-for-group group)))
11692 ;    (and t (message "%s GROUP %s" (car method) group))
11693     (funcall (gnus-get-function method 'request-group) 
11694              (gnus-group-real-name group) (nth 1 method) dont-check)))
11695
11696 (defun gnus-request-asynchronous (group &optional articles)
11697   (let ((method (gnus-find-method-for-group group)))
11698     (funcall (gnus-get-function method 'request-asynchronous) 
11699              (gnus-group-real-name group) (nth 1 method) articles)))
11700
11701 (defun gnus-list-active-group (group)
11702   (let ((method (gnus-find-method-for-group group))
11703         (func 'list-active-group))
11704     (and (gnus-check-backend-function func group)
11705          (funcall (gnus-get-function method func) 
11706                   (gnus-group-real-name group) (nth 1 method)))))
11707
11708 (defun gnus-request-group-description (group)
11709   (let ((method (gnus-find-method-for-group group))
11710         (func 'request-group-description))
11711     (and (gnus-check-backend-function func group)
11712          (funcall (gnus-get-function method func) 
11713                   (gnus-group-real-name group) (nth 1 method)))))
11714
11715 (defun gnus-close-group (group)
11716   (let ((method (gnus-find-method-for-group group)))
11717     (funcall (gnus-get-function method 'close-group) 
11718              (gnus-group-real-name group) (nth 1 method))))
11719
11720 (defun gnus-retrieve-headers (articles group)
11721   (let ((method (gnus-find-method-for-group group)))
11722     (if (and gnus-use-cache (numberp (car articles)))
11723         (gnus-cache-retrieve-headers articles group)
11724       (funcall (gnus-get-function method 'retrieve-headers) 
11725                articles (gnus-group-real-name group) (nth 1 method)))))
11726
11727 (defun gnus-retrieve-groups (groups method)
11728   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11729
11730 (defun gnus-request-article (article group &optional buffer)
11731   (let ((method (gnus-find-method-for-group group)))
11732     (funcall (gnus-get-function method 'request-article) 
11733              article (gnus-group-real-name group) (nth 1 method) buffer)))
11734
11735 (defun gnus-request-head (article group)
11736   (let ((method (gnus-find-method-for-group group)))
11737     (funcall (gnus-get-function method 'request-head) 
11738              article (gnus-group-real-name group) (nth 1 method))))
11739
11740 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11741 (defun gnus-request-post-buffer (post group subject header artbuf
11742                                       info follow-to respect-poster)
11743    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11744                                             group gnus-newsrc-hashtb)))))
11745           (method
11746            (if (and gnus-post-method
11747                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11748                     (memq 'post (assoc
11749                                  (format "%s" (car (gnus-find-method-for-group
11750                                                     gnus-newsgroup-name)))
11751                                         gnus-valid-select-methods)))
11752                gnus-post-method
11753              (gnus-find-method-for-group gnus-newsgroup-name))))
11754      (or (gnus-server-opened method)
11755          (gnus-open-server method)
11756          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11757      (let ((mail-self-blind nil)
11758            (mail-archive-file-name nil))
11759        (funcall (gnus-get-function method 'request-post-buffer) 
11760                 post group subject header artbuf info follow-to
11761                 respect-poster))))
11762
11763 (defun gnus-request-post (method &optional force)
11764   (and (stringp method)
11765        (setq method (gnus-server-to-method method)))
11766   (and (not force) gnus-post-method
11767        (memq 'post (assoc (format "%s" (car method))
11768                           gnus-valid-select-methods))
11769        (setq method gnus-post-method))
11770   (funcall (gnus-get-function method 'request-post) 
11771            (nth 1 method)))
11772
11773 (defun gnus-request-expire-articles (articles group &optional force)
11774   (let ((method (gnus-find-method-for-group group)))
11775     (funcall (gnus-get-function method 'request-expire-articles) 
11776              articles (gnus-group-real-name group) (nth 1 method)
11777              force)))
11778
11779 (defun gnus-request-move-article 
11780   (article group server accept-function &optional last)
11781   (let ((method (gnus-find-method-for-group group)))
11782     (funcall (gnus-get-function method 'request-move-article) 
11783              article (gnus-group-real-name group) 
11784              (nth 1 method) accept-function last)))
11785
11786 (defun gnus-request-accept-article (group &optional last)
11787   (let ((func (if (symbolp group) group
11788                 (car (gnus-find-method-for-group group)))))
11789     (funcall (intern (format "%s-request-accept-article" func))
11790              (if (stringp group) (gnus-group-real-name group) group)
11791              last)))
11792
11793 (defun gnus-request-replace-article (article group buffer)
11794   (let ((func (car (gnus-find-method-for-group group))))
11795     (funcall (intern (format "%s-request-replace-article" func))
11796              article (gnus-group-real-name group) buffer)))
11797
11798 (defun gnus-request-create-group (group)
11799   (let ((method (gnus-find-method-for-group group)))
11800     (funcall (gnus-get-function method 'request-create-group) 
11801              (gnus-group-real-name group) (nth 1 method))))
11802
11803 (defun gnus-member-of-valid (symbol group)
11804   (memq symbol (assoc
11805                 (format "%s" (car (gnus-find-method-for-group group)))
11806                 gnus-valid-select-methods)))
11807
11808 (defsubst gnus-secondary-method-p (method)
11809   (member method gnus-secondary-select-methods))
11810
11811 (defun gnus-find-method-for-group (group &optional info)
11812   (or gnus-override-method
11813       (and (not group)
11814            gnus-select-method)
11815       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11816             method)
11817         (if (or (not info)
11818                 (not (setq method (nth 4 info))))
11819             (setq method gnus-select-method)
11820           (setq method
11821                 (cond ((stringp method)
11822                        (gnus-server-to-method method))
11823                       ((stringp (car method))
11824                        (gnus-server-extend-method group method))
11825                       (t
11826                        method))))
11827         (gnus-server-add-address method))))
11828
11829 (defun gnus-check-backend-function (func group)
11830   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11831                  group)))
11832     (fboundp (intern (format "%s-%s" method func)))))
11833
11834 (defun gnus-methods-using (method)
11835   (let ((valids gnus-valid-select-methods)
11836         outs)
11837     (while valids
11838       (if (memq method (car valids)) 
11839           (setq outs (cons (car valids) outs)))
11840       (setq valids (cdr valids)))
11841     outs))
11842
11843 ;;; 
11844 ;;; Active & Newsrc File Handling
11845 ;;;
11846
11847 ;; Newsrc related functions.
11848 ;; Gnus internal format of gnus-newsrc-alist:
11849 ;; (("alt.general" 3 (1 . 1))
11850 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11851 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11852 ;; The first item is the group name; the second is the subscription
11853 ;; level; the third is either a range of a list of ranges of read
11854 ;; articles, the optional fourth element is a list of marked articles,
11855 ;; the optional fifth element is the select method.
11856 ;;
11857 ;; Gnus internal format of gnus-newsrc-hashtb:
11858 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11859 ;; This is the entry for "alt.misc". The first element is the number
11860 ;; of unread articles in "alt.misc". The cdr of this entry is the
11861 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11862 ;; trivial to remove or add new elements into gnus-newsrc-alist
11863 ;; without scanning the entire list. So, to get the actual information
11864 ;; of "alt.misc", you'd say something like 
11865 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11866 ;;
11867 ;; Gnus internal format of gnus-active-hashtb:
11868 ;; ((1 . 1))
11869 ;;  (5 . 10))
11870 ;;  (67 . 99)) ...)
11871 ;; The only element in each entry in this hash table is a range of
11872 ;; (possibly) available articles. (Articles in this range may have
11873 ;; been expired or canceled.)
11874 ;;
11875 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11876 ;; ("alt.misc" "alt.test" "alt.general" ...)
11877
11878 (defun gnus-setup-news (&optional rawfile level)
11879   "Setup news information.
11880 If RAWFILE is non-nil, the .newsrc file will also be read.
11881 If LEVEL is non-nil, the news will be set up at level LEVEL."
11882   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11883     ;; Clear some variables to re-initialize news information.
11884     (if init (setq gnus-newsrc-alist nil 
11885                    gnus-active-hashtb nil))
11886
11887     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11888     (if init (gnus-read-newsrc-file rawfile))
11889
11890     ;; Read the active file and create `gnus-active-hashtb'.
11891     ;; If `gnus-read-active-file' is nil, then we just create an empty
11892     ;; hash table. The partial filling out of the hash table will be
11893     ;; done in `gnus-get-unread-articles'.
11894     (if (and gnus-read-active-file 
11895              (not level))
11896         (gnus-read-active-file)
11897       (setq gnus-active-hashtb (make-vector 4095 0)))
11898
11899     ;; Possibly eval the dribble file.
11900     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11901
11902     ;; Find the number of unread articles in each non-dead group.
11903     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11904     ;; Find new newsgroups and treat them.
11905     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11906              (gnus-server-opened gnus-select-method))
11907         (gnus-find-new-newsgroups))
11908     (if (and init gnus-check-bogus-newsgroups 
11909              gnus-read-active-file (not level)
11910              (gnus-server-opened gnus-select-method))
11911         (gnus-check-bogus-newsgroups))))
11912
11913 (defun gnus-find-new-newsgroups ()
11914   "Search for new newsgroups and add them.
11915 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11916 The `-n' option line from .newsrc is respected."
11917   (interactive)
11918   (or (gnus-check-first-time-used)
11919       (if (or (consp gnus-check-new-newsgroups)
11920               (eq gnus-check-new-newsgroups 'ask-server))
11921           (gnus-ask-server-for-new-groups)
11922         (let ((groups 0)
11923               group new-newsgroups)
11924           (gnus-message 5 "Checking for new newsgroups...")
11925           (or gnus-have-read-active-file (gnus-read-active-file))
11926           (setq gnus-newsrc-last-checked-date (current-time-string))
11927           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11928           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11929           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11930           (mapatoms
11931            (lambda (sym)
11932              (if (or (null (setq group (symbol-name sym)))
11933                      (gnus-gethash group gnus-killed-hashtb)
11934                      (gnus-gethash group gnus-newsrc-hashtb))
11935                  ()
11936                (let ((do-sub (gnus-matches-options-n group)))
11937                  (cond ((eq do-sub 'subscribe)
11938                         (setq groups (1+ groups))
11939                         (gnus-sethash group group gnus-killed-hashtb)
11940                         (funcall 
11941                          gnus-subscribe-options-newsgroup-method group))
11942                        ((eq do-sub 'ignore)
11943                         nil)
11944                        (t
11945                         (setq groups (1+ groups))
11946                         (gnus-sethash group group gnus-killed-hashtb)
11947                         (if gnus-subscribe-hierarchical-interactive
11948                             (setq new-newsgroups (cons group new-newsgroups))
11949                           (funcall gnus-subscribe-newsgroup-method group)))))))
11950            gnus-active-hashtb)
11951           (if new-newsgroups 
11952               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11953           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11954           (if (> groups 0)
11955               (gnus-message 6 "%d new newsgroup%s arrived." 
11956                             groups (if (> groups 1) "s have" " has"))
11957             (gnus-message 6 "No new newsgroups."))))))
11958
11959 (defun gnus-matches-options-n (group)
11960   ;; Returns `subscribe' if the group is to be uncoditionally
11961   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11962   ;; no match for the group.
11963
11964   ;; First we check the two user variables.
11965   (cond
11966    ((and gnus-options-subscribe
11967          (string-match gnus-options-subscribe group))
11968     'subscribe)
11969    ((and gnus-options-not-subscribe
11970          (string-match gnus-options-not-subscribe group))
11971     'ignore)
11972    ;; Then we go through the list that was retrieved from the .newsrc
11973    ;; file.  This list has elements on the form 
11974    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11975    ;; is in the reverse order of the options line) is returned.
11976    (t
11977     (let ((regs gnus-newsrc-options-n))
11978       (while (and regs
11979                   (not (string-match (car (car regs)) group)))
11980         (setq regs (cdr regs)))
11981       (and regs (cdr (car regs)))))))
11982
11983 (defun gnus-ask-server-for-new-groups ()
11984   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11985          (methods (cons gnus-select-method 
11986                         (append
11987                          (and (consp gnus-check-new-newsgroups)
11988                               gnus-check-new-newsgroups)
11989                          gnus-secondary-select-methods)))
11990          (groups 0)
11991          (new-date (current-time-string))
11992          hashtb group new-newsgroups got-new)
11993     ;; Go thorugh both primary and secondary select methods and
11994     ;; request new newsgroups.  
11995     (while methods
11996       (and (or (gnus-server-opened (car methods))
11997                (gnus-open-server (car methods)))
11998            (gnus-request-newgroups date (car methods))
11999            (save-excursion
12000              (setq got-new t)
12001              (set-buffer nntp-server-buffer)
12002              (or hashtb (setq hashtb (gnus-make-hashtable 
12003                                       (count-lines (point-min) (point-max)))))
12004              ;; Enter all the new groups in a hashtable.
12005              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
12006       (setq methods (cdr methods)))
12007     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12008     ;; Now all new groups from all select methods are in `hashtb'.
12009     (mapatoms
12010      (lambda (group-sym)
12011        (setq group (symbol-name group-sym))
12012        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12013                (member group gnus-zombie-list)
12014                (member group gnus-killed-list))
12015            ;; The group is already known.
12016            ()
12017          (and (symbol-value group-sym)
12018               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12019          (let ((do-sub (gnus-matches-options-n group)))
12020            (cond ((eq do-sub 'subscribe)
12021                   (setq groups (1+ groups))
12022                   (gnus-sethash group group gnus-killed-hashtb)
12023                   (funcall 
12024                    gnus-subscribe-options-newsgroup-method group))
12025                  ((eq do-sub 'ignore)
12026                   nil)
12027                  (t
12028                   (setq groups (1+ groups))
12029                   (gnus-sethash group group gnus-killed-hashtb)
12030                   (if gnus-subscribe-hierarchical-interactive
12031                       (setq new-newsgroups (cons group new-newsgroups))
12032                     (funcall gnus-subscribe-newsgroup-method group)))))))
12033      hashtb)
12034     (if new-newsgroups 
12035         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12036     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12037     (if (> groups 0)
12038         (gnus-message 6 "%d new newsgroup%s arrived." 
12039                       groups (if (> groups 1) "s have" " has")))
12040     got-new))
12041
12042 (defun gnus-check-first-time-used ()
12043   (if (or (> (length gnus-newsrc-alist) 1)
12044           (file-exists-p gnus-startup-file)
12045           (file-exists-p (concat gnus-startup-file ".el"))
12046           (file-exists-p (concat gnus-startup-file ".eld")))
12047       nil
12048     (gnus-message 6 "First time user; subscribing you to default groups")
12049     (or gnus-have-read-active-file (gnus-read-active-file))
12050     (setq gnus-newsrc-last-checked-date (current-time-string))
12051     (let ((groups gnus-default-subscribed-newsgroups)
12052           group)
12053       (if (eq groups t)
12054           nil
12055         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12056         (mapatoms
12057          (lambda (sym)
12058            (if (null (setq group (symbol-name sym)))
12059                ()
12060              (let ((do-sub (gnus-matches-options-n group)))
12061                (cond 
12062                 ((eq do-sub 'subscribe)
12063                  (gnus-sethash group group gnus-killed-hashtb)
12064                  (funcall gnus-subscribe-options-newsgroup-method group))
12065                 ((eq do-sub 'ignore)
12066                  nil)
12067                 (t
12068                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12069          gnus-active-hashtb)
12070         (while groups
12071           (if (gnus-gethash (car groups) gnus-active-hashtb)
12072               (gnus-group-change-level 
12073                (car groups) gnus-level-default-subscribed gnus-level-killed))
12074           (setq groups (cdr groups)))
12075         (gnus-group-make-help-group)
12076         (and gnus-novice-user
12077              (gnus-message 7 "`A k' to list killed groups"))))))
12078
12079 (defun gnus-subscribe-group (group previous &optional method)
12080   (gnus-group-change-level 
12081    (if method
12082        (list t group gnus-level-default-subscribed nil nil method)
12083      group) 
12084    gnus-level-default-subscribed gnus-level-killed previous t))
12085
12086 ;; `gnus-group-change-level' is the fundamental function for changing
12087 ;; subscription levels of newsgroups. This might mean just changing
12088 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12089 ;; again, which subscribes/unsubscribes a group, which is equally
12090 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12091 ;; from 8-9 to 1-7 means that you remove the group from the list of
12092 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12093 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12094 ;; which is trivial.
12095 ;; ENTRY can either be a string (newsgroup name) or a list (if
12096 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12097 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12098 ;; entries. 
12099 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12100 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12101 ;; after. 
12102 (defun gnus-group-change-level (entry level &optional oldlevel
12103                                       previous fromkilled)
12104   (let (group info active num)
12105     ;; Glean what info we can from the arguments
12106     (if (consp entry)
12107         (if fromkilled (setq group (nth 1 entry))
12108           (setq group (car (nth 2 entry))))
12109       (setq group entry))
12110     (if (and (stringp entry)
12111              oldlevel 
12112              (< oldlevel gnus-level-zombie))
12113         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12114     (if (and (not oldlevel)
12115              (consp entry))
12116         (setq oldlevel (car (cdr (nth 2 entry)))))
12117     (if (stringp previous)
12118         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12119
12120     (if (and (>= oldlevel gnus-level-zombie)
12121              (gnus-gethash group gnus-newsrc-hashtb))
12122         ;; We are trying to subscribe a group that is already
12123         ;; subscribed. 
12124         () ; Do nothing. 
12125
12126       (gnus-dribble-enter
12127        (format "(gnus-group-change-level %S %S %S %S %S)" 
12128                group level oldlevel (car (nth 2 previous)) fromkilled))
12129     
12130       ;; Then we remove the newgroup from any old structures, if needed.
12131       ;; If the group was killed, we remove it from the killed or zombie
12132       ;; list. If not, and it is in fact going to be killed, we remove
12133       ;; it from the newsrc hash table and assoc.
12134       (cond ((>= oldlevel gnus-level-zombie)
12135              (if (= oldlevel gnus-level-zombie)
12136                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12137                (setq gnus-killed-list (delete group gnus-killed-list))))
12138             (t
12139              (if (and (>= level gnus-level-zombie)
12140                       entry)
12141                  (progn
12142                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12143                    (if (nth 3 entry)
12144                        (setcdr (gnus-gethash (car (nth 3 entry))
12145                                              gnus-newsrc-hashtb)
12146                                (cdr entry)))
12147                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12148
12149       ;; Finally we enter (if needed) the list where it is supposed to
12150       ;; go, and change the subscription level. If it is to be killed,
12151       ;; we enter it into the killed or zombie list.
12152       (cond ((>= level gnus-level-zombie)
12153              ;; Remove from the hash table.
12154              (gnus-sethash group nil gnus-newsrc-hashtb)
12155              (or (gnus-group-foreign-p group)
12156                  ;; We do not enter foreign groups into the list of dead
12157                  ;; groups.  
12158                  (if (= level gnus-level-zombie)
12159                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12160                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12161             (t
12162              ;; If the list is to be entered into the newsrc assoc, and
12163              ;; it was killed, we have to create an entry in the newsrc
12164              ;; hashtb format and fix the pointers in the newsrc assoc.
12165              (if (>= oldlevel gnus-level-zombie)
12166                  (progn
12167                    (if (listp entry)
12168                        (progn
12169                          (setq info (cdr entry))
12170                          (setq num (car entry)))
12171                      (setq active (gnus-gethash group gnus-active-hashtb))
12172                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12173                      ;; Check whether the group is foreign. If so, the
12174                      ;; foreign select method has to be entered into the
12175                      ;; info. 
12176                      (let ((method (gnus-group-method-name group)))
12177                        (if (eq method gnus-select-method)
12178                            (setq info (list group level nil))
12179                          (setq info (list group level nil nil method)))))
12180                    (or previous 
12181                        (setq previous 
12182                              (let ((p gnus-newsrc-alist))
12183                                (while (cdr (cdr p))
12184                                  (setq p (cdr p)))
12185                                p)))
12186                    (setq entry (cons info (cdr (cdr previous))))
12187                    (if (cdr previous)
12188                        (progn
12189                          (setcdr (cdr previous) entry)
12190                          (gnus-sethash group (cons num (cdr previous)) 
12191                                        gnus-newsrc-hashtb))
12192                      (setcdr previous entry)
12193                      (gnus-sethash group (cons num previous)
12194                                    gnus-newsrc-hashtb))
12195                    (if (cdr entry)
12196                        (setcdr (gnus-gethash (car (car (cdr entry)))
12197                                              gnus-newsrc-hashtb)
12198                                entry)))
12199                ;; It was alive, and it is going to stay alive, so we
12200                ;; just change the level and don't change any pointers or
12201                ;; hash table entries.
12202                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12203
12204 (defun gnus-kill-newsgroup (newsgroup)
12205   "Obsolete function. Kills a newsgroup."
12206   (gnus-group-change-level
12207    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12208
12209 (defun gnus-check-bogus-newsgroups (&optional confirm)
12210   "Remove bogus newsgroups.
12211 If CONFIRM is non-nil, the user has to confirm the deletion of every
12212 newsgroup." 
12213   (let ((newsrc (cdr gnus-newsrc-alist))
12214         bogus group entry)
12215     (gnus-message 5 "Checking bogus newsgroups...")
12216     (or gnus-have-read-active-file (gnus-read-active-file))
12217     ;; Find all bogus newsgroup that are subscribed.
12218     (while newsrc
12219       (setq group (car (car newsrc)))
12220       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12221               (nth 4 (car newsrc))      ; Foreign
12222               (and confirm
12223                    (not (gnus-y-or-n-p
12224                          (format "Remove bogus newsgroup: %s " group)))))
12225           ;; Don't remove.
12226           ()
12227         ;; Found a bogus newsgroup.
12228         (setq bogus (cons group bogus)))
12229       (setq newsrc (cdr newsrc)))
12230     ;; Remove all bogus subscribed groups by first killing them, and
12231     ;; then removing them from the list of killed groups.
12232     (while bogus
12233       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12234            (progn
12235              (gnus-group-change-level entry gnus-level-killed)
12236              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12237       (setq bogus (cdr bogus)))
12238     ;; Then we remove all bogus groups from the list of killed and
12239     ;; zombie groups. They are are removed without confirmation.
12240     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12241           killed)
12242       (while dead-lists
12243         (setq killed (symbol-value (car dead-lists)))
12244         (while killed
12245           (setq group (car killed))
12246           (or (gnus-gethash group gnus-active-hashtb)
12247               ;; The group is bogus.
12248               (set (car dead-lists)
12249                    (delete group (symbol-value (car dead-lists)))))
12250           (setq killed (cdr killed)))
12251         (setq dead-lists (cdr dead-lists))))
12252     (gnus-message 5 "Checking bogus newsgroups...done")))
12253
12254 (defun gnus-check-duplicate-killed-groups ()
12255   "Remove duplicates from the list of killed groups."
12256   (interactive)
12257   (let ((killed gnus-killed-list))
12258     (while killed
12259       (gnus-message 9 "%d" (length killed))
12260       (setcdr killed (delete (car killed) (cdr killed)))
12261       (setq killed (cdr killed)))))
12262
12263 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12264 ;; and compute how many unread articles there are in each group.
12265 (defun gnus-get-unread-articles (&optional level) 
12266   (let* ((newsrc (cdr gnus-newsrc-alist))
12267          (level (or level (1+ gnus-level-subscribed)))
12268          (foreign-level
12269           (min 
12270            (cond ((and gnus-activate-foreign-newsgroups 
12271                        (not (numberp gnus-activate-foreign-newsgroups)))
12272                   (1+ gnus-level-subscribed))
12273                  ((numberp gnus-activate-foreign-newsgroups)
12274                   gnus-activate-foreign-newsgroups)
12275                  (t 0))
12276            level))
12277          info group active virtuals method)
12278     (gnus-message 5 "Checking new news...")
12279
12280     (while newsrc
12281       (setq info (car newsrc)
12282             group (car info)
12283             active (gnus-gethash group gnus-active-hashtb))
12284
12285       ;; Check newsgroups. If the user doesn't want to check them, or
12286       ;; they can't be checked (for instance, if the news server can't
12287       ;; be reached) we just set the number of unread articles in this
12288       ;; newsgroup to t. This means that Gnus thinks that there are
12289       ;; unread articles, but it has no idea how many.
12290       (if (and (setq method (nth 4 info))
12291                (not (gnus-server-equal gnus-select-method
12292                                        (gnus-server-get-method nil method)))
12293                (not (gnus-secondary-method-p method)))
12294           ;; These groups are foreign. Check the level.
12295           (if (<= (nth 1 info) foreign-level)
12296               (if (eq (car (if (stringp method) 
12297                                (gnus-server-to-method method)
12298                              (nth 4 info))) 'nnvirtual)
12299                   ;; We have to activate the virtual groups after all
12300                   ;; the others, so we just pop them on a list for
12301                   ;; now. 
12302                   (setq virtuals (cons info virtuals))
12303                 (and (setq active (gnus-activate-newsgroup (car info)))
12304                      ;; Close the groups as we look at them!
12305                      (gnus-close-group group))))
12306                 
12307         ;; These groups are native or secondary. 
12308         (if (and (not gnus-read-active-file)
12309                  (<= (nth 1 info) level))
12310             (setq active (gnus-activate-newsgroup (car info)))))
12311       
12312       (if active
12313           (gnus-get-unread-articles-in-group info active)
12314         ;; The group couldn't be reached, so we nix out the number of
12315         ;; unread articles and stuff.
12316         (gnus-sethash group nil gnus-active-hashtb)
12317         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12318
12319       (setq newsrc (cdr newsrc)))
12320
12321     ;; Activate the virtual groups. This has to be done after all the
12322     ;; other groups. 
12323     ;; !!! If one virtual group contains another virtual group, even
12324     ;; doing it this way might cause problems.
12325    (while virtuals
12326       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12327            (gnus-get-unread-articles-in-group (car virtuals) active))
12328       (setq virtuals (cdr virtuals)))
12329
12330     (gnus-message 5 "Checking new news...done")))
12331
12332 ;; Create a hash table out of the newsrc alist. The `car's of the
12333 ;; alist elements are used as keys.
12334 (defun gnus-make-hashtable-from-newsrc-alist ()
12335   (let ((alist gnus-newsrc-alist)
12336         (ohashtb gnus-newsrc-hashtb)
12337         prev)
12338     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12339     (setq alist 
12340           (setq prev (setq gnus-newsrc-alist 
12341                            (if (equal (car (car gnus-newsrc-alist))
12342                                       "dummy.group")
12343                                gnus-newsrc-alist
12344                              (cons (list "dummy.group" 0 nil) alist)))))
12345     (while alist
12346       (gnus-sethash (car (car alist)) 
12347                     (cons (and ohashtb (car (gnus-gethash 
12348                                              (car (car alist)) ohashtb))) 
12349                           prev) gnus-newsrc-hashtb)
12350       (setq prev alist
12351             alist (cdr alist)))))
12352
12353 (defun gnus-make-hashtable-from-killed ()
12354   "Create a hash table from the killed and zombie lists."
12355   (let ((lists '(gnus-killed-list gnus-zombie-list))
12356         list)
12357     (setq gnus-killed-hashtb 
12358           (gnus-make-hashtable 
12359            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12360     (while lists
12361       (setq list (symbol-value (car lists)))
12362       (setq lists (cdr lists))
12363       (while list
12364         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12365         (setq list (cdr list))))))
12366
12367 (defun gnus-get-unread-articles-in-group (info active)
12368   (let* ((range (nth 2 info))
12369          (num 0)
12370          (marked (nth 3 info)))
12371     ;; If a cache is present, we may have to alter the active info.
12372     (and gnus-use-cache
12373          (gnus-cache-possibly-alter-active (car info) active))
12374     ;; Modify the list of read articles according to what articles 
12375     ;; are available; then tally the unread articles and add the
12376     ;; number to the group hash table entry.
12377     (cond 
12378      ((zerop (cdr active))
12379       (setq num 0))
12380      ((not range)
12381       (setq num (- (1+ (cdr active)) (car active))))
12382      ((not (listp (cdr range)))
12383       ;; Fix a single (num . num) range according to the
12384       ;; active hash table.
12385       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12386       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12387       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12388       ;; Compute number of unread articles.
12389       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12390      (t
12391       ;; The read list is a list of ranges. Fix them according to
12392       ;; the active hash table.
12393       ;; First peel off any elements that are below the lower
12394       ;; active limit. 
12395       (while (and (cdr range) 
12396                   (>= (car active) 
12397                       (or (and (atom (car (cdr range))) (car (cdr range)))
12398                           (car (car (cdr range))))))
12399         (if (numberp (car range))
12400             (setcar range 
12401                     (cons (car range) 
12402                           (or (and (numberp (car (cdr range)))
12403                                    (car (cdr range))) 
12404                               (cdr (car (cdr range))))))
12405           (setcdr (car range) 
12406                   (or (and (numberp (nth 1 range)) (nth 1 range))
12407                       (cdr (car (cdr range))))))
12408         (setcdr range (cdr (cdr range))))
12409       ;; Adjust the first element to be the same as the lower limit. 
12410       (if (and (not (atom (car range))) 
12411                (< (cdr (car range)) (car active)))
12412           (setcdr (car range) (1- (car active))))
12413       ;; Then we want to peel off any elements that are higher
12414       ;; than the upper active limit.  
12415       (let ((srange range))
12416         ;; Go past all legal elements.
12417         (while (and (cdr srange) 
12418                     (<= (or (and (atom (car (cdr srange)))
12419                                  (car (cdr srange)))
12420                             (car (car (cdr srange)))) (cdr active)))
12421           (setq srange (cdr srange)))
12422         (if (cdr srange)
12423             ;; Nuke all remaining illegal elements.
12424             (setcdr srange nil))
12425
12426         ;; Adjust the final element.
12427         (if (and (not (atom (car srange)))
12428                  (> (cdr (car srange)) (cdr active)))
12429             (setcdr (car srange) (cdr active))))
12430       ;; Compute the number of unread articles.
12431       (while range
12432         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12433                                     (cdr (car range))))
12434                             (or (and (atom (car range)) (car range))
12435                                 (car (car range))))))
12436         (setq range (cdr range)))
12437       (setq num (max 0 (- (cdr active) num)))))
12438     (and info
12439          (progn
12440            (and (assq 'tick marked)
12441                 (inline (gnus-remove-illegal-marked-articles
12442                          (assq 'tick marked) (nth 2 info))))
12443            (and (assq 'dormant marked)
12444                 (inline (gnus-remove-illegal-marked-articles
12445                          (assq 'dormant marked) (nth 2 info))))
12446            (setcar
12447             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12448             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12449                                 (length (cdr (assq 'dormant marked)))))))))
12450     num))
12451
12452 (defun gnus-remove-illegal-marked-articles (marked ranges)
12453   (let ((m (cdr marked)))
12454     ;; Make sure that all ticked articles are a subset of the unread
12455     ;; articles. 
12456     (while m
12457       (if (gnus-member-of-range (car m) ranges)
12458           (setcdr marked (cdr m))
12459         (setq marked m))
12460       (setq m (cdr m)))))
12461
12462 (defun gnus-activate-newsgroup (group)
12463   (let ((method (gnus-find-method-for-group group))
12464         active)
12465     (and (or (gnus-server-opened method) (gnus-open-server method))
12466          (gnus-request-group group)
12467          (save-excursion
12468            (set-buffer nntp-server-buffer)
12469            (goto-char (point-min))
12470            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12471                 (progn
12472                   (goto-char (match-beginning 1))
12473                   (gnus-sethash 
12474                    group (setq active (cons (read (current-buffer))
12475                                             (read (current-buffer))))
12476                    gnus-active-hashtb))
12477                 active)))))
12478
12479 (defun gnus-update-read-articles 
12480   (group unread unselected ticked &optional domarks replied expirable killed
12481          dormant bookmark score)
12482   "Update the list of read and ticked articles in GROUP using the
12483 UNREAD and TICKED lists.
12484 Note: UNSELECTED has to be sorted over `<'.
12485 Returns whether the updating was successful."
12486   (let* ((active (or gnus-newsgroup-active 
12487                      (gnus-gethash group gnus-active-hashtb)))
12488          (entry (gnus-gethash group gnus-newsrc-hashtb))
12489          (info (nth 2 entry))
12490          (marked (nth 3 info))
12491          (prev 1)
12492          (unread (sort (copy-sequence unread) (function <)))
12493          read)
12494     (if (or (not info) (not active))
12495         ;; There is no info on this group if it was, in fact,
12496         ;; killed. Gnus stores no information on killed groups, so
12497         ;; there's nothing to be done. 
12498         ;; One could store the information somewhere temporarily,
12499         ;; perhaps... Hmmm... 
12500         ()
12501       ;; Remove any negative articles numbers.
12502       (while (and unread (< (car unread) 0))
12503         (setq unread (cdr unread)))
12504       ;; Remove any expired article numbers
12505       (while (and unread (< (car unread) (car active)))
12506         (setq unread (cdr unread)))
12507       (while (and ticked (< (car ticked) (car active)))
12508         (setq ticked (cdr ticked)))
12509       (while (and dormant (< (car dormant) (car active)))
12510         (setq dormant (cdr dormant)))
12511       (setq unread (sort (append unselected unread) '<))
12512       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12513       (setcar entry (max 0 (- (length unread) (length ticked) 
12514                               (length dormant))))
12515       ;; Compute the ranges of read articles by looking at the list of
12516       ;; unread articles.  
12517       (while unread
12518         (if (/= (car unread) prev)
12519             (setq read (cons (if (= prev (1- (car unread))) prev
12520                                (cons prev (1- (car unread)))) read)))
12521         (setq prev (1+ (car unread)))
12522         (setq unread (cdr unread)))
12523       (if (<= prev (cdr active))
12524           (setq read (cons (cons prev (cdr active)) read)))
12525       ;; Enter this list into the group info.
12526       (setcar (cdr (cdr info)) 
12527               (if (> (length read) 1) (nreverse read) read))
12528       ;; Enter the list of ticked articles.
12529       (gnus-set-marked-articles 
12530        info ticked
12531        (if domarks replied (cdr (assq 'reply marked)))
12532        (if domarks expirable (cdr (assq 'expire marked)))
12533        (if domarks killed (cdr (assq 'killed marked)))
12534        (if domarks dormant (cdr (assq 'dormant marked)))
12535        (if domarks bookmark (cdr (assq 'bookmark marked)))
12536        (if domarks score (cdr (assq 'score marked))))
12537       t)))
12538
12539 (defun gnus-make-articles-unread (group articles)
12540   "Mark ARTICLES in GROUP as unread."
12541   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12542                           (gnus-gethash (gnus-group-real-name group)
12543                                         gnus-newsrc-hashtb))))
12544          (ranges (nth 2 info))
12545          news)
12546     (while articles
12547       (and (gnus-member-of-range (car articles) ranges)
12548            (setq news (cons (car articles) news)))
12549       (setq articles (cdr articles)))
12550     (if (not news)
12551         ()
12552       (setcar (nthcdr 2 info)
12553               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12554       (gnus-group-update-group group t))))
12555
12556 ;; Get the active file(s) from the backend(s).
12557 (defun gnus-read-active-file ()
12558   (gnus-group-set-mode-line)
12559   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12560                          (gnus-open-server gnus-select-method))
12561                      ;; The native server is available.
12562                      (cons gnus-select-method gnus-secondary-select-methods)
12563                    ;; The native server is down, so we just do the
12564                    ;; secondary ones.   
12565                    gnus-secondary-select-methods))
12566         list-type)
12567     (setq gnus-have-read-active-file nil)
12568     (save-excursion
12569       (set-buffer nntp-server-buffer)
12570       (while methods
12571         (let* ((where (nth 1 (car methods)))
12572                (mesg (format "Reading active file%s via %s..."
12573                              (if (and where (not (zerop (length where))))
12574                                  (concat " from " where) "")
12575                              (car (car methods)))))
12576           (gnus-message 5 mesg)
12577           (gnus-check-news-server (car methods))
12578           (cond 
12579            ((and (eq gnus-read-active-file 'some)
12580                  (gnus-check-backend-function
12581                   'retrieve-groups (car (car methods))))
12582             (let ((newsrc (cdr gnus-newsrc-alist))
12583                   groups)
12584               (while newsrc
12585                 (and (gnus-server-equal 
12586                       (gnus-find-method-for-group
12587                        (car (car newsrc)) (car newsrc))
12588                       (gnus-server-get-method nil (car methods)))
12589                      (setq groups (cons (car (car newsrc)) groups)))
12590                 (setq newsrc (cdr newsrc)))
12591               (setq list-type (gnus-retrieve-groups groups (car methods)))
12592               (cond ((not list-type)
12593                      (gnus-message 
12594                       1 "Cannot read partial active file from %s server." 
12595                       (car (car methods)))
12596                      (ding)
12597                      (sit-for 2))
12598                     ((eq list-type 'active)
12599                      (gnus-active-to-gnus-format (car methods)))
12600                     (t
12601                      (gnus-groups-to-gnus-format (car methods))))))
12602            (t
12603             (if (not (gnus-request-list (car methods)))
12604                 (progn
12605                   (gnus-message 1 "Cannot read active file from %s server." 
12606                                 (car (car methods)))
12607                   (ding))
12608               (gnus-active-to-gnus-format (car methods))
12609               ;; We mark this active file as read.
12610               (setq gnus-have-read-active-file
12611                     (cons (car methods) gnus-have-read-active-file))
12612               (gnus-message 5 "%sdone" mesg)))))
12613         (setq methods (cdr methods))))))
12614
12615 ;; Read an active file and place the results in `gnus-active-hashtb'.
12616 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12617   (let ((cur (current-buffer))
12618         (hashtb (or hashtb 
12619                     (if (and gnus-active-hashtb 
12620                              (not (equal method gnus-select-method)))
12621                         gnus-active-hashtb
12622                       (setq gnus-active-hashtb
12623                             (if (equal method gnus-select-method)
12624                                 (gnus-make-hashtable 
12625                                  (count-lines (point-min) (point-max)))
12626                               (gnus-make-hashtable 4096))))))
12627         (flag-hashtb (gnus-make-hashtable 60)))
12628     ;; Delete unnecessary lines.
12629     (goto-char (point-min))
12630     (while (search-forward "\nto." nil t)
12631       (delete-region (1+ (match-beginning 0)) 
12632                      (progn (forward-line 1) (point))))
12633     (or (string= gnus-ignored-newsgroups "")
12634         (progn
12635           (goto-char (point-min))
12636           (delete-matching-lines gnus-ignored-newsgroups)))
12637     ;; If these are groups from a foreign select method, we insert the
12638     ;; group prefix in front of the group names. 
12639     (and method (not (eq method gnus-select-method))
12640          (let ((prefix (gnus-group-prefixed-name "" method)))
12641            (goto-char (point-min))
12642            (while (and (not (eobp))
12643                        (progn (insert prefix)
12644                               (zerop (forward-line 1)))))))
12645     (goto-char (point-min))
12646     ;; Store active file in hashtable.
12647     (goto-char (point-min))
12648     (if (string-match "%[oO]" gnus-group-line-format)
12649         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12650         ;; If we want information on moderated groups, we use this
12651         ;; loop...   
12652         (let* ((mod-hashtb (make-vector 7 0))
12653                (m (intern "m" mod-hashtb))
12654                group max min)
12655           (while (not (eobp))
12656             (condition-case nil
12657                 (progn
12658                   (narrow-to-region (point) (gnus-point-at-eol))
12659                   (setq group (let ((obarray hashtb)) (read cur)))
12660                   (if (and (numberp (setq max (read cur)))
12661                            (numberp (setq min (read cur)))
12662                            (progn 
12663                              (skip-chars-forward " \t")
12664                              (not
12665                               (or (= (following-char) ?=)
12666                                   (= (following-char) ?x)
12667                                   (= (following-char) ?j)))))
12668                       (set group (cons min max))
12669                     (set group nil))
12670                   ;; Enter moderated groups into a list.
12671                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12672                       (setq gnus-moderated-list 
12673                             (cons (symbol-name group) gnus-moderated-list))))
12674               (error nil))
12675             (widen)
12676             (forward-line 1)))
12677       ;; And if we do not care about moderation, we use this loop,
12678       ;; which is faster.
12679       (let (group max min)
12680         (while (not (eobp))
12681           (condition-case ()
12682               (progn
12683                 (narrow-to-region (point) (gnus-point-at-eol))
12684                 ;; group gets set to a symbol interned in the hash table
12685                 ;; (what a hack!!) - jwz
12686                 (setq group (let ((obarray hashtb)) (read cur)))
12687                 (if (and (numberp (setq max (read cur)))
12688                          (numberp (setq min (read cur)))
12689                          (progn 
12690                            (skip-chars-forward " \t")
12691                            (not
12692                             (or (= (following-char) ?=)
12693                                 (= (following-char) ?x)
12694                                 (= (following-char) ?j)))))
12695                     (set group (cons min max))
12696                   (set group nil)))
12697             (error 
12698              (progn 
12699                (if ignore-errors
12700                    (set group nil)
12701                  (ding) 
12702                  (gnus-message 3 "Warning - illegal active: %s"
12703                                (buffer-substring 
12704                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12705                  nil))))
12706           (widen)
12707           (forward-line 1))))))
12708
12709 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12710   ;; Parse a "groups" active file.
12711   (let ((cur (current-buffer))
12712         (hashtb (or hashtb 
12713                     (if method
12714                         gnus-active-hashtb
12715                       (setq gnus-active-hashtb
12716                             (gnus-make-hashtable 
12717                              (count-lines (point-min) (point-max)))))))
12718         (prefix (and method (not (eq method gnus-select-method))
12719                      (gnus-group-prefixed-name "" method))))
12720
12721     (goto-char (point-min))
12722     (condition-case ()
12723         ;; We split this into to separate loops, one with the prefix
12724         ;; and one without to speed the reading up somewhat.
12725         (if prefix
12726             (let (min max opoint)
12727               (while (not (eobp))
12728                 (read cur) (read cur)
12729                 (setq min (read cur)
12730                       max (read cur)
12731                       opoint (point))
12732                 (skip-chars-forward " \t")
12733                 (insert prefix)
12734                 (goto-char opoint)
12735                 (set (let ((obarray hashtb)) (read cur)) 
12736                      (cons min max))
12737                 (forward-line 1)))
12738           (let (min max)
12739             (while (not (eobp))
12740               (if (= (following-char) ?2)
12741                   (progn
12742                     (read cur) (read cur)
12743                     (setq min (read cur)
12744                           max (read cur))
12745                     (set (let ((obarray hashtb)) (read cur)) 
12746                          (cons min max))))
12747               (forward-line 1))))
12748       (error 
12749        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12750
12751 (defun gnus-read-newsrc-file (&optional force)
12752   "Read startup file.
12753 If FORCE is non-nil, the .newsrc file is read."
12754   ;; Reset variables that might be defined in the .newsrc.eld file.
12755   (let ((variables gnus-variable-list))
12756     (while variables
12757       (set (car variables) nil)
12758       (setq variables (cdr variables))))
12759   (let* ((newsrc-file gnus-current-startup-file)
12760          (quick-file (concat newsrc-file ".el")))
12761     (save-excursion
12762       ;; We always load the .newsrc.eld file. If always contains
12763       ;; much information that can not be gotten from the .newsrc
12764       ;; file (ticked articles, killed groups, foreign methods, etc.)
12765       (gnus-read-newsrc-el-file quick-file)
12766  
12767       (if (or force
12768               (and (file-newer-than-file-p newsrc-file quick-file)
12769                    (file-newer-than-file-p newsrc-file 
12770                                            (concat quick-file "d")))
12771               (not gnus-newsrc-alist))
12772           ;; We read the .newsrc file. Note that if there if a
12773           ;; .newsrc.eld file exists, it has already been read, and
12774           ;; the `gnus-newsrc-hashtb' has been created. While reading
12775           ;; the .newsrc file, Gnus will only use the information it
12776           ;; can find there for changing the data already read -
12777           ;; ie. reading the .newsrc file will not trash the data
12778           ;; already read (except for read articles).
12779           (save-excursion
12780             (gnus-message 5 "Reading %s..." newsrc-file)
12781             (set-buffer (find-file-noselect newsrc-file))
12782             (buffer-disable-undo (current-buffer))
12783             (gnus-newsrc-to-gnus-format)
12784             (kill-buffer (current-buffer))
12785             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12786
12787 (defun gnus-read-newsrc-el-file (file)
12788   (let ((ding-file (concat file "d")))
12789     ;; We always, always read the .eld file.
12790     (gnus-message 5 "Reading %s..." ding-file)
12791     (let (gnus-newsrc-assoc)
12792       (condition-case nil
12793           (load ding-file t t t)
12794         (error nil))
12795       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12796     (let ((inhibit-quit t))
12797       (gnus-uncompress-newsrc-alist))
12798     (gnus-make-hashtable-from-newsrc-alist)
12799     (if (not (file-newer-than-file-p file ding-file))
12800         ()
12801       ;; Old format quick file
12802       (gnus-message 5 "Reading %s..." file)
12803       ;; The .el file is newer than the .eld file, so we read that one
12804       ;; as well. 
12805       (gnus-read-old-newsrc-el-file file))))
12806
12807 ;; Parse the old-style quick startup file
12808 (defun gnus-read-old-newsrc-el-file (file)
12809   (let (newsrc killed marked group m)
12810     (prog1
12811         (let ((gnus-killed-assoc nil)
12812               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12813           (prog1
12814               (condition-case nil
12815                   (load file t t t)
12816                 (error nil))
12817             (setq newsrc gnus-newsrc-assoc
12818                   killed gnus-killed-assoc
12819                   marked gnus-marked-assoc)))
12820       (setq gnus-newsrc-alist nil)
12821       (while newsrc
12822         (setq group (car newsrc))
12823         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12824           (if info
12825               (progn
12826                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12827                 (setcar (cdr info)
12828                         (if (nth 1 group) gnus-level-default-subscribed 
12829                           gnus-level-default-unsubscribed))
12830                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12831             (setq gnus-newsrc-alist
12832                   (cons 
12833                    (setq info
12834                          (list (car group)
12835                                (if (nth 1 group) gnus-level-default-subscribed
12836                                  gnus-level-default-unsubscribed) 
12837                                (cdr (cdr group))))
12838                    gnus-newsrc-alist)))
12839           (if (setq m (assoc (car group) marked))
12840             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12841         (setq newsrc (cdr newsrc)))
12842       (setq newsrc killed)
12843       (while newsrc
12844         (setcar newsrc (car (car newsrc)))
12845         (setq newsrc (cdr newsrc)))
12846       (setq gnus-killed-list killed))
12847     ;; The .el file version of this variable does not begin with
12848     ;; "options", while the .eld version does, so we just add it if it
12849     ;; isn't there.
12850     (and
12851      gnus-newsrc-options 
12852      (progn
12853        (and (not (string-match "^ *options" gnus-newsrc-options))
12854             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12855        (and (not (string-match "\n$" gnus-newsrc-options))
12856             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12857     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12858     (gnus-make-hashtable-from-newsrc-alist)))
12859       
12860 (defun gnus-make-newsrc-file (file)
12861   "Make server dependent file name by catenating FILE and server host name."
12862   (let* ((file (expand-file-name file nil))
12863          (real-file (concat file "-" (nth 1 gnus-select-method))))
12864     (if (or (file-exists-p real-file)
12865             (file-exists-p (concat real-file ".el"))
12866             (file-exists-p (concat real-file ".eld")))
12867         real-file file)))
12868
12869 (defun gnus-uncompress-newsrc-alist ()
12870   ;; Uncompress all lists of marked articles in the newsrc assoc.
12871   (let ((newsrc gnus-newsrc-alist)
12872         marked)
12873     (while newsrc
12874       (if (not (setq marked (nth 3 (car newsrc))))
12875           ()
12876         (while marked
12877           (or (eq 'score (car (car marked)))
12878               (eq 'bookmark (car (car marked)))
12879               (eq 'killed (car (car marked)))
12880               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12881           (setq marked (cdr marked))))
12882       (setq newsrc (cdr newsrc)))))
12883
12884 (defun gnus-compress-newsrc-alist ()
12885   ;; Compress all lists of marked articles in the newsrc assoc.
12886   (let ((newsrc gnus-newsrc-alist)
12887         marked)
12888     (while newsrc
12889       (if (not (setq marked (nth 3 (car newsrc))))
12890           ()
12891         (while marked
12892           (or (eq 'score (car (car marked)))
12893               (eq 'bookmark (car (car marked)))
12894               (eq 'killed (car (car marked)))
12895               (setcdr (car marked) 
12896                       (condition-case ()
12897                           (gnus-compress-sequence 
12898                            (sort (cdr (car marked)) '<) t)
12899                         (error (cdr (car marked))))))
12900           (setq marked (cdr marked))))
12901       (setq newsrc (cdr newsrc)))))
12902
12903 (defun gnus-newsrc-to-gnus-format ()
12904   (setq gnus-newsrc-options "")
12905   (setq gnus-newsrc-options-n nil)
12906
12907   (or gnus-active-hashtb
12908       (setq gnus-active-hashtb (make-vector 4095 0)))
12909   (let ((buf (current-buffer))
12910         (already-read (> (length gnus-newsrc-alist) 1))
12911         group subscribed options-symbol newsrc Options-symbol
12912         symbol reads num1)
12913     (goto-char (point-min))
12914     ;; We intern the symbol `options' in the active hashtb so that we
12915     ;; can `eq' against it later.
12916     (setq options-symbol (intern "options" gnus-active-hashtb))
12917     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12918   
12919     (while (not (eobp))
12920       ;; We first read the first word on the line by narrowing and
12921       ;; then reading into `gnus-active-hashtb'.  Most groups will
12922       ;; already exist in that hashtb, so this will save some string
12923       ;; space.
12924       (narrow-to-region
12925        (point)
12926        (progn (skip-chars-forward "^ \t!:\n") (point)))
12927       (goto-char (point-min))
12928       (setq symbol 
12929             (and (/= (point-min) (point-max))
12930                  (let ((obarray gnus-active-hashtb)) (read buf))))
12931       (widen)
12932       ;; Now, the symbol we have read is either `options' or a group
12933       ;; name.  If it is an options line, we just add it to a string. 
12934       (cond 
12935        ((or (eq symbol options-symbol)
12936             (eq symbol Options-symbol))
12937         (setq gnus-newsrc-options
12938               ;; This concatting is quite inefficient, but since our
12939               ;; thorough studies show that approx 99.37% of all
12940               ;; .newsrc files only contain a single options line, we
12941               ;; don't give a damn, frankly, my dear.
12942               (concat gnus-newsrc-options
12943                       (buffer-substring 
12944                        (gnus-point-at-bol)
12945                        ;; Options may continue on the next line.
12946                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12947                                 (progn (beginning-of-line) (point)))
12948                            (point))))))
12949        (symbol
12950         ;; It was a group name.
12951         (setq subscribed (= (following-char) ?:)
12952               group (symbol-name symbol)
12953               reads nil)
12954         (if (eolp)
12955             ;; If the line ends here, this is clearly a buggy line, so
12956             ;; we put point a the beginning of line and let the cond
12957             ;; below do the error handling.
12958             (beginning-of-line)
12959           ;; We skip to the beginning of the ranges.
12960           (skip-chars-forward "!: \t"))
12961         ;; We are now at the beginning of the list of read articles.
12962         ;; We read them range by range.
12963         (while
12964             (cond 
12965              ((looking-at "[0-9]+")
12966               ;; We narrow and read a number instead of buffer-substring/
12967               ;; string-to-int because it's faster. narrow/widen is
12968               ;; faster than save-restriction/narrow, and save-restriction
12969               ;; produces a garbage object.
12970               (setq num1 (progn
12971                            (narrow-to-region (match-beginning 0) (match-end 0))
12972                            (read buf)))
12973               (widen)
12974               ;; If the next character is a dash, then this is a range.
12975               (if (= (following-char) ?-)
12976                   (progn
12977                     ;; We read the upper bound of the range.
12978                     (forward-char 1)
12979                     (if (not (looking-at "[0-9]+"))
12980                         ;; This is a buggy line, by we pretend that
12981                         ;; it's kinda OK. Perhaps the user should be
12982                         ;; dinged? 
12983                         (setq reads (cons num1 reads))
12984                       (setq reads 
12985                             (cons 
12986                              (cons num1 (progn
12987                                           (narrow-to-region (match-beginning 0) 
12988                                                             (match-end 0))
12989                                           (read buf)))
12990                              reads))
12991                       (widen)))
12992                 ;; It was just a simple number, so we add it to the
12993                 ;; list of ranges.
12994                 (setq reads (cons num1 reads)))
12995               ;; If the next char in ?\n, then we have reached the end
12996               ;; of the line and return nil.
12997               (/= (following-char) ?\n))
12998              ((= (following-char) ?\n)
12999               ;; End of line, so we end.
13000               nil)
13001              (t
13002               ;; Not numbers and not eol, so this might be a buggy
13003               ;; line... 
13004               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13005                   (progn
13006                     ;; The line was buggy.
13007                     (setq group nil)
13008                     (gnus-message 3 "Mangled line: %s" 
13009                                   (buffer-substring (gnus-point-at-bol) 
13010                                                     (gnus-point-at-eol)))
13011                     (ding)
13012                     (sit-for 1)))
13013               nil))
13014           ;; Skip past ", ". Spaces are illegal in these ranges, but
13015           ;; we allow them, because it's a common mistake to put a
13016           ;; space after the comma.
13017           (skip-chars-forward ", "))
13018
13019         ;; We have already read .newsrc.eld, so we gently update the
13020         ;; data in the hash table with the information we have just
13021         ;; read. 
13022         (if (not group)
13023             ()
13024           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13025                 level)
13026             (if info
13027                 ;; There is an entry for this file in the alist.
13028                 (progn
13029                   (setcar (nthcdr 2 info) (nreverse reads))
13030                   ;; We update the level very gently.  In fact, we
13031                   ;; only change it if there's been a status change
13032                   ;; from subscribed to unsubscribed, or vice versa.
13033                   (setq level (nth 1 info))
13034                   (cond ((and (<= level gnus-level-subscribed)
13035                               (not subscribed))
13036                          (setq level (if reads
13037                                          gnus-level-default-unsubscribed 
13038                                        (1+ gnus-level-default-unsubscribed))))
13039                         ((and (> level gnus-level-subscribed) subscribed)
13040                          (setq level gnus-level-default-subscribed)))
13041                   (setcar (cdr info) level))
13042               ;; This is a new group.
13043               (setq info (list group 
13044                                (if subscribed
13045                                    gnus-level-default-subscribed 
13046                                  (if reads
13047                                      (1+ gnus-level-subscribed)
13048                                    gnus-level-default-unsubscribed))
13049                                (nreverse reads))))
13050             (setq newsrc (cons info newsrc))))))
13051       (forward-line 1))
13052     
13053     (setq newsrc (nreverse newsrc))
13054
13055     (if (not already-read)
13056         ()
13057       ;; We now have two newsrc lists - `newsrc', which is what we
13058       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13059       ;; what we've read from .newsrc.eld. We have to merge these
13060       ;; lists. We do this by "attaching" any (foreign) groups in the
13061       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13062       (let ((rc (cdr gnus-newsrc-alist))
13063             (prev gnus-newsrc-alist)
13064             entry mentry)
13065         (while rc
13066           (or (null (nth 4 (car rc))) ; It's a native group.
13067               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13068               (if (setq entry (assoc (car (car prev)) newsrc))
13069                   (setcdr (setq mentry (memq entry newsrc))
13070                           (cons (car rc) (cdr mentry)))
13071                 (setq newsrc (cons (car rc) newsrc))))
13072           (setq prev rc
13073                 rc (cdr rc)))))
13074
13075     (setq gnus-newsrc-alist newsrc)
13076     ;; We make the newsrc hashtb.
13077     (gnus-make-hashtable-from-newsrc-alist)
13078
13079     ;; Finally, if we read some options lines, we parse them.
13080     (or (string= gnus-newsrc-options "")
13081         (gnus-newsrc-parse-options gnus-newsrc-options))))
13082
13083 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13084 ;; The return value will be a list on the form
13085 ;; ((regexp1 . ignore)
13086 ;;  (regexp2 . subscribe)...)
13087 ;; When handling new newsgroups, groups that match a `ignore' regexp
13088 ;; will be ignored, and groups that match a `subscribe' regexp will be
13089 ;; subscribed. A line like
13090 ;; options -n !all rec.all
13091 ;; will lead to a list that looks like
13092 ;; (("^rec\\..+" . subscribe) 
13093 ;;  ("^.+" . ignore))
13094 ;; So all "rec.*" groups will be subscribed, while all the other
13095 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13096 ;; different from "options -n rec.all !all". 
13097 (defun gnus-newsrc-parse-options (options)
13098   (let (out eol)
13099     (save-excursion
13100       (gnus-set-work-buffer)
13101       (insert (regexp-quote options))
13102       ;; First we treat all continuation lines.
13103       (goto-char (point-min))
13104       (while (re-search-forward "\n[ \t]+" nil t)
13105         (replace-match " " t t))
13106       ;; Then we transform all "all"s into ".+"s.
13107       (goto-char (point-min))
13108       (while (re-search-forward "\\ball\\b" nil t)
13109         (replace-match ".+" t t))
13110       (goto-char (point-min))
13111       ;; We remove all other options than the "-n" ones.
13112       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13113         (replace-match " ")
13114         (forward-char -1))
13115       (goto-char (point-min))
13116
13117       ;; We are only interested in "options -n" lines - we
13118       ;; ignore the other option lines.
13119       (while (re-search-forward "[ \t]-n" nil t)
13120         (setq eol 
13121               (or (save-excursion
13122                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13123                          (- (point) 2)))
13124                   (gnus-point-at-eol)))
13125         ;; Search for all "words"...
13126         (while (re-search-forward "[^ \t,\n]+" eol t)
13127           (if (= (char-after (match-beginning 0)) ?!)
13128               ;; If the word begins with a bang (!), this is a "not"
13129               ;; spec. We put this spec (minus the bang) and the
13130               ;; symbol `ignore' into the list.
13131               (setq out (cons (cons (concat 
13132                                      "^" (buffer-substring 
13133                                           (1+ (match-beginning 0))
13134                                           (match-end 0)))
13135                                     'ignore) out))
13136             ;; There was no bang, so this is a "yes" spec.
13137             (setq out (cons (cons (concat 
13138                                    "^" (buffer-substring (match-beginning 0)
13139                                                          (match-end 0)))
13140                                   'subscribe) out)))))
13141     
13142       (setq gnus-newsrc-options-n out))))
13143                
13144
13145 (defun gnus-save-newsrc-file ()
13146   "Save .newsrc file."
13147   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13148   ;; from the variable gnus-newsrc-alist.
13149   (and (or gnus-newsrc-alist gnus-killed-list)
13150        gnus-current-startup-file
13151        (progn
13152          (run-hooks 'gnus-save-newsrc-hook)
13153          (save-excursion
13154            (if (and gnus-use-dribble-file
13155                     (or (not gnus-dribble-buffer)
13156                         (not (buffer-name gnus-dribble-buffer))
13157                         (zerop (save-excursion
13158                                  (set-buffer gnus-dribble-buffer)
13159                                  (buffer-size)))))
13160                (gnus-message 4 "(No changes need to be saved)")
13161              (if gnus-save-newsrc-file
13162                  (progn
13163                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13164                    ;; Make backup file of master newsrc.
13165                    (gnus-gnus-to-newsrc-format)
13166                    (gnus-message 5 "Saving %s...done"
13167                                  gnus-current-startup-file)))
13168              ;; Quickly loadable .newsrc.
13169              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13170              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13171              (gnus-add-current-to-buffer-list)
13172              (buffer-disable-undo (current-buffer))
13173              (erase-buffer)
13174              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13175              (gnus-gnus-to-quick-newsrc-format)
13176              (save-buffer)
13177              (kill-buffer (current-buffer))
13178              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13179              (gnus-dribble-delete-file))))))
13180
13181 (defun gnus-gnus-to-quick-newsrc-format ()
13182   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13183   (insert ";; (ding) Gnus startup file.\n")
13184   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13185   (insert ";; to read .newsrc.\n")
13186   (let ((variables gnus-variable-list)
13187         (inhibit-quit t)
13188         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13189         variable)
13190     ;; insert lisp expressions.
13191     (gnus-compress-newsrc-alist)
13192     (while variables
13193       (setq variable (car variables))
13194       (and (boundp variable)
13195            (symbol-value variable)
13196            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13197            (insert "(setq " (symbol-name variable) " '"
13198                    (prin1-to-string (symbol-value variable))
13199                    ")\n"))
13200       (setq variables (cdr variables)))
13201     (gnus-uncompress-newsrc-alist)))
13202
13203
13204 (defun gnus-gnus-to-newsrc-format ()
13205   ;; Generate and save the .newsrc file.
13206   (let ((newsrc (cdr gnus-newsrc-alist))
13207         info ranges range)
13208     (save-excursion
13209       (set-buffer (create-file-buffer gnus-current-startup-file))
13210       (set-visited-file-name gnus-current-startup-file)
13211       (buffer-disable-undo (current-buffer))
13212       (erase-buffer)
13213       ;; Write options.
13214       (if gnus-newsrc-options (insert gnus-newsrc-options))
13215       ;; Write subscribed and unsubscribed.
13216       (while newsrc
13217         (setq info (car newsrc))
13218         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13219             (progn
13220               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13221                                      "!" ":"))
13222               (if (setq ranges (nth 2 info))
13223                   (progn
13224                     (insert " ")
13225                     (if (not (listp (cdr ranges)))
13226                         (if (= (car ranges) (cdr ranges))
13227                             (insert (int-to-string (car ranges)))
13228                           (insert (int-to-string (car ranges)) "-" 
13229                                   (int-to-string (cdr ranges))))
13230                       (while ranges
13231                         (setq range (car ranges)
13232                               ranges (cdr ranges))
13233                         (if (or (atom range) (= (car range) (cdr range)))
13234                             (insert (int-to-string 
13235                                      (or (and (atom range) range) 
13236                                          (car range))))
13237                           (insert (int-to-string (car range)) "-"
13238                                   (int-to-string (cdr range))))
13239                         (if ranges (insert ","))))))
13240               (insert "\n")))
13241         (setq newsrc (cdr newsrc)))
13242       (save-buffer)
13243       (kill-buffer (current-buffer)))))
13244
13245 (defun gnus-read-all-descriptions-files ()
13246   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13247     (while methods
13248       (gnus-read-descriptions-file (car methods))
13249       (setq methods (cdr methods)))
13250     t))
13251
13252 (defun gnus-read-descriptions-file (&optional method)
13253   (let ((method (or method gnus-select-method)))
13254     ;; We create the hashtable whether we manage to read the desc file
13255     ;; to avoid trying to re-read after a failed read.
13256     (or gnus-description-hashtb
13257         (setq gnus-description-hashtb 
13258               (gnus-make-hashtable (length gnus-active-hashtb))))
13259     ;; Mark this method's desc file as read.
13260     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13261                   gnus-description-hashtb)
13262
13263     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13264     (cond 
13265      ((not (or (gnus-server-opened method)
13266                (gnus-open-server method)))
13267       (gnus-message 1 "Couldn't open server")
13268       nil)
13269      ((not (gnus-request-list-newsgroups method))
13270       (gnus-message 1 "Couldn't read newsgroups descriptions")
13271       nil)
13272      (t
13273       (let (group)
13274         (save-excursion
13275           (save-restriction
13276             (set-buffer nntp-server-buffer)
13277             (goto-char (point-min))
13278             (if (or (search-forward "\n.\n" nil t)
13279                     (goto-char (point-max)))
13280                 (progn
13281                   (beginning-of-line)
13282                   (narrow-to-region (point-min) (point))))
13283             (goto-char (point-min))
13284             (while (not (eobp))
13285               ;; If we get an error, we set group to 0, which is not a
13286               ;; symbol... 
13287               (setq group 
13288                     (condition-case ()
13289                         (let ((obarray gnus-description-hashtb))
13290                           ;; Group is set to a symbol interned in this
13291                           ;; hash table.
13292                           (read nntp-server-buffer))
13293                       (error 0)))
13294               (skip-chars-forward " \t")
13295               ;; ... which leads to this line being effectively ignored.
13296               (and (symbolp group)
13297                    (set group (buffer-substring 
13298                                (point) (progn (end-of-line) (point)))))
13299               (forward-line 1))))
13300         (gnus-message 5 "Reading descriptions file...done")
13301         t)))))
13302
13303 (defun gnus-group-get-description (group)
13304   ;; Get the description of a group by sending XGTITLE to the server.
13305   (and (gnus-request-group-description group)
13306        (save-excursion
13307          (set-buffer nntp-server-buffer)
13308          (goto-char (point-min))
13309          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13310               (buffer-substring (match-beginning 1) (match-end 1))))))
13311
13312 ;;;
13313 ;;; Server
13314 ;;;
13315
13316 (defvar gnus-server-mode-hook nil
13317   "Hook run in `gnus-server-mode' buffers.")
13318
13319 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13320   "Format of server lines.
13321 It works along the same lines as a normal formatting string,
13322 with some simple extensions.")
13323
13324 (defvar gnus-server-mode-line-format "(ding) List of servers"
13325   "The format specification for the server mode line.")
13326
13327 (defconst gnus-server-line-format-alist
13328   (list (list ?h 'how ?s)
13329         (list ?n 'name ?s)
13330         (list ?w 'where ?s)
13331         ))
13332
13333 (defconst gnus-server-mode-line-format-alist 
13334   (list (list ?S 'news-server ?s)
13335         (list ?M 'news-method ?s)
13336         (list ?u 'user-defined ?s)))
13337
13338 (defvar gnus-server-line-format-spec nil)
13339 (defvar gnus-server-mode-line-format-spec nil)
13340 (defvar gnus-server-killed-servers nil)
13341
13342 (defvar gnus-server-mode-map nil)
13343 (put 'gnus-server-mode 'mode-class 'special)
13344
13345 (if gnus-server-mode-map
13346     nil
13347   (setq gnus-server-mode-map (make-sparse-keymap))
13348   (suppress-keymap gnus-server-mode-map)
13349   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13350   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13351   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13352   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13353   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13354   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13355   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13356   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13357   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13358   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13359
13360 (defun gnus-server-mode ()
13361   "Major mode for listing and editing servers.
13362
13363 All normal editing commands are switched off.
13364 \\<gnus-server-mode-map>
13365
13366 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13367
13368 The following commands are available:
13369
13370 \\{gnus-server-mode-map}"
13371   (interactive)
13372   (if gnus-visual (gnus-server-make-menu-bar))
13373   (kill-all-local-variables)
13374   (setq mode-line-modified "-- ")
13375   (make-local-variable 'mode-line-format)
13376   (setq mode-line-format (copy-sequence mode-line-format))
13377   (and (equal (nth 3 mode-line-format) "   ")
13378        (setcar (nthcdr 3 mode-line-format) ""))
13379   (setq major-mode 'gnus-server-mode)
13380   (setq mode-name "Server")
13381 ;  (gnus-group-set-mode-line)
13382   (setq mode-line-process nil)
13383   (use-local-map gnus-server-mode-map)
13384   (buffer-disable-undo (current-buffer))
13385   (setq truncate-lines t)
13386   (setq buffer-read-only t)
13387   (run-hooks 'gnus-server-mode-hook))
13388
13389 (defun gnus-server-insert-server-line (sformat name method)
13390   (let* ((sformat (or sformat gnus-server-line-format-spec))
13391          (how (car method))
13392          (where (nth 1 method))
13393          b)
13394     (beginning-of-line)
13395     (setq b (point))
13396     ;; Insert the text.
13397     (insert (eval sformat))
13398     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13399
13400 (defun gnus-server-setup-buffer ()
13401   (if (get-buffer gnus-server-buffer)
13402       ()
13403     (save-excursion
13404       (set-buffer (get-buffer-create gnus-server-buffer))
13405       (gnus-server-mode)
13406       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13407
13408 (defun gnus-server-prepare ()
13409   (setq gnus-server-mode-line-format-spec 
13410         (gnus-parse-format gnus-server-mode-line-format 
13411                            gnus-server-mode-line-format-alist))
13412   (setq gnus-server-line-format-spec 
13413         (gnus-parse-format gnus-server-line-format 
13414                            gnus-server-line-format-alist))
13415   (let ((alist gnus-server-alist)
13416         (buffer-read-only nil))
13417     (erase-buffer)
13418     (while alist
13419       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13420       (setq alist (cdr alist))))
13421   (goto-char (point-min))
13422   (gnus-server-position-cursor))
13423
13424 (defun gnus-server-server-name ()
13425   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13426     (and server (symbol-name server))))
13427
13428 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13429
13430 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13431
13432 (defun gnus-server-update-server (server)
13433   (save-excursion
13434     (set-buffer gnus-server-buffer)
13435     (let ((buffer-read-only nil)
13436           (info (cdr (assoc server gnus-server-alist))))
13437       (gnus-dribble-enter 
13438        (concat "(gnus-server-set-info \"" server "\" '"
13439                (prin1-to-string info) ")"))
13440       ;; Buffer may be narrowed.
13441       (save-restriction
13442         (widen)
13443         (if (gnus-server-goto-server server)
13444             (delete-region (progn (beginning-of-line) (point))
13445                            (progn (forward-line 1) (point))))
13446         (let ((entry (assoc server gnus-server-alist)))
13447           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13448           (gnus-server-position-cursor))))))
13449
13450 (defun gnus-server-set-info (server info)
13451   ;; Enter a select method into the virtual server alist.
13452   (gnus-dribble-enter 
13453    (concat "(gnus-server-set-info \"" server "\" '"
13454            (prin1-to-string info) ")"))
13455   (let* ((server (nth 1 info))
13456          (entry (assoc server gnus-server-alist)))
13457     (if entry (setcdr entry info)
13458       (setq gnus-server-alist
13459             (nconc gnus-server-alist (list (cons server info)))))))
13460
13461 (defun gnus-server-to-method (server)
13462   ;; Map virtual server names to select methods.
13463   (or (and (equal server "native") gnus-select-method)
13464       (cdr (assoc server gnus-server-alist))))
13465
13466 (defun gnus-server-extend-method (group method)
13467   ;; This function "extends" a virtual server.  If the server is
13468   ;; "hello", and the select method is ("hello" (my-var "something")) 
13469   ;; in the group "alt.alt", this will result in a new virtual server
13470   ;; called "helly+alt.alt".
13471   (let ((entry
13472          (gnus-copy-sequence 
13473           (if (equal (car method) "native") gnus-select-method
13474               (cdr (assoc (car method) gnus-server-alist))))))
13475     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13476     (nconc entry (cdr method))))
13477
13478 (defun gnus-server-get-method (group method)
13479   ;; Input either a server name, and extended server name, or a
13480   ;; select method, and return a select method. 
13481   (cond ((stringp method)
13482          (gnus-server-to-method method))
13483         ((stringp (car method))
13484          (gnus-server-extend-method group method))
13485         (t
13486          (gnus-server-add-address method))))
13487
13488 (defun gnus-server-add-address (method)
13489   (let ((method-name (symbol-name (car method))))
13490     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13491              (not (assq (intern (concat method-name "-address")) method)))
13492         (append method (list (list (intern (concat method-name "-address"))
13493                                    (nth 1 method))))
13494       method)))
13495
13496 (defun gnus-server-equal (s1 s2)
13497   (or (equal s1 s2)
13498       (and (= (length s1) (length s2))
13499            (progn
13500              (while (and s1 (member (car s1) s2))
13501                (setq s1 (cdr s1)))
13502              (null s1)))))
13503
13504 ;;; Interactive server functions.
13505
13506 (defun gnus-server-kill-server (server)
13507   "Kill the server on the current line."
13508   (interactive (list (gnus-server-server-name)))
13509   (or (gnus-server-goto-server server)
13510       (if server (error "No such server: %s" server)
13511         (error "No server on the current line")))
13512   (let ((buffer-read-only nil))
13513     (delete-region (progn (beginning-of-line) (point))
13514                    (progn (forward-line 1) (point))))
13515   (setq gnus-server-killed-servers 
13516         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13517   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13518                                 gnus-server-alist))
13519   (gnus-server-position-cursor))
13520
13521 (defun gnus-server-yank-server ()
13522   "Yank the previously killed server."
13523   (interactive)
13524   (or gnus-server-killed-servers
13525       (error "No killed servers to be yanked"))
13526   (let ((alist gnus-server-alist)
13527         (server (gnus-server-server-name))
13528         (killed (car gnus-server-killed-servers)))
13529     (if (not server) 
13530         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13531       (if (string= server (car (car gnus-server-alist)))
13532           (setq gnus-server-alist (cons killed gnus-server-alist))
13533         (while (and (cdr alist)
13534                     (not (string= server (car (car (cdr alist))))))
13535           (setq alist (cdr alist)))
13536         (setcdr alist (cons killed (cdr alist)))))
13537     (gnus-server-update-server (car killed))
13538     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13539     (gnus-server-position-cursor)))
13540
13541 (defun gnus-server-exit ()
13542   "Return to the group buffer."
13543   (interactive)
13544   (kill-buffer (current-buffer))
13545   (switch-to-buffer gnus-group-buffer))
13546
13547 (defun gnus-server-list-servers ()
13548   "List all available servers."
13549   (interactive)
13550   (let ((cur (gnus-server-server-name)))
13551     (gnus-server-prepare)
13552     (if cur (gnus-server-goto-server cur)
13553       (goto-char (point-max))
13554       (forward-line -1))
13555     (gnus-server-position-cursor)))
13556
13557 (defun gnus-server-copy-server (from to)
13558   (interactive
13559    (list
13560     (or (gnus-server-server-name)
13561         (error "No server on the current line"))
13562     (read-string "Copy to: ")))
13563   (or from (error "No server on current line"))
13564   (or (and to (not (string= to ""))) (error "No name to copy to"))
13565   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13566   (or (assoc from gnus-server-alist) 
13567       (error "%s: no such server" from))
13568   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13569     (setcar to-entry to)
13570     (setcar (nthcdr 2 to-entry) to)
13571     (setq gnus-server-killed-servers 
13572           (cons to-entry gnus-server-killed-servers))
13573     (gnus-server-yank-server)))
13574
13575 (defun gnus-server-add-server (how where)
13576   (interactive 
13577    (list (intern (completing-read "Server method: "
13578                                   gnus-valid-select-methods nil t))
13579          (read-string "Server name: ")))
13580   (setq gnus-server-killed-servers 
13581         (cons (list where how where) gnus-server-killed-servers))
13582   (gnus-server-yank-server))
13583
13584 (defun gnus-server-goto-server (server)
13585   "Jump to a server line."
13586   (interactive
13587    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13588   (let ((to (text-property-any (point-min) (point-max) 
13589                                'gnus-server (intern server))))
13590     (and to
13591          (progn
13592            (goto-char to) 
13593            (gnus-server-position-cursor)))))
13594
13595 (defun gnus-server-edit-server (server)
13596   "Edit the server on the current line."
13597   (interactive (list (gnus-server-server-name)))
13598   (or server
13599       (error "No server on current line"))
13600   (let ((winconf (current-window-configuration)))
13601     (get-buffer-create gnus-server-edit-buffer)
13602     (gnus-configure-windows 'edit-server)
13603     (gnus-add-current-to-buffer-list)
13604     (emacs-lisp-mode)
13605     (make-local-variable 'gnus-prev-winconf)
13606     (setq gnus-prev-winconf winconf)
13607     (use-local-map (copy-keymap (current-local-map)))
13608     (let ((done-func '(lambda () 
13609                         "Exit editing mode and update the information."
13610                         (interactive)
13611                         (gnus-server-edit-server-done 'group))))
13612       (setcar (cdr (nth 4 done-func)) server)
13613       (local-set-key "\C-c\C-c" done-func))
13614     (erase-buffer)
13615     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13616     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13617
13618 (defun gnus-server-edit-server-done (server)
13619   (interactive)
13620   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13621   (goto-char (point-min))
13622   (let ((form (read (current-buffer)))
13623         (winconf gnus-prev-winconf))
13624     (gnus-server-set-info server form)
13625     (kill-buffer (current-buffer))
13626     (and winconf (set-window-configuration winconf))
13627     (set-buffer gnus-server-buffer)
13628     (gnus-server-update-server (gnus-server-server-name))
13629     (gnus-server-position-cursor)))
13630
13631 (defun gnus-server-read-server (server)
13632   "Browse a server."
13633   (interactive (list (gnus-server-server-name)))
13634   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13635
13636 (defun gnus-mouse-pick-server (e)
13637   (interactive "e")
13638   (mouse-set-point e)
13639   (gnus-server-read-server (gnus-server-server-name)))
13640
13641 ;;;
13642 ;;; entry points into gnus-score.el
13643 ;;;
13644
13645 ;;; Finding score files. 
13646
13647 (defvar gnus-global-score-files nil
13648   "*List of global score files and directories.
13649 Set this variable if you want to use people's score files.  One entry
13650 for each score file or each score file directory.  Gnus will decide
13651 by itself what score files are applicable to which group.
13652
13653 Say you want to use the single score file
13654 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13655 score files in the \"/ftp.some-where:/pub/score\" directory.
13656
13657  (setq gnus-global-score-files
13658        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13659          \"/ftp.some-where:/pub/score\"))")
13660
13661 (defun gnus-score-score-files (group)
13662   "Return a list of all possible score files."
13663   ;; Search and set any global score files.
13664   (and gnus-global-score-files 
13665        (or gnus-internal-global-score-files
13666            (gnus-score-search-global-directories gnus-global-score-files)))
13667   ;; Fix the kill-file dir variable.
13668   (setq gnus-kill-files-directory 
13669         (file-name-as-directory
13670          (or gnus-kill-files-directory "~/News/")))
13671   ;; If we can't read it, there are no score files.
13672   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13673       (setq gnus-score-file-list nil)
13674     (if (gnus-use-long-file-name 'not-score)
13675         ;; We want long file names.
13676         (if (or (not gnus-score-file-list)
13677                 (not (car gnus-score-file-list))
13678                 (gnus-file-newer-than gnus-kill-files-directory
13679                                       (car gnus-score-file-list)))
13680               (setq gnus-score-file-list 
13681                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13682                           (nreverse 
13683                            (directory-files 
13684                             gnus-kill-files-directory t 
13685                             (gnus-score-file-regexp))))))
13686       ;; We do not use long file names, so we have to do some
13687       ;; directory traversing.  
13688       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13689             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13690             dir files suffix)
13691         (while suffixes
13692           (setq dir (expand-file-name
13693                      (concat gnus-kill-files-directory
13694                              (gnus-replace-chars-in-string group ?. ?/))))
13695           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13696           (setq suffix (car suffixes)
13697                 suffixes (cdr suffixes))
13698           (if (file-exists-p (concat dir "/" suffix))
13699               (setq files (cons (concat dir "/" suffix) files)))
13700           (while (>= (1+ (length dir)) mdir)
13701             (and (file-exists-p (concat dir "/all/" suffix))
13702                  (setq files (cons (concat dir "/all/" suffix) files)))
13703             (string-match "/[^/]*$" dir)
13704             (setq dir (substring dir 0 (match-beginning 0)))))
13705         (setq gnus-score-file-list 
13706               (cons nil (nreverse files)))))
13707     (cdr gnus-score-file-list)))
13708
13709 (defun gnus-score-file-regexp ()
13710   (concat "\\(" gnus-score-file-suffix 
13711           "\\|" gnus-adaptive-file-suffix "\\)$"))
13712         
13713 (defun gnus-score-find-bnews (group)
13714   "Return a list of score files for GROUP.
13715 The score files are those files in the ~/News directory which matches
13716 GROUP using BNews sys file syntax."
13717   (let* ((sfiles (append (gnus-score-score-files group)
13718                          gnus-internal-global-score-files))
13719          (kill-dir (file-name-as-directory 
13720                     (expand-file-name gnus-kill-files-directory)))
13721          (klen (length kill-dir))
13722          ofiles not-match regexp)
13723     (save-excursion
13724       (set-buffer (get-buffer-create "*gnus score files*"))
13725       (buffer-disable-undo (current-buffer))
13726       ;; Go through all score file names and create regexp with them
13727       ;; as the source.  
13728       (while sfiles
13729         (erase-buffer)
13730         (insert (car sfiles))
13731         (goto-char (point-min))
13732         ;; First remove the suffix itself.
13733         (re-search-forward (concat "." (gnus-score-file-regexp)))
13734         (replace-match "" t t) 
13735         (goto-char (point-min))
13736         (if (looking-at (regexp-quote kill-dir))
13737             ;; If the file name was just "SCORE", `klen' is one character
13738             ;; too much.
13739             (delete-char (min (1- (point-max)) klen))
13740           (goto-char (point-max))
13741           (search-backward "/")
13742           (delete-region (1+ (point)) (point-min)))
13743         ;; If short file names were used, we have to translate slashes.
13744         (goto-char (point-min))
13745         (while (re-search-forward "[/:]" nil t)
13746           (replace-match "." t t))
13747         ;; Translate "all" to ".*".
13748         (while (search-forward "all" nil t)
13749           (replace-match ".*" t t))
13750         (goto-char (point-min))
13751         ;; Deal with "not."s.
13752         (if (looking-at "not.")
13753             (progn
13754               (setq not-match t)
13755               (setq regexp (buffer-substring 5 (point-max))))
13756           (setq regexp (buffer-substring 1 (point-max)))
13757           (setq not-match nil))
13758         ;; Finally - if this resulting regexp matches the group name,
13759         ;; we add this score file to the list of score files
13760         ;; applicable to this group.
13761         (if (or (and not-match
13762                      (not (string-match regexp group)))
13763                 (and (not not-match)
13764                      (string-match regexp group)))
13765             (setq ofiles (cons (car sfiles) ofiles)))
13766         (setq sfiles (cdr sfiles)))
13767       (kill-buffer (current-buffer))
13768       ;; Slight kludge here - the last score file returned should be
13769       ;; the local score file, whether it exists or not. This is so
13770       ;; that any score commands the user enters will go to the right
13771       ;; file, and not end up in some global score file.
13772       (let ((localscore
13773              (expand-file-name
13774               (if (gnus-use-long-file-name 'not-score)
13775                   (concat gnus-kill-files-directory group "." 
13776                           gnus-score-file-suffix)
13777                 (concat gnus-kill-files-directory
13778                         (gnus-replace-chars-in-string group ?. ?/)
13779                         "/" gnus-score-file-suffix)))))
13780         (and (member localscore ofiles)
13781              (delete localscore ofiles))
13782         (setq ofiles (cons localscore ofiles)))
13783       (nreverse ofiles))))
13784
13785 (defun gnus-score-find-single (group)
13786   "Return list containing the score file for GROUP."
13787   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13788         (gnus-score-file-name group)))
13789
13790 (defun gnus-score-find-hierarchical (group)
13791   "Return list of score files for GROUP.
13792 This includes the score file for the group and all its parents."
13793   (let ((all (copy-sequence '(nil)))
13794         (start 0))
13795     (while (string-match "\\." group (1+ start))
13796       (setq start (match-beginning 0))
13797       (setq all (cons (substring group 0 start) all)))
13798     (setq all (cons group all))
13799     (nconc
13800      (mapcar (lambda (newsgroup)
13801                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13802              (setq all (nreverse all)))
13803      (mapcar 'gnus-score-file-name all))))
13804
13805 (defvar gnus-score-file-alist-cache nil)
13806
13807 (defun gnus-score-find-alist (group)
13808   "Return list of score files for GROUP.
13809 The list is determined from the variable gnus-score-file-alist."
13810   (let ((alist gnus-score-file-multiple-match-alist)
13811         score-files)
13812     ;; if this group has been seen before, return the cached entry
13813     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13814         (cdr score-files)      ; ensures caching of groups with no matches
13815       ;; handle the multiple match alist
13816       (while alist
13817         (and (string-match (car (car alist)) group)
13818              (setq score-files
13819                    (nconc score-files (copy-sequence (cdr (car alist))))))
13820         (setq alist (cdr alist)))
13821       (setq alist gnus-score-file-single-match-alist)
13822       ;; handle the single match alist
13823       (while alist
13824         (and (string-match (car (car alist)) group)
13825              ;; progn used just in case ("regexp") has no files
13826              ;; and score-files is still nil. -sj
13827              ;; this can be construed as a "stop searching here" feature :>
13828              ;; and used to simplify regexps in the single-alist 
13829              (progn
13830                (setq score-files
13831                      (nconc score-files (copy-sequence (cdr (car alist)))))
13832                (setq alist nil)))
13833         (setq alist (cdr alist)))
13834       ;; cache the score files
13835       (setq gnus-score-file-alist-cache
13836             (cons (cons group score-files) gnus-score-file-alist-cache))
13837       score-files)))
13838
13839
13840 (defun gnus-possibly-score-headers (&optional trace)
13841   (let ((func gnus-score-find-score-files-function)
13842         score-files)
13843     (and func (not (listp func))
13844          (setq func (list func)))
13845     ;; Go through all the functions for finding score files (or actual
13846     ;; scores) and add them to a list.
13847     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13848     (while func
13849       (and (symbolp (car func))
13850            (fboundp (car func))
13851            (setq score-files 
13852                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13853       (setq func (cdr func)))
13854     (if score-files (gnus-score-headers score-files trace))))
13855
13856 (defun gnus-score-file-name (newsgroup &optional suffix)
13857   "Return the name of a score file for NEWSGROUP."
13858   (let ((suffix (or suffix gnus-score-file-suffix)))
13859     (cond  ((or (null newsgroup)
13860                 (string-equal newsgroup ""))
13861             ;; The global score file is placed at top of the directory.
13862             (expand-file-name 
13863              suffix (or gnus-kill-files-directory "~/News")))
13864            ((gnus-use-long-file-name 'not-score)
13865             ;; Append ".SCORE" to newsgroup name.
13866             (expand-file-name (concat newsgroup "." suffix)
13867                               (or gnus-kill-files-directory "~/News")))
13868            (t
13869             ;; Place "SCORE" under the hierarchical directory.
13870             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13871                                       "/" suffix)
13872                               (or gnus-kill-files-directory "~/News"))))))
13873
13874 (defun gnus-score-search-global-directories (files)
13875   "Scan all global score directories for score files."
13876   ;; Set the variable `gnus-internal-global-score-files' to all
13877   ;; available global score files.
13878   (interactive (list gnus-global-score-files))
13879   (let (out)
13880     (while files
13881       (if (string-match "/$" (car files))
13882           (setq out (nconc (directory-files 
13883                             (car files) t
13884                             (concat (gnus-score-file-regexp) "$"))))
13885         (setq out (cons (car files) out)))
13886       (setq files (cdr files)))
13887     (setq gnus-internal-global-score-files out)))
13888
13889 ;; Allow redefinition of Gnus functions.
13890
13891 (gnus-ems-redefine)
13892
13893 (provide 'gnus)
13894
13895 ;;; gnus.el ends here